Skip to content

Commit a8d6d08

Browse files
committed
MC-40833: Create automated test for: "Checking product images after Add/Update import failure"
1 parent d33134e commit a8d6d08

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ImportWithNotExistImagesTest.php

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
12+
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
1213
use Magento\CatalogImportExport\Model\Import\ProductImport;
1314
use Magento\Framework\App\Filesystem\DirectoryList;
1415
use Magento\Framework\File\Csv;
@@ -22,17 +23,21 @@
2223
use Magento\ImportExport\Model\Import\Source\CsvFactory;
2324
use Magento\MysqlMq\Model\Driver\Queue;
2425
use Magento\TestFramework\Helper\Bootstrap;
26+
use Magento\TestFramework\MysqlMq\DeleteTopicRelatedMessages;
2527
use PHPUnit\Framework\TestCase;
2628

2729
/**
28-
* Checks that import with not exist images will fail
30+
* Checks import behaviour if specified images do not exist
2931
*
3032
* @see \Magento\CatalogImportExport\Model\Import\Product
3133
*
3234
* @magentoAppArea adminhtml
3335
*/
3436
class ImportWithNotExistImagesTest extends TestCase
3537
{
38+
/** @var string */
39+
const TOPIC = 'import_export.export';
40+
3641
/** @var ObjectManagerInterface */
3742
private $objectManager;
3843

@@ -66,6 +71,20 @@ class ImportWithNotExistImagesTest extends TestCase
6671
/** @var ProductRepositoryInterface */
6772
private $productRepository;
6873

74+
/**
75+
* @inheritdoc
76+
*/
77+
public static function setUpBeforeClass(): void
78+
{
79+
parent::setUpBeforeClass();
80+
81+
$objectManager = Bootstrap::getObjectManager();
82+
/** @var DeleteTopicRelatedMessages $deleteMessages */
83+
$deleteMessages = $objectManager->get(DeleteTopicRelatedMessages::class);
84+
$deleteMessages->execute(self::TOPIC);
85+
}
86+
87+
6988
/**
7089
* @inheritdoc
7190
*/
@@ -83,6 +102,7 @@ protected function setUp(): void
83102
$this->csvFactory = $this->objectManager->get(CsvFactory::class);
84103
$this->fileSystem = $this->objectManager->get(Filesystem::class);
85104
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
105+
$this->productRepository->cleanCache();
86106
}
87107

88108
/**
@@ -102,19 +122,19 @@ protected function tearDown(): void
102122
*
103123
* @return void
104124
*/
105-
public function testImportFailure(): void
125+
public function testImportWithUnexistingImages(): void
106126
{
107127
$this->exportProducts();
108-
$this->assertTrue($this->directory->isExist($this->filePath));
109-
$csv = $this->csvReader->getData($this->directory->getAbsolutePath($this->filePath));
110-
$this->assertCount(2, $csv);
111-
$this->updateExportFile();
128+
$this->assertTrue($this->directory->isExist($this->filePath), 'Products were not imported to file');
129+
$fileContent = $this->csvReader->getData($this->directory->getAbsolutePath($this->filePath));
130+
$this->assertCount(2, $fileContent);
131+
$this->updateFileImagesToInvalidValues();
112132
$this->import->setParameters([
113133
'entity' => Product::ENTITY,
114134
'behavior' => ImportModel::BEHAVIOR_ADD_UPDATE,
115135
]);
116136
$this->assertImportErrors();
117-
$this->assertProductNoHaveChanges();
137+
$this->assertProductImages('/m/a/magento_image.jpg', 'simple');
118138
}
119139

120140
/**
@@ -125,7 +145,7 @@ public function testImportFailure(): void
125145
private function exportProducts(): void
126146
{
127147
$envelope = $this->queue->dequeue();
128-
$decodedMessage = $this->messageEncoder->decode('import_export.export', $envelope->getBody());
148+
$decodedMessage = $this->messageEncoder->decode(self::TOPIC, $envelope->getBody());
129149
$this->consumer->process($decodedMessage);
130150
$this->filePath = 'export/' . $decodedMessage->getFileName();
131151
}
@@ -135,14 +155,18 @@ private function exportProducts(): void
135155
*
136156
* @return void
137157
*/
138-
private function updateExportFile(): void
158+
private function updateFileImagesToInvalidValues(): void
139159
{
140160
$absolutePath = $this->directory->getAbsolutePath($this->filePath);
141161
$csv = $this->csvReader->getData($absolutePath);
142-
foreach ($csv[1] as $key => $data) {
143-
if ($data === '/m/a/magento_image.jpg') {
144-
$csv[1][$key] = '/m/a/invalid_image.jpg';
145-
}
162+
$imagesKeys = ['base_image', 'small_image', 'thumbnail_image'];
163+
$imagesPositions = [];
164+
foreach ($imagesKeys as $key) {
165+
$imagesPositions[] = array_search($key, $csv[0]);
166+
}
167+
168+
foreach ($imagesPositions as $imagesPosition) {
169+
$csv[1][$imagesPosition] = '/m/a/invalid_image.jpg';
146170
}
147171

148172
$this->csvReader->appendData($absolutePath, $csv);
@@ -169,27 +193,34 @@ private function prepareFile(string $file): CsvSource
169193
*/
170194
private function assertImportErrors(): void
171195
{
172-
$errors = $this->import->setSource($this->prepareFile($this->filePath))->validateData();
173-
$this->assertEmpty($errors->getAllErrors());
196+
$validationErrors = $this->import->setSource($this->prepareFile($this->filePath))->validateData();
197+
$this->assertEmpty($validationErrors->getAllErrors());
198+
$this->import->getErrorAggregator()->clear();
174199
$this->import->importData();
175-
$this->assertEquals(1, $errors->getErrorsCount());
176-
$error = $errors->getAllErrors()[0];
177-
$this->assertEquals('mediaUrlNotAvailable', $error->getErrorCode());
200+
$importErrors = $this->import->getErrorAggregator()->getAllErrors();
201+
$this->assertCount(1, $importErrors);
202+
$importError = reset($importErrors);
203+
$this->assertEquals(
204+
RowValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE,
205+
$importError->getErrorCode()
206+
);
178207
$errorMsg = (string)__('Imported resource (image) could not be downloaded ' .
179208
'from external resource due to timeout or access permissions');
180-
$this->assertEquals($errorMsg, $error->getErrorMessage());
209+
$this->assertEquals($errorMsg, $importError->getErrorMessage());
181210
}
182211

183212
/**
184213
* Assert product images were not changed after import
185214
*
215+
* @param string $imageName
216+
* @param string $productSku
186217
* @return void
187218
*/
188-
private function assertProductNoHaveChanges(): void
219+
private function assertProductImages(string $imageName, string $productSku): void
189220
{
190-
$product = $this->productRepository->get('simple');
191-
$this->assertEquals('/m/a/magento_image.jpg', $product->getImage());
192-
$this->assertEquals('/m/a/magento_image.jpg', $product->getSmallImage());
193-
$this->assertEquals('/m/a/magento_image.jpg', $product->getThumbnail());
221+
$product = $this->productRepository->get($productSku);
222+
$this->assertEquals($imageName, $product->getImage());
223+
$this->assertEquals($imageName, $product->getSmallImage());
224+
$this->assertEquals($imageName, $product->getThumbnail());
194225
}
195226
}

0 commit comments

Comments
 (0)