7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 if (!defined('PHPUnit_MAIN_METHOD')) {
24 define('PHPUnit_MAIN_METHOD', 'Zend_Form_FormTest::main');
27 require_once dirname(__FILE__
) . '/../../TestHelper.php';
29 // error_reporting(E_ALL);
31 require_once 'Zend/Form.php';
33 require_once 'Zend/Config.php';
34 require_once 'Zend/Controller/Action/HelperBroker.php';
35 require_once 'Zend/Form/Decorator/Form.php';
36 require_once 'Zend/Form/DisplayGroup.php';
37 require_once 'Zend/Form/Element.php';
38 require_once 'Zend/Form/Element/Text.php';
39 require_once 'Zend/Form/Element/File.php';
40 require_once 'Zend/Form/SubForm.php';
41 require_once 'Zend/Loader/PluginLoader.php';
42 require_once 'Zend/Registry.php';
43 require_once 'Zend/Translate.php';
44 require_once 'Zend/View.php';
49 * @subpackage UnitTests
50 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
51 * @license http://framework.zend.com/license/new-bsd New BSD License
54 class Zend_Form_FormTest
extends PHPUnit_Framework_TestCase
56 public static function main()
58 $suite = new PHPUnit_Framework_TestSuite('Zend_Form_FormTest');
59 $result = PHPUnit_TextUI_TestRunner
::run($suite);
62 public function clearRegistry()
64 if (Zend_Registry
::isRegistered('Zend_Translate')) {
65 $registry = Zend_Registry
::getInstance();
66 unset($registry['Zend_Translate']);
70 public function setUp()
72 $this->clearRegistry();
73 Zend_Form
::setDefaultTranslator(null);
75 if (isset($this->error
)) {
79 Zend_Controller_Action_HelperBroker
::resetHelpers();
80 $this->form
= new Zend_Form();
83 public function tearDown()
85 $this->clearRegistry();
88 public function testZendFormImplementsZendValidateInterface()
90 $this->assertTrue($this->form
instanceof Zend_Validate_Interface
);
95 public function getOptions()
99 'class' => 'someform',
100 'action' => '/foo/bar',
106 public function testCanSetObjectStateViaSetOptions()
108 $options = $this->getOptions();
109 $this->form
->setOptions($options);
110 $this->assertEquals('foo', $this->form
->getName());
111 $this->assertEquals('someform', $this->form
->getAttrib('class'));
112 $this->assertEquals('/foo/bar', $this->form
->getAction());
113 $this->assertEquals('put', $this->form
->getMethod());
116 public function testCanSetObjectStateByPassingOptionsToConstructor()
118 $options = $this->getOptions();
119 $form = new Zend_Form($options);
120 $this->assertEquals('foo', $form->getName());
121 $this->assertEquals('someform', $form->getAttrib('class'));
122 $this->assertEquals('/foo/bar', $form->getAction());
123 $this->assertEquals('put', $form->getMethod());
126 public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
128 $options = $this->getOptions();
129 $config = new Zend_Config($options);
130 $options['config'] = $config;
131 $options['options'] = $config->toArray();
132 $this->form
->setOptions($options);
135 public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNonObjectPassed()
137 $options = $this->getOptions();
138 $options['pluginLoader'] = true;
139 $options['subForms'] = true;
140 $options['view'] = true;
141 $options['translator'] = true;
142 $options['default'] = true;
143 $options['attrib'] = true;
144 $this->form
->setOptions($options);
147 public function testSetOptionsWithAttribsDoesNotOverwriteActionOrMethodOrName()
149 $attribs = $this->getOptions();
150 unset($attribs['action'], $attribs['method']);
153 'action' => '/bar/baz',
155 'attribs' => $attribs,
157 $form = new Zend_Form($options);
158 $this->assertEquals($options['name'], $form->getName());
159 $this->assertEquals($options['action'], $form->getAction());
160 $this->assertEquals(strtolower($options['method']), strtolower($form->getMethod()));
163 public function getElementOptions()
167 array('text', 'bar', array('class' => 'foobar')),
169 'options' => array('class' => 'barbaz'),
174 'options' => array('class' => 'bazbat'),
179 array('class' => 'lolcat'),
185 public function testSetOptionsSetsElements()
187 $options = $this->getOptions();
188 $options['elements'] = $this->getElementOptions();
189 $this->form
->setOptions($options);
191 $this->assertTrue(isset($this->form
->foo
));
192 $this->assertTrue($this->form
->foo
instanceof Zend_Form_Element_Text
);
194 $this->assertTrue(isset($this->form
->bar
));
195 $this->assertTrue($this->form
->bar
instanceof Zend_Form_Element_Text
);
196 $this->assertEquals('foobar', $this->form
->bar
->class);
198 $this->assertTrue(isset($this->form
->baz
));
199 $this->assertTrue($this->form
->baz
instanceof Zend_Form_Element_Text
);
200 $this->assertEquals('barbaz', $this->form
->baz
->class);
202 $this->assertTrue(isset($this->form
->bat
));
203 $this->assertTrue($this->form
->bat
instanceof Zend_Form_Element_Text
);
204 $this->assertEquals('bazbat', $this->form
->bat
->class);
206 $this->assertTrue(isset($this->form
->lol
));
207 $this->assertTrue($this->form
->lol
instanceof Zend_Form_Element_Text
);
208 $this->assertEquals('lolcat', $this->form
->lol
->class);
211 public function testSetOptionsSetsDefaultValues()
213 $options = $this->getOptions();
214 $options['defaults'] = array(
218 $options['elements'] = $this->getElementOptions();
219 $this->form
->setOptions($options);
221 $this->assertEquals('barvalue', $this->form
->bar
->getValue());
222 $this->assertEquals('batvalue', $this->form
->bat
->getValue());
225 public function testSetOptionsSetsArrayOfStringDecorators()
227 $this->_checkZf2794();
229 $options = $this->getOptions();
230 $options['decorators'] = array('label', 'errors');
231 $this->form
->setOptions($options);
232 $this->assertFalse($this->form
->getDecorator('form'));
234 $decorator = $this->form
->getDecorator('label');
235 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label
);
236 $decorator = $this->form
->getDecorator('errors');
237 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors
);
240 public function testSetOptionsSetsArrayOfArrayDecorators()
242 $this->_checkZf2794();
244 $options = $this->getOptions();
245 $options['decorators'] = array(
246 array('label', array('id' => 'mylabel')),
247 array('errors', array('id' => 'errors')),
249 $this->form
->setOptions($options);
250 $this->assertFalse($this->form
->getDecorator('form'));
252 $decorator = $this->form
->getDecorator('label');
253 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label
);
254 $options = $decorator->getOptions();
255 $this->assertEquals('mylabel', $options['id']);
257 $decorator = $this->form
->getDecorator('errors');
258 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors
);
259 $options = $decorator->getOptions();
260 $this->assertEquals('errors', $options['id']);
263 public function testSetOptionsSetsArrayOfAssocArrayDecorators()
265 $this->_checkZf2794();
267 $options = $this->getOptions();
268 $options['decorators'] = array(
270 'options' => array('id' => 'mylabel'),
271 'decorator' => 'label',
274 'options' => array('id' => 'errors'),
275 'decorator' => 'errors',
278 $this->form
->setOptions($options);
279 $this->assertFalse($this->form
->getDecorator('form'));
281 $decorator = $this->form
->getDecorator('label');
282 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label
);
283 $options = $decorator->getOptions();
284 $this->assertEquals('mylabel', $options['id']);
286 $decorator = $this->form
->getDecorator('errors');
287 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors
);
288 $options = $decorator->getOptions();
289 $this->assertEquals('errors', $options['id']);
292 public function testSetOptionsSetsGlobalPrefixPaths()
294 $options = $this->getOptions();
295 $options['prefixPath'] = array(
296 'prefix' => 'Zend_Foo',
297 'path' => 'Zend/Foo/'
299 $this->form
->setOptions($options);
301 foreach (array('element', 'decorator') as $type) {
302 $loader = $this->form
->getPluginLoader($type);
303 $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
304 $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
305 $this->assertFalse(empty($paths));
306 $this->assertContains('Foo', $paths[0]);
310 public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
312 $options = $this->getOptions();
313 $options['prefixPath'] = array(
314 'element' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
316 $this->form
->setOptions($options);
318 $loader = $this->form
->getPluginLoader('element');
319 $paths = $loader->getPaths('Zend_Foo');
320 $this->assertTrue(is_array($paths));
321 $this->assertFalse(empty($paths));
322 $this->assertContains('Foo', $paths[0]);
325 public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
327 $options = $this->getOptions();
328 $options['prefixPath'] = array(
329 array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
331 $this->form
->setOptions($options);
333 $loader = $this->form
->getPluginLoader('decorator');
334 $paths = $loader->getPaths('Zend_Foo');
335 $this->assertTrue(is_array($paths));
336 $this->assertFalse(empty($paths));
337 $this->assertContains('Foo', $paths[0]);
340 public function testSetOptionsSetsDisplayGroups()
342 $options = $this->getOptions();
343 $options['displayGroups'] = array(
344 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
345 array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
348 'elements' => array('ghi', 'abc'),
349 'options' => array('order' => 15),
352 $options['elements'] = array(
362 $this->form
->setOptions($options);
364 $this->assertTrue(isset($this->form
->barbat
));
365 $elements = $this->form
->barbat
->getElements();
366 $expected = array('bar', 'bat');
367 $this->assertEquals($expected, array_keys($elements));
368 $this->assertEquals(20, $this->form
->barbat
->getOrder());
370 $this->assertTrue(isset($this->form
->foobaz
));
371 $elements = $this->form
->foobaz
->getElements();
372 $expected = array('foo', 'baz');
373 $this->assertEquals($expected, array_keys($elements));
374 $this->assertEquals(10, $this->form
->foobaz
->getOrder());
376 $this->assertTrue(isset($this->form
->ghiabc
));
377 $elements = $this->form
->ghiabc
->getElements();
378 $expected = array('ghi', 'abc');
379 $this->assertEquals($expected, array_keys($elements));
380 $this->assertEquals(15, $this->form
->ghiabc
->getOrder());
386 public function testDisplayGroupOrderInConfigShouldNotMatter()
388 require_once 'Zend/Config/Xml.php';
389 $config = new Zend_Config_Xml(dirname(__FILE__
) . '/_files/config/zf3250.xml', 'sitearea', true);
390 $form = new Zend_Form($config->test
);
391 // no assertions needed; throws error if order matters
397 public function testSetOptionsShouldCreateDisplayGroupsLast()
400 $options['displayGroups'] = array(
401 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
402 array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
405 'elements' => array('ghi', 'abc'),
406 'options' => array('order' => 15),
409 $options = array_merge($options, $this->getOptions());
410 $options['elements'] = array(
420 $this->form
= new Zend_Form($options);
422 $this->assertTrue(isset($this->form
->barbat
));
423 $elements = $this->form
->barbat
->getElements();
424 $expected = array('bar', 'bat');
425 $this->assertEquals($expected, array_keys($elements));
426 $this->assertEquals(20, $this->form
->barbat
->getOrder());
428 $this->assertTrue(isset($this->form
->foobaz
));
429 $elements = $this->form
->foobaz
->getElements();
430 $expected = array('foo', 'baz');
431 $this->assertEquals($expected, array_keys($elements));
432 $this->assertEquals(10, $this->form
->foobaz
->getOrder());
434 $this->assertTrue(isset($this->form
->ghiabc
));
435 $elements = $this->form
->ghiabc
->getElements();
436 $expected = array('ghi', 'abc');
437 $this->assertEquals($expected, array_keys($elements));
438 $this->assertEquals(15, $this->form
->ghiabc
->getOrder());
441 public function testSetConfigSetsObjectState()
443 $config = new Zend_Config($this->getOptions());
444 $this->form
->setConfig($config);
445 $this->assertEquals('foo', $this->form
->getName());
446 $this->assertEquals('someform', $this->form
->getAttrib('class'));
447 $this->assertEquals('/foo/bar', $this->form
->getAction());
448 $this->assertEquals('put', $this->form
->getMethod());
451 public function testCanSetObjectStateByPassingConfigObjectToConstructor()
453 $config = new Zend_Config($this->getOptions());
454 $form = new Zend_Form($config);
455 $this->assertEquals('foo', $form->getName());
456 $this->assertEquals('someform', $form->getAttrib('class'));
457 $this->assertEquals('/foo/bar', $form->getAction());
458 $this->assertEquals('put', $form->getMethod());
464 public function testAttribsArrayInitiallyEmpty()
466 $attribs = $this->form
->getAttribs();
467 $this->assertTrue(is_array($attribs));
468 $this->assertTrue(empty($attribs));
471 public function testRetrievingUndefinedAttribReturnsNull()
473 $this->assertNull($this->form
->getAttrib('foo'));
476 public function testCanAddAndRetrieveSingleAttribs()
478 $this->testRetrievingUndefinedAttribReturnsNull();
479 $this->form
->setAttrib('foo', 'bar');
480 $this->assertEquals('bar', $this->form
->getAttrib('foo'));
483 public function testCanAddAndRetrieveMultipleAttribs()
485 $this->form
->setAttrib('foo', 'bar');
486 $this->assertEquals('bar', $this->form
->getAttrib('foo'));
487 $this->form
->addAttribs(array(
492 $test = $this->form
->getAttribs();
499 $this->assertSame($attribs, $test);
502 public function testSetAttribsOverwritesExistingAttribs()
504 $this->testCanAddAndRetrieveMultipleAttribs();
505 $array = array('bogus' => 'value', 'not' => 'real');
506 $this->form
->setAttribs($array);
507 $this->assertSame($array, $this->form
->getAttribs());
510 public function testCanRemoveSingleAttrib()
512 $this->testCanAddAndRetrieveSingleAttribs();
513 $this->assertTrue($this->form
->removeAttrib('foo'));
514 $this->assertNull($this->form
->getAttrib('foo'));
517 public function testRemoveAttribReturnsFalseIfAttribDoesNotExist()
519 $this->assertFalse($this->form
->removeAttrib('foo'));
522 public function testCanClearAllAttribs()
524 $this->testCanAddAndRetrieveMultipleAttribs();
525 $this->form
->clearAttribs();
526 $attribs = $this->form
->getAttribs();
527 $this->assertTrue(is_array($attribs));
528 $this->assertTrue(empty($attribs));
531 public function testNameIsInitiallyNull()
533 $this->assertNull($this->form
->getName());
536 public function testCanSetName()
538 $this->testNameIsInitiallyNull();
539 $this->form
->setName('foo');
540 $this->assertEquals('foo', $this->form
->getName());
543 public function testZeroAsNameIsAllowed()
546 $this->form
->setName(0);
547 $this->assertEquals(0, $this->form
->getName());
548 } catch (Zend_Form_Exception
$e) {
549 $this->fail('Should allow zero as form name');
553 public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
555 $this->form
->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
556 $this->assertEquals('foobar', $this->form
->getName());
559 $this->form
->setName('%\^&*)\(%$#@!.}{;-,');
560 $this->fail('Empty names should raise exception');
561 } catch (Zend_Form_Exception
$e) {
562 $this->assertContains('Invalid name provided', $e->getMessage());
566 public function testActionDefaultsToEmptyString()
568 $this->assertSame('', $this->form
->getAction());
571 public function testCanSetAction()
573 $this->testActionDefaultsToEmptyString();
574 $this->form
->setAction('/foo/bar');
575 $this->assertEquals('/foo/bar', $this->form
->getAction());
578 public function testMethodDefaultsToPost()
580 $this->assertEquals('post', $this->form
->getMethod());
583 public function testCanSetMethod()
585 $this->testMethodDefaultsToPost();
586 $this->form
->setMethod('get');
587 $this->assertEquals('get', $this->form
->getMethod());
590 public function testMethodLimitedToGetPostPutAndDelete()
592 foreach (array('get', 'post', 'put', 'delete') as $method) {
593 $this->form
->setMethod($method);
594 $this->assertEquals($method, $this->form
->getMethod());
597 $this->form
->setMethod('bogus');
598 $this->fail('Invalid method type should throw exception');
599 } catch (Zend_Form_Exception
$e) {
600 $this->assertContains('invalid', $e->getMessage());
604 public function testEnctypeDefaultsToUrlEncoded()
606 $this->assertEquals(Zend_Form
::ENCTYPE_URLENCODED
, $this->form
->getEnctype());
609 public function testCanSetEnctype()
611 $this->testEnctypeDefaultsToUrlEncoded();
612 $this->form
->setEnctype(Zend_Form
::ENCTYPE_MULTIPART
);
613 $this->assertEquals(Zend_Form
::ENCTYPE_MULTIPART
, $this->form
->getEnctype());
616 public function testLegendInitiallyNull()
618 $this->assertNull($this->form
->getLegend());
621 public function testCanSetLegend()
623 $this->testLegendInitiallyNull();
624 $legend = "This is a legend";
625 $this->form
->setLegend($legend);
626 $this->assertEquals($legend, $this->form
->getLegend());
629 public function testDescriptionInitiallyNull()
631 $this->assertNull($this->form
->getDescription());
634 public function testCanSetDescription()
636 $this->testDescriptionInitiallyNull();
637 $description = "This is a description";
638 $this->form
->setDescription($description);
639 $this->assertEquals($description, $this->form
->getDescription());
644 public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
646 $loader = $this->form
->getPluginLoader('decorator');
647 $this->assertTrue($loader instanceof Zend_Loader_PluginLoader
);
648 $paths = $loader->getPaths('Zend_Form_Decorator');
649 $this->assertTrue(is_array($paths), var_export($loader, 1));
650 $this->assertTrue(0 < count($paths));
651 $this->assertContains('Form', $paths[0]);
652 $this->assertContains('Decorator', $paths[0]);
655 public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
657 $loader = new Zend_Loader_PluginLoader();
659 $this->form
->setPluginLoader($loader, 'foo');
660 $this->fail('Invalid plugin loader type should raise exception');
661 } catch (Zend_Form_Exception
$e) {
662 $this->assertContains('Invalid type', $e->getMessage());
666 public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
669 $this->form
->getPluginLoader('foo');
670 $this->fail('Invalid plugin loader type should raise exception');
671 } catch (Zend_Form_Exception
$e) {
672 $this->assertContains('Invalid type', $e->getMessage());
676 public function testCanSetCustomDecoratorPluginLoader()
678 $loader = new Zend_Loader_PluginLoader();
679 $this->form
->setPluginLoader($loader, 'decorator');
680 $test = $this->form
->getPluginLoader('decorator');
681 $this->assertSame($loader, $test);
684 public function testPassingInvalidTypeToAddPrefixPathThrowsException()
687 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
688 $this->fail('Passing invalid loader type to addPrefixPath() should raise exception');
689 } catch (Zend_Form_Exception
$e) {
690 $this->assertContains('Invalid type', $e->getMessage());
694 public function testCanAddDecoratorPluginLoaderPrefixPath()
696 $loader = $this->form
->getPluginLoader('decorator');
697 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
698 $paths = $loader->getPaths('Zend_Foo');
699 $this->assertTrue(is_array($paths));
700 $this->assertContains('Foo', $paths[0]);
703 public function testUpdatedDecoratorPrefixPathUsedForNewElements()
705 $loader = $this->form
->getPluginLoader('decorator');
706 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
707 $foo = new Zend_Form_Element_Text('foo');
708 $this->form
->addElement($foo);
709 $loader = $foo->getPluginLoader('decorator');
710 $paths = $loader->getPaths('Zend_Foo');
711 $this->assertTrue(is_array($paths));
712 $this->assertContains('Foo', $paths[0]);
714 $this->form
->addElement('text', 'bar');
715 $bar = $this->form
->bar
;
716 $loader = $bar->getPluginLoader('decorator');
717 $paths = $loader->getPaths('Zend_Foo');
718 $this->assertTrue(is_array($paths));
719 $this->assertContains('Foo', $paths[0]);
722 public function testUpdatedDecoratorPrefixPathUsedForNewDisplayGroups()
724 $loader = $this->form
->getPluginLoader('decorator');
725 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
726 $this->setupElements();
727 $foo = $this->form
->foo
;
728 $loader = $foo->getPluginLoader('decorator');
729 $paths = $loader->getPaths('Zend_Foo');
730 $this->assertTrue(is_array($paths));
731 $this->assertContains('Foo', $paths[0]);
734 public function testUpdatedPrefixPathUsedForNewSubForms()
736 $loader = $this->form
->getPluginLoader('decorator');
737 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
738 $this->setupSubForm();
739 $loader = $this->form
->sub
->getPluginLoader('decorator');
740 $paths = $loader->getPaths('Zend_Foo');
741 $this->assertTrue(is_array($paths));
742 $this->assertContains('Foo', $paths[0]);
745 public function testGetPluginLoaderRetrievesDefaultElementPluginLoader()
747 $loader = $this->form
->getPluginLoader('element');
748 $this->assertTrue($loader instanceof Zend_Loader_PluginLoader
);
749 $paths = $loader->getPaths('Zend_Form_Element');
750 $this->assertTrue(is_array($paths), var_export($loader, 1));
751 $this->assertTrue(0 < count($paths));
752 $this->assertContains('Form', $paths[0]);
753 $this->assertContains('Element', $paths[0]);
756 public function testCanSetCustomDecoratorElementLoader()
758 $loader = new Zend_Loader_PluginLoader();
759 $this->form
->setPluginLoader($loader, 'element');
760 $test = $this->form
->getPluginLoader('element');
761 $this->assertSame($loader, $test);
764 public function testCanAddElementPluginLoaderPrefixPath()
766 $loader = $this->form
->getPluginLoader('element');
767 $this->form
->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'element');
768 $paths = $loader->getPaths('Zend_Foo');
769 $this->assertTrue(is_array($paths));
770 $this->assertContains('Foo', $paths[0]);
773 public function testAddAllPluginLoaderPrefixPathsSimultaneously()
775 $decoratorLoader = new Zend_Loader_PluginLoader();
776 $elementLoader = new Zend_Loader_PluginLoader();
777 $this->form
->setPluginLoader($decoratorLoader, 'decorator')
778 ->setPluginLoader($elementLoader, 'element')
779 ->addPrefixPath('Zend', 'Zend/');
781 $paths = $decoratorLoader->getPaths('Zend_Decorator');
782 $this->assertTrue(is_array($paths), var_export($paths, 1));
783 $this->assertContains('Decorator', $paths[0]);
785 $paths = $elementLoader->getPaths('Zend_Element');
786 $this->assertTrue(is_array($paths), var_export($paths, 1));
787 $this->assertContains('Element', $paths[0]);
792 public function testCanAddAndRetrieveSingleElements()
794 $element = new Zend_Form_Element('foo');
795 $this->form
->addElement($element);
796 $this->assertSame($element, $this->form
->getElement('foo'));
799 public function testGetElementReturnsNullForUnregisteredElement()
801 $this->assertNull($this->form
->getElement('foo'));
804 public function testCanAddAndRetrieveSingleElementsByStringType()
806 $this->form
->addElement('text', 'foo');
807 $element = $this->form
->getElement('foo');
808 $this->assertTrue($element instanceof Zend_Form_Element
);
809 $this->assertTrue($element instanceof Zend_Form_Element_Text
);
810 $this->assertEquals('foo', $element->getName());
813 public function testAddElementAsStringElementThrowsExceptionWhenNoNameProvided()
816 $this->form
->addElement('text');
817 $this->fail('Should not be able to specify string element type without name');
818 } catch (Zend_Form_Exception
$e) {
819 $this->assertContains('must have', $e->getMessage());
823 public function testCreateElementReturnsNewElement()
825 $element = $this->form
->createElement('text', 'foo');
826 $this->assertTrue($element instanceof Zend_Form_Element
);
829 public function testCreateElementDoesNotAttachElementToForm()
831 $element = $this->form
->createElement('text', 'foo');
832 $this->assertTrue($element instanceof Zend_Form_Element
);
833 $this->assertNull($this->form
->foo
);
836 public function testCanAddAndRetrieveMultipleElements()
838 $this->form
->addElements(array(
840 array('text', 'bar'),
841 array('text', 'baz', array('foo' => 'bar')),
842 new Zend_Form_Element_Text('bat'),
844 $elements = $this->form
->getElements();
845 $names = array('foo', 'bar', 'baz', 'bat');
846 $this->assertEquals($names, array_keys($elements));
847 $foo = $elements['foo'];
848 $this->assertTrue($foo instanceof Zend_Form_Element_Text
);
849 $bar = $elements['bar'];
850 $this->assertTrue($bar instanceof Zend_Form_Element_Text
);
851 $baz = $elements['baz'];
852 $this->assertTrue($baz instanceof Zend_Form_Element_Text
);
853 $this->assertEquals('bar', $baz->foo
, var_export($baz->getAttribs(), 1));
854 $bat = $elements['bat'];
855 $this->assertTrue($bat instanceof Zend_Form_Element_Text
);
858 public function testSetElementsOverwritesExistingElements()
860 $this->testCanAddAndRetrieveMultipleElements();
861 $this->form
->setElements(array(
864 $elements = $this->form
->getElements();
865 $names = array('bogus');
866 $this->assertEquals($names, array_keys($elements));
869 public function testCanRemoveSingleElement()
871 $this->testCanAddAndRetrieveMultipleElements();
872 $this->assertTrue($this->form
->removeElement('bar'));
873 $this->assertNull($this->form
->getElement('bar'));
876 public function testRemoveElementReturnsFalseWhenElementNotRegistered()
878 $this->assertFalse($this->form
->removeElement('bogus'));
881 public function testCanClearAllElements()
883 $this->testCanAddAndRetrieveMultipleElements();
884 $this->form
->clearElements();
885 $elements = $this->form
->getElements();
886 $this->assertTrue(is_array($elements));
887 $this->assertTrue(empty($elements));
890 public function testGetValueReturnsNullForUndefinedElements()
892 $this->assertNull($this->form
->getValue('foo'));
895 public function testCanSetElementDefaultValues()
897 $this->testCanAddAndRetrieveMultipleElements();
904 $this->form
->setDefaults($values);
905 $elements = $this->form
->getElements();
906 foreach (array_keys($values) as $name) {
907 $this->assertEquals($name . 'value', $elements[$name]->getValue(), var_export($elements[$name], 1));
911 public function testSettingElementDefaultsDoesNotSetElementValuesToNullIfNotInDefaultsArray()
913 $this->testCanAddAndRetrieveMultipleElements();
914 $this->form
->baz
->setValue('testing');
915 $this->form
->bar
->setValue('testing');
920 $this->form
->setDefaults($values);
921 $this->assertEquals('foovalue', $this->form
->foo
->getValue());
922 $this->assertEquals('batvalue', $this->form
->bat
->getValue());
923 $this->assertNotNull($this->form
->baz
->getValue());
924 $this->assertNotNull($this->form
->bar
->getValue());
927 public function testCanRetrieveSingleElementValue()
929 $this->form
->addElement('text', 'foo', array('value' => 'foovalue'));
930 $this->assertEquals('foovalue', $this->form
->getValue('foo'));
933 public function testCanRetrieveAllElementValues()
935 $this->testCanAddAndRetrieveMultipleElements();
942 $this->form
->setDefaults($values);
943 $test = $this->form
->getValues();
944 $elements = $this->form
->getElements();
945 foreach (array_keys($values) as $name) {
946 $this->assertEquals($values[$name], $test[$name]);
950 public function testRetrievingAllElementValuesSkipsThoseFlaggedAsIgnore()
952 $this->form
->addElements(array(
957 $this->form
->setDefaults(array(
958 'foo' => 'Foo Value',
959 'bar' => 'Bar Value',
960 'baz' => 'Baz Value',
962 $this->form
->bar
->setIgnore(true);
963 $test = $this->form
->getValues();
964 $this->assertFalse(array_key_exists('bar', $test));
965 $this->assertTrue(array_key_exists('foo', $test));
966 $this->assertTrue(array_key_exists('baz', $test));
969 public function testCanRetrieveSingleUnfilteredElementValue()
971 $foo = new Zend_Form_Element_Text('foo');
972 $foo->addFilter('StringToUpper')
973 ->setValue('foovalue');
974 $this->form
->addElement($foo);
975 $this->assertEquals('FOOVALUE', $this->form
->getValue('foo'));
976 $this->assertEquals('foovalue', $this->form
->getUnfilteredValue('foo'));
979 public function testCanRetrieveAllUnfilteredElementValues()
981 $foo = new Zend_Form_Element_Text('foo');
982 $foo->addFilter('StringToUpper')
983 ->setValue('foovalue');
984 $bar = new Zend_Form_Element_Text('bar');
985 $bar->addFilter('StringToUpper')
986 ->setValue('barvalue');
987 $this->form
->addElements(array($foo, $bar));
988 $values = $this->form
->getValues();
989 $unfiltered = $this->form
->getUnfilteredValues();
990 foreach (array('foo', 'bar') as $key) {
991 $value = $key . 'value';
992 $this->assertEquals(strtoupper($value), $values[$key]);
993 $this->assertEquals($value, $unfiltered[$key]);
997 public function testOverloadingElements()
999 $this->form
->addElement('text', 'foo');
1000 $this->assertTrue(isset($this->form
->foo
));
1001 $element = $this->form
->foo
;
1002 $this->assertTrue($element instanceof Zend_Form_Element
);
1003 unset($this->form
->foo
);
1004 $this->assertFalse(isset($this->form
->foo
));
1006 $bar = new Zend_Form_Element_Text('bar');
1007 $this->form
->bar
= $bar;
1008 $this->assertTrue(isset($this->form
->bar
));
1009 $element = $this->form
->bar
;
1010 $this->assertSame($bar, $element);
1013 public function testOverloadingGetReturnsNullForUndefinedFormItems()
1015 $this->assertNull($this->form
->bogus
);
1018 public function testOverloadingSetThrowsExceptionForInvalidTypes()
1021 $this->form
->foo
= true;
1022 $this->fail('Overloading should not allow scalars');
1023 } catch (Zend_Form_Exception
$e) {
1024 $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
1028 $this->form
->foo
= new Zend_Config(array());
1029 $this->fail('Overloading should not allow arbitrary object types');
1030 } catch (Zend_Form_Exception
$e) {
1031 $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
1032 $this->assertContains('Zend_Config', $e->getMessage());
1036 public function testFormIsNotAnArrayByDefault()
1038 $this->assertFalse($this->form
->isArray());
1041 public function testCanSetArrayFlag()
1043 $this->testFormIsNotAnArrayByDefault();
1044 $this->form
->setIsArray(true);
1045 $this->assertTrue($this->form
->isArray());
1046 $this->form
->setIsArray(false);
1047 $this->assertFalse($this->form
->isArray());
1050 public function testElementsBelongToReturnsFormNameWhenFormIsArray()
1052 $this->form
->setName('foo')
1054 $this->assertEquals('foo', $this->form
->getElementsBelongTo());
1057 public function testElementsInitiallyBelongToNoArrays()
1059 $this->assertNull($this->form
->getElementsBelongTo());
1062 public function testCanSetArrayToWhichElementsBelong()
1064 $this->testElementsInitiallyBelongToNoArrays();
1065 $this->form
->setElementsBelongTo('foo');
1066 $this->assertEquals('foo', $this->form
->getElementsBelongTo());
1069 public function testSettingArrayToWhichElementsBelongSetsArrayFlag()
1071 $this->testFormIsNotAnArrayByDefault();
1072 $this->testCanSetArrayToWhichElementsBelong();
1073 $this->assertTrue($this->form
->isArray());
1076 public function testArrayToWhichElementsBelongCanConsistOfValidVariableCharsOnly()
1078 $this->testElementsInitiallyBelongToNoArrays();
1079 $this->form
->setElementsBelongTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
1080 $this->assertEquals('foobar', $this->form
->getElementsBelongTo());
1083 public function testSettingArrayToWhichElementsBelongEmptyClearsIt()
1085 $this->testCanSetArrayToWhichElementsBelong();
1086 $this->form
->setElementsBelongTo('');
1087 $this->assertNull($this->form
->getElementsBelongTo());
1090 public function testSettingArrayToWhichElementsBelongEmptySetsArrayFlagToFalse()
1092 $this->testSettingArrayToWhichElementsBelongEmptyClearsIt();
1093 $this->assertFalse($this->form
->isArray());
1099 public function testSetElementsBelongToShouldApplyToBothExistingAndFutureElements()
1101 $this->form
->addElement('text', 'testBelongsTo');
1102 $this->form
->setElementsBelongTo('foo');
1103 $this->assertEquals('foo', $this->form
->testBelongsTo
->getBelongsTo(), 'Failed determining testBelongsTo belongs to array');
1104 $this->setupElements();
1105 foreach ($this->form
->getElements() as $element) {
1106 $message = sprintf('Failed determining element "%s" belongs to foo', $element->getName());
1107 $this->assertEquals('foo', $element->getBelongsTo(), $message);
1114 public function testElementsInDisplayGroupsShouldInheritFormElementsBelongToSetting()
1116 $subForm = new Zend_Form_SubForm();
1117 $subForm->addElements(array(
1118 new Zend_Form_Element_Text('foo'),
1119 new Zend_Form_Element_Text('bar'),
1120 new Zend_Form_Element_Text('baz'),
1121 new Zend_Form_Element_Text('bat'),
1123 ->addDisplayGroup(array('bar', 'baz'), 'barbaz');
1124 $this->form
->addSubForm($subForm, 'sub')
1125 ->setElementsBelongTo('myform')
1126 ->setView(new Zend_View
);
1127 $html = $this->form
->render();
1128 foreach (array('foo', 'bar', 'baz', 'bat') as $test) {
1129 $this->assertContains('id="myform-sub-' . $test . '"', $html);
1130 $this->assertContains('name="myform[sub][' . $test . ']"', $html);
1134 public function testIsValidWithOneLevelElementsBelongTo()
1136 $this->form
->addElement('text', 'test')->test
1137 ->addValidator('Identical', false, array('Test Value'));
1138 $this->form
->setElementsBelongTo('foo');
1142 'test' => 'Test Value',
1146 $this->assertTrue($this->form
->isValid($data));
1149 public function testIsValidWithMultiLevelElementsBelongTo()
1151 $this->form
->addElement('text', 'test')->test
1152 ->addValidator('Identical', false, array('Test Value'));
1153 $this->form
->setElementsBelongTo('foo[bar][zot]');
1159 'test' => 'Test Value',
1165 $this->assertTrue($this->form
->isValid($data));
1170 public function testCanAddAndRetrieveSingleSubForm()
1172 $subForm = new Zend_Form_SubForm
;
1173 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1174 $this->form
->addSubForm($subForm, 'page1');
1175 $test = $this->form
->getSubForm('page1');
1176 $this->assertSame($subForm, $test);
1179 public function testAddingSubFormSetsSubFormName()
1181 $subForm = new Zend_Form_SubForm
;
1182 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1183 $this->form
->addSubForm($subForm, 'page1');
1184 $this->assertEquals('page1', $subForm->getName());
1187 public function testGetSubFormReturnsNullForUnregisteredSubForm()
1189 $this->assertNull($this->form
->getSubForm('foo'));
1192 public function testCanAddAndRetrieveMultipleSubForms()
1194 $page1 = new Zend_Form_SubForm();
1195 $page2 = new Zend_Form_SubForm();
1196 $page3 = new Zend_Form_SubForm();
1197 $this->form
->addSubForms(array(
1199 array($page2, 'page2'),
1200 array($page3, 'page3', 3)
1202 $subforms = $this->form
->getSubForms();
1203 $keys = array('page1', 'page2', 'page3');
1204 $this->assertEquals($keys, array_keys($subforms));
1205 $this->assertSame($page1, $subforms['page1']);
1206 $this->assertSame($page2, $subforms['page2']);
1207 $this->assertSame($page3, $subforms['page3']);
1210 public function testSetSubFormsOverwritesExistingSubForms()
1212 $this->testCanAddAndRetrieveMultipleSubForms();
1213 $foo = new Zend_Form_SubForm();
1214 $this->form
->setSubForms(array('foo' => $foo));
1215 $subforms = $this->form
->getSubForms();
1216 $keys = array('foo');
1217 $this->assertEquals($keys, array_keys($subforms));
1218 $this->assertSame($foo, $subforms['foo']);
1221 public function testCanRemoveSingleSubForm()
1223 $this->testCanAddAndRetrieveMultipleSubForms();
1224 $this->assertTrue($this->form
->removeSubForm('page2'));
1225 $this->assertNull($this->form
->getSubForm('page2'));
1228 public function testRemoveSubFormReturnsFalseForNonexistantSubForm()
1230 $this->assertFalse($this->form
->removeSubForm('foo'));
1233 public function testCanClearAllSubForms()
1235 $this->testCanAddAndRetrieveMultipleSubForms();
1236 $this->form
->clearSubForms();
1237 $subforms = $this->form
->getSubForms();
1238 $this->assertTrue(is_array($subforms));
1239 $this->assertTrue(empty($subforms));
1242 public function testOverloadingSubForms()
1244 $foo = new Zend_Form_SubForm
;
1245 $this->form
->addSubForm($foo, 'foo');
1246 $this->assertTrue(isset($this->form
->foo
));
1247 $subform = $this->form
->foo
;
1248 $this->assertSame($foo, $subform);
1249 unset($this->form
->foo
);
1250 $this->assertFalse(isset($this->form
->foo
));
1252 $bar = new Zend_Form_SubForm();
1253 $this->form
->bar
= $bar;
1254 $this->assertTrue(isset($this->form
->bar
));
1255 $subform = $this->form
->bar
;
1256 $this->assertSame($bar, $subform);
1259 public function testCanSetDefaultsForSubFormElementsFromForm()
1261 $subForm = new Zend_Form_SubForm
;
1262 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1263 $this->form
->addSubForm($subForm, 'page1');
1265 $data = array('foo' => 'foo value', 'bar' => 'bar value');
1266 $this->form
->setDefaults($data);
1267 $this->assertEquals($data['foo'], $subForm->foo
->getValue());
1268 $this->assertEquals($data['bar'], $subForm->bar
->getValue());
1271 public function testCanSetDefaultsForSubFormElementsFromFormWithArray()
1273 $subForm = new Zend_Form_SubForm
;
1274 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1275 $this->form
->addSubForm($subForm, 'page1');
1277 $data = array( 'page1' => array(
1278 'foo' => 'foo value',
1279 'bar' => 'bar value'
1281 $this->form
->setDefaults($data);
1282 $this->assertEquals($data['page1']['foo'], $subForm->foo
->getValue());
1283 $this->assertEquals($data['page1']['bar'], $subForm->bar
->getValue());
1286 public function testGetValuesReturnsSubFormValues()
1288 $subForm = new Zend_Form_SubForm
;
1289 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1290 $subForm->foo
->setValue('foo value');
1291 $subForm->bar
->setValue('bar value');
1292 $this->form
->addSubForm($subForm, 'page1');
1294 $values = $this->form
->getValues();
1295 $this->assertTrue(isset($values['page1']));
1296 $this->assertTrue(isset($values['page1']['foo']));
1297 $this->assertTrue(isset($values['page1']['bar']));
1298 $this->assertEquals($subForm->foo
->getValue(), $values['page1']['foo']);
1299 $this->assertEquals($subForm->bar
->getValue(), $values['page1']['bar']);
1302 public function testGetValuesReturnsSubFormValuesFromArrayToWhichElementsBelong()
1304 $subForm = new Zend_Form_SubForm
;
1305 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
1306 ->setElementsBelongTo('subform');
1307 $subForm->foo
->setValue('foo value');
1308 $subForm->bar
->setValue('bar value');
1309 $this->form
->addSubForm($subForm, 'page1');
1311 $values = $this->form
->getValues();
1312 $this->assertTrue(isset($values['subform']), var_export($values, 1));
1313 $this->assertTrue(isset($values['subform']['foo']));
1314 $this->assertTrue(isset($values['subform']['bar']));
1315 $this->assertEquals($subForm->foo
->getValue(), $values['subform']['foo']);
1316 $this->assertEquals($subForm->bar
->getValue(), $values['subform']['bar']);
1319 public function testGetValuesReturnsNestedSubFormValuesFromArraysToWhichElementsBelong()
1321 $form = new Zend_Form();
1322 $form->setElementsBelongTo('foobar');
1324 $form->addElement('text', 'firstName')
1325 ->getElement('firstName')
1326 ->setRequired(true);
1328 $form->addElement('text', 'lastName')
1329 ->getElement('lastName')
1330 ->setRequired(true);
1332 $subForm = new Zend_Form_SubForm();
1333 $subForm->setElementsBelongTo('baz[quux]');
1334 $subForm->addElement('text', 'email')
1335 ->getElement('email')->setRequired(true);
1337 $subSubForm = new Zend_Form_SubForm();
1338 $subSubForm->setElementsBelongTo('bat');
1339 $subSubForm->addElement('checkbox', 'home')
1340 ->getElement('home')->setRequired(true);
1342 $subForm->addSubForm($subSubForm, 'subSub');
1344 $form->addSubForm($subForm, 'sub')
1345 ->addElement('submit', 'save', array('value' => 'submit', 'ignore' => true));
1348 $data = array('foobar' => array(
1349 'firstName' => 'Mabel',
1350 'lastName' => 'Cow',
1353 'email' => 'mabel@cow.org',
1360 $this->assertTrue($form->isValid($data));
1362 $values = $form->getValues();
1363 $this->assertEquals($data, $values);
1366 public function testGetValueCanReturnSubFormValues()
1368 $subForm = new Zend_Form_SubForm
;
1369 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
1370 $subForm->foo
->setValue('foo value');
1371 $subForm->bar
->setValue('bar value');
1372 $this->form
->addSubForm($subForm, 'page1');
1374 $values = $this->form
->getValue('page1');
1375 $this->assertTrue(isset($values['foo']), var_export($values, 1));
1376 $this->assertTrue(isset($values['bar']));
1377 $this->assertEquals($subForm->foo
->getValue(), $values['foo']);
1378 $this->assertEquals($subForm->bar
->getValue(), $values['bar']);
1381 public function testGetValueCanReturnSubFormValuesFromArrayToWhichElementsBelong()
1383 $subForm = new Zend_Form_SubForm
;
1384 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
1385 ->setElementsBelongTo('subform');
1386 $subForm->foo
->setValue('foo value');
1387 $subForm->bar
->setValue('bar value');
1388 $this->form
->addSubForm($subForm, 'page1');
1390 $values = $this->form
->getValue('subform');
1391 $this->assertTrue(isset($values['foo']), var_export($values, 1));
1392 $this->assertTrue(isset($values['bar']));
1393 $this->assertEquals($subForm->foo
->getValue(), $values['foo']);
1394 $this->assertEquals($subForm->bar
->getValue(), $values['bar']);
1397 public function testIsValidCanValidateSubFormsWithArbitraryElementsBelong()
1399 $subForm = new Zend_Form_SubForm();
1400 $subForm->addElement('text', 'test')->test
1401 ->setRequired(true)->addValidator('Identical', false, array('Test Value'));
1402 $this->form
->addSubForm($subForm, 'sub');
1404 $this->form
->setElementsBelongTo('foo[bar]');
1405 $subForm->setElementsBelongTo('my[subform]');
1412 'test' => 'Test Value',
1419 $this->assertTrue($this->form
->isValid($data));
1422 public function testIsValidCanValidateNestedSubFormsWithArbitraryElementsBelong()
1424 $subForm = new Zend_Form_SubForm();
1425 $subForm->addElement('text', 'test1')->test1
1426 ->setRequired(true)->addValidator('Identical', false, array('Test1 Value'));
1427 $this->form
->addSubForm($subForm, 'sub');
1429 $subSubForm = new Zend_Form_SubForm();
1430 $subSubForm->addElement('text', 'test2')->test2
1431 ->setRequired(true)->addValidator('Identical', false, array('Test2 Value'));
1432 $subForm->addSubForm($subSubForm, 'subSub');
1434 $this->form
->setElementsBelongTo('form[first]');
1435 // Notice we skipped subForm, to mix manual and auto elementsBelongTo.
1436 $subSubForm->setElementsBelongTo('subsubform[first]');
1442 'test1' => 'Test1 Value',
1444 'subsubform' => array(
1446 'test2' => 'Test2 Value',
1454 $this->assertTrue($this->form
->isValid($data));
1460 public function testCanAddAndRetrieveSingleDisplayGroups()
1462 $this->testCanAddAndRetrieveMultipleElements();
1463 $this->form
->addDisplayGroup(array('bar', 'bat'), 'barbat');
1464 $group = $this->form
->getDisplayGroup('barbat');
1465 $this->assertTrue($group instanceof Zend_Form_DisplayGroup
);
1466 $elements = $group->getElements();
1467 $expected = array('bar' => $this->form
->bar
, 'bat' => $this->form
->bat
);
1468 $this->assertEquals($expected, $elements);
1471 public function testDisplayGroupsMustContainAtLeastOneElement()
1474 $this->form
->addDisplayGroup(array(), 'foo');
1475 $this->fail('Empty display group should raise exception');
1476 } catch (Zend_Form_Exception
$e) {
1477 $this->assertContains('No valid elements', $e->getMessage());
1481 public function testCanAddAndRetrieveMultipleDisplayGroups()
1483 $this->testCanAddAndRetrieveMultipleElements();
1484 $this->form
->addDisplayGroups(array(
1485 array(array('bar', 'bat'), 'barbat'),
1486 'foobaz' => array('baz', 'foo')
1488 $groups = $this->form
->getDisplayGroups();
1490 'barbat' => array('bar' => $this->form
->bar
, 'bat' => $this->form
->bat
),
1491 'foobaz' => array('baz' => $this->form
->baz
, 'foo' => $this->form
->foo
),
1493 foreach ($groups as $group) {
1494 $this->assertTrue($group instanceof Zend_Form_DisplayGroup
);
1496 $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
1497 $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
1500 public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
1502 $this->testCanAddAndRetrieveMultipleDisplayGroups();
1503 $this->form
->setDisplayGroups(array('foobar' => array('bar', 'foo')));
1504 $groups = $this->form
->getDisplayGroups();
1505 $expected = array('bar' => $this->form
->bar
, 'foo' => $this->form
->foo
);
1506 $this->assertEquals(1, count($groups));
1507 $this->assertTrue(isset($groups['foobar']));
1508 $this->assertEquals($expected, $groups['foobar']->getElements());
1511 public function testCanRemoveSingleDisplayGroup()
1513 $this->testCanAddAndRetrieveMultipleDisplayGroups();
1514 $this->assertTrue($this->form
->removeDisplayGroup('barbat'));
1515 $this->assertNull($this->form
->getDisplayGroup('barbat'));
1518 public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
1520 $this->assertFalse($this->form
->removeDisplayGroup('bogus'));
1523 public function testCanClearAllDisplayGroups()
1525 $this->testCanAddAndRetrieveMultipleDisplayGroups();
1526 $this->form
->clearDisplayGroups();
1527 $groups = $this->form
->getDisplayGroups();
1528 $this->assertTrue(is_array($groups));
1529 $this->assertTrue(empty($groups));
1532 public function testOverloadingDisplayGroups()
1534 $this->testCanAddAndRetrieveMultipleElements();
1535 $this->form
->addDisplayGroup(array('foo', 'bar'), 'foobar');
1536 $this->assertTrue(isset($this->form
->foobar
));
1537 $group = $this->form
->foobar
;
1538 $expected = array('foo' => $this->form
->foo
, 'bar' => $this->form
->bar
);
1539 $this->assertEquals($expected, $group->getElements());
1540 unset($this->form
->foobar
);
1541 $this->assertFalse(isset($this->form
->foobar
));
1543 $this->form
->barbaz
= array('bar', 'baz');
1544 $this->assertTrue(isset($this->form
->barbaz
));
1545 $group = $this->form
->barbaz
;
1546 $expected = array('bar' => $this->form
->bar
, 'baz' => $this->form
->baz
);
1547 $this->assertSame($expected, $group->getElements());
1550 public function testDefaultDisplayGroupClassExists()
1552 $this->assertEquals('Zend_Form_DisplayGroup', $this->form
->getDefaultDisplayGroupClass());
1555 public function testCanSetDefaultDisplayGroupClass()
1557 $this->testDefaultDisplayGroupClassExists();
1558 $this->form
->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
1559 $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form
->getDefaultDisplayGroupClass());
1562 public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
1564 $this->form
->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
1565 $this->setupElements();
1566 $this->form
->addDisplayGroup(array('foo', 'bar'), 'foobar');
1567 $displayGroup = $this->form
->getDisplayGroup('foobar');
1568 $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup
);
1571 public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
1573 $this->setupElements();
1574 $this->form
->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
1575 $this->assertTrue($this->form
->foobar
instanceof Zend_Form_FormTest_DisplayGroup
);
1581 public function testAddingDisplayGroupShouldPassOptions()
1583 $this->testCanAddAndRetrieveMultipleElements();
1584 $this->form
->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
1585 $group = $this->form
->getDisplayGroup('barbat');
1586 $this->assertTrue($group instanceof Zend_Form_DisplayGroup
);
1587 $decorators = $group->getDecorators();
1588 $this->assertTrue(is_array($decorators));
1589 $this->assertTrue(empty($decorators));
1594 public function testPopulateProxiesToSetDefaults()
1596 $this->testCanAddAndRetrieveMultipleElements();
1598 'foo' => 'foovalue',
1599 'bar' => 'barvalue',
1600 'baz' => 'bazvalue',
1603 $this->form
->populate($values);
1604 $test = $this->form
->getValues();
1605 $elements = $this->form
->getElements();
1606 foreach (array_keys($values) as $name) {
1607 $this->assertEquals($values[$name], $test[$name]);
1611 public function setupElements()
1613 $foo = new Zend_Form_Element_Text('foo');
1614 $foo->addValidator('NotEmpty')
1615 ->addValidator('Alpha');
1616 $bar = new Zend_Form_Element_Text('bar');
1617 $bar->addValidator('NotEmpty')
1618 ->addValidator('Digits');
1619 $baz = new Zend_Form_Element_Text('baz');
1620 $baz->addValidator('NotEmpty')
1621 ->addValidator('Alnum');
1622 $this->form
->addElements(array($foo, $bar, $baz));
1623 $this->elementValues
= array(
1624 'foo' => 'fooBarBAZ',
1625 'bar' => '123456789',
1626 'baz' => 'foo123BAR',
1630 public function testIsValidShouldThrowExceptionWithNonArrayArgument()
1633 $this->form
->isValid(true);
1634 $this->fail('isValid() should raise exception with non-array argument');
1635 } catch (Zend_Form_Exception
$e) {
1636 $this->assertContains('expects an array', $e->getMessage());
1640 public function testCanValidateFullFormContainingOnlyElements()
1642 $this->_checkZf2794();
1644 $this->setupElements();
1645 $this->assertTrue($this->form
->isValid($this->elementValues
));
1651 $this->assertFalse($this->form
->isValid($values));
1653 $validator = $this->form
->foo
->getValidator('alpha');
1654 $this->assertEquals('12345', $validator->value
);
1656 $validator = $this->form
->bar
->getValidator('digits');
1657 $this->assertEquals('abc', $validator->value
);
1659 $validator = $this->form
->baz
->getValidator('alnum');
1660 $this->assertEquals('abc-123', $validator->value
);
1663 public function testValidationTakesElementRequiredFlagsIntoAccount()
1665 $this->_checkZf2794();
1667 $this->setupElements();
1669 $this->assertTrue($this->form
->isValid(array()));
1671 $this->form
->getElement('foo')->setRequired(true);
1672 $this->assertTrue($this->form
->isValid(array(
1676 $this->assertFalse($this->form
->isValid(array(
1681 public function testCanValidatePartialFormContainingOnlyElements()
1683 $this->_checkZf2794();
1685 $this->setupElements();
1686 $this->form
->getElement('foo')->setRequired(true);
1687 $this->form
->getElement('bar')->setRequired(true);
1688 $this->form
->getElement('baz')->setRequired(true);
1689 $this->assertTrue($this->form
->isValidPartial(array(
1693 $this->assertFalse($this->form
->isValidPartial(array(
1699 public function setupSubForm()
1701 $subForm = new Zend_Form_SubForm();
1702 $foo = new Zend_Form_Element_Text('subfoo');
1703 $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
1704 $bar = new Zend_Form_Element_Text('subbar');
1705 $bar->addValidators(array('NotEmpty', 'Digits'));
1706 $baz = new Zend_Form_Element_Text('subbaz');
1707 $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
1708 $subForm->addElements(array($foo, $bar, $baz));
1709 $this->form
->addSubForm($subForm, 'sub');
1712 public function testFullDataArrayUsedToValidateSubFormByDefault()
1714 $this->_checkZf2794();
1716 $this->setupElements();
1717 $this->setupSubForm();
1722 'subfoo' => 'abcdef',
1723 'subbar' => '123456',
1724 'subbaz' => '123abc',
1726 $this->assertTrue($this->form
->isValid($data));
1734 'subbaz' => '123-abc',
1736 $this->assertFalse($this->form
->isValid($data));
1743 'subbaz' => '123abc',
1745 $this->assertTrue($this->form
->isValid($data));
1752 'subbaz' => '123abc',
1754 $this->assertFalse($this->form
->isValid($data));
1757 public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
1759 $this->_checkZf2794();
1761 $this->setupElements();
1762 $this->setupSubForm();
1768 'subfoo' => 'abcdef',
1769 'subbar' => '123456',
1770 'subbaz' => '123abc',
1773 $this->assertTrue($this->form
->isValid($data));
1782 'subbaz' => '123-abc',
1785 $this->assertFalse($this->form
->isValid($data));
1793 'subbaz' => '123abc',
1796 $this->assertTrue($this->form
->isValid($data));
1804 'subbaz' => '123abc',
1807 $this->assertFalse($this->form
->isValid($data));
1810 public function testCanValidateNestedFormsWithElementsBelongingToArrays()
1812 $form = new Zend_Form();
1813 $form->setElementsBelongTo('foobar');
1815 $form->addElement('text', 'firstName')
1816 ->getElement('firstName')
1817 ->setRequired(true);
1819 $form->addElement('text', 'lastName')
1820 ->getElement('lastName')
1821 ->setRequired(true);
1823 $subForm = new Zend_Form_SubForm();
1824 $subForm->setElementsBelongTo('baz');
1825 $subForm->addElement('text', 'email')
1826 ->getElement('email')->setRequired(true);
1828 $subSubForm = new Zend_Form_SubForm();
1829 $subSubForm->setElementsBelongTo('bat');
1830 $subSubForm->addElement('checkbox', 'home')
1831 ->getElement('home')->setRequired(true);
1833 $subForm->addSubForm($subSubForm, 'subSub');
1835 $form->addSubForm($subForm, 'sub')
1836 ->addElement('submit', 'save', array('value' => 'submit'));
1839 $data = array('foobar' => array(
1840 'firstName' => 'Mabel',
1841 'lastName' => 'Cow',
1843 'email' => 'mabel@cow.org',
1849 $this->assertTrue($form->isValid($data));
1850 $this->assertEquals('Mabel', $form->firstName
->getValue());
1851 $this->assertEquals('Cow', $form->lastName
->getValue());
1852 $this->assertEquals('mabel@cow.org', $form->sub
->email
->getValue());
1853 $this->assertEquals(1, $form->sub
->subSub
->home
->getValue());
1856 public function testCanValidatePartialFormContainingSubForms()
1858 $this->_checkZf2794();
1860 $this->setupElements();
1861 $this->setupSubForm();
1864 'subfoo' => 'abcdef',
1865 'subbar' => '123456',
1867 $this->assertTrue($this->form
->isValidPartial($data));
1876 $this->assertTrue($this->form
->isValidPartial($data));
1886 $this->assertFalse($this->form
->isValidPartial($data));
1889 public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
1891 $this->_checkZf2794();
1893 $form = new Zend_Form();
1894 $form->setElementsBelongTo('foobar');
1896 $form->addElement('text', 'firstName')
1897 ->getElement('firstName')
1898 ->setRequired(false);
1900 $form->addElement('text', 'lastName')
1901 ->getElement('lastName')
1902 ->setRequired(true);
1904 $subForm = new Zend_Form_SubForm();
1905 $subForm->setElementsBelongTo('baz');
1906 $subForm->addElement('text', 'email')
1907 ->getElement('email')
1909 ->addValidator('NotEmpty');
1911 $subSubForm = new Zend_Form_SubForm();
1912 $subSubForm->setElementsBelongTo('bat');
1913 $subSubForm->addElement('checkbox', 'home')
1914 ->getElement('home')
1916 ->addValidator('InArray', false, array(array('1')));
1918 $subForm->addSubForm($subSubForm, 'subSub');
1920 $form->addSubForm($subForm, 'sub')
1921 ->addElement('submit', 'save', array('value' => 'submit'));
1924 $data = array('foobar' => array(
1925 'lastName' => 'Cow',
1927 $this->assertTrue($form->isValidPartial($data));
1928 $this->assertEquals('Cow', $form->lastName
->getValue());
1929 $firstName = $form->firstName
->getValue();
1930 $email = $form->sub
->email
->getValue();
1931 $home = $form->sub
->subSub
->home
->getValue();
1932 $this->assertTrue(empty($firstName));
1933 $this->assertTrue(empty($email));
1934 $this->assertTrue(empty($home));
1936 $form->sub
->subSub
->home
->addValidator('StringLength', false, array(4, 6));
1937 $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
1939 $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
1940 $this->assertEquals('0', $form->sub
->subSub
->home
->getValue());
1941 $messages = $form->getMessages();
1942 $this->assertFalse(empty($messages));
1943 $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
1944 $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
1947 public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
1949 $this->_checkZf2794();
1951 $form = new Zend_Form();
1952 $form->setElementsBelongTo('foo[bar]');
1954 $form->addElement('text', 'firstName')
1955 ->getElement('firstName')
1956 ->setRequired(false);
1958 $form->addElement('text', 'lastName')
1959 ->getElement('lastName')
1960 ->setRequired(true);
1962 $subForm = new Zend_Form_SubForm();
1963 $subForm->setElementsBelongTo('baz');
1964 $subForm->addElement('text', 'email')
1965 ->getElement('email')
1967 ->addValidator('NotEmpty');
1969 $subSubForm = new Zend_Form_SubForm();
1970 $subSubForm->setElementsBelongTo('bat[quux]');
1971 $subSubForm->addElement('checkbox', 'home')
1972 ->getElement('home')
1974 ->addValidator('InArray', false, array(array('1')));
1976 $subForm->addSubForm($subSubForm, 'subSub');
1978 $form->addSubForm($subForm, 'sub')
1979 ->addElement('submit', 'save', array('value' => 'submit'));
1982 $data = array('foo' => array(
1984 'lastName' => 'Cow',
1987 $this->assertTrue($form->isValidPartial($data));
1988 $this->assertEquals('Cow', $form->lastName
->getValue());
1989 $firstName = $form->firstName
->getValue();
1990 $email = $form->sub
->email
->getValue();
1991 $home = $form->sub
->subSub
->home
->getValue();
1992 $this->assertTrue(empty($firstName));
1993 $this->assertTrue(empty($email));
1994 $this->assertTrue(empty($home));
1996 $form->sub
->subSub
->home
->addValidator('StringLength', false, array(4, 6));
1997 $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
1999 $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
2000 $this->assertEquals('0', $form->sub
->subSub
->home
->getValue());
2003 public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
2005 $this->_checkZf2794();
2007 $form = new Zend_Form();
2008 $form->setElementsBelongTo('foo[bar]');
2010 $form->addElement('text', 'firstName')
2011 ->getElement('firstName')
2012 ->setRequired(false);
2014 $form->addElement('text', 'lastName')
2015 ->getElement('lastName')
2016 ->setRequired(true);
2018 $subForm = new Zend_Form_SubForm();
2019 $subForm->setElementsBelongTo('baz');
2020 $subForm->addElement('text', 'email')
2021 ->getElement('email')
2023 ->addValidator('NotEmpty');
2025 $subSubForm = new Zend_Form_SubForm();
2026 $subSubForm->setElementsBelongTo('bat[quux]');
2027 $subSubForm->addElement('checkbox', 'home')
2028 ->getElement('home')
2030 ->addValidator('InArray', false, array(array('1')));
2032 $subForm->addSubForm($subSubForm, 'subSub');
2034 $form->addSubForm($subForm, 'sub')
2035 ->addElement('submit', 'save', array('value' => 'submit'));
2038 $data = array('foo' => array(
2040 'lastName' => 'Cow',
2045 $form->sub
->subSub
->home
->addValidator('StringLength', false, array(4, 6));
2046 $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
2048 $form->isValidPartial($data);
2050 $messages = $form->getMessages();
2051 $this->assertFalse(empty($messages));
2052 $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
2053 $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
2056 public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
2058 $this->setupElements();
2059 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz');
2060 $this->assertTrue($this->form
->isValid($this->elementValues
));
2061 $this->assertFalse($this->form
->isValid(array(
2068 public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
2070 $this->setupElements();
2071 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz');
2072 $this->assertTrue($this->form
->isValid(array(
2076 $this->assertFalse($this->form
->isValid(array(
2082 public function testProcessAjaxReturnsJsonTrueForValidForm()
2084 $this->setupElements();
2085 $return = $this->form
->processAjax($this->elementValues
);
2086 $this->assertTrue(Zend_Json
::decode($return));
2089 public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
2091 $this->setupElements();
2092 $data = array('foo' => 'abcdef', 'baz' => 'abc123');
2093 $return = $this->form
->processAjax($data);
2094 $this->assertTrue(Zend_Json
::decode($return));
2097 public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
2099 $this->setupElements();
2100 $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
2101 $return = Zend_Json
::decode($this->form
->processAjax($data));
2102 $this->assertTrue(is_array($return));
2103 $this->assertEquals(array_keys($data), array_keys($return));
2106 public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
2108 $this->setupElements();
2109 $data = array('baz' => 'abc-123');
2110 $return = Zend_Json
::decode($this->form
->processAjax($data));
2111 $this->assertTrue(is_array($return));
2112 $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
2115 public function testPersistDataStoresDataInSession()
2117 $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
2120 public function testCanCheckIfErrorsAreRegistered()
2122 $this->assertFalse($this->form
->isErrors());
2123 $this->testCanValidateFullFormContainingOnlyElements();
2124 $this->assertTrue($this->form
->isErrors());
2127 public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
2129 $this->_checkZf2794();
2131 $this->testCanValidateFullFormContainingOnlyElements();
2132 $codes = $this->form
->getErrors();
2133 $keys = array('foo', 'bar', 'baz');
2134 $this->assertEquals($keys, array_keys($codes));
2137 public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
2139 $this->_checkZf2794();
2141 $this->testCanValidateFullFormContainingOnlyElements();
2142 $codes = $this->form
->getErrors();
2143 $keys = array('foo', 'bar', 'baz');
2144 $errors = $this->form
->getErrors('foo');
2145 $foo = $this->form
->foo
;
2146 $this->assertEquals($foo->getErrors(), $errors);
2149 public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
2151 $this->_checkZf2794();
2153 $this->testCanValidateFullFormContainingOnlyElements();
2154 $codes = $this->form
->getMessages();
2155 $keys = array('foo', 'bar', 'baz');
2156 $this->assertEquals($keys, array_keys($codes));
2159 public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
2161 $this->_checkZf2794();
2163 $this->testCanValidateFullFormContainingOnlyElements();
2164 $codes = $this->form
->getMessages();
2165 $keys = array('foo', 'bar', 'baz');
2166 $messages = $this->form
->getMessages('foo');
2167 $foo = $this->form
->foo
;
2168 $this->assertEquals($foo->getMessages(), $messages);
2171 public function testErrorCodesFromSubFormReturnedInSeparateArray()
2173 $this->_checkZf2794();
2175 $this->testFullDataArrayUsedToValidateSubFormByDefault();
2176 $codes = $this->form
->getErrors();
2177 $this->assertTrue(array_key_exists('sub', $codes));
2178 $this->assertTrue(is_array($codes['sub']));
2179 $keys = array('subfoo', 'subbar', 'subbaz');
2180 $this->assertEquals($keys, array_keys($codes['sub']));
2183 public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
2185 $this->_checkZf2794();
2187 $this->testFullDataArrayUsedToValidateSubFormByDefault();
2188 $codes = $this->form
->getErrors('sub');
2189 $this->assertTrue(is_array($codes));
2190 $this->assertFalse(empty($codes));
2191 $keys = array('subfoo', 'subbar', 'subbaz');
2192 $this->assertEquals($keys, array_keys($codes));
2195 public function testGetErrorsHonorsElementsBelongTo()
2197 $this->_checkZf2794();
2199 $subForm = new Zend_Form_SubForm();
2200 $subForm->setElementsBelongTo('foo[bar]');
2201 $subForm->addElement('text', 'test')->test
2202 ->setRequired(true);
2204 $this->form
->addSubForm($subForm, 'sub');
2206 $data = array('foo' => array(
2212 $this->form
->isValid($data);
2213 $codes = $this->form
->getErrors();
2214 $this->assertFalse(empty($codes['foo']['bar']['test']));
2217 public function testErrorMessagesFromSubFormReturnedInSeparateArray()
2219 $this->_checkZf2794();
2221 $this->testFullDataArrayUsedToValidateSubFormByDefault();
2228 'subbaz' => '123-abc',
2230 $this->assertFalse($this->form
->isValid($data));
2232 $codes = $this->form
->getMessages();
2233 $this->assertTrue(array_key_exists('sub', $codes));
2234 $this->assertTrue(is_array($codes['sub']));
2235 $keys = array('subfoo', 'subbar', 'subbaz');
2236 $this->assertEquals($keys, array_keys($codes['sub']));
2239 public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
2241 $this->_checkZf2794();
2243 $this->testFullDataArrayUsedToValidateSubFormByDefault();
2250 'subbaz' => '123-abc',
2253 $this->assertFalse($this->form
->isValid($data));
2254 $codes = $this->form
->getMessages('sub');
2255 $this->assertTrue(is_array($codes));
2256 $this->assertFalse(empty($codes));
2257 $keys = array('subfoo', 'subbar', 'subbaz');
2258 $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
2261 public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
2263 $this->_checkZf2794();
2265 $translations = include dirname(__FILE__
) . '/_files/locale/array.php';
2266 $translate = new Zend_Translate('array', $translations, 'en');
2267 $translate->setLocale('en');
2269 $this->form
->addElements(array(
2274 'validators' => array('NotEmpty')
2281 'validators' => array('Digits')
2285 ->setTranslator($translate);
2291 if ($this->form
->isValid($data)) {
2292 $this->fail('Form should not validate');
2295 $messages = $this->form
->getMessages();
2296 $this->assertTrue(isset($messages['foo']));
2297 $this->assertTrue(isset($messages['bar']));
2299 foreach ($messages['foo'] as $key => $message) {
2300 if (array_key_exists($key, $translations)) {
2301 $this->assertEquals($translations[$key], $message);
2303 $this->fail('Translation for ' . $key . ' does not exist?');
2306 foreach ($messages['bar'] as $key => $message) {
2307 if (array_key_exists($key, $translations)) {
2308 $this->assertEquals($translations[$key], $message);
2310 $this->fail('Translation for ' . $key . ' does not exist?');
2315 public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
2317 $this->_checkZf2794();
2319 $translations = include dirname(__FILE__
) . '/_files/locale/array.php';
2320 $translate = new Zend_Translate('array', $translations, 'en');
2321 $translate->setLocale('en');
2323 $this->form
->addElements(array(
2328 'validators' => array('NotEmpty')
2335 'validators' => array('Digits')
2339 ->setTranslator($translate);
2344 if ($this->form
->isValidPartial($data)) {
2345 $this->fail('Form should not validate');
2348 $messages = $this->form
->getMessages();
2349 $this->assertTrue(isset($messages['foo']));
2350 $this->assertFalse(isset($messages['bar']));
2352 foreach ($messages['foo'] as $key => $message) {
2353 if (array_key_exists($key, $translations)) {
2354 $this->assertEquals($translations[$key], $message);
2356 $this->fail('Translation for ' . $key . ' does not exist?');
2361 public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
2363 $this->_checkZf2794();
2365 $translations = include dirname(__FILE__
) . '/_files/locale/array.php';
2366 $translate = new Zend_Translate('array', $translations, 'en');
2367 $translate->setLocale('en');
2369 $this->form
->addElements(array(
2374 'validators' => array('NotEmpty')
2381 'validators' => array('Digits')
2385 ->setTranslator($translate);
2390 $return = $this->form
->processAjax($data);
2391 $messages = Zend_Json
::decode($return);
2392 $this->assertTrue(is_array($messages));
2394 $this->assertTrue(isset($messages['foo']));
2395 $this->assertFalse(isset($messages['bar']));
2397 foreach ($messages['foo'] as $key => $message) {
2398 if (array_key_exists($key, $translations)) {
2399 $this->assertEquals($translations[$key], $message);
2401 $this->fail('Translation for ' . $key . ' does not exist?');
2409 public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
2411 $this->form
->addElement('text', 'foo', array('validators' => array('Alpha')));
2412 $this->form
->addErrorMessage('Invalid values entered');
2413 $this->assertFalse($this->form
->isValid(array('foo' => 123)));
2414 $messages = $this->form
->getMessages();
2415 $this->assertEquals(1, count($messages));
2416 $this->assertEquals('Invalid values entered', array_shift($messages));
2419 public function testCustomErrorMessagesShouldBeManagedInAStack()
2421 $this->form
->addElement('text', 'foo', array('validators' => array('Alpha')));
2422 $this->form
->addErrorMessage('Invalid values entered');
2423 $this->form
->addErrorMessage('Really, they are not valid');
2424 $messages = $this->form
->getErrorMessages();
2425 $this->assertEquals(2, count($messages));
2427 $this->assertFalse($this->form
->isValid(array('foo' => 123)));
2428 $messages = $this->form
->getMessages();
2429 $this->assertEquals(2, count($messages));
2430 $this->assertEquals('Invalid values entered', array_shift($messages));
2431 $this->assertEquals('Really, they are not valid', array_shift($messages));
2434 public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
2436 $set1 = array('foo', 'bar', 'baz');
2437 $this->form
->addErrorMessages($set1);
2438 $this->assertSame($set1, $this->form
->getErrorMessages());
2441 public function testSetErrorMessagesShouldOverwriteMessages()
2443 $set1 = array('foo', 'bar', 'baz');
2444 $set2 = array('bat', 'cat');
2445 $this->form
->addErrorMessages($set1);
2446 $this->assertSame($set1, $this->form
->getErrorMessages());
2447 $this->form
->setErrorMessages($set2);
2448 $this->assertSame($set2, $this->form
->getErrorMessages());
2451 public function testCustomErrorMessageStackShouldBeClearable()
2453 $this->testCustomErrorMessagesShouldBeManagedInAStack();
2454 $this->form
->clearErrorMessages();
2455 $messages = $this->form
->getErrorMessages();
2456 $this->assertTrue(empty($messages));
2459 public function testCustomErrorMessagesShouldBeTranslated()
2461 $translations = array(
2462 'foo' => 'Foo message',
2464 $translate = new Zend_Translate('array', $translations);
2465 $this->form
->addElement('text', 'foo', array('validators' => array('Alpha')));
2466 $this->form
->setTranslator($translate)
2467 ->addErrorMessage('foo');
2468 $this->assertFalse($this->form
->isValid(array('foo' => 123)));
2469 $messages = $this->form
->getMessages();
2470 $this->assertEquals(1, count($messages));
2471 $this->assertEquals('Foo message', array_shift($messages));
2474 public function testShouldAllowMarkingFormAsInvalid()
2476 $this->form
->addErrorMessage('Invalid values entered');
2477 $this->assertFalse($this->form
->isErrors());
2478 $this->form
->markAsError();
2479 $this->assertTrue($this->form
->isErrors());
2480 $messages = $this->form
->getMessages();
2481 $this->assertEquals(1, count($messages));
2482 $this->assertEquals('Invalid values entered', array_shift($messages));
2485 public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
2487 $this->assertFalse($this->form
->isErrors());
2488 $this->form
->setErrors(array('Error 1', 'Error 2'))
2489 ->addError('Error 3')
2490 ->addErrors(array('Error 4', 'Error 5'));
2491 $this->assertTrue($this->form
->isErrors());
2492 $messages = $this->form
->getMessages();
2493 $this->assertEquals(5, count($messages));
2494 foreach (range(1, 5) as $id) {
2495 $message = 'Error ' . $id;
2496 $this->assertContains($message, $messages);
2504 public function getView()
2506 $view = new Zend_View();
2507 $libPath = dirname(__FILE__
) . '/../../../library';
2508 $view->addHelperPath($libPath . '/Zend/View/Helper');
2512 public function testGetViewRetrievesFromViewRendererByDefault()
2514 $viewRenderer = Zend_Controller_Action_HelperBroker
::getStaticHelper('viewRenderer');
2515 $viewRenderer->initView();
2516 $view = $viewRenderer->view
;
2517 $test = $this->form
->getView();
2518 $this->assertSame($view, $test);
2521 public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
2523 $this->assertNull($this->form
->getView());
2526 public function testCanSetView()
2528 $view = new Zend_View();
2529 $this->assertNull($this->form
->getView());
2530 $this->form
->setView($view);
2531 $received = $this->form
->getView();
2532 $this->assertSame($view, $received);
2537 public function testFormDecoratorRegisteredByDefault()
2539 $this->_checkZf2794();
2541 $decorator = $this->form
->getDecorator('form');
2542 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form
);
2545 public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
2547 $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
2548 $decorators = $form->getDecorators();
2549 $this->assertEquals(array(), $decorators);
2552 public function testCanAddSingleDecoratorAsString()
2554 $this->_checkZf2794();
2556 $this->form
->clearDecorators();
2557 $this->assertFalse($this->form
->getDecorator('viewHelper'));
2559 $this->form
->addDecorator('viewHelper');
2560 $decorator = $this->form
->getDecorator('viewHelper');
2561 $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper
);
2564 public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
2566 $this->assertFalse($this->form
->getDecorator('Zend_Form_Decorator_Form'));
2569 public function testCanAddSingleDecoratorAsDecoratorObject()
2571 $this->form
->clearDecorators();
2572 $this->assertFalse($this->form
->getDecorator('viewHelper'));
2574 $decorator = new Zend_Form_Decorator_ViewHelper
;
2575 $this->form
->addDecorator($decorator);
2576 $test = $this->form
->getDecorator('Zend_Form_Decorator_ViewHelper');
2577 $this->assertSame($decorator, $test);
2580 public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
2582 $this->_checkZf2794();
2584 $this->form
->clearDecorators();
2585 $this->assertFalse($this->form
->getDecorator('viewHelper'));
2587 $decorator = new Zend_Form_Decorator_ViewHelper
;
2588 $this->form
->addDecorator($decorator);
2589 $test = $this->form
->getDecorator('viewHelper');
2590 $this->assertSame($decorator, $test);
2593 public function testCanAddMultipleDecorators()
2595 $this->_checkZf2794();
2597 $this->form
->clearDecorators();
2598 $this->assertFalse($this->form
->getDecorator('viewHelper'));
2600 $testDecorator = new Zend_Form_Decorator_Errors
;
2601 $this->form
->addDecorators(array(
2606 $viewHelper = $this->form
->getDecorator('viewHelper');
2607 $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper
);
2608 $decorator = $this->form
->getDecorator('errors');
2609 $this->assertSame($testDecorator, $decorator);
2612 public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
2614 $this->_checkZf2794();
2616 $this->assertFalse($this->form
->removeDecorator('foobar'));
2619 public function testCanRemoveDecorator()
2621 $this->_checkZf2794();
2623 $this->testFormDecoratorRegisteredByDefault();
2624 $this->form
->removeDecorator('form');
2625 $this->assertFalse($this->form
->getDecorator('form'));
2631 public function testRemovingNamedDecoratorShouldWork()
2633 $this->_checkZf2794();
2634 $this->form
->setDecorators(array(
2636 array(array('div' => 'HtmlTag'), array('tag' => 'div')),
2637 array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
2639 $decorators = $this->form
->getDecorators();
2640 $this->assertTrue(array_key_exists('div', $decorators));
2641 $this->assertTrue(array_key_exists('fieldset', $decorators));
2642 $this->form
->removeDecorator('div');
2643 $decorators = $this->form
->getDecorators();
2644 $this->assertFalse(array_key_exists('div', $decorators));
2645 $this->assertTrue(array_key_exists('fieldset', $decorators));
2648 public function testCanClearAllDecorators()
2650 $this->_checkZf2794();
2652 $this->testCanAddMultipleDecorators();
2653 $this->form
->clearDecorators();
2654 $this->assertFalse($this->form
->getDecorator('viewHelper'));
2655 $this->assertFalse($this->form
->getDecorator('fieldset'));
2658 public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
2660 $this->_checkZf2794();
2662 $this->form
->setDecorators(array(
2663 array('HtmlTag', array('tag' => 'div')),
2664 array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
2666 $decorator = $this->form
->getDecorator('FooBar');
2667 $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag
);
2668 $this->assertEquals('dd', $decorator->getOption('tag'));
2670 $decorator = $this->form
->getDecorator('HtmlTag');
2671 $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag
);
2672 $this->assertEquals('div', $decorator->getOption('tag'));
2675 public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
2677 $this->form
->setDecorators(array(
2679 array(array('div' => 'HtmlTag'), array('tag' => 'div')),
2680 array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
2684 $decorator = $this->form
->getDecorator('fieldset');
2685 $decorators = $this->form
->getDecorators();
2689 foreach (array_keys($decorators) as $name) {
2693 $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
2698 public function checkMarkup($html)
2700 $this->assertFalse(empty($html));
2701 $this->assertContains('<form', $html);
2702 $this->assertRegexp('/<form[^>]+action="' . $this->form
->getAction() . '"/', $html);
2703 $this->assertRegexp('/<form[^>]+method="' . $this->form
->getMethod() . '"/i', $html);
2704 $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
2705 $this->assertContains('</form>', $html);
2708 public function testRenderReturnsMarkup()
2710 $this->setupElements();
2711 $html = $this->form
->render($this->getView());
2712 $this->checkMarkup($html);
2715 public function testRenderReturnsMarkupRepresentingAllElements()
2717 $this->testRenderReturnsMarkup();
2718 $html = $this->form
->render();
2719 foreach ($this->form
->getElements() as $key => $element) {
2720 $this->assertFalse(empty($key));
2721 $this->assertFalse(is_numeric($key));
2722 $this->assertContains('<input', $html);
2723 $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
2727 public function testRenderReturnsMarkupContainingSubForms()
2729 $this->setupElements();
2730 $this->setupSubForm();
2731 $this->form
->setView($this->getView());
2732 $html = $this->form
->render();
2733 $this->assertRegexp('/<fieldset/', $html);
2734 $this->assertContains('</fieldset>', $html);
2735 foreach ($this->form
->sub
as $key => $item) {
2736 $this->assertFalse(empty($key));
2737 $this->assertFalse(is_numeric($key));
2738 $this->assertContains('<input', $html);
2739 $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
2740 $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
2744 public function testRenderReturnsMarkupContainingDisplayGroups()
2746 $this->setupElements();
2747 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
2748 $this->form
->setView($this->getView());
2749 $html = $this->html
= $this->form
->render();
2750 $this->assertRegexp('/<fieldset/', $html);
2751 $this->assertContains('</fieldset>', $html);
2752 $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
2753 $dom = new DOMDocument();
2754 $dom->loadHTML($html);
2755 $fieldsets = $dom->getElementsByTagName('fieldset');
2756 $this->assertTrue(0 < $fieldsets->length
);
2757 $fieldset = $fieldsets->item(0);
2758 $nodes = $fieldset->childNodes
;
2759 $this->assertNotNull($nodes);
2760 for ($i = 0; $i < $nodes->length
; ++
$i) {
2761 $node = $nodes->item($i);
2762 if ('input' != $node->nodeName
) {
2765 $this->assertTrue($node->hasAttribute('name'));
2766 $nameNode = $node->getAttributeNode('name');
2769 $this->assertEquals('foo', $nameNode->nodeValue
);
2772 $this->assertEquals('baz', $nameNode->nodeValue
);
2775 $this->fail('There should only be two input nodes in this display group: ' . $html);
2780 public function testRenderDoesNotRepeatElementsInDisplayGroups()
2782 $this->testRenderReturnsMarkupContainingDisplayGroups();
2783 if (!preg_match_all('#<input[^>]+name="foo"#', $this->html
, $matches)) {
2784 $this->fail("Should find foo element in rendered form");
2786 $this->assertEquals(1, count($matches));
2787 $this->assertEquals(1, count($matches[0]));
2790 public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
2792 $this->setupElements();
2793 $this->form
->setElementsBelongTo('anArray');
2794 $html = $this->form
->render($this->getView());
2795 $this->assertContains('name="anArray[foo]"', $html);
2796 $this->assertContains('name="anArray[bar]"', $html);
2797 $this->assertContains('name="anArray[baz]"', $html);
2798 $this->assertContains('id="anArray-foo"', $html);
2799 $this->assertContains('id="anArray-bar"', $html);
2800 $this->assertContains('id="anArray-baz"', $html);
2803 public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
2805 $this->setupElements();
2806 $this->form
->setElementsBelongTo('data[foo]');
2807 $html = $this->form
->render($this->getView());
2808 $this->assertContains('name="data[foo][foo]"', $html);
2809 $this->assertContains('name="data[foo][bar]"', $html);
2810 $this->assertContains('name="data[foo][baz]"', $html);
2811 $this->assertContains('id="data-foo-foo"', $html);
2812 $this->assertContains('id="data-foo-bar"', $html);
2813 $this->assertContains('id="data-foo-baz"', $html);
2816 public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
2818 $this->setupElements();
2819 $this->form
->setName('data')
2821 $html = $this->form
->render($this->getView());
2822 $this->assertContains('name="data[foo]"', $html);
2823 $this->assertContains('name="data[bar]"', $html);
2824 $this->assertContains('name="data[baz]"', $html);
2825 $this->assertContains('id="data-foo"', $html);
2826 $this->assertContains('id="data-bar"', $html);
2827 $this->assertContains('id="data-baz"', $html);
2830 public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
2832 $this->form
->setName('data')
2834 $subForm = new Zend_Form_SubForm();
2835 $subForm->setElementsBelongTo('billing[info]');
2836 $subForm->addElement('text', 'name');
2837 $subForm->addElement('text', 'number');
2838 $this->form
->addSubForm($subForm, 'sub');
2840 $html = $this->form
->render($this->getView());
2841 $this->assertContains('name="data[billing][info][name]', $html);
2842 $this->assertContains('name="data[billing][info][number]', $html);
2843 $this->assertContains('id="data-billing-info-name"', $html);
2844 $this->assertContains('id="data-billing-info-number"', $html);
2847 public function testToStringProxiesToRender()
2849 $this->setupElements();
2850 $this->form
->setView($this->getView());
2851 $html = $this->form
->__toString();
2852 $this->checkMarkup($html);
2855 public function raiseDecoratorException($content, $element, $options)
2857 throw new Exception('Raising exception in decorator callback');
2860 public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
2862 $this->error
= $errstr;
2865 public function testToStringRaisesErrorWhenExceptionCaught()
2867 $this->form
->setDecorators(array(
2869 'decorator' => 'Callback',
2870 'options' => array('callback' => array($this, 'raiseDecoratorException'))
2873 $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING
);
2875 $text = $this->form
->__toString();
2877 restore_error_handler();
2879 $this->assertTrue(empty($text));
2880 $this->assertTrue(isset($this->error
));
2881 $this->assertContains('Raising exception in decorator callback', $this->error
);
2887 public function testHiddenElementsGroupedWhenRendered()
2889 $this->markTestIncomplete('Scheduling for future release');
2890 $this->form
->addElements(array(
2891 array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
2892 array('type' => 'text', 'name' => 'testone'),
2893 array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
2894 array('type' => 'text', 'name' => 'testtwo'),
2895 array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
2896 array('type' => 'text', 'name' => 'testthree'),
2898 $html = $this->form
->render($this->getView());
2899 if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
2900 $this->fail('Hidden elements should be grouped');
2902 foreach (array('first', 'second', 'third') as $which) {
2903 $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
2904 $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
2910 public function testTranslatorIsNullByDefault()
2912 $this->assertNull($this->form
->getTranslator());
2915 public function testCanSetTranslator()
2917 require_once 'Zend/Translate/Adapter/Array.php';
2918 $translator = new Zend_Translate('array', array('foo' => 'bar'));
2919 $this->form
->setTranslator($translator);
2920 $received = $this->form
->getTranslator($translator);
2921 $this->assertSame($translator->getAdapter(), $received);
2924 public function testCanSetDefaultGlobalTranslator()
2926 $this->assertNull($this->form
->getTranslator());
2927 $translator = new Zend_Translate('array', array('foo' => 'bar'));
2928 Zend_Form
::setDefaultTranslator($translator);
2930 $received = Zend_Form
::getDefaultTranslator();
2931 $this->assertSame($translator->getAdapter(), $received);
2933 $received = $this->form
->getTranslator();
2934 $this->assertSame($translator->getAdapter(), $received);
2936 $form = new Zend_Form();
2937 $received = $form->getTranslator();
2938 $this->assertSame($translator->getAdapter(), $received);
2941 public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
2943 $this->assertNull($this->form
->getTranslator());
2944 $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
2945 Zend_Form
::setDefaultTranslator($translatorDefault);
2947 $received = $this->form
->getTranslator();
2948 $this->assertSame($translatorDefault->getAdapter(), $received);
2950 $translator = new Zend_Translate('array', array('foo' => 'bar'));
2951 $this->form
->setTranslator($translator);
2952 $received = $this->form
->getTranslator();
2953 $this->assertNotSame($translatorDefault->getAdapter(), $received);
2954 $this->assertSame($translator->getAdapter(), $received);
2957 public function testTranslatorFromRegistryUsedWhenNoneRegistered()
2959 $this->assertNull($this->form
->getTranslator());
2960 $translator = new Zend_Translate('array', array('foo' => 'bar'));
2961 Zend_Registry
::set('Zend_Translate', $translator);
2963 $received = Zend_Form
::getDefaultTranslator();
2964 $this->assertSame($translator->getAdapter(), $received);
2966 $received = $this->form
->getTranslator();
2967 $this->assertSame($translator->getAdapter(), $received);
2969 $form = new Zend_Form();
2970 $received = $form->getTranslator();
2971 $this->assertSame($translator->getAdapter(), $received);
2974 public function testCanDisableTranslation()
2976 $this->testCanSetDefaultGlobalTranslator();
2977 $this->form
->setDisableTranslator(true);
2978 $this->assertNull($this->form
->getTranslator());
2983 public function testFormObjectIsIterableAndIteratesElements()
2985 $this->setupElements();
2986 $expected = array('foo', 'bar', 'baz');
2987 $received = array();
2988 foreach ($this->form
as $key => $value) {
2991 $this->assertSame($expected, $received);
2994 public function testFormObjectIteratesElementsInExpectedOrder()
2996 $this->setupElements();
2997 $this->form
->addElement('text', 'checkorder', array('order' => 2));
2998 $expected = array('foo', 'bar', 'checkorder', 'baz');
2999 $received = array();
3000 foreach ($this->form
as $key => $value) {
3002 $this->assertTrue($value instanceof Zend_Form_Element
);
3004 $this->assertSame($expected, $received);
3007 public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
3009 $this->form
->addElement('submit', 'submit')->submit
->setLabel('Submit')->setOrder(30);
3010 $this->form
->addElement('text', 'name')->name
->setLabel('Name')->setOrder(10);
3011 $this->form
->addElement('text', 'email')->email
->setLabel('E-mail')->setOrder(20);
3013 $expected = array('name', 'email', 'submit');
3014 $received = array();
3015 foreach ($this->form
as $key => $value) {
3017 $this->assertTrue($value instanceof Zend_Form_Element
);
3019 $this->assertSame($expected, $received);
3022 public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
3024 $this->form
->addElement(new Zend_Form_Element('a',array('label'=>'a')))
3025 ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
3026 ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
3027 ->setView($this->getView());
3028 $test = $this->form
->render();
3029 $this->assertContains('name="a"', $test);
3030 if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
3031 $this->fail('Expected markup not found');
3034 foreach ($matches[1] as $element) {
3035 if (preg_match('/name="(a|b|c)"/', $element, $m)) {
3039 $this->assertSame(array('b', 'c', 'a'), $order);
3042 public function testFormObjectIteratesElementsAndSubforms()
3044 $this->setupElements();
3045 $this->setupSubForm();
3046 $expected = array('foo', 'bar', 'baz', 'sub');
3047 $received = array();
3048 foreach ($this->form
as $key => $value) {
3050 $this->assertTrue(($value instanceof Zend_Form_Element
)
3051 or ($value instanceof Zend_Form_SubForm
));
3053 $this->assertSame($expected, $received);
3056 public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
3058 $this->setupElements();
3059 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz');
3060 $expected = array('bar', 'foobaz');
3061 $received = array();
3062 foreach ($this->form
as $key => $value) {
3064 $this->assertTrue(($value instanceof Zend_Form_Element
)
3065 or ($value instanceof Zend_Form_DisplayGroup
));
3067 $this->assertSame($expected, $received);
3070 public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
3072 $this->setupElements();
3073 $bar = $this->form
->bar
;
3074 $this->form
->removeElement('bar');
3077 foreach ($this->form
as $item) {
3079 } catch (Exception
$e) {
3080 $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
3083 $this->form
->addElement($bar);
3084 $this->form
->addDisplayGroup(array('baz', 'bar'), 'bazbar');
3085 $this->form
->removeDisplayGroup('bazbar');
3088 foreach ($this->form
as $item) {
3090 } catch (Exception
$e) {
3091 $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
3094 $subForm = new Zend_Form_SubForm
;
3095 $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
3096 $this->form
->addSubForm($subForm, 'page1');
3097 $this->form
->removeSubForm('page1');
3100 foreach ($this->form
as $item) {
3102 } catch (Exception
$e) {
3103 $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
3107 public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
3109 $form = new Zend_Form();
3110 $form->addElements(array(
3111 'username' => 'text',
3112 'password' => 'text',
3114 $form->clearElements();
3117 foreach ($form as $item) {
3119 } catch (Zend_Form_Exception
$e) {
3120 $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
3122 $this->fail($message);
3125 $form->addElements(array(
3126 'username' => 'text',
3127 'password' => 'text',
3129 ->addDisplayGroup(array('username', 'password'), 'login');
3130 $form->clearDisplayGroups();
3133 foreach ($form as $item) {
3135 } catch (Zend_Form_Exception
$e) {
3136 $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
3138 $this->fail($message);
3141 $subForm = new Zend_Form_SubForm();
3142 $form->addSubForm($subForm, 'foo');
3143 $form->clearSubForms();
3146 foreach ($form as $item) {
3148 } catch (Zend_Form_Exception
$e) {
3149 $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
3151 $this->fail($message);
3157 public function testCanCountFormObject()
3159 $this->setupElements();
3160 $this->assertEquals(3, count($this->form
));
3163 public function testCountingFormObjectCountsSubForms()
3165 $this->setupElements();
3166 $this->setupSubForm();
3167 $this->assertEquals(4, count($this->form
));
3170 public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
3172 $this->testCountingFormObjectCountsSubForms();
3173 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz');
3174 $this->assertEquals(3, count($this->form
));
3177 // Element decorators and plugin paths
3179 public function testCanSetAllElementDecoratorsAtOnce()
3181 $this->_checkZf2794();
3183 $this->setupElements();
3184 $this->form
->setElementDecorators(array(
3185 array('ViewHelper'),
3189 foreach ($this->form
->getElements() as $element) {
3190 $this->assertFalse($element->getDecorator('Errors'));
3191 $this->assertFalse($element->getDecorator('HtmlTag'));
3192 $decorator = $element->getDecorator('ViewHelper');
3193 $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper
);
3194 $decorator = $element->getDecorator('Label');
3195 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label
);
3196 $decorator = $element->getDecorator('Fieldset');
3197 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset
);
3204 public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
3206 $this->form
->setDecorators(array(
3208 array('HtmlTag', array('tag' => 'table')),
3211 $this->form
->addElementPrefixPath('My_Decorator', dirname(__FILE__
) . '/_files/decorators/', 'decorator');
3212 $this->form
->addElement('text', 'test', array(
3214 'description' => 'sample description',
3217 require_once dirname(__FILE__
) . '/_files/decorators/TableRow.php';
3218 $decorator = new My_Decorator_TableRow();
3219 $this->form
->setElementDecorators(array(
3223 $html = $this->form
->render($this->getView());
3224 $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
3230 public function testShouldAllowSpecifyingSpecificElementsToDecorate()
3232 $this->_checkZf2794();
3234 $this->setupElements();
3235 $this->form
->setElementDecorators(
3246 $element = $this->form
->bar
;
3247 $this->assertFalse($element->getDecorator('ViewHelper'));
3248 $this->assertFalse($element->getDecorator('Errors'));
3249 $this->assertFalse($element->getDecorator('Label'));
3250 $this->assertFalse($element->getDecorator('HtmlTag'));
3251 $decorator = $element->getDecorator('Description');
3252 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description
);
3253 $decorator = $element->getDecorator('Form');
3254 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form
);
3255 $decorator = $element->getDecorator('Fieldset');
3256 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset
);
3258 foreach (array('foo', 'baz') as $name) {
3259 $element = $this->form
->$name;
3260 $this->assertFalse($element->getDecorator('Form'));
3261 $this->assertFalse($element->getDecorator('Fieldset'));
3265 public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
3267 $this->_checkZf2794();
3269 $this->setupElements();
3270 $this->form
->setElementDecorators(
3283 $element = $this->form
->bar
;
3284 $this->assertFalse($element->getDecorator('ViewHelper'));
3285 $this->assertFalse($element->getDecorator('Errors'));
3286 $this->assertFalse($element->getDecorator('Label'));
3287 $this->assertFalse($element->getDecorator('HtmlTag'));
3288 $decorator = $element->getDecorator('Description');
3289 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description
);
3290 $decorator = $element->getDecorator('Form');
3291 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form
);
3292 $decorator = $element->getDecorator('Fieldset');
3293 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset
);
3295 foreach (array('foo', 'baz') as $name) {
3296 $element = $this->form
->$name;
3297 $this->assertFalse($element->getDecorator('Form'));
3298 $this->assertFalse($element->getDecorator('Fieldset'));
3303 public function testCanSetAllElementFiltersAtOnce()
3305 $this->_checkZf2794();
3307 $this->setupElements();
3308 $this->form
->setElementFilters(array(
3312 foreach ($this->form
->getElements() as $element) {
3313 $filter = $element->getFilter('Alnum');
3314 $this->assertTrue($filter instanceof Zend_Filter_Alnum
);
3315 $filter = $element->getFilter('StringToLower');
3316 $this->assertTrue($filter instanceof Zend_Filter_StringToLower
);
3320 public function testCanSetGlobalElementPrefixPath()
3322 $this->setupElements();
3323 $this->form
->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
3324 $this->form
->addElement('text', 'prefixTest');
3325 foreach ($this->form
->getElements() as $element) {
3326 $loader = $element->getPluginLoader('validate');
3327 $paths = $loader->getPaths('Zend_Foo_Validate');
3328 $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
3329 $this->assertContains('Foo', $paths[0]);
3330 $this->assertContains('Validate', $paths[0]);
3332 $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
3333 $this->assertFalse(empty($paths));
3334 $this->assertContains('Foo', $paths[0]);
3335 $this->assertContains('Filter', $paths[0]);
3337 $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
3338 $this->assertFalse(empty($paths));
3339 $this->assertContains('Foo', $paths[0]);
3340 $this->assertContains('Decorator', $paths[0]);
3344 public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
3346 $this->_checkZf2794();
3348 $this->form
->addElementPrefixPath('My_Decorator', dirname(__FILE__
) . '/_files/decorators', 'decorator');
3349 $this->form
->addElement('text', 'prefixTest');
3350 $element = $this->form
->prefixTest
;
3351 $label = $element->getDecorator('Label');
3352 $this->assertTrue($label instanceof My_Decorator_Label
, get_class($label));
3358 public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
3360 $subForm = new Zend_Form_SubForm();
3361 $subForm->addElement('text', 'foo');
3362 $this->form
->addSubForm($subForm, 'subForm');
3363 $this->form
->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
3364 $loader = $this->form
->subForm
->foo
->getPluginLoader('decorator');
3365 $paths = $loader->getPaths('Zend_Foo_Decorator');
3366 $this->assertFalse(empty($paths));
3367 $this->assertContains('Foo', $paths[0]);
3368 $this->assertContains('Decorator', $paths[0]);
3371 public function testCanSetElementValidatorPrefixPath()
3373 $this->setupElements();
3374 $this->form
->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
3375 $this->form
->addElement('text', 'prefixTest');
3376 foreach ($this->form
->getElements() as $element) {
3377 $loader = $element->getPluginLoader('validate');
3378 $paths = $loader->getPaths('Zend_Foo');
3379 $this->assertFalse(empty($paths));
3380 $this->assertContains('Foo', $paths[0]);
3381 $this->assertNotContains('Validate', $paths[0]);
3385 public function testCanSetElementFilterPrefixPath()
3387 $this->setupElements();
3388 $this->form
->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
3389 $this->form
->addElement('text', 'prefixTest');
3390 foreach ($this->form
->getElements() as $element) {
3391 $loader = $element->getPluginLoader('filter');
3392 $paths = $loader->getPaths('Zend_Foo');
3393 $this->assertFalse(empty($paths));
3394 $this->assertContains('Foo', $paths[0]);
3395 $this->assertNotContains('Filter', $paths[0]);
3399 public function testCanSetElementDecoratorPrefixPath()
3401 $this->setupElements();
3402 $this->form
->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
3403 $this->form
->addElement('text', 'prefixTest');
3404 foreach ($this->form
->getElements() as $element) {
3405 $loader = $element->getPluginLoader('decorator');
3406 $paths = $loader->getPaths('Zend_Foo');
3407 $this->assertFalse(empty($paths));
3408 $this->assertContains('Foo', $paths[0]);
3409 $this->assertNotContains('Decorator', $paths[0]);
3413 // Display Group decorators and plugin paths
3415 public function setupDisplayGroups()
3417 $this->testCanAddAndRetrieveMultipleElements();
3418 $this->form
->addElements(array(
3424 $this->form
->addDisplayGroup(array('bar', 'bat'), 'barbat');
3425 $this->form
->addDisplayGroup(array('foo', 'baz'), 'foobaz');
3428 public function testCanSetAllDisplayGroupDecoratorsAtOnce()
3430 $this->_checkZf2794();
3432 $this->setupDisplayGroups();
3433 $this->form
->setDisplayGroupDecorators(array(
3434 array('Callback', array('callback' => 'strip_tags')),
3436 foreach ($this->form
->getDisplayGroups() as $element) {
3437 $this->assertFalse($element->getDecorator('FormElements'));
3438 $this->assertFalse($element->getDecorator('HtmlTag'));
3439 $this->assertFalse($element->getDecorator('Fieldset'));
3440 $this->assertFalse($element->getDecorator('DtDdWrapper'));
3442 $decorator = $element->getDecorator('Callback');
3443 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback
);
3447 public function testCanSetDisplayGroupPrefixPath()
3449 $this->setupDisplayGroups();
3450 $this->form
->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
3451 $this->form
->addDisplayGroup(array('test1', 'test2'), 'testgroup');
3452 foreach ($this->form
->getDisplayGroups() as $group) {
3453 $loader = $group->getPluginLoader();
3454 $paths = $loader->getPaths('Zend_Foo');
3455 $this->assertFalse(empty($paths));
3456 $this->assertContains('Foo', $paths[0]);
3463 public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
3465 require_once 'Zend/Config/Ini.php';
3466 $config = new Zend_Config_Ini(dirname(__FILE__
) . '/_files/config/zf3213.ini', 'form');
3467 $form = new Zend_Form($config);
3468 $dg = $form->foofoo
;
3469 $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
3470 $this->assertTrue($paths !== false);
3473 // Subform decorators
3475 public function testCanSetAllSubFormDecoratorsAtOnce()
3477 $this->_checkZf2794();
3479 $this->setupSubForm();
3480 $this->form
->setSubFormDecorators(array(
3481 array('Callback', array('callback' => 'strip_tags')),
3483 foreach ($this->form
->getSubForms() as $subForm) {
3484 $this->assertFalse($subForm->getDecorator('FormElements'));
3485 $this->assertFalse($subForm->getDecorator('HtmlTag'));
3486 $this->assertFalse($subForm->getDecorator('Fieldset'));
3487 $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
3489 $decorator = $subForm->getDecorator('Callback');
3490 $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback
);
3496 public function testInitCalledPriorToLoadingDefaultDecorators()
3498 $form = new Zend_Form_FormTest_FormExtension();
3499 $decorators = $form->getDecorators();
3500 $this->assertTrue(empty($decorators));
3508 public function testCloningShouldCloneAllChildren()
3510 $form = new Zend_Form();
3511 $foo = new Zend_Form_SubForm(array(
3513 'elements' => array(
3518 $form->addElement('text', 'bar')
3519 ->addElement('text', 'baz')
3520 ->addElement('text', 'bat')
3521 ->addDisplayGroup(array('bar', 'bat'), 'barbat')
3522 ->addSubForm($foo, 'foo');
3526 $barbat = $form->barbat
;
3528 $cloned = clone $form;
3529 $this->assertNotSame($foo, $cloned->foo
);
3530 $this->assertNotSame($bar, $cloned->bar
);
3531 $this->assertNotSame($baz, $cloned->baz
);
3532 $this->assertNotSame($bat, $cloned->bat
);
3533 $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
3534 $this->assertNotSame($foo->one
, $cloned->foo
->one
);
3535 $this->assertNotSame($foo->two
, $cloned->foo
->two
);
3543 public function testFormsShouldAllowResetting()
3545 $form = new Zend_Form();
3546 $foo = new Zend_Form_SubForm(array(
3548 'elements' => array(
3553 $form->addElement('text', 'bar')
3554 ->addElement('text', 'baz')
3555 ->addElement('text', 'bat')
3556 ->addDisplayGroup(array('bar', 'bat'), 'barbat')
3557 ->addSubForm($foo, 'foo');
3559 'bar' => 'Bar Value',
3560 'baz' => 'Baz Value',
3561 'bat' => 'Bat Value',
3563 'one' => 'One Value',
3564 'two' => 'Two Value',
3567 $form->populate($values);
3568 $test = $form->getValues();
3569 $this->assertEquals($values, $test);
3571 $test = $form->getValues();
3572 $this->assertNotEquals($values, $test);
3573 $this->assertEquals(0, array_sum($test));
3579 public function testFormShouldOverloadToRenderDecorators()
3581 $this->setupElements();
3582 $this->form
->setView($this->getView());
3583 $html = $this->form
->renderFormElements();
3584 foreach ($this->form
->getElements() as $element) {
3585 $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
3587 $this->assertNotContains('<dl', $html);
3588 $this->assertNotContains('<form', $html);
3590 $html = $this->form
->renderForm('this is the content');
3591 $this->assertContains('<form', $html);
3592 $this->assertContains('</form>', $html);
3593 $this->assertContains('this is the content', $html);
3598 * @expectedException Zend_Form_Exception
3600 public function testOverloadingToInvalidMethodsShouldThrowAnException()
3602 $html = $this->form
->bogusMethodCall();
3608 public function testDtDdElementsWithLabelGetUniqueId()
3610 $form = new Zend_Form();
3611 $form->setView($this->getView());
3613 $fooElement = new Zend_Form_Element_Text('foo');
3614 $fooElement->setLabel('Foo');
3616 $form->addElement($fooElement);
3618 $html = $form->render();
3620 $this->assertContains('<dt id="foo-label">', $html);
3621 $this->assertContains('<dd id="foo-element">', $html);
3627 public function testDtDdElementsWithoutLabelGetUniqueId()
3629 $form = new Zend_Form();
3630 $form->setView($this->getView())
3631 ->addElement(new Zend_Form_Element_Text('foo'));
3633 $html = $form->render();
3635 $this->assertContains('<dt id="foo-label"> </dt>', $html);
3636 $this->assertContains('<dd id="foo-element">', $html);
3642 public function testSubFormGetsUniqueIdWithName()
3644 $form = new Zend_Form();
3645 $form->setView($this->getView())
3646 ->setName('testform')
3647 ->addSubForm(new Zend_Form_SubForm(), 'testform');
3649 $html = $form->render();
3651 $this->assertContains('<dt id="testform-label"> </dt>', $html);
3652 $this->assertContains('<dd id="testform-element">', $html);
3658 public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
3660 $file = new Zend_Form_Element_File('txt');
3661 $this->form
->addElement($file);
3663 $html = $this->form
->render($this->getView());
3664 $this->assertFalse(empty($html));
3665 $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
3671 public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
3673 $subForm = new Zend_Form_SubForm();
3674 $subForm->addElement('file', 'txt');
3675 $this->form
->addSubForm($subForm, 'page1')
3676 ->setView(new Zend_View
);
3677 $html = $this->form
->render();
3679 $this->assertContains('id="txt"', $html);
3680 $this->assertContains('name="txt"', $html);
3681 $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
3687 public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
3689 $this->form
->addElement('file', 'txt')
3690 ->addDisplayGroup(array('txt'), 'txtdisplay')
3691 ->setView(new Zend_View
);
3692 $html = $this->form
->render();
3694 $this->assertContains('id="txt"', $html);
3695 $this->assertContains('name="txt"', $html);
3696 $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
3702 public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
3704 $this->form
->setOptions(array(
3705 'elementDecorators' => array(
3709 'elements' => array(
3713 'decorators' => array(
3721 $element = $this->form
->getElement('foo');
3722 $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
3724 foreach ($element->getDecorators() as $decorator) {
3725 $actual[] = get_class($decorator);
3727 $this->assertSame($expected, $actual);
3733 public function testIsValidShouldFailIfAddErrorHasBeenCalled()
3735 $this->form
->addError('Error');
3736 $this->assertFalse($this->form
->isValid(array()));
3740 * Used by test methods susceptible to ZF-2794, marks a test as incomplete
3742 * @link http://framework.zend.com/issues/browse/ZF-2794
3745 protected function _checkZf2794()
3747 if (strtolower(substr(PHP_OS
, 0, 3)) == 'win' && version_compare(PHP_VERSION
, '5.1.4', '=')) {
3748 $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
3753 class Zend_Form_FormTest_DisplayGroup
extends Zend_Form_DisplayGroup
3757 class Zend_Form_FormTest_FormExtension
extends Zend_Form
3759 public function init()
3761 $this->setDisableLoadDefaultDecorators(true);
3765 if (PHPUnit_MAIN_METHOD
== 'Zend_Form_FormTest::main') {
3766 Zend_Form_FormTest
::main();