1
0

NavigationManagerTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. 'classes' => '',
  68. ],
  69. [
  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. [
  82. 'id' => 'entry id',
  83. 'name' => 'link text',
  84. 'order' => 1,
  85. //'icon' => 'optional',
  86. 'href' => 'url',
  87. 'active' => true,
  88. ],
  89. [
  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[0]);
  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[0]);
  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[0]);
  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. [
  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. [
  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. [
  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' => [array_merge([$defaults[0]], [[
  257. 'id' => 'test',
  258. 'order' => 100,
  259. 'href' => '/apps/test/',
  260. 'icon' => '/apps/test/img/app.svg',
  261. 'name' => 'Test',
  262. 'active' => false,
  263. 'type' => 'link',
  264. 'classes' => '',
  265. ]], [$defaults[1]]), ['navigations' => [['route' => 'test.page.index', 'name' => 'Test']]]],
  266. 'minimalistic-settings' => [array_merge([$defaults[0]], [[
  267. 'id' => 'test',
  268. 'order' => 100,
  269. 'href' => '/apps/test/',
  270. 'icon' => '/apps/test/img/app.svg',
  271. 'name' => 'Test',
  272. 'active' => false,
  273. 'type' => 'settings',
  274. 'classes' => '',
  275. ]], [$defaults[1]]), ['navigations' => [['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']]]],
  276. 'admin' => [array_merge([$defaults[0]], $apps, [[
  277. 'id' => 'test',
  278. 'order' => 100,
  279. 'href' => '/apps/test/',
  280. 'icon' => '/apps/test/img/app.svg',
  281. 'name' => 'Test',
  282. 'active' => false,
  283. 'type' => 'link',
  284. 'classes' => '',
  285. ]], [$defaults[1]]), ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]], true],
  286. 'no name' => [array_merge([$defaults[0]], $apps, [$defaults[1]]), ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']]], true],
  287. 'no admin' => [$defaults, ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]]]
  288. ];
  289. }
  290. }