allow overwriting the appmanager in oc_util by subclassing

This commit is contained in:
Robin Appelman 2015-02-18 14:24:50 +01:00
parent e672f8cc8f
commit 5542fafd36
2 changed files with 32 additions and 27 deletions

View File

@ -11,6 +11,10 @@ class OC_Util {
private static $rootMounted = false; private static $rootMounted = false;
private static $fsSetup = false; private static $fsSetup = false;
protected static function getAppManager() {
return \OC::$server->getAppManager();
}
private static function initLocalStorageRootFS() { private static function initLocalStorageRootFS() {
// mount local file backend as root // mount local file backend as root
$configDataDirectory = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data"); $configDataDirectory = OC_Config::getValue("datadirectory", OC::$SERVERROOT . "/data");
@ -926,7 +930,7 @@ class OC_Util {
// find the first app that is enabled for the current user // find the first app that is enabled for the current user
foreach ($defaultApps as $defaultApp) { foreach ($defaultApps as $defaultApp) {
$defaultApp = OC_App::cleanAppId(strip_tags($defaultApp)); $defaultApp = OC_App::cleanAppId(strip_tags($defaultApp));
if (OC_App::isEnabled($defaultApp)) { if (static::getAppManager()->isEnabledForUser($defaultApp)) {
$appId = $defaultApp; $appId = $defaultApp;
break; break;
} }

View File

@ -1,11 +1,11 @@
<?php <?php
/** /**
* Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch> * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
* This file is licensed under the Affero General Public License version 3 or * This file is licensed under the Affero General Public License version 3 or
* later. * later.
* See the COPYING-README file. * See the COPYING-README file.
*/ */
class Test_Util extends \Test\TestCase { class Test_Util extends \Test\TestCase {
public function testGetVersion() { public function testGetVersion() {
$version = \OC_Util::getVersion(); $version = \OC_Util::getVersion();
@ -105,7 +105,7 @@ class Test_Util extends \Test\TestCase {
$this->assertEquals('This is a good string without HTML.', $result); $this->assertEquals('This is a good string without HTML.', $result);
} }
function testEncodePath(){ function testEncodePath() {
$component = '/§#@test%&^ä/-child'; $component = '/§#@test%&^ä/-child';
$result = OC_Util::encodePath($component); $result = OC_Util::encodePath($component);
$this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result); $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
@ -210,14 +210,12 @@ class Test_Util extends \Test\TestCase {
/** /**
* @dataProvider baseNameProvider * @dataProvider baseNameProvider
*/ */
public function testBaseName($expected, $file) public function testBaseName($expected, $file) {
{
$base = \OC_Util::basename($file); $base = \OC_Util::basename($file);
$this->assertEquals($expected, $base); $this->assertEquals($expected, $base);
} }
public function baseNameProvider() public function baseNameProvider() {
{
return array( return array(
array('public_html', '/home/user/public_html/'), array('public_html', '/home/user/public_html/'),
array('public_html', '/home/user/public_html'), array('public_html', '/home/user/public_html'),
@ -288,11 +286,11 @@ class Test_Util extends \Test\TestCase {
\OC_User::createUser($uid, "passwd"); \OC_User::createUser($uid, "passwd");
foreach($groups as $group) { foreach ($groups as $group) {
\OC_Group::createGroup($group); \OC_Group::createGroup($group);
} }
foreach($membership as $group) { foreach ($membership as $group) {
\OC_Group::addToGroup($uid, $group); \OC_Group::addToGroup($uid, $group);
} }
@ -308,7 +306,7 @@ class Test_Util extends \Test\TestCase {
\OC_User::deleteUser($uid); \OC_User::deleteUser($uid);
\OC_User::setUserId(''); \OC_User::setUserId('');
foreach($groups as $group) { foreach ($groups as $group) {
\OC_Group::deleteGroup($group); \OC_Group::deleteGroup($group);
} }
@ -317,7 +315,7 @@ class Test_Util extends \Test\TestCase {
} }
public function dataProviderForTestIsSharingDisabledForUser() { public function dataProviderForTestIsSharingDisabledForUser() {
return array( return array(
// existing groups, groups the user belong to, groups excluded from sharing, expected result // existing groups, groups the user belong to, groups excluded from sharing, expected result
array(array('g1', 'g2', 'g3'), array(), array('g1'), false), array(array('g1', 'g2', 'g3'), array(), array('g1'), false),
@ -327,8 +325,8 @@ class Test_Util extends \Test\TestCase {
array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1'), false), array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1'), false),
array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2'), true), array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2'), true),
array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true), array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true),
); );
} }
/** /**
* Test default apps * Test default apps
@ -341,15 +339,21 @@ class Test_Util extends \Test\TestCase {
$oldWebRoot = \OC::$WEBROOT; $oldWebRoot = \OC::$WEBROOT;
\OC::$WEBROOT = ''; \OC::$WEBROOT = '';
Dummy_OC_App::setEnabledApps($enabledApps); $appManager = $this->getMock('\OCP\App\IAppManager');
$appManager->expects($this->any())
->method('isEnabledForUser')
->will($this->returnCallback(function($appId) use ($enabledApps){
return in_array($appId, $enabledApps);
}));
Dummy_OC_Util::$appManager = $appManager;
// need to set a user id to make sure enabled apps are read from cache // need to set a user id to make sure enabled apps are read from cache
\OC_User::setUserId($this->getUniqueID()); \OC_User::setUserId($this->getUniqueID());
\OCP\Config::setSystemValue('defaultapp', $defaultAppConfig); \OCP\Config::setSystemValue('defaultapp', $defaultAppConfig);
$this->assertEquals('http://localhost/' . $expectedPath, \OC_Util::getDefaultPageUrl()); $this->assertEquals('http://localhost/' . $expectedPath, Dummy_OC_Util::getDefaultPageUrl());
// restore old state // restore old state
\OC::$WEBROOT = $oldWebRoot; \OC::$WEBROOT = $oldWebRoot;
Dummy_OC_App::restore();
\OCP\Config::setSystemValue('defaultapp', $oldDefaultApps); \OCP\Config::setSystemValue('defaultapp', $oldDefaultApps);
\OC_User::setUserId(null); \OC_User::setUserId(null);
} }
@ -405,18 +409,15 @@ class Test_Util extends \Test\TestCase {
} }
/** /**
* Dummy OC Apps class to make it possible to override * Dummy OC Util class to make it possible to override the app manager
* enabled apps
*/ */
class Dummy_OC_App extends OC_App { class Dummy_OC_Util extends OC_Util {
private static $enabledAppsCacheBackup; /**
* @var \OCP\App\IAppManager
*/
public static $appManager;
public static function setEnabledApps($enabledApps) { protected static function getAppManager() {
self::$enabledAppsCacheBackup = self::$enabledAppsCache; return self::$appManager;
self::$enabledAppsCache = $enabledApps;
}
public static function restore() {
self::$enabledAppsCache = self::$enabledAppsCacheBackup;
} }
} }