DisplayGroupTest.php 26 KB

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