Skip to content

Commit 769e46d

Browse files
committed
Improving the current link logic, by covering all the possible cases for default parts
1 parent 508832b commit 769e46d

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ public function getHref()
6464
private function getMca()
6565
{
6666
$routeParts = [
67-
'module' => $this->_request->getModuleName(),
68-
'controller' => $this->_request->getControllerName(),
69-
'action' => $this->_request->getActionName(),
67+
$this->_request->getModuleName(),
68+
$this->_request->getControllerName(),
69+
$this->_request->getActionName(),
7070
];
7171

7272
$parts = [];
73+
$pathParts = explode('/', $this->getPath());
7374
foreach ($routeParts as $key => $value) {
74-
if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
75+
if (isset($pathParts[$key]) && $pathParts[$key] === $value) {
7576
$parts[] = $value;
7677
}
7778
}
@@ -85,7 +86,7 @@ private function getMca()
8586
*/
8687
public function isCurrent()
8788
{
88-
return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getPathInfo());
89+
return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getMca());
8990
}
9091

9192
/**
@@ -151,14 +152,4 @@ private function getAttributesHtml()
151152

152153
return $attributesHtml;
153154
}
154-
155-
/**
156-
* Get current page path info
157-
*
158-
* @return string
159-
*/
160-
private function getPathInfo()
161-
{
162-
return trim($this->_request->getPathInfo(), '/');
163-
}
164155
}

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

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,31 @@ public function testIsCurrentIfIsset()
5757
/**
5858
* Test if the current url is the same as link path
5959
*
60-
* @dataProvider linkPathProvider
61-
* @param string $linkPath
62-
* @param string $currentPathInfo
63-
* @param bool $expected
6460
* @return void
6561
*/
66-
public function testIsCurrent($linkPath, $currentPathInfo, $expected)
62+
public function testIsCurrent()
6763
{
68-
$baseUrl = 'http://example.com/';
69-
$trimmed = trim($currentPathInfo, '/');
64+
$path = 'test/index';
65+
$url = 'http://example.com/test/index';
7066

71-
$this->_requestMock->expects($this->any())->method('getPathInfo')->willReturn($currentPathInfo);
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'));
7276
$this->_urlBuilderMock->expects($this->at(0))
7377
->method('getUrl')
74-
->with($linkPath)
75-
->will($this->returnValue($baseUrl . $linkPath));
78+
->with($path)
79+
->will($this->returnValue($url));
7680
$this->_urlBuilderMock->expects($this->at(1))
7781
->method('getUrl')
78-
->with($trimmed)
79-
->will($this->returnValue($baseUrl . $trimmed));
82+
->with('test/index')
83+
->will($this->returnValue($url));
84+
8085
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
8186
$link = $this->_objectManager->getObject(
8287
\Magento\Framework\View\Element\Html\Link\Current::class,
@@ -86,22 +91,8 @@ public function testIsCurrent($linkPath, $currentPathInfo, $expected)
8691
]
8792
);
8893

89-
$link->setCurrent(false);
90-
$link->setPath($linkPath);
91-
$this->assertEquals($expected, $link->isCurrent());
92-
}
93-
94-
/**
95-
* @return array
96-
*/
97-
public function linkPathProvider()
98-
{
99-
return [
100-
['test/index', '/test/index/', true],
101-
['test/index/index', '/test/index/index/', true],
102-
['test/route', '/test/index/', false],
103-
['test/index', '/test/', false]
104-
];
94+
$link->setPath($path);
95+
$this->assertTrue($link->isCurrent());
10596
}
10697

10798
public function testIsCurrentFalse()

0 commit comments

Comments
 (0)