From 541c6db53ada3bc378344769b0295d199ee8fbae Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 15 Dec 2016 14:02:40 +0100 Subject: [PATCH] FEATURE: Allow to configure tables to be processed * Add typoscript option which TYPO3 tables should be processed and which should not be processed. * Respect new optiion in hook. * Also add necessary tests. * Remove old no longer needed tests for hook. --- .travis.yml | 2 +- .../Configuration/ConfigurationContainer.php | 2 +- Classes/Domain/Service/DataHandler.php | 30 ++++ Classes/Hook/DataHandler.php | 25 +-- Configuration/TypoScript/setup.txt | 6 + Makefile | 8 - .../Functional/AbstractFunctionalTestCase.php | 25 ++- Tests/Functional/Fixtures/BasicSetup.ts | 4 + .../AllowedTables.xml} | 4 +- .../DataHandler/MultipleAllowedTables.ts | 11 ++ .../Hooks/DataHandler/NonAllowedTables.xml | 18 +++ .../IgnoresUnkownOperationTest.php | 78 ++++++++++ .../DataHandler/NonAllowedTablesTest.php | 117 +++++++++++++++ ...TablesWithMultipleTablesConfiguredTest.php | 32 ++++ .../ProcessesAllowedTablesTest.php | 136 +++++++++++++++++ ...TablesWithMultipleTablesConfiguredTest.php | 37 +++++ Tests/Functional/Hooks/DataHandlerTest.php | 115 -------------- Tests/Functional/Indexing/TcaIndexerTest.php | 18 +-- Tests/Unit/Hook/DataHandlerTest.php | 142 ------------------ Tests/Unit/UnitTests.xml | 33 ---- ext_localconf.php | 4 +- 21 files changed, 496 insertions(+), 351 deletions(-) rename Tests/Functional/Fixtures/Hooks/{DataHandler.xml => DataHandler/AllowedTables.xml} (81%) create mode 100644 Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts create mode 100644 Tests/Functional/Fixtures/Hooks/DataHandler/NonAllowedTables.xml create mode 100644 Tests/Functional/Hooks/DataHandler/IgnoresUnkownOperationTest.php create mode 100644 Tests/Functional/Hooks/DataHandler/NonAllowedTablesTest.php create mode 100644 Tests/Functional/Hooks/DataHandler/NonAllowedTablesWithMultipleTablesConfiguredTest.php create mode 100644 Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesTest.php create mode 100644 Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesWithMultipleTablesConfiguredTest.php delete mode 100644 Tests/Functional/Hooks/DataHandlerTest.php delete mode 100644 Tests/Unit/Hook/DataHandlerTest.php delete mode 100644 Tests/Unit/UnitTests.xml diff --git a/.travis.yml b/.travis.yml index 1b76868..44af4d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,7 +41,7 @@ services: install: make install -script: make Tests +script: make functionalTests after_script: - make uploadCodeCoverage diff --git a/Classes/Configuration/ConfigurationContainer.php b/Classes/Configuration/ConfigurationContainer.php index f5a50b8..79496b9 100644 --- a/Classes/Configuration/ConfigurationContainer.php +++ b/Classes/Configuration/ConfigurationContainer.php @@ -59,7 +59,7 @@ class ConfigurationContainer implements ConfigurationContainerInterface { if (!isset($this->settings[$section]) || !isset($this->settings[$section][$key])) { throw new InvalidArgumentException( - 'The given configuration option does not exit.', + 'The given configuration option does not exist.', InvalidArgumentException::OPTION_DOES_NOT_EXIST ); } diff --git a/Classes/Domain/Service/DataHandler.php b/Classes/Domain/Service/DataHandler.php index cf24616..eddb3f9 100644 --- a/Classes/Domain/Service/DataHandler.php +++ b/Classes/Domain/Service/DataHandler.php @@ -20,7 +20,9 @@ namespace Leonmrni\SearchCore\Domain\Service; * 02110-1301, USA. */ +use Leonmrni\SearchCore\Configuration\ConfigurationContainerInterface; use TYPO3\CMS\Core\SingletonInterface as Singleton; +use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Handles all data related things like updates, deletes and inserts. @@ -30,10 +32,14 @@ use TYPO3\CMS\Core\SingletonInterface as Singleton; * * TODO: Probably a candidate for deletion. Currently this class makes use of * extbase DI. We have to resolve this somehow. + * + * I think we keep it for easier testing and DI. */ class DataHandler implements Singleton { /** + * TODO: Only inject on first use?! + * * @var \Leonmrni\SearchCore\Connection\ConnectionInterface * @inject */ @@ -45,6 +51,12 @@ class DataHandler implements Singleton */ protected $indexerFactory; + /** + * @var ConfigurationContainerInterface + * @inject + */ + protected $configuration; + /** * @var \TYPO3\CMS\Core\Log\Logger */ @@ -60,6 +72,24 @@ class DataHandler implements Singleton $this->logger = $logManager->getLogger(__CLASS__); } + /** + * @param ConfigurationContainerInterface $configuration + */ + public function __construct(ConfigurationContainerInterface $configuration) + { + $this->configuration = $configuration; + } + + /** + * Get all tables that are allowed for indexing. + * + * @return array + */ + public function getAllowedTablesForIndexing() + { + return GeneralUtility::trimExplode(',', $this->configuration->get('index', 'allowedTables')); + } + /** * @param string $table * @param array $record diff --git a/Classes/Hook/DataHandler.php b/Classes/Hook/DataHandler.php index a49b454..0c102ab 100644 --- a/Classes/Hook/DataHandler.php +++ b/Classes/Hook/DataHandler.php @@ -48,9 +48,6 @@ class DataHandler implements Singleton * Dependency injection as TYPO3 doesn't provide it on it's own. * Still you can submit your own dataHandler. * - * TODO: Inject datahandler only on use?! Using getter / setter or something else? - * Otherwise a connection to elastic and whole bootstrapping will be triggered. - * * @param OwnDataHandler $dataHandler * @param Logger $logger */ @@ -109,7 +106,7 @@ class DataHandler implements Singleton if ($status === 'new') { $fieldArray['uid'] = $dataHandler->substNEWwithIDs[$uid]; $this->dataHandler->add($table, $fieldArray); - return false; + return true; } if ($status === 'update') { @@ -117,28 +114,14 @@ class DataHandler implements Singleton if ($record !== null) { $this->dataHandler->update($table, $record); } - return false; + return true; } $this->logger->debug( 'Database update not processed, cause status is unhandled.', [$status, $table, $uid, $fieldArray] ); - return true; - } - - /** - * Returns array containing tables that should be processed by this hook. - * - * TODO: Fetch from config - * - * @return string[] - */ - protected function getTablesToProcess() - { - return [ - 'tt_content', - ]; + return false; } /** @@ -147,7 +130,7 @@ class DataHandler implements Singleton */ protected function shouldProcessTable($table) { - return in_array($table, $this->getTablesToProcess()); + return in_array($table, $this->dataHandler->getAllowedTablesForIndexing()); } /** diff --git a/Configuration/TypoScript/setup.txt b/Configuration/TypoScript/setup.txt index 7eeead8..5e22e71 100644 --- a/Configuration/TypoScript/setup.txt +++ b/Configuration/TypoScript/setup.txt @@ -7,6 +7,12 @@ plugin { } index { + # Pages are not supported yet, see + # https://github.com/DanielSiepmann/search_core/issues/24 but + # should also be added, together with additionalWhereClause + # based on doktypes + allowedTables = tt_content + tt_content { additionalWhereClause = tt_content.CType NOT IN ('gridelements_pi1', 'list', 'div', 'menu') } diff --git a/Makefile b/Makefile index be9f02c..969ced8 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,6 @@ install: clean COMPOSER_PROCESS_TIMEOUT=1000 composer require -vv --dev --prefer-source typo3/cms="$(TYPO3_VERSION)" git checkout composer.json -unitTests: - TYPO3_PATH_WEB=$(TYPO3_WEB_DIR) \ - .Build/bin/phpunit --colors --debug -v \ - -c Tests/Unit/UnitTests.xml - functionalTests: typo3DatabaseName=$(typo3DatabaseName) \ typo3DatabaseUsername=$(typo3DatabaseUsername) \ @@ -28,9 +23,6 @@ functionalTests: .Build/bin/phpunit --colors --debug -v \ -c Tests/Functional/FunctionalTests.xml -.PHONY: Tests -Tests: unitTests functionalTests - uploadCodeCoverage: uploadCodeCoverageToScrutinizer uploadCodeCoverageToCodacy uploadCodeCoverageToScrutinizer: diff --git a/Tests/Functional/AbstractFunctionalTestCase.php b/Tests/Functional/AbstractFunctionalTestCase.php index 5c253be..eec956d 100644 --- a/Tests/Functional/AbstractFunctionalTestCase.php +++ b/Tests/Functional/AbstractFunctionalTestCase.php @@ -29,18 +29,6 @@ abstract class AbstractFunctionalTestCase extends CoreTestCase { protected $testExtensionsToLoad = ['typo3conf/ext/search_core']; - /** - * Define whether to setup default typoscript on page 1. - * - * Set to false if you need to add further ts and use getDefaultPageTs to get the default one. - * - * This is necessary as setUpFrontendRootPage will allways add a new record - * and only the first one is used. - * - * @var bool - */ - protected $loadDefaultTs = true; - public function setUp() { parent::setUp(); @@ -51,12 +39,17 @@ abstract class AbstractFunctionalTestCase extends CoreTestCase // Provide necessary configuration for extension $this->importDataSet('Tests/Functional/Fixtures/BasicSetup.xml'); - if ($this->loadDefaultTs) { - $this->setUpFrontendRootPage(1, $this->getDefaultPageTs()); - } + $this->setUpFrontendRootPage(1, $this->getTypoScriptFilesForFrontendRootPage()); } - protected function getDefaultPageTs() + /** + * Overwrite to import different files. + * + * Defines which TypoScript Files should be imported. + * + * @return array + */ + protected function getTypoScriptFilesForFrontendRootPage() { return ['EXT:search_core/Tests/Functional/Fixtures/BasicSetup.ts']; } diff --git a/Tests/Functional/Fixtures/BasicSetup.ts b/Tests/Functional/Fixtures/BasicSetup.ts index 22c4ed8..a1b143c 100644 --- a/Tests/Functional/Fixtures/BasicSetup.ts +++ b/Tests/Functional/Fixtures/BasicSetup.ts @@ -5,6 +5,10 @@ plugin { host = localhost port = 9200 } + + index { + allowedTables = tt_content + } } } } diff --git a/Tests/Functional/Fixtures/Hooks/DataHandler.xml b/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml similarity index 81% rename from Tests/Functional/Fixtures/Hooks/DataHandler.xml rename to Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml index db3f5ba..70e924b 100644 --- a/Tests/Functional/Fixtures/Hooks/DataHandler.xml +++ b/Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml @@ -1,7 +1,7 @@ - 6 + 1 1 1480686370 1480686370 @@ -9,7 +9,7 @@ 72 textmedia
test
- this is the content of textmedia content element that should get indexed + Record that can be processed by hook 0 0 0 diff --git a/Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts b/Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts new file mode 100644 index 0000000..7f91f88 --- /dev/null +++ b/Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts @@ -0,0 +1,11 @@ +plugin { + tx_searchcore { + settings { + index { + allowedTables = tt_content, fe_user + } + } + } +} + +module.tx_searchcore < plugin.tx_searchcore diff --git a/Tests/Functional/Fixtures/Hooks/DataHandler/NonAllowedTables.xml b/Tests/Functional/Fixtures/Hooks/DataHandler/NonAllowedTables.xml new file mode 100644 index 0000000..3d4f624 --- /dev/null +++ b/Tests/Functional/Fixtures/Hooks/DataHandler/NonAllowedTables.xml @@ -0,0 +1,18 @@ + + + + 1 + 1 + 1480686370 + 1480686370 + 0 + 0 + 0 + 1 + Some Category that should not be indexed as sys_category is not allowed + + 0 + 0 + + + diff --git a/Tests/Functional/Hooks/DataHandler/IgnoresUnkownOperationTest.php b/Tests/Functional/Hooks/DataHandler/IgnoresUnkownOperationTest.php new file mode 100644 index 0000000..a66e3d8 --- /dev/null +++ b/Tests/Functional/Hooks/DataHandler/IgnoresUnkownOperationTest.php @@ -0,0 +1,78 @@ + + * + * 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\Configuration\ConfigurationContainerInterface; +use Leonmrni\SearchCore\Domain\Service\DataHandler as DataHandlerService; +use Leonmrni\SearchCore\Hook\DataHandler as DataHandlerHook; +use Leonmrni\SearchCore\Tests\Functional\AbstractFunctionalTestCase; +use TYPO3\CMS\Core\DataHandling\DataHandler as Typo3DataHandler; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +class IgnoresUnkownOperationTest extends AbstractFunctionalTestCase +{ + /** + * @var DataHandlerService|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface + */ + protected $subject; + + public function setUp() + { + parent::setUp(); + + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + + $this->subject = $this->getAccessibleMock( + DataHandlerService::class, + [ + 'add', + 'update', + 'delete', + ], + [$objectManager->get(ConfigurationContainerInterface::class)] + ); + + // This way TYPO3 will use our mock instead of a new instance. + $GLOBALS['T3_VAR']['getUserObj']['&' . DataHandlerHook::class] = new DataHandlerHook($this->subject); + } + + /** + * @test + */ + public function deletionWillBeTriggeredForTtContent() + { + $this->subject->expects($this->exactly(0))->method('add'); + $this->subject->expects($this->exactly(0))->method('update'); + $this->subject->expects($this->exactly(0))->method('delete'); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([], [ + 'tt_content' => [ + '1' => [ + 'something' => 1, + ], + ], + ]); + $tce->process_cmdmap(); + } +} diff --git a/Tests/Functional/Hooks/DataHandler/NonAllowedTablesTest.php b/Tests/Functional/Hooks/DataHandler/NonAllowedTablesTest.php new file mode 100644 index 0000000..1238d18 --- /dev/null +++ b/Tests/Functional/Hooks/DataHandler/NonAllowedTablesTest.php @@ -0,0 +1,117 @@ + + * + * 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\Configuration\ConfigurationContainerInterface; +use Leonmrni\SearchCore\Domain\Service\DataHandler as DataHandlerService; +use Leonmrni\SearchCore\Hook\DataHandler as DataHandlerHook; +use Leonmrni\SearchCore\Tests\Functional\AbstractFunctionalTestCase; +use TYPO3\CMS\Core\DataHandling\DataHandler as Typo3DataHandler; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +class NonAllowedTablesTest extends AbstractFunctionalTestCase +{ + /** + * @var DataHandlerService|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface + */ + protected $subject; + + public function setUp() + { + parent::setUp(); + $this->importDataSet('Tests/Functional/Fixtures/Hooks/DataHandler/NonAllowedTables.xml'); + + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + + $this->subject = $this->getAccessibleMock( + DataHandlerService::class, + [ + 'add', + 'update', + 'delete', + ], + [$objectManager->get(ConfigurationContainerInterface::class)] + ); + + // This way TYPO3 will use our mock instead of a new instance. + $GLOBALS['T3_VAR']['getUserObj']['&' . DataHandlerHook::class] = new DataHandlerHook($this->subject); + } + + /** + * @test + */ + public function deletionWillNotBeTriggeredForSysCategories() + { + $this->subject->expects($this->exactly(0))->method('delete'); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([], [ + 'sys_category' => [ + '1' => [ + 'delete' => 1, + ], + ], + ]); + $tce->process_cmdmap(); + } + + /** + * @test + */ + public function updateWillNotBeTriggeredForSysCategory() + { + $this->subject->expects($this->exactly(0))->method('update'); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([ + 'sys_category' => [ + '1' => [ + 'title' => 'something new', + ], + ], + ], []); + $tce->process_datamap(); + } + + /** + * @test + */ + public function addWillNotBeTriggeredForSysCategoy() + { + $this->subject->expects($this->exactly(0))->method('add'); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([ + 'sys_category' => [ + 'NEW_1' => [ + 'pid' => 1, + 'title' => 'a new record', + ], + ], + ], []); + + $tce->process_datamap(); + } +} diff --git a/Tests/Functional/Hooks/DataHandler/NonAllowedTablesWithMultipleTablesConfiguredTest.php b/Tests/Functional/Hooks/DataHandler/NonAllowedTablesWithMultipleTablesConfiguredTest.php new file mode 100644 index 0000000..0bdf563 --- /dev/null +++ b/Tests/Functional/Hooks/DataHandler/NonAllowedTablesWithMultipleTablesConfiguredTest.php @@ -0,0 +1,32 @@ + + * + * 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. + */ + +class NonAllowedTablesWithMultipleTablesConfiguredTest extends NonAllowedTablesTest +{ + protected function getTypoScriptFilesForFrontendRootPage() + { + return array_merge( + parent::getTypoScriptFilesForFrontendRootPage(), + ['EXT:search_core/ Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts'] + ); + } +} diff --git a/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesTest.php b/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesTest.php new file mode 100644 index 0000000..f07767c --- /dev/null +++ b/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesTest.php @@ -0,0 +1,136 @@ + + * + * 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\Configuration\ConfigurationContainerInterface; +use Leonmrni\SearchCore\Domain\Service\DataHandler as DataHandlerService; +use Leonmrni\SearchCore\Hook\DataHandler as DataHandlerHook; +use Leonmrni\SearchCore\Tests\Functional\AbstractFunctionalTestCase; +use TYPO3\CMS\Core\DataHandling\DataHandler as Typo3DataHandler; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Object\ObjectManager; + +class ProcessesAllowedTablesTest extends AbstractFunctionalTestCase +{ + /** + * @var DataHandlerService|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface + */ + protected $subject; + + public function setUp() + { + parent::setUp(); + $this->importDataSet('Tests/Functional/Fixtures/Hooks/DataHandler/AllowedTables.xml'); + + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + + $this->subject = $this->getAccessibleMock( + DataHandlerService::class, + [ + 'add', + 'update', + 'delete', + ], + [$objectManager->get(ConfigurationContainerInterface::class)] + ); + + // This way TYPO3 will use our mock instead of a new instance. + $GLOBALS['T3_VAR']['getUserObj']['&' . DataHandlerHook::class] = new DataHandlerHook($this->subject); + } + + /** + * @test + */ + public function deletionWillBeTriggeredForTtContent() + { + $this->subject->expects($this->exactly(1))->method('delete') + ->with($this->equalTo('tt_content'), $this->equalTo('1')); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([], [ + 'tt_content' => [ + '1' => [ + 'delete' => 1, + ], + ], + ]); + $tce->process_cmdmap(); + } + + /** + * @test + */ + public function updateWillBeTriggeredForTtContent() + { + $this->subject->expects($this->exactly(1))->method('update') + ->with( + $this->equalTo('tt_content'), + $this->callback(function ($record) { + return isset($record['uid']) && $record['uid'] === '1' + && isset($record['pid']) && $record['pid'] === '1' + && isset($record['colPos']) && $record['colPos'] === '1' + ; + }) + ); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([ + 'tt_content' => [ + '1' => [ + 'colPos' => 1, + ], + ], + ], []); + $tce->process_datamap(); + } + + /** + * @test + */ + public function addWillBeTriggeredForTtContent() + { + $this->subject->expects($this->exactly(1))->method('add') + ->with( + $this->equalTo('tt_content'), + $this->callback(function ($record) { + return isset($record['uid']) && $record['uid'] === 2 + && isset($record['pid']) && $record['pid'] === 1 + && isset($record['header']) && $record['header'] === 'a new record' + ; + }) + ); + + $tce = GeneralUtility::makeInstance(Typo3DataHandler::class); + $tce->stripslashes_values = 0; + $tce->start([ + 'tt_content' => [ + 'NEW_1' => [ + 'pid' => 1, + 'header' => 'a new record', + ], + ], + ], []); + + $tce->process_datamap(); + } +} diff --git a/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesWithMultipleTablesConfiguredTest.php b/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesWithMultipleTablesConfiguredTest.php new file mode 100644 index 0000000..f743bdb --- /dev/null +++ b/Tests/Functional/Hooks/DataHandler/ProcessesAllowedTablesWithMultipleTablesConfiguredTest.php @@ -0,0 +1,37 @@ + + * + * 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. + */ + +class ProcessesAllowedTablesWithMultipleTablesConfiguredTest extends ProcessesAllowedTablesTest +{ + /** + * @var DataHandlerService|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface + */ + protected $subject; + + protected function getTypoScriptFilesForFrontendRootPage() + { + return array_merge( + parent::getTypoScriptFilesForFrontendRootPage(), + ['EXT:search_core/ Tests/Functional/Fixtures/Hooks/DataHandler/MultipleAllowedTables.ts'] + ); + } +} diff --git a/Tests/Functional/Hooks/DataHandlerTest.php b/Tests/Functional/Hooks/DataHandlerTest.php deleted file mode 100644 index 7139934..0000000 --- a/Tests/Functional/Hooks/DataHandlerTest.php +++ /dev/null @@ -1,115 +0,0 @@ - - * - * 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\Hook\DataHandler as Hook; -use Leonmrni\SearchCore\Tests\Functional\Connection\Elasticsearch\AbstractFunctionalTestCase; -use TYPO3\CMS\Core\DataHandling\DataHandler as CoreDataHandler; -use TYPO3\CMS\Extbase\Object\ObjectManager; - -/** - * TODO: Rewrite as this test doesn't test what it should do. - * We have to split it up in two tests: - * 1. Test whether TYPO3 DataHandler will our hook as expected. - * 2. Test whether our hook will send the documents to connection as expected. - */ -class DataHandlerTest extends AbstractFunctionalTestCase -{ - public function setUp() - { - parent::setUp(); - $this->importDataSet('Tests/Functional/Fixtures/Hooks/DataHandler.xml'); - } - - /** - * @test - */ - public function nonAllowedTablesWillNotBeProcessed() - { - $dataHandler = new CoreDataHandler(); - - $hook = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Hook::class); - $this->assertFalse($hook->processDatamap_afterDatabaseOperations('new', 'some_strange_table', 'NEW34', [], $dataHandler)); - $this->assertFalse($hook->processDatamap_afterDatabaseOperations('update', 'some_strange_table', 6, [], $dataHandler)); - $this->assertFalse($hook->processCmdmap_deleteAction('some_strange_table', 6, [], false, $dataHandler)); - } - - /** - * @test - */ - public function addNewElement() - { - $dataHandler = new CoreDataHandler(); - $dataHandler->substNEWwithIDs = ['NEW34' => 6]; - - $hook = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Hook::class); - $hook->processDatamap_afterDatabaseOperations('new', 'tt_content', 'NEW34', [], $dataHandler); - - $response = $this->client->request('typo3content/_search?q=*:*'); - $this->assertTrue($response->isOK()); - $this->assertSame($response->getData()['hits']['total'], 1, 'Not exactly 1 document was indexed.'); - } - - /** - * @test - * TODO: Make sure the indexed document was updated, e.g. by changing some content. - */ - public function updateExistingElement() - { - $dataHandler = new CoreDataHandler(); - $hook = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Hook::class); - $hook->processDatamap_afterDatabaseOperations('update', 'tt_content', 6, [], $dataHandler); - - $response = $this->client->request('typo3content/_search?q=*:*'); - $this->assertTrue($response->isOK(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 1, 'Not exactly 1 document was indexed.'); - } - - /** - * @test - */ - public function deleteExistingElement() - { - $this->addNewElement(); - $dataHandler = new CoreDataHandler(); - $hook = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Hook::class); - $hook->processCmdmap_deleteAction('tt_content', 6, [], false, $dataHandler); - - $response = $this->client->request('typo3content/_search?q=*:*'); - $this->assertTrue($response->isOK(), 'Elastica did not answer with ok code.'); - $this->assertSame($response->getData()['hits']['total'], 0, 'Not exactly 0 document was indexed.'); - } - - /** - * @test - * @expectedException \Elastica\Exception\ResponseException - */ - public function someUnknownOperationDoesNotBreakSomething() - { - $dataHandler = new CoreDataHandler(); - $hook = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Hook::class); - //TODO: this test is senseless, checking an exception not correct, this operation should not do anything! - $hook->processDatamap_afterDatabaseOperations('something', 'tt_content', 6, [], $dataHandler); - - // Should trigger Exception - $this->client->request('typo3content/_search?q=*:*'); - } -} diff --git a/Tests/Functional/Indexing/TcaIndexerTest.php b/Tests/Functional/Indexing/TcaIndexerTest.php index 1610cc0..e3af23b 100644 --- a/Tests/Functional/Indexing/TcaIndexerTest.php +++ b/Tests/Functional/Indexing/TcaIndexerTest.php @@ -30,22 +30,12 @@ use TYPO3\CMS\Extbase\Object\ObjectManager; class TcaIndexerTest extends AbstractFunctionalTestCase { - protected $loadDefaultTs = false; - /** * @test */ public function respectRootLineBlacklist() { $this->importDataSet('Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.xml'); - $this->setUpFrontendRootPage( - 1, - array_merge( - $this->getDefaultPageTs(), - ['EXT:search_core/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.ts'] - ) - ); - $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class); $tableName = 'tt_content'; $tableService = $objectManager->get( @@ -83,4 +73,12 @@ class TcaIndexerTest extends AbstractFunctionalTestCase $objectManager->get(TcaIndexer::class, $tableService, $connection)->indexAllDocuments(); } + + protected function getTypoScriptFilesForFrontendRootPage() + { + return array_merge( + parent::getTypoScriptFilesForFrontendRootPage(), + ['EXT:search_core/Tests/Functional/Fixtures/Indexing/TcaIndexer/RespectRootLineBlacklist.ts'] + ); + } } diff --git a/Tests/Unit/Hook/DataHandlerTest.php b/Tests/Unit/Hook/DataHandlerTest.php deleted file mode 100644 index 50b346d..0000000 --- a/Tests/Unit/Hook/DataHandlerTest.php +++ /dev/null @@ -1,142 +0,0 @@ - - * - * 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 TYPO3\CMS\Core\DataHandling\DataHandler as CoreDataHandler; -use TYPO3\CMS\Core\Tests\UnitTestCase; -use Leonmrni\SearchCore\Domain\Service\DataHandler; -use Leonmrni\SearchCore\Hook\DataHandler as Hook; - -/** - * - */ -class DataHandlerTest extends UnitTestCase -{ - /** - * @var DataHandler|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface - */ - protected $subject; - - /** - * @var Hook|\PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface - */ - protected $hook; - - /** - * Set up the tests - */ - protected function setUp() - { - $this->subject = $this->getAccessibleMock(DataHandler::class); - $this->hook = $this->getAccessibleMock( - Hook::class, - [ - 'getTablesToProcess', - 'getRecord' - ], - [$this->subject] - ); - - $this->hook->method('getTablesToProcess') - ->willReturn(['table']); - $this->hook->method('getRecord') - ->willReturn([ - 'title' => 'some title', - 'bodytext' => 'some text', - ]); - } - - /** - * @test - */ - public function notConfiguredTablesWillNotBeProcessed() - { - $table = 'noneConfiguredTable'; - $recordUid = 1; - $this->subject->expects($this->exactly(0))->method('delete'); - $this->subject->expects($this->exactly(0))->method('add'); - $this->subject->expects($this->exactly(0))->method('update'); - - $dataHandler = new CoreDataHandler(); - $dataHandler->substNEWwithIDs = ['NEW34' => $recordUid]; - - $this->hook->processCmdmap_deleteAction($table, $recordUid, [], false, $dataHandler); - $this->hook->processDatamap_afterDatabaseOperations('new', $table, 'NEW34', [], $dataHandler); - $this->hook->processDatamap_afterDatabaseOperations('update', $table, $recordUid, [], $dataHandler); - } - - /** - * @test - */ - public function configuredTablesWillBeProcessed() - { - $table = 'table'; - $recordUid = 1; - $this->subject->expects($this->once())->method('delete'); - $this->subject->expects($this->once())->method('add'); - $this->subject->expects($this->once())->method('update'); - - $dataHandler = new CoreDataHandler(); - $dataHandler->substNEWwithIDs = ['NEW34' => $recordUid]; - - $this->hook->processCmdmap_deleteAction($table, $recordUid, [], false, $dataHandler); - $this->hook->processDatamap_afterDatabaseOperations('new', $table, 'NEW34', [], $dataHandler); - $this->hook->processDatamap_afterDatabaseOperations('update', $table, $recordUid, [], $dataHandler); - } - - /** - * @test - */ - public function deletionWillBeTriggered() - { - $table = 'table'; - $recordUid = 1; - $this->subject->expects($this->once()) - ->method('delete') - ->with( - $this->equalTo($table), - $this->equalTo($recordUid) - ); - - $this->hook->processCmdmap_deleteAction($table, $recordUid, [], false, new CoreDataHandler()); - } - - /** - * @test - */ - public function updateWillBeTriggered() - { - $table = 'table'; - $recordUid = 1; - $record = [ - 'title' => 'some title', - 'bodytext' => 'some text', - ]; - $this->subject->expects($this->once()) - ->method('update') - ->with( - $this->equalTo($table), - $this->equalTo($record) - ); - - $this->hook->processDatamap_afterDatabaseOperations('update', $table, $recordUid, $record, new CoreDataHandler); - } -} diff --git a/Tests/Unit/UnitTests.xml b/Tests/Unit/UnitTests.xml deleted file mode 100644 index ac98de1..0000000 --- a/Tests/Unit/UnitTests.xml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - . - - - - - - ../../Classes - - - - - - - - diff --git a/ext_localconf.php b/ext_localconf.php index 458d21e..7495d73 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -16,10 +16,10 @@ call_user_func( ], 't3lib/class.t3lib_tcemain.php' => [ 'processCmdmapClass' => [ - $extensionKey => \Leonmrni\SearchCore\Hook\DataHandler::class, + $extensionKey => '&' . \Leonmrni\SearchCore\Hook\DataHandler::class, ], 'processDatamapClass' => [ - $extensionKey => \Leonmrni\SearchCore\Hook\DataHandler::class, + $extensionKey => '&' . \Leonmrni\SearchCore\Hook\DataHandler::class, ], ], ],