From aebe58721d6cc31fc8f71a062fd7bec00eae8edc Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 10 Oct 2017 11:15:06 +0200 Subject: [PATCH] TASK: Add changes from customer project --- .../Connection/Elasticsearch/IndexFactory.php | 40 +++++-- .../Connection/Elasticsearch/SearchResult.php | 2 +- Classes/Connection/SearchResultInterface.php | 2 +- Classes/DataProcessing/CopyToProcessor.php | 54 ++++++++++ Classes/DataProcessing/ProcessorInterface.php | 39 +++++++ .../Domain/Index/TcaIndexer/PagesIndexer.php | 46 +++++++- .../Index/TcaIndexer/RelationResolver.php | 14 +++ .../Index/TcaIndexer/TcaTableService.php | 13 +++ Classes/Domain/Search/QueryFactory.php | 33 +++--- .../Connection/Elasticsearch/FilterTest.php | 4 +- Tests/Functional/Fixtures/BasicSetup.ts | 1 + .../Elasticsearch/IndexFactoryTest.php | 1 + .../Index/TcaIndexer/TcaTableServiceTest.php | 20 +--- Tests/Unit/Domain/Search/QueryFactoryTest.php | 102 +++++++++--------- ext_localconf.php | 2 +- 15 files changed, 270 insertions(+), 103 deletions(-) create mode 100644 Classes/DataProcessing/CopyToProcessor.php create mode 100644 Classes/DataProcessing/ProcessorInterface.php diff --git a/Classes/Connection/Elasticsearch/IndexFactory.php b/Classes/Connection/Elasticsearch/IndexFactory.php index 66b448b..49956f0 100644 --- a/Classes/Connection/Elasticsearch/IndexFactory.php +++ b/Classes/Connection/Elasticsearch/IndexFactory.php @@ -39,6 +39,21 @@ class IndexFactory implements Singleton */ protected $configuration; + /** + * @var \TYPO3\CMS\Core\Log\Logger + */ + protected $logger; + + /** + * Inject log manager to get concrete logger from it. + * + * @param \TYPO3\CMS\Core\Log\LogManager $logManager + */ + public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager) + { + $this->logger = $logManager->getLogger(__CLASS__); + } + /** * @param ConfigurationContainerInterface $configuration */ @@ -60,7 +75,10 @@ class IndexFactory implements Singleton $index = $connection->getClient()->getIndex('typo3content'); if ($index->exists() === false) { - $index->create($this->getConfigurationFor($documentType)); + $config = $this->getConfigurationFor($documentType); + $this->logger->debug(sprintf('Create index %s.', $documentType), [$documentType, $config]); + $index->create($config); + $this->logger->debug(sprintf('Created index %s.', $documentType), [$documentType]); } return $index; @@ -76,9 +94,11 @@ class IndexFactory implements Singleton try { $configuration = $this->configuration->get('indexing.' . $documentType . '.index'); - if (isset($configuration['analysis']['analyzer'])) { - foreach ($configuration['analysis']['analyzer'] as $key => $analyzer) { - $configuration['analysis']['analyzer'][$key] = $this->prepareAnalyzerConfiguration($analyzer); + foreach (['analyzer', 'filter'] as $optionsToExpand) { + if (isset($configuration['analysis'][$optionsToExpand])) { + foreach ($configuration['analysis'][$optionsToExpand] as $key => $options) { + $configuration['analysis'][$optionsToExpand][$key] = $this->prepareOptions($options); + } } } @@ -89,20 +109,20 @@ class IndexFactory implements Singleton } /** - * @param array $analyzer + * @param array $options * * @return array */ - protected function prepareAnalyzerConfiguration(array $analyzer) + protected function prepareOptions(array $options) { - $fieldsToExplode = ['char_filter', 'filter']; + $fieldsToExplode = ['char_filter', 'filter', 'word_list']; foreach ($fieldsToExplode as $fieldToExplode) { - if (isset($analyzer[$fieldToExplode])) { - $analyzer[$fieldToExplode] = GeneralUtility::trimExplode(',', $analyzer[$fieldToExplode], true); + if (isset($options[$fieldToExplode])) { + $options[$fieldToExplode] = GeneralUtility::trimExplode(',', $options[$fieldToExplode], true); } } - return $analyzer; + return $options; } } diff --git a/Classes/Connection/Elasticsearch/SearchResult.php b/Classes/Connection/Elasticsearch/SearchResult.php index f45f02f..3919722 100644 --- a/Classes/Connection/Elasticsearch/SearchResult.php +++ b/Classes/Connection/Elasticsearch/SearchResult.php @@ -83,7 +83,7 @@ class SearchResult implements SearchResultInterface /** * Return all facets, if any. * - * @return array + * @return array */ public function getFacets() { diff --git a/Classes/Connection/SearchResultInterface.php b/Classes/Connection/SearchResultInterface.php index d52a8ba..60718cd 100644 --- a/Classes/Connection/SearchResultInterface.php +++ b/Classes/Connection/SearchResultInterface.php @@ -35,7 +35,7 @@ interface SearchResultInterface extends \Iterator, \Countable, QueryResultInterf /** * Return all facets, if any. * - * @return array + * @return array */ public function getFacets(); diff --git a/Classes/DataProcessing/CopyToProcessor.php b/Classes/DataProcessing/CopyToProcessor.php new file mode 100644 index 0000000..bac60d0 --- /dev/null +++ b/Classes/DataProcessing/CopyToProcessor.php @@ -0,0 +1,54 @@ + + * + * 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. + */ + +/** + * Copies values from one field to another one. + */ +class CopyToProcessor implements ProcessorInterface +{ + public function processRecord(array $record, array $configuration) + { + $all = []; + + $this->addArray($all, $record); + $all = array_filter($all); + $record[$configuration['to']] = implode(PHP_EOL, $all); + + return $record; + } + + /** + * @param array &$to + * @param array $from + */ + protected function addArray(array &$to, array $from) + { + foreach ($from as $property => $value) { + if (is_array($value)) { + $this->addArray($to, $value, $exclude); + continue; + } + + $to[] = (string) $value; + } + } +} diff --git a/Classes/DataProcessing/ProcessorInterface.php b/Classes/DataProcessing/ProcessorInterface.php new file mode 100644 index 0000000..841edb9 --- /dev/null +++ b/Classes/DataProcessing/ProcessorInterface.php @@ -0,0 +1,39 @@ + + * + * 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. + */ + +/** + * All DataProcessing Processor should implement this interface, otherwise they + * will not be executed. + */ +interface ProcessorInterface +{ + /** + * Processes the given record. + * Also retrieves the configuration for this processor instance. + * + * @param array $record + * @param array $configuration + * + * @return array + */ + public function processRecord(array $record, array $configuration); +} diff --git a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php index b6b71be..ebf2b49 100644 --- a/Classes/Domain/Index/TcaIndexer/PagesIndexer.php +++ b/Classes/Domain/Index/TcaIndexer/PagesIndexer.php @@ -34,6 +34,12 @@ class PagesIndexer extends TcaIndexer */ protected $contentTableService; + /** + * @var \TYPO3\CMS\Core\Resource\FileRepository + * @inject + */ + protected $fileRepository; + /** * @param TcaTableService $tcaTableService * @param TcaTableService $tcaTableService @@ -65,7 +71,9 @@ class PagesIndexer extends TcaIndexer } } - $record['content'] = $this->fetchContentForPage($record['uid']); + $content = $this->fetchContentForPage($record['uid']); + $record['content'] = $content['content']; + $record['media'] = array_unique(array_merge($record['media'], $content['images'])); parent::prepareRecord($record); } @@ -88,14 +96,42 @@ class PagesIndexer extends TcaIndexer } $this->logger->debug('Fetched content for page ' . $uid); + $images = []; $content = []; foreach ($contentElements as $contentElement) { + $images = array_merge( + $images, + $this->getContentElementImages($contentElement['uid']) + ); $content[] = $contentElement['bodytext']; } - // Remove Tags. - // Interpret escaped new lines and special chars. - // Trim, e.g. trailing or leading new lines. - return trim(stripcslashes(strip_tags(implode(' ', $content)))); + return [ + // Remove Tags. + // Interpret escaped new lines and special chars. + // Trim, e.g. trailing or leading new lines. + 'content' => trim(stripcslashes(strip_tags(implode(' ', $content)))), + 'images' => $images, + ]; + } + + /** + * @param int $uidOfContentElement + * @return array + */ + protected function getContentElementImages($uidOfContentElement) + { + $imageRelationUids = []; + $imageRelations = $this->fileRepository->findByRelation( + 'tt_content', + 'image', + $uidOfContentElement + ); + + foreach ($imageRelations as $relation) { + $imageRelationUids[] = $relation->getUid(); + } + + return $imageRelationUids; } } diff --git a/Classes/Domain/Index/TcaIndexer/RelationResolver.php b/Classes/Domain/Index/TcaIndexer/RelationResolver.php index ceb5506..42dafc1 100644 --- a/Classes/Domain/Index/TcaIndexer/RelationResolver.php +++ b/Classes/Domain/Index/TcaIndexer/RelationResolver.php @@ -86,6 +86,9 @@ class RelationResolver implements Singleton */ protected function resolveValue($value, array $config) { + if (isset($config['foreign_table']) && $config['foreign_table'] === 'sys_file_reference') { + return $this->resolveFalRelations($value); + } if ($value === '' || $value === '0') { return ''; } @@ -102,6 +105,17 @@ class RelationResolver implements Singleton return ''; } + /** + * @param string $value + * @return array + */ + protected function resolveFalRelations($value) + { + $files = GeneralUtility::trimExplode(',', $value); + $files = array_filter($files); + return array_map('intval', $files); + } + /** * @param array Column config. * @return bool diff --git a/Classes/Domain/Index/TcaIndexer/TcaTableService.php b/Classes/Domain/Index/TcaIndexer/TcaTableService.php index 70bb52d..e68a94f 100644 --- a/Classes/Domain/Index/TcaIndexer/TcaTableService.php +++ b/Classes/Domain/Index/TcaIndexer/TcaTableService.php @@ -21,6 +21,8 @@ namespace Codappix\SearchCore\Domain\Index\TcaIndexer; */ use Codappix\SearchCore\Configuration\ConfigurationContainerInterface; +use Codappix\SearchCore\Configuration\InvalidArgumentException as InvalidConfigurationArgumentException; +use Codappix\SearchCore\DataProcessing\ProcessorInterface; use Codappix\SearchCore\Domain\Index\IndexingException; use TYPO3\CMS\Backend\Utility\BackendUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -148,6 +150,17 @@ class TcaTableService { $this->relationResolver->resolveRelationsForRecord($this, $record); + try { + foreach ($this->configuration->get('indexing.' . $this->tableName . '.dataProcessing') as $configuration) { + $dataProcessor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($configuration['_typoScriptNodeValue']); + if ($dataProcessor instanceof ProcessorInterface) { + $record = $dataProcessor->processRecord($record, $configuration); + } + } + } catch (InvalidConfigurationArgumentException $e) { + // Nothing to do. + } + if (isset($record['uid']) && !isset($record['search_identifier'])) { $record['search_identifier'] = $record['uid']; } diff --git a/Classes/Domain/Search/QueryFactory.php b/Classes/Domain/Search/QueryFactory.php index 9455f80..b30fcbf 100644 --- a/Classes/Domain/Search/QueryFactory.php +++ b/Classes/Domain/Search/QueryFactory.php @@ -25,6 +25,7 @@ use Codappix\SearchCore\Configuration\InvalidArgumentException; use Codappix\SearchCore\Connection\ConnectionInterface; use Codappix\SearchCore\Connection\Elasticsearch\Query; use Codappix\SearchCore\Connection\SearchRequestInterface; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Utility\ArrayUtility; class QueryFactory @@ -109,20 +110,18 @@ class QueryFactory return; } - $query = ArrayUtility::setValueByPath( - $query, - 'query.bool.must.0.match._all.query', - $searchRequest->getSearchTerm() - ); + $matchExpression = [ + 'type' => 'most_fields', + 'query' => $searchRequest->getSearchTerm(), + 'fields' => GeneralUtility::trimExplode(',', $this->configuration->get('searching.fields')), + ]; $minimumShouldMatch = $this->configuration->getIfExists('searching.minimumShouldMatch'); if ($minimumShouldMatch) { - $query = ArrayUtility::setValueByPath( - $query, - 'query.bool.must.0.match._all.minimum_should_match', - $minimumShouldMatch - ); + $matchExpression['minimum_should_match'] = $minimumShouldMatch; } + + $query = ArrayUtility::setValueByPath($query, 'query.bool.must.0.multi_match', $matchExpression); } /** @@ -150,13 +149,15 @@ class QueryFactory ]; } - $query = ArrayUtility::arrayMergeRecursiveOverrule($query, [ - 'query' => [ - 'bool' => [ - 'should' => $boostQueryParts, + if (!empty($boostQueryParts)) { + $query = ArrayUtility::arrayMergeRecursiveOverrule($query, [ + 'query' => [ + 'bool' => [ + 'should' => $boostQueryParts, + ], ], - ], - ]); + ]); + } } /** diff --git a/Tests/Functional/Connection/Elasticsearch/FilterTest.php b/Tests/Functional/Connection/Elasticsearch/FilterTest.php index 9c5aada..2430833 100644 --- a/Tests/Functional/Connection/Elasticsearch/FilterTest.php +++ b/Tests/Functional/Connection/Elasticsearch/FilterTest.php @@ -48,7 +48,7 @@ class FilterTest extends AbstractFunctionalTestCase $searchService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class) ->get(SearchService::class); - $searchRequest = new SearchRequest('Search Word'); + $searchRequest = new SearchRequest(); $result = $searchService->search($searchRequest); $this->assertSame(2, count($result), 'Did not receive both indexed elements without filter.'); @@ -73,7 +73,7 @@ class FilterTest extends AbstractFunctionalTestCase $searchService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class) ->get(SearchService::class); - $searchRequest = new SearchRequest('Search Word'); + $searchRequest = new SearchRequest(); $result = $searchService->search($searchRequest); $this->assertSame(1, count($result->getFacets()), 'Did not receive the single defined facet.'); diff --git a/Tests/Functional/Fixtures/BasicSetup.ts b/Tests/Functional/Fixtures/BasicSetup.ts index 1e2b3a9..9795b0f 100644 --- a/Tests/Functional/Fixtures/BasicSetup.ts +++ b/Tests/Functional/Fixtures/BasicSetup.ts @@ -37,6 +37,7 @@ plugin { } searching { + fields = search_title facets { contentTypes { field = CType diff --git a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php index c73fe36..4ede3cb 100644 --- a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php +++ b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php @@ -38,6 +38,7 @@ class IndexFactoryTest extends AbstractUnitTestCase $this->configuration = $this->getMockBuilder(ConfigurationContainerInterface::class)->getMock(); $this->subject = new IndexFactory($this->configuration); + $this->subject->injectLogger($this->getMockedLogger()); } /** diff --git a/Tests/Unit/Domain/Index/TcaIndexer/TcaTableServiceTest.php b/Tests/Unit/Domain/Index/TcaIndexer/TcaTableServiceTest.php index 7fb0280..9e88d4b 100644 --- a/Tests/Unit/Domain/Index/TcaIndexer/TcaTableServiceTest.php +++ b/Tests/Unit/Domain/Index/TcaIndexer/TcaTableServiceTest.php @@ -60,18 +60,10 @@ class TcaTableServiceTest extends AbstractUnitTestCase ->method('getIfExists') ->withConsecutive(['indexing.table.additionalWhereClause'], ['indexing.table.rootLineBlacklist']) ->will($this->onConsecutiveCalls(null, false)); - $this->subject->expects($this->once()) - ->method('getSystemWhereClause') - ->will($this->returnValue('1=1 AND pages.no_search = 0')); - $whereClause = $this->subject->getWhereClause(); $this->assertSame( '1=1 AND pages.no_search = 0', - $whereClause->getStatement() - ); - $this->assertSame( - [], - $whereClause->getParameters() + $this->subject->getWhereClause() ); } @@ -84,18 +76,10 @@ class TcaTableServiceTest extends AbstractUnitTestCase ->method('getIfExists') ->withConsecutive(['indexing.table.additionalWhereClause'], ['indexing.table.rootLineBlacklist']) ->will($this->onConsecutiveCalls('table.field = "someValue"', false)); - $this->subject->expects($this->once()) - ->method('getSystemWhereClause') - ->will($this->returnValue('1=1 AND pages.no_search = 0')); - $whereClause = $this->subject->getWhereClause(); $this->assertSame( '1=1 AND pages.no_search = 0 AND table.field = "someValue"', - $whereClause->getStatement() - ); - $this->assertSame( - [], - $whereClause->getParameters() + $this->subject->getWhereClause() ); } } diff --git a/Tests/Unit/Domain/Search/QueryFactoryTest.php b/Tests/Unit/Domain/Search/QueryFactoryTest.php index 54fdc2a..f719fdf 100644 --- a/Tests/Unit/Domain/Search/QueryFactoryTest.php +++ b/Tests/Unit/Domain/Search/QueryFactoryTest.php @@ -50,13 +50,11 @@ class QueryFactoryTest extends AbstractUnitTestCase /** * @test */ - public function creatonOfQueryWorksInGeneral() + public function creationOfQueryWorksInGeneral() { - $searchRequest = new SearchRequest('SearchWord'); + $this->mockConfiguration(); - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $searchRequest = new SearchRequest('SearchWord'); $query = $this->subject->create($searchRequest); $this->assertInstanceOf( @@ -71,9 +69,7 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function filterIsAddedToQuery() { - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $this->mockConfiguration(); $searchRequest = new SearchRequest('SearchWord'); $searchRequest->setFilter(['field' => 'content']); @@ -83,7 +79,7 @@ class QueryFactoryTest extends AbstractUnitTestCase [ ['term' => ['field' => 'content']] ], - $query->toArray()['query']['bool']['filter'], + $query->toArray()['query']['function_score']['query']['bool']['filter'], 'Filter was not added to query.' ); } @@ -93,9 +89,7 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function emptyFilterIsNotAddedToQuery() { - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $this->mockConfiguration(); $searchRequest = new SearchRequest('SearchWord'); $searchRequest->setFilter([ @@ -112,7 +106,7 @@ class QueryFactoryTest extends AbstractUnitTestCase $query = $this->subject->create($searchRequest); $this->assertSame( null, - $query->toArray()['query']['bool']['filter'], + $query->toArray()['query']['function_score']['query']['bool']['filter'], 'Filter was added to query, even if no filter exists.' ); } @@ -122,9 +116,8 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function facetsAreAddedToQuery() { - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $this->mockConfiguration(); + $searchRequest = new SearchRequest('SearchWord'); $searchRequest->addFacet(new FacetRequest('Identifier', 'FieldName')); $searchRequest->addFacet(new FacetRequest('Identifier 2', 'FieldName 2')); @@ -153,9 +146,8 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function sizeIsAddedToQuery() { - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $this->mockConfiguration(); + $searchRequest = new SearchRequest('SearchWord'); $searchRequest->setLimit(45); $searchRequest->setOffset(35); @@ -178,10 +170,9 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function searchTermIsAddedToQuery() { + $this->mockConfiguration(); + $searchRequest = new SearchRequest('SearchWord'); - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); $query = $this->subject->create($searchRequest); $this->assertSame( @@ -189,16 +180,18 @@ class QueryFactoryTest extends AbstractUnitTestCase 'bool' => [ 'must' => [ [ - 'match' => [ - '_all' => [ - 'query' => 'SearchWord', + 'multi_match' => [ + 'type' => 'most_fields', + 'query' => 'SearchWord', + 'fields' => [ + 0 => 'test_field', ], ], ], ], ], ], - $query->toArray()['query'], + $query->toArray()['query']['function_score']['query'], 'Search term was not added to query as expected.' ); } @@ -208,14 +201,13 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function minimumShouldMatchIsAddedToQuery() { - $searchRequest = new SearchRequest('SearchWord'); $this->configuration->expects($this->once()) ->method('getIfExists') ->with('searching.minimumShouldMatch') ->willReturn('50%'); - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); + $this->mockConfiguration(); + + $searchRequest = new SearchRequest('SearchWord'); $query = $this->subject->create($searchRequest); $this->assertArraySubset( @@ -223,16 +215,14 @@ class QueryFactoryTest extends AbstractUnitTestCase 'bool' => [ 'must' => [ [ - 'match' => [ - '_all' => [ - 'minimum_should_match' => '50%', - ], + 'multi_match' => [ + 'minimum_should_match' => '50%', ], ], ], ], ], - $query->toArray()['query'], + $query->toArray()['query']['function_score']['query'], 'minimum_should_match was not added to query as configured.' ); } @@ -244,10 +234,11 @@ class QueryFactoryTest extends AbstractUnitTestCase { $searchRequest = new SearchRequest('SearchWord'); - $this->configuration->expects($this->exactly(2)) + $this->configuration->expects($this->exactly(3)) ->method('get') - ->withConsecutive(['searching.boost'], ['searching.fieldValueFactor']) + ->withConsecutive(['searching.fields'], ['searching.boost'], ['searching.fieldValueFactor']) ->will($this->onConsecutiveCalls( + 'test_field', [ 'search_title' => 3, 'search_abstract' => 1.5, @@ -292,10 +283,11 @@ class QueryFactoryTest extends AbstractUnitTestCase 'factor' => '2', 'missing' => '1', ]; - $this->configuration->expects($this->exactly(2)) + $this->configuration->expects($this->exactly(3)) ->method('get') - ->withConsecutive(['searching.boost'], ['searching.fieldValueFactor']) + ->withConsecutive(['searching.fields'], ['searching.boost'], ['searching.fieldValueFactor']) ->will($this->onConsecutiveCalls( + 'test_field', $this->throwException(new InvalidArgumentException), $fieldConfig )); @@ -308,9 +300,11 @@ class QueryFactoryTest extends AbstractUnitTestCase 'bool' => [ 'must' => [ [ - 'match' => [ - '_all' => [ - 'query' => 'SearchWord', + 'multi_match' => [ + 'type' => 'most_fields', + 'query' => 'SearchWord', + 'fields' => [ + 0 => 'test_field', ], ], ], @@ -330,17 +324,27 @@ class QueryFactoryTest extends AbstractUnitTestCase */ public function emptySearchStringWillNotAddSearchToQuery() { + $this->mockConfiguration(); + $searchRequest = new SearchRequest(); - $this->configuration->expects($this->any()) - ->method('get') - ->will($this->throwException(new InvalidArgumentException)); - $query = $this->subject->create($searchRequest); - $this->assertInstanceOf( - stdClass, - $query->toArray()['query']['match_all'], + $this->assertNull( + $query->toArray()['query']['function_score']['query'], 'Empty search request does not create expected query.' ); } + + protected function mockConfiguration() + { + $this->configuration->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function ($option) { + if ($option === 'searching.fields') { + return 'test_field'; + } + + return $this->throwException(new InvalidArgumentException); + })); + } } diff --git a/ext_localconf.php b/ext_localconf.php index 54bf43b..882f6ad 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -11,7 +11,7 @@ call_user_func( 'SC_OPTIONS' => [ 'extbase' => [ 'commandControllers' => [ - Codappix\SearchCore\Command\IndexCommandController::class, + $extensionKey . '::index' => Codappix\SearchCore\Command\IndexCommandController::class, ], ], 't3lib/class.t3lib_tcemain.php' => [