From b8d2efb95996084d458bc6c70ff005acf9182801 Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 11:11:05 +0200 Subject: [PATCH 01/12] MAGETWO-63716: Add target to admin menu element --- .../Magento/Backend/Block/AnchorRenderer.php | 86 +++++++++++ app/code/Magento/Backend/Block/Menu.php | 121 ++++------------ .../Magento/Backend/Block/MenuItemChecker.php | 48 +++++++ .../Backend/Model/Menu/Config/Converter.php | 1 + app/code/Magento/Backend/Model/Menu/Item.php | 19 +++ .../Test/Unit/Block/AnchorRendererTest.php | 133 ++++++++++++++++++ .../Test/Unit/Block/MenuItemCheckerTest.php | 86 +++++++++++ .../_files/menu_item_constructor_data.php | 9 +- .../Test/Unit/Model/_files/menu_item_data.php | 7 +- app/code/Magento/Backend/etc/di.xml | 6 + app/code/Magento/Backend/etc/menu.xsd | 2 + index.php | 2 + 12 files changed, 420 insertions(+), 100 deletions(-) create mode 100644 app/code/Magento/Backend/Block/AnchorRenderer.php create mode 100644 app/code/Magento/Backend/Block/MenuItemChecker.php create mode 100644 app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php create mode 100644 app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php new file mode 100644 index 0000000000000..4eaebb197544c --- /dev/null +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -0,0 +1,86 @@ +menuItemChecker = $menuItemChecker; + $this->escaper = $escaper; + } + + /** + * Render menu item anchor + * @param Item $activeItem + * @param Item $menuItem + * @param int $level + * @return string + */ + public function renderAnchor(Item $activeItem, Item $menuItem, $level) + { + if ($level == 1 && $menuItem->getUrl() == '#') { + $output = '' + . '' . $this->escaper->escapeHtml(__($menuItem->getTitle())) . '' + . ''; + } else { + $target = $menuItem->getTarget() ? ('target=' . $menuItem->getTarget()) : ''; + $output = '_renderItemAnchorTitle( + $menuItem + ) . $this->_renderItemOnclickFunction( + $menuItem + ) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '') + . '">' . '' . $this->escaper->escapeHtml(__($menuItem->getTitle())) + . '' . ''; + } + + return $output; + } + + /** + * Render menu item anchor title + * + * @param Item $menuItem + * @return string + */ + private function _renderItemAnchorTitle($menuItem) + { + return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : ''; + } + + /** + * Render menu item onclick function + * + * @param Item $menuItem + * @return string + */ + private function _renderItemOnclickFunction($menuItem) + { + return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : ''; + } +} diff --git a/app/code/Magento/Backend/Block/Menu.php b/app/code/Magento/Backend/Block/Menu.php index f3b99a2c14a13..ba805db285d24 100644 --- a/app/code/Magento/Backend/Block/Menu.php +++ b/app/code/Magento/Backend/Block/Menu.php @@ -62,6 +62,16 @@ class Menu extends \Magento\Backend\Block\Template */ protected $_localeResolver; + /** + * @var MenuItemChecker + */ + private $menuItemChecker; + + /** + * @var AnchorRenderer + */ + private $anchorRenderer; + /** * @param Template\Context $context * @param \Magento\Backend\Model\UrlInterface $url @@ -70,6 +80,8 @@ class Menu extends \Magento\Backend\Block\Template * @param \Magento\Backend\Model\Menu\Config $menuConfig * @param \Magento\Framework\Locale\ResolverInterface $localeResolver * @param array $data + * @param MenuItemChecker|null $menuItemChecker + * @param AnchorRenderer|null $anchorRenderer */ public function __construct( \Magento\Backend\Block\Template\Context $context, @@ -78,13 +90,17 @@ public function __construct( \Magento\Backend\Model\Auth\Session $authSession, \Magento\Backend\Model\Menu\Config $menuConfig, \Magento\Framework\Locale\ResolverInterface $localeResolver, - array $data = [] + array $data = [], + MenuItemChecker $menuItemChecker = null, + AnchorRenderer $anchorRenderer = null ) { $this->_url = $url; $this->_iteratorFactory = $iteratorFactory; $this->_authSession = $authSession; $this->_menuConfig = $menuConfig; $this->_localeResolver = $localeResolver; + $this->menuItemChecker = $menuItemChecker; + $this->anchorRenderer = $anchorRenderer; parent::__construct($context, $data); } @@ -99,30 +115,6 @@ protected function _construct() $this->setCacheTags([self::CACHE_TAGS]); } - /** - * Check whether given item is currently selected - * - * @param \Magento\Backend\Model\Menu\Item $item - * @param int $level - * @return bool - */ - protected function _isItemActive(\Magento\Backend\Model\Menu\Item $item, $level) - { - $itemModel = $this->getActiveItemModel(); - $output = false; - - if ($level == 0 && - $itemModel instanceof \Magento\Backend\Model\Menu\Item && - ($itemModel->getId() == $item->getId() || - $item->getChildren()->get( - $itemModel->getId() - ) !== null) - ) { - $output = true; - } - return $output; - } - /** * Render menu item anchor label * @@ -134,40 +126,6 @@ protected function _getAnchorLabel($menuItem) return $this->escapeHtml(__($menuItem->getTitle())); } - /** - * Render menu item anchor title - * - * @param \Magento\Backend\Model\Menu\Item $menuItem - * @return string - */ - protected function _renderItemAnchorTitle($menuItem) - { - return $menuItem->hasTooltip() ? 'title="' . __($menuItem->getTooltip()) . '"' : ''; - } - - /** - * Render menu item onclick function - * - * @param \Magento\Backend\Model\Menu\Item $menuItem - * @return string - */ - protected function _renderItemOnclickFunction($menuItem) - { - return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : ''; - } - - /** - * Render menu item anchor css class - * - * @param \Magento\Backend\Model\Menu\Item $menuItem - * @param int $level - * @return string - */ - protected function _renderAnchorCssClass($menuItem, $level) - { - return $this->_isItemActive($menuItem, $level) ? '_active' : ''; - } - /** * Render menu item mouse events * @param \Magento\Backend\Model\Menu\Item $menuItem @@ -188,10 +146,11 @@ protected function _renderMouseEvent($menuItem) protected function _renderItemCssClass($menuItem, $level) { $isLast = 0 == $level && (bool)$this->getMenuModel()->isLast($menuItem) ? 'last' : ''; - $output = ($this->_isItemActive( - $menuItem, - $level - ) ? '_current _active' : '') . + $output = ($this->menuItemChecker->isItemActive( + $this->getActiveItemModel(), + $menuItem, + $level + ) ? '_current _active' : '') . ' ' . ($menuItem->hasChildren() ? 'parent' : '') . ' ' . @@ -202,34 +161,6 @@ protected function _renderItemCssClass($menuItem, $level) return $output; } - /** - * Render menu item anchor - * @param \Magento\Backend\Model\Menu\Item $menuItem - * @param int $level - * @return string - */ - protected function _renderAnchor($menuItem, $level) - { - if ($level == 1 && $menuItem->getUrl() == '#') { - $output = '' - . '' . $this->_getAnchorLabel($menuItem) . '' - . ''; - } else { - $output = '_renderItemAnchorTitle( - $menuItem - ) . $this->_renderItemOnclickFunction( - $menuItem - ) . ' class="' . $this->_renderAnchorCssClass( - $menuItem, - $level - ) . '">' . '' . $this->_getAnchorLabel( - $menuItem - ) . '' . ''; - } - - return $output; - } - /** * Get menu filter iterator * @@ -336,7 +267,7 @@ public function renderMenu($menu, $level = 0) $menuItem->getId() ) . 'role="menuitem">'; - $output .= $this->_renderAnchor($menuItem, $level); + $output .= $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level); if ($menuItem->hasChildren()) { $output .= $this->renderMenu($menuItem->getChildren(), $level + 1); @@ -456,7 +387,7 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = []) $id = $this->getJsId($menuItem->getId()); $subMenu = $this->_addSubMenu($menuItem, $level, $limit, $id); - $anchor = $this->_renderAnchor($menuItem, $level); + $anchor = $this->anchorRenderer->renderAnchor($this->getActiveItemModel(), $menuItem, $level); $output .= '
  • getUiId($menuItem->getId()) . ' class="item-' . $itemClass . ' ' . $this->_renderItemCssClass($menuItem, $level) . ($level == 0 ? '" id="' . $id . '" aria-haspopup="true' : '') @@ -474,14 +405,14 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = []) /** * Get current selected menu item * - * @return \Magento\Backend\Model\Menu\Item|null|bool + * @return \Magento\Backend\Model\Menu\Item|null */ public function getActiveItemModel() { if (is_null($this->_activeItemModel)) { $this->_activeItemModel = $this->getMenuModel()->get($this->getActive()); if (false == $this->_activeItemModel instanceof \Magento\Backend\Model\Menu\Item) { - $this->_activeItemModel = false; + $this->_activeItemModel = null; } } return $this->_activeItemModel; diff --git a/app/code/Magento/Backend/Block/MenuItemChecker.php b/app/code/Magento/Backend/Block/MenuItemChecker.php new file mode 100644 index 0000000000000..045a75740ecff --- /dev/null +++ b/app/code/Magento/Backend/Block/MenuItemChecker.php @@ -0,0 +1,48 @@ +isActiveItemEqualOrChild($activeItem, $item) + ) { + $output = true; + } + return $output; + } + + /** + * @param Item $activeItem, + * @param Item $item + * @return bool + */ + private function isActiveItemEqualOrChild($activeItem, $item) + { + return ($activeItem->getId() == $item->getId()) + || ($item->getChildren()->get($activeItem->getId()) !== null); + } +} diff --git a/app/code/Magento/Backend/Model/Menu/Config/Converter.php b/app/code/Magento/Backend/Model/Menu/Config/Converter.php index 2c0d9905f36db..a7084b1f21551 100644 --- a/app/code/Magento/Backend/Model/Menu/Config/Converter.php +++ b/app/code/Magento/Backend/Model/Menu/Config/Converter.php @@ -26,6 +26,7 @@ public function convert($dom) 'resource', 'dependsOnModule', 'dependsOnConfig', + 'target' ]; $xpath = new \DOMXPath($dom); $nodeList = $xpath->query('/config/menu/*'); diff --git a/app/code/Magento/Backend/Model/Menu/Item.php b/app/code/Magento/Backend/Model/Menu/Item.php index 32a45336e6e00..be27490052447 100644 --- a/app/code/Magento/Backend/Model/Menu/Item.php +++ b/app/code/Magento/Backend/Model/Menu/Item.php @@ -147,6 +147,13 @@ class Item */ private $_moduleManager; + /** + * Menu item target + * + * @var string|null + */ + protected $target; + /** * @param Item\Validator $validator * @param \Magento\Framework\AuthorizationInterface $authorization @@ -201,6 +208,16 @@ public function getId() return $this->_id; } + /** + * Retrieve item target + * + * @return string + */ + public function getTarget() + { + return $this->target; + } + /** * Check whether item has subnodes * @@ -456,6 +473,7 @@ public function toArray() 'depends_on_module' => $this->_dependsOnModule, 'tooltip' => $this->_tooltip, 'title' => $this->_title, + 'target' => $this->target, 'sub_menu' => isset($this->_submenu) ? $this->_submenu->toArray() : null ]; } @@ -479,6 +497,7 @@ public function populateFromArray(array $data) $this->_dependsOnModule = $this->_getArgument($data, 'depends_on_module'); $this->_tooltip = $this->_getArgument($data, 'tooltip', ''); $this->_title = $this->_getArgument($data, 'title'); + $this->target = $this->_getArgument($data, 'target'); if (isset($data['sub_menu'])) { $menu = $this->_menuFactory->create(); $menu->populateFromArray($data['sub_menu']); diff --git a/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php new file mode 100644 index 0000000000000..669f9eaf2adba --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php @@ -0,0 +1,133 @@ +activeMenuItemMock = $this->getMockBuilder(Item::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuItemMock = $this->getMockBuilder(Item::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuItemCheckerMock = $this->getMockBuilder(MenuItemChecker::class) + ->disableOriginalConstructor() + ->getMock(); + $this->escaperMock = $this->getMockBuilder(Escaper::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->anchorRenderer = $this->objectManagerHelper->getObject( + AnchorRenderer::class, + [ + 'menuItemChecker' => $this->menuItemCheckerMock, + 'escaper' => $this->escaperMock + ] + ); + } + + public function testRenderAnchorLevelIsOne() + { + $title = 'Title'; + $html = 'Test html'; + $this->menuItemMock->expects($this->once())->method('getUrl')->willReturn('#'); + $this->menuItemMock->expects($this->once())->method('getTitle')->willReturn($title); + $this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($html); + + $expected = '' + . '' . $html . '' + . ''; + + $this->assertEquals( + $expected, + $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, 1) + ); + } + + /** + * @param bool $hasTarget + * @dataProvider targetDataProvider + */ + public function testRenderAnchorLevelIsNotOne($hasTarget) + { + $level = 0; + $title = 'Title'; + $html = 'Test html'; + $url = 'test/url'; + $tooltip = 'Anchor title'; + $onclick = ''; + $target = '_blank'; + $finalTarget = $hasTarget ? ('target=' . $target) : ''; + $this->menuItemMock->expects($this->any())->method('getTarget')->willReturn($hasTarget ? $target : null); + $this->menuItemMock->expects($this->once())->method('getUrl')->willReturn($url); + $this->menuItemMock->expects($this->once())->method('getTitle')->willReturn($title); + $this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($html); + $this->menuItemMock->expects($this->once())->method('hasTooltip')->willReturn(true); + $this->menuItemMock->expects($this->any())->method('getTooltip')->willReturn(__($tooltip)); + $this->menuItemMock->expects($this->once())->method('hasClickCallback')->willReturn(true); + $this->menuItemMock->expects($this->once())->method('getClickCallback')->willReturn($onclick); + $this->menuItemCheckerMock->expects($this->once()) + ->method('isItemActive') + ->with($this->menuItemCheckerMock, $this->menuItemMock, $level)->willReturn(true); + + $expected = '' . '' . $html + . '' . ''; + + $this->assertEquals( + $expected, + $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level)); + } + + public function targetDataProvider() + { + return [ + 'item has target' => [true], + 'item does not have target' => [false] + ]; + } +} diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php new file mode 100644 index 0000000000000..215c18d78ef37 --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php @@ -0,0 +1,86 @@ +menuItem = $this->getMockBuilder(Item::class) + ->disableOriginalConstructor() + ->getMock(); + $this->activeMenuItem = $this->getMockBuilder(Item::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuItemChecker = new MenuItemChecker(); + } + + /** + * @param int $activeItemId + * @param int $itemId + * @param bool $isItem + * @param bool $expected + * @dataProvider dataProvider + */ + public function testIsItemActive( $activeItemId, $itemId, $isItem, $expected) + { + $this->menu = $this->getMockBuilder(Menu::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuItem->expects($this->any())->method('getId')->willReturn($itemId); + $this->activeMenuItem->expects($this->any())->method('getId')->willReturn($activeItemId); + $this->menuItem->expects($this->any())->method('getChildren')->willReturn($this->menu); + $this->menu->expects($this->any()) + ->method('get') + ->with($activeItemId) + ->willReturn($isItem ? $this->activeMenuItem : null); + $this->assertEquals($expected, + $this->menuItemChecker->isItemActive($this->activeMenuItem, $this->menuItem, 0) + + ); + } + + public function testIsItemActiveLevelNotZero() + { + $this->assertFalse( + $this->menuItemChecker->isItemActive($this->activeMenuItem, $this->menuItem, 1) + + ); + } + + public function dataProvider() + { + return [ + 'outputItemEquals' => ['1', '1', false, true], + 'outputItemIsChild' => ['1', '2', true, true], + 'outputItemIsChildNull' => ['1', '2', false, false], + ]; + } +} 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 index 0902ce48e217d..7199a2b3c01b1 100644 --- 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 @@ -28,7 +28,8 @@ 'depends_on_module' => 'Magento_Backend', 'tooltip' => 'Item tooltip', 'title' => 'Item Title', - 'sub_menu' => null + 'sub_menu' => null, + 'target' => null ], ], 'data without submenu to constructor' => [ @@ -75,7 +76,8 @@ 'depends_on_module' => null, 'tooltip' => '', 'title' => null, - 'sub_menu' => ['submenuArray'] + 'sub_menu' => ['submenuArray'], + 'target' => null ], ], 'data with submenu to constructor' => [ @@ -127,7 +129,8 @@ 'depends_on_module' => null, 'tooltip' => '', 'title' => null, - 'sub_menu' => ['submenuArray'] + 'sub_menu' => ['submenuArray'], + 'target' => null ], ] ]; 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 index d697e3c55d600..bc336aad5783d 100644 --- 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 @@ -29,6 +29,7 @@ 'tooltip' => 'Item tooltip', 'title' => 'Item Title', 'sub_menu' => null, + 'target' => null ] ], 'with submenu' => [ @@ -74,7 +75,8 @@ 'depends_on_module' => 'Magento_Backend', 'depends_on_config' => 'system/config/isEnabled', 'tooltip' => 'Item tooltip', - ] + ], + 'target' => null ] ], 'small set of data' => [ @@ -112,7 +114,8 @@ 'depends_on_module' => 'Magento_Backend', 'depends_on_config' => 'system/config/isEnabled', 'tooltip' => 'Item tooltip', - ] + ], + 'target' => null ] ] ]; diff --git a/app/code/Magento/Backend/etc/di.xml b/app/code/Magento/Backend/etc/di.xml index 632d9e9414a88..8894f2293fa35 100644 --- a/app/code/Magento/Backend/etc/di.xml +++ b/app/code/Magento/Backend/etc/di.xml @@ -207,4 +207,10 @@ + + + Magento\Backend\Block\MenuItemChecker + Magento\Backend\Block\AnchorRenderer + + diff --git a/app/code/Magento/Backend/etc/menu.xsd b/app/code/Magento/Backend/etc/menu.xsd index 5c5ad89bd89c1..f7a22103e2e90 100644 --- a/app/code/Magento/Backend/etc/menu.xsd +++ b/app/code/Magento/Backend/etc/menu.xsd @@ -31,6 +31,7 @@ + @@ -51,6 +52,7 @@ + diff --git a/index.php b/index.php index 81e6f89f2fc46..facc6fb9bf92a 100644 --- a/index.php +++ b/index.php @@ -32,6 +32,8 @@ HTML; exit(1); } +error_reporting(E_ALL); +ini_set('display_errors', 1); $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ From 787b72014ff2eccc8d21e5e13ff2e70b3d12f46c Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 14:30:48 +0200 Subject: [PATCH 02/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/Menu.php | 2 +- .../Test/Unit/Block/AnchorRendererTest.php | 12 +- .../Test/Unit/Block/MenuItemCheckerTest.php | 28 ++-- .../Backend/Test/Unit/Block/MenuTest.php | 153 ++++++++++++++++++ 4 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 app/code/Magento/Backend/Test/Unit/Block/MenuTest.php diff --git a/app/code/Magento/Backend/Block/Menu.php b/app/code/Magento/Backend/Block/Menu.php index ba805db285d24..8a83bb5fc2424 100644 --- a/app/code/Magento/Backend/Block/Menu.php +++ b/app/code/Magento/Backend/Block/Menu.php @@ -38,7 +38,7 @@ class Menu extends \Magento\Backend\Block\Template /** * Current selected item * - * @var \Magento\Backend\Model\Menu\Item|null|bool + * @var \Magento\Backend\Model\Menu\Item|null */ protected $_activeItemModel = null; diff --git a/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php index 669f9eaf2adba..4c952729bf5e1 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php @@ -61,10 +61,10 @@ protected function setUp() $this->objectManagerHelper = new ObjectManagerHelper($this); $this->anchorRenderer = $this->objectManagerHelper->getObject( AnchorRenderer::class, - [ - 'menuItemChecker' => $this->menuItemCheckerMock, - 'escaper' => $this->escaperMock - ] + [ + 'menuItemChecker' => $this->menuItemCheckerMock, + 'escaper' => $this->escaperMock + ] ); } @@ -110,7 +110,7 @@ public function testRenderAnchorLevelIsNotOne($hasTarget) $this->menuItemMock->expects($this->once())->method('getClickCallback')->willReturn($onclick); $this->menuItemCheckerMock->expects($this->once()) ->method('isItemActive') - ->with($this->menuItemCheckerMock, $this->menuItemMock, $level)->willReturn(true); + ->with($this->activeMenuItemMock, $this->menuItemMock, $level)->willReturn(true); $expected = 'anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level)); } - public function targetDataProvider() + public function targetDataProvider() { return [ 'item has target' => [true], diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php index 215c18d78ef37..8f351a08e6204 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php @@ -14,17 +14,17 @@ class MenuItemCheckerTest extends \PHPUnit_Framework_TestCase /** * @var Item|\PHPUnit_Framework_MockObject_MockObject */ - private $activeMenuItem; + private $activeMenuItemMock; /** * @var Item|\PHPUnit_Framework_MockObject_MockObject */ - private $menuItem; + private $menuItemMock; /** * @var Menu|\PHPUnit_Framework_MockObject_MockObject */ - private $menu; + private $menuMock; /** * @var MenuItemChecker; @@ -33,10 +33,10 @@ class MenuItemCheckerTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->menuItem = $this->getMockBuilder(Item::class) + $this->menuItemMock = $this->getMockBuilder(Item::class) ->disableOriginalConstructor() ->getMock(); - $this->activeMenuItem = $this->getMockBuilder(Item::class) + $this->activeMenuItemMock = $this->getMockBuilder(Item::class) ->disableOriginalConstructor() ->getMock(); $this->menuItemChecker = new MenuItemChecker(); @@ -49,20 +49,20 @@ protected function setUp() * @param bool $expected * @dataProvider dataProvider */ - public function testIsItemActive( $activeItemId, $itemId, $isItem, $expected) + public function testIsItemActive($activeItemId, $itemId, $isItem, $expected) { - $this->menu = $this->getMockBuilder(Menu::class) + $this->menuMock = $this->getMockBuilder(Menu::class) ->disableOriginalConstructor() ->getMock(); - $this->menuItem->expects($this->any())->method('getId')->willReturn($itemId); - $this->activeMenuItem->expects($this->any())->method('getId')->willReturn($activeItemId); - $this->menuItem->expects($this->any())->method('getChildren')->willReturn($this->menu); - $this->menu->expects($this->any()) + $this->menuItemMock->expects($this->any())->method('getId')->willReturn($itemId); + $this->activeMenuItemMock->expects($this->any())->method('getId')->willReturn($activeItemId); + $this->menuItemMock->expects($this->any())->method('getChildren')->willReturn($this->menuMock); + $this->menuMock->expects($this->any()) ->method('get') ->with($activeItemId) - ->willReturn($isItem ? $this->activeMenuItem : null); + ->willReturn($isItem ? $this->activeMenuItemMock : null); $this->assertEquals($expected, - $this->menuItemChecker->isItemActive($this->activeMenuItem, $this->menuItem, 0) + $this->menuItemChecker->isItemActive($this->activeMenuItemMock, $this->menuItemMock, 0) ); } @@ -70,7 +70,7 @@ public function testIsItemActive( $activeItemId, $itemId, $isItem, $expected) public function testIsItemActiveLevelNotZero() { $this->assertFalse( - $this->menuItemChecker->isItemActive($this->activeMenuItem, $this->menuItem, 1) + $this->menuItemChecker->isItemActive($this->activeMenuItemMock, $this->menuItemMock, 1) ); } diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php new file mode 100644 index 0000000000000..ff49f896c31cd --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php @@ -0,0 +1,153 @@ +activeItemMock = $this->getMockBuilder(Item::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->urlMock = $this->getMockBuilder(UrlInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->iteratorFactoryMock = $this->getMockBuilder(IteratorFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->authSessionMock = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuConfigMock = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->getMock(); + $this->localeResolverMock = $this->getMockBuilder(ResolverInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menuItemChecker = $this->getMockBuilder(MenuItemChecker::class) + ->disableOriginalConstructor() + ->getMock(); + $this->anchorRendererMock = $this->getMockBuilder(AnchorRenderer::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->menu = $this->objectManagerHelper->getObject( + Menu::class, + [ + 'url' => $this->urlMock, + 'iteratorFactory' => $this->iteratorFactoryMock, + 'authSession' => $this->authSessionMock, + 'menuConfig' => $this->menuConfigMock, + 'localeResolver' => $this->localeResolverMock, + 'menuItemChecker' => $this->menuItemCheckerMock, + 'anchorRenderer' => $this->anchorRendererMock + ] + ); + } + + public function testGetActiveItemModelMenuIsNotNull() + { + $this->menuModelMock = $this->getMockBuilder(MenuModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menu->setActive($this->activeItemMock); + $this->menuConfigMock->expects($this->once())->method('getMenu')->willReturn($this->menuModelMock); + $this->menuModelMock->expects($this->once()) + ->method('get') + ->willReturn($this->activeItemMock); + + $this->assertEquals($this->activeItemMock, $this->menu->getActiveItemModel()); + } + + public function testGetActiveItemModelMenuIsNull() + { + $this->menuModelMock = $this->getMockBuilder(MenuModel::class) + ->disableOriginalConstructor() + ->getMock(); + $this->menu->setActive(null); + $this->menuConfigMock->expects($this->once())->method('getMenu')->willReturn($this->menuModelMock); + $this->menuModelMock->expects($this->once()) + ->method('get') + ->willReturn(null); + + $this->assertNull($this->menu->getActiveItemModel()); + } +} From 33498a237c07d3f8a053a8cf497d8a07005c88fe Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 14:49:31 +0200 Subject: [PATCH 03/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/AnchorRenderer.php | 1 + app/code/Magento/Backend/Model/Menu/Item.php | 4 ++-- app/code/Magento/Backend/Test/Unit/Block/MenuTest.php | 6 +++--- index.php | 2 -- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index 4eaebb197544c..508db43f6e13b 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -37,6 +37,7 @@ public function __construct( /** * Render menu item anchor + * * @param Item $activeItem * @param Item $menuItem * @param int $level diff --git a/app/code/Magento/Backend/Model/Menu/Item.php b/app/code/Magento/Backend/Model/Menu/Item.php index be27490052447..b9f67809f9b0b 100644 --- a/app/code/Magento/Backend/Model/Menu/Item.php +++ b/app/code/Magento/Backend/Model/Menu/Item.php @@ -152,7 +152,7 @@ class Item * * @var string|null */ - protected $target; + private $target; /** * @param Item\Validator $validator @@ -211,7 +211,7 @@ public function getId() /** * Retrieve item target * - * @return string + * @return string|null */ public function getTarget() { diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php index ff49f896c31cd..ebc725e16f5ca 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php @@ -38,12 +38,12 @@ class MenuTest extends \PHPUnit_Framework_TestCase /** * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlMock; + private $urlMock; /** * @var IteratorFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $iteratorFactoryMock; + private $iteratorFactoryMock; /** * @var Session|\PHPUnit_Framework_MockObject_MockObject @@ -71,7 +71,7 @@ class MenuTest extends \PHPUnit_Framework_TestCase private $menuItemCheckerMock; /** - * @var AnchorRendererMock|\PHPUnit_Framework_MockObject_MockObject + * @var AnchorRenderer|\PHPUnit_Framework_MockObject_MockObject */ private $anchorRendererMock; diff --git a/index.php b/index.php index facc6fb9bf92a..81e6f89f2fc46 100644 --- a/index.php +++ b/index.php @@ -32,8 +32,6 @@ HTML; exit(1); } -error_reporting(E_ALL); -ini_set('display_errors', 1); $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); /** @var \Magento\Framework\App\Http $app */ From 78c2352acb0641f46b50f71fbe50495c7e0ab39d Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 15:26:02 +0200 Subject: [PATCH 04/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/AnchorRenderer.php | 4 ++-- app/code/Magento/Backend/Block/MenuItemChecker.php | 4 ++-- .../Magento/Backend/Test/Unit/Block/AnchorRendererTest.php | 3 ++- .../Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php | 5 ++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index 508db43f6e13b..fbebab3c74f44 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -52,8 +52,8 @@ public function renderAnchor(Item $activeItem, Item $menuItem, $level) } else { $target = $menuItem->getTarget() ? ('target=' . $menuItem->getTarget()) : ''; $output = '_renderItemAnchorTitle( - $menuItem - ) . $this->_renderItemOnclickFunction( + $menuItem + ) . $this->_renderItemOnclickFunction( $menuItem ) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '') . '">' . '' . $this->escaper->escapeHtml(__($menuItem->getTitle())) diff --git a/app/code/Magento/Backend/Block/MenuItemChecker.php b/app/code/Magento/Backend/Block/MenuItemChecker.php index 045a75740ecff..d081ad27bfe26 100644 --- a/app/code/Magento/Backend/Block/MenuItemChecker.php +++ b/app/code/Magento/Backend/Block/MenuItemChecker.php @@ -41,8 +41,8 @@ public function isItemActive( * @return bool */ private function isActiveItemEqualOrChild($activeItem, $item) - { + { return ($activeItem->getId() == $item->getId()) || ($item->getChildren()->get($activeItem->getId()) !== null); - } + } } diff --git a/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php index 4c952729bf5e1..a8a520960e768 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/AnchorRendererTest.php @@ -120,7 +120,8 @@ public function testRenderAnchorLevelIsNotOne($hasTarget) $this->assertEquals( $expected, - $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level)); + $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level) + ); } public function targetDataProvider() diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php index 8f351a08e6204..603f825518d4a 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuItemCheckerTest.php @@ -61,9 +61,9 @@ public function testIsItemActive($activeItemId, $itemId, $isItem, $expected) ->method('get') ->with($activeItemId) ->willReturn($isItem ? $this->activeMenuItemMock : null); - $this->assertEquals($expected, + $this->assertEquals( + $expected, $this->menuItemChecker->isItemActive($this->activeMenuItemMock, $this->menuItemMock, 0) - ); } @@ -71,7 +71,6 @@ public function testIsItemActiveLevelNotZero() { $this->assertFalse( $this->menuItemChecker->isItemActive($this->activeMenuItemMock, $this->menuItemMock, 1) - ); } From 3962e308ed74ec7ac253195129b2d21322d3a135 Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 15:28:23 +0200 Subject: [PATCH 05/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Test/Unit/Block/MenuTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php index ebc725e16f5ca..eae483df839fe 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php @@ -8,7 +8,6 @@ use Magento\Backend\Model\Menu\Item; use Magento\Backend\Model\Menu as MenuModel; use Magento\Backend\Block\Menu; -use Magento\Backend\Block\Template\Context; use Magento\Backend\Model\UrlInterface; use Magento\Backend\Model\Menu\Filter\IteratorFactory; use Magento\Backend\Model\Auth\Session; @@ -30,11 +29,6 @@ class MenuTest extends \PHPUnit_Framework_TestCase */ private $menuModelMock; - /** - * @var Context|\PHPUnit_Framework_MockObject_MockObject - */ - private $contextMock; - /** * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */ From ed3cb39f433a1ddc32553b5dd952af21d39bc210 Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 16:20:29 +0200 Subject: [PATCH 06/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/AnchorRenderer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index fbebab3c74f44..0d7546b2bbe1f 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -54,8 +54,8 @@ public function renderAnchor(Item $activeItem, Item $menuItem, $level) $output = '_renderItemAnchorTitle( $menuItem ) . $this->_renderItemOnclickFunction( - $menuItem - ) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '') + $menuItem + ) . ' class="' . ($this->menuItemChecker->isItemActive($activeItem, $menuItem, $level) ? '_active' : '') . '">' . '' . $this->escaper->escapeHtml(__($menuItem->getTitle())) . '' . ''; } @@ -80,7 +80,7 @@ private function _renderItemAnchorTitle($menuItem) * @param Item $menuItem * @return string */ - private function _renderItemOnclickFunction($menuItem) + private function _renderItemOnclickFunction($menuItem) { return $menuItem->hasClickCallback() ? ' onclick="' . $menuItem->getClickCallback() . '"' : ''; } From 4faa58ff25a6b991c4620ef62e1e48b53d65bb9a Mon Sep 17 00:00:00 2001 From: olysenko Date: Mon, 30 Jan 2017 16:38:57 +0200 Subject: [PATCH 07/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/AnchorRenderer.php | 4 ++-- app/code/Magento/Backend/Block/MenuItemChecker.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index 0d7546b2bbe1f..9071702df4444 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -38,12 +38,12 @@ public function __construct( /** * Render menu item anchor * - * @param Item $activeItem + * @param Item|null $activeItem * @param Item $menuItem * @param int $level * @return string */ - public function renderAnchor(Item $activeItem, Item $menuItem, $level) + public function renderAnchor($activeItem, Item $menuItem, $level) { if ($level == 1 && $menuItem->getUrl() == '#') { $output = '' diff --git a/app/code/Magento/Backend/Block/MenuItemChecker.php b/app/code/Magento/Backend/Block/MenuItemChecker.php index d081ad27bfe26..fdeab52cfb644 100644 --- a/app/code/Magento/Backend/Block/MenuItemChecker.php +++ b/app/code/Magento/Backend/Block/MenuItemChecker.php @@ -15,19 +15,20 @@ class MenuItemChecker /** * Check whether given item is currently selected * - * @param Item $activeItem, + * @param Item|null $activeItem, * @param Item $item * @param int $level * @return bool */ public function isItemActive( - Item $activeItem, + $activeItem, Item $item, $level ) { $output = false; if ($level == 0 + && $activeItem instanceof \Magento\Backend\Model\Menu\Item && $this->isActiveItemEqualOrChild($activeItem, $item) ) { $output = true; From 9f03024725c347217900c73986e60ca01c8fd5ae Mon Sep 17 00:00:00 2001 From: olysenko Date: Wed, 1 Feb 2017 10:33:25 +0200 Subject: [PATCH 08/12] MAGETWO-63716: Add target to admin menu element --- .../Magento/Backend/Block/AnchorRenderer.php | 5 +- app/code/Magento/Backend/Block/Menu.php | 6 +- .../Magento/Backend/Block/MenuItemChecker.php | 10 +- .../Adminhtml/System/Store/Index.php | 9 +- .../Backend/Test/Unit/Block/MenuTest.php | 3 +- .../Adminhtml/System/Store/IndexTest.php | 97 +++++++++++++++ .../Magento/Reports/etc/adminhtml/menu.xml | 2 +- .../Search/Controller/Adminhtml/Term.php | 2 +- .../Controller/Adminhtml/Term/Report.php | 4 +- .../Controller/Adminhtml/Term/IndexTest.php | 101 ++++++++++++++++ .../Controller/Adminhtml/Term/ReportTest.php | 114 ++++++++++++++++++ 11 files changed, 334 insertions(+), 19 deletions(-) create mode 100644 app/code/Magento/Backend/Test/Unit/Controller/Adminhtml/System/Store/IndexTest.php create mode 100644 app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php create mode 100644 app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/ReportTest.php diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index 9071702df4444..6b5579fc02d0e 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -36,9 +36,10 @@ public function __construct( } /** - * Render menu item anchor + * Render menu item anchor. * - * @param Item|null $activeItem + * @param Item|false $activeItem Can be false if menu item is inaccessible + * but was triggered directly using controller. It is a legacy code behaviour. * @param Item $menuItem * @param int $level * @return string diff --git a/app/code/Magento/Backend/Block/Menu.php b/app/code/Magento/Backend/Block/Menu.php index 8a83bb5fc2424..4c4d75b7977b5 100644 --- a/app/code/Magento/Backend/Block/Menu.php +++ b/app/code/Magento/Backend/Block/Menu.php @@ -38,7 +38,7 @@ class Menu extends \Magento\Backend\Block\Template /** * Current selected item * - * @var \Magento\Backend\Model\Menu\Item|null + * @var \Magento\Backend\Model\Menu\Item|false|null */ protected $_activeItemModel = null; @@ -405,14 +405,14 @@ public function renderNavigation($menu, $level = 0, $limit = 0, $colBrakes = []) /** * Get current selected menu item * - * @return \Magento\Backend\Model\Menu\Item|null + * @return \Magento\Backend\Model\Menu\Item|false */ public function getActiveItemModel() { if (is_null($this->_activeItemModel)) { $this->_activeItemModel = $this->getMenuModel()->get($this->getActive()); if (false == $this->_activeItemModel instanceof \Magento\Backend\Model\Menu\Item) { - $this->_activeItemModel = null; + $this->_activeItemModel = false; } } return $this->_activeItemModel; diff --git a/app/code/Magento/Backend/Block/MenuItemChecker.php b/app/code/Magento/Backend/Block/MenuItemChecker.php index fdeab52cfb644..dbcba24d3d16a 100644 --- a/app/code/Magento/Backend/Block/MenuItemChecker.php +++ b/app/code/Magento/Backend/Block/MenuItemChecker.php @@ -15,16 +15,14 @@ class MenuItemChecker /** * Check whether given item is currently selected * - * @param Item|null $activeItem, + * @param Item|false $activeItem Can be false if menu item is inaccessible + * but was triggered directly using controller. It is a legacy code behaviour. * @param Item $item * @param int $level * @return bool */ - public function isItemActive( - $activeItem, - Item $item, - $level - ) { + public function isItemActive($activeItem, Item $item, $level) + { $output = false; if ($level == 0 diff --git a/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php b/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php index 8741f1b37ea7d..8129c9e0a2f86 100644 --- a/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php +++ b/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php @@ -6,6 +6,8 @@ */ namespace Magento\Backend\Controller\Adminhtml\System\Store; +use Magento\Framework\Controller\ResultFactory; + class Index extends \Magento\Backend\Controller\Adminhtml\System\Store { /** @@ -13,9 +15,12 @@ class Index extends \Magento\Backend\Controller\Adminhtml\System\Store */ public function execute() { - $resultPage = $this->resultPageFactory->create(); + /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ + $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE); + $resultPage->setActiveMenu('Magento_Backend::system_store'); + $resultPage->addBreadcrumb(__('Stores'), __('Stores')); + $resultPage->addBreadcrumb(__('All Stores'), __('All Stores')); $resultPage->getConfig()->getTitle()->prepend(__('Stores')); - return $resultPage; } } diff --git a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php index eae483df839fe..ccc0e719aaecb 100644 --- a/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php +++ b/app/code/Magento/Backend/Test/Unit/Block/MenuTest.php @@ -79,7 +79,6 @@ protected function setUp() $this->activeItemMock = $this->getMockBuilder(Item::class) ->disableOriginalConstructor() ->getMock(); - $this->urlMock = $this->getMockBuilder(UrlInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -142,6 +141,6 @@ public function testGetActiveItemModelMenuIsNull() ->method('get') ->willReturn(null); - $this->assertNull($this->menu->getActiveItemModel()); + $this->assertFalse($this->menu->getActiveItemModel()); } } diff --git a/app/code/Magento/Backend/Test/Unit/Controller/Adminhtml/System/Store/IndexTest.php b/app/code/Magento/Backend/Test/Unit/Controller/Adminhtml/System/Store/IndexTest.php new file mode 100644 index 0000000000000..d65dee798b367 --- /dev/null +++ b/app/code/Magento/Backend/Test/Unit/Controller/Adminhtml/System/Store/IndexTest.php @@ -0,0 +1,97 @@ +resultFactoryMock = $this->getMockBuilder(ResultFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageMock = $this->getMockBuilder(Page::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageConfigMock = $this->getMockBuilder(Config::class) + ->setMethods(['getTitle']) + ->disableOriginalConstructor() + ->getMock(); + $this->titleMock = $this->getMockBuilder(Title::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->indexController = $this->objectManagerHelper->getObject( + Index::class, + [ + 'resultFactory' => $this->resultFactoryMock + ] + ); + } + + public function testIndex() + { + $this->resultFactoryMock->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_PAGE) + ->willReturn($this->pageMock); + $this->pageMock->expects($this->once()) + ->method('setActiveMenu') + ->with('Magento_Backend::system_store') + ->willReturnSelf(); + $this->pageMock->expects($this->exactly(2)) + ->method('addBreadcrumb') + ->withConsecutive( + [__('Stores'), __('Stores')], + [__('All Stores'), __('All Stores')] + ); + $this->pageMock->expects($this->once()) + ->method('getConfig') + ->willReturn($this->pageConfigMock); + $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($this->titleMock); + $this->titleMock->expects($this->once())->method('prepend')->with(__('Stores'))->willReturn($this->pageMock); + + $this->assertSame($this->pageMock, $this->indexController->execute()); + } +} diff --git a/app/code/Magento/Reports/etc/adminhtml/menu.xml b/app/code/Magento/Reports/etc/adminhtml/menu.xml index a92d1df052b6d..b5bd516e49018 100644 --- a/app/code/Magento/Reports/etc/adminhtml/menu.xml +++ b/app/code/Magento/Reports/etc/adminhtml/menu.xml @@ -29,6 +29,6 @@ - + diff --git a/app/code/Magento/Search/Controller/Adminhtml/Term.php b/app/code/Magento/Search/Controller/Adminhtml/Term.php index 5ceb718c75e96..60662688e1315 100644 --- a/app/code/Magento/Search/Controller/Adminhtml/Term.php +++ b/app/code/Magento/Search/Controller/Adminhtml/Term.php @@ -24,7 +24,7 @@ protected function createPage() { /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE); - $resultPage->setActiveMenu('Magento_Search::search_term') + $resultPage->setActiveMenu('Magento_Search::search_terms') ->addBreadcrumb(__('Search'), __('Search')); return $resultPage; } diff --git a/app/code/Magento/Search/Controller/Adminhtml/Term/Report.php b/app/code/Magento/Search/Controller/Adminhtml/Term/Report.php index a7db452258c87..8591ebffef248 100644 --- a/app/code/Magento/Search/Controller/Adminhtml/Term/Report.php +++ b/app/code/Magento/Search/Controller/Adminhtml/Term/Report.php @@ -27,10 +27,10 @@ public function execute() $this->_eventManager->dispatch('on_view_report', ['report' => 'search']); /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE); - $resultPage->setActiveMenu('Magento_Reports::report_search') + $resultPage->setActiveMenu('Magento_Search::report_search_term') ->addBreadcrumb(__('Reports'), __('Reports')) ->addBreadcrumb(__('Search Terms'), __('Search Terms')); - $resultPage->getConfig()->getTitle()->set(__('Search Terms Report')); + $resultPage->getConfig()->getTitle()->prepend(__('Search Terms Report')); return $resultPage; } } diff --git a/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php new file mode 100644 index 0000000000000..5f2fa412d7308 --- /dev/null +++ b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php @@ -0,0 +1,101 @@ +resultFactoryMock = $this->getMockBuilder(ResultFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageMock = $this->getMockBuilder(Page::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageConfigMock = $this->getMockBuilder(Config::class) + ->setMethods(['getTitle']) + ->disableOriginalConstructor() + ->getMock(); + $this->titleMock = $this->getMockBuilder(Title::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->indexController = $this->objectManagerHelper->getObject( + Index::class, + [ + 'resultFactory' => $this->resultFactoryMock + ] + ); + } + + public function testIndex() + { + $this->resultFactoryMock->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_PAGE) + ->willReturn($this->pageMock); + $this->pageMock->expects($this->once()) + ->method('setActiveMenu') + ->with('Magento_Search::search_terms') + ->willReturnSelf(); + $this->pageMock->expects($this->exactly(2)) + ->method('addBreadcrumb') + ->withConsecutive( + [__('Search'), __('Search')], + [__('Search'), __('Search')] + ); + $this->pageMock->expects($this->once()) + ->method('getConfig') + ->willReturn($this->pageConfigMock); + $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($this->titleMock); + $this->titleMock->expects($this->once()) + ->method('prepend') + ->with(__('Search Terms')) + ->willReturn($this->pageMock); + + $this->assertSame($this->pageMock, $this->indexController->execute()); + } +} diff --git a/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/ReportTest.php b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/ReportTest.php new file mode 100644 index 0000000000000..044640b9d4a1f --- /dev/null +++ b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/ReportTest.php @@ -0,0 +1,114 @@ +resultFactoryMock = $this->getMockBuilder(ResultFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageMock = $this->getMockBuilder(Page::class) + ->disableOriginalConstructor() + ->getMock(); + $this->pageConfigMock = $this->getMockBuilder(Config::class) + ->setMethods(['getTitle']) + ->disableOriginalConstructor() + ->getMock(); + $this->titleMock = $this->getMockBuilder(Title::class) + ->disableOriginalConstructor() + ->getMock(); + $this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->contextMock = $this->getMockBuilder(Context::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->reportController = $this->objectManagerHelper->getObject( + Report::class, + [ + '_eventManager' => $this->eventManagerMock, + 'resultFactory' => $this->resultFactoryMock + ] + ); + } + + public function testReport() + { + $this->eventManagerMock->expects($this->once())->method('dispatch'); + + $this->resultFactoryMock->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_PAGE) + ->willReturn($this->pageMock); + $this->pageMock->expects($this->once()) + ->method('setActiveMenu') + ->with('Magento_Search::report_search_term') + ->willReturnSelf(); + $this->pageMock->expects($this->exactly(2)) + ->method('addBreadcrumb') + ->withConsecutive([__('Reports'), __('Reports')], [__('Search Terms'), __('Search Terms')]) + ->willReturnSelf(); + $this->pageMock->expects($this->once()) + ->method('getConfig') + ->willReturn($this->pageConfigMock); + $this->pageConfigMock->expects($this->once())->method('getTitle')->willReturn($this->titleMock); + $this->titleMock->expects($this->once()) + ->method('prepend') + ->with(__('Search Terms Report')) + ->willReturn($this->pageMock); + + $this->assertSame($this->pageMock, $this->reportController->execute()); + } +} From 12b3d3d14689cbe56033354543ef7bb8e36e686d Mon Sep 17 00:00:00 2001 From: olysenko Date: Wed, 1 Feb 2017 12:32:31 +0200 Subject: [PATCH 09/12] MAGETWO-63716: Add target to admin menu element --- .../Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php | 1 - .../integration/testsuite/Magento/Backend/Model/MenuTest.php | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php index 5f2fa412d7308..1c5914508f574 100644 --- a/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php +++ b/app/code/Magento/Search/Test/Unit/Controller/Adminhtml/Term/IndexTest.php @@ -45,7 +45,6 @@ class IndexTest extends \PHPUnit_Framework_TestCase */ private $indexController; - public function setUp() { $this->resultFactoryMock = $this->getMockBuilder(ResultFactory::class) diff --git a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php index e5e25638c2201..d0b38622cb87c 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php @@ -120,7 +120,7 @@ public function testSerialize() . '"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}]}]'; + . '"target":null,"sub_menu":null}]}]'; $this->assertEquals($expected, $serializedString); } @@ -135,7 +135,7 @@ public function testUnserialize() . '"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}]}]'; + . '"target":null,"sub_menu":null}]}]'; /** @var Menu $menu */ $menu = $this->objectManager->get(\Magento\Backend\Model\MenuFactory::class)->create(); $menu->unserialize($serializedMenu); From 6a87c469420cf8963a1cfeb5349b0e526ab45919 Mon Sep 17 00:00:00 2001 From: olysenko Date: Wed, 1 Feb 2017 13:37:48 +0200 Subject: [PATCH 10/12] MAGETWO-63716: Add target to admin menu element --- .../testsuite/Magento/Backend/Model/MenuTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php index d0b38622cb87c..1b8517f830dc7 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Model/MenuTest.php @@ -117,7 +117,7 @@ public function testSerialize() $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,' + . '"target":null,"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",' . '"target":null,"sub_menu":null}]}]'; @@ -132,7 +132,7 @@ 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,' + . '"target":null,"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",' . '"target":null,"sub_menu":null}]}]'; @@ -152,6 +152,7 @@ public function testUnserialize() 'depends_on_module' => null, 'tooltip' => '', 'title' => 'Extended System', + 'target' => null, 'sub_menu' => [ [ @@ -167,6 +168,7 @@ public function testUnserialize() 'tooltip' => '', 'title' => 'Acl', 'sub_menu' => null, + 'target' => null ], ], ], From 68fe4b4450b77d4b1b5a75e5beca3872db27dbae Mon Sep 17 00:00:00 2001 From: olysenko Date: Thu, 2 Feb 2017 12:00:18 +0200 Subject: [PATCH 11/12] MAGETWO-63716: Add target to admin menu element --- .../Backend/Controller/Adminhtml/System/Store/Index.php | 5 +++++ app/code/Magento/Backend/Model/Menu/Config/Converter.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php b/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php index 8129c9e0a2f86..4ab19ea435892 100644 --- a/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php +++ b/app/code/Magento/Backend/Controller/Adminhtml/System/Store/Index.php @@ -8,9 +8,14 @@ use Magento\Framework\Controller\ResultFactory; +/** + * Class Index returns Stores page + */ class Index extends \Magento\Backend\Controller\Adminhtml\System\Store { /** + * Returns Stores page + * * @return \Magento\Backend\Model\View\Result\Page */ public function execute() diff --git a/app/code/Magento/Backend/Model/Menu/Config/Converter.php b/app/code/Magento/Backend/Model/Menu/Config/Converter.php index a7084b1f21551..f806cf5fd4391 100644 --- a/app/code/Magento/Backend/Model/Menu/Config/Converter.php +++ b/app/code/Magento/Backend/Model/Menu/Config/Converter.php @@ -5,9 +5,14 @@ */ namespace Magento\Backend\Model\Menu\Config; +/** + * Class Converter converts xml to appropriate array + */ class Converter implements \Magento\Framework\Config\ConverterInterface { /** + * Converts xml to appropriate array + * * @param mixed $dom * @return array */ From 7689c8a55e4288f49f74ba0e017b8b742a66e34f Mon Sep 17 00:00:00 2001 From: olysenko Date: Fri, 3 Feb 2017 11:23:10 +0200 Subject: [PATCH 12/12] MAGETWO-63716: Add target to admin menu element --- app/code/Magento/Backend/Block/AnchorRenderer.php | 2 ++ app/code/Magento/Backend/Block/MenuItemChecker.php | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Backend/Block/AnchorRenderer.php b/app/code/Magento/Backend/Block/AnchorRenderer.php index 6b5579fc02d0e..c871de5f67c22 100644 --- a/app/code/Magento/Backend/Block/AnchorRenderer.php +++ b/app/code/Magento/Backend/Block/AnchorRenderer.php @@ -38,6 +38,8 @@ public function __construct( /** * Render menu item anchor. * + * It is used in backend menu to render anchor menu. + * * @param Item|false $activeItem Can be false if menu item is inaccessible * but was triggered directly using controller. It is a legacy code behaviour. * @param Item $menuItem diff --git a/app/code/Magento/Backend/Block/MenuItemChecker.php b/app/code/Magento/Backend/Block/MenuItemChecker.php index dbcba24d3d16a..b85b060cd379d 100644 --- a/app/code/Magento/Backend/Block/MenuItemChecker.php +++ b/app/code/Magento/Backend/Block/MenuItemChecker.php @@ -13,7 +13,9 @@ class MenuItemChecker { /** - * Check whether given item is currently selected + * Check whether given menu item is currently selected. + * + * It is used in backend menu to highlight active menu item. * * @param Item|false $activeItem Can be false if menu item is inaccessible * but was triggered directly using controller. It is a legacy code behaviour.