2015-08-12 12:00:37 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-08-12 12:00:37 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
namespace OCA\Files_External\Tests\Service;
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
use OCA\Files_External\Config\IConfigHandler;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCA\Files_External\Lib\Auth\AuthMechanism;
|
|
|
|
use OCA\Files_External\Lib\Backend\Backend;
|
2016-09-02 11:37:20 +03:00
|
|
|
use OCA\Files_External\Lib\Config\IAuthMechanismProvider;
|
|
|
|
use OCA\Files_External\Lib\Config\IBackendProvider;
|
2019-02-12 01:18:08 +03:00
|
|
|
use OCA\Files_External\Service\BackendService;
|
2016-09-02 11:37:20 +03:00
|
|
|
use OCP\IConfig;
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
class BackendServiceTest extends \Test\TestCase {
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2015-08-12 12:00:37 +03:00
|
|
|
protected $config;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-09-02 11:37:20 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|
|
|
|
|
2015-12-07 15:05:27 +03:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
2019-02-12 01:18:08 +03:00
|
|
|
* @return \OCA\Files_External\Lib\Backend\Backend|\PHPUnit_Framework_MockObject_MockObject
|
2015-12-07 15:05:27 +03:00
|
|
|
*/
|
2015-08-12 12:00:37 +03:00
|
|
|
protected function getBackendMock($class) {
|
2017-10-24 16:26:53 +03:00
|
|
|
$backend = $this->getMockBuilder(Backend::class)
|
2015-08-12 12:00:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
|
|
|
|
$backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
|
2016-05-11 23:24:53 +03:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*
|
2019-02-12 01:18:08 +03:00
|
|
|
* @return \OCA\Files_External\Lib\Auth\AuthMechanism|\PHPUnit_Framework_MockObject_MockObject
|
2016-05-11 23:24:53 +03:00
|
|
|
*/
|
|
|
|
protected function getAuthMechanismMock($class) {
|
2017-10-24 16:26:53 +03:00
|
|
|
$backend = $this->getMockBuilder(AuthMechanism::class)
|
2016-05-11 23:24:53 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
|
|
|
|
$backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
|
|
|
|
return $backend;
|
|
|
|
}
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
public function testRegisterBackend() {
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
$backend = $this->getBackendMock('\Foo\Bar');
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var \OCA\Files_External\Lib\Backend\Backend|\PHPUnit_Framework_MockObject_MockObject $backendAlias */
|
2017-10-24 16:26:53 +03:00
|
|
|
$backendAlias = $this->getMockBuilder(Backend::class)
|
2015-08-12 12:00:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$backendAlias->method('getIdentifierAliases')
|
|
|
|
->willReturn(['identifier_real', 'identifier_alias']);
|
|
|
|
$backendAlias->method('getIdentifier')
|
|
|
|
->willReturn('identifier_real');
|
|
|
|
|
|
|
|
$service->registerBackend($backend);
|
|
|
|
$service->registerBackend($backendAlias);
|
|
|
|
|
|
|
|
$this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
|
|
|
|
$this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
|
|
|
|
$this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
|
|
|
|
|
|
|
|
$backends = $service->getBackends();
|
|
|
|
$this->assertCount(2, $backends);
|
|
|
|
$this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
|
|
|
|
$this->assertArrayHasKey('identifier_real', $backends);
|
|
|
|
$this->assertArrayNotHasKey('identifier_alias', $backends);
|
|
|
|
}
|
|
|
|
|
2016-05-11 23:24:53 +03:00
|
|
|
public function testBackendProvider() {
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2016-05-11 23:24:53 +03:00
|
|
|
|
|
|
|
$backend1 = $this->getBackendMock('\Foo\Bar');
|
|
|
|
$backend2 = $this->getBackendMock('\Bar\Foo');
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var IBackendProvider|\PHPUnit_Framework_MockObject_MockObject $providerMock */
|
2016-09-02 11:37:20 +03:00
|
|
|
$providerMock = $this->createMock(IBackendProvider::class);
|
2016-05-11 23:24:53 +03:00
|
|
|
$providerMock->expects($this->once())
|
|
|
|
->method('getBackends')
|
|
|
|
->willReturn([$backend1, $backend2]);
|
|
|
|
$service->registerBackendProvider($providerMock);
|
|
|
|
|
|
|
|
$this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
|
|
|
|
$this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
|
|
|
|
|
|
|
|
$this->assertCount(2, $service->getBackends());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthMechanismProvider() {
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2016-05-11 23:24:53 +03:00
|
|
|
|
|
|
|
$backend1 = $this->getAuthMechanismMock('\Foo\Bar');
|
|
|
|
$backend2 = $this->getAuthMechanismMock('\Bar\Foo');
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var IAuthMechanismProvider|\PHPUnit_Framework_MockObject_MockObject $providerMock */
|
2016-09-02 11:37:20 +03:00
|
|
|
$providerMock = $this->createMock(IAuthMechanismProvider::class);
|
2016-05-11 23:24:53 +03:00
|
|
|
$providerMock->expects($this->once())
|
|
|
|
->method('getAuthMechanisms')
|
|
|
|
->willReturn([$backend1, $backend2]);
|
|
|
|
$service->registerAuthMechanismProvider($providerMock);
|
|
|
|
|
|
|
|
$this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
|
|
|
|
$this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
|
|
|
|
|
|
|
|
$this->assertCount(2, $service->getAuthMechanisms());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleBackendProviders() {
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2016-05-11 23:24:53 +03:00
|
|
|
|
|
|
|
$backend1a = $this->getBackendMock('\Foo\Bar');
|
|
|
|
$backend1b = $this->getBackendMock('\Bar\Foo');
|
|
|
|
|
|
|
|
$backend2 = $this->getBackendMock('\Dead\Beef');
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var IBackendProvider|\PHPUnit_Framework_MockObject_MockObject $provider1Mock */
|
2016-09-02 11:37:20 +03:00
|
|
|
$provider1Mock = $this->createMock(IBackendProvider::class);
|
2016-05-11 23:24:53 +03:00
|
|
|
$provider1Mock->expects($this->once())
|
|
|
|
->method('getBackends')
|
|
|
|
->willReturn([$backend1a, $backend1b]);
|
|
|
|
$service->registerBackendProvider($provider1Mock);
|
2019-02-12 01:18:08 +03:00
|
|
|
/** @var IBackendProvider|\PHPUnit_Framework_MockObject_MockObject $provider2Mock */
|
2016-09-02 11:37:20 +03:00
|
|
|
$provider2Mock = $this->createMock(IBackendProvider::class);
|
2016-05-11 23:24:53 +03:00
|
|
|
$provider2Mock->expects($this->once())
|
|
|
|
->method('getBackends')
|
|
|
|
->willReturn([$backend2]);
|
|
|
|
$service->registerBackendProvider($provider2Mock);
|
|
|
|
|
|
|
|
$this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
|
|
|
|
$this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
|
|
|
|
$this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
|
|
|
|
|
|
|
|
$this->assertCount(3, $service->getBackends());
|
|
|
|
}
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
public function testUserMountingBackends() {
|
|
|
|
$this->config->expects($this->exactly(2))
|
|
|
|
->method('getAppValue')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['files_external', 'allow_user_mounting', 'yes', 'yes'],
|
|
|
|
['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
|
|
|
|
]));
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
$backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
|
|
|
|
$backendAllowed->expects($this->never())
|
2015-09-17 12:24:19 +03:00
|
|
|
->method('removeVisibility');
|
2015-08-12 12:00:37 +03:00
|
|
|
$backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
|
|
|
|
$backendNotAllowed->expects($this->once())
|
2015-09-17 12:24:19 +03:00
|
|
|
->method('removeVisibility')
|
|
|
|
->with(BackendService::VISIBILITY_PERSONAL);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$backendAlias = $this->getMockBuilder(Backend::class)
|
2015-08-12 12:00:37 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$backendAlias->method('getIdentifierAliases')
|
|
|
|
->willReturn(['identifier_real', 'identifier_alias']);
|
|
|
|
$backendAlias->expects($this->never())
|
|
|
|
->method('removeVisibility');
|
|
|
|
|
|
|
|
$service->registerBackend($backendAllowed);
|
|
|
|
$service->registerBackend($backendNotAllowed);
|
|
|
|
$service->registerBackend($backendAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvailableBackends() {
|
2019-02-12 01:18:08 +03:00
|
|
|
$service = new BackendService($this->config);
|
2015-08-12 12:00:37 +03:00
|
|
|
|
|
|
|
$backendAvailable = $this->getBackendMock('\Backend\Available');
|
|
|
|
$backendAvailable->expects($this->once())
|
|
|
|
->method('checkDependencies')
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
$backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
|
|
|
|
$backendNotAvailable->expects($this->once())
|
|
|
|
->method('checkDependencies')
|
|
|
|
->will($this->returnValue([
|
|
|
|
$this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock()
|
|
|
|
]));
|
|
|
|
|
|
|
|
$service->registerBackend($backendAvailable);
|
|
|
|
$service->registerBackend($backendNotAvailable);
|
|
|
|
|
|
|
|
$availableBackends = $service->getAvailableBackends();
|
|
|
|
$this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
|
|
|
|
$this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
|
|
|
|
}
|
|
|
|
|
2019-02-12 01:18:08 +03:00
|
|
|
public function invalidConfigPlaceholderProvider() {
|
|
|
|
return [
|
|
|
|
[['@user']],
|
2019-02-16 00:41:29 +03:00
|
|
|
[['$user']],
|
2019-02-12 01:18:08 +03:00
|
|
|
[['hællo']],
|
|
|
|
[['spa ce']],
|
|
|
|
[['yo\o']],
|
|
|
|
[['<script>…</script>']],
|
|
|
|
[['xxyoloxx', 'invÆlid']],
|
|
|
|
[['tautology', 'tautology']],
|
|
|
|
[['tautology2', 'TAUTOLOGY2']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider invalidConfigPlaceholderProvider
|
|
|
|
* @expectedException \RuntimeException
|
|
|
|
*/
|
|
|
|
public function testRegisterConfigHandlerInvalid(array $placeholders) {
|
|
|
|
$service = new BackendService($this->config);
|
|
|
|
$mock = $this->createMock(IConfigHandler::class);
|
|
|
|
$cb = function () use ($mock) { return $mock; };
|
|
|
|
foreach ($placeholders as $placeholder) {
|
|
|
|
$service->registerConfigHandler($placeholder, $cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigHandlers() {
|
|
|
|
$service = new BackendService($this->config);
|
|
|
|
$mock = $this->createMock(IConfigHandler::class);
|
|
|
|
$mock->expects($this->exactly(3))
|
|
|
|
->method('handle');
|
|
|
|
$cb = function () use ($mock) { return $mock; };
|
|
|
|
$service->registerConfigHandler('one', $cb);
|
|
|
|
$service->registerConfigHandler('2', $cb);
|
|
|
|
$service->registerConfigHandler('Three', $cb);
|
|
|
|
|
|
|
|
/** @var IConfigHandler[] $handlers */
|
|
|
|
$handlers = $service->getConfigHandlers();
|
|
|
|
|
|
|
|
foreach ($handlers as $handler) {
|
|
|
|
$handler->handle('Something');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 12:00:37 +03:00
|
|
|
}
|