Skip to content

Commit 606c381

Browse files
authored
ENGCOM-7064: #26499 Always transliterate product url key #26506
2 parents c989e5d + f9cf684 commit 606c381

File tree

6 files changed

+156
-25
lines changed

6 files changed

+156
-25
lines changed

app/code/Magento/Bundle/Test/Mftf/Test/AdminCreateAndEditBundleProductSettingsTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126

127127
<!-- Verify Url Key after changing -->
128128
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="openProductPage">
129-
<argument name="productUrl" value="{{ApiBundleProduct.name}}"/>
129+
<argument name="productUrl" value="{{ApiBundleProduct.urlKey}}"/>
130130
</actionGroup>
131131

132132
<!-- Assert product design settings "Layout empty" -->

app/code/Magento/CatalogUrlRewrite/Model/ProductUrlPathGenerator.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\CatalogUrlRewrite\Model;
89

9-
use Magento\Store\Model\Store;
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Category;
1212
use Magento\Catalog\Model\Product;
13-
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1413
use Magento\Framework\App\Config\ScopeConfigInterface;
1514
use Magento\Store\Model\ScopeInterface;
1615
use Magento\Store\Model\StoreManagerInterface;
1716

1817
/**
19-
* Class ProductUrlPathGenerator
18+
* Model product url path generator
2019
*/
2120
class ProductUrlPathGenerator
2221
{
@@ -150,7 +149,7 @@ protected function prepareProductUrlKey(Product $product)
150149
$urlKey = (string)$product->getUrlKey();
151150
$urlKey = trim(strtolower($urlKey));
152151

153-
return $urlKey ?: $product->formatUrlKey($product->getName());
152+
return $product->formatUrlKey($urlKey ?: $product->getName());
154153
}
155154

