Skip to content

Commit

Permalink
MAGETWO-55447: Portdown MAGETWO-54718 down to M2.1.x branch
Browse files Browse the repository at this point in the history
- MAGETWO-54718: [GitHub] Exception thrown where no Product Image file found #5184

- (cherry picked from commit 3459ef1)
  • Loading branch information
oserediuk authored and vzabaznov committed Sep 26, 2016
1 parent 7e9f653 commit 7a900b8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,18 @@ public function getImagesJson()
is_array($value['images']) &&
count($value['images'])
) {
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
$staticDir = $this->_filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
$images = $this->sortImagesByPosition($value['images']);
foreach ($images as &$image) {
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
try {
$fileHandler = $mediaDir->stat($this->_mediaConfig->getMediaPath($image['file']));
$image['size'] = $fileHandler['size'];
} catch (\Exception $e) {
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('image');
} catch (\Magento\Framework\Exception\FileSystemException $e) {
$staticDir = $this->_filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
$image['url'] = $this->getImageHelper()->getDefaultPlaceholderUrl('thumbnail');
$fileHandler = $staticDir->stat(
$this->getAssetRepo()->createAsset($this->getImageHelper()->getPlaceholder('image'))->getPath()
$this->getAssetRepo()->createAsset($this->getImageHelper()->getPlaceholder('thumbnail'))->getPath()
);
$image['size'] = $fileHandler['size'];
$this->_logger->warning($e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Framework\Filesystem;
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content;
use Magento\Framework\Phrase;

class ContentTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -40,14 +41,30 @@ class ContentTest extends \PHPUnit_Framework_TestCase
*/
protected $galleryMock;

/**
* @var \Magento\Catalog\Helper\Image|\PHPUnit_Framework_MockObject_MockObject
*/
protected $imageHelper;

/**
* @var \Magento\Framework\View\Asset\Repository|\PHPUnit_Framework_MockObject_MockObject
*/
protected $assetRepo;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
protected $objectManager;

public function setUp()
{
$this->fileSystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
$this->fileSystemMock = $this->getMock(
'Magento\Framework\Filesystem',
['stat', 'getDirectoryRead'],
[],
'',
false
);
$this->readMock = $this->getMock('Magento\Framework\Filesystem\Directory\ReadInterface');
$this->galleryMock = $this->getMock(
'Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery',
Expand All @@ -56,7 +73,13 @@ public function setUp()
'',
false
);
$this->mediaConfigMock = $this->getMock('Magento\Catalog\Model\Product\Media\Config', [], [], '', false);
$this->mediaConfigMock = $this->getMock(
'Magento\Catalog\Model\Product\Media\Config',
['getMediaUrl', 'getMediaPath'],
[],
'',
false
);
$this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface')
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -126,11 +149,10 @@ public function testGetImagesJson()

$this->content->setElement($this->galleryMock);
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
$this->fileSystemMock->expects($this->exactly(2))->method('getDirectoryRead')->willReturn($this->readMock);
$this->fileSystemMock->expects($this->once())->method('getDirectoryRead')->willReturn($this->readMock);

$this->mediaConfigMock->expects($this->any())->method('getMediaUrl')->willReturnMap($url);
$this->mediaConfigMock->expects($this->any())->method('getMediaPath')->willReturnMap($mediaPath);

$this->readMock->expects($this->any())->method('stat')->willReturnMap($sizeMap);
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');

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

$this->assertSame('[]', $this->content->getImagesJson());
}

public function testGetImagesJsonWithException()
{
$this->imageHelper = $this->getMockBuilder('Magento\Catalog\Helper\Image')
->disableOriginalConstructor()
->setMethods(['getDefaultPlaceholderUrl', 'getPlaceholder'])
->getMock();

$this->assetRepo = $this->getMockBuilder('Magento\Framework\View\Asset\Repository')
->disableOriginalConstructor()
->setMethods(['createAsset', 'getPath'])
->getMock();

$this->objectManager->setBackwardCompatibleProperty(
$this->content,
'imageHelper',
$this->imageHelper
);

$this->objectManager->setBackwardCompatibleProperty(
$this->content,
'assetRepo',
$this->assetRepo
);

$placeholderUrl = 'url_to_the_placeholder/placeholder.jpg';

$sizePlaceholder = ['size' => 399659];

$imagesResult = [
[
'value_id' => '2',
'file' => 'file_2.jpg',
'media_type' => 'image',
'position' => '0',
'url' => 'url_to_the_placeholder/placeholder.jpg',
'size' => 399659
],
[
'value_id' => '1',
'file' => 'file_1.jpg',
'media_type' => 'image',
'position' => '1',
'url' => 'url_to_the_placeholder/placeholder.jpg',
'size' => 399659
]
];

$images = [
'images' => [
[
'value_id' => '1',
'file' => 'file_1.jpg',
'media_type' => 'image',
'position' => '1'
],
[
'value_id' => '2',
'file' => 'file_2.jpg',
'media_type' => 'image',
'position' => '0'
]
]
];

$this->content->setElement($this->galleryMock);
$this->galleryMock->expects($this->once())->method('getImages')->willReturn($images);
$this->fileSystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($this->readMock);
$this->mediaConfigMock->expects($this->any())->method('getMediaUrl');
$this->mediaConfigMock->expects($this->any())->method('getMediaPath');
$this->readMock->expects($this->any())->method('stat')->willReturnOnConsecutiveCalls(
$this->throwException(
new \Magento\Framework\Exception\FileSystemException(new \Magento\Framework\Phrase('test'))
),
$sizePlaceholder,
$this->throwException(
new \Magento\Framework\Exception\FileSystemException(new \Magento\Framework\Phrase('test'))
),
$sizePlaceholder
);
$this->imageHelper->expects($this->any())->method('getDefaultPlaceholderUrl')->willReturn($placeholderUrl);
$this->imageHelper->expects($this->any())->method('getPlaceholder');
$this->assetRepo->expects($this->any())->method('createAsset')->willReturnSelf();
$this->assetRepo->expects($this->any())->method('getPath');
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');

$this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
}
}

0 comments on commit 7a900b8

Please sign in to comment.