[TASK] Move arguments to initializeArguments() in CropVH in ext:fluid 07/48907/5
authorRoberto Torresani <erreti@gmail.com>
Mon, 11 Jul 2016 15:30:29 +0000 (17:30 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Mon, 11 Jul 2016 19:51:57 +0000 (21:51 +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
Change the UnitTest CropViewHelperTest with setArguments()
Introduce the function setArgumentsUnderTest()

Resolves: #77038
Releases: master
Change-Id: Iec2b53cd770bcaf449915687e761d565fbc56418
Reviewed-on: https://review.typo3.org/48907
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/fluid/Classes/ViewHelpers/Format/CropViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/CropViewHelperTest.php

index c5169d6..5cafe75 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
 
 /*
@@ -64,28 +65,44 @@ use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 class CropViewHelper extends AbstractViewHelper
 {
     /**
-     * The output may contain HTML and can not be escaped
+     * The output may contain HTML and can not be escaped.
      *
      * @var bool
      */
     protected $escapeOutput = false;
 
     /**
-     * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
+     * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current['TSFE'] if used in BE mode
      */
     protected static $tsfeBackup;
 
     /**
-     * Render the cropped text
+     * Initialize arguments.
+     *
+     * @api
+     * @throws \TYPO3Fluid\Fluid\Core\ViewHelper\Exception
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('maxCharacters', 'int', 'Place where to truncate the string', true);
+        $this->registerArgument('append', 'string', 'What to append, if truncation happened', false, '...');
+        $this->registerArgument('respectWordBoundaries', 'bool', 'If TRUE and division is in the middle of a word, the remains of that word is removed.', false, true);
+        $this->registerArgument('respectHtml', 'bool', 'If TRUE the cropped string will respect HTML tags and entities. Technically that means, that cropHTML() is called rather than crop()', false, true);
+    }
+
+    /**
+     * Render the cropped text.
      *
-     * @param int $maxCharacters Place where to truncate the string
-     * @param string $append What to append, if truncation happened
-     * @param bool $respectWordBoundaries If TRUE and division is in the middle of a word, the remains of that word is removed.
-     * @param bool $respectHtml If TRUE the cropped string will respect HTML tags and entities. Technically that means, that cropHTML() is called rather than crop()
      * @return string cropped text
+     * @throws \InvalidArgumentException
      */
-    public function render($maxCharacters, $append = '...', $respectWordBoundaries = true, $respectHtml = true)
+    public function render()
     {
+        $maxCharacters = $this->arguments['maxCharacters'];
+        $append = $this->arguments['append'];
+        $respectWordBoundaries = $this->arguments['respectWordBoundaries'];
+        $respectHtml = $this->arguments['respectHtml'];
+
         return static::renderStatic(
             array(
                 'maxCharacters' => $maxCharacters,
@@ -102,7 +119,9 @@ class CropViewHelper extends AbstractViewHelper
      * @param array $arguments
      * @param \Closure $renderChildrenClosure
      * @param RenderingContextInterface $renderingContext
+     *
      * @return string
+     * @throws \InvalidArgumentException
      */
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
@@ -132,15 +151,15 @@ class CropViewHelper extends AbstractViewHelper
         if (TYPO3_MODE === 'BE') {
             self::resetFrontendEnvironment();
         }
+
         return $content;
     }
 
     /**
      * Sets the global variable $GLOBALS['TSFE']->csConvObj in Backend mode
      * This somewhat hacky work around is currently needed because the crop() and cropHTML() functions of
-     * ContentObjectRenderer rely on those variables to be set
-     *
-     * @return void
+     * ContentObjectRenderer rely on those variables to be set.
+     * @throws \InvalidArgumentException
      */
     protected static function simulateFrontendEnvironment()
     {
@@ -153,9 +172,8 @@ class CropViewHelper extends AbstractViewHelper
     }
 
     /**
-     * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
+     * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment().
      *
-     * @return void
      * @see simulateFrontendEnvironment()
      */
     protected static function resetFrontendEnvironment()
index 3619a0d..d76621b 100644 (file)
@@ -13,21 +13,24 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use PHPUnit_Framework_MockObject_MockObject;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
+use TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 
 /**
- * Test case
+ * Test case.
  */
-class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
+class CropViewHelperTest extends ViewHelperBaseTestcase
 {
     /**
-     * @var \TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper
+     * @var CropViewHelper
      */
     protected $viewHelper;
 
     /**
-     * @var ContentObjectRenderer
+     * @var ContentObjectRenderer|PHPUnit_Framework_MockObject_MockObject
      */
     protected $mockContentObject;
 
@@ -35,13 +38,13 @@ class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         parent::setUp();
         $this->mockContentObject = $this->createMock(ContentObjectRenderer::class);
-        $this->viewHelper = $this->getMockBuilder(\TYPO3\CMS\Fluid\ViewHelpers\Format\CropViewHelper::class)
-            ->setMethods(array('renderChildren'))
-            ->getMock();
-
-        $renderingContext = $this->createMock(\TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture::class);
-        $this->viewHelper->setRenderingContext($renderingContext);
-        $this->viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('Some Content'));
+        $this->viewHelper = new CropViewHelper();
+        $this->injectDependenciesIntoViewHelper($this->viewHelper);
+        $this->viewHelper->setRenderChildrenClosure(
+            function () {
+                return 'Some Content';
+            }
+        );
     }
 
     /**
@@ -51,7 +54,13 @@ class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '123|...|1')->will($this->returnValue('Cropped Content'));
         GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
-        $actualResult = $this->viewHelper->render(123);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'maxCharacters' => '123',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Cropped Content', $actualResult);
     }
 
@@ -62,7 +71,14 @@ class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '-321|custom suffix|1')->will($this->returnValue('Cropped Content'));
         GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
-        $actualResult = $this->viewHelper->render(-321, 'custom suffix');
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'maxCharacters' => '-321',
+                'append' => 'custom suffix',
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Cropped Content', $actualResult);
     }
 
@@ -73,7 +89,15 @@ class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $this->mockContentObject->expects($this->once())->method('cropHTML')->with('Some Content', '123|...|')->will($this->returnValue('Cropped Content'));
         GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
-        $actualResult = $this->viewHelper->render(123, '...', false);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'maxCharacters' => '123',
+                'append' => '...',
+                'respectWordBoundaries' => false,
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Cropped Content', $actualResult);
     }
 
@@ -84,7 +108,16 @@ class CropViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $this->mockContentObject->expects($this->once())->method('crop')->with('Some Content', '123|...|1')->will($this->returnValue('Cropped Content'));
         GeneralUtility::addInstance(ContentObjectRenderer::class, $this->mockContentObject);
-        $actualResult = $this->viewHelper->render(123, '...', true, false);
+        $this->setArgumentsUnderTest(
+            $this->viewHelper,
+            [
+                'maxCharacters' => '123',
+                'append' => '...',
+                'respectWordBoundaries' => true,
+                'respectHtml' => false,
+            ]
+        );
+        $actualResult = $this->viewHelper->initializeArgumentsAndRender();
         $this->assertEquals('Cropped Content', $actualResult);
     }
 }