NavigationManagerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 OCP\IConfig;
  14. use OCP\IGroupManager;
  15. use OCP\IL10N;
  16. use OCP\IURLGenerator;
  17. use OCP\IUser;
  18. use OCP\IUserSession;
  19. use OCP\L10N\IFactory;
  20. use OC\App\AppManager;
  21. use OC\Group\Manager;
  22. use OC\NavigationManager;
  23. use OC\SubAdmin;
  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() {
  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. ],
  69. 'entry id2' => [
  70. 'id' => 'entry id',
  71. 'name' => 'link text',
  72. 'order' => 1,
  73. 'icon' => 'optional',
  74. 'href' => 'url',
  75. 'active' => false,
  76. 'type' => 'settings',
  77. 'classes' => ''
  78. ]
  79. ],
  80. [
  81. 'entry id' => [
  82. 'id' => 'entry id',
  83. 'name' => 'link text',
  84. 'order' => 1,
  85. //'icon' => 'optional',
  86. 'href' => 'url',
  87. 'active' => true
  88. ],
  89. 'entry id2' => [
  90. 'id' => 'entry id',
  91. 'name' => 'link text',
  92. 'order' => 1,
  93. 'icon' => '',
  94. 'href' => 'url',
  95. 'active' => false,
  96. 'type' => 'link',
  97. 'classes' => ''
  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. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  187. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  188. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function ($appName, $file) {
  189. return "/apps/$appName/img/$file";
  190. });
  191. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function ($route) {
  192. if ($route === 'core.login.logout') {
  193. return 'https://example.com/logout';
  194. }
  195. return '/apps/test/';
  196. });
  197. $user = $this->createMock(IUser::class);
  198. $user->expects($this->any())->method('getUID')->willReturn('user001');
  199. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  200. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  201. $this->appManager->expects($this->once())
  202. ->method('getEnabledAppsForUser')
  203. ->with($user)
  204. ->willReturn(['test']);
  205. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  206. $subadmin = $this->createMock(SubAdmin::class);
  207. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  208. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  209. $this->navigationManager->clear();
  210. $entries = $this->navigationManager->getAll('all');
  211. $this->assertEquals($expected, $entries);
  212. }
  213. public function providesNavigationConfig() {
  214. $apps = [
  215. 'core_apps' => [
  216. 'id' => 'core_apps',
  217. 'order' => 3,
  218. 'href' => '/apps/test/',
  219. 'icon' => '/apps/settings/img/apps.svg',
  220. 'name' => 'Apps',
  221. 'active' => false,
  222. 'type' => 'settings',
  223. 'classes' => ''
  224. ]
  225. ];
  226. $defaults = [
  227. 'settings' => [
  228. 'id' => 'settings',
  229. 'order' => 1,
  230. 'href' => '/apps/test/',
  231. 'icon' => '/apps/settings/img/admin.svg',
  232. 'name' => 'Settings',
  233. 'active' => false,
  234. 'type' => 'settings',
  235. 'classes' => ''
  236. ],
  237. 'logout' => [
  238. 'id' => 'logout',
  239. 'order' => 99999,
  240. 'href' => 'https://example.com/logout',
  241. 'icon' => '/apps/core/img/actions/logout.svg',
  242. 'name' => 'Log out',
  243. 'active' => false,
  244. 'type' => 'settings',
  245. 'classes' => ''
  246. ]
  247. ];
  248. return [
  249. 'minimalistic' => [
  250. array_merge(
  251. ['settings' => $defaults['settings']],
  252. ['test' => [
  253. 'id' => 'test',
  254. 'order' => 100,
  255. 'href' => '/apps/test/',
  256. 'icon' => '/apps/test/img/app.svg',
  257. 'name' => 'Test',
  258. 'active' => false,
  259. 'type' => 'link',
  260. 'classes' => ''
  261. ]],
  262. ['logout' => $defaults['logout']]
  263. ),
  264. ['navigations' => [
  265. 'navigation' => [
  266. ['route' => 'test.page.index', 'name' => 'Test']
  267. ]
  268. ]]
  269. ],
  270. 'minimalistic-settings' => [
  271. array_merge(
  272. ['settings' => $defaults['settings']],
  273. ['test' => [
  274. 'id' => 'test',
  275. 'order' => 100,
  276. 'href' => '/apps/test/',
  277. 'icon' => '/apps/test/img/app.svg',
  278. 'name' => 'Test',
  279. 'active' => false,
  280. 'type' => 'settings',
  281. 'classes' => ''
  282. ]],
  283. ['logout' => $defaults['logout']]
  284. ),
  285. ['navigations' => [
  286. 'navigation' => [
  287. ['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']
  288. ],
  289. ]]
  290. ],
  291. 'admin' => [
  292. array_merge(
  293. ['settings' => $defaults['settings']],
  294. $apps,
  295. ['test' => [
  296. 'id' => 'test',
  297. 'order' => 100,
  298. 'href' => '/apps/test/',
  299. 'icon' => '/apps/test/img/app.svg',
  300. 'name' => 'Test',
  301. 'active' => false,
  302. 'type' => 'link',
  303. 'classes' => ''
  304. ]],
  305. ['logout' => $defaults['logout']]
  306. ),
  307. ['navigations' => [
  308. 'navigation' => [
  309. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']
  310. ],
  311. ]],
  312. true
  313. ],
  314. 'no name' => [
  315. array_merge(
  316. ['settings' => $defaults['settings']],
  317. $apps,
  318. ['logout' => $defaults['logout']]
  319. ),
  320. ['navigations' => [
  321. 'navigation' => [
  322. ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']
  323. ],
  324. ]],
  325. true
  326. ],
  327. 'no admin' => [
  328. $defaults,
  329. ['navigations' => [[
  330. '@attributes' => ['role' => 'admin'],
  331. 'route' => 'test.page.index',
  332. 'name' => 'Test'
  333. ]]]
  334. ]
  335. ];
  336. }
  337. }