|
14 | 14 | */
|
15 | 15 | namespace Magento\CatalogImportExport\Model\Import;
|
16 | 16 |
|
| 17 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 18 | +use Magento\Catalog\Model\Category; |
17 | 19 | use Magento\Framework\App\Bootstrap;
|
18 | 20 | use Magento\Framework\App\Filesystem\DirectoryList;
|
19 | 21 | use Magento\ImportExport\Model\Import;
|
@@ -887,6 +889,70 @@ public function testProductCategories($fixture, $separator)
|
887 | 889 | $this->assertTrue(count($categories) == 2);
|
888 | 890 | }
|
889 | 891 |
|
| 892 | + /** |
| 893 | + * @magentoAppArea adminhtml |
| 894 | + * @magentoDbIsolation enabled |
| 895 | + * @magentoAppIsolation enabled |
| 896 | + * @magentoDataFixture Magento/Catalog/_files/multiple_products.php |
| 897 | + * @magentoDataFixture Magento/Catalog/_files/category.php |
| 898 | + */ |
| 899 | + public function testProductPositionInCategory() |
| 900 | + { |
| 901 | + /* @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */ |
| 902 | + $collection = $this->objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class); |
| 903 | + $collection->addNameToResult()->load(); |
| 904 | + /** @var Category $category */ |
| 905 | + $category = $collection->getItemByColumnValue('name', 'Category 1'); |
| 906 | + |
| 907 | + /** @var ProductRepositoryInterface $productRepository */ |
| 908 | + $productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 909 | + |
| 910 | + $categoryProducts = []; |
| 911 | + $i = 51; |
| 912 | + foreach (['simple1', 'simple2', 'simple3'] as $sku) { |
| 913 | + $categoryProducts[$productRepository->get($sku)->getId()] = $i++; |
| 914 | + } |
| 915 | + $category->setPostedProducts($categoryProducts); |
| 916 | + $category->save(); |
| 917 | + |
| 918 | + $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 919 | + \Magento\Framework\Filesystem::class |
| 920 | + ); |
| 921 | + |
| 922 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 923 | + $source = $this->objectManager->create( |
| 924 | + \Magento\ImportExport\Model\Import\Source\Csv::class, |
| 925 | + [ |
| 926 | + 'file' => __DIR__ . '/_files/products_to_import.csv', |
| 927 | + 'directory' => $directory |
| 928 | + ] |
| 929 | + ); |
| 930 | + $errors = $this->_model->setSource( |
| 931 | + $source |
| 932 | + )->setParameters( |
| 933 | + [ |
| 934 | + 'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, |
| 935 | + 'entity' => 'catalog_product' |
| 936 | + ] |
| 937 | + )->validateData(); |
| 938 | + |
| 939 | + $this->assertTrue($errors->getErrorsCount() == 0); |
| 940 | + $this->_model->importData(); |
| 941 | + |
| 942 | + /** @var \Magento\Framework\App\ResourceConnection $resourceConnection */ |
| 943 | + $resourceConnection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( |
| 944 | + \Magento\Framework\App\ResourceConnection::class |
| 945 | + ); |
| 946 | + $tableName = $resourceConnection->getTableName('catalog_category_product'); |
| 947 | + $select = $resourceConnection->getConnection()->select()->from($tableName) |
| 948 | + ->where('category_id = ?', $category->getId()); |
| 949 | + $items = $resourceConnection->getConnection()->fetchAll($select); |
| 950 | + $this->assertCount(3, $items); |
| 951 | + foreach ($items as $item) { |
| 952 | + $this->assertGreaterThan(50, $item['position']); |
| 953 | + } |
| 954 | + } |
| 955 | + |
890 | 956 | /**
|
891 | 957 | * @return array
|
892 | 958 | */
|
|
0 commit comments