[TASK] Move render arguments StripTagsViewHelper 98/48898/4
authorivaylo.ivanov <ivaylo.ivanov@dkd.de>
Mon, 11 Jul 2016 13:21:46 +0000 (15:21 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 12 Jul 2016 12:44:35 +0000 (14:44 +0200)
Move the argument registrations away from the render() method
to initializeArguments(), to prevent any errors with PHP7 and
subclassed ViewHelpers if/when render() method signatures change.

Resolves: #77008
Releases: master
Change-Id: Ia77204e3987e8013bb2e5ba11a40eb4c1c56e62f
Reviewed-on: https://review.typo3.org/48898
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/fluid/Classes/ViewHelpers/Format/StripTagsViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/StripTagsViewHelperTest.php

index c2f2c90..b063880 100644 (file)
@@ -50,6 +50,19 @@ class StripTagsViewHelper extends AbstractViewHelper
     protected $escapeOutput = false;
 
     /**
+     * Initialize ViewHelper arguments
+     *
+     * @return void
+     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('value', 'string', 'string to format');
+        $this->registerArgument('allowedTags', 'string', 'Optional string of allowed tags as required by PHPs strip_tags() f
+unction');
+    }
+
+    /**
      * To ensure all tags are removed, child node's output must not be escaped
      *
      * @var bool
@@ -59,14 +72,15 @@ class StripTagsViewHelper extends AbstractViewHelper
     /**
      * Escapes special characters with their escaped counterparts as needed using PHPs strip_tags() function.
      *
-     * @param string $value string to format
-     * @param string $allowedTags Optional string of allowed tags as required by PHPs strip_tags() function
      * @return mixed
      * @see http://www.php.net/manual/function.strip-tags.php
      * @api
      */
-    public function render($value = null, $allowedTags = null)
+    public function render()
     {
+        $value = $this->arguments['value'];
+        $allowedTags = $this->arguments['allowedTags'];
+
         return static::renderStatic(
             array(
                 'value' => $value,
@@ -85,15 +99,19 @@ class StripTagsViewHelper extends AbstractViewHelper
      * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
      * @return string
      */
-    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
-    {
+    public static function renderStatic(
+        array $arguments,
+        \Closure $renderChildrenClosure,
+        RenderingContextInterface $renderingContext
+    ) {
         $value = $arguments['value'];
+        $allowedTags = $arguments['allowedTags'];
         if ($value === null) {
             $value = $renderChildrenClosure();
         }
         if (!is_string($value)) {
             return $value;
         }
-        return strip_tags($value, (string)$arguments['allowedTags']);
+        return strip_tags($value, $allowedTags);
     }
 }
index 95f5983..ac12bd1 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  * The TYPO3 project - inspiring people to share!
  */
 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3\CMS\Fluid\ViewHelpers\Format\StripTagsViewHelper;
 
 /**
  * Test case
@@ -28,11 +29,8 @@ class StripTagsViewHelperTest extends ViewHelperBaseTestcase
     protected function setUp()
     {
         parent::setUp();
-        $this->viewHelper = $this->getMockBuilder(\TYPO3\CMS\Fluid\ViewHelpers\Format\StripTagsViewHelper::class)
-            ->setMethods(array('renderChildren'))
-            ->getMock();
+        $this->viewHelper = new StripTagsViewHelper();
         $this->injectDependenciesIntoViewHelper($this->viewHelper);
-        $this->viewHelper->initializeArguments();
     }
 
     /**
@@ -40,8 +38,13 @@ class StripTagsViewHelperTest extends ViewHelperBaseTestcase
      */
     public function renderUsesValueAsSourceIfSpecified()
     {
-        $this->viewHelper->expects($this->never())->method('renderChildren');
-        $actualResult = $this->viewHelper->render('Some string');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'value' => 'Some string',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Some string', $actualResult);
     }
 
@@ -50,8 +53,13 @@ class StripTagsViewHelperTest extends ViewHelperBaseTestcase
      */
     public function renderUsesChildnodesAsSourceIfSpecified()
     {
-        $this->viewHelper->expects($this->atLeastOnce())->method('renderChildren')->will($this->returnValue('Some string'));
-        $actualResult = $this->viewHelper->render();
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'value' => 'Some string',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Some string', $actualResult);
     }
 
@@ -73,10 +81,20 @@ class StripTagsViewHelperTest extends ViewHelperBaseTestcase
     /**
      * @test
      * @dataProvider stringsTestDataProvider
+     * @param $source
+     * @param $allowed
+     * @param $expectedResult
      */
     public function renderCorrectlyConvertsIntoPlaintext($source, $allowed, $expectedResult)
     {
-        $actualResult = $this->viewHelper->render($source, $allowed);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'value' => $source,
+                'allowedTags' => $allowed
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertSame($expectedResult, $actualResult);
     }
 
@@ -86,7 +104,13 @@ class StripTagsViewHelperTest extends ViewHelperBaseTestcase
     public function renderReturnsUnmodifiedSourceIfItIsNoString()
     {
         $source = new \stdClass();
-        $actualResult = $this->viewHelper->render($source);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'value' => $source
+            ]
+        );
+        $actualResult = $this->viewHelper->render();
         $this->assertSame($source, $actualResult);
     }
 }