NavigationManagerTest.php 16 KB

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