Skip to content

Commit 82194f3

Browse files
committed
Merge remote-tracking branch 'origin/MC-23870' into 2.4-develop-pr7
2 parents 4ce5583 + 52de4aa commit 82194f3

File tree

3 files changed

+129
-11
lines changed

3 files changed

+129
-11
lines changed

app/code/Magento/Catalog/Model/Product/ProductFrontendAction/Synchronizer.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,16 @@ private function filterNewestActions(array $productsData, $typeId)
125125
$lifetime = $this->getLifeTimeByNamespace($typeId);
126126
$actionsNumber = $lifetime * self::TIME_TO_DO_ONE_ACTION;
127127

128-
uasort($productsData, function (array $firstProduct, array $secondProduct) {
129-
return $firstProduct['added_at'] > $secondProduct['added_at'];
130-
});
128+
uasort(
129+
$productsData,
130+
function (array $firstProduct, array $secondProduct) {
131+
if (isset($firstProduct['added_at'], $secondProduct['added_at'])) {
132+
return $firstProduct['added_at'] > $secondProduct['added_at'];
133+
}
134+
135+
return false;
136+
}
137+
);
131138

132139
return array_slice($productsData, 0, $actionsNumber, true);
133140
}
@@ -185,15 +192,17 @@ public function syncActions(array $productsData, $typeId)
185192

186193
foreach ($productsData as $productId => $productData) {
187194
/** @var ProductFrontendActionInterface $action */
188-
$action = $this->productFrontendActionFactory->create([
189-
'data' => [
190-
'visitor_id' => $customerId ? null : $visitorId,
191-
'customer_id' => $this->session->getCustomerId(),
192-
'added_at' => $productData['added_at'],
193-
'product_id' => $productId,
194-
'type_id' => $typeId
195+
$action = $this->productFrontendActionFactory->create(
196+
[
197+
'data' => [
198+
'visitor_id' => $customerId ? null : $visitorId,
199+
'customer_id' => $this->session->getCustomerId(),
200+
'added_at' => $productData['added_at'],
201+
'product_id' => $productId,
202+
'type_id' => $typeId
203+
]
195204
]
196-
]);
205+
);
197206

198207
$this->entityManager->save($action);
199208
}

app/code/Magento/Catalog/view/frontend/layout/default.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
<item name="updateRequestConfig" xsi:type="array">
2727
<item name="url" xsi:type="serviceUrl" path="/products-render-info"/>
2828
</item>
29+
<item name="requestConfig" xsi:type="array">
30+
<item name="syncUrl" xsi:type="url" path="catalog/product/frontend_action_synchronize"/>
31+
</item>
2932
</item>
3033
</argument>
3134
</arguments>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\Catalog\Model\Product\ProductFrontendAction;
9+
10+
use Magento\Catalog\Model\ProductRepository;
11+
12+
/**
13+
* Test for \Magento\Catalog\Model\Product\ProductFrontendAction\Synchronizer.
14+
*/
15+
class SynchronizerTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* @var Synchronizer
19+
*/
20+
private $synchronizer;
21+
22+
/**
23+
* @var ProductRepository
24+
*/
25+
private $productRepository;
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function setUp()
31+
{
32+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
33+
34+
$this->synchronizer = $objectManager->get(Synchronizer::class);
35+
$this->productRepository = $objectManager->get(ProductRepository::class);
36+
}
37+
38+
/**
39+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
40+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
41+
*
42+
* @return void
43+
*/
44+
public function testSyncActions(): void
45+
{
46+
$actionsType = 'recently_viewed_product';
47+
$product1 = $this->productRepository->get('simple');
48+
$product2 = $this->productRepository->get('simple2');
49+
$product1Id = $product1->getId();
50+
$product2Id = $product2->getId();
51+
$productsData = [
52+
$product1Id => [
53+
'added_at' => '1576582660',
54+
'product_id' => $product1Id,
55+
],
56+
$product2Id => [
57+
'added_at' => '1576587153',
58+
'product_id' => $product2Id,
59+
],
60+
];
61+
62+
$this->synchronizer->syncActions($productsData, $actionsType);
63+
64+
$synchronizedCollection = $this->synchronizer->getActionsByType($actionsType);
65+
$synchronizedCollection->addFieldToFilter(
66+
'product_id',
67+
[
68+
$product1Id,
69+
$product2Id,
70+
]
71+
);
72+
73+
foreach ($synchronizedCollection as $item) {
74+
$this->assertArrayHasKey($item->getProductId(), $productsData);
75+
$this->assertEquals($productsData[$item->getProductId()]['added_at'], $item->getAddedAt());
76+
}
77+
}
78+
79+
/**
80+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
81+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
82+
*
83+
* @return void
84+
*/
85+
public function testSyncActionsWithoutActionsType(): void
86+
{
87+
$product1 = $this->productRepository->get('simple');
88+
$product2 = $this->productRepository->get('simple2');
89+
$product1Id = $product1->getId();
90+
$product2Id = $product2->getId();
91+
$productsData = [
92+
$product1Id => [
93+
'id' => $product1Id,
94+
'name' => $product1->getName(),
95+
'type' => $product1->getTypeId(),
96+
],
97+
$product2Id => [
98+
'id' => $product2Id,
99+
'name' => $product2->getName(),
100+
'type' => $product2->getTypeId(),
101+
],
102+
];
103+
104+
$this->synchronizer->syncActions($productsData, '');
105+
}
106+
}

0 commit comments

Comments
 (0)