Skip to content

[Framework] New Link is not correctly shown as Current if contains default parts #19134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
26 changes: 16 additions & 10 deletions lib/internal/Magento/Framework/View/Element/Html/Link/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
namespace Magento\Framework\View\Element\Html\Link;

use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

/**
* Block representing link with two possible states.
* "Current" state means link leads to URL equivalent to URL of currently displayed page.
Expand All @@ -17,25 +21,25 @@
* @method null|bool getCurrent()
* @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
*/
class Current extends \Magento\Framework\View\Element\Template
class Current extends Template
{
/**
* Default path
*
* @var \Magento\Framework\App\DefaultPathInterface
* @var DefaultPathInterface
*/
protected $_defaultPath;

/**
* Constructor
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\App\DefaultPathInterface $defaultPath
* @param Context $context
* @param DefaultPathInterface $defaultPath
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\DefaultPathInterface $defaultPath,
Context $context,
DefaultPathInterface $defaultPath,
array $data = []
) {
parent::__construct($context, $data);
Expand All @@ -56,18 +60,20 @@ public function getHref()
* Get current mca
*
* @return string
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
*/
private function getMca()
{
$routeParts = [
'module' => $this->_request->getModuleName(),
'controller' => $this->_request->getControllerName(),
'action' => $this->_request->getActionName(),
(string)$this->_request->getModuleName(),
(string)$this->_request->getControllerName(),
(string)$this->_request->getActionName(),
];

$parts = [];
$pathParts = explode('/', trim($this->_request->getPathInfo(), '/'));
foreach ($routeParts as $key => $value) {
if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
if (isset($pathParts[$key]) && $pathParts[$key] === $value) {
$parts[] = $value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class CurrentTest extends \PHPUnit\Framework\TestCase
*/
protected $_requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $_defaultPathMock;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
Expand All @@ -32,7 +27,6 @@ protected function setUp()
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
$this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
$this->_defaultPathMock = $this->createMock(\Magento\Framework\App\DefaultPathInterface::class);
}

public function testGetUrl()
Expand Down Expand Up @@ -60,29 +54,46 @@ public function testIsCurrentIfIsset()
$this->assertTrue($link->isCurrent());
}

/**
* Test if the current url is the same as link path
*
* @return void
*/
public function testIsCurrent()
{
$path = 'test/path';
$url = 'http://example.com/a/b';

$this->_requestMock->expects($this->once())->method('getModuleName')->will($this->returnValue('a'));
$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
$this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('d'));
$this->_defaultPathMock->expects($this->atLeastOnce())->method('getPart')->will($this->returnValue('d'));
$path = 'test/index';
$url = 'http://example.com/test/index';

$this->_requestMock->expects($this->once())
->method('getPathInfo')
->will($this->returnValue('/test/index/'));
$this->_requestMock->expects($this->once())
->method('getModuleName')
->will($this->returnValue('test'));
$this->_requestMock->expects($this->once())
->method('getControllerName')
->will($this->returnValue('index'));
$this->_requestMock->expects($this->once())
->method('getActionName')
->will($this->returnValue('index'));
$this->_urlBuilderMock->expects($this->at(0))
->method('getUrl')
->with($path)
->will($this->returnValue($url));
$this->_urlBuilderMock->expects($this->at(1))
->method('getUrl')
->with('test/index')
->will($this->returnValue($url));

$this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->with($path)->will($this->returnValue($url));
$this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->with('a/b')->will($this->returnValue($url));

$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
$link = $this->_objectManager->getObject(
\Magento\Framework\View\Element\Html\Link\Current::class,
[
'urlBuilder' => $this->_urlBuilderMock,
'request' => $this->_requestMock,
'defaultPath' => $this->_defaultPathMock
'request' => $this->_requestMock
]
);

$link->setPath($path);
$this->assertTrue($link->isCurrent());
}
Expand Down