Skip to content

Commit 822f5b5

Browse files
ENGCOM-3517: Add child identities to products instead of parent identities #19313
- Merge Pull Request #19313 from mediact/magento2:feature/bundle-configurable-product-identies-plugin - Merged commits: 1. f04154e 2. edd9d4c 3. c6fdd50 4. 2f3396f 5. c0d6730 6. 3b22449
2 parents b3d9a05 + 3b22449 commit 822f5b5

File tree

8 files changed

+396
-0
lines changed

8 files changed

+396
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Frontend;
9+
10+
use Magento\Bundle\Model\Product\Type;
11+
use Magento\Catalog\Model\Product as CatalogProduct;
12+
13+
/**
14+
* Add child identities to product identities on storefront.
15+
*/
16+
class Product
17+
{
18+
/**
19+
* @var Type
20+
*/
21+
private $type;
22+
23+
/**
24+
* @param Type $type
25+
*/
26+
public function __construct(Type $type)
27+
{
28+
$this->type = $type;
29+
}
30+
31+
/**
32+
* Add child identities to product identities
33+
*
34+
* @param CatalogProduct $product
35+
* @param array $identities
36+
* @return array
37+
*/
38+
public function afterGetIdentities(CatalogProduct $product, array $identities): array
39+
{
40+
foreach ($this->type->getChildrenIds($product->getEntityId()) as $childIds) {
41+
foreach ($childIds as $childId) {
42+
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId;
43+
}
44+
}
45+
46+
return array_unique($identities);
47+
}
48+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Bundle\Test\Unit\Model\Plugin\Frontend;
8+
9+
use Magento\Bundle\Model\Plugin\Frontend\Product as ProductPlugin;
10+
use Magento\Bundle\Model\Product\Type;
11+
use Magento\Catalog\Model\Product;
12+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
13+
14+
class ProductTest extends \PHPUnit\Framework\TestCase
15+
{
16+
/** @var \Magento\Bundle\Model\Plugin\Product */
17+
private $plugin;
18+
19+
/** @var MockObject|Type */
20+
private $type;
21+
22+
/** @var MockObject|\Magento\Catalog\Model\Product */
23+
private $product;
24+
25+
protected function setUp()
26+
{
27+
$this->product = $this->getMockBuilder(Product::class)
28+
->disableOriginalConstructor()
29+
->setMethods(['getEntityId'])
30+
->getMock();
31+
32+
$this->type = $this->getMockBuilder(Type::class)
33+
->disableOriginalConstructor()
34+
->setMethods(['getChildrenIds'])
35+
->getMock();
36+
37+
$this->plugin = new ProductPlugin($this->type);
38+
}
39+
40+
public function testAfterGetIdentities()
41+
{
42+
$baseIdentities = [
43+
'SomeCacheId',
44+
'AnotherCacheId',
45+
];
46+
$id = 12345;
47+
$childIds = [
48+
1 => [1, 2, 5, 100500],
49+
12 => [7, 22, 45, 24612]
50+
];
51+
$expectedIdentities = [
52+
'SomeCacheId',
53+
'AnotherCacheId',
54+
Product::CACHE_TAG . '_' . 1,
55+
Product::CACHE_TAG . '_' . 2,
56+
Product::CACHE_TAG . '_' . 5,
57+
Product::CACHE_TAG . '_' . 100500,
58+
Product::CACHE_TAG . '_' . 7,
59+
Product::CACHE_TAG . '_' . 22,
60+
Product::CACHE_TAG . '_' . 45,
61+
Product::CACHE_TAG . '_' . 24612,
62+
];
63+
$this->product->expects($this->once())
64+
->method('getEntityId')
65+
->will($this->returnValue($id));
66+
$this->type->expects($this->once())
67+
->method('getChildrenIds')
68+
->with($id)
69+
->will($this->returnValue($childIds));
70+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
71+
$this->assertEquals($expectedIdentities, $identities);
72+
}
73+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\Catalog\Model\Product">
17+
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Frontend\Product" sortOrder="100" />
18+
</type>
1619
</config>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\ConfigurableProduct\Model\Plugin\Frontend;
9+
10+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11+
use Magento\Catalog\Model\Product;
12+
13+
/**
14+
* Extender of product identities for child of configurable products
15+
*/
16+
class ProductIdentitiesExtender
17+
{
18+
/**
19+
* @var Configurable
20+
*/
21+
private $configurableType;
22+
23+
/**
24+
* @param Configurable $configurableType
25+
*/
26+
public function __construct(Configurable $configurableType)
27+
{
28+
$this->configurableType = $configurableType;
29+
}
30+
31+
/**
32+
* Add child identities to product identities
33+
*
34+
* @param Product $subject
35+
* @param array $identities
36+
* @return array
37+
*/
38+
public function afterGetIdentities(Product $subject, array $identities): array
39+
{
40+
foreach ($this->configurableType->getChildrenIds($subject->getId()) as $childIds) {
41+
foreach ($childIds as $childId) {
42+
$identities[] = Product::CACHE_TAG . '_' . $childId;
43+
}
44+
}
45+
46+
return array_unique($identities);
47+
}
48+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\ConfigurableProduct\Test\Unit\Model\Plugin\Frontend;
9+
10+
use Magento\ConfigurableProduct\Model\Plugin\Frontend\ProductIdentitiesExtender;
11+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
12+
use Magento\Catalog\Model\Product;
13+
14+
/**
15+
* Class ProductIdentitiesExtenderTest
16+
*/
17+
class ProductIdentitiesExtenderTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject|Configurable
21+
*/
22+
private $configurableTypeMock;
23+
24+
/**
25+
* @var ProductIdentitiesExtender
26+
*/
27+
private $plugin;
28+
29+
/** @var MockObject|\Magento\Catalog\Model\Product */
30+
private $product;
31+
32+
protected function setUp()
33+
{
34+
$this->product = $this->getMockBuilder(Product::class)
35+
->disableOriginalConstructor()
36+
->setMethods(['getId'])
37+
->getMock();
38+
39+
$this->configurableTypeMock = $this->getMockBuilder(Configurable::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
43+
$this->plugin = new ProductIdentitiesExtender($this->configurableTypeMock);
44+
}
45+
46+
public function testAfterGetIdentities()
47+
{
48+
$identities = [
49+
'SomeCacheId',
50+
'AnotherCacheId',
51+
];
52+
$productId = 12345;
53+
$childIdentities = [
54+
0 => [1, 2, 5, 100500]
55+
];
56+
$expectedIdentities = [
57+
'SomeCacheId',
58+
'AnotherCacheId',
59+
Product::CACHE_TAG . '_' . 1,
60+
Product::CACHE_TAG . '_' . 2,
61+
Product::CACHE_TAG . '_' . 5,
62+
Product::CACHE_TAG . '_' . 100500,
63+
];
64+
65+
$this->product->expects($this->once())
66+
->method('getId')
67+
->willReturn($productId);
68+
69+
$this->configurableTypeMock->expects($this->once())
70+
->method('getChildrenIds')
71+
->with($productId)
72+
->willReturn($childIdentities);
73+
74+
$productIdentities = $this->plugin->afterGetIdentities($this->product, $identities);
75+
$this->assertEquals($expectedIdentities, $productIdentities);
76+
}
77+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
1111
<plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
1212
</type>
13+
<type name="Magento\Catalog\Model\Product">
14+
<plugin name="product_identities_extender" type="Magento\ConfigurableProduct\Model\Plugin\Frontend\ProductIdentitiesExtender" />
15+
</type>
1316
</config>
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+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Bundle\Model\Plugin\Frontend;
10+
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Interception\PluginList;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test bundle fronted product plugin adds children products ids to bundle product identities.
18+
*/
19+
class ProductTest extends TestCase
20+
{
21+
/**
22+
* Check, product plugin is registered for storefront.
23+
*
24+
* @magentoAppArea frontend
25+
* @return void
26+
*/
27+
public function testProductIsRegistered(): void
28+
{
29+
$pluginInfo = Bootstrap::getObjectManager()->get(PluginList::class)
30+
->get(\Magento\Catalog\Model\Product::class, []);
31+
$this->assertSame(Product::class, $pluginInfo['bundle']['instance']);
32+
}
33+
34+
/**
35+
* Check plugin will add children ids to bundle product identities on storefront.
36+
*
37+
* @magentoDataFixture Magento/Bundle/_files/product.php
38+
* @magentoAppArea frontend
39+
* @return void
40+
*/
41+
public function testGetIdentitiesForBundleProductOnStorefront(): void
42+
{
43+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
44+
$bundleProduct = $productRepository->get('bundle-product');
45+
$simpleProduct = $productRepository->get('simple');
46+
$expectedIdentities = [
47+
'cat_p_' . $bundleProduct->getId(),
48+
'cat_p',
49+
'cat_p_' . $simpleProduct->getId(),
50+
51+
];
52+
$this->assertEquals($expectedIdentities, $bundleProduct->getIdentities());
53+
}
54+
55+
/**
56+
* Check plugin won't add children ids to bundle product identities in admin area.
57+
*
58+
* @magentoDataFixture Magento/Bundle/_files/product.php
59+
* @magentoAppArea adminhtml
60+
* @return void
61+
*/
62+
public function testGetIdentitiesForBundleProductInAdminArea(): void
63+
{
64+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
65+
$bundleProduct = $productRepository->get('bundle-product');
66+
$expectedIdentities = [
67+
'cat_p_' . $bundleProduct->getId(),
68+
];
69+
$this->assertEquals($expectedIdentities, $bundleProduct->getIdentities());
70+
}
71+
}

0 commit comments

Comments
 (0)