16
16
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
17
17
use Magento \Catalog \Model \ResourceModel \Product \Gallery ;
18
18
use Magento \Framework \App \Filesystem \DirectoryList ;
19
+ use Magento \Framework \Exception \ConfigurationMismatchException ;
20
+ use Magento \Framework \Exception \CouldNotSaveException ;
21
+ use Magento \Framework \Exception \FileSystemException ;
22
+ use Magento \Framework \Exception \InputException ;
23
+ use Magento \Framework \Exception \LocalizedException ;
24
+ use Magento \Framework \Exception \NoSuchEntityException ;
25
+ use Magento \Framework \Exception \StateException ;
19
26
use Magento \Framework \Exception \ValidatorException ;
20
27
use Magento \Framework \EntityManager \MetadataPool ;
21
28
use Magento \Framework \Filesystem ;
25
32
use Magento \Store \Model \Store ;
26
33
use Magento \Store \Model \StoreManagerInterface ;
27
34
use Magento \TestFramework \Helper \Bootstrap ;
35
+ use PHPUnit \Framework \TestCase ;
28
36
29
37
/**
30
38
* Provides tests for media gallery images update during product save.
31
39
*
32
40
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
33
41
*/
34
- class UpdateHandlerTest extends \ PHPUnit \ Framework \ TestCase
42
+ class UpdateHandlerTest extends TestCase
35
43
{
36
44
/**
37
45
* @var ObjectManagerInterface
@@ -133,6 +141,8 @@ protected function setUp(): void
133
141
* @magentoDataFixture Magento/Catalog/_files/product_image.php
134
142
*
135
143
* @return void
144
+ * @throws LocalizedException
145
+ * @throws NoSuchEntityException
136
146
*/
137
147
public function testExecuteWithIllegalFilename (): void
138
148
{
@@ -161,6 +171,8 @@ public function testExecuteWithIllegalFilename(): void
161
171
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
162
172
* @magentoDbIsolation enabled
163
173
* @return void
174
+ * @throws LocalizedException
175
+ * @throws NoSuchEntityException
164
176
*/
165
177
public function testExecuteWithOneImage (): void
166
178
{
@@ -176,6 +188,38 @@ public function testExecuteWithOneImage(): void
176
188
$ this ->assertEquals ('1 ' , $ updatedImage ['disabled_default ' ]);
177
189
}
178
190
191
+ /**
192
+ * Tests updating image label and label default during product save.
193
+ *
194
+ * @magentoDataFixture Magento/Catalog/_files/product_with_image.php
195
+ * @magentoDbIsolation enabled
196
+ * @return void
197
+ * @throws LocalizedException
198
+ */
199
+ public function testExecuteImageWithUpdatedAttributeLabel (): void
200
+ {
201
+ $ product = $ this ->getProduct ();
202
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
203
+ $ updatedImage = reset ($ productImages );
204
+ $ this ->assertIsArray ($ updatedImage );
205
+ $ this ->assertEquals ('Image Alt Text ' , $ updatedImage ['label ' ]);
206
+ $ this ->assertEquals ('Image Alt Text ' , $ updatedImage ['label_default ' ]);
207
+ $ this ->updateProductGalleryImages ($ product , ['label ' => 'New image ' ]);
208
+ $ this ->updateHandler ->execute ($ product );
209
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
210
+ $ updatedImage = reset ($ productImages );
211
+ $ this ->assertIsArray ($ updatedImage );
212
+ $ this ->assertEquals ('New image ' , $ updatedImage ['label ' ]);
213
+ $ this ->assertEquals ('New image ' , $ updatedImage ['label_default ' ]);
214
+ $ this ->updateProductGalleryImages ($ product , ['label ' => '' ]);
215
+ $ this ->updateHandler ->execute ($ product );
216
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
217
+ $ updatedImage = reset ($ productImages );
218
+ $ this ->assertIsArray ($ updatedImage );
219
+ $ this ->assertEquals ('' , $ updatedImage ['label ' ]);
220
+ $ this ->assertEquals ('' , $ updatedImage ['label_default ' ]);
221
+ }
222
+
179
223
/**
180
224
* Tests updating image roles during product save.
181
225
*
@@ -184,6 +228,7 @@ public function testExecuteWithOneImage(): void
184
228
* @magentoDbIsolation enabled
185
229
* @param array $roles
186
230
* @return void
231
+ * @throws LocalizedException
187
232
*/
188
233
public function testExecuteWithTwoImagesAndDifferentRoles (array $ roles ): void
189
234
{
@@ -208,6 +253,9 @@ public function testExecuteWithTwoImagesAndDifferentRoles(array $roles): void
208
253
* @magentoDbIsolation enabled
209
254
* @param array $roles
210
255
* @return void
256
+ * @throws LocalizedException
257
+ * @throws ConfigurationMismatchException
258
+ * @throws NoSuchEntityException
211
259
*/
212
260
public function testExecuteWithTwoImagesAndDifferentRolesOnStoreView (array $ roles ): void
213
261
{
@@ -275,6 +323,7 @@ public function executeWithTwoImagesAndRolesDataProvider(): array
275
323
* @magentoDataFixture Magento/Catalog/_files/product_with_multiple_images.php
276
324
* @magentoDbIsolation enabled
277
325
* @return void
326
+ * @throws LocalizedException
278
327
*/
279
328
public function testExecuteWithTwoImagesAndChangedPosition (): void
280
329
{
@@ -303,6 +352,7 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
303
352
* @magentoDataFixture Magento/Catalog/_files/product_with_image.php
304
353
* @magentoDbIsolation enabled
305
354
* @return void
355
+ * @throws LocalizedException
306
356
*/
307
357
public function testExecuteWithImageToDelete (): void
308
358
{
@@ -332,6 +382,8 @@ public function testExecuteWithImageToDelete(): void
332
382
* @magentoDataFixture Magento/Store/_files/second_store.php
333
383
* @magentoDbIsolation enabled
334
384
* @return void
385
+ * @throws LocalizedException
386
+ * @throws NoSuchEntityException
335
387
*/
336
388
public function testExecuteWithTwoImagesOnStoreView (): void
337
389
{
@@ -382,6 +434,7 @@ public function testExecuteWithTwoImagesOnStoreView(): void
382
434
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
383
435
*
384
436
* @return void
437
+ * @throws LocalizedException
385
438
*/
386
439
public function testDeleteSharedImage (): void
387
440
{
@@ -416,6 +469,7 @@ protected function tearDown(): void
416
469
* @param int|null $storeId
417
470
* @param string|null $sku
418
471
* @return ProductInterface|Product
472
+ * @throws NoSuchEntityException
419
473
*/
420
474
private function getProduct (?int $ storeId = null , ?string $ sku = null ): ProductInterface
421
475
{
@@ -441,6 +495,11 @@ private function updateProductGalleryImages(ProductInterface $product, array $im
441
495
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
442
496
* @magentoDbIsolation disabled
443
497
* @return void
498
+ * @throws LocalizedException
499
+ * @throws NoSuchEntityException
500
+ * @throws CouldNotSaveException
501
+ * @throws InputException
502
+ * @throws StateException
444
503
*/
445
504
public function testDeleteWithMultiWebsites (): void
446
505
{
@@ -518,6 +577,9 @@ public function testDeleteWithMultiWebsites(): void
518
577
* @param string $expectedFile
519
578
* @param bool $exist
520
579
* @return void
580
+ * @throws LocalizedException
581
+ * @throws NoSuchEntityException
582
+ * @throws FileSystemException
521
583
*/
522
584
public function testUpdateImage (string $ newFile , string $ expectedFile , bool $ exist ): void
523
585
{
@@ -607,6 +669,8 @@ public function updateImageDataProvider(): array
607
669
* @param array $expectedImages
608
670
* @param array $select
609
671
* @return void
672
+ * @throws LocalizedException
673
+ * @throws NoSuchEntityException
610
674
*/
611
675
public function testAddImages (
612
676
string $ addFromStore ,
@@ -697,6 +761,7 @@ public function addImagesDataProvider(): array
697
761
* @param ProductInterface $product
698
762
* @param string $imagePath
699
763
* @return void
764
+ * @throws FileSystemException
700
765
*/
701
766
private function checkProductImageExist (ProductInterface $ product , string $ imagePath ): void
702
767
{
@@ -735,6 +800,8 @@ private function prepareRemoveImage(ProductInterface $product): ProductInterface
735
800
* @param string $imagePath
736
801
* @param string $productSku
737
802
* @return void
803
+ * @throws LocalizedException
804
+ * @throws NoSuchEntityException
738
805
*/
739
806
private function duplicateMediaGalleryForProduct (string $ imagePath , string $ productSku ): void
740
807
{
0 commit comments