DataDirectoryProtectedTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(['runHEAD'])
  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): void {
  54. $responses = array_map(function ($state) {
  55. $response = $this->createMock(IResponse::class);
  56. $response->expects($this->any())->method('getStatusCode')->willReturn($state);
  57. return $response;
  58. }, $status);
  59. $this->setupcheck
  60. ->expects($this->once())
  61. ->method('runHEAD')
  62. ->will($this->generate($responses));
  63. $this->config
  64. ->expects($this->once())
  65. ->method('getSystemValue')
  66. ->willReturn('');
  67. $result = $this->setupcheck->run();
  68. $this->assertEquals($expected, $result->getSeverity());
  69. }
  70. public function dataTestStatusCode(): array {
  71. return [
  72. 'success: forbidden access' => [[403], SetupResult::SUCCESS],
  73. 'error: can access' => [[200], SetupResult::ERROR],
  74. 'error: one forbidden one can access' => [[403, 200], SetupResult::ERROR],
  75. 'warning: connection issue' => [[], SetupResult::WARNING],
  76. ];
  77. }
  78. public function testNoResponse(): void {
  79. $response = $this->createMock(IResponse::class);
  80. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  81. $this->setupcheck
  82. ->expects($this->once())
  83. ->method('runHEAD')
  84. ->will($this->generate([]));
  85. $this->config
  86. ->expects($this->once())
  87. ->method('getSystemValue')
  88. ->willReturn('');
  89. $result = $this->setupcheck->run();
  90. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  91. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  92. }
  93. /**
  94. * Helper function creates a nicer interface for mocking Generator behavior
  95. */
  96. protected function generate(array $yield_values) {
  97. return $this->returnCallback(function () use ($yield_values) {
  98. yield from $yield_values;
  99. });
  100. }
  101. }