From bcd05dd515f6efd94d4a2867ce5f43c68a92aba9 Mon Sep 17 00:00:00 2001 From: magento-engcom-team Date: Thu, 25 Jan 2018 18:19:23 +0200 Subject: [PATCH] :arrow_double_up: Forwardport of magento/magento2#11323 to 2.3-develop branch --- .../Catalog/Block/Product/View/Gallery.php | 2 +- .../Model/Product/Gallery/CreateHandler.php | 101 +++++++++++++--- .../Unit/Block/Product/View/GalleryTest.php | 109 ++++++++++++++++++ 3 files changed, 195 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/Catalog/Block/Product/View/Gallery.php b/app/code/Magento/Catalog/Block/Product/View/Gallery.php index 90e89acfba77a..5568df1350014 100644 --- a/app/code/Magento/Catalog/Block/Product/View/Gallery.php +++ b/app/code/Magento/Catalog/Block/Product/View/Gallery.php @@ -132,7 +132,7 @@ public function getGalleryImagesJson() 'thumb' => $image->getData('small_image_url'), 'img' => $image->getData('medium_image_url'), 'full' => $image->getData('large_image_url'), - 'caption' => $image->getData('label'), + 'caption' => ($image->getLabel() ?: $this->getProduct()->getName()), 'position' => $image->getData('position'), 'isMain' => $this->isMainImage($image), 'type' => str_replace('external-', '', $image->getMediaType()), diff --git a/app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php b/app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php index 03d418f3ba0d9..cb045aee20899 100644 --- a/app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php +++ b/app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php @@ -167,23 +167,19 @@ public function execute($product, $arguments = []) if (empty($attrData) && empty($clearImages) && empty($newImages) && empty($existImages)) { continue; } - if (in_array($attrData, $clearImages)) { - $product->setData($mediaAttrCode, 'no_selection'); - } - - if (in_array($attrData, array_keys($newImages))) { - $product->setData($mediaAttrCode, $newImages[$attrData]['new_file']); - $product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']); - } - - if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) { - $product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']); - } - if (!empty($product->getData($mediaAttrCode))) { - $product->addAttributeUpdate( + $this->processMediaAttribute( + $product, + $mediaAttrCode, + $clearImages, + $newImages + ); + if (in_array($mediaAttrCode, ['image', 'small_image', 'thumbnail'])) { + $this->processMediaAttributeLabel( + $product, $mediaAttrCode, - $product->getData($mediaAttrCode), - $product->getStoreId() + $clearImages, + $newImages, + $existImages ); } } @@ -448,4 +444,77 @@ private function getMediaAttributeCodes() } return $this->mediaAttributeCodes; } + + /** + * @param \Magento\Catalog\Model\Product $product + * @param $mediaAttrCode + * @param array $clearImages + * @param array $newImages + */ + private function processMediaAttribute( + \Magento\Catalog\Model\Product $product, + $mediaAttrCode, + array $clearImages, + array $newImages + ) { + $attrData = $product->getData($mediaAttrCode); + if (in_array($attrData, $clearImages)) { + $product->setData($mediaAttrCode, 'no_selection'); + } + + if (in_array($attrData, array_keys($newImages))) { + $product->setData($mediaAttrCode, $newImages[$attrData]['new_file']); + } + if (!empty($product->getData($mediaAttrCode))) { + $product->addAttributeUpdate( + $mediaAttrCode, + $product->getData($mediaAttrCode), + $product->getStoreId() + ); + } + } + + /** + * @param \Magento\Catalog\Model\Product $product + * @param $mediaAttrCode + * @param array $clearImages + * @param array $newImages + * @param array $existImages + */ + private function processMediaAttributeLabel( + \Magento\Catalog\Model\Product $product, + $mediaAttrCode, + array $clearImages, + array $newImages, + array $existImages + ) { + $resetLabel = false; + $attrData = $product->getData($mediaAttrCode); + if (in_array($attrData, $clearImages)) { + $product->setData($mediaAttrCode . '_label', null); + $resetLabel = true; + } + + if (in_array($attrData, array_keys($newImages))) { + $product->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']); + } + + if (in_array($attrData, array_keys($existImages)) && isset($existImages[$attrData]['label'])) { + $product->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']); + } + + if ($attrData === 'no_selection' && !empty($product->getData($mediaAttrCode . '_label'))) { + $product->setData($mediaAttrCode . '_label', null); + $resetLabel = true; + } + if (!empty($product->getData($mediaAttrCode . '_label')) + || $resetLabel === true + ) { + $product->addAttributeUpdate( + $mediaAttrCode . '_label', + $product->getData($mediaAttrCode . '_label'), + $product->getStoreId() + ); + } + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php index 32c90496bfc32..16c3e10a337d6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php @@ -91,6 +91,89 @@ protected function mockContext() ->willReturn($this->registry); } + public function testGetGalleryImagesJsonWithLabel() + { + $this->prepareGetGalleryImagesJsonMocks(); + $json = $this->model->getGalleryImagesJson(); + $decodedJson = json_decode($json, true); + $this->assertEquals('product_page_image_small_url', $decodedJson[0]['thumb']); + $this->assertEquals('product_page_image_medium_url', $decodedJson[0]['img']); + $this->assertEquals('product_page_image_large_url', $decodedJson[0]['full']); + $this->assertEquals('test_label', $decodedJson[0]['caption']); + $this->assertEquals('2', $decodedJson[0]['position']); + $this->assertEquals(false, $decodedJson[0]['isMain']); + $this->assertEquals('test_media_type', $decodedJson[0]['type']); + $this->assertEquals('test_video_url', $decodedJson[0]['videoUrl']); + } + + public function testGetGalleryImagesJsonWithoutLabel() + { + $this->prepareGetGalleryImagesJsonMocks(false); + $json = $this->model->getGalleryImagesJson(); + $decodedJson = json_decode($json, true); + $this->assertEquals('test_product_name', $decodedJson[0]['caption']); + } + + private function prepareGetGalleryImagesJsonMocks($hasLabel = true) + { + $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) + ->disableOriginalConstructor() + ->getMock(); + + $productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class) + ->disableOriginalConstructor() + ->getMock(); + + $productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type\AbstractType::class) + ->disableOriginalConstructor() + ->getMock(); + $productTypeMock->expects($this->any()) + ->method('getStoreFilter') + ->with($productMock) + ->willReturn($storeMock); + + $productMock->expects($this->any()) + ->method('getTypeInstance') + ->willReturn($productTypeMock); + $productMock->expects($this->any()) + ->method('getMediaGalleryImages') + ->willReturn($this->getImagesCollectionWithPopulatedDataObject($hasLabel)); + $productMock->expects($this->any()) + ->method('getName') + ->willReturn('test_product_name'); + + $this->registry->expects($this->any()) + ->method('registry') + ->with('product') + ->willReturn($productMock); + + $this->imageHelper->expects($this->any()) + ->method('init') + ->willReturnMap([ + [$productMock, 'product_page_image_small', [], $this->imageHelper], + [$productMock, 'product_page_image_medium_no_frame', [], $this->imageHelper], + [$productMock, 'product_page_image_large_no_frame', [], $this->imageHelper], + ]) + ->willReturnSelf(); + $this->imageHelper->expects($this->any()) + ->method('setImageFile') + ->with('test_file') + ->willReturnSelf(); + $this->imageHelper->expects($this->at(2)) + ->method('getUrl') + ->willReturn('product_page_image_small_url'); + $this->imageHelper->expects($this->at(5)) + ->method('getUrl') + ->willReturn('product_page_image_medium_url'); + $this->imageHelper->expects($this->at(8)) + ->method('getUrl') + ->willReturn('product_page_image_large_url'); + + $this->galleryImagesConfigMock->expects($this->exactly(2)) + ->method('getItems') + ->willReturn($this->getGalleryImagesConfigItems()); + } + public function testGetGalleryImages() { $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) @@ -225,4 +308,30 @@ private function getGalleryImagesConfigItems() ]) ]; } + + /** + * @return \Magento\Framework\Data\Collection + */ + private function getImagesCollectionWithPopulatedDataObject($hasLabel) + { + $collectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection::class) + ->disableOriginalConstructor() + ->getMock(); + + $items = [ + new \Magento\Framework\DataObject([ + 'file' => 'test_file', + 'label' => ($hasLabel ? 'test_label' : ''), + 'position' => '2', + 'media_type' => 'external-test_media_type', + "video_url" => 'test_video_url' + ]), + ]; + + $collectionMock->expects($this->any()) + ->method('getIterator') + ->willReturn(new \ArrayIterator($items)); + + return $collectionMock; + } }