SettingsControllerTest.php 4.3 KB

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