diff --git a/typo3/sysext/frontend/Classes/Page/PageGenerator.php b/typo3/sysext/frontend/Classes/Page/PageGenerator.php index e228368928b9f8e0c7485768d03cf3afa174e7fe..fc1c4f33e9eda9d1175ce55af8face458b8dac15 100644 --- a/typo3/sysext/frontend/Classes/Page/PageGenerator.php +++ b/typo3/sysext/frontend/Classes/Page/PageGenerator.php @@ -106,17 +106,7 @@ class PageGenerator { if ($GLOBALS['TSFE']->config['config']['setJS_openPic']) { $GLOBALS['TSFE']->setJS('openPic'); } - $GLOBALS['TSFE']->sWordRegEx = ''; - $GLOBALS['TSFE']->sWordList = GeneralUtility::_GP('sword_list'); - if (is_array($GLOBALS['TSFE']->sWordList)) { - $space = !empty($GLOBALS['TSFE']->config['config']['sword_standAlone']) ? '[[:space:]]' : ''; - foreach ($GLOBALS['TSFE']->sWordList as $val) { - if (trim($val) !== '') { - $GLOBALS['TSFE']->sWordRegEx .= $space . quotemeta($val) . $space . '|'; - } - } - $GLOBALS['TSFE']->sWordRegEx = preg_replace('/\\|$/', '', $GLOBALS['TSFE']->sWordRegEx); - } + static::initializeSearchWordDataInTsfe(); // linkVars $GLOBALS['TSFE']->calculateLinkVars(); // dtdAllowsFrames indicates whether to use the target attribute in links @@ -1200,4 +1190,24 @@ class PageGenerator { return $metaTags; } + /** + * Fills the sWordList property and builds the regular expression in TSFE that can be used to split + * strings by the submitted search words. + * + * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::sWordList + * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::sWordRegEx + */ + static protected function initializeSearchWordDataInTsfe() { + $GLOBALS['TSFE']->sWordRegEx = ''; + $GLOBALS['TSFE']->sWordList = GeneralUtility::_GP('sword_list'); + if (is_array($GLOBALS['TSFE']->sWordList)) { + $space = !empty($GLOBALS['TSFE']->config['config']['sword_standAlone']) ? '[[:space:]]' : ''; + foreach ($GLOBALS['TSFE']->sWordList as $val) { + if (trim($val) !== '') { + $GLOBALS['TSFE']->sWordRegEx .= $space . preg_quote($val, '/') . $space . '|'; + } + } + $GLOBALS['TSFE']->sWordRegEx = rtrim($GLOBALS['TSFE']->sWordRegEx, '|'); + } + } } diff --git a/typo3/sysext/frontend/Tests/Unit/Page/Fixtures/PageGeneratorFixture.php b/typo3/sysext/frontend/Tests/Unit/Page/Fixtures/PageGeneratorFixture.php index 85e837b8b3c28cd24ec69554396cdb93f2b33a22..0d96f4e9ca18788c50577491ecef3a1456e48abb 100644 --- a/typo3/sysext/frontend/Tests/Unit/Page/Fixtures/PageGeneratorFixture.php +++ b/typo3/sysext/frontend/Tests/Unit/Page/Fixtures/PageGeneratorFixture.php @@ -36,4 +36,10 @@ class PageGeneratorFixture extends PageGenerator { return self::generateMetaTagHtml($metaTagTypoScript, $xhtml, $cObj); } + /** + * Public accessor for the initializeSearchWordDataInTsfe() method. + */ + public function callInitializeSearchWordDataInTsfe() { + static::initializeSearchWordDataInTsfe(); + } } \ No newline at end of file diff --git a/typo3/sysext/frontend/Tests/Unit/Page/PageGeneratorTest.php b/typo3/sysext/frontend/Tests/Unit/Page/PageGeneratorTest.php index d638ffee9a107c2e778c5e5685feb028c7e675da..6fffa656c9d27398f6ffdd434da6ccd1c71ca765 100644 --- a/typo3/sysext/frontend/Tests/Unit/Page/PageGeneratorTest.php +++ b/typo3/sysext/frontend/Tests/Unit/Page/PageGeneratorTest.php @@ -146,4 +146,57 @@ class PageGeneratorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase { $this->assertSame($expectedTags, $result); } + /** + * @return array + */ + public function initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider() { + return array( + 'one simple search word' => array( + array('test'), + FALSE, + 'test', + ), + 'one simple search word with standalone words' => array( + array('test'), + TRUE, + '[[:space:]]test[[:space:]]', + ), + 'two simple search words' => array( + array('test', 'test2'), + FALSE, + 'test|test2', + ), + 'two simple search words with standalone words' => array( + array('test', 'test2'), + TRUE, + '[[:space:]]test[[:space:]]|[[:space:]]test2[[:space:]]', + ), + 'word with regex chars' => array( + array('A \\ word with / a bunch of [] regex () chars .*'), + FALSE, + 'A word with \\/ a bunch of \\[\\] regex \\(\\) chars \\.\\*', + ), + ); + } + + /** + * @test + * @dataProvider initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider + * + * @param array $searchWordGetParameters The values that should be loaded in the sword_list GET parameter. + * @param bool $enableStandaloneSearchWords If TRUE the sword_standAlone option will be enabled. + * @param string $expectedRegex The expected regex after processing the search words. + */ + public function initializeSearchWordDataInTsfeBuildsCorrectRegex(array $searchWordGetParameters, $enableStandaloneSearchWords, $expectedRegex) { + + $_GET['sword_list'] = $searchWordGetParameters; + + $GLOBALS['TSFE'] = new \stdClass(); + if ($enableStandaloneSearchWords) { + $GLOBALS['TSFE']->config = array('config' => array('sword_standAlone' => 1)); + } + + $this->pageGeneratorFixture->callInitializeSearchWordDataInTsfe(); + $this->assertEquals($GLOBALS['TSFE']->sWordRegEx, $expectedRegex); + } }