|
6 | 6 | namespace Magento\Catalog\Test\Unit\Block\Adminhtml\Product\Helper\Form\Gallery;
|
7 | 7 |
|
8 | 8 | use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content;
|
9 |
| -use Magento\Framework\Filesystem; |
| 9 | +use Magento\Catalog\Model\Entity\Attribute; |
| 10 | +use Magento\Catalog\Model\Product; |
10 | 11 | use Magento\Framework\Phrase;
|
11 | 12 |
|
| 13 | +/** |
| 14 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 15 | + */ |
12 | 16 | class ContentTest extends \PHPUnit\Framework\TestCase
|
13 | 17 | {
|
14 | 18 | /**
|
@@ -219,4 +223,146 @@ public function testGetImagesJsonWithException()
|
219 | 223 |
|
220 | 224 | $this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
|
221 | 225 | }
|
| 226 | + |
| 227 | + /** |
| 228 | + * Test GetImageTypes() will return value for given attribute from data persistor. |
| 229 | + * |
| 230 | + * @return void |
| 231 | + */ |
| 232 | + public function testGetImageTypesFromDataPersistor() |
| 233 | + { |
| 234 | + $attributeCode = 'thumbnail'; |
| 235 | + $value = 'testImageValue'; |
| 236 | + $scopeLabel = 'testScopeLabel'; |
| 237 | + $label = 'testLabel'; |
| 238 | + $name = 'testName'; |
| 239 | + $expectedTypes = [ |
| 240 | + $attributeCode => [ |
| 241 | + 'code' => $attributeCode, |
| 242 | + 'value' => $value, |
| 243 | + 'label' => $label, |
| 244 | + 'name' => $name, |
| 245 | + ], |
| 246 | + ]; |
| 247 | + $product = $this->getMockBuilder(Product::class) |
| 248 | + ->disableOriginalConstructor() |
| 249 | + ->getMock(); |
| 250 | + $product->expects($this->once()) |
| 251 | + ->method('getData') |
| 252 | + ->with($this->identicalTo($attributeCode)) |
| 253 | + ->willReturn(null); |
| 254 | + $mediaAttribute = $this->getMediaAttribute($label, $attributeCode); |
| 255 | + $product->expects($this->once()) |
| 256 | + ->method('getMediaAttributes') |
| 257 | + ->willReturn([$mediaAttribute]); |
| 258 | + $this->galleryMock->expects($this->exactly(2)) |
| 259 | + ->method('getDataObject') |
| 260 | + ->willReturn($product); |
| 261 | + $this->galleryMock->expects($this->once()) |
| 262 | + ->method('getImageValue') |
| 263 | + ->with($this->identicalTo($attributeCode)) |
| 264 | + ->willReturn($value); |
| 265 | + $this->galleryMock->expects($this->once()) |
| 266 | + ->method('getScopeLabel') |
| 267 | + ->with($this->identicalTo($mediaAttribute)) |
| 268 | + ->willReturn($scopeLabel); |
| 269 | + $this->galleryMock->expects($this->once()) |
| 270 | + ->method('getAttributeFieldName') |
| 271 | + ->with($this->identicalTo($mediaAttribute)) |
| 272 | + ->willReturn($name); |
| 273 | + $this->getImageTypesAssertions($attributeCode, $scopeLabel, $expectedTypes); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Test GetImageTypes() will return value for given attribute from product. |
| 278 | + * |
| 279 | + * @return void |
| 280 | + */ |
| 281 | + public function testGetImageTypesFromProduct() |
| 282 | + { |
| 283 | + $attributeCode = 'thumbnail'; |
| 284 | + $value = 'testImageValue'; |
| 285 | + $scopeLabel = 'testScopeLabel'; |
| 286 | + $label = 'testLabel'; |
| 287 | + $name = 'testName'; |
| 288 | + $expectedTypes = [ |
| 289 | + $attributeCode => [ |
| 290 | + 'code' => $attributeCode, |
| 291 | + 'value' => $value, |
| 292 | + 'label' => $label, |
| 293 | + 'name' => $name, |
| 294 | + ], |
| 295 | + ]; |
| 296 | + $product = $this->getMockBuilder(Product::class) |
| 297 | + ->disableOriginalConstructor() |
| 298 | + ->getMock(); |
| 299 | + $product->expects($this->once()) |
| 300 | + ->method('getData') |
| 301 | + ->with($this->identicalTo($attributeCode)) |
| 302 | + ->willReturn($value); |
| 303 | + $mediaAttribute = $this->getMediaAttribute($label, $attributeCode); |
| 304 | + $product->expects($this->once()) |
| 305 | + ->method('getMediaAttributes') |
| 306 | + ->willReturn([$mediaAttribute]); |
| 307 | + $this->galleryMock->expects($this->exactly(2)) |
| 308 | + ->method('getDataObject') |
| 309 | + ->willReturn($product); |
| 310 | + $this->galleryMock->expects($this->never()) |
| 311 | + ->method('getImageValue'); |
| 312 | + $this->galleryMock->expects($this->once()) |
| 313 | + ->method('getScopeLabel') |
| 314 | + ->with($this->identicalTo($mediaAttribute)) |
| 315 | + ->willReturn($scopeLabel); |
| 316 | + $this->galleryMock->expects($this->once()) |
| 317 | + ->method('getAttributeFieldName') |
| 318 | + ->with($this->identicalTo($mediaAttribute)) |
| 319 | + ->willReturn($name); |
| 320 | + $this->getImageTypesAssertions($attributeCode, $scopeLabel, $expectedTypes); |
| 321 | + } |
| 322 | + |
| 323 | + /** |
| 324 | + * Perform assertions. |
| 325 | + * |
| 326 | + * @param string $attributeCode |
| 327 | + * @param string $scopeLabel |
| 328 | + * @param array $expectedTypes |
| 329 | + * @return void |
| 330 | + */ |
| 331 | + private function getImageTypesAssertions(string $attributeCode, string $scopeLabel, array $expectedTypes) |
| 332 | + { |
| 333 | + $this->content->setElement($this->galleryMock); |
| 334 | + $result = $this->content->getImageTypes(); |
| 335 | + $scope = $result[$attributeCode]['scope']; |
| 336 | + $this->assertSame($scopeLabel, $scope->getText()); |
| 337 | + unset($result[$attributeCode]['scope']); |
| 338 | + $this->assertSame($expectedTypes, $result); |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Get media attribute mock. |
| 343 | + * |
| 344 | + * @param string $label |
| 345 | + * @param string $attributeCode |
| 346 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 347 | + */ |
| 348 | + private function getMediaAttribute(string $label, string $attributeCode) |
| 349 | + { |
| 350 | + $frontend = $this->getMockBuilder(Product\Attribute\Frontend\Image::class) |
| 351 | + ->disableOriginalConstructor() |
| 352 | + ->getMock(); |
| 353 | + $frontend->expects($this->once()) |
| 354 | + ->method('getLabel') |
| 355 | + ->willReturn($label); |
| 356 | + $mediaAttribute = $this->getMockBuilder(Attribute::class) |
| 357 | + ->disableOriginalConstructor() |
| 358 | + ->getMock(); |
| 359 | + $mediaAttribute->expects($this->any()) |
| 360 | + ->method('getAttributeCode') |
| 361 | + ->willReturn($attributeCode); |
| 362 | + $mediaAttribute->expects($this->once()) |
| 363 | + ->method('getFrontend') |
| 364 | + ->willReturn($frontend); |
| 365 | + |
| 366 | + return $mediaAttribute; |
| 367 | + } |
222 | 368 | }
|
0 commit comments