Merge pull request #18733 from owncloud/ext-dependencycheck

Make checkDependencies a real method
This commit is contained in:
Robin McCorkell 2015-09-11 09:46:40 +01:00
commit 57e49d2005
11 changed files with 162 additions and 64 deletions

View File

@ -26,10 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\AmazonS3\AccessKey; use \OCA\Files_External\Lib\Auth\AmazonS3\AccessKey;
class AmazonS3 extends Backend { class AmazonS3 extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, AccessKey $legacyAuth) { public function __construct(IL10N $l, AccessKey $legacyAuth) {
$this $this
->setIdentifier('amazons3') ->setIdentifier('amazons3')
@ -49,7 +53,6 @@ class AmazonS3 extends Backend {
(new DefinitionParameter('use_path_style', $l->t('Enable Path Style'))) (new DefinitionParameter('use_path_style', $l->t('Enable Path Style')))
->setType(DefinitionParameter::VALUE_BOOLEAN), ->setType(DefinitionParameter::VALUE_BOOLEAN),
]) ])
->setDependencyCheck('\OC\Files\Storage\AmazonS3::checkDependencies')
->addAuthScheme(AccessKey::SCHEME_AMAZONS3_ACCESSKEY) ->addAuthScheme(AccessKey::SCHEME_AMAZONS3_ACCESSKEY)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -26,11 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\Password\Password; use \OCA\Files_External\Lib\Auth\Password\Password;
class DAV extends Backend { class DAV extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, Password $legacyAuth) { public function __construct(IL10N $l, Password $legacyAuth) {
$this $this
->setIdentifier('dav') ->setIdentifier('dav')
@ -44,7 +47,6 @@ class DAV extends Backend {
(new DefinitionParameter('secure', $l->t('Secure https://'))) (new DefinitionParameter('secure', $l->t('Secure https://')))
->setType(DefinitionParameter::VALUE_BOOLEAN), ->setType(DefinitionParameter::VALUE_BOOLEAN),
]) ])
->setDependencyCheck('\OC\Files\Storage\DAV::checkDependencies')
->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -26,10 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\OAuth1\OAuth1; use \OCA\Files_External\Lib\Auth\OAuth1\OAuth1;
class Dropbox extends Backend { class Dropbox extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, OAuth1 $legacyAuth) { public function __construct(IL10N $l, OAuth1 $legacyAuth) {
$this $this
->setIdentifier('dropbox') ->setIdentifier('dropbox')
@ -39,7 +43,6 @@ class Dropbox extends Backend {
->addParameters([ ->addParameters([
// all parameters handled in OAuth1 mechanism // all parameters handled in OAuth1 mechanism
]) ])
->setDependencyCheck('\OC\Files\Storage\Dropbox::checkDependencies')
->addAuthScheme(AuthMechanism::SCHEME_OAUTH1) ->addAuthScheme(AuthMechanism::SCHEME_OAUTH1)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -26,11 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\Password\Password; use \OCA\Files_External\Lib\Auth\Password\Password;
class FTP extends Backend { class FTP extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, Password $legacyAuth) { public function __construct(IL10N $l, Password $legacyAuth) {
$this $this
->setIdentifier('ftp') ->setIdentifier('ftp')
@ -44,7 +47,6 @@ class FTP extends Backend {
(new DefinitionParameter('secure', $l->t('Secure ftps://'))) (new DefinitionParameter('secure', $l->t('Secure ftps://')))
->setType(DefinitionParameter::VALUE_BOOLEAN), ->setType(DefinitionParameter::VALUE_BOOLEAN),
]) ])
->setDependencyCheck('\OC\Files\Storage\FTP::checkDependencies')
->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -26,10 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\OAuth2\OAuth2; use \OCA\Files_External\Lib\Auth\OAuth2\OAuth2;
class Google extends Backend { class Google extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, OAuth2 $legacyAuth) { public function __construct(IL10N $l, OAuth2 $legacyAuth) {
$this $this
->setIdentifier('googledrive') ->setIdentifier('googledrive')
@ -39,7 +43,6 @@ class Google extends Backend {
->addParameters([ ->addParameters([
// all parameters handled in OAuth2 mechanism // all parameters handled in OAuth2 mechanism
]) ])
->setDependencyCheck('\OC\Files\Storage\Google::checkDependencies')
->addAuthScheme(AuthMechanism::SCHEME_OAUTH2) ->addAuthScheme(AuthMechanism::SCHEME_OAUTH2)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -24,12 +24,21 @@ namespace OCA\Files_External\Lib\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Backend\Backend; use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\Auth\Builtin; use \OCA\Files_External\Lib\Auth\Builtin;
use \OCA\Files_External\Lib\MissingDependency;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
/** /**
* Legacy compatibility for OC_Mount_Config::registerBackend() * Legacy compatibility for OC_Mount_Config::registerBackend()
*/ */
class LegacyBackend extends Backend { class LegacyBackend extends Backend {
use LegacyDependencyCheckPolyfill {
LegacyDependencyCheckPolyfill::checkDependencies as doCheckDependencies;
}
/** @var bool */
protected $hasDependencies = false;
/** /**
* @param string $class * @param string $class
* @param array $definition * @param array $definition
@ -78,8 +87,18 @@ class LegacyBackend extends Backend {
$this->setCustomJs($definition['custom']); $this->setCustomJs($definition['custom']);
} }
if (isset($definition['has_dependencies']) && $definition['has_dependencies']) { if (isset($definition['has_dependencies']) && $definition['has_dependencies']) {
$this->setDependencyCheck($class . '::checkDependencies'); $this->hasDependencies = true;
} }
} }
/**
* @return MissingDependency[]
*/
public function checkDependencies() {
if ($this->hasDependencies) {
return $this->doCheckDependencies();
}
return [];
}
} }

View File

@ -26,11 +26,14 @@ use \OCA\Files_External\Lib\Backend\Backend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\Auth\AuthMechanism; use \OCA\Files_External\Lib\Auth\AuthMechanism;
use \OCA\Files_External\Service\BackendService; use \OCA\Files_External\Service\BackendService;
use \OCA\Files_External\Lib\LegacyDependencyCheckPolyfill;
use \OCA\Files_External\Lib\Auth\Password\Password; use \OCA\Files_External\Lib\Auth\Password\Password;
class SMB extends Backend { class SMB extends Backend {
use LegacyDependencyCheckPolyfill;
public function __construct(IL10N $l, Password $legacyAuth) { public function __construct(IL10N $l, Password $legacyAuth) {
$this $this
->setIdentifier('smb') ->setIdentifier('smb')
@ -43,7 +46,6 @@ class SMB extends Backend {
(new DefinitionParameter('root', $l->t('Remote subfolder'))) (new DefinitionParameter('root', $l->t('Remote subfolder')))
->setFlag(DefinitionParameter::FLAG_OPTIONAL), ->setFlag(DefinitionParameter::FLAG_OPTIONAL),
]) ])
->setDependencyCheck('\OC\Files\Storage\SMB::checkDependencies')
->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD)
->setLegacyAuthMechanism($legacyAuth) ->setLegacyAuthMechanism($legacyAuth)
; ;

View File

@ -28,58 +28,13 @@ use \OCA\Files_External\Lib\MissingDependency;
*/ */
trait DependencyTrait { trait DependencyTrait {
/** @var callable|null dependency check */
private $dependencyCheck = null;
/**
* @return bool
*/
public function hasDependencies() {
return !is_null($this->dependencyCheck);
}
/**
* @param callable $dependencyCheck
* @return self
*/
public function setDependencyCheck(callable $dependencyCheck) {
$this->dependencyCheck = $dependencyCheck;
return $this;
}
/** /**
* Check if object is valid for use * Check if object is valid for use
* *
* @return MissingDependency[] Unsatisfied dependencies * @return MissingDependency[] Unsatisfied dependencies
*/ */
public function checkDependencies() { public function checkDependencies() {
$ret = []; return []; // no dependencies by default
if ($this->hasDependencies()) {
$result = call_user_func($this->dependencyCheck);
if ($result !== true) {
if (!is_array($result)) {
$result = [$result];
}
foreach ($result as $key => $value) {
if (!($value instanceof MissingDependency)) {
$module = null;
$message = null;
if (is_numeric($key)) {
$module = $value;
} else {
$module = $key;
$message = $value;
}
$value = new MissingDependency($module, $this);
$value->setMessage($message);
}
$ret[] = $value;
}
}
}
return $ret;
} }
} }

View File

@ -0,0 +1,70 @@
<?php
/**
* @author Robin McCorkell <rmccorkell@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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/>
*
*/
namespace OCA\Files_External\Lib;
use \OCA\Files_External\Lib\MissingDependency;
/**
* Polyfill for checking dependencies using legacy Storage::checkDependencies()
*/
trait LegacyDependencyCheckPolyfill {
/**
* @return string
*/
abstract public function getStorageClass();
/**
* Check if object is valid for use
*
* @return MissingDependency[] Unsatisfied dependencies
*/
public function checkDependencies() {
$ret = [];
$result = call_user_func([$this->getStorageClass(), 'checkDependencies']);
if ($result !== true) {
if (!is_array($result)) {
$result = [$result];
}
foreach ($result as $key => $value) {
if (!($value instanceof MissingDependency)) {
$module = null;
$message = null;
if (is_numeric($key)) {
$module = $value;
} else {
$module = $key;
$message = $value;
}
$value = new MissingDependency($module, $this);
$value->setMessage($message);
}
$ret[] = $value;
}
}
return $ret;
}
}

View File

@ -23,15 +23,25 @@ namespace OCA\Files_External\Tests\Backend;
use \OCA\Files_External\Lib\Backend\LegacyBackend; use \OCA\Files_External\Lib\Backend\LegacyBackend;
use \OCA\Files_External\Lib\DefinitionParameter; use \OCA\Files_External\Lib\DefinitionParameter;
use \OCA\Files_External\Lib\MissingDependency;
class LegacyBackendTest extends \Test\TestCase { class LegacyBackendTest extends \Test\TestCase {
/**
* @return MissingDependency[]
*/
public static function checkDependencies() {
return [
(new MissingDependency('abc'))->setMessage('foobar')
];
}
public function testConstructor() { public function testConstructor() {
$auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\Builtin') $auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\Builtin')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$class = '\OC\Files\Storage\SMB'; $class = '\OCA\Files_External\Tests\Backend\LegacyBackendTest';
$definition = [ $definition = [
'configuration' => [ 'configuration' => [
'textfield' => 'Text field', 'textfield' => 'Text field',
@ -49,14 +59,18 @@ class LegacyBackendTest extends \Test\TestCase {
$backend = new LegacyBackend($class, $definition, $auth); $backend = new LegacyBackend($class, $definition, $auth);
$this->assertEquals('\OC\Files\Storage\SMB', $backend->getStorageClass()); $this->assertEquals('\OCA\Files_External\Tests\Backend\LegacyBackendTest', $backend->getStorageClass());
$this->assertEquals('Backend text', $backend->getText()); $this->assertEquals('Backend text', $backend->getText());
$this->assertEquals(123, $backend->getPriority()); $this->assertEquals(123, $backend->getPriority());
$this->assertEquals('foo/bar.js', $backend->getCustomJs()); $this->assertEquals('foo/bar.js', $backend->getCustomJs());
$this->assertEquals(true, $backend->hasDependencies());
$this->assertArrayHasKey('builtin', $backend->getAuthSchemes()); $this->assertArrayHasKey('builtin', $backend->getAuthSchemes());
$this->assertEquals($auth, $backend->getLegacyAuthMechanism()); $this->assertEquals($auth, $backend->getLegacyAuthMechanism());
$dependencies = $backend->checkDependencies();
$this->assertCount(1, $dependencies);
$this->assertEquals('abc', $dependencies[0]->getDependency());
$this->assertEquals('foobar', $dependencies[0]->getMessage());
$parameters = $backend->getParameters(); $parameters = $backend->getParameters();
$this->assertEquals('Text field', $parameters['textfield']->getText()); $this->assertEquals('Text field', $parameters['textfield']->getText());
$this->assertEquals(DefinitionParameter::VALUE_TEXT, $parameters['textfield']->getType()); $this->assertEquals(DefinitionParameter::VALUE_TEXT, $parameters['textfield']->getType());
@ -78,4 +92,22 @@ class LegacyBackendTest extends \Test\TestCase {
$this->assertEquals(DefinitionParameter::FLAG_OPTIONAL, $parameters['optionalpassword']->getFlags()); $this->assertEquals(DefinitionParameter::FLAG_OPTIONAL, $parameters['optionalpassword']->getFlags());
} }
public function testNoDependencies() {
$auth = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\Builtin')
->disableOriginalConstructor()
->getMock();
$class = '\OCA\Files_External\Tests\Backend\LegacyBackendTest';
$definition = [
'configuration' => [
],
'backend' => 'Backend text',
];
$backend = new LegacyBackend($class, $definition, $auth);
$dependencies = $backend->checkDependencies();
$this->assertCount(0, $dependencies);
}
} }

View File

@ -23,16 +23,23 @@ namespace OCA\Files_External\Tests;
use \OCA\Files_External\Lib\MissingDependency; use \OCA\Files_External\Lib\MissingDependency;
class DependencyTraitTest extends \Test\TestCase { class LegacyDependencyCheckPolyfillTest extends \Test\TestCase {
public function testCheckDependencies() { /**
$trait = $this->getMockForTrait('\OCA\Files_External\Lib\DependencyTrait'); * @return MissingDependency[]
$trait->setDependencyCheck(function() { */
public static function checkDependencies() {
return [ return [
(new MissingDependency('dependency'))->setMessage('missing dependency'), (new MissingDependency('dependency'))->setMessage('missing dependency'),
(new MissingDependency('program'))->setMessage('cannot find program'), (new MissingDependency('program'))->setMessage('cannot find program'),
]; ];
}); }
public function testCheckDependencies() {
$trait = $this->getMockForTrait('\OCA\Files_External\Lib\LegacyDependencyCheckPolyfill');
$trait->expects($this->once())
->method('getStorageClass')
->willReturn('\OCA\Files_External\Tests\LegacyDependencyCheckPolyfillTest');
$dependencies = $trait->checkDependencies(); $dependencies = $trait->checkDependencies();
$this->assertCount(2, $dependencies); $this->assertCount(2, $dependencies);