NavigationManagerTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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() {
  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. [
  61. 'id' => 'entry id',
  62. 'name' => 'link text',
  63. 'order' => 1,
  64. 'icon' => 'optional',
  65. 'href' => 'url',
  66. 'type' => 'settings',
  67. ],
  68. [
  69. 'id' => 'entry id',
  70. 'name' => 'link text',
  71. 'order' => 1,
  72. 'icon' => 'optional',
  73. 'href' => 'url',
  74. 'active' => false,
  75. 'type' => 'settings',
  76. ],
  77. ],
  78. [
  79. [
  80. 'id' => 'entry id',
  81. 'name' => 'link text',
  82. 'order' => 1,
  83. //'icon' => 'optional',
  84. 'href' => 'url',
  85. 'active' => true,
  86. ],
  87. [
  88. 'id' => 'entry id',
  89. 'name' => 'link text',
  90. 'order' => 1,
  91. 'icon' => '',
  92. 'href' => 'url',
  93. 'active' => false,
  94. 'type' => 'link',
  95. ],
  96. ],
  97. ];
  98. }
  99. /**
  100. * @dataProvider addArrayData
  101. *
  102. * @param array $entry
  103. * @param array $expectedEntry
  104. */
  105. public function testAddArray(array $entry, array $expectedEntry) {
  106. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
  107. $this->navigationManager->add($entry);
  108. $navigationEntries = $this->navigationManager->getAll('all');
  109. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  110. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  111. $this->navigationManager->clear(false);
  112. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  113. }
  114. /**
  115. * @dataProvider addArrayData
  116. *
  117. * @param array $entry
  118. * @param array $expectedEntry
  119. */
  120. public function testAddClosure(array $entry, array $expectedEntry) {
  121. global $testAddClosureNumberOfCalls;
  122. $testAddClosureNumberOfCalls = 0;
  123. $this->navigationManager->add(function () use ($entry) {
  124. global $testAddClosureNumberOfCalls;
  125. $testAddClosureNumberOfCalls++;
  126. return $entry;
  127. });
  128. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  129. $navigationEntries = $this->navigationManager->getAll('all');
  130. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
  131. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  132. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  133. $navigationEntries = $this->navigationManager->getAll('all');
  134. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
  135. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  136. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  137. $this->navigationManager->clear(false);
  138. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  139. }
  140. public function testAddArrayClearGetAll() {
  141. $entry = [
  142. 'id' => 'entry id',
  143. 'name' => 'link text',
  144. 'order' => 1,
  145. 'icon' => 'optional',
  146. 'href' => 'url',
  147. ];
  148. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  149. $this->navigationManager->add($entry);
  150. $this->navigationManager->clear(false);
  151. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  152. }
  153. public function testAddClosureClearGetAll() {
  154. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  155. $entry = [
  156. 'id' => 'entry id',
  157. 'name' => 'link text',
  158. 'order' => 1,
  159. 'icon' => 'optional',
  160. 'href' => 'url',
  161. ];
  162. global $testAddClosureNumberOfCalls;
  163. $testAddClosureNumberOfCalls = 0;
  164. $this->navigationManager->add(function () use ($entry) {
  165. global $testAddClosureNumberOfCalls;
  166. $testAddClosureNumberOfCalls++;
  167. return $entry;
  168. });
  169. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  170. $this->navigationManager->clear(false);
  171. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
  172. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  173. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
  174. }
  175. /**
  176. * @dataProvider providesNavigationConfig
  177. */
  178. public function testWithAppManager($expected, $navigation, $isAdmin = false) {
  179. $l = $this->createMock(IL10N::class);
  180. $l->expects($this->any())->method('t')->willReturnCallback(function($text, $parameters = []) {
  181. return vsprintf($text, $parameters);
  182. });
  183. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  184. $this->l10nFac->expects($this->any())->method('get')->willReturn($l);
  185. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function($appName, $file) {
  186. return "/apps/$appName/img/$file";
  187. });
  188. $this->urlGenerator->expects($this->any())->method('linkToRoute')->willReturnCallback(function() {
  189. return "/apps/test/";
  190. });
  191. $user = $this->createMock(IUser::class);
  192. $user->expects($this->any())->method('getUID')->willReturn('user001');
  193. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  194. $this->userSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
  195. $this->appManager->expects($this->once())
  196. ->method('getEnabledAppsForUser')
  197. ->with($user)
  198. ->willReturn(['test']);
  199. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  200. $subadmin = $this->createMock(SubAdmin::class);
  201. $subadmin->expects($this->any())->method('isSubAdmin')->with($user)->willReturn(false);
  202. $this->groupManager->expects($this->any())->method('getSubAdmin')->willReturn($subadmin);
  203. $this->navigationManager->clear();
  204. $entries = $this->navigationManager->getAll('all');
  205. $this->assertEquals($expected, $entries);
  206. }
  207. public function providesNavigationConfig() {
  208. $apps = [
  209. [
  210. 'id' => 'core_apps',
  211. 'order' => 3,
  212. 'href' => '/apps/test/',
  213. 'icon' => '/apps/settings/img/apps.svg',
  214. 'name' => 'Apps',
  215. 'active' => false,
  216. 'type' => 'settings',
  217. ]
  218. ];
  219. $defaults = [
  220. [
  221. 'id' => 'settings',
  222. 'order' => 1,
  223. 'href' => '/apps/test/',
  224. 'icon' => '/apps/settings/img/admin.svg',
  225. 'name' => 'Settings',
  226. 'active' => false,
  227. 'type' => 'settings',
  228. ],
  229. [
  230. 'id' => 'logout',
  231. 'order' => 99999,
  232. 'href' => null,
  233. 'icon' => '/apps/core/img/actions/logout.svg',
  234. 'name' => 'Log out',
  235. 'active' => false,
  236. 'type' => 'settings',
  237. ],
  238. ];
  239. return [
  240. 'minimalistic' => [array_merge($defaults, [[
  241. 'id' => 'test',
  242. 'order' => 100,
  243. 'href' => '/apps/test/',
  244. 'icon' => '/apps/test/img/app.svg',
  245. 'name' => 'Test',
  246. 'active' => false,
  247. 'type' => 'link',
  248. ]]), ['navigations' => [['route' => 'test.page.index', 'name' => 'Test']]]],
  249. 'minimalistic-settings' => [array_merge($defaults, [[
  250. 'id' => 'test',
  251. 'order' => 100,
  252. 'href' => '/apps/test/',
  253. 'icon' => '/apps/test/img/app.svg',
  254. 'name' => 'Test',
  255. 'active' => false,
  256. 'type' => 'settings',
  257. ]]), ['navigations' => [['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']]]],
  258. 'admin' => [array_merge($apps, $defaults, [[
  259. 'id' => 'test',
  260. 'order' => 100,
  261. 'href' => '/apps/test/',
  262. 'icon' => '/apps/test/img/app.svg',
  263. 'name' => 'Test',
  264. 'active' => false,
  265. 'type' => 'link',
  266. ]]), ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]], true],
  267. 'no name' => [array_merge($apps, $defaults), ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']]], true],
  268. 'no admin' => [$defaults, ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]]]
  269. ];
  270. }
  271. }