1
0

OcxProvicersTest.php 6.1 KB

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