|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Indexer\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Helper\DefaultCategory; |
| 13 | +use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Category as CategoryResource; |
| 15 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
| 16 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
| 19 | +use Magento\TestFramework\Catalog\Model\GetCategoryByName; |
| 20 | +use Magento\TestFramework\Helper\Bootstrap; |
| 21 | +use Magento\TestFramework\Indexer\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks category products indexing |
| 25 | + * |
| 26 | + * @magentoAppArea adminhtml |
| 27 | + * @magentoAppIsolation enabled |
| 28 | + * @magentoDbIsolation disabled |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 30 | + */ |
| 31 | +class CategoryIndexTest extends TestCase |
| 32 | +{ |
| 33 | + /** @var ObjectManagerInterface */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** @var ProductRepositoryInterface */ |
| 37 | + private $productRepository; |
| 38 | + |
| 39 | + /** @var StoreManagerInterface */ |
| 40 | + private $storeManager; |
| 41 | + |
| 42 | + /** @var AdapterInterface */ |
| 43 | + private $connection; |
| 44 | + |
| 45 | + /** @var TableMaintainer */ |
| 46 | + private $tableMaintainer; |
| 47 | + |
| 48 | + /** @var ProductResource */ |
| 49 | + private $productResource; |
| 50 | + |
| 51 | + /** @var CategoryRepositoryInterface */ |
| 52 | + private $categoryRepository; |
| 53 | + |
| 54 | + /** @var CategoryResource */ |
| 55 | + private $categoryResource; |
| 56 | + |
| 57 | + /** @var GetCategoryByName */ |
| 58 | + private $getCategoryByName; |
| 59 | + |
| 60 | + /** @var DefaultCategory */ |
| 61 | + private $defaultCategoryHelper; |
| 62 | + |
| 63 | + /** |
| 64 | + * @inheritdoc |
| 65 | + */ |
| 66 | + protected function setUp() |
| 67 | + { |
| 68 | + parent::setUp(); |
| 69 | + |
| 70 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 71 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 72 | + $this->productResource = $this->objectManager->get(ProductResource::class); |
| 73 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 74 | + $this->connection = $this->productResource->getConnection(); |
| 75 | + $this->tableMaintainer = $this->objectManager->get(TableMaintainer::class); |
| 76 | + $this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class); |
| 77 | + $this->categoryResource = $this->objectManager->get(CategoryResource::class); |
| 78 | + $this->getCategoryByName = $this->objectManager->create(GetCategoryByName::class); |
| 79 | + $this->defaultCategoryHelper = $this->objectManager->get(DefaultCategory::class); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @magentoDataFixture Magento/Catalog/_files/category_with_parent_anchor.php |
| 84 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 85 | + * |
| 86 | + * @dataProvider assignCategoriesDataProvider |
| 87 | + * |
| 88 | + * @param string $categoryName |
| 89 | + * @param int $expectedItemsCount |
| 90 | + * @return void |
| 91 | + */ |
| 92 | + public function testProductAssignCategory(string $categoryName, int $expectedItemsCount): void |
| 93 | + { |
| 94 | + $product = $this->productRepository->get('simple2'); |
| 95 | + $category = $this->getCategoryByName->execute($categoryName); |
| 96 | + $product->setCategoryIds(array_merge($product->getCategoryIds(), [$category->getId()])); |
| 97 | + $this->productResource->save($product); |
| 98 | + $result = $this->getIndexRecordsByProductId((int)$product->getId()); |
| 99 | + $this->assertEquals($expectedItemsCount, $result); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return array |
| 104 | + */ |
| 105 | + public function assignCategoriesDataProvider(): array |
| 106 | + { |
| 107 | + return [ |
| 108 | + 'assign_to_category' => [ |
| 109 | + 'category_name' => 'Parent category', |
| 110 | + 'expected_records_count' => 1, |
| 111 | + ], |
| 112 | + 'assign_to_category_with_parent_anchor_category' => [ |
| 113 | + 'category_name' => 'Child category', |
| 114 | + 'expected_records_count' => 2, |
| 115 | + ], |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @magentoDataFixture Magento/Catalog/_files/category_with_parent_anchor.php |
| 121 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 122 | + * |
| 123 | + * @dataProvider assignProductsDataProvider |
| 124 | + * |
| 125 | + * @param string $categoryName |
| 126 | + * @param int $expectedCount |
| 127 | + * @return void |
| 128 | + */ |
| 129 | + public function testCategoryAssignProduct(string $categoryName, int $expectedCount): void |
| 130 | + { |
| 131 | + $product = $this->productRepository->get('simple2'); |
| 132 | + $category = $this->getCategoryByName->execute($categoryName); |
| 133 | + $data = ['posted_products' => [$product->getId() => 0]]; |
| 134 | + $category->addData($data); |
| 135 | + $this->categoryResource->save($category); |
| 136 | + $result = $this->getIndexRecordsByProductId((int)$product->getId()); |
| 137 | + $this->assertEquals($expectedCount, $result); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return array |
| 142 | + */ |
| 143 | + public function assignProductsDataProvider(): array |
| 144 | + { |
| 145 | + return [ |
| 146 | + 'assign_product_to_category' => [ |
| 147 | + 'category_name' => 'Parent category', |
| 148 | + 'expected_records_count' => 1, |
| 149 | + ], |
| 150 | + 'assign_product_to_category_with_parent_anchor_category' => [ |
| 151 | + 'category_name' => 'Child category', |
| 152 | + 'expected_records_count' => 2, |
| 153 | + ], |
| 154 | + ]; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @magentoDataFixture Magento/Catalog/_files/category_product_assigned_to_website.php |
| 159 | + * @magentoDataFixture Magento/Catalog/_files/category_with_parent_anchor.php |
| 160 | + * |
| 161 | + * @return void |
| 162 | + */ |
| 163 | + public function testCategoryMove(): void |
| 164 | + { |
| 165 | + $product = $this->productRepository->get('product_with_category'); |
| 166 | + $category = $this->getCategoryByName->execute('Category with product'); |
| 167 | + $newParentCategory = $this->getCategoryByName->execute('Parent category'); |
| 168 | + $afterCategory = $this->getCategoryByName->execute('Child category'); |
| 169 | + $category->move($newParentCategory->getId(), $afterCategory->getId()); |
| 170 | + $result = $this->getIndexRecordsByProductId((int)$product->getId()); |
| 171 | + $this->assertEquals(2, $result); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @magentoDataFixture Magento/Catalog/_files/category_product_assigned_to_website.php |
| 176 | + * |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + public function testDeleteProduct(): void |
| 180 | + { |
| 181 | + $product = $this->productRepository->get('product_with_category'); |
| 182 | + $this->productRepository->delete($product); |
| 183 | + $result = $this->getIndexRecordsByProductId((int)$product->getId()); |
| 184 | + $this->assertEmpty($result); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Fetch data from category product index table |
| 189 | + * |
| 190 | + * @param int $productId |
| 191 | + * @return int |
| 192 | + */ |
| 193 | + private function getIndexRecordsByProductId(int $productId): int |
| 194 | + { |
| 195 | + $tableName = $this->tableMaintainer->getMainTable((int)$this->storeManager->getStore()->getId()); |
| 196 | + $select = $this->connection->select(); |
| 197 | + $select->from(['index_table' => $tableName], new \Zend_Db_Expr('COUNT(*)')) |
| 198 | + ->where('index_table.product_id = ?', $productId) |
| 199 | + ->where('index_table.category_id != ?', $this->defaultCategoryHelper->getId()); |
| 200 | + |
| 201 | + return (int)$this->connection->fetchOne($select); |
| 202 | + } |
| 203 | +} |
0 commit comments