[TASK] Make Link/TypolinkViewHelper compilable 28/39228/7
authordirk_juettner <dirk.juettner@gmail.com>
Sun, 3 May 2015 08:58:07 +0000 (10:58 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 3 May 2015 10:52:27 +0000 (12:52 +0200)
Resolves: #66767
Releases: master
Change-Id: Ibbab38408b4c5bf16cf74ea57d8f62b86e62b9c4
Reviewed-on: http://review.typo3.org/39228
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/fluid/Classes/ViewHelpers/Link/TypolinkViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Link/TypolinkViewHelperTest.php

index 2bf98d6..0136b88 100644 (file)
@@ -15,7 +15,9 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
  *                                                                        */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 
 /**
@@ -48,7 +50,7 @@ use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
  * </output>
  *
  */
-class TypolinkViewHelper extends AbstractViewHelper {
+class TypolinkViewHelper extends AbstractViewHelper implements CompilableInterface {
 
        /**
         * Render
@@ -63,8 +65,38 @@ class TypolinkViewHelper extends AbstractViewHelper {
         * @return string
         */
        public function render($parameter, $target = '', $class = '', $title = '', $additionalParams = '', $additionalAttributes = array()) {
+               return self::renderStatic(
+                       array(
+                               'parameter' => $parameter,
+                               'target' => $target,
+                               'class' => $class,
+                               'title' => $title,
+                               'additionalParams' => $additionalParams,
+                               'additionalAttributes' => $additionalAttributes
+                       ),
+                       $this->buildRenderChildrenClosure(),
+                       $this->renderingContext
+               );
+       }
+
+       /**
+        * @param array $arguments
+        * @param callable $renderChildrenClosure
+        * @param RenderingContextInterface $renderingContext
+        * @return mixed|string
+        * @throws \InvalidArgumentException
+        * @throws \UnexpectedValueException
+        */
+       static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
+               $parameter = $arguments['parameter'];
+               $target = $arguments['target'];
+               $class = $arguments['class'];
+               $title = $arguments['title'];
+               $additionalParams = $arguments['additionalParams'];
+               $additionalAttributes = $arguments['additionalAttributes'];
+
                // Merge the $parameter with other arguments
-               $typolinkParameter = $this->createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
+               $typolinkParameter = self::createTypolinkParameterArrayFromArguments($parameter, $target, $class, $title, $additionalParams);
 
                // array(param1 -> value1, param2 -> value2) --> "param1=value1 param2=>value2" for typolink.ATagParams
                $extraAttributes = array();
@@ -74,11 +106,11 @@ class TypolinkViewHelper extends AbstractViewHelper {
                $aTagParams = implode(' ', $extraAttributes);
 
                // If no link has to be rendered, the inner content will be returned as such
-               $content = $this->renderChildren();
+               $content = $renderChildrenClosure();
 
                if ($parameter) {
                        /** @var ContentObjectRenderer $contentObject */
-                       $contentObject = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
+                       $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
                        $contentObject->start(array(), '');
                        $content = $contentObject->stdWrap(
                                $content,
@@ -105,7 +137,7 @@ class TypolinkViewHelper extends AbstractViewHelper {
         *
         * @return array Final merged typolink.parameter as array to be imploded with empty string later
         */
-       protected function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '') {
+       static protected function createTypolinkParameterArrayFromArguments($parameter, $target = '', $class = '', $title = '', $additionalParams = '') {
                // Explode $parameter by whitespace and remove any " around resulting array values
                $parameterArray = GeneralUtility::unQuoteFilenames($parameter, TRUE);
 
index df4a421..5be197e 100644 (file)
@@ -14,8 +14,10 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Link;
  * Public License for more details.                                       *
  *                                                                        */
 
+use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper;
 
 /**
  * Class TypolinkViewHelperTest
@@ -23,69 +25,26 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 class TypolinkViewHelperTest extends ViewHelperBaseTestcase {
 
        /**
-        * @test
-        */
-       public function renderCallsRenderChildrenOnce() {
-               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
-               $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
-               $subject->expects($this->once())->method('renderChildren');
-               $subject->render('');
-       }
-
-       /**
-        * @test
-        */
-       public function renderReturnsContentOfRenderChildrenIfNoLinkParameterIsGiven() {
-               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
-               $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
-               $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
-               $this->assertEquals('innerContent', $subject->render(''));
-       }
-
-       /**
-        * @test
+        * @var TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject
         */
-       public function renderGivesMergedParametersToContentObjectRenderer() {
-               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
-               $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
-               $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
-               $contentObjectRendererMock = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, array(), array(), '', FALSE);
-
-               $params = '19 _blank css-class "testtitle with whitespace" &X=y';
-               $target = '-';
-               $class = 'fluid_class';
-               $title = 'a new title';
-               $additionalParams = '&a=b';
-               $additionalAttributes = array(
-                       'value1' => 'param1',
-                       'value2' => 'par&am2', // Check htmlspecialchars is applied
-               );
-
-               $expectedParametersToStdWrap = array(
-                       'typolink.' => array(
-                               'parameter' => '"19" - "css-class fluid_class" "a new title" "&X=y&a=b"',
-                               'ATagParams' => 'value1="param1" value2="par&amp;am2"',
-                       ),
-               );
-
-               $contentObjectRendererMock->expects($this->once())->method('stdWrap')->with('innerContent', $expectedParametersToStdWrap);
-
-               GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $contentObjectRendererMock);
+       protected $subject;
 
-               $subject->render($params, $target, $class, $title, $additionalParams, $additionalAttributes);
+       public function setUp() {
+               $this->subject = $this->getAccessibleMock(TypolinkViewHelper::class, array('renderChildren'));
+               /** @var RenderingContext  $renderingContext */
+               $renderingContext = $this->getMock(RenderingContext::class);
+               $this->subject->setRenderingContext($renderingContext);
        }
 
        /**
         * @test
         */
        public function renderReturnsResultOfContentObjectRenderer() {
-               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
-               $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
-               $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
+               $this->subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
                $contentObjectRendererMock = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, array(), array(), '', FALSE);
                $contentObjectRendererMock->expects($this->once())->method('stdWrap')->will($this->returnValue('foo'));
                GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $contentObjectRendererMock);
-               $this->assertEquals('foo', $subject->render('42'));
+               $this->assertEquals('foo', $this->subject->render('42'));
        }
 
        /**
@@ -246,9 +205,7 @@ class TypolinkViewHelperTest extends ViewHelperBaseTestcase {
         * @dataProvider typoScriptConfigurationData
         */
        public function createTypolinkParameterArrayFromArgumentsReturnsExpectedArray($input, $targetFromFluid, $classFromFluid, $titleFromFluid, $additionalParametersFromFluid, $expected) {
-               /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
-               $subject = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('dummy'));
-               $result = $subject->_call('createTypolinkParameterArrayFromArguments', $input, $targetFromFluid, $classFromFluid, $titleFromFluid, $additionalParametersFromFluid);
+               $result = $this->subject->_call('createTypolinkParameterArrayFromArguments', $input, $targetFromFluid, $classFromFluid, $titleFromFluid, $additionalParametersFromFluid);
                $this->assertSame($expected, $result);
        }