Skip to content

Commit b1062d0

Browse files
committed
Merge branch 'MAGETWO-65245' into develop-pr2
2 parents 0932c53 + d33e205 commit b1062d0

File tree

4 files changed

+130
-145
lines changed

4 files changed

+130
-145
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext.php

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer;
77

8-
use Magento\Catalog\Api\Data\ProductInterface;
98
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\FullFactory;
109
use Magento\CatalogSearch\Model\Indexer\Scope\State;
1110
use Magento\CatalogSearch\Model\ResourceModel\Fulltext as FulltextResource;
1211
use Magento\Framework\App\ObjectManager;
13-
use Magento\Framework\EntityManager\MetadataPool;
1412
use Magento\Framework\Search\Request\Config as SearchRequestConfig;
1513
use Magento\Framework\Search\Request\DimensionFactory;
1614
use Magento\Store\Model\StoreManagerInterface;
1715

1816
/**
1917
* Provide functionality for Fulltext Search indexing.
20-
*
21-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2218
*/
2319
class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
2420
{
@@ -72,13 +68,6 @@ class Fulltext implements \Magento\Framework\Indexer\ActionInterface, \Magento\F
7268
*/
7369
private $indexScopeState;
7470

75-
/**
76-
* Holder for MetadataPool instance.
77-
*
78-
* @var MetadataPool
79-
*/
80-
private $metadataPool = null;
81-
8271
/**
8372
* @param FullFactory $fullActionFactory
8473
* @param IndexerHandlerFactory $indexerHandlerFactory
@@ -133,37 +122,12 @@ public function execute($ids)
133122
]);
134123
foreach ($storeIds as $storeId) {
135124
$dimension = $this->dimensionFactory->create(['name' => 'scope', 'value' => $storeId]);
136-
$productIds = array_unique(array_merge($ids, $this->getRelationsByChild($ids)));
125+
$productIds = array_unique(array_merge($ids, $this->fulltextResource->getRelationsByChild($ids)));
137126
$saveHandler->deleteIndex([$dimension], new \ArrayObject($productIds));
138127
$saveHandler->saveIndex([$dimension], $this->fullAction->rebuildStoreIndex($storeId, $ids));
139128
}
140129
}
141130

142-
/**
143-
* Retrieve product relations by children.
144-
*
145-
* @param int|array $childIds
146-
* @return array
147-
*/
148-
private function getRelationsByChild($childIds)
149-
{
150-
$connection = $this->fulltextResource->getConnection();
151-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
152-
$select = $connection->select()->from(
153-
['relation' => $this->fulltextResource->getTable('catalog_product_relation')],
154-
[]
155-
)->join(
156-
['cpe' => $this->fulltextResource->getTable('catalog_product_entity')],
157-
'cpe.' . $linkField . ' = relation.parent_id',
158-
['cpe.entity_id']
159-
)->where(
160-
'relation.child_id IN (?)',
161-
$childIds
162-
)->distinct(true);
163-
164-
return $connection->fetchCol($select);
165-
}
166-
167131
/**
168132
* Execute full indexation
169133
*
@@ -211,18 +175,4 @@ public function executeRow($id)
211175
{
212176
$this->execute([$id]);
213177
}
214-
215-
/**
216-
* Get Metadata Pool instance.
217-
*
218-
* @return MetadataPool
219-
*/
220-
private function getMetadataPool()
221-
{
222-
if ($this->metadataPool === null) {
223-
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
224-
}
225-
226-
return $this->metadataPool;
227-
}
228178
}

app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\CatalogSearch\Model\ResourceModel;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\EntityManager\MetadataPool;
11+
812
/**
913
* CatalogSearch Fulltext Index resource model
1014
*/
@@ -17,17 +21,27 @@ class Fulltext extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
1721
*/
1822
protected $_eventManager;
1923

