Skip to content

[2.2] Reworked gallery.phtml and include unit tests #17920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions app/code/Magento/Catalog/Block/Product/View/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,145 @@ public function getGalleryImagesJson()
return json_encode($imagesItems);
}

/**
* Retrieve gallery options in JSON format
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getGalleryOptionsJson()
{
$optionItems = null;

if ($this->getVar("gallery/nav")) {
$optionItems['nav'] = $this->escapeHtml($this->getVar("gallery/nav"));
}
if ($this->getVar("gallery/loop")) {
$optionItems['loop'] = filter_var($this->getVar("gallery/loop"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/keyboard")) {
$optionItems['keyboard'] = filter_var($this->getVar("gallery/keyboard"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/arrows")) {
$optionItems['arrows'] = filter_var($this->getVar("gallery/arrows"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/caption")) {
$optionItems['showCaption'] = filter_var($this->getVar("gallery/caption"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/allowfullscreen")) {
$optionItems['allowfullscreen'] = filter_var(
$this->getVar("gallery/allowfullscreen"),
FILTER_VALIDATE_BOOLEAN
);
}
if ($this->getVar("gallery/navdir")) {
$optionItems['navdir'] = $this->escapeHtml($this->getVar("gallery/navdir"));
}
if ($this->getVar("gallery/navarrows")) {
$optionItems['navarrows'] = filter_var($this->getVar("gallery/navarrows"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/navtype")) {
$optionItems['navtype'] = $this->escapeHtml($this->getVar("gallery/navtype"));
}
if ($this->getVar("gallery/thumbmargin")) {
$optionItems['thumbmargin'] = filter_var($this->getVar("gallery/thumbmargin"), FILTER_VALIDATE_INT);
}
if ($this->getVar("gallery/transition/effect")) {
$optionItems['transition'] = $this->escapeHtml($this->getVar("gallery/transition/effect"));
}
if ($this->getVar("gallery/transition/duration")) {
$optionItems['transitionduration'] = filter_var(
$this->getVar("gallery/transition/duration"),
FILTER_VALIDATE_INT
);
}

$optionItems['width'] = filter_var(
$this->getImageAttribute('product_page_image_medium', 'width'),
FILTER_VALIDATE_INT
);
$optionItems['thumbwidth'] = filter_var(
$this->getImageAttribute('product_page_image_small', 'width'),
FILTER_VALIDATE_INT
);
$imgHeight = $this->getImageAttribute('product_page_image_medium', 'height')
?: $this->getImageAttribute('product_page_image_medium', 'width');
if ($imgHeight) {
$optionItems['height'] = filter_var($imgHeight, FILTER_VALIDATE_INT);
}
$thumbHeight = $this->getImageAttribute('product_page_image_small', 'height')
?: $this->getImageAttribute('product_page_image_small', 'width');
if ($thumbHeight) {
$optionItems['thumbheight'] = filter_var($thumbHeight, FILTER_VALIDATE_INT);
}

return json_encode($optionItems);
}

/**
* Retrieve gallery fullscreen options in JSON format
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getGalleryFSOptionsJson()
{
$fsOptionItems = null;

if ($this->getVar("gallery/fullscreen/nav")) {
$fsOptionItems['nav'] = $this->escapeHtml($this->getVar("gallery/fullscreen/nav"));
}
if ($this->getVar("gallery/fullscreen/loop")) {
$fsOptionItems['loop'] = filter_var($this->getVar("gallery/fullscreen/loop"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/fullscreen/keyboard")) {
$fsOptionItems['keyboard'] = filter_var(
$this->getVar("gallery/fullscreen/keyboard"),
FILTER_VALIDATE_BOOLEAN
);
}
if ($this->getVar("gallery/fullscreen/arrows")) {
$fsOptionItems['arrows'] = filter_var($this->getVar("gallery/fullscreen/arrows"), FILTER_VALIDATE_BOOLEAN);
}
if ($this->getVar("gallery/fullscreen/caption")) {
$fsOptionItems['showCaption'] = filter_var(
$this->getVar("gallery/fullscreen/caption"),
FILTER_VALIDATE_BOOLEAN
);
}
if ($this->getVar("gallery/fullscreen/navdir")) {
$fsOptionItems['navdir'] = $this->escapeHtml($this->getVar("gallery/fullscreen/navdir"));
}
if ($this->getVar("gallery/fullscreen/navarrows")) {
$fsOptionItems['navarrows'] = filter_var(
$this->getVar("gallery/fullscreen/navarrows"),
FILTER_VALIDATE_BOOLEAN
);
}
if ($this->getVar("gallery/fullscreen/navtype")) {
$fsOptionItems['navtype'] = $this->escapeHtml($this->getVar("gallery/fullscreen/navtype"));
}
if ($this->getVar("gallery/fullscreen/thumbmargin")) {
$fsOptionItems['thumbmargin'] = filter_var(
$this->getVar("gallery/fullscreen/thumbmargin"),
FILTER_VALIDATE_INT
);
}
if ($this->getVar("gallery/fullscreen/transition/effect")) {
$fsOptionItems['transition'] = $this->escapeHtml($this->getVar("gallery/fullscreen/transition/effect"));
}
if ($this->getVar("gallery/fullscreen/transition/duration")) {
$fsOptionItems['transitionduration'] = filter_var(
$this->getVar("gallery/fullscreen/transition/duration"),
FILTER_VALIDATE_INT
);
}

return json_encode($fsOptionItems);
}

/**
* Retrieve gallery url
*
Expand Down
148 changes: 148 additions & 0 deletions app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Catalog\Test\Unit\Block\Product\View;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GalleryTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -37,6 +40,26 @@ class GalleryTest extends \PHPUnit\Framework\TestCase
*/
protected $jsonEncoderMock;

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

