Skip to content

Commit 3459ef1

Browse files
committed
MAGETWO-54718: [GitHub] Exception thrown where no Product Image file found #5184
1 parent 39d7f39 commit 3459ef1

File tree

2 files changed

+119
-9
lines changed
  • app/code/Magento/Catalog

2 files changed

+119
-9
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,18 @@ public function getImagesJson()
138138
is_array($value['images']) &&
139139
count($value['images'])
140140
) {
141-
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
142-
$staticDir = $this->_filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
141+
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
143142
$images = $this->sortImagesByPosition($value['images']);
144143
foreach ($images as &$image) {
145144
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
146145
try {
147146
$fileHandler = $mediaDir->stat($this->_mediaConfig->getMediaPath($image['file']));
148147
$image['size'] = $fileHandler['size'];
149-
} catch (\Exception $e) {
150-
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('image');
148+
} catch (\Magento\Framework\Exception\FileSystemException $e) {
149+
$staticDir = $this->_filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
150+
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('thumbnail');
151151
$fileHandler = $staticDir->stat(
152-
$this->getAssetRepo()->createAsset($this->getImageHelper()->getPlaceholder('image'))->getPath()
152+
$this->getAssetRepo()->createAsset($this->getImageHelper()->getPlaceholder('thumbnail'))->getPath()
153153
);
154154
$image['size'] = $fileHandler['size'];
155155
$this->_logger->warning($e);

app/code/Magento/Catalog/Test/Unit/Block/Adminhtml/Product/Helper/Form/Gallery/ContentTest.php

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\Filesystem;
99
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content;
10+
use Magento\Framework\Phrase;
1011

1112
class ContentTest extends \PHPUnit_Framework_TestCase
1213
{
@@ -40,14 +41,30 @@ class ContentTest extends \PHPUnit_Framework_TestCase
4041
*/
4142
protected $galleryMock;
4243

44+
/**
45+
* @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $imageHelper;
48+
49+
/**
50+
* @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
protected $assetRepo;
53+
4354
/**
4455
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
4556
*/
4657
protected $objectManager;
4758

4859
public function setUp()
4960
{
50-
$this->fileSystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
61+
$this->fileSystemMock = $this->getMock(
62+
'Magento\Framework\Filesystem',
63+
['stat', 'getDirectoryRead'],
64+
[],
65+
'',
66+
false
67+
);
5168
$this->readMock = $this->getMock('Magento\Framework\Filesystem\Directory\ReadInterface');
5269
$this->galleryMock = $this->getMock(
5370
'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery',
@@ -56,7 +73,13 @@ public function setUp()
5673
'',
5774
false
5875
);
59-
$this->mediaConfigMock = $this->getMock('Magento\Catalog\Model\Product\Media\Config', [], [], '', false);
76+
$this->mediaConfigMock = $this->getMock(
77+
'Magento\Catalog\Model\Product\Media\Config',
78+
['getMediaUrl', 'getMediaPath'],
79+
[],
80+
'',
81+
false
82+
);
6083
$this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface')
6184
->disableOriginalConstructor()
6285
->getMock();
@@ -126,11 +149,10 @@ public function testGetImagesJson()
126149

127150
$this->content->setElement($this->galleryMock);
128151
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
129-
$this->fileSystemMock->expects($this->exactly(2))->method('getDirectoryRead')->willReturn($this->readMock);
152+
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->willReturn($this->readMock);
130153

131154
$this->mediaConfigMock->expects($this->any())->method('getMediaUrl')->willReturnMap($url);
132155
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturnMap($mediaPath);
133-
134156
$this->readMock->expects($this->any())->method('stat')->willReturnMap($sizeMap);
135157
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');
136158

@@ -144,4 +166,92 @@ public function testGetImagesJsonWithoutImages()
144166

145167
$this->assertSame('[]', $this->content->getImagesJson());
146168
}
169+
170+
public function testGetImagesJsonWithException()
171+
{
172+
$this->imageHelper = $this->getMockBuilder('Magento\Catalog\Helper\Image')
173+
->disableOriginalConstructor()
174+
->setMethods(['getDefaultPlaceholderUrl', 'getPlaceholder'])
175+
->getMock();
176+
177+
$this->assetRepo = $this->getMockBuilder('Magento\Framework\View\Asset\Repository')
178+
->disableOriginalConstructor()
179+
->setMethods(['createAsset', 'getPath'])
180+
->getMock();
181+
182+
$this->objectManager->setBackwardCompatibleProperty(
183+
$this->content,
184+
'imageHelper',
185+
$this->imageHelper
186+
);
187+
188+
$this->objectManager->setBackwardCompatibleProperty(
189+
$this->content,
190+
'assetRepo',
191+
$this->assetRepo
192+
);
193+
194+
$placeholderUrl = 'url_to_the_placeholder/placeholder.jpg';
195+
196+
$sizePlaceholder = ['size' => 399659];
197+
198+
$imagesResult = [
199+
[
200+
'value_id' => '2',
201+
'file' => 'file_2.jpg',
202+
'media_type' => 'image',
203+
'position' => '0',
204+
'url' => 'url_to_the_placeholder/placeholder.jpg',
205+
'size' => 399659
206+
],
207+
[
208+
'value_id' => '1',
209+
'file' => 'file_1.jpg',
210+
'media_type' => 'image',
211+
'position' => '1',
212+
'url' => 'url_to_the_placeholder/placeholder.jpg',
213+
'size' => 399659
214+
]
215+
];
216+
217+
$images = [
218+
'images' => [
219+
[
220+
'value_id' => '1',
221+
'file' => 'file_1.jpg',
222+
'media_type' => 'image',
223+
'position' => '1'
224+
],
225+
[
226+
'value_id' => '2',
227+
'file' => 'file_2.jpg',
228+
'media_type' => 'image',
229+
'position' => '0'
230+
]
231+
]
232+
];
233+
234+
$this->content->setElement($this->galleryMock);
235+
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
236+
$this->fileSystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($this->readMock);
237+
$this->mediaConfigMock->expects($this->any())->method('getMediaUrl');
238+
$this->mediaConfigMock->expects($this->any())->method('getMediaPath');
239+
$this->readMock->expects($this->any())->method('stat')->willReturnOnConsecutiveCalls(
240+
$this->throwException(
241+
new \Magento\Framework\Exception\FileSystemException(new \Magento\Framework\Phrase('test'))
242+
),
243+
$sizePlaceholder,
244+
$this->throwException(
245+
new \Magento\Framework\Exception\FileSystemException(new \Magento\Framework\Phrase('test'))
246+
),
247+
$sizePlaceholder
248+
);
249+
$this->imageHelper->expects($this->any())->method('getDefaultPlaceholderUrl')->willReturn($placeholderUrl);
250+
$this->imageHelper->expects($this->any())->method('getPlaceholder');
251+
$this->assetRepo->expects($this->any())->method('createAsset')->willReturnSelf();
252+
$this->assetRepo->expects($this->any())->method('getPath');
253+
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');
254+
255+
$this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
256+
}
147257
}

0 commit comments

Comments
 (0)