1
0

NavigationManagerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\App\AppManager;
  9. use OC\Group\Manager;
  10. use OC\NavigationManager;
  11. use OC\SubAdmin;
  12. use OCP\IConfig;
  13. use OCP\IGroupManager;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\IUser;
  17. use OCP\IUserSession;
  18. use OCP\L10N\IFactory;
  19. class NavigationManagerTest extends TestCase {
  20. /** @var AppManager|\PHPUnit\Framework\MockObject\MockObject */
  21. protected $appManager;
  22. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  23. protected $urlGenerator;
  24. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  25. protected $l10nFac;
  26. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $userSession;
  28. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $groupManager;
  30. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  31. protected $config;
  32. /** @var \OC\NavigationManager */
  33. protected $navigationManager;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->appManager = $this->createMock(AppManager::class);
  37. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  38. $this->l10nFac = $this->createMock(IFactory::class);
  39. $this->userSession = $this->createMock(IUserSession::class);
  40. $this->groupManager = $this->createMock(Manager::class);
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->navigationManager = new NavigationManager(
  43. $this->appManager,
  44. $this->urlGenerator,
  45. $this->l10nFac,
  46. $this->userSession,
  47. $this->groupManager,
  48. $this->config
  49. );
  50. $this->navigationManager->clear(false);
  51. }
  52. public function addArrayData() {
  53. return [
  54. [
  55. 'entry id' => [
  56. 'id' => 'entry id',
  57. 'name' => 'link text',
  58. 'order' => 1,
  59. 'icon' => 'optional',
  60. 'href' => 'url',
  61. 'type' => 'settings',
  62. 'classes' => '',
  63. 'unread' => 0
  64. ],
  65. 'entry id2' => [
  66. 'id' => 'entry id',
  67. 'name' => 'link text',
  68. 'order' => 1,
  69. 'icon' => 'optional',
  70. 'href' => 'url',
  71. 'active' => false,
  72. 'type' => 'settings',
  73. 'classes' => '',
  74. 'unread' => 0
  75. ]
  76. ],
  77. [
  78. 'entry id' => [
  79. 'id' => 'entry id',
  80. 'name' => 'link text',
  81. 'order' => 1,
  82. //'icon' => 'optional',
  83. 'href' => 'url',
  84. 'active' => true,
  85. 'unread' => 0,
  86. ],
  87. 'entry id2' => [
  88. 'id' => 'entry id',
  89. 'name' => 'link text',
  90. 'order' => 1,
  91. 'icon' => '',
  92. 'href' => 'url',
  93. 'active' => false,
  94. 'type' => 'link',
  95. 'classes' => '',
  96. 'unread' => 0,
  97. 'default' => true,
  98. ]
  99. ]
  100. ];
  101. }
  102. /**
  103. * @dataProvider addArrayData
  104. *
  105. * @param array $entry
  106. * @param array $expectedEntry
  107. */
  108. public function testAddArray(array $entry, array $expectedEntry) {
  109. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
  110. $this->navigationManager->add($entry);
  111. $navigationEntries = $this->navigationManager->getAll('all');
  112. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  113. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  114. $this->navigationManager->clear(false);
  115. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  116. }
  117. /**
  118. * @dataProvider addArrayData
  119. *
  120. * @param array $entry
  121. * @param array $expectedEntry
  122. */
  123. public function testAddClosure(array $entry, array $expectedEntry) {
  124. global $testAddClosureNumberOfCalls;
  125. $testAddClosureNumberOfCalls = 0;
  126. $this->navigationManager->add(function () use ($entry) {
  127. global $testAddClosureNumberOfCalls;
  128. $testAddClosureNumberOfCalls++;
  129. return $entry;
  130. });
  131. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  132. $navigationEntries = $this->navigationManager->getAll('all');
  133. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
  134. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  135. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  136. $navigationEntries = $this->navigationManager->getAll('all');
  137. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
  138. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  139. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  140. $this->navigationManager->clear(false);
  141. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  142. }
  143. public function testAddArrayClearGetAll() {
  144. $entry = [
  145. 'id' => 'entry id',
  146. 'name' => 'link text',
  147. 'order' => 1,
  148. 'icon' => 'optional',
  149. 'href' => 'url'
  150. ];
  151. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  152. $this->navigationManager->add($entry);
  153. $this->navigationManager->clear(false);
  154. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  155. }
  156. public function testAddClosureClearGetAll() {
  157. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  158. $entry = [
  159. 'id' => 'entry id',
  160. 'name' => 'link text',
  161. 'order' => 1,
  162. 'icon' => 'optional',
  163. 'href' => 'url'
  164. ];
  165. global $testAddClosureNumberOfCalls;
  166. $testAddClosureNumberOfCalls = 0;
  167. $this->navigationManager->add(function () use ($entry) {
  168. global $testAddClosureNumberOfCalls;
  169. $testAddClosureNumberOfCalls++;
  170. return $entry;
  171. });
  172. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  173. $this->navigationManager->clear(false);
  174. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
  175. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  176. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
  177. }
  178. /**
  179. * @dataProvider providesNavigationConfig
  180. */
  181. public function testWithAppManager($expected, $navigation, $isAdmin = false) {
  182. $l = $this->createMock(IL10N::class);
  183. $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
  184. return vsprintf($text, $parameters);
  185. });
  186. /* Return default value */
  187. $this->config->method('getUserValue')
  188. ->willReturnArgument(3);
  189. $this->appManager->expects($this->any())
  190. ->method('isEnabledForUser')
  191. ->with('theming')
  192. ->willReturn(true);
  193. $this->appManager->expects($this->once())
  194. ->method('getAppInfo')
  195. ->with('test')
  196. ->willReturn($navigation);
  197. $this->urlGenerator->expects($this->any())
  198. ->method('imagePath')
  199. ->willReturnCallback(function ($appName, $file) {
  200. return "/apps/$appName/img/$file";
  201. });
  202. $this->appManager->expects($this->any())
  203. ->method('getAppIcon')
  204. ->willReturnCallback(fn (string $appName) => "/apps/$appName/img/app.svg");
  205. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  206. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  207. if ($route === 'core.login.logout') {
  208. return 'https://example.com/logout';
  209. }
  210. return '/apps/test/';
  211. });
  212. $user = $this->createMock(IUser::class);
  213. $user->expects($this->any())->method('getUID')->willReturn('user001');
  214. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  215. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  216. $this->appManager->expects($this->any())
  217. ->method('getEnabledAppsForUser')
  218. ->with($user)
  219. ->willReturn(['test']);
  220. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  221. $subadmin = $this->createMock(SubAdmin::class);
  222. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  223. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  224. $this->navigationManager->clear();
  225. $entries = $this->navigationManager->getAll('all');
  226. $this->assertEquals($expected, $entries);
  227. }
  228. public function providesNavigationConfig() {
  229. $apps = [
  230. 'core_apps' => [
  231. 'id' => 'core_apps',
  232. 'order' => 5,
  233. 'href' => '/apps/test/',
  234. 'icon' => '/apps/settings/img/apps.svg',
  235. 'name' => 'Apps',
  236. 'active' => false,
  237. 'type' => 'settings',
  238. 'classes' => '',
  239. 'unread' => 0
  240. ]
  241. ];
  242. $defaults = [
  243. 'profile' => [
  244. 'type' => 'settings',
  245. 'id' => 'profile',
  246. 'order' => 1,
  247. 'href' => '/apps/test/',
  248. 'name' => 'View profile',
  249. 'icon' => '',
  250. 'active' => false,
  251. 'classes' => '',
  252. 'unread' => 0,
  253. ],
  254. 'accessibility_settings' => [
  255. 'type' => 'settings',
  256. 'id' => 'accessibility_settings',
  257. 'order' => 2,
  258. 'href' => '/apps/test/',
  259. 'name' => 'Appearance and accessibility',
  260. 'icon' => '/apps/theming/img/accessibility-dark.svg',
  261. 'active' => false,
  262. 'classes' => '',
  263. 'unread' => 0,
  264. ],
  265. 'settings' => [
  266. 'id' => 'settings',
  267. 'order' => 3,
  268. 'href' => '/apps/test/',
  269. 'icon' => '/apps/settings/img/admin.svg',
  270. 'name' => 'Settings',
  271. 'active' => false,
  272. 'type' => 'settings',
  273. 'classes' => '',
  274. 'unread' => 0
  275. ],
  276. 'logout' => [
  277. 'id' => 'logout',
  278. 'order' => 99999,
  279. 'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
  280. 'icon' => '/apps/core/img/actions/logout.svg',
  281. 'name' => 'Log out',
  282. 'active' => false,
  283. 'type' => 'settings',
  284. 'classes' => '',
  285. 'unread' => 0
  286. ]
  287. ];
  288. $adminSettings = [
  289. 'accessibility_settings' => $defaults['accessibility_settings'],
  290. 'settings' => [
  291. 'id' => 'settings',
  292. 'order' => 3,
  293. 'href' => '/apps/test/',
  294. 'icon' => '/apps/settings/img/personal.svg',
  295. 'name' => 'Personal settings',
  296. 'active' => false,
  297. 'type' => 'settings',
  298. 'classes' => '',
  299. 'unread' => 0
  300. ],
  301. 'admin_settings' => [
  302. 'id' => 'admin_settings',
  303. 'order' => 4,
  304. 'href' => '/apps/test/',
  305. 'icon' => '/apps/settings/img/admin.svg',
  306. 'name' => 'Administration settings',
  307. 'active' => false,
  308. 'type' => 'settings',
  309. 'classes' => '',
  310. 'unread' => 0
  311. ]
  312. ];
  313. return [
  314. 'minimalistic' => [
  315. array_merge(
  316. ['profile' => $defaults['profile']],
  317. ['accessibility_settings' => $defaults['accessibility_settings']],
  318. ['settings' => $defaults['settings']],
  319. ['test' => [
  320. 'id' => 'test',
  321. 'order' => 100,
  322. 'href' => '/apps/test/',
  323. 'icon' => '/apps/test/img/app.svg',
  324. 'name' => 'Test',
  325. 'active' => false,
  326. 'type' => 'link',
  327. 'classes' => '',
  328. 'unread' => 0,
  329. 'default' => true,
  330. 'app' => 'test',
  331. ]],
  332. ['logout' => $defaults['logout']]
  333. ),
  334. ['navigations' => [
  335. 'navigation' => [
  336. ['route' => 'test.page.index', 'name' => 'Test']
  337. ]
  338. ]]
  339. ],
  340. 'minimalistic-settings' => [
  341. array_merge(
  342. ['profile' => $defaults['profile']],
  343. ['accessibility_settings' => $defaults['accessibility_settings']],
  344. ['settings' => $defaults['settings']],
  345. ['test' => [
  346. 'id' => 'test',
  347. 'order' => 100,
  348. 'href' => '/apps/test/',
  349. 'icon' => '/apps/test/img/app.svg',
  350. 'name' => 'Test',
  351. 'active' => false,
  352. 'type' => 'settings',
  353. 'classes' => '',
  354. 'unread' => 0,
  355. ]],
  356. ['logout' => $defaults['logout']]
  357. ),
  358. ['navigations' => [
  359. 'navigation' => [
  360. ['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
  361. ],
  362. ]]
  363. ],
  364. 'with-multiple' => [
  365. array_merge(
  366. ['profile' => $defaults['profile']],
  367. ['accessibility_settings' => $defaults['accessibility_settings']],
  368. ['settings' => $defaults['settings']],
  369. ['test' => [
  370. 'id' => 'test',
  371. 'order' => 100,
  372. 'href' => '/apps/test/',
  373. 'icon' => '/apps/test/img/app.svg',
  374. 'name' => 'Test',
  375. 'active' => false,
  376. 'type' => 'link',
  377. 'classes' => '',
  378. 'unread' => 0,
  379. 'default' => false,
  380. 'app' => 'test',
  381. ],
  382. 'test1' => [
  383. 'id' => 'test1',
  384. 'order' => 50,
  385. 'href' => '/apps/test/',
  386. 'icon' => '/apps/test/img/app.svg',
  387. 'name' => 'Other test',
  388. 'active' => false,
  389. 'type' => 'link',
  390. 'classes' => '',
  391. 'unread' => 0,
  392. 'default' => true, // because of order
  393. 'app' => 'test',
  394. ]],
  395. ['logout' => $defaults['logout']]
  396. ),
  397. ['navigations' => [
  398. 'navigation' => [
  399. ['route' => 'test.page.index', 'name' => 'Test'],
  400. ['route' => 'test.page.index', 'name' => 'Other test', 'order' => 50],
  401. ]
  402. ]]
  403. ],
  404. 'admin' => [
  405. array_merge(
  406. ['profile' => $defaults['profile']],
  407. $adminSettings,
  408. $apps,
  409. ['test' => [
  410. 'id' => 'test',
  411. 'order' => 100,
  412. 'href' => '/apps/test/',
  413. 'icon' => '/apps/test/img/app.svg',
  414. 'name' => 'Test',
  415. 'active' => false,
  416. 'type' => 'link',
  417. 'classes' => '',
  418. 'unread' => 0,
  419. 'default' => true,
  420. 'app' => 'test',
  421. ]],
  422. ['logout' => $defaults['logout']]
  423. ),
  424. ['navigations' => [
  425. 'navigation' => [
  426. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  427. ],
  428. ]],
  429. true
  430. ],
  431. 'no name' => [
  432. array_merge(
  433. ['profile' => $defaults['profile']],
  434. $adminSettings,
  435. $apps,
  436. ['logout' => $defaults['logout']]
  437. ),
  438. ['navigations' => [
  439. 'navigation' => [
  440. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
  441. ],
  442. ]],
  443. true
  444. ],
  445. 'no admin' => [
  446. $defaults,
  447. ['navigations' => [
  448. 'navigation' => [
  449. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  450. ],
  451. ]],
  452. ]
  453. ];
  454. }
  455. public function testWithAppManagerAndApporder() {
  456. $l = $this->createMock(IL10N::class);
  457. $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
  458. return vsprintf($text, $parameters);
  459. });
  460. $testOrder = 12;
  461. $expected = [
  462. 'test' => [
  463. 'type' => 'link',
  464. 'id' => 'test',
  465. 'order' => $testOrder,
  466. 'href' => '/apps/test/',
  467. 'name' => 'Test',
  468. 'icon' => '/apps/test/img/app.svg',
  469. 'active' => false,
  470. 'classes' => '',
  471. 'unread' => 0,
  472. 'default' => true,
  473. 'app' => 'test',
  474. ],
  475. ];
  476. $navigation = ['navigations' => [
  477. 'navigation' => [
  478. ['route' => 'test.page.index', 'name' => 'Test']
  479. ],
  480. ]];
  481. $this->config->method('getUserValue')
  482. ->willReturnCallback(
  483. function (string $userId, string $appName, string $key, mixed $default = '') use ($testOrder) {
  484. $this->assertEquals('user001', $userId);
  485. if ($key === 'apporder') {
  486. return json_encode(['test' => ['app' => 'test', 'order' => $testOrder]]);
  487. }
  488. return $default;
  489. }
  490. );
  491. $this->appManager->expects($this->any())
  492. ->method('isEnabledForUser')
  493. ->with('theming')
  494. ->willReturn(true);
  495. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  496. $this->appManager->expects($this->once())->method('getAppIcon')->with('test')->willReturn('/apps/test/img/app.svg');
  497. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  498. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  499. return "/apps/$appName/img/$file";
  500. });
  501. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  502. if ($route === 'core.login.logout') {
  503. return 'https://example.com/logout';
  504. }
  505. return '/apps/test/';
  506. });
  507. $user = $this->createMock(IUser::class);
  508. $user->expects($this->any())->method('getUID')->willReturn('user001');
  509. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  510. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  511. $this->appManager->expects($this->any())
  512. ->method('getEnabledAppsForUser')
  513. ->with($user)
  514. ->willReturn(['test']);
  515. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn(false);
  516. $subadmin = $this->createMock(SubAdmin::class);
  517. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  518. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  519. $this->navigationManager->clear();
  520. $entries = $this->navigationManager->getAll();
  521. $this->assertEquals($expected, $entries);
  522. }
  523. }