diff --git a/app/code/Magento/Backend/Model/Menu.php b/app/code/Magento/Backend/Model/Menu.php index 516319ad983f9a35804e9933d27bbb42575e7b30..ccae5ebce16a01926aa0166344976185ad6769bf 100644 --- a/app/code/Magento/Backend/Model/Menu.php +++ b/app/code/Magento/Backend/Model/Menu.php @@ -5,6 +5,12 @@ */ namespace Magento\Backend\Model; +use Magento\Backend\Model\Menu\Item; +use Magento\Backend\Model\Menu\Item\Factory; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\SerializerInterface; +use Psr\Log\LoggerInterface; + /** * Backend menu model */ @@ -18,33 +24,54 @@ class Menu extends \ArrayObject protected $_path = ''; /** - * @var \Psr\Log\LoggerInterface + * @var LoggerInterface */ protected $_logger; /** - * @param \Psr\Log\LoggerInterface $logger + * @var Factory + */ + private $menuItemFactory; + + /** + * @var SerializerInterface + */ + private $serializer; + + /** + * Menu constructor + * + * @param LoggerInterface $logger * @param string $pathInMenuStructure + * @param Factory|null $menuItemFactory + * @param SerializerInterface|null $serializer */ - public function __construct(\Psr\Log\LoggerInterface $logger, $pathInMenuStructure = '') - { + public function __construct( + LoggerInterface $logger, + $pathInMenuStructure = '', + Factory $menuItemFactory = null, + SerializerInterface $serializer = null + ) { if ($pathInMenuStructure) { $this->_path = $pathInMenuStructure . '/'; } $this->_logger = $logger; $this->setIteratorClass(\Magento\Backend\Model\Menu\Iterator::class); + $this->menuItemFactory = $menuItemFactory ?: ObjectManager::getInstance() + ->create(Factory::class); + $this->serializer = $serializer ?: ObjectManager::getInstance()->create(SerializerInterface::class); } /** * Add child to menu item * - * @param \Magento\Backend\Model\Menu\Item $item + * @param Item $item * @param string $parentId * @param int $index * @return void * @throws \InvalidArgumentException */ - public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $index = null) + public function add(Item $item, $parentId = null, $index = null) { if ($parentId !== null) { $parentItem = $this->get($parentId); @@ -69,13 +96,13 @@ class Menu extends \ArrayObject * Retrieve menu item by id * * @param string $itemId - * @return \Magento\Backend\Model\Menu\Item|null + * @return Item|null */ public function get($itemId) { $result = null; + /** @var Item $item */ foreach ($this as $item) { - /** @var $item \Magento\Backend\Model\Menu\Item */ if ($item->getId() == $itemId) { $result = $item; break; @@ -116,8 +143,8 @@ class Menu extends \ArrayObject public function remove($itemId) { $result = false; + /** @var Item $item */ foreach ($this as $key => $item) { - /** @var $item \Magento\Backend\Model\Menu\Item */ if ($item->getId() == $itemId) { unset($this[$key]); $result = true; @@ -144,8 +171,8 @@ class Menu extends \ArrayObject public function reorder($itemId, $position) { $result = false; + /** @var Item $item */ foreach ($this as $key => $item) { - /** @var $item \Magento\Backend\Model\Menu\Item */ if ($item->getId() == $itemId) { unset($this[$key]); $this->add($item, null, $position); @@ -161,10 +188,10 @@ class Menu extends \ArrayObject /** * Check whether provided item is last in list * - * @param \Magento\Backend\Model\Menu\Item $item + * @param Item $item * @return bool */ - public function isLast(\Magento\Backend\Model\Menu\Item $item) + public function isLast(Item $item) { return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId(); } @@ -172,12 +199,12 @@ class Menu extends \ArrayObject /** * Find first menu item that user is able to access * - * @return \Magento\Backend\Model\Menu\Item|null + * @return Item|null */ public function getFirstAvailable() { $result = null; - /** @var $item \Magento\Backend\Model\Menu\Item */ + /** @var Item $item */ foreach ($this as $item) { if ($item->isAllowed() && !$item->isDisabled()) { if ($item->hasChildren()) { @@ -198,7 +225,7 @@ class Menu extends \ArrayObject * Get parent items by item id * * @param string $itemId - * @return \Magento\Backend\Model\Menu\Item[] + * @return Item[] */ public function getParentItems($itemId) { @@ -217,8 +244,8 @@ class Menu extends \ArrayObject */ protected function _findParentItems($menu, $itemId, &$parents) { + /** @var Item $item */ foreach ($menu as $item) { - /** @var $item \Magento\Backend\Model\Menu\Item */ if ($item->getId() == $itemId) { return true; } @@ -233,16 +260,54 @@ class Menu extends \ArrayObject } /** - * Hack to unset logger instance which cannot be serialized + * Serialize menu * * @return string */ public function serialize() { - $logger = $this->_logger; - unset($this->_logger); - $result = parent::serialize(); - $this->_logger = $logger; - return $result; + return $this->serializer->serialize($this->toArray()); + } + + /** + * Get menu data represented as an array + * + * @return array + */ + public function toArray() + { + $data = []; + foreach ($this as $item) { + $data[] = $item->toArray(); + } + return $data; + } + + /** + * Unserialize menu + * + * @param string $serialized + * @return void + */ + public function unserialize($serialized) + { + $data = $this->serializer->unserialize($serialized); + $this->populateFromArray($data); + } + + /** + * Populate the menu with data from array + * + * @param array $data + * @return void + */ + public function populateFromArray(array $data) + { + $items = []; + foreach ($data as $itemData) { + $item = $this->menuItemFactory->create($itemData); + $items[] = $item; + } + $this->exchangeArray($items); } } diff --git a/app/code/Magento/Backend/Model/Menu/Item.php b/app/code/Magento/Backend/Model/Menu/Item.php index a49920a67663c7115fde12d799846c1b1da353b1..ef1aa864588f412126bf74e32ca462a41a3182d5 100644 --- a/app/code/Magento/Backend/Model/Menu/Item.php +++ b/app/code/Magento/Backend/Model/Menu/Item.php @@ -4,10 +4,11 @@ * See COPYING.txt for license details. */ -// @codingStandardsIgnoreFile - namespace Magento\Backend\Model\Menu; +use Magento\Backend\Model\Menu; +use Magento\Store\Model\ScopeInterface; + /** * Menu item. Should be used to create nested menu structures with \Magento\Backend\Model\Menu * @@ -102,7 +103,7 @@ class Item /** * Submenu item list * - * @var \Magento\Backend\Model\Menu + * @var Menu */ protected $_submenu; @@ -130,6 +131,7 @@ class Item * Serialized submenu string * * @var string + * @deprecated */ protected $_serializedSubmenu; @@ -167,22 +169,13 @@ class Item ) { $this->_validator = $validator; $this->_validator->validate($data); - $this->_moduleManager = $moduleManager; $this->_acl = $authorization; $this->_scopeConfig = $scopeConfig; $this->_menuFactory = $menuFactory; $this->_urlModel = $urlModel; - $this->_moduleName = isset($data['module']) ? $data['module'] : 'Magento_Backend'; $this->_moduleList = $moduleList; - - $this->_id = $data['id']; - $this->_title = $data['title']; - $this->_action = $this->_getArgument($data, 'action'); - $this->_resource = $this->_getArgument($data, 'resource'); - $this->_dependsOnModule = $this->_getArgument($data, 'dependsOnModule'); - $this->_dependsOnConfig = $this->_getArgument($data, 'dependsOnConfig'); - $this->_tooltip = $this->_getArgument($data, 'toolTip', ''); + $this->populateFromArray($data); } /** @@ -215,13 +208,13 @@ class Item */ public function hasChildren() { - return !is_null($this->_submenu) && (bool)$this->_submenu->count(); + return (null !== $this->_submenu) && (bool)$this->_submenu->count(); } /** * Retrieve submenu * - * @return \Magento\Backend\Model\Menu + * @return Menu */ public function getChildren() { @@ -425,7 +418,7 @@ class Item protected function _isConfigDependenciesAvailable() { if ($this->_dependsOnConfig) { - return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); + return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, ScopeInterface::SCOPE_STORE); } return true; } @@ -445,45 +438,53 @@ class Item } /** - * @return string[] + * Get menu item data represented as an array + * + * @return array */ - public function __sleep() + public function toArray() { - if ($this->_submenu) { - $this->_serializedSubmenu = $this->_submenu->serialize(); - } return [ - '_parentId', - '_moduleName', - '_sortIndex', - '_dependsOnConfig', - '_id', - '_resource', - '_path', - '_action', - '_dependsOnModule', - '_tooltip', - '_title', - '_serializedSubmenu' + 'parent_id' => $this->_parentId, + 'module_name' => $this->_moduleName, + 'sort_index' => $this->_sortIndex, + 'depends_on_config' => $this->_dependsOnConfig, + 'id' => $this->_id, + 'resource' => $this->_resource, + 'path' => $this->_path, + 'action' => $this->_action, + 'depends_on_module' => $this->_dependsOnModule, + 'tooltip' => $this->_tooltip, + 'title' => $this->_title, + 'sub_menu' => isset($this->_submenu) ? $this->_submenu->toArray() : null ]; } /** + * Populate the menu item with data from array + * + * @param array $data * @return void */ - public function __wakeup() + public function populateFromArray(array $data) { - $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); - $this->_moduleManager = $objectManager->get(\Magento\Framework\Module\Manager::class); - $this->_validator = $objectManager->get(\Magento\Backend\Model\Menu\Item\Validator::class); - $this->_acl = $objectManager->get(\Magento\Framework\AuthorizationInterface::class); - $this->_scopeConfig = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class); - $this->_menuFactory = $objectManager->get(\Magento\Backend\Model\MenuFactory::class); - $this->_urlModel = $objectManager->get(\Magento\Backend\Model\UrlInterface::class); - $this->_moduleList = $objectManager->get(\Magento\Framework\Module\ModuleListInterface::class); - if ($this->_serializedSubmenu) { - $this->_submenu = $this->_menuFactory->create(); - $this->_submenu->unserialize($this->_serializedSubmenu); + $this->_parentId = $this->_getArgument($data, 'parent_id'); + $this->_moduleName = $this->_getArgument($data, 'module_name', 'Magento_Backend'); + $this->_sortIndex = $this->_getArgument($data, 'sort_index'); + $this->_dependsOnConfig = $this->_getArgument($data, 'depends_on_config'); + $this->_id = $this->_getArgument($data, 'id'); + $this->_resource = $this->_getArgument($data, 'resource'); + $this->_path = $this->_getArgument($data, 'path', ''); + $this->_action = $this->_getArgument($data, 'action'); + $this->_dependsOnModule = $this->_getArgument($data, 'depends_on_module'); + $this->_tooltip = $this->_getArgument($data, 'tooltip', ''); + $this->_title = $this->_getArgument($data, 'title'); + if (isset($data['sub_menu'])) { + $menu = $this->_menuFactory->create(); + $menu->populateFromArray($data['sub_menu']); + $this->_submenu = $menu; + } else { + $this->_submenu = null; } } } diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/BuilderTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/BuilderTest.php index 7bda9b38a2b95a3a338f5e8e8e07c2463ab734d1..ec015029675eae4591b14bfb9cc02ea0417cdad6 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/BuilderTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/BuilderTest.php @@ -5,33 +5,36 @@ */ namespace Magento\Backend\Test\Unit\Model\Menu; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + class BuilderTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Backend\Model\Menu\Builder */ - protected $_model; + private $model; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject */ - protected $_menuMock; + private $menuMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu\Item\Factory|\PHPUnit_Framework_MockObject_MockObject */ - protected $_factoryMock; + private $factoryMock; protected function setUp() { - $this->_factoryMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Factory::class, [], [], '', false); - $this->_menuMock = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] + $this->factoryMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Factory::class, [], [], '', false); + $this->menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); + + $this->model = (new ObjectManager($this))->getObject( + \Magento\Backend\Model\Menu\Builder::class, + [ + 'menuItemFactory' => $this->factoryMock + ] ); - - $this->_model = new \Magento\Backend\Model\Menu\Builder($this->_factoryMock, $this->_menuMock); } public function testProcessCommand() @@ -41,20 +44,20 @@ class BuilderTest extends \PHPUnit_Framework_TestCase $command2 = $this->getMock(\Magento\Backend\Model\Menu\Builder\Command\Update::class, [], [], '', false); $command2->expects($this->any())->method('getId')->will($this->returnValue(1)); $command->expects($this->once())->method('chain')->with($this->equalTo($command2)); - $this->_model->processCommand($command); - $this->_model->processCommand($command2); + $this->model->processCommand($command); + $this->model->processCommand($command2); } public function testGetResultBuildsTreeStructure() { $item1 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $item1->expects($this->once())->method('getChildren')->will($this->returnValue($this->_menuMock)); - $this->_factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1)); + $item1->expects($this->once())->method('getChildren')->will($this->returnValue($this->menuMock)); + $this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1)); $item2 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $this->_factoryMock->expects($this->at(1))->method('create')->will($this->returnValue($item2)); + $this->factoryMock->expects($this->at(1))->method('create')->will($this->returnValue($item2)); - $this->_menuMock->expects( + $this->menuMock->expects( $this->at(0) )->method( 'add' @@ -64,7 +67,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase $this->equalTo(2) ); - $this->_menuMock->expects( + $this->menuMock->expects( $this->at(1) )->method( 'add' @@ -74,7 +77,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase $this->equalTo(4) ); - $this->_model->processCommand( + $this->model->processCommand( new \Magento\Backend\Model\Menu\Builder\Command\Add( [ 'id' => 'item1', @@ -85,7 +88,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase ] ) ); - $this->_model->processCommand( + $this->model->processCommand( new \Magento\Backend\Model\Menu\Builder\Command\Add( [ 'id' => 'item2', @@ -98,12 +101,12 @@ class BuilderTest extends \PHPUnit_Framework_TestCase ) ); - $this->_model->getResult($this->_menuMock); + $this->model->getResult($this->menuMock); } public function testGetResultSkipsRemovedItems() { - $this->_model->processCommand( + $this->model->processCommand( new \Magento\Backend\Model\Menu\Builder\Command\Add( [ 'id' => 1, @@ -113,11 +116,11 @@ class BuilderTest extends \PHPUnit_Framework_TestCase ] ) ); - $this->_model->processCommand(new \Magento\Backend\Model\Menu\Builder\Command\Remove(['id' => 1])); + $this->model->processCommand(new \Magento\Backend\Model\Menu\Builder\Command\Remove(['id' => 1])); - $this->_menuMock->expects($this->never())->method('addChild'); + $this->menuMock->expects($this->never())->method('addChild'); - $this->_model->getResult($this->_menuMock); + $this->model->getResult($this->menuMock); } /** @@ -126,9 +129,9 @@ class BuilderTest extends \PHPUnit_Framework_TestCase public function testGetResultSkipItemsWithInvalidParent() { $item1 = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $this->_factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1)); + $this->factoryMock->expects($this->any())->method('create')->will($this->returnValue($item1)); - $this->_model->processCommand( + $this->model->processCommand( new \Magento\Backend\Model\Menu\Builder\Command\Add( [ 'id' => 'item1', @@ -140,6 +143,6 @@ class BuilderTest extends \PHPUnit_Framework_TestCase ) ); - $this->_model->getResult($this->_menuMock); + $this->model->getResult($this->menuMock); } } diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php index dee7518be3ac911b8e64b2d9cda0b84f9496b611..5f50c17db8d326466e9eeb90cc8e3d9e50295610 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php @@ -5,59 +5,43 @@ */ namespace Magento\Backend\Test\Unit\Model\Menu; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_cacheInstanceMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_directorMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $_configReaderMock; + private $cacheInstanceMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ - protected $_menuFactoryMock; + private $configReaderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject */ - protected $_eventManagerMock; + private $menuMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu\Builder|\PHPUnit_Framework_MockObject_MockObject */ - protected $_menuMock; + private $menuBuilderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $_menuBuilderMock; - - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_logger; + private $logger; /** * @var \Magento\Backend\Model\Menu\Config */ - protected $_model; + private $model; protected function setUp() { - $this->_cacheInstanceMock = $this->getMock( + $this->cacheInstanceMock = $this->getMock( \Magento\Framework\App\Cache\Type\Config::class, [], [], @@ -65,15 +49,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase false ); - $this->_directorMock = $this->getMock( - \Magento\Backend\Model\Menu\AbstractDirector::class, - [], - [], - '', - false - ); - - $this->_menuFactoryMock = $this->getMock( + $menuFactoryMock = $this->getMock( \Magento\Backend\Model\MenuFactory::class, ['create'], [], @@ -81,7 +57,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase false ); - $this->_configReaderMock = $this->getMock( + $this->configReaderMock = $this->getMock( \Magento\Backend\Model\Menu\Config\Reader::class, [], [], @@ -89,30 +65,15 @@ class ConfigTest extends \PHPUnit_Framework_TestCase false ); - $this->_eventManagerMock = $this->getMock( - \Magento\Framework\Event\ManagerInterface::class, - [], - [], - '', - false, - false - ); - - $this->_logger = $this->getMock(\Psr\Log\LoggerInterface::class); - - $this->_menuMock = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] - ); + $this->logger = $this->getMock(\Psr\Log\LoggerInterface::class); - $this->_menuBuilderMock = $this->getMock(\Magento\Backend\Model\Menu\Builder::class, [], [], '', false); + $this->menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); - $this->_menuFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_menuMock)); + $this->menuBuilderMock = $this->getMock(\Magento\Backend\Model\Menu\Builder::class, [], [], '', false); - $scopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); + $menuFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->menuMock)); - $this->_configReaderMock->expects($this->any())->method('read')->will($this->returnValue([])); + $this->configReaderMock->expects($this->any())->method('read')->will($this->returnValue([])); $appState = $this->getMock(\Magento\Framework\App\State::class, ['getAreaCode'], [], '', false); $appState->expects( @@ -123,22 +84,22 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->returnValue(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) ); - $this->_model = new \Magento\Backend\Model\Menu\Config( - $this->_menuBuilderMock, - $this->_directorMock, - $this->_menuFactoryMock, - $this->_configReaderMock, - $this->_cacheInstanceMock, - $this->_eventManagerMock, - $this->_logger, - $scopeConfig, - $appState + $this->model = (new ObjectManager($this))->getObject( + \Magento\Backend\Model\Menu\Config::class, + [ + 'menuBuilder' => $this->menuBuilderMock, + 'menuFactory' => $menuFactoryMock, + 'configReader' => $this->configReaderMock, + 'configCacheType' => $this->cacheInstanceMock, + 'logger' => $this->logger, + 'appState' => $appState, + ] ); } public function testGetMenuWithCachedObjectReturnsUnserializedObject() { - $this->_cacheInstanceMock->expects( + $this->cacheInstanceMock->expects( $this->once() )->method( 'load' @@ -148,14 +109,14 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->returnValue('menu_cache') ); - $this->_menuMock->expects($this->once())->method('unserialize')->with('menu_cache'); + $this->menuMock->expects($this->once())->method('unserialize')->with('menu_cache'); - $this->assertEquals($this->_menuMock, $this->_model->getMenu()); + $this->assertEquals($this->menuMock, $this->model->getMenu()); } public function testGetMenuWithNotCachedObjectBuidlsObject() { - $this->_cacheInstanceMock->expects( + $this->cacheInstanceMock->expects( $this->at(0) )->method( 'load' @@ -165,17 +126,17 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->returnValue(false) ); - $this->_configReaderMock->expects($this->once())->method('read')->will($this->returnValue([])); + $this->configReaderMock->expects($this->once())->method('read')->will($this->returnValue([])); - $this->_menuBuilderMock->expects( + $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' )->will( - $this->returnValue($this->_menuMock) + $this->returnValue($this->menuMock) ); - $this->assertEquals($this->_menuMock, $this->_model->getMenu()); + $this->assertEquals($this->menuMock, $this->model->getMenu()); } /** @@ -186,7 +147,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testGetMenuExceptionLogged($expectedException) { $this->setExpectedException($expectedException); - $this->_menuBuilderMock->expects( + $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' @@ -194,7 +155,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->throwException(new $expectedException()) ); - $this->_model->getMenu(); + $this->model->getMenu(); } public function getMenuExceptionLoggedDataProvider() @@ -208,9 +169,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testGetMenuGenericExceptionIsNotLogged() { - $this->_logger->expects($this->never())->method('critical'); + $this->logger->expects($this->never())->method('critical'); - $this->_menuBuilderMock->expects( + $this->menuBuilderMock->expects( $this->exactly(1) )->method( 'getResult' @@ -218,7 +179,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->throwException(new \Exception()) ); try { - $this->_model->getMenu(); + $this->model->getMenu(); } catch (\Exception $e) { return; } diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php index 19836ba7c94017d575aaae060ab120667a273ff2..06dee1f01fd88439ad1a1bcf1f959e19eb13e9be 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/Filter/IteratorTest.php @@ -5,49 +5,49 @@ */ namespace Magento\Backend\Test\Unit\Model\Menu\Filter; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + class IteratorTest extends \PHPUnit_Framework_TestCase { /** * @var \Magento\Backend\Model\Menu */ - protected $_menuModel; + private $menuModel; /** * @var \Magento\Backend\Model\Menu\Item[] */ - protected $_items = []; + private $items = []; protected function setUp() { - $this->_items['item1'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $this->_items['item1']->expects($this->any())->method('getId')->will($this->returnValue('item1')); - $this->_items['item1']->expects($this->any())->method('isDisabled')->will($this->returnValue(false)); - $this->_items['item1']->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); - - $this->_items['item2'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $this->_items['item2']->expects($this->any())->method('getId')->will($this->returnValue('item2')); - $this->_items['item2']->expects($this->any())->method('isDisabled')->will($this->returnValue(true)); - $this->_items['item2']->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); - - $this->_items['item3'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); - $this->_items['item3']->expects($this->any())->method('getId')->will($this->returnValue('item3')); - $this->_items['item3']->expects($this->any())->method('isDisabled')->will($this->returnValue(false)); - $this->_items['item3']->expects($this->any())->method('isAllowed')->will($this->returnValue(false)); - - $loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class); - - $this->_menuModel = new \Magento\Backend\Model\Menu($loggerMock); + $this->items['item1'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); + $this->items['item1']->expects($this->any())->method('getId')->will($this->returnValue('item1')); + $this->items['item1']->expects($this->any())->method('isDisabled')->will($this->returnValue(false)); + $this->items['item1']->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); + + $this->items['item2'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); + $this->items['item2']->expects($this->any())->method('getId')->will($this->returnValue('item2')); + $this->items['item2']->expects($this->any())->method('isDisabled')->will($this->returnValue(true)); + $this->items['item2']->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); + + $this->items['item3'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); + $this->items['item3']->expects($this->any())->method('getId')->will($this->returnValue('item3')); + $this->items['item3']->expects($this->any())->method('isDisabled')->will($this->returnValue(false)); + $this->items['item3']->expects($this->any())->method('isAllowed')->will($this->returnValue(false)); + + $this->menuModel = (new ObjectManager($this))->getObject(\Magento\Backend\Model\Menu::class); } public function testLoopWithAllItemsDisabledDoesntIterate() { - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); $filterIteratorModel = new \Magento\Backend\Model\Menu\Filter\Iterator( - $this->_menuModel->getIterator() + $this->menuModel->getIterator() ); $items = []; @@ -59,15 +59,15 @@ class IteratorTest extends \PHPUnit_Framework_TestCase public function testLoopIteratesOnlyValidItems() { - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->_items['item1']); + $this->menuModel->add($this->items['item1']); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); $filterIteratorModel = new \Magento\Backend\Model\Menu\Filter\Iterator( - $this->_menuModel->getIterator() + $this->menuModel->getIterator() ); $items = []; @@ -79,16 +79,16 @@ class IteratorTest extends \PHPUnit_Framework_TestCase public function testLoopIteratesDosntIterateDisabledItems() { - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->_items['item1']); - $this->_menuModel->add($this->_items['item2']); + $this->menuModel->add($this->items['item1']); + $this->menuModel->add($this->items['item2']); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); $filterIteratorModel = new \Magento\Backend\Model\Menu\Filter\Iterator( - $this->_menuModel->getIterator() + $this->menuModel->getIterator() ); $items = []; @@ -100,16 +100,16 @@ class IteratorTest extends \PHPUnit_Framework_TestCase public function testLoopIteratesDosntIterateNotAllowedItems() { - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->_items['item1']); - $this->_menuModel->add($this->_items['item3']); + $this->menuModel->add($this->items['item1']); + $this->menuModel->add($this->items['item3']); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); $filterIteratorModel = new \Magento\Backend\Model\Menu\Filter\Iterator( - $this->_menuModel->getIterator() + $this->menuModel->getIterator() ); $items = []; @@ -121,17 +121,17 @@ class IteratorTest extends \PHPUnit_Framework_TestCase public function testLoopIteratesMixedItems() { - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->_items['item1']); - $this->_menuModel->add($this->_items['item2']); - $this->_menuModel->add($this->_items['item3']); + $this->menuModel->add($this->items['item1']); + $this->menuModel->add($this->items['item2']); + $this->menuModel->add($this->items['item3']); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); - $this->_menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); + $this->menuModel->add($this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false)); $filterIteratorModel = new \Magento\Backend\Model\Menu\Filter\Iterator( - $this->_menuModel->getIterator() + $this->menuModel->getIterator() ); $items = []; diff --git a/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php b/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php index fcef1bd374c01b4160880143c095464e3020fb18..b6cd61870d60f69f6b93eea88dd6aa56b121d66a 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php @@ -40,16 +40,14 @@ class ItemTest extends \PHPUnit_Framework_TestCase */ protected $_moduleManager; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - protected $_validatorMock; - /** * @var \PHPUnit_Framework_MockObject_MockObject */ protected $_moduleListMock; + /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */ + private $objectManager; + /** * @var array */ @@ -58,8 +56,8 @@ class ItemTest extends \PHPUnit_Framework_TestCase 'title' => 'Item Title', 'action' => '/system/config', 'resource' => 'Magento_Config::config', - 'dependsOnModule' => 'Magento_Backend', - 'dependsOnConfig' => 'system/config/isEnabled', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', 'tooltip' => 'Item tooltip', ]; @@ -76,15 +74,15 @@ class ItemTest extends \PHPUnit_Framework_TestCase ); $this->_urlModelMock = $this->getMock(\Magento\Backend\Model\Url::class, [], [], '', false); $this->_moduleManager = $this->getMock(\Magento\Framework\Module\Manager::class, [], [], '', false); - $this->_validatorMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Validator::class); - $this->_validatorMock->expects($this->any())->method('validate'); + $validatorMock = $this->getMock(\Magento\Backend\Model\Menu\Item\Validator::class); + $validatorMock->expects($this->any())->method('validate'); $this->_moduleListMock = $this->getMock(\Magento\Framework\Module\ModuleListInterface::class); - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_model = $helper->getObject( + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->_model = $this->objectManager->getObject( \Magento\Backend\Model\Menu\Item::class, [ - 'validator' => $this->_validatorMock, + 'validator' => $validatorMock, 'authorization' => $this->_aclMock, 'scopeConfig' => $this->_scopeConfigMock, 'menuFactory' => $this->_menuFactoryMock, @@ -99,8 +97,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function testGetUrlWithEmptyActionReturnsHashSign() { $this->_params['action'] = ''; - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $item = $helper->getObject( + $item = $this->objectManager->getObject( \Magento\Backend\Model\Menu\Item::class, ['menuFactory' => $this->_menuFactoryMock, 'data' => $this->_params] ); @@ -129,8 +126,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function testHasClickCallbackReturnsTrueIfItemHasNoAction() { $this->_params['action'] = ''; - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $item = $helper->getObject( + $item = $this->objectManager->getObject( \Magento\Backend\Model\Menu\Item::class, ['menuFactory' => $this->_menuFactoryMock, 'data' => $this->_params] ); @@ -140,8 +136,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function testGetClickCallbackReturnsStoppingJsIfItemDoesntHaveAction() { $this->_params['action'] = ''; - $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $item = $helper->getObject( + $item = $this->objectManager->getObject( \Magento\Backend\Model\Menu\Item::class, ['menuFactory' => $this->_menuFactoryMock, 'data' => $this->_params] ); @@ -218,15 +213,86 @@ class ItemTest extends \PHPUnit_Framework_TestCase public function testGetChildrenCreatesSubmenuOnFirstCall() { - $menuMock = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] - ); + $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); $this->_menuFactoryMock->expects($this->once())->method('create')->will($this->returnValue($menuMock)); $this->_model->getChildren(); $this->_model->getChildren(); } + + /** + * @param array $data + * @param array $expected + * @dataProvider toArrayDataProvider + */ + public function testToArray(array $data, array $expected) + { + $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); + $this->_menuFactoryMock->method('create')->will($this->returnValue($menuMock)); + $menuMock->method('toArray') + ->willReturn($data['sub_menu']); + + $model = $this->objectManager->getObject( + \Magento\Backend\Model\Menu\Item::class, + [ + 'authorization' => $this->_aclMock, + 'scopeConfig' => $this->_scopeConfigMock, + 'menuFactory' => $this->_menuFactoryMock, + 'urlModel' => $this->_urlModelMock, + 'moduleList' => $this->_moduleListMock, + 'moduleManager' => $this->_moduleManager, + 'data' => $data + ] + ); + $this->assertEquals($expected, $model->toArray()); + } + + /** + * @return array + */ + public function toArrayDataProvider() + { + return include __DIR__ . '/../_files/menu_item_data.php'; + } + + /** + * @param array $constructorData + * @param array $populateFromData + * @param array $expected + * @dataProvider populateFromArrayDataProvider + */ + public function testPopulateFromArray( + array $constructorData, + array $populateFromData, + array $expected + ) { + $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); + $this->_menuFactoryMock->method('create')->willReturn($menuMock); + $menuMock->method('toArray') + ->willReturn(['submenuArray']); + + $model = $this->objectManager->getObject( + \Magento\Backend\Model\Menu\Item::class, + [ + 'authorization' => $this->_aclMock, + 'scopeConfig' => $this->_scopeConfigMock, + 'menuFactory' => $this->_menuFactoryMock, + 'urlModel' => $this->_urlModelMock, + 'moduleList' => $this->_moduleListMock, + 'moduleManager' => $this->_moduleManager, + 'data' => $constructorData + ] + ); + $model->populateFromArray($populateFromData); + $this->assertEquals($expected, $model->toArray()); + } + + /** + * @return array + */ + public function populateFromArrayDataProvider() + { + return include __DIR__ . '/../_files/menu_item_constructor_data.php'; + } } diff --git a/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php index c985bec92074a8f93e4df4b9fb73e336a5037f99..12968df9f340f888a2999d516a1304510b181316 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/MenuTest.php @@ -5,6 +5,10 @@ */ namespace Magento\Backend\Test\Unit\Model; +use Magento\Backend\Model\Menu\Item; +use Magento\Backend\Model\Menu\Item\Factory; +use Magento\Framework\Serialize\SerializerInterface; + class MenuTest extends \PHPUnit_Framework_TestCase { /** @@ -22,8 +26,14 @@ class MenuTest extends \PHPUnit_Framework_TestCase */ protected $_items = []; + /** + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager + */ + private $objectManagerHelper; + protected function setUp() { + $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_items['item1'] = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); $this->_items['item1']->expects($this->any())->method('getId')->will($this->returnValue('item1')); @@ -35,7 +45,12 @@ class MenuTest extends \PHPUnit_Framework_TestCase $this->_logger = $this->getMock(\Psr\Log\LoggerInterface::class); - $this->_model = new \Magento\Backend\Model\Menu($this->_logger); + $this->_model = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger + ] + ); } public function testAdd() @@ -53,7 +68,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase public function testAddToItem() { - $subMenu = $this->getMock(\Magento\Backend\Model\Menu::class, [], [$this->_logger]); + $subMenu = $this->getMockBuilder(\Magento\Backend\Model\Menu::class)->disableOriginalConstructor()->getMock(); $subMenu->expects($this->once())->method("add")->with($this->_items['item2']); $this->_items['item1']->expects($this->once())->method("getChildren")->will($this->returnValue($subMenu)); @@ -101,19 +116,29 @@ class MenuTest extends \PHPUnit_Framework_TestCase public function testGetRecursive() { - $menu1 = new \Magento\Backend\Model\Menu($this->_logger); - $menu2 = new \Magento\Backend\Model\Menu($this->_logger); + $menuOne = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger + ] + ); + $menuTwo = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger + ] + ); $this->_items['item1']->expects($this->any())->method('hasChildren')->will($this->returnValue(true)); - $this->_items['item1']->expects($this->any())->method('getChildren')->will($this->returnValue($menu1)); + $this->_items['item1']->expects($this->any())->method('getChildren')->will($this->returnValue($menuOne)); $this->_model->add($this->_items['item1']); $this->_items['item2']->expects($this->any())->method('hasChildren')->will($this->returnValue(true)); - $this->_items['item2']->expects($this->any())->method('getChildren')->will($this->returnValue($menu2)); - $menu1->add($this->_items['item2']); + $this->_items['item2']->expects($this->any())->method('getChildren')->will($this->returnValue($menuTwo)); + $menuOne->add($this->_items['item2']); $this->_items['item3']->expects($this->any())->method('hasChildren')->will($this->returnValue(false)); - $menu2->add($this->_items['item3']); + $menuTwo->add($this->_items['item3']); $this->assertEquals($this->_items['item1'], $this->_model->get('item1')); $this->assertEquals($this->_items['item2'], $this->_model->get('item2')); @@ -126,11 +151,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase $this->_model->add($this->_items['item2']); $this->_model->add($this->_items['item3']); - $subMenu = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] - ); + $subMenu = $this->getMockBuilder(\Magento\Backend\Model\Menu::class)->disableOriginalConstructor()->getMock(); $subMenu->expects($this->once())->method("add")->with($this->_items['item3']); $this->_items['item1']->expects($this->once())->method("getChildren")->will($this->returnValue($subMenu)); @@ -179,11 +200,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase public function testRemoveRemovesMenuItemRecursively() { - $menuMock = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] - ); + $menuMock = $this->getMockBuilder(\Magento\Backend\Model\Menu::class)->disableOriginalConstructor()->getMock(); $menuMock->expects($this->once())->method('remove')->with($this->equalTo('item2')); $this->_items['item1']->expects($this->any())->method('hasChildren')->will($this->returnValue(true)); @@ -214,7 +231,12 @@ class MenuTest extends \PHPUnit_Framework_TestCase { $this->_logger->expects($this->any())->method('log'); - $subMenu = new \Magento\Backend\Model\Menu($this->_logger); + $subMenu = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger + ] + ); $this->_items['item1']->expects($this->any())->method("hasChildren")->will($this->returnValue(true)); @@ -285,11 +307,11 @@ class MenuTest extends \PHPUnit_Framework_TestCase $items[] = $item->getId(); } - $items2 = []; + $itemsTwo = []; foreach ($this->_model as $item) { - $items2[] = $item->getId(); + $itemsTwo[] = $item->getId(); } - $this->assertEquals($items, $items2); + $this->assertEquals($items, $itemsTwo); } /** @@ -307,10 +329,10 @@ class MenuTest extends \PHPUnit_Framework_TestCase 'item3' => ['item1', 'item2', 'item3'], ]; $actual = []; - foreach ($this->_model as $valLoop1) { - $keyLevel1 = $valLoop1->getId(); - foreach ($this->_model as $valLoop2) { - $actual[$keyLevel1][] = $valLoop2->getId(); + foreach ($this->_model as $valLoopOne) { + $keyLevelOne = $valLoopOne->getId(); + foreach ($this->_model as $valLoopTwo) { + $actual[$keyLevelOne][] = $valLoopTwo->getId(); } } $this->assertEquals($expected, $actual); @@ -318,7 +340,45 @@ class MenuTest extends \PHPUnit_Framework_TestCase public function testSerialize() { - $this->assertNotEmpty($this->_model->serialize()); - $this->_model->add($this->_items['item1']); + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->expects($this->once()) + ->method('serialize') + ->with([['arrayData']]) + ->willReturn('serializedString'); + $menu = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger, + 'serializer' => $serializerMock, + ] + ); + $itemMock = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false); + $itemMock->expects($this->any())->method('getId')->will($this->returnValue('item1')); + $itemMock->expects($this->once()) + ->method('toArray') + ->willReturn(['arrayData']); + $menu->add($itemMock); + $this->assertEquals('serializedString', $menu->serialize()); + } + + public function testUnserialize() + { + $serializerMock = $this->getMock(SerializerInterface::class); + $serializerMock->expects($this->once()) + ->method('unserialize') + ->willReturn([['unserializedData']]); + $menuItemFactoryMock = $this->getMock(Factory::class, [], [], '', false); + $menuItemFactoryMock->expects($this->once()) + ->method('create') + ->with(['unserializedData']); + $menu = $this->objectManagerHelper->getObject( + \Magento\Backend\Model\Menu::class, + [ + 'logger' => $this->_logger, + 'serializer' => $serializerMock, + 'menuItemFactory' => $menuItemFactoryMock, + ] + ); + $menu->unserialize('serializedString'); } } diff --git a/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php b/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php index 4eda145156c6dbf881eadf3d88981ffb9318d32c..fa83eb4210d467ba7d33935f934cbcc4801470fa 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/UrlTest.php @@ -75,11 +75,7 @@ class UrlTest extends \PHPUnit_Framework_TestCase protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->_menuMock = $this->getMock( - \Magento\Backend\Model\Menu::class, - [], - [$this->getMock(\Psr\Log\LoggerInterface::class)] - ); + $this->_menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); $this->_menuConfigMock = $this->getMock(\Magento\Backend\Model\Menu\Config::class, [], [], '', false); $this->_menuConfigMock->expects($this->any())->method('getMenu')->will($this->returnValue($this->_menuMock)); diff --git a/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_constructor_data.php b/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_constructor_data.php new file mode 100644 index 0000000000000000000000000000000000000000..213e98de13866b610be60c311b984466d27dc7c0 --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_constructor_data.php @@ -0,0 +1,133 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +return [ + 'default data to constructor' => [ + [], + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + [ + 'parent_id' => null, + 'module_name' => 'Magento_Backend', + 'sort_index' => null, + 'depends_on_config' => 'system/config/isEnabled', + 'id' => 'item', + 'resource' => 'Magento_Config::config', + 'path' => '', + 'action' => '/system/config', + 'depends_on_module' => 'Magento_Backend', + 'tooltip' => 'Item tooltip', + 'title' => 'Item Title', + 'sub_menu' => null + ], + ], + 'data without submenu to constructor' => [ + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => '5', + 'resource' => null, + 'path' => null, + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => null, + 'title' => null, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => '5', + 'resource' => null, + 'path' => '', + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => null, + 'sub_menu' => ['submenuArray'] + ], + ], + 'data with submenu to constructor' => [ + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => '5', + 'resource' => null, + 'path' => null, + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => null, + 'title' => null, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => null, + 'resource' => null, + 'path' => '', + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => null, + 'sub_menu' => ['submenuArray'] + ], + ] +]; diff --git a/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_data.php b/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_data.php new file mode 100644 index 0000000000000000000000000000000000000000..c4c97e95af68e7a95b4a7c53df64b35c6fb85d14 --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Model/_files/menu_item_data.php @@ -0,0 +1,118 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +return [ + 'no submenu' => [ + [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + 'sub_menu' => null, + ], + [ + 'parent_id' => null, + 'module_name' => 'Magento_Backend', + 'sort_index' => null, + 'depends_on_config' => 'system/config/isEnabled', + 'id' => 'item', + 'resource' => 'Magento_Config::config', + 'path' => '', + 'action' => '/system/config', + 'depends_on_module' => 'Magento_Backend', + 'tooltip' => 'Item tooltip', + 'title' => 'Item Title', + 'sub_menu' => null, + ] + ], + 'with submenu' => [ + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => '5', + 'resource' => null, + 'path' => null, + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => null, + 'title' => null, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => '5', + 'resource' => null, + 'path' => null, + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => null, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ] + ] + ], + 'small set of data' => [ + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ], + ], + [ + 'parent_id' => '1', + 'module_name' => 'Magento_Module1', + 'sort_index' => '50', + 'depends_on_config' => null, + 'id' => null, + 'resource' => null, + 'path' => '', + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => null, + 'sub_menu' => [ + 'id' => 'item', + 'title' => 'Item Title', + 'action' => '/system/config', + 'resource' => 'Magento_Config::config', + 'depends_on_module' => 'Magento_Backend', + 'depends_on_config' => 'system/config/isEnabled', + 'tooltip' => 'Item tooltip', + ] + ] + ] +]; diff --git a/dev/tests/integration/testsuite/Magento/Backend/Block/MenuTest.php b/dev/tests/integration/testsuite/Magento/Backend/Block/MenuTest.php index f36c17889aa30145019690c3f2ad302780aa4dd1..c3988187c686d939206c045c4dd9a78d2422de6c 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Block/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Block/MenuTest.php @@ -28,6 +28,11 @@ class MenuTest extends \PHPUnit_Framework_TestCase */ protected $backupRegistrar; + /** + * @var \Magento\Backend\Model\Menu\Config + */ + private $menuConfig; + protected function setUp() { $this->configCacheType = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( @@ -35,15 +40,18 @@ class MenuTest extends \PHPUnit_Framework_TestCase ); $this->configCacheType->save('', \Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT); - $this->blockMenu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Block\Menu::class - ); - $reflection = new \ReflectionClass(\Magento\Framework\Component\ComponentRegistrar::class); $paths = $reflection->getProperty('paths'); $paths->setAccessible(true); $this->backupRegistrar = $paths->getValue(); $paths->setAccessible(false); + + $this->menuConfig = $this->prepareMenuConfig(); + + $this->blockMenu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( + \Magento\Backend\Block\Menu::class, + ['menuConfig' => $this->menuConfig] + ); } /** @@ -51,8 +59,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase */ public function testRenderNavigation() { - $menuConfig = $this->prepareMenuConfig(); - $menuHtml = $this->blockMenu->renderNavigation($menuConfig->getMenu()); + $menuHtml = $this->blockMenu->renderNavigation($this->menuConfig->getMenu()); $menu = new \SimpleXMLElement($menuHtml); $item = $menu->xpath('/ul/li/a/span')[0]; diff --git a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php index f6fad14d210e9a2b2602014e353451b8f049c213..6c51e061f9db7af18ebf5629742c4edeb03771ca 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php @@ -15,30 +15,28 @@ class MenuTest extends \PHPUnit_Framework_TestCase /** * @var \Magento\Backend\Model\Menu */ - protected $_model; + private $model; + + /** @var \Magento\Framework\ObjectManagerInterface */ + private $objectManager; protected function setUp() { parent::setUp(); \Magento\TestFramework\Helper\Bootstrap::getInstance() ->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); - $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() - ->create(\Magento\Backend\Model\Auth::class); - \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Framework\Config\ScopeInterface::class - )->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->model = $this->objectManager->create(\Magento\Backend\Model\Auth::class); + $this->objectManager->get(\Magento\Framework\Config\ScopeInterface::class) + ->setCurrentScope(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE); } public function testMenuItemManipulation() { /* @var $menu \Magento\Backend\Model\Menu */ - $menu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Backend\Model\Menu\Config::class - )->getMenu(); + $menu = $this->objectManager->create(\Magento\Backend\Model\Menu\Config::class)->getMenu(); /* @var $itemFactory \Magento\Backend\Model\Menu\Item\Factory */ - $itemFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Model\Menu\Item\Factory::class - ); + $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class); // Add new item in top level $menu->add( @@ -52,7 +50,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase ) ); - //Add submenu + // Add submenu $menu->add( $itemFactory->create( [ @@ -79,4 +77,100 @@ class MenuTest extends \PHPUnit_Framework_TestCase // Move menu item $menu->move('Magento_Catalog::catalog_products', 'Magento_Backend::system2'); } + + /** + * @magentoAppIsolation enabled + */ + public function testSerialize() + { + /** @var Menu $menu */ + $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create(); + /* @var \Magento\Backend\Model\Menu\Item\Factory $itemFactory */ + $itemFactory = $this->objectManager->create(\Magento\Backend\Model\Menu\Item\Factory::class); + + // Add new item in top level + $menu->add( + $itemFactory->create( + [ + 'id' => 'Magento_Backend::system3', + 'title' => 'Extended System', + 'module' => 'Magento_Backend', + 'resource' => 'Magento_Backend::system3', + ] + ) + ); + + // Add submenu + $menu->add( + $itemFactory->create( + [ + 'id' => 'Magento_Backend::system3_acl', + 'title' => 'Acl', + 'module' => 'Magento_Backend', + 'action' => 'admin/backend/acl/index', + 'resource' => 'Magento_Backend::system3_acl', + ] + ), + 'Magento_Backend::system3' + ); + $serializedString = $menu->serialize(); + $expected = '[{"parent_id":null,"module_name":"Magento_Backend","sort_index":null,"depends_on_config":null,' + . '"id":"Magento_Backend::system3","resource":"Magento_Backend::system3","path":"","action":null,' + . '"depends_on_module":null,"tooltip":"","title":"Extended System",' + . '"sub_menu":[{"parent_id":null,"module_name":"Magento_Backend","sort_index":null,' + . '"depends_on_config":null,"id":"Magento_Backend::system3_acl","resource":"Magento_Backend::system3_acl",' + . '"path":"","action":"admin\/backend\/acl\/index","depends_on_module":null,"tooltip":"","title":"Acl",' + . '"sub_menu":null}]}]'; + $this->assertEquals($expected, $serializedString); + } + + /** + * @magentoAppIsolation enabled + */ + public function testUnserialize() + { + $serializedMenu = '[{"parent_id":null,"module_name":"Magento_Backend","sort_index":null,' + . '"depends_on_config":null,"id":"Magento_Backend::system3","resource":"Magento_Backend::system3",' + . '"path":"","action":null,"depends_on_module":null,"tooltip":"","title":"Extended System",' + . '"sub_menu":[{"parent_id":null,"module_name":"Magento_Backend","sort_index":null,' + . '"depends_on_config":null,"id":"Magento_Backend::system3_acl","resource":"Magento_Backend::system3_acl",' + . '"path":"","action":"admin\/backend\/acl\/index","depends_on_module":null,"tooltip":"","title":"Acl",' + . '"sub_menu":null}]}]'; + /** @var Menu $menu */ + $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create(); + $menu->unserialize($serializedMenu); + $expected = [ + [ + 'parent_id' => null, + 'module_name' => 'Magento_Backend', + 'sort_index' => null, + 'depends_on_config' => null, + 'id' => 'Magento_Backend::system3', + 'resource' => 'Magento_Backend::system3', + 'path' => '', + 'action' => null, + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => 'Extended System', + 'sub_menu' => + [ + [ + 'parent_id' => null, + 'module_name' => 'Magento_Backend', + 'sort_index' => null, + 'depends_on_config' => null, + 'id' => 'Magento_Backend::system3_acl', + 'resource' => 'Magento_Backend::system3_acl', + 'path' => '', + 'action' => 'admin/backend/acl/index', + 'depends_on_module' => null, + 'tooltip' => '', + 'title' => 'Acl', + 'sub_menu' => null, + ], + ], + ], + ]; + $this->assertEquals($expected, $menu->toArray()); + } }