diff --git a/app/code/Magento/Backend/Model/Menu.php b/app/code/Magento/Backend/Model/Menu.php index 15432dd71edaf775be21b2501bcff17911ef697b..ccae5ebce16a01926aa0166344976185ad6769bf 100644 --- a/app/code/Magento/Backend/Model/Menu.php +++ b/app/code/Magento/Backend/Model/Menu.php @@ -305,8 +305,7 @@ class Menu extends \ArrayObject { $items = []; foreach ($data as $itemData) { - $item = $this->menuItemFactory->create(); - $item->populateFromArray($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 a0b82f1c4e16c4d1d71813d161bd5e2866465346..ef1aa864588f412126bf74e32ca462a41a3182d5 100644 --- a/app/code/Magento/Backend/Model/Menu/Item.php +++ b/app/code/Magento/Backend/Model/Menu/Item.php @@ -168,9 +168,7 @@ class Item array $data = [] ) { $this->_validator = $validator; - if (!empty($data)) { - $this->_validator->validate($data); - } + $this->_validator->validate($data); $this->_moduleManager = $moduleManager; $this->_acl = $authorization; $this->_scopeConfig = $scopeConfig; 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 28ce4148777ad43a6d1c79cca370d7ce5cdac271..5f50c17db8d326466e9eeb90cc8e3d9e50295610 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/ConfigTest.php @@ -10,27 +10,27 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; class ConfigTest extends \PHPUnit_Framework_TestCase { /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Framework\App\Cache\Type\Config|\PHPUnit_Framework_MockObject_MockObject */ private $cacheInstanceMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu\Config\Reader|\PHPUnit_Framework_MockObject_MockObject */ private $configReaderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu|\PHPUnit_Framework_MockObject_MockObject */ private $menuMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Backend\Model\Menu\Builder|\PHPUnit_Framework_MockObject_MockObject */ private $menuBuilderMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject + * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ private $logger; 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 218dfb62ba8c3f239f6f3230934b6023877517b8..b6cd61870d60f69f6b93eea88dd6aa56b121d66a 100644 --- a/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php +++ b/app/code/Magento/Backend/Test/Unit/Model/Menu/ItemTest.php @@ -231,7 +231,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); $this->_menuFactoryMock->method('create')->will($this->returnValue($menuMock)); $menuMock->method('toArray') - ->willReturn(isset($data['sub_menu']) ? $data['sub_menu'] : null); + ->willReturn($data['sub_menu']); $model = $this->objectManager->getObject( \Magento\Backend\Model\Menu\Item::class, @@ -248,119 +248,12 @@ class ItemTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $model->toArray()); } + /** + * @return array + */ public function toArrayDataProvider() { - 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', - ], - [ - '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', - ] - ] - ] - ]; + return include __DIR__ . '/../_files/menu_item_data.php'; } /** @@ -375,7 +268,7 @@ class ItemTest extends \PHPUnit_Framework_TestCase array $expected ) { $menuMock = $this->getMock(\Magento\Backend\Model\Menu::class, [], [], '', false); - $this->_menuFactoryMock->method('create')->will($this->returnValue($menuMock)); + $this->_menuFactoryMock->method('create')->willReturn($menuMock); $menuMock->method('toArray') ->willReturn(['submenuArray']); @@ -395,134 +288,11 @@ class ItemTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $model->toArray()); } + /** + * @return array + */ public function populateFromArrayDataProvider() { - 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'] - ], - ] - ]; + return include __DIR__ . '/../_files/menu_item_constructor_data.php'; } } 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..1723b9a7030c88616bafc55462b75bbb566ec296 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,8 +40,11 @@ class MenuTest extends \PHPUnit_Framework_TestCase ); $this->configCacheType->save('', \Magento\Backend\Model\Menu\Config::CACHE_MENU_OBJECT); + $this->menuConfig = $this->prepareMenuConfig(); + $this->blockMenu = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( - \Magento\Backend\Block\Menu::class + \Magento\Backend\Block\Menu::class, + ['menuConfig' => $this->menuConfig] ); $reflection = new \ReflectionClass(\Magento\Framework\Component\ComponentRegistrar::class); @@ -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 5cc7ddb911bcd00fc99b1d20858b4cec7ec59edf..7bdb208475ee133dd054f7d054ca33e6c88b3fff 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php @@ -50,7 +50,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase ) ); - //Add submenu + // Add submenu $menu->add( $itemFactory->create( [