NavigationManagerTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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())->method('getAppInfo')->with('test')->willReturn($navigation);
  199. /*
  200. $this->appManager->expects($this->any())
  201. ->method('getAppInfo')
  202. ->will($this->returnValueMap([
  203. ['test', null, null, $navigation],
  204. ['theming', null, null, null],
  205. ]));
  206. */
  207. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  208. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  209. return "/apps/$appName/img/$file";
  210. });
  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. 'accessibility_settings' => [
  249. 'type' => 'settings',
  250. 'id' => 'accessibility_settings',
  251. 'order' => 2,
  252. 'href' => '/apps/test/',
  253. 'name' => 'Appearance and accessibility',
  254. 'icon' => '/apps/theming/img/accessibility-dark.svg',
  255. 'active' => false,
  256. 'classes' => '',
  257. 'unread' => 0,
  258. ],
  259. 'settings' => [
  260. 'id' => 'settings',
  261. 'order' => 3,
  262. 'href' => '/apps/test/',
  263. 'icon' => '/apps/settings/img/admin.svg',
  264. 'name' => 'Settings',
  265. 'active' => false,
  266. 'type' => 'settings',
  267. 'classes' => '',
  268. 'unread' => 0
  269. ],
  270. 'logout' => [
  271. 'id' => 'logout',
  272. 'order' => 99999,
  273. 'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
  274. 'icon' => '/apps/core/img/actions/logout.svg',
  275. 'name' => 'Log out',
  276. 'active' => false,
  277. 'type' => 'settings',
  278. 'classes' => '',
  279. 'unread' => 0
  280. ]
  281. ];
  282. $adminSettings = [
  283. 'accessibility_settings' => $defaults['accessibility_settings'],
  284. 'settings' => [
  285. 'id' => 'settings',
  286. 'order' => 3,
  287. 'href' => '/apps/test/',
  288. 'icon' => '/apps/settings/img/personal.svg',
  289. 'name' => 'Personal settings',
  290. 'active' => false,
  291. 'type' => 'settings',
  292. 'classes' => '',
  293. 'unread' => 0
  294. ],
  295. 'admin_settings' => [
  296. 'id' => 'admin_settings',
  297. 'order' => 4,
  298. 'href' => '/apps/test/',
  299. 'icon' => '/apps/settings/img/admin.svg',
  300. 'name' => 'Administration settings',
  301. 'active' => false,
  302. 'type' => 'settings',
  303. 'classes' => '',
  304. 'unread' => 0
  305. ]
  306. ];
  307. return [
  308. 'minimalistic' => [
  309. array_merge(
  310. ['accessibility_settings' => $defaults['accessibility_settings']],
  311. ['settings' => $defaults['settings']],
  312. ['test' => [
  313. 'id' => 'test',
  314. 'order' => 100,
  315. 'href' => '/apps/test/',
  316. 'icon' => '/apps/test/img/app.svg',
  317. 'name' => 'Test',
  318. 'active' => false,
  319. 'type' => 'link',
  320. 'classes' => '',
  321. 'unread' => 0,
  322. 'default' => true,
  323. 'app' => 'test',
  324. 'key' => 0,
  325. ]],
  326. ['logout' => $defaults['logout']]
  327. ),
  328. ['navigations' => [
  329. 'navigation' => [
  330. ['route' => 'test.page.index', 'name' => 'Test']
  331. ]
  332. ]]
  333. ],
  334. 'minimalistic-settings' => [
  335. array_merge(
  336. ['accessibility_settings' => $defaults['accessibility_settings']],
  337. ['settings' => $defaults['settings']],
  338. ['test' => [
  339. 'id' => 'test',
  340. 'order' => 100,
  341. 'href' => '/apps/test/',
  342. 'icon' => '/apps/test/img/app.svg',
  343. 'name' => 'Test',
  344. 'active' => false,
  345. 'type' => 'settings',
  346. 'classes' => '',
  347. 'unread' => 0,
  348. ]],
  349. ['logout' => $defaults['logout']]
  350. ),
  351. ['navigations' => [
  352. 'navigation' => [
  353. ['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
  354. ],
  355. ]]
  356. ],
  357. 'with-multiple' => [
  358. array_merge(
  359. ['accessibility_settings' => $defaults['accessibility_settings']],
  360. ['settings' => $defaults['settings']],
  361. ['test' => [
  362. 'id' => 'test',
  363. 'order' => 100,
  364. 'href' => '/apps/test/',
  365. 'icon' => '/apps/test/img/app.svg',
  366. 'name' => 'Test',
  367. 'active' => false,
  368. 'type' => 'link',
  369. 'classes' => '',
  370. 'unread' => 0,
  371. 'default' => false,
  372. 'app' => 'test',
  373. 'key' => 0,
  374. ],
  375. 'test1' => [
  376. 'id' => 'test1',
  377. 'order' => 50,
  378. 'href' => '/apps/test/',
  379. 'icon' => '/apps/test/img/app.svg',
  380. 'name' => 'Other test',
  381. 'active' => false,
  382. 'type' => 'link',
  383. 'classes' => '',
  384. 'unread' => 0,
  385. 'default' => true, // because of order
  386. 'app' => 'test',
  387. 'key' => 1,
  388. ]],
  389. ['logout' => $defaults['logout']]
  390. ),
  391. ['navigations' => [
  392. 'navigation' => [
  393. ['route' => 'test.page.index', 'name' => 'Test'],
  394. ['route' => 'test.page.index', 'name' => 'Other test', 'order' => 50],
  395. ]
  396. ]]
  397. ],
  398. 'admin' => [
  399. array_merge(
  400. $adminSettings,
  401. $apps,
  402. ['test' => [
  403. 'id' => 'test',
  404. 'order' => 100,
  405. 'href' => '/apps/test/',
  406. 'icon' => '/apps/test/img/app.svg',
  407. 'name' => 'Test',
  408. 'active' => false,
  409. 'type' => 'link',
  410. 'classes' => '',
  411. 'unread' => 0,
  412. 'default' => true,
  413. 'app' => 'test',
  414. 'key' => 0,
  415. ]],
  416. ['logout' => $defaults['logout']]
  417. ),
  418. ['navigations' => [
  419. 'navigation' => [
  420. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  421. ],
  422. ]],
  423. true
  424. ],
  425. 'no name' => [
  426. array_merge(
  427. $adminSettings,
  428. $apps,
  429. ['logout' => $defaults['logout']]
  430. ),
  431. ['navigations' => [
  432. 'navigation' => [
  433. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
  434. ],
  435. ]],
  436. true
  437. ],
  438. 'no admin' => [
  439. $defaults,
  440. ['navigations' => [
  441. 'navigation' => [
  442. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  443. ],
  444. ]],
  445. ]
  446. ];
  447. }
  448. public function testWithAppManagerAndApporder() {
  449. $l = $this->createMock(IL10N::class);
  450. $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
  451. return vsprintf($text, $parameters);
  452. });
  453. $testOrder = 12;
  454. $expected = [
  455. 'test' => [
  456. 'type' => 'link',
  457. 'id' => 'test',
  458. 'order' => $testOrder,
  459. 'href' => '/apps/test/',
  460. 'name' => 'Test',
  461. 'icon' => '/apps/test/img/app.svg',
  462. 'active' => false,
  463. 'classes' => '',
  464. 'unread' => 0,
  465. 'default' => true,
  466. 'app' => 'test',
  467. 'key' => 0,
  468. ],
  469. ];
  470. $navigation = ['navigations' => [
  471. 'navigation' => [
  472. ['route' => 'test.page.index', 'name' => 'Test']
  473. ],
  474. ]];
  475. $this->config->method('getUserValue')
  476. ->willReturnCallback(
  477. function (string $userId, string $appName, string $key, mixed $default = '') use ($testOrder) {
  478. $this->assertEquals('user001', $userId);
  479. if ($key === 'apporder') {
  480. return json_encode(['test' => [$testOrder]]);
  481. }
  482. return $default;
  483. }
  484. );
  485. $this->appManager->expects($this->any())
  486. ->method('isEnabledForUser')
  487. ->with('theming')
  488. ->willReturn(true);
  489. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  490. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  491. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  492. return "/apps/$appName/img/$file";
  493. });
  494. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  495. if ($route === 'core.login.logout') {
  496. return 'https://example.com/logout';
  497. }
  498. return '/apps/test/';
  499. });
  500. $user = $this->createMock(IUser::class);
  501. $user->expects($this->any())->method('getUID')->willReturn('user001');
  502. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  503. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  504. $this->appManager->expects($this->any())
  505. ->method('getEnabledAppsForUser')
  506. ->with($user)
  507. ->willReturn(['test']);
  508. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn(false);
  509. $subadmin = $this->createMock(SubAdmin::class);
  510. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  511. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  512. $this->navigationManager->clear();
  513. $entries = $this->navigationManager->getAll();
  514. $this->assertEquals($expected, $entries);
  515. }
  516. }