NavigationManagerTest.php 11 KB

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