1
0

NavigationManagerTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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\NavigationManager;
  15. use OCP\IConfig;
  16. use OCP\IGroupManager;
  17. use OCP\IL10N;
  18. use OCP\IURLGenerator;
  19. use OCP\IUser;
  20. use OCP\IUserSession;
  21. use OCP\L10N\IFactory;
  22. class NavigationManagerTest extends TestCase {
  23. /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $appManager;
  25. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $urlGenerator;
  27. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $l10nFac;
  29. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $userSession;
  31. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $groupManager;
  33. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $config;
  35. /** @var \OC\NavigationManager */
  36. protected $navigationManager;
  37. protected function setUp() {
  38. parent::setUp();
  39. $this->appManager = $this->createMock(AppManager::class);
  40. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  41. $this->l10nFac = $this->createMock(IFactory::class);
  42. $this->userSession = $this->createMock(IUserSession::class);
  43. $this->groupManager = $this->createMock(IGroupManager::class);
  44. $this->config = $this->createMock(IConfig::class);
  45. $this->navigationManager = new NavigationManager(
  46. $this->appManager,
  47. $this->urlGenerator,
  48. $this->l10nFac,
  49. $this->userSession,
  50. $this->groupManager,
  51. $this->config
  52. );
  53. $this->navigationManager->clear(false);
  54. }
  55. public function addArrayData() {
  56. return [
  57. [
  58. [
  59. 'id' => 'entry id',
  60. 'name' => 'link text',
  61. 'order' => 1,
  62. 'icon' => 'optional',
  63. 'href' => 'url',
  64. 'type' => 'settings',
  65. ],
  66. [
  67. 'id' => 'entry id',
  68. 'name' => 'link text',
  69. 'order' => 1,
  70. 'icon' => 'optional',
  71. 'href' => 'url',
  72. 'active' => false,
  73. 'type' => 'settings',
  74. ],
  75. ],
  76. [
  77. [
  78. 'id' => 'entry id',
  79. 'name' => 'link text',
  80. 'order' => 1,
  81. //'icon' => 'optional',
  82. 'href' => 'url',
  83. 'active' => true,
  84. ],
  85. [
  86. 'id' => 'entry id',
  87. 'name' => 'link text',
  88. 'order' => 1,
  89. 'icon' => '',
  90. 'href' => 'url',
  91. 'active' => false,
  92. 'type' => 'link',
  93. ],
  94. ],
  95. ];
  96. }
  97. /**
  98. * @dataProvider addArrayData
  99. *
  100. * @param array $entry
  101. * @param array $expectedEntry
  102. */
  103. public function testAddArray(array $entry, array $expectedEntry) {
  104. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists');
  105. $this->navigationManager->add($entry);
  106. $navigationEntries = $this->navigationManager->getAll('all');
  107. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  108. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  109. $this->navigationManager->clear(false);
  110. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  111. }
  112. /**
  113. * @dataProvider addArrayData
  114. *
  115. * @param array $entry
  116. * @param array $expectedEntry
  117. */
  118. public function testAddClosure(array $entry, array $expectedEntry) {
  119. global $testAddClosureNumberOfCalls;
  120. $testAddClosureNumberOfCalls = 0;
  121. $this->navigationManager->add(function () use ($entry) {
  122. global $testAddClosureNumberOfCalls;
  123. $testAddClosureNumberOfCalls++;
  124. return $entry;
  125. });
  126. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  127. $navigationEntries = $this->navigationManager->getAll('all');
  128. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()');
  129. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  130. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  131. $navigationEntries = $this->navigationManager->getAll('all');
  132. $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()');
  133. $this->assertCount(1, $navigationEntries, 'Expected that 1 navigation entry exists');
  134. $this->assertEquals($expectedEntry, $navigationEntries[0]);
  135. $this->navigationManager->clear(false);
  136. $this->assertEmpty($this->navigationManager->getAll('all'), 'Expected no navigation entry exists after clear()');
  137. }
  138. public function testAddArrayClearGetAll() {
  139. $entry = [
  140. 'id' => 'entry id',
  141. 'name' => 'link text',
  142. 'order' => 1,
  143. 'icon' => 'optional',
  144. 'href' => 'url',
  145. ];
  146. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  147. $this->navigationManager->add($entry);
  148. $this->navigationManager->clear(false);
  149. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  150. }
  151. public function testAddClosureClearGetAll() {
  152. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists');
  153. $entry = [
  154. 'id' => 'entry id',
  155. 'name' => 'link text',
  156. 'order' => 1,
  157. 'icon' => 'optional',
  158. 'href' => 'url',
  159. ];
  160. global $testAddClosureNumberOfCalls;
  161. $testAddClosureNumberOfCalls = 0;
  162. $this->navigationManager->add(function () use ($entry) {
  163. global $testAddClosureNumberOfCalls;
  164. $testAddClosureNumberOfCalls++;
  165. return $entry;
  166. });
  167. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()');
  168. $this->navigationManager->clear(false);
  169. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()');
  170. $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
  171. $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
  172. }
  173. /**
  174. * @dataProvider providesNavigationConfig
  175. */
  176. public function testWithAppManager($expected, $navigation, $isAdmin = false) {
  177. $l = $this->createMock(IL10N::class);
  178. $l->expects($this->any())->method('t')->willReturnCallback(function($text, $parameters = []) {
  179. return vsprintf($text, $parameters);
  180. });
  181. $this->appManager->expects($this->once())->method('getInstalledApps')->willReturn(['test']);
  182. $this->appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($navigation);
  183. $this->l10nFac->expects($this->exactly(count($expected) + 1))->method('get')->willReturn($l);
  184. $this->urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function($appName, $file) {
  185. return "/apps/$appName/img/$file";
  186. });
  187. $this->urlGenerator->expects($this->exactly(count($expected)))->method('linkToRoute')->willReturnCallback(function() {
  188. return "/apps/test/";
  189. });
  190. $user = $this->createMock(IUser::class);
  191. $user->expects($this->any())->method('getUID')->willReturn('user001');
  192. $this->userSession->expects($this->any())->method('getUser')->willReturn($user);
  193. $this->groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
  194. $this->navigationManager->clear();
  195. $entries = $this->navigationManager->getAll('all');
  196. $this->assertEquals($expected, $entries);
  197. }
  198. public function providesNavigationConfig() {
  199. return [
  200. 'minimalistic' => [[[
  201. 'id' => 'test',
  202. 'order' => 100,
  203. 'href' => '/apps/test/',
  204. 'icon' => '/apps/test/img/app.svg',
  205. 'name' => 'Test',
  206. 'active' => false,
  207. 'type' => 'link',
  208. ]], ['navigations' => [['route' => 'test.page.index', 'name' => 'Test']]]],
  209. 'minimalistic-settings' => [[[
  210. 'id' => 'test',
  211. 'order' => 100,
  212. 'href' => '/apps/test/',
  213. 'icon' => '/apps/test/img/app.svg',
  214. 'name' => 'Test',
  215. 'active' => false,
  216. 'type' => 'settings',
  217. ]], ['navigations' => [['route' => 'test.page.index', 'name' => 'Test', 'type' => 'settings']]]],
  218. 'no admin' => [[[
  219. 'id' => 'test',
  220. 'order' => 100,
  221. 'href' => '/apps/test/',
  222. 'icon' => '/apps/test/img/app.svg',
  223. 'name' => 'Test',
  224. 'active' => false,
  225. 'type' => 'link',
  226. ]], ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]], true],
  227. 'no name' => [[], ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']]], true],
  228. 'admin' => [[], ['navigations' => [['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index', 'name' => 'Test']]]]
  229. ];
  230. }
  231. }