DisplayGroupTest.php 25 KB

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