PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Form/FormTest.php

https://bitbucket.org/ksekar/campus
PHP | 4586 lines | 3757 code | 613 blank | 216 comment | 31 complexity | 880fcb1b5dd37cd1df3f4daa4e24e202 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FormTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Form_FormTest::main');
  24. }
  25. require_once 'Zend/Form.php';
  26. require_once 'Zend/Config.php';
  27. require_once 'Zend/Controller/Action/HelperBroker.php';
  28. require_once 'Zend/Form/Decorator/Form.php';
  29. require_once 'Zend/Form/DisplayGroup.php';
  30. require_once 'Zend/Form/Element.php';
  31. require_once 'Zend/Form/Element/Text.php';
  32. require_once 'Zend/Form/Element/File.php';
  33. require_once 'Zend/Form/SubForm.php';
  34. require_once 'Zend/Loader/PluginLoader.php';
  35. require_once 'Zend/Registry.php';
  36. require_once 'Zend/Translate.php';
  37. require_once 'Zend/View.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Form
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Form
  45. */
  46. class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * @var Zend_Form
  50. */
  51. public $form;
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_FormTest');
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. public function clearRegistry()
  58. {
  59. if (Zend_Registry::isRegistered('Zend_Translate')) {
  60. $registry = Zend_Registry::getInstance();
  61. unset($registry['Zend_Translate']);
  62. }
  63. }
  64. public function setUp()
  65. {
  66. $this->clearRegistry();
  67. Zend_Form::setDefaultTranslator(null);
  68. if (isset($this->error)) {
  69. unset($this->error);
  70. }
  71. Zend_Controller_Action_HelperBroker::resetHelpers();
  72. $this->form = new Zend_Form();
  73. }
  74. public function tearDown()
  75. {
  76. $this->clearRegistry();
  77. }
  78. public function testZendFormImplementsZendValidateInterface()
  79. {
  80. $this->assertTrue($this->form instanceof Zend_Validate_Interface);
  81. }
  82. // Configuration
  83. public function getOptions()
  84. {
  85. $options = array(
  86. 'name' => 'foo',
  87. 'class' => 'someform',
  88. 'action' => '/foo/bar',
  89. 'method' => 'put',
  90. );
  91. return $options;
  92. }
  93. public function testCanSetObjectStateViaSetOptions()
  94. {
  95. $options = $this->getOptions();
  96. $this->form->setOptions($options);
  97. $this->assertEquals('foo', $this->form->getName());
  98. $this->assertEquals('someform', $this->form->getAttrib('class'));
  99. $this->assertEquals('/foo/bar', $this->form->getAction());
  100. $this->assertEquals('put', $this->form->getMethod());
  101. }
  102. public function testCanSetObjectStateByPassingOptionsToConstructor()
  103. {
  104. $options = $this->getOptions();
  105. $form = new Zend_Form($options);
  106. $this->assertEquals('foo', $form->getName());
  107. $this->assertEquals('someform', $form->getAttrib('class'));
  108. $this->assertEquals('/foo/bar', $form->getAction());
  109. $this->assertEquals('put', $form->getMethod());
  110. }
  111. public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
  112. {
  113. $options = $this->getOptions();
  114. $config = new Zend_Config($options);
  115. $options['config'] = $config;
  116. $options['options'] = $config->toArray();
  117. $this->form->setOptions($options);
  118. }
  119. public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNonObjectPassed()
  120. {
  121. $options = $this->getOptions();
  122. $options['pluginLoader'] = true;
  123. $options['subForms'] = true;
  124. $options['view'] = true;
  125. $options['translator'] = true;
  126. $options['default'] = true;
  127. $options['attrib'] = true;
  128. $this->form->setOptions($options);
  129. }
  130. public function testSetOptionsWithAttribsDoesNotOverwriteActionOrMethodOrName()
  131. {
  132. $attribs = $this->getOptions();
  133. unset($attribs['action'], $attribs['method']);
  134. $options = array(
  135. 'name' => 'MYFORM',
  136. 'action' => '/bar/baz',
  137. 'method' => 'GET',
  138. 'attribs' => $attribs,
  139. );
  140. $form = new Zend_Form($options);
  141. $this->assertEquals($options['name'], $form->getName());
  142. $this->assertEquals($options['action'], $form->getAction());
  143. $this->assertEquals(strtolower($options['method']), strtolower($form->getMethod()));
  144. }
  145. public function getElementOptions()
  146. {
  147. $elements = array(
  148. 'foo' => 'text',
  149. array('text', 'bar', array('class' => 'foobar')),
  150. array(
  151. 'options' => array('class' => 'barbaz'),
  152. 'type' => 'text',
  153. 'name' => 'baz',
  154. ),
  155. 'bat' => array(
  156. 'options' => array('class' => 'bazbat'),
  157. 'type' => 'text',
  158. ),
  159. 'lol' => array(
  160. 'text',
  161. array('class' => 'lolcat'),
  162. )
  163. );
  164. return $elements;
  165. }
  166. public function testSetOptionsSetsElements()
  167. {
  168. $options = $this->getOptions();
  169. $options['elements'] = $this->getElementOptions();
  170. $this->form->setOptions($options);
  171. $this->assertTrue(isset($this->form->foo));
  172. $this->assertTrue($this->form->foo instanceof Zend_Form_Element_Text);
  173. $this->assertTrue(isset($this->form->bar));
  174. $this->assertTrue($this->form->bar instanceof Zend_Form_Element_Text);
  175. $this->assertEquals('foobar', $this->form->bar->class);
  176. $this->assertTrue(isset($this->form->baz));
  177. $this->assertTrue($this->form->baz instanceof Zend_Form_Element_Text);
  178. $this->assertEquals('barbaz', $this->form->baz->class);
  179. $this->assertTrue(isset($this->form->bat));
  180. $this->assertTrue($this->form->bat instanceof Zend_Form_Element_Text);
  181. $this->assertEquals('bazbat', $this->form->bat->class);
  182. $this->assertTrue(isset($this->form->lol));
  183. $this->assertTrue($this->form->lol instanceof Zend_Form_Element_Text);
  184. $this->assertEquals('lolcat', $this->form->lol->class);
  185. }
  186. public function testSetOptionsSetsDefaultValues()
  187. {
  188. $options = $this->getOptions();
  189. $options['defaults'] = array(
  190. 'bar' => 'barvalue',
  191. 'bat' => 'batvalue',
  192. );
  193. $options['elements'] = $this->getElementOptions();
  194. $this->form->setOptions($options);
  195. $this->assertEquals('barvalue', $this->form->bar->getValue());
  196. $this->assertEquals('batvalue', $this->form->bat->getValue());
  197. }
  198. public function testSetOptionsSetsArrayOfStringDecorators()
  199. {
  200. $this->_checkZf2794();
  201. $options = $this->getOptions();
  202. $options['decorators'] = array('label', 'errors');
  203. $this->form->setOptions($options);
  204. $this->assertFalse($this->form->getDecorator('form'));
  205. $decorator = $this->form->getDecorator('label');
  206. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  207. $decorator = $this->form->getDecorator('errors');
  208. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  209. }
  210. public function testSetOptionsSetsArrayOfArrayDecorators()
  211. {
  212. $this->_checkZf2794();
  213. $options = $this->getOptions();
  214. $options['decorators'] = array(
  215. array('label', array('id' => 'mylabel')),
  216. array('errors', array('id' => 'errors')),
  217. );
  218. $this->form->setOptions($options);
  219. $this->assertFalse($this->form->getDecorator('form'));
  220. $decorator = $this->form->getDecorator('label');
  221. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  222. $options = $decorator->getOptions();
  223. $this->assertEquals('mylabel', $options['id']);
  224. $decorator = $this->form->getDecorator('errors');
  225. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  226. $options = $decorator->getOptions();
  227. $this->assertEquals('errors', $options['id']);
  228. }
  229. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  230. {
  231. $this->_checkZf2794();
  232. $options = $this->getOptions();
  233. $options['decorators'] = array(
  234. array(
  235. 'options' => array('id' => 'mylabel'),
  236. 'decorator' => 'label',
  237. ),
  238. array(
  239. 'options' => array('id' => 'errors'),
  240. 'decorator' => 'errors',
  241. ),
  242. );
  243. $this->form->setOptions($options);
  244. $this->assertFalse($this->form->getDecorator('form'));
  245. $decorator = $this->form->getDecorator('label');
  246. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  247. $options = $decorator->getOptions();
  248. $this->assertEquals('mylabel', $options['id']);
  249. $decorator = $this->form->getDecorator('errors');
  250. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  251. $options = $decorator->getOptions();
  252. $this->assertEquals('errors', $options['id']);
  253. }
  254. public function testSetOptionsSetsGlobalPrefixPaths()
  255. {
  256. $options = $this->getOptions();
  257. $options['prefixPath'] = array(
  258. 'prefix' => 'Zend_Foo',
  259. 'path' => 'Zend/Foo/'
  260. );
  261. $this->form->setOptions($options);
  262. foreach (array('element', 'decorator') as $type) {
  263. $loader = $this->form->getPluginLoader($type);
  264. $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
  265. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  266. $this->assertFalse(empty($paths));
  267. $this->assertContains('Foo', $paths[0]);
  268. }
  269. }
  270. public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
  271. {
  272. $options = $this->getOptions();
  273. $options['prefixPath'] = array(
  274. 'element' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  275. );
  276. $this->form->setOptions($options);
  277. $loader = $this->form->getPluginLoader('element');
  278. $paths = $loader->getPaths('Zend_Foo');
  279. $this->assertTrue(is_array($paths));
  280. $this->assertFalse(empty($paths));
  281. $this->assertContains('Foo', $paths[0]);
  282. }
  283. public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
  284. {
  285. $options = $this->getOptions();
  286. $options['prefixPath'] = array(
  287. array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  288. );
  289. $this->form->setOptions($options);
  290. $loader = $this->form->getPluginLoader('decorator');
  291. $paths = $loader->getPaths('Zend_Foo');
  292. $this->assertTrue(is_array($paths));
  293. $this->assertFalse(empty($paths));
  294. $this->assertContains('Foo', $paths[0]);
  295. }
  296. public function testSetOptionsSetsDisplayGroups()
  297. {
  298. $options = $this->getOptions();
  299. $options['displayGroups'] = array(
  300. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  301. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  302. array(
  303. 'name' => 'ghiabc',
  304. 'elements' => array('ghi', 'abc'),
  305. 'options' => array('order' => 15),
  306. ),
  307. );
  308. $options['elements'] = array(
  309. 'foo' => 'text',
  310. 'bar' => 'text',
  311. 'baz' => 'text',
  312. 'bat' => 'text',
  313. 'abc' => 'text',
  314. 'ghi' => 'text',
  315. 'jkl' => 'text',
  316. 'mno' => 'text',
  317. );
  318. $this->form->setOptions($options);
  319. $this->assertTrue(isset($this->form->barbat));
  320. $elements = $this->form->barbat->getElements();
  321. $expected = array('bar', 'bat');
  322. $this->assertEquals($expected, array_keys($elements));
  323. $this->assertEquals(20, $this->form->barbat->getOrder());
  324. $this->assertTrue(isset($this->form->foobaz));
  325. $elements = $this->form->foobaz->getElements();
  326. $expected = array('foo', 'baz');
  327. $this->assertEquals($expected, array_keys($elements));
  328. $this->assertEquals(10, $this->form->foobaz->getOrder());
  329. $this->assertTrue(isset($this->form->ghiabc));
  330. $elements = $this->form->ghiabc->getElements();
  331. $expected = array('ghi', 'abc');
  332. $this->assertEquals($expected, array_keys($elements));
  333. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  334. }
  335. /**
  336. * @group ZF-3250
  337. */
  338. public function testDisplayGroupOrderInConfigShouldNotMatter()
  339. {
  340. require_once 'Zend/Config/Xml.php';
  341. $config = new Zend_Config_Xml(dirname(__FILE__) . '/_files/config/zf3250.xml', 'sitearea', true);
  342. $form = new Zend_Form($config->test);
  343. // no assertions needed; throws error if order matters
  344. }
  345. /**
  346. * @group ZF-3112
  347. */
  348. public function testSetOptionsShouldCreateDisplayGroupsLast()
  349. {
  350. $options = array();
  351. $options['displayGroups'] = array(
  352. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  353. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  354. array(
  355. 'name' => 'ghiabc',
  356. 'elements' => array('ghi', 'abc'),
  357. 'options' => array('order' => 15),
  358. ),
  359. );
  360. $options = array_merge($options, $this->getOptions());
  361. $options['elements'] = array(
  362. 'foo' => 'text',
  363. 'bar' => 'text',
  364. 'baz' => 'text',
  365. 'bat' => 'text',
  366. 'abc' => 'text',
  367. 'ghi' => 'text',
  368. 'jkl' => 'text',
  369. 'mno' => 'text',
  370. );
  371. $this->form = new Zend_Form($options);
  372. $this->assertTrue(isset($this->form->barbat));
  373. $elements = $this->form->barbat->getElements();
  374. $expected = array('bar', 'bat');
  375. $this->assertEquals($expected, array_keys($elements));
  376. $this->assertEquals(20, $this->form->barbat->getOrder());
  377. $this->assertTrue(isset($this->form->foobaz));
  378. $elements = $this->form->foobaz->getElements();
  379. $expected = array('foo', 'baz');
  380. $this->assertEquals($expected, array_keys($elements));
  381. $this->assertEquals(10, $this->form->foobaz->getOrder());
  382. $this->assertTrue(isset($this->form->ghiabc));
  383. $elements = $this->form->ghiabc->getElements();
  384. $expected = array('ghi', 'abc');
  385. $this->assertEquals($expected, array_keys($elements));
  386. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  387. }
  388. public function testSetConfigSetsObjectState()
  389. {
  390. $config = new Zend_Config($this->getOptions());
  391. $this->form->setConfig($config);
  392. $this->assertEquals('foo', $this->form->getName());
  393. $this->assertEquals('someform', $this->form->getAttrib('class'));
  394. $this->assertEquals('/foo/bar', $this->form->getAction());
  395. $this->assertEquals('put', $this->form->getMethod());
  396. }
  397. public function testCanSetObjectStateByPassingConfigObjectToConstructor()
  398. {
  399. $config = new Zend_Config($this->getOptions());
  400. $form = new Zend_Form($config);
  401. $this->assertEquals('foo', $form->getName());
  402. $this->assertEquals('someform', $form->getAttrib('class'));
  403. $this->assertEquals('/foo/bar', $form->getAction());
  404. $this->assertEquals('put', $form->getMethod());
  405. }
  406. // Attribs:
  407. public function testAttribsArrayInitiallyEmpty()
  408. {
  409. $attribs = $this->form->getAttribs();
  410. $this->assertTrue(is_array($attribs));
  411. $this->assertTrue(empty($attribs));
  412. }
  413. public function testRetrievingUndefinedAttribReturnsNull()
  414. {
  415. $this->assertNull($this->form->getAttrib('foo'));
  416. }
  417. public function testCanAddAndRetrieveSingleAttribs()
  418. {
  419. $this->testRetrievingUndefinedAttribReturnsNull();
  420. $this->form->setAttrib('foo', 'bar');
  421. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  422. }
  423. public function testCanAddAndRetrieveMultipleAttribs()
  424. {
  425. $this->form->setAttrib('foo', 'bar');
  426. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  427. $this->form->addAttribs(array(
  428. 'bar' => 'baz',
  429. 'baz' => 'bat',
  430. 'bat' => 'foo'
  431. ));
  432. $test = $this->form->getAttribs();
  433. $attribs = array(
  434. 'foo' => 'bar',
  435. 'bar' => 'baz',
  436. 'baz' => 'bat',
  437. 'bat' => 'foo'
  438. );
  439. $this->assertSame($attribs, $test);
  440. }
  441. public function testSetAttribsOverwritesExistingAttribs()
  442. {
  443. $this->testCanAddAndRetrieveMultipleAttribs();
  444. $array = array('bogus' => 'value', 'not' => 'real');
  445. $this->form->setAttribs($array);
  446. $this->assertSame($array, $this->form->getAttribs());
  447. }
  448. public function testCanRemoveSingleAttrib()
  449. {
  450. $this->testCanAddAndRetrieveSingleAttribs();
  451. $this->assertTrue($this->form->removeAttrib('foo'));
  452. $this->assertNull($this->form->getAttrib('foo'));
  453. }
  454. public function testRemoveAttribReturnsFalseIfAttribDoesNotExist()
  455. {
  456. $this->assertFalse($this->form->removeAttrib('foo'));
  457. }
  458. public function testCanClearAllAttribs()
  459. {
  460. $this->testCanAddAndRetrieveMultipleAttribs();
  461. $this->form->clearAttribs();
  462. $attribs = $this->form->getAttribs();
  463. $this->assertTrue(is_array($attribs));
  464. $this->assertTrue(empty($attribs));
  465. }
  466. public function testNameIsInitiallyNull()
  467. {
  468. $this->assertNull($this->form->getName());
  469. }
  470. public function testCanSetName()
  471. {
  472. $this->testNameIsInitiallyNull();
  473. $this->form->setName('foo');
  474. $this->assertEquals('foo', $this->form->getName());
  475. }
  476. public function testZeroAsNameIsAllowed()
  477. {
  478. try {
  479. $this->form->setName(0);
  480. $this->assertEquals(0, $this->form->getName());
  481. } catch (Zend_Form_Exception $e) {
  482. $this->fail('Should allow zero as form name');
  483. }
  484. }
  485. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  486. {
  487. $this->form->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  488. $this->assertEquals('foobar', $this->form->getName());
  489. try {
  490. $this->form->setName('%\^&*)\(%$#@!.}{;-,');
  491. $this->fail('Empty names should raise exception');
  492. } catch (Zend_Form_Exception $e) {
  493. $this->assertContains('Invalid name provided', $e->getMessage());
  494. }
  495. }
  496. public function testActionDefaultsToEmptyString()
  497. {
  498. $this->assertSame('', $this->form->getAction());
  499. }
  500. public function testCanSetAction()
  501. {
  502. $this->testActionDefaultsToEmptyString();
  503. $this->form->setAction('/foo/bar');
  504. $this->assertEquals('/foo/bar', $this->form->getAction());
  505. }
  506. /**
  507. * @group ZF-7067
  508. */
  509. public function testCanSetActionWithGetParams()
  510. {
  511. $this->testActionDefaultsToEmptyString();
  512. $this->form->setAction('/foo.php?bar')
  513. ->setView(new Zend_View);
  514. $html = $this->form->render();
  515. $this->assertContains('action="/foo.php?bar"', $html);
  516. $this->assertEquals('/foo.php?bar', $this->form->getAction());
  517. }
  518. public function testMethodDefaultsToPost()
  519. {
  520. $this->assertEquals('post', $this->form->getMethod());
  521. }
  522. public function testCanSetMethod()
  523. {
  524. $this->testMethodDefaultsToPost();
  525. $this->form->setMethod('get');
  526. $this->assertEquals('get', $this->form->getMethod());
  527. }
  528. public function testMethodLimitedToGetPostPutAndDelete()
  529. {
  530. foreach (array('get', 'post', 'put', 'delete') as $method) {
  531. $this->form->setMethod($method);
  532. $this->assertEquals($method, $this->form->getMethod());
  533. }
  534. try {
  535. $this->form->setMethod('bogus');
  536. $this->fail('Invalid method type should throw exception');
  537. } catch (Zend_Form_Exception $e) {
  538. $this->assertContains('invalid', $e->getMessage());
  539. }
  540. }
  541. public function testEnctypeDefaultsToUrlEncoded()
  542. {
  543. $this->assertEquals(Zend_Form::ENCTYPE_URLENCODED, $this->form->getEnctype());
  544. }
  545. public function testCanSetEnctype()
  546. {
  547. $this->testEnctypeDefaultsToUrlEncoded();
  548. $this->form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  549. $this->assertEquals(Zend_Form::ENCTYPE_MULTIPART, $this->form->getEnctype());
  550. }
  551. public function testLegendInitiallyNull()
  552. {
  553. $this->assertNull($this->form->getLegend());
  554. }
  555. public function testCanSetLegend()
  556. {
  557. $this->testLegendInitiallyNull();
  558. $legend = "This is a legend";
  559. $this->form->setLegend($legend);
  560. $this->assertEquals($legend, $this->form->getLegend());
  561. }
  562. public function testDescriptionInitiallyNull()
  563. {
  564. $this->assertNull($this->form->getDescription());
  565. }
  566. public function testCanSetDescription()
  567. {
  568. $this->testDescriptionInitiallyNull();
  569. $description = "This is a description";
  570. $this->form->setDescription($description);
  571. $this->assertEquals($description, $this->form->getDescription());
  572. }
  573. // Plugin loaders
  574. public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
  575. {
  576. $loader = $this->form->getPluginLoader('decorator');
  577. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  578. $paths = $loader->getPaths('Zend_Form_Decorator');
  579. $this->assertTrue(is_array($paths), var_export($loader, 1));
  580. $this->assertTrue(0 < count($paths));
  581. $this->assertContains('Form', $paths[0]);
  582. $this->assertContains('Decorator', $paths[0]);
  583. }
  584. public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
  585. {
  586. $loader = new Zend_Loader_PluginLoader();
  587. try {
  588. $this->form->setPluginLoader($loader, 'foo');
  589. $this->fail('Invalid plugin loader type should raise exception');
  590. } catch (Zend_Form_Exception $e) {
  591. $this->assertContains('Invalid type', $e->getMessage());
  592. }
  593. }
  594. public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
  595. {
  596. try {
  597. $this->form->getPluginLoader('foo');
  598. $this->fail('Invalid plugin loader type should raise exception');
  599. } catch (Zend_Form_Exception $e) {
  600. $this->assertContains('Invalid type', $e->getMessage());
  601. }
  602. }
  603. public function testCanSetCustomDecoratorPluginLoader()
  604. {
  605. $loader = new Zend_Loader_PluginLoader();
  606. $this->form->setPluginLoader($loader, 'decorator');
  607. $test = $this->form->getPluginLoader('decorator');
  608. $this->assertSame($loader, $test);
  609. }
  610. public function testPassingInvalidTypeToAddPrefixPathThrowsException()
  611. {
  612. try {
  613. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
  614. $this->fail('Passing invalid loader type to addPrefixPath() should raise exception');
  615. } catch (Zend_Form_Exception $e) {
  616. $this->assertContains('Invalid type', $e->getMessage());
  617. }
  618. }
  619. public function testCanAddDecoratorPluginLoaderPrefixPath()
  620. {
  621. $loader = $this->form->getPluginLoader('decorator');
  622. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  623. $paths = $loader->getPaths('Zend_Foo');
  624. $this->assertTrue(is_array($paths));
  625. $this->assertContains('Foo', $paths[0]);
  626. }
  627. public function testUpdatedDecoratorPrefixPathUsedForNewElements()
  628. {
  629. $loader = $this->form->getPluginLoader('decorator');
  630. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  631. $foo = new Zend_Form_Element_Text('foo');
  632. $this->form->addElement($foo);
  633. $loader = $foo->getPluginLoader('decorator');
  634. $paths = $loader->getPaths('Zend_Foo');
  635. $this->assertTrue(is_array($paths));
  636. $this->assertContains('Foo', $paths[0]);
  637. $this->form->addElement('text', 'bar');
  638. $bar = $this->form->bar;
  639. $loader = $bar->getPluginLoader('decorator');
  640. $paths = $loader->getPaths('Zend_Foo');
  641. $this->assertTrue(is_array($paths));
  642. $this->assertContains('Foo', $paths[0]);
  643. }
  644. public function testUpdatedDecoratorPrefixPathUsedForNewDisplayGroups()
  645. {
  646. $loader = $this->form->getPluginLoader('decorator');
  647. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  648. $this->setupElements();
  649. $foo = $this->form->foo;
  650. $loader = $foo->getPluginLoader('decorator');
  651. $paths = $loader->getPaths('Zend_Foo');
  652. $this->assertTrue(is_array($paths));
  653. $this->assertContains('Foo', $paths[0]);
  654. }
  655. public function testUpdatedPrefixPathUsedForNewSubForms()
  656. {
  657. $loader = $this->form->getPluginLoader('decorator');
  658. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  659. $this->setupSubForm();
  660. $loader = $this->form->sub->getPluginLoader('decorator');
  661. $paths = $loader->getPaths('Zend_Foo');
  662. $this->assertTrue(is_array($paths));
  663. $this->assertContains('Foo', $paths[0]);
  664. }
  665. public function testGetPluginLoaderRetrievesDefaultElementPluginLoader()
  666. {
  667. $loader = $this->form->getPluginLoader('element');
  668. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  669. $paths = $loader->getPaths('Zend_Form_Element');
  670. $this->assertTrue(is_array($paths), var_export($loader, 1));
  671. $this->assertTrue(0 < count($paths));
  672. $this->assertContains('Form', $paths[0]);
  673. $this->assertContains('Element', $paths[0]);
  674. }
  675. public function testCanSetCustomDecoratorElementLoader()
  676. {
  677. $loader = new Zend_Loader_PluginLoader();
  678. $this->form->setPluginLoader($loader, 'element');
  679. $test = $this->form->getPluginLoader('element');
  680. $this->assertSame($loader, $test);
  681. }
  682. public function testCanAddElementPluginLoaderPrefixPath()
  683. {
  684. $loader = $this->form->getPluginLoader('element');
  685. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'element');
  686. $paths = $loader->getPaths('Zend_Foo');
  687. $this->assertTrue(is_array($paths));
  688. $this->assertContains('Foo', $paths[0]);
  689. }
  690. public function testAddAllPluginLoaderPrefixPathsSimultaneously()
  691. {
  692. $decoratorLoader = new Zend_Loader_PluginLoader();
  693. $elementLoader = new Zend_Loader_PluginLoader();
  694. $this->form->setPluginLoader($decoratorLoader, 'decorator')
  695. ->setPluginLoader($elementLoader, 'element')
  696. ->addPrefixPath('Zend', 'Zend/');
  697. $paths = $decoratorLoader->getPaths('Zend_Decorator');
  698. $this->assertTrue(is_array($paths), var_export($paths, 1));
  699. $this->assertContains('Decorator', $paths[0]);
  700. $paths = $elementLoader->getPaths('Zend_Element');
  701. $this->assertTrue(is_array($paths), var_export($paths, 1));
  702. $this->assertContains('Element', $paths[0]);
  703. }
  704. // Elements:
  705. public function testCanAddAndRetrieveSingleElements()
  706. {
  707. $element = new Zend_Form_Element('foo');
  708. $this->form->addElement($element);
  709. $this->assertSame($element, $this->form->getElement('foo'));
  710. }
  711. public function testGetElementReturnsNullForUnregisteredElement()
  712. {
  713. $this->assertNull($this->form->getElement('foo'));
  714. }
  715. public function testCanAddAndRetrieveSingleElementsByStringType()
  716. {
  717. $this->form->addElement('text', 'foo');
  718. $element = $this->form->getElement('foo');
  719. $this->assertTrue($element instanceof Zend_Form_Element);
  720. $this->assertTrue($element instanceof Zend_Form_Element_Text);
  721. $this->assertEquals('foo', $element->getName());
  722. }
  723. public function testAddElementAsStringElementThrowsExceptionWhenNoNameProvided()
  724. {
  725. try {
  726. $this->form->addElement('text');
  727. $this->fail('Should not be able to specify string element type without name');
  728. } catch (Zend_Form_Exception $e) {
  729. $this->assertContains('must have', $e->getMessage());
  730. }
  731. }
  732. public function testCreateElementReturnsNewElement()
  733. {
  734. $element = $this->form->createElement('text', 'foo');
  735. $this->assertTrue($element instanceof Zend_Form_Element);
  736. }
  737. public function testCreateElementDoesNotAttachElementToForm()
  738. {
  739. $element = $this->form->createElement('text', 'foo');
  740. $this->assertTrue($element instanceof Zend_Form_Element);
  741. $this->assertNull($this->form->foo);
  742. }
  743. public function testCanAddAndRetrieveMultipleElements()
  744. {
  745. $this->form->addElements(array(
  746. 'foo' => 'text',
  747. array('text', 'bar'),
  748. array('text', 'baz', array('foo' => 'bar')),
  749. new Zend_Form_Element_Text('bat'),
  750. ));
  751. $elements = $this->form->getElements();
  752. $names = array('foo', 'bar', 'baz', 'bat');
  753. $this->assertEquals($names, array_keys($elements));
  754. $foo = $elements['foo'];
  755. $this->assertTrue($foo instanceof Zend_Form_Element_Text);
  756. $bar = $elements['bar'];
  757. $this->assertTrue($bar instanceof Zend_Form_Element_Text);
  758. $baz = $elements['baz'];
  759. $this->assertTrue($baz instanceof Zend_Form_Element_Text);
  760. $this->assertEquals('bar', $baz->foo, var_export($baz->getAttribs(), 1));
  761. $bat = $elements['bat'];
  762. $this->assertTrue($bat instanceof Zend_Form_Element_Text);
  763. }
  764. public function testSetElementsOverwritesExistingElements()
  765. {
  766. $this->testCanAddAndRetrieveMultipleElements();
  767. $this->form->setElements(array(
  768. 'bogus' => 'text'
  769. ));
  770. $elements = $this->form->getElements();
  771. $names = array('bogus');
  772. $this->assertEquals($names, array_keys($elements));
  773. }
  774. public function testCanRemoveSingleElement()
  775. {
  776. $this->testCanAddAndRetrieveMultipleElements();
  777. $this->assertTrue($this->form->removeElement('bar'));
  778. $this->assertNull($this->form->getElement('bar'));
  779. }
  780. public function testRemoveElementReturnsFalseWhenElementNotRegistered()
  781. {
  782. $this->assertFalse($this->form->removeElement('bogus'));
  783. }
  784. public function testCanClearAllElements()
  785. {
  786. $this->testCanAddAndRetrieveMultipleElements();
  787. $this->form->clearElements();
  788. $elements = $this->form->getElements();
  789. $this->assertTrue(is_array($elements));
  790. $this->assertTrue(empty($elements));
  791. }
  792. public function testGetValueReturnsNullForUndefinedElements()
  793. {
  794. $this->assertNull($this->form->getValue('foo'));
  795. }
  796. public function testCanSetElementDefaultValues()
  797. {
  798. $this->testCanAddAndRetrieveMultipleElements();
  799. $values = array(
  800. 'foo' => 'foovalue',
  801. 'bar' => 'barvalue',
  802. 'baz' => 'bazvalue',
  803. 'bat' => 'batvalue'
  804. );
  805. $this->form->setDefaults($values);
  806. $elements = $this->form->getElements();
  807. foreach (array_keys($values) as $name) {
  808. $this->assertEquals($name . 'value', $elements[$name]->getValue(), var_export($elements[$name], 1));
  809. }
  810. }
  811. public function testSettingElementDefaultsDoesNotSetElementValuesToNullIfNotInDefaultsArray()
  812. {
  813. $this->testCanAddAndRetrieveMultipleElements();
  814. $this->form->baz->setValue('testing');
  815. $this->form->bar->setValue('testing');
  816. $values = array(
  817. 'foo' => 'foovalue',
  818. 'bat' => 'batvalue'
  819. );
  820. $this->form->setDefaults($values);
  821. $this->assertEquals('foovalue', $this->form->foo->getValue());
  822. $this->assertEquals('batvalue', $this->form->bat->getValue());
  823. $this->assertNotNull($this->form->baz->getValue());
  824. $this->assertNotNull($this->form->bar->getValue());
  825. }
  826. public function testCanRetrieveSingleElementValue()
  827. {
  828. $this->form->addElement('text', 'foo', array('value' => 'foovalue'));
  829. $this->assertEquals('foovalue', $this->form->getValue('foo'));
  830. }
  831. public function testCanRetrieveAllElementValues()
  832. {
  833. $this->testCanAddAndRetrieveMultipleElements();
  834. $values = array(
  835. 'foo' => 'foovalue',
  836. 'bar' => 'barvalue',
  837. 'baz' => 'bazvalue',
  838. 'bat' => 'batvalue'
  839. );
  840. $this->form->setDefaults($values);
  841. $test = $this->form->getValues();
  842. $elements = $this->form->getElements();
  843. foreach (array_keys($values) as $name) {
  844. $this->assertEquals($values[$name], $test[$name]);
  845. }
  846. }
  847. public function testRetrievingAllElementValuesSkipsThoseFlaggedAsIgnore()
  848. {
  849. $this->form->addElements(array(
  850. 'foo' => 'text',
  851. 'bar' => 'text',
  852. 'baz' => 'text'
  853. ));
  854. $this->form->setDefaults(array(
  855. 'foo' => 'Foo Value',
  856. 'bar' => 'Bar Value',
  857. 'baz' => 'Baz Value',
  858. ));
  859. $this->form->bar->setIgnore(true);
  860. $test = $this->form->getValues();
  861. $this->assertFalse(array_key_exists('bar', $test));
  862. $this->assertTrue(array_key_exists('foo', $test));
  863. $this->assertTrue(array_key_exists('baz', $test));
  864. }
  865. public function testCanRetrieveSingleUnfilteredElementValue()
  866. {
  867. $foo = new Zend_Form_Element_Text('foo');
  868. $foo->addFilter('StringToUpper')
  869. ->setValue('foovalue');
  870. $this->form->addElement($foo);
  871. $this->assertEquals('FOOVALUE', $this->form->getValue('foo'));
  872. $this->assertEquals('foovalue', $this->form->getUnfilteredValue('foo'));
  873. }
  874. public function testCanRetrieveAllUnfilteredElementValues()
  875. {
  876. $foo = new Zend_Form_Element_Text('foo');
  877. $foo->addFilter('StringToUpper')
  878. ->setValue('foovalue');
  879. $bar = new Zend_Form_Element_Text('bar');
  880. $bar->addFilter('StringToUpper')
  881. ->setValue('barvalue');
  882. $this->form->addElements(array($foo, $bar));
  883. $values = $this->form->getValues();
  884. $unfiltered = $this->form->getUnfilteredValues();
  885. foreach (array('foo', 'bar') as $key) {
  886. $value = $key . 'value';
  887. $this->assertEquals(strtoupper($value), $values[$key]);
  888. $this->assertEquals($value, $unfiltered[$key]);
  889. }
  890. }
  891. public function testOverloadingElements()
  892. {
  893. $this->form->addElement('text', 'foo');
  894. $this->assertTrue(isset($this->form->foo));
  895. $element = $this->form->foo;
  896. $this->assertTrue($element instanceof Zend_Form_Element);
  897. unset($this->form->foo);
  898. $this->assertFalse(isset($this->form->foo));
  899. $bar = new Zend_Form_Element_Text('bar');
  900. $this->form->bar = $bar;
  901. $this->assertTrue(isset($this->form->bar));
  902. $element = $this->form->bar;
  903. $this->assertSame($bar, $element);
  904. }
  905. public function testOverloadingGetReturnsNullForUndefinedFormItems()
  906. {
  907. $this->assertNull($this->form->bogus);
  908. }
  909. public function testOverloadingSetThrowsExceptionForInvalidTypes()
  910. {
  911. try {
  912. $this->form->foo = true;
  913. $this->fail('Overloading should not allow scalars');
  914. } catch (Zend_Form_Exception $e) {
  915. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  916. }
  917. try {
  918. $this->form->foo = new Zend_Config(array());
  919. $this->fail('Overloading should not allow arbitrary object types');
  920. } catch (Zend_Form_Exception $e) {
  921. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  922. $this->assertContains('Zend_Config', $e->getMessage());
  923. }
  924. }
  925. public function testFormIsNotAnArrayByDefault()
  926. {
  927. $this->assertFalse($this->form->isArray());
  928. }
  929. public function testCanSetArrayFlag()
  930. {
  931. $this->testFormIsNotAnArrayByDefault();
  932. $this->form->setIsArray(true);
  933. $this->assertTrue($this->form->isArray());
  934. $this->form->setIsArray(false);
  935. $this->assertFalse($this->form->isArray());
  936. }
  937. public function testElementsBelongToReturnsFormNameWhenFormIsArray()
  938. {
  939. $this->form->setName('foo')
  940. ->setIsArray(true);
  941. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  942. }
  943. public function testElementsInitiallyBelongToNoArrays()
  944. {
  945. $this->assertNull($this->form->getElementsBelongTo());
  946. }
  947. public function testCanSetArrayToWhichElementsBelong()
  948. {
  949. $this->testElementsInitiallyBelongToNoArrays();
  950. $this->form->setElementsBelongTo('foo');
  951. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  952. }
  953. public function testSettingArrayToWhichElementsBelongSetsArrayFlag()
  954. {
  955. $this->testFormIsNotAnArrayByDefault();
  956. $this->testCanSetArrayToWhichElementsBelong();
  957. $this->assertTrue($this->form->isArray());
  958. }
  959. public function testArrayToWhichElementsBelongCanConsistOfValidVariableCharsOnly()
  960. {
  961. $this->testElementsInitiallyBelongToNoArrays();
  962. $this->form->setElementsBelongTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
  963. $this->assertEquals('foobar', $this->form->getElementsBelongTo());
  964. }
  965. public function testSettingArrayToWhichElementsBelongEmptyClearsIt()
  966. {
  967. $this->testCanSetArrayToWhichElementsBelong();
  968. $this->form->setElementsBelongTo('');
  969. $this->assertNull($this->form->getElementsBelongTo());
  970. }
  971. public function testSettingArrayToWhichElementsBelongEmptySetsArrayFlagToFalse()
  972. {
  973. $this->testSettingArrayToWhichElementsBelongEmptyClearsIt();
  974. $this->assertFalse($this->form->isArray());
  975. }
  976. /**
  977. * @group ZF-6741
  978. */
  979. public function testUseIdForDdTagByDefault()
  980. {
  981. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  982. ->bar->addElement('text', 'foo');
  983. $html = $this->form->setView($this->getView())->render();
  984. $this->assertRegexp('/<dd.*?bar-foo.*?>/', $html);
  985. }
  986. public function testUseIdForDtTagByDefault()
  987. {
  988. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  989. ->bar->addElement('text', 'foo');
  990. $html = $this->form->setView($this->getView())->render();
  991. $this->assertRegexp('/<dt.*?bar-foo.*?>/', $html);
  992. }
  993. /**
  994. * @group ZF-3146
  995. */
  996. public function testSetElementsBelongToShouldApplyToBothExistingAndFutureElements()
  997. {
  998. $this->form->addElement('text', 'testBelongsTo');
  999. $this->form->setElementsBelongTo('foo');
  1000. $this->assertEquals('foo', $this->form->testBelongsTo->getBelongsTo(), 'Failed determining testBelongsTo belongs to array');
  1001. $this->setupElements();
  1002. foreach ($this->form->getElements() as $element) {
  1003. $message = sprintf('Failed determining element "%s" belongs to foo', $element->getName());
  1004. $this->assertEquals('foo', $element->getBelongsTo(), $message);
  1005. }
  1006. }
  1007. /**
  1008. * @group ZF-3742
  1009. */
  1010. public function testElementsInDisplayGroupsShouldInheritFormElementsBelongToSetting()
  1011. {
  1012. $subForm = new Zend_Form_SubForm();
  1013. $subForm->addElements(array(
  1014. new Zend_Form_Element_Text('foo'),
  1015. new Zend_Form_Element_Text('bar'),
  1016. new Zend_Form_Element_Text('baz'),
  1017. new Zend_Form_Element_Text('bat'),
  1018. ))
  1019. ->addDisplayGroup(array('bar', 'baz'), 'barbaz');
  1020. $this->form->addSubForm($subForm, 'sub')
  1021. ->setElementsBelongTo('myform')
  1022. ->setView(new Zend_View);
  1023. $html = $this->form->render();
  1024. foreach (array('foo', 'bar', 'baz', 'bat') as $test) {
  1025. $this->assertContains('id="myform-sub-' . $test . '"', $html);
  1026. $this->assertContains('name="myform[sub][' . $test . ']"', $html);
  1027. }
  1028. }
  1029. public function testIsValidWithOneLevelElementsBelongTo()
  1030. {
  1031. $this->form->addElement('text', 'test')->test
  1032. ->addValidator('Identical', false, array('Test Value'));
  1033. $this->form->setElementsBelongTo('foo');
  1034. $data = array(
  1035. 'foo' => array(
  1036. 'test' => 'Test Value',
  1037. ),
  1038. );
  1039. $this->assertTrue($this->form->isValid($data));
  1040. }
  1041. public function testIsValidWithMultiLevelElementsBelongTo()
  1042. {
  1043. $this->form->addElement('text', 'test')->test
  1044. ->addValidator('Identical', false, array('Test Value'));
  1045. $this->form->setElementsBelongTo('foo[bar][zot]');
  1046. $data = array(
  1047. 'foo' => array(
  1048. 'bar' => array(
  1049. 'zot' => array(
  1050. 'test' => 'Test Value',
  1051. ),
  1052. ),
  1053. ),
  1054. );
  1055. $this->assertTrue($this->form->isValid($data));
  1056. }
  1057. // Sub forms
  1058. public function testCanAddAndRetrieveSingleSubForm()
  1059. {
  1060. $subForm = new Zend_Form_SubForm;
  1061. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1062. $this->form->addSubForm($subForm, 'page1');
  1063. $test = $this->form->getSubForm('page1');
  1064. $this->assertSame($subForm, $test);
  1065. }
  1066. public function testAddingSubFormSetsSubFormName()
  1067. {
  1068. $subForm = new Zend_Form_SubForm;
  1069. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1070. $this->form->addSubForm($subForm, 'page1');
  1071. $this->assertEquals('page1', $subForm->getName());
  1072. }
  1073. public function testAddingSubFormResetsBelongsToWithDifferentSubFormName()
  1074. {
  1075. $subForm = new Zend_Form_SubForm;
  1076. $subForm->setName('quo')
  1077. ->addElement('text', 'foo');
  1078. $this->form->addSubForm($subForm, 'bar');
  1079. $this->assertEquals('bar', $subForm->foo->getBelongsTo());
  1080. }
  1081. public function testGetSubFormReturnsNullForUnregisteredSubForm()
  1082. {
  1083. $this->assertNull($this->form->getSubForm('foo'));
  1084. }
  1085. public function testCanAddAndRetrieveMultipleSubForms()
  1086. {
  1087. $page1 = new Zend_Form_SubForm();
  1088. $page2 = new Zend_Form_SubForm();
  1089. $page3 = new Zend_Form_SubForm();
  1090. $this->form->addSubForms(array(
  1091. 'page1' => $page1,
  1092. array($page2, 'page2'),
  1093. array($page3, 'page3', 3)
  1094. ));
  1095. $subforms = $this->form->getSubForms();
  1096. $keys = array('page1', 'page2', 'page3');
  1097. $this->assertEquals($keys, array_keys($subforms));
  1098. $this->assertSame($page1, $subforms['page1']);
  1099. $this->assertSame($page2, $subforms['page2']);
  1100. $this->assertSame($page3, $subforms['page3']);
  1101. }
  1102. public function testSetSubFormsOverwritesExistingSubForms()
  1103. {
  1104. $this->testCanAddAndRetrieveMultipleSubForms();
  1105. $foo = new Zend_Form_SubForm();
  1106. $this->form->setSubForms(array('foo' => $foo));
  1107. $subforms = $this->form->getSubForms();
  1108. $keys = array('foo');
  1109. $this->assertEquals($keys, array_keys($subforms));
  1110. $this->assertSame($foo, $subforms['foo']);
  1111. }
  1112. public function testCanRemoveSingleSubForm()
  1113. {
  1114. $this->testCanAddAndRetrieveMultipleSubForms();
  1115. $this->assertTrue($this->form->removeSubForm('page2'));
  1116. $this->assertNull($this->form->getSubForm('page2'));
  1117. }
  1118. public function testRemoveSubFormReturnsFalseForNonexistantSubForm()
  1119. {
  1120. $this->assertFalse($this->form->removeSubForm('foo'));
  1121. }
  1122. public function testCanClearAllSubForms()
  1123. {
  1124. $this->testCanAddAndRetrieveMultipleSubForms();
  1125. $this->form->clearSubForms();
  1126. $subforms = $this->form->getSubForms();
  1127. $this->assertTrue(is_array($subforms));
  1128. $this->assertTrue(empty($subforms));
  1129. }
  1130. public function testOverloadingSubForms()
  1131. {
  1132. $foo = new Zend_Form_SubForm;
  1133. $this->form->addSubForm($foo, 'foo');
  1134. $this->assertTrue(isset($this->form->foo));
  1135. $subform = $this->form->foo;
  1136. $this->assertSame($foo, $subform);
  1137. unset($this->form->foo);
  1138. $this->assertFalse(isset($this->form->foo));
  1139. $bar = new Zend_Form_SubForm();
  1140. $this->form->bar = $bar;
  1141. $this->assertTrue(isset($this->form->bar));
  1142. $subform = $this->form->bar;
  1143. $this->assertSame($bar, $subform);
  1144. }
  1145. public function testCanSetDefaultsForSubFormElementsFromForm()
  1146. {
  1147. $subForm = new Zend_Form_SubForm;
  1148. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1149. $this->form->addSubForm($subForm, 'page1');
  1150. $data = array('foo' => 'foo value', 'bar' => 'bar value');
  1151. $this->form->setDefaults($data);
  1152. $this->assertEquals($data['foo'], $subForm->foo->getValue());
  1153. $this->assertEquals($data['bar'], $subForm->bar->getValue());
  1154. }
  1155. public function testCanSetDefaultsForSubFormElementsFromFormWithArray()
  1156. {
  1157. $subForm = new Zend_Form_SubForm;
  1158. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1159. $this->form->addSubForm($subForm, 'page1');
  1160. $data = array( 'page1' => array(
  1161. 'foo' => 'foo value',
  1162. 'bar' => 'bar value'
  1163. ));
  1164. $this->form->setDefaults($data);
  1165. $this->assertEquals($data['page1']['foo'], $subForm->foo->getValue());
  1166. $this->assertEquals($data['page1']['bar'], $subForm->bar->getValue());
  1167. }
  1168. public function testGetValuesReturnsSubFormValues()
  1169. {
  1170. $subForm = new Zend_Form_SubForm;
  1171. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1172. $subForm->foo->setValue('foo value');
  1173. $subForm->bar->setValue('bar value');
  1174. $this->form->addSubForm($subForm, 'page1');
  1175. $values = $this->form->getValues();
  1176. $this->assertTrue(isset($values['page1']));
  1177. $this->assertTrue(isset($values['page1']['foo']));
  1178. $this->assertTrue(isset($values['page1']['bar']));
  1179. $this->assertEquals($subForm->foo->getValue(), $values['page1']['foo']);
  1180. $this->assertEquals($subForm->bar->getValue(), $values['page1']['bar']);
  1181. }
  1182. public function testGetValuesReturnsSubFormValuesFromArrayToWhichElementsBelong()
  1183. {
  1184. $subForm = new Zend_Form_SubForm;
  1185. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1186. ->setElementsBelongTo('subform');
  1187. $subForm->foo->setValue('foo value');
  1188. $subForm->bar->setValue('bar value');
  1189. $this->form->addSubForm($subForm, 'page1');
  1190. $values = $this->form->getValues();
  1191. $this->assertTrue(isset($values['subform']), var_export($values, 1));
  1192. $this->assertTrue(isset($values['subform']['foo']));
  1193. $this->assertTrue(isset($values['subform']['bar']));
  1194. $this->assertEquals($subForm->foo->getValue(), $values['subform']['foo']);
  1195. $this->assertEquals($subForm->bar->getValue(), $values['subform']['bar']);
  1196. }
  1197. public function testGetValuesReturnsNestedSubFormValuesFromArraysToWhichElementsBelong()
  1198. {
  1199. $form = new Zend_Form();
  1200. $form->setElementsBelongTo('foobar');
  1201. $form->addElement('text', 'firstName')
  1202. ->getElement('firstName')
  1203. ->setRequired(true);
  1204. $form->addElement('text', 'lastName')
  1205. ->getElement('lastName')
  1206. ->setRequired(true);
  1207. $subForm = new Zend_Form_SubForm();
  1208. $subForm->setElementsBelongTo('baz[quux]');
  1209. $subForm->addElement('text', 'email')
  1210. ->getElement('email')->setRequired(true);
  1211. $subSubForm = new Zend_Form_SubForm();
  1212. $subSubForm->setElementsBelongTo('bat');
  1213. $subSubForm->addElement('checkbox', 'home')
  1214. ->getElement('home')->setRequired(true);
  1215. $subForm->addSubForm($subSubForm, 'subSub');
  1216. $form->addSubForm($subForm, 'sub')
  1217. ->addElement('submit', 'save', array('value' => 'submit', 'ignore' => true));
  1218. $data = array('foobar' => array(
  1219. 'firstName' => 'Mabel',
  1220. 'lastName' => 'Cow',
  1221. 'baz' => array(
  1222. 'quux' => array(
  1223. 'email' => 'mabel@cow.org',
  1224. 'bat' => array(
  1225. 'home' => 1,
  1226. )
  1227. ),
  1228. )
  1229. ));
  1230. $this->assertTrue($form->isValid($data));
  1231. $values = $form->getValues();
  1232. $this->assertEquals($data, $values);
  1233. }
  1234. public function testGetValueCanReturnSubFormValues()
  1235. {
  1236. $subForm = new Zend_Form_SubForm;
  1237. $subForm->addElements(array('foo' =>

Large files files are truncated, but you can click here to view the full file