|
8 | 8 |
|
9 | 9 | use Magento\Catalog\Api\CategoryRepositoryInterface;
|
10 | 10 | use Magento\Catalog\Model\Category;
|
| 11 | +use Magento\Catalog\Model\CategoryRepository; |
11 | 12 | use Magento\Catalog\Model\ProductRepository;
|
| 13 | +use Magento\CatalogUrlRewrite\Model\ResourceModel\Category\Product; |
12 | 14 | use Magento\Framework\Exception\CouldNotSaveException;
|
13 | 15 | use Magento\Framework\Exception\LocalizedException;
|
14 | 16 | use Magento\Framework\Exception\NoSuchEntityException;
|
@@ -318,6 +320,53 @@ public function testGenerateUrlRewritesWithoutGenerateProductRewrites()
|
318 | 320 | $this->assertResults($productExpectedResult, $actualResults);
|
319 | 321 | }
|
320 | 322 |
|
| 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 | + |
321 | 370 | /**
|
322 | 371 | * @param array $expected
|
323 | 372 | * @param array $actual
|
|
0 commit comments