Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use InvalidArgumentException;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\ElementType;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexQueueOperation;
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateFolderIndexDataEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Event\DataObject\UpdateIndexDataEvent;
use Pimcore\Bundle\GenericDataIndexBundle\Event\UpdateIndexDataEventInterface;
Expand Down Expand Up @@ -105,22 +106,10 @@ public function getRelatedItemsOnUpdateQuery(
}

if (!$element->getClass()->getAllowInherit()) {
if ($includeElement) {
return $this->dbConnection->createQueryBuilder()
->select([
$element->getId(),
"'" . ElementType::DATA_OBJECT->value . "'",
'className',
"'$operation'",
"'$operationTime'",
'0',
])
->from('objects') // just a dummy query to fit into the query builder interface
->where('id = :id')
->setMaxResults(1)
->setParameter('id', $element->getId());
}
return $this->getRelatedItemsQueryBuilder($element, $operation, $operationTime, $includeElement);
}

if ($operation !== IndexQueueOperation::UPDATE->value) {
return null;
}

Expand Down Expand Up @@ -163,4 +152,45 @@ public function getUpdateIndexDataEvent(

throw new InvalidArgumentException('Element must be instance of ' . AbstractObject::class);
}

private function getRelatedItemsQueryBuilder(
Concrete $element,
string $operation,
int $operationTime,
bool $includeElement = false
): ?QueryBuilder {
if (!$includeElement) {
return null;
}

$queryBuilder = $this->dbConnection->createQueryBuilder()
->select($this->getSelectParametersByOperation($element, $operation, $operationTime))
->setMaxResults(1);

if ($operation === IndexQueueOperation::DELETE->value) {
return $queryBuilder->from('DUAL');
}

return $queryBuilder
->from('objects')
->where('id = :id')
->setParameter('id', $element->getId());
}

private function getSelectParametersByOperation(Concrete $element, string $operation, int $operationTime): array
{
$classId = 'className';
if ($operation === IndexQueueOperation::DELETE->value) {
$classId = "'" . $element->getClassId() . "'";
}

return [
$element->getId(),
"'" . ElementType::DATA_OBJECT->value . "'",
$classId,
"'$operation'",
"'$operationTime'",
'0',
];
}
}
54 changes: 36 additions & 18 deletions tests/Functional/SearchIndex/IndexQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,52 @@ public function testAssetSaveProcessQueue(): void
/**
* @throws Exception
*/
public function testElementDeleteWithQueue(): void
public function testAssetDeleteWithQueue(): void
{
$this->checkAndDeleteElement(
TestHelper::createImageAsset(),
$this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME)
);
$asset = TestHelper::createImageAsset();
$assetIndex = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);
$this->consume();

$this->checkAndDeleteElement(
TestHelper::createEmptyDocument(),
$this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME)
);
$this->checkAndDeleteElement($asset, $assetIndex);
$this->consume();

$object = TestHelper::createEmptyObject('', false);
$this->checkAndDeleteElement(
$object,
$this->searchIndexConfigService->getIndexName($object->getClassName())
);
$this->tester->checkDeletedIndexEntry($asset->getId(), $assetIndex);
}

private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
/**
* @throws Exception
*/
public function testDocumentDeleteWithQueue(): void
{
$document = TestHelper::createEmptyDocument();
$documentIndex = $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME);
$this->consume();
$this->tester->checkIndexEntry($element->getId(), $indexName);

$element->delete();
$this->checkAndDeleteElement($document, $documentIndex);
$this->consume();
$this->expectException(Missing404Exception::class);

$this->tester->checkDeletedIndexEntry($document->getId(), $documentIndex);
}

/**
* @throws Exception
*/
public function testDataObjectDeleteWithQueue(): void
{
$object = TestHelper::createEmptyObject();
$objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName());
$this->consume();

$this->checkAndDeleteElement($object, $objectIndex);
$this->consume();

$this->tester->checkDeletedIndexEntry($object->getId(), $objectIndex);
}

private function checkAndDeleteElement(ElementInterface $element, string $indexName): void
{
$this->tester->checkIndexEntry($element->getId(), $indexName);
$element->delete();
}

private function consume(): void
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/Helper/GenericDataIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ public function checkIndexEntry(string $id, string $index): array
return $response;
}

public function checkDeletedIndexEntry(string $id, string $index): void
{
$client = $this->getIndexSearchClient();
$response = $client->get([
'id' => $id,
'index' => $index,
'client' => ['ignore' => [404]],
]);

if (isset($response['found'])) {
$this->assertFalse($response['found'], 'Check OpenSearch document id of element');

return;
}

$this->assertNotContains($id, $response);
}

public function flushIndex()
{
$client = $this->getIndexSearchClient();
Expand Down
Loading