|
| 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\CatalogUrlRewrite\Model\Category\Plugin; |
| 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\Bootstrap as AppBootstrap; |
| 14 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\Framework\Filesystem; |
| 17 | +use Magento\Framework\Filesystem\Driver\File; |
| 18 | +use Magento\Framework\ObjectManagerInterface; |
| 19 | +use Magento\ImportExport\Model\Import; |
| 20 | +use Magento\CatalogImportExport\Model\Import\ProductFactory; |
| 21 | +use Magento\ImportExport\Model\Import\Source\Csv; |
| 22 | +use Magento\ImportExport\Model\Import\Source\CsvFactory; |
| 23 | +use Magento\ImportExport\Model\ResourceModel\Import\Data; |
| 24 | +use Magento\TestFramework\Helper\Bootstrap; |
| 25 | +use PHPUnit\Framework\TestCase; |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks that product import with same images can be successfully done |
| 29 | + * |
| 30 | + * @magentoAppArea adminhtml |
| 31 | + * |
| 32 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 33 | + */ |
| 34 | +class StorageTest extends TestCase |
| 35 | +{ |
| 36 | + /** @var ObjectManagerInterface */ |
| 37 | + private $objectManager; |
| 38 | + |
| 39 | + /** @var Filesystem */ |
| 40 | + private $fileSystem; |
| 41 | + |
| 42 | + /** @var ProductRepositoryInterface */ |
| 43 | + private $productRepository; |
| 44 | + |
| 45 | + /** @var File */ |
| 46 | + private $fileDriver; |
| 47 | + |
| 48 | + /** @var Import */ |
| 49 | + private $import; |
| 50 | + |
| 51 | + /** @var ConfigInterface */ |
| 52 | + private $mediaConfig; |
| 53 | + |
| 54 | + /** @var array */ |
| 55 | + private $appParams; |
| 56 | + |
| 57 | + /** @var array */ |
| 58 | + private $createdProductsSkus = []; |
| 59 | + |
| 60 | + /** @var array */ |
| 61 | + private $filesToRemove = []; |
| 62 | + |
| 63 | + /** @var CsvFactory */ |
| 64 | + private $csvFactory; |
| 65 | + |
| 66 | + /** @var Data */ |
| 67 | + private $importDataResource; |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + protected function setUp(): void |
| 73 | + { |
| 74 | + parent::setUp(); |
| 75 | + |
| 76 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 77 | + $this->fileSystem = $this->objectManager->get(Filesystem::class); |
| 78 | + $this->fileDriver = $this->objectManager->get(File::class); |
| 79 | + $this->mediaConfig = $this->objectManager->get(ConfigInterface::class); |
| 80 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 81 | + $this->productRepository->cleanCache(); |
| 82 | + $this->import = $this->objectManager->get(ProductFactory::class)->create(); |
| 83 | + $this->csvFactory = $this->objectManager->get(CsvFactory::class); |
| 84 | + $this->importDataResource = $this->objectManager->get(Data::class); |
| 85 | + $this->appParams = Bootstrap::getInstance()->getBootstrap()->getApplication() |
| 86 | + ->getInitParams()[AppBootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @inheritdoc |
| 91 | + */ |
| 92 | + protected function tearDown(): void |
| 93 | + { |
| 94 | + $this->removeFiles(); |
| 95 | + $this->removeProducts(); |
| 96 | + $this->importDataResource->cleanBunches(); |
| 97 | + |
| 98 | + parent::tearDown(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return void |
| 103 | + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/issue34210_structure.php |
| 104 | + * @magentoDbIsolation disabled |
| 105 | + */ |
| 106 | + public function testIssue34210(): void |
| 107 | + { |
| 108 | + $source = $this->prepareFile('issue34210.csv'); |
| 109 | + $this->updateUploader(); |
| 110 | + $errors = $this->import->setParameters([ |
| 111 | + 'behavior' => Import::BEHAVIOR_ADD_UPDATE, |
| 112 | + 'entity' => ProductEntity::ENTITY, |
| 113 | + ]) |
| 114 | + ->setSource($source)->validateData(); |
| 115 | + $this->assertEmpty($errors->getAllErrors()); |
| 116 | + |
| 117 | + $result = $this->import->importData(); |
| 118 | + $this->assertTrue($result); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Remove created files |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + private function removeFiles(): void |
| 127 | + { |
| 128 | + foreach ($this->filesToRemove as $file) { |
| 129 | + if ($this->fileDriver->isExists($file)) { |
| 130 | + $this->fileDriver->deleteFile($file); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Remove created products |
| 137 | + * |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + private function removeProducts(): void |
| 141 | + { |
| 142 | + foreach ($this->createdProductsSkus as $sku) { |
| 143 | + try { |
| 144 | + $this->productRepository->deleteById($sku); |
| 145 | + } catch (NoSuchEntityException $e) { |
| 146 | + //already removed |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Prepare file |
| 153 | + * |
| 154 | + * @param string $fileName |
| 155 | + * @return Csv |
| 156 | + */ |
| 157 | + private function prepareFile(string $fileName): Csv |
| 158 | + { |
| 159 | + $tmpDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 160 | + $fixtureDir = realpath(__DIR__ . '/../../../_files'); |
| 161 | + $filePath = $tmpDirectory->getAbsolutePath($fileName); |
| 162 | + $this->filesToRemove[] = $filePath; |
| 163 | + $tmpDirectory->getDriver()->copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath); |
| 164 | + $source = $this->csvFactory->create( |
| 165 | + [ |
| 166 | + 'file' => $fileName, |
| 167 | + 'directory' => $tmpDirectory |
| 168 | + ] |
| 169 | + ); |
| 170 | + |
| 171 | + return $source; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Update upload to use sandbox folders |
| 176 | + * |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + private function updateUploader(): void |
| 180 | + { |
| 181 | + $uploader = $this->import->getUploader(); |
| 182 | + $rootDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::ROOT); |
| 183 | + $destDir = $rootDirectory->getRelativePath( |
| 184 | + $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH] |
| 185 | + . DIRECTORY_SEPARATOR . $this->mediaConfig->getBaseMediaPath() |
| 186 | + ); |
| 187 | + $tmpDir = $rootDirectory->getRelativePath( |
| 188 | + $this->appParams[DirectoryList::MEDIA][DirectoryList::PATH] |
| 189 | + ); |
| 190 | + $rootDirectory->create($destDir); |
| 191 | + $rootDirectory->create($tmpDir); |
| 192 | + $uploader->setDestDir($destDir); |
| 193 | + $uploader->setTmpDir($tmpDir); |
| 194 | + } |
| 195 | +} |
0 commit comments