/**
* @var \Magento\Framework\Config\View
*/
protected $configView;

/**
* @var \Magento\Framework\View\Config
*/
protected $viewConfig;

/**
* @var \Magento\Framework\Escaper
*/
protected $escaper;

protected function setUp()
{
$this->mockContext();
Expand Down Expand Up @@ -75,6 +98,26 @@ protected function mockContext()
$this->context->expects($this->any())
->method('getRegistry')
->willReturn($this->registry);

$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->escaper = $this->objectManager->getObject(\Magento\Framework\Escaper::class);
$this->context->expects($this->any())
->method('getEscaper')
->willReturn($this->escaper);
$this->viewConfig = $this->getMockBuilder(\Magento\Framework\View\Config::class)
->disableOriginalConstructor()
->getMock();
$this->configView = $this->getMockBuilder(\Magento\Framework\Config\View::class)
->disableOriginalConstructor()
->getMock();

$this->viewConfig->expects($this->any())
->method('getViewConfig')
->willReturn($this->configView);
$this->context->expects($this->any())
->method('getViewConfig')
->willReturn($this->viewConfig);
}

public function testGetGalleryImagesJsonWithLabel()
Expand Down Expand Up @@ -262,4 +305,109 @@ private function getImagesCollectionWithPopulatedDataObject($hasLabel)

return $collectionMock;
}

