Skip to content

Commit d89462f

Browse files
committed
Merge branch '1.x' into 2.x
# Conflicts: # tests/Functional/SearchIndex/IndexQueueTest.php
2 parents a0a2baf + bee02be commit d89462f

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;
@@ -108,22 +109,10 @@ public function getRelatedItemsOnUpdateQuery(
108109
}
109110

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

115+
if ($operation !== IndexQueueOperation::UPDATE->value) {
127116
return null;
128117
}
129118

@@ -166,4 +155,45 @@ public function getUpdateIndexDataEvent(
166155

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

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(), true)
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)