Skip to content

Commit db91250

Browse files
authored
Merge pull request #5420 from magento-tango/PR-02-03
[tango] bug fixes
2 parents 02dc172 + 96fa0ac commit db91250

File tree

8 files changed

+145
-14
lines changed

8 files changed

+145
-14
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*
2121
* Service which allows to sync product widget information, such as product id with db. In order to reuse this info
2222
* on different devices
23+
*
24+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2325
*/
2426
class Synchronizer
2527
{
@@ -94,6 +96,7 @@ public function __construct(
9496
*
9597
* @param string $namespace
9698
* @return int
99+
* @throws \Magento\Framework\Exception\LocalizedException
97100
*/
98101
private function getLifeTimeByNamespace($namespace)
99102
{
@@ -119,6 +122,7 @@ private function getLifeTimeByNamespace($namespace)
119122
* @param array $productsData (product action data, that came from frontend)
120123
* @param string $typeId namespace (type of action)
121124
* @return array
125+
* @throws \Magento\Framework\Exception\LocalizedException
122126
*/
123127
private function filterNewestActions(array $productsData, $typeId)
124128
{
@@ -166,6 +170,7 @@ private function getProductIdsByActions(array $actions)
166170
* @param array $productsData
167171
* @param string $typeId
168172
* @return void
173+
* @throws \Exception
169174
*/
170175
public function syncActions(array $productsData, $typeId)
171176
{
@@ -189,16 +194,15 @@ public function syncActions(array $productsData, $typeId)
189194
foreach ($collection as $item) {
190195
$this->entityManager->delete($item);
191196
}
192-
193-
foreach ($productsData as $productId => $productData) {
197+
foreach ($productsData as $productData) {
194198
/** @var ProductFrontendActionInterface $action */
195199
$action = $this->productFrontendActionFactory->create(
196200
[
197201
'data' => [
198202
'visitor_id' => $customerId ? null : $visitorId,
199203
'customer_id' => $this->session->getCustomerId(),
200204
'added_at' => $productData['added_at'],
201-
'product_id' => $productId,
205+
'product_id' => $productData['product_id'],
202206
'type_id' => $typeId
203207
]
204208
]

app/code/Magento/Catalog/Test/Unit/Model/Product/ProductFrontendAction/SynchronizerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public function testFilterProductActions()
8282
{
8383
$typeId = 'recently_compared_product';
8484
$productsData = [
85-
1 => [
85+
'website-1-1' => [
8686
'added_at' => 12,
8787
'product_id' => 1,
8888
],
89-
2 => [
89+
'website-1-2' => [
9090
'added_at' => 13,
9191
'product_id' => '2',
9292
],
93-
3 => [
93+
'website-2-3' => [
9494
'added_at' => 14,
9595
'product_id' => 3,
9696
]

app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public function apply(\Magento\Framework\App\RequestInterface $request)
6767

6868
$labels = [];
6969
foreach ((array) $attributeValue as $value) {
70-
$labels[] = (array) $this->getOptionText($value);
70+
$label = $this->getOptionText($value);
71+
$labels[] = is_array($label) ? $label : [$label];
7172
}
7273
$label = implode(',', array_unique(array_merge(...$labels)));
7374
$this->getLayer()

app/code/Magento/Indexer/Console/Command/IndexerReindexCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ private function validateSharedIndex($sharedIndex)
256256
$indexer = $this->getIndexerRegistry()->get($indexerId);
257257
/** @var \Magento\Indexer\Model\Indexer\State $state */
258258
$state = $indexer->getState();
259+
$state->setStatus(StateInterface::STATUS_WORKING);
260+
$state->save();
259261
$state->setStatus(StateInterface::STATUS_VALID);
260262
$state->save();
261263
}

app/code/Magento/Indexer/Model/Processor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Framework\Indexer\IndexerInterfaceFactory;
1111
use Magento\Framework\Indexer\StateInterface;
1212

13+
/**
14+
* Indexer processor
15+
*/
1316
class Processor
1417
{
1518
/**
@@ -70,6 +73,8 @@ public function reindexAllInvalid()
7073
} else {
7174
/** @var \Magento\Indexer\Model\Indexer\State $state */
7275
$state = $indexer->getState();
76+
$state->setStatus(StateInterface::STATUS_WORKING);
77+
$state->save();
7378
$state->setStatus(StateInterface::STATUS_VALID);
7479
$state->save();
7580
}

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/ProductFrontendAction/SynchronizerTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,24 @@ protected function setUp()
4040
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
4141
*
4242
* @return void
43+
* @throws \Magento\Framework\Exception\LocalizedException
44+
* @throws \Magento\Framework\Exception\NoSuchEntityException
4345
*/
4446
public function testSyncActions(): void
4547
{
4648
$actionsType = 'recently_viewed_product';
49+
$productScope = 'website';
50+
$scopeId = 1;
4751
$product1 = $this->productRepository->get('simple');
4852
$product2 = $this->productRepository->get('simple2');
4953
$product1Id = $product1->getId();
5054
$product2Id = $product2->getId();
5155
$productsData = [
52-
$product1Id => [
56+
$productScope . '-' . $scopeId . '-' . $product1Id => [
5357
'added_at' => '1576582660',
5458
'product_id' => $product1Id,
5559
],
56-
$product2Id => [
60+
$productScope . '-' . $scopeId . '-' . $product2Id => [
5761
'added_at' => '1576587153',
5862
'product_id' => $product2Id,
5963
],
@@ -71,8 +75,9 @@ public function testSyncActions(): void
7175
);
7276

7377
foreach ($synchronizedCollection as $item) {
74-
$this->assertArrayHasKey($item->getProductId(), $productsData);
75-
$this->assertEquals($productsData[$item->getProductId()]['added_at'], $item->getAddedAt());
78+
$productScopeId = $productScope . '-' . $scopeId . '-' . $item->getProductId();
79+
$this->assertArrayHasKey($productScopeId, $productsData);
80+
$this->assertEquals($productsData[$productScopeId]['added_at'], $item->getAddedAt());
7681
}
7782
}
7883

@@ -81,6 +86,8 @@ public function testSyncActions(): void
8186
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
8287
*
8388
* @return void
89+
* @throws \Magento\Framework\Exception\LocalizedException
90+
* @throws \Magento\Framework\Exception\NoSuchEntityException
8491
*/
8592
public function testSyncActionsWithoutActionsType(): void
8693
{

dev/tests/integration/testsuite/Magento/Indexer/Console/Command/IndexerReindexCommandTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
namespace Magento\Indexer\Console\Command;
99

1010
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Indexer\Model\Indexer\CollectionFactory as IndexerCollectionFactory;
1112
use Magento\TestFramework\Helper\Bootstrap;
12-
use PHPUnit_Framework_MockObject_MockObject as Mock;
13+
use PHPUnit\Framework\MockObject\MockObject;
1314
use Symfony\Component\Console\Input\InputInterface;
1415
use Symfony\Component\Console\Output\OutputInterface;
1516

@@ -27,12 +28,12 @@ class IndexerReindexCommandTest extends \PHPUnit\Framework\TestCase
2728
private $objectManager;
2829

2930
/**
30-
* @var InputInterface|Mock
31+
* @var InputInterface|MockObject
3132
*/
3233
private $inputMock;
3334

3435
/**
35-
* @var OutputInterface|Mock
36+
* @var OutputInterface|MockObject
3637
*/
3738
private $outputMock;
3839

@@ -41,6 +42,11 @@ class IndexerReindexCommandTest extends \PHPUnit\Framework\TestCase
4142
*/
4243
private $command;
4344

45+
/**
46+
* @var IndexerCollectionFactory
47+
*/
48+
private $indexerCollectionFactory;
49+
4450
/**
4551
* @inheritdoc
4652
*/
@@ -52,6 +58,7 @@ protected function setUp()
5258
$this->outputMock = $this->getMockBuilder(OutputInterface::class)->getMockForAbstractClass();
5359

5460
$this->command = $this->objectManager->get(IndexerReindexCommand::class);
61+
$this->indexerCollectionFactory = $this->objectManager->create(IndexerCollectionFactory::class);
5562
}
5663

5764
/**
@@ -65,5 +72,19 @@ public function testReindexAll()
6572
$status,
6673
'Index wasn\'t success'
6774
);
75+
76+
$notValidIndexers = [];
77+
$indexers = $this->indexerCollectionFactory->create()->getItems();
78+
foreach ($indexers as $indexer) {
79+
if ($indexer->isValid()) {
80+
continue;
81+
}
82+
83+
$notValidIndexers[] = $indexer->getId();
84+
}
85+
$this->assertEmpty(
86+
$notValidIndexers,
87+
'Following indexers are not valid: ' . implode(', ', $notValidIndexers)
88+
);
6889
}
6990
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Indexer\Model;
9+
10+
use Magento\Framework\Indexer\ConfigInterface as IndexerConfig;
11+
use Magento\Framework\Indexer\IndexerInterfaceFactory;
12+
use Magento\Indexer\Model\Indexer\CollectionFactory as IndexerCollectionFactory;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
/**
16+
* Test for \Magento\Indexer\Model\Processor
17+
*/
18+
class ProcessorTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var \Magento\Framework\Indexer\Config\Converter
22+
*/
23+
private $processor;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp()
29+
{
30+
$this->processor = Bootstrap::getObjectManager()->create(Processor::class);
31+
}
32+
33+
/**
34+
* @return void
35+
*/
36+
public function testReindexAllInvalid()
37+
{
38+
$indexerConfig = Bootstrap::getObjectManager()->create(IndexerConfig::class);
39+
$indexerFactory = Bootstrap::getObjectManager()->create(IndexerInterfaceFactory::class);
40+
$indexerIds = array_keys($indexerConfig->getIndexers());
41+
42+
foreach ($indexerIds as $indexerId) {
43+
$indexer = $indexerFactory->create()->load($indexerId);
44+
$indexer->invalidate();
45+
}
46+
47+
$this->processor->reindexAllInvalid();
48+
49+
$notValidIndexers = [];
50+
foreach ($indexerIds as $indexerId) {
51+
$indexer = $indexerFactory->create()->load($indexerId);
52+
if ($indexer->isValid()) {
53+
continue;
54+
}
55+
56+
$notValidIndexers[] = $indexer->getId();
57+
}
58+
$this->assertEmpty(
59+
$notValidIndexers,
60+
'Following indexers are not valid: ' . implode(', ', $notValidIndexers)
61+
);
62+
}
63+
64+
/**
65+
* @return void
66+
*/
67+
public function testReindexAll()
68+
{
69+
$indexerCollectionFactory = Bootstrap::getObjectManager()->create(IndexerCollectionFactory::class);
70+
$indexers = $indexerCollectionFactory->create()->getItems();
71+
foreach ($indexers as $indexer) {
72+
$indexer->invalidate();
73+
}
74+
75+
$this->processor->reindexAll();
76+
77+
$notValidIndexers = [];
78+
$indexers = $indexerCollectionFactory->create()->getItems();
79+
foreach ($indexers as $indexer) {
80+
if ($indexer->isValid()) {
81+
continue;
82+
}
83+
84+
$notValidIndexers[] = $indexer->getId();
85+
}
86+
$this->assertEmpty(
87+
$notValidIndexers,
88+
'Following indexers are not valid: ' . implode(', ', $notValidIndexers)
89+
);
90+
}
91+
}

0 commit comments

Comments
 (0)