Skip to content

Commit df48c9a

Browse files
authored
[Fix][Queue]: Data object folders are not indexed in async mode (#298)
* add async folder deletion * Apply php-cs-fixer changes --------- Co-authored-by: lukmzig <[email protected]>
1 parent 483890a commit df48c9a

File tree

6 files changed

+126
-33
lines changed

6 files changed

+126
-33
lines changed

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ public function getRelatedItemsOnUpdateQuery(
101101
int $operationTime,
102102
bool $includeElement = false
103103
): ?QueryBuilder {
104-
if (!$element instanceof Concrete) {
104+
if (!$element instanceof AbstractObject) {
105105
return null;
106106
}
107107

108-
if (!$element->getClass()->getAllowInherit()) {
108+
if (!$element instanceof Concrete || !$element->getClass()->getAllowInherit()) {
109109
return $this->getRelatedItemsQueryBuilder($element, $operation, $operationTime, $includeElement);
110110
}
111111

@@ -154,7 +154,7 @@ public function getUpdateIndexDataEvent(
154154
}
155155

156156
private function getRelatedItemsQueryBuilder(
157-
Concrete $element,
157+
AbstractObject $element,
158158
string $operation,
159159
int $operationTime,
160160
bool $includeElement = false
@@ -177,20 +177,29 @@ private function getRelatedItemsQueryBuilder(
177177
->setParameter('id', $element->getId());
178178
}
179179

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-
180+
private function getSelectParametersByOperation(
181+
AbstractObject $element,
182+
string $operation,
183+
int $operationTime
184+
): array {
187185
return [
188186
$element->getId(),
189187
"'" . ElementType::DATA_OBJECT->value . "'",
190-
$classId,
188+
$this->getIndexName($element, $operation),
191189
"'$operation'",
192190
"'$operationTime'",
193191
'0',
194192
];
195193
}
194+
195+
private function getIndexName(AbstractObject $element, string $operation): string
196+
{
197+
if ($element instanceof Concrete) {
198+
return $operation === IndexQueueOperation::DELETE->value
199+
? "'{$element->getClassName()}'"
200+
: 'className';
201+
}
202+
203+
return "'" . IndexName::DATA_OBJECT_FOLDER->value . "'";
204+
}
196205
}

tests/Functional/SearchIndex/AssetBasicTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
namespace Functional\SearchIndex;
1717

18+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
1819
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Document;
1920
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Folder;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Image;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\SearchResult\SearchResultItem\Video;
2223
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
2324
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
2425
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
26+
use Pimcore\Db;
2527
use Pimcore\Tests\Support\Util\TestHelper;
2628

2729
class AssetBasicTest extends \Codeception\Test\Unit
@@ -72,6 +74,33 @@ public function testAssetIndexing()
7274

7375
}
7476

77+
public function testFolderIndexingAsynchronous()
78+
{
79+
$this->tester->disableSynchronousProcessing();
80+
$searchIndexConfigService = $this->tester->grabService(SearchIndexConfigServiceInterface::class);
81+
$indexName = $searchIndexConfigService->getIndexName(IndexName::ASSET->value);
82+
$folder = TestHelper::createAssetFolder();
83+
84+
$folder->setKey('my-test-folder');
85+
$folder->save();
86+
87+
$this->assertGreaterThan(
88+
0,
89+
Db::get()->fetchOne(
90+
'select count(elementId) from generic_data_index_queue where elementId = ? and elementType="asset"',
91+
[$folder->getId()]
92+
)
93+
);
94+
$this->tester->consume();
95+
96+
$response = $this->tester->checkIndexEntry($folder->getId(), $indexName);
97+
$this->assertEquals('my-test-folder', $response['_source']['system_fields']['key']);
98+
99+
$folder->delete();
100+
$this->tester->consume();
101+
$this->tester->checkDeletedIndexEntry($folder->getId(), $indexName);
102+
}
103+
75104
public function testAssetSearch()
76105
{
77106
$asset = TestHelper::createImageAsset();

tests/Functional/SearchIndex/DataObjectBasicTest.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testIndexingWithInheritanceAsynchronous()
8787
[$object->getId()]
8888
)
8989
);
90-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
90+
$this->tester->consume();
9191
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
9292
$this->assertEquals($object->getKey(), $response['_source']['system_fields']['key']);
9393
}
@@ -126,11 +126,37 @@ public function testIndexingWithoutInheritanceAsynchronous()
126126
[$object->getId()]
127127
)
128128
);
129-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
129+
$this->tester->consume();
130130
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
131131
$this->assertEquals($object->getKey(), $response['_source']['system_fields']['key']);
132132
}
133133

