SettingsControllerTest.php 3.6 KB

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