[+TASK] Fluid (Tests): Backported FormViewHelperTest from Fluid package
authorBastian Waidelich <bastian@typo3.org>
Mon, 19 Jul 2010 19:40:53 +0000 (19:40 +0000)
committerBastian Waidelich <bastian@typo3.org>
Mon, 19 Jul 2010 19:40:53 +0000 (19:40 +0000)
[+BUGFIX] Fluid (ViewHelpers): FormViewHelper wraps hidden fields with a div tag to create XHTML valid output. This fixes #5512 (backported from Fluid package)

typo3/sysext/fluid/Classes/ViewHelpers/FormViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/FormViewHelperTest.php [new file with mode: 0644]

index 2783818..8bf49fb 100644 (file)
@@ -138,10 +138,12 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
 
                $formContent = $this->renderChildren();
 
-               $content = $this->renderHiddenIdentityField($this->arguments['object'], $this->arguments['name']);
+               $content = chr(10) . '<div style="display: none">';
+               $content .= $this->renderHiddenIdentityField($this->arguments['object'], $this->arguments['name']);
                $content .= $this->renderAdditionalIdentityFields();
                $content .= $this->renderHiddenReferrerFields();
                $content .= $this->renderRequestHashField(); // Render hmac after everything else has been rendered
+               $content .= chr(10) . '</div>' . chr(10);
                $content .= $formContent;
 
                $this->tag->setContent($content);
@@ -173,9 +175,9 @@ class Tx_Fluid_ViewHelpers_FormViewHelper extends Tx_Fluid_ViewHelpers_Form_Abst
                                ->setUseCacheHash(!$this->arguments['noCacheHash'])
                                ->setSection($this->arguments['section'])
                                ->setCreateAbsoluteUri($this->arguments['absolute'])
-                               ->setArguments($this->arguments['additionalParams'])
+                               ->setArguments((array)$this->arguments['additionalParams'])
                                ->setAddQueryString($this->arguments['addQueryString'])
