1
0

SettingsControllerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Carl Schwan <carl@carlschwan.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Federation\Tests\Controller;
  26. use OCA\Federation\Controller\SettingsController;
  27. use OCA\Federation\TrustedServers;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\IL10N;
  30. use OCP\IRequest;
  31. use Test\TestCase;
  32. class SettingsControllerTest extends TestCase {
  33. private SettingsController $controller;
  34. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IRequest */
  35. private $request;
  36. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
  37. private $l10n;
  38. /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Federation\TrustedServers */
  39. private $trustedServers;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  43. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  44. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  45. ->disableOriginalConstructor()->getMock();
  46. $this->controller = new SettingsController(
  47. 'SettingsControllerTest',
  48. $this->request,
  49. $this->l10n,
  50. $this->trustedServers
  51. );
  52. }
  53. public function testAddServer(): void {
  54. $this->trustedServers
  55. ->expects($this->once())
  56. ->method('isTrustedServer')
  57. ->with('url')
  58. ->willReturn(false);
  59. $this->trustedServers
  60. ->expects($this->once())
  61. ->method('isNextcloudServer')
  62. ->with('url')
  63. ->willReturn(true);
  64. $result = $this->controller->addServer('url');
  65. $this->assertTrue($result instanceof DataResponse);
  66. $data = $result->getData();
  67. $this->assertSame(200, $result->getStatus());
  68. $this->assertSame('url', $data['url']);
  69. $this->assertArrayHasKey('id', $data);
  70. }
  71. /**
  72. * @dataProvider checkServerFails
  73. */
  74. public function testAddServerFail(bool $isTrustedServer, bool $isNextcloud): void {
  75. $this->expectException(\OCP\HintException::class);
  76. $this->trustedServers
  77. ->expects($this->any())
  78. ->method('isTrustedServer')
  79. ->with('url')
  80. ->willReturn($isTrustedServer);
  81. $this->trustedServers
  82. ->expects($this->any())
  83. ->method('isNextcloudServer')
  84. ->with('url')
  85. ->willReturn($isNextcloud);
  86. $this->controller->addServer('url');
  87. }
  88. public function testRemoveServer(): void {
  89. $this->trustedServers->expects($this->once())
  90. ->method('removeServer')
  91. ->with(1);
  92. $result = $this->controller->removeServer(1);
  93. $this->assertTrue($result instanceof DataResponse);
  94. $this->assertSame(200, $result->getStatus());
  95. }
  96. public function testCheckServer(): void {
  97. $this->trustedServers
  98. ->expects($this->once())
  99. ->method('isTrustedServer')
  100. ->with('url')
  101. ->willReturn(false);
  102. $this->trustedServers
  103. ->expects($this->once())
  104. ->method('isNextcloudServer')
  105. ->with('url')
  106. ->willReturn(true);
  107. $this->assertTrue(
  108. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  109. );
  110. }
  111. /**
  112. * @dataProvider checkServerFails
  113. */
  114. public function testCheckServerFail(bool $isTrustedServer, bool $isNextcloud): void {
  115. $this->expectException(\OCP\HintException::class);
  116. $this->trustedServers
  117. ->expects($this->any())
  118. ->method('isTrustedServer')
  119. ->with('url')
  120. ->willReturn($isTrustedServer);
  121. $this->trustedServers
  122. ->expects($this->any())
  123. ->method('isNextcloudServer')
  124. ->with('url')
  125. ->willReturn($isNextcloud);
  126. $this->assertTrue(
  127. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  128. );
  129. }
  130. /**
  131. * Data to simulate checkServer fails
  132. */
  133. public function checkServerFails(): array {
  134. return [
  135. [true, true],
  136. [false, false]
  137. ];
  138. }
  139. }