Skip to content

Commit d5cc8cb

Browse files
committed
Fix#27985 CMS page link active on current page
1 parent f442fa9 commit d5cc8cb

File tree

2 files changed

+147
-66
lines changed

2 files changed

+147
-66
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,31 @@ private function getMca()
9292
*/
9393
public function isCurrent()
9494
{
95+
$urlByPath = preg_replace(self::REGEX_INDEX_URL_PATTERN, '', $this->getUrl($this->getPath()));
9596
return $this->getCurrent() ||
96-
preg_replace(self::REGEX_INDEX_URL_PATTERN, '', $this->getUrl($this->getPath()))
97-
== preg_replace(self::REGEX_INDEX_URL_PATTERN, '', $this->getUrl($this->getMca()));
97+
($urlByPath == preg_replace(self::REGEX_INDEX_URL_PATTERN, '', $this->getUrl($this->getMca()))) ||
98+
$this->isCurrentCmsUrl($urlByPath);
99+
}
100+
101+
/**
102+
* Get Current displayed page url
103+
*
104+
* @return string
105+
*/
106+
private function getCurrentUrl()
107+
{
108+
return $this->getUrl('*/*/*', ['_current' => false, '_use_rewrite' => true]);
109+
}
110+
111+
/**
112+
* Check if link URL equivalent to URL of currently displayed CMS page
113+
*
114+
* @param string $urlByPath
115+
* @return bool
116+
*/
117+
private function isCurrentCmsUrl($urlByPath)
118+
{
119+
return ($urlByPath == preg_replace(self::REGEX_INDEX_URL_PATTERN, '', $this->getCurrentUrl()));
98120
}
99121

