NavigationManagerTest.php 11 KB

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