TYPO3 API  SVNRelease
AbstractFormFieldViewHelperTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 require_once(dirname(__FILE__) . '/../ViewHelperBaseTestcase.php');
00024 
00025 /**
00026  * Test for the Abstract Form view helper
00027  *
00028  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00029  */
00030 class Tx_Fluid_Tests_Unit_ViewHelpers_Form_AbstractFormFieldViewHelperTest extends Tx_Fluid_ViewHelpers_ViewHelperBaseTestcase {
00031 
00032     /**
00033      * @test
00034      * @author Robert Lemke <robert@typo3.org>
00035      * @author Bastian Waidelich <bastian@typo3.org>
00036      */
00037     public function ifAnAttributeValueIsAnObjectMaintainedByThePersistenceManagerItIsConvertedToAUID() { $this->markTestIncomplete("Works differently in v4.");
00038         $mockPersistenceManager = $this->getMock('Tx_Extbase_Persistence_ManagerInterface');
00039         $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValue('6f487e40-4483-11de-8a39-0800200c9a66'));
00040 
00041         $className = 'Object' . uniqid();
00042         $fullClassName = 'Tx_Fluid_ViewHelpers_Form_' . $className;
00043         eval('class ' . $className . ' implements \\F3\\FLOW3\\Persistence\\Aspect\\PersistenceMagicInterface {
00044             public function FLOW3_Persistence_isClone() { return FALSE; }
00045             public function FLOW3_AOP_Proxy_getProperty($name) {}
00046             public function FLOW3_AOP_Proxy_getProxyTargetClassName() {}
00047             public function __clone() {}
00048         }');
00049         $object = $this->getMock($fullClassName);
00050         $object->expects($this->any())->method('FLOW3_Persistence_isNew')->will($this->returnValue(FALSE));
00051 
00052         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
00053         $this->injectDependenciesIntoViewHelper($formViewHelper);
00054         $formViewHelper->injectPersistenceManager($mockPersistenceManager);
00055 
00056         // TODO mock arguments
00057         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'foo', 'value' => $object, 'property' => NULL));
00058         $formViewHelper->_set('arguments', $arguments);
00059         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
00060 
00061         $this->assertSame('foo[__identity]', $formViewHelper->_call('getName'));
00062         $this->assertSame('6f487e40-4483-11de-8a39-0800200c9a66', $formViewHelper->_call('getValue'));
00063     }
00064 
00065     /**
00066      * @test
00067      * @author Sebastian Kurfürst <sebastian@typo3.org>
00068      * @author Bastian Waidelich <bastian@typo3.org>
00069      */
00070     public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndPropertyIfInObjectAccessorMode() {
00071         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00072         $this->injectDependenciesIntoViewHelper($formViewHelper);
00073 
00074         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
00075         $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
00076         $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
00077         $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
00078 
00079             // TODO mock arguments
00080         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
00081         $formViewHelper->_set('arguments', $arguments);
00082         $expected = 'formPrefix[myObjectName][bla]';
00083         $actual = $formViewHelper->_call('getName');
00084         $this->assertSame($expected, $actual);
00085     }
00086 
00087     /**
00088      * @test
00089      * @author Sebastian Kurfürst <sebastian@typo3.org>
00090      * @author Bastian Waidelich <bastian@typo3.org>
00091      */
00092     public function getNameBuildsNameFromFieldNamePrefixFormObjectNameAndHierarchicalPropertyIfInObjectAccessorMode() {
00093         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00094         $this->injectDependenciesIntoViewHelper($formViewHelper);
00095 
00096         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
00097         $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('myObjectName'));
00098         $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
00099         $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
00100 
00101             // TODO mock arguments
00102         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla.blubb'));
00103         $formViewHelper->_set('arguments', $arguments);
00104         $expected = 'formPrefix[myObjectName][bla][blubb]';
00105         $actual = $formViewHelper->_call('getName');
00106         $this->assertSame($expected, $actual);
00107     }
00108 
00109     /**
00110      * @test
00111      * @author Bastian Waidelich <bastian@typo3.org>
00112      */
00113     public function getNameBuildsNameFromFieldNamePrefixAndPropertyIfInObjectAccessorModeAndNoFormObjectNameIsSpecified() {
00114         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00115         $this->injectDependenciesIntoViewHelper($formViewHelper);
00116 
00117         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
00118         $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(NULL));
00119         $this->viewHelperVariableContainer->expects($this->at(1))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
00120         $this->viewHelperVariableContainer->expects($this->at(2))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
00121 
00122             // TODO mock arguments
00123         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
00124         $formViewHelper->_set('arguments', $arguments);
00125         $expected = 'formPrefix[bla]';
00126         $actual = $formViewHelper->_call('getName');
00127         $this->assertSame($expected, $actual);
00128     }
00129 
00130     /**
00131      * @test
00132      * @author Bastian Waidelich <bastian@typo3.org>
00133      */
00134     public function getNameBuildsNameFromFieldNamePrefixAndFieldNameIfNotInObjectAccessorMode() {
00135         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00136         $this->injectDependenciesIntoViewHelper($formViewHelper);
00137 
00138         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
00139         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue(TRUE));
00140         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'fieldNamePrefix')->will($this->returnValue('formPrefix'));
00141 
00142             // TODO mock arguments
00143         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => 'fieldName', 'value' => 'fieldValue', 'property' => 'bla'));
00144         $formViewHelper->_set('arguments', $arguments);
00145         $expected = 'formPrefix[fieldName]';
00146         $actual = $formViewHelper->_call('getName');
00147         $this->assertSame($expected, $actual);
00148     }
00149 
00150     /**
00151      * @test
00152      * @author Sebastian Kurfürst <sebastian@typo3.org>
00153      * @author Bastian Waidelich <bastian@typo3.org>
00154      */
00155     public function getValueBuildsValueFromPropertyAndFormObjectIfInObjectAccessorMode() {
00156         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode', 'addAdditionalIdentityPropertiesIfNeeded'), array(), '', FALSE);
00157         $this->injectDependenciesIntoViewHelper($formViewHelper);
00158 
00159         $className = 'test_' . uniqid();
00160         $mockObject = eval('
00161             class ' . $className . ' {
00162                 public function getSomething() {
00163                     return "MyString";
00164                 }
00165                 public function getValue() {
00166                     return new ' . $className . ';
00167                 }
00168             }
00169             return new ' . $className . ';
00170         ');
00171 
00172         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
00173         $formViewHelper->expects($this->once())->method('addAdditionalIdentityPropertiesIfNeeded');
00174         $this->viewHelperVariableContainer->expects($this->once())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockObject));
00175         $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue(TRUE));
00176 
00177         // TODO mock arguments
00178         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'value.something'));
00179         $formViewHelper->_set('arguments', $arguments);
00180         $expected = 'MyString';
00181         $actual = $formViewHelper->_call('getValue');
00182         $this->assertSame($expected, $actual);
00183     }
00184 
00185     /**
00186      * @test
00187      * @author Bastian Waidelich <bastian@typo3.org>
00188      */
00189     public function getValueReturnsNullIfNotInObjectAccessorModeAndValueArgumentIsNoSet() {
00190         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00191         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
00192 
00193         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00194         $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(FALSE));
00195         $formViewHelper->_set('arguments', $mockArguments);
00196 
00197         $this->assertNull($formViewHelper->_call('getValue'));
00198     }
00199 
00200     /**
00201      * @test
00202      * @author Bastian Waidelich <bastian@typo3.org>
00203      */
00204     public function getValueReturnsValueArgumentIfSpecified() {
00205         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00206         $formViewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(FALSE));
00207 
00208         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00209         $mockArguments->expects($this->any())->method('hasArgument')->with('value')->will($this->returnValue(TRUE));
00210         $mockArguments->expects($this->any())->method('offsetGet')->with('value')->will($this->returnValue('someValue'));
00211         $formViewHelper->_set('arguments', $mockArguments);
00212 
00213         $this->assertEquals('someValue', $formViewHelper->_call('getValue'));
00214     }
00215 
00216     /**
00217      * @test
00218      * @author Sebastian Kurfürst <sebastian@typo3.org>
00219      */
00220     public function isObjectAccessorModeReturnsTrueIfPropertyIsSetAndFormObjectIsGiven() {
00221         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('dummy'), array(), '', FALSE);
00222         $this->injectDependenciesIntoViewHelper($formViewHelper);
00223 
00224         $this->viewHelperVariableContainer->expects($this->once())->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue(TRUE));
00225 
00226         $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => 'bla')));
00227         $this->assertTrue($formViewHelper->_call('isObjectAccessorMode'));
00228 
00229         $formViewHelper->_set('arguments', new Tx_Fluid_Core_ViewHelper_Arguments(array('name' => NULL, 'value' => NULL, 'property' => NULL)));
00230         $this->assertFalse($formViewHelper->_call('isObjectAccessorMode'));
00231     }
00232 
00233     /**
00234      * @test
00235      * @author Christopher Hlubek <hlubek@networkteam.com>
00236      */
00237     public function getErrorsForPropertyReturnsErrorsFromRequestIfPropertyIsSet() {
00238         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('isObjectAccessorMode'), array(), '', FALSE);
00239         $this->injectDependenciesIntoViewHelper($formViewHelper);
00240         $formViewHelper->expects($this->once())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
00241         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00242         $mockArguments->expects($this->once())->method('offsetGet')->with('property')->will($this->returnValue('bar'));
00243         $formViewHelper->_set('arguments', $mockArguments);
00244         $this->viewHelperVariableContainer->expects($this->any())->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue('foo'));
00245 
00246         $mockArgumentError = $this->getMock('Tx_Extbase_MVC_Controller_ArgumentError', array(), array('foo'));
00247         $mockArgumentError->expects($this->once())->method('getPropertyName')->will($this->returnValue('foo'));
00248         $mockPropertyError = $this->getMock('Tx_Extbase_Validation_PropertyError', array(), array('bar'));
00249         $mockPropertyError->expects($this->once())->method('getPropertyName')->will($this->returnValue('bar'));
00250         $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
00251         $mockPropertyError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockError)));
00252         $mockArgumentError->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockPropertyError)));
00253         $this->request->expects($this->once())->method('getErrors')->will($this->returnValue(array($mockArgumentError)));
00254 
00255         $errors = $formViewHelper->_call('getErrorsForProperty');
00256         $this->assertEquals(array($mockError), $errors);
00257     }
00258 
00259     /**
00260      * @test
00261      * @author Bastian Waidelich <bastian@typo3.org>
00262      */
00263     public function getErrorsForPropertyReturnsEmptyArrayIfPropertyIsNotSet() {
00264         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument'), array(), '', FALSE);
00265         $this->injectDependenciesIntoViewHelper($formViewHelper);
00266         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00267         $mockArguments->expects($this->once())->method('hasArgument')->with('property')->will($this->returnValue(FALSE));
00268         $formViewHelper->_set('arguments', $mockArguments);
00269 
00270         $errors = $formViewHelper->_call('getErrorsForProperty');
00271         $this->assertEquals(array(), $errors);
00272     }
00273 
00274 
00275     /**
00276      * @test
00277      * @author Bastian Waidelich <bastian@typo3.org>
00278      */
00279     public function setErrorClassAttributeDoesNotSetClassAttributeIfNoErrorOccured() {
00280         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
00281         $this->injectDependenciesIntoViewHelper($formViewHelper);
00282         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00283         $mockArguments->expects($this->once())->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
00284         $formViewHelper->_set('arguments', $mockArguments);
00285 
00286         $this->tagBuilder->expects($this->never())->method('addAttribute');
00287 
00288         $formViewHelper->_call('setErrorClassAttribute');
00289     }
00290 
00291     /**
00292      * @test
00293      * @author Bastian Waidelich <bastian@typo3.org>
00294      */
00295     public function setErrorClassAttributeSetsErrorClassIfAnErrorOccured() {
00296         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
00297         $this->injectDependenciesIntoViewHelper($formViewHelper);
00298         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00299         $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
00300         $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
00301         $formViewHelper->_set('arguments', $mockArguments);
00302 
00303         $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
00304         $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
00305 
00306         $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'error');
00307 
00308         $formViewHelper->_call('setErrorClassAttribute');
00309     }
00310 
00311     /**
00312      * @test
00313      * @author Bastian Waidelich <bastian@typo3.org>
00314      */
00315     public function setErrorClassAttributeAppendsErrorClassToExistingClassesIfAnErrorOccured() {
00316         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
00317         $this->injectDependenciesIntoViewHelper($formViewHelper);
00318         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00319         $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
00320         $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
00321         $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(FALSE));
00322         $formViewHelper->_set('arguments', $mockArguments);
00323 
00324         $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
00325         $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
00326 
00327         $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes error');
00328 
00329         $formViewHelper->_call('setErrorClassAttribute');
00330     }
00331 
00332     /**
00333      * @test
00334      * @author Bastian Waidelich <bastian@typo3.org>
00335      */
00336     public function setErrorClassAttributeSetsCustomErrorClassIfAnErrorOccured() {
00337         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
00338         $this->injectDependenciesIntoViewHelper($formViewHelper);
00339         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00340         $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(FALSE));
00341         $mockArguments->expects($this->at(1))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
00342         $mockArguments->expects($this->at(2))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
00343         $formViewHelper->_set('arguments', $mockArguments);
00344 
00345         $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
00346         $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
00347 
00348         $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'custom-error-class');
00349 
00350         $formViewHelper->_call('setErrorClassAttribute');
00351     }
00352 
00353     /**
00354      * @test
00355      * @author Bastian Waidelich <bastian@typo3.org>
00356      */
00357     public function setErrorClassAttributeAppendsCustomErrorClassIfAnErrorOccured() {
00358         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('hasArgument', 'getErrorsForProperty'), array(), '', FALSE);
00359         $this->injectDependenciesIntoViewHelper($formViewHelper);
00360         $mockArguments = $this->getMock('Tx_Fluid_Core_ViewHelper_Arguments', array(), array(), '', FALSE);
00361         $mockArguments->expects($this->at(0))->method('hasArgument')->with('class')->will($this->returnValue(TRUE));
00362         $mockArguments->expects($this->at(1))->method('offsetGet')->with('class')->will($this->returnValue('default classes'));
00363         $mockArguments->expects($this->at(2))->method('hasArgument')->with('errorClass')->will($this->returnValue(TRUE));
00364         $mockArguments->expects($this->at(3))->method('offsetGet')->with('errorClass')->will($this->returnValue('custom-error-class'));
00365         $formViewHelper->_set('arguments', $mockArguments);
00366 
00367         $mockError = $this->getMock('Tx_Extbase_Error_Error', array(), array(), '', FALSE);
00368         $formViewHelper->expects($this->once())->method('getErrorsForProperty')->will($this->returnValue(array($mockError)));
00369 
00370         $this->tagBuilder->expects($this->once())->method('addAttribute')->with('class', 'default classes custom-error-class');
00371 
00372         $formViewHelper->_call('setErrorClassAttribute');
00373     }
00374 
00375     /**
00376      * @test
00377      * @author Sebastian Kurfürst <sebastian@typo3.org>
00378      */
00379     public function addAdditionalIdentityPropertiesIfNeededDoesNotCreateAnythingIfPropertyIsWithoutDot() {
00380         $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
00381         $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
00382         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => 'simple'));
00383         $formFieldViewHelper->expects($this->any())->method('renderHiddenIdentityField')->will($this->throwException(new Exception('Should not be executed!!!')));
00384         $formFieldViewHelper->_set('arguments', $arguments);
00385         $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
00386     }
00387 
00388     /**
00389      * @test
00390      * @author Sebastian Kurfürst <sebastian@typo3.org>
00391      */
00392     public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParameters() {
00393         $className = 'test_' . uniqid();
00394         $mockFormObject = eval('
00395             class ' . $className . ' {
00396                 public function getSomething() {
00397                     return "MyString";
00398                 }
00399                 public function getValue() {
00400                     return new ' . $className . ';
00401                 }
00402             }
00403             return new ' . $className . ';
00404         ');
00405         $property = 'value.something';
00406         $objectName = 'myObject';
00407         $expectedProperty = 'myObject[value]';
00408 
00409         $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
00410         $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
00411         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
00412         $formFieldViewHelper->_set('arguments', $arguments);
00413         $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
00414         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
00415 
00416         $formFieldViewHelper->expects($this->once())->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty);
00417 
00418         $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
00419     }
00420 
00421     /**
00422      * @test
00423      * @author Sebastian Kurfürst <sebastian@typo3.org>
00424      */
00425     public function addAdditionalIdentityPropertiesIfNeededCallsRenderIdentityFieldWithTheRightParametersWithMoreHierarchyLevels() {
00426         $className = 'test_' . uniqid();
00427         $mockFormObject = eval('
00428             class ' . $className . ' {
00429                 public function getSomething() {
00430                     return "MyString";
00431                 }
00432                 public function getValue() {
00433                     return new ' . $className . ';
00434                 }
00435             }
00436             return new ' . $className . ';
00437         ');
00438         $property = 'value.value.something';
00439         $objectName = 'myObject';
00440         $expectedProperty1 = 'myObject[value]';
00441         $expectedProperty2 = 'myObject[value][value]';
00442 
00443         $formFieldViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('renderHiddenIdentityField'), array(), '', FALSE);
00444         $this->injectDependenciesIntoViewHelper($formFieldViewHelper);
00445         $arguments = new Tx_Fluid_Core_ViewHelper_Arguments(array('property' => $property));
00446         $formFieldViewHelper->_set('arguments', $arguments);
00447         $this->viewHelperVariableContainer->expects($this->at(0))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObject')->will($this->returnValue($mockFormObject));
00448         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'formObjectName')->will($this->returnValue($objectName));
00449 
00450         $formFieldViewHelper->expects($this->at(0))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty1);
00451         $formFieldViewHelper->expects($this->at(1))->method('renderHiddenIdentityField')->with($mockFormObject, $expectedProperty2);
00452 
00453         $formFieldViewHelper->_call('addAdditionalIdentityPropertiesIfNeeded');
00454     }
00455 
00456     /**
00457      * @test
00458      * @author Bastian Waidelich <bastian@typo3.org>
00459      */
00460     public function renderHiddenFieldForEmptyValueRendersHiddenFieldIfItHasNotBeenRenderedBefore() {
00461         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
00462         $this->injectDependenciesIntoViewHelper($formViewHelper);
00463 
00464         $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
00465         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
00466         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
00467 
00468         $expected = '<input type="hidden" name="SomeFieldName" value="" />';
00469         $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
00470         $this->assertEquals($expected, $actual);
00471     }
00472 
00473     /**
00474      * @test
00475      * @author Bastian Waidelich <bastian@typo3.org>
00476      */
00477     public function renderHiddenFieldForEmptyValueAddsHiddenFieldNameToVariableContainerIfItHasBeenRendered() {
00478         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
00479         $this->injectDependenciesIntoViewHelper($formViewHelper);
00480 
00481         $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('NewFieldName'));
00482         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
00483         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('OldFieldName')));
00484         $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('OldFieldName', 'NewFieldName'));
00485 
00486         $formViewHelper->_call('renderHiddenFieldForEmptyValue');
00487     }
00488 
00489     /**
00490      * @test
00491      * @author Bastian Waidelich <bastian@typo3.org>
00492      */
00493     public function renderHiddenFieldForEmptyValueDoesNotRenderHiddenFieldIfItHasBeenRenderedBefore() {
00494         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
00495         $this->injectDependenciesIntoViewHelper($formViewHelper);
00496 
00497         $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName'));
00498         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
00499         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array('SomeFieldName')));
00500         $this->viewHelperVariableContainer->expects($this->never())->method('addOrUpdate');
00501 
00502         $expected = '';
00503         $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
00504         $this->assertEquals($expected, $actual);
00505     }
00506 
00507     /**
00508      * @test
00509      * @author Bastian Waidelich <bastian@typo3.org>
00510      */
00511     public function renderHiddenFieldForEmptyValueRemovesEmptySquareBracketsFromHiddenFieldName() {
00512         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
00513         $this->injectDependenciesIntoViewHelper($formViewHelper);
00514 
00515         $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][]'));
00516         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
00517         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
00518         $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets]'));
00519 
00520         $expected = '<input type="hidden" name="SomeFieldName[WithBrackets]" value="" />';
00521         $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
00522         $this->assertEquals($expected, $actual);
00523     }
00524 
00525     /**
00526      * @test
00527      * @author Bastian Waidelich <bastian@typo3.org>
00528      */
00529     public function renderHiddenFieldForEmptyValueDoesNotRemoveNonEmptySquareBracketsFromHiddenFieldName() {
00530         $formViewHelper = $this->getAccessibleMock('Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper', array('getName'), array(), '', FALSE);
00531         $this->injectDependenciesIntoViewHelper($formViewHelper);
00532 
00533         $formViewHelper->expects($this->any())->method('getName')->will($this->returnValue('SomeFieldName[WithBrackets][foo]'));
00534         $this->viewHelperVariableContainer->expects($this->at(0))->method('exists')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(TRUE));
00535         $this->viewHelperVariableContainer->expects($this->at(1))->method('get')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields')->will($this->returnValue(array()));
00536         $this->viewHelperVariableContainer->expects($this->at(2))->method('addOrUpdate')->with('Tx_Fluid_ViewHelpers_FormViewHelper', 'renderedHiddenFields', array('SomeFieldName[WithBrackets][foo]'));
00537 
00538         $expected = '<input type="hidden" name="SomeFieldName[WithBrackets][foo]" value="" />';
00539         $actual = $formViewHelper->_call('renderHiddenFieldForEmptyValue');
00540         $this->assertEquals($expected, $actual);
00541     }
00542 }
00543 
00544 ?>