This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,188 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Observer;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
/**
* Test Cases:
*
* - has changes for url_key, is_anchor, changed product list
* - in global scope
* - in store
* - generate canonical
* - generate children
* - has children
* - generate current
* - has rewrites history
* @magentoAppArea adminhtml
*/
class CategoryProcessUrlRewriteSavingObserverTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Framework\ObjectManagerInterface */
protected $objectManager;
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
}
/**
* @param array $filter
* @return array
*/
private function getActualResults(array $filter)
{
/** @var \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder */
$urlFinder = $this->objectManager->get(\Magento\UrlRewrite\Model\UrlFinderInterface::class);
$actualResults = [];
foreach ($urlFinder->findAllByData($filter) as $url) {
$actualResults[] = [
'request_path' => $url->getRequestPath(),
'target_path' => $url->getTargetPath(),
'is_auto_generated' => (int)$url->getIsAutogenerated(),
'redirect_type' => $url->getRedirectType()
];
}
return $actualResults;
}
protected function tearDown(): void
{
$category = $this->objectManager->create(\Magento\Catalog\Model\Category::class);
$category->load(3);
$category->delete();
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testUrlKeyHasChanged()
{
$categoryFilter = [
UrlRewrite::ENTITY_TYPE => 'category',
];
$expected = [
[
'request_path' => "category-1.html",
'target_path' => "catalog/category/view/id/3",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-1/category-1-1.html",
'target_path' => "catalog/category/view/id/4",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-1/category-1-1/category-1-1-1.html",
'target_path' => "catalog/category/view/id/5",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-2.html",
'target_path' => "catalog/category/view/id/6",
'is_auto_generated' => 1,
'redirect_type' => 0
]
];
$actual = $this->getActualResults($categoryFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
/** @var \Magento\Catalog\Model\Category $category */
$category = $this->objectManager->create(\Magento\Catalog\Model\Category::class);
$category->load(3);
$category->setData('save_rewrites_history', false);
$category->setUrlKey('new-url');
$category->save();
$expected = [
[
'request_path' => "new-url.html",
'target_path' => "catalog/category/view/id/3",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "new-url/category-1-1.html",
'target_path' => "catalog/category/view/id/4",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "new-url/category-1-1/category-1-1-1.html",
'target_path' => "catalog/category/view/id/5",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-2.html",
'target_path' => "catalog/category/view/id/6",
'is_auto_generated' => 1,
'redirect_type' => 0
]
];
$actual = $this->getActualResults($categoryFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
public function testIsAnchorHasChanged()
{
$categoryFilter = [
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
];
/** @var \Magento\Catalog\Model\Category $category */
$category = $this->objectManager->create(\Magento\Catalog\Model\Category::class);
$category->load(3);
$category->setData('is_anchor', false);
$category->save();
$expected = [
[
'request_path' => "category-1.html",
'target_path' => "catalog/category/view/id/3",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-1/category-1-1.html",
'target_path' => "catalog/category/view/id/4",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-1/category-1-1/category-1-1-1.html",
'target_path' => "catalog/category/view/id/5",
'is_auto_generated' => 1,
'redirect_type' => 0
],
[
'request_path' => "category-2.html",
'target_path' => "catalog/category/view/id/6",
'is_auto_generated' => 1,
'redirect_type' => 0
]
];
$actual = $this->getActualResults($categoryFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
}
@@ -0,0 +1,292 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Observer;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
use Magento\Store\Test\Fixture\Store as StoreFixture;
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
use Magento\TestFramework\Fixture\AppIsolation;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ProcessUrlRewriteOnChangeVisibilityObserverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* @var DataFixtureStorage
*/
private $fixtures;
/**
* Set up
*/
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->eventManager = $this->objectManager->create(ManagerInterface::class);
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @magentoAppIsolation enabled
*/
public function testMakeProductInvisibleViaMassAction()
{
/** @var \Magento\Catalog\Model\Product $product*/
$product = $this->productRepository->get('product1');
/** @var StoreManagerInterface $storeManager */
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
$firstStore = current($product->getStoreIds());
$testStore = $storeManager->getStore('test');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $firstStore,
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore->getId(),
]
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
$this->eventManager->dispatch(
'catalog_product_attribute_update_before',
[
'attributes_data' => [ ProductInterface::VISIBILITY => Visibility::VISIBILITY_NOT_VISIBLE ],
'product_ids' => [$product->getId()],
'store_id' => $firstStore,
]
);
$actual = $this->getActualResults($productFilter);
//Initially count was 2, when visibility of 1 store view is set to not visible individually, the new count is 1
$this->assertCount(1, $actual);
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_invisible_multistore.php
* @magentoAppIsolation enabled
*/
public function testMakeProductVisibleViaMassAction()
{
/** @var \Magento\Catalog\Model\Product $product*/
$product = $this->productRepository->get('product1');
/** @var StoreManagerInterface $storeManager */
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
$storeManager->setCurrentStore(0);
$testStore = $storeManager->getStore('test');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$actual = $this->getActualResults($productFilter);
$this->assertCount(0, $actual);
$this->eventManager->dispatch(
'catalog_product_attribute_update_before',
[
'attributes_data' => [ ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH ],
'product_ids' => [$product->getId()]
]
);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => '1',
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore->getId(),
]
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
/**
* Test for multistore properties of the product to be respected in generated UrlRewrites
* during the mass update for visibility change
*
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
*/
#[
AppIsolation(true),
DataFixture(WebsiteFixture::class, as: 'w1'),
DataFixture(StoreGroupFixture::class, ['website_id' => '$w1.id$'], 'g1'),
DataFixture(StoreFixture::class, ['store_group_id' => '$g1.id$'], 's1'),
DataFixture(CategoryFixture::class, as: 'c1'),
DataFixture(
ProductFixture::class,
['category_ids' => ['$c1.id$'], 'visibility' => 1, 'website_ids' => [1, '$w1.id$']],
'p1'
),
]
public function testMassActionUrlRewriteForStore()
{
$product = $this->fixtures->get('p1');
$category = $this->fixtures->get('c1');
$store = $this->fixtures->get('s1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$beforeUpdate = $this->getActualResults($productFilter);
$this->assertCount(0, $beforeUpdate);
$this->eventManager->dispatch(
'catalog_product_attribute_update_before',
[
'attributes_data' => [ ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH ],
'product_ids' => [$product->getId()],
'store_id' => Store::DEFAULT_STORE_ID
]
);
$expected = [
[
'request_path' => $product->getUrlKey() . ".html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => (String) Store::DISTRO_STORE_ID
],
[
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . ".html",
'target_path' => "catalog/product/view/id/" . $product->getId() . '/category/' . $category->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => (String) Store::DISTRO_STORE_ID
],
[
'request_path' => $product->getUrlKey() . ".html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => (String) $store->getId()
],
[
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . ".html",
'target_path' => "catalog/product/view/id/" . $product->getId() . '/category/' . $category->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => (String) $store->getId()
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/products_invisible.php
* @magentoAppIsolation enabled
*/
public function testErrorOnDuplicatedUrlKey()
{
$skus = ['product1', 'product2'];
foreach ($skus as $sku) {
/** @var \Magento\Catalog\Model\Product $product */
$productIds[] = $this->productRepository->get($sku)->getId();
}
$this->expectException(UrlAlreadyExistsException::class);
$this->expectExceptionMessage('Can not change the visibility of the product with SKU equals "product2". '
. 'URL key "product-1" for specified store already exists.');
$this->eventManager->dispatch(
'catalog_product_attribute_update_before',
[
'attributes_data' => [ ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH ],
'product_ids' => $productIds
]
);
}
/**
* @param array $filter
* @return array
*/
private function getActualResults(array $filter)
{
/** @var \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder */
$urlFinder = $this->objectManager->get(\Magento\UrlRewrite\Model\UrlFinderInterface::class);
$actualResults = [];
foreach ($urlFinder->findAllByData($filter) as $url) {
$actualResults[] = [
'request_path' => $url->getRequestPath(),
'target_path' => $url->getTargetPath(),
'is_auto_generated' => (int)$url->getIsAutogenerated(),
'redirect_type' => $url->getRedirectType(),
'store_id' => $url->getStoreId()
];
}
return $actualResults;
}
}
@@ -0,0 +1,855 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Observer;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Test\Fixture\Group as GroupFixture;
use Magento\Store\Test\Fixture\Store as StoreFixture;
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
use Magento\TestFramework\Fixture\AppIsolation;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureBeforeTransaction;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Fixture\DbIsolation;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ProductProcessUrlRewriteSavingObserverTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var UrlPersistInterface
*/
private $urlPersist;
/**
* @var DataFixtureStorage
*/
private $fixtures;
/**
* Set up
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->urlPersist = $this->objectManager->get(UrlPersistInterface::class);
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
}
/**
* @param array $filter
* @return array
*/
private function getActualResults(array $filter)
{
/** @var UrlFinderInterface $urlFinder */
$urlFinder = $this->objectManager->get(UrlFinderInterface::class);
$actualResults = [];
foreach ($urlFinder->findAllByData($filter) as $url) {
$actualResults[$url->getRequestPath() . '-' . $url->getStoreId()] = [
'request_path' => $url->getRequestPath(),
'target_path' => $url->getTargetPath(),
'is_auto_generated' => (int)$url->getIsAutogenerated(),
'redirect_type' => $url->getRedirectType(),
'store_id' => $url->getStoreId()
];
}
return $actualResults;
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @magentoAppIsolation enabled
*/
public function testUrlKeyHasChangedInGlobalContext()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContainsEquals($row, $actual);
}
$product->setData('save_rewrites_history', true);
$product->setUrlKey('new-url');
$product->setUrlPath('new-path');
$this->productRepository->save($product);
$expected = [
[
'request_path' => "new-url.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "new-url.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "new-url.html",
'is_auto_generated' => 0,
'redirect_type' => 301,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "new-url.html",
'is_auto_generated' => 0,
'redirect_type' => 301,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContainsEquals($row, $actual);
}
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @magentoAppIsolation enabled
*/
public function testUrlKeyHasChangedInStoreviewContextWithPermanentRedirection()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore($testStore1);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$product->setData('save_rewrites_history', true);
$product->setUrlKey('new-url');
$product->setUrlPath('new-path');
$this->productRepository->save($product);
$expected = [
[
'request_path' => "new-url.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "new-url.html",
'is_auto_generated' => 0,
'redirect_type' => 301,
'store_id' => $testStore1->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContainsEquals($row, $actual);
}
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @magentoAppIsolation enabled
*/
public function testUrlKeyHasChangedInStoreviewContextWithoutPermanentRedirection()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore(1);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
$product->setData('save_rewrites_history', false);
$product->setUrlKey('new-url');
$product->setUrlPath('new-path');
$this->productRepository->save($product);
$expected = [
[
'request_path' => "new-url.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
/**
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @magentoAppIsolation enabled
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testAddAndRemoveProductFromWebsite()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore2 = $this->storeManager->getStore('fixture_second_store');
$testStore3 = $this->storeManager->getStore('fixture_third_store');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
//Product in 1st website. Should result in being in 1st and 4th stores.
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
//Add product to websites corresponding to all 4 stores.
//Rewrites should be present for all stores.
$product->setWebsiteIds(
array_unique(
[
$testStore1->getWebsiteId(),
$testStore2->getWebsiteId(),
$testStore3->getWebsiteId(),
$testStore4->getWebsiteId(),
]
)
);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore3->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
]
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
//Remove product from stores 1 and 4 and leave assigned to stores 2 and 3.
$product->setWebsiteIds(
array_unique(
[
$testStore2->getWebsiteId(),
$testStore3->getWebsiteId(),
]
)
);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore3->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
}
/**
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testChangeVisibilityGlobalScope()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore2 = $this->storeManager->getStore('fixture_second_store');
$testStore3 = $this->storeManager->getStore('fixture_third_store');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
UrlRewrite::ENTITY_ID => $product->getId(),
];
//Product in 1st website. Should result in being in 1st and 4th stores.
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
]
];
$actual = $this->getActualResults($productFilter);
$this->assertEqualsCanonicalizing($expected, $actual);
/** @var Product $product*/
$store4Product = $this->objectManager->get(ProductFactory::class)->create();
$store4Product->setStoreId($testStore4->getId());
$store4Product->setSku($product->getSku());
$store4Product->setUrlKey('product-1-store4');
$this->productRepository->save($store4Product);
//Set product to be not visible at global scope
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
$product->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
$this->productRepository->save($product);
$this->assertEmpty($this->getActualResults($productFilter));
//Add product to websites corresponding to all 4 stores.
//Rewrites should not be present as the product is hidden
//at the global scope.
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
$product->setWebsiteIds(
array_unique(
[
$testStore1->getWebsiteId(),
$testStore2->getWebsiteId(),
$testStore3->getWebsiteId(),
$testStore4->getWebsiteId(),
]
)
);
$this->productRepository->save($product);
$actual = $this->getActualResults($productFilter);
$this->assertEmpty($actual);
//Set product to be visible at global scope
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
$product->setVisibility(Visibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore3->getId(),
],
[
'request_path' => "product-1-store4.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
$this->assertEqualsCanonicalizing($expected, $actual);
}
/**
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testChangeVisibilityLocalScope()
{
$testStore1 = $this->storeManager->getStore('default');
$testStore2 = $this->storeManager->getStore('fixture_second_store');
$testStore3 = $this->storeManager->getStore('fixture_third_store');
$testStore4 = $this->storeManager->getStore('test');
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
/** @var Product $product*/
$product = $this->productRepository->get('product1');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
];
//Product in 1st website. Should result in being in 1st and 4th stores.
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
//Set product to be not visible at store 4 scope
//Rewrite should only be present for store 1
$this->storeManager->setCurrentStore($testStore4);
$product = $this->productRepository->get('product1', true, $testStore4->getId());
$product->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId()
]
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
self::assertCount(count($expected), $actual);
//Add product to websites corresponding to all 4 stores.
//Rewrites should be present for stores 1,2 and 3.
//No rewrites should be present for store 4 as that is not visible
//at local scope.
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
$product = $this->productRepository->get('product1');
$product->getExtensionAttributes()->setWebsiteIds(
array_unique(
[
$testStore1->getWebsiteId(),
$testStore2->getWebsiteId(),
$testStore3->getWebsiteId(),
$testStore4->getWebsiteId()
],
)
);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore3->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContains($row, $actual);
}
//Set product to be visible at store 4 scope only
$this->storeManager->setCurrentStore($testStore4);
$product = $this->productRepository->get('product1');
$product->setVisibility(Visibility::VISIBILITY_BOTH);
$this->productRepository->save($product);
$expected = [
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore3->getId(),
],
[
'request_path' => "product-1.html",
'target_path' => "catalog/product/view/id/" . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore4->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($expected as $row) {
$this->assertContainsEquals($row, $actual);
}
}
#[
DataFixture(WebsiteFixture::class, as: 'website'),
DataFixture(GroupFixture::class, ['website_id' => '$website.id$'], 'store_group'),
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group.id$'], 'store'),
DataFixture(ProductFixture::class, ['sku' => 'simple1', 'website_ids' => [1,'$website.id$'], 'product']),
]
public function testRemoveProductFromAllWebsites(): void
{
$testStore1 = $this->storeManager->getStore('default');
$testStore2 = $this->fixtures->get('store');
$productFilter = [UrlRewrite::ENTITY_TYPE => 'product'];
/** @var Product $product*/
$product = $this->productRepository->get('simple1');
$product->setWebsiteIds([])
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
$this->productRepository->save($product);
$unexpected = [
[
'request_path' => 'simple1.html',
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore1->getId(),
],
[
'request_path' => 'simple1.html',
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $testStore2->getId(),
],
];
$actual = $this->getActualResults($productFilter);
foreach ($unexpected as $row) {
$this->assertNotContains($row, $actual);
}
}
#[
AppIsolation(true),
DbIsolation(true),
DataFixtureBeforeTransaction(
StoreFixture::class,
['store_group_id' => 1, 'website_id' => 1],
as: 'store2'
),
DataFixture(CategoryFixture::class, as: 'category'),
DataFixture(ProductFixture::class, ['category_ids' => ['$category.id$']], as: 'product')
]
public function testNotVisibleOnDefaultStoreVisibleOnDefaultScope()
{
$category = $this->fixtures->get('category');
$product = $this->fixtures->get('product');
$secondStore = $this->fixtures->get('store2');
$this->urlPersist->deleteByData(
[
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => [1, $secondStore->getId()],
]
);
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
'entity_id' => $product->getId(),
'store_id' => [$secondStore->getId()]
];
$actualResults = $this->getActualResults($productFilter);
$this->assertCount(0, $actualResults);
$product->setStoreId(0);
$this->productRepository->save($product);
$actualResults = $this->getActualResults($productFilter);
$this->assertCount(2, $actualResults);
$expected = [
[
'request_path' => $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $secondStore->getId(),
],
[
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $secondStore->getId(),
]
];
foreach ($expected as $row) {
$this->assertContains($row, $actualResults);
}
}
#[
DataFixture(StoreFixture::class, ['group_id' => 1, 'website_id' => 1], as: 'store2'),
DataFixture(CategoryFixture::class, as: 'category'),
DataFixture(ProductFixture::class, ['category_ids' => ['$category.id$']], as: 'product')
]
public function testUrlRewriteGenerationBasedOnScopeVisibility()
{
$secondStore = $this->fixtures->get('store2');
$category = $this->fixtures->get('category');
$product = $this->fixtures->get('product');
$productFilter = [
UrlRewrite::ENTITY_TYPE => 'product',
'entity_id' => $product->getId(),
'store_id' => [1, $secondStore->getId()]
];
$actualResults = $this->getActualResults($productFilter);
$this->assertCount(4, $actualResults);
$productScopeStore1 = $this->productRepository->get($product->getSku(), true, 1);
$productScopeStore1->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
$this->productRepository->save($productScopeStore1);
$actualResults = $this->getActualResults($productFilter);
$this->assertCount(2, $actualResults);
$productGlobal = $this->productRepository->get($product->getSku(), true, Store::DEFAULT_STORE_ID);
$productGlobal->setVisibility(Visibility::VISIBILITY_IN_CATALOG);
$this->productRepository->save($productGlobal);
$actualResults = $this->getActualResults($productFilter);
$this->assertCount(2, $actualResults);
$expected = [
[
'request_path' => $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $secondStore->getId(),
],
[
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $secondStore->getId(),
]
];
$unexpected = [
[
'request_path' => $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => 1 //not expected url rewrite for store 1
],
[
'request_path' => $category->getUrlKey() . '/' . $product->getUrlKey() . '.html',
'target_path' => 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => 1,
],
[
'request_path' => '/'.$product->getUrlKey() . '.html',// not expected anchor root category url rewrite
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => $secondStore->getId(),
],
[
'request_path' => '/'.$product->getUrlKey() . '.html',// not expected anchor root category url rewrite
'target_path' => 'catalog/product/view/id/' . $product->getId(),
'is_auto_generated' => 1,
'redirect_type' => 0,
'store_id' => 1,
]
];
foreach ($expected as $row) {
$this->assertContains($row, $actualResults);
}
foreach ($unexpected as $row) {
$this->assertNotContains($row, $actualResults);
}
}
}
@@ -0,0 +1,138 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CatalogUrlRewrite\Observer;
use Magento\Catalog\Api\CategoryListInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
*/
class UrlRewriteHandlerTest extends TestCase
{
/**
* @var UrlRewriteHandler
*/
private $handler;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
$this->handler = $this->objectManager->create(UrlRewriteHandler::class);
}
/**
* Checks category URLs rewrites generation with enabled `Use Categories Path for Product URLs` option and
* store's specific product URL key.
*
* @magentoDataFixture Magento/CatalogUrlRewrite/Fixtures/product_custom_url_key.php
* @magentoConfigFixture admin_store catalog/seo/product_use_categories 1
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
*/
public function testGenerateProductUrlRewrites()
{
$product = $this->productRepository->get('p002');
$category = $this->getCategory('category 1');
// change the category scope to the global
$category->setStoreId(0)
->setChangedProductIds([$product->getId()])
->setAffectedProductIds([$product->getId()])
->setAnchorsAbove(false);
$generatedUrls = $this->handler->generateProductUrlRewrites($category);
$actual = array_values(
array_map(
function (UrlRewrite $urlRewrite) {
return $urlRewrite->getRequestPath();
},
$generatedUrls
)
);
$expected = [
'store-1-key.html', // the Default store
'cat-1/store-1-key.html', // the Default store with Category URL key, first store view
'cat-1/store-1-key.html', // the Default store with Category URL key, second store view
'p002.html', // the Secondary store
'cat-1-2/p002.html', // the Secondary store with Category URL key, first store view
'cat-1-2/p002.html', // the Secondary store with Category URL key, second store view
];
self::assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
}
/**
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/category_with_products.php
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
*/
public function testGenerateProductUrlRewrites2()
{
$product1 = $this->productRepository->get('simple');
$product2 = $this->productRepository->get('12345');
$category = $this->getCategory('Category 1');
$category->setChangedProductIds([$product1->getId()]);
$category->setAffectedProductIds([$product1->getId(), $product2->getId()]);
$category->setAnchorsAbove(false);
$generatedUrls = $this->handler->generateProductUrlRewrites($category);
$actual = array_values(
array_map(
function (UrlRewrite $urlRewrite) {
return $urlRewrite->getRequestPath();
},
$generatedUrls
)
);
$expected = [
'simple-product.html',
'category-1/simple-product.html'
];
$this->assertEquals($expected, $actual, 'Generated URLs rewrites do not match.');
}
/**
* Gets category by name.
*
* @param string $name
* @return CategoryInterface
*/
private function getCategory(string $name): CategoryInterface
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('name', $name)
->create();
/** @var CategoryListInterface $repository */
$repository = $this->objectManager->get(CategoryListInterface::class);
$items = $repository->getList($searchCriteria)
->getItems();
return array_pop($items);
}
}