ElementTest.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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-2009 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_ElementTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. // error_reporting(E_ALL);
  27. require_once 'Zend/Form/Element.php';
  28. require_once 'Zend/Config.php';
  29. require_once 'Zend/Controller/Action/HelperBroker.php';
  30. require_once 'Zend/Form.php';
  31. require_once 'Zend/Form/Decorator/Abstract.php';
  32. require_once 'Zend/Form/Decorator/HtmlTag.php';
  33. require_once 'Zend/Loader/PluginLoader.php';
  34. require_once 'Zend/Translate.php';
  35. require_once 'Zend/Validate/NotEmpty.php';
  36. require_once 'Zend/Validate/EmailAddress.php';
  37. require_once 'Zend/View.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Form
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Form
  45. */
  46. class Zend_Form_ElementTest extends PHPUnit_Framework_TestCase
  47. {
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_ElementTest');
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. public function setUp()
  54. {
  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. * @see 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. * @see 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. * @see 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. * @see 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->element->setLabel('Foo')
  1201. ->setDescription('sample description')
  1202. ->clearDecorators()
  1203. ->addDecorators(array(
  1204. 'ViewHelper',
  1205. $decorator,
  1206. ));
  1207. $html = $this->element->render($this->getView());
  1208. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  1209. }
  1210. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  1211. {
  1212. $this->_checkZf2794();
  1213. $this->element->clearDecorators();
  1214. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1215. $decorator = new Zend_Form_Decorator_ViewHelper;
  1216. $this->element->addDecorator($decorator);
  1217. $test = $this->element->getDecorator('viewHelper');
  1218. $this->assertSame($decorator, $test);
  1219. }
  1220. public function testCanAddMultipleDecorators()
  1221. {
  1222. $this->_checkZf2794();
  1223. $this->element->clearDecorators();
  1224. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1225. $testDecorator = new Zend_Form_ElementTest_Decorator;
  1226. $this->element->addDecorators(array(
  1227. 'ViewHelper',
  1228. $testDecorator
  1229. ));
  1230. $viewHelper = $this->element->getDecorator('viewHelper');
  1231. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  1232. $decorator = $this->element->getDecorator('decorator');
  1233. $this->assertSame($testDecorator, $decorator);
  1234. }
  1235. public function testRemovingUnregisteredDecoratorReturnsObjectInstance()
  1236. {
  1237. $this->_checkZf2794();
  1238. $this->assertSame($this->element, $this->element->removeDecorator('bogus'));
  1239. }
  1240. public function testCanRemoveDecorator()
  1241. {
  1242. $this->testViewHelperDecoratorRegisteredByDefault();
  1243. $this->element->removeDecorator('viewHelper');
  1244. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1245. }
  1246. /**
  1247. * @see ZF-3069
  1248. */
  1249. public function testRemovingNamedDecoratorsShouldWork()
  1250. {
  1251. $this->_checkZf2794();
  1252. $this->element->setDecorators(array(
  1253. 'ViewHelper',
  1254. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  1255. array(array('div2' => 'HtmlTag'), array('tag' => 'div')),
  1256. ));
  1257. $decorators = $this->element->getDecorators();
  1258. $this->assertTrue(array_key_exists('div', $decorators));
  1259. $this->assertTrue(array_key_exists('div2', $decorators));
  1260. $this->element->removeDecorator('div');
  1261. $decorators = $this->element->getDecorators();
  1262. $this->assertFalse(array_key_exists('div', $decorators));
  1263. $this->assertTrue(array_key_exists('div2', $decorators));
  1264. }
  1265. public function testCanClearAllDecorators()
  1266. {
  1267. $this->testCanAddMultipleDecorators();
  1268. $this->element->clearDecorators();
  1269. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1270. $this->assertFalse($this->element->getDecorator('decorator'));
  1271. }
  1272. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  1273. {
  1274. $this->_checkZf2794();
  1275. $this->element->setDecorators(array(
  1276. array('HtmlTag', array('tag' => 'span')),
  1277. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'div')),
  1278. ));
  1279. $decorator = $this->element->getDecorator('FooBar');
  1280. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1281. $this->assertEquals('div', $decorator->getOption('tag'));
  1282. $decorator = $this->element->getDecorator('HtmlTag');
  1283. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1284. $this->assertEquals('span', $decorator->getOption('tag'));
  1285. }
  1286. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  1287. {
  1288. $this->element->setDecorators(array(
  1289. 'ViewHelper',
  1290. 'Errors',
  1291. array(array('inner' => 'HtmlTag'), array('tag' => 'div', 'class' => 'element')),
  1292. 'Label',
  1293. array(array('outer' => 'HtmlTag'), array('tag' => 'div')),
  1294. ));
  1295. $decorator = $this->element->getDecorator('inner');
  1296. $decorators = $this->element->getDecorators();
  1297. $i = 0;
  1298. $order = array();
  1299. foreach (array_keys($decorators) as $name) {
  1300. $order[$name] = $i;
  1301. ++$i;
  1302. }
  1303. $this->assertEquals(2, $order['inner'], var_export($order, 1));
  1304. }
  1305. /**
  1306. * @see ZF-3376
  1307. */
  1308. public function testSetDecoratorsShouldAcceptReturnOfGetDecorators()
  1309. {
  1310. $this->element->setDecorators(array(
  1311. 'ViewHelper',
  1312. 'Errors',
  1313. array('input' => 'HtmlTag', array('tag' => 'div', 'class' => 'input')),
  1314. 'Label',
  1315. array('element' => 'HtmlTag', array('tag' => 'div', 'class' => 'element')),
  1316. ));
  1317. $decorators = $this->element->getDecorators();
  1318. $this->element->setDecorators($decorators);
  1319. $this->assertSame($decorators, $this->element->getDecorators());
  1320. }
  1321. public function testRenderElementReturnsMarkup()
  1322. {
  1323. $this->element->setName('foo');
  1324. $html = $this->element->render($this->getView());
  1325. $this->assertTrue(is_string($html));
  1326. $this->assertFalse(empty($html));
  1327. $this->assertContains('<input', $html);
  1328. $this->assertContains('"foo"', $html);
  1329. }
  1330. public function testRenderElementRendersLabelWhenProvided()
  1331. {
  1332. $this->element->setView($this->getView());
  1333. $this->element->setName('foo')
  1334. ->setLabel('Foo');
  1335. $html = $this->element->render();
  1336. $this->assertTrue(is_string($html));
  1337. $this->assertFalse(empty($html));
  1338. $this->assertContains('<label', $html);
  1339. $this->assertContains('Foo', $html);
  1340. $this->assertContains('</label>', $html);
  1341. }
  1342. public function testRenderElementRendersValueWhenProvided()
  1343. {
  1344. $this->element->setView($this->getView());
  1345. $this->element->setName('foo')
  1346. ->setValue('bar');
  1347. $html = $this->element->render();
  1348. $this->assertTrue(is_string($html));
  1349. $this->assertFalse(empty($html));
  1350. $this->assertContains('<input', $html);
  1351. $this->assertContains('"foo"', $html);
  1352. $this->assertContains('"bar"', $html);
  1353. }
  1354. public function testRenderElementRendersErrorsWhenProvided()
  1355. {
  1356. $this->_checkZf2794();
  1357. $this->element->setView($this->getView())
  1358. ->setRequired(true)
  1359. ->setName('foo')
  1360. ->addValidator('NotEmpty');
  1361. $this->element->isValid('');
  1362. $html = $this->element->render();
  1363. $this->assertTrue(is_string($html));
  1364. $this->assertFalse(empty($html));
  1365. $this->assertContains('error', $html);
  1366. $this->assertRegexp('/empty/i', $html);
  1367. }
  1368. public function testToStringProxiesToRender()
  1369. {
  1370. $this->element->setView($this->getView());
  1371. $this->element->setName('foo');
  1372. $html = $this->element->__toString();
  1373. $this->assertTrue(is_string($html));
  1374. $this->assertFalse(empty($html));
  1375. $this->assertContains('<input', $html);
  1376. $this->assertContains('"foo"', $html);
  1377. }
  1378. public function raiseDecoratorException($content, $element, $options)
  1379. {
  1380. throw new Exception('Raising exception in decorator callback');
  1381. }
  1382. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  1383. {
  1384. $this->error = $errstr;
  1385. }
  1386. public function testToStringRaisesErrorWhenExceptionCaught()
  1387. {
  1388. $this->element->setDecorators(array(
  1389. array(
  1390. 'decorator' => 'Callback',
  1391. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  1392. ),
  1393. ));
  1394. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  1395. $text = $this->element->__toString();
  1396. restore_error_handler();
  1397. $this->assertTrue(empty($text));
  1398. $this->assertTrue(isset($this->error));
  1399. $this->assertEquals('Raising exception in decorator callback', $this->error);
  1400. }
  1401. public function getOptions()
  1402. {
  1403. $options = array(
  1404. 'name' => 'changed',
  1405. 'value' => 'foo',
  1406. 'label' => 'bar',
  1407. 'order' => 50,
  1408. 'required' => false,
  1409. 'foo' => 'bar',
  1410. 'baz' => 'bat'
  1411. );
  1412. return $options;
  1413. }
  1414. public function testCanSetObjectStateViaSetOptions()
  1415. {
  1416. $options = $this->getOptions();
  1417. $this->element->setOptions($options);
  1418. $this->assertEquals('changed', $this->element->getName());
  1419. $this->assertEquals('foo', $this->element->getValue());
  1420. $this->assertEquals('bar', $this->element->getLabel());
  1421. $this->assertEquals(50, $this->element->getOrder());
  1422. $this->assertFalse($this->element->isRequired());
  1423. $this->assertEquals('bar', $this->element->foo);
  1424. $this->assertEquals('bat', $this->element->baz);
  1425. }
  1426. public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
  1427. {
  1428. $options = $this->getOptions();
  1429. $config = new Zend_Config($options);
  1430. $options['config'] = $config;
  1431. $options['options'] = $config->toArray();
  1432. $this->element->setOptions($options);
  1433. }
  1434. public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNoObjectPresent()
  1435. {
  1436. $options = $this->getOptions();
  1437. $options['translator'] = true;
  1438. $options['pluginLoader'] = true;
  1439. $options['view'] = true;
  1440. $this->element->setOptions($options);
  1441. }
  1442. public function testSetOptionsSetsArrayOfStringValidators()
  1443. {
  1444. $this->_checkZf2794();
  1445. $options = $this->getOptions();
  1446. $options['validators'] = array(
  1447. 'notEmpty',
  1448. 'digits'
  1449. );
  1450. $this->element->setOptions($options);
  1451. $validator = $this->element->getValidator('notEmpty');
  1452. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1453. $validator = $this->element->getValidator('digits');
  1454. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1455. }
  1456. public function testSetOptionsSetsArrayOfArrayValidators()
  1457. {
  1458. $this->_checkZf2794();
  1459. $options = $this->getOptions();
  1460. $options['validators'] = array(
  1461. array('notEmpty', true, array('bar')),
  1462. array('digits', true, array('bar')),
  1463. );
  1464. $this->element->setOptions($options);
  1465. $validator = $this->element->getValidator('notEmpty');
  1466. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1467. $this->assertTrue($validator->zfBreakChainOnFailure);
  1468. $validator = $this->element->getValidator('digits');
  1469. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1470. $this->assertTrue($validator->zfBreakChainOnFailure);
  1471. }
  1472. public function testSetOptionsSetsArrayOfAssociativeArrayValidators()
  1473. {
  1474. $this->_checkZf2794();
  1475. $options = $this->getOptions();
  1476. $options['validators'] = array(
  1477. array(
  1478. 'options' => array('bar'),
  1479. 'breakChainOnFailure' => true,
  1480. 'validator' => 'notEmpty',
  1481. ),
  1482. array(
  1483. 'options' => array('bar'),
  1484. 'validator' => 'digits',
  1485. 'breakChainOnFailure' => true,
  1486. ),
  1487. );
  1488. $this->element->setOptions($options);
  1489. $validator = $this->element->getValidator('notEmpty');
  1490. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1491. $this->assertTrue($validator->zfBreakChainOnFailure);
  1492. $validator = $this->element->getValidator('digits');
  1493. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1494. $this->assertTrue($validator->zfBreakChainOnFailure);
  1495. }
  1496. public function testSetOptionsSetsArrayOfStringFilters()
  1497. {
  1498. $this->_checkZf2794();
  1499. $options = $this->getOptions();
  1500. $options['filters'] = array('StringToUpper', 'Alpha');
  1501. $this->element->setOptions($options);
  1502. $filter = $this->element->getFilter('StringToUpper');
  1503. $this->assertTrue($filter instanceof Zend_Filter_StringToUpper);
  1504. $filter = $this->element->getFilter('Alpha');
  1505. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1506. }
  1507. public function testSetOptionsSetsArrayOfArrayFilters()
  1508. {
  1509. $this->_checkZf2794();
  1510. $options = $this->getOptions();
  1511. $options['filters'] = array(
  1512. array('Digits', array('bar' => 'baz')),
  1513. array('Alpha', array('foo')),
  1514. );
  1515. $this->element->setOptions($options);
  1516. $filter = $this->element->getFilter('Digits');
  1517. $this->assertTrue($filter instanceof Zend_Filter_Digits);
  1518. $filter = $this->element->getFilter('Alpha');
  1519. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1520. }
  1521. public function testSetOptionsSetsArrayOfAssociativeArrayFilters()
  1522. {
  1523. $this->_checkZf2794();
  1524. $options = $this->getOptions();
  1525. $options['filters'] = array(
  1526. array(
  1527. 'options' => array('baz'),
  1528. 'filter' => 'Digits'
  1529. ),
  1530. array(
  1531. 'options' => array('foo'),
  1532. 'filter' => 'Alpha',
  1533. ),
  1534. );
  1535. $this->element->setOptions($options);
  1536. $filter = $this->element->getFilter('Digits');
  1537. $this->assertTrue($filter instanceof Zend_Filter_Digits);
  1538. $filter = $this->element->getFilter('Alpha');
  1539. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1540. }
  1541. public function testSetOptionsSetsArrayOfStringDecorators()
  1542. {
  1543. $this->_checkZf2794();
  1544. $options = $this->getOptions();
  1545. $options['decorators'] = array('label', 'form');
  1546. $this->element->setOptions($options);
  1547. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1548. $this->assertFalse($this->element->getDecorator('errors'));
  1549. $decorator = $this->element->getDecorator('label');
  1550. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1551. $decorator = $this->element->getDecorator('form');
  1552. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1553. }
  1554. public function testSetOptionsSetsArrayOfArrayDecorators()
  1555. {
  1556. $this->_checkZf2794();
  1557. $options = $this->getOptions();
  1558. $options['decorators'] = array(
  1559. array('label', array('id' => 'mylabel')),
  1560. array('form', array('id' => 'form')),
  1561. );
  1562. $this->element->setOptions($options);
  1563. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1564. $this->assertFalse($this->element->getDecorator('errors'));
  1565. $decorator = $this->element->getDecorator('label');
  1566. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1567. $options = $decorator->getOptions();
  1568. $this->assertEquals('mylabel', $options['id']);
  1569. $decorator = $this->element->getDecorator('form');
  1570. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1571. $options = $decorator->getOptions();
  1572. $this->assertEquals('form', $options['id']);
  1573. }
  1574. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  1575. {
  1576. $this->_checkZf2794();
  1577. $options = $this->getOptions();
  1578. $options['decorators'] = array(
  1579. array(
  1580. 'options' => array('id' => 'mylabel'),
  1581. 'decorator' => 'label',
  1582. ),
  1583. array(
  1584. 'options' => array('id' => 'form'),
  1585. 'decorator' => 'form',
  1586. ),
  1587. );
  1588. $this->element->setOptions($options);
  1589. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1590. $this->assertFalse($this->element->getDecorator('errors'));
  1591. $decorator = $this->element->getDecorator('label');
  1592. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1593. $options = $decorator->getOptions();
  1594. $this->assertEquals('mylabel', $options['id']);
  1595. $decorator = $this->element->getDecorator('form');
  1596. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1597. $options = $decorator->getOptions();
  1598. $this->assertEquals('form', $options['id']);
  1599. }
  1600. public function testSetOptionsSetsGlobalPrefixPaths()
  1601. {
  1602. $options = $this->getOptions();
  1603. $options['prefixPath'] = array(
  1604. 'prefix' => 'Zend_Foo',
  1605. 'path' => 'Zend/Foo/'
  1606. );
  1607. $this->element->setOptions($options);
  1608. foreach (array('validate', 'filter', 'decorator') as $type) {
  1609. $loader = $this->element->getPluginLoader($type);
  1610. $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
  1611. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  1612. $this->assertFalse(empty($paths));
  1613. $this->assertContains('Foo', $paths[0]);
  1614. }
  1615. }
  1616. public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
  1617. {
  1618. $options = $this->getOptions();
  1619. $options['prefixPath'] = array(
  1620. 'filter' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  1621. );
  1622. $this->element->setOptions($options);
  1623. $loader = $this->element->getPluginLoader('filter');
  1624. $paths = $loader->getPaths('Zend_Foo');
  1625. $this->assertTrue(is_array($paths));
  1626. $this->assertFalse(empty($paths));
  1627. $this->assertContains('Foo', $paths[0]);
  1628. }
  1629. public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
  1630. {
  1631. $options = $this->getOptions();
  1632. $options['prefixPath'] = array(
  1633. array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  1634. );
  1635. $this->element->setOptions($options);
  1636. $loader = $this->element->getPluginLoader('decorator');
  1637. $paths = $loader->getPaths('Zend_Foo');
  1638. $this->assertTrue(is_array($paths));
  1639. $this->assertFalse(empty($paths));
  1640. $this->assertContains('Foo', $paths[0]);
  1641. }
  1642. public function testCanSetObjectStateViaSetConfig()
  1643. {
  1644. $config = new Zend_Config($this->getOptions());
  1645. $this->element->setConfig($config);
  1646. $this->assertEquals('changed', $this->element->getName());
  1647. $this->assertEquals('foo', $this->element->getValue());
  1648. $this->assertEquals('bar', $this->element->getLabel());
  1649. $this->assertEquals(50, $this->element->getOrder());
  1650. $this->assertFalse($this->element->isRequired());
  1651. $this->assertEquals('bar', $this->element->foo);
  1652. $this->assertEquals('bat', $this->element->baz);
  1653. }
  1654. public function testPassingConfigObjectToConstructorSetsObjectState()
  1655. {
  1656. $config = new Zend_Config($this->getOptions());
  1657. $element = new Zend_Form_Element($config);
  1658. $this->assertEquals('changed', $element->getName());
  1659. $this->assertEquals('foo', $element->getValue());
  1660. $this->assertEquals('bar', $element->getLabel());
  1661. $this->assertEquals(50, $element->getOrder());
  1662. $this->assertFalse($element->isRequired());
  1663. $this->assertEquals('bar', $element->foo);
  1664. $this->assertEquals('bat', $element->baz);
  1665. }
  1666. public function testValueIsFilteredPriorToValidation()
  1667. {
  1668. $this->_checkZf2794();
  1669. $this->element->addFilter('StringTrim')
  1670. ->addValidator('StringLength', false, array(3, 8));
  1671. $this->assertTrue($this->element->isValid(' foobar '));
  1672. $this->assertEquals('foobar', $this->element->getValue());
  1673. $this->element->setFilters(array('StringTrim'))
  1674. ->setRequired(true)
  1675. ->setValidators(array('NotEmpty'));
  1676. $this->assertFalse($this->element->isValid(' '));
  1677. }
  1678. // Extensions
  1679. public function testInitCalledBeforeLoadDecorators()
  1680. {
  1681. $element = new Zend_Form_ElementTest_Element('test');
  1682. $decorators = $element->getDecorators();
  1683. $this->assertTrue(empty($decorators));
  1684. }
  1685. /**
  1686. * @group ZF-3217
  1687. */
  1688. public function testElementShouldOverloadToRenderDecorators()
  1689. {
  1690. $this->element->setLabel('Foo Label')
  1691. ->setView($this->getView());
  1692. $html = $this->element->renderViewHelper();
  1693. $this->assertContains('<input', $html);
  1694. $this->assertContains('id="' . $this->element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  1695. $this->assertNotContains('<dd', $html);
  1696. $this->assertNotContains('<label', $html);
  1697. $html = $this->element->renderLabel('this is the content');
  1698. $this->assertRegexp('#<label[^>]*for="' . $this->element->getFullyQualifiedName() . '"[^>]*>Foo Label</label>#', $html);
  1699. $this->assertContains('this is the content', $html);
  1700. $this->assertNotContains('<input', $html);
  1701. }
  1702. /**
  1703. * @group ZF-3217
  1704. * @expectedException Zend_Form_Element_Exception
  1705. */
  1706. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  1707. {
  1708. $html = $this->element->bogusMethodCall();
  1709. }
  1710. /**
  1711. * @group ZF-5150
  1712. */
  1713. public function testMarkingAsErrorShouldCauseIsErrorToReturnFalse()
  1714. {
  1715. $this->element->setValue('foo');
  1716. $this->element->markAsError();
  1717. $this->assertFalse($this->element->isValid('foo'));
  1718. }
  1719. /**
  1720. * @group ZF-4915
  1721. */
  1722. public function testElementShouldAllowSettingDefaultErrorMessageSeparator()
  1723. {
  1724. $this->element->setErrorMessageSeparator('|');
  1725. $this->assertEquals('|', $this->element->getErrorMessageSeparator());
  1726. }
  1727. /**
  1728. * @group ZF-4915
  1729. */
  1730. public function testElementShouldUseSemicolonAndSpaceAsDefaultErrorMessageSeparator()
  1731. {
  1732. $this->assertEquals('; ', $this->element->getErrorMessageSeparator());
  1733. }
  1734. /**
  1735. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  1736. *
  1737. * @link http://framework.zend.com/issues/browse/ZF-2794
  1738. * @return void
  1739. */
  1740. protected function _checkZf2794()
  1741. {
  1742. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  1743. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  1744. }
  1745. }
  1746. }
  1747. class Zend_Form_ElementTest_Decorator extends Zend_Form_Decorator_Abstract
  1748. {
  1749. }
  1750. class Zend_Form_ElementTest_Element extends Zend_Form_Element
  1751. {
  1752. public function init()
  1753. {
  1754. $this->setDisableLoadDefaultDecorators(true);
  1755. }
  1756. }
  1757. class Zend_Form_ElementTest_ArrayFilter implements Zend_Filter_Interface
  1758. {
  1759. public function filter($value)
  1760. {
  1761. $value = array_filter($value, array($this, '_filter'));
  1762. return $value;
  1763. }
  1764. protected function _filter($value)
  1765. {
  1766. if (is_array($value)) {
  1767. return array_filter($value, array($this, '_filter'));
  1768. }
  1769. return (strstr($value, 'ba'));
  1770. }
  1771. /**
  1772. * Check array notation for validators
  1773. */
  1774. public function testValidatorsGivenArrayKeysOnValidation()
  1775. {
  1776. $username = new Zend_Form_Element('username');
  1777. $username->addValidator('stringLength', true, array('min' => 5, 'max' => 20, 'ignore' => 'something'));
  1778. $form = new Zend_Form(array('elements' => array($username)));
  1779. $this->assertTrue($form->isValid(array('username' => 'abcde')));
  1780. }
  1781. }
  1782. if (PHPUnit_MAIN_METHOD == 'Zend_Form_ElementTest::main') {
  1783. Zend_Form_ElementTest::main();
  1784. }