Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#26499 Always transliterate product url key #26506

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@

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

<!-- Assert product design settings "Layout empty" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Model;

use Magento\Store\Model\Store;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class ProductUrlPathGenerator
* Model product url path generator
*/
class ProductUrlPathGenerator
{
Expand Down Expand Up @@ -150,7 +149,7 @@ protected function prepareProductUrlKey(Product $product)
$urlKey = (string)$product->getUrlKey();
$urlKey = trim(strtolower($urlKey));

return $urlKey ?: $product->formatUrlKey($product->getName());
return $product->formatUrlKey($urlKey ?: $product->getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogUrlRewrite\Setup\Patch\Data;

use Magento\Catalog\Model\Product\Url;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;

/**
* Update url_key all products.
*/
class UpdateUrlKeyForProducts implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var EavSetup
*/
private $eavSetup;

/**
* @var Url
*/
private $urlProduct;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
* @param Url $urlProduct
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
Url $urlProduct
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetup = $eavSetupFactory->create(['setup' => $moduleDataSetup]);
$this->urlProduct = $urlProduct;
}

/**
* @inheritdoc
*/
public function apply()
{
$productTypeId = $this->eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$table = $this->moduleDataSetup->getTable('catalog_product_entity_varchar');
$select = $this->moduleDataSetup->getConnection()->select()->from(
$table,
['value_id', 'value']
)->where(
'attribute_id = ?',
$this->eavSetup->getAttributeId($productTypeId, 'url_key')
);

$result = $this->moduleDataSetup->getConnection()->fetchAll($select);
foreach ($result as $key => $item) {
$result[$key]['value'] = $this->urlProduct->formatUrlKey($item['value']);
}

foreach (array_chunk($result, 500, true) as $pathResult) {
$this->moduleDataSetup->getConnection()->insertOnDuplicate($table, $pathResult, ['value']);
}

return $this;
}

/**
* @inheritDoc
*/
public static function getVersion()
{
return "2.4.0";
}

/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}

/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,50 @@

namespace Magento\CatalogUrlRewrite\Test\Unit\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Class ProductUrlPathGeneratorTest
* Verify ProductUrlPathGenerator class
*/
class ProductUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
class ProductUrlPathGeneratorTest extends TestCase
{
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator */
/** @var ProductUrlPathGenerator */
protected $productUrlPathGenerator;

/** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var StoreManagerInterface|MockObject */
protected $storeManager;

/** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var ScopeConfigInterface|MockObject */
protected $scopeConfig;

/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var CategoryUrlPathGenerator|MockObject */
protected $categoryUrlPathGenerator;

/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
/** @var Product|MockObject */
protected $product;

/** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
/** @var ProductRepositoryInterface|MockObject */
protected $productRepository;

/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
/** @var Category|MockObject */
protected $category;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->category = $this->createMock(\Magento\Catalog\Model\Category::class);
$this->category = $this->createMock(Category::class);
$productMethods = [
'__wakeup',
'getData',
Expand All @@ -54,17 +62,17 @@ protected function setUp(): void
'setStoreId',
];

$this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, $productMethods);
$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$this->product = $this->createPartialMock(Product::class, $productMethods);
$this->storeManager = $this->createMock(StoreManagerInterface::class);
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
$this->categoryUrlPathGenerator = $this->createMock(
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
CategoryUrlPathGenerator::class
);
$this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
$this->productRepository->expects($this->any())->method('getById')->willReturn($this->product);

$this->productUrlPathGenerator = (new ObjectManager($this))->getObject(
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class,
ProductUrlPathGenerator::class,
[
'storeManager' => $this->storeManager,
'scopeConfig' => $this->scopeConfig,
Expand All @@ -75,20 +83,24 @@ protected function setUp(): void
}

/**
* Data provider for testGetUrlPath.
*
* @return array
*/
public function getUrlPathDataProvider(): array
{
return [
'path based on url key uppercase' => ['Url-Key', null, 0, 'url-key'],
'path based on url key' => ['url-key', null, 0, 'url-key'],
'path based on url key uppercase' => ['Url-Key', null, 1, 'url-key'],
'path based on url key' => ['url-key', null, 1, 'url-key'],
'path based on product name 1' => ['', 'product-name', 1, 'product-name'],
'path based on product name 2' => [null, 'product-name', 1, 'product-name'],
'path based on product name 3' => [false, 'product-name', 1, 'product-name']
];
}

/**
* Verify get url path.
*
* @dataProvider getUrlPathDataProvider
* @param string|null|bool $urlKey
* @param string|null|bool $productName
Expand All @@ -109,6 +121,8 @@ public function testGetUrlPath($urlKey, $productName, $formatterCalled, $result)
}

/**
* Verify get url key.
*
* @param string|bool $productUrlKey
* @param string|bool $expectedUrlKey
* @return void
Expand All @@ -122,6 +136,8 @@ public function testGetUrlKey($productUrlKey, $expectedUrlKey): void
}

/**
* Data provider for testGetUrlKey.
*
* @return array
*/
public function getUrlKeyDataProvider(): array
Expand All @@ -133,6 +149,8 @@ public function getUrlKeyDataProvider(): array
}

/**
* Verify get url path with default utl key.
*
* @param string|null|bool $storedUrlKey
* @param string|null|bool $productName
* @param string $expectedUrlKey
Expand All @@ -150,6 +168,8 @@ public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expect
}

/**
* Data provider for testGetUrlPathDefaultUrlKey.
*
* @return array
*/
public function getUrlPathDefaultUrlKeyDataProvider(): array
Expand All @@ -161,6 +181,8 @@ public function getUrlPathDefaultUrlKeyDataProvider(): array
}

/**
* Verify get url path with category.
*
* @return void
*/
public function testGetUrlPathWithCategory(): void
Expand All @@ -177,6 +199,8 @@ public function testGetUrlPathWithCategory(): void
}

/**
* Verify get url path with suffix.
*
* @return void
*/
public function testGetUrlPathWithSuffix(): void
Expand All @@ -198,6 +222,8 @@ public function testGetUrlPathWithSuffix(): void
}

/**
* Verify get url path with suffix and category and store.
*
* @return void
*/
public function testGetUrlPathWithSuffixAndCategoryAndStore(): void
Expand All @@ -219,6 +245,8 @@ public function testGetUrlPathWithSuffixAndCategoryAndStore(): void
}

/**
* Verify get canonical url path.
*
* @return void
*/
public function testGetCanonicalUrlPath(): void
Expand All @@ -232,6 +260,8 @@ public function testGetCanonicalUrlPath(): void
}

/**
* Verify get canonical path with category.
*
* @return void
*/
public function testGetCanonicalUrlPathWithCategory(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

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

<!-- Assert product design settings "Layout empty" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

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

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