1
0

DataDirectoryProtectedTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests;
  8. use OCA\Settings\SetupChecks\DataDirectoryProtected;
  9. use OCP\Http\Client\IClientService;
  10. use OCP\Http\Client\IResponse;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use OCP\SetupCheck\SetupResult;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. use Psr\Log\LoggerInterface;
  17. use Test\TestCase;
  18. class DataDirectoryProtectedTest extends TestCase {
  19. private IL10N&MockObject $l10n;
  20. private IConfig&MockObject $config;
  21. private IURLGenerator&MockObject $urlGenerator;
  22. private IClientService&MockObject $clientService;
  23. private LoggerInterface&MockObject $logger;
  24. private DataDirectoryProtected&MockObject $setupcheck;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. /** @var IL10N&MockObject */
  28. $this->l10n = $this->getMockBuilder(IL10N::class)
  29. ->disableOriginalConstructor()->getMock();
  30. $this->l10n->expects($this->any())
  31. ->method('t')
  32. ->willReturnCallback(function ($message, array $replace) {
  33. return vsprintf($message, $replace);
  34. });
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  37. $this->clientService = $this->createMock(IClientService::class);
  38. $this->logger = $this->createMock(LoggerInterface::class);
  39. $this->setupcheck = $this->getMockBuilder(DataDirectoryProtected::class)
  40. ->onlyMethods(['runRequest'])
  41. ->setConstructorArgs([
  42. $this->l10n,
  43. $this->config,
  44. $this->urlGenerator,
  45. $this->clientService,
  46. $this->logger,
  47. ])
  48. ->getMock();
  49. }
  50. /**
  51. * @dataProvider dataTestStatusCode
  52. */
  53. public function testStatusCode(array $status, string $expected, bool $hasBody): void {
  54. $responses = array_map(function ($state) use ($hasBody) {
  55. $response = $this->createMock(IResponse::class);
  56. $response->expects($this->any())->method('getStatusCode')->willReturn($state);
  57. $response->expects(($this->atMost(1)))->method('getBody')->willReturn($hasBody ? '# Nextcloud data directory' : 'something else');
  58. return $response;
  59. }, $status);
  60. $this->setupcheck
  61. ->expects($this->once())
  62. ->method('runRequest')
  63. ->will($this->generate($responses));
  64. $this->config
  65. ->expects($this->once())
  66. ->method('getSystemValueString')
  67. ->willReturn('');
  68. $result = $this->setupcheck->run();
  69. $this->assertEquals($expected, $result->getSeverity());
  70. }
  71. public static function dataTestStatusCode(): array {
  72. return [
  73. 'success: forbidden access' => [[403], SetupResult::SUCCESS, true],
  74. 'success: forbidden access with redirect' => [[200], SetupResult::SUCCESS, false],
  75. 'error: can access' => [[200], SetupResult::ERROR, true],
  76. 'error: one forbidden one can access' => [[403, 200], SetupResult::ERROR, true],
  77. 'warning: connection issue' => [[], SetupResult::WARNING, true],
  78. ];
  79. }
  80. public function testNoResponse(): void {
  81. $response = $this->createMock(IResponse::class);
  82. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  83. $this->setupcheck
  84. ->expects($this->once())
  85. ->method('runRequest')
  86. ->will($this->generate([]));
  87. $this->config
  88. ->expects($this->once())
  89. ->method('getSystemValueString')
  90. ->willReturn('');
  91. $result = $this->setupcheck->run();
  92. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  93. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  94. }
  95. /**
  96. * Helper function creates a nicer interface for mocking Generator behavior
  97. */
  98. protected function generate(array $yield_values) {
  99. return $this->returnCallback(function () use ($yield_values) {
  100. yield from $yield_values;
  101. });
  102. }
  103. }