public function testGalleryOptions()
{
$configMap = [
['Magento_Catalog', 'gallery/nav', 'thumbs'],
['Magento_Catalog', 'gallery/loop', 'false'],
['Magento_Catalog', 'gallery/keyboard', 'true'],
['Magento_Catalog', 'gallery/arrows', 'true'],
['Magento_Catalog', 'gallery/caption', 'false'],
['Magento_Catalog', 'gallery/allowfullscreen', 'true'],
['Magento_Catalog', 'gallery/navdir', 'horizontal'],
['Magento_Catalog', 'gallery/navarrows', 'true'],
['Magento_Catalog', 'gallery/navtype', 'slides'],
['Magento_Catalog', 'gallery/thumbmargin', '5'],
['Magento_Catalog', 'gallery/transition/effect', 'slide'],
['Magento_Catalog', 'gallery/transition/duration', '500'],
];

$mediaAttributesMap = [
[
'Magento_Catalog',
\Magento\Catalog\Helper\Image::MEDIA_TYPE_CONFIG_NODE,
'product_page_image_medium',
[
'height' => 100,
'width' => 200
]
],
[
'Magento_Catalog',
\Magento\Catalog\Helper\Image::MEDIA_TYPE_CONFIG_NODE,
'product_page_image_small',
[
'height' => 300,
'width' => 400
]
],
];

$this->configView->expects($this->any())
->method('getVarValue')
->will($this->returnValueMap($configMap));
$this->configView->expects($this->any())
->method('getMediaAttributes')
->will($this->returnValueMap($mediaAttributesMap));

$json = $this->model->getGalleryOptionsJson();
$decodedJson = json_decode($json, true);

$this->assertEquals('thumbs', $decodedJson['nav']);
$this->assertEquals(false, $decodedJson['loop']);
$this->assertEquals(true, $decodedJson['keyboard']);
$this->assertEquals(true, $decodedJson['arrows']);
$this->assertEquals(false, $decodedJson['showCaption']);
$this->assertEquals(true, $decodedJson['allowfullscreen']);
$this->assertEquals('horizontal', $decodedJson['navdir']);
$this->assertEquals(true, $decodedJson['navarrows']);
$this->assertEquals('slides', $decodedJson['navtype']);
$this->assertEquals(5, $decodedJson['thumbmargin']);
$this->assertEquals('slide', $decodedJson['transition']);
$this->assertEquals(500, $decodedJson['transitionduration']);
$this->assertEquals(100, $decodedJson['height']);
$this->assertEquals(200, $decodedJson['width']);
$this->assertEquals(300, $decodedJson['thumbheight']);
$this->assertEquals(400, $decodedJson['thumbwidth']);
}

public function testGalleryFSOptions()
{
$configMap = [
['Magento_Catalog', 'gallery/fullscreen/nav', 'false'],
['Magento_Catalog', 'gallery/fullscreen/loop', 'true'],
['Magento_Catalog', 'gallery/fullscreen/keyboard', 'false'],
['Magento_Catalog', 'gallery/fullscreen/arrows', 'false'],
['Magento_Catalog', 'gallery/fullscreen/caption', 'true'],
['Magento_Catalog', 'gallery/fullscreen/navdir', 'vertical'],
['Magento_Catalog', 'gallery/fullscreen/navarrows', 'false'],
['Magento_Catalog', 'gallery/fullscreen/navtype', 'thumbs'],
['Magento_Catalog', 'gallery/fullscreen/thumbmargin', '10'],
['Magento_Catalog', 'gallery/fullscreen/transition/effect', 'dissolve'],
['Magento_Catalog', 'gallery/fullscreen/transition/duration', '300']
];

$this->configView->expects($this->any())
->method('getVarValue')
->will($this->returnValueMap($configMap));

$json = $this->model->getGalleryFSOptionsJson();
$decodedJson = json_decode($json, true);

//Note, this tests the special case for nav variable set to false. It
//Should not be converted to boolean.
$this->assertEquals('false', $decodedJson['nav']);

$this->assertEquals(true, $decodedJson['loop']);
$this->assertEquals(false, $decodedJson['arrows']);
$this->assertEquals(false, $decodedJson['keyboard']);
$this->assertEquals(true, $decodedJson['showCaption']);
$this->assertEquals('vertical', $decodedJson['navdir']);
$this->assertEquals(false, $decodedJson['navarrows']);
$this->assertEquals(10, $decodedJson['thumbmargin']);
$this->assertEquals('thumbs', $decodedJson['navtype']);
$this->assertEquals('dissolve', $decodedJson['transition']);
$this->assertEquals(300, $decodedJson['transitionduration']);
}
}
Loading