-                               ->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString'])
+                               ->setArgumentsToBeExcludedFromQueryString((array)$this->arguments['argumentsToBeExcludedFromQueryString'])
                                ->setFormat($this->arguments['format'])
                                ->uriFor($this->arguments['action'], $this->arguments['arguments'], $this->arguments['controller'], $this->arguments['extensionName'], $this->arguments['pluginName']);
                        $this->formActionUriArguments = $uriBuilder->getArguments();
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/FormViewHelperTest.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/FormViewHelperTest.php
new file mode 100644 (file)
index 0000000..4a31043
--- /dev/null
@@ -0,0 +1,244 @@
+<?php
+
+/*                                                                        *
+ * This script belongs to the FLOW3 package "Fluid".                      *
+ *                                                                        *
+ * It is free software; you can redistribute it and/or modify it under    *
+ * the terms of the GNU General Public License as published by the Free   *
+ * Software Foundation, either version 3 of the License, or (at your      *
+ * option) any later version.                                             *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        *
+ * You should have received a copy of the GNU General Public License      *
+ * along with the script.                                                 *
+ * If not, see http://www.gnu.org/licenses/gpl.html                       *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ */
+
+require_once(dirname(__FILE__) . '/ViewHelperBaseTestcase.php');
+
+/**
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_FormViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function renderAddsObjectToViewHelperVariableContainer() {
+               $formObject = new stdClass();
+
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormNameToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormNameFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $formObject)));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject', $formObject);
+               $this->viewHelperVariableContainer->expects($this->at(1))->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties', array());
+               $this->viewHelperVariableContainer->expects($this->at(2))->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
+               $this->viewHelperVariableContainer->expects($this->at(3))->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties');
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function renderAddsFormNameToTemplateVariableContainer() {
+               $formName = 'someFormName';
+
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormObjectToViewHelperVariableContainer', 'addFieldNamePrefixToViewHelperVariableContainer', 'removeFormObjectFromViewHelperVariableContainer', 'removeFieldNamePrefixFromViewHelperVariableContainer', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => $formName)));
+
+               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName', $formName);
+               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function renderCallsRenderHiddenReferrerFields() {
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenReferrerFields'), array(), '', FALSE);
+               $viewHelper->expects($this->once())->method('renderHiddenReferrerFields');
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function renderCallsRenderHiddenIdentityField() {
+               $object = new stdClass();
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderHiddenIdentityField'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('object' => $object, 'name' => 'MyName')));
+               $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($object, 'MyName');
+
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function renderCallsRenderAdditionalIdentityFields() {
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderRequestHashField', 'renderAdditionalIdentityFields'), array(), '', FALSE);
+               $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields');
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderWrapsHiddenFieldsWithDivForXhtmlCompatibility() {
+               $viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_FormViewHelper'), array('renderChildren', 'renderHiddenIdentityField', 'renderAdditionalIdentityFields', 'renderHiddenReferrerFields', 'renderRequestHashField'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+               $viewHelper->expects($this->once())->method('renderHiddenIdentityField')->will($this->returnValue('hiddenIdentityField'));
+               $viewHelper->expects($this->once())->method('renderAdditionalIdentityFields')->will($this->returnValue('additionalIdentityFields'));
+               $viewHelper->expects($this->once())->method('renderHiddenReferrerFields')->will($this->returnValue('hiddenReferrerFields'));
+               $viewHelper->expects($this->once())->method('renderRequestHashField')->will($this->returnValue('requestHashField'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('formContent'));
+
+               $expectedResult = chr(10) . '<div style="display: none">' . 'hiddenIdentityFieldadditionalIdentityFieldshiddenReferrerFieldsrequestHashField' . chr(10) . '</div>' . chr(10) . 'formContent';
+               $this->tagBuilder->expects($this->once())->method('setContent')->with($expectedResult);
+
+               $viewHelper->render();
+       }
+
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function renderAdditionalIdentityFieldsFetchesTheFieldsFromViewHelperVariableContainerAndBuildsHiddenFieldsForThem() {
+               $identityProperties = array(
+                       'object1[object2]' => '<input type="hidden" name="object1[object2][__identity]" value="42" />',
+                       'object1[object2][subobject]' => '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />'
+               );
+               $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue(TRUE));
+               $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'additionalIdentityProperties')->will($this->returnValue($identityProperties));
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $expected = chr(10) . '<input type="hidden" name="object1[object2][__identity]" value="42" />' . chr(10) .
+                       '<input type="hidden" name="object1[object2][subobject][__identity]" value="21" />';
+               $actual = $viewHelper->_call('renderAdditionalIdentityFields');
+               $this->assertEquals($expected, $actual);
+       }
+
+       /**
+        * @test
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function renderHiddenReferrerFieldsAddCurrentControllerAndActionAsHiddenFields() {
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $this->request->expects($this->atLeastOnce())->method('getControllerExtensionName')->will($this->returnValue('extensionName'));
+               $this->request->expects($this->never())->method('getControllerSubextensionName');
+               $this->request->expects($this->atLeastOnce())->method('getControllerName')->will($this->returnValue('controllerName'));
+               $this->request->expects($this->atLeastOnce())->method('getControllerActionName')->will($this->returnValue('controllerActionName'));
+
+               $hiddenFields = $viewHelper->_call('renderHiddenReferrerFields');
+               $expectedResult = chr(10) . '<input type="hidden" name="__referrer[extensionName]" value="extensionName" />' . chr(10) .
+                       '<input type="hidden" name="__referrer[controllerName]" value="controllerName" />' . chr(10) .
+                       '<input type="hidden" name="__referrer[actionName]" value="controllerActionName" />' . chr(10);
+               $this->assertEquals($expectedResult, $hiddenFields);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsSpecifiedPrefixToTemplateVariableContainer() {
+               $prefix = 'somePrefix';
+
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $viewHelper->setArguments(new Tx_Fluid_Core_ViewHelper_Arguments(array('fieldNamePrefix' => $prefix)));
+
+               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $prefix);
+               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
+               $viewHelper->render();
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsDefaultFieldNamePrefixToTemplateVariableContainerIfNoPrefixIsSpecified() {
+               $expectedPrefix = 'tx_someextension_someplugin';
+               $this->request->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtension'));
+               $this->request->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePlugin'));
+
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('renderChildren', 'renderHiddenIdentityField', 'renderHiddenReferrerFields', 'renderRequestHashField', 'addFormFieldNamesToViewHelperVariableContainer', 'removeFormFieldNamesFromViewHelperVariableContainer'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($viewHelper);
+
+               $this->viewHelperVariableContainer->expects($this->once())->method('add')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix', $expectedPrefix);
+               $this->viewHelperVariableContainer->expects($this->once())->method('remove')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix');
+               $viewHelper->render();
+       }
+
+       /**
+        * Data Provider for postProcessUriArgumentsForRequestHashWorks
+        */
+       public function argumentsForPostProcessUriArgumentsForRequestHash() {
+               return array(
+                       // simple values
+                       array(
+                               array(
+                                       'bla' => 'X',
+                                       'blubb' => 'Y'
+                               ),
+                               array(
+                                       'bla',
+                                       'blubb'
+                               )
+                       ),
+                       // Arrays
+                       array(
+                               array(
+                                       'bla' => array(
+                                               'test1' => 'X',
+                                               'test2' => 'Y'
+                                       ),
+                                       'blubb' => 'Y'
+                               ),
+                               array(
+                                       'bla[test1]',
+                                       'bla[test2]',
+                                       'blubb'
+                               )
+                       )
+               );
+       }
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @dataProvider argumentsForPostProcessUriArgumentsForRequestHash
+        */
+       public function postProcessUriArgumentsForRequestHashWorks($arguments, $expectedResults) {
+               $viewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_FormViewHelper', array('dummy'), array(), '', FALSE);
+               $results = array();
+               $viewHelper->_callRef('postProcessUriArgumentsForRequestHash', $arguments, $results);
+               $this->assertEquals($expectedResults, $results);
+       }
+}
+?>
\ No newline at end of file