100122
/**

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

Lines changed: 123 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,111 +3,170 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Framework\View\Test\Unit\Element\Html\Link;
79

8-
class CurrentTest extends \PHPUnit\Framework\TestCase
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Framework\View\Element\Html\Link\Current;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @covers \Magento\Framework\View\Element\Html\Link\Current
19+
*/
20+
class CurrentTest extends TestCase
921
{
1022
/**
11-
* @var \PHPUnit_Framework_MockObject_MockObject
23+
* @var UrlInterface|MockObject
1224
*/
13-
protected $_urlBuilderMock;
25+
private $_urlBuilderMock;
1426

1527
/**
16-
* @var \PHPUnit_Framework_MockObject_MockObject
28+
* @var Http|MockObject
1729
*/
18-
protected $_requestMock;
30+
private $_requestMock;
1931

2032
/**
21-
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
33+
* @var Current
2234
*/
23-
protected $_objectManager;
35+
private $currentLink;
2436

25-
protected function setUp()
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function setUp(): void
2641
{
27-
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
28-
$this->_urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
29-
$this->_requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
42+
$this->_urlBuilderMock = $this->createMock(UrlInterface::class);
43+
$this->_requestMock = $this->createMock(Http::class);
44+
45+
$this->currentLink = (new ObjectManager($this))->getObject(
46+
Current::class,
47+
[
48+
'urlBuilder' => $this->_urlBuilderMock,
49+
'request' => $this->_requestMock
50+
]
51+
);
3052
}
3153

32-
public function testGetUrl()
54+
/**
55+
* Test get Url
56+
*/
57+
public function testGetUrl(): void
3358
{
34-
$path = 'test/path';
35-
$url = 'http://example.com/asdasd';
59+
$pathStub = 'test/path';
60+
$urlStub = 'http://example.com/asdasd';
3661

37-
$this->_urlBuilderMock->expects($this->once())->method('getUrl')->with($path)->will($this->returnValue($url));
62+
$this->_urlBuilderMock->expects($this->once())
63+
->method('getUrl')
64+
->with($pathStub)
65+
->will($this->returnValue($urlStub));
3866

39-
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
40-
$link = $this->_objectManager->getObject(
41-
\Magento\Framework\View\Element\Html\Link\Current::class,
42-
['urlBuilder' => $this->_urlBuilderMock]
43-
);
67+
$this->currentLink->setPath($pathStub);
4468

45-
$link->setPath($path);
46-
$this->assertEquals($url, $link->getHref());
69+
$this->assertEquals($urlStub, $this->currentLink->getHref());
4770
}
4871

49-
public function testIsCurrentIfIsset()
72+
/**
73+
* Test if set current
74+
*/
75+
public function testIsCurrentIfIsset(): void
5076
{
51-
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
52-
$link = $this->_objectManager->getObject(\Magento\Framework\View\Element\Html\Link\Current::class);
53-
$link->setCurrent(true);
54-
$this->assertTrue($link->isCurrent());
77+
$this->currentLink->setCurrent(true);
78+
$this->assertTrue($this->currentLink->isCurrent());
5579
}
5680

5781
/**
5882
* Test if the current url is the same as link path
5983
*
60-
* @return void
84+
* @param string $pathStub
85+
* @param string $urlStub
86+
* @param array $request
87+
* @param bool $expected
88+
* @dataProvider isCurrentDataProvider
6189
*/
62-
public function testIsCurrent()
90+
public function testIsCurrent($pathStub, $urlStub, $request, $expected): void
6391
{
64-
$path = 'test/index';
65-
$url = 'http://example.com/test/index';
66-
67-
$this->_requestMock->expects($this->once())
92+
$this->_requestMock->expects($this->any())
6893
->method('getPathInfo')
69-
->will($this->returnValue('/test/index/'));
70-
$this->_requestMock->expects($this->once())
94+
->will($this->returnValue($request['pathInfoStub']));
95+
$this->_requestMock->expects($this->any())
7196
->method('getModuleName')
72-
->will($this->returnValue('test'));
73-
$this->_requestMock->expects($this->once())
97+
->will($this->returnValue($request['moduleStub']));
98+
$this->_requestMock->expects($this->any())
7499
->method('getControllerName')
75-
->will($this->returnValue('index'));
76-
$this->_requestMock->expects($this->once())
100+
->will($this->returnValue($request['controllerStub']));
101+
$this->_requestMock->expects($this->any())
77102
->method('getActionName')
78-
->will($this->returnValue('index'));
103+
->will($this->returnValue($request['actionStub']));
104+
79105
$this->_urlBuilderMock->expects($this->at(0))
80106
->method('getUrl')
81-
->with($path)
82-
->will($this->returnValue($url));
107+
->with($pathStub)
108+
->will($this->returnValue($urlStub));
83109
$this->_urlBuilderMock->expects($this->at(1))
84110
->method('getUrl')
85-
->with('test/index')
86-
->will($this->returnValue($url));
111+
->with($request['mcaStub'])
112+
->will($this->returnValue($request['getUrl']));
87113

88-
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
89-
$link = $this->_objectManager->getObject(
90-
\Magento\Framework\View\Element\Html\Link\Current::class,
91-
[
92-
'urlBuilder' => $this->_urlBuilderMock,
93-
'request' => $this->_requestMock
94-
]
95-
);
114+
if ($request['mcaStub'] == '') {
115+
$this->_urlBuilderMock->expects($this->at(2))
116+
->method('getUrl')
117+
->with('*/*/*', ['_current' => false, '_use_rewrite' => true])
118+
->will($this->returnValue($urlStub));
119+
}
96120

97-
$link->setPath($path);
98-
$this->assertTrue($link->isCurrent());
121+
$this->currentLink->setPath($pathStub);
122+
$this->assertEquals($expected, $this->currentLink->isCurrent());
99123
}
100124

101-
public function testIsCurrentFalse()
125+
/**
126+
* Data provider for is current
127+
*/
128+
public function isCurrentDataProvider(): array
102129
{
103-
$this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->will($this->returnValue('1'));
104-
$this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->will($this->returnValue('2'));
105-
106-
/** @var \Magento\Framework\View\Element\Html\Link\Current $link */
107-
$link = $this->_objectManager->getObject(
108-
\Magento\Framework\View\Element\Html\Link\Current::class,
109-
['urlBuilder' => $this->_urlBuilderMock, 'request' => $this->_requestMock]
110-
);
111-
$this->assertFalse($link->isCurrent());
130+
return [
131+
'url with MCA' => [
132+
'pathStub' => 'test/path',
133+
'urlStub' => 'http://example.com/asdasd',
134+
'requestStub' => [
135+
'pathInfoStub' => '/test/index/',
136+
'moduleStub' => 'test',
137+
'controllerStub' => 'index',
138+
'actionStub' => 'index',
139+
'mcaStub' => 'test/index',
140+
'getUrl' => 'http://example.com/asdasd/'
141+
],
142+
'excepted' => true
143+
],
144+
'url with CMS' => [
145+
'pathStub' => 'test',
146+
'urlStub' => 'http://example.com/test',
147+
'requestStub' => [
148+
'pathInfoStub' => '//test//',
149+
'moduleStub' => 'cms',
150+
'controllerStub' => 'page',
151+
'actionStub' => 'view',
152+
'mcaStub' => '',
153+
'getUrl' => 'http://example.com/'
154+
],
155+
'excepted' => true
156+
],
157+
'Test if is current false' => [
158+
'pathStub' => 'test/path',
159+
'urlStub' => 'http://example.com/tests',
160+
'requestStub' => [
161+
'pathInfoStub' => '/test/index/',
162+
'moduleStub' => 'test',
163+
'controllerStub' => 'index',
164+
'actionStub' => 'index',
165+
'mcaStub' => 'test/index',
166+
'getUrl' => 'http://example.com/asdasd/'
167+
],
168+
'excepted' => false
169+
]
170+
];
112171
}
113172
}

0 commit comments

Comments
 (0)