|
| 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\CatalogImportExport\Model\Import\ProductTest; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Category; |
| 12 | +use Magento\CatalogImportExport\Model\Import\ProductTestBase; |
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use Magento\ImportExport\Model\Import; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration test for \Magento\CatalogImportExport\Model\Import\Product class. |
| 18 | + * |
| 19 | + * @magentoAppIsolation enabled |
| 20 | + * @magentoDbIsolation enabled |
| 21 | + * @magentoAppArea adminhtml |
| 22 | + * @magentoDataFixtureBeforeTransaction Magento/Catalog/_files/enable_reindex_schedule.php |
| 23 | + * @magentoDataFixtureBeforeTransaction Magento/Catalog/_files/enable_catalog_product_reindex_schedule.php |
| 24 | + */ |
| 25 | +class ProductCategoriesTest extends ProductTestBase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @magentoAppArea adminhtml |
| 29 | + * @dataProvider categoryTestDataProvider |
| 30 | + * @magentoDbIsolation enabled |
| 31 | + * @magentoAppIsolation enabled |
| 32 | + */ |
| 33 | + public function testProductCategories($fixture, $separator) |
| 34 | + { |
| 35 | + // import data from CSV file |
| 36 | + $pathToFile = __DIR__ . '/../_files/' . $fixture; |
| 37 | + $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 38 | + \Magento\Framework\Filesystem::class |
| 39 | + ); |
| 40 | + |
| 41 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 42 | + $source = $this->objectManager->create( |
| 43 | + \Magento\ImportExport\Model\Import\Source\Csv::class, |
| 44 | + [ |
| 45 | + 'file' => $pathToFile, |
| 46 | + 'directory' => $directory |
| 47 | + ] |
| 48 | + ); |
| 49 | + $errors = $this->_model->setSource( |
| 50 | + $source |
| 51 | + )->setParameters( |
| 52 | + [ |
| 53 | + 'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, |
| 54 | + 'entity' => 'catalog_product', |
| 55 | + Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR => $separator |
| 56 | + ] |
| 57 | + )->validateData(); |
| 58 | + |
| 59 | + $this->assertTrue($errors->getErrorsCount() == 0); |
| 60 | + $this->_model->importData(); |
| 61 | + |
| 62 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 63 | + $resource = $objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class); |
| 64 | + $productId = $resource->getIdBySku('simple1'); |
| 65 | + $this->assertIsNumeric($productId); |
| 66 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 67 | + $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 68 | + \Magento\Catalog\Model\Product::class |
| 69 | + ); |
| 70 | + $product->load($productId); |
| 71 | + $this->assertFalse($product->isObjectNew()); |
| 72 | + $categories = $product->getCategoryIds(); |
| 73 | + $this->assertTrue(count($categories) == 2); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @magentoAppArea adminhtml |
| 78 | + * @magentoDbIsolation disabled |
| 79 | + * @magentoAppIsolation enabled |
| 80 | + * @magentoDataFixture Magento/Catalog/_files/multiple_products.php |
| 81 | + * @magentoDataFixture Magento/Catalog/_files/category.php |
| 82 | + */ |
| 83 | + public function testProductPositionInCategory() |
| 84 | + { |
| 85 | + /* @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ |
| 86 | + $collection = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class); |
| 87 | + $collection->addNameToResult()->load(); |
| 88 | + /** @var Category $category */ |
| 89 | + $category = $collection->getItemByColumnValue('name', 'Category 1'); |
| 90 | + |
| 91 | + /** @var ProductRepositoryInterface $productRepository */ |
| 92 | + $productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 93 | + |
| 94 | + $categoryProducts = []; |
| 95 | + $i = 51; |
| 96 | + foreach (['simple1', 'simple2', 'simple3'] as $sku) { |
| 97 | + $categoryProducts[$productRepository->get($sku)->getId()] = $i++; |
| 98 | + } |
| 99 | + $category->setPostedProducts($categoryProducts); |
| 100 | + $category->save(); |
| 101 | + |
| 102 | + $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 103 | + \Magento\Framework\Filesystem::class |
| 104 | + ); |
| 105 | + |
| 106 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 107 | + $source = $this->objectManager->create( |
| 108 | + \Magento\ImportExport\Model\Import\Source\Csv::class, |
| 109 | + [ |
| 110 | + 'file' => __DIR__ . '/../_files/products_to_import.csv', |
| 111 | + 'directory' => $directory |
| 112 | + ] |
| 113 | + ); |
| 114 | + $errors = $this->_model->setSource( |
| 115 | + $source |
| 116 | + )->setParameters( |
| 117 | + [ |
| 118 | + 'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, |
| 119 | + 'entity' => 'catalog_product' |
| 120 | + ] |
| 121 | + )->validateData(); |
| 122 | + |
| 123 | + $this->assertTrue($errors->getErrorsCount() == 0); |
| 124 | + $this->_model->importData(); |
| 125 | + |
| 126 | + /** @var \Magento\Framework\App\ResourceConnection $resourceConnection */ |
| 127 | + $resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
| 128 | + \Magento\Framework\App\ResourceConnection::class |
| 129 | + ); |
| 130 | + $tableName = $resourceConnection->getTableName('catalog_category_product'); |
| 131 | + $select = $resourceConnection->getConnection()->select()->from($tableName) |
| 132 | + ->where('category_id = ?', $category->getId()); |
| 133 | + $items = $resourceConnection->getConnection()->fetchAll($select); |
| 134 | + $this->assertCount(3, $items); |
| 135 | + foreach ($items as $item) { |
| 136 | + $this->assertGreaterThan(50, $item['position']); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return array |
| 142 | + */ |
| 143 | + public function categoryTestDataProvider() |
| 144 | + { |
| 145 | + return [ |
| 146 | + ['import_new_categories_default_separator.csv', ','], |
| 147 | + ['import_new_categories_custom_separator.csv', '|'] |
| 148 | + ]; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @magentoAppArea adminhtml |
| 153 | + * @magentoDbIsolation disabled |
| 154 | + * @magentoAppIsolation enabled |
| 155 | + * @magentoDataFixture Magento/CatalogImportExport/_files/update_category_duplicates.php |
| 156 | + */ |
| 157 | + public function testProductDuplicateCategories() |
| 158 | + { |
| 159 | + $csvFixture = 'products_duplicate_category.csv'; |
| 160 | + // import data from CSV file |
| 161 | + $pathToFile = __DIR__ . '/../_files/' . $csvFixture; |
| 162 | + $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 163 | + \Magento\Framework\Filesystem::class |
| 164 | + ); |
| 165 | + |
| 166 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 167 | + $source = $this->objectManager->create( |
| 168 | + \Magento\ImportExport\Model\Import\Source\Csv::class, |
| 169 | + [ |
| 170 | + 'file' => $pathToFile, |
| 171 | + 'directory' => $directory |
| 172 | + ] |
| 173 | + ); |
| 174 | + $errors = $this->_model->setSource($source)->setParameters( |
| 175 | + [ |
| 176 | + 'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, |
| 177 | + 'entity' => 'catalog_product' |
| 178 | + ] |
| 179 | + )->validateData(); |
| 180 | + |
| 181 | + $this->assertTrue($errors->getErrorsCount() === 0); |
| 182 | + |
| 183 | + $this->_model->importData(); |
| 184 | + |
| 185 | + $errorProcessor = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
| 186 | + \Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregator::class |
| 187 | + ); |
| 188 | + $errorCount = count($errorProcessor->getAllErrors()); |
| 189 | + $this->assertTrue($errorCount === 1, 'Error expected'); |
| 190 | + |
| 191 | + $errorMessage = $errorProcessor->getAllErrors()[0]->getErrorMessage(); |
| 192 | + $this->assertStringContainsString('URL key for specified store already exists', $errorMessage); |
| 193 | + $this->assertStringContainsString('Default Category/Category 2', $errorMessage); |
| 194 | + |
| 195 | + $categoryAfter = $this->loadCategoryByName('Category 2'); |
| 196 | + $this->assertTrue($categoryAfter === null); |
| 197 | + |
| 198 | + /** @var \Magento\Catalog\Model\Product $product */ |
| 199 | + $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 200 | + \Magento\Catalog\Model\Product::class |
| 201 | + ); |
| 202 | + $product->load(1); |
| 203 | + $categories = $product->getCategoryIds(); |
| 204 | + $this->assertTrue(count($categories) == 1); |
| 205 | + } |
| 206 | + |
| 207 | + protected function loadCategoryByName($categoryName) |
| 208 | + { |
| 209 | + /* @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ |
| 210 | + $collection = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class); |
| 211 | + $collection->addNameToResult()->load(); |
| 212 | + return $collection->getItemByColumnValue('name', $categoryName); |
| 213 | + } |
| 214 | +} |
0 commit comments