OcxProvicersTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\OcxProviders;
  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 OcxProvicersTest 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 OcxProviders|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(OcxProviders::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. public function testSuccess(): void {
  51. $response = $this->createMock(IResponse::class);
  52. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  53. $this->setupcheck
  54. ->expects($this->exactly(2))
  55. ->method('runRequest')
  56. ->willReturnOnConsecutiveCalls($this->generate([$response]), $this->generate([$response]));
  57. $result = $this->setupcheck->run();
  58. $this->assertEquals(SetupResult::SUCCESS, $result->getSeverity());
  59. }
  60. public function testLateSuccess(): void {
  61. $response1 = $this->createMock(IResponse::class);
  62. $response1->expects($this->exactly(3))->method('getStatusCode')->willReturnOnConsecutiveCalls(404, 500, 200);
  63. $response2 = $this->createMock(IResponse::class);
  64. $response2->expects($this->any())->method('getStatusCode')->willReturnOnConsecutiveCalls(200);
  65. $this->setupcheck
  66. ->expects($this->exactly(2))
  67. ->method('runRequest')
  68. ->willReturnOnConsecutiveCalls($this->generate([$response1, $response1, $response1]), $this->generate([$response2])); // only one response out of two
  69. $result = $this->setupcheck->run();
  70. $this->assertEquals(SetupResult::SUCCESS, $result->getSeverity());
  71. }
  72. public function testNoResponse(): void {
  73. $response = $this->createMock(IResponse::class);
  74. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  75. $this->setupcheck
  76. ->expects($this->exactly(2))
  77. ->method('runRequest')
  78. ->willReturnOnConsecutiveCalls($this->generate([]), $this->generate([])); // No responses
  79. $result = $this->setupcheck->run();
  80. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  81. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  82. }
  83. public function testPartialResponse(): void {
  84. $response = $this->createMock(IResponse::class);
  85. $response->expects($this->any())->method('getStatusCode')->willReturn(200);
  86. $this->setupcheck
  87. ->expects($this->exactly(2))
  88. ->method('runRequest')
  89. ->willReturnOnConsecutiveCalls($this->generate([$response]), $this->generate([])); // only one response out of two
  90. $result = $this->setupcheck->run();
  91. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  92. $this->assertMatchesRegularExpression('/^Could not check/', $result->getDescription());
  93. }
  94. public function testInvalidResponse(): void {
  95. $response = $this->createMock(IResponse::class);
  96. $response->expects($this->any())->method('getStatusCode')->willReturn(404);
  97. $this->setupcheck
  98. ->expects($this->exactly(2))
  99. ->method('runRequest')
  100. ->willReturnOnConsecutiveCalls($this->generate([$response]), $this->generate([$response])); // only one response out of two
  101. $result = $this->setupcheck->run();
  102. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  103. $this->assertMatchesRegularExpression('/^Your web server is not properly set up/', $result->getDescription());
  104. }
  105. public function testPartialInvalidResponse(): void {
  106. $response1 = $this->createMock(IResponse::class);
  107. $response1->expects($this->any())->method('getStatusCode')->willReturnOnConsecutiveCalls(200);
  108. $response2 = $this->createMock(IResponse::class);
  109. $response2->expects($this->any())->method('getStatusCode')->willReturnOnConsecutiveCalls(404);
  110. $this->setupcheck
  111. ->expects($this->exactly(2))
  112. ->method('runRequest')
  113. ->willReturnOnConsecutiveCalls($this->generate([$response1]), $this->generate([$response2]));
  114. $result = $this->setupcheck->run();
  115. $this->assertEquals(SetupResult::WARNING, $result->getSeverity());
  116. $this->assertMatchesRegularExpression('/^Your web server is not properly set up/', $result->getDescription());
  117. }
  118. /**
  119. * Helper function creates a nicer interface for mocking Generator behavior
  120. */
  121. protected function generate(array $yield_values) {
  122. return $this->returnCallback(function () use ($yield_values) {
  123. yield from $yield_values;
  124. });
  125. }
  126. }