2014-11-07 16:26:12 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\App;
|
|
|
|
|
|
|
|
use OC\Group\Group;
|
|
|
|
use OC\User\User;
|
|
|
|
|
|
|
|
class Manager extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
|
|
* @return \OCP\IAppConfig | \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
|
|
|
protected function getAppConfig() {
|
|
|
|
$appConfig = array();
|
|
|
|
$config = $this->getMockBuilder('\OCP\IAppConfig')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getValue')
|
|
|
|
->will($this->returnCallback(function ($app, $key, $default) use (&$appConfig) {
|
|
|
|
return (isset($appConfig[$app]) and isset($appConfig[$app][$key])) ? $appConfig[$app][$key] : $default;
|
|
|
|
}));
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('setValue')
|
|
|
|
->will($this->returnCallback(function ($app, $key, $value) use (&$appConfig) {
|
|
|
|
if (!isset($appConfig[$app])) {
|
|
|
|
$appConfig[$app] = array();
|
|
|
|
}
|
|
|
|
$appConfig[$app][$key] = $value;
|
|
|
|
}));
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getValues')
|
|
|
|
->will($this->returnCallback(function ($app, $key) use (&$appConfig) {
|
|
|
|
if ($app) {
|
|
|
|
return $appConfig[$app];
|
|
|
|
} else {
|
|
|
|
$values = array();
|
|
|
|
foreach ($appConfig as $app => $appData) {
|
|
|
|
if (isset($appData[$key])) {
|
|
|
|
$values[$app] = $appData[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
/** @var \OCP\IUserSession */
|
|
|
|
protected $userSession;
|
|
|
|
|
|
|
|
/** @var \OCP\IGroupManager */
|
|
|
|
protected $groupManager;
|
|
|
|
|
|
|
|
/** @var \OCP\IAppConfig */
|
|
|
|
protected $appConfig;
|
|
|
|
|
|
|
|
/** @var \OCP\ICache */
|
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
/** @var \OCP\ICacheFactory */
|
|
|
|
protected $cacheFactory;
|
|
|
|
|
|
|
|
/** @var \OCP\App\IAppManager */
|
|
|
|
protected $manager;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->userSession = $this->getMock('\OCP\IUserSession');
|
|
|
|
$this->groupManager = $this->getMock('\OCP\IGroupManager');
|
|
|
|
$this->appConfig = $this->getAppConfig();
|
|
|
|
$this->cacheFactory = $this->getMock('\OCP\ICacheFactory');
|
|
|
|
$this->cache = $this->getMock('\OCP\ICache');
|
|
|
|
$this->cacheFactory->expects($this->any())
|
|
|
|
->method('create')
|
|
|
|
->with('settings')
|
|
|
|
->willReturn($this->cache);
|
|
|
|
$this->manager = new \OC\App\AppManager($this->userSession, $this->appConfig, $this->groupManager, $this->cacheFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function expectClearCache() {
|
|
|
|
$this->cache->expects($this->once())
|
|
|
|
->method('clear')
|
|
|
|
->with('listApps');
|
|
|
|
}
|
|
|
|
|
2014-11-07 16:26:12 +03:00
|
|
|
public function testEnableApp() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->expectClearCache();
|
|
|
|
$this->manager->enableApp('test');
|
|
|
|
$this->assertEquals('yes', $this->appConfig->getValue('test', 'enabled', 'no'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisableApp() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->expectClearCache();
|
|
|
|
$this->manager->disableApp('test');
|
|
|
|
$this->assertEquals('no', $this->appConfig->getValue('test', 'enabled', 'no'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnableAppForGroups() {
|
|
|
|
$groups = array(
|
|
|
|
new Group('group1', array(), null),
|
|
|
|
new Group('group2', array(), null)
|
|
|
|
);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->expectClearCache();
|
|
|
|
$this->manager->enableAppForGroups('test', $groups);
|
|
|
|
$this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsInstalledEnabled() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', 'yes');
|
|
|
|
$this->assertTrue($this->manager->isInstalled('test'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsInstalledDisabled() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', 'no');
|
|
|
|
$this->assertFalse($this->manager->isInstalled('test'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsInstalledEnabledForGroups() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', '["foo"]');
|
|
|
|
$this->assertTrue($this->manager->isInstalled('test'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserEnabled() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', 'yes');
|
2014-11-07 16:26:12 +03:00
|
|
|
$user = new User('user1', null);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->assertTrue($this->manager->isEnabledForUser('test', $user));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserDisabled() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', 'no');
|
2014-11-07 16:26:12 +03:00
|
|
|
$user = new User('user1', null);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->assertFalse($this->manager->isEnabledForUser('test', $user));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserEnabledForGroup() {
|
|
|
|
$user = new User('user1', null);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-11-07 16:26:12 +03:00
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(array('foo', 'bar')));
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', '["foo"]');
|
|
|
|
$this->assertTrue($this->manager->isEnabledForUser('test', $user));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserDisabledForGroup() {
|
|
|
|
$user = new User('user1', null);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-11-07 16:26:12 +03:00
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(array('bar')));
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', '["foo"]');
|
|
|
|
$this->assertFalse($this->manager->isEnabledForUser('test', $user));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserLoggedOut() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', '["foo"]');
|
|
|
|
$this->assertFalse($this->manager->IsEnabledForUser('test'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsEnabledForUserLoggedIn() {
|
|
|
|
$user = new User('user1', null);
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->userSession->expects($this->once())
|
2014-11-07 16:26:12 +03:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->groupManager->expects($this->once())
|
2014-11-07 16:26:12 +03:00
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(array('foo', 'bar')));
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test', 'enabled', '["foo"]');
|
|
|
|
$this->assertTrue($this->manager->isEnabledForUser('test'));
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|
2015-02-02 16:47:29 +03:00
|
|
|
|
|
|
|
public function testGetInstalledApps() {
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test1', 'enabled', 'yes');
|
|
|
|
$this->appConfig->setValue('test2', 'enabled', 'no');
|
|
|
|
$this->appConfig->setValue('test3', 'enabled', '["foo"]');
|
|
|
|
$this->assertEquals(['test1', 'test3'], $this->manager->getInstalledApps());
|
2015-02-02 16:47:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAppsForUser() {
|
|
|
|
$user = new User('user1', null);
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->groupManager->expects($this->any())
|
2015-02-02 16:47:29 +03:00
|
|
|
->method('getUserGroupIds')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(array('foo', 'bar')));
|
|
|
|
|
2015-04-01 18:19:44 +03:00
|
|
|
$this->appConfig->setValue('test1', 'enabled', 'yes');
|
|
|
|
$this->appConfig->setValue('test2', 'enabled', 'no');
|
|
|
|
$this->appConfig->setValue('test3', 'enabled', '["foo"]');
|
|
|
|
$this->appConfig->setValue('test4', 'enabled', '["asd"]');
|
|
|
|
$this->assertEquals(['test1', 'test3'], $this->manager->getEnabledAppsForUser($user));
|
2015-02-02 16:47:29 +03:00
|
|
|
}
|
2014-11-07 16:26:12 +03:00
|
|
|
}
|