1
0

DataDirectoryProtectedTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Tests;
  25. use OCA\Settings\SetupChecks\DataDirectoryProtected;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\Http\Client\IResponse;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\SetupCheck\SetupResult;
  32. use PHPUnit\Framework\MockObject\MockObject;
  33. use Psr\Log\LoggerInterface;
  34. use Test\TestCase;
  35. class DataDirectoryProtectedTest extends TestCase {
  36. private IL10N|MockObject $l10n;
  37. private IConfig|MockObject $config;
  38. private IURLGenerator|MockObject $urlGenerator;
  39. private IClientService|MockObject $clientService;
  40. private LoggerInterface|MockObject $logger;
  41. private DataDirectoryProtected|MockObject $setupcheck;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. /** @var IL10N|MockObject */
  45. $this->l10n = $this->getMockBuilder(IL10N::class)
  46. ->disableOriginalConstructor()->getMock();
  47. $this->l10n->expects($this->any())
  48. ->method('t')
  49. ->willReturnCallback(function ($message, array $replace) {
  50. return vsprintf($message, $replace);
  51. });
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  54. $this->clientService = $this->createMock(IClientService::class);
  55. $this->logger = $this->createMock(LoggerInterface::class);
  56. $this->setupcheck = $this->getMockBuilder(DataDirectoryProtected::class)
  57. ->onlyMethods(['runHEAD'])
  58. ->setConstructorArgs([
  59. $this->l10n,
  60. $this->config,
  61. $this->urlGenerator,
  62. $this->clientService,
  63. $this->logger,
  64. ])
  65. ->getMock();
  66. }
  67. /**
  68. * @dataProvider dataTestStatusCode
  69. */
  70. public function testStatusCode(array $status, string $expected): void {
  71. $responses = array_map(function ($state) {
  72. $response = $this->createMock(IResponse::class);
  73. $response->expects($this->any())->method('getStatusCode')->willReturn($state);
  74. return $response;
  75. }, $status);
  76. $this->setupcheck
  77. ->expects($this->once())
  78. ->method('runHEAD')
  79. ->will($this->generate($responses));
  80. $this->config
  81. ->expects($this->once())
  82. ->method('getSystemValue')
  83. ->willReturn('');
  84. $result = $this->setupcheck->run();
  85. $this->assertEquals($expected, $result->getSeverity());
  86. }
  87. public function dataTestStatusCode(): array {
  88. return [
  89. 'success: forbidden access' => [[403], SetupResult::SUCCESS],
  90. 'error: can access' => [[200], SetupResult::ERROR],
  91. 'error: one forbidden one can access' => [[403, 200], SetupResult::ERROR],
  92. 'warning: connection issue' => [[], SetupResult::WARNING],
  93. ];
  94. }
  95. public function testNoResponse(): void {
  96. $response = $this->createMock(IResponse::class);
  97. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  98. $this->setupcheck
  99. ->expects($this->once())
  100. ->method('runHEAD')
  101. ->will($this->generate([]));
  102. $this->config
  103. ->expects($this->once())
  104. ->method('getSystemValue')
  105. ->willReturn('');
  106. $result = $this->setupcheck->run();
  107. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  108. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  109. }
  110. /**
  111. * Helper function creates a nicer interface for mocking Generator behavior
  112. */
  113. protected function generate(array $yield_values) {
  114. return $this->returnCallback(function () use ($yield_values) {
  115. yield from $yield_values;
  116. });
  117. }
  118. }