Skip to content

Commit 435a64c

Browse files
authored
ENGCOM-3471: [Framework] New Link is not correctly shown as Current if contains default parts #19134
2 parents c608d77 + a1dd861 commit 435a64c

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

lib/internal/Magento/Framework/View/Element/Html/Link/Current.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Framework\View\Element\Html\Link;
77

8+
use Magento\Framework\App\DefaultPathInterface;
9+
use Magento\Framework\View\Element\Template;
10+
use Magento\Framework\View\Element\Template\Context;
11+
812
/**
913
* Block representing link with two possible states.
1014
* "Current" state means link leads to URL equivalent to URL of currently displayed page.
@@ -17,25 +21,25 @@
1721
* @method null|bool getCurrent()
1822
* @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
1923
*/
20-
class Current extends \Magento\Framework\View\Element\Template
24+
class Current extends Template
2125
{
2226
/**
2327
* Default path
2428
*
25-
* @var \Magento\Framework\App\DefaultPathInterface
29+
* @var DefaultPathInterface
2630
*/
2731
protected $_defaultPath;
2832

2933
/**
3034
* Constructor
3135
*
32-
* @param \Magento\Framework\View\Element\Template\Context $context
33-
* @param \Magento\Framework\App\DefaultPathInterface $defaultPath
36+
* @param Context $context
37+
* @param DefaultPathInterface $defaultPath
3438
* @param array $data
3539
*/
3640
public function __construct(
37-
\Magento\Framework\View\Element\Template\Context $context,
38-
\Magento\Framework\App\DefaultPathInterface $defaultPath,
41+
Context $context,
42+
DefaultPathInterface $defaultPath,
3943
array $data = []
4044
) {
4145
parent::__construct($context, $data);
@@ -56,18 +60,20 @@ public function getHref()
5660
* Get current mca
5761
*
5862
* @return string
63+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
5964
*/
6065
private function getMca()
6166
{
6267
$routeParts = [
63-
'module' => $this->_request->getModuleName(),
64-
'controller' => $this->_request->getControllerName(),
65-
'action' => $this->_request->getActionName(),
68+
(string)$this->_request->getModuleName(),
69+
(string)$this->_request->getControllerName(),
70+
(string)$this->_request->getActionName(),
6671
];
6772

6873
$parts = [];
74+
$pathParts = explode('/', $this->getPath());
6975
foreach ($routeParts as $key => $value) {
70-
if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
76+
if (isset($pathParts[$key]) && $pathParts[$key] === $value) {
7177
$parts[] = $value;
7278
}
7379
}

lib/internal/Magento/Framework/View/Test/Unit/Element/Html/Link/CurrentTest.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class CurrentTest extends \PHPUnit\Framework\TestCase
1717
*/
1818
protected $_requestMock;
1919

20-
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
22-
*/
23-
protected $_defaultPathMock;
24-
2520
/**
2621
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
2722
*/
@@ -32,7 +27,6 @@ protected function setUp()
3227
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3328
$this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
3429
$this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
35-
$this->_defaultPathMock = $this->createMock(\Magento\Framework\App\DefaultPathInterface::class);
3630
}
3731

3832
public function testGetUrl()
@@ -60,29 +54,43 @@ public function testIsCurrentIfIsset()
6054
$this->assertTrue($link->isCurrent());
6155
}
6256

57+
/**
58+
* Test if the current url is the same as link path
59+
*
60+
* @return void
61+
*/
6362
public function testIsCurrent()
6463
{
65-
$path = 'test/path';
66-
$url = 'http://example.com/a/b';
67-
68-
$this->_requestMock->expects($this->once())->method('getModuleName')->will($this->returnValue('a'));
69-
$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
70-
$this->_requestMock->expects($this->once())->method('getActionName')->will($this->returnValue('d'));
71-
$this->_defaultPathMock->expects($this->atLeastOnce())->method('getPart')->will($this->returnValue('d'));
64+
$path = 'test/index';
65+
$url = 'http://example.com/test/index';
66+
67+
$this->_requestMock->expects($this->once())
68+
->method('getModuleName')
69+
->will($this->returnValue('test'));
70+
$this->_requestMock->expects($this->once())
71+
->method('getControllerName')
72+
->will($this->returnValue('index'));
73+
$this->_requestMock->expects($this->once())
74+
->method('getActionName')
75+
->will($this->returnValue('index'));
76+
$this->_urlBuilderMock->expects($this->at(0))
77+
->method('getUrl')
78+
->with($path)
79+
->will($this->returnValue($url));
80+
$this->_urlBuilderMock->expects($this->at(1))
81+
->method('getUrl')
82+
->with('test/index')
83+
->will($this->returnValue($url));
7284

73-
$this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->with($path)->will($this->returnValue($url));
74-
$this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->with('a/b')->will($this->returnValue($url));
75-
76-
$this->_requestMock->expects($this->once())->method('getControllerName')->will($this->returnValue('b'));
7785
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
7886
$link = $this->_objectManager->getObject(
7987
\Magento\Framework\View\Element\Html\Link\Current::class,
8088
[
8189
'urlBuilder' => $this->_urlBuilderMock,
82-
'request' => $this->_requestMock,
83-
'defaultPath' => $this->_defaultPathMock
90+
'request' => $this->_requestMock
8491
]
8592
);
93+
8694
$link->setPath($path);
8795
$this->assertTrue($link->isCurrent());
8896
}

0 commit comments

Comments
 (0)