AnchorRendererTest.php 5.34 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Backend\Test\Unit\Block;

use Magento\Backend\Block\AnchorRenderer;
use Magento\Backend\Block\MenuItemChecker;
use Magento\Backend\Model\Menu\Item;
use Magento\Framework\Escaper;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class AnchorRendererTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var Item|\PHPUnit_Framework_MockObject_MockObject
     */
    private $activeMenuItemMock;

    /**
     * @var Item|\PHPUnit_Framework_MockObject_MockObject
     */
    private $menuItemMock;

    /**
     * @var Escaper|\PHPUnit_Framework_MockObject_MockObject
     */
    private $escaperMock;

    /**
     * @var ObjectManagerHelper
     */
    private $objectManagerHelper;

    /**
     * @var MenuItemChecker|\PHPUnit_Framework_MockObject_MockObject
     */
    private $menuItemCheckerMock;

    /**
     * @var AnchorRenderer
     */
    private $anchorRenderer;

    protected function setUp()
    {
        $this->activeMenuItemMock = $this->getMockBuilder(Item::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->menuItemMock = $this->getMockBuilder(Item::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->menuItemWithoutChildrenMock = $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->menuItemMock->expects($this->once())->method('hasChildren')->willReturn(true);
        $this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($html);

        $expected =  '<strong class="submenu-group-title" role="presentation">'
            . '<span>' . $html . '</span>'
            . '</strong>';

        $this->assertEquals(
            $expected,
            $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, 1)
        );
    }

    public function testRenderAnchorWithoutChildrenAndLevelIsOne()
    {
        $this->menuItemWithoutChildrenMock->expects($this->once())->method('getUrl')->willReturn('#');
        $this->menuItemWithoutChildrenMock->expects($this->once())->method('hasChildren')->willReturn(false);

        $expected =  '';

        $this->assertEquals(
            $expected,
            $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemWithoutChildrenMock, 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->activeMenuItemMock, $this->menuItemMock, $level)->willReturn(true);

        $expected = '<a href="' . $url . '" ' . $finalTarget . ' ' . 'title="' . $tooltip . '"'
            . ' onclick="' . $onclick . '"'
            . ' class="' . '_active'
            . '">' . '<span>' . $html
            . '</span>' . '</a>';

        $this->assertEquals(
            $expected,
            $this->anchorRenderer->renderAnchor($this->activeMenuItemMock, $this->menuItemMock, $level)
        );
    }

    /**
     * @return array
     */
    public function targetDataProvider()
    {
        return [
            'item has target' => [true],
            'item does not have target' => [false]
        ];
    }
}