FEATURE: Resolve further relations

* Resolve static select items
* Add test for further relation resolvings

Relates: #17
This commit is contained in:
Daniel Siepmann 2016-12-20 11:47:15 +01:00
parent 8441dcdff2
commit 9bf2657318
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
12 changed files with 298 additions and 28 deletions

View file

@ -22,6 +22,7 @@ namespace Leonmrni\SearchCore\Domain\Index\TcaIndexer;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\SingletonInterface as Singleton;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Resolves relations from TCA using TCA.
@ -64,11 +65,7 @@ class RelationResolver implements Singleton
continue;
}
$record[$column] = $this->resolveValue(
$preprocessedData[$column],
$config,
$column
);
$record[$column] = $this->resolveValue($preprocessedData[$column], $config);
}
}
@ -82,24 +79,44 @@ 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>|string
*/
protected function resolveValue($value)
protected function resolveValue($value, array $config)
{
$newValue = [];
if ($value === '' || $value === '0') {
return '';
}
if (strpos($value, '|') === false) {
return $value;
// Select
if (strpos($value, '|') !== false) {
foreach (\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $value) as $value) {
$value = substr($value, strpos($value, '|') + 1);
$value = rawurldecode($value);
$newValue[] = $value;
}
return $newValue;
}
foreach (\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $value) as $value) {
$value = substr($value, strpos($value, '|') + 1);
$value = rawurldecode($value);
$newValue[] = $value;
// Inline
if (strpos($value, ',') !== false) {
foreach ($GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', $config['foreign_table'], 'uid in (' . $value . ')') as $record) {
$newValue[] = BackendUtility::getRecordTitle($config['foreign_table'], $record);
}
return $newValue;
}
// Static select items
if ($config['type'] === 'select' && is_array($config['items'])) {
foreach ($config['items'] as $item) {
if ($item[1] === $value) {
return LocalizationUtility::translate($item[0], '');
}
}
}
return $newValue;

View file

@ -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],

View file

@ -7,7 +7,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>test</header>
<bodytext>Record that can be processed by hook</bodytext>
<media>0</media>

View file

@ -7,9 +7,9 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>indexed content element</header>
<bodytext>this is the content of textmedia content element that should get indexed</bodytext>
<bodytext>this is the content of header content element that should get indexed</bodytext>
<media>0</media>
<layout>0</layout>
<deleted>0</deleted>
@ -27,7 +27,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>endtime hidden record</header>
<bodytext></bodytext>
<media>0</media>
@ -47,7 +47,7 @@
<crdate>1480686370</crdate>
<hidden>1</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>Hidden record</header>
<bodytext></bodytext>
<media>0</media>

View file

@ -7,7 +7,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>Record with relation to multiple sys category record</header>
<bodytext>some content</bodytext>
<media>0</media>
@ -28,7 +28,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>Record with relation to a single sys category record</header>
<bodytext>some content</bodytext>
<media>0</media>

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<tt_content>
<uid>1</uid>
<pid>1</pid>
<CType>shortcut</CType>
<records>2,3</records>
<header>Record 1</header>
<deleted>0</deleted>
</tt_content>
<tt_content>
<uid>2</uid>
<pid>1</pid>
<CType>header</CType>
<header>Record 2</header>
<deleted>0</deleted>
</tt_content>
<tt_content>
<uid>3</uid>
<pid>1</pid>
<CType>header</CType>
<header>Record 3</header>
<deleted>0</deleted>
</tt_content>
</dataset>

View file

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<tt_content>
<uid>1</uid>
<pid>1</pid>
<CType>header</CType>
<header>Record with relation to multiple sys category record</header>
<deleted>0</deleted>
<categories>3</categories>
</tt_content>
<sys_category>
<uid>1</uid>
<pid>1</pid>
<deleted>0</deleted>
<sorting>1</sorting>
<title>Category 1</title>
<description>Category for testing</description>
<parent>0</parent>
<items>1</items>
</sys_category>
<sys_category>
<uid>2</uid>
<pid>1</pid>
<deleted>0</deleted>
<sorting>1</sorting>
<title>Category 2</title>
<description>Another category for testing</description>
<parent>1</parent>
<items>1</items>
</sys_category>
<sys_category>
<uid>3</uid>
<pid>1</pid>
<deleted>1</deleted>
<sorting>1</sorting>
<title>Deleted category</title>
<description></description>
<parent>0</parent>
<items>1</items>
</sys_category>
<sys_category_record_mm>
<uid_local>1</uid_local>
<uid_foreign>1</uid_foreign>
<tablenames>tt_content</tablenames>
<fieldname>categories</fieldname>
<sorting>2</sorting>
<sorting_foreign>2</sorting_foreign>
</sys_category_record_mm>
<sys_category_record_mm>
<uid_local>2</uid_local>
<uid_foreign>1</uid_foreign>
<tablenames>tt_content</tablenames>
<fieldname>categories</fieldname>
<sorting>1</sorting>
<sorting_foreign>1</sorting_foreign>
</sys_category_record_mm>
<sys_category_record_mm>
<uid_local>3</uid_local>
<uid_foreign>1</uid_foreign>
<tablenames>tt_content</tablenames>
<fieldname>categories</fieldname>
<sorting>3</sorting>
<sorting_foreign>3</sorting_foreign>
</sys_category_record_mm>
</dataset>

View file

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<sys_file>
<uid>1</uid>
<pid>0</pid>
<metadata>2</metadata>
<name>title of file</name>
</sys_file>
<sys_file_metadata>
<uid>1</uid>
<pid>0</pid>
<file>1</file>
<title>Metadata title 1</title>
</sys_file_metadata>
<sys_file_metadata>
<uid>2</uid>
<pid>0</pid>
<file>1</file>
<title>Metadata title 2</title>
</sys_file_metadata>
<sys_file_metadata>
<uid>3</uid>
<pid>0</pid>
<file>2</file>
<title>Metadata title with no relation</title>
</sys_file_metadata>
</dataset>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<tt_content>
<uid>1</uid>
<pid>1</pid>
<CType>list</CType>
<header_layout>1</header_layout>
<deleted>0</deleted>
</tt_content>
</dataset>

View file

@ -25,9 +25,9 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>indexed content element</header>
<bodytext>this is the content of textmedia content element that should get indexed</bodytext>
<bodytext>this is the content of header content element that should get indexed</bodytext>
<media>0</media>
<layout>0</layout>
<deleted>0</deleted>
@ -45,9 +45,9 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>indexed content element</header>
<bodytext>this is the content of textmedia content element that should get indexed</bodytext>
<bodytext>this is the content of header content element that should get indexed</bodytext>
<media>0</media>
<layout>0</layout>
<deleted>0</deleted>
@ -65,7 +65,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>This content should not be indexed due to root line contraints</header>
<bodytext></bodytext>
<media>0</media>
@ -85,7 +85,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>This content should not be indexed due to root line contraints</header>
<bodytext></bodytext>
<media>0</media>

View file

@ -7,7 +7,7 @@
<crdate>1480686370</crdate>
<hidden>0</hidden>
<sorting>72</sorting>
<CType>textmedia</CType>
<CType>header</CType>
<header>Also indexable record</header>
<bodytext></bodytext>
<media>0</media>

View file

@ -0,0 +1,119 @@
<?php
namespace Leonmrni\SearchCore\Tests\Indexing\TcaIndexer;
/*
* Copyright (C) 2016 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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.'
);
}
}