DisplayGroupTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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-2010 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_DisplayGroupTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. require_once 'Zend/Form/DisplayGroup.php';
  27. require_once 'Zend/Config.php';
  28. require_once 'Zend/Controller/Action/HelperBroker.php';
  29. require_once 'Zend/Form.php';
  30. require_once 'Zend/Form/Decorator/Form.php';
  31. require_once 'Zend/Form/Decorator/HtmlTag.php';
  32. require_once 'Zend/Form/Element.php';
  33. require_once 'Zend/Form/Element/Text.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-2010 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_DisplayGroupTest extends PHPUnit_Framework_TestCase
  47. {
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_DisplayGroupTest');
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. public function setUp()
  54. {
  55. Zend_Registry::_unsetInstance();
  56. Zend_Form::setDefaultTranslator(null);
  57. if (isset($this->error)) {
  58. unset($this->error);
  59. }
  60. Zend_Controller_Action_HelperBroker::resetHelpers();
  61. $this->loader = new Zend_Loader_PluginLoader(
  62. array('Zend_Form_Decorator' => 'Zend/Form/Decorator')
  63. );
  64. $this->group = new Zend_Form_DisplayGroup(
  65. 'test',
  66. $this->loader
  67. );
  68. }
  69. public function tearDown()
  70. {
  71. }
  72. public function getView()
  73. {
  74. $view = new Zend_View();
  75. $libPath = dirname(__FILE__) . '/../../../library';
  76. $view->addHelperPath($libPath . '/Zend/View/Helper');
  77. return $view;
  78. }
  79. // General
  80. public function testConstructorRequiresNameAndPluginLoader()
  81. {
  82. $this->assertEquals('test', $this->group->getName());
  83. $this->assertSame($this->loader, $this->group->getPluginLoader());
  84. }
  85. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  86. {
  87. $this->group->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  88. $this->assertEquals('foobar', $this->group->getName());
  89. try {
  90. $this->group->setName('%\^&*)\(%$#@!.}{;-,');
  91. $this->fail('Empty names should raise exception');
  92. } catch (Zend_Form_Exception $e) {
  93. $this->assertContains('Invalid name provided', $e->getMessage());
  94. }
  95. }
  96. public function testZeroIsAValidGroupName()
  97. {
  98. try {
  99. $this->group->setName(0);
  100. $this->assertSame('0', $this->group->getName());
  101. } catch (Zend_Form_Exception $e) {
  102. $this->fail('Should allow zero as group name');
  103. }
  104. }
  105. public function testOrderNullByDefault()
  106. {
  107. $this->assertNull($this->group->getOrder());
  108. }
  109. public function testCanSetOrder()
  110. {
  111. $this->testOrderNullByDefault();
  112. $this->group->setOrder(50);
  113. $this->assertEquals(50, $this->group->getOrder());
  114. }
  115. public function testDescriptionInitiallyNull()
  116. {
  117. $this->assertNull($this->group->getDescription());
  118. }
  119. public function testCanSetDescription()
  120. {
  121. $this->testDescriptionInitiallyNull();
  122. $description = "this is a description";
  123. $this->group->setDescription($description);
  124. $this->assertEquals($description, $this->group->getDescription());
  125. }
  126. // Elements
  127. public function testPassingInvalidElementsToAddElementsThrowsException()
  128. {
  129. $elements = array('foo' => true);
  130. try {
  131. $this->group->addElements($elements);
  132. $this->fail('Invalid elements should raise exception');
  133. } catch (Zend_Form_Exception $e) {
  134. $this->assertContains('must be Zend_Form_Elements only', $e->getMessage());
  135. }
  136. }
  137. public function testCanAddElements()
  138. {
  139. $foo = new Zend_Form_Element('foo');
  140. $this->group->addElement($foo);
  141. $element = $this->group->getElement('foo');
  142. $this->assertSame($foo, $element);
  143. }
  144. public function testCanAddMultipleElements()
  145. {
  146. $foo = new Zend_Form_Element('foo');
  147. $bar = new Zend_Form_Element('bar');
  148. $this->group->addElements(array($foo, $bar));
  149. $elements = $this->group->getElements();
  150. $this->assertEquals(array('foo' => $foo, 'bar' => $bar), $elements);
  151. }
  152. public function testSetElementsOverWritesExistingElements()
  153. {
  154. $this->testCanAddMultipleElements();
  155. $baz = new Zend_Form_Element('baz');
  156. $this->group->setElements(array($baz));
  157. $elements = $this->group->getElements();
  158. $this->assertEquals(array('baz' => $baz), $elements);
  159. }
  160. public function testCanRemoveSingleElements()
  161. {
  162. $this->testCanAddMultipleElements();
  163. $this->group->removeElement('bar');
  164. $this->assertNull($this->group->getElement('bar'));
  165. }
  166. public function testRemoveElementReturnsFalseIfElementNotRegistered()
  167. {
  168. $this->assertFalse($this->group->removeElement('bar'));
  169. }
  170. public function testCanRemoveAllElements()
  171. {
  172. $this->testCanAddMultipleElements();
  173. $this->group->clearElements();
  174. $elements = $this->group->getElements();
  175. $this->assertTrue(is_array($elements));
  176. $this->assertTrue(empty($elements));
  177. }
  178. // Plugin loader
  179. public function testCanSetPluginLoader()
  180. {
  181. $loader = new Zend_Loader_PluginLoader();
  182. $this->group->setPluginLoader($loader);
  183. $this->assertSame($loader, $this->group->getPluginLoader());
  184. }
  185. // Decorators
  186. public function testDefaultDecoratorsRegistered()
  187. {
  188. $this->_checkZf2794();
  189. $decorator = $this->group->getDecorator('FormElements');
  190. $this->assertTrue($decorator instanceof Zend_Form_Decorator_FormElements);
  191. $decorator = $this->group->getDecorator('Fieldset');
  192. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  193. }
  194. public function testCanDisableRegisteringDefaultDecoratorsDuringInitialization()
  195. {
  196. $group = new Zend_Form_DisplayGroup(
  197. 'test',
  198. $this->loader,
  199. array('disableLoadDefaultDecorators' => true)
  200. );
  201. $decorators = $group->getDecorators();
  202. $this->assertEquals(array(), $decorators);
  203. }
  204. public function testAddingInvalidDecoratorThrowsException()
  205. {
  206. try {
  207. $this->group->addDecorator(123);
  208. $this->fail('Invalid decorator should raise exception');
  209. } catch (Zend_Form_Exception $e) {
  210. $this->assertContains('Invalid decorator', $e->getMessage());
  211. }
  212. }
  213. public function testCanAddSingleDecoratorAsString()
  214. {
  215. $this->_checkZf2794();
  216. $this->group->clearDecorators();
  217. $this->assertFalse($this->group->getDecorator('form'));
  218. $this->group->addDecorator('viewHelper');
  219. $decorator = $this->group->getDecorator('viewHelper');
  220. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  221. }
  222. public function testCanNotRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  223. {
  224. $this->assertFalse($this->group->getDecorator('Zend_Form_Decorator_FormElements'));
  225. }
  226. public function testCanAddSingleDecoratorAsDecoratorObject()
  227. {
  228. $this->group->clearDecorators();
  229. $this->assertFalse($this->group->getDecorator('form'));
  230. $decorator = new Zend_Form_Decorator_ViewHelper;
  231. $this->group->addDecorator($decorator);
  232. $test = $this->group->getDecorator('Zend_Form_Decorator_ViewHelper');
  233. $this->assertSame($decorator, $test);
  234. }
  235. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  236. {
  237. $this->_checkZf2794();
  238. $this->group->clearDecorators();
  239. $this->assertFalse($this->group->getDecorator('form'));
  240. $decorator = new Zend_Form_Decorator_Form;
  241. $this->group->addDecorator($decorator);
  242. $test = $this->group->getDecorator('form');
  243. $this->assertSame($decorator, $test);
  244. }
  245. public function testCanAddMultipleDecorators()
  246. {
  247. $this->_checkZf2794();
  248. $this->group->clearDecorators();
  249. $this->assertFalse($this->group->getDecorator('form'));
  250. $testDecorator = new Zend_Form_Decorator_HtmlTag;
  251. $this->group->addDecorators(array(
  252. 'ViewHelper',
  253. $testDecorator
  254. ));
  255. $viewHelper = $this->group->getDecorator('viewHelper');
  256. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  257. $decorator = $this->group->getDecorator('HtmlTag');
  258. $this->assertSame($testDecorator, $decorator);
  259. }
  260. public function testCanRemoveDecorator()
  261. {
  262. $this->_checkZf2794();
  263. $this->testDefaultDecoratorsRegistered();
  264. $this->group->removeDecorator('form');
  265. $this->assertFalse($this->group->getDecorator('form'));
  266. }
  267. /**
  268. * @see ZF-3069
  269. */
  270. public function testRemovingNamedDecoratorsShouldWork()
  271. {
  272. $this->_checkZf2794();
  273. $this->group->setDecorators(array(
  274. 'FormElements',
  275. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  276. array(array('div2' => 'HtmlTag'), array('tag' => 'div')),
  277. ));
  278. $decorators = $this->group->getDecorators();
  279. $this->assertTrue(array_key_exists('div', $decorators));
  280. $this->assertTrue(array_key_exists('div2', $decorators));
  281. $this->group->removeDecorator('div');
  282. $decorators = $this->group->getDecorators();
  283. $this->assertFalse(array_key_exists('div', $decorators));
  284. $this->assertTrue(array_key_exists('div2', $decorators));
  285. }
  286. public function testCanClearAllDecorators()
  287. {
  288. $this->_checkZf2794();
  289. $this->testCanAddMultipleDecorators();
  290. $this->group->clearDecorators();
  291. $this->assertFalse($this->group->getDecorator('viewHelper'));
  292. $this->assertFalse($this->group->getDecorator('HtmlTag'));
  293. }
  294. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  295. {
  296. $this->_checkZf2794();
  297. $this->group->setDecorators(array(
  298. array('HtmlTag', array('tag' => 'fieldset')),
  299. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  300. ));
  301. $decorator = $this->group->getDecorator('FooBar');
  302. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  303. $this->assertEquals('dd', $decorator->getOption('tag'));
  304. $decorator = $this->group->getDecorator('HtmlTag');
  305. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  306. $this->assertEquals('fieldset', $decorator->getOption('tag'));
  307. }
  308. /**
  309. * @see ZF-3494
  310. */
  311. public function testGetViewShouldNotReturnNullWhenViewRendererIsActive()
  312. {
  313. require_once 'Zend/Controller/Action/HelperBroker.php';
  314. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
  315. $viewRenderer->initView();
  316. $view = $this->group->getView();
  317. $this->assertSame($viewRenderer->view, $view);
  318. }
  319. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  320. {
  321. $this->group->setDecorators(array(
  322. 'FormElements',
  323. array(array('dl' => 'HtmlTag'), array('tag' => 'dl')),
  324. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  325. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  326. ));
  327. $decorator = $this->group->getDecorator('div');
  328. $decorators = $this->group->getDecorators();
  329. $i = 0;
  330. $order = array();
  331. foreach (array_keys($decorators) as $name) {
  332. $order[$name] = $i;
  333. ++$i;
  334. }
  335. $this->assertEquals(2, $order['div'], var_export($order, 1));
  336. }
  337. public function testRenderingRendersAllElementsWithinFieldsetByDefault()
  338. {
  339. $foo = new Zend_Form_Element_Text('foo');
  340. $bar = new Zend_Form_Element_Text('bar');
  341. $this->group->addElements(array($foo, $bar));
  342. $html = $this->group->render($this->getView());
  343. $this->assertRegexp('#^<dt[^>]*>&\#160;</dt><dd[^>]*><fieldset.*?</fieldset></dd>$#s', $html, $html);
  344. $this->assertContains('<input', $html, $html);
  345. $this->assertContains('"foo"', $html);
  346. $this->assertContains('"bar"', $html);
  347. }
  348. public function testToStringProxiesToRender()
  349. {
  350. $foo = new Zend_Form_Element_Text('foo');
  351. $bar = new Zend_Form_Element_Text('bar');
  352. $this->group->addElements(array($foo, $bar))
  353. ->setView($this->getView());
  354. $html = $this->group->__toString();
  355. $this->assertRegexp('#^<dt[^>]*>&\#160;</dt><dd[^>]*><fieldset.*?</fieldset></dd>$#s', $html, $html);
  356. $this->assertContains('<input', $html);
  357. $this->assertContains('"foo"', $html);
  358. $this->assertContains('"bar"', $html);
  359. }
  360. public function raiseDecoratorException($content, $element, $options)
  361. {
  362. throw new Exception('Raising exception in decorator callback');
  363. }
  364. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  365. {
  366. $this->error = $errstr;
  367. }
  368. public function testToStringRaisesErrorWhenExceptionCaught()
  369. {
  370. $this->group->setDecorators(array(
  371. array(
  372. 'decorator' => 'Callback',
  373. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  374. ),
  375. ));
  376. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  377. $text = $this->group->__toString();
  378. restore_error_handler();
  379. $this->assertTrue(empty($text));
  380. $this->assertTrue(isset($this->error));
  381. $this->assertEquals('Raising exception in decorator callback', $this->error);
  382. }
  383. public function testNoTranslatorByDefault()
  384. {
  385. $this->assertNull($this->group->getTranslator());
  386. }
  387. public function testGetTranslatorRetrievesGlobalDefaultWhenAvailable()
  388. {
  389. $this->testNoTranslatorByDefault();
  390. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  391. Zend_Form::setDefaultTranslator($translator);
  392. $received = $this->group->getTranslator();
  393. $this->assertSame($translator->getAdapter(), $received);
  394. }
  395. public function testTranslatorAccessorsWorks()
  396. {
  397. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  398. $this->group->setTranslator($translator);
  399. $received = $this->group->getTranslator($translator);
  400. $this->assertSame($translator->getAdapter(), $received);
  401. }
  402. public function testCanDisableTranslation()
  403. {
  404. $this->testGetTranslatorRetrievesGlobalDefaultWhenAvailable();
  405. $this->group->setDisableTranslator(true);
  406. $this->assertNull($this->group->getTranslator());
  407. }
  408. // Iteration
  409. public function setupIteratorElements()
  410. {
  411. $foo = new Zend_Form_Element('foo');
  412. $bar = new Zend_Form_Element('bar');
  413. $baz = new Zend_Form_Element('baz');
  414. $this->group->addElements(array($foo, $bar, $baz));
  415. }
  416. public function testDisplayGroupIsIterableAndIteratesElements()
  417. {
  418. $this->setupIteratorElements();
  419. $expected = array('foo', 'bar', 'baz');
  420. $received = array();
  421. foreach ($this->group as $key => $element) {
  422. $received[] = $key;
  423. $this->assertTrue($element instanceof Zend_Form_Element);
  424. }
  425. $this->assertSame($expected, $received);
  426. }
  427. public function testDisplayGroupIteratesElementsInExpectedOrder()
  428. {
  429. $this->setupIteratorElements();
  430. $test = new Zend_Form_Element('checkorder', array('order' => 1));
  431. $this->group->addElement($test);
  432. $expected = array('foo', 'checkorder', 'bar', 'baz');
  433. $received = array();
  434. foreach ($this->group as $key => $element) {
  435. $received[] = $key;
  436. }
  437. $this->assertSame($expected, $received);
  438. }
  439. public function testDisplayGroupIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  440. {
  441. $a = new Zend_Form_Element('a',array('label'=>'a'));
  442. $b = new Zend_Form_Element('b',array('label'=>'b', 'order' => 0));
  443. $c = new Zend_Form_Element('c',array('label'=>'c', 'order' => 1));
  444. $this->group->addElement($a)
  445. ->addElement($b)
  446. ->addElement($c)
  447. ->setView($this->getView());
  448. $test = $this->group->render();
  449. $this->assertContains('name="a"', $test);
  450. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  451. $this->fail('Expected markup not found');
  452. }
  453. $order = array();
  454. foreach ($matches[1] as $element) {
  455. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  456. $order[] = $m[1];
  457. }
  458. }
  459. $this->assertSame(array('b', 'c', 'a'), $order);
  460. }
  461. public function testRemovingElementsShouldNotRaiseExceptionsDuringIteration()
  462. {
  463. $this->setupIteratorElements();
  464. $bar = $this->group->getElement('bar');
  465. $this->group->removeElement('bar');
  466. try {
  467. foreach ($this->group as $item) {
  468. }
  469. } catch (Exception $e) {
  470. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  471. }
  472. }
  473. // Countable
  474. public function testCanCountDisplayGroup()
  475. {
  476. $this->setupIteratorElements();
  477. $this->assertEquals(3, count($this->group));
  478. }
  479. // Configuration
  480. public function getOptions()
  481. {
  482. $options = array(
  483. 'name' => 'foo',
  484. 'legend' => 'Display Group',
  485. 'order' => 20,
  486. 'class' => 'foobar'
  487. );
  488. return $options;
  489. }
  490. public function testCanSetObjectStateViaSetOptions()
  491. {
  492. $this->group->setOptions($this->getOptions());
  493. $this->assertEquals('foo', $this->group->getName());
  494. $this->assertEquals('Display Group', $this->group->getLegend());
  495. $this->assertEquals(20, $this->group->getOrder());
  496. $this->assertEquals('foobar', $this->group->getAttrib('class'));
  497. }
  498. public function testSetOptionsOmitsAccessorsRequiringObjectsOrMultipleParams()
  499. {
  500. $options = $this->getOptions();
  501. $config = new Zend_Config($options);
  502. $options['config'] = $config;
  503. $options['options'] = $config->toArray();
  504. $options['pluginLoader'] = true;
  505. $options['view'] = true;
  506. $options['translator'] = true;
  507. $options['attrib'] = true;
  508. $this->group->setOptions($options);
  509. }
  510. public function testSetOptionsSetsArrayOfStringDecorators()
  511. {
  512. $this->_checkZf2794();
  513. $options = $this->getOptions();
  514. $options['decorators'] = array('label', 'form');
  515. $this->group->setOptions($options);
  516. $this->assertFalse($this->group->getDecorator('group'));
  517. $decorator = $this->group->getDecorator('label');
  518. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  519. $decorator = $this->group->getDecorator('form');
  520. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  521. }
  522. public function testSetOptionsSetsArrayOfArrayDecorators()
  523. {
  524. $this->_checkZf2794();
  525. $options = $this->getOptions();
  526. $options['decorators'] = array(
  527. array('label', array('id' => 'mylabel')),
  528. array('form', array('id' => 'form')),
  529. );
  530. $this->group->setOptions($options);
  531. $this->assertFalse($this->group->getDecorator('group'));
  532. $decorator = $this->group->getDecorator('label');
  533. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  534. $options = $decorator->getOptions();
  535. $this->assertEquals('mylabel', $options['id']);
  536. $decorator = $this->group->getDecorator('form');
  537. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  538. $options = $decorator->getOptions();
  539. $this->assertEquals('form', $options['id']);
  540. }
  541. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  542. {
  543. $this->_checkZf2794();
  544. $options = $this->getOptions();
  545. $options['decorators'] = array(
  546. array(
  547. 'options' => array('id' => 'mylabel'),
  548. 'decorator' => 'label',
  549. ),
  550. array(
  551. 'options' => array('id' => 'form'),
  552. 'decorator' => 'form',
  553. ),
  554. );
  555. $this->group->setOptions($options);
  556. $this->assertFalse($this->group->getDecorator('group'));
  557. $decorator = $this->group->getDecorator('label');
  558. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  559. $options = $decorator->getOptions();
  560. $this->assertEquals('mylabel', $options['id']);
  561. $decorator = $this->group->getDecorator('form');
  562. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  563. $options = $decorator->getOptions();
  564. $this->assertEquals('form', $options['id']);
  565. }
  566. public function testCanSetObjectStateViaSetConfig()
  567. {
  568. $config = new Zend_Config($this->getOptions());
  569. $this->group->setConfig($config);
  570. $this->assertEquals('foo', $this->group->getName());
  571. $this->assertEquals('Display Group', $this->group->getLegend());
  572. $this->assertEquals(20, $this->group->getOrder());
  573. $this->assertEquals('foobar', $this->group->getAttrib('class'));
  574. }
  575. public function testPassingConfigObjectToConstructorSetsObjectState()
  576. {
  577. $config = new Zend_Config($this->getOptions());
  578. $group = new Zend_Form_DisplayGroup('foo', $this->loader, $config);
  579. $this->assertEquals('foo', $group->getName());
  580. $this->assertEquals('Display Group', $group->getLegend());
  581. $this->assertEquals(20, $group->getOrder());
  582. $this->assertEquals('foobar', $group->getAttrib('class'));
  583. }
  584. public function testGetAttribReturnsNullForUndefinedAttribs()
  585. {
  586. $this->assertNull($this->group->getAttrib('bogus'));
  587. }
  588. public function testCanAddMultipleAttribsSimultaneously()
  589. {
  590. $attribs = array(
  591. 'foo' => 'fooval',
  592. 'bar' => 'barval',
  593. 'baz' => 'bazval'
  594. );
  595. $this->group->addAttribs($attribs);
  596. $this->assertEquals($attribs, $this->group->getAttribs());
  597. }
  598. public function testSetAttribsOverwritesPreviouslySetAttribs()
  599. {
  600. $this->testCanAddMultipleAttribsSimultaneously();
  601. $attribs = array(
  602. 'foo' => 'valfoo',
  603. 'bat' => 'batval'
  604. );
  605. $this->group->setAttribs($attribs);
  606. $this->assertEquals($attribs, $this->group->getAttribs());
  607. }
  608. public function testCanRemoveSingleAttrib()
  609. {
  610. $this->testCanAddMultipleAttribsSimultaneously();
  611. $this->group->removeAttrib('bar');
  612. $this->assertNull($this->group->getAttrib('bar'));
  613. }
  614. public function testCanClearAllAttribs()
  615. {
  616. $this->testCanAddMultipleAttribsSimultaneously();
  617. $this->group->clearAttribs();
  618. $this->assertEquals(array(), $this->group->getAttribs());
  619. }
  620. // Extension
  621. public function testInitCalledBeforeLoadDecorators()
  622. {
  623. $group = new Zend_Form_DisplayGroupTest_DisplayGroup(
  624. 'test',
  625. $this->loader
  626. );
  627. $decorators = $group->getDecorators();
  628. $this->assertTrue(empty($decorators));
  629. }
  630. /**
  631. * @group ZF-3217
  632. */
  633. public function testGroupShouldOverloadToRenderDecorators()
  634. {
  635. $foo = new Zend_Form_Element_Text('foo');
  636. $bar = new Zend_Form_Element_Text('bar');
  637. $this->group->addElements(array($foo, $bar));
  638. $this->group->setView($this->getView());
  639. $html = $this->group->renderFormElements();
  640. foreach ($this->group->getElements() as $element) {
  641. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  642. }
  643. $this->assertNotContains('<dl', $html);
  644. $this->assertNotContains('<form', $html);
  645. $html = $this->group->renderFieldset('this is the content');
  646. $this->assertContains('<fieldset', $html);
  647. $this->assertContains('</fieldset>', $html);
  648. $this->assertContains('this is the content', $html);
  649. }
  650. /**
  651. * @group ZF-3217
  652. * @expectedException Zend_Form_Exception
  653. */
  654. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  655. {
  656. $html = $this->group->bogusMethodCall();
  657. }
  658. /**
  659. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  660. *
  661. * @link http://framework.zend.com/issues/browse/ZF-2794
  662. * @return void
  663. */
  664. protected function _checkZf2794()
  665. {
  666. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  667. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  668. }
  669. }
  670. /**
  671. * Prove the fluent interface on Zend_Form::loadDefaultDecorators
  672. *
  673. * @link http://framework.zend.com/issues/browse/ZF-9913
  674. * @return void
  675. */
  676. public function testFluentInterfaceOnLoadDefaultDecorators()
  677. {
  678. $this->assertSame($this->group, $this->group->loadDefaultDecorators());
  679. }
  680. /**
  681. * @group ZF-7552
  682. */
  683. public function testAddDecoratorsKeepsNonNumericKeyNames()
  684. {
  685. $this->group->addDecorators(array(array(array('td' => 'HtmlTag'),
  686. array('tag' => 'td')),
  687. array(array('tr' => 'HtmlTag'),
  688. array('tag' => 'tr')),
  689. array('HtmlTag', array('tag' => 'baz'))));
  690. $t1 = $this->group->getDecorators();
  691. $this->group->setDecorators($t1);
  692. $t2 = $this->group->getDecorators();
  693. $this->assertEquals($t1, $t2);
  694. }
  695. }
  696. class Zend_Form_DisplayGroupTest_DisplayGroup extends Zend_Form_DisplayGroup
  697. {
  698. public function init()
  699. {
  700. $this->setDisableLoadDefaultDecorators(true);
  701. }
  702. }
  703. if (PHPUnit_MAIN_METHOD == 'Zend_Form_DisplayGroupTest::main') {
  704. Zend_Form_DisplayGroupTest::main();
  705. }