@@ -73,6 +73,7 @@ class UpdateHandlerTest extends \PHPUnit\Framework\TestCase
73
73
* @var string
74
74
*/
75
75
private $ fileName ;
76
+ private $ mediaAttributeId ;
76
77
77
78
/**
78
79
* @inheritdoc
@@ -86,6 +87,7 @@ protected function setUp()
86
87
$ this ->storeRepository = $ this ->objectManager ->create (StoreRepositoryInterface::class);
87
88
$ this ->galleryResource = $ this ->objectManager ->create (Gallery::class);
88
89
$ this ->productResource = $ this ->objectManager ->create (ProductResource::class);
90
+ $ this ->mediaAttributeId = $ this ->productResource ->getAttribute ('media_gallery ' )->getAttributeId ();
89
91
$ this ->config = $ this ->objectManager ->get (Config::class);
90
92
$ this ->mediaDirectory = $ this ->objectManager ->get (Filesystem::class)
91
93
->getDirectoryWrite (DirectoryList::MEDIA );
@@ -109,7 +111,7 @@ public function testExecuteWithIllegalFilename(): void
109
111
'images ' => [
110
112
'image ' => [
111
113
'value_id ' => '100 ' ,
112
- 'file ' => str_repeat ( '/.. ' , 2 ) . DIRECTORY_SEPARATOR . $ this ->fileName ,
114
+ 'file ' => '/../.. ' . DIRECTORY_SEPARATOR . $ this ->fileName ,
113
115
'label ' => 'New image ' ,
114
116
'removed ' => 1 ,
115
117
],
@@ -130,13 +132,11 @@ public function testExecuteWithIllegalFilename(): void
130
132
public function testExecuteWithOneImage (): void
131
133
{
132
134
$ product = $ this ->getProduct ();
133
- $ this ->prepareProductWithOneImage ($ product , ['label ' => 'New image ' , 'disabled ' => '1 ' ]);
135
+ $ this ->updateProductGalleryImages ($ product , ['label ' => 'New image ' , 'disabled ' => '1 ' ]);
134
136
$ this ->updateHandler ->execute ($ product );
135
- $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId (
136
- $ product ,
137
- $ this ->productResource ->getAttribute ('media_gallery ' )->getAttributeId ()
138
- );
137
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
139
138
$ updatedImage = reset ($ productImages );
139
+ $ this ->assertTrue (is_array ($ updatedImage ));
140
140
$ this ->assertEquals ('New image ' , $ updatedImage ['label ' ]);
141
141
$ this ->assertEquals ('New image ' , $ updatedImage ['label_default ' ]);
142
142
$ this ->assertEquals ('1 ' , $ updatedImage ['disabled ' ]);
@@ -248,8 +248,7 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
248
248
$ product ->setData ('store_id ' , Store::DEFAULT_STORE_ID );
249
249
$ product ->setData ('media_gallery ' , ['images ' => $ images ]);
250
250
$ this ->updateHandler ->execute ($ product );
251
- $ galleryAttributeId = $ this ->productResource ->getAttribute ('media_gallery ' )->getAttributeId ();
252
- $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ galleryAttributeId );
251
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
253
252
foreach ($ productImages as $ updatedImage ) {
254
253
$ this ->assertEquals ($ positionMap [$ updatedImage ['file ' ]], $ updatedImage ['position ' ]);
255
254
$ this ->assertEquals ($ positionMap [$ updatedImage ['file ' ]], $ updatedImage ['position_default ' ]);
@@ -266,12 +265,9 @@ public function testExecuteWithTwoImagesAndChangedPosition(): void
266
265
public function testExecuteWithImageToDelete (): void
267
266
{
268
267
$ product = $ this ->getProduct ();
269
- $ this ->prepareProductWithOneImage ($ product , ['removed ' => '1 ' ]);
268
+ $ this ->updateProductGalleryImages ($ product , ['removed ' => '1 ' ]);
270
269
$ this ->updateHandler ->execute ($ product );
271
- $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId (
272
- $ product ,
273
- $ this ->productResource ->getAttribute ('media_gallery ' )->getAttributeId ()
274
- );
270
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
275
271
$ this ->assertCount (0 , $ productImages );
276
272
$ this ->assertFileNotExists (
277
273
$ this ->mediaDirectory ->getAbsolutePath ($ this ->config ->getBaseMediaPath () . '/m/a/magento_image.jpg ' )
@@ -324,8 +320,7 @@ public function testExecuteWithTwoImagesOnStoreView(): void
324
320
}
325
321
$ product ->setData ('media_gallery ' , ['images ' => $ images ]);
326
322
$ this ->updateHandler ->execute ($ product );
327
- $ galleryAttributeId = $ this ->productResource ->getAttribute ('media_gallery ' )->getAttributeId ();
328
- $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ galleryAttributeId );
323
+ $ productImages = $ this ->galleryResource ->loadProductGalleryByAttributeId ($ product , $ this ->mediaAttributeId );
329
324
foreach ($ productImages as $ image ) {
330
325
$ imageToAssert = [
331
326
'label ' => $ image ['label ' ],
@@ -363,10 +358,10 @@ private function getProduct(?int $storeId = null): ProductInterface
363
358
* @param array $imageData
364
359
* @return void
365
360
*/
366
- private function prepareProductWithOneImage (ProductInterface $ product , array $ imageData ): void
361
+ private function updateProductGalleryImages (ProductInterface $ product , array $ imageData ): void
367
362
{
368
363
$ images = $ product ->getData ('media_gallery ' )['images ' ];
369
- $ image = reset ($ images );
364
+ $ image = reset ($ images ) ?: [] ;
370
365
$ product ->setData ('store_id ' , Store::DEFAULT_STORE_ID );
371
366
$ product ->setData ('media_gallery ' , ['images ' => ['image ' => array_merge ($ image , $ imageData )]]);
372
367
}
0 commit comments