1
0

NavigationManagerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. ]
  103. ]
  104. ];
  105. }
  106. /**
  107. * @dataProvider addArrayData
  108. *
  109. * @param array $entry
  110. * @param array $expectedEntry
  111. */
  112. public function testAddArray(array $entry, array $expectedEntry) {
  113. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
  114. $this->navigationManager->add($entry);
  115. $navigationEntries = $this->navigationManager->getAll('all');
  116. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  117. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  118. $this->navigationManager->clear(false);
  119. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  120. }
  121. /**
  122. * @dataProvider addArrayData
  123. *
  124. * @param array $entry
  125. * @param array $expectedEntry
  126. */
  127. public function testAddClosure(array $entry, array $expectedEntry) {
  128. global $testAddClosureNumberOfCalls;
  129. $testAddClosureNumberOfCalls = 0;
  130. $this->navigationManager->add(function () use ($entry) {
  131. global $testAddClosureNumberOfCalls;
  132. $testAddClosureNumberOfCalls++;
  133. return $entry;
  134. });
  135. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  136. $navigationEntries = $this->navigationManager->getAll('all');
  137. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
  138. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  139. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  140. $navigationEntries = $this->navigationManager->getAll('all');
  141. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
  142. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  143. $this->assertEquals($expectedEntry, $navigationEntries['entry id']);
  144. $this->navigationManager->clear(false);
  145. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  146. }
  147. public function testAddArrayClearGetAll() {
  148. $entry = [
  149. 'id' => 'entry id',
  150. 'name' => 'link text',
  151. 'order' => 1,
  152. 'icon' => 'optional',
  153. 'href' => 'url'
  154. ];
  155. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  156. $this->navigationManager->add($entry);
  157. $this->navigationManager->clear(false);
  158. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  159. }
  160. public function testAddClosureClearGetAll() {
  161. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  162. $entry = [
  163. 'id' => 'entry id',
  164. 'name' => 'link text',
  165. 'order' => 1,
  166. 'icon' => 'optional',
  167. 'href' => 'url'
  168. ];
  169. global $testAddClosureNumberOfCalls;
  170. $testAddClosureNumberOfCalls = 0;
  171. $this->navigationManager->add(function () use ($entry) {
  172. global $testAddClosureNumberOfCalls;
  173. $testAddClosureNumberOfCalls++;
  174. return $entry;
  175. });
  176. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  177. $this->navigationManager->clear(false);
  178. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
  179. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  180. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
  181. }
  182. /**
  183. * @dataProvider providesNavigationConfig
  184. */
  185. public function testWithAppManager($expected, $navigation, $isAdmin = false) {
  186. $l = $this->createMock(IL10N::class);
  187. $l->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
  188. return vsprintf($text, $parameters);
  189. });
  190. $this->appManager->expects($this->any())
  191. ->method('isEnabledForUser')
  192. ->with('theming')
  193. ->willReturn(true);
  194. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  195. /*
  196. $this->appManager->expects($this->any())
  197. ->method('getAppInfo')
  198. ->will($this->returnValueMap([
  199. ['test', null, null, $navigation],
  200. ['theming', null, null, null],
  201. ]));
  202. */
  203. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  204. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  205. return "/apps/$appName/img/$file";
  206. });
  207. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  208. if ($route === 'core.login.logout') {
  209. return 'https://example.com/logout';
  210. }
  211. return '/apps/test/';
  212. });
  213. $user = $this->createMock(IUser::class);
  214. $user->expects($this->any())->method('getUID')->willReturn('user001');
  215. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  216. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  217. $this->appManager->expects($this->any())
  218. ->method('getEnabledAppsForUser')
  219. ->with($user)
  220. ->willReturn(['test']);
  221. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  222. $subadmin = $this->createMock(SubAdmin::class);
  223. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  224. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  225. $this->navigationManager->clear();
  226. $entries = $this->navigationManager->getAll('all');
  227. $this->assertEquals($expected, $entries);
  228. }
  229. public function providesNavigationConfig() {
  230. $apps = [
  231. 'core_apps' => [
  232. 'id' => 'core_apps',
  233. 'order' => 5,
  234. 'href' => '/apps/test/',
  235. 'icon' => '/apps/settings/img/apps.svg',
  236. 'name' => 'Apps',
  237. 'active' => false,
  238. 'type' => 'settings',
  239. 'classes' => '',
  240. 'unread' => 0
  241. ]
  242. ];
  243. $defaults = [
  244. 'accessibility_settings' => [
  245. 'type' => 'settings',
  246. 'id' => 'accessibility_settings',
  247. 'order' => 2,
  248. 'href' => '/apps/test/',
  249. 'name' => 'Appearance and accessibility',
  250. 'icon' => '/apps/theming/img/accessibility-dark.svg',
  251. 'active' => false,
  252. 'classes' => '',
  253. 'unread' => 0,
  254. ],
  255. 'settings' => [
  256. 'id' => 'settings',
  257. 'order' => 3,
  258. 'href' => '/apps/test/',
  259. 'icon' => '/apps/settings/img/admin.svg',
  260. 'name' => 'Settings',
  261. 'active' => false,
  262. 'type' => 'settings',
  263. 'classes' => '',
  264. 'unread' => 0
  265. ],
  266. 'logout' => [
  267. 'id' => 'logout',
  268. 'order' => 99999,
  269. 'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
  270. 'icon' => '/apps/core/img/actions/logout.svg',
  271. 'name' => 'Log out',
  272. 'active' => false,
  273. 'type' => 'settings',
  274. 'classes' => '',
  275. 'unread' => 0
  276. ]
  277. ];
  278. $adminSettings = [
  279. 'accessibility_settings' => $defaults['accessibility_settings'],
  280. 'settings' => [
  281. 'id' => 'settings',
  282. 'order' => 3,
  283. 'href' => '/apps/test/',
  284. 'icon' => '/apps/settings/img/personal.svg',
  285. 'name' => 'Personal settings',
  286. 'active' => false,
  287. 'type' => 'settings',
  288. 'classes' => '',
  289. 'unread' => 0
  290. ],
  291. 'admin_settings' => [
  292. 'id' => 'admin_settings',
  293. 'order' => 4,
  294. 'href' => '/apps/test/',
  295. 'icon' => '/apps/settings/img/admin.svg',
  296. 'name' => 'Administration settings',
  297. 'active' => false,
  298. 'type' => 'settings',
  299. 'classes' => '',
  300. 'unread' => 0
  301. ]
  302. ];
  303. return [
  304. 'minimalistic' => [
  305. array_merge(
  306. ['accessibility_settings' => $defaults['accessibility_settings']],
  307. ['settings' => $defaults['settings']],
  308. ['test' => [
  309. 'id' => 'test',
  310. 'order' => 100,
  311. 'href' => '/apps/test/',
  312. 'icon' => '/apps/test/img/app.svg',
  313. 'name' => 'Test',
  314. 'active' => false,
  315. 'type' => 'link',
  316. 'classes' => '',
  317. 'unread' => 0
  318. ]],
  319. ['logout' => $defaults['logout']]
  320. ),
  321. ['navigations' => [
  322. 'navigation' => [
  323. ['route' => 'test.page.index', 'name' => 'Test']
  324. ]
  325. ]]
  326. ],
  327. 'minimalistic-settings' => [
  328. array_merge(
  329. ['accessibility_settings' => $defaults['accessibility_settings']],
  330. ['settings' => $defaults['settings']],
  331. ['test' => [
  332. 'id' => 'test',
  333. 'order' => 100,
  334. 'href' => '/apps/test/',
  335. 'icon' => '/apps/test/img/app.svg',
  336. 'name' => 'Test',
  337. 'active' => false,
  338. 'type' => 'settings',
  339. 'classes' => '',
  340. 'unread' => 0
  341. ]],
  342. ['logout' => $defaults['logout']]
  343. ),
  344. ['navigations' => [
  345. 'navigation' => [
  346. ['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
  347. ],
  348. ]]
  349. ],
  350. 'admin' => [
  351. array_merge(
  352. $adminSettings,
  353. $apps,
  354. ['test' => [
  355. 'id' => 'test',
  356. 'order' => 100,
  357. 'href' => '/apps/test/',
  358. 'icon' => '/apps/test/img/app.svg',
  359. 'name' => 'Test',
  360. 'active' => false,
  361. 'type' => 'link',
  362. 'classes' => '',
  363. 'unread' => 0
  364. ]],
  365. ['logout' => $defaults['logout']]
  366. ),
  367. ['navigations' => [
  368. 'navigation' => [
  369. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  370. ],
  371. ]],
  372. true
  373. ],
  374. 'no name' => [
  375. array_merge(
  376. $adminSettings,
  377. $apps,
  378. ['logout' => $defaults['logout']]
  379. ),
  380. ['navigations' => [
  381. 'navigation' => [
  382. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
  383. ],
  384. ]],
  385. true
  386. ],
  387. 'no admin' => [
  388. $defaults,
  389. ['navigations' => [[
  390. '@attributes' => ['role' => 'admin'],
  391. 'route' => 'test.page.index',
  392. 'name' => 'Test'
  393. ]]]
  394. ]
  395. ];
  396. }
  397. }