init
This commit is contained in:
+132
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
|
||||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Base class for url rewrites tests logic
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
abstract class AbstractUrlRewriteTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
protected $objectManager;
|
||||
|
||||
/** @var StoreRepositoryInterface */
|
||||
protected $storeRepository;
|
||||
|
||||
/** @var ScopeConfigInterface */
|
||||
protected $config;
|
||||
|
||||
/** @var UrlRewriteCollectionFactory */
|
||||
protected $urlRewriteCollectionFactory;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
|
||||
$this->config = $this->objectManager->get(ScopeConfigInterface::class);
|
||||
$this->urlRewriteCollectionFactory = $this->objectManager->get(UrlRewriteCollectionFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all rewrite ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllRewriteIds(): array
|
||||
{
|
||||
$urlRewriteCollection = $this->urlRewriteCollectionFactory->create();
|
||||
|
||||
return $urlRewriteCollection->getAllIds();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that actual data contains of expected values
|
||||
*
|
||||
* @param UrlRewriteCollection $collection
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
protected function assertRewrites(UrlRewriteCollection $collection, array $expectedData): void
|
||||
{
|
||||
$collectionItems = $collection->toArray()['items'];
|
||||
$this->assertTrue(count($collectionItems) === count($expectedData));
|
||||
foreach ($expectedData as $expectedItem) {
|
||||
$found = false;
|
||||
foreach ($collectionItems as $item) {
|
||||
$found = array_intersect_assoc($item, $expectedItem) == $expectedItem;
|
||||
if ($found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found, 'The actual data does not contains of expected values');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get category url rewrites collection
|
||||
*
|
||||
* @param string|array $entityId
|
||||
* @return UrlRewriteCollection
|
||||
*/
|
||||
protected function getEntityRewriteCollection($entityId): UrlRewriteCollection
|
||||
{
|
||||
$condition = is_array($entityId) ? ['in' => $entityId] : $entityId;
|
||||
$entityRewriteCollection = $this->urlRewriteCollectionFactory->create();
|
||||
$entityRewriteCollection->addFieldToFilter(UrlRewrite::ENTITY_ID, $condition)
|
||||
->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => $this->getEntityType()]);
|
||||
|
||||
return $entityRewriteCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare expected data
|
||||
*
|
||||
* @param array $expectedData
|
||||
* @param int|null $id
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareData(array $expectedData, ?int $id = null): array
|
||||
{
|
||||
$newData = [];
|
||||
foreach ($expectedData as $key => $expectedItem) {
|
||||
$newData[$key] = str_replace(['%suffix%', '%id%'], [$this->getUrlSuffix(), $id], $expectedItem);
|
||||
}
|
||||
|
||||
return $newData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getEntityType(): string;
|
||||
|
||||
/**
|
||||
* Get config value for url suffix
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getUrlSuffix(): string;
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product as ProductEntity;
|
||||
use Magento\Catalog\Model\Product\Media\ConfigInterface;
|
||||
use Magento\Framework\App\Bootstrap as AppBootstrap;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Filesystem\Directory\Write;
|
||||
use Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\ImportExport\Model\Import;
|
||||
use Magento\CatalogImportExport\Model\Import\ProductFactory;
|
||||
use Magento\ImportExport\Model\Import\Source\Csv;
|
||||
use Magento\ImportExport\Model\Import\Source\CsvFactory;
|
||||
use Magento\ImportExport\Model\ResourceModel\Import\Data;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Checks that product import with same images can be successfully done
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class StorageTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var Filesystem */
|
||||
private $fileSystem;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/** @var File */
|
||||
private $fileDriver;
|
||||
|
||||
/** @var Import */
|
||||
private $import;
|
||||
|
||||
/** @var ConfigInterface */
|
||||
private $mediaConfig;
|
||||
|
||||
/** @var array */
|
||||
private $appParams;
|
||||
|
||||
/** @var array */
|
||||
private $createdProductsSkus = [];
|
||||
|
||||
/** @var array */
|
||||
private $filesToRemove = [];
|
||||
|
||||
/** @var CsvFactory */
|
||||
private $csvFactory;
|
||||
|
||||
/** @var Data */
|
||||
private $importDataResource;
|
||||
|
||||
/** @var Write */
|
||||
private $mediaDirectory;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->fileSystem = $this->objectManager->get(Filesystem::class);
|
||||
$this->fileDriver = $this->objectManager->get(File::class);
|
||||
$this->mediaConfig = $this->objectManager->get(ConfigInterface::class);
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->productRepository->cleanCache();
|
||||
$this->import = $this->objectManager->get(ProductFactory::class)->create();
|
||||
$this->csvFactory = $this->objectManager->get(CsvFactory::class);
|
||||
$this->importDataResource = $this->objectManager->get(Data::class);
|
||||
$this->appParams = Bootstrap::getInstance()->getBootstrap()->getApplication()
|
||||
->getInitParams()[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
|
||||
$this->mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->removeFiles();
|
||||
$this->removeProducts();
|
||||
$this->importDataResource->cleanBunches();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/issue34210_structure.php
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
public function testIssue34210(): void
|
||||
{
|
||||
$source = $this->prepareFile('issue34210.csv');
|
||||
$this->updateUploader();
|
||||
$errors = $this->import->setParameters([
|
||||
'behavior' => Import::BEHAVIOR_ADD_UPDATE,
|
||||
'entity' => ProductEntity::ENTITY,
|
||||
])
|
||||
->setSource($source)->validateData();
|
||||
$this->assertEmpty($errors->getAllErrors());
|
||||
|
||||
$result = $this->import->importData();
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove created files
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeFiles(): void
|
||||
{
|
||||
foreach ($this->filesToRemove as $file) {
|
||||
if ($this->fileDriver->isExists($file)) {
|
||||
$this->fileDriver->deleteFile($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove created products
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function removeProducts(): void
|
||||
{
|
||||
$resource = $this->objectManager->get(ResourceConnection::class);
|
||||
$connection = $resource->getConnection();
|
||||
$connection->delete('catalog_category_product');
|
||||
foreach ($this->createdProductsSkus as $sku) {
|
||||
try {
|
||||
$this->productRepository->deleteById($sku);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
//already removed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare file
|
||||
*
|
||||
* @param string $fileName
|
||||
* @return Csv
|
||||
*/
|
||||
private function prepareFile(string $fileName): Csv
|
||||
{
|
||||
$tmpDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
|
||||
$fixtureDir = realpath(__DIR__ . '/../../../_files');
|
||||
$filePath = $tmpDirectory->getAbsolutePath($fileName);
|
||||
$this->filesToRemove[] = $filePath;
|
||||
$tmpDirectory->getDriver()->copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath);
|
||||
$source = $this->csvFactory->create(
|
||||
[
|
||||
'file' => $fileName,
|
||||
'directory' => $tmpDirectory
|
||||
]
|
||||
);
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update upload to use sandbox folders
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function updateUploader(): void
|
||||
{
|
||||
$mediaDir = !$this->mediaDirectory->getDriver() instanceof File ?
|
||||
DirectoryList::MEDIA : $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH];
|
||||
$destDir = $mediaDir . DIRECTORY_SEPARATOR . $this->mediaConfig->getBaseMediaPath();
|
||||
$tmpDir = $mediaDir . DIRECTORY_SEPARATOR . 'import';
|
||||
$this->mediaDirectory->create($this->mediaConfig->getBaseMediaPath());
|
||||
$this->mediaDirectory->create('import');
|
||||
$this->import->setParameters([Import::FIELD_NAME_IMG_FILE_DIR => $tmpDir]);
|
||||
$uploader = $this->import->getUploader();
|
||||
$uploader->setDestDir($destDir);
|
||||
$uploader->setTmpDir($tmpDir);
|
||||
}
|
||||
}
|
||||
+381
@@ -0,0 +1,381 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\CategoryRepository;
|
||||
use Magento\Catalog\Model\ProductRepository;
|
||||
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Exception\CouldNotSaveException;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Model\OptionProvider;
|
||||
use Magento\UrlRewrite\Model\Storage\DbStorage;
|
||||
use Magento\UrlRewrite\Model\UrlFinderInterface;
|
||||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
|
||||
use PHPUnit\Framework\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CategoryUrlRewriteGeneratorTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGenerateUrlRewritesWithoutSaveHistory()
|
||||
{
|
||||
$resource = $this->objectManager->get(ResourceConnection::class);
|
||||
$connection = $resource->getConnection();
|
||||
$connection->delete(DbStorage::TABLE_NAME);
|
||||
/** @var Category $category */
|
||||
$category = $this->objectManager->create(Category::class);
|
||||
$category->load(3);
|
||||
$category->setData('save_rewrites_history', false);
|
||||
$category->setUrlKey('new-url');
|
||||
$category->save();
|
||||
|
||||
$categoryFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [3, 4, 5]
|
||||
];
|
||||
$actualResults = $this->getActualResults($categoryFilter);
|
||||
$categoryExpectedResult = [
|
||||
['new-url.html', 'catalog/category/view/id/3', 1, 0],
|
||||
['new-url/category-1-1.html', 'catalog/category/view/id/4', 1, 0],
|
||||
['new-url/category-1-1/category-1-1-1.html', 'catalog/category/view/id/5', 1, 0],
|
||||
];
|
||||
|
||||
$this->assertResults($categoryExpectedResult, $actualResults);
|
||||
|
||||
/** @var ProductRepository $productRepository */
|
||||
$productRepository = $this->objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('12345');
|
||||
$productForTest = $product->getId();
|
||||
|
||||
$productFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [$productForTest]
|
||||
];
|
||||
$actualResults = $this->getActualResults($productFilter);
|
||||
$productExpectedResult = [
|
||||
[
|
||||
'simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest,
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'new-url/category-1-1/category-1-1-1/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/5',
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'new-url/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/3',
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'new-url/category-1-1/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/4',
|
||||
1,
|
||||
0
|
||||
]
|
||||
];
|
||||
|
||||
$this->assertResults($productExpectedResult, $actualResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGenerateUrlRewritesWithSaveHistory()
|
||||
{
|
||||
/** @var Category $category */
|
||||
$category = $this->objectManager->create(Category::class);
|
||||
$category->load(3);
|
||||
$category->setData('save_rewrites_history', true);
|
||||
$category->setUrlKey('new-url');
|
||||
$category->save();
|
||||
|
||||
$categoryFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [3, 4, 5]
|
||||
];
|
||||
$actualResults = $this->getActualResults($categoryFilter);
|
||||
$categoryExpectedResult = [
|
||||
['new-url.html', 'catalog/category/view/id/3', 1, 0],
|
||||
['new-url/category-1-1.html', 'catalog/category/view/id/4', 1, 0],
|
||||
['new-url/category-1-1/category-1-1-1.html', 'catalog/category/view/id/5', 1, 0],
|
||||
['category-1.html', 'new-url.html', 0, OptionProvider::PERMANENT],
|
||||
['category-1/category-1-1.html', 'new-url/category-1-1.html', 0, OptionProvider::PERMANENT],
|
||||
[
|
||||
'category-1/category-1-1/category-1-1-1.html',
|
||||
'new-url/category-1-1/category-1-1-1.html',
|
||||
0,
|
||||
OptionProvider::PERMANENT
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertResults($categoryExpectedResult, $actualResults);
|
||||
|
||||
/** @var ProductRepository $productRepository */
|
||||
$productRepository = $this->objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('12345');
|
||||
$productForTest = $product->getId();
|
||||
|
||||
$productFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [$productForTest]
|
||||
];
|
||||
$actualResults = $this->getActualResults($productFilter);
|
||||
$productExpectedResult = [
|
||||
[
|
||||
'simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest,
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'new-url/category-1-1/category-1-1-1/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/5',
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'category-1/category-1-1/category-1-1-1/simple-product-two.html',
|
||||
'new-url/category-1-1/category-1-1-1/simple-product-two.html',
|
||||
0,
|
||||
OptionProvider::PERMANENT
|
||||
],
|
||||
[
|
||||
'new-url/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/3',
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'new-url/category-1-1/simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest . '/category/4',
|
||||
1,
|
||||
0
|
||||
],
|
||||
[
|
||||
'category-1/simple-product-two.html',
|
||||
'new-url/simple-product-two.html',
|
||||
0,
|
||||
OptionProvider::PERMANENT
|
||||
],
|
||||
[
|
||||
'category-1/category-1-1/simple-product-two.html',
|
||||
'new-url/category-1-1/simple-product-two.html',
|
||||
0,
|
||||
OptionProvider::PERMANENT
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertResults($productExpectedResult, $actualResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @param string $urlKey
|
||||
* @throws CouldNotSaveException
|
||||
* @throws NoSuchEntityException
|
||||
* @throws Exception
|
||||
* @dataProvider incorrectUrlRewritesDataProvider
|
||||
*/
|
||||
public function testGenerateUrlRewritesWithIncorrectUrlKey($urlKey)
|
||||
{
|
||||
$this->expectException(LocalizedException::class);
|
||||
$this->expectExceptionMessage('Invalid URL key');
|
||||
/** @var CategoryRepositoryInterface $repository */
|
||||
$repository = $this->objectManager->get(CategoryRepositoryInterface::class);
|
||||
$category = $repository->get(3);
|
||||
$category->setUrlKey($urlKey);
|
||||
$repository->save($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function incorrectUrlRewritesDataProvider()
|
||||
{
|
||||
return [
|
||||
['#'],
|
||||
['//']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $filter
|
||||
* @return array
|
||||
*/
|
||||
protected function getActualResults(array $filter)
|
||||
{
|
||||
/** @var UrlFinderInterface $urlFinder */
|
||||
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
|
||||
$actualResults = [];
|
||||
foreach ($urlFinder->findAllByData($filter) as $url) {
|
||||
$actualResults[] = [
|
||||
$url->getRequestPath(),
|
||||
$url->getTargetPath(),
|
||||
(int)$url->getIsAutogenerated(),
|
||||
$url->getRedirectType()
|
||||
];
|
||||
}
|
||||
return $actualResults;
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 0
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGenerateUrlRewritesWithoutGenerateCategoryRewrites()
|
||||
{
|
||||
/** @var Category $category */
|
||||
$category = $this->objectManager->create(Category::class);
|
||||
$category->load(3);
|
||||
$category->setData('save_rewrites_history', false);
|
||||
$category->setUrlKey('new-url');
|
||||
$category->save();
|
||||
|
||||
$categoryFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [3, 4, 5]
|
||||
];
|
||||
$actualResults = $this->getActualResults($categoryFilter);
|
||||
$categoryExpectedResult = [
|
||||
['new-url.html', 'catalog/category/view/id/3', 1, 0],
|
||||
['new-url/category-1-1.html', 'catalog/category/view/id/4', 1, 0],
|
||||
['new-url/category-1-1/category-1-1-1.html', 'catalog/category/view/id/5', 1, 0],
|
||||
];
|
||||
|
||||
$this->assertResults($categoryExpectedResult, $actualResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 0
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGenerateUrlRewritesWithoutGenerateProductRewrites()
|
||||
{
|
||||
/** @var ProductRepository $productRepository */
|
||||
$productRepository = $this->objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('12345');
|
||||
$productForTest = $product->getId();
|
||||
|
||||
$productFilter = [
|
||||
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
|
||||
UrlRewrite::ENTITY_ID => [$productForTest]
|
||||
];
|
||||
$actualResults = $this->getActualResults($productFilter);
|
||||
$productExpectedResult = [
|
||||
[
|
||||
'simple-product-two.html',
|
||||
'catalog/product/view/id/' . $productForTest,
|
||||
1,
|
||||
0
|
||||
]
|
||||
];
|
||||
|
||||
$this->assertResults($productExpectedResult, $actualResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check number of records after removing product
|
||||
*
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDbIsolation enabled
|
||||
* @magentoAppIsolation enabled
|
||||
*
|
||||
* @return void
|
||||
* @throws NoSuchEntityException
|
||||
* @throws \Magento\Framework\Exception\StateException
|
||||
*/
|
||||
public function testRemoveCatalogUrlRewrites()
|
||||
{
|
||||
/** @var CategoryRepository $categoryRepository */
|
||||
$categoryRepository = $this->objectManager->create(CategoryRepository::class);
|
||||
$category = $categoryRepository->get(5);
|
||||
$categoryId = $category->getId();
|
||||
|
||||
/** @var ProductRepository $productRepository */
|
||||
$productRepository = $this->objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('12345');
|
||||
$productId = $product->getId();
|
||||
|
||||
$countBeforeRemoving = $this->getCountOfRewrites($productId, $categoryId);
|
||||
$productRepository->delete($product);
|
||||
$countAfterRemoving = $this->getCountOfRewrites($productId, $categoryId);
|
||||
$this->assertEquals($countBeforeRemoving - 1, $countAfterRemoving);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of records in table
|
||||
*
|
||||
* @param $productId
|
||||
* @param $categoryId
|
||||
* @return string
|
||||
*/
|
||||
private function getCountOfRewrites($productId, $categoryId): string
|
||||
{
|
||||
/** @var Product $model */
|
||||
$model = $this->objectManager->get(Product::class);
|
||||
$connection = $model->getConnection();
|
||||
$select = $connection->select();
|
||||
$select->from($model->getTable(Product::TABLE_NAME), 'COUNT(*)');
|
||||
$select->where('category_id = ?', $categoryId);
|
||||
$select->where('product_id = ?', $productId);
|
||||
return $connection->fetchOne($select);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $expected
|
||||
* @param array $actual
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function assertResults($expected, $actual)
|
||||
{
|
||||
$this->assertEquals(count($expected), count($actual), 'Number of rewrites does not match');
|
||||
foreach ($expected as $row) {
|
||||
$this->assertContains(
|
||||
$row,
|
||||
$actual,
|
||||
'Expected: ' . var_export($row, true) . "\nIn Actual: " . var_export($actual, true)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+456
@@ -0,0 +1,456 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Catalog\Api\CategoryLinkManagementInterface;
|
||||
use Magento\Catalog\Api\CategoryRepositoryInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\CategoryFactory;
|
||||
use Magento\Catalog\Model\ResourceModel\CategoryFactory as CategoryResourceFactory;
|
||||
use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap;
|
||||
use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
|
||||
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
|
||||
use Magento\UrlRewrite\Model\OptionProvider;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory;
|
||||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Class for category url rewrites tests
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class CategoryUrlRewriteTest extends TestCase
|
||||
{
|
||||
/** @var ObjectManagerInterface */
|
||||
private $objectManager;
|
||||
|
||||
/** @var CategoryFactory */
|
||||
private $categoryFactory;
|
||||
|
||||
/** @var UrlRewriteCollectionFactory */
|
||||
private $urlRewriteCollectionFactory;
|
||||
|
||||
/** @var CategoryRepositoryInterface */
|
||||
private $categoryRepository;
|
||||
|
||||
/** @var CategoryResourceFactory */
|
||||
private $categoryResourceFactory;
|
||||
|
||||
/** @var CategoryLinkManagementInterface */
|
||||
private $categoryLinkManagment;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/** @var StoreRepositoryInterface */
|
||||
private $storeRepository;
|
||||
|
||||
/** @var ScopeConfigInterface */
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->categoryFactory = $this->objectManager->get(CategoryFactory::class);
|
||||
$this->urlRewriteCollectionFactory = $this->objectManager->get(UrlRewriteCollectionFactory::class);
|
||||
$this->categoryRepository = $this->objectManager->create(CategoryRepositoryInterface::class);
|
||||
$this->categoryResourceFactory = $this->objectManager->get(CategoryResourceFactory::class);
|
||||
$this->categoryLinkManagment = $this->objectManager->create(CategoryLinkManagementInterface::class);
|
||||
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
|
||||
$this->config = $this->objectManager->get(ScopeConfigInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/category_with_position.php
|
||||
* @dataProvider categoryProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnCategorySave(array $data): void
|
||||
{
|
||||
$categoryModel = $this->categoryFactory->create();
|
||||
$categoryModel->isObjectNew(true);
|
||||
$categoryModel->setData($data['data']);
|
||||
$categoryResource = $this->categoryResourceFactory->create();
|
||||
$categoryResource->save($categoryModel);
|
||||
$this->assertNotNull($categoryModel->getId(), 'The category was not created');
|
||||
$urlRewriteCollection = $this->getCategoryRewriteCollection($categoryModel->getId());
|
||||
foreach ($urlRewriteCollection as $item) {
|
||||
foreach ($data['expected_data'] as $field => $expectedItem) {
|
||||
$this->assertEquals(
|
||||
sprintf($expectedItem, $categoryModel->getId()),
|
||||
$item[$field],
|
||||
'The expected data does not match actual value'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function categoryProvider(): array
|
||||
{
|
||||
return [
|
||||
'without_url_key' => [
|
||||
[
|
||||
'data' => [
|
||||
'name' => 'Test Category',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 2,
|
||||
'path' => '1/2',
|
||||
'is_active' => true,
|
||||
],
|
||||
'expected_data' => [
|
||||
'request_path' => 'test-category.html',
|
||||
'target_path' => 'catalog/category/view/id/%s',
|
||||
],
|
||||
],
|
||||
],
|
||||
'subcategory_without_url_key' => [
|
||||
[
|
||||
'data' => [
|
||||
'name' => 'Test Sub Category',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 444,
|
||||
'path' => '1/2/444',
|
||||
'is_active' => true,
|
||||
],
|
||||
'expected_data' => [
|
||||
'request_path' => 'category-1/test-sub-category.html',
|
||||
'target_path' => 'catalog/category/view/id/%s',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/category_tree.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
* @dataProvider productRewriteProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testCategoryProductUrlRewrite(array $data): void
|
||||
{
|
||||
$category = $this->categoryRepository->get(402);
|
||||
$this->categoryLinkManagment->assignProductToCategories('simple2', [$category->getId()]);
|
||||
$productRewriteCollection = $this->getProductRewriteCollection(array_keys($category->getParentCategories()));
|
||||
$this->assertRewrites($productRewriteCollection, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function productRewriteProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
[
|
||||
[
|
||||
'request_path' => 'category-1/category-1-1/category-1-1-1/simple-product2.html',
|
||||
'target_path' => 'catalog/product/view/id/6/category/402',
|
||||
],
|
||||
[
|
||||
'request_path' => 'category-1/simple-product2.html',
|
||||
'target_path' => 'catalog/product/view/id/6/category/400',
|
||||
],
|
||||
[
|
||||
'request_path' => 'category-1/category-1-1/simple-product2.html',
|
||||
'target_path' => 'catalog/product/view/id/6/category/401',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
|
||||
* @magentoAppIsolation enabled
|
||||
* @dataProvider existingUrlProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnCategorySaveWithExistingUrlKey(array $data): void
|
||||
{
|
||||
$this->expectException(UrlAlreadyExistsException::class);
|
||||
$this->expectExceptionMessage((string)__('URL key for specified store already exists.'));
|
||||
$category = $this->categoryFactory->create();
|
||||
$category->setData($data);
|
||||
$categoryResource = $this->categoryResourceFactory->create();
|
||||
$categoryResource->save($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function existingUrlProvider(): array
|
||||
{
|
||||
return [
|
||||
'with_specified_existing_product_url_key' => [
|
||||
'data' => [
|
||||
'name' => 'Test Category',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 2,
|
||||
'path' => '1/2',
|
||||
'is_active' => true,
|
||||
'url_key' => 'simple-product',
|
||||
],
|
||||
],
|
||||
'with_autogenerated_existing_product_url_key' => [
|
||||
'data' => [
|
||||
'name' => 'Simple Product',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 2,
|
||||
'path' => '1/2',
|
||||
'is_active' => true,
|
||||
],
|
||||
],
|
||||
'with_specified_existing_category_url_key' => [
|
||||
'data' => [
|
||||
'name' => 'Test Category',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 2,
|
||||
'path' => '1/2',
|
||||
'is_active' => true,
|
||||
'url_key' => 'category-1',
|
||||
],
|
||||
],
|
||||
'with_autogenerated_existing_category_url_key' => [
|
||||
'data' => [
|
||||
'name' => 'Category 1',
|
||||
'attribute_set_id' => '3',
|
||||
'parent_id' => 2,
|
||||
'path' => '1/2',
|
||||
'is_active' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/category_product.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/catalog_category_with_slash.php
|
||||
* @dataProvider categoryMoveProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnCategoryMove(array $data): void
|
||||
{
|
||||
$categoryId = $data['data']['id'];
|
||||
$category = $this->categoryRepository->get($categoryId);
|
||||
$category->move($data['data']['pid'], $data['data']['aid']);
|
||||
$productRewriteCollection = $this->getProductRewriteCollection(array_keys($category->getParentCategories()));
|
||||
$categoryRewriteCollection = $this->getCategoryRewriteCollection($categoryId);
|
||||
$this->assertRewrites($categoryRewriteCollection, $data['expected_data']['category']);
|
||||
$this->assertRewrites($productRewriteCollection, $data['expected_data']['product']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function categoryMoveProvider(): array
|
||||
{
|
||||
return [
|
||||
'append_category' => [
|
||||
[
|
||||
'data' => [
|
||||
'id' => '333',
|
||||
'pid' => '3331',
|
||||
'aid' => '0',
|
||||
],
|
||||
'expected_data' => [
|
||||
'category' => [
|
||||
[
|
||||
'request_path' => 'category-1.html',
|
||||
'target_path' => 'category-with-slash-symbol/category-1.html',
|
||||
'redirect_type' => OptionProvider::PERMANENT,
|
||||
],
|
||||
[
|
||||
'request_path' => 'category-with-slash-symbol/category-1.html',
|
||||
'target_path' => 'catalog/category/view/id/333',
|
||||
],
|
||||
],
|
||||
'product' => [
|
||||
[
|
||||
'request_path' => 'category-with-slash-symbol/simple-product-three.html',
|
||||
'target_path' => 'catalog/product/view/id/333/category/3331',
|
||||
],
|
||||
[
|
||||
'request_path' => 'category-with-slash-symbol/category-1/simple-product-three.html',
|
||||
'target_path' => 'catalog/product/view/id/333/category/333',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/Catalog/_files/category.php
|
||||
* @magentoAppArea adminhtml
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewritesAfterCategoryDelete(): void
|
||||
{
|
||||
$categoryId = 333;
|
||||
$categoryItemIds = $this->getCategoryRewriteCollection($categoryId)->getAllIds();
|
||||
$this->categoryRepository->deleteByIdentifier($categoryId);
|
||||
$this->assertEmpty(
|
||||
array_intersect($this->getAllRewriteIds(), $categoryItemIds),
|
||||
'Not all expected category url rewrites were deleted'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewritesAfterCategoryWithProductsDelete(): void
|
||||
{
|
||||
$category = $this->categoryRepository->get(3);
|
||||
$childIds = explode(',', $category->getAllChildren());
|
||||
$productRewriteIds = $this->getProductRewriteCollection($childIds)->getAllIds();
|
||||
$categoryItemIds = $this->getCategoryRewriteCollection($childIds)->getAllIds();
|
||||
$this->categoryRepository->deleteByIdentifier($category->getId());
|
||||
$allIds = $this->getAllRewriteIds();
|
||||
$this->assertEmpty(
|
||||
array_intersect($allIds, $categoryItemIds),
|
||||
'Not all expected category url rewrites were deleted'
|
||||
);
|
||||
$this->assertEmpty(
|
||||
array_intersect($allIds, $productRewriteIds),
|
||||
'Not all expected category-product url rewrites were deleted'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/category.php
|
||||
* @return void
|
||||
*/
|
||||
public function testCategoryUrlRewritePerStoreViews(): void
|
||||
{
|
||||
$urlSuffix = $this->config->getValue(
|
||||
CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
$urlKeySecondStore = 'url-key-for-second-store';
|
||||
$secondStoreId = $this->storeRepository->get('fixture_second_store')->getId();
|
||||
$categoryId = 333;
|
||||
$category = $this->categoryRepository->get($categoryId);
|
||||
$urlKeyFirstStore = $category->getUrlKey();
|
||||
$category->setStoreId($secondStoreId);
|
||||
$category->setUrlKey($urlKeySecondStore);
|
||||
$categoryResource = $this->categoryResourceFactory->create();
|
||||
$categoryResource->save($category);
|
||||
$urlRewriteItems = $this->getCategoryRewriteCollection($categoryId)->getItems();
|
||||
foreach ($urlRewriteItems as $item) {
|
||||
$item->getData('store_id') == $secondStoreId
|
||||
? $this->assertEquals($urlKeySecondStore . $urlSuffix, $item->getRequestPath())
|
||||
: $this->assertEquals($urlKeyFirstStore . $urlSuffix, $item->getRequestPath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products url rewrites collection referred to categories
|
||||
*
|
||||
* @param string|array $categoryId
|
||||
* @return UrlRewriteCollection
|
||||
*/
|
||||
private function getProductRewriteCollection($categoryId): UrlRewriteCollection
|
||||
{
|
||||
$condition = is_array($categoryId) ? ['in' => $categoryId] : $categoryId;
|
||||
$productRewriteCollection = $this->urlRewriteCollectionFactory->create();
|
||||
$productRewriteCollection
|
||||
->join(
|
||||
['p' => Product::TABLE_NAME],
|
||||
'main_table.url_rewrite_id = p.url_rewrite_id',
|
||||
'category_id'
|
||||
)
|
||||
->addFieldToFilter('category_id', $condition)
|
||||
->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => DataProductUrlRewriteDatabaseMap::ENTITY_TYPE]);
|
||||
|
||||
return $productRewriteCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all rewrite ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getAllRewriteIds(): array
|
||||
{
|
||||
$urlRewriteCollection = $this->urlRewriteCollectionFactory->create();
|
||||
|
||||
return $urlRewriteCollection->getAllIds();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get category url rewrites collection
|
||||
*
|
||||
* @param string|array $categoryId
|
||||
* @return UrlRewriteCollection
|
||||
*/
|
||||
private function getCategoryRewriteCollection($categoryId): UrlRewriteCollection
|
||||
{
|
||||
$condition = is_array($categoryId) ? ['in' => $categoryId] : $categoryId;
|
||||
$categoryRewriteCollection = $this->urlRewriteCollectionFactory->create();
|
||||
$categoryRewriteCollection->addFieldToFilter(UrlRewrite::ENTITY_ID, $condition)
|
||||
->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => DataCategoryUrlRewriteDatabaseMap::ENTITY_TYPE]);
|
||||
|
||||
return $categoryRewriteCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that actual data contains of expected values
|
||||
*
|
||||
* @param UrlRewriteCollection $collection
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
private function assertRewrites(UrlRewriteCollection $collection, array $expectedData): void
|
||||
{
|
||||
$collectionItems = $collection->toArray()['items'];
|
||||
foreach ($collectionItems as $item) {
|
||||
$found = false;
|
||||
foreach ($expectedData as $expectedItem) {
|
||||
$found = array_intersect_assoc($item, $expectedItem) == $expectedItem;
|
||||
if ($found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found, 'The actual data does not contains of expected values');
|
||||
}
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model\Product;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Verify generate url rewrites for anchor categories.
|
||||
*/
|
||||
class AnchorUrlRewriteGeneratorTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var ObjectRegistryFactory
|
||||
*/
|
||||
private $objectRegistryFactory;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp(); // TODO: Change the autogenerated stub
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$this->objectRegistryFactory = $this->objectManager->create(ObjectRegistryFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify correct generate of the relative "StoreId"
|
||||
*
|
||||
* @param string $expect
|
||||
* @return void
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_with_stores.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @dataProvider getConfigGenerate
|
||||
*/
|
||||
public function testGenerate(string $expect): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple');
|
||||
$categories = $product->getCategoryCollection();
|
||||
$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);
|
||||
|
||||
/** @var AnchorUrlRewriteGenerator $generator */
|
||||
$generator = $this->objectManager->get(AnchorUrlRewriteGenerator::class);
|
||||
|
||||
/** @var $store Store */
|
||||
$store = Bootstrap::getObjectManager()->get(Store::class);
|
||||
$store->load('fixture_second_store', 'code');
|
||||
|
||||
$urls = $generator->generate($store->getId(), $product, $productCategories);
|
||||
|
||||
$this->assertEquals($expect, $urls[0]->getRequestPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testGenerate
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConfigGenerate(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'expect' => 'category-1-custom/simple-product.html'
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\ObjectManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class ProductUrlRewriteGeneratorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_with_category.php
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGenerateWithSpecificCategoryUrlKey()
|
||||
{
|
||||
/** @var ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$product = $productRepository->get('p002');
|
||||
// set global store
|
||||
$product->setStoreId(0);
|
||||
|
||||
/** @var ProductUrlRewriteGenerator $generator */
|
||||
$generator = $this->objectManager->get(ProductUrlRewriteGenerator::class);
|
||||
$urls = $generator->generate($product);
|
||||
|
||||
$actualUrls = array_map(
|
||||
function ($url) {
|
||||
/** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url */
|
||||
return $url->getRequestPath();
|
||||
},
|
||||
$urls
|
||||
);
|
||||
|
||||
self::assertTrue(in_array('p002.html', $actualUrls));
|
||||
self::assertTrue(in_array('cat-1/p002.html', $actualUrls));
|
||||
self::assertTrue(in_array('cat-1-2/p002.html', $actualUrls));
|
||||
}
|
||||
}
|
||||
+390
@@ -0,0 +1,390 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Product\Type;
|
||||
use Magento\Catalog\Model\Product\Visibility;
|
||||
use Magento\Catalog\Model\ProductFactory;
|
||||
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
|
||||
use Magento\CatalogImportExport\Model\Import\Product;
|
||||
use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\ImportExport\Model\Import;
|
||||
use Magento\ImportExport\Model\Import\Source\Csv;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
|
||||
use Magento\UrlRewrite\Model\OptionProvider;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class for product url rewrites tests
|
||||
*
|
||||
* @magentoDbIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class ProductUrlRewriteTest extends AbstractUrlRewriteTest
|
||||
{
|
||||
/** @var ProductFactory */
|
||||
private $productFactory;
|
||||
|
||||
/** @var string */
|
||||
private $suffix;
|
||||
|
||||
/** @var ProductResource */
|
||||
private $productResource;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->productResource = $this->objectManager->create(ProductResource::class);
|
||||
$this->productFactory = $this->objectManager->get(ProductFactory::class);
|
||||
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$this->suffix = $this->config->getValue(
|
||||
ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @dataProvider productDataProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnProductSave(array $data): void
|
||||
{
|
||||
$product = $this->saveProduct($data['data']);
|
||||
$this->assertNotNull($product->getId(), 'The product was not created');
|
||||
$productUrlRewriteCollection = $this->getEntityRewriteCollection($product->getId());
|
||||
$this->assertRewrites(
|
||||
$productUrlRewriteCollection,
|
||||
$this->prepareData($data['expected_data'], (int)$product->getId())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function productDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'without_url_key' => [
|
||||
[
|
||||
'data' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'visibility' => Visibility::VISIBILITY_BOTH,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-product',
|
||||
'name' => 'test product',
|
||||
'price' => 150,
|
||||
'website_ids' => [1]
|
||||
],
|
||||
'expected_data' => [
|
||||
[
|
||||
'request_path' => 'test-product%suffix%',
|
||||
'target_path' => 'catalog/product/view/id/%id%',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'with_url_key' => [
|
||||
[
|
||||
'data' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-product',
|
||||
'visibility' => Visibility::VISIBILITY_BOTH,
|
||||
'name' => 'test product',
|
||||
'price' => 150,
|
||||
'url_key' => 'test-product-url-key',
|
||||
'website_ids' => [1]
|
||||
],
|
||||
'expected_data' => [
|
||||
[
|
||||
'request_path' => 'test-product-url-key%suffix%',
|
||||
'target_path' => 'catalog/product/view/id/%id%',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'with_invisible_product' => [
|
||||
[
|
||||
'data' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-product',
|
||||
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE,
|
||||
'name' => 'test product',
|
||||
'price' => 150,
|
||||
'url_key' => 'test-product-url-key',
|
||||
'website_ids' => [1]
|
||||
],
|
||||
'expected_data' => [],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_simple.php
|
||||
* @dataProvider productEditProvider
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnProductEdit(array $expectedData): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple');
|
||||
$data = [
|
||||
'url_key' => 'new-url-key',
|
||||
'url_key_create_redirect' => $product->getUrlKey(),
|
||||
'save_rewrites_history' => true,
|
||||
];
|
||||
$product = $this->saveProduct($data, $product);
|
||||
$productRewriteCollection = $this->getEntityRewriteCollection($product->getId());
|
||||
$this->assertRewrites(
|
||||
$productRewriteCollection,
|
||||
$this->prepareData($expectedData, (int)$product->getId())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function productEditProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'expected_data' => [
|
||||
[
|
||||
'request_path' => 'new-url-key%suffix%',
|
||||
'target_path' => 'catalog/product/view/id/%id%',
|
||||
],
|
||||
[
|
||||
'request_path' => 'simple-product%suffix%',
|
||||
'target_path' => 'new-url-key%suffix%',
|
||||
'redirect_type' => OptionProvider::PERMANENT,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/category_with_products.php
|
||||
* @dataProvider existingUrlKeyProvider
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function testUrlRewriteOnProductSaveWithExistingUrlKey(array $data): void
|
||||
{
|
||||
$this->expectException(UrlAlreadyExistsException::class);
|
||||
$this->expectExceptionMessage((string)__('URL key for specified store already exists.'));
|
||||
$this->saveProduct($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function existingUrlKeyProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'with_specified_existing_product_url_key' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-simple-product',
|
||||
'name' => 'test-simple-product',
|
||||
'price' => 150,
|
||||
'url_key' => 'simple-product',
|
||||
'store_ids' => [1]
|
||||
],
|
||||
'with_autogenerated_existing_product_url_key' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-simple-product',
|
||||
'name' => 'simple product',
|
||||
'price' => 150,
|
||||
'store_ids' => [1]
|
||||
],
|
||||
'with_specified_existing_category_url_key' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-simple-product',
|
||||
'name' => 'test-simple-product',
|
||||
'price' => 150,
|
||||
'url_key' => 'category-1',
|
||||
'store_ids' => [1]
|
||||
],
|
||||
'with_autogenerated_existing_category_url_key' => [
|
||||
'type_id' => Type::TYPE_SIMPLE,
|
||||
'attribute_set_id' => 4,
|
||||
'sku' => 'test-simple-product',
|
||||
'name' => 'category 1',
|
||||
'price' => 150,
|
||||
'store_ids' => [1]
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
*/
|
||||
public function testUrlRewritesAfterProductDelete(): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple2');
|
||||
$rewriteIds = $this->getEntityRewriteCollection($product->getId())->getAllIds();
|
||||
$this->productRepository->delete($product);
|
||||
$this->assertEmpty(
|
||||
array_intersect($this->getAllRewriteIds(), $rewriteIds),
|
||||
'Not all expected category url rewrites were deleted'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
|
||||
* @return void
|
||||
*/
|
||||
public function testProductUrlRewritePerStoreViews(): void
|
||||
{
|
||||
$urlKeySecondStore = 'url-key-for-second-store';
|
||||
$secondStoreId = $this->storeRepository->get('fixture_second_store')->getId();
|
||||
$product = $this->productRepository->get('simple2');
|
||||
$urlKeyFirstStore = $product->getUrlKey();
|
||||
$product = $this->saveProduct(
|
||||
['store_id' => $secondStoreId, 'url_key' => $urlKeySecondStore],
|
||||
$product
|
||||
);
|
||||
$urlRewriteItems = $this->getEntityRewriteCollection($product->getId())->getItems();
|
||||
$this->assertTrue(count($urlRewriteItems) == 2);
|
||||
foreach ($urlRewriteItems as $item) {
|
||||
$item->getData('store_id') == $secondStoreId
|
||||
? $this->assertEquals($urlKeySecondStore . $this->suffix, $item->getRequestPath())
|
||||
: $this->assertEquals($urlKeyFirstStore . $this->suffix, $item->getRequestPath());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* Check if redirects are generated correctly while product urls are changed during import process.
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testImportProductRewrites()
|
||||
{
|
||||
$data = [
|
||||
['sku' => 'simple1', 'request_path' => 'simple-product1', 'target_path' => 'product-1-updated'],
|
||||
['sku' => 'simple2', 'request_path' => 'simple-product2', 'target_path' => 'product-2-updated'],
|
||||
['sku' => 'simple3', 'request_path' => 'simple-product3', 'target_path' => 'product-3-updated'],
|
||||
];
|
||||
|
||||
$logger = $this->getMockBuilder(LoggerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMockForAbstractClass();
|
||||
$productImport = $this->objectManager->create(
|
||||
Product::class,
|
||||
['logger' => $logger]
|
||||
);
|
||||
$filesystem = $this->objectManager->get(Filesystem::class);
|
||||
|
||||
foreach ($data as $datum) {
|
||||
$this->assertEquals(
|
||||
$datum['request_path'],
|
||||
$this->productRepository->get($datum['sku'], false, null, true)->getUrlKey()
|
||||
);
|
||||
}
|
||||
|
||||
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
|
||||
$source = $this->objectManager->create(
|
||||
Csv::class,
|
||||
[
|
||||
'file' => __DIR__ . '/../_files/products_to_import_with_rewrites.csv',
|
||||
'directory' => $directory
|
||||
]
|
||||
);
|
||||
$errors = $productImport->setParameters(
|
||||
['behavior' => Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
|
||||
)->setSource(
|
||||
$source
|
||||
)->validateData();
|
||||
$this->assertTrue($errors->getErrorsCount() === 0);
|
||||
$productImport->importData();
|
||||
|
||||
foreach ($data as $datum) {
|
||||
$product = $this->productRepository->get($datum['sku'], false, null, true);
|
||||
$this->assertEquals(
|
||||
$datum['target_path'],
|
||||
$product->getUrlKey()
|
||||
);
|
||||
|
||||
$productUrlRewriteCollection = $this->getEntityRewriteCollection($product->getId());
|
||||
$rewriteExists = false;
|
||||
foreach ($productUrlRewriteCollection as $item) {
|
||||
if ($item->getTargetPath() === $datum['target_path'] . $this->suffix &&
|
||||
$item->getRequestPath() === $datum['request_path'] . $this->suffix) {
|
||||
$rewriteExists = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->assertTrue($rewriteExists);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save product with data using resource model directly
|
||||
*
|
||||
* @param array $data
|
||||
* @param ProductInterface|null $product
|
||||
* @return ProductInterface
|
||||
*/
|
||||
protected function saveProduct(array $data, $product = null): ProductInterface
|
||||
{
|
||||
$product = $product ?: $this->productFactory->create();
|
||||
$product->addData($data);
|
||||
$this->productResource->save($product);
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getUrlSuffix(): string
|
||||
{
|
||||
return $this->suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return DataProductUrlRewriteDatabaseMap::ENTITY_TYPE;
|
||||
}
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogUrlRewrite\Model;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\Catalog\Model\Product\Visibility;
|
||||
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
||||
|
||||
/**
|
||||
* Class for product url rewrites tests
|
||||
*
|
||||
*/
|
||||
class ProductUrlRewriteVisibilityTest extends AbstractUrlRewriteTest
|
||||
{
|
||||
private const URL_KEY_EMPTY_MESSAGE = 'Failed asserting URL key is empty for the given product';
|
||||
|
||||
/** @var string */
|
||||
private $suffix;
|
||||
|
||||
/** @var ProductRepositoryInterface */
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$this->suffix = $this->config->getValue(
|
||||
ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
|
||||
* @dataProvider invisibleProductDataProvider
|
||||
* @param array $expectedData
|
||||
* @return void
|
||||
*/
|
||||
#[
|
||||
DataFixture(ProductFixture::class, ['sku' => 'simple', 'name' => 'Simple Url Test Product',
|
||||
'visibility' => Visibility::VISIBILITY_NOT_VISIBLE]),
|
||||
]
|
||||
public function testUrlRewriteOnInvisibleProductEdit(array $expectedData): void
|
||||
{
|
||||
$product = $this->productRepository->get('simple', true, 0, true);
|
||||
$this->assertUrlKeyEmpty($product, self::URL_KEY_EMPTY_MESSAGE);
|
||||
|
||||
//Update visibility and check the database entry
|
||||
$product->setVisibility(Visibility::VISIBILITY_BOTH);
|
||||
$product = $this->productRepository->save($product);
|
||||
|
||||
$productUrlRewriteCollection = $this->getEntityRewriteCollection($product->getId());
|
||||
$this->assertRewrites(
|
||||
$productUrlRewriteCollection,
|
||||
$this->prepareData($expectedData, (int)$product->getId())
|
||||
);
|
||||
|
||||
//Update visibility and check if the entry is removed from the database
|
||||
$product = $this->productRepository->get('simple', true, 0, true);
|
||||
$product->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
|
||||
$product = $this->productRepository->save($product);
|
||||
|
||||
$this->assertUrlKeyEmpty($product, self::URL_KEY_EMPTY_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function invisibleProductDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'expected_data' => [
|
||||
[
|
||||
'request_path' => 'simple-url-test-product%suffix%',
|
||||
'target_path' => 'catalog/product/view/id/%id%',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert URL key is empty in database for the given product
|
||||
*
|
||||
* @param $product
|
||||
* @param string $message
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function assertUrlKeyEmpty($product, $message = ''): void
|
||||
{
|
||||
$productUrlRewriteItems = $this->getEntityRewriteCollection($product->getId())->getItems();
|
||||
$this->assertEmpty($productUrlRewriteItems, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getUrlSuffix(): string
|
||||
{
|
||||
return $this->suffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function getEntityType(): string
|
||||
{
|
||||
return DataProductUrlRewriteDatabaseMap::ENTITY_TYPE;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user