156155
/**
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Setup\Patch\Data;
9+
10+
use Magento\Catalog\Model\Product\Url;
11+
use Magento\Eav\Setup\EavSetup;
12+
use Magento\Eav\Setup\EavSetupFactory;
13+
use Magento\Framework\Setup\ModuleDataSetupInterface;
14+
use Magento\Framework\Setup\Patch\DataPatchInterface;
15+
use Magento\Framework\Setup\Patch\PatchVersionInterface;
16+
17+
/**
18+
* Update url_key all products.
19+
*/
20+
class UpdateUrlKeyForProducts implements DataPatchInterface, PatchVersionInterface
21+
{
22+
/**
23+
* @var ModuleDataSetupInterface
24+
*/
25+
private $moduleDataSetup;
26+
27+
/**
28+
* @var EavSetup
29+
*/
30+
private $eavSetup;
31+
32+
/**
33+
* @var Url
34+
*/
35+
private $urlProduct;
36+
37+
/**
38+
* @param ModuleDataSetupInterface $moduleDataSetup
39+
* @param EavSetupFactory $eavSetupFactory
40+
* @param Url $urlProduct
41+
*/
42+
public function __construct(
43+
ModuleDataSetupInterface $moduleDataSetup,
44+
EavSetupFactory $eavSetupFactory,
45+
Url $urlProduct
46+
) {
47+
$this->moduleDataSetup = $moduleDataSetup;
48+
$this->eavSetup = $eavSetupFactory->create(['setup' => $moduleDataSetup]);
49+
$this->urlProduct = $urlProduct;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function apply()
56+
{
57+
$productTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
58+
$table = $this->moduleDataSetup->getTable('catalog_product_entity_varchar');
59+
$select = $this->moduleDataSetup->getConnection()->select()->from(
60+
$table,
61+
['value_id', 'value']
62+
)->where(
63+
'attribute_id = ?',
64+
$this->eavSetup->getAttributeId($productTypeId, 'url_key')
65+
);
66+
67+
$result = $this->moduleDataSetup->getConnection()->fetchAll($select);
68+
foreach ($result as $key => $item) {
69+
$result[$key]['value'] = $this->urlProduct->formatUrlKey($item['value']);
70+
}
71+
72+
foreach (array_chunk($result, 500, true) as $pathResult) {
73+
$this->moduleDataSetup->getConnection()->insertOnDuplicate($table, $pathResult, ['value']);
74+
}
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public static function getVersion()
83+
{
84+
return "2.4.0";
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
public static function getDependencies()
91+
{
92+
return [];
93+
}
94+
95+
/**
96+
* @inheritdoc
97+
*/
98+
public function getAliases()
99+
{
100+
return [];
101+
}
102+
}

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductUrlPathGeneratorTest.php

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,50 @@
77

88
namespace Magento\CatalogUrlRewrite\Test\Unit\Model;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Category;
12+
use Magento\Catalog\Model\Product;
13+
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1014
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
1116
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1217
use Magento\Store\Model\ScopeInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
19+
use PHPUnit\Framework\TestCase;
20+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1321

1422
/**
15-
* Class ProductUrlPathGeneratorTest
23+
* Verify ProductUrlPathGenerator class
1624
*/
17-
class ProductUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
25+
class ProductUrlPathGeneratorTest extends TestCase
1826
{
19-
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator */
27+
/** @var ProductUrlPathGenerator */
2028
protected $productUrlPathGenerator;
2129

22-
/** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
30+
/** @var StoreManagerInterface|MockObject */
2331
protected $storeManager;
2432

25-
/** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
33+
/** @var ScopeConfigInterface|MockObject */
2634
protected $scopeConfig;
2735

28-
/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
36+
/** @var CategoryUrlPathGenerator|MockObject */
2937
protected $categoryUrlPathGenerator;
3038

31-
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
39+
/** @var Product|MockObject */
3240
protected $product;
3341

34-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
42+
/** @var ProductRepositoryInterface|MockObject */
3543
protected $productRepository;
3644

37-
/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
45+
/** @var Category|MockObject */
3846
protected $category;
3947

4048
/**
4149
* @inheritdoc
4250
*/
4351
protected function setUp(): void
4452
{
45-
$this->category = $this->createMock(\Magento\Catalog\Model\Category::class);
53+
$this->category = $this->createMock(Category::class);
4654
$productMethods = [
4755
'__wakeup',
4856
'getData',
@@ -54,17 +62,17 @@ protected function setUp(): void
5462
'setStoreId',
5563
];
5664

57-
$this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, $productMethods);
58-
$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
59-
$this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
65+
$this->product = $this->createPartialMock(Product::class, $productMethods);
66+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
67+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
6068
$this->categoryUrlPathGenerator = $this->createMock(
61-
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
69+
CategoryUrlPathGenerator::class
6270
);
63-
$this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
71+
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
6472
$this->productRepository->expects($this->any())->method('getById')->willReturn($this->product);
6573

6674
$this->productUrlPathGenerator = (new ObjectManager($this))->getObject(
67-
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class,
75+
ProductUrlPathGenerator::class,
6876
[
6977
'storeManager' => $this->storeManager,
7078
'scopeConfig' => $this->scopeConfig,
@@ -75,20 +83,24 @@ protected function setUp(): void
7583
}
7684

7785
/**
86+
* Data provider for testGetUrlPath.
87+
*
7888
* @return array
7989
*/
8090
public function getUrlPathDataProvider(): array
8191
{
8292
return [
83-
'path based on url key uppercase' => ['Url-Key', null, 0, 'url-key'],
84-
'path based on url key' => ['url-key', null, 0, 'url-key'],
93+
'path based on url key uppercase' => ['Url-Key', null, 1, 'url-key'],
94+
'path based on url key' => ['url-key', null, 1, 'url-key'],
8595
'path based on product name 1' => ['', 'product-name', 1, 'product-name'],
8696
'path based on product name 2' => [null, 'product-name', 1, 'product-name'],
8797
'path based on product name 3' => [false, 'product-name', 1, 'product-name']
8898
];
8999
}
90100

91101
/**
102+
* Verify get url path.
103+
*
92104
* @dataProvider getUrlPathDataProvider
93105
* @param string|null|bool $urlKey
94106
* @param string|null|bool $productName
@@ -109,6 +121,8 @@ public function testGetUrlPath($urlKey, $productName, $formatterCalled, $result)
109121
}
110122

111123
/**
124+
* Verify get url key.
125+
*
112126
* @param string|bool $productUrlKey
113127
* @param string|bool $expectedUrlKey
114128
* @return void
@@ -122,6 +136,8 @@ public function testGetUrlKey($productUrlKey, $expectedUrlKey): void
122136
}
123137

124138
/**
139+
* Data provider for testGetUrlKey.
140+
*
125141
* @return array
126142
*/
127143
public function getUrlKeyDataProvider(): array
@@ -133,6 +149,8 @@ public function getUrlKeyDataProvider(): array
133149
}
134150

135151
/**
152+
* Verify get url path with default utl key.
153+
*
136154
* @param string|null|bool $storedUrlKey
137155
* @param string|null|bool $productName
138156
* @param string $expectedUrlKey
@@ -150,6 +168,8 @@ public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expect
150168
}
151169

152170
/**
171+
* Data provider for testGetUrlPathDefaultUrlKey.
172+
*
153173
* @return array
154174
*/
155175
public function getUrlPathDefaultUrlKeyDataProvider(): array
@@ -161,6 +181,8 @@ public function getUrlPathDefaultUrlKeyDataProvider(): array
161181
}
162182

163183
/**
184+
* Verify get url path with category.
185+
*
164186
* @return void
165187
*/
166188
public function testGetUrlPathWithCategory(): void
@@ -177,6 +199,8 @@ public function testGetUrlPathWithCategory(): void
177199
}
178200

179201
/**
202+
* Verify get url path with suffix.
203+
*
180204
* @return void
181205
*/
182206
public function testGetUrlPathWithSuffix(): void
@@ -198,6 +222,8 @@ public function testGetUrlPathWithSuffix(): void
198222
}
199223

200224
/**
225+
* Verify get url path with suffix and category and store.
226+
*
201227
* @return void
202228
*/
203229
public function testGetUrlPathWithSuffixAndCategoryAndStore(): void
@@ -219,6 +245,8 @@ public function testGetUrlPathWithSuffixAndCategoryAndStore(): void
219245
}
220246

