Skip to content

Commit f3328d8

Browse files
committed
MAGETWO-50770: Impossible upload image for product via API
1 parent 2e0aa4a commit f3328d8

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Magento\Framework\Api\Data\ImageContentInterface;
1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Framework\Exception\InputException;
12-
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\Filesystem;
1413
use Magento\Framework\Phrase;
1514

@@ -186,7 +185,7 @@ private function getFileName($imageContent)
186185
$fileName = $imageContent->getName();
187186
if (!pathinfo($fileName, PATHINFO_EXTENSION)) {
188187
if (!$imageContent->getType() || !$this->getMimeTypeExtension($imageContent->getType())) {
189-
throw new LocalizedException(__('Cannot recognize image extension'));
188+
throw new InputException(new Phrase('Cannot recognize image extension.'));
190189
}
191190
$fileName .= '.' . $this->getMimeTypeExtension($imageContent->getType());
192191
}

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)