Skip to content

Commit 0b0d2c0

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-91589-V2' into EPAM-PR-58
2 parents 8e16abc + 216d9df commit 0b0d2c0

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

app/code/Magento/CatalogUrlRewrite/Model/ResourceModel/Category/Product.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\CatalogUrlRewrite\Model\ResourceModel\Category;
710

811
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
@@ -49,7 +52,7 @@ protected function _construct()
4952
public function saveMultiple(array $insertData)
5053
{
5154
$connection = $this->getConnection();
52-
if (sizeof($insertData) <= self::CHUNK_SIZE) {
55+
if (count($insertData) <= self::CHUNK_SIZE) {
5356
return $connection->insertMultiple($this->getTable(self::TABLE_NAME), $insertData);
5457
}
5558
$data = array_chunk($insertData, self::CHUNK_SIZE);
@@ -98,10 +101,13 @@ public function removeMultipleByProductCategory(array $filter)
98101
private function prepareSelect($data)
99102
{
100103
$select = $this->getConnection()->select();
101-
$select->from($this->getTable(DbStorage::TABLE_NAME), 'url_rewrite_id');
102-
104+
$select->from(DbStorage::TABLE_NAME);
105+
$select->join(
106+
self::TABLE_NAME,
107+
DbStorage::TABLE_NAME . '.url_rewrite_id = ' . self::TABLE_NAME . '.url_rewrite_id'
108+
);
103109
foreach ($data as $column => $value) {
104-
$select->where($this->getConnection()->quoteIdentifier($column) . ' IN (?)', $value);
110+
$select->where(DbStorage::TABLE_NAME . '.' . $column . ' IN (?)', $value);
105111
}
106112
return $select;
107113
}

dev/tests/integration/testsuite/Magento/CatalogUrlRewrite/Model/CategoryUrlRewriteGeneratorTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
use Magento\Catalog\Api\CategoryRepositoryInterface;
1010
use Magento\Catalog\Model\Category;
11+
use Magento\Catalog\Model\CategoryRepository;
1112
use Magento\Catalog\Model\ProductRepository;
13+
use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product;
1214
use Magento\Framework\Exception\CouldNotSaveException;
1315
use Magento\Framework\Exception\LocalizedException;
1416
use Magento\Framework\Exception\NoSuchEntityException;
@@ -318,6 +320,53 @@ public function testGenerateUrlRewritesWithoutGenerateProductRewrites()
318320
$this->assertResults($productExpectedResult, $actualResults);
319321
}
320322

323+
/**
324+
* Check number of records after removing product
325+
*
326+
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_products.php
327+
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
328+
* @magentoDbIsolation enabled
329+
* @magentoAppIsolation enabled
330+
*
331+
* @return void
332+
*/
333+
public function testRemoveCatalogUrlRewrites()
334+
{
335+
/** @var CategoryRepository $categoryRepository */
336+
$categoryRepository = $this->objectManager->create(CategoryRepository::class);
337+
$category = $categoryRepository->get(5);
338+
$categoryId = $category->getId();
339+
340+
/** @var ProductRepository $productRepository */
341+
$productRepository = $this->objectManager->create(ProductRepository::class);
342+
$product = $productRepository->get('12345');
343+
$productId = $product->getId();
344+
345+
$countBeforeRemoving = $this->getCountOfRewrites($productId, $categoryId);
346+
$productRepository->delete($product);
347+
$countAfterRemoving = $this->getCountOfRewrites($productId, $categoryId);
348+
$this->assertEquals($countBeforeRemoving - 1, $countAfterRemoving);
349+
}
350+
351+
/**
352+
* Get count of records in table
353+
*
354+
* @param $productId
355+
* @param $categoryId
356+
* @return string
357+
*/
358+
private function getCountOfRewrites($productId, $categoryId): string
359+
{
360+
/** @var Product $model */
361+
$model = $this->objectManager->get(Product::class);
362+
$connection = $model->getConnection();
363+
$select = $connection->select();
364+
$select->from(Product::TABLE_NAME, 'COUNT(*)');
365+
$select->where('category_id = ?', $categoryId);
366+
$select->where('product_id = ?', $productId);
367+
return $connection->fetchOne($select);
368+
}
369+
321370
/**
322371
* @param array $expected
323372
* @param array $actual

0 commit comments

Comments
 (0)