BackendServiceTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Tests\Service;
  25. use OCA\Files_External\Lib\Auth\AuthMechanism;
  26. use OCA\Files_External\Lib\Backend\Backend;
  27. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  28. use OCA\Files_External\Lib\Config\IBackendProvider;
  29. use \OCA\Files_External\Service\BackendService;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. class BackendServiceTest extends \Test\TestCase {
  33. /** @var \OCP\IConfig */
  34. protected $config;
  35. /** @var \OCP\IL10N */
  36. protected $l10n;
  37. protected function setUp() {
  38. $this->config = $this->createMock(IConfig::class);
  39. $this->l10n = $this->createMock(IL10N::class);
  40. }
  41. /**
  42. * @param string $class
  43. *
  44. * @return \OCA\Files_External\Lib\Backend\Backend
  45. */
  46. protected function getBackendMock($class) {
  47. $backend = $this->getMockBuilder(Backend::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
  51. $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
  52. return $backend;
  53. }
  54. /**
  55. * @param string $class
  56. *
  57. * @return \OCA\Files_External\Lib\Auth\AuthMechanism
  58. */
  59. protected function getAuthMechanismMock($class) {
  60. $backend = $this->getMockBuilder(AuthMechanism::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
  64. $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
  65. return $backend;
  66. }
  67. public function testRegisterBackend() {
  68. $service = new BackendService($this->config, $this->l10n);
  69. $backend = $this->getBackendMock('\Foo\Bar');
  70. $backendAlias = $this->getMockBuilder(Backend::class)
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $backendAlias->method('getIdentifierAliases')
  74. ->willReturn(['identifier_real', 'identifier_alias']);
  75. $backendAlias->method('getIdentifier')
  76. ->willReturn('identifier_real');
  77. $service->registerBackend($backend);
  78. $service->registerBackend($backendAlias);
  79. $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
  80. $this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
  81. $this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
  82. $backends = $service->getBackends();
  83. $this->assertCount(2, $backends);
  84. $this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
  85. $this->assertArrayHasKey('identifier_real', $backends);
  86. $this->assertArrayNotHasKey('identifier_alias', $backends);
  87. }
  88. public function testBackendProvider() {
  89. $service = new BackendService($this->config, $this->l10n);
  90. $backend1 = $this->getBackendMock('\Foo\Bar');
  91. $backend2 = $this->getBackendMock('\Bar\Foo');
  92. $providerMock = $this->createMock(IBackendProvider::class);
  93. $providerMock->expects($this->once())
  94. ->method('getBackends')
  95. ->willReturn([$backend1, $backend2]);
  96. $service->registerBackendProvider($providerMock);
  97. $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
  98. $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
  99. $this->assertCount(2, $service->getBackends());
  100. }
  101. public function testAuthMechanismProvider() {
  102. $service = new BackendService($this->config, $this->l10n);
  103. $backend1 = $this->getAuthMechanismMock('\Foo\Bar');
  104. $backend2 = $this->getAuthMechanismMock('\Bar\Foo');
  105. $providerMock = $this->createMock(IAuthMechanismProvider::class);
  106. $providerMock->expects($this->once())
  107. ->method('getAuthMechanisms')
  108. ->willReturn([$backend1, $backend2]);
  109. $service->registerAuthMechanismProvider($providerMock);
  110. $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
  111. $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
  112. $this->assertCount(2, $service->getAuthMechanisms());
  113. }
  114. public function testMultipleBackendProviders() {
  115. $service = new BackendService($this->config, $this->l10n);
  116. $backend1a = $this->getBackendMock('\Foo\Bar');
  117. $backend1b = $this->getBackendMock('\Bar\Foo');
  118. $backend2 = $this->getBackendMock('\Dead\Beef');
  119. $provider1Mock = $this->createMock(IBackendProvider::class);
  120. $provider1Mock->expects($this->once())
  121. ->method('getBackends')
  122. ->willReturn([$backend1a, $backend1b]);
  123. $service->registerBackendProvider($provider1Mock);
  124. $provider2Mock = $this->createMock(IBackendProvider::class);
  125. $provider2Mock->expects($this->once())
  126. ->method('getBackends')
  127. ->willReturn([$backend2]);
  128. $service->registerBackendProvider($provider2Mock);
  129. $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
  130. $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
  131. $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
  132. $this->assertCount(3, $service->getBackends());
  133. }
  134. public function testUserMountingBackends() {
  135. $this->config->expects($this->exactly(2))
  136. ->method('getAppValue')
  137. ->will($this->returnValueMap([
  138. ['files_external', 'allow_user_mounting', 'yes', 'yes'],
  139. ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
  140. ]));
  141. $service = new BackendService($this->config, $this->l10n);
  142. $backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
  143. $backendAllowed->expects($this->never())
  144. ->method('removeVisibility');
  145. $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
  146. $backendNotAllowed->expects($this->once())
  147. ->method('removeVisibility')
  148. ->with(BackendService::VISIBILITY_PERSONAL);
  149. $backendAlias = $this->getMockBuilder(Backend::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $backendAlias->method('getIdentifierAliases')
  153. ->willReturn(['identifier_real', 'identifier_alias']);
  154. $backendAlias->expects($this->never())
  155. ->method('removeVisibility');
  156. $service->registerBackend($backendAllowed);
  157. $service->registerBackend($backendNotAllowed);
  158. $service->registerBackend($backendAlias);
  159. }
  160. public function testGetAvailableBackends() {
  161. $service = new BackendService($this->config, $this->l10n);
  162. $backendAvailable = $this->getBackendMock('\Backend\Available');
  163. $backendAvailable->expects($this->once())
  164. ->method('checkDependencies')
  165. ->will($this->returnValue([]));
  166. $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
  167. $backendNotAvailable->expects($this->once())
  168. ->method('checkDependencies')
  169. ->will($this->returnValue([
  170. $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
  171. ->disableOriginalConstructor()
  172. ->getMock()
  173. ]));
  174. $service->registerBackend($backendAvailable);
  175. $service->registerBackend($backendNotAvailable);
  176. $availableBackends = $service->getAvailableBackends();
  177. $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
  178. $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
  179. }
  180. }