134+
public function testFolderIndexingAsynchronous()
135+
{
136+
$this->tester->disableSynchronousProcessing();
137+
$object = TestHelper::createObjectFolder();
138+
$indexName = $this->searchIndexConfigService->getIndexName(IndexName::DATA_OBJECT_FOLDER->value);
139+
140+
$object->setKey('my-test-folder');
141+
$object->save();
142+
143+
$this->assertGreaterThan(
144+
0,
145+
Db::get()->fetchOne(
146+
'select count(elementId) from generic_data_index_queue where elementId = ? and elementType="dataObject"',
147+
[$object->getId()]
148+
)
149+
);
150+
$this->tester->consume();
151+
152+
$response = $this->tester->checkIndexEntry($object->getId(), $indexName);
153+
$this->assertEquals('my-test-folder', $response['_source']['system_fields']['key']);
154+
155+
$object->delete();
156+
$this->tester->consume();
157+
$this->tester->checkDeletedIndexEntry($object->getId(), $indexName);
158+
}
159+
134160
public function testFolderIndexing()
135161
{
136162
$object = TestHelper::createObjectFolder();
@@ -189,19 +215,19 @@ public function testSettingsStoreMapping()
189215
$this->assertNull($settingsStoreService->getClassMappingCheckSum($classId));
190216

191217
$class->save();
192-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
218+
$this->tester->consume();
193219
$checkSum = $settingsStoreService->getClassMappingCheckSum($classId);
194220
$this->assertNotNull($checkSum);
195221

196222
$class->save();
197-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
223+
$this->tester->consume();
198224
$this->assertEquals($checkSum, $settingsStoreService->getClassMappingCheckSum($classId));
199225

200226
$input = new Input();
201227
$input->setName('settingsTest');
202228
$class->addFieldDefinition('settingsTest', $input);
203229
$class->save();
204-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
230+
$this->tester->consume();
205231
$this->assertNotEquals($checkSum, $settingsStoreService->getClassMappingCheckSum($classId));
206232
}
207233

@@ -216,7 +242,7 @@ public function testClassDefinitionMapping(): void
216242
$input->setName('mappingTest');
217243
$class->addFieldDefinition('mappingTest', $input);
218244
$class->save();
219-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
245+
$this->tester->consume();
220246

221247
$indexName = $this->tester->getIndexName($object->getClassName());
222248
$mapping = $this->tester->getIndexMapping($index);
@@ -225,7 +251,7 @@ public function testClassDefinitionMapping(): void
225251

226252
$class->setFieldDefinitions($originalFields);
227253
$class->save();
228-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
254+
$this->tester->consume();
229255

230256
$indexName = $this->tester->getIndexName($object->getClassName());
231257
$mapping = $this->tester->getIndexMapping($index);

tests/Functional/SearchIndex/DocumentBasicTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Codeception\Test\Unit;
2020
use Exception;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Enum\SearchIndex\IndexName;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\SearchResultItem\Email;
2223
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\SearchResultItem\Folder;
2324
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Document\SearchResult\SearchResultItem\HardLink;
@@ -28,6 +29,7 @@
2829
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface;
2930
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\SearchIndexConfigServiceInterface;
3031
use Pimcore\Bundle\GenericDataIndexBundle\Tests\IndexTester;
32+
use Pimcore\Db;
3133
use Pimcore\Tests\Support\Util\TestHelper;
3234

3335
/**
@@ -58,7 +60,7 @@ public function testDocumentIndexing()
5860
$indexName = $searchIndexConfigService->getIndexName('document');
5961

6062
$document = TestHelper::createEmptyDocument();
61-
$documentId = (string)$document->getId();
63+
$documentId = $document->getId();
6264

6365
// check indexed
6466
$response = $this->tester->checkIndexEntry($documentId, $indexName);
@@ -74,6 +76,33 @@ public function testDocumentIndexing()
7476
$this->tester->checkDeletedIndexEntry($documentId, $indexName);
7577
}
7678

79+
public function testFolderIndexingAsynchronous()
80+
{
81+
$this->tester->disableSynchronousProcessing();
82+
$searchIndexConfigService = $this->tester->grabService(SearchIndexConfigServiceInterface::class);
83+
$indexName = $searchIndexConfigService->getIndexName(IndexName::DOCUMENT->value);
84+
$folder = TestHelper::createDocumentFolder();
85+
86+
$folder->setKey('my-test-folder');
87+
$folder->save();
88+
89+
$this->assertGreaterThan(
90+
0,
91+
Db::get()->fetchOne(
92+
'select count(elementId) from generic_data_index_queue where elementId = ? and elementType="document"',
93+
[$folder->getId()]
94+
)
95+
);
96+
$this->tester->consume();
97+
98+
$response = $this->tester->checkIndexEntry($folder->getId(), $indexName);
99+
$this->assertEquals('my-test-folder', $response['_source']['system_fields']['key']);
100+
101+
$folder->delete();
102+
$this->tester->consume();
103+
$this->tester->checkDeletedIndexEntry($folder->getId(), $indexName);
104+
}
105+
77106
/**
78107
* @throws Exception
79108
*/

