Skip to content

Commit aac7c4c

Browse files
authored
ENGCOM-7514: Fix #27985 CMS page link active on current page #28004
2 parents 7cba59a + a39e94a commit aac7c4c

File tree

2 files changed

+137
-65
lines changed

2 files changed

+137
-65
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: 113 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,109 +14,159 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616

17+
/**
18+
* @covers \Magento\Framework\View\Element\Html\Link\Current
19+
*/
1720
class CurrentTest extends TestCase
1821
{
1922
/**
20-
* @var MockObject
23+
* @var UrlInterface|MockObject
2124
*/
22-
protected $_urlBuilderMock;
25+
private $_urlBuilderMock;
2326

2427
/**
25-
* @var MockObject
28+
* @var Http|MockObject
2629
*/
27-
protected $_requestMock;
30+
private $_requestMock;
2831

2932
/**
30-
* @var ObjectManager
33+
* @var Current
3134
*/
32-
protected $_objectManager;
35+
private $currentLink;
3336

37+
/**
38+
* @inheritDoc
39+
*/
3440
protected function setUp(): void
3541
{
36-
$this->_objectManager = new ObjectManager($this);
37-
$this->_urlBuilderMock = $this->getMockForAbstractClass(UrlInterface::class);
42+
$this->_urlBuilderMock = $this->createMock(UrlInterface::class);
3843
$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+
);
3952
}
4053

41-
public function testGetUrl()
54+
/**
55+
* Test get Url
56+
*/
57+
public function testGetUrl(): void
4258
{
43-
$path = 'test/path';
44-
$url = 'http://example.com/asdasd';
59+
$pathStub = 'test/path';
60+
$urlStub = 'http://example.com/asdasd';
4561

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

48-
/** @var Current $link */
49-
$link = $this->_objectManager->getObject(
50-
Current::class,
51-
['urlBuilder' => $this->_urlBuilderMock]
52-
);
67+
$this->currentLink->setPath($pathStub);
5368

54-
$link->setPath($path);
55-
$this->assertEquals($url, $link->getHref());
69+
$this->assertEquals($urlStub, $this->currentLink->getHref());
5670
}
5771

58-
public function testIsCurrentIfIsset()
72+
/**
73+
* Test if set current
74+
*/
75+
public function testIsCurrentIfIsset(): void
5976
{
60-
/** @var Current $link */
61-
$link = $this->_objectManager->getObject(Current::class);
62-
$link->setCurrent(true);
63-
$this->assertTrue($link->isCurrent());
77+
$this->currentLink->setCurrent(true);
78+
$this->assertTrue($this->currentLink->isCurrent());
6479
}
6580

6681
/**
6782
* Test if the current url is the same as link path
6883
*
69-
* @return void
84+
* @param string $pathStub
85+
* @param string $urlStub
86+
* @param array $request
87+
* @param bool $expected
88+
* @dataProvider isCurrentDataProvider
7089
*/
71-
public function testIsCurrent()
90+
public function testIsCurrent($pathStub, $urlStub, $request, $expected): void
7291
{
73-
$path = 'test/index';
74-
$url = 'http://example.com/test/index';
75-
76-
$this->_requestMock->expects($this->once())
92+
$this->_requestMock->expects($this->any())
7793
->method('getPathInfo')
78-
->willReturn('/test/index/');
79-
$this->_requestMock->expects($this->once())
94+
->will($this->returnValue($request['pathInfoStub']));
95+
$this->_requestMock->expects($this->any())
8096
->method('getModuleName')
81-
->willReturn('test');
82-
$this->_requestMock->expects($this->once())
97+
->will($this->returnValue($request['moduleStub']));
98+
$this->_requestMock->expects($this->any())
8399
->method('getControllerName')
84-
->willReturn('index');
85-
$this->_requestMock->expects($this->once())
100+
->will($this->returnValue($request['controllerStub']));
101+
$this->_requestMock->expects($this->any())
86102
->method('getActionName')
87-
->willReturn('index');
103+
->will($this->returnValue($request['actionStub']));
104+
88105
$this->_urlBuilderMock->expects($this->at(0))
89106
->method('getUrl')
90-
->with($path)
91-
->willReturn($url);
107+
->with($pathStub)
108+
->will($this->returnValue($urlStub));
92109
$this->_urlBuilderMock->expects($this->at(1))
93110
->method('getUrl')
94-
->with('test/index')
95-
->willReturn($url);
96-
97-
/** @var Current $link */
98-
$link = $this->_objectManager->getObject(
99-
Current::class,
100-
[
101-
'urlBuilder' => $this->_urlBuilderMock,
102-
'request' => $this->_requestMock
103-
]
104-
);
105-
106-
$link->setPath($path);
107-
$this->assertTrue($link->isCurrent());
111+
->with($request['mcaStub'])
112+
->will($this->returnValue($request['getUrl']));
113+
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+
}
120+
121+
$this->currentLink->setPath($pathStub);
122+
$this->assertEquals($expected, $this->currentLink->isCurrent());
108123
}
109124

110-
public function testIsCurrentFalse()
125+
/**
126+
* Data provider for is current
127+
*/
128+
public function isCurrentDataProvider(): array
111129
{
112-
$this->_urlBuilderMock->expects($this->at(0))->method('getUrl')->willReturn('1');
113-
$this->_urlBuilderMock->expects($this->at(1))->method('getUrl')->willReturn('2');
114-
115-
/** @var Current $link */
116-
$link = $this->_objectManager->getObject(
117-
Current::class,
118-
['urlBuilder' => $this->_urlBuilderMock, 'request' => $this->_requestMock]
119-
);
120-
$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+
];
121171
}
122172
}

0 commit comments

Comments
 (0)