NavigationManagerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  191. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  192. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  193. return "/apps/$appName/img/$file";
  194. });
  195. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  196. if ($route === 'core.login.logout') {
  197. return 'https://example.com/logout';
  198. }
  199. return '/apps/test/';
  200. });
  201. $user = $this->createMock(IUser::class);
  202. $user->expects($this->any())->method('getUID')->willReturn('user001');
  203. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  204. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  205. $this->appManager->expects($this->once())
  206. ->method('getEnabledAppsForUser')
  207. ->with($user)
  208. ->willReturn(['test']);
  209. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  210. $subadmin = $this->createMock(SubAdmin::class);
  211. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  212. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  213. $this->navigationManager->clear();
  214. $entries = $this->navigationManager->getAll('all');
  215. $this->assertEquals($expected, $entries);
  216. }
  217. public function providesNavigationConfig() {
  218. $apps = [
  219. 'core_apps' => [
  220. 'id' => 'core_apps',
  221. 'order' => 4,
  222. 'href' => '/apps/test/',
  223. 'icon' => '/apps/settings/img/apps.svg',
  224. 'name' => 'Apps',
  225. 'active' => false,
  226. 'type' => 'settings',
  227. 'classes' => '',
  228. 'unread' => 0
  229. ]
  230. ];
  231. $defaults = [
  232. 'settings' => [
  233. 'id' => 'settings',
  234. 'order' => 2,
  235. 'href' => '/apps/test/',
  236. 'icon' => '/apps/settings/img/admin.svg',
  237. 'name' => 'Settings',
  238. 'active' => false,
  239. 'type' => 'settings',
  240. 'classes' => '',
  241. 'unread' => 0
  242. ],
  243. 'logout' => [
  244. 'id' => 'logout',
  245. 'order' => 99999,
  246. 'href' => 'https://example.com/logout?requesttoken='. urlencode(\OCP\Util::callRegister()),
  247. 'icon' => '/apps/core/img/actions/logout.svg',
  248. 'name' => 'Log out',
  249. 'active' => false,
  250. 'type' => 'settings',
  251. 'classes' => '',
  252. 'unread' => 0
  253. ]
  254. ];
  255. return [
  256. 'minimalistic' => [
  257. array_merge(
  258. ['settings' => $defaults['settings']],
  259. ['test' => [
  260. 'id' => 'test',
  261. 'order' => 100,
  262. 'href' => '/apps/test/',
  263. 'icon' => '/apps/test/img/app.svg',
  264. 'name' => 'Test',
  265. 'active' => false,
  266. 'type' => 'link',
  267. 'classes' => '',
  268. 'unread' => 0
  269. ]],
  270. ['logout' => $defaults['logout']]
  271. ),
  272. ['navigations' => [
  273. 'navigation' => [
  274. ['route' => 'test.page.index', 'name' => 'Test']
  275. ]
  276. ]]
  277. ],
  278. 'minimalistic-settings' => [
  279. array_merge(
  280. ['settings' => $defaults['settings']],
  281. ['test' => [
  282. 'id' => 'test',
  283. 'order' => 100,
  284. 'href' => '/apps/test/',
  285. 'icon' => '/apps/test/img/app.svg',
  286. 'name' => 'Test',
  287. 'active' => false,
  288. 'type' => 'settings',
  289. 'classes' => '',
  290. 'unread' => 0
  291. ]],
  292. ['logout' => $defaults['logout']]
  293. ),
  294. ['navigations' => [
  295. 'navigation' => [
  296. ['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
  297. ],
  298. ]]
  299. ],
  300. 'admin' => [
  301. array_merge(
  302. ['settings' => $defaults['settings']],
  303. $apps,
  304. ['test' => [
  305. 'id' => 'test',
  306. 'order' => 100,
  307. 'href' => '/apps/test/',
  308. 'icon' => '/apps/test/img/app.svg',
  309. 'name' => 'Test',
  310. 'active' => false,
  311. 'type' => 'link',
  312. 'classes' => '',
  313. 'unread' => 0
  314. ]],
  315. ['logout' => $defaults['logout']]
  316. ),
  317. ['navigations' => [
  318. 'navigation' => [
  319. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  320. ],
  321. ]],
  322. true
  323. ],
  324. 'no name' => [
  325. array_merge(
  326. ['settings' => $defaults['settings']],
  327. $apps,
  328. ['logout' => $defaults['logout']]
  329. ),
  330. ['navigations' => [
  331. 'navigation' => [
  332. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
  333. ],
  334. ]],
  335. true
  336. ],
  337. 'no admin' => [
  338. $defaults,
  339. ['navigations' => [[
  340. '@attributes' => ['role' => 'admin'],
  341. 'route' => 'test.page.index',
  342. 'name' => 'Test'
  343. ]]]
  344. ]
  345. ];
  346. }
  347. }