tests/Functional/SearchIndex/IndexQueueTest.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testAssetSaveProcessQueue(): void
127127
)
128128
);
129129

130-
$this->consume();
130+
$this->tester->consume();
131131
$result = $this->tester->checkIndexEntry($asset->getId(), $indexName);
132132
$this->assertEquals($asset->getId(), $result['_source']['system_fields']['id']);
133133
}
@@ -139,10 +139,10 @@ public function testAssetDeleteWithQueue(): void
139139
{
140140
$asset = TestHelper::createImageAsset();
141141
$assetIndex = $this->searchIndexConfigService->getIndexName(self::ASSET_INDEX_NAME);
142-
$this->consume();
142+
$this->tester->consume();
143143

144144
$this->checkAndDeleteElement($asset, $assetIndex);
145-
$this->consume();
145+
$this->tester->consume();
146146

147147
$this->tester->checkDeletedIndexEntry($asset->getId(), $assetIndex);
148148
}
@@ -154,10 +154,10 @@ public function testDocumentDeleteWithQueue(): void
154154
{
155155
$document = TestHelper::createEmptyDocument();
156156
$documentIndex = $this->searchIndexConfigService->getIndexName(self::DOCUMENT_INDEX_NAME);
157-
$this->consume();
157+
$this->tester->consume();
158158

159159
$this->checkAndDeleteElement($document, $documentIndex);
160-
$this->consume();
160+
$this->tester->consume();
161161

162162
$this->tester->checkDeletedIndexEntry($document->getId(), $documentIndex);
163163
}
@@ -169,10 +169,10 @@ public function testDataObjectDeleteWithQueue(): void
169169
{
170170
$object = TestHelper::createEmptyObject();
171171
$objectIndex = $this->searchIndexConfigService->getIndexName($object->getClassName());
172-
$this->consume();
172+
$this->tester->consume();
173173

174174
$this->checkAndDeleteElement($object, $objectIndex);
175-
$this->consume();
175+
$this->tester->consume();
176176

177177
$this->tester->checkDeletedIndexEntry($object->getId(), $objectIndex);
178178
}
@@ -182,9 +182,4 @@ private function checkAndDeleteElement(ElementInterface $element, string $indexN
182182
$this->tester->checkIndexEntry($element->getId(), $indexName);
183183
$element->delete();
184184
}
185-
186-
private function consume(): void
187-
{
188-
$this->tester->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
189-
}
190185
}

tests/Support/Helper/GenericDataIndex.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getIndexSearchClient(): mixed
132132
return $this->grabService('generic-data-index.search-client');
133133
}
134134

135-
public function checkIndexEntry(string $id, string $index): array
135+
public function checkIndexEntry(int|string $id, string $index): array
136136
{
137137
/** @var SearchClientInterface $client */
138138
$client = $this->getIndexSearchClient();
@@ -146,7 +146,7 @@ public function checkIndexEntry(string $id, string $index): array
146146
return $response;
147147
}
148148

149-
public function checkDeletedIndexEntry(string $id, string $index): void
149+
public function checkDeletedIndexEntry(int|string $id, string $index): void
150150
{
151151
/** @var SearchClientInterface $client */
152152
$client = $this->getIndexSearchClient();
@@ -249,4 +249,9 @@ public function getIndexMapping(string $indexName): array
249249

250250
return $client->getIndexMapping(['index' => $indexName]);
251251
}
252+
253+
public function consume(): void
254+
{
255+
$this->runCommand('messenger:consume', ['--limit'=>2], ['pimcore_generic_data_index_queue']);
256+
}
252257
}

0 commit comments

Comments
 (0)