2015-03-16 18:46:17 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Joas Schilling
|
|
|
|
* @copyright 2015 Joas Schilling nickvergessen@owncloud.com
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
use OCP\IConfig;
|
2016-12-08 19:43:46 +03:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\L10N\IFactory;
|
2018-07-13 15:47:00 +03:00
|
|
|
use OC\App\AppManager;
|
|
|
|
use OC\Group\Manager;
|
|
|
|
use OC\NavigationManager;
|
|
|
|
use OC\SubAdmin;
|
2015-03-16 18:46:17 +03:00
|
|
|
|
|
|
|
class NavigationManagerTest extends TestCase {
|
2017-03-26 22:15:25 +03:00
|
|
|
/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $appManager;
|
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $urlGenerator;
|
|
|
|
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $l10nFac;
|
|
|
|
/** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $userSession;
|
|
|
|
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $groupManager;
|
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
|
|
|
|
2015-03-16 18:46:17 +03:00
|
|
|
/** @var \OC\NavigationManager */
|
|
|
|
protected $navigationManager;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->appManager = $this->createMock(AppManager::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->l10nFac = $this->createMock(IFactory::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->groupManager = $this->createMock(Manager::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager = new NavigationManager(
|
|
|
|
$this->appManager,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->l10nFac,
|
|
|
|
$this->userSession,
|
|
|
|
$this->groupManager,
|
|
|
|
$this->config
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->navigationManager->clear(false);
|
2015-03-16 18:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addArrayData() {
|
|
|
|
return [
|
|
|
|
[
|
2018-07-13 15:47:00 +03:00
|
|
|
'entry id' => [
|
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
|
|
|
'icon' => 'optional',
|
|
|
|
'href' => 'url',
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
2015-03-16 18:46:17 +03:00
|
|
|
],
|
2018-07-13 15:47:00 +03:00
|
|
|
'entry id2' => [
|
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
|
|
|
'icon' => 'optional',
|
|
|
|
'href' => 'url',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
|
|
|
]
|
2015-03-16 18:46:17 +03:00
|
|
|
],
|
|
|
|
[
|
2018-07-13 15:47:00 +03:00
|
|
|
'entry id' => [
|
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
2015-03-16 18:46:17 +03:00
|
|
|
//'icon' => 'optional',
|
2018-07-13 15:47:00 +03:00
|
|
|
'href' => 'url',
|
|
|
|
'active' => true
|
2015-03-16 18:46:17 +03:00
|
|
|
],
|
2018-07-13 15:47:00 +03:00
|
|
|
'entry id2' => [
|
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
|
|
|
'icon' => '',
|
|
|
|
'href' => 'url',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'link',
|
|
|
|
'classes' => ''
|
|
|
|
]
|
|
|
|
]
|
2015-03-16 18:46:17 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider addArrayData
|
|
|
|
*
|
|
|
|
* @param array $entry
|
|
|
|
* @param array $expectedEntry
|
|
|
|
*/
|
|
|
|
public function testAddArray(array $entry, array $expectedEntry) {
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
|
2015-03-16 18:46:17 +03:00
|
|
|
$this->navigationManager->add($entry);
|
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$navigationEntries = $this->navigationManager->getAll('all');
|
|
|
|
$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->assertEquals($expectedEntry, $navigationEntries['entry id']);
|
2015-03-16 18:46:17 +03:00
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager->clear(false);
|
|
|
|
$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
|
2015-03-16 18:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider addArrayData
|
|
|
|
*
|
|
|
|
* @param array $entry
|
|
|
|
* @param array $expectedEntry
|
|
|
|
*/
|
|
|
|
public function testAddClosure(array $entry, array $expectedEntry) {
|
|
|
|
global $testAddClosureNumberOfCalls;
|
|
|
|
$testAddClosureNumberOfCalls = 0;
|
|
|
|
|
|
|
|
$this->navigationManager->add(function () use ($entry) {
|
|
|
|
global $testAddClosureNumberOfCalls;
|
|
|
|
$testAddClosureNumberOfCalls++;
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
|
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$navigationEntries = $this->navigationManager->getAll('all');
|
2015-03-16 18:46:17 +03:00
|
|
|
$this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->assertEquals($expectedEntry, $navigationEntries['entry id']);
|
2015-03-16 18:46:17 +03:00
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$navigationEntries = $this->navigationManager->getAll('all');
|
2015-03-16 18:46:17 +03:00
|
|
|
$this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->assertEquals($expectedEntry, $navigationEntries['entry id']);
|
2015-03-16 18:46:17 +03:00
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager->clear(false);
|
|
|
|
$this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
|
2015-03-16 18:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddArrayClearGetAll() {
|
|
|
|
$entry = [
|
2018-07-13 15:47:00 +03:00
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
|
|
|
'icon' => 'optional',
|
|
|
|
'href' => 'url'
|
2015-03-16 18:46:17 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
|
|
|
|
$this->navigationManager->add($entry);
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager->clear(false);
|
2015-03-16 18:46:17 +03:00
|
|
|
$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddClosureClearGetAll() {
|
|
|
|
$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
|
|
|
|
|
|
|
|
$entry = [
|
2018-07-13 15:47:00 +03:00
|
|
|
'id' => 'entry id',
|
|
|
|
'name' => 'link text',
|
|
|
|
'order' => 1,
|
|
|
|
'icon' => 'optional',
|
|
|
|
'href' => 'url'
|
2015-03-16 18:46:17 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
global $testAddClosureNumberOfCalls;
|
|
|
|
$testAddClosureNumberOfCalls = 0;
|
|
|
|
|
|
|
|
$this->navigationManager->add(function () use ($entry) {
|
|
|
|
global $testAddClosureNumberOfCalls;
|
|
|
|
$testAddClosureNumberOfCalls++;
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager->clear(false);
|
2015-03-16 18:46:17 +03:00
|
|
|
$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
|
|
|
|
$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
|
|
|
|
$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
|
|
|
|
}
|
2016-12-08 19:43:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesNavigationConfig
|
|
|
|
*/
|
2017-03-26 22:15:25 +03:00
|
|
|
public function testWithAppManager($expected, $navigation, $isAdmin = false) {
|
2016-12-08 19:43:46 +03:00
|
|
|
|
|
|
|
$l = $this->createMock(IL10N::class);
|
2018-07-13 15:47:00 +03:00
|
|
|
$l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
|
2016-12-08 19:43:46 +03:00
|
|
|
return vsprintf($text, $parameters);
|
|
|
|
});
|
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
|
2017-07-05 14:52:51 +03:00
|
|
|
$this->l10nFac->expects($this->any())->method('get')->willReturn($l);
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
|
2016-12-08 19:43:46 +03:00
|
|
|
return "/apps/$appName/img/$file";
|
|
|
|
});
|
2018-07-13 15:47:00 +03:00
|
|
|
$this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function () {
|
|
|
|
return '/apps/test/';
|
2016-12-08 19:43:46 +03:00
|
|
|
});
|
2017-08-18 16:32:40 +03:00
|
|
|
$this->urlGenerator
|
2019-01-23 12:21:14 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.logout')
|
|
|
|
->willReturn('https://example.com/logout');
|
2016-12-08 19:43:46 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->expects($this->any())->method('getUID')->willReturn('user001');
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->userSession->expects($this->any())->method('getUser')->willReturn($user);
|
2017-07-05 14:52:51 +03:00
|
|
|
$this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
|
|
|
|
$this->appManager->expects($this->once())
|
2018-07-13 15:47:00 +03:00
|
|
|
->method('getEnabledAppsForUser')
|
|
|
|
->with($user)
|
|
|
|
->willReturn(['test']);
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
|
2017-07-05 14:52:51 +03:00
|
|
|
$subadmin = $this->createMock(SubAdmin::class);
|
|
|
|
$subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
|
|
|
|
$this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
|
2016-12-08 19:43:46 +03:00
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->navigationManager->clear();
|
|
|
|
$entries = $this->navigationManager->getAll('all');
|
2016-12-08 19:43:46 +03:00
|
|
|
$this->assertEquals($expected, $entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesNavigationConfig() {
|
2017-07-05 14:52:51 +03:00
|
|
|
$apps = [
|
2018-07-13 15:47:00 +03:00
|
|
|
'core_apps' => [
|
|
|
|
'id' => 'core_apps',
|
|
|
|
'order' => 3,
|
|
|
|
'href' => '/apps/test/',
|
|
|
|
'icon' => '/apps/settings/img/apps.svg',
|
|
|
|
'name' => 'Apps',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
2017-07-05 14:52:51 +03:00
|
|
|
]
|
|
|
|
];
|
|
|
|
$defaults = [
|
2018-07-13 15:47:00 +03:00
|
|
|
'settings' => [
|
|
|
|
'id' => 'settings',
|
|
|
|
'order' => 1,
|
|
|
|
'href' => '/apps/test/',
|
|
|
|
'icon' => '/apps/settings/img/admin.svg',
|
|
|
|
'name' => 'Settings',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
2017-07-05 14:52:51 +03:00
|
|
|
],
|
2018-07-13 15:47:00 +03:00
|
|
|
'logout' => [
|
|
|
|
'id' => 'logout',
|
|
|
|
'order' => 99999,
|
2019-01-23 18:16:31 +03:00
|
|
|
'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
|
2018-07-13 15:47:00 +03:00
|
|
|
'icon' => '/apps/core/img/actions/logout.svg',
|
|
|
|
'name' => 'Log out',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
|
|
|
]
|
2017-07-05 14:52:51 +03:00
|
|
|
];
|
2018-07-13 15:47:00 +03:00
|
|
|
|
2016-12-08 19:43:46 +03:00
|
|
|
return [
|
2018-07-13 15:47:00 +03:00
|
|
|
'minimalistic' => [
|
|
|
|
array_merge(
|
|
|
|
['settings' => $defaults['settings']],
|
|
|
|
['test' => [
|
|
|
|
'id' => 'test',
|
|
|
|
'order' => 100,
|
|
|
|
'href' => '/apps/test/',
|
|
|
|
'icon' => '/apps/test/img/app.svg',
|
|
|
|
'name' => 'Test',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'link',
|
|
|
|
'classes' => ''
|
|
|
|
]],
|
|
|
|
['logout' => $defaults['logout']]
|
|
|
|
),
|
|
|
|
['navigations' => [
|
|
|
|
['route' => 'test.page.index', 'name' => 'Test']
|
|
|
|
]]
|
|
|
|
],
|
|
|
|
'minimalistic-settings' => [
|
|
|
|
array_merge(
|
|
|
|
['settings' => $defaults['settings']],
|
|
|
|
['test' => [
|
|
|
|
'id' => 'test',
|
|
|
|
'order' => 100,
|
|
|
|
'href' => '/apps/test/',
|
|
|
|
'icon' => '/apps/test/img/app.svg',
|
|
|
|
'name' => 'Test',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'settings',
|
|
|
|
'classes' => ''
|
|
|
|
]],
|
|
|
|
['logout' => $defaults['logout']]
|
|
|
|
),
|
|
|
|
['navigations' => [
|
|
|
|
['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
|
|
|
|
]
|
|
|
|
]],
|
|
|
|
'admin' => [
|
|
|
|
array_merge(
|
|
|
|
['settings' => $defaults['settings']],
|
|
|
|
$apps,
|
|
|
|
['test' => [
|
|
|
|
'id' => 'test',
|
|
|
|
'order' => 100,
|
|
|
|
'href' => '/apps/test/',
|
|
|
|
'icon' => '/apps/test/img/app.svg',
|
|
|
|
'name' => 'Test',
|
|
|
|
'active' => false,
|
|
|
|
'type' => 'link',
|
|
|
|
'classes' => ''
|
|
|
|
]],
|
|
|
|
['logout' => $defaults['logout']]
|
|
|
|
),
|
|
|
|
['navigations' => [
|
|
|
|
['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
|
|
|
|
]],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
'no name' => [
|
|
|
|
array_merge(
|
|
|
|
['settings' => $defaults['settings']],
|
|
|
|
$apps,
|
|
|
|
['logout' => $defaults['logout']]
|
|
|
|
),
|
|
|
|
['navigations' => [
|
|
|
|
['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
|
|
|
|
]],
|
|
|
|
true
|
|
|
|
],
|
|
|
|
'no admin' => [
|
|
|
|
$defaults,
|
|
|
|
['navigations' => [[
|
|
|
|
'@attributes' => ['role' => 'admin'],
|
|
|
|
'route' => 'test.page.index',
|
|
|
|
'name' => 'Test'
|
|
|
|
]]]
|
|
|
|
]
|
2016-12-08 19:43:46 +03:00
|
|
|
];
|
|
|
|
}
|
2015-03-16 18:46:17 +03:00
|
|
|
}
|