|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Block\Product\View; |
| 7 | + |
| 8 | +use Magento\Catalog\Block\Product\Context; |
| 9 | +use Magento\Catalog\Block\Product\View\Gallery; |
| 10 | +use Magento\Catalog\Block\Product\View\GalleryOptions; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Framework\Escaper; |
| 13 | +use Magento\Framework\View\Config; |
| 14 | +use Magento\Framework\Config\View; |
| 15 | +use Magento\Framework\Serialize\Serializer\Json; |
| 16 | + |
| 17 | +/** |
| 18 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 19 | + */ |
| 20 | +class GalleryOptionsTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var GalleryOptions |
| 24 | + */ |
| 25 | + private $model; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Gallery|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $gallery; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $context; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Json |
| 39 | + */ |
| 40 | + private $jsonSerializer; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var View|\PHPUnit_Framework_MockObject_MockObject |
| 44 | + */ |
| 45 | + private $configView; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Config|\PHPUnit_Framework_MockObject_MockObject |
| 49 | + */ |
| 50 | + private $viewConfig; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var Escaper |
| 54 | + */ |
| 55 | + private $escaper; |
| 56 | + |
| 57 | + protected function setUp() |
| 58 | + { |
| 59 | + $objectManager = new ObjectManager($this); |
| 60 | + |
| 61 | + $this->escaper = $objectManager->getObject(Escaper::class); |
| 62 | + $this->configView = $this->createMock(View::class); |
| 63 | + |
| 64 | + $this->viewConfig = $this->createConfiguredMock( |
| 65 | + Config::class, |
| 66 | + [ |
| 67 | + 'getViewConfig' => $this->configView |
| 68 | + ] |
| 69 | + ); |
| 70 | + |
| 71 | + $this->context = $this->createConfiguredMock( |
| 72 | + Context::class, |
| 73 | + [ |
| 74 | + 'getEscaper' => $this->escaper, |
| 75 | + 'getViewConfig' => $this->viewConfig |
| 76 | + ] |
| 77 | + ); |
| 78 | + |
| 79 | + $this->gallery = $this->createMock(Gallery::class); |
| 80 | + |
| 81 | + $this->jsonSerializer = $objectManager->getObject( |
| 82 | + Json::class |
| 83 | + ); |
| 84 | + |
| 85 | + $this->model = $objectManager->getObject(GalleryOptions::class, [ |
| 86 | + 'context' => $this->context, |
| 87 | + 'jsonSerializer' => $this->jsonSerializer, |
| 88 | + 'gallery' => $this->gallery |
| 89 | + ]); |
| 90 | + } |
| 91 | + |
| 92 | + public function testGetOptionsJson() |
| 93 | + { |
| 94 | + $configMap = [ |
| 95 | + ['Magento_Catalog', 'gallery/nav', 'thumbs'], |
| 96 | + ['Magento_Catalog', 'gallery/loop', false], |
| 97 | + ['Magento_Catalog', 'gallery/keyboard', true], |
| 98 | + ['Magento_Catalog', 'gallery/arrows', true], |
| 99 | + ['Magento_Catalog', 'gallery/caption', false], |
| 100 | + ['Magento_Catalog', 'gallery/allowfullscreen', true], |
| 101 | + ['Magento_Catalog', 'gallery/navdir', 'horizontal'], |
| 102 | + ['Magento_Catalog', 'gallery/navarrows', true], |
| 103 | + ['Magento_Catalog', 'gallery/navtype', 'slides'], |
| 104 | + ['Magento_Catalog', 'gallery/thumbmargin', '5'], |
| 105 | + ['Magento_Catalog', 'gallery/transition/effect', 'slide'], |
| 106 | + ['Magento_Catalog', 'gallery/transition/duration', '500'], |
| 107 | + ]; |
| 108 | + |
| 109 | + $imageAttributesMap = [ |
| 110 | + ['product_page_image_medium','height',null, 100], |
| 111 | + ['product_page_image_medium','width',null, 200], |
| 112 | + ['product_page_image_small','height',null, 300], |
| 113 | + ['product_page_image_small','width',null, 400] |
| 114 | + ]; |
| 115 | + |
| 116 | + $this->configView->expects($this->any()) |
| 117 | + ->method('getVarValue') |
| 118 | + ->will($this->returnValueMap($configMap)); |
| 119 | + $this->gallery->expects($this->any()) |
| 120 | + ->method('getImageAttribute') |
| 121 | + ->will($this->returnValueMap($imageAttributesMap)); |
| 122 | + |
| 123 | + $json = $this->model->getOptionsJson(); |
| 124 | + |
| 125 | + $decodedJson = $this->jsonSerializer->unserialize($json); |
| 126 | + |
| 127 | + $this->assertSame('thumbs', $decodedJson['nav']); |
| 128 | + $this->assertSame(false, $decodedJson['loop']); |
| 129 | + $this->assertSame(true, $decodedJson['keyboard']); |
| 130 | + $this->assertSame(true, $decodedJson['arrows']); |
| 131 | + $this->assertSame(false, $decodedJson['showCaption']); |
| 132 | + $this->assertSame(true, $decodedJson['allowfullscreen']); |
| 133 | + $this->assertSame('horizontal', $decodedJson['navdir']); |
| 134 | + $this->assertSame(true, $decodedJson['navarrows']); |
| 135 | + $this->assertSame('slides', $decodedJson['navtype']); |
| 136 | + $this->assertSame(5, $decodedJson['thumbmargin']); |
| 137 | + $this->assertSame('slide', $decodedJson['transition']); |
| 138 | + $this->assertSame(500, $decodedJson['transitionduration']); |
| 139 | + $this->assertSame(100, $decodedJson['height']); |
| 140 | + $this->assertSame(200, $decodedJson['width']); |
| 141 | + $this->assertSame(300, $decodedJson['thumbheight']); |
| 142 | + $this->assertSame(400, $decodedJson['thumbwidth']); |
| 143 | + } |
| 144 | + |
| 145 | + public function testGetFSOptionsJson() |
| 146 | + { |
| 147 | + $configMap = [ |
| 148 | + ['Magento_Catalog', 'gallery/fullscreen/nav', false], |
| 149 | + ['Magento_Catalog', 'gallery/fullscreen/loop', true], |
| 150 | + ['Magento_Catalog', 'gallery/fullscreen/keyboard', true], |
| 151 | + ['Magento_Catalog', 'gallery/fullscreen/arrows', false], |
| 152 | + ['Magento_Catalog', 'gallery/fullscreen/caption', true], |
| 153 | + ['Magento_Catalog', 'gallery/fullscreen/navdir', 'vertical'], |
| 154 | + ['Magento_Catalog', 'gallery/fullscreen/navarrows', false], |
| 155 | + ['Magento_Catalog', 'gallery/fullscreen/navtype', 'thumbs'], |
| 156 | + ['Magento_Catalog', 'gallery/fullscreen/thumbmargin', '10'], |
| 157 | + ['Magento_Catalog', 'gallery/fullscreen/transition/effect', 'dissolve'], |
| 158 | + ['Magento_Catalog', 'gallery/fullscreen/transition/duration', '300'] |
| 159 | + ]; |
| 160 | + |
| 161 | + $this->configView->expects($this->any()) |
| 162 | + ->method('getVarValue') |
| 163 | + ->will($this->returnValueMap($configMap)); |
| 164 | + |
| 165 | + $json = $this->model->getFSOptionsJson(); |
| 166 | + |
| 167 | + $decodedJson = $this->jsonSerializer->unserialize($json); |
| 168 | + |
| 169 | + //Note, this tests the special case for nav variable set to false. It |
| 170 | + //Should not be converted to boolean. |
| 171 | + $this->assertSame('false', $decodedJson['nav']); |
| 172 | + $this->assertSame(true, $decodedJson['loop']); |
| 173 | + $this->assertSame(false, $decodedJson['arrows']); |
| 174 | + $this->assertSame(true, $decodedJson['keyboard']); |
| 175 | + $this->assertSame(true, $decodedJson['showCaption']); |
| 176 | + $this->assertSame('vertical', $decodedJson['navdir']); |
| 177 | + $this->assertSame(false, $decodedJson['navarrows']); |
| 178 | + $this->assertSame(10, $decodedJson['thumbmargin']); |
| 179 | + $this->assertSame('thumbs', $decodedJson['navtype']); |
| 180 | + $this->assertSame('dissolve', $decodedJson['transition']); |
| 181 | + $this->assertSame(300, $decodedJson['transitionduration']); |
| 182 | + } |
| 183 | + |
| 184 | + public function testGetOptionsJsonOptionals() |
| 185 | + { |
| 186 | + $configMap = [ |
| 187 | + ['Magento_Catalog', 'gallery/fullscreen/thumbmargin', false], |
| 188 | + ['Magento_Catalog', 'gallery/fullscreen/transition/duration', false] |
| 189 | + ]; |
| 190 | + |
| 191 | + $this->configView->expects($this->any()) |
| 192 | + ->method('getVarValue') |
| 193 | + ->will($this->returnValueMap($configMap)); |
| 194 | + |
| 195 | + $json = $this->model->getOptionsJson(); |
| 196 | + |
| 197 | + $decodedJson = $this->jsonSerializer->unserialize($json); |
| 198 | + |
| 199 | + $this->assertArrayNotHasKey('thumbmargin', $decodedJson); |
| 200 | + $this->assertArrayNotHasKey('transitionduration', $decodedJson); |
| 201 | + } |
| 202 | + |
| 203 | + public function testGetFSOptionsJsonOptionals() |
| 204 | + { |
| 205 | + $configMap = [ |
| 206 | + ['Magento_Catalog', 'gallery/fullscreen/keyboard', false], |
| 207 | + ['Magento_Catalog', 'gallery/fullscreen/thumbmargin', false], |
| 208 | + ['Magento_Catalog', 'gallery/fullscreen/transition/duration', false] |
| 209 | + ]; |
| 210 | + |
| 211 | + $this->configView->expects($this->any()) |
| 212 | + ->method('getVarValue') |
| 213 | + ->will($this->returnValueMap($configMap)); |
| 214 | + |
| 215 | + $json = $this->model->getFSOptionsJson(); |
| 216 | + |
| 217 | + $decodedJson = $this->jsonSerializer->unserialize($json); |
| 218 | + |
| 219 | + $this->assertArrayNotHasKey('thumbmargin', $decodedJson); |
| 220 | + $this->assertArrayNotHasKey('keyboard', $decodedJson); |
| 221 | + $this->assertArrayNotHasKey('transitionduration', $decodedJson); |
| 222 | + } |
| 223 | +} |
0 commit comments