Skip to content

Commit c0c7fe7

Browse files
Merge branch 'MC-30564' into 2.4-develop-com-pr4
2 parents 0e51710 + 205e976 commit c0c7fe7

7 files changed

+537
-24
lines changed

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/ConfigurableTest.php

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,78 @@
77

88
namespace Magento\ConfigurableProduct\Block\Product\View\Type;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1313
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection;
14+
use Magento\Framework\Api\SearchCriteriaBuilder;
1415
use Magento\Framework\ObjectManagerInterface;
1516
use Magento\Framework\Serialize\SerializerInterface;
1617
use Magento\Framework\View\LayoutInterface;
1718
use Magento\TestFramework\Helper\Bootstrap;
1819
use PHPUnit\Framework\TestCase;
1920

2021
/**
21-
* Test class to check configurable product view behaviour
22+
* Test class to check configurable product view behaviour.
2223
*
2324
* @see \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable
2425
*
2526
* @magentoAppIsolation enabled
27+
* @magentoDbIsolation enabled
2628
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
2729
*/
2830
class ConfigurableTest extends TestCase
2931
{
30-
/** @var ObjectManagerInterface */
32+
/**
33+
* @var ObjectManagerInterface
34+
*/
3135
private $objectManager;
3236

33-
/** @var Configurable */
34-
private $block;
37+
/**
38+
* @var SerializerInterface
39+
*/
40+
private $serializer;
3541

36-
/** @var Product */
37-
private $product;
42+
/**
43+
* @var SearchCriteriaBuilder
44+
*/
45+
private $searchBuilder;
3846

39-
/** @var LayoutInterface */
40-
private $layout;
47+
/**
48+
* @var Configurable
49+
*/
50+
private $block;
4151

42-
/** @var ProductRepositoryInterface */
52+
/**
53+
* @var ProductRepositoryInterface
54+
*/
4355
private $productRepository;
4456

45-
/** @var SerializerInterface */
46-
private $json;
47-
48-
/** @var ProductResource */
57+
/**
58+
* @var ProductResource
59+
*/
4960
private $productResource;
5061

62+
/**
63+
* @var ProductInterface
64+
*/
65+
private $product;
66+
5167
/**
5268
* @inheritdoc
5369
*/
5470
protected function setUp()
5571
{
72+
parent::setUp();
5673
$this->objectManager = Bootstrap::getObjectManager();
57-
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
74+
$this->serializer = $this->objectManager->get(SerializerInterface::class);
75+
$this->searchBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
76+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
77+
$this->productRepository->cleanCache();
78+
$this->productResource = $this->objectManager->create(ProductResource::class);
5879
$this->product = $this->productRepository->get('configurable');
59-
$this->layout = $this->objectManager->get(LayoutInterface::class);
60-
$this->block = $this->layout->createBlock(Configurable::class);
61-
$this->json = $this->objectManager->get(SerializerInterface::class);
80+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Configurable::class);
6281
$this->block->setProduct($this->product);
63-
$this->productResource = $this->objectManager->create(ProductResource::class);
6482
}
6583

6684
/**
@@ -89,7 +107,7 @@ public function testGetAllowProducts(): void
89107
$products = $this->block->getAllowProducts();
90108
$this->assertGreaterThanOrEqual(2, count($products));
91109
foreach ($products as $product) {
92-
$this->assertInstanceOf(Product::class, $product);
110+
$this->assertInstanceOf(ProductInterface::class, $product);
93111
}
94112
}
95113

@@ -98,14 +116,43 @@ public function testGetAllowProducts(): void
98116
*/
99117
public function testGetJsonConfig(): void
100118
{
101-
$config = $this->json->unserialize($this->block->getJsonConfig());
119+
$config = $this->serializer->unserialize($this->block->getJsonConfig());
102120
$this->assertNotEmpty($config);
103121
$this->assertArrayHasKey('productId', $config);
104122
$this->assertEquals(1, $config['productId']);
105123
$this->assertArrayHasKey('attributes', $config);
106124
$this->assertArrayHasKey('template', $config);
107125
$this->assertArrayHasKey('prices', $config);
108126
$this->assertArrayHasKey('basePrice', $config['prices']);
127+
$this->assertArrayHasKey('images', $config);
128+
$this->assertCount(0, $config['images']);
129+
}
130+
131+
/**
132+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_child_products_with_images.php
133+
* @return void
134+
*/
135+
public function testGetJsonConfigWithChildProductsImages(): void
136+
{
137+
$config = $this->serializer->unserialize($this->block->getJsonConfig());
138+
$this->assertNotEmpty($config);
139+
$this->assertArrayHasKey('images', $config);
140+
$this->assertCount(2, $config['images']);
141+
$products = $this->getProducts(
142+
$this->product->getExtensionAttributes()->getConfigurableProductLinks()
143+
);
144+
$i = 0;
145+
foreach ($products as $simpleProduct) {
146+
$i++;
147+
$resultImage = reset($config['images'][$simpleProduct->getId()]);
148+
$this->assertContains($simpleProduct->getImage(), $resultImage['thumb']);
149+
$this->assertContains($simpleProduct->getImage(), $resultImage['img']);
150+
$this->assertContains($simpleProduct->getImage(), $resultImage['full']);
151+
$this->assertTrue($resultImage['isMain']);
152+
$this->assertEquals('image', $resultImage['type']);
153+
$this->assertEquals($i, $resultImage['position']);
154+
$this->assertNull($resultImage['videoUrl']);
155+
}
109156
}
110157

