Skip to content

Commit 1fb5dcc

Browse files
committed
Merge remote-tracking branch 'remotes/origin/MAGETWO-50770' into PR-3
2 parents 7ba4e19 + f3328d8 commit 1fb5dcc

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

app/code/Magento/GoogleOptimizer/Observer/AbstractSave.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function __construct(
5252
*
5353
* @param Observer $observer
5454
* @return $this
55-
* @throws \InvalidArgumentException
5655
*/
5756
public function execute(Observer $observer)
5857
{
@@ -182,7 +181,7 @@ protected function _deleteCode()
182181
}
183182

184183
/**
185-
* @return array
184+
* @return bool
186185
*/
187186
private function isDataAvailable()
188187
{

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeMediaGalleryManagementInterfaceTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ public function testCreate()
136136
$this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
137137
}
138138

139+
/**
140+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
141+
*/
142+
public function testCreateWithoutFileExtension()
143+
{
144+
$requestData = [
145+
'id' => null,
146+
'media_type' => \Magento\Catalog\Model\Product\Attribute\Backend\Media\ImageEntryConverter::MEDIA_TYPE_CODE,
147+
'label' => 'Image Text',
148+
'position' => 1,
149+
'types' => ['image'],
150+
'disabled' => false,
151+
'content' => [
152+
ImageContentInterface::BASE64_ENCODED_DATA => base64_encode(file_get_contents($this->testImagePath)),
153+
ImageContentInterface::TYPE => 'image/jpeg',
154+
ImageContentInterface::NAME => 'test_image'
155+
]
156+
];
157+
158+
$actualResult = $this->_webApiCall($this->createServiceInfo, ['sku' => 'simple', 'entry' => $requestData]);
159+
$targetProduct = $this->getTargetSimpleProduct();
160+
$mediaGallery = $targetProduct->getData('media_gallery');
161+
162+
$this->assertCount(1, $mediaGallery['images']);
163+
$updatedImage = array_shift($mediaGallery['images']);
164+
$this->assertEquals($actualResult, $updatedImage['value_id']);
165+
$this->assertEquals('Image Text', $updatedImage['label']);
166+
$this->assertEquals(1, $updatedImage['position']);
167+
$this->assertEquals(0, $updatedImage['disabled']);
168+
$this->assertStringStartsWith('/t/e/test_image', $updatedImage['file']);
169+
$this->assertEquals($updatedImage['file'], $targetProduct->getData('image'));
170+
}
171+
139172
/**
140173
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
141174
*/

lib/internal/Magento/Framework/Api/ImageProcessor.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
namespace Magento\Framework\Api;
88

9+
use Magento\Framework\Api\Data\ImageContentInterface;
910
use Magento\Framework\App\Filesystem\DirectoryList;
1011
use Magento\Framework\Exception\InputException;
1112
use Magento\Framework\Filesystem;
1213
use Magento\Framework\Phrase;
1314

1415
/**
1516
* Class ImageProcessor
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1618
*/
1719
class ImageProcessor implements ImageProcessorInterface
1820
{
@@ -142,11 +144,12 @@ public function processImageContent($entityType, $imageContent)
142144

143145
$fileContent = @base64_decode($imageContent->getBase64EncodedData(), true);
144146
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
145-
$fileName = substr(md5(rand()), 0, 7) . '.' . $imageContent->getName();
146-
$tmpDirectory->writeFile($fileName, $fileContent);
147+
$fileName = $this->getFileName($imageContent);
148+
$tmpFileName = substr(md5(rand()), 0, 7) . '.' . $fileName;
149+
$tmpDirectory->writeFile($tmpFileName, $fileContent);
147150

148151
$fileAttributes = [
149-
'tmp_name' => $tmpDirectory->getAbsolutePath() . $fileName,
152+
'tmp_name' => $tmpDirectory->getAbsolutePath() . $tmpFileName,
150153
'name' => $imageContent->getName()
151154
];
152155

@@ -156,7 +159,7 @@ public function processImageContent($entityType, $imageContent)
156159
$this->uploader->setFilenamesCaseSensitivity(false);
157160
$this->uploader->setAllowRenameFiles(true);
158161
$destinationFolder = $entityType;
159-
$this->uploader->save($this->mediaDirectory->getAbsolutePath($destinationFolder), $imageContent->getName());
162+
$this->uploader->save($this->mediaDirectory->getAbsolutePath($destinationFolder), $fileName);
160163
} catch (\Exception $e) {
161164
$this->logger->critical($e);
162165
}
@@ -169,10 +172,23 @@ public function processImageContent($entityType, $imageContent)
169172
*/
170173
protected function getMimeTypeExtension($mimeType)
171174
{
172-
if (isset($this->mimeTypeExtensionMap[$mimeType])) {
173-
return $this->mimeTypeExtensionMap[$mimeType];
174-
} else {
175-
return "";
175+
return isset($this->mimeTypeExtensionMap[$mimeType]) ? $this->mimeTypeExtensionMap[$mimeType] : '';
176+
}
177+
178+
/**
179+
* @param ImageContentInterface $imageContent
180+
* @return string
181+
* @throws \Magento\Framework\Exception\LocalizedException
182+
*/
183+
private function getFileName($imageContent)
184+
{
185+
$fileName = $imageContent->getName();
186+
if (!pathinfo($fileName, PATHINFO_EXTENSION)) {
187+
if (!$imageContent->getType() || !$this->getMimeTypeExtension($imageContent->getType())) {
188+
throw new InputException(new Phrase('Cannot recognize image extension.'));
189+
}
190+
$fileName .= '.' . $this->getMimeTypeExtension($imageContent->getType());
176191
}
192+
return $fileName;
177193
}
178194
}

lib/internal/Magento/Framework/Api/Test/Unit/Api/ImageProcessorTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ public function testSaveWithNoPreviousData()
162162
$imageContent->expects($this->any())
163163
->method('getName')
164164
->willReturn('testFileName');
165+
$imageContent->expects($this->any())
166+
->method('getType')
167+
->willReturn('image/jpg');
165168

166169
$imageDataObject = $this->getMockBuilder('Magento\Framework\Api\AttributeValue')
167170
->disableOriginalConstructor()
@@ -200,7 +203,7 @@ public function testSaveWithPreviousData()
200203
->willReturn('testImageData');
201204
$imageContent->expects($this->any())
202205
->method('getName')
203-
->willReturn('testFileName');
206+
->willReturn('testFileName.png');
204207

205208
$imageDataObject = $this->getMockBuilder('Magento\Framework\Api\AttributeValue')
206209
->disableOriginalConstructor()
@@ -238,4 +241,43 @@ public function testSaveWithPreviousData()
238241

239242
$this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType', $prevImageData));
240243
}
244+
245+
/**
246+
* @expectedException \Magento\Framework\Exception\InputException
247+
* @expectedExceptionMessage Cannot recognize image extension.
248+
*/
249+
public function testSaveWithoutFileExtension()
250+
{
251+
$imageContent = $this->getMockBuilder('Magento\Framework\Api\Data\ImageContentInterface')
252+
->disableOriginalConstructor()
253+
->getMock();
254+
$imageContent->expects($this->once())
255+
->method('getBase64EncodedData')
256+
->willReturn('testImageData');
257+
$imageContent->expects($this->once())
258+
->method('getName')
259+
->willReturn('testFileName');
260+
261+
$imageDataObject = $this->getMockBuilder('Magento\Framework\Api\AttributeValue')
262+
->disableOriginalConstructor()
263+
->getMock();
264+
$imageDataObject->expects($this->once())
265+
->method('getValue')
266+
->willReturn($imageContent);
267+
268+
$imageData = $this->getMockForAbstractClass('Magento\Framework\Api\CustomAttributesDataInterface');
269+
$imageData->expects($this->once())
270+
->method('getCustomAttributes')
271+
->willReturn([]);
272+
273+
$this->dataObjectHelperMock->expects($this->once())
274+
->method('getCustomAttributeValueByType')
275+
->willReturn([$imageDataObject]);
276+
277+
$this->contentValidatorMock->expects($this->once())
278+
->method('isValid')
279+
->willReturn(true);
280+
281+
$this->assertEquals($imageData, $this->imageProcessor->save($imageData, 'testEntityType'));
282+
}
241283
}

0 commit comments

Comments
 (0)