Skip to content

Commit 288beb3

Browse files
committed
BUG#AC-2567: Removing a product from all websites in admin causes an SQLSTATE[42000]: Syntax error or access violation-issue fixed
1 parent 28c1f28 commit 288beb3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/ProductProcessUrlRewriteSavingObserver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private function deleteObsoleteRewrites(Product $product): void
105105
$isGlobalScope = $product->getStoreId() == Store::DEFAULT_STORE_ID;
106106
$storesToRemove[] = $isGlobalScope ? $product->getStoreIds() : $product->getStoreId();
107107
}
108+
$storesToRemove = array_filter($storesToRemove);
108109
if ($storesToRemove) {
109110
$this->urlPersist->deleteByData(
110111
[
@@ -130,7 +131,6 @@ private function isWebsiteChanged(Product $product)
130131
return array_diff($oldWebsiteIds, $newWebsiteIds) || array_diff($newWebsiteIds, $oldWebsiteIds);
131132
}
132133

133-
134134
/**
135135
* Is product rewrites need to be updated
136136
*

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Observer/ProductProcessUrlRewriteSavingObserverTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\ObjectManagerInterface;
1212
use Magento\Store\Model\Store;
1313
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Fixture\DataFixtureStorage;
15+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1416
use Magento\TestFramework\Helper\Bootstrap;
1517
use Magento\UrlRewrite\Model\UrlFinderInterface;
1618
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
@@ -37,6 +39,11 @@ class ProductProcessUrlRewriteSavingObserverTest extends TestCase
3739
*/
3840
private $productRepository;
3941

42+
/**
43+
* @var DataFixtureStorage
44+
*/
45+
private $fixtures;
46+
4047
/**
4148
* Set up
4249
*/
@@ -45,6 +52,7 @@ protected function setUp(): void
4552
$this->objectManager = Bootstrap::getObjectManager();
4653
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
4754
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
55+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
4856
}
4957

5058
/**
@@ -632,4 +640,46 @@ public function testChangeVisibilityLocalScope()
632640
$this->assertContainsEquals($row, $actual);
633641
}
634642
}
643+
644+
/**
645+
* phpcs:disable Generic.Files.LineLength.TooLong
646+
* @magentoDataFixture Magento\Store\Test\Fixture\Website as:website
647+
* @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"website_id":"$website.id$"} as:store_group
648+
* @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group.id$"} as:store
649+
* @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"sku":"simple1","website_ids":[1,"$website.id$"]} as:product
650+
* @magentoAppIsolation enabled
651+
*/
652+
public function testRemoveProductFromAllWebsites(): void
653+
{
654+
$testStore1 = $this->storeManager->getStore('default');
655+
$testStore2 = $this->fixtures->get('store');
656+
657+
$productFilter = [UrlRewrite::ENTITY_TYPE => 'product'];
658+
659+
/** @var Product $product*/
660+
$product = $this->productRepository->get('simple1');
661+
$product->setWebsiteIds([])
662+
->setVisibility(Visibility::VISIBILITY_NOT_VISIBLE);
663+
$this->productRepository->save($product);
664+
$unexpected = [
665+
[
666+
'request_path' => 'simple1.html',
667+
'target_path' => 'catalog/product/view/id/' . $product->getId(),
668+
'is_auto_generated' => 1,
669+
'redirect_type' => 0,
670+
'store_id' => $testStore1->getId(),
671+
],
672+
[
673+
'request_path' => 'simple1.html',
674+
'target_path' => 'catalog/product/view/id/' . $product->getId(),
675+
'is_auto_generated' => 1,
676+
'redirect_type' => 0,
677+
'store_id' => $testStore2->getId(),
678+
],
679+
];
680+
$actual = $this->getActualResults($productFilter);
681+
foreach ($unexpected as $row) {
682+
$this->assertNotContains($row, $actual);
683+
}
684+
}
635685
}

0 commit comments

Comments
 (0)