Skip to content

Commit be308fa

Browse files
author
Gabriel da Gama
authored
Merge branch '2.4-develop' into mhidalgo/fix-handlers-not-being-merged-correctly
2 parents 0cfb88a + e9fa180 commit be308fa

File tree

76 files changed

+1501
-372
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1501
-372
lines changed

app/code/Magento/Bundle/Model/Plugin/Frontend/Product.php renamed to app/code/Magento/Bundle/Model/Plugin/Frontend/ProductIdentitiesExtender.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77

88
namespace Magento\Bundle\Model\Plugin\Frontend;
99

10-
use Magento\Bundle\Model\Product\Type;
10+
use Magento\Bundle\Model\Product\Type as BundleType;
1111
use Magento\Catalog\Model\Product as CatalogProduct;
1212

1313
/**
1414
* Add child identities to product identities on storefront.
1515
*/
16-
class Product
16+
class ProductIdentitiesExtender
1717
{
1818
/**
19-
* @var Type
19+
* @var BundleType
2020
*/
2121
private $type;
2222

2323
/**
24-
* @param Type $type
24+
* @var array
2525
*/
26-
public function __construct(Type $type)
26+
private $cacheChildrenIds = [];
27+
28+
/**
29+
* @param BundleType $type
30+
*/
31+
public function __construct(BundleType $type)
2732
{
2833
$this->type = $type;
2934
}
@@ -37,12 +42,30 @@ public function __construct(Type $type)
3742
*/
3843
public function afterGetIdentities(CatalogProduct $product, array $identities): array
3944
{
40-
foreach ($this->type->getChildrenIds($product->getEntityId()) as $childIds) {
45+
if ($product->getTypeId() !== BundleType::TYPE_CODE) {
46+
return $identities;
47+
}
48+
foreach ($this->getChildrenIds($product->getEntityId()) as $childIds) {
4149
foreach ($childIds as $childId) {
4250
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId;
4351
}
4452
}
4553

4654
return array_unique($identities);
4755
}
56+
57+
/**
58+
* Get children ids with cache use
59+
*
60+
* @param mixed $entityId
61+
* @return array
62+
*/
63+
private function getChildrenIds($entityId): array
64+
{
65+
if (!isset($this->cacheChildrenIds[$entityId])) {
66+
$this->cacheChildrenIds[$entityId] = $this->type->getChildrenIds($entityId);
67+
}
68+
69+
return $this->cacheChildrenIds[$entityId];
70+
}
4871
}

app/code/Magento/Bundle/Model/Plugin/Product.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
namespace Magento\Bundle\Model\Plugin;
9+
10+
use Magento\Bundle\Model\Product\Type as BundleType;
11+
use Magento\Catalog\Model\Product as CatalogProduct;
12+
13+
/**
14+
* Add parent identities to product identities.
15+
*/
16+
class ProductIdentitiesExtender
17+
{
18+
/**
19+
* @var BundleType
20+
*/
21+
private $type;
22+
23+
/**
24+
* @var array
25+
*/
26+
private $cacheParentIdsByChild = [];
27+
28+
/**
29+
* @param BundleType $type
30+
*/
31+
public function __construct(BundleType $type)
32+
{
33+
$this->type = $type;
34+
}
35+
36+
/**
37+
* Add parent identities to product identities
38+
*
39+
* @param CatalogProduct $product
40+
* @param array $identities
41+
* @return string[]
42+
*/
43+
public function afterGetIdentities(
44+
CatalogProduct $product,
45+
array $identities
46+
) {
47+
if ($product->getTypeId() !== BundleType::TYPE_CODE) {
48+
return $identities;
49+
}
50+
foreach ($this->getParentIdsByChild($product->getEntityId()) as $parentId) {
51+
$identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId;
52+
}
53+
54+
return $identities;
55+
}
56+
57+
/**
58+
* Get parent ids by child with cache use
59+
*
60+
* @param mixed $entityId
61+
* @return array
62+
*/
63+
private function getParentIdsByChild($entityId): array
64+
{
65+
if (!isset($this->cacheParentIdsByChild[$entityId])) {
66+
$this->cacheParentIdsByChild[$entityId] = $this->type->getParentIdsByChild($entityId);
67+
}
68+
69+
return $this->cacheParentIdsByChild[$entityId];
70+
}
71+
}

app/code/Magento/Bundle/Test/Mftf/Test/NewProductsListWidgetBundleProductTest.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
<!-- Create a product to appear in the widget, fill in basic info first -->
3737

3838
<actionGroup ref="AdminOpenProductIndexPageActionGroup" stepKey="amOnProductList"/>
39-
<click selector="{{AdminProductGridActionSection.addProductToggle}}" stepKey="clickAddProductToggle"/>
40-
<click selector="{{AdminProductGridActionSection.addBundleProduct}}" stepKey="clickAddBundleProduct"/>
39+
<comment userInput="Adding the comment to replace clickAddProductToggle action for preserving Backward Compatibility" stepKey="clickAddProductToggle"/>
40+
<actionGroup ref="AdminClickAddProductToggleAndSelectProductTypeActionGroup" stepKey="clickAddBundleProduct">
41+
<argument name="productType" value="bundle"/>
42+
</actionGroup>
4143
<fillField selector="{{AdminProductFormSection.productName}}" userInput="{{_defaultProduct.name}}" stepKey="fillProductName"/>
4244
<fillField selector="{{AdminProductFormSection.productSku}}" userInput="{{_defaultProduct.sku}}" stepKey="fillProductSku"/>
4345
<fillField selector="{{AdminProductFormSection.setProductAsNewFrom}}" userInput="01/1/2000" stepKey="fillProductNewFrom"/>

app/code/Magento/Bundle/Test/Unit/Model/Plugin/Frontend/ProductTest.php renamed to app/code/Magento/Bundle/Test/Unit/Model/Plugin/Frontend/ProductIdentitiesExtenderTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,34 @@
77

88
namespace Magento\Bundle\Test\Unit\Model\Plugin\Frontend;
99

10-
use Magento\Bundle\Model\Plugin\Frontend\Product as ProductPlugin;
10+
use Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender as ProductPlugin;
1111
use Magento\Bundle\Model\Product\Type;
1212
use Magento\Catalog\Model\Product;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
1515

16-
class ProductTest extends TestCase
16+
/**
17+
* Unit test for \Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender
18+
*/
19+
class ProductIdentitiesExtenderTest extends TestCase
1720
{
18-
/** @var \Magento\Bundle\Model\Plugin\Product */
21+
/** @var ProductPlugin */
1922
private $plugin;
2023

21-
/** @var MockObject|Type */
24+
/** @var MockObject|Type */
2225
private $type;
2326

24-
/** @var MockObject|\Magento\Catalog\Model\Product */
27+
/** @var MockObject|Product */
2528
private $product;
2629

30+
/**
31+
* @inheritdoc
32+
*/
2733
protected function setUp(): void
2834
{
2935
$this->product = $this->getMockBuilder(Product::class)
3036
->disableOriginalConstructor()
31-
->setMethods(['getEntityId'])
37+
->setMethods(['getEntityId', 'getTypeId'])
3238
->getMock();
3339

3440
$this->type = $this->getMockBuilder(Type::class)
@@ -39,7 +45,7 @@ protected function setUp(): void
3945
$this->plugin = new ProductPlugin($this->type);
4046
}
4147

42-
public function testAfterGetIdentities()
48+
public function testAfterGetIdentities(): void
4349
{
4450
$baseIdentities = [
4551
'SomeCacheId',
@@ -62,14 +68,24 @@ public function testAfterGetIdentities()
6268
Product::CACHE_TAG . '_' . 45,
6369
Product::CACHE_TAG . '_' . 24612,
6470
];
65-
$this->product->expects($this->once())
71+
$this->product->expects($this->exactly(2))
6672
->method('getEntityId')
6773
->willReturn($id);
74+
$this->product->expects($this->exactly(2))
75+
->method('getTypeId')
76+
->willReturn(Type::TYPE_CODE);
6877
$this->type->expects($this->once())
6978
->method('getChildrenIds')
7079
->with($id)
7180
->willReturn($childIds);
7281
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
7382
$this->assertEquals($expectedIdentities, $identities);
83+
84+
$this->type->expects($this->never())
85+
->method('getChildrenIds')
86+
->with($id)
87+
->willReturn($childIds);
88+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
89+
$this->assertEquals($expectedIdentities, $identities);
7490
}
7591
}

app/code/Magento/Bundle/Test/Unit/Model/Plugin/ProductTest.php renamed to app/code/Magento/Bundle/Test/Unit/Model/Plugin/ProductIdentitiesExtenderTest.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
namespace Magento\Bundle\Test\Unit\Model\Plugin;
99

1010
use Magento\Bundle\Model\Product\Type;
11+
use Magento\Bundle\Model\Plugin\ProductIdentitiesExtender as Plugin;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1314
use PHPUnit\Framework\MockObject\MockObject;
1415
use PHPUnit\Framework\TestCase;
1516

16-
class ProductTest extends TestCase
17+
/**
18+
* Unit test for \Magento\Bundle\Model\Plugin\ProductIdentitiesExtender
19+
*/
20+
class ProductIdentitiesExtenderTest extends TestCase
1721
{
18-
/** @var \Magento\Bundle\Model\Plugin\Product */
22+
/** @var Plugin */
1923
private $plugin;
2024

2125
/** @var MockObject|Type */
@@ -24,27 +28,35 @@ class ProductTest extends TestCase
2428
/** @var MockObject|Product */
2529
private $product;
2630

31+
/**
32+
* @inheritdoc
33+
*/
2734
protected function setUp(): void
2835
{
2936
$objectManager = new ObjectManager($this);
3037

3138
$this->product = $this->getMockBuilder(Product::class)
3239
->disableOriginalConstructor()
33-
->setMethods(['getEntityId'])
40+
->setMethods(['getEntityId', 'getTypeId'])
3441
->getMock();
3542
$this->type = $this->getMockBuilder(Type::class)
3643
->disableOriginalConstructor()
3744
->setMethods(['getParentIdsByChild'])
3845
->getMock();
3946

4047
$this->plugin = $objectManager->getObject(
41-
\Magento\Bundle\Model\Plugin\Product::class,
48+
Plugin::class,
4249
[
4350
'type' => $this->type,
4451
]
4552
);
4653
}
4754

55+
/**
56+
* Verify after get identities
57+
*
58+
* @return void
59+
*/
4860
public function testAfterGetIdentities()
4961
{
5062
$baseIdentities = [
@@ -61,14 +73,24 @@ public function testAfterGetIdentities()
6173
Product::CACHE_TAG . '_' . 5,
6274
Product::CACHE_TAG . '_' . 100500,
6375
];
64-
$this->product->expects($this->once())
76+
$this->product->expects($this->exactly(2))
6577
->method('getEntityId')
6678
->willReturn($id);
79+
$this->product->expects($this->exactly(2))
80+
->method('getTypeId')
81+
->willReturn(Type::TYPE_CODE);
6782
$this->type->expects($this->once())
6883
->method('getParentIdsByChild')
6984
->with($id)
7085
->willReturn($parentIds);
7186
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
7287
$this->assertEquals($expectedIdentities, $identities);
88+
89+
$this->type->expects($this->never())
90+
->method('getParentIdsByChild')
91+
->with($id)
92+
->willReturn($parentIds);
93+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
94+
$this->assertEquals($expectedIdentities, $identities);
7395
}
7496
}

app/code/Magento/Bundle/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\PriceBackend" sortOrder="100" />
8787
</type>
8888
<type name="Magento\Catalog\Model\Product">
89-
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Product" sortOrder="100" />
89+
<plugin name="add_bundle_parent_identities" type="Magento\Bundle\Model\Plugin\ProductIdentitiesExtender" sortOrder="100" />
9090
</type>
9191
<type name="Magento\Sales\Model\Order\Item">
9292
<plugin name="bundle" type="Magento\Bundle\Model\Sales\Order\Plugin\Item" sortOrder="100" />

app/code/Magento/Bundle/etc/frontend/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
</arguments>
1515
</type>
1616
<type name="Magento\Catalog\Model\Product">
17-
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Frontend\Product" sortOrder="100" />
17+
<plugin name="add_bundle_child_identities" type="Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender" sortOrder="100"/>
1818
</type>
1919
</config>

0 commit comments

Comments
 (0)