2
0

ElementTest.php 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. <?php
  2. if (!defined('PHPUnit_MAIN_METHOD')) {
  3. define('PHPUnit_MAIN_METHOD', 'Zend_Form_ElementTest::main');
  4. }
  5. require_once dirname(__FILE__) . '/../../TestHelper.php';
  6. // error_reporting(E_ALL);
  7. require_once 'Zend/Form/Element.php';
  8. require_once 'Zend/Config.php';
  9. require_once 'Zend/Controller/Action/HelperBroker.php';
  10. require_once 'Zend/Form.php';
  11. require_once 'Zend/Form/Decorator/Abstract.php';
  12. require_once 'Zend/Form/Decorator/HtmlTag.php';
  13. require_once 'Zend/Loader/PluginLoader.php';
  14. require_once 'Zend/Translate.php';
  15. require_once 'Zend/Validate/NotEmpty.php';
  16. require_once 'Zend/Validate/EmailAddress.php';
  17. require_once 'Zend/View.php';
  18. class Zend_Form_ElementTest extends PHPUnit_Framework_TestCase
  19. {
  20. public static function main()
  21. {
  22. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_ElementTest');
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. public function setUp()
  26. {
  27. Zend_Form::setDefaultTranslator(null);
  28. if (isset($this->error)) {
  29. unset($this->error);
  30. }
  31. $this->element = new Zend_Form_Element('foo');
  32. Zend_Controller_Action_HelperBroker::resetHelpers();
  33. }
  34. public function tearDown()
  35. {
  36. }
  37. public function getView()
  38. {
  39. $view = new Zend_View();
  40. $libPath = dirname(__FILE__) . '/../../../library';
  41. $view->addHelperPath($libPath . '/Zend/View/Helper');
  42. return $view;
  43. }
  44. public function testConstructorRequiresMinimallyElementName()
  45. {
  46. try {
  47. $element = new Zend_Form_Element(1);
  48. $this->fail('Zend_Form_Element constructor should not accept integer argument');
  49. } catch (Zend_Form_Exception $e) {
  50. }
  51. try {
  52. $element = new Zend_Form_Element(true);
  53. $this->fail('Zend_Form_Element constructor should not accept boolean argument');
  54. } catch (Zend_Form_Exception $e) {
  55. }
  56. try {
  57. $element = new Zend_Form_Element('foo');
  58. } catch (Exception $e) {
  59. $this->fail('Zend_Form_Element constructor should accept String values');
  60. }
  61. $config = array('foo' => 'bar');
  62. try {
  63. $element = new Zend_Form_Element($config);
  64. $this->fail('Zend_Form_Element constructor requires array with name element');
  65. } catch (Zend_Form_Exception $e) {
  66. }
  67. $config = array('name' => 'bar');
  68. try {
  69. $element = new Zend_Form_Element($config);
  70. } catch (Zend_Form_Exception $e) {
  71. $this->fail('Zend_Form_Element constructor should accept array with name element');
  72. }
  73. $config = new Zend_Config(array('foo' => 'bar'));
  74. try {
  75. $element = new Zend_Form_Element($config);
  76. $this->fail('Zend_Form_Element constructor requires Zend_Config object with name element');
  77. } catch (Zend_Form_Exception $e) {
  78. }
  79. $config = new Zend_Config(array('name' => 'bar'));
  80. try {
  81. $element = new Zend_Form_Element($config);
  82. } catch (Zend_Form_Exception $e) {
  83. $this->fail('Zend_Form_Element constructor should accept Zend_Config with name element');
  84. }
  85. }
  86. public function testNoTranslatorByDefault()
  87. {
  88. $this->assertNull($this->element->getTranslator());
  89. }
  90. public function testGetTranslatorRetrievesGlobalDefaultWhenAvailable()
  91. {
  92. $this->testNoTranslatorByDefault();
  93. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  94. Zend_Form::setDefaultTranslator($translator);
  95. $received = $this->element->getTranslator();
  96. $this->assertSame($translator->getAdapter(), $received);
  97. }
  98. public function testTranslatorAccessorsWork()
  99. {
  100. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  101. $this->element->setTranslator($translator);
  102. $received = $this->element->getTranslator($translator);
  103. $this->assertSame($translator->getAdapter(), $received);
  104. }
  105. public function testCanDisableTranslation()
  106. {
  107. $this->testGetTranslatorRetrievesGlobalDefaultWhenAvailable();
  108. $this->element->setDisableTranslator(true);
  109. $this->assertNull($this->element->getTranslator());
  110. }
  111. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  112. {
  113. $this->element->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  114. $this->assertEquals('foobar', $this->element->getName());
  115. try {
  116. $this->element->setName('%\^&*)\(%$#@!.}{;-,');
  117. $this->fail('Empty names should raise exception');
  118. } catch (Zend_Form_Exception $e) {
  119. $this->assertContains('Invalid name provided', $e->getMessage());
  120. }
  121. }
  122. public function testZeroIsAllowedAsElementName()
  123. {
  124. try {
  125. $this->element->setName(0);
  126. $this->assertSame('0', $this->element->getName());
  127. } catch (Zend_Form_Exception $e) {
  128. $this->fail('Should allow zero as element name');
  129. }
  130. }
  131. /**
  132. * @see ZF-2851
  133. */
  134. public function testSetNameShouldNotAllowEmptyString()
  135. {
  136. foreach (array('', ' ', ' ') as $name) {
  137. try {
  138. $this->element->setName($name);
  139. $this->fail('setName() should not allow empty string');
  140. } catch (Zend_Form_Exception $e) {
  141. $this->assertContains('Invalid name', $e->getMessage());
  142. }
  143. }
  144. }
  145. public function testElementValueInitiallyNull()
  146. {
  147. $this->assertNull($this->element->getValue());
  148. }
  149. public function testValueAccessorsWork()
  150. {
  151. $this->element->setValue('bar');
  152. $this->assertContains('bar', $this->element->getValue());
  153. }
  154. public function testGetValueFiltersValue()
  155. {
  156. $this->element->setValue('This 0 is 1 a-2-TEST')
  157. ->addFilter('alnum')
  158. ->addFilter('stringToUpper');
  159. $test = $this->element->getValue();
  160. $this->assertEquals('THIS0IS1A2TEST', $test);
  161. }
  162. public function checkFilterValues($item, $key)
  163. {
  164. $this->assertRegexp('/^[A-Z]+$/', $item);
  165. }
  166. public function testRetrievingArrayValueFiltersAllArrayValues()
  167. {
  168. $this->element->setValue(array(
  169. 'foo',
  170. array(
  171. 'bar',
  172. 'baz'
  173. ),
  174. 'bat'
  175. ))
  176. ->setIsArray(true)
  177. ->addFilter('StringToUpper');
  178. $test = $this->element->getValue();
  179. $this->assertTrue(is_array($test));
  180. array_walk_recursive($test, array($this, 'checkFilterValues'));
  181. }
  182. public function testRetrievingArrayValueDoesNotFilterAllValuesWhenNotIsArray()
  183. {
  184. $values = array(
  185. 'foo',
  186. array(
  187. 'bar',
  188. 'baz'
  189. ),
  190. 'bat'
  191. );
  192. $this->element->setValue($values)
  193. ->addFilter(new Zend_Form_ElementTest_ArrayFilter());
  194. $test = $this->element->getValue();
  195. $this->assertTrue(is_array($test));
  196. require_once 'Zend/Json.php';
  197. $test = Zend_Json::encode($test);
  198. $this->assertNotContains('foo', $test);
  199. foreach (array('bar', 'baz', 'bat') as $value) {
  200. $this->assertContains($value, $test);
  201. }
  202. }
  203. public function testGetUnfilteredValueRetrievesOriginalValue()
  204. {
  205. $this->element->setValue('bar');
  206. $this->assertSame('bar', $this->element->getUnfilteredValue());
  207. }
  208. public function testLabelInitiallyNull()
  209. {
  210. $this->assertNull($this->element->getLabel());
  211. }
  212. public function testLabelAccessorsWork()
  213. {
  214. $this->element->setLabel('FooBar');
  215. $this->assertEquals('FooBar', $this->element->getLabel());
  216. }
  217. public function testOrderNullByDefault()
  218. {
  219. $this->assertNull($this->element->getOrder());
  220. }
  221. public function testCanSetOrder()
  222. {
  223. $this->testOrderNullByDefault();
  224. $this->element->setOrder(50);
  225. $this->assertEquals(50, $this->element->getOrder());
  226. }
  227. public function testRequiredFlagFalseByDefault()
  228. {
  229. $this->assertFalse($this->element->isRequired());
  230. }
  231. public function testRequiredAcccessorsWork()
  232. {
  233. $this->assertFalse($this->element->isRequired());
  234. $this->element->setRequired(true);
  235. $this->assertTrue($this->element->isRequired());
  236. }
  237. public function testIsValidInsertsNotEmptyValidatorWhenElementIsRequiredByDefault()
  238. {
  239. $this->_checkZf2794();
  240. $this->element->setRequired(true);
  241. $this->assertFalse($this->element->isValid(''));
  242. $validator = $this->element->getValidator('NotEmpty');
  243. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  244. $this->assertTrue($validator->zfBreakChainOnFailure);
  245. }
  246. /**
  247. * @see ZF-2862
  248. */
  249. public function testBreakChainOnFailureFlagsForExistingValidatorsRemainSetWhenNotEmptyValidatorAutoInserted()
  250. {
  251. $this->_checkZf2794();
  252. $username = new Zend_Form_Element('username');
  253. $username->addValidator('stringLength', true, array(5, 20))
  254. ->addValidator('regex', true, array('/^[a-zA-Z0-9_]*$/'))
  255. ->addFilter('StringToLower')
  256. ->setRequired(true);
  257. $form = new Zend_Form(array('elements' => array($username)));
  258. $form->isValid(array('username' => '#'));
  259. $validator = $username->getValidator('stringLength');
  260. $this->assertTrue($validator->zfBreakChainOnFailure);
  261. $validator = $username->getValidator('regex');
  262. $this->assertTrue($validator->zfBreakChainOnFailure);
  263. }
  264. public function testAutoInsertNotEmptyValidatorFlagTrueByDefault()
  265. {
  266. $this->assertTrue($this->element->autoInsertNotEmptyValidator());
  267. }
  268. public function testCanSetAutoInsertNotEmptyValidatorFlag()
  269. {
  270. $this->testAutoInsertNotEmptyValidatorFlagTrueByDefault();
  271. $this->element->setAutoInsertNotEmptyValidator(false);
  272. $this->assertFalse($this->element->autoInsertNotEmptyValidator());
  273. $this->element->setAutoInsertNotEmptyValidator(true);
  274. $this->assertTrue($this->element->autoInsertNotEmptyValidator());
  275. }
  276. public function testIsValidDoesNotInsertNotEmptyValidatorWhenElementIsRequiredButAutoInsertNotEmptyValidatorFlagIsFalse()
  277. {
  278. $this->element->setAutoInsertNotEmptyValidator(false)
  279. ->setRequired(true);
  280. $this->assertTrue($this->element->isValid(''));
  281. }
  282. public function testDescriptionInitiallyNull()
  283. {
  284. $this->assertNull($this->element->getDescription());
  285. }
  286. public function testCanSetDescription()
  287. {
  288. $this->testDescriptionInitiallyNull();
  289. $this->element->setDescription('element hint');
  290. $this->assertEquals('element hint', $this->element->getDescription());
  291. }
  292. public function testElementIsNotArrayByDefault()
  293. {
  294. $this->assertFalse($this->element->isArray());
  295. }
  296. public function testCanSetArrayFlag()
  297. {
  298. $this->testElementIsNotArrayByDefault();
  299. $this->element->setIsArray(true);
  300. $this->assertTrue($this->element->isArray());
  301. $this->element->setIsArray(false);
  302. $this->assertFalse($this->element->isArray());
  303. }
  304. public function testElementBelongsToNullByDefault()
  305. {
  306. $this->assertNull($this->element->getBelongsTo());
  307. }
  308. public function testCanSetArrayElementBelongsTo()
  309. {
  310. $this->testElementBelongsToNullByDefault();
  311. $this->element->setBelongsTo('foo');
  312. $this->assertEquals('foo', $this->element->getBelongsTo());
  313. }
  314. public function testArrayElementBelongsToNormalizedToValidVariableCharactersOnly()
  315. {
  316. $this->testElementBelongsToNullByDefault();
  317. $this->element->setBelongsTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
  318. $this->assertEquals('foobar', $this->element->getBelongsTo());
  319. }
  320. public function testGetTypeReturnsCurrentElementClass()
  321. {
  322. $this->assertEquals('Zend_Form_Element', $this->element->getType());
  323. }
  324. public function testCanUseAccessorsToSetIndidualAttribs()
  325. {
  326. $this->element->setAttrib('foo', 'bar')
  327. ->setAttrib('bar', 'baz')
  328. ->setAttrib('baz', 'bat');
  329. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  330. $this->assertEquals('baz', $this->element->getAttrib('bar'));
  331. $this->assertEquals('bat', $this->element->getAttrib('baz'));
  332. }
  333. public function testGetUndefinedAttribShouldReturnNull()
  334. {
  335. $this->assertNull($this->element->getAttrib('bogus'));
  336. }
  337. public function testSetAttribThrowsExceptionsForKeysWithLeadingUnderscores()
  338. {
  339. try {
  340. $this->element->setAttrib('_foo', 'bar');
  341. $this->fail('setAttrib() should throw an exception for invalid keys');
  342. } catch (Zend_Form_Exception $e) {
  343. $this->assertContains('Invalid attribute', $e->getMessage());
  344. }
  345. }
  346. public function testPassingNullValueToSetAttribUnsetsAttrib()
  347. {
  348. $this->element->setAttrib('foo', 'bar');
  349. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  350. $this->element->setAttrib('foo', null);
  351. $this->assertFalse(isset($this->element->foo));
  352. }
  353. public function testSetAttribsSetsMultipleAttribs()
  354. {
  355. $this->element->setAttribs(array(
  356. 'foo' => 'bar',
  357. 'bar' => 'baz',
  358. 'baz' => 'bat'
  359. ));
  360. $this->assertEquals('bar', $this->element->getAttrib('foo'));
  361. $this->assertEquals('baz', $this->element->getAttrib('bar'));
  362. $this->assertEquals('bat', $this->element->getAttrib('baz'));
  363. }
  364. public function testGetAttribsRetrievesAllAttributes()
  365. {
  366. $attribs = array(
  367. 'foo' => 'bar',
  368. 'bar' => 'baz',
  369. 'baz' => 'bat'
  370. );
  371. $this->element->setAttribs($attribs);
  372. $attribs['helper'] = 'formText';
  373. $received = $this->element->getAttribs();
  374. $this->assertEquals($attribs, $received);
  375. }
  376. public function testPassingNullValuesToSetAttribsUnsetsAttribs()
  377. {
  378. $this->testSetAttribsSetsMultipleAttribs();
  379. $this->element->setAttribs(array('foo' => null));
  380. $this->assertNull($this->element->foo);
  381. }
  382. public function testRetrievingOverloadedValuesThrowsExceptionWithInvalidKey()
  383. {
  384. try {
  385. $name = $this->element->_name;
  386. $this->fail('Overloading should not return protected or private members');
  387. } catch (Zend_Form_Exception $e) {
  388. $this->assertContains('Cannot retrieve value for protected/private', $e->getMessage());
  389. }
  390. }
  391. public function testCanSetAndRetrieveAttribsViaOverloading()
  392. {
  393. $this->element->foo = 'bar';
  394. $this->assertEquals('bar', $this->element->foo);
  395. }
  396. public function testGetPluginLoaderRetrievesDefaultValidatorPluginLoader()
  397. {
  398. $loader = $this->element->getPluginLoader('validate');
  399. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  400. $paths = $loader->getPaths('Zend_Validate');
  401. $this->assertTrue(is_array($paths), var_export($loader, 1));
  402. $this->assertTrue(0 < count($paths));
  403. $this->assertContains('Validate', $paths[0]);
  404. }
  405. public function testGetPluginLoaderRetrievesDefaultFilterPluginLoader()
  406. {
  407. $loader = $this->element->getPluginLoader('filter');
  408. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  409. $paths = $loader->getPaths('Zend_Filter');
  410. $this->assertTrue(is_array($paths));
  411. $this->assertTrue(0 < count($paths));
  412. $this->assertContains('Filter', $paths[0]);
  413. }
  414. public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
  415. {
  416. $loader = $this->element->getPluginLoader('decorator');
  417. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  418. $paths = $loader->getPaths('Zend_Form_Decorator');
  419. $this->assertTrue(is_array($paths));
  420. $this->assertTrue(0 < count($paths));
  421. $this->assertContains('Decorator', $paths[0]);
  422. }
  423. public function testCanSetCustomValidatorPluginLoader()
  424. {
  425. $loader = new Zend_Loader_PluginLoader();
  426. $this->element->setPluginLoader($loader, 'validate');
  427. $test = $this->element->getPluginLoader('validate');
  428. $this->assertSame($loader, $test);
  429. }
  430. public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
  431. {
  432. $loader = new Zend_Loader_PluginLoader();
  433. try {
  434. $this->element->setPluginLoader($loader, 'foo');
  435. $this->fail('Invalid loader type should raise exception');
  436. } catch (Zend_Form_Exception $e) {
  437. $this->assertContains('Invalid type', $e->getMessage());
  438. }
  439. }
  440. public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
  441. {
  442. try {
  443. $this->element->getPluginLoader('foo');
  444. $this->fail('Invalid loader type should raise exception');
  445. } catch (Zend_Form_Exception $e) {
  446. $this->assertContains('Invalid type', $e->getMessage());
  447. }
  448. }
  449. public function testCanSetCustomFilterPluginLoader()
  450. {
  451. $loader = new Zend_Loader_PluginLoader();
  452. $this->element->setPluginLoader($loader, 'filter');
  453. $test = $this->element->getPluginLoader('filter');
  454. $this->assertSame($loader, $test);
  455. }
  456. public function testCanSetCustomDecoratorPluginLoader()
  457. {
  458. $loader = new Zend_Loader_PluginLoader();
  459. $this->element->setPluginLoader($loader, 'decorator');
  460. $test = $this->element->getPluginLoader('decorator');
  461. $this->assertSame($loader, $test);
  462. }
  463. public function testPassingInvalidLoaderTypeToAddPrefixPathThrowsException()
  464. {
  465. try {
  466. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
  467. $this->fail('Invalid loader type should raise exception');
  468. } catch (Zend_Form_Exception $e) {
  469. $this->assertContains('Invalid type', $e->getMessage());
  470. }
  471. }
  472. public function testCanAddValidatorPluginLoaderPrefixPath()
  473. {
  474. $loader = $this->element->getPluginLoader('validate');
  475. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  476. $paths = $loader->getPaths('Zend_Form');
  477. $this->assertTrue(is_array($paths));
  478. $this->assertContains('Form', $paths[0]);
  479. }
  480. public function testAddingValidatorPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  481. {
  482. $validateLoader = $this->element->getPluginLoader('validate');
  483. $filterLoader = $this->element->getPluginLoader('filter');
  484. $decoratorLoader = $this->element->getPluginLoader('decorator');
  485. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  486. $this->assertFalse($filterLoader->getPaths('Zend_Form'));
  487. $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
  488. }
  489. public function testCanAddFilterPluginLoaderPrefixPath()
  490. {
  491. $loader = $this->element->getPluginLoader('validate');
  492. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
  493. $paths = $loader->getPaths('Zend_Form');
  494. $this->assertTrue(is_array($paths));
  495. $this->assertContains('Form', $paths[0]);
  496. }
  497. public function testAddingFilterPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  498. {
  499. $filterLoader = $this->element->getPluginLoader('filter');
  500. $validateLoader = $this->element->getPluginLoader('validate');
  501. $decoratorLoader = $this->element->getPluginLoader('decorator');
  502. $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'filter');
  503. $this->assertFalse($validateLoader->getPaths('Zend_Form'));
  504. $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
  505. }
  506. public function testCanAddDecoratorPluginLoaderPrefixPath()
  507. {
  508. $loader = $this->element->getPluginLoader('decorator');
  509. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  510. $paths = $loader->getPaths('Zend_Foo');
  511. $this->assertTrue(is_array($paths));
  512. $this->assertContains('Foo', $paths[0]);
  513. }
  514. public function testAddingDecoratorrPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
  515. {
  516. $decoratorLoader = $this->element->getPluginLoader('decorator');
  517. $filterLoader = $this->element->getPluginLoader('filter');
  518. $validateLoader = $this->element->getPluginLoader('validate');
  519. $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  520. $this->assertFalse($validateLoader->getPaths('Zend_Foo'));
  521. $this->assertFalse($filterLoader->getPaths('Zend_Foo'));
  522. }
  523. public function testCanAddAllPluginLoaderPrefixPathsSimultaneously()
  524. {
  525. $validatorLoader = new Zend_Loader_PluginLoader();
  526. $filterLoader = new Zend_Loader_PluginLoader();
  527. $decoratorLoader = new Zend_Loader_PluginLoader();
  528. $this->element->setPluginLoader($validatorLoader, 'validate')
  529. ->setPluginLoader($filterLoader, 'filter')
  530. ->setPluginLoader($decoratorLoader, 'decorator')
  531. ->addPrefixPath('Zend', 'Zend/');
  532. $paths = $filterLoader->getPaths('Zend_Filter');
  533. $this->assertTrue(is_array($paths));
  534. $this->assertContains('Filter', $paths[0]);
  535. $paths = $validatorLoader->getPaths('Zend_Validate');
  536. $this->assertTrue(is_array($paths));
  537. $this->assertContains('Validate', $paths[0]);
  538. $paths = $decoratorLoader->getPaths('Zend_Decorator');
  539. $this->assertTrue(is_array($paths), var_export($paths, 1));
  540. $this->assertContains('Decorator', $paths[0]);
  541. }
  542. public function testPassingInvalidValidatorToAddValidatorThrowsException()
  543. {
  544. try {
  545. $this->element->addValidator(123);
  546. $this->fail('Invalid validator should raise exception');
  547. } catch (Zend_Form_Exception $e) {
  548. $this->assertContains('Invalid validator', $e->getMessage());
  549. }
  550. }
  551. public function testCanAddSingleValidatorAsString()
  552. {
  553. $this->_checkZf2794();
  554. $this->assertFalse($this->element->getValidator('digits'));
  555. $this->element->addValidator('digits');
  556. $validator = $this->element->getValidator('digits');
  557. $this->assertTrue($validator instanceof Zend_Validate_Digits, var_export($validator, 1));
  558. $this->assertFalse($validator->zfBreakChainOnFailure);
  559. }
  560. public function testCanNotRetrieveSingleValidatorRegisteredAsStringUsingClassName()
  561. {
  562. $this->assertFalse($this->element->getValidator('digits'));
  563. $this->element->addValidator('digits');
  564. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  565. }
  566. public function testCanAddSingleValidatorAsValidatorObject()
  567. {
  568. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  569. require_once 'Zend/Validate/Digits.php';
  570. $validator = new Zend_Validate_Digits();
  571. $this->element->addValidator($validator);
  572. $test = $this->element->getValidator('Zend_Validate_Digits');
  573. $this->assertSame($validator, $test);
  574. $this->assertFalse($validator->zfBreakChainOnFailure);
  575. }
  576. public function testOptionsAreCastToArrayWhenAddingValidator()
  577. {
  578. $this->_checkZf2794();
  579. try {
  580. $this->element->addValidator('Alnum', false, true);
  581. } catch (Exception $e) {
  582. $this->fail('Should be able to add non-array validator options');
  583. }
  584. $validator = $this->element->getValidator('Alnum');
  585. $this->assertTrue($validator instanceof Zend_Validate_Alnum);
  586. $this->assertTrue($validator->allowWhiteSpace);
  587. }
  588. public function testCanRetrieveSingleValidatorRegisteredAsValidatorObjectUsingShortName()
  589. {
  590. $this->_checkZf2794();
  591. $this->assertFalse($this->element->getValidator('digits'));
  592. require_once 'Zend/Validate/Digits.php';
  593. $validator = new Zend_Validate_Digits();
  594. $this->element->addValidator($validator);
  595. $test = $this->element->getValidator('digits');
  596. $this->assertSame($validator, $test);
  597. $this->assertFalse($validator->zfBreakChainOnFailure);
  598. }
  599. public function testRetrievingNamedValidatorShouldNotReorderValidators()
  600. {
  601. $this->element->addValidators(array(
  602. 'NotEmpty',
  603. 'Alnum',
  604. 'Digits',
  605. ));
  606. $validator = $this->element->getValidator('Alnum');
  607. $validators = $this->element->getValidators();
  608. $i = 0;
  609. $order = array();
  610. foreach (array_keys($validators) as $name) {
  611. $order[$name] = $i;
  612. ++$i;
  613. }
  614. $this->assertEquals(1, $order['Zend_Validate_Alnum'], var_export($order, 1));
  615. }
  616. public function testCanAddMultipleValidators()
  617. {
  618. $this->_checkZf2794();
  619. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  620. $this->assertFalse($this->element->getValidator('Zend_Validate_Alnum'));
  621. $this->element->addValidators(array('digits', 'alnum'));
  622. $digits = $this->element->getValidator('digits');
  623. $this->assertTrue($digits instanceof Zend_Validate_Digits);
  624. $alnum = $this->element->getValidator('alnum');
  625. $this->assertTrue($alnum instanceof Zend_Validate_Alnum);
  626. }
  627. public function testRemovingUnregisteredValidatorReturnsObjectInstance()
  628. {
  629. $this->assertSame($this->element, $this->element->removeValidator('bogus'));
  630. }
  631. public function testPassingMessagesOptionToAddValidatorSetsValidatorMessages()
  632. {
  633. $messageTemplates = array(
  634. Zend_Validate_Digits::NOT_DIGITS => 'Value should only contain digits',
  635. Zend_Validate_Digits::STRING_EMPTY => 'Value needs some digits',
  636. );
  637. $this->element->setAllowEmpty(false)
  638. ->addValidator('digits', false, array('messages' => $messageTemplates));
  639. $this->element->isValid('');
  640. $messages = $this->element->getMessages();
  641. $found = false;
  642. foreach ($messages as $key => $message) {
  643. if ($key == Zend_Validate_Digits::STRING_EMPTY) {
  644. $found = true;
  645. break;
  646. }
  647. }
  648. $this->assertTrue($found, 'Empty string message not found: ' . var_export($messages, 1));
  649. $this->assertEquals($messageTemplates[Zend_Validate_Digits::STRING_EMPTY], $message);
  650. $this->element->isValid('abc');
  651. $messages = $this->element->getMessages();
  652. $found = false;
  653. foreach ($messages as $key => $message) {
  654. if ($key == Zend_Validate_Digits::NOT_DIGITS) {
  655. $found = true;
  656. break;
  657. }
  658. }
  659. $this->assertTrue($found, 'Not digits message not found');
  660. $this->assertEquals($messageTemplates[Zend_Validate_Digits::NOT_DIGITS], $message);
  661. }
  662. public function testCanPassSingleMessageToValidatorToSetValidatorMessages()
  663. {
  664. $this->_checkZf2794();
  665. $message = 'My custom empty message';
  666. $this->element->addValidator('notEmpty', false, array('messages' => $message))
  667. ->setRequired(true);
  668. $this->element->isValid('');
  669. $messages = $this->element->getMessages();
  670. $this->assertEquals(1, count($messages));
  671. $this->assertEquals($message, current($messages));
  672. }
  673. public function testMessagesAreTranslatedForCurrentLocale()
  674. {
  675. $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
  676. $translations = include($localeFile);
  677. $translator = new Zend_Translate('array', $translations, 'en');
  678. $translator->setLocale('en');
  679. $this->element->setAllowEmpty(false)
  680. ->setTranslator($translator)
  681. ->addValidator('digits');
  682. $this->element->isValid('');
  683. $messages = $this->element->getMessages();
  684. $found = false;
  685. foreach ($messages as $key => $message) {
  686. if ($key == 'stringEmpty') {
  687. $found = true;
  688. break;
  689. }
  690. }
  691. $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
  692. $this->assertEquals($translations['stringEmpty'], $message);
  693. $this->element->isValid('abc');
  694. $messages = $this->element->getMessages();
  695. $found = false;
  696. foreach ($messages as $key => $message) {
  697. if ($key == 'notDigits') {
  698. $found = true;
  699. break;
  700. }
  701. }
  702. $this->assertTrue($found, 'Not Digits message not found');
  703. $this->assertEquals($translations['notDigits'], $message);
  704. }
  705. /**#@+
  706. * @see ZF-2988
  707. */
  708. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  709. {
  710. $this->element->addValidator('Alpha');
  711. $this->element->addErrorMessage('Invalid value entered');
  712. $this->assertFalse($this->element->isValid(123));
  713. $messages = $this->element->getMessages();
  714. $this->assertEquals(1, count($messages));
  715. $this->assertEquals('Invalid value entered', array_shift($messages));
  716. }
  717. public function testCustomErrorMessagesShouldBeManagedInAStack()
  718. {
  719. $this->element->addValidator('Alpha');
  720. $this->element->addErrorMessage('Invalid value entered');
  721. $this->element->addErrorMessage('Really, it is not valid');
  722. $messages = $this->element->getErrorMessages();
  723. $this->assertEquals(2, count($messages));
  724. $this->assertFalse($this->element->isValid(123));
  725. $messages = $this->element->getMessages();
  726. $this->assertEquals(2, count($messages));
  727. $this->assertEquals('Invalid value entered', array_shift($messages));
  728. $this->assertEquals('Really, it is not valid', array_shift($messages));
  729. }
  730. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  731. {
  732. $set1 = array('foo', 'bar', 'baz');
  733. $this->element->addErrorMessages($set1);
  734. $this->assertSame($set1, $this->element->getErrorMessages());
  735. }
  736. public function testSetErrorMessagesShouldOverwriteMessages()
  737. {
  738. $set1 = array('foo', 'bar', 'baz');
  739. $set2 = array('bat', 'cat');
  740. $this->element->addErrorMessages($set1);
  741. $this->assertSame($set1, $this->element->getErrorMessages());
  742. $this->element->setErrorMessages($set2);
  743. $this->assertSame($set2, $this->element->getErrorMessages());
  744. }
  745. public function testCustomErrorMessageStackShouldBeClearable()
  746. {
  747. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  748. $this->element->clearErrorMessages();
  749. $messages = $this->element->getErrorMessages();
  750. $this->assertTrue(empty($messages));
  751. }
  752. public function testCustomErrorMessagesShouldBeTranslated()
  753. {
  754. $translations = array(
  755. 'foo' => 'Foo message',
  756. );
  757. $translate = new Zend_Translate('array', $translations);
  758. $this->element->setTranslator($translate)
  759. ->addErrorMessage('foo')
  760. ->addValidator('Alpha');
  761. $this->assertFalse($this->element->isValid(123));
  762. $messages = $this->element->getMessages();
  763. $this->assertEquals(1, count($messages));
  764. $this->assertEquals('Foo message', array_shift($messages));
  765. }
  766. public function testCustomErrorMessagesShouldAllowValueSubstitution()
  767. {
  768. $this->element->addErrorMessage('"%value%" is an invalid value')
  769. ->addValidator('Alpha');
  770. $this->assertFalse($this->element->isValid(123));
  771. $this->assertTrue($this->element->hasErrors());
  772. $messages = $this->element->getMessages();
  773. $this->assertEquals(1, count($messages));
  774. $this->assertEquals('"123" is an invalid value', array_shift($messages));
  775. }
  776. public function testShouldAllowMarkingElementAsInvalid()
  777. {
  778. $this->element->setValue('foo');
  779. $this->element->addErrorMessage('Invalid value entered');
  780. $this->assertFalse($this->element->hasErrors());
  781. $this->element->markAsError();
  782. $this->assertTrue($this->element->hasErrors());
  783. $messages = $this->element->getMessages();
  784. $this->assertEquals(1, count($messages));
  785. $this->assertEquals('Invalid value entered', array_shift($messages));
  786. }
  787. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  788. {
  789. $this->element->setValue('foo');
  790. $this->assertFalse($this->element->hasErrors());
  791. $this->element->setErrors(array('Error 1', 'Error 2'))
  792. ->addError('Error 3')
  793. ->addErrors(array('Error 4', 'Error 5'));
  794. $this->assertTrue($this->element->hasErrors());
  795. $messages = $this->element->getMessages();
  796. $this->assertEquals(5, count($messages));
  797. foreach (range(1, 5) as $id) {
  798. $message = 'Error ' . $id;
  799. $this->assertContains($message, $messages);
  800. }
  801. }
  802. public function testHasErrorsShouldIndicateStatusOfValidationErrors()
  803. {
  804. $this->element->setValue('foo');
  805. $this->assertFalse($this->element->hasErrors());
  806. $this->element->markAsError();
  807. $this->assertTrue($this->element->hasErrors());
  808. }
  809. /**#@-*/
  810. public function testAddingErrorToArrayElementShouldLoopOverAllValues()
  811. {
  812. $this->element->setIsArray(true)
  813. ->setValue(array('foo', 'bar', 'baz'))
  814. ->addError('error with value %value%');
  815. $errors = $this->element->getMessages();
  816. require_once 'Zend/Json.php';
  817. $errors = Zend_Json::encode($errors);
  818. foreach (array('foo', 'bar', 'baz') as $value) {
  819. $message = 'error with value ' . $value;
  820. $this->assertContains($message, $errors);
  821. }
  822. }
  823. /** ZF-2568 */
  824. public function testTranslatedMessagesCanContainVariableSubstitution()
  825. {
  826. $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
  827. $translations = include($localeFile);
  828. $translations['notDigits'] .= ' "%value%"';
  829. $translator = new Zend_Translate('array', $translations, 'en');
  830. $translator->setLocale('en');
  831. $this->element->setAllowEmpty(false)
  832. ->setTranslator($translator)
  833. ->addValidator('digits');
  834. $this->element->isValid('abc');
  835. $messages = $this->element->getMessages();
  836. $found = false;
  837. foreach ($messages as $key => $message) {
  838. if ($key == 'notDigits') {
  839. $found = true;
  840. break;
  841. }
  842. }
  843. $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
  844. $this->assertContains(' "abc"', $message);
  845. $this->assertContains('Translating the notDigits string', $message);
  846. }
  847. public function testCanRemoveValidator()
  848. {
  849. $this->_checkZf2794();
  850. $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
  851. $this->element->addValidator('digits');
  852. $digits = $this->element->getValidator('digits');
  853. $this->assertTrue($digits instanceof Zend_Validate_Digits);
  854. $this->element->removeValidator('digits');
  855. $this->assertFalse($this->element->getValidator('digits'));
  856. }
  857. public function testCanClearAllValidators()
  858. {
  859. $this->_checkZf2794();
  860. $this->testCanAddMultipleValidators();
  861. $validators = $this->element->getValidators();
  862. $this->element->clearValidators();
  863. $test = $this->element->getValidators();
  864. $this->assertNotEquals($validators, $test);
  865. $this->assertTrue(empty($test));
  866. foreach (array_keys($validators) as $validator) {
  867. $this->assertFalse($this->element->getValidator($validator));
  868. }
  869. }
  870. public function testCanValidateElement()
  871. {
  872. $this->element->addValidator(new Zend_Validate_NotEmpty())
  873. ->addValidator(new Zend_Validate_EmailAddress());
  874. try {
  875. $result = $this->element->isValid('matthew@zend.com');
  876. } catch (Exception $e) {
  877. $this->fail('Validating an element should work');
  878. }
  879. }
  880. public function testCanValidateArrayValue()
  881. {
  882. $this->element->setIsArray(true)
  883. ->addValidator('InArray', false, array(array('foo', 'bar', 'baz', 'bat')));
  884. $this->assertTrue($this->element->isValid(array('foo', 'bat')));
  885. }
  886. public function testShouldAllowZeroAsNonEmptyValue()
  887. {
  888. $this->element->addValidator('between', false, array(1, 100));
  889. $this->assertFalse($this->element->isValid('0'));
  890. }
  891. public function testIsValidPopulatesElementValue()
  892. {
  893. $this->testCanValidateElement();
  894. $this->assertEquals('matthew@zend.com', $this->element->getValue());
  895. }
  896. public function testErrorsPopulatedFollowingFailedIsValidCheck()
  897. {
  898. $this->element->addValidator(new Zend_Validate_NotEmpty())
  899. ->addValidator(new Zend_Validate_EmailAddress());
  900. $result = $this->element->isValid('matthew');
  901. if ($result) {
  902. $this->fail('Invalid data should fail validations');
  903. }
  904. $errors = $this->element->getErrors();
  905. $this->assertTrue(is_array($errors));
  906. $this->assertTrue(0 < count($errors));
  907. }
  908. public function testMessagesPopulatedFollowingFailedIsValidCheck()
  909. {
  910. require_once 'Zend/Validate/NotEmpty.php';
  911. require_once 'Zend/Validate/EmailAddress.php';
  912. $this->element->addValidator(new Zend_Validate_NotEmpty())
  913. ->addValidator(new Zend_Validate_EmailAddress());
  914. $result = $this->element->isValid('matthew');
  915. if ($result) {
  916. $this->fail('Invalid data should fail validations');
  917. }
  918. $messages = $this->element->getMessages();
  919. $this->assertTrue(is_array($messages));
  920. $this->assertTrue(0 < count($messages));
  921. }
  922. public function testOptionalElementDoesNotPerformValidationsOnEmptyValuesByDefault()
  923. {
  924. $this->element->addValidator(new Zend_Validate_EmailAddress());
  925. $result = $this->element->isValid('');
  926. if (!$result) {
  927. $this->fail('Empty data should not fail validations');
  928. }
  929. $errors = $this->element->getErrors();
  930. $this->assertTrue(is_array($errors));
  931. $this->assertTrue(empty($errors));
  932. }
  933. public function testOptionalElementDoesPerformValidationsWhenAllowEmptyIsFalse()
  934. {
  935. $this->element->setAllowEmpty(false)
  936. ->addValidator(new Zend_Validate_EmailAddress());
  937. $result = $this->element->isValid('');
  938. if ($result) {
  939. $this->fail('Empty data should fail validations when AllowEmpty is false');
  940. }
  941. $errors = $this->element->getErrors();
  942. $this->assertTrue(is_array($errors));
  943. $this->assertTrue(0 < count($errors));
  944. }
  945. public function testAddingInvalidFilterTypeThrowsException()
  946. {
  947. try {
  948. $this->element->addFilter(123);
  949. $this->fail('Invalid filter type should raise exception');
  950. } catch (Zend_Form_Exception $e) {
  951. $this->assertContains('Invalid filter', $e->getMessage());
  952. }
  953. }
  954. public function testCanAddSingleFilterAsString()
  955. {
  956. $this->_checkZf2794();
  957. $this->assertFalse($this->element->getFilter('digits'));
  958. $this->element->addFilter('digits');
  959. $filter = $this->element->getFilter('digits');
  960. $this->assertTrue($filter instanceof Zend_Filter_Digits);
  961. }
  962. public function testCanNotRetrieveSingleFilterRegisteredAsStringUsingClassName()
  963. {
  964. $this->assertFalse($this->element->getFilter('digits'));
  965. $this->element->addFilter('digits');
  966. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  967. }
  968. public function testCanAddSingleFilterAsFilterObject()
  969. {
  970. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  971. require_once 'Zend/Filter/Digits.php';
  972. $filter = new Zend_Filter_Digits();
  973. $this->element->addFilter($filter);
  974. $test = $this->element->getFilter('Zend_Filter_Digits');
  975. $this->assertSame($filter, $test);
  976. }
  977. public function testCanRetrieveSingleFilterRegisteredAsFilterObjectUsingShortName()
  978. {
  979. $this->_checkZf2794();
  980. $this->assertFalse($this->element->getFilter('digits'));
  981. require_once 'Zend/Filter/Digits.php';
  982. $filter = new Zend_Filter_Digits();
  983. $this->element->addFilter($filter);
  984. $test = $this->element->getFilter('digits');
  985. }
  986. public function testRetrievingNamedFilterShouldNotReorderFilters()
  987. {
  988. $this->element->addFilters(array(
  989. 'Alpha',
  990. 'Alnum',
  991. 'Digits',
  992. ));
  993. $filter = $this->element->getFilter('Alnum');
  994. $filters = $this->element->getFilters();
  995. $i = 0;
  996. $order = array();
  997. foreach (array_keys($filters) as $name) {
  998. $order[$name] = $i;
  999. ++$i;
  1000. }
  1001. $this->assertEquals(1, $order['Zend_Filter_Alnum'], var_export($order, 1));
  1002. }
  1003. public function testOptionsAreCastToArrayWhenAddingFilter()
  1004. {
  1005. $this->_checkZf2794();
  1006. try {
  1007. $this->element->addFilter('Alnum', true);
  1008. } catch (Exception $e) {
  1009. $this->fail('Should be able to add non-array filter options');
  1010. }
  1011. $filter = $this->element->getFilter('Alnum');
  1012. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  1013. $this->assertTrue($filter->allowWhiteSpace);
  1014. }
  1015. public function testShouldUseFilterConstructorOptionsAsPassedToAddFilter()
  1016. {
  1017. $this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'UTF-8')));
  1018. $filter = $this->element->getFilter('HtmlEntities');
  1019. $this->assertTrue($filter instanceof Zend_Filter_HtmlEntities);
  1020. $this->assertEquals(ENT_QUOTES, $filter->getQuoteStyle());
  1021. $this->assertEquals('UTF-8', $filter->getCharSet());
  1022. }
  1023. public function testCanAddMultipleFilters()
  1024. {
  1025. $this->_checkZf2794();
  1026. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  1027. $this->assertFalse($this->element->getFilter('Zend_Filter_Alnum'));
  1028. $this->element->addFilters(array('digits', 'alnum'));
  1029. $digits = $this->element->getFilter('digits');
  1030. $this->assertTrue($digits instanceof Zend_Filter_Digits);
  1031. $alnum = $this->element->getFilter('alnum');
  1032. $this->assertTrue($alnum instanceof Zend_Filter_Alnum);
  1033. }
  1034. public function testRemovingUnregisteredFilterReturnsObjectInstance()
  1035. {
  1036. $this->assertSame($this->element, $this->element->removeFilter('bogus'));
  1037. }
  1038. public function testCanRemoveFilter()
  1039. {
  1040. $this->_checkZf2794();
  1041. $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
  1042. $this->element->addFilter('digits');
  1043. $digits = $this->element->getFilter('digits');
  1044. $this->assertTrue($digits instanceof Zend_Filter_Digits);
  1045. $this->element->removeFilter('digits');
  1046. $this->assertFalse($this->element->getFilter('digits'));
  1047. }
  1048. public function testCanClearAllFilters()
  1049. {
  1050. $this->_checkZf2794();
  1051. $this->testCanAddMultipleFilters();
  1052. $filters = $this->element->getFilters();
  1053. $this->element->clearFilters();
  1054. $test = $this->element->getFilters();
  1055. $this->assertNotEquals($filters, $test);
  1056. $this->assertTrue(empty($test));
  1057. foreach (array_keys($filters) as $filter) {
  1058. $this->assertFalse($this->element->getFilter($filter));
  1059. }
  1060. }
  1061. public function testGetViewReturnsNullWithNoViewRenderer()
  1062. {
  1063. $this->assertNull($this->element->getView());
  1064. }
  1065. public function testGetViewReturnsViewRendererViewInstanceIfViewRendererActive()
  1066. {
  1067. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  1068. $viewRenderer->initView();
  1069. $view = $viewRenderer->view;
  1070. $test = $this->element->getView();
  1071. $this->assertSame($view, $test);
  1072. }
  1073. public function testCanSetView()
  1074. {
  1075. $view = new Zend_View();
  1076. $this->assertNull($this->element->getView());
  1077. $this->element->setView($view);
  1078. $received = $this->element->getView();
  1079. $this->assertSame($view, $received);
  1080. }
  1081. public function testViewHelperDecoratorRegisteredByDefault()
  1082. {
  1083. $this->_checkZf2794();
  1084. $decorator = $this->element->getDecorator('viewHelper');
  1085. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  1086. }
  1087. /**
  1088. * @group ZF-4822
  1089. */
  1090. public function testErrorsDecoratorRegisteredByDefault()
  1091. {
  1092. $this->_checkZf2794();
  1093. $decorator = $this->element->getDecorator('errors');
  1094. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  1095. }
  1096. /**
  1097. * @group ZF-4822
  1098. */
  1099. public function testDescriptionDecoratorRegisteredByDefault()
  1100. {
  1101. $this->_checkZf2794();
  1102. $decorator = $this->element->getDecorator('description');
  1103. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  1104. $options = $decorator->getOptions();
  1105. $this->assertTrue(array_key_exists('tag', $options));
  1106. $this->assertEquals('p', $options['tag']);
  1107. $this->assertTrue(array_key_exists('class', $options));
  1108. $this->assertEquals('description', $options['class']);
  1109. }
  1110. /**
  1111. * @group ZF-4822
  1112. */
  1113. public function testHtmlTagDecoratorRegisteredByDefault()
  1114. {
  1115. $this->_checkZf2794();
  1116. $decorator = $this->element->getDecorator('HtmlTag');
  1117. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1118. }
  1119. /**
  1120. * @group ZF-4822
  1121. */
  1122. public function testLabelDecoratorRegisteredByDefault()
  1123. {
  1124. $this->_checkZf2794();
  1125. $decorator = $this->element->getDecorator('Label');
  1126. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1127. }
  1128. public function testCanDisableRegisteringDefaultDecoratorsDuringInitialization()
  1129. {
  1130. $element = new Zend_Form_Element('foo', array('disableLoadDefaultDecorators' => true));
  1131. $decorators = $element->getDecorators();
  1132. $this->assertEquals(array(), $decorators);
  1133. }
  1134. public function testAddingInvalidDecoratorThrowsException()
  1135. {
  1136. try {
  1137. $this->element->addDecorator(123);
  1138. $this->fail('Invalid decorator type should raise exception');
  1139. } catch (Zend_Form_Exception $e) {
  1140. $this->assertContains('Invalid decorator', $e->getMessage());
  1141. }
  1142. }
  1143. public function testCanAddSingleDecoratorAsString()
  1144. {
  1145. $this->_checkZf2794();
  1146. $this->element->clearDecorators();
  1147. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1148. $this->element->addDecorator('viewHelper');
  1149. $decorator = $this->element->getDecorator('viewHelper');
  1150. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  1151. }
  1152. public function testCanNotRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  1153. {
  1154. $this->assertFalse($this->element->getDecorator('Zend_Form_Decorator_ViewHelper'));
  1155. }
  1156. public function testCanAddSingleDecoratorAsDecoratorObject()
  1157. {
  1158. $this->element->clearDecorators();
  1159. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1160. $decorator = new Zend_Form_Decorator_ViewHelper;
  1161. $this->element->addDecorator($decorator);
  1162. $test = $this->element->getDecorator('Zend_Form_Decorator_ViewHelper');
  1163. $this->assertSame($decorator, $test);
  1164. }
  1165. /**
  1166. * @see ZF-3597
  1167. */
  1168. public function testAddingConcreteDecoratorShouldHonorOrder()
  1169. {
  1170. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  1171. $decorator = new My_Decorator_TableRow();
  1172. $this->element->setLabel('Foo')
  1173. ->setDescription('sample description')
  1174. ->clearDecorators()
  1175. ->addDecorators(array(
  1176. 'ViewHelper',
  1177. $decorator,
  1178. ));
  1179. $html = $this->element->render($this->getView());
  1180. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  1181. }
  1182. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  1183. {
  1184. $this->_checkZf2794();
  1185. $this->element->clearDecorators();
  1186. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1187. $decorator = new Zend_Form_Decorator_ViewHelper;
  1188. $this->element->addDecorator($decorator);
  1189. $test = $this->element->getDecorator('viewHelper');
  1190. $this->assertSame($decorator, $test);
  1191. }
  1192. public function testCanAddMultipleDecorators()
  1193. {
  1194. $this->_checkZf2794();
  1195. $this->element->clearDecorators();
  1196. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1197. $testDecorator = new Zend_Form_ElementTest_Decorator;
  1198. $this->element->addDecorators(array(
  1199. 'ViewHelper',
  1200. $testDecorator
  1201. ));
  1202. $viewHelper = $this->element->getDecorator('viewHelper');
  1203. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  1204. $decorator = $this->element->getDecorator('decorator');
  1205. $this->assertSame($testDecorator, $decorator);
  1206. }
  1207. public function testRemovingUnregisteredDecoratorReturnsObjectInstance()
  1208. {
  1209. $this->_checkZf2794();
  1210. $this->assertSame($this->element, $this->element->removeDecorator('bogus'));
  1211. }
  1212. public function testCanRemoveDecorator()
  1213. {
  1214. $this->testViewHelperDecoratorRegisteredByDefault();
  1215. $this->element->removeDecorator('viewHelper');
  1216. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1217. }
  1218. /**
  1219. * @see ZF-3069
  1220. */
  1221. public function testRemovingNamedDecoratorsShouldWork()
  1222. {
  1223. $this->_checkZf2794();
  1224. $this->element->setDecorators(array(
  1225. 'ViewHelper',
  1226. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  1227. array(array('div2' => 'HtmlTag'), array('tag' => 'div')),
  1228. ));
  1229. $decorators = $this->element->getDecorators();
  1230. $this->assertTrue(array_key_exists('div', $decorators));
  1231. $this->assertTrue(array_key_exists('div2', $decorators));
  1232. $this->element->removeDecorator('div');
  1233. $decorators = $this->element->getDecorators();
  1234. $this->assertFalse(array_key_exists('div', $decorators));
  1235. $this->assertTrue(array_key_exists('div2', $decorators));
  1236. }
  1237. public function testCanClearAllDecorators()
  1238. {
  1239. $this->testCanAddMultipleDecorators();
  1240. $this->element->clearDecorators();
  1241. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1242. $this->assertFalse($this->element->getDecorator('decorator'));
  1243. }
  1244. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  1245. {
  1246. $this->_checkZf2794();
  1247. $this->element->setDecorators(array(
  1248. array('HtmlTag', array('tag' => 'span')),
  1249. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'div')),
  1250. ));
  1251. $decorator = $this->element->getDecorator('FooBar');
  1252. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1253. $this->assertEquals('div', $decorator->getOption('tag'));
  1254. $decorator = $this->element->getDecorator('HtmlTag');
  1255. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  1256. $this->assertEquals('span', $decorator->getOption('tag'));
  1257. }
  1258. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  1259. {
  1260. $this->element->setDecorators(array(
  1261. 'ViewHelper',
  1262. 'Errors',
  1263. array(array('inner' => 'HtmlTag'), array('tag' => 'div', 'class' => 'element')),
  1264. 'Label',
  1265. array(array('outer' => 'HtmlTag'), array('tag' => 'div')),
  1266. ));
  1267. $decorator = $this->element->getDecorator('inner');
  1268. $decorators = $this->element->getDecorators();
  1269. $i = 0;
  1270. $order = array();
  1271. foreach (array_keys($decorators) as $name) {
  1272. $order[$name] = $i;
  1273. ++$i;
  1274. }
  1275. $this->assertEquals(2, $order['inner'], var_export($order, 1));
  1276. }
  1277. /**
  1278. * @see ZF-3376
  1279. */
  1280. public function testSetDecoratorsShouldAcceptReturnOfGetDecorators()
  1281. {
  1282. $this->element->setDecorators(array(
  1283. 'ViewHelper',
  1284. 'Errors',
  1285. array('input' => 'HtmlTag', array('tag' => 'div', 'class' => 'input')),
  1286. 'Label',
  1287. array('element' => 'HtmlTag', array('tag' => 'div', 'class' => 'element')),
  1288. ));
  1289. $decorators = $this->element->getDecorators();
  1290. $this->element->setDecorators($decorators);
  1291. $this->assertSame($decorators, $this->element->getDecorators());
  1292. }
  1293. public function testRenderElementReturnsMarkup()
  1294. {
  1295. $this->element->setName('foo');
  1296. $html = $this->element->render($this->getView());
  1297. $this->assertTrue(is_string($html));
  1298. $this->assertFalse(empty($html));
  1299. $this->assertContains('<input', $html);
  1300. $this->assertContains('"foo"', $html);
  1301. }
  1302. public function testRenderElementRendersLabelWhenProvided()
  1303. {
  1304. $this->element->setView($this->getView());
  1305. $this->element->setName('foo')
  1306. ->setLabel('Foo');
  1307. $html = $this->element->render();
  1308. $this->assertTrue(is_string($html));
  1309. $this->assertFalse(empty($html));
  1310. $this->assertContains('<label', $html);
  1311. $this->assertContains('Foo', $html);
  1312. $this->assertContains('</label>', $html);
  1313. }
  1314. public function testRenderElementRendersValueWhenProvided()
  1315. {
  1316. $this->element->setView($this->getView());
  1317. $this->element->setName('foo')
  1318. ->setValue('bar');
  1319. $html = $this->element->render();
  1320. $this->assertTrue(is_string($html));
  1321. $this->assertFalse(empty($html));
  1322. $this->assertContains('<input', $html);
  1323. $this->assertContains('"foo"', $html);
  1324. $this->assertContains('"bar"', $html);
  1325. }
  1326. public function testRenderElementRendersErrorsWhenProvided()
  1327. {
  1328. $this->_checkZf2794();
  1329. $this->element->setView($this->getView())
  1330. ->setRequired(true)
  1331. ->setName('foo')
  1332. ->addValidator('NotEmpty');
  1333. $this->element->isValid('');
  1334. $html = $this->element->render();
  1335. $this->assertTrue(is_string($html));
  1336. $this->assertFalse(empty($html));
  1337. $this->assertContains('error', $html);
  1338. $this->assertRegexp('/empty/i', $html);
  1339. }
  1340. public function testToStringProxiesToRender()
  1341. {
  1342. $this->element->setView($this->getView());
  1343. $this->element->setName('foo');
  1344. $html = $this->element->__toString();
  1345. $this->assertTrue(is_string($html));
  1346. $this->assertFalse(empty($html));
  1347. $this->assertContains('<input', $html);
  1348. $this->assertContains('"foo"', $html);
  1349. }
  1350. public function raiseDecoratorException($content, $element, $options)
  1351. {
  1352. throw new Exception('Raising exception in decorator callback');
  1353. }
  1354. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  1355. {
  1356. $this->error = $errstr;
  1357. }
  1358. public function testToStringRaisesErrorWhenExceptionCaught()
  1359. {
  1360. $this->element->setDecorators(array(
  1361. array(
  1362. 'decorator' => 'Callback',
  1363. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  1364. ),
  1365. ));
  1366. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  1367. $text = $this->element->__toString();
  1368. restore_error_handler();
  1369. $this->assertTrue(empty($text));
  1370. $this->assertTrue(isset($this->error));
  1371. $this->assertEquals('Raising exception in decorator callback', $this->error);
  1372. }
  1373. public function getOptions()
  1374. {
  1375. $options = array(
  1376. 'name' => 'changed',
  1377. 'value' => 'foo',
  1378. 'label' => 'bar',
  1379. 'order' => 50,
  1380. 'required' => false,
  1381. 'foo' => 'bar',
  1382. 'baz' => 'bat'
  1383. );
  1384. return $options;
  1385. }
  1386. public function testCanSetObjectStateViaSetOptions()
  1387. {
  1388. $options = $this->getOptions();
  1389. $this->element->setOptions($options);
  1390. $this->assertEquals('changed', $this->element->getName());
  1391. $this->assertEquals('foo', $this->element->getValue());
  1392. $this->assertEquals('bar', $this->element->getLabel());
  1393. $this->assertEquals(50, $this->element->getOrder());
  1394. $this->assertFalse($this->element->isRequired());
  1395. $this->assertEquals('bar', $this->element->foo);
  1396. $this->assertEquals('bat', $this->element->baz);
  1397. }
  1398. public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
  1399. {
  1400. $options = $this->getOptions();
  1401. $config = new Zend_Config($options);
  1402. $options['config'] = $config;
  1403. $options['options'] = $config->toArray();
  1404. $this->element->setOptions($options);
  1405. }
  1406. public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNoObjectPresent()
  1407. {
  1408. $options = $this->getOptions();
  1409. $options['translator'] = true;
  1410. $options['pluginLoader'] = true;
  1411. $options['view'] = true;
  1412. $this->element->setOptions($options);
  1413. }
  1414. public function testSetOptionsSetsArrayOfStringValidators()
  1415. {
  1416. $this->_checkZf2794();
  1417. $options = $this->getOptions();
  1418. $options['validators'] = array(
  1419. 'notEmpty',
  1420. 'digits'
  1421. );
  1422. $this->element->setOptions($options);
  1423. $validator = $this->element->getValidator('notEmpty');
  1424. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1425. $validator = $this->element->getValidator('digits');
  1426. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1427. }
  1428. public function testSetOptionsSetsArrayOfArrayValidators()
  1429. {
  1430. $this->_checkZf2794();
  1431. $options = $this->getOptions();
  1432. $options['validators'] = array(
  1433. array('notEmpty', true, array('bar')),
  1434. array('digits', true, array('bar')),
  1435. );
  1436. $this->element->setOptions($options);
  1437. $validator = $this->element->getValidator('notEmpty');
  1438. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1439. $this->assertTrue($validator->zfBreakChainOnFailure);
  1440. $validator = $this->element->getValidator('digits');
  1441. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1442. $this->assertTrue($validator->zfBreakChainOnFailure);
  1443. }
  1444. public function testSetOptionsSetsArrayOfAssociativeArrayValidators()
  1445. {
  1446. $this->_checkZf2794();
  1447. $options = $this->getOptions();
  1448. $options['validators'] = array(
  1449. array(
  1450. 'options' => array('bar'),
  1451. 'breakChainOnFailure' => true,
  1452. 'validator' => 'notEmpty',
  1453. ),
  1454. array(
  1455. 'options' => array('bar'),
  1456. 'validator' => 'digits',
  1457. 'breakChainOnFailure' => true,
  1458. ),
  1459. );
  1460. $this->element->setOptions($options);
  1461. $validator = $this->element->getValidator('notEmpty');
  1462. $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
  1463. $this->assertTrue($validator->zfBreakChainOnFailure);
  1464. $validator = $this->element->getValidator('digits');
  1465. $this->assertTrue($validator instanceof Zend_Validate_Digits);
  1466. $this->assertTrue($validator->zfBreakChainOnFailure);
  1467. }
  1468. public function testSetOptionsSetsArrayOfStringFilters()
  1469. {
  1470. $this->_checkZf2794();
  1471. $options = $this->getOptions();
  1472. $options['filters'] = array('StringToUpper', 'Alpha');
  1473. $this->element->setOptions($options);
  1474. $filter = $this->element->getFilter('StringToUpper');
  1475. $this->assertTrue($filter instanceof Zend_Filter_StringToUpper);
  1476. $filter = $this->element->getFilter('Alpha');
  1477. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1478. }
  1479. public function testSetOptionsSetsArrayOfArrayFilters()
  1480. {
  1481. $this->_checkZf2794();
  1482. $options = $this->getOptions();
  1483. $options['filters'] = array(
  1484. array('StringToUpper', array('bar' => 'baz')),
  1485. array('Alpha', array('foo')),
  1486. );
  1487. $this->element->setOptions($options);
  1488. $filter = $this->element->getFilter('StringToUpper');
  1489. $this->assertTrue($filter instanceof Zend_Filter_StringToUpper);
  1490. $filter = $this->element->getFilter('Alpha');
  1491. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1492. }
  1493. public function testSetOptionsSetsArrayOfAssociativeArrayFilters()
  1494. {
  1495. $this->_checkZf2794();
  1496. $options = $this->getOptions();
  1497. $options['filters'] = array(
  1498. array(
  1499. 'options' => array('baz'),
  1500. 'filter' => 'StringToUpper'
  1501. ),
  1502. array(
  1503. 'options' => array('foo'),
  1504. 'filter' => 'Alpha',
  1505. ),
  1506. );
  1507. $this->element->setOptions($options);
  1508. $filter = $this->element->getFilter('StringToUpper');
  1509. $this->assertTrue($filter instanceof Zend_Filter_StringToUpper);
  1510. $filter = $this->element->getFilter('Alpha');
  1511. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  1512. }
  1513. public function testSetOptionsSetsArrayOfStringDecorators()
  1514. {
  1515. $this->_checkZf2794();
  1516. $options = $this->getOptions();
  1517. $options['decorators'] = array('label', 'form');
  1518. $this->element->setOptions($options);
  1519. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1520. $this->assertFalse($this->element->getDecorator('errors'));
  1521. $decorator = $this->element->getDecorator('label');
  1522. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1523. $decorator = $this->element->getDecorator('form');
  1524. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1525. }
  1526. public function testSetOptionsSetsArrayOfArrayDecorators()
  1527. {
  1528. $this->_checkZf2794();
  1529. $options = $this->getOptions();
  1530. $options['decorators'] = array(
  1531. array('label', array('id' => 'mylabel')),
  1532. array('form', array('id' => 'form')),
  1533. );
  1534. $this->element->setOptions($options);
  1535. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1536. $this->assertFalse($this->element->getDecorator('errors'));
  1537. $decorator = $this->element->getDecorator('label');
  1538. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1539. $options = $decorator->getOptions();
  1540. $this->assertEquals('mylabel', $options['id']);
  1541. $decorator = $this->element->getDecorator('form');
  1542. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1543. $options = $decorator->getOptions();
  1544. $this->assertEquals('form', $options['id']);
  1545. }
  1546. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  1547. {
  1548. $this->_checkZf2794();
  1549. $options = $this->getOptions();
  1550. $options['decorators'] = array(
  1551. array(
  1552. 'options' => array('id' => 'mylabel'),
  1553. 'decorator' => 'label',
  1554. ),
  1555. array(
  1556. 'options' => array('id' => 'form'),
  1557. 'decorator' => 'form',
  1558. ),
  1559. );
  1560. $this->element->setOptions($options);
  1561. $this->assertFalse($this->element->getDecorator('viewHelper'));
  1562. $this->assertFalse($this->element->getDecorator('errors'));
  1563. $decorator = $this->element->getDecorator('label');
  1564. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  1565. $options = $decorator->getOptions();
  1566. $this->assertEquals('mylabel', $options['id']);
  1567. $decorator = $this->element->getDecorator('form');
  1568. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  1569. $options = $decorator->getOptions();
  1570. $this->assertEquals('form', $options['id']);
  1571. }
  1572. public function testSetOptionsSetsGlobalPrefixPaths()
  1573. {
  1574. $options = $this->getOptions();
  1575. $options['prefixPath'] = array(
  1576. 'prefix' => 'Zend_Foo',
  1577. 'path' => 'Zend/Foo/'
  1578. );
  1579. $this->element->setOptions($options);
  1580. foreach (array('validate', 'filter', 'decorator') as $type) {
  1581. $loader = $this->element->getPluginLoader($type);
  1582. $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
  1583. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  1584. $this->assertFalse(empty($paths));
  1585. $this->assertContains('Foo', $paths[0]);
  1586. }
  1587. }
  1588. public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
  1589. {
  1590. $options = $this->getOptions();
  1591. $options['prefixPath'] = array(
  1592. 'filter' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  1593. );
  1594. $this->element->setOptions($options);
  1595. $loader = $this->element->getPluginLoader('filter');
  1596. $paths = $loader->getPaths('Zend_Foo');
  1597. $this->assertTrue(is_array($paths));
  1598. $this->assertFalse(empty($paths));
  1599. $this->assertContains('Foo', $paths[0]);
  1600. }
  1601. public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
  1602. {
  1603. $options = $this->getOptions();
  1604. $options['prefixPath'] = array(
  1605. array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  1606. );
  1607. $this->element->setOptions($options);
  1608. $loader = $this->element->getPluginLoader('decorator');
  1609. $paths = $loader->getPaths('Zend_Foo');
  1610. $this->assertTrue(is_array($paths));
  1611. $this->assertFalse(empty($paths));
  1612. $this->assertContains('Foo', $paths[0]);
  1613. }
  1614. public function testCanSetObjectStateViaSetConfig()
  1615. {
  1616. $config = new Zend_Config($this->getOptions());
  1617. $this->element->setConfig($config);
  1618. $this->assertEquals('changed', $this->element->getName());
  1619. $this->assertEquals('foo', $this->element->getValue());
  1620. $this->assertEquals('bar', $this->element->getLabel());
  1621. $this->assertEquals(50, $this->element->getOrder());
  1622. $this->assertFalse($this->element->isRequired());
  1623. $this->assertEquals('bar', $this->element->foo);
  1624. $this->assertEquals('bat', $this->element->baz);
  1625. }
  1626. public function testPassingConfigObjectToConstructorSetsObjectState()
  1627. {
  1628. $config = new Zend_Config($this->getOptions());
  1629. $element = new Zend_Form_Element($config);
  1630. $this->assertEquals('changed', $element->getName());
  1631. $this->assertEquals('foo', $element->getValue());
  1632. $this->assertEquals('bar', $element->getLabel());
  1633. $this->assertEquals(50, $element->getOrder());
  1634. $this->assertFalse($element->isRequired());
  1635. $this->assertEquals('bar', $element->foo);
  1636. $this->assertEquals('bat', $element->baz);
  1637. }
  1638. public function testValueIsFilteredPriorToValidation()
  1639. {
  1640. $this->_checkZf2794();
  1641. $this->element->addFilter('StringTrim')
  1642. ->addValidator('StringLength', false, array(3, 8));
  1643. $this->assertTrue($this->element->isValid(' foobar '));
  1644. $this->assertEquals('foobar', $this->element->getValue());
  1645. $this->element->setFilters(array('StringTrim'))
  1646. ->setRequired(true)
  1647. ->setValidators(array('NotEmpty'));
  1648. $this->assertFalse($this->element->isValid(' '));
  1649. }
  1650. // Extensions
  1651. public function testInitCalledBeforeLoadDecorators()
  1652. {
  1653. $element = new Zend_Form_ElementTest_Element('test');
  1654. $decorators = $element->getDecorators();
  1655. $this->assertTrue(empty($decorators));
  1656. }
  1657. /**
  1658. * @group ZF-3217
  1659. */
  1660. public function testElementShouldOverloadToRenderDecorators()
  1661. {
  1662. $this->element->setLabel('Foo Label')
  1663. ->setView($this->getView());
  1664. $html = $this->element->renderViewHelper();
  1665. $this->assertContains('<input', $html);
  1666. $this->assertContains('id="' . $this->element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  1667. $this->assertNotContains('<dd', $html);
  1668. $this->assertNotContains('<label', $html);
  1669. $html = $this->element->renderLabel('this is the content');
  1670. $this->assertRegexp('#<label[^>]*for="' . $this->element->getFullyQualifiedName() . '"[^>]*>Foo Label</label>#', $html);
  1671. $this->assertContains('this is the content', $html);
  1672. $this->assertNotContains('<input', $html);
  1673. }
  1674. /**
  1675. * @group ZF-3217
  1676. * @expectedException Zend_Form_Element_Exception
  1677. */
  1678. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  1679. {
  1680. $html = $this->element->bogusMethodCall();
  1681. }
  1682. /**
  1683. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  1684. *
  1685. * @link http://framework.zend.com/issues/browse/ZF-2794
  1686. * @return void
  1687. */
  1688. protected function _checkZf2794()
  1689. {
  1690. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  1691. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  1692. }
  1693. }
  1694. }
  1695. class Zend_Form_ElementTest_Decorator extends Zend_Form_Decorator_Abstract
  1696. {
  1697. }
  1698. class Zend_Form_ElementTest_Element extends Zend_Form_Element
  1699. {
  1700. public function init()
  1701. {
  1702. $this->setDisableLoadDefaultDecorators(true);
  1703. }
  1704. }
  1705. class Zend_Form_ElementTest_ArrayFilter implements Zend_Filter_Interface
  1706. {
  1707. public function filter($value)
  1708. {
  1709. $value = array_filter($value, array($this, '_filter'));
  1710. return $value;
  1711. }
  1712. protected function _filter($value)
  1713. {
  1714. if (is_array($value)) {
  1715. return array_filter($value, array($this, '_filter'));
  1716. }
  1717. return (strstr($value, 'ba'));
  1718. }
  1719. }
  1720. if (PHPUnit_MAIN_METHOD == 'Zend_Form_ElementTest::main') {
  1721. Zend_Form_ElementTest::main();
  1722. }