[~TASK] Fluid (ViewHelpers): Follow-up. Added several files.
authorJochen Rau <j.rau@web.de>
Mon, 10 Aug 2009 23:39:22 +0000 (23:39 +0000)
committerJochen Rau <j.rau@web.de>
Mon, 10 Aug 2009 23:39:22 +0000 (23:39 +0000)
typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Form/ErrorsViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/Form/AbstractFormFieldViewHelperTest_testcase.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/Form/HiddenViewHelperTest_testcase.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/ViewHelperBaseTestcase_testcase.php [new file with mode: 0644]

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php
new file mode 100644 (file)
index 0000000..9a42a73
--- /dev/null
@@ -0,0 +1,183 @@
+<?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 Lesser 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 Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * Abstract Form View Helper. Bundles functionality related to direct property access of objects in other Form ViewHelpers.
+ *
+ * If you set the "property" attribute to the name of the property to resolve from the object, this class will
+ * automatically set the name and value of a form element.
+ *
+ * @version $Id$
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ * @scope prototype
+ */
+abstract class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormViewHelper {
+
+       /**
+        * Initialize arguments.
+        *
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @api
+        */
+       public function initializeArguments() {
+               parent::initializeArguments();
+               $this->registerArgument('name', 'string', 'Name of input tag');
+               $this->registerArgument('value', 'mixed', 'Value of input tag');
+               $this->registerArgument('property', 'string', 'Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.');
+       }
+
+       /**
+        * Get the name of this form element.
+        * Either returns arguments['name'], or the correct name for Object Access.
+        *
+        * @return string Name
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @author Robert Lemke <robert@typo3.org>
+        * @author Karsten Dambekalns <karsten@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function getName() {
+               if ($this->isObjectAccessorMode()) {
+                       $formName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+                       if (!empty($formName)) {
+                               $name = $formName . '[' . $this->arguments['property'] . ']';
+                       } else {
+                               $name = $this->arguments['property'];
+                       }
+               } else {
+                       $name = $this->arguments['name'];
+               }
+               if ($this->arguments->hasArgument('value') && is_object($this->arguments['value'])) {
+                       if (NULL !== $this->persistenceManager->getBackend()->getIdentifierByObject($this->arguments['value'])
+                               && (!$this->persistenceManager->getBackend()->isNewObject($this->arguments['value']))) {
+                               $name .= '[uid]';
+                       }
+               }
+               return $this->prefixFieldName($name);
+       }
+
+       /**
+        * Get the value of this form element.
+        * Either returns arguments['value'], or the correct value for Object Access.
+        *
+        * @return mixed Value
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @author Robert Lemke <robert@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function getValue() {
+               $value = NULL;
+               if ($this->arguments->hasArgument('value')) {
+                       $value = $this->arguments['value'];
+               } elseif ($this->isObjectAccessorMode() && $this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')) {
+                       $value = $this->getPropertyValue();
+               }
+               if (is_object($value)) {
+                       $identifier = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
+                       if ($identifier !== NULL) {
+                               $value = $identifier;
+                       }
+               }
+               return $value;
+       }
+
+       /**
+        * Get the current property of the object bound to this form.
+        *
+        * @return mixed Value
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function getPropertyValue() {
+               $formObject = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject');
+               $propertyName = $this->arguments['property'];
+               if (is_array($formObject)) {
+                       return isset($formObject[$propertyName]) ? $formObject[$propertyName] : NULL;
+               }
+               return Tx_Extbase_Reflection_ObjectAccess::getProperty($formObject, $propertyName);
+       }
+
+       /**
+        * Internal method which checks if we should evaluate a domain object or just output arguments['name'] and arguments['value']
+        *
+        * @return boolean TRUE if we should evaluate the domain object, FALSE otherwise.
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       protected function isObjectAccessorMode() {
+               return $this->arguments->hasArgument('property')
+                       && $this->viewHelperVariableContainer->exists('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+       }
+
+       /**
+        * Add an CSS class if this view helper has errors
+        *
+        * @return void
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function setErrorClassAttribute() {
+               if ($this->arguments->hasArgument('class')) {
+                       $cssClass = $this->arguments['class'] . ' ';
+               } else {
+                       $cssClass = '';
+               }
+               $errors = $this->getErrorsForProperty();
+               if (count($errors) > 0) {
+                       if ($this->arguments->hasArgument('errorClass')) {
+                               $cssClass .= $this->arguments['errorClass'];
+                       } else {
+                               $cssClass .= 'error';
+                       }
+                       $this->tag->addAttribute('class', $cssClass);
+               }
+       }
+
+       /**
+        * Get errors for the property and form name of this view helper
+        *
+        * @return array An array of Tx_Fluid_Error_Error objects
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       protected function getErrorsForProperty() {
+               if (!$this->isObjectAccessorMode()) {
+                       return array();
+               }
+               $errors = $this->controllerContext->getRequest()->getErrors();
+               $formName = $this->viewHelperVariableContainer->get('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName');
+               $propertyName = $this->arguments['property'];
+               $formErrors = array();
+               foreach ($errors as $error) {
+                       if ($error instanceof Tx_Extbase_Validation_PropertyError && $error->getPropertyName() === $formName) {
+                               $formErrors = $error->getErrors();
+                               foreach ($formErrors as $formError) {
+                                       if ($formError instanceof Tx_Extbase_Validation_PropertyError && $formError->getPropertyName() === $propertyName) {
+                                               return $formError->getErrors();
+                                       }
+                               }
+                       }
+               }
+               return array();
+       }
+}
+
+?>
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Form/ErrorsViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Form/ErrorsViewHelper.php
new file mode 100644 (file)
index 0000000..bf68b78
--- /dev/null
@@ -0,0 +1,90 @@
+<?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 Lesser 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 Lesser       *
+ * General Public License for more details.                               *
+ *                                                                        *
+ * You should have received a copy of the GNU Lesser General Public       *
+ * License along with the script.                                         *
+ * If not, see http://www.gnu.org/licenses/lgpl.html                      *
+ *                                                                        *
+ * The TYPO3 project - inspiring people to share!                         *
+ *                                                                        */
+
+/**
+ * Error messages view helper
+ *
+ * = Examples =
+ *
+ * <code title="Output error messages as a list">
+ * <ul class="errors">
+ *   <f:errors>
+ *     <li>{error.code}: {error.message}</li>
+ *   </f:errors>
+ * </ul>
+ * </code>
+ *
+ * Output:
+ * <ul>
+ *   <li>1234567890: Validation errors for argument "newBlog"</li>
+ * </ul>
+ *
+ * @version $Id$
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ * @scope prototype
+ */
+class Tx_Fluid_ViewHelpers_Form_ErrorsViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
+
+       /**
+        * Iterates through selected errors of the request.
+        *
+        * @param string $for The name of the error name (e.g. argument name or property name)
+        * @param string $as The name of the variable to store the current error
+        * @return string Rendered string
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        * @api
+        */
+       public function render($for = '', $as = 'error') {
+               $errors = $this->controllerContext->getRequest()->getErrors();
+               if ($for !== '') {
+                       $errors = $this->getErrorsForProperty($for, $errors);
+               }
+               $output = '';
+               foreach ($errors as $errorKey => $error) {
+                       $this->templateVariableContainer->add($as, $error);
+                       $output .= $this->renderChildren();
+                       $this->templateVariableContainer->remove($as);
+               }
+               return $output;
+       }
+
+       /**
+        * Find errors for a specific property in the given errors array
+        *
+        * @param string $propertyName The property name to look up
+        * @param array $errors An array of Tx_Fluid_Error_Error objects
+        * @return array An array of errors for $propertyName
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       protected function getErrorsForProperty($propertyName, $errors) {
+               foreach ($errors as $error) {
+                       if ($error instanceof Tx_Extbase_Validation_PropertyError) {
+                               if ($error->getPropertyName() === $propertyName) {
+                                       return $error->getErrors();
+                               }
+                       }
+               }
+               return array();
+       }
+}
+
+?>
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/Form/AbstractFormFieldViewHelperTest_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/Form/AbstractFormFieldViewHelperTest_testcase.php
new file mode 100644 (file)
index 0000000..669cf70
--- /dev/null
@@ -0,0 +1,348 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * 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.                                       *
+ *                                                                        */
+
+require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
+
+/**
+ * Test for the Abstract Form view helper
+ *
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
+ */
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+class Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelperTest_testcase extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
+
+       /**
+        * @test
+        * @author Robert Lemke <robert@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function ifAnAttributeValueIsAnObjectMaintainedByThePersistenceManagerItIsConvertedToAUID() {
+               $mockPersistenceBackend = $this->getMock('Tx_Extbase_Persistence_BackendInterface');
+               $mockPersistenceBackend->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
+
+               $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
+               $mockPersistenceManager->expects($this->any())->method('getBackend')->will($this->returnValue($mockPersistenceBackend));
+
+               $className = 'Object' . uniqid();
+               $fullClassName = 'Tx_Fluid_ViewHelpers_Form_' . $className;
+               eval('class ' . $className . '  {
+                       public function FLOW3_Persistence_isNew() { return FALSE; }
+                       public function FLOW3_Persistence_isClone() { return FALSE; }
+                       public function FLOW3_Persistence_isDirty($propertyName) {}
+                       public function FLOW3_Persistence_memorizeCleanState($propertyName = NULL) {}
+                       public function FLOW3_AOP_Proxy_getProperty($name) {}
+                       public function FLOW3_AOP_Proxy_getProxyTargetClassName() {}
+                       public function __clone() {}
+               }');
+               $object = $this->getMock($fullClassName);
+               $object->expects($this->any())->method('FLOW3_Persistence_isNew')->will($this->returnValue(FALSE));
+
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('dummy'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $formViewHelper->injectPersistenceManager($mockPersistenceManager);
+
+               // TODO mock arguments
+               $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'foo', 'value' => $object, 'property' => NULL));
+               $formViewHelper->_set('arguments', $arguments);
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
+
+               $this->assertSame('foo[uid]', $formViewHelper->_call('getName'));
+               $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getNameBuildsNameFromFieldNamePrefixFormNameAndPropertyIfInObjectAccessorMode() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue('myFormName'));
+               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
+
+                       // TODO mock arguments
+               $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
+               $formViewHelper->_set('arguments', $arguments);
+               $expected = 'formPrefix[myFormName][bla]';
+               $actual = $formViewHelper->_call('getName');
+               $this->assertSame($expected, $actual);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormNameIsSpecified() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue(NULL));
+               $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
+
+                       // TODO mock arguments
+               $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
+               $formViewHelper->_set('arguments', $arguments);
+               $expected = 'formPrefix[bla]';
+               $actual = $formViewHelper->_call('getName');
+               $this->assertSame($expected, $actual);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
+               $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
+
+                       // TODO mock arguments
+               $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
+               $formViewHelper->_set('arguments', $arguments);
+               $expected = 'formPrefix[fieldName]';
+               $actual = $formViewHelper->_call('getName');
+               $this->assertSame($expected, $actual);
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getValueBuildsValueFromPropertyAndFormObjectIfInObjectAccessorMode() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+
+               $className = 'test_' . uniqid();
+               $mockObject = eval('
+                       class ' . $className . ' {
+                               public function getSomething() {
+                                       return "MyString";
+                               }
+                       }
+                       return new ' . $className . ';
+               ');
+
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
+               $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockObject));
+               $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue(TRUE));
+
+               // TODO mock arguments
+               $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'something'));
+               $formViewHelper->_set('arguments', $arguments);
+               $expected = 'MyString';
+               $actual = $formViewHelper->_call('getValue');
+               $this->assertSame($expected, $actual);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getValueReturnsNullIfNotInObjectAccessorModeAndValueArgumentIsNoSet() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
+
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(FALSE));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $this->assertNull($formViewHelper->_call('getValue'));
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getValueReturnsValueArgumentIfSpecified() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
+
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(TRUE));
+               $mockArguments->expects($this->any())->method('offsetGet')->with('value')->will($this->returnValue('someValue'));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $this->assertEquals('someValue', $formViewHelper->_call('getValue'));
+       }
+
+       /**
+        * @test
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('dummy'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+
+               $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue(TRUE));
+
+               $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'bla')));
+               $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
+
+               $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => NULL)));
+               $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
+       }
+
+       /**
+        * @test
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function getErrorsForPropertyReturnsErrorsFromRequestIfPropertyIsSet() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('isObjectAccessorMode'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $formViewHelper->expects($this->once())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->once())->method('offsetGet')->with('property')->will($this->returnValue('bar'));
+               $formViewHelper->_set('arguments', $mockArguments);
+               $this->viewHelperVariableContainer->expects($this->any())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formName')->will($this->returnValue('foo'));
+
+               $mockArgumentError = $this->getMock('Tx_Extbase_MVC_Controller_ArgumentError', array(), array('foo'));
+               $mockArgumentError->expects($this->once())->method('getPropertyName')->will($this->returnValue('foo'));
+               $mockPropertyError = $this->getMock('Tx_Extbase_Validation_PropertyError', array(), array('bar'));
+               $mockPropertyError->expects($this->once())->method('getPropertyName')->will($this->returnValue('bar'));
+               $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
+               $mockPropertyError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockError)));
+               $mockArgumentError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockPropertyError)));
+               $this->request->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockArgumentError)));
+
+               $errors = $formViewHelper->_call('getErrorsForProperty');
+               $this->assertEquals(array($mockError), $errors);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function getErrorsForPropertyReturnsEmptyArrayIfPropertyIsNotSet() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->once())->method('hasArgument')->with('property')->will($this->returnValue(FALSE));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $errors = $formViewHelper->_call('getErrorsForProperty');
+               $this->assertEquals(array(), $errors);
+       }
+
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function setErrorClassAttributeDoesNotSetClassAttributeIfNoErrorOccured() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->once())->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $this->tagBuilder->expects($this->never())->method('addAttribute');
+
+               $formViewHelper->_call('setErrorClassAttribute');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function setErrorClassAttributeSetsErrorClassIfAnErrorOccured() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
+               $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
+               $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
+
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'error');
+
+               $formViewHelper->_call('setErrorClassAttribute');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function setErrorClassAttributeAppendsErrorClassToExistingClassesIfAnErrorOccured() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
+               $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
+               $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
+               $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
+
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes error');
+
+               $formViewHelper->_call('setErrorClassAttribute');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function setErrorClassAttributeSetsCustomErrorClassIfAnErrorOccured() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
+               $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
+               $mockArguments->expects($this->at(2))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
+               $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
+
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'custom-error-class');
+
+               $formViewHelper->_call('setErrorClassAttribute');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function setErrorClassAttributeAppendsCustomErrorClassIfAnErrorOccured() {
+               $formViewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper'), array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
+               $this->injectDependenciesIntoViewHelper($formViewHelper);
+               $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+               $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
+               $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
+               $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
+               $mockArguments->expects($this->at(3))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
+               $formViewHelper->_set('arguments', $mockArguments);
+
+               $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
+               $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
+
+               $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes custom-error-class');
+
+               $formViewHelper->_call('setErrorClassAttribute');
+       }
+}
+
+?>
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/Form/HiddenViewHelperTest_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/Form/HiddenViewHelperTest_testcase.php
new file mode 100644 (file)
index 0000000..b8ab60a
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * 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.                                       *
+ *                                                                        */
+
+require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
+
+/**
+ * Test for the "Hidden" Form view helper
+ *
+ * @version $Id$
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
+ */
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+class Tx_Fluid_ViewHelpers_Form_HiddenViewHelperTest_testcase extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
+
+       /**
+        * var Tx_Fluid_ViewHelpers_Form_HiddenViewHelper
+        */
+       protected $viewHelper;
+
+       public function setUp() {
+               parent::setUp();
+               $this->viewHelper = $this->getMock($this->buildAccessibleProxy('Tx_Fluid_ViewHelpers_Form_HiddenViewHelper'), array('setErrorClassAttribute', 'getName', 'getValue'));
+               $this->injectDependenciesIntoViewHelper($this->viewHelper);
+               $this->viewHelper->initializeArguments();
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderCorrectlySetsTagNameAndDefaultAttributes() {
+               $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute'));
+               $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
+               $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'hidden');
+               $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
+               $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
+
+               $this->viewHelper->expects($this->once())->method('getName')->will($this->returnValue('foo'));
+               $this->viewHelper->expects($this->once())->method('getValue')->will($this->returnValue('bar'));
+               $this->viewHelper->injectTagBuilder($mockTagBuilder);
+
+               $this->viewHelper->initialize();
+               $this->viewHelper->render();
+       }
+}
+
+?>
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/ViewHelperBaseTestcase_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/ViewHelperBaseTestcase_testcase.php
new file mode 100644 (file)
index 0000000..1b4a76c
--- /dev/null
@@ -0,0 +1,95 @@
+<?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!                         *
+ *                                                                        */
+
+/**
+ * @version $Id$
+ */
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+abstract class Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase_testcase extends Tx_Extbase_Base_testcase {
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer
+        */
+       protected $viewHelperVariableContainer;
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_TemplateVariableContainer
+        */
+       protected $templateVariableContainer;
+
+       /**
+        * @var Tx_Fluid_MVC_Web_Routing_URIBuilder
+        */
+       protected $uriBuilder;
+
+       /**
+        * @var \Tx_Extbase_MVC_Controller_ControllerContext
+        */
+       protected $controllerContext;
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_TagBuilder
+        */
+       protected $tagBuilder;
+
+       /**
+        * @var Tx_Fluid_Core_ViewHelper_Arguments
+        */
+       protected $arguments;
+
+       /**
+        * @var \Tx_Extbase_MVC_Request
+        */
+       protected $request;
+
+       /**
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       public function setUp() {
+               $this->viewHelperVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
+               $this->templateVariableContainer = $this->getMock('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer');
+               $this->uriBuilder = $this->getMock('Tx_Fluid_MVC_Web_Routing_URIBuilder');
+               $this->request = $this->getMock('Tx_Extbase_MVC_Request');
+               $this->controllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext');
+               $this->controllerContext->expects($this->any())->method('getURIBuilder')->will($this->returnValue($this->uriBuilder));
+               $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
+               $this->tagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder');
+               $this->arguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
+       }
+
+       /**
+        * @param Tx_Fluid_Core_ViewHelper_AbstractViewHelper $viewHelper
+        * @return void
+        * @author Sebastian Kurfürst <sebastian@typo3.org>
+        */
+       protected function injectDependenciesIntoViewHelper(Tx_Fluid_Core_ViewHelper_AbstractViewHelper $viewHelper) {
+               $viewHelper->setViewHelperVariableContainer($this->viewHelperVariableContainer);
+               $viewHelper->setTemplateVariableContainer($this->templateVariableContainer);
+               $viewHelper->setControllerContext($this->controllerContext);
+               $viewHelper->setArguments($this->arguments);
+               if ($viewHelper instanceof Tx_Fluid_Core_ViewHelper_TagBasedViewHelper) {
+                       $viewHelper->injectTagBuilder($this->tagBuilder);
+               }
+       }
+}
+?>