[TASK] Move ViewHelper arguments to initializeArguments() in ext:fluid 66/48866/6
authorAnna Färber <anna.faerber@dkd.de>
Sun, 10 Jul 2016 10:36:56 +0000 (12:36 +0200)
committerJan Helke <typo3@helke.de>
Mon, 11 Jul 2016 09:27:19 +0000 (11:27 +0200)
HtmlentitiesViewHelper and HtmlentitiesViewHelperTest are affected.

Resolves: #77000
Releases: master
Change-Id: Ic6347d86a55aa0f7d740a9ff897bbb6f61d5b67f
Reviewed-on: https://review.typo3.org/48866
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Tested-by: Anna Färber <anna.faerber@dkd.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
typo3/sysext/fluid/Classes/ViewHelpers/Format/HtmlentitiesViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/HtmlentitiesViewHelperTest.php

index 0deab99..513302b 100644 (file)
@@ -55,18 +55,32 @@ class HtmlentitiesViewHelper extends AbstractEncodingViewHelper implements Singl
     protected $escapeChildren = false;
 
     /**
+     * Initialize ViewHelper arguments
+     *
+     * @return void
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('value', 'string', 'string to format', '', null);
+        $this->registerArgument('keepQuotes', 'bool', 'If TRUE, single and double quotes won\'t be replaced (sets ENT_NOQUOTES flag).', '', false);
+        $this->registerArgument('encoding', 'string', '', '', null);
+        $this->registerArgument('doubleEncode', 'bool', 'If FALSE existing html entities won\'t be encoded, the default is to convert everything.', '', true);
+    }
+
+    /**
      * Escapes special characters with their escaped counterparts as needed using PHPs htmlentities() function.
      *
-     * @param string $value string to format
-     * @param bool $keepQuotes if TRUE, single and double quotes won't be replaced (sets ENT_NOQUOTES flag)
-     * @param string $encoding
-     * @param bool $doubleEncode If FALSE existing html entities won't be encoded, the default is to convert everything.
      * @return string the altered string
      * @see http://www.php.net/manual/function.htmlentities.php
      * @api
      */
-    public function render($value = null, $keepQuotes = false, $encoding = null, $doubleEncode = true)
+    public function render()
     {
+        $value = $this->arguments['value'];
+        $encoding = $this->arguments['encoding'];
+        $keepQuotes = $this->arguments['keepQuotes'];
+        $doubleEncode = $this->arguments['doubleEncode'];
+
         if ($value === null) {
             $value = $this->renderChildren();
         }
index b9b328d..0ea3106 100644 (file)
@@ -13,6 +13,9 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use Prophecy\Prophecy\ObjectProphecy;
+use TYPO3\CMS\Extbase\Reflection\ReflectionService;
+use TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper;
 
 /**
  * Test case
@@ -20,16 +23,27 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
 class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 {
     /**
+     * @var ReflectionService|ObjectProphecy
+     */
+    protected $reflectionServiceProphecy;
+
+    /**
      * @var \TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper
      */
     protected $viewHelper;
 
+    /**
+     * shortcut for default Arguments which would be prepared by initializeArguments()
+     *
+     * @var array
+     */
+    protected $defaultArguments;
+
     protected function setUp()
     {
-        $this->viewHelper = $this->getMockBuilder(\TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper::class)
-            ->setMethods(array('renderChildren', 'resolveDefaultEncoding'))
-            ->getMock();
-        $this->viewHelper->expects($this->any())->method('resolveDefaultEncoding')->will($this->returnValue('UTF-8'));
+        $this->reflectionServiceProphecy = $this->prophesize(ReflectionService::class);
+        $this->viewHelper = new HtmlentitiesViewHelper();
+        $this->viewHelper->injectReflectionService($this->reflectionServiceProphecy->reveal());
     }
 
     /**
@@ -37,8 +51,12 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function renderUsesValueAsSourceIfSpecified()
     {
-        $this->viewHelper->expects($this->never())->method('renderChildren');
-        $actualResult = $this->viewHelper->render('Some string');
+        $this->setArgumentsUnderTest(
+            [
+                'value' => 'Some string',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Some string', $actualResult);
     }
 
@@ -47,8 +65,13 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
      */
     public function renderUsesChildnodesAsSourceIfSpecified()
     {
-        $this->viewHelper->expects($this->atLeastOnce())->method('renderChildren')->will($this->returnValue('Some string'));
-        $actualResult = $this->viewHelper->render();
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 'Some string';
+            }
+        );
+        $this->setArgumentsUnderTest();
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Some string', $actualResult);
     }
 
