mirror of
https://github.com/Codappix/search_core.git
synced 2024-12-22 23:36:10 +01:00
Merge pull request #38 from DanielSiepmann/feature/17-resolve-inline-relations
FEATURE: Resolve further relations
This commit is contained in:
commit
257b23d8e6
12 changed files with 340 additions and 33 deletions
|
@ -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>|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 '';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
119
Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php
Normal file
119
Tests/Functional/Indexing/TcaIndexer/RelationResolverTest.php
Normal 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.'
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue