DisplayGroupTest.php 28 KB

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