PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/Zend/Form/ElementTest.php

https://bitbucket.org/ksekar/campus
PHP | 2225 lines | 1812 code | 298 blank | 115 comment | 23 complexity | 68ec5ac154673861cf4e78c23f144ef5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT

Large files files are truncated, but you can click here to view the full file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: ElementTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Form_ElementTest::main');
  24. }
  25. require_once 'Zend/Form/Element.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/Abstract.php';
  30. require_once 'Zend/Form/Decorator/HtmlTag.php';
  31. require_once 'Zend/Loader/PluginLoader.php';
  32. require_once 'Zend/Registry.php';
  33. require_once 'Zend/Translate.php';
  34. require_once 'Zend/Validate/NotEmpty.php';
  35. require_once 'Zend/Validate/EmailAddress.php';
  36. require_once 'Zend/View.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Form
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2012 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_ElementTest extends PHPUnit_Framework_TestCase
  46. {
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_ElementTest');
  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. $this->element = new Zend_Form_Element('foo');
  60. Zend_Controller_Action_HelperBroker::resetHelpers();
  61. }
  62. public function tearDown()
  63. {
  64. }
  65. public function getView()
  66. {
  67. $view = new Zend_View();
  68. $libPath = dirname(__FILE__) . '/../../../library';
  69. $view->addHelperPath($libPath . '/Zend/View/Helper');
  70. return $view;
  71. }
  72. public function testConstructorRequiresMinimallyElementName()
  73. {
  74. try {
  75. $element = new Zend_Form_Element(1);
  76. $this->fail('Zend_Form_Element constructor should not accept integer argument');
  77. } catch (Zend_Form_Exception $e) {
  78. }
  79. try {
  80. $element = new Zend_Form_Element(true);
  81. $this->fail('Zend_Form_Element constructor should not accept boolean argument');
  82. } catch (Zend_Form_Exception $e) {
  83. }
  84. try {
  85. $element = new Zend_Form_Element('foo');
  86. } catch (Exception $e) {
  87. $this->fail('Zend_Form_Element constructor should accept String values');
  88. }
  89. $config = array('foo' => 'bar');
  90. try {
  91. $element = new Zend_Form_Element($config);
  92. $this->fail('Zend_Form_Element constructor requires array with name element');
  93. } catch (Zend_Form_Exception $e) {
  94. }
  95. $config = array('name' => 'bar');
  96. try {
  97. $element = new Zend_Form_Element($config);
  98. } catch (Zend_Form_Exception $e) {
  99. $this->fail('Zend_Form_Element constructor should accept array with name element');
  100. }
  101. $config = new Zend_Config(array('foo' => 'bar'));
  102. try {
  103. $element = new Zend_Form_Element($config);
  104. $this->fail('Zend_Form_Element constructor requires Zend_Config object with name element');
  105. } catch (Zend_Form_Exception $e) {
  106. }
  107. $config = new Zend_Config(array('name' => 'bar'));
  108. try {
  109. $element = new Zend_Form_Element($config);
  110. } catch (Zend_Form_Exception $e) {
  111. $this->fail('Zend_Form_Element constructor should accept Zend_Config with name element');
  112. }
  113. }
  114. public function testNoTranslatorByDefault()
  115. {
  116. $this->assertNull($this->element->getTranslator());
  117. }
  118. public function testGetTranslatorRetrievesGlobalDefaultWhenAvailable()
  119. {
  120. $this->testNoTranslatorByDefault();
  121. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  122. Zend_Form::setDefaultTranslator($translator);
  123. $received = $this->element->getTranslator();
  124. $this->assertSame($translator->getAdapter(), $received);
  125. }
  126. public function testTranslatorAccessorsWork()
  127. {
  128. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  129. $this->element->setTranslator($translator);
  130. $received = $this->element->getTranslator($translator);
  131. $this->assertSame($translator->getAdapter(), $received);
  132. }
  133. public function testCanDisableTranslation()
  134. {
  135. $this->testGetTranslatorRetrievesGlobalDefaultWhenAvailable();
  136. $this->element->setDisableTranslator(true);
  137. $this->assertNull($this->element->getTranslator());
  138. }
  139. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  140. {
  141. $this->element->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  142. $this->assertEquals('foobar', $this->element->getName());
  143. try {
  144. $this->element->setName('%\^&*)\(%$#@!.}{;-,');
  145. $this->fail('Empty names should raise exception');
  146. } catch (Zend_Form_Exception $e) {
  147. $this->assertContains('Invalid name provided', $e->getMessage());
  148. }
  149. }
  150. public function testZeroIsAllowedAsElementName()
  151. {
  152. try {
  153. $this->element->setName(0);
  154. $this->assertSame('0', $this->element->getName());
  155. } catch (Zend_Form_Exception $e) {
  156. $this->fail('Should allow zero as element name');
  157. }
  158. }
  159. /**
  160. * @group ZF-2851
  161. */
  162. public function testSetNameShouldNotAllowEmptyString()
  163. {
  164. foreach (array('', ' ', ' ') as $name) {
  165. try {
  166. $this->element->setName($name);
  167. $this->fail('setName() should not allow empty string');
  168. } catch (Zend_Form_Exception $e) {
  169. $this->assertContains('Invalid name', $e->getMessage());
  170. }
  171. }
  172. }
  173. public function testElementValueInitiallyNull()
  174. {
  175. $this->assertNull($this->element->getValue());
  176. }
  177. public function testValueAccessorsWork()
  178. {
  179. $this->element->setValue('bar');
  180. $this->assertContains('bar', $this->element->getValue());
  181. }
  182. public function testGetValueFiltersValue()
  183. {
  184. $this->element->setValue('This 0 is 1 a-2-TEST')
  185. ->addFilter('alnum')
  186. ->addFilter('stringToUpper');
  187. $test = $this->element->getValue();
  188. $this->assertEquals('THIS0IS1A2TEST', $test);
  189. }
  190. public function checkFilterValues($item, $key)
  191. {
  192. $this->assertRegexp('/^[A-Z]+$/', $item);
  193. }
  194. public function testRetrievingArrayValueFiltersAllArrayValues()
  195. {
  196. $this->element->setValue(array(
  197. 'foo',
  198. array(
  199. 'bar',
  200. 'baz'
  201. ),
  202. 'bat'
  203. ))
  204. ->setIsArray(true)
  205. ->addFilter('StringToUpper');
  206. $test = $this->element->getValue();
  207. $this->assertTrue(is_array($test));
  208. array_walk_recursive($test, array($this, 'checkFilterValues'));
  209. }
  210. public function testRetrievingArrayValueDoesNotFilterAllValuesWhenNotIsArray()
  211. {
  212. $values = array(
  213. 'foo',
  214. array(
  215. 'bar',
  216. 'baz'
  217. ),
  218. 'bat'
  219. );
  220. $this->element->setValue($values)
  221. ->addFilter(new Zend_Form_ElementTest_ArrayFilter());
  222. $test = $this->element->getValue();
  223. $this->assertTrue(is_array($test));
  224. require_once 'Zend/Json.php';
  225. $test = Zend_Json::encode($test);
  226. $this->assertNotContains('foo', $test);
  227. foreach (array('bar', 'baz', 'bat') as $value) {
  228. $this->assertContains($value, $test);
  229. }
  230. }
  231. public function testGetUnfilteredValueRetrievesOriginalValue()
  232. {
  233. $this->element->setValue('bar');
  234. $this->assertSame('bar', $this->element->getUnfilteredValue());
  235. }
  236. public function testLabelInitiallyNull()
  237. {
  238. $this->assertNull($this->element->getLabel());
  239. }
  240. public function testLabelAccessorsWork()
  241. {
  242. $this->element->setLabel('FooBar');
  243. $this->assertEquals('FooBar', $this->element->getLabel());
  244. }
  245. public function testOrderNullByDefault()
  246. {
  247. $this->assertNull($this->element->getOrder());
  248. }
  249. public function testCanSetOrder()
  250. {
  251. $this->testOrderNullByDefault();
  252. $this->element->setOrder(50);
  253. $this->assertEquals(50, $this->element->getOrder());
  254. }
  255. public function testRequiredFlagFalseByDefault()
  256. {
  257. $this->assertFalse($this->element->isRequired());
  258. }
  259. public function testRequiredAcccessorsWork()
  260. {
  261. $this->assertFalse($this->element->isRequired());
  262. $this->element->setRequired(true);
  263. $this->assertTrue($this->element->isRequired());
  264. }
  265. public function testIsValidInsertsNotEmptyValidatorWhenElementIsRequiredByDefault()
  266. {
  267. $this->_checkZf2794();
  268. $this->element->setRequired(true);
  269. $this->assertFalse($this->element->isValid(''));
  270. $validator = $this->element->getValidator('NotEmpty');
  271. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  272. $this->assertTrue($validator->zfBreakChainOnFailure);
  273. }
  274. /**
  275. * @group ZF-2862
  276. */
  277. public function testBreakChainOnFailureFlagsForExistingValidatorsRemainSetWhenNotEmptyValidatorAutoInserted()
  278. {
  279. $this->_checkZf2794();
  280. $username = new Zend_Form_Element('username');
  281. $username->addValidator('stringLength', true, array(5, 20))
  282. ->addValidator('regex', true, array('/^[a-zA-Z0-9_]*$/'))
  283. ->addFilter('StringToLower')
  284. ->setRequired(true);
  285. $form = new Zend_Form(array('elements' => array($username)));
  286. $form->isValid(array('username' => '#'));
  287. $validator = $username->getValidator('stringLength');
  288. $this->assertTrue($validator->zfBreakChainOnFailure);
  289. $validator = $username->getValidator('regex');
  290. $this->assertTrue($validator->zfBreakChainOnFailure);
  291. }
  292. public function testAutoInsertNotEmptyValidatorFlagTrueByDefault()
  293. {
  294. $this->assertTrue($this->element->autoInsertNotEmptyValidator());
  295. }
  296. public function testCanSetAutoInsertNotEmptyValidatorFlag()
  297. {
  298. $this->testAutoInsertNotEmptyValidatorFlagTrueByDefault();
  299. $this->element->setAutoInsertNotEmptyValidator(false);
  300. $this->assertFalse($this->element->autoInsertNotEmptyValidator());
  301. $this->element->setAutoInsertNotEmptyValidator(true);
  302. $this->assertTrue($this->element->autoInsertNotEmptyValidator());
  303. }
  304. public function testIsValidDoesNotInsertNotEmptyValidatorWhenElementIsRequiredButAutoInsertNotEmptyValidatorFlagIsFalse()
  305. {
  306. $this->element->setAutoInsertNotEmptyValidator(false)
  307. ->setRequired(true);
  308. $this->assertTrue($this->element->isValid(''));
  309. }
  310. public function testDescriptionInitiallyNull()
  311. {
  312. $this->assertNull($this->element->getDescription());
  313. }
  314. public function testCanSetDescription()
  315. {
  316. $this->testDescriptionInitiallyNull();
  317. $this->element->setDescription('element hint');
  318. $this->assertEquals('element hint', $this->element->getDescription());
  319. }
  320. public function testElementIsNotArrayByDefault()
  321. {
  322. $this->assertFalse($this->element->isArray());
  323. }
  324. public function testCanSetArrayFlag()
  325. {
  326. $this->testElementIsNotArrayByDefault();
  327. $this->element->setIsArray(true);
  328. $this->assertTrue($this->element->isArray());
  329. $this->element->setIsArray(false);
  330. $this->assertFalse($this->element->isArray());
  331. }
  332. public function testElementBelongsToNullByDefault()
  333. {
  334. $this->assertNull($this->element->getBelongsTo());
  335. }
  336. public function testCanSetArrayElementBelongsTo()
  337. {
  338. $this->testElementBelongsToNullByDefault();
  339. $this->element->setBelongsTo('foo');
  340. $this->assertEquals('foo', $this->element->getBelongsTo());
  341. }
  342. public function testArrayElementBelongsToNormalizedToValidVariableCharactersOnly()
  343. {
  344. $this->testElementBelongsToNullByDefault();
  345. $this->element->setBelongsTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
  346. $this->assertEquals('foobar', $this->element->getBelongsTo());
  347. }
  348. public function testGetTypeReturnsCurrentElementClass()
  349. {
  350. $this->assertEquals('Zend_Form_Element', $this->element->getType());
  351. }
  352. public function testCanUseAccessorsToSetIndidualAttribs()
  353. {
  354. $this->element->setAttrib('foo', 'bar')
  355. ->setAttrib('bar', 'baz')
  356. ->setAttrib('baz', 'bat');
  357. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  358. $this->assertEquals('baz', $this->element->getAttrib('bar'));
  359. $this->assertEquals('bat', $this->element->getAttrib('baz'));
  360. }
  361. public function testGetUndefinedAttribShouldReturnNull()
  362. {
  363. $this->assertNull($this->element->getAttrib('bogus'));
  364. }
  365. public function testSetAttribThrowsExceptionsForKeysWithLeadingUnderscores()
  366. {
  367. try {
  368. $this->element->setAttrib('_foo', 'bar');
  369. $this->fail('setAttrib() should throw an exception for invalid keys');
  370. } catch (Zend_Form_Exception $e) {
  371. $this->assertContains('Invalid attribute', $e->getMessage());
  372. }
  373. }
  374. public function testPassingNullValueToSetAttribUnsetsAttrib()
  375. {
  376. $this->element->setAttrib('foo', 'bar');
  377. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  378. $this->element->setAttrib('foo', null);
  379. $this->assertFalse(isset($this->element->foo));
  380. }
  381. public function testSetAttribsSetsMultipleAttribs()
  382. {
  383. $this->element->setAttribs(array(
  384. 'foo' => 'bar',
  385. 'bar' => 'baz',
  386. 'baz' => 'bat'
  387. ));
  388. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  389. $this->assertEquals('baz', $this->element->getAttrib('bar'));
  390. $this->assertEquals('bat', $this->element->getAttrib('baz'));
  391. }
  392. public function testGetAttribsRetrievesAllAttributes()
  393. {
  394. $attribs = array(
  395. 'foo' => 'bar',
  396. 'bar' => 'baz',
  397. 'baz' => 'bat'
  398. );
  399. $this->element->setAttribs($attribs);
  400. $attribs['helper'] = 'formText';
  401. $received = $this->element->getAttribs();
  402. $this->assertEquals($attribs, $received);
  403. }
  404. public function testPassingNullValuesToSetAttribsUnsetsAttribs()
  405. {
  406. $this->testSetAttribsSetsMultipleAttribs();
  407. $this->element->setAttribs(array('foo' => null));
  408. $this->assertNull($this->element->foo);
  409. }
  410. public function testRetrievingOverloadedValuesThrowsExceptionWithInvalidKey()
  411. {
  412. try {
  413. $name = $this->element->_name;
  414. $this->fail('Overloading should not return protected or private members');
  415. } catch (Zend_Form_Exception $e) {
  416. $this->assertContains('Cannot retrieve value for protected/private', $e->getMessage());
  417. }
  418. }
  419. public function testCanSetAndRetrieveAttribsViaOverloading()
  420. {
  421. $this->element->foo = 'bar';
  422. $this->assertEquals('bar', $this->element->foo);
  423. }
  424. public function testGetPluginLoaderRetrievesDefaultValidatorPluginLoader()
  425. {
  426. $loader = $this->element->getPluginLoader('validate');
  427. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  428. $paths = $loader->getPaths('Zend_Validate');
  429. $this->assertTrue(is_array($paths), var_export($loader, 1));
  430. $this->assertTrue(0 < count($paths));
  431. $this->assertContains('Validate', $paths[0]);
  432. }
  433. public function testGetPluginLoaderRetrievesDefaultFilterPluginLoader()
  434. {
  435. $loader = $this->element->getPluginLoader('filter');
  436. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  437. $paths = $loader->getPaths('Zend_Filter');
  438. $this->assertTrue(is_array($paths));
  439. $this->assertTrue(0 < count($paths));
  440. $this->assertContains('Filter', $paths[0]);
  441. }
  442. public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
  443. {
  444. $loader = $this->element->getPluginLoader('decorator');
  445. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  446. $paths = $loader->getPaths('Zend_Form_Decorator');
  447. $this->assertTrue(is_array($paths));
  448. $this->assertTrue(0 < count($paths));
  449. $this->assertContains('Decorator', $paths[0]);
  450. }
  451. public function testCanSetCustomValidatorPluginLoader()
  452. {
  453. $loader = new Zend_Loader_PluginLoader();
  454. $this->element->setPluginLoader($loader, 'validate');
  455. $test = $this->element->getPluginLoader('validate');
  456. $this->assertSame($loader, $test);
  457. }
  458. public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
  459. {
  460. $loader = new Zend_Loader_PluginLoader();
  461. try {
  462. $this->element->setPluginLoader($loader, 'foo');
  463. $this->fail('Invalid loader type should raise exception');
  464. } catch (Zend_Form_Exception $e) {
  465. $this->assertContains('Invalid type', $e->getMessage());
  466. }
  467. }
  468. public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
  469. {
  470. try {
  471. $this->element->getPluginLoader('foo');
  472. $this->fail('Invalid loader type should raise exception');
  473. } catch (Zend_Form_Exception $e) {
  474. $this->assertContains('Invalid type', $e->getMessage());
  475. }
  476. }
  477. public function testCanSetCustomFilterPluginLoader()
  478. {
  479. $loader = new Zend_Loader_PluginLoader();
  480. $this->element->setPluginLoader($loader, 'filter');
  481. $test = $this->element->getPluginLoader('filter');
  482. $this->assertSame($loader, $test);
  483. }
  484. public function testCanSetCustomDecoratorPluginLoader()
  485. {
  486. $loader = new Zend_Loader_PluginLoader();
  487. $this->element->setPluginLoader($loader, 'decorator');
  488. $test = $this->element->getPluginLoader('decorator');
  489. $this->assertSame($loader, $test);
  490. }
  491. public function testPassingInvalidLoaderTypeToAddPrefixPathThrowsException()
  492. {
  493. try {
  494. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
  495. $this->fail('Invalid loader type should raise exception');
  496. } catch (Zend_Form_Exception $e) {
  497. $this->assertContains('Invalid type', $e->getMessage());
  498. }
  499. }
  500. public function testCanAddValidatorPluginLoaderPrefixPath()
  501. {
  502. $loader = $this->element->getPluginLoader('validate');
  503. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  504. $paths = $loader->getPaths('Zend_Form');
  505. $this->assertTrue(is_array($paths));
  506. $this->assertContains('Form', $paths[0]);
  507. }
  508. public function testAddingValidatorPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  509. {
  510. $validateLoader = $this->element->getPluginLoader('validate');
  511. $filterLoader = $this->element->getPluginLoader('filter');
  512. $decoratorLoader = $this->element->getPluginLoader('decorator');
  513. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  514. $this->assertFalse($filterLoader->getPaths('Zend_Form'));
  515. $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
  516. }
  517. public function testCanAddFilterPluginLoaderPrefixPath()
  518. {
  519. $loader = $this->element->getPluginLoader('validate');
  520. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  521. $paths = $loader->getPaths('Zend_Form');
  522. $this->assertTrue(is_array($paths));
  523. $this->assertContains('Form', $paths[0]);
  524. }
  525. public function testAddingFilterPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  526. {
  527. $filterLoader = $this->element->getPluginLoader('filter');
  528. $validateLoader = $this->element->getPluginLoader('validate');
  529. $decoratorLoader = $this->element->getPluginLoader('decorator');
  530. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'filter');
  531. $this->assertFalse($validateLoader->getPaths('Zend_Form'));
  532. $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
  533. }
  534. public function testCanAddDecoratorPluginLoaderPrefixPath()
  535. {
  536. $loader = $this->element->getPluginLoader('decorator');
  537. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  538. $paths = $loader->getPaths('Zend_Foo');
  539. $this->assertTrue(is_array($paths));
  540. $this->assertContains('Foo', $paths[0]);
  541. }
  542. public function testAddingDecoratorrPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  543. {
  544. $decoratorLoader = $this->element->getPluginLoader('decorator');
  545. $filterLoader = $this->element->getPluginLoader('filter');
  546. $validateLoader = $this->element->getPluginLoader('validate');
  547. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  548. $this->assertFalse($validateLoader->getPaths('Zend_Foo'));
  549. $this->assertFalse($filterLoader->getPaths('Zend_Foo'));
  550. }
  551. public function testCanAddAllPluginLoaderPrefixPathsSimultaneously()
  552. {
  553. $validatorLoader = new Zend_Loader_PluginLoader();
  554. $filterLoader = new Zend_Loader_PluginLoader();
  555. $decoratorLoader = new Zend_Loader_PluginLoader();
  556. $this->element->setPluginLoader($validatorLoader, 'validate')
  557. ->setPluginLoader($filterLoader, 'filter')
  558. ->setPluginLoader($decoratorLoader, 'decorator')
  559. ->addPrefixPath('Zend', 'Zend/');
  560. $paths = $filterLoader->getPaths('Zend_Filter');
  561. $this->assertTrue(is_array($paths));
  562. $this->assertContains('Filter', $paths[0]);
  563. $paths = $validatorLoader->getPaths('Zend_Validate');
  564. $this->assertTrue(is_array($paths));
  565. $this->assertContains('Validate', $paths[0]);
  566. $paths = $decoratorLoader->getPaths('Zend_Decorator');
  567. $this->assertTrue(is_array($paths), var_export($paths, 1));
  568. $this->assertContains('Decorator', $paths[0]);
  569. }
  570. public function testPassingInvalidValidatorToAddValidatorThrowsException()
  571. {
  572. try {
  573. $this->element->addValidator(123);
  574. $this->fail('Invalid validator should raise exception');
  575. } catch (Zend_Form_Exception $e) {
  576. $this->assertContains('Invalid validator', $e->getMessage());
  577. }
  578. }
  579. public function testCanAddSingleValidatorAsString()
  580. {
  581. $this->_checkZf2794();
  582. $this->assertFalse($this->element->getValidator('digits'));
  583. $this->element->addValidator('digits');
  584. $validator = $this->element->getValidator('digits');
  585. $this->assertTrue($validator instanceof Zend_Validate_Digits, var_export($validator, 1));
  586. $this->assertFalse($validator->zfBreakChainOnFailure);
  587. }
  588. public function testCanNotRetrieveSingleValidatorRegisteredAsStringUsingClassName()
  589. {
  590. $this->assertFalse($this->element->getValidator('digits'));
  591. $this->element->addValidator('digits');
  592. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  593. }
  594. public function testCanAddSingleValidatorAsValidatorObject()
  595. {
  596. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  597. require_once 'Zend/Validate/Digits.php';
  598. $validator = new Zend_Validate_Digits();
  599. $this->element->addValidator($validator);
  600. $test = $this->element->getValidator('Zend_Validate_Digits');
  601. $this->assertSame($validator, $test);
  602. $this->assertFalse($validator->zfBreakChainOnFailure);
  603. }
  604. public function testOptionsAreCastToArrayWhenAddingValidator()
  605. {
  606. $this->_checkZf2794();
  607. try {
  608. $this->element->addValidator('Alnum', false, true);
  609. } catch (Exception $e) {
  610. $this->fail('Should be able to add non-array validator options');
  611. }
  612. $validator = $this->element->getValidator('Alnum');
  613. $this->assertTrue($validator instanceof Zend_Validate_Alnum);
  614. $this->assertTrue($validator->allowWhiteSpace);
  615. }
  616. public function testCanRetrieveSingleValidatorRegisteredAsValidatorObjectUsingShortName()
  617. {
  618. $this->_checkZf2794();
  619. $this->assertFalse($this->element->getValidator('digits'));
  620. require_once 'Zend/Validate/Digits.php';
  621. $validator = new Zend_Validate_Digits();
  622. $this->element->addValidator($validator);
  623. $test = $this->element->getValidator('digits');
  624. $this->assertSame($validator, $test);
  625. $this->assertFalse($validator->zfBreakChainOnFailure);
  626. }
  627. public function testRetrievingNamedValidatorShouldNotReorderValidators()
  628. {
  629. $this->element->addValidators(array(
  630. 'NotEmpty',
  631. 'Alnum',
  632. 'Digits',
  633. ));
  634. $validator = $this->element->getValidator('Alnum');
  635. $validators = $this->element->getValidators();
  636. $i = 0;
  637. $order = array();
  638. foreach (array_keys($validators) as $name) {
  639. $order[$name] = $i;
  640. ++$i;
  641. }
  642. $this->assertEquals(1, $order['Zend_Validate_Alnum'], var_export($order, 1));
  643. }
  644. public function testCanAddMultipleValidators()
  645. {
  646. $this->_checkZf2794();
  647. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  648. $this->assertFalse($this->element->getValidator('Zend_Validate_Alnum'));
  649. $this->element->addValidators(array('digits', 'alnum'));
  650. $digits = $this->element->getValidator('digits');
  651. $this->assertTrue($digits instanceof Zend_Validate_Digits);
  652. $alnum = $this->element->getValidator('alnum');
  653. $this->assertTrue($alnum instanceof Zend_Validate_Alnum);
  654. }
  655. public function testRemovingUnregisteredValidatorReturnsObjectInstance()
  656. {
  657. $this->assertSame($this->element, $this->element->removeValidator('bogus'));
  658. }
  659. public function testPassingMessagesOptionToAddValidatorSetsValidatorMessages()
  660. {
  661. $messageTemplates = array(
  662. Zend_Validate_Digits::NOT_DIGITS => 'Value should only contain digits',
  663. Zend_Validate_Digits::STRING_EMPTY => 'Value needs some digits',
  664. );
  665. $this->element->setAllowEmpty(false)
  666. ->addValidator('digits', false, array('messages' => $messageTemplates));
  667. $this->element->isValid('');
  668. $messages = $this->element->getMessages();
  669. $found = false;
  670. foreach ($messages as $key => $message) {
  671. if ($key == Zend_Validate_Digits::STRING_EMPTY) {
  672. $found = true;
  673. break;
  674. }
  675. }
  676. $this->assertTrue($found, 'Empty string message not found: ' . var_export($messages, 1));
  677. $this->assertEquals($messageTemplates[Zend_Validate_Digits::STRING_EMPTY], $message);
  678. $this->element->isValid('abc');
  679. $messages = $this->element->getMessages();
  680. $found = false;
  681. foreach ($messages as $key => $message) {
  682. if ($key == Zend_Validate_Digits::NOT_DIGITS) {
  683. $found = true;
  684. break;
  685. }
  686. }
  687. $this->assertTrue($found, 'Not digits message not found');
  688. $this->assertEquals($messageTemplates[Zend_Validate_Digits::NOT_DIGITS], $message);
  689. }
  690. public function testCanPassSingleMessageToValidatorToSetValidatorMessages()
  691. {
  692. $this->_checkZf2794();
  693. $message = 'My custom empty message';
  694. $this->element->addValidator('notEmpty', false, array('messages' => $message))
  695. ->setRequired(true);
  696. $this->element->isValid('');
  697. $messages = $this->element->getMessages();
  698. $this->assertEquals(1, count($messages));
  699. $this->assertEquals($message, current($messages));
  700. }
  701. public function testMessagesAreTranslatedForCurrentLocale()
  702. {
  703. $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
  704. $translations = include($localeFile);
  705. $translator = new Zend_Translate('array', $translations, 'en');
  706. $translator->setLocale('en');
  707. $this->element->setAllowEmpty(false)
  708. ->setTranslator($translator)
  709. ->addValidator('digits');
  710. $this->element->isValid('');
  711. $messages = $this->element->getMessages();
  712. $found = false;
  713. foreach ($messages as $key => $message) {
  714. if ($key == 'digitsStringEmpty') {
  715. $found = true;
  716. break;
  717. }
  718. }
  719. $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
  720. $this->assertEquals($translations['stringEmpty'], $message);
  721. $this->element->isValid('abc');
  722. $messages = $this->element->getMessages();
  723. $found = false;
  724. foreach ($messages as $key => $message) {
  725. if ($key == 'notDigits') {
  726. $found = true;
  727. break;
  728. }
  729. }
  730. $this->assertTrue($found, 'Not Digits message not found');
  731. $this->assertEquals($translations['notDigits'], $message);
  732. }
  733. /**#@+
  734. * @group ZF-2988
  735. */
  736. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  737. {
  738. $this->element->addValidator('Alpha');
  739. $this->element->addErrorMessage('Invalid value entered');
  740. $this->assertFalse($this->element->isValid(123));
  741. $messages = $this->element->getMessages();
  742. $this->assertEquals(1, count($messages));
  743. $this->assertEquals('Invalid value entered', array_shift($messages));
  744. }
  745. public function testCustomErrorMessagesShouldBeManagedInAStack()
  746. {
  747. $this->element->addValidator('Alpha');
  748. $this->element->addErrorMessage('Invalid value entered');
  749. $this->element->addErrorMessage('Really, it is not valid');
  750. $messages = $this->element->getErrorMessages();
  751. $this->assertEquals(2, count($messages));
  752. $this->assertFalse($this->element->isValid(123));
  753. $messages = $this->element->getMessages();
  754. $this->assertEquals(2, count($messages));
  755. $this->assertEquals('Invalid value entered', array_shift($messages));
  756. $this->assertEquals('Really, it is not valid', array_shift($messages));
  757. }
  758. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  759. {
  760. $set1 = array('foo', 'bar', 'baz');
  761. $this->element->addErrorMessages($set1);
  762. $this->assertSame($set1, $this->element->getErrorMessages());
  763. }
  764. public function testSetErrorMessagesShouldOverwriteMessages()
  765. {
  766. $set1 = array('foo', 'bar', 'baz');
  767. $set2 = array('bat', 'cat');
  768. $this->element->addErrorMessages($set1);
  769. $this->assertSame($set1, $this->element->getErrorMessages());
  770. $this->element->setErrorMessages($set2);
  771. $this->assertSame($set2, $this->element->getErrorMessages());
  772. }
  773. public function testCustomErrorMessageStackShouldBeClearable()
  774. {
  775. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  776. $this->element->clearErrorMessages();
  777. $messages = $this->element->getErrorMessages();
  778. $this->assertTrue(empty($messages));
  779. }
  780. public function testCustomErrorMessagesShouldBeTranslated()
  781. {
  782. $translations = array(
  783. 'foo' => 'Foo message',
  784. );
  785. $translate = new Zend_Translate('array', $translations);
  786. $this->element->setTranslator($translate)
  787. ->addErrorMessage('foo')
  788. ->addValidator('Alpha');
  789. $this->assertFalse($this->element->isValid(123));
  790. $messages = $this->element->getMessages();
  791. $this->assertEquals(1, count($messages));
  792. $this->assertEquals('Foo message', array_shift($messages));
  793. }
  794. public function testCustomErrorMessagesShouldAllowValueSubstitution()
  795. {
  796. $this->element->addErrorMessage('"%value%" is an invalid value')
  797. ->addValidator('Alpha');
  798. $this->assertFalse($this->element->isValid(123));
  799. $this->assertTrue($this->element->hasErrors());
  800. $messages = $this->element->getMessages();
  801. $this->assertEquals(1, count($messages));
  802. $this->assertEquals('"123" is an invalid value', array_shift($messages));
  803. }
  804. public function testShouldAllowMarkingElementAsInvalid()
  805. {
  806. $this->element->setValue('foo');
  807. $this->element->addErrorMessage('Invalid value entered');
  808. $this->assertFalse($this->element->hasErrors());
  809. $this->element->markAsError();
  810. $this->assertTrue($this->element->hasErrors());
  811. $messages = $this->element->getMessages();
  812. $this->assertEquals(1, count($messages));
  813. $this->assertEquals('Invalid value entered', array_shift($messages));
  814. }
  815. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  816. {
  817. $this->element->setValue('foo');
  818. $this->assertFalse($this->element->hasErrors());
  819. $this->element->setErrors(array('Error 1', 'Error 2'))
  820. ->addError('Error 3')
  821. ->addErrors(array('Error 4', 'Error 5'));
  822. $this->assertTrue($this->element->hasErrors());
  823. $messages = $this->element->getMessages();
  824. $this->assertEquals(5, count($messages));
  825. foreach (range(1, 5) as $id) {
  826. $message = 'Error ' . $id;
  827. $this->assertContains($message, $messages);
  828. }
  829. }
  830. public function testHasErrorsShouldIndicateStatusOfValidationErrors()
  831. {
  832. $this->element->setValue('foo');
  833. $this->assertFalse($this->element->hasErrors());
  834. $this->element->markAsError();
  835. $this->assertTrue($this->element->hasErrors());
  836. }
  837. /**#@-*/
  838. public function testAddingErrorToArrayElementShouldLoopOverAllValues()
  839. {
  840. $this->element->setIsArray(true)
  841. ->setValue(array('foo', 'bar', 'baz'))
  842. ->addError('error with value %value%');
  843. $errors = $this->element->getMessages();
  844. require_once 'Zend/Json.php';
  845. $errors = Zend_Json::encode($errors);
  846. foreach (array('foo', 'bar', 'baz') as $value) {
  847. $message = 'error with value ' . $value;
  848. $this->assertContains($message, $errors);
  849. }
  850. }
  851. /** ZF-2568 */
  852. public function testTranslatedMessagesCanContainVariableSubstitution()
  853. {
  854. $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
  855. $translations = include($localeFile);
  856. $translations['notDigits'] .= ' "%value%"';
  857. $translator = new Zend_Translate('array', $translations, 'en');
  858. $translator->setLocale('en');
  859. $this->element->setAllowEmpty(false)
  860. ->setTranslator($translator)
  861. ->addValidator('digits');
  862. $this->element->isValid('abc');
  863. $messages = $this->element->getMessages();
  864. $found = false;
  865. foreach ($messages as $key => $message) {
  866. if ($key == 'notDigits') {
  867. $found = true;
  868. break;
  869. }
  870. }
  871. $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
  872. $this->assertContains(' "abc"', $message);
  873. $this->assertContains('Translating the notDigits string', $message);
  874. }
  875. public function testCanRemoveValidator()
  876. {
  877. $this->_checkZf2794();
  878. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  879. $this->element->addValidator('digits');
  880. $digits = $this->element->getValidator('digits');
  881. $this->assertTrue($digits instanceof Zend_Validate_Digits);
  882. $this->element->removeValidator('digits');
  883. $this->assertFalse($this->element->getValidator('digits'));
  884. }
  885. public function testCanClearAllValidators()
  886. {
  887. $this->_checkZf2794();
  888. $this->testCanAddMultipleValidators();
  889. $validators = $this->element->getValidators();
  890. $this->element->clearValidators();
  891. $test = $this->element->getValidators();
  892. $this->assertNotEquals($validators, $test);
  893. $this->assertTrue(empty($test));
  894. foreach (array_keys($validators) as $validator) {
  895. $this->assertFalse($this->element->getValidator($validator));
  896. }
  897. }
  898. public function testCanValidateElement()
  899. {
  900. $this->element->addValidator(new Zend_Validate_NotEmpty())
  901. ->addValidator(new Zend_Validate_EmailAddress());
  902. try {
  903. $result = $this->element->isValid('matthew@zend.com');
  904. } catch (Exception $e) {
  905. $this->fail('Validating an element should work');
  906. }
  907. }
  908. public function testCanValidateArrayValue()
  909. {
  910. $this->element->setIsArray(true)
  911. ->addValidator('InArray', false, array(array('foo', 'bar', 'baz', 'bat')));
  912. $this->assertTrue($this->element->isValid(array('foo', 'bat')));
  913. }
  914. public function testShouldAllowZeroAsNonEmptyValue()
  915. {
  916. $this->element->addValidator('between', false, array(1, 100));
  917. $this->assertFalse($this->element->isValid('0'));
  918. }
  919. public function testIsValidPopulatesElementValue()
  920. {
  921. $this->testCanValidateElement();
  922. $this->assertEquals('matthew@zend.com', $this->element->getValue());
  923. }
  924. public function testErrorsPopulatedFollowingFailedIsValidCheck()
  925. {
  926. $this->element->addValidator(new Zend_Validate_NotEmpty())
  927. ->addValidator(new Zend_Validate_EmailAddress());
  928. $result = $this->element->isValid('matthew');
  929. if ($result) {
  930. $this->fail('Invalid data should fail validations');
  931. }
  932. $errors = $this->element->getErrors();
  933. $this->assertTrue(is_array($errors));
  934. $this->assertTrue(0 < count($errors));
  935. }
  936. public function testMessagesPopulatedFollowingFailedIsValidCheck()
  937. {
  938. require_once 'Zend/Validate/NotEmpty.php';
  939. require_once 'Zend/Validate/EmailAddress.php';
  940. $this->element->addValidator(new Zend_Validate_NotEmpty())
  941. ->addValidator(new Zend_Validate_EmailAddress());
  942. $result = $this->element->isValid('matthew');
  943. if ($result) {
  944. $this->fail('Invalid data should fail validations');
  945. }
  946. $messages = $this->element->getMessages();
  947. $this->assertTrue(is_array($messages));
  948. $this->assertTrue(0 < count($messages));
  949. }
  950. public function testOptionalElementDoesNotPerformValidationsOnEmptyValuesByDefault()
  951. {
  952. $this->element->addValidator(new Zend_Validate_EmailAddress());
  953. $result = $this->element->isValid('');
  954. if (!$result) {
  955. $this->fail('Empty data should not fail validations');
  956. }
  957. $errors = $this->element->getErrors();
  958. $this->assertTrue(is_array($errors));
  959. $this->assertTrue(empty($errors));
  960. }
  961. public function testOptionalElementDoesPerformValidationsWhenAllowEmptyIsFalse()
  962. {
  963. $this->element->setAllowEmpty(false)
  964. ->addValidator(new Zend_Validate_EmailAddress());
  965. $result = $this->element->isValid('');
  966. if ($result) {
  967. $this->fail('Empty data should fail validations when AllowEmpty is false');
  968. }
  969. $errors = $this->element->getErrors();
  970. $this->assertTrue(is_array($errors));
  971. $this->assertTrue(0 < count($errors));
  972. }
  973. public function testAddingInvalidFilterTypeThrowsException()
  974. {
  975. try {
  976. $this->element->addFilter(123);
  977. $this->fail('Invalid filter type should raise exception');
  978. } catch (Zend_Form_Exception $e) {
  979. $this->assertContains('Invalid filter', $e->getMessage());
  980. }
  981. }
  982. public function testCanAddSingleFilterAsString()
  983. {
  984. $this->_checkZf2794();
  985. $this->assertFalse($this->element->getFilter('digits'));
  986. $this->element->addFilter('digits');
  987. $filter = $this->element->getFilter('digits');
  988. $this->assertTrue($filter instanceof Zend_Filter_Digits);
  989. }
  990. public function testCanNotRetrieveSingleFilterRegisteredAsStringUsingClassName()
  991. {
  992. $this->assertFalse($this->element->getFilter('digits'));
  993. $this->element->addFilter('digits');
  994. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  995. }
  996. public function testCanAddSingleFilterAsFilterObject()
  997. {
  998. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  999. require_once 'Zend/Filter/Digits.php';
  1000. $filter = new Zend_Filter_Digits();
  1001. $this->element->addFilter($filter);
  1002. $test = $this->element->getFilter('Zend_Filter_Digits');
  1003. $this->assertSame($filter, $test);
  1004. }
  1005. public function testCanRetrieveSingleFilterRegisteredAsFilterObjectUsingShortName()
  1006. {
  1007. $this->_checkZf2794();
  1008. $this->assertFalse($this->element->getFilter('digits'));
  1009. require_once 'Zend/Filter/Digits.php';
  1010. $filter = new Zend_Filter_Digits();
  1011. $this->element->addFilter($filter);
  1012. $test = $this->element->getFilter('digits');
  1013. }
  1014. public function testRetrievingNamedFilterShouldNotReorderFilters()
  1015. {
  1016. $this->element->addFilters(array(
  1017. 'Alpha',
  1018. 'Alnum',
  1019. 'Digits',
  1020. ));
  1021. $filter = $this->element->getFilter('Alnum');
  1022. $filters = $this->element->getFilters();
  1023. $i = 0;
  1024. $order = array();
  1025. foreach (array_keys($filters) as $name) {
  1026. $order[$name] = $i;
  1027. ++$i;
  1028. }
  1029. $this->assertEquals(1, $order['Zend_Filter_Alnum'], var_export($order, 1));
  1030. }
  1031. public function testOptionsAreCastToArrayWhenAddingFilter()
  1032. {
  1033. $this->_checkZf2794();
  1034. try {
  1035. $this->element->addFilter('Alnum', true);
  1036. } catch (Exception $e) {
  1037. $this->fail('Should be able to add non-array filter options');
  1038. }
  1039. $filter = $this->element->getFilter('Alnum');
  1040. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  1041. $this->assertTrue($filter->allowWhiteSpace);
  1042. }
  1043. public function testShouldUseFilterConstructorOptionsAsPassedToAddFilter()
  1044. {
  1045. $this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'UTF-8')));
  1046. $filter = $this->element->getFilter('HtmlEntities');
  1047. $this->assertTrue($filter instanceof Zend_Filter_HtmlEntities);
  1048. $this->assertEquals(ENT_QUOTES, $filter->getQuoteStyle());
  1049. $this->assertEquals('UTF-8', $filter->getCharSet());
  1050. }
  1051. public function testCanAddMultipleFilters()
  1052. {
  1053. $this->_checkZf2794();
  1054. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  1055. $this->assertFalse($this->element->getFilter('Zend_Filter_Alnum'));
  1056. $this->element->addFilters(array('digits', 'alnum'));
  1057. $digits = $this->element->getFilter('digits');
  1058. $this->assertTrue($digits instanceof Zend_Filter_Digits);
  1059. $alnum = $this->element->getFilter('alnum');
  1060. $this->assertTrue($alnum instanceof Zend_Filter_Alnum);
  1061. }
  1062. public function testRemovingUnregisteredFilterReturnsObjectInstance()
  1063. {
  1064. $this->assertSame($this->element, $this->element->removeFilter('bogus'));
  1065. }
  1066. public function testCanRemoveFilter()
  1067. {
  1068. $this->_checkZf2794();
  1069. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  1070. $this->element->addFilter('digits');
  1071. $digits = $this->element->getFilter('digits');
  1072. $this->assertTrue($digits instanceof Zend_Filter_Digits);
  1073. $this->element->removeFilter('digits');
  1074. $this->assertFalse($this->element->getFilter('digits'));
  1075. }
  1076. public function testCanClearAllFilters()
  1077. {
  1078. $this->_checkZf2794();
  1079. $this->testCanAddMultipleFilters();
  1080. $filters = $this->element->getFilters();
  1081. $this->element->clearFilters();
  1082. $test = $this->element->getFilters();
  1083. $this->assertNotEquals($filters, $test);
  1084. $this->assertTrue(empty($test));
  1085. foreach (array_keys($filters) as $filter) {
  1086. $this->assertFalse($this->element->getFilter($filter));
  1087. }
  1088. }
  1089. public function testGetViewReturnsNullWithNoViewRenderer()
  1090. {
  1091. $this->assertNull($this->element->getView());
  1092. }
  1093. public function testGetViewReturnsViewRendererViewInstanceIfViewRendererActive()
  1094. {
  1095. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  1096. $viewRenderer->initView();
  1097. $view = $viewRenderer->view;
  1098. $test = $this->element->getView();
  1099. $this->assertSame($view, $test);
  1100. }
  1101. public function testCanSetView()
  1102. {
  1103. $view = new Zend_View();
  1104. $this->assertNull($this->element->getView());
  1105. $this->element->setView($view);
  1106. $received = $this->element->getView();
  1107. $this->assertSame($view, $received);
  1108. }
  1109. public function testViewHelperDecoratorRegisteredByDefault()
  1110. {
  1111. $this->_checkZf2794();
  1112. $decorator = $this->element->getDecorator('viewHelper');
  1113. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  1114. }
  1115. /**
  1116. * @group ZF-4822
  1117. */
  1118. public function testErrorsDecoratorRegisteredByDefault()
  1119. {
  1120. $this->_checkZf2794();
  1121. $decorator = $this->element->getDecorator('errors');
  1122. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  1123. }
  1124. /**
  1125. * @group ZF-4822
  1126. */
  1127. public function testDescriptionDecoratorRegisteredByDefault()
  1128. {
  1129. $this->_checkZf2794();
  1130. $decorator = $this->element->getDecorator('description');
  1131. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  1132. $options = $decorator->getOptions();
  1133. $this->assertTrue(array_key_exists('tag', $options));
  1134. $this->assertEquals('p', $options['tag']);
  1135. $this->assertTrue(array_key_exists('class', $options));
  1136. $this->assertEquals('description', $options['class']);
  1137. }
  1138. /**
  1139. * @group ZF-4822
  1140. */
  1141. public function testHtmlTagDecoratorRegisteredByDefault()
  1142. {
  1143. $this->_checkZf2794();
  1144. $decorator = $this->element->getDecorator('HtmlTag');
  1145. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1146. }
  1147. /**
  1148. * @group ZF-4822
  1149. */
  1150. public function testLabelDecoratorRegisteredByDefault()
  1151. {
  1152. $this->_checkZf2794();
  1153. $decorator = $this->element->getDecorator('Label');
  1154. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1155. }
  1156. public function testCanDisableRegisteringDefaultDecoratorsDuringInitialization()
  1157. {
  1158. $element = new Zend_Form_Element('foo', array('disableLoadDefaultDecorators' => true));
  1159. $decorators = $element->getDecorators();
  1160. $this->assertEquals(array(), $decorators);
  1161. }
  1162. public function testAddingInvalidDecoratorThrowsException()
  1163. {
  1164. try {
  1165. $this->element->addDecorator(123);
  1166. $this->fail('Invalid decorator type should raise exception');
  1167. } catch (Zend_Form_Exception $e) {
  1168. $this->assertContains('Invalid decorator', $e->getMessage());
  1169. }
  1170. }
  1171. public function testCanAddSingleDecoratorAsString()
  1172. {
  1173. $this->_checkZf2794();
  1174. $this->element->clearDecorators();
  1175. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1176. $this->element->addDecorator('viewHelper');
  1177. $decorator = $this->element->getDecorator('viewHelper');
  1178. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  1179. }
  1180. public function testCanNotRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  1181. {
  1182. $this->assertFalse($this->element->getDecorator('Zend_Form_Decorator_ViewHelper'));
  1183. }
  1184. public function testCanAddSingleDecoratorAsDecoratorObject()
  1185. {
  1186. $this->element->clearDecorators();
  1187. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1188. $decorator = new Zend_Form_Decorator_ViewHelper;
  1189. $this->element->addDecorator($decorator);
  1190. $test = $this->element->getDecorator('Zend_Form_Decorator_ViewHelper');
  1191. $this->assertSame($decorator, $test);
  1192. }
  1193. /**
  1194. * @group ZF-3597
  1195. */
  1196. public function testAddingConcreteDecoratorShouldHonorOrder()
  1197. {
  1198. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  1199. $decorator = new My_Decorator_TableRow();
  1200. $this->elemen

Large files files are truncated, but you can click here to view the full file