Skip to content

Commit d33e205

Browse files
committed
MAGETWO-65245: parent_id of deleted configurable product variation does not get sent to ElasticSearch
1 parent 0b918af commit d33e205

File tree

2 files changed

+89
-93
lines changed

2 files changed

+89
-93
lines changed

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
}

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\CatalogSearch\Model\ResourceModel\Fulltext;
1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\EntityManager\EntityMetadata;
13+
use Magento\Framework\EntityManager\MetadataPool;
1214
use Magento\Framework\Model\ResourceModel\Db\Context;
1315
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1416

@@ -29,6 +31,13 @@ class FulltextTest extends \PHPUnit_Framework_TestCase
2931
*/
3032
private $context;
3133

34+
/**
35+
* Holder for MetadataPool mock object.
36+
*
37+
* @var MetadataPool|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $metadataPool;
40+
3241
/**
3342
* @var Fulltext
3443
*/
@@ -51,12 +60,16 @@ protected function setUp()
5160
$this->resource->expects($this->once())
5261
->method('getConnection')
5362
->willReturn($this->connection);
63+
$this->metadataPool = $this->getMockBuilder(MetadataPool::class)
64+
->disableOriginalConstructor()
65+
->getMock();
5466

5567
$objectManager = new ObjectManager($this);
5668
$this->target = $objectManager->getObject(
5769
\Magento\CatalogSearch\Model\ResourceModel\Fulltext::class,
5870
[
5971
'context' => $this->context,
72+
'metadataPool' => $this->metadataPool
6073
]
6174
);
6275
}
@@ -74,4 +87,71 @@ public function testResetSearchResult()
7487
$result = $this->target->resetSearchResults();
7588
$this->assertEquals($this->target, $result);
7689
}
90+
91+
/**
92+
* @covers \Magento\CatalogSearch\Model\ResourceModel\Fulltext::getRelationsByChild()
93+
*/
94+
public function testGetRelationsByChild()
95+
{
96+
$ids = [1, 2, 3];
97+
$testTable1 = 'testTable1';
98+
$testTable2 = 'testTable2';
99+
$fieldForParent = 'testLinkField';
100+
101+
$metadata = $this->getMockBuilder(EntityMetadata::class)
102+
->disableOriginalConstructor()
103+
->getMock();
104+
$metadata->expects($this->once())
105+
->method('getLinkField')
106+
->willReturn($fieldForParent);
107+
108+
$this->metadataPool->expects($this->once())
109+
->method('getMetadata')
110+
->with(\Magento\Catalog\Api\Data\ProductInterface::class)
111+
->willReturn($metadata);
112+
113+
$select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
114+
->disableOriginalConstructor()
115+
->getMock();
116+
$select->expects($this->once())
117+
->method('from')
118+
->with(['relation' => $testTable1])
119+
->willReturnSelf();
120+
$select->expects($this->once())
121+
->method('distinct')
122+
->with(true)
123+
->willReturnSelf();
124+
$select->expects($this->once())
125+
->method('where')
126+
->with('relation.child_id IN (?)', $ids)
127+
->willReturnSelf();
128+
$select->expects($this->once())
129+
->method('join')
130+
->with(
131+
['cpe' => $testTable2],
132+
'cpe.' . $fieldForParent . ' = relation.parent_id',
133+
['cpe.entity_id']
134+
)->willReturnSelf();
135+
136+
$this->connection->expects($this->once())
137+
->method('select')
138+
->willReturn($select);
139+
$this->connection->expects($this->once())
140+
->method('fetchCol')
141+
->with($select)
142+
->willReturn($ids);
143+
144+
$this->resource->expects($this->exactly(2))
145+
->method('getTableName')
146+
->withConsecutive(
147+
['catalog_product_relation', ResourceConnection::DEFAULT_CONNECTION],
148+
['catalog_product_entity', ResourceConnection::DEFAULT_CONNECTION]
149+
)
150+
->will($this->onConsecutiveCalls(
151+
$testTable1,
152+
$testTable2
153+
));
154+
155+
self::assertSame($ids, $this->target->getRelationsByChild($ids));
156+
}
77157
}

0 commit comments

Comments
 (0)