diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php index 242cdff911..613f0b2609 100755 --- a/apps/files_external/lib/config.php +++ b/apps/files_external/lib/config.php @@ -41,6 +41,11 @@ class OC_Mount_Config { private static $backends = array(); + /** + * @param string $class + * @param array $definition + * @return bool + */ public static function registerBackend($class, $definition) { if (!isset($definition['backend'])) { return false; @@ -50,6 +55,18 @@ class OC_Mount_Config { return true; } + /** + * Setup backends + * + * @return array of previously registered backends + */ + public static function setUp($backends = array()) { + $backup = self::$backends; + self::$backends = $backends; + + return $backup; + } + /** * Get details on each of the external storage backends, used for the mount config UI * If a custom UI is needed, add the key 'custom' and a javascript file with that name will be loaded @@ -57,28 +74,32 @@ class OC_Mount_Config { * If the configuration parameter is a boolean, add a '!' to the beginning of the value * If the configuration parameter is optional, add a '&' to the beginning of the value * If the configuration parameter is hidden, add a '#' to the beginning of the value - * @return string + * @return array */ public static function getBackends() { $sortFunc = function($a, $b) { return strcasecmp($a['backend'], $b['backend']); }; + $backEnds = array(); + foreach (OC_Mount_Config::$backends as $class => $backend) { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { if (!method_exists($class, 'checkDependencies')) { - \OCP\Util::writeLog('files_external', "Backend class $class has dependencies but doesn't provide method checkDependencies()", \OCP\Util::DEBUG); + \OCP\Util::writeLog('files_external', + "Backend class $class has dependencies but doesn't provide method checkDependencies()", + \OCP\Util::DEBUG); continue; } elseif ($class::checkDependencies() !== true) { continue; } } - $backends[$class] = $backend; + $backEnds[$class] = $backend; } - uasort($backends, $sortFunc); + uasort($backEnds, $sortFunc); - return $backends; + return $backEnds; } /** @@ -185,19 +206,19 @@ class OC_Mount_Config { */ public static function getPersonalBackends() { - $backends = self::getBackends(); + $backEnds = self::getBackends(); // Remove local storage and other disabled storages - unset($backends['\OC\Files\Storage\Local']); + unset($backEnds['\OC\Files\Storage\Local']); - $allowed_backends = explode(',', OCP\Config::getAppValue('files_external', 'user_mounting_backends', '')); - foreach ($backends as $backend => $null) { - if (!in_array($backend, $allowed_backends)) { - unset($backends[$backend]); + $allowedBackEnds = explode(',', OCP\Config::getAppValue('files_external', 'user_mounting_backends', '')); + foreach ($backEnds as $backend => $null) { + if (!in_array($backend, $allowedBackEnds)) { + unset($backEnds[$backend]); } } - return $backends; + return $backEnds; } /** @@ -280,7 +301,7 @@ class OC_Mount_Config { */ public static function getPersonalMountPoints() { $mountPoints = self::readData(true); - $backends = self::getBackends(); + $backEnds = self::getBackends(); $uid = OCP\User::getUser(); $personal = array(); if (isset($mountPoints[self::MOUNT_TYPE_USER][$uid])) { @@ -294,7 +315,7 @@ class OC_Mount_Config { 'class' => $mount['class'], // Remove '/uid/files/' from mount point 'mountpoint' => substr($mountPoint, strlen($uid) + 8), - 'backend' => $backends[$mount['class']]['backend'], + 'backend' => $backEnds[$mount['class']]['backend'], 'options' => $mount['options'], 'status' => self::getBackendStatus($mount['class'], $mount['options'], true) ); @@ -529,6 +550,9 @@ class OC_Mount_Config { if (isset($backend['has_dependencies']) and $backend['has_dependencies'] === true) { $result = $class::checkDependencies(); if ($result !== true) { + if (!is_array($result)) { + $result = array($result); + } foreach ($result as $key => $value) { if (is_numeric($key)) { OC_Mount_Config::addDependency($dependencies, $value, $backend['backend']); diff --git a/apps/files_external/tests/dynamicmountconfig.php b/apps/files_external/tests/dynamicmountconfig.php new file mode 100644 index 0000000000..81a31e14c6 --- /dev/null +++ b/apps/files_external/tests/dynamicmountconfig.php @@ -0,0 +1,103 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see . + * + */ + +require_once __DIR__ . '/../../../lib/base.php'; + +require __DIR__ . '/../lib/config.php'; + +/** + * Class Test_Mount_Config_Dummy_Backend + */ +class Test_Mount_Config_Dummy_Backend { + public static $checkDependencies = true; + + public static function checkDependencies() { + return self::$checkDependencies; + } +} + +/** + * Class Test_Dynamic_Mount_Config + */ +class Test_Dynamic_Mount_Config extends \PHPUnit_Framework_TestCase { + + private $backup; + + public function testRegistration() { + + // second registration shall return false + $result = OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Backend', array( + 'backend' => 'Test Dummy', + 'configuration' => array(), + 'has_dependencies' => true)); + + $this->assertTrue($result); + } + + public function testDependencyGetBackend() { + + // is the backend listed? + Test_Mount_Config_Dummy_Backend::$checkDependencies = true; + $backEnds = OC_Mount_Config::getBackends(); + $this->assertArrayHasKey('Test_Mount_Config_Dummy_Backend', $backEnds); + + // backend shall not be listed + Test_Mount_Config_Dummy_Backend::$checkDependencies = false; + + $backEnds = OC_Mount_Config::getBackends(); + $this->assertArrayNotHasKey('Test_Mount_Config_Dummy_Backend', $backEnds); + + } + + public function testCheckDependencies() { + + Test_Mount_Config_Dummy_Backend::$checkDependencies = true; + $message = OC_Mount_Config::checkDependencies(); + $this->assertEmpty($message); + + // backend shall not be listed + Test_Mount_Config_Dummy_Backend::$checkDependencies = array('dummy'); + + $message = OC_Mount_Config::checkDependencies(); + $this->assertEquals('
Note: "dummy" is not installed. Mounting of Test Dummy is not possible. Please ask your system administrator to install it.', + $message); + + } + + protected function setUp() { + + $this->backup = OC_Mount_Config::setUp(); + + // register dummy backend + $result = OC_Mount_Config::registerBackend('Test_Mount_Config_Dummy_Backend', array( + 'backend' => 'Test Dummy', + 'configuration' => array(), + 'has_dependencies' => true)); + + $this->assertTrue($result); + } + + protected function tearDown() + { + OC_Mount_Config::setUp($this->backup); + } +}