24+
/**
25+
* Holder for MetadataPool instance.
26+
*
27+
* @var MetadataPool
28+
*/
29+
private $metadataPool;
30+
2031
/**
2132
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
2233
* @param \Magento\Framework\Event\ManagerInterface $eventManager
2334
* @param string $connectionName
35+
* @param MetadataPool $metadataPool
2436
*/
2537
public function __construct(
2638
\Magento\Framework\Model\ResourceModel\Db\Context $context,
2739
\Magento\Framework\Event\ManagerInterface $eventManager,
28-
$connectionName = null
40+
$connectionName = null,
41+
MetadataPool $metadataPool = null
2942
) {
3043
$this->_eventManager = $eventManager;
44+
$this->metadataPool = $metadataPool ? : ObjectManager::getInstance()->get(MetadataPool::class);
3145
parent::__construct($context, $connectionName);
3246
}
3347

@@ -53,4 +67,29 @@ public function resetSearchResults()
5367
$this->_eventManager->dispatch('catalogsearch_reset_search_result');
5468
return $this;
5569
}
70+
71+
/**
72+
* Retrieve product relations by children.
73+
*
74+
* @param int|array $childIds
75+
* @return array
76+
*/
77+
public function getRelationsByChild($childIds)
78+
{
79+
$connection = $this->getConnection();
80+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
81+
$select = $connection->select()->from(
82+
['relation' => $this->getTable('catalog_product_relation')],
83+
[]
84+
)->join(
85+
['cpe' => $this->getTable('catalog_product_entity')],
86+
'cpe.' . $linkField . ' = relation.parent_id',
87+
['cpe.entity_id']
88+
)->where(
89+
'relation.child_id IN (?)',
90+
$childIds
91+
)->distinct(true);
92+
93+
return $connection->fetchCol($select);
94+
}
5695
}

app/code/Magento/CatalogSearch/Test/Unit/Model/Indexer/FulltextTest.php

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\CatalogSearch\Test\Unit\Model\Indexer;
77

8-
use Magento\Framework\DB\Adapter\Pdo\Mysql;
9-
use Magento\Framework\EntityManager\EntityMetadata;
108
use Magento\Framework\Search\Request\Dimension;
119
use Magento\Framework\Search\Request\DimensionFactory;
1210
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
@@ -56,13 +54,6 @@ class FulltextTest extends \PHPUnit_Framework_TestCase
5654
*/
5755
private $indexSwitcher;
5856

59-
/**
60-
* Holder for MetadataPool mock instance.
61-
*
62-
* @var \Magento\Framework\EntityManager\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
63-
*/
64-
private $metadataPool;
65-
6657
protected function setUp()
6758
{
6859
$this->fullAction = $this->getClassMock(\Magento\CatalogSearch\Model\Indexer\Fulltext\Action\Full::class);
@@ -104,10 +95,6 @@ protected function setUp()
10495
->setMethods(['switchIndex'])
10596
->getMock();
10697

107-
$this->metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class)
108-
->disableOriginalConstructor()
109-
->getMock();
110-
11198
$objectManagerHelper = new ObjectManagerHelper($this);
11299
$this->model = $objectManagerHelper->getObject(
113100
\Magento\CatalogSearch\Model\Indexer\Fulltext::class,
@@ -122,7 +109,6 @@ protected function setUp()
122109
'indexSwitcher' => $this->indexSwitcher,
123110
]
124111
);
125-
$objectManagerHelper->setBackwardCompatibleProperty($this->model, 'metadataPool', $this->metadataPool);
126112
}
127113

