BackendServiceTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Service;
  8. use OCA\Files_External\Config\IConfigHandler;
  9. use OCA\Files_External\Lib\Auth\AuthMechanism;
  10. use OCA\Files_External\Lib\Backend\Backend;
  11. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  12. use OCA\Files_External\Lib\Config\IBackendProvider;
  13. use OCA\Files_External\Service\BackendService;
  14. use OCP\IConfig;
  15. class BackendServiceTest extends \Test\TestCase {
  16. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  17. protected $config;
  18. protected function setUp(): void {
  19. $this->config = $this->createMock(IConfig::class);
  20. }
  21. /**
  22. * @param string $class
  23. *
  24. * @return \OCA\Files_External\Lib\Backend\Backend|\PHPUnit\Framework\MockObject\MockObject
  25. */
  26. protected function getBackendMock($class) {
  27. $backend = $this->getMockBuilder(Backend::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $backend->method('getIdentifier')->willReturn('identifier:'.$class);
  31. $backend->method('getIdentifierAliases')->willReturn(['identifier:'.$class]);
  32. return $backend;
  33. }
  34. /**
  35. * @param string $class
  36. *
  37. * @return \OCA\Files_External\Lib\Auth\AuthMechanism|\PHPUnit\Framework\MockObject\MockObject
  38. */
  39. protected function getAuthMechanismMock($class) {
  40. $backend = $this->getMockBuilder(AuthMechanism::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $backend->method('getIdentifier')->willReturn('identifier:'.$class);
  44. $backend->method('getIdentifierAliases')->willReturn(['identifier:'.$class]);
  45. return $backend;
  46. }
  47. public function testRegisterBackend() {
  48. $service = new BackendService($this->config);
  49. $backend = $this->getBackendMock('\Foo\Bar');
  50. /** @var \OCA\Files_External\Lib\Backend\Backend|\PHPUnit\Framework\MockObject\MockObject $backendAlias */
  51. $backendAlias = $this->getMockBuilder(Backend::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $backendAlias->method('getIdentifierAliases')
  55. ->willReturn(['identifier_real', 'identifier_alias']);
  56. $backendAlias->method('getIdentifier')
  57. ->willReturn('identifier_real');
  58. $service->registerBackend($backend);
  59. $service->registerBackend($backendAlias);
  60. $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
  61. $this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
  62. $this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
  63. $backends = $service->getBackends();
  64. $this->assertCount(2, $backends);
  65. $this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
  66. $this->assertArrayHasKey('identifier_real', $backends);
  67. $this->assertArrayNotHasKey('identifier_alias', $backends);
  68. }
  69. public function testBackendProvider() {
  70. $service = new BackendService($this->config);
  71. $backend1 = $this->getBackendMock('\Foo\Bar');
  72. $backend2 = $this->getBackendMock('\Bar\Foo');
  73. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $providerMock */
  74. $providerMock = $this->createMock(IBackendProvider::class);
  75. $providerMock->expects($this->once())
  76. ->method('getBackends')
  77. ->willReturn([$backend1, $backend2]);
  78. $service->registerBackendProvider($providerMock);
  79. $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
  80. $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
  81. $this->assertCount(2, $service->getBackends());
  82. }
  83. public function testAuthMechanismProvider() {
  84. $service = new BackendService($this->config);
  85. $backend1 = $this->getAuthMechanismMock('\Foo\Bar');
  86. $backend2 = $this->getAuthMechanismMock('\Bar\Foo');
  87. /** @var IAuthMechanismProvider|\PHPUnit\Framework\MockObject\MockObject $providerMock */
  88. $providerMock = $this->createMock(IAuthMechanismProvider::class);
  89. $providerMock->expects($this->once())
  90. ->method('getAuthMechanisms')
  91. ->willReturn([$backend1, $backend2]);
  92. $service->registerAuthMechanismProvider($providerMock);
  93. $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
  94. $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
  95. $this->assertCount(2, $service->getAuthMechanisms());
  96. }
  97. public function testMultipleBackendProviders() {
  98. $service = new BackendService($this->config);
  99. $backend1a = $this->getBackendMock('\Foo\Bar');
  100. $backend1b = $this->getBackendMock('\Bar\Foo');
  101. $backend2 = $this->getBackendMock('\Dead\Beef');
  102. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $provider1Mock */
  103. $provider1Mock = $this->createMock(IBackendProvider::class);
  104. $provider1Mock->expects($this->once())
  105. ->method('getBackends')
  106. ->willReturn([$backend1a, $backend1b]);
  107. $service->registerBackendProvider($provider1Mock);
  108. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $provider2Mock */
  109. $provider2Mock = $this->createMock(IBackendProvider::class);
  110. $provider2Mock->expects($this->once())
  111. ->method('getBackends')
  112. ->willReturn([$backend2]);
  113. $service->registerBackendProvider($provider2Mock);
  114. $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
  115. $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
  116. $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
  117. $this->assertCount(3, $service->getBackends());
  118. }
  119. public function testUserMountingBackends() {
  120. $this->config->expects($this->exactly(2))
  121. ->method('getAppValue')
  122. ->willReturnMap([
  123. ['files_external', 'allow_user_mounting', 'yes', 'yes'],
  124. ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
  125. ]);
  126. $service = new BackendService($this->config);
  127. $backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
  128. $backendAllowed->expects($this->never())
  129. ->method('removeVisibility');
  130. $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
  131. $backendNotAllowed->expects($this->once())
  132. ->method('removeVisibility')
  133. ->with(BackendService::VISIBILITY_PERSONAL);
  134. $backendAlias = $this->getMockBuilder(Backend::class)
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $backendAlias->method('getIdentifierAliases')
  138. ->willReturn(['identifier_real', 'identifier_alias']);
  139. $backendAlias->expects($this->never())
  140. ->method('removeVisibility');
  141. $service->registerBackend($backendAllowed);
  142. $service->registerBackend($backendNotAllowed);
  143. $service->registerBackend($backendAlias);
  144. }
  145. public function testGetAvailableBackends() {
  146. $service = new BackendService($this->config);
  147. $backendAvailable = $this->getBackendMock('\Backend\Available');
  148. $backendAvailable->expects($this->once())
  149. ->method('checkDependencies')
  150. ->willReturn([]);
  151. $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
  152. $backendNotAvailable->expects($this->once())
  153. ->method('checkDependencies')
  154. ->willReturn([
  155. $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
  156. ->disableOriginalConstructor()
  157. ->getMock()
  158. ]);
  159. $service->registerBackend($backendAvailable);
  160. $service->registerBackend($backendNotAvailable);
  161. $availableBackends = $service->getAvailableBackends();
  162. $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
  163. $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
  164. }
  165. public function invalidConfigPlaceholderProvider() {
  166. return [
  167. [['@user']],
  168. [['$user']],
  169. [['hællo']],
  170. [['spa ce']],
  171. [['yo\o']],
  172. [['<script>…</script>']],
  173. [['xxyoloxx', 'invÆlid']],
  174. [['tautology', 'tautology']],
  175. [['tautology2', 'TAUTOLOGY2']],
  176. ];
  177. }
  178. /**
  179. * @dataProvider invalidConfigPlaceholderProvider
  180. */
  181. public function testRegisterConfigHandlerInvalid(array $placeholders) {
  182. $this->expectException(\RuntimeException::class);
  183. $service = new BackendService($this->config);
  184. $mock = $this->createMock(IConfigHandler::class);
  185. $cb = function () use ($mock) {
  186. return $mock;
  187. };
  188. foreach ($placeholders as $placeholder) {
  189. $service->registerConfigHandler($placeholder, $cb);
  190. }
  191. }
  192. public function testConfigHandlers() {
  193. $service = new BackendService($this->config);
  194. $mock = $this->createMock(IConfigHandler::class);
  195. $mock->expects($this->exactly(3))
  196. ->method('handle');
  197. $cb = function () use ($mock) {
  198. return $mock;
  199. };
  200. $service->registerConfigHandler('one', $cb);
  201. $service->registerConfigHandler('2', $cb);
  202. $service->registerConfigHandler('Three', $cb);
  203. /** @var IConfigHandler[] $handlers */
  204. $handlers = $service->getConfigHandlers();
  205. foreach ($handlers as $handler) {
  206. $handler->handle('Something');
  207. }
  208. }
  209. }