1
0

BackendServiceTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_External\Tests\Service;
  27. use OCA\Files_External\Config\IConfigHandler;
  28. use OCA\Files_External\Lib\Auth\AuthMechanism;
  29. use OCA\Files_External\Lib\Backend\Backend;
  30. use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
  31. use OCA\Files_External\Lib\Config\IBackendProvider;
  32. use OCA\Files_External\Service\BackendService;
  33. use OCP\IConfig;
  34. class BackendServiceTest extends \Test\TestCase {
  35. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $config;
  37. protected function setUp(): void {
  38. $this->config = $this->createMock(IConfig::class);
  39. }
  40. /**
  41. * @param string $class
  42. *
  43. * @return \OCA\Files_External\Lib\Backend\Backend|\PHPUnit\Framework\MockObject\MockObject
  44. */
  45. protected function getBackendMock($class) {
  46. $backend = $this->getMockBuilder(Backend::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $backend->method('getIdentifier')->willReturn('identifier:'.$class);
  50. $backend->method('getIdentifierAliases')->willReturn(['identifier:'.$class]);
  51. return $backend;
  52. }
  53. /**
  54. * @param string $class
  55. *
  56. * @return \OCA\Files_External\Lib\Auth\AuthMechanism|\PHPUnit\Framework\MockObject\MockObject
  57. */
  58. protected function getAuthMechanismMock($class) {
  59. $backend = $this->getMockBuilder(AuthMechanism::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $backend->method('getIdentifier')->willReturn('identifier:'.$class);
  63. $backend->method('getIdentifierAliases')->willReturn(['identifier:'.$class]);
  64. return $backend;
  65. }
  66. public function testRegisterBackend() {
  67. $service = new BackendService($this->config);
  68. $backend = $this->getBackendMock('\Foo\Bar');
  69. /** @var \OCA\Files_External\Lib\Backend\Backend|\PHPUnit\Framework\MockObject\MockObject $backendAlias */
  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);
  90. $backend1 = $this->getBackendMock('\Foo\Bar');
  91. $backend2 = $this->getBackendMock('\Bar\Foo');
  92. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $providerMock */
  93. $providerMock = $this->createMock(IBackendProvider::class);
  94. $providerMock->expects($this->once())
  95. ->method('getBackends')
  96. ->willReturn([$backend1, $backend2]);
  97. $service->registerBackendProvider($providerMock);
  98. $this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
  99. $this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
  100. $this->assertCount(2, $service->getBackends());
  101. }
  102. public function testAuthMechanismProvider() {
  103. $service = new BackendService($this->config);
  104. $backend1 = $this->getAuthMechanismMock('\Foo\Bar');
  105. $backend2 = $this->getAuthMechanismMock('\Bar\Foo');
  106. /** @var IAuthMechanismProvider|\PHPUnit\Framework\MockObject\MockObject $providerMock */
  107. $providerMock = $this->createMock(IAuthMechanismProvider::class);
  108. $providerMock->expects($this->once())
  109. ->method('getAuthMechanisms')
  110. ->willReturn([$backend1, $backend2]);
  111. $service->registerAuthMechanismProvider($providerMock);
  112. $this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
  113. $this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
  114. $this->assertCount(2, $service->getAuthMechanisms());
  115. }
  116. public function testMultipleBackendProviders() {
  117. $service = new BackendService($this->config);
  118. $backend1a = $this->getBackendMock('\Foo\Bar');
  119. $backend1b = $this->getBackendMock('\Bar\Foo');
  120. $backend2 = $this->getBackendMock('\Dead\Beef');
  121. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $provider1Mock */
  122. $provider1Mock = $this->createMock(IBackendProvider::class);
  123. $provider1Mock->expects($this->once())
  124. ->method('getBackends')
  125. ->willReturn([$backend1a, $backend1b]);
  126. $service->registerBackendProvider($provider1Mock);
  127. /** @var IBackendProvider|\PHPUnit\Framework\MockObject\MockObject $provider2Mock */
  128. $provider2Mock = $this->createMock(IBackendProvider::class);
  129. $provider2Mock->expects($this->once())
  130. ->method('getBackends')
  131. ->willReturn([$backend2]);
  132. $service->registerBackendProvider($provider2Mock);
  133. $this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
  134. $this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
  135. $this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
  136. $this->assertCount(3, $service->getBackends());
  137. }
  138. public function testUserMountingBackends() {
  139. $this->config->expects($this->exactly(2))
  140. ->method('getAppValue')
  141. ->willReturnMap([
  142. ['files_external', 'allow_user_mounting', 'yes', 'yes'],
  143. ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
  144. ]);
  145. $service = new BackendService($this->config);
  146. $backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
  147. $backendAllowed->expects($this->never())
  148. ->method('removeVisibility');
  149. $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
  150. $backendNotAllowed->expects($this->once())
  151. ->method('removeVisibility')
  152. ->with(BackendService::VISIBILITY_PERSONAL);
  153. $backendAlias = $this->getMockBuilder(Backend::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $backendAlias->method('getIdentifierAliases')
  157. ->willReturn(['identifier_real', 'identifier_alias']);
  158. $backendAlias->expects($this->never())
  159. ->method('removeVisibility');
  160. $service->registerBackend($backendAllowed);
  161. $service->registerBackend($backendNotAllowed);
  162. $service->registerBackend($backendAlias);
  163. }
  164. public function testGetAvailableBackends() {
  165. $service = new BackendService($this->config);
  166. $backendAvailable = $this->getBackendMock('\Backend\Available');
  167. $backendAvailable->expects($this->once())
  168. ->method('checkDependencies')
  169. ->willReturn([]);
  170. $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
  171. $backendNotAvailable->expects($this->once())
  172. ->method('checkDependencies')
  173. ->willReturn([
  174. $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
  175. ->disableOriginalConstructor()
  176. ->getMock()
  177. ]);
  178. $service->registerBackend($backendAvailable);
  179. $service->registerBackend($backendNotAvailable);
  180. $availableBackends = $service->getAvailableBackends();
  181. $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
  182. $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
  183. }
  184. public function invalidConfigPlaceholderProvider() {
  185. return [
  186. [['@user']],
  187. [['$user']],
  188. [['hællo']],
  189. [['spa ce']],
  190. [['yo\o']],
  191. [['<script>…</script>']],
  192. [['xxyoloxx', 'invÆlid']],
  193. [['tautology', 'tautology']],
  194. [['tautology2', 'TAUTOLOGY2']],
  195. ];
  196. }
  197. /**
  198. * @dataProvider invalidConfigPlaceholderProvider
  199. */
  200. public function testRegisterConfigHandlerInvalid(array $placeholders) {
  201. $this->expectException(\RuntimeException::class);
  202. $service = new BackendService($this->config);
  203. $mock = $this->createMock(IConfigHandler::class);
  204. $cb = function () use ($mock) {
  205. return $mock;
  206. };
  207. foreach ($placeholders as $placeholder) {
  208. $service->registerConfigHandler($placeholder, $cb);
  209. }
  210. }
  211. public function testConfigHandlers() {
  212. $service = new BackendService($this->config);
  213. $mock = $this->createMock(IConfigHandler::class);
  214. $mock->expects($this->exactly(3))
  215. ->method('handle');
  216. $cb = function () use ($mock) {
  217. return $mock;
  218. };
  219. $service->registerConfigHandler('one', $cb);
  220. $service->registerConfigHandler('2', $cb);
  221. $service->registerConfigHandler('Three', $cb);
  222. /** @var IConfigHandler[] $handlers */
  223. $handlers = $service->getConfigHandlers();
  224. foreach ($handlers as $handler) {
  225. $handler->handle('Something');
  226. }
  227. }
  228. }