128114
/**
@@ -139,13 +125,15 @@ public function testExecute()
139125
$ids = [1, 2, 3];
140126
$stores = [0 => 'Store 1', 1 => 'Store 2'];
141127
$indexData = new \ArrayObject([]);
128+
$this->fulltextResource->expects($this->exactly(2))
129+
->method('getRelationsByChild')
130+
->willReturn($ids);
142131
$this->storeManager->expects($this->once())->method('getStores')->willReturn($stores);
143132
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
144133
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
145134
$this->fullAction->expects($this->exactly(2))
146135
->method('rebuildStoreIndex')
147136
->willReturn(new \ArrayObject([$indexData, $indexData]));
148-
$this->mockGetRelationsByChild($ids);
149137

150138
$this->model->execute($ids);
151139
}
@@ -196,13 +184,15 @@ public function testExecuteList()
196184
$ids = [1, 2, 3];
197185
$stores = [0 => 'Store 1', 1 => 'Store 2'];
198186
$indexData = new \ArrayObject([]);
187+
$this->fulltextResource->expects($this->exactly(2))
188+
->method('getRelationsByChild')
189+
->willReturn($ids);
199190
$this->storeManager->expects($this->once())->method('getStores')->willReturn($stores);
200191
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
201192
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
202193
$this->fullAction->expects($this->exactly(2))
203194
->method('rebuildStoreIndex')
204195
->willReturn(new \ArrayObject([$indexData, $indexData]));
205-
$this->mockGetRelationsByChild($ids);
206196

207197
$this->model->executeList($ids);
208198
}
@@ -212,90 +202,16 @@ public function testExecuteRow()
212202
$id = 1;
213203
$stores = [0 => 'Store 1', 1 => 'Store 2'];
214204
$indexData = new \ArrayObject([]);
205+
$this->fulltextResource->expects($this->exactly(2))
206+
->method('getRelationsByChild')
207+
->willReturn([$id]);
215208
$this->storeManager->expects($this->once())->method('getStores')->willReturn($stores);
216209
$this->saveHandler->expects($this->exactly(count($stores)))->method('deleteIndex');
217210
$this->saveHandler->expects($this->exactly(2))->method('saveIndex');
218211
$this->fullAction->expects($this->exactly(2))
219212
->method('rebuildStoreIndex')
220213
->willReturn(new \ArrayObject([$indexData, $indexData]));
221-
$this->mockGetRelationsByChild([$id]);
222214

223215
$this->model->executeRow($id);
224216
}
225-
226-
/**
227-
* Mock getRelationsByChild() method.
228-
*
229-
* @param array $ids
230-
* @return void
231-
*/
232-
private function mockGetRelationsByChild(array $ids)
233-
{
234-
$testTable1 = 'testTable1';
235-
$testTable2 = 'testTable2';
236-
$fieldForParent = 'testLinkField';
237-
238-
$metadata = $this->getMockBuilder(EntityMetadata::class)
239-
->disableOriginalConstructor()
240-
->getMock();
241-
$metadata->expects($this->exactly(2))
242-
->method('getLinkField')
243-
->willReturn($fieldForParent);
244-
245-
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
246-
->disableOriginalConstructor()
247-
->getMock();
248-
$select->expects($this->exactly(2))
249-
->method('from')
250-
->with(['relation' => $testTable1])
251-
->willReturnSelf();
252-
$select->expects($this->exactly(2))
253-
->method('distinct')
254-
->with(true)
255-
->willReturnSelf();
256-
$select->expects($this->exactly(2))
257-
->method('where')
258-
->with('relation.child_id IN (?)', $ids)
259-
->willReturnSelf();
260-
$select->expects($this->exactly(2))
261-
->method('join')
262-
->with(
263-
['cpe' => $testTable2],
264-
'cpe.' . $fieldForParent . ' = relation.parent_id',
265-
['cpe.entity_id']
266-
)->willReturnSelf();
267-
268-
$connection = $this->getMockBuilder(Mysql::class)
269-
->disableOriginalConstructor()
270-
->getMock();
271-
$connection->expects($this->exactly(2))
272-
->method('select')
273-
->willReturn($select);
274-
$connection->expects($this->exactly(2))
275-
->method('fetchCol')
276-
->with($select)
277-
->willReturn($ids);
278-
279-
$this->fulltextResource->expects($this->exactly(2))
280-
->method('getConnection')
281-
->willReturn($connection);
282-
$this->fulltextResource->expects($this->exactly(4))
283-
->method('getTable')
284-
->withConsecutive(
285-
['catalog_product_relation'],
286-
['catalog_product_entity'],
287-
['catalog_product_relation'],
288-
['catalog_product_entity']
289-
)
290-
->will($this->onConsecutiveCalls(
291-
$testTable1,
292-
$testTable2,
293-
$testTable1,
294-
$testTable2
295-
));
296-
$this->metadataPool->expects($this->exactly(2))
297-
->method('getMetadata')
298-
->with(\Magento\Catalog\Api\Data\ProductInterface::class)
299-
->willReturn($metadata);
300-
}
301217
}

0 commit comments

Comments
 (0)