111158
/**
@@ -121,7 +168,7 @@ public function testConfigurableProductView(string $label, array $expectedConfig
121168
$this->assertCount(1, $attributes);
122169
$attribute = $attributes->getFirstItem();
123170
$this->assertEquals($label, $attribute->getLabel());
124-
$config = $this->json->unserialize($this->block->getJsonConfig())['attributes'] ?? null;
171+
$config = $this->serializer->unserialize($this->block->getJsonConfig())['attributes'] ?? null;
125172
$this->assertNotNull($config);
126173
$this->assertConfig(reset($config), $expectedConfig);
127174
}
@@ -158,7 +205,7 @@ public function expectedDataProvider(): array
158205
* @param array $expectedData
159206
* @return void
160207
*/
161-
private function assertConfig($data, $expectedData): void
208+
private function assertConfig(array $data, array $expectedData): void
162209
{
163210
$this->assertEquals($expectedData['label'], $data['label']);
164211
$skus = array_column($expectedData['options'], 'sku');
@@ -175,4 +222,17 @@ private function assertConfig($data, $expectedData): void
175222
}
176223
}
177224
}
225+
226+
/**
227+
* Returns products by ids list.
228+
*
229+
* @param array $productIds
230+
* @return ProductInterface[]
231+
*/
232+
private function getProducts(array $productIds): array
233+
{
234+
$criteria = $this->searchBuilder->addFilter('entity_id', $productIds, 'in')
235+
->create();
236+
return $this->productRepository->getList($criteria)->getItems();
237+
}
178238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Store\Model\Store;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
require __DIR__ . '/../../../Magento/Catalog/_files/product_image.php';
14+
require __DIR__ . '/product_configurable.php';
15+
16+
$objectManager = Bootstrap::getObjectManager();
17+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
18+
$firstSimple = $productRepository->get('simple_10');
19+
$secondSimple = $productRepository->get('simple_20');
20+
/** @var $firstSimple Product */
21+
$firstSimple->setStoreId(Store::DEFAULT_STORE_ID)
22+
->setImage('/m/a/magento_image.jpg')
23+
->setSmallImage('/m/a/magento_image.jpg')
24+
->setThumbnail('/m/a/magento_image.jpg')
25+
->setData(
26+
'media_gallery',
27+
[
28+
'images' => [
29+
[
30+
'file' => '/m/a/magento_image.jpg',
31+
'position' => 1,
32+
'label' => 'Image Alt Text',
33+
'disabled' => 0,
34+
'media_type' => 'image'
35+
],
36+
]
37+
]
38+
)
39+
->setCanSaveCustomOptions(true)
40+
->save();
41+
/** @var $secondSimple Product */
42+
$secondSimple->setStoreId(Store::DEFAULT_STORE_ID)
43+
->setImage('/m/a/magento_thumbnail.jpg')
44+
->setSmallImage('/m/a/magento_thumbnail.jpg')
45+
->setThumbnail('/m/a/magento_thumbnail.jpg')
46+
->setSwatchImage('/m/a/magento_thumbnail.jpg')
47+
->setData(
48+
'media_gallery',
49+
[
50+
'images' => [
51+
[
52+
'file' => '/m/a/magento_thumbnail.jpg',
53+
'position' => 2,
54+
'label' => 'Thumbnail Image',
55+
'disabled' => 0,
56+
'media_type' => 'image'
57+
],
58+
]
59+
]
60+
)
61+
->setCanSaveCustomOptions(true)
62+
->save();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
require __DIR__ . '/product_configurable_rollback.php';
9+
require __DIR__ . '/../../../Magento/Catalog/_files/product_image_rollback.php';

0 commit comments

Comments
 (0)