1
0

SettingsControllerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 OCA\Federation\TrustedServers;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. use Test\TestCase;
  29. class SettingsControllerTest extends TestCase {
  30. /** @var SettingsController */
  31. private $controller;
  32. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IRequest */
  33. private $request;
  34. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IL10N */
  35. private $l10n;
  36. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCA\Federation\TrustedServers */
  37. private $trustedServers;
  38. public function setUp() {
  39. parent::setUp();
  40. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  41. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  42. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  43. ->disableOriginalConstructor()->getMock();
  44. $this->controller = new SettingsController(
  45. 'SettingsControllerTest',
  46. $this->request,
  47. $this->l10n,
  48. $this->trustedServers
  49. );
  50. }
  51. public function testAddServer() {
  52. $this->trustedServers
  53. ->expects($this->once())
  54. ->method('isTrustedServer')
  55. ->with('url')
  56. ->willReturn(false);
  57. $this->trustedServers
  58. ->expects($this->once())
  59. ->method('isOwnCloudServer')
  60. ->with('url')
  61. ->willReturn(true);
  62. $result = $this->controller->addServer('url');
  63. $this->assertTrue($result instanceof DataResponse);
  64. $data = $result->getData();
  65. $this->assertSame(200, $result->getStatus());
  66. $this->assertSame('url', $data['url']);
  67. $this->assertArrayHasKey('id', $data);
  68. }
  69. /**
  70. * @dataProvider checkServerFails
  71. * @expectedException \OC\HintException
  72. *
  73. * @param bool $isTrustedServer
  74. * @param bool $isOwnCloud
  75. */
  76. public function testAddServerFail($isTrustedServer, $isOwnCloud) {
  77. $this->trustedServers
  78. ->expects($this->any())
  79. ->method('isTrustedServer')
  80. ->with('url')
  81. ->willReturn($isTrustedServer);
  82. $this->trustedServers
  83. ->expects($this->any())
  84. ->method('isOwnCloudServer')
  85. ->with('url')
  86. ->willReturn($isOwnCloud);
  87. $this->controller->addServer('url');
  88. }
  89. public function testRemoveServer() {
  90. $this->trustedServers->expects($this->once())->method('removeServer')
  91. ->with('url');
  92. $result = $this->controller->removeServer('url');
  93. $this->assertTrue($result instanceof DataResponse);
  94. $this->assertSame(200, $result->getStatus());
  95. }
  96. public function testCheckServer() {
  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('isOwnCloudServer')
  105. ->with('url')
  106. ->willReturn(true);
  107. $this->assertTrue(
  108. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  109. );
  110. }
  111. /**
  112. * @dataProvider checkServerFails
  113. * @expectedException \OC\HintException
  114. *
  115. * @param bool $isTrustedServer
  116. * @param bool $isOwnCloud
  117. */
  118. public function testCheckServerFail($isTrustedServer, $isOwnCloud) {
  119. $this->trustedServers
  120. ->expects($this->any())
  121. ->method('isTrustedServer')
  122. ->with('url')
  123. ->willReturn($isTrustedServer);
  124. $this->trustedServers
  125. ->expects($this->any())
  126. ->method('isOwnCloudServer')
  127. ->with('url')
  128. ->willReturn($isOwnCloud);
  129. $this->assertTrue(
  130. $this->invokePrivate($this->controller, 'checkServer', ['url'])
  131. );
  132. }
  133. /**
  134. * data to simulate checkServer fails
  135. *
  136. * @return array
  137. */
  138. public function checkServerFails() {
  139. return [
  140. [true, true],
  141. [false, false]
  142. ];
  143. }
  144. }