SubFormTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Form_SubFormTest::main');
  24. }
  25. // error_reporting(E_ALL);
  26. require_once 'Zend/Form/SubForm.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/Version.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Form
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Form
  36. */
  37. class Zend_Form_SubFormTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_SubFormTest');
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. public function setUp()
  45. {
  46. Zend_Form::setDefaultTranslator(null);
  47. $this->form = new Zend_Form_SubForm();
  48. }
  49. public function tearDown()
  50. {
  51. }
  52. // General
  53. public function testSubFormUtilizesDefaultDecorators()
  54. {
  55. $decorators = $this->form->getDecorators();
  56. $this->assertTrue(array_key_exists('Zend_Form_Decorator_FormElements', $decorators));
  57. $this->assertTrue(array_key_exists('Zend_Form_Decorator_HtmlTag', $decorators));
  58. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Fieldset', $decorators));
  59. $this->assertTrue(array_key_exists('Zend_Form_Decorator_DtDdWrapper', $decorators));
  60. $htmlTag = $decorators['Zend_Form_Decorator_HtmlTag'];
  61. $tag = $htmlTag->getOption('tag');
  62. $this->assertEquals('dl', $tag);
  63. }
  64. public function testSubFormIsArrayByDefault()
  65. {
  66. $this->assertTrue($this->form->isArray());
  67. }
  68. public function testElementsBelongToSubFormNameByDefault()
  69. {
  70. $this->testSubFormIsArrayByDefault();
  71. $this->form->setName('foo');
  72. $this->assertEquals($this->form->getName(), $this->form->getElementsBelongTo());
  73. }
  74. // Extensions
  75. public function testInitCalledBeforeLoadDecorators()
  76. {
  77. $form = new Zend_Form_SubFormTest_SubForm();
  78. $decorators = $form->getDecorators();
  79. $this->assertTrue(empty($decorators));
  80. }
  81. // Bugfixes
  82. /**
  83. * @group ZF-2883
  84. */
  85. public function testDisplayGroupsShouldInheritSubFormNamespace()
  86. {
  87. $this->form->addElement('text', 'foo')
  88. ->addElement('text', 'bar')
  89. ->addDisplayGroup(array('foo', 'bar'), 'foobar');
  90. $form = new Zend_Form();
  91. $form->addSubForm($this->form, 'attributes');
  92. $html = $form->render(new Zend_View());
  93. $this->assertContains('name="attributes[foo]"', $html);
  94. $this->assertContains('name="attributes[bar]"', $html);
  95. }
  96. /**
  97. * @group ZF-3272
  98. */
  99. public function testRenderedSubFormDtShouldContainNoBreakSpace()
  100. {
  101. $subForm = new Zend_Form_SubForm(array(
  102. 'elements' => array(
  103. 'foo' => 'text',
  104. 'bar' => 'text',
  105. ),
  106. ));
  107. $form = new Zend_Form();
  108. $form->addSubForm($subForm, 'foobar')
  109. ->setView(new Zend_View);
  110. $html = $form->render();
  111. $this->assertContains('>&#160;</dt>', $html );
  112. }
  113. /**
  114. * Prove the fluent interface on Zend_Form_Subform::loadDefaultDecorators
  115. *
  116. * @link http://framework.zend.com/issues/browse/ZF-9913
  117. * @return void
  118. */
  119. public function testFluentInterfaceOnLoadDefaultDecorators()
  120. {
  121. $this->assertSame($this->form, $this->form->loadDefaultDecorators());
  122. }
  123. /**
  124. * @see ZF-11504
  125. */
  126. public function testSubFormWithNumericName()
  127. {
  128. $subForm = new Zend_Form_SubForm(array(
  129. 'elements' => array(
  130. 'foo' => 'text',
  131. 'bar' => 'text',
  132. ),
  133. ));
  134. $form = new Zend_Form();
  135. $form->addSubForm($subForm, 0);
  136. $form->addSubForm($subForm, 234);
  137. $form2 = clone $form;
  138. $this->assertEquals($form2->getSubForm(234)->getName(),234);
  139. $this->assertEquals($form2->getSubForm(0)->getName(),0);
  140. }
  141. }
  142. class Zend_Form_SubFormTest_SubForm extends Zend_Form_SubForm
  143. {
  144. public function init()
  145. {
  146. $this->setDisableLoadDefaultDecorators(true);
  147. }
  148. }
  149. if (PHPUnit_MAIN_METHOD == 'Zend_Form_SubFormTest::main') {
  150. Zend_Form_SubFormTest::main();
  151. }