Skip to content

Commit d178e25

Browse files
committed
Merge remote-tracking branch 'origin/MC-31837' into 2.4-develop-pr16
2 parents fba3331 + 45eb2d1 commit d178e25

File tree

4 files changed

+193
-12
lines changed

4 files changed

+193
-12
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Website/Link.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
use Magento\Catalog\Api\Data\ProductInterface;
99
use Magento\Framework\App\ResourceConnection;
10-
use Magento\Framework\EntityManager\MetadataPool;
1110

11+
/**
12+
* Class Link used for assign website to the product
13+
*/
1214
class Link
1315
{
1416
/**
@@ -28,6 +30,7 @@ public function __construct(
2830

2931
/**
3032
* Retrieve associated with product websites ids
33+
*
3134
* @param int $productId
3235
* @return array
3336
*/
@@ -48,29 +51,53 @@ public function getWebsiteIdsByProductId($productId)
4851

4952
/**
5053
* Return true - if websites was changed, and false - if not
54+
*
5155
* @param ProductInterface $product
5256
* @param array $websiteIds
5357
* @return bool
5458
*/
5559
public function saveWebsiteIds(ProductInterface $product, array $websiteIds)
60+
{
61+
$productId = (int) $product->getId();
62+
return $this->updateProductWebsite($productId, $websiteIds);
63+
}
64+
65+
/**
66+
* Get Product website table
67+
*
68+
* @return string
69+
*/
70+
private function getProductWebsiteTable()
71+
{
72+
return $this->resourceConnection->getTableName('catalog_product_website');
73+
}
74+
75+
/**
76+
* Update product website table
77+
*
78+
* @param int $productId
79+
* @param array $websiteIds
80+
* @return bool
81+
*/
82+
public function updateProductWebsite(int $productId, array $websiteIds): bool
5683
{
5784
$connection = $this->resourceConnection->getConnection();
5885

59-
$oldWebsiteIds = $this->getWebsiteIdsByProductId($product->getId());
86+
$oldWebsiteIds = $this->getWebsiteIdsByProductId($productId);
6087
$insert = array_diff($websiteIds, $oldWebsiteIds);
6188
$delete = array_diff($oldWebsiteIds, $websiteIds);
6289

6390
if (!empty($insert)) {
6491
$data = [];
6592
foreach ($insert as $websiteId) {
66-
$data[] = ['product_id' => (int) $product->getId(), 'website_id' => (int) $websiteId];
93+
$data[] = ['product_id' => $productId, 'website_id' => (int)$websiteId];
6794
}
6895
$connection->insertMultiple($this->getProductWebsiteTable(), $data);
6996
}
7097

7198
if (!empty($delete)) {
7299
foreach ($delete as $websiteId) {
73-
$condition = ['product_id = ?' => (int) $product->getId(), 'website_id = ?' => (int) $websiteId];
100+
$condition = ['product_id = ?' => $productId, 'website_id = ?' => (int)$websiteId];
74101
$connection->delete($this->getProductWebsiteTable(), $condition);
75102
}
76103
}
@@ -81,12 +108,4 @@ public function saveWebsiteIds(ProductInterface $product, array $websiteIds)
81108

82109
return false;
83110
}
84-
85-
/**
86-
* @return string
87-
*/
88-
private function getProductWebsiteTable()
89-
{
90-
return $this->resourceConnection->getTableName('catalog_product_website');
91-
}
92111
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Catalog\Model\ResourceModel;
8+
9+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link;
10+
use Magento\Framework\EntityManager\Operation\AttributeInterface;
11+
12+
/**
13+
* Class purpose is to handle product websites assignment
14+
*/
15+
class ProductWebsiteAssignmentHandler implements AttributeInterface
16+
{
17+
/**
18+
* @var Link
19+
*/
20+
private $productLink;
21+
22+
/**
23+
* ProductWebsiteAssignmentHandler constructor
24+
*
25+
* @param Link $productLink
26+
*/
27+
public function __construct(
28+
Link $productLink
29+
) {
30+
$this->productLink = $productLink;
31+
}
32+
33+
/**
34+
* Assign product website entity to the product repository
35+
*
36+
* @param string $entityType
37+
* @param array $entityData
38+
* @param array $arguments
39+
* @return array
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
* @throws \Exception
42+
*/
43+
public function execute($entityType, $entityData, $arguments = []): array
44+
{
45+
$websiteIds = array_key_exists('website_ids', $entityData) ?
46+
array_filter($entityData['website_ids'], function ($websiteId) {
47+
return $websiteId !== null;
48+
}) : [];
49+
$productId = array_key_exists('entity_id', $entityData) ? (int) $entityData['entity_id'] : null;
50+
51+
if (!empty($productId) && !empty($websiteIds)) {
52+
$this->productLink->updateProductWebsite($productId, $websiteIds);
53+
}
54+
return $entityData;
55+
}
56+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
namespace Magento\Catalog\Test\Unit\Model\ResourceModel;
8+
9+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link;
10+
use Magento\Catalog\Model\ResourceModel\ProductWebsiteAssignmentHandler;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class ProductWebsiteAssignmentHandlerTest extends TestCase
15+
{
16+
/**
17+
* @var ProductWebsiteAssignmentHandler
18+
*/
19+
protected $handler;
20+
21+
/**
22+
* @var Link|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $productLinkMock;
25+
26+
protected function setUp()
27+
{
28+
$objectManager = new ObjectManager($this);
29+
30+
$this->productLinkMock = $this->createPartialMock(
31+
Link::class,
32+
['updateProductWebsite']
33+
);
34+
$this->handler = $objectManager->getObject(
35+
ProductWebsiteAssignmentHandler::class,
36+
[
37+
'productLink' => $this->productLinkMock
38+
]
39+
);
40+
}
41+
42+
/**
43+
* @param $actualData
44+
* @param $expectedResult
45+
* @dataProvider productWebsitesDataProvider
46+
* @throws \Exception
47+
*/
48+
public function testUpdateProductWebsiteReturnValidResult($actualData, $expectedResult)
49+
{
50+
$this->productLinkMock->expects($this->any())->method('updateProductWebsite')->willReturn($expectedResult);
51+
$this->assertEquals(
52+
$actualData['entityData'],
53+
$this->handler->execute($actualData['entityType'], $actualData['entityData'])
54+
);
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function productWebsitesDataProvider(): array
61+
{
62+
return [
63+
[
64+
[
65+
'entityType' => 'product',
66+
'entityData' => [
67+
'entity_id' => '12345',
68+
'website_ids' => ['1', '2', '3'],
69+
'name' => 'test-1',
70+
'sku' => 'test-1'
71+
]
72+
],
73+
true
74+
],
75+
[
76+
[
77+
'entityType' => 'product',
78+
'entityData' => [
79+
'entity_id' => null,
80+
'website_ids' => ['1', '2', '3'],
81+
'name' => 'test-1',
82+
'sku' => 'test-1'
83+
]
84+
],
85+
false
86+
],
87+
[
88+
[
89+
'entityType' => 'product',
90+
'entityData' => [
91+
'entity_id' => '12345',
92+
'website_ids' => [null],
93+
'name' => 'test-1',
94+
'sku' => 'test-1'
95+
]
96+
],
97+
false
98+
]
99+
];
100+
}
101+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,11 @@
891891
<item name="update" xsi:type="string">Magento\Catalog\Model\ResourceModel\UpdateHandler</item>
892892
</item>
893893
</item>
894+
<item name="websites" xsi:type="array">
895+
<item name="Magento\Catalog\Api\Data\ProductInterface" xsi:type="array">
896+
<item name="create" xsi:type="string">Magento\Catalog\Model\ResourceModel\ProductWebsiteAssignmentHandler</item>
897+
</item>
898+
</item>
894899
</argument>
895900
</arguments>
896901
</type>

0 commit comments

Comments
 (0)