|
| 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; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product as ProductEntity; |
| 12 | +use Magento\Catalog\Model\Product\Media\ConfigInterface; |
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 15 | +use Magento\Framework\Filesystem; |
| 16 | +use Magento\Framework\Filesystem\Driver\File; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
| 18 | +use Magento\ImportExport\Model\Import; |
| 19 | +use Magento\ImportExport\Model\Import\Source\Csv; |
| 20 | +use Magento\ImportExport\Model\Import\Source\CsvFactory; |
| 21 | +use Magento\ImportExport\Model\ResourceModel\Import\Data; |
| 22 | +use Magento\TestFramework\Helper\Bootstrap; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Checks that product import with same images can be successfully done |
| 27 | + * |
| 28 | + * @magentoAppArea adminhtml |
| 29 | + * @magentoDbIsolation enabled |
| 30 | + * |
| 31 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 32 | + */ |
| 33 | +class ImportWithSharedImagesTest extends TestCase |
| 34 | +{ |
| 35 | + /** @var ObjectManagerInterface */ |
| 36 | + private $objectManager; |
| 37 | + |
| 38 | + /** @var Filesystem */ |
| 39 | + private $fileSystem; |
| 40 | + |
| 41 | + /** @var ProductRepositoryInterface */ |
| 42 | + private $productRepository; |
| 43 | + |
| 44 | + /** @var File */ |
| 45 | + private $fileDriver; |
| 46 | + |
| 47 | + /** @var Import */ |
| 48 | + private $import; |
| 49 | + |
| 50 | + /** @var ConfigInterface */ |
| 51 | + private $mediaConfig; |
| 52 | + |
| 53 | + /** @var array */ |
| 54 | + private $appParams; |
| 55 | + |
| 56 | + /** @var array */ |
| 57 | + private $createdProductsSkus = []; |
| 58 | + |
| 59 | + /** @var array */ |
| 60 | + private $filesToRemove = []; |
| 61 | + |
| 62 | + /** @var CsvFactory */ |
| 63 | + private $csvFactory; |
| 64 | + |
| 65 | + /** @var Data */ |
| 66 | + private $importDataResource; |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritdoc |
| 70 | + */ |
| 71 | + protected function setUp(): void |
| 72 | + { |
| 73 | + parent::setUp(); |
| 74 | + |
| 75 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 76 | + $this->fileSystem = $this->objectManager->get(Filesystem::class); |
| 77 | + $this->fileDriver = $this->objectManager->get(File::class); |
| 78 | + $this->mediaConfig = $this->objectManager->get(ConfigInterface::class); |
| 79 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 80 | + $this->productRepository->cleanCache(); |
| 81 | + $this->import = $this->objectManager->get(ProductFactory::class)->create(); |
| 82 | + $this->csvFactory = $this->objectManager->get(CsvFactory::class); |
| 83 | + $this->importDataResource = $this->objectManager->get(Data::class); |
| 84 | + $this->appParams = Bootstrap::getInstance()->getBootstrap()->getApplication() |
| 85 | + ->getInitParams()[\Magento\Framework\App\Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @inheritdoc |
| 90 | + */ |
| 91 | + protected function tearDown(): void |
| 92 | + { |
| 93 | + $this->removeFiles(); |
| 94 | + $this->removeProducts(); |
| 95 | + $this->importDataResource->cleanBunches(); |
| 96 | + |
| 97 | + parent::tearDown(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function testImportProductsWithSameImages(): void |
| 104 | + { |
| 105 | + $this->moveImages('magento_image.jpg'); |
| 106 | + $source = $this->prepareFile('catalog_import_products_with_same_images.csv'); |
| 107 | + $this->updateUploader(); |
| 108 | + $errors = $this->import->setParameters([ |
| 109 | + 'behavior' => Import::BEHAVIOR_ADD_UPDATE, |
| 110 | + 'entity' => ProductEntity::ENTITY, |
| 111 | + ]) |
| 112 | + ->setSource($source)->validateData(); |
| 113 | + $this->assertEmpty($errors->getAllErrors()); |
| 114 | + $this->import->importData(); |
| 115 | + $this->createdProductsSkus = ['SimpleProductForTest1', 'SimpleProductForTest2']; |
| 116 | + $this->checkProductsImages('/m/a/magento_image.jpg', $this->createdProductsSkus); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Check product images |
| 121 | + * |
| 122 | + * @param string $expectedImagePath |
| 123 | + * @param array $productSkus |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + private function checkProductsImages(string $expectedImagePath, array $productSkus): void |
| 127 | + { |
| 128 | + foreach ($productSkus as $productSku) { |
| 129 | + $product = $this->productRepository->get($productSku); |
| 130 | + $productImageItem = $product->getMediaGalleryImages()->getFirstItem(); |
| 131 | + $productImageFile = $productImageItem->getFile(); |
| 132 | + $productImagePath = $productImageItem->getPath(); |
| 133 | + $this->filesToRemove[] = $productImagePath; |
| 134 | + $this->assertEquals($expectedImagePath, $productImageFile); |
| 135 | + $this->assertNotEmpty($productImagePath); |
| 136 | + $this->assertTrue($this->fileDriver->isExists($productImagePath)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Remove created files |
| 142 | + * |
| 143 | + * @return void |
| 144 | + */ |
| 145 | + private function removeFiles(): void |
| 146 | + { |
| 147 | + foreach ($this->filesToRemove as $file) { |
| 148 | + if ($this->fileDriver->isExists($file)) { |
| 149 | + $this->fileDriver->deleteFile($file); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Remove created products |
| 156 | + * |
| 157 | + * @return void |
| 158 | + */ |
| 159 | + private function removeProducts(): void |
| 160 | + { |
| 161 | + foreach ($this->createdProductsSkus as $sku) { |
| 162 | + try { |
| 163 | + $this->productRepository->deleteById($sku); |
| 164 | + } catch (NoSuchEntityException $e) { |
| 165 | + //already removed |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Prepare file |
| 172 | + * |
| 173 | + * @param string $fileName |
| 174 | + * @return Csv |
| 175 | + */ |
| 176 | + private function prepareFile(string $fileName): Csv |
| 177 | + { |
| 178 | + $tmpDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 179 | + $fixtureDir = realpath(__DIR__ . '/../../_files'); |
| 180 | + $filePath = $tmpDirectory->getAbsolutePath($fileName); |
| 181 | + $this->filesToRemove[] = $filePath; |
| 182 | + $tmpDirectory->getDriver()->copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath); |
| 183 | + $source = $this->csvFactory->create( |
| 184 | + [ |
| 185 | + 'file' => $fileName, |
| 186 | + 'directory' => $tmpDirectory |
| 187 | + ] |
| 188 | + ); |
| 189 | + |
| 190 | + return $source; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Update upload to use sandbox folders |
| 195 | + * |
| 196 | + * @return void |
| 197 | + */ |
| 198 | + private function updateUploader(): void |
| 199 | + { |
| 200 | + $uploader = $this->import->getUploader(); |
| 201 | + $rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT); |
| 202 | + $destDir = $rootDirectory->getRelativePath( |
| 203 | + $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH] |
| 204 | + . DS . $this->mediaConfig->getBaseMediaPath() |
| 205 | + ); |
| 206 | + $tmpDir = $rootDirectory->getRelativePath( |
| 207 | + $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH] |
| 208 | + ); |
| 209 | + $rootDirectory->create($destDir); |
| 210 | + $rootDirectory->create($tmpDir); |
| 211 | + $uploader->setDestDir($destDir); |
| 212 | + $uploader->setTmpDir($tmpDir); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Move images to appropriate folder |
| 217 | + * |
| 218 | + * @param string $fileName |
| 219 | + * @return void |
| 220 | + */ |
| 221 | + private function moveImages(string $fileName): void |
| 222 | + { |
| 223 | + $rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT); |
| 224 | + $tmpDir = $rootDirectory->getRelativePath( |
| 225 | + $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH] |
| 226 | + ); |
| 227 | + $fixtureDir = realpath(__DIR__ . '/../../_files'); |
| 228 | + $tmpFilePath = $rootDirectory->getAbsolutePath($tmpDir . DS . $fileName); |
| 229 | + $this->fileDriver->createDirectory($tmpDir); |
| 230 | + $rootDirectory->getDriver()->copy( |
| 231 | + $fixtureDir . DIRECTORY_SEPARATOR . $fileName, |
| 232 | + $tmpFilePath |
| 233 | + ); |
| 234 | + $this->filesToRemove[] = $tmpFilePath; |
| 235 | + } |
| 236 | +} |
0 commit comments