SettingsControllerTest.php 4.1 KB

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