Skip to content

Commit 4ee3c54

Browse files
committed
fix: pass correct index name for data objects deletion
1 parent 5224888 commit 4ee3c54

File tree

3 files changed

+100
-33
lines changed

3 files changed

+100
-33
lines changed

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

Lines changed: 47 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,47 @@ 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+
{
163+
if (!$includeElement) {
164+
return null;
165+
}
166+
167+
if ($operation === IndexQueueOperation::UPDATE->value) {
168+
return $this->dbConnection->createQueryBuilder()
169+
->select($this->getSelectArrayByOperation($element, $operation, $operationTime))
170+
->setMaxResults(1)
171+
->from('objects')
172+
->where('id = :id')
173+
->setParameter('id', $element->getId());
174+
}
175+
176+
return $this->dbConnection->createQueryBuilder()
177+
->select($this->getSelectArrayByOperation($element, $operation, $operationTime))
178+
->setMaxResults(1)
179+
->from('DUAL');
180+
}
181+
182+
private function getSelectArrayByOperation(Concrete $element, string $operation, int $operationTime): array
183+
{
184+
$classId = 'className';
185+
if ($operation === IndexQueueOperation::DELETE->value) {
186+
$classId = "'" . $element->getClassId() . "'";
187+
}
188+
189+
return [
190+
$element->getId(),
191+
"'" . ElementType::DATA_OBJECT->value . "'",
192+
$classId,
193+
"'$operation'",
194+
"'$operationTime'",
195+
'0',
196+
];
197+
}
166198
}

tests/Functional/SearchIndex/IndexQueueTest.php

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,38 +133,55 @@ public function testAssetSaveProcessQueue(): void
133133
$result = $this->tester->checkIndexEntry($asset->getId(), $indexName);
134134
$this->assertEquals($asset->getId(), $result['_source']['system_fields']['id']);
135135
}
136+
/**
137+
* @throws Exception
138+
*/
139+
public function testAssetDeleteWithQueue(): void
140+
{
141+
$asset = TestHelper::createImageAsset();
142+
$assetIndex = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);
143+
$this->consume();
144+
145+
$this->checkAndDeleteElement($asset, $assetIndex);
146+
$this->consume();
147+
148+
$this->tester->checkDeletedIndexEntry($asset->getId(), $assetIndex);
149+
}
136150

137151
/**
138152
* @throws Exception
139153
*/
140-
public function testElementDeleteWithQueue(): void
154+
public function testDocumentDeleteWithQueue(): void
141155
{
142-
$this->checkAndDeleteElement(
143-
TestHelper::createImageAsset(),
144-
$this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME)
145-
);
156+
$document = TestHelper::createEmptyDocument();
157+
$documentIndex = $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME);
158+
$this->consume();
146159

147-
$this->checkAndDeleteElement(
148-
TestHelper::createEmptyDocument(),
149-
$this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME)
150-
);
160+
$this->checkAndDeleteElement($document, $documentIndex);
161+
$this->consume();
151162

152-
$object = TestHelper::createEmptyObject('', false);
153-
$this->checkAndDeleteElement(
154-
$object,
155-
$this->searchIndexConfigService->getIndexName($object->getClassName())
156-
);
163+
$this->tester->checkDeletedIndexEntry($document->getId(), $documentIndex);
157164
}
158165

159-
private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
166+
/**
167+
* @throws Exception
168+
*/
169+
public function testDataObjectDeleteWithQueue(): void
160170
{
171+
$object = TestHelper::createEmptyObject();
172+
$objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName());
161173
$this->consume();
162-
$this->tester->checkIndexEntry($element->getId(), $indexName);
163174

164-
$element->delete();
175+
$this->checkAndDeleteElement($object, $objectIndex);
165176
$this->consume();
166-
$this->expectException(Missing404Exception::class);
177+
178+
$this->tester->checkDeletedIndexEntry($object->getId(), $objectIndex);
179+
}
180+
181+
private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
182+
{
167183
$this->tester->checkIndexEntry($element->getId(), $indexName);
184+
$element->delete();
168185
}
169186

170187
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)