@@ -58,7 +81,12 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderDoesNotModifyValueIfItDoesNotContainSpecialCharacters()
     {
         $source = 'This is a sample text without special characters.';
-        $actualResult = $this->viewHelper->render($source);
+        $this->setArgumentsUnderTest(
+            [
+                'value' => $source,
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertSame($source, $actualResult);
     }
 
@@ -68,8 +96,11 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderDecodesSimpleString()
     {
         $source = 'Some special characters: &©"\'';
+        $this->setArgumentsUnderTest(
+            ['value' => $source]
+        );
         $expectedResult = 'Some special characters: &amp;&copy;&quot;\'';
-        $actualResult = $this->viewHelper->render($source);
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals($expectedResult, $actualResult);
     }
 
@@ -79,8 +110,14 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderRespectsKeepQuoteArgument()
     {
         $source = 'Some special characters: &©"\'';
+        $this->setArgumentsUnderTest(
+            [
+                'value' => $source,
+                'keepQuotes' => true,
+            ]
+        );
         $expectedResult = 'Some special characters: &amp;&copy;"\'';
-        $actualResult = $this->viewHelper->render($source, true);
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals($expectedResult, $actualResult);
     }
 
@@ -90,8 +127,14 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderRespectsEncodingArgument()
     {
         $source = utf8_decode('Some special characters: &©"\'');
+        $this->setArgumentsUnderTest(
+            [
+                'value' => $source,
+                'encoding' => 'ISO-8859-1',
+            ]
+        );
         $expectedResult = 'Some special characters: &amp;&copy;&quot;\'';
-        $actualResult = $this->viewHelper->render($source, false, 'ISO-8859-1');
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals($expectedResult, $actualResult);
     }
 
@@ -101,8 +144,11 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderConvertsAlreadyConvertedEntitiesByDefault()
     {
         $source = 'already &quot;encoded&quot;';
+        $this->setArgumentsUnderTest(
+            ['value' => $source]
+        );
         $expectedResult = 'already &amp;quot;encoded&amp;quot;';
-        $actualResult = $this->viewHelper->render($source);
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals($expectedResult, $actualResult);
     }
 
@@ -112,18 +158,47 @@ class HtmlentitiesViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     public function renderDoesNotConvertAlreadyConvertedEntitiesIfDoubleQuoteIsFalse()
     {
         $source = 'already &quot;encoded&quot;';
+        $this->setArgumentsUnderTest(
+            [
+                'value' => $source,
+                'doubleEncode' => false,
+            ]
+        );
         $expectedResult = 'already &quot;encoded&quot;';
-        $actualResult = $this->viewHelper->render($source, false, 'UTF-8', false);
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals($expectedResult, $actualResult);
     }
 
     /**
+     * This test tests the behaviour of render without relying on the validation of registerArguments
+     * In the normal course of things "value" can't be anything but a string as it is registered that way
+     *
      * @test
      */
     public function renderReturnsUnmodifiedSourceIfItIsNoString()
     {
         $source = new \stdClass();
-        $actualResult = $this->viewHelper->render($source);
+        $this->setArgumentsUnderTest(
+            ['value' => $source]
+        );
+        $actualResult = $this->viewHelper->render();
         $this->assertSame($source, $actualResult);
     }
+
+    /**
+     * Helper function to merge arguments with default arguments according to their registration
+     * This usually happens in ViewHelperInvoker before the view helper methods are called
+     *
+     * @param array $arguments
+     */
+    protected function setArgumentsUnderTest(array $arguments = [])
+    {
+        $argumentDefinitions = $this->viewHelper->prepareArguments();
+        foreach ($argumentDefinitions as $argumentName => $argumentDefinition) {
+            if (!isset($arguments[$argumentName])) {
+                $arguments[$argumentName] = $argumentDefinition->getDefaultValue();
+            }
+        }
+        $this->viewHelper->setArguments($arguments);
+    }
 }