2015-03-16 11:46:17 -04:00
< ? php
/**
2024-05-10 09:09:14 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2015-03-16 11:46:17 -04:00
*/
namespace Test ;
2019-11-22 14:52:10 -05:00
use OC\App\AppManager ;
use OC\Group\Manager ;
use OC\NavigationManager ;
use OC\SubAdmin ;
2024-12-17 14:11:19 -05:00
use OCP\EventDispatcher\IEventDispatcher ;
2017-03-26 15:15:25 -04:00
use OCP\IConfig ;
2016-12-08 11:43:46 -05:00
use OCP\IGroupManager ;
use OCP\IL10N ;
use OCP\IURLGenerator ;
use OCP\IUser ;
use OCP\IUserSession ;
use OCP\L10N\IFactory ;
2024-12-17 14:11:19 -05:00
use OCP\Navigation\Events\LoadAdditionalEntriesEvent ;
use PHPUnit\Framework\MockObject\MockObject ;
2024-08-27 06:13:04 -04:00
use Psr\Log\LoggerInterface ;
2015-03-16 11:46:17 -04:00
class NavigationManagerTest extends TestCase {
2020-08-11 15:32:18 -04:00
/** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $appManager ;
2020-08-11 15:32:18 -04:00
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $urlGenerator ;
2020-08-11 15:32:18 -04:00
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $l10nFac ;
2020-08-11 15:32:18 -04:00
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $userSession ;
2020-08-11 15:32:18 -04:00
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $groupManager ;
2020-08-11 15:32:18 -04:00
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
2017-03-26 15:15:25 -04:00
protected $config ;
2024-12-17 14:11:19 -05:00
protected IEVentDispatcher | MockObject $dispatcher ;
2015-03-16 11:46:17 -04:00
/** @var \OC\NavigationManager */
protected $navigationManager ;
2024-08-27 06:13:04 -04:00
protected LoggerInterface $logger ;
2015-03-16 11:46:17 -04:00
2019-11-21 10:40:38 -05:00
protected function setUp () : void {
2015-03-16 11:46:17 -04:00
parent :: setUp ();
2020-10-05 09:12:57 -04: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 );
2024-08-27 06:13:04 -04:00
$this -> logger = $this -> createMock ( LoggerInterface :: class );
2024-12-17 14:11:19 -05:00
$this -> dispatcher = $this -> createMock ( IEventDispatcher :: class );
2017-03-26 15:15:25 -04:00
$this -> navigationManager = new NavigationManager (
$this -> appManager ,
$this -> urlGenerator ,
$this -> l10nFac ,
$this -> userSession ,
$this -> groupManager ,
2024-08-27 06:13:04 -04:00
$this -> config ,
$this -> logger ,
2024-12-17 14:11:19 -05:00
$this -> dispatcher ,
2017-03-26 15:15:25 -04:00
);
$this -> navigationManager -> clear ( false );
2015-03-16 11:46:17 -04:00
}
public function addArrayData () {
return [
[
2020-10-05 09:12:57 -04:00
'entry id' => [
'id' => 'entry id' ,
'name' => 'link text' ,
'order' => 1 ,
'icon' => 'optional' ,
'href' => 'url' ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
'unread' => 0
2015-03-16 11:46:17 -04:00
],
2018-07-13 08:47:00 -04:00
'entry id2' => [
2020-10-05 09:12:57 -04:00
'id' => 'entry id' ,
'name' => 'link text' ,
'order' => 1 ,
'icon' => 'optional' ,
'href' => 'url' ,
'active' => false ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
'unread' => 0
2018-07-13 08:47:00 -04:00
]
2015-03-16 11:46:17 -04:00
],
[
2020-10-05 09:12:57 -04:00
'entry id' => [
'id' => 'entry id' ,
'name' => 'link text' ,
'order' => 1 ,
2015-03-16 11:46:17 -04:00
//'icon' => 'optional',
2020-10-05 09:12:57 -04:00
'href' => 'url' ,
2021-05-11 02:22:51 -04:00
'active' => true ,
2023-10-10 08:24:34 -04:00
'unread' => 0 ,
2015-03-16 11:46:17 -04:00
],
2018-07-13 08:47:00 -04:00
'entry id2' => [
2020-10-05 09:12:57 -04:00
'id' => 'entry id' ,
'name' => 'link text' ,
'order' => 1 ,
'icon' => '' ,
'href' => 'url' ,
'active' => false ,
'type' => 'link' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
2023-10-10 08:24:34 -04:00
'unread' => 0 ,
'default' => true ,
2018-07-13 08:47:00 -04:00
]
]
2015-03-16 11:46:17 -04:00
];
}
/**
* @ dataProvider addArrayData
*
* @ param array $entry
* @ param array $expectedEntry
*/
2024-09-15 16:32:31 -04:00
public function testAddArray ( array $entry , array $expectedEntry ) : void {
2017-03-26 15:15:25 -04:00
$this -> assertEmpty ( $this -> navigationManager -> getAll ( 'all' ), 'Expected no navigation entry exists' );
2015-03-16 11:46:17 -04:00
$this -> navigationManager -> add ( $entry );
2017-03-26 15:15:25 -04:00
$navigationEntries = $this -> navigationManager -> getAll ( 'all' );
$this -> assertCount ( 1 , $navigationEntries , 'Expected that 1 navigation entry exists' );
2018-07-13 08:47:00 -04:00
$this -> assertEquals ( $expectedEntry , $navigationEntries [ 'entry id' ]);
2015-03-16 11:46:17 -04:00
2017-03-26 15:15:25 -04:00
$this -> navigationManager -> clear ( false );
$this -> assertEmpty ( $this -> navigationManager -> getAll ( 'all' ), 'Expected no navigation entry exists after clear()' );
2015-03-16 11:46:17 -04:00
}
/**
* @ dataProvider addArrayData
*
* @ param array $entry
* @ param array $expectedEntry
*/
2024-09-15 16:32:31 -04:00
public function testAddClosure ( array $entry , array $expectedEntry ) : void {
2015-03-16 11:46:17 -04: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 15:15:25 -04:00
$navigationEntries = $this -> navigationManager -> getAll ( 'all' );
2015-03-16 11:46:17 -04:00
$this -> assertEquals ( 1 , $testAddClosureNumberOfCalls , 'Expected that the closure is called by getAll()' );
2017-03-26 15:15:25 -04:00
$this -> assertCount ( 1 , $navigationEntries , 'Expected that 1 navigation entry exists' );
2018-07-13 08:47:00 -04:00
$this -> assertEquals ( $expectedEntry , $navigationEntries [ 'entry id' ]);
2015-03-16 11:46:17 -04:00
2017-03-26 15:15:25 -04:00
$navigationEntries = $this -> navigationManager -> getAll ( 'all' );
2015-03-16 11:46:17 -04:00
$this -> assertEquals ( 1 , $testAddClosureNumberOfCalls , 'Expected that the closure is only called once for getAll()' );
2017-03-26 15:15:25 -04:00
$this -> assertCount ( 1 , $navigationEntries , 'Expected that 1 navigation entry exists' );
2018-07-13 08:47:00 -04:00
$this -> assertEquals ( $expectedEntry , $navigationEntries [ 'entry id' ]);
2015-03-16 11:46:17 -04:00
2017-03-26 15:15:25 -04:00
$this -> navigationManager -> clear ( false );
$this -> assertEmpty ( $this -> navigationManager -> getAll ( 'all' ), 'Expected no navigation entry exists after clear()' );
2015-03-16 11:46:17 -04:00
}
2024-09-15 16:32:31 -04:00
public function testAddArrayClearGetAll () : void {
2015-03-16 11:46:17 -04:00
$entry = [
2020-10-05 09:12:57 -04:00
'id' => 'entry id' ,
'name' => 'link text' ,
2018-07-13 08:47:00 -04:00
'order' => 1 ,
2020-10-05 09:12:57 -04:00
'icon' => 'optional' ,
'href' => 'url'
2015-03-16 11:46:17 -04:00
];
$this -> assertEmpty ( $this -> navigationManager -> getAll (), 'Expected no navigation entry exists' );
$this -> navigationManager -> add ( $entry );
2017-03-26 15:15:25 -04:00
$this -> navigationManager -> clear ( false );
2015-03-16 11:46:17 -04:00
$this -> assertEmpty ( $this -> navigationManager -> getAll (), 'Expected no navigation entry exists after clear()' );
}
2024-09-15 16:32:31 -04:00
public function testAddClosureClearGetAll () : void {
2015-03-16 11:46:17 -04:00
$this -> assertEmpty ( $this -> navigationManager -> getAll (), 'Expected no navigation entry exists' );
$entry = [
2020-10-05 09:12:57 -04:00
'id' => 'entry id' ,
'name' => 'link text' ,
2018-07-13 08:47:00 -04:00
'order' => 1 ,
2020-10-05 09:12:57 -04:00
'icon' => 'optional' ,
'href' => 'url'
2015-03-16 11:46:17 -04: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 15:15:25 -04:00
$this -> navigationManager -> clear ( false );
2015-03-16 11:46:17 -04: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 11:43:46 -05:00
/**
* @ dataProvider providesNavigationConfig
*/
2024-09-15 16:32:31 -04:00
public function testWithAppManager ( $expected , $navigation , $isAdmin = false ) : void {
2016-12-08 11:43:46 -05:00
$l = $this -> createMock ( IL10N :: class );
2018-07-13 08:47:00 -04:00
$l -> expects ( $this -> any ()) -> method ( 't' ) -> willReturnCallback ( function ( $text , $parameters = []) {
2016-12-08 11:43:46 -05:00
return vsprintf ( $text , $parameters );
});
2023-09-26 10:21:22 -04:00
/* Return default value */
$this -> config -> method ( 'getUserValue' )
-> willReturnArgument ( 3 );
2022-09-14 05:24:11 -04:00
$this -> appManager -> expects ( $this -> any ())
2024-08-23 09:10:27 -04:00
-> method ( 'isEnabledForUser' )
-> with ( 'theming' )
-> willReturn ( true );
2024-03-06 05:13:29 -05:00
$this -> appManager -> expects ( $this -> once ())
-> method ( 'getAppInfo' )
-> with ( 'test' )
-> willReturn ( $navigation );
$this -> urlGenerator -> expects ( $this -> any ())
-> method ( 'imagePath' )
-> willReturnCallback ( function ( $appName , $file ) {
return " /apps/ $appName /img/ $file " ;
});
2022-09-14 05:24:11 -04:00
$this -> appManager -> expects ( $this -> any ())
2024-03-06 05:13:29 -05:00
-> method ( 'getAppIcon' )
-> willReturnCallback ( fn ( string $appName ) => " /apps/ $appName /img/app.svg " );
2017-07-05 07:52:51 -04:00
$this -> l10nFac -> expects ( $this -> any ()) -> method ( 'get' ) -> willReturn ( $l );
2019-05-07 16:56:44 -04:00
$this -> urlGenerator -> expects ( $this -> any ()) -> method ( 'linkToRoute' ) -> willReturnCallback ( function ( $route ) {
if ( $route === 'core.login.logout' ) {
return 'https://example.com/logout' ;
}
2018-07-13 08:47:00 -04:00
return '/apps/test/' ;
2016-12-08 11:43:46 -05:00
});
$user = $this -> createMock ( IUser :: class );
$user -> expects ( $this -> any ()) -> method ( 'getUID' ) -> willReturn ( 'user001' );
2017-03-26 15:15:25 -04:00
$this -> userSession -> expects ( $this -> any ()) -> method ( 'getUser' ) -> willReturn ( $user );
2017-07-05 07:52:51 -04:00
$this -> userSession -> expects ( $this -> any ()) -> method ( 'isLoggedIn' ) -> willReturn ( true );
2022-09-14 05:24:11 -04:00
$this -> appManager -> expects ( $this -> any ())
2024-08-23 09:10:27 -04:00
-> method ( 'getEnabledAppsForUser' )
-> with ( $user )
-> willReturn ([ 'test' ]);
2017-03-26 15:15:25 -04:00
$this -> groupManager -> expects ( $this -> any ()) -> method ( 'isAdmin' ) -> willReturn ( $isAdmin );
2017-07-05 07:52:51 -04: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 11:43:46 -05:00
2017-03-26 15:15:25 -04:00
$this -> navigationManager -> clear ();
2024-12-17 14:11:19 -05:00
$this -> dispatcher -> expects ( $this -> once ())
-> method ( 'dispatchTyped' )
-> willReturnCallback ( function ( $event ) {
$this -> assertInstanceOf ( LoadAdditionalEntriesEvent :: class , $event );
});
2017-03-26 15:15:25 -04:00
$entries = $this -> navigationManager -> getAll ( 'all' );
2016-12-08 11:43:46 -05:00
$this -> assertEquals ( $expected , $entries );
}
public function providesNavigationConfig () {
2017-07-05 07:52:51 -04:00
$apps = [
2018-07-13 08:47:00 -04:00
'core_apps' => [
2020-10-05 09:12:57 -04:00
'id' => 'core_apps' ,
2022-09-14 05:24:11 -04:00
'order' => 5 ,
2020-10-05 09:12:57 -04:00
'href' => '/apps/test/' ,
'icon' => '/apps/settings/img/apps.svg' ,
'name' => 'Apps' ,
'active' => false ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
'unread' => 0
2017-07-05 07:52:51 -04:00
]
];
$defaults = [
2023-10-26 13:30:22 -04:00
'profile' => [
'type' => 'settings' ,
'id' => 'profile' ,
'order' => 1 ,
'href' => '/apps/test/' ,
'name' => 'View profile' ,
'icon' => '' ,
'active' => false ,
'classes' => '' ,
'unread' => 0 ,
],
2022-09-14 05:24:11 -04:00
'accessibility_settings' => [
'type' => 'settings' ,
'id' => 'accessibility_settings' ,
'order' => 2 ,
'href' => '/apps/test/' ,
'name' => 'Appearance and accessibility' ,
'icon' => '/apps/theming/img/accessibility-dark.svg' ,
'active' => false ,
'classes' => '' ,
'unread' => 0 ,
],
2018-07-13 08:47:00 -04:00
'settings' => [
2020-10-05 09:12:57 -04:00
'id' => 'settings' ,
2022-09-14 05:24:11 -04:00
'order' => 3 ,
2020-10-05 09:12:57 -04:00
'href' => '/apps/test/' ,
'icon' => '/apps/settings/img/admin.svg' ,
'name' => 'Settings' ,
'active' => false ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
'unread' => 0
2017-07-05 07:52:51 -04:00
],
2018-07-13 08:47:00 -04:00
'logout' => [
2020-10-05 09:12:57 -04:00
'id' => 'logout' ,
'order' => 99999 ,
2024-09-19 05:10:31 -04:00
'href' => 'https://example.com/logout?requesttoken=' . urlencode ( \OCP\Util :: callRegister ()),
2020-10-05 09:12:57 -04:00
'icon' => '/apps/core/img/actions/logout.svg' ,
'name' => 'Log out' ,
'active' => false ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
'unread' => 0
2018-07-13 08:47:00 -04:00
]
2017-07-05 07:52:51 -04:00
];
2022-08-30 15:59:58 -04:00
$adminSettings = [
2022-09-14 05:24:11 -04:00
'accessibility_settings' => $defaults [ 'accessibility_settings' ],
2022-08-30 15:59:58 -04:00
'settings' => [
'id' => 'settings' ,
2022-09-14 05:24:11 -04:00
'order' => 3 ,
2022-08-30 15:59:58 -04:00
'href' => '/apps/test/' ,
2022-08-31 09:37:48 -04:00
'icon' => '/apps/settings/img/personal.svg' ,
2022-08-30 15:59:58 -04:00
'name' => 'Personal settings' ,
'active' => false ,
'type' => 'settings' ,
'classes' => '' ,
'unread' => 0
],
'admin_settings' => [
'id' => 'admin_settings' ,
2022-09-14 05:24:11 -04:00
'order' => 4 ,
2022-08-30 15:59:58 -04:00
'href' => '/apps/test/' ,
'icon' => '/apps/settings/img/admin.svg' ,
2022-09-02 06:41:49 -04:00
'name' => 'Administration settings' ,
2022-08-30 15:59:58 -04:00
'active' => false ,
'type' => 'settings' ,
'classes' => '' ,
'unread' => 0
]
];
2018-07-13 08:47:00 -04:00
2016-12-08 11:43:46 -05:00
return [
2018-07-13 08:47:00 -04:00
'minimalistic' => [
array_merge (
2023-10-26 13:30:22 -04:00
[ 'profile' => $defaults [ 'profile' ]],
2022-09-14 05:24:11 -04:00
[ 'accessibility_settings' => $defaults [ 'accessibility_settings' ]],
2018-07-13 08:47:00 -04:00
[ 'settings' => $defaults [ 'settings' ]],
[ 'test' => [
2020-10-05 09:12:57 -04:00
'id' => 'test' ,
'order' => 100 ,
'href' => '/apps/test/' ,
'icon' => '/apps/test/img/app.svg' ,
'name' => 'Test' ,
'active' => false ,
'type' => 'link' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
2023-10-10 08:24:34 -04:00
'unread' => 0 ,
'default' => true ,
'app' => 'test' ,
2018-07-13 08:47:00 -04:00
]],
[ 'logout' => $defaults [ 'logout' ]]
),
[ 'navigations' => [
2019-02-22 09:45:55 -05:00
'navigation' => [
[ 'route' => 'test.page.index' , 'name' => 'Test' ]
]
2018-07-13 08:47:00 -04:00
]]
],
'minimalistic-settings' => [
array_merge (
2023-10-26 13:30:22 -04:00
[ 'profile' => $defaults [ 'profile' ]],
2022-09-14 05:24:11 -04:00
[ 'accessibility_settings' => $defaults [ 'accessibility_settings' ]],
2018-07-13 08:47:00 -04:00
[ 'settings' => $defaults [ 'settings' ]],
[ 'test' => [
2020-10-05 09:12:57 -04:00
'id' => 'test' ,
'order' => 100 ,
'href' => '/apps/test/' ,
'icon' => '/apps/test/img/app.svg' ,
'name' => 'Test' ,
'active' => false ,
'type' => 'settings' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
2023-10-10 08:24:34 -04:00
'unread' => 0 ,
2018-07-13 08:47:00 -04:00
]],
[ 'logout' => $defaults [ 'logout' ]]
),
[ 'navigations' => [
2019-02-22 09:45:55 -05:00
'navigation' => [
[ 'route' => 'test.page.index' , 'name' => 'Test' , 'type' => 'settings' ]
],
]]
],
2023-10-10 08:24:34 -04:00
'with-multiple' => [
array_merge (
2023-10-26 13:30:22 -04:00
[ 'profile' => $defaults [ 'profile' ]],
2023-10-10 08:24:34 -04:00
[ 'accessibility_settings' => $defaults [ 'accessibility_settings' ]],
[ 'settings' => $defaults [ 'settings' ]],
[ 'test' => [
'id' => 'test' ,
'order' => 100 ,
'href' => '/apps/test/' ,
'icon' => '/apps/test/img/app.svg' ,
'name' => 'Test' ,
'active' => false ,
'type' => 'link' ,
'classes' => '' ,
'unread' => 0 ,
'default' => false ,
'app' => 'test' ,
],
'test1' => [
'id' => 'test1' ,
'order' => 50 ,
'href' => '/apps/test/' ,
'icon' => '/apps/test/img/app.svg' ,
'name' => 'Other test' ,
'active' => false ,
'type' => 'link' ,
'classes' => '' ,
'unread' => 0 ,
'default' => true , // because of order
'app' => 'test' ,
]],
[ 'logout' => $defaults [ 'logout' ]]
),
[ 'navigations' => [
'navigation' => [
[ 'route' => 'test.page.index' , 'name' => 'Test' ],
[ 'route' => 'test.page.index' , 'name' => 'Other test' , 'order' => 50 ],
]
]]
],
2018-07-13 08:47:00 -04:00
'admin' => [
array_merge (
2023-10-26 13:30:22 -04:00
[ 'profile' => $defaults [ 'profile' ]],
2022-08-30 15:59:58 -04:00
$adminSettings ,
2018-07-13 08:47:00 -04:00
$apps ,
[ 'test' => [
2020-10-05 09:12:57 -04:00
'id' => 'test' ,
'order' => 100 ,
'href' => '/apps/test/' ,
'icon' => '/apps/test/img/app.svg' ,
'name' => 'Test' ,
'active' => false ,
'type' => 'link' ,
2021-05-11 02:22:51 -04:00
'classes' => '' ,
2023-10-10 08:24:34 -04:00
'unread' => 0 ,
'default' => true ,
'app' => 'test' ,
2018-07-13 08:47:00 -04:00
]],
[ 'logout' => $defaults [ 'logout' ]]
),
[ 'navigations' => [
2019-02-22 09:45:55 -05:00
'navigation' => [
[ '@attributes' => [ 'role' => 'admin' ], 'route' => 'test.page.index' , 'name' => 'Test' ]
],
2018-07-13 08:47:00 -04:00
]],
true
],
'no name' => [
array_merge (
2023-10-26 13:30:22 -04:00
[ 'profile' => $defaults [ 'profile' ]],
2022-08-30 15:59:58 -04:00
$adminSettings ,
2018-07-13 08:47:00 -04:00
$apps ,
[ 'logout' => $defaults [ 'logout' ]]
),
[ 'navigations' => [
2019-02-22 09:45:55 -05:00
'navigation' => [
[ '@attributes' => [ 'role' => 'admin' ], 'route' => 'test.page.index' ]
],
2018-07-13 08:47:00 -04:00
]],
true
],
'no admin' => [
$defaults ,
2023-09-26 10:21:22 -04:00
[ 'navigations' => [
'navigation' => [
[ '@attributes' => [ 'role' => 'admin' ], 'route' => 'test.page.index' , 'name' => 'Test' ]
],
]],
2018-07-13 08:47:00 -04:00
]
2016-12-08 11:43:46 -05:00
];
}
2023-09-26 10:21:22 -04:00
2024-09-15 16:32:31 -04:00
public function testWithAppManagerAndApporder () : void {
2023-09-26 10:21:22 -04:00
$l = $this -> createMock ( IL10N :: class );
$l -> expects ( $this -> any ()) -> method ( 't' ) -> willReturnCallback ( function ( $text , $parameters = []) {
return vsprintf ( $text , $parameters );
});
$testOrder = 12 ;
$expected = [
'test' => [
'type' => 'link' ,
'id' => 'test' ,
'order' => $testOrder ,
'href' => '/apps/test/' ,
'name' => 'Test' ,
'icon' => '/apps/test/img/app.svg' ,
'active' => false ,
'classes' => '' ,
'unread' => 0 ,
2023-10-10 08:24:34 -04:00
'default' => true ,
'app' => 'test' ,
2023-09-26 10:21:22 -04:00
],
];
$navigation = [ 'navigations' => [
'navigation' => [
[ 'route' => 'test.page.index' , 'name' => 'Test' ]
],
]];
$this -> config -> method ( 'getUserValue' )
-> willReturnCallback (
function ( string $userId , string $appName , string $key , mixed $default = '' ) use ( $testOrder ) {
$this -> assertEquals ( 'user001' , $userId );
if ( $key === 'apporder' ) {
2023-11-08 08:15:25 -05:00
return json_encode ([ 'test' => [ 'app' => 'test' , 'order' => $testOrder ]]);
2023-09-26 10:21:22 -04:00
}
return $default ;
}
);
$this -> appManager -> expects ( $this -> any ())
2024-08-23 09:10:27 -04:00
-> method ( 'isEnabledForUser' )
-> with ( 'theming' )
-> willReturn ( true );
2023-09-26 10:21:22 -04:00
$this -> appManager -> expects ( $this -> once ()) -> method ( 'getAppInfo' ) -> with ( 'test' ) -> willReturn ( $navigation );
2024-03-06 05:13:29 -05:00
$this -> appManager -> expects ( $this -> once ()) -> method ( 'getAppIcon' ) -> with ( 'test' ) -> willReturn ( '/apps/test/img/app.svg' );
2023-09-26 10:21:22 -04:00
$this -> l10nFac -> expects ( $this -> any ()) -> method ( 'get' ) -> willReturn ( $l );
$this -> urlGenerator -> expects ( $this -> any ()) -> method ( 'imagePath' ) -> willReturnCallback ( function ( $appName , $file ) {
return " /apps/ $appName /img/ $file " ;
});
$this -> urlGenerator -> expects ( $this -> any ()) -> method ( 'linkToRoute' ) -> willReturnCallback ( function ( $route ) {
if ( $route === 'core.login.logout' ) {
return 'https://example.com/logout' ;
}
return '/apps/test/' ;
});
$user = $this -> createMock ( IUser :: class );
$user -> expects ( $this -> any ()) -> method ( 'getUID' ) -> willReturn ( 'user001' );
$this -> userSession -> expects ( $this -> any ()) -> method ( 'getUser' ) -> willReturn ( $user );
$this -> userSession -> expects ( $this -> any ()) -> method ( 'isLoggedIn' ) -> willReturn ( true );
$this -> appManager -> expects ( $this -> any ())
2024-08-23 09:10:27 -04:00
-> method ( 'getEnabledAppsForUser' )
-> with ( $user )
-> willReturn ([ 'test' ]);
2023-09-26 10:21:22 -04:00
$this -> groupManager -> expects ( $this -> any ()) -> method ( 'isAdmin' ) -> willReturn ( false );
$subadmin = $this -> createMock ( SubAdmin :: class );
$subadmin -> expects ( $this -> any ()) -> method ( 'isSubAdmin' ) -> with ( $user ) -> willReturn ( false );
$this -> groupManager -> expects ( $this -> any ()) -> method ( 'getSubAdmin' ) -> willReturn ( $subadmin );
$this -> navigationManager -> clear ();
2024-12-17 14:11:19 -05:00
$this -> dispatcher -> expects ( $this -> once ())
-> method ( 'dispatchTyped' )
-> willReturnCallback ( function ( $event ) {
$this -> assertInstanceOf ( LoadAdditionalEntriesEvent :: class , $event );
});
2023-09-26 10:21:22 -04:00
$entries = $this -> navigationManager -> getAll ();
$this -> assertEquals ( $expected , $entries );
}
2024-08-27 06:13:04 -04:00
public static function provideDefaultEntries () : array {
return [
// none specified, default to files
[
'' ,
'' ,
'{}' ,
true ,
'files' ,
],
// none specified, without fallback
[
'' ,
'' ,
'{}' ,
false ,
'' ,
],
// unexisting or inaccessible app specified, default to files
[
'unexist' ,
'' ,
'{}' ,
true ,
'files' ,
],
// unexisting or inaccessible app specified, without fallbacks
[
'unexist' ,
'' ,
'{}' ,
false ,
'' ,
],
// non-standard app
[
'settings' ,
'' ,
'{}' ,
true ,
'settings' ,
],
// non-standard app, without fallback
[
'settings' ,
'' ,
'{}' ,
false ,
'settings' ,
],
// non-standard app with fallback
[
'unexist,settings' ,
'' ,
'{}' ,
true ,
'settings' ,
],
// system default app and user apporder
[
// system default is settings
'unexist,settings' ,
'' ,
// apporder says default app is files (order is lower)
'{"files_id":{"app":"files","order":1},"settings_id":{"app":"settings","order":2}}' ,
true ,
// system default should override apporder
'settings'
],
// user-customized defaultapp
[
'' ,
'files' ,
'' ,
true ,
'files' ,
],
// user-customized defaultapp with systemwide
[
'unexist,settings' ,
'files' ,
'' ,
true ,
'files' ,
],
// user-customized defaultapp with system wide and apporder
[
'unexist,settings' ,
'files' ,
'{"settings_id":{"app":"settings","order":1},"files_id":{"app":"files","order":2}}' ,
true ,
'files' ,
],
// user-customized apporder fallback
[
'' ,
'' ,
'{"settings_id":{"app":"settings","order":1},"files":{"app":"files","order":2}}' ,
true ,
'settings' ,
],
// user-customized apporder fallback with missing app key (entries added by closures does not always have an app key set (Nextcloud 27 spreed app for example))
[
'' ,
'' ,
'{"spreed":{"order":1},"files":{"app":"files","order":2}}' ,
true ,
'files' ,
],
// user-customized apporder, but called without fallback
[
'' ,
'' ,
'{"settings":{"app":"settings","order":1},"files":{"app":"files","order":2}}' ,
false ,
'' ,
],
// user-customized apporder with an app that has multiple routes
[
'' ,
'' ,
'{"settings_id":{"app":"settings","order":1},"settings_id_2":{"app":"settings","order":3},"id_files":{"app":"files","order":2}}' ,
true ,
'settings' ,
],
];
}
/**
* @ dataProvider provideDefaultEntries
*/
2024-09-15 16:32:31 -04:00
public function testGetDefaultEntryIdForUser ( $defaultApps , $userDefaultApps , $userApporder , $withFallbacks , $expectedApp ) : void {
2024-08-27 06:13:04 -04:00
$this -> navigationManager -> add ([
'id' => 'files' ,
]);
$this -> navigationManager -> add ([
'id' => 'settings' ,
]);
2025-02-10 05:35:38 -05:00
$this -> appManager -> method ( 'getEnabledApps' ) -> willReturn ([]);
2024-08-27 06:13:04 -04:00
$user = $this -> createMock ( IUser :: class );
$user -> method ( 'getUID' ) -> willReturn ( 'user1' );
$this -> userSession -> expects ( $this -> once ())
-> method ( 'getUser' )
-> willReturn ( $user );
$this -> config -> expects ( $this -> once ())
-> method ( 'getSystemValueString' )
-> with ( 'defaultapp' , $this -> anything ())
-> willReturn ( $defaultApps );
$this -> config -> expects ( $this -> atLeastOnce ())
-> method ( 'getUserValue' )
-> willReturnMap ([
[ 'user1' , 'core' , 'defaultapp' , '' , $userDefaultApps ],
[ 'user1' , 'core' , 'apporder' , '[]' , $userApporder ],
]);
$this -> assertEquals ( $expectedApp , $this -> navigationManager -> getDefaultEntryIdForUser ( null , $withFallbacks ));
}
2024-09-04 11:48:30 -04:00
2024-09-15 16:32:31 -04:00
public function testDefaultEntryUpdated () : void {
2025-02-10 05:35:38 -05:00
$this -> appManager -> method ( 'getEnabledApps' ) -> willReturn ([]);
2024-09-04 11:48:30 -04:00
$user = $this -> createMock ( IUser :: class );
$user -> method ( 'getUID' ) -> willReturn ( 'user1' );
$this -> userSession
-> method ( 'getUser' )
-> willReturn ( $user );
$this -> config
-> method ( 'getSystemValueString' )
-> with ( 'defaultapp' , $this -> anything ())
-> willReturn ( 'app4,app3,app2,app1' );
$this -> config
-> method ( 'getUserValue' )
-> willReturnMap ([
[ 'user1' , 'core' , 'defaultapp' , '' , '' ],
[ 'user1' , 'core' , 'apporder' , '[]' , '' ],
]);
$this -> navigationManager -> add ([
'id' => 'app1' ,
]);
$this -> assertEquals ( 'app1' , $this -> navigationManager -> getDefaultEntryIdForUser ( null , false ));
$this -> assertEquals ( true , $this -> navigationManager -> get ( 'app1' )[ 'default' ]);
$this -> navigationManager -> add ([
'id' => 'app3' ,
]);
$this -> assertEquals ( 'app3' , $this -> navigationManager -> getDefaultEntryIdForUser ( null , false ));
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app1' )[ 'default' ]);
$this -> assertEquals ( true , $this -> navigationManager -> get ( 'app3' )[ 'default' ]);
$this -> navigationManager -> add ([
'id' => 'app2' ,
]);
$this -> assertEquals ( 'app3' , $this -> navigationManager -> getDefaultEntryIdForUser ( null , false ));
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app1' )[ 'default' ]);
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app2' )[ 'default' ]);
$this -> assertEquals ( true , $this -> navigationManager -> get ( 'app3' )[ 'default' ]);
$this -> navigationManager -> add ([
'id' => 'app4' ,
]);
$this -> assertEquals ( 'app4' , $this -> navigationManager -> getDefaultEntryIdForUser ( null , false ));
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app1' )[ 'default' ]);
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app2' )[ 'default' ]);
$this -> assertEquals ( false , $this -> navigationManager -> get ( 'app3' )[ 'default' ]);
$this -> assertEquals ( true , $this -> navigationManager -> get ( 'app4' )[ 'default' ]);
}
2015-03-16 11:46:17 -04:00
}