NavigationManagerTest.php 7.2 KB

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