9
9
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
+ use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface ;
12
13
use Magento \CatalogImportExport \Model \Import \ProductImport ;
13
14
use Magento \Framework \App \Filesystem \DirectoryList ;
14
15
use Magento \Framework \File \Csv ;
22
23
use Magento \ImportExport \Model \Import \Source \CsvFactory ;
23
24
use Magento \MysqlMq \Model \Driver \Queue ;
24
25
use Magento \TestFramework \Helper \Bootstrap ;
26
+ use Magento \TestFramework \MysqlMq \DeleteTopicRelatedMessages ;
25
27
use PHPUnit \Framework \TestCase ;
26
28
27
29
/**
28
- * Checks that import with not exist images will fail
30
+ * Checks import behaviour if specified images do not exist
29
31
*
30
32
* @see \Magento\CatalogImportExport\Model\Import\Product
31
33
*
32
34
* @magentoAppArea adminhtml
33
35
*/
34
36
class ImportWithNotExistImagesTest extends TestCase
35
37
{
38
+ /** @var string */
39
+ const TOPIC = 'import_export.export ' ;
40
+
36
41
/** @var ObjectManagerInterface */
37
42
private $ objectManager ;
38
43
@@ -66,6 +71,20 @@ class ImportWithNotExistImagesTest extends TestCase
66
71
/** @var ProductRepositoryInterface */
67
72
private $ productRepository ;
68
73
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
+
69
88
/**
70
89
* @inheritdoc
71
90
*/
@@ -83,6 +102,7 @@ protected function setUp(): void
83
102
$ this ->csvFactory = $ this ->objectManager ->get (CsvFactory::class);
84
103
$ this ->fileSystem = $ this ->objectManager ->get (Filesystem::class);
85
104
$ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
105
+ $ this ->productRepository ->cleanCache ();
86
106
}
87
107
88
108
/**
@@ -102,19 +122,19 @@ protected function tearDown(): void
102
122
*
103
123
* @return void
104
124
*/
105
- public function testImportFailure (): void
125
+ public function testImportWithUnexistingImages (): void
106
126
{
107
127
$ 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 ();
112
132
$ this ->import ->setParameters ([
113
133
'entity ' => Product::ENTITY ,
114
134
'behavior ' => ImportModel::BEHAVIOR_ADD_UPDATE ,
115
135
]);
116
136
$ this ->assertImportErrors ();
117
- $ this ->assertProductNoHaveChanges ( );
137
+ $ this ->assertProductImages ( ' /m/a/magento_image.jpg ' , ' simple ' );
118
138
}
119
139
120
140
/**
@@ -125,7 +145,7 @@ public function testImportFailure(): void
125
145
private function exportProducts (): void
126
146
{
127
147
$ envelope = $ this ->queue ->dequeue ();
128
- $ decodedMessage = $ this ->messageEncoder ->decode (' import_export.export ' , $ envelope ->getBody ());
148
+ $ decodedMessage = $ this ->messageEncoder ->decode (self :: TOPIC , $ envelope ->getBody ());
129
149
$ this ->consumer ->process ($ decodedMessage );
130
150
$ this ->filePath = 'export/ ' . $ decodedMessage ->getFileName ();
131
151
}
@@ -135,14 +155,18 @@ private function exportProducts(): void
135
155
*
136
156
* @return void
137
157
*/
138
- private function updateExportFile (): void
158
+ private function updateFileImagesToInvalidValues (): void
139
159
{
140
160
$ absolutePath = $ this ->directory ->getAbsolutePath ($ this ->filePath );
141
161
$ 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 ' ;
146
170
}
147
171
148
172
$ this ->csvReader ->appendData ($ absolutePath , $ csv );
@@ -169,27 +193,34 @@ private function prepareFile(string $file): CsvSource
169
193
*/
170
194
private function assertImportErrors (): void
171
195
{
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 ();
174
199
$ 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
+ );
178
207
$ errorMsg = (string )__ ('Imported resource (image) could not be downloaded ' .
179
208
'from external resource due to timeout or access permissions ' );
180
- $ this ->assertEquals ($ errorMsg , $ error ->getErrorMessage ());
209
+ $ this ->assertEquals ($ errorMsg , $ importError ->getErrorMessage ());
181
210
}
182
211
183
212
/**
184
213
* Assert product images were not changed after import
185
214
*
215
+ * @param string $imageName
216
+ * @param string $productSku
186
217
* @return void
187
218
*/
188
- private function assertProductNoHaveChanges ( ): void
219
+ private function assertProductImages ( string $ imageName , string $ productSku ): void
189
220
{
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 ());
194
225
}
195
226
}
0 commit comments