Skip to content

Commit 67d6184

Browse files
committed
Merge branch 'ACP2E-2286' of https://github.com/magento-l3/magento2ce into PR2-L3-09132023
2 parents 0c0e319 + 63f2cb3 commit 67d6184

File tree

2 files changed

+135
-16
lines changed

2 files changed

+135
-16
lines changed

app/code/Magento/ConfigurableProduct/Model/Plugin/ProductIdentitiesExtender.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
namespace Magento\ConfigurableProduct\Model\Plugin;
99

1010
use Magento\Catalog\Model\Product\Type as ProductTypes;
11+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
1112
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableType;
1213
use Magento\Catalog\Api\ProductRepositoryInterface;
1314
use Magento\Catalog\Model\Product;
1415
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
16+
use Magento\Store\Model\Store;
1517

1618
/**
1719
* Extender of product identities for child of configurable products
@@ -28,6 +30,11 @@ class ProductIdentitiesExtender implements ResetAfterRequestInterface
2830
*/
2931
private $productRepository;
3032

33+
/**
34+
* @var ProductWebsiteLink
35+
*/
36+
private $productWebsiteLink;
37+
3138
/**
3239
* @var array
3340
*/
@@ -36,11 +43,16 @@ class ProductIdentitiesExtender implements ResetAfterRequestInterface
3643
/**
3744
* @param ConfigurableType $configurableType
3845
* @param ProductRepositoryInterface $productRepository
46+
* @param ProductWebsiteLink $productWebsiteLink
3947
*/
40-
public function __construct(ConfigurableType $configurableType, ProductRepositoryInterface $productRepository)
41-
{
48+
public function __construct(
49+
ConfigurableType $configurableType,
50+
ProductRepositoryInterface $productRepository,
51+
ProductWebsiteLink $productWebsiteLink
52+
) {
4253
$this->configurableType = $configurableType;
4354
$this->productRepository = $productRepository;
55+
$this->productWebsiteLink = $productWebsiteLink;
4456
}
4557

4658
/**
@@ -56,10 +68,19 @@ public function afterGetIdentities(Product $subject, array $identities): array
5668
if ($subject->getTypeId() !== ProductTypes::TYPE_SIMPLE) {
5769
return $identities;
5870
}
71+
72+
$store = $subject->getStore();
5973
$parentProductsIdentities = [];
6074
foreach ($this->getParentIdsByChild($subject->getId()) as $parentId) {
61-
$parentProduct = $this->productRepository->getById($parentId);
62-
$parentProductsIdentities[] = $parentProduct->getIdentities();
75+
$addParentIdentities = true;
76+
if (Store::DEFAULT_STORE_ID !== (int) $store->getId()) {
77+
$parentWebsiteIds = $this->productWebsiteLink->getWebsiteIdsByProductId($parentId);
78+
$addParentIdentities = in_array($store->getWebsiteId(), $parentWebsiteIds);
79+
}
80+
if ($addParentIdentities) {
81+
$parentProduct = $this->productRepository->getById($parentId);
82+
$parentProductsIdentities[] = $parentProduct->getIdentities();
83+
}
6384
}
6485
$identities = array_merge($identities, ...$parentProductsIdentities);
6586

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/ProductIdentitiesExtenderTest.php

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\Product\Type;
13+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLink;
1314
use Magento\ConfigurableProduct\Model\Plugin\ProductIdentitiesExtender;
1415
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
16+
use Magento\Store\Model\Store;
1517
use PHPUnit\Framework\MockObject\MockObject;
1618
use PHPUnit\Framework\TestCase;
1719

@@ -30,6 +32,11 @@ class ProductIdentitiesExtenderTest extends TestCase
3032
*/
3133
private $productRepositoryMock;
3234

35+
/**
36+
* @var ProductWebsiteLink|MockObject
37+
*/
38+
private $productWebsiteLinkMock;
39+
3340
/**
3441
* @var ProductIdentitiesExtender
3542
*/
@@ -40,13 +47,15 @@ class ProductIdentitiesExtenderTest extends TestCase
4047
*/
4148
protected function setUp(): void
4249
{
43-
$this->configurableTypeMock = $this->getMockBuilder(Configurable::class)
44-
->disableOriginalConstructor()
45-
->getMock();
46-
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
47-
->getMock();
50+
$this->configurableTypeMock = $this->createMock(Configurable::class);
51+
$this->productRepositoryMock = $this->createMock(ProductRepositoryInterface::class);
52+
$this->productWebsiteLinkMock = $this->createMock(ProductWebsiteLink::class);
4853

49-
$this->plugin = new ProductIdentitiesExtender($this->configurableTypeMock, $this->productRepositoryMock);
54+
$this->plugin = new ProductIdentitiesExtender(
55+
$this->configurableTypeMock,
56+
$this->productRepositoryMock,
57+
$this->productWebsiteLinkMock
58+
);
5059
}
5160

5261
/**
@@ -58,25 +67,30 @@ public function testAfterGetIdentities()
5867
{
5968
$productId = 1;
6069
$productIdentity = 'cache_tag_1';
61-
$productMock = $this->getMockBuilder(Product::class)
62-
->disableOriginalConstructor()
63-
->getMock();
70+
$productMock = $this->createMock(Product::class);
6471
$parentProductId = 2;
6572
$parentProductIdentity = 'cache_tag_2';
66-
$parentProductMock = $this->getMockBuilder(Product::class)
67-
->disableOriginalConstructor()
68-
->getMock();
73+
$parentProductMock = $this->createMock(Product::class);
6974

7075
$productMock->expects($this->exactly(2))
7176
->method('getId')
7277
->willReturn($productId);
7378
$productMock->expects($this->exactly(2))
7479
->method('getTypeId')
7580
->willReturn(Type::TYPE_SIMPLE);
81+
$storeMock = $this->createMock(Store::class);
82+
$productMock->expects($this->atLeastOnce())
83+
->method('getStore')
84+
->willReturn($storeMock);
85+
$storeMock->expects($this->atLeastOnce())
86+
->method('getId')
87+
->willReturn(Store::DEFAULT_STORE_ID);
7688
$this->configurableTypeMock->expects($this->once())
7789
->method('getParentIdsByChild')
7890
->with($productId)
7991
->willReturn([$parentProductId]);
92+
$this->productWebsiteLinkMock->expects($this->never())
93+
->method('getWebsiteIdsByProductId');
8094
$this->productRepositoryMock->expects($this->exactly(2))
8195
->method('getById')
8296
->with($parentProductId)
@@ -95,4 +109,88 @@ public function testAfterGetIdentities()
95109
$productIdentities = $this->plugin->afterGetIdentities($productMock, [$productIdentity]);
96110
$this->assertEquals([$productIdentity, $parentProductIdentity], $productIdentities);
97111
}
112+
113+
public function testAfterGetIdentitiesWhenWebsitesMatched()
114+
{
115+
$productId = 1;
116+
$websiteId = 1;
117+
$productIdentity = 'cache_tag_1';
118+
$productMock = $this->createMock(Product::class);
119+
$parentProductId = 2;
120+
$parentProductIdentity = 'cache_tag_2';
121+
$parentProductMock = $this->createMock(Product::class);
122+
123+
$productMock->expects($this->atLeastOnce())
124+
->method('getId')
125+
->willReturn($productId);
126+
$productMock->expects($this->once())
127+
->method('getTypeId')
128+
->willReturn(Type::TYPE_SIMPLE);
129+
$storeMock = $this->createMock(Store::class);
130+
$productMock->expects($this->once())
131+
->method('getStore')
132+
->willReturn($storeMock);
133+
$storeMock->expects($this->atLeastOnce())
134+
->method('getId')
135+
->willReturn(1);
136+
$storeMock->expects($this->atLeastOnce())
137+
->method('getWebsiteId')
138+
->willReturn($websiteId);
139+
$this->configurableTypeMock->expects($this->once())
140+
->method('getParentIdsByChild')
141+
->with($productId)
142+
->willReturn([$parentProductId]);
143+
$this->productWebsiteLinkMock->expects($this->once())
144+
->method('getWebsiteIdsByProductId')
145+
->with($parentProductId)
146+
->willReturn([$websiteId]);
147+
$this->productRepositoryMock->expects($this->once())
148+
->method('getById')
149+
->with($parentProductId)
150+
->willReturn($parentProductMock);
151+
$parentProductMock->expects($this->once())
152+
->method('getIdentities')
153+
->willReturn([$parentProductIdentity]);
154+
155+
$productIdentities = $this->plugin->afterGetIdentities($productMock, [$productIdentity]);
156+
$this->assertEquals([$productIdentity, $parentProductIdentity], $productIdentities);
157+
}
158+
159+
public function testAfterGetIdentitiesWhenWebsitesNotMatched()
160+
{
161+
$productId = 1;
162+
$productIdentity = 'cache_tag_1';
163+
$productMock = $this->createMock(Product::class);
164+
$parentProductId = 2;
165+
166+
$productMock->expects($this->atLeastOnce())
167+
->method('getId')
168+
->willReturn($productId);
169+
$productMock->expects($this->once())
170+
->method('getTypeId')
171+
->willReturn(Type::TYPE_SIMPLE);
172+
$storeMock = $this->createMock(Store::class);
173+
$productMock->expects($this->once())
174+
->method('getStore')
175+
->willReturn($storeMock);
176+
$storeMock->expects($this->atLeastOnce())
177+
->method('getId')
178+
->willReturn(1);
179+
$storeMock->expects($this->atLeastOnce())
180+
->method('getWebsiteId')
181+
->willReturn(1);
182+
$this->configurableTypeMock->expects($this->once())
183+
->method('getParentIdsByChild')
184+
->with($productId)
185+
->willReturn([$parentProductId]);
186+
$this->productWebsiteLinkMock->expects($this->once())
187+
->method('getWebsiteIdsByProductId')
188+
->with($parentProductId)
189+
->willReturn([2]);
190+
$this->productRepositoryMock->expects($this->never())
191+
->method('getById');
192+
193+
$productIdentities = $this->plugin->afterGetIdentities($productMock, [$productIdentity]);
194+
$this->assertEquals([$productIdentity], $productIdentities);
195+
}
98196
}

0 commit comments

Comments
 (0)