Skip to content

Commit 433b517

Browse files
committed
MC-19713: Relative Category links created using PageBuilder have the store name as a URL parameter
- Fix category/product link to different store should have a store code in the URL
1 parent 7d19fe0 commit 433b517

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

app/code/Magento/Catalog/Block/Widget/Link.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,32 @@ public function getHref()
8989

9090
if ($rewrite) {
9191
$href = $store->getUrl('', ['_direct' => $rewrite->getRequestPath()]);
92+
93+
if ($this->addStoreCodeParam($store, $href)) {
94+
$href .= (strpos($href, '?') === false ? '?' : '&') . '___store=' . $store->getCode();
95+
}
9296
}
9397
$this->_href = $href;
9498
}
9599
return $this->_href;
96100
}
97101

102+
/**
103+
* Checks whether store code query param should be appended to the URL
104+
*
105+
* @param \Magento\Store\Model\Store $store
106+
* @param string $url
107+
* @return bool
108+
* @throws \Magento\Framework\Exception\NoSuchEntityException
109+
*/
110+
private function addStoreCodeParam(\Magento\Store\Model\Store $store, string $url): bool
111+
{
112+
return $this->getStoreId()
113+
&& !$store->isUseStoreInUrl()
114+
&& $store->getId() !== $this->_storeManager->getStore()->getId()
115+
&& strpos($url, '___store') === false;
116+
}
117+
98118
/**
99119
* Parse id_path
100120
*

app/code/Magento/Catalog/Test/Unit/Block/Widget/LinkTest.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,19 @@ public function testGetHrefIfRewriteIsNotFound()
141141
*
142142
* @dataProvider dataProviderForTestGetHrefWithoutUrlStoreSuffix
143143
* @param string $path
144+
* @param int|null $storeId
144145
* @param bool $includeStoreCode
145146
* @param string $expected
146147
* @throws \ReflectionException
147148
*/
148149
public function testStoreCodeShouldBeIncludedInURLOnlyIfItIsConfiguredSo(
149150
string $path,
151+
?int $storeId,
150152
bool $includeStoreCode,
151153
string $expected
152154
) {
153155
$this->block->setData('id_path', 'entity_type/entity_id');
156+
$this->block->setData('store_id', $storeId);
154157
$objectManager = new ObjectManager($this);
155158

156159
$rewrite = $this->createPartialMock(UrlRewrite::class, ['getRequestPath']);
@@ -192,25 +195,35 @@ public function testStoreCodeShouldBeIncludedInURLOnlyIfItIsConfiguredSo(
192195
$url->expects($this->any())
193196
->method('getUrl')
194197
->willReturnCallback(
195-
function ($route, $params) use ($store) {
196-
return rtrim($store->getBaseUrl(), '/') .'/'. ltrim($params['_direct'], '/');
198+
function ($route, $params) use ($storeId) {
199+
$baseUrl = rtrim($this->storeManager->getStore($storeId)->getBaseUrl(), '/');
200+
return $baseUrl .'/' . ltrim($params['_direct'], '/');
197201
}
198202
);
199203

200204
$store->addData(['store_id' => 1, 'code' => 'french']);
201205

206+
$store2 = clone $store;
207+
$store2->addData(['store_id' => 2, 'code' => 'german']);
208+
202209
$this->storeManager
203210
->expects($this->any())
204211
->method('getStore')
205-
->willReturn($store);
212+
->willReturnMap(
213+
[
214+
[null, $store],
215+
[1, $store],
216+
[2, $store2],
217+
]
218+
);
206219

207220
$this->urlFinder->expects($this->once())
208221
->method('findOneByData')
209222
->with(
210223
[
211224
UrlRewrite::ENTITY_ID => 'entity_id',
212225
UrlRewrite::ENTITY_TYPE => 'entity_type',
213-
UrlRewrite::STORE_ID => $store->getStoreId(),
226+
UrlRewrite::STORE_ID => $this->storeManager->getStore($storeId)->getStoreId(),
214227
]
215228
)
216229
->will($this->returnValue($rewrite));
@@ -219,7 +232,7 @@ function ($route, $params) use ($store) {
219232
->method('getRequestPath')
220233
->will($this->returnValue($path));
221234

222-
$this->assertContains($expected, $this->block->getHref());
235+
$this->assertEquals($expected, $this->block->getHref());
223236
}
224237

225238
/**
@@ -255,8 +268,13 @@ public function testGetLabelWithoutCustomText()
255268
public function dataProviderForTestGetHrefWithoutUrlStoreSuffix()
256269
{
257270
return [
258-
['/accessories.html', true, 'french/accessories.html'],
259-
['/accessories.html', false, '/accessories.html'],
271+
['/accessories.html', null, true, 'french/accessories.html'],
272+
['/accessories.html', null, false, '/accessories.html'],
273+
['/accessories.html', 1, true, 'french/accessories.html'],
274+
['/accessories.html', 1, false, '/accessories.html'],
275+
['/accessories.html', 2, true, 'german/accessories.html'],
276+
['/accessories.html', 2, false, '/accessories.html?___store=german'],
277+
['/accessories.html?___store=german', 2, false, '/accessories.html?___store=german'],
260278
];
261279
}
262280

0 commit comments

Comments
 (0)