@@ -507,6 +507,92 @@ public function testDeleteWithMultiWebsites(): void
507
507
$ this ->assertArrayNotHasKey ($ secondStoreId , $ imageRolesPerStore );
508
508
}
509
509
510
+ /**
511
+ * Check that product images should be updated successfully regardless if the existing images exist or not
512
+ *
513
+ * @magentoDataFixture Magento/Catalog/_files/product_with_image.php
514
+ * @dataProvider updateImageDataProvider
515
+ * @param string $newFile
516
+ * @param string $expectedFile
517
+ * @param bool $exist
518
+ * @return void
519
+ */
520
+ public function testUpdateImage (string $ newFile , string $ expectedFile , bool $ exist ): void
521
+ {
522
+ $ product = $ this ->getProduct (Store::DEFAULT_STORE_ID );
523
+ $ images = $ product ->getData ('media_gallery ' )['images ' ];
524
+ $ this ->assertCount (1 , $ images );
525
+ $ oldImage = reset ($ images ) ?: [];
526
+ $ this ->assertEquals ($ oldImage ['file ' ], $ product ->getImage ());
527
+ $ this ->assertEquals ($ oldImage ['file ' ], $ product ->getSmallImage ());
528
+ $ this ->assertEquals ($ oldImage ['file ' ], $ product ->getThumbnail ());
529
+ $ path = $ this ->mediaDirectory ->getAbsolutePath ($ this ->config ->getBaseMediaPath () . $ oldImage ['file ' ]);
530
+ $ tmpPath = $ this ->mediaDirectory ->getAbsolutePath ($ this ->config ->getBaseTmpMediaPath () . $ oldImage ['file ' ]);
531
+ $ this ->assertFileExists ($ path );
532
+ $ this ->mediaDirectory ->getDriver ()->copy ($ path , $ tmpPath );
533
+ if (!$ exist ) {
534
+ $ this ->mediaDirectory ->getDriver ()->deleteFile ($ path );
535
+ $ this ->assertFileDoesNotExist ($ path );
536
+ }
537
+ // delete old image
538
+ $ oldImage ['removed ' ] = 1 ;
539
+ $ newImage = [
540
+ 'file ' => $ newFile ,
541
+ 'position ' => 1 ,
542
+ 'label ' => 'New Image Alt Text ' ,
543
+ 'disabled ' => 0 ,
544
+ 'media_type ' => 'image '
545
+ ];
546
+ $ newImageRoles = [
547
+ 'image ' => $ newFile ,
548
+ 'small_image ' => 'no_selection ' ,
549
+ 'thumbnail ' => 'no_selection ' ,
550
+ ];
551
+ $ product ->setData ('media_gallery ' , ['images ' => [$ oldImage , $ newImage ]]);
552
+ $ product ->addData ($ newImageRoles );
553
+ $ this ->updateHandler ->execute ($ product );
554
+ $ product = $ this ->getProduct (Store::DEFAULT_STORE_ID );
555
+ $ images = $ product ->getData ('media_gallery ' )['images ' ];
556
+ $ this ->assertCount (1 , $ images );
557
+ $ image = reset ($ images ) ?: [];
558
+ $ this ->assertEquals ($ newImage ['label ' ], $ image ['label ' ]);
559
+ $ this ->assertEquals ($ expectedFile , $ product ->getImage ());
560
+ $ this ->assertEquals ($ newImageRoles ['small_image ' ], $ product ->getSmallImage ());
561
+ $ this ->assertEquals ($ newImageRoles ['thumbnail ' ], $ product ->getThumbnail ());
562
+ $ path = $ this ->mediaDirectory ->getAbsolutePath ($ this ->config ->getBaseMediaPath () . $ product ->getImage ());
563
+ // Assert that the image exists on disk.
564
+ $ this ->assertFileExists ($ path );
565
+ }
566
+
567
+ /**
568
+ * @return array[]
569
+ */
570
+ public function updateImageDataProvider (): array
571
+ {
572
+ return [
573
+ [
574
+ '/m/a/magento_image.jpg ' ,
575
+ '/m/a/magento_image_1.jpg ' ,
576
+ true
577
+ ],
578
+ [
579
+ '/m/a/magento_image.jpg ' ,
580
+ '/m/a/magento_image.jpg ' ,
581
+ false
582
+ ],
583
+ [
584
+ '/m/a/magento_small_image.jpg ' ,
585
+ '/m/a/magento_small_image.jpg ' ,
586
+ true
587
+ ],
588
+ [
589
+ '/m/a/magento_small_image.jpg ' ,
590
+ '/m/a/magento_small_image.jpg ' ,
591
+ false
592
+ ]
593
+ ];
594
+ }
595
+
510
596
/**
511
597
* Check product image link and product image exist
512
598
*
0 commit comments