SettingsControllerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Tests\Controller;
  8. use OCA\Federation\Controller\SettingsController;
  9. use OCA\Federation\TrustedServers;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\HintException;
  12. use OCP\IL10N;
  13. use OCP\IRequest;
  14. use Test\TestCase;
  15. class SettingsControllerTest extends TestCase {
  16. private SettingsController $controller;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
  18. private $request;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject|IL10N */
  20. private $l10n;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
  22. private $trustedServers;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  26. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  27. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  28. ->disableOriginalConstructor()->getMock();
  29. $this->controller = new SettingsController(
  30. 'SettingsControllerTest',
  31. $this->request,
  32. $this->l10n,
  33. $this->trustedServers
  34. );
  35. }
  36. public function testAddServer(): void {
  37. $this->trustedServers
  38. ->expects($this->once())
  39. ->method('isTrustedServer')
  40. ->with('url')
  41. ->willReturn(false);
  42. $this->trustedServers
  43. ->expects($this->once())
  44. ->method('isNextcloudServer')
  45. ->with('url')
  46. ->willReturn(true);
  47. $result = $this->controller->addServer('url');
  48. $this->assertTrue($result instanceof DataResponse);
  49. $data = $result->getData();
  50. $this->assertSame(200, $result->getStatus());
  51. $this->assertSame('url', $data['url']);
  52. $this->assertArrayHasKey('id', $data);
  53. }
  54. /**
  55. * @dataProvider checkServerFails
  56. */
  57. public function testAddServerFail(bool $isTrustedServer, bool $isNextcloud): void {
  58. $this->expectException(HintException::class);
  59. $this->trustedServers
  60. ->expects($this->any())
  61. ->method('isTrustedServer')
  62. ->with('url')
  63. ->willReturn($isTrustedServer);
  64. $this->trustedServers
  65. ->expects($this->any())
  66. ->method('isNextcloudServer')
  67. ->with('url')
  68. ->willReturn($isNextcloud);
  69. $this->controller->addServer('url');
  70. }
  71. public function testRemoveServer(): void {
  72. $this->trustedServers->expects($this->once())
  73. ->method('removeServer')
  74. ->with(1);
  75. $result = $this->controller->removeServer(1);
  76. $this->assertTrue($result instanceof DataResponse);
  77. $this->assertSame(200, $result->getStatus());
  78. }
  79. public function testCheckServer(): void {
  80. $this->trustedServers
  81. ->expects($this->once())
  82. ->method('isTrustedServer')
  83. ->with('url')
  84. ->willReturn(false);
  85. $this->trustedServers
  86. ->expects($this->once())
  87. ->method('isNextcloudServer')
  88. ->with('url')
  89. ->willReturn(true);
  90. $this->assertTrue(
  91. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  92. );
  93. }
  94. /**
  95. * @dataProvider checkServerFails
  96. */
  97. public function testCheckServerFail(bool $isTrustedServer, bool $isNextcloud): void {
  98. $this->expectException(HintException::class);
  99. $this->trustedServers
  100. ->expects($this->any())
  101. ->method('isTrustedServer')
  102. ->with('url')
  103. ->willReturn($isTrustedServer);
  104. $this->trustedServers
  105. ->expects($this->any())
  106. ->method('isNextcloudServer')
  107. ->with('url')
  108. ->willReturn($isNextcloud);
  109. $this->assertTrue(
  110. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  111. );
  112. }
  113. /**
  114. * Data to simulate checkServer fails
  115. */
  116. public function checkServerFails(): array {
  117. return [
  118. [true, true],
  119. [false, false]
  120. ];
  121. }
  122. }