diff --git a/Classes/Domain/Index/TcaIndexer/RelationResolver.php b/Classes/Domain/Index/TcaIndexer/RelationResolver.php index fe8dc4c..1ece8ec 100644 --- a/Classes/Domain/Index/TcaIndexer/RelationResolver.php +++ b/Classes/Domain/Index/TcaIndexer/RelationResolver.php @@ -22,6 +22,8 @@ namespace Leonmrni\SearchCore\Domain\Index\TcaIndexer; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\SingletonInterface as Singleton; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Utility\LocalizationUtility; /** * Resolves relations from TCA using TCA. @@ -64,11 +66,7 @@ class RelationResolver implements Singleton continue; } - $record[$column] = $this->resolveValue( - $preprocessedData[$column], - $config, - $column - ); + $record[$column] = $this->resolveValue($preprocessedData[$column], $config); } } @@ -82,27 +80,26 @@ class RelationResolver implements Singleton * exactly that we need to tackle this place. * * @param string $value The value from FormEngine to resolve. + * @param array $config The tca config of the relation. * * @return array|string */ - protected function resolveValue($value) + protected function resolveValue($value, array $config) { - $newValue = []; if ($value === '' || $value === '0') { return ''; } - - if (strpos($value, '|') === false) { - return $value; + if (strpos($value, '|') !== false) { + return $this->resolveSelectValue($value); + } + if (strpos($value, ',') !== false) { + return $this->resolveInlineValue($value, $config['foreign_table']); + } + if ($config['type'] === 'select' && is_array($config['items'])) { + return $this->resolveSelectItemValue($value, $config['items']); } - foreach (\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $value) as $value) { - $value = substr($value, strpos($value, '|') + 1); - $value = rawurldecode($value); - $newValue[] = $value; - } - - return $newValue; + return ''; } /** @@ -116,4 +113,61 @@ class RelationResolver implements Singleton || (isset($config['internal_type']) && strtolower($config['internal_type']) === 'db') ; } + + /** + * Resolves internal representation of select to array of labels. + * + * @param string $value + * @return array + */ + protected function resolveSelectValue($value) + { + $newValue = []; + + foreach (GeneralUtility::trimExplode(',', $value) as $value) { + $value = substr($value, strpos($value, '|') + 1); + $value = rawurldecode($value); + $newValue[] = $value; + } + + return $newValue; + } + + /** + * @param string $value + * @param string $table + * + * @return array + */ + protected function resolveInlineValue($value, $table) + { + $newValue = []; + $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $table, 'uid in (' . $value . ')'); + if ($records === null) { + return $newValue; + } + + foreach ($records as $record) { + $newValue[] = BackendUtility::getRecordTitle($table, $record); + } + + return $newValue; + } + + /** + * @param string $value + * @param array $items + * + * @return string + */ + protected function resolveSelectItemValue($value, array $items) + { + foreach ($items as $item) { + if ($item[1] === $value) { + return LocalizationUtility::translate($item[0], ''); + } + } + + return ''; + } } diff --git a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php index a6120da..9f9b50b 100644 --- a/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php +++ b/Tests/Functional/Connection/Elasticsearch/IndexTcaTableTest.php @@ -146,7 +146,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $this->assertArraySubset( ['_source' => [ 'uid' => '9', - 'CType' => 'textmedia', // Testing items + 'CType' => 'Header', // Testing items 'categories' => ['Category 2', 'Category 1'], // Testing mm (with sorting) ]], $response->getData()['hits']['hits'][0], @@ -158,7 +158,7 @@ class IndexTcaTableTest extends AbstractFunctionalTestCase $this->assertArraySubset( ['_source' => [ 'uid' => '10', - 'CType' => 'textmedia', + 'CType' => 'Header', 'categories' => ['Category 2'], ]], $response->getData()['hits']['hits'][0], diff --git a/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml b/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml index 70e924b..7a633a8 100644 --- a/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml +++ b/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml @@ -7,7 +7,7 @@ 1480686370 0 72 - textmedia + header
test
Record that can be processed by hook 0 diff --git a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml index f25a402..75a1f35 100644 --- a/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml +++ b/Tests/Functional/Fixtures/Indexing/IndexTcaTable.xml @@ -7,9 +7,9 @@ 1480686370 0 72 - textmedia + header
indexed content element
- this is the content of textmedia content element that should get indexed + this is the content of header content element that should get indexed 0 0 0 @@ -27,7 +27,7 @@ 1480686370 0 72 - textmedia + header
endtime hidden record
0 @@ -47,7 +47,7 @@ 1480686370 1 72 - textmedia + header
Hidden record
0 diff --git a/Tests/Functional/Fixtures/Indexing/ResolveRelations.xml b/Tests/Functional/Fixtures/Indexing/ResolveRelations.xml index 1c4b5cd..971ecf7 100644 --- a/Tests/Functional/Fixtures/Indexing/ResolveRelations.xml +++ b/Tests/Functional/Fixtures/Indexing/ResolveRelations.xml @@ -7,7 +7,7 @@ 1480686370 0 72 - textmedia + header
Record with relation to multiple sys category record
some content 0 @@ -28,7 +28,7 @@ 1480686370 0 72 - textmedia + header
Record with relation to a single sys category record
some content 0 diff --git a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignDb.xml b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignDb.xml new file mode 100644 index 0000000..c581790 --- /dev/null +++ b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignDb.xml @@ -0,0 +1,25 @@ + + + + 1 + 1 + shortcut + 2,3 +
Record 1
+ 0 +
+ + 2 + 1 + header +
Record 2
+ 0 +
+ + 3 + 1 + header +
Record 3
+ 0 +
+
diff --git a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignMmSelect.xml b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignMmSelect.xml new file mode 100644 index 0000000..ff9ad47 --- /dev/null +++ b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignMmSelect.xml @@ -0,0 +1,69 @@ + + + + 1 + 1 + header +
Record with relation to multiple sys category record
+ 0 + 3 +
+ + + 1 + 1 + 0 + 1 + Category 1 + Category for testing + 0 + 1 + + + + 2 + 1 + 0 + 1 + Category 2 + Another category for testing + 1 + 1 + + + + 3 + 1 + 1 + 1 + Deleted category + + 0 + 1 + + + + 1 + 1 + tt_content + categories + 2 + 2 + + + 2 + 1 + tt_content + categories + 1 + 1 + + + 3 + 1 + tt_content + categories + 3 + 3 + +
diff --git a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/InlineRelation.xml b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/InlineRelation.xml new file mode 100644 index 0000000..b60804d --- /dev/null +++ b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/InlineRelation.xml @@ -0,0 +1,30 @@ + + + + 1 + 0 + 2 + title of file + + + + 1 + 0 + 1 + Metadata title 1 + + + + 2 + 0 + 1 + Metadata title 2 + + + + 3 + 0 + 2 + Metadata title with no relation + + diff --git a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/StaticSelectItems.xml b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/StaticSelectItems.xml new file mode 100644 index 0000000..e2a5319 --- /dev/null +++ b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/StaticSelectItems.xml @@ -0,0 +1,10 @@ + + + + 1 + 1 + list + 1 + 0 + + diff --git a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.xml b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.xml index 0e9cbda..55158ac 100644 --- a/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.xml +++ b/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.xml @@ -25,9 +25,9 @@ 1480686370 0 72 - textmedia + header
indexed content element
- this is the content of textmedia content element that should get indexed + this is the content of header content element that should get indexed 0 0 0 @@ -45,9 +45,9 @@ 1480686370 0 72 - textmedia + header
indexed content element
- this is the content of textmedia content element that should get indexed + this is the content of header content element that should get indexed 0 0 0 @@ -65,7 +65,7 @@ 1480686370 0 72 - textmedia + header
This content should not be indexed due to root line contraints
0 @@ -85,7 +85,7 @@ 1480686370 0 72 - textmedia + header
This content should not be indexed due to root line contraints
0 diff --git a/Tests/Functional/Fixtures/Indexing/UserWhereClause.xml b/Tests/Functional/Fixtures/Indexing/UserWhereClause.xml index 5d50d1c..71212b2 100644 --- a/Tests/Functional/Fixtures/Indexing/UserWhereClause.xml +++ b/Tests/Functional/Fixtures/Indexing/UserWhereClause.xml @@ -7,7 +7,7 @@ 1480686370 0 72 - textmedia + header
Also indexable record
0 diff --git a/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php b/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php new file mode 100644 index 0000000..0010e30 --- /dev/null +++ b/Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php @@ -0,0 +1,119 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use Leonmrni\SearchCore\Domain\Index\TcaIndexer\TcaTableService; +use Leonmrni\SearchCore\Tests\Functional\AbstractFunctionalTestCase; +use TYPO3\CMS\Backend\Utility\BackendUtility; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +class RelationResolverTest extends AbstractFunctionalTestCase +{ + /** + * @test + */ + public function resolveInlineRelation() + { + $this->importDataSet('Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/InlineRelation.xml'); + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + $table = 'sys_file'; + + $subject = $objectManager->get(TcaTableService::class, $table); + $record = BackendUtility::getRecord($table, 1); + $subject->prepareRecord($record); + + $this->assertEquals( + [ + 'title of file', + 'title of file', + ], + $record['metadata'], + 'Inline relation was not resolved as expected.' + ); + } + + /** + * @test + */ + public function resolveStaticSelectItems() + { + $this->importDataSet('Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/StaticSelectItems.xml'); + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + $table = 'tt_content'; + + $subject = $objectManager->get(TcaTableService::class, $table); + $record = BackendUtility::getRecord($table, 1); + $subject->prepareRecord($record); + + $this->assertEquals( + 'Insert Plugin', + $record['CType'], + 'Static select item was not resolved as expected.' + ); + } + + /** + * @test + */ + public function resolveForeignDb() + { + $this->importDataSet('Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignDb.xml'); + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + $table = 'tt_content'; + + $subject = $objectManager->get(TcaTableService::class, $table); + $record = BackendUtility::getRecord($table, 1); + $subject->prepareRecord($record); + + $this->assertEquals( + [ + 'Record 2', + 'Record 3', + ], + $record['records'], + 'Foreign db relation was not resolved as expected.' + ); + } + + /** + * @test + */ + public function resolveForeignMmSelect() + { + $this->importDataSet('Tests/Functional/Fixtures/Indexing/TcaIndexer/RelationResolver/ForeignMmSelect.xml'); + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + $table = 'tt_content'; + + $subject = $objectManager->get(TcaTableService::class, $table); + $record = BackendUtility::getRecord($table, 1); + $subject->prepareRecord($record); + + $this->assertEquals( + [ + 'Category 2', + 'Category 1', + ], + $record['categories'], + 'Foreign mm select relation was not resolved as expected.' + ); + } +}