MenuTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model;
  7. /**
  8. * Test class for \Magento\Backend\Model\Auth.
  9. *
  10. * @magentoAppArea adminhtml
  11. */
  12. class MenuTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Backend\Model\Menu
  16. */
  17. private $model;
  18. /** @var \Magento\Framework\ObjectManagerInterface */
  19. private $objectManager;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. \Magento\TestFramework\Helper\Bootstrap::getInstance()
  24. ->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  25. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  26. $this->model = $this->objectManager->create(\Magento\Backend\Model\Auth::class);
  27. $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class)
  28. ->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  29. }
  30. public function testMenuItemManipulation()
  31. {
  32. /* @var $menu \Magento\Backend\Model\Menu */
  33. $menu = $this->objectManager->create(\Magento\Backend\Model\Menu\Config::class)->getMenu();
  34. /* @var $itemFactory \Magento\Backend\Model\Menu\Item\Factory */
  35. $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class);
  36. // Add new item in top level
  37. $menu->add(
  38. $itemFactory->create(
  39. [
  40. 'id' => 'Magento_Backend::system2',
  41. 'title' => 'Extended System',
  42. 'module' => 'Magento_Backend',
  43. 'resource' => 'Magento_Backend::system2',
  44. ]
  45. )
  46. );
  47. // Add submenu
  48. $menu->add(
  49. $itemFactory->create(
  50. [
  51. 'id' => 'Magento_Backend::system2_acl',
  52. 'title' => 'Acl',
  53. 'module' => 'Magento_Backend',
  54. 'action' => 'admin/backend/acl/index',
  55. 'resource' => 'Magento_Backend::system2_acl',
  56. ]
  57. ),
  58. 'Magento_Backend::system2'
  59. );
  60. // Modify existing menu item
  61. $menu->get('Magento_Backend::system2')->setTitle('Base system')->setAction('admin/backend/system/base');
  62. // remove dependency from config
  63. // Change sort order
  64. $menu->reorder('Magento_Backend::system', 40);
  65. // Remove menu item
  66. $menu->remove('Magento_Backend::catalog_attribute');
  67. // Move menu item
  68. $menu->move('Magento_Catalog::catalog_products', 'Magento_Backend::system2');
  69. }
  70. /**
  71. * @magentoAppIsolation enabled
  72. */
  73. public function testSerialize()
  74. {
  75. /** @var Menu $menu */
  76. $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create();
  77. /* @var \Magento\Backend\Model\Menu\Item\Factory $itemFactory */
  78. $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class);
  79. // Add new item in top level
  80. $menu->add(
  81. $itemFactory->create(
  82. [
  83. 'id' => 'Magento_Backend::system3',
  84. 'title' => 'Extended System',
  85. 'module' => 'Magento_Backend',
  86. 'resource' => 'Magento_Backend::system3',
  87. 'dependsOnConfig' => 'dev/test/system',
  88. ]
  89. )
  90. );
  91. // Add submenu
  92. $menu->add(
  93. $itemFactory->create(
  94. [
  95. 'id' => 'Magento_Backend::system3_acl',
  96. 'title' => 'Acl',
  97. 'module' => 'Magento_Backend',
  98. 'action' => 'admin/backend/acl/index',
  99. 'resource' => 'Magento_Backend::system3_acl',
  100. ]
  101. ),
  102. 'Magento_Backend::system3'
  103. );
  104. $serializedString = $menu->serialize();
  105. $expected = '[{"parent_id":null,"module":"Magento_Backend","sort_index":null,'
  106. . '"dependsOnConfig":"dev\/test\/system",'
  107. . '"id":"Magento_Backend::system3","resource":"Magento_Backend::system3","path":"","action":null,'
  108. . '"dependsOnModule":null,"toolTip":null,"title":"Extended System",'
  109. . '"target":null,"sub_menu":[{"parent_id":null,"module":"Magento_Backend","sort_index":null,'
  110. . '"dependsOnConfig":null,"id":"Magento_Backend::system3_acl","resource":"Magento_Backend::system3_acl",'
  111. . '"path":"","action":"admin\/backend\/acl\/index","dependsOnModule":null,"toolTip":null,"title":"Acl",'
  112. . '"target":null,"sub_menu":null}]}]';
  113. $this->assertEquals($expected, $serializedString);
  114. }
  115. /**
  116. * @magentoAppIsolation enabled
  117. */
  118. public function testUnserialize()
  119. {
  120. $serializedMenu = '[{"parent_id":null,"module":"Magento_Backend","sort_index":null,'
  121. . '"dependsOnConfig":"dev\/test","id":"Magento_Backend::system3","resource":"Magento_Backend::system3",'
  122. . '"path":"","action":null,"dependsOnModule":null,"toolTip":null,"title":"Extended System",'
  123. . '"target":null,"sub_menu":[{"parent_id":null,"module":"Magento_Backend","sort_index":null,'
  124. . '"dependsOnConfig":null,"id":"Magento_Backend::system3_acl","resource":"Magento_Backend::system3_acl",'
  125. . '"path":"","action":"admin\/backend\/acl\/index","dependsOnModule":null,"toolTip":null,"title":"Acl",'
  126. . '"target":null,"sub_menu":null}]}]';
  127. /** @var Menu $menu */
  128. $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create();
  129. $menu->unserialize($serializedMenu);
  130. $expected = [
  131. [
  132. 'parent_id' => null,
  133. 'module' => 'Magento_Backend',
  134. 'sort_index' => null,
  135. 'dependsOnConfig' => 'dev/test',
  136. 'id' => 'Magento_Backend::system3',
  137. 'resource' => 'Magento_Backend::system3',
  138. 'path' => '',
  139. 'action' => null,
  140. 'dependsOnModule' => null,
  141. 'toolTip' => null,
  142. 'title' => 'Extended System',
  143. 'target' => null,
  144. 'sub_menu' =>
  145. [
  146. [
  147. 'parent_id' => null,
  148. 'module' => 'Magento_Backend',
  149. 'sort_index' => null,
  150. 'dependsOnConfig' => null,
  151. 'id' => 'Magento_Backend::system3_acl',
  152. 'resource' => 'Magento_Backend::system3_acl',
  153. 'path' => '',
  154. 'action' => 'admin/backend/acl/index',
  155. 'dependsOnModule' => null,
  156. 'toolTip' => null,
  157. 'title' => 'Acl',
  158. 'sub_menu' => null,
  159. 'target' => null
  160. ],
  161. ],
  162. ],
  163. ];
  164. $this->assertEquals($expected, $menu->toArray());
  165. }
  166. }