Skip to content

Commit d802655

Browse files
authored
[Bug] Wring index name passed for data objects deletion (#251)
* fix: pass correct index name for data objects deletion * Apply php-cs-fixer changes * code style --------- Co-authored-by: lukmzig <[email protected]>
1 parent 5224888 commit d802655

File tree

3 files changed

+99
-33
lines changed

3 files changed

+99
-33
lines changed

src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use InvalidArgumentException;
2323
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
2424
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
25+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
2526
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateFolderIndexDataEvent;
2627
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateIndexDataEvent;
2728
use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
@@ -105,22 +106,10 @@ public function getRelatedItemsOnUpdateQuery(
105106
}
106107

107108
if (!$element->getClass()->getAllowInherit()) {
108-
if ($includeElement) {
109-
return $this->dbConnection->createQueryBuilder()
110-
->select([
111-
$element->getId(),
112-
"'" . ElementType::DATA_OBJECT->value . "'",
113-
'className',
114-
"'$operation'",
115-
"'$operationTime'",
116-
'0',
117-
])
118-
->from('objects') // just a dummy query to fit into the query builder interface
119-
->where('id = :id')
120-
->setMaxResults(1)
121-
->setParameter('id', $element->getId());
122-
}
109+
return $this->getRelatedItemsQueryBuilder($element, $operation, $operationTime, $includeElement);
110+
}
123111

112+
if ($operation !== IndexQueueOperation::UPDATE->value) {
124113
return null;
125114
}
126115

@@ -163,4 +152,45 @@ public function getUpdateIndexDataEvent(
163152

164153
throw new InvalidArgumentException('Element must be instance of ' . AbstractObject::class);
165154
}
155+
156+
private function getRelatedItemsQueryBuilder(
157+
Concrete $element,
158+
string $operation,
159+
int $operationTime,
160+
bool $includeElement = false
161+
): ?QueryBuilder {
162+
if (!$includeElement) {
163+
return null;
164+
}
165+
166+
$queryBuilder = $this->dbConnection->createQueryBuilder()
167+
->select($this->getSelectParametersByOperation($element, $operation, $operationTime))
168+
->setMaxResults(1);
169+
170+
if ($operation === IndexQueueOperation::DELETE->value) {
171+
return $queryBuilder->from('DUAL');
172+
}
173+
174+
return $queryBuilder
175+
->from('objects')
176+
->where('id = :id')
177+
->setParameter('id', $element->getId());
178+
}
179+
180+
private function getSelectParametersByOperation(Concrete $element, string $operation, int $operationTime): array
181+
{
182+
$classId = 'className';
183+
if ($operation === IndexQueueOperation::DELETE->value) {
184+
$classId = "'" . $element->getClassId() . "'";
185+
}
186+
187+
return [
188+
$element->getId(),
189+
"'" . ElementType::DATA_OBJECT->value . "'",
190+
$classId,
191+
"'$operation'",
192+
"'$operationTime'",
193+
'0',
194+
];
195+
}
166196
}

tests/Functional/SearchIndex/IndexQueueTest.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,34 +137,52 @@ public function testAssetSaveProcessQueue(): void
137137
/**
138138
* @throws Exception
139139
*/
140-
public function testElementDeleteWithQueue(): void
140+
public function testAssetDeleteWithQueue(): void
141141
{
142-
$this->checkAndDeleteElement(
143-
TestHelper::createImageAsset(),
144-
$this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME)
145-
);
142+
$asset = TestHelper::createImageAsset();
143+
$assetIndex = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);
144+
$this->consume();
146145

147-
$this->checkAndDeleteElement(
148-
TestHelper::createEmptyDocument(),
149-
$this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME)
150-
);
146+
$this->checkAndDeleteElement($asset, $assetIndex);
147+
$this->consume();
151148

152-
$object = TestHelper::createEmptyObject('', false);
153-
$this->checkAndDeleteElement(
154-
$object,
155-
$this->searchIndexConfigService->getIndexName($object->getClassName())
156-
);
149+
$this->tester->checkDeletedIndexEntry($asset->getId(), $assetIndex);
157150
}
158151

159-
private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
152+
/**
153+
* @throws Exception
154+
*/
155+
public function testDocumentDeleteWithQueue(): void
160156
{
157+
$document = TestHelper::createEmptyDocument();
158+
$documentIndex = $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME);
161159
$this->consume();
162-
$this->tester->checkIndexEntry($element->getId(), $indexName);
163160

164-
$element->delete();
161+
$this->checkAndDeleteElement($document, $documentIndex);
165162
$this->consume();
166-
$this->expectException(Missing404Exception::class);
163+
164+
$this->tester->checkDeletedIndexEntry($document->getId(), $documentIndex);
165+
}
166+
167+
/**
168+
* @throws Exception
169+
*/
170+
public function testDataObjectDeleteWithQueue(): void
171+
{
172+
$object = TestHelper::createEmptyObject();
173+
$objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName());
174+
$this->consume();
175+
176+
$this->checkAndDeleteElement($object, $objectIndex);
177+
$this->consume();
178+
179+
$this->tester->checkDeletedIndexEntry($object->getId(), $objectIndex);
180+
}
181+
182+
private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
183+
{
167184
$this->tester->checkIndexEntry($element->getId(), $indexName);
185+
$element->delete();
168186
}
169187

170188
private function consume(): void

tests/Support/Helper/GenericDataIndex.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ public function checkIndexEntry(string $id, string $index): array
147147
return $response;
148148
}
149149

150+
public function checkDeletedIndexEntry(string $id, string $index): void
151+
{
152+
$client = $this->getIndexSearchClient();
153+
$response = $client->get([
154+
'id' => $id,
155+
'index' => $index,
156+
'client' => ['ignore' => [404]],
157+
]);
158+
159+
if (isset($response['found'])) {
160+
$this->assertFalse($response['found'], 'Check OpenSearch document id of element');
161+
162+
return;
163+
}
164+
165+
$this->assertNotContains($id, $response);
166+
}
167+
150168
public function flushIndex()
151169
{
152170
$client = $this->getIndexSearchClient();

0 commit comments

Comments
 (0)