From 6423f510e24cc4cc79f22d652b3cbf3608d00dbf Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 12 Dec 2017 11:26:46 +0100 Subject: [PATCH 1/4] !!!|BUGFIX: Index media field of pages as relation uids Always index media field of pages as array. Index reference_uids for files. --- .../Domain/Index/TcaIndexer/PagesIndexer.php | 21 ++++++ .../Elasticsearch/IndexTcaTableTest.php | 29 ++++++++ .../Fixtures/Indexing/IndexTcaTable.xml | 67 +++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php index d666410..2c0833e 100644 --- a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php +++ b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php @@ -71,6 +71,7 @@ class PagesIndexer extends TcaIndexer } } + $record['media'] = $this->fetchMediaForPage($record['uid']); $content = $this->fetchContentForPage($record['uid']); if ($content !== []) { $record['content'] = $content['content']; @@ -136,4 +137,24 @@ class PagesIndexer extends TcaIndexer return $imageRelationUids; } + + /** + * @param int $uid + * @return [] + */ + protected function fetchMediaForPage($uid) + { + $imageRelationUids = []; + $imageRelations = $this->fileRepository->findByRelation( + 'pages', + 'media', + $uid + ); + + foreach ($imageRelations as $relation) { + $imageRelationUids[] = $relation->getUid(); + } + + return $imageRelationUids; + } } diff --git a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php index 02e6ca3..4267d79 100644 --- a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php +++ b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php @@ -205,4 +205,33 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase 'Record was indexed with resolved category relation, but should not have any.' ); } + + /** + * @test + */ + public function indexPagesMedia() + { + \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class) + ->get(IndexerFactory::class) + ->getIndexer('pages') + ->indexAllDocuments() + ; + + $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' => [ + 'media' => [ + 10, 1 + ] + ] + ], + $response->getData()['hits']['hits'][0], + false, + 'Record was not indexed.' + ); + } } diff --git a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml index 3692135..aa2a8fa 100644 --- a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml +++ b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml @@ -99,4 +99,71 @@ 0 0 + + + 2 + 1 + Example page with + Used as abstract as no abstract is defined. + 2 + + + + 1 + 1 + 1 + 2 + pages + media + 2 + sys_file + + + + + + 10 + 1 + 2 + 2 + pages + media + 1 + sys_file + + + + + + 1 + 0 + 1 + 2 + full/file/path.png + 94dd1f9645114cf1344438ac7188db972768f59f + 67887283a03f465f1004d1d43a157ee1f1c59be5 + png + image/png + path2.png + c1dd34eb34d651ee6ca6ac52dea595db75d8e9d2 + 3044 + 1432306303 + 1432306303 + + + 2 + 0 + 1 + 2 + full/file/path2.png + 94dd1f9645114cf1344438ac7188db972768f59f + 67887283a03f465f1004d1d43a157ee1f1c59be5 + png + image/png + path2.png + c1dd34eb34d651ee6ca6ac52dea595db75d8e9d2 + 3044 + 1432306303 + 1432306303 + From a998f155c1b003d0ef2570ee0d64ec4019d2e4c3 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 12 Dec 2017 11:34:38 +0100 Subject: [PATCH 2/4] TASK: Refactor common code base --- .../Domain/Index/TcaIndexer/PagesIndexer.php | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php index 2c0833e..b5ddf56 100644 --- a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php +++ b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php @@ -124,18 +124,7 @@ class PagesIndexer extends TcaIndexer */ protected function getContentElementImages($uidOfContentElement) { - $imageRelationUids = []; - $imageRelations = $this->fileRepository->findByRelation( - 'tt_content', - 'image', - $uidOfContentElement - ); - - foreach ($imageRelations as $relation) { - $imageRelationUids[] = $relation->getUid(); - } - - return $imageRelationUids; + return $this->fetchSysFileReferenceUids($uidOfContentElement, 'tt_content', 'image'); } /** @@ -143,13 +132,20 @@ class PagesIndexer extends TcaIndexer * @return [] */ protected function fetchMediaForPage($uid) + { + return $this->fetchSysFileReferenceUids($uid, 'pages', 'media'); + } + + /** + * @param int $uid + * @param string $tablename + * @param string $fieldname + * @return [] + */ + protected function fetchSysFileReferenceUids($uid, $tablename, $fieldname) { $imageRelationUids = []; - $imageRelations = $this->fileRepository->findByRelation( - 'pages', - 'media', - $uid - ); + $imageRelations = $this->fileRepository->findByRelation($tablename, $fieldname, $uid); foreach ($imageRelations as $relation) { $imageRelationUids[] = $relation->getUid(); From 9699a3815888ef09ac26898e44de5c8b132d80e7 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 12 Dec 2017 11:51:17 +0100 Subject: [PATCH 3/4] TASK: Adjust test to expect two pages As we now import two pages into db, adjust test assertion. --- Tests/Functional/Indexing/PagesIndexerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'] === From b6070cdb420569fe704a00d1635e1193cac4aff6 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 12 Dec 2017 11:51:38 +0100 Subject: [PATCH 4/4] TASK: Fix wrong namespace for functional tests --- Tests/Functional/Indexing/PagesIndexerTest.php | 2 +- Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php | 2 +- Tests/Functional/Indexing/TcaIndexerTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Functional/Indexing/PagesIndexerTest.php b/Tests/Functional/Indexing/PagesIndexerTest.php index bc26dfd..bf5a35f 100644 --- a/Tests/Functional/Indexing/PagesIndexerTest.php +++ b/Tests/Functional/Indexing/PagesIndexerTest.php @@ -1,5 +1,5 @@ diff --git a/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php b/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php index a491677..62c5158 100644 --- a/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php +++ b/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php @@ -1,5 +1,5 @@ diff --git a/Tests/Functional/Indexing/TcaIndexerTest.php b/Tests/Functional/Indexing/TcaIndexerTest.php index edf1a74..ff89318 100644 --- a/Tests/Functional/Indexing/TcaIndexerTest.php +++ b/Tests/Functional/Indexing/TcaIndexerTest.php @@ -1,5 +1,5 @@