221247
/**
248+
* Verify get canonical url path.
249+
*
222250
* @return void
223251
*/
224252
public function testGetCanonicalUrlPath(): void
@@ -232,6 +260,8 @@ public function testGetCanonicalUrlPath(): void
232260
}
233261

234262
/**
263+
* Verify get canonical path with category.
264+
*
235265
* @return void
236266
*/
237267
public function testGetCanonicalUrlPathWithCategory(): void

app/code/Magento/ConfigurableProduct/Test/Mftf/Test/AdminCreateAndEditConfigurableProductSettingsTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
<!-- Verify Url Key after changing -->
9393
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="openProductPage">
94-
<argument name="productUrl" value="{{ApiConfigurableProduct.name}}"/>
94+
<argument name="productUrl" value="{{ApiConfigurableProduct.urlKey}}"/>
9595
</actionGroup>
9696

9797
<!-- Assert product design settings "Layout empty" -->

app/code/Magento/Downloadable/Test/Mftf/Test/AdminCreateAndEditDownloadableProductSettingsTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797

9898
<!-- Verify Url Key after changing -->
9999
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="openProductPage">
100-
<argument name="productUrl" value="{{ApiDownloadableProduct.name}}"/>
100+
<argument name="productUrl" value="{{ApiDownloadableProduct.urlKey}}"/>
101101
</actionGroup>
102102

103103
<!-- Assert product design settings "left bar is present at product page with 2 columns" -->

0 commit comments

Comments
 (0)