From a232ae0b3679ece95ae940b6a3bb3ea68ce6d5b1 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Wed, 14 Mar 2018 21:32:42 +0100 Subject: [PATCH] BUGFIX: Respect page uid of content records Do not index content records of all pages while indexing pages in CMS 8.x. We add same logic as for CMS 7.x, we extend the query to only fetch records from current page. Resolves: #135 --- .../Domain/Index/TcaIndexer/PagesIndexer.php | 10 +++++-- .../Elasticsearch/IndexTcaTableTest.php | 19 +++++++------ .../Fixtures/Indexing/IndexTcaTable.xml | 27 +++++++++++++++++++ .../Functional/Indexing/PagesIndexerTest.php | 2 +- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php index 7124ca1..3f0d39b 100644 --- a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php +++ b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php @@ -79,8 +79,14 @@ class PagesIndexer extends TcaIndexer protected function fetchContentForPage(int $uid) : array { if ($this->contentTableService instanceof TcaTableService) { - $contentElements = $this->contentTableService->getQuery() - ->execute()->fetchAll(); + $queryBuilder = $this->contentTableService->getQuery(); + $queryBuilder->andWhere( + $queryBuilder->expr()->eq( + $this->contentTableService->getTableName() . '.pid', + $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT) + ) + ); + $contentElements = $queryBuilder->execute()->fetchAll(); } else { $contentElements = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows( $this->contentTableService->getFields(), diff --git a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php index 19245bf..ae671dd 100644 --- a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php +++ b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php @@ -50,11 +50,10 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $response = $this->client->request('typo3content/_search?q=*:*'); $this->assertTrue($response->isOk(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 2, 'Not exactly 2 documents were indexed.'); - $this->assertArraySubset( - ['_source' => ['header' => 'indexed content element']], - $response->getData()['hits']['hits'][1], - false, + $this->assertSame($response->getData()['hits']['total'], 3, 'Not exactly 3 documents were indexed.'); + $this->assertSame( + 'indexed content element', + $response->getData()['hits']['hits'][2]['_source']['header'], 'Record was not indexed.' ); } @@ -113,7 +112,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $response = $this->client->request('typo3content/_search?q=*:*'); $this->assertTrue($response->isOk(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 2, 'Not exactly 2 documents were indexed.'); + $this->assertSame($response->getData()['hits']['total'], 3, 'Not exactly 3 documents were indexed.'); } /** @@ -136,7 +135,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $response = $this->client->request('typo3content/_search?q=*:*'); $this->assertTrue($response->isOk(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 3, 'Not exactly 3 documents were indexed.'); + $this->assertSame($response->getData()['hits']['total'], 4, 'Not exactly 4 documents were indexed.'); $response = $this->client->request('typo3content/_search?q=uid:11'); $this->assertArraySubset( ['_source' => ['header' => 'Also indexable record']], @@ -168,7 +167,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $response = $this->client->request('typo3content/_search?q=*:*'); $this->assertTrue($response->isOk(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 4, 'Not exactly 4 documents were indexed.'); + $this->assertSame($response->getData()['hits']['total'], 5, 'Not exactly 5 documents were indexed.'); $response = $this->client->request('typo3content/_search?q=uid:11'); $this->assertArraySubset( @@ -218,7 +217,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase ; $response = $this->client->request('typo3content/_search?q=*:*'); - $this->assertSame($response->getData()['hits']['total'], 2, 'Not exactly 2 documents were indexed.'); + $this->assertSame($response->getData()['hits']['total'], 3, 'Not exactly 3 documents were indexed.'); if ($this->isLegacyVersion()) { $this->getDatabaseConnection() @@ -239,6 +238,6 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase ; $response = $this->client->request('typo3content/_search?q=*:*'); - $this->assertSame($response->getData()['hits']['total'], 1, 'Not exactly 1 document is in index.'); + $this->assertSame($response->getData()['hits']['total'], 2, 'Not exactly 2 document is in index.'); } } diff --git a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml index c236f07..409e46c 100644 --- a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml +++ b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml @@ -99,4 +99,31 @@ 0 0 + + + 100 + 2 + 1480686370 + 1480686370 + 0 + 72 + header +
Indexed on page 2
+ This element is on a different page + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 +
+ + + 2 + 1 + Second page with content + Used to check whether content is indexed only for parent page. + diff --git a/Tests/Functional/Indexing/PagesIndexerTest.php b/Tests/Functional/Indexing/PagesIndexerTest.php index 244413d..bc26dfd 100644 --- a/Tests/Functional/Indexing/PagesIndexerTest.php +++ b/Tests/Functional/Indexing/PagesIndexerTest.php @@ -48,7 +48,7 @@ class PagesIndexerTest extends AbstractFunctionalTestCase ->with( $this->stringContains($tableName), $this->callback(function ($documents) { - return count($documents) === 1 + return count($documents) === 2 && isset($documents[0]['content']) && $documents[0]['content'] === 'this is the content of header content element that should get indexed Some text in paragraph' && isset($documents[0]['search_abstract']) && $documents[0]['search_abstract'] ===