From 4ee3c5469d63713b89c79a81bb124467fb8fda1d Mon Sep 17 00:00:00 2001 From: lukmzig Date: Wed, 13 Nov 2024 09:14:37 +0100 Subject: [PATCH 1/3] fix: pass correct index name for data objects deletion --- .../DataObjectTypeAdapter.php | 62 ++++++++++++++----- .../Functional/SearchIndex/IndexQueueTest.php | 53 ++++++++++------ tests/Support/Helper/GenericDataIndex.php | 18 ++++++ 3 files changed, 100 insertions(+), 33 deletions(-) diff --git a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php index 8cb39284..95b2502d 100644 --- a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php +++ b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php @@ -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; @@ -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; } @@ -163,4 +152,47 @@ 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; + } + + if ($operation === IndexQueueOperation::UPDATE->value) { + return $this->dbConnection->createQueryBuilder() + ->select($this->getSelectArrayByOperation($element, $operation, $operationTime)) + ->setMaxResults(1) + ->from('objects') + ->where('id = :id') + ->setParameter('id', $element->getId()); + } + + return $this->dbConnection->createQueryBuilder() + ->select($this->getSelectArrayByOperation($element, $operation, $operationTime)) + ->setMaxResults(1) + ->from('DUAL'); + } + + private function getSelectArrayByOperation(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', + ]; + } } diff --git a/tests/Functional/SearchIndex/IndexQueueTest.php b/tests/Functional/SearchIndex/IndexQueueTest.php index 90d8a170..2349b0bc 100644 --- a/tests/Functional/SearchIndex/IndexQueueTest.php +++ b/tests/Functional/SearchIndex/IndexQueueTest.php @@ -133,38 +133,55 @@ public function testAssetSaveProcessQueue(): void $result = $this->tester->checkIndexEntry($asset->getId(), $indexName); $this->assertEquals($asset->getId(), $result['_source']['system_fields']['id']); } + /** + * @throws Exception + */ + public function testAssetDeleteWithQueue(): void + { + $asset = TestHelper::createImageAsset(); + $assetIndex = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME); + $this->consume(); + + $this->checkAndDeleteElement($asset, $assetIndex); + $this->consume(); + + $this->tester->checkDeletedIndexEntry($asset->getId(), $assetIndex); + } /** * @throws Exception */ - public function testElementDeleteWithQueue(): void + public function testDocumentDeleteWithQueue(): void { - $this->checkAndDeleteElement( - TestHelper::createImageAsset(), - $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME) - ); + $document = TestHelper::createEmptyDocument(); + $documentIndex = $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME); + $this->consume(); - $this->checkAndDeleteElement( - TestHelper::createEmptyDocument(), - $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME) - ); + $this->checkAndDeleteElement($document, $documentIndex); + $this->consume(); - $object = TestHelper::createEmptyObject('', false); - $this->checkAndDeleteElement( - $object, - $this->searchIndexConfigService->getIndexName($object->getClassName()) - ); + $this->tester->checkDeletedIndexEntry($document->getId(), $documentIndex); } - private function checkAndDeleteElement(ElementInterface $element, string $indexName): void + /** + * @throws Exception + */ + public function testDataObjectDeleteWithQueue(): void { + $object = TestHelper::createEmptyObject(); + $objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName()); $this->consume(); - $this->tester->checkIndexEntry($element->getId(), $indexName); - $element->delete(); + $this->checkAndDeleteElement($object, $objectIndex); $this->consume(); - $this->expectException(Missing404Exception::class); + + $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 diff --git a/tests/Support/Helper/GenericDataIndex.php b/tests/Support/Helper/GenericDataIndex.php index c60a8132..c9f82e49 100644 --- a/tests/Support/Helper/GenericDataIndex.php +++ b/tests/Support/Helper/GenericDataIndex.php @@ -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(); From 631fb12b5412344ccbca92be41831c6ba9c36987 Mon Sep 17 00:00:00 2001 From: lukmzig Date: Wed, 13 Nov 2024 08:16:21 +0000 Subject: [PATCH 2/3] Apply php-cs-fixer changes --- .../IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php | 3 +-- tests/Functional/SearchIndex/IndexQueueTest.php | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php index 95b2502d..ab747956 100644 --- a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php +++ b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php @@ -158,8 +158,7 @@ private function getRelatedItemsQueryBuilder( string $operation, int $operationTime, bool $includeElement = false - ): ?QueryBuilder - { + ): ?QueryBuilder { if (!$includeElement) { return null; } diff --git a/tests/Functional/SearchIndex/IndexQueueTest.php b/tests/Functional/SearchIndex/IndexQueueTest.php index 2349b0bc..04aa188f 100644 --- a/tests/Functional/SearchIndex/IndexQueueTest.php +++ b/tests/Functional/SearchIndex/IndexQueueTest.php @@ -133,6 +133,7 @@ public function testAssetSaveProcessQueue(): void $result = $this->tester->checkIndexEntry($asset->getId(), $indexName); $this->assertEquals($asset->getId(), $result['_source']['system_fields']['id']); } + /** * @throws Exception */ From f756fc1ded931f172a69fe108f9fd6df48c18aad Mon Sep 17 00:00:00 2001 From: lukmzig Date: Wed, 13 Nov 2024 09:22:46 +0100 Subject: [PATCH 3/3] code style --- .../DataObjectTypeAdapter.php | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php index ab747956..f7f1ce02 100644 --- a/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php +++ b/src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php @@ -163,22 +163,21 @@ private function getRelatedItemsQueryBuilder( return null; } - if ($operation === IndexQueueOperation::UPDATE->value) { - return $this->dbConnection->createQueryBuilder() - ->select($this->getSelectArrayByOperation($element, $operation, $operationTime)) - ->setMaxResults(1) - ->from('objects') - ->where('id = :id') - ->setParameter('id', $element->getId()); + $queryBuilder = $this->dbConnection->createQueryBuilder() + ->select($this->getSelectParametersByOperation($element, $operation, $operationTime)) + ->setMaxResults(1); + + if ($operation === IndexQueueOperation::DELETE->value) { + return $queryBuilder->from('DUAL'); } - return $this->dbConnection->createQueryBuilder() - ->select($this->getSelectArrayByOperation($element, $operation, $operationTime)) - ->setMaxResults(1) - ->from('DUAL'); + return $queryBuilder + ->from('objects') + ->where('id = :id') + ->setParameter('id', $element->getId()); } - private function getSelectArrayByOperation(Concrete $element, string $operation, int $operationTime): array + private function getSelectParametersByOperation(Concrete $element, string $operation, int $operationTime): array { $classId = 'className'; if ($operation === IndexQueueOperation::DELETE->value) {