[TASK] Extract _GP call from TSFE->initializeSearchWordData 81/58081/2
authorBenni Mack <benni@typo3.org>
Thu, 30 Aug 2018 09:14:44 +0000 (11:14 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 30 Aug 2018 12:04:29 +0000 (14:04 +0200)
In order to centralize usages regarding global magic ($_GET/$_POST) within
TypoScriptFrontendController, the method initializeSearchWordData()
is renamed to something useful, and is now handed in the variable from
GeneralUtility::_GP() and also gets some unit tests, making this method
also notice-free, as a side-effect.

This pre-patch allows to inject something else (PSR-7 request) in the future
without ever touching this method.

Resolves: #86035
Releases: master
Change-Id: I3db94dd3ce23de8d2f8772067ec15d38d0f7cf64
Reviewed-on: https://review.typo3.org/58081
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Tests/Unit/Controller/TypoScriptFrontendControllerTest.php

index 9df0947..a473cb9 100644 (file)
@@ -3452,7 +3452,7 @@ class TypoScriptFrontendController implements LoggerAwareInterface
             $this->absRefPrefix = '';
         }
         $this->ATagParams = trim($this->config['config']['ATagParams'] ?? '') ? ' ' . trim($this->config['config']['ATagParams']) : '';
-        $this->initializeSearchWordDataInTsfe();
+        $this->initializeSearchWordData(GeneralUtility::_GP('sword_list'));
         // linkVars
         $this->calculateLinkVars();
         // Setting XHTML-doctype from doctype
@@ -3492,21 +3492,23 @@ class TypoScriptFrontendController implements LoggerAwareInterface
      * Fills the sWordList property and builds the regular expression in TSFE that can be used to split
      * strings by the submitted search words.
      *
+     * @param mixed $searchWords - usually an array, but we can't be sure (yet)
      * @see sWordList
      * @see sWordRegEx
      */
-    protected function initializeSearchWordDataInTsfe()
+    protected function initializeSearchWordData($searchWords)
     {
         $this->sWordRegEx = '';
-        $this->sWordList = GeneralUtility::_GP('sword_list');
+        $this->sWordList = $searchWords === null ? '' : $searchWords;
         if (is_array($this->sWordList)) {
-            $space = !empty($this->config['config']['sword_standAlone']) ? '[[:space:]]' : '';
+            $space = !empty($this->config['config']['sword_standAlone'] ?? null) ? '[[:space:]]' : '';
+            $regexpParts = [];
             foreach ($this->sWordList as $val) {
                 if (trim($val) !== '') {
-                    $this->sWordRegEx .= $space . preg_quote($val, '/') . $space . '|';
+                    $regexpParts[] = $space . preg_quote($val, '/') . $space;
                 }
             }
-            $this->sWordRegEx = rtrim($this->sWordRegEx, '|');
+            $this->sWordRegEx = implode('|', $regexpParts);
         }
     }
 
index cace168..7e9865c 100644 (file)
@@ -413,4 +413,60 @@ class TypoScriptFrontendControllerTest extends UnitTestCase
             ],
         ];
     }
+
+    /**
+     * @test
+     */
+    public function initializeSearchWordDataDoesNothingWithNullValue()
+    {
+        $subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
+        $subject->_call('initializeSearchWordData', null);
+        $this->assertEquals('', $subject->sWordRegEx);
+        $this->assertEquals('', $subject->sWordList);
+    }
+
+    /**
+     * @test
+     */
+    public function initializeSearchWordDataDoesNothingWithEmptyStringValue()
+    {
+        $subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
+        $subject->_call('initializeSearchWordData', '');
+        $this->assertEquals('', $subject->sWordRegEx);
+        $this->assertEquals('', $subject->sWordList);
+    }
+
+    /**
+     * @test
+     */
+    public function initializeSearchWordDataDoesNothingWithEmptyArrayValue()
+    {
+        $subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
+        $subject->_call('initializeSearchWordData', []);
+        $this->assertEquals('', $subject->sWordRegEx);
+        $this->assertEquals([], $subject->sWordList);
+    }
+
+    /**
+     * @test
+     */
+    public function initializeSearchWordDataFillsProperRegexpWithArray()
+    {
+        $subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
+        $subject->_call('initializeSearchWordData', ['stop', 'word']);
+        $this->assertEquals('stop|word', $subject->sWordRegEx);
+        $this->assertEquals(['stop', 'word'], $subject->sWordList);
+    }
+
+    /**
+     * @test
+     */
+    public function initializeSearchWordDataFillsProperRegexpWithArrayAndStandaloneOption()
+    {
+        $subject = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
+        $subject->config['config']['sword_standAlone'] = 1;
+        $subject->_call('initializeSearchWordData', ['stop', 'word']);
+        $this->assertEquals('[[:space:]]stop[[:space:]]|[[:space:]]word[[:space:]]', $subject->sWordRegEx);
+        $this->assertEquals(['stop', 'word'], $subject->sWordList);
+    }
 }