BackendServiceTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  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\Files_External\Tests\Service;
  23. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  24. use OCA\Files_External\Lib\Config\IBackendProvider;
  25. use \OCA\Files_External\Service\BackendService;
  26. use OCP\IConfig;
  27. use OCP\IL10N;
  28. class BackendServiceTest extends \Test\TestCase {
  29. /** @var \OCP\IConfig */
  30. protected $config;
  31. /** @var \OCP\IL10N */
  32. protected $l10n;
  33. protected function setUp() {
  34. $this->config = $this->createMock(IConfig::class);
  35. $this->l10n = $this->createMock(IL10N::class);
  36. }
  37. /**
  38. * @param string $class
  39. *
  40. * @return \OCA\Files_External\Lib\Backend\Backend
  41. */
  42. protected function getBackendMock($class) {
  43. $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
  47. $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
  48. return $backend;
  49. }
  50. /**
  51. * @param string $class
  52. *
  53. * @return \OCA\Files_External\Lib\Auth\AuthMechanism
  54. */
  55. protected function getAuthMechanismMock($class) {
  56. $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism')
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
  60. $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
  61. return $backend;
  62. }
  63. public function testRegisterBackend() {
  64. $service = new BackendService($this->config, $this->l10n);
  65. $backend = $this->getBackendMock('\Foo\Bar');
  66. $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $backendAlias->method('getIdentifierAliases')
  70. ->willReturn(['identifier_real', 'identifier_alias']);
  71. $backendAlias->method('getIdentifier')
  72. ->willReturn('identifier_real');
  73. $service->registerBackend($backend);
  74. $service->registerBackend($backendAlias);
  75. $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
  76. $this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
  77. $this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
  78. $backends = $service->getBackends();
  79. $this->assertCount(2, $backends);
  80. $this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
  81. $this->assertArrayHasKey('identifier_real', $backends);
  82. $this->assertArrayNotHasKey('identifier_alias', $backends);
  83. }
  84. public function testBackendProvider() {
  85. $service = new BackendService($this->config, $this->l10n);
  86. $backend1 = $this->getBackendMock('\Foo\Bar');
  87. $backend2 = $this->getBackendMock('\Bar\Foo');
  88. $providerMock = $this->createMock(IBackendProvider::class);
  89. $providerMock->expects($this->once())
  90. ->method('getBackends')
  91. ->willReturn([$backend1, $backend2]);
  92. $service->registerBackendProvider($providerMock);
  93. $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
  94. $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
  95. $this->assertCount(2, $service->getBackends());
  96. }
  97. public function testAuthMechanismProvider() {
  98. $service = new BackendService($this->config, $this->l10n);
  99. $backend1 = $this->getAuthMechanismMock('\Foo\Bar');
  100. $backend2 = $this->getAuthMechanismMock('\Bar\Foo');
  101. $providerMock = $this->createMock(IAuthMechanismProvider::class);
  102. $providerMock->expects($this->once())
  103. ->method('getAuthMechanisms')
  104. ->willReturn([$backend1, $backend2]);
  105. $service->registerAuthMechanismProvider($providerMock);
  106. $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
  107. $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
  108. $this->assertCount(2, $service->getAuthMechanisms());
  109. }
  110. public function testMultipleBackendProviders() {
  111. $service = new BackendService($this->config, $this->l10n);
  112. $backend1a = $this->getBackendMock('\Foo\Bar');
  113. $backend1b = $this->getBackendMock('\Bar\Foo');
  114. $backend2 = $this->getBackendMock('\Dead\Beef');
  115. $provider1Mock = $this->createMock(IBackendProvider::class);
  116. $provider1Mock->expects($this->once())
  117. ->method('getBackends')
  118. ->willReturn([$backend1a, $backend1b]);
  119. $service->registerBackendProvider($provider1Mock);
  120. $provider2Mock = $this->createMock(IBackendProvider::class);
  121. $provider2Mock->expects($this->once())
  122. ->method('getBackends')
  123. ->willReturn([$backend2]);
  124. $service->registerBackendProvider($provider2Mock);
  125. $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
  126. $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
  127. $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
  128. $this->assertCount(3, $service->getBackends());
  129. }
  130. public function testUserMountingBackends() {
  131. $this->config->expects($this->exactly(2))
  132. ->method('getAppValue')
  133. ->will($this->returnValueMap([
  134. ['files_external', 'allow_user_mounting', 'yes', 'yes'],
  135. ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
  136. ]));
  137. $service = new BackendService($this->config, $this->l10n);
  138. $backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
  139. $backendAllowed->expects($this->never())
  140. ->method('removeVisibility');
  141. $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
  142. $backendNotAllowed->expects($this->once())
  143. ->method('removeVisibility')
  144. ->with(BackendService::VISIBILITY_PERSONAL);
  145. $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $backendAlias->method('getIdentifierAliases')
  149. ->willReturn(['identifier_real', 'identifier_alias']);
  150. $backendAlias->expects($this->never())
  151. ->method('removeVisibility');
  152. $service->registerBackend($backendAllowed);
  153. $service->registerBackend($backendNotAllowed);
  154. $service->registerBackend($backendAlias);
  155. }
  156. public function testGetAvailableBackends() {
  157. $service = new BackendService($this->config, $this->l10n);
  158. $backendAvailable = $this->getBackendMock('\Backend\Available');
  159. $backendAvailable->expects($this->once())
  160. ->method('checkDependencies')
  161. ->will($this->returnValue([]));
  162. $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
  163. $backendNotAvailable->expects($this->once())
  164. ->method('checkDependencies')
  165. ->will($this->returnValue([
  166. $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
  167. ->disableOriginalConstructor()
  168. ->getMock()
  169. ]));
  170. $service->registerBackend($backendAvailable);
  171. $service->registerBackend($backendNotAvailable);
  172. $availableBackends = $service->getAvailableBackends();
  173. $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
  174. $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
  175. }
  176. }