FormTest.php 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781
  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_FormTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. // error_reporting(E_ALL);
  27. require_once 'Zend/Form.php';
  28. require_once 'Zend/Config.php';
  29. require_once 'Zend/Controller/Action/HelperBroker.php';
  30. require_once 'Zend/Form/Decorator/Form.php';
  31. require_once 'Zend/Form/DisplayGroup.php';
  32. require_once 'Zend/Form/Element.php';
  33. require_once 'Zend/Form/Element/Text.php';
  34. require_once 'Zend/Form/Element/File.php';
  35. require_once 'Zend/Form/SubForm.php';
  36. require_once 'Zend/Loader/PluginLoader.php';
  37. require_once 'Zend/Registry.php';
  38. require_once 'Zend/Translate.php';
  39. require_once 'Zend/View.php';
  40. /**
  41. * @category Zend
  42. * @package Zend_Form
  43. * @subpackage UnitTests
  44. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. * @group Zend_Form
  47. */
  48. class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
  49. {
  50. public static function main()
  51. {
  52. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_FormTest');
  53. $result = PHPUnit_TextUI_TestRunner::run($suite);
  54. }
  55. public function clearRegistry()
  56. {
  57. if (Zend_Registry::isRegistered('Zend_Translate')) {
  58. $registry = Zend_Registry::getInstance();
  59. unset($registry['Zend_Translate']);
  60. }
  61. }
  62. public function setUp()
  63. {
  64. $this->clearRegistry();
  65. Zend_Form::setDefaultTranslator(null);
  66. if (isset($this->error)) {
  67. unset($this->error);
  68. }
  69. Zend_Controller_Action_HelperBroker::resetHelpers();
  70. $this->form = new Zend_Form();
  71. }
  72. public function tearDown()
  73. {
  74. $this->clearRegistry();
  75. }
  76. public function testZendFormImplementsZendValidateInterface()
  77. {
  78. $this->assertTrue($this->form instanceof Zend_Validate_Interface);
  79. }
  80. // Configuration
  81. public function getOptions()
  82. {
  83. $options = array(
  84. 'name' => 'foo',
  85. 'class' => 'someform',
  86. 'action' => '/foo/bar',
  87. 'method' => 'put',
  88. );
  89. return $options;
  90. }
  91. public function testCanSetObjectStateViaSetOptions()
  92. {
  93. $options = $this->getOptions();
  94. $this->form->setOptions($options);
  95. $this->assertEquals('foo', $this->form->getName());
  96. $this->assertEquals('someform', $this->form->getAttrib('class'));
  97. $this->assertEquals('/foo/bar', $this->form->getAction());
  98. $this->assertEquals('put', $this->form->getMethod());
  99. }
  100. public function testCanSetObjectStateByPassingOptionsToConstructor()
  101. {
  102. $options = $this->getOptions();
  103. $form = new Zend_Form($options);
  104. $this->assertEquals('foo', $form->getName());
  105. $this->assertEquals('someform', $form->getAttrib('class'));
  106. $this->assertEquals('/foo/bar', $form->getAction());
  107. $this->assertEquals('put', $form->getMethod());
  108. }
  109. public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
  110. {
  111. $options = $this->getOptions();
  112. $config = new Zend_Config($options);
  113. $options['config'] = $config;
  114. $options['options'] = $config->toArray();
  115. $this->form->setOptions($options);
  116. }
  117. public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNonObjectPassed()
  118. {
  119. $options = $this->getOptions();
  120. $options['pluginLoader'] = true;
  121. $options['subForms'] = true;
  122. $options['view'] = true;
  123. $options['translator'] = true;
  124. $options['default'] = true;
  125. $options['attrib'] = true;
  126. $this->form->setOptions($options);
  127. }
  128. public function testSetOptionsWithAttribsDoesNotOverwriteActionOrMethodOrName()
  129. {
  130. $attribs = $this->getOptions();
  131. unset($attribs['action'], $attribs['method']);
  132. $options = array(
  133. 'name' => 'MYFORM',
  134. 'action' => '/bar/baz',
  135. 'method' => 'GET',
  136. 'attribs' => $attribs,
  137. );
  138. $form = new Zend_Form($options);
  139. $this->assertEquals($options['name'], $form->getName());
  140. $this->assertEquals($options['action'], $form->getAction());
  141. $this->assertEquals(strtolower($options['method']), strtolower($form->getMethod()));
  142. }
  143. public function getElementOptions()
  144. {
  145. $elements = array(
  146. 'foo' => 'text',
  147. array('text', 'bar', array('class' => 'foobar')),
  148. array(
  149. 'options' => array('class' => 'barbaz'),
  150. 'type' => 'text',
  151. 'name' => 'baz',
  152. ),
  153. 'bat' => array(
  154. 'options' => array('class' => 'bazbat'),
  155. 'type' => 'text',
  156. ),
  157. 'lol' => array(
  158. 'text',
  159. array('class' => 'lolcat'),
  160. )
  161. );
  162. return $elements;
  163. }
  164. public function testSetOptionsSetsElements()
  165. {
  166. $options = $this->getOptions();
  167. $options['elements'] = $this->getElementOptions();
  168. $this->form->setOptions($options);
  169. $this->assertTrue(isset($this->form->foo));
  170. $this->assertTrue($this->form->foo instanceof Zend_Form_Element_Text);
  171. $this->assertTrue(isset($this->form->bar));
  172. $this->assertTrue($this->form->bar instanceof Zend_Form_Element_Text);
  173. $this->assertEquals('foobar', $this->form->bar->class);
  174. $this->assertTrue(isset($this->form->baz));
  175. $this->assertTrue($this->form->baz instanceof Zend_Form_Element_Text);
  176. $this->assertEquals('barbaz', $this->form->baz->class);
  177. $this->assertTrue(isset($this->form->bat));
  178. $this->assertTrue($this->form->bat instanceof Zend_Form_Element_Text);
  179. $this->assertEquals('bazbat', $this->form->bat->class);
  180. $this->assertTrue(isset($this->form->lol));
  181. $this->assertTrue($this->form->lol instanceof Zend_Form_Element_Text);
  182. $this->assertEquals('lolcat', $this->form->lol->class);
  183. }
  184. public function testSetOptionsSetsDefaultValues()
  185. {
  186. $options = $this->getOptions();
  187. $options['defaults'] = array(
  188. 'bar' => 'barvalue',
  189. 'bat' => 'batvalue',
  190. );
  191. $options['elements'] = $this->getElementOptions();
  192. $this->form->setOptions($options);
  193. $this->assertEquals('barvalue', $this->form->bar->getValue());
  194. $this->assertEquals('batvalue', $this->form->bat->getValue());
  195. }
  196. public function testSetOptionsSetsArrayOfStringDecorators()
  197. {
  198. $this->_checkZf2794();
  199. $options = $this->getOptions();
  200. $options['decorators'] = array('label', 'errors');
  201. $this->form->setOptions($options);
  202. $this->assertFalse($this->form->getDecorator('form'));
  203. $decorator = $this->form->getDecorator('label');
  204. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  205. $decorator = $this->form->getDecorator('errors');
  206. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  207. }
  208. public function testSetOptionsSetsArrayOfArrayDecorators()
  209. {
  210. $this->_checkZf2794();
  211. $options = $this->getOptions();
  212. $options['decorators'] = array(
  213. array('label', array('id' => 'mylabel')),
  214. array('errors', array('id' => 'errors')),
  215. );
  216. $this->form->setOptions($options);
  217. $this->assertFalse($this->form->getDecorator('form'));
  218. $decorator = $this->form->getDecorator('label');
  219. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  220. $options = $decorator->getOptions();
  221. $this->assertEquals('mylabel', $options['id']);
  222. $decorator = $this->form->getDecorator('errors');
  223. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  224. $options = $decorator->getOptions();
  225. $this->assertEquals('errors', $options['id']);
  226. }
  227. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  228. {
  229. $this->_checkZf2794();
  230. $options = $this->getOptions();
  231. $options['decorators'] = array(
  232. array(
  233. 'options' => array('id' => 'mylabel'),
  234. 'decorator' => 'label',
  235. ),
  236. array(
  237. 'options' => array('id' => 'errors'),
  238. 'decorator' => 'errors',
  239. ),
  240. );
  241. $this->form->setOptions($options);
  242. $this->assertFalse($this->form->getDecorator('form'));
  243. $decorator = $this->form->getDecorator('label');
  244. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  245. $options = $decorator->getOptions();
  246. $this->assertEquals('mylabel', $options['id']);
  247. $decorator = $this->form->getDecorator('errors');
  248. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  249. $options = $decorator->getOptions();
  250. $this->assertEquals('errors', $options['id']);
  251. }
  252. public function testSetOptionsSetsGlobalPrefixPaths()
  253. {
  254. $options = $this->getOptions();
  255. $options['prefixPath'] = array(
  256. 'prefix' => 'Zend_Foo',
  257. 'path' => 'Zend/Foo/'
  258. );
  259. $this->form->setOptions($options);
  260. foreach (array('element', 'decorator') as $type) {
  261. $loader = $this->form->getPluginLoader($type);
  262. $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
  263. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  264. $this->assertFalse(empty($paths));
  265. $this->assertContains('Foo', $paths[0]);
  266. }
  267. }
  268. public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
  269. {
  270. $options = $this->getOptions();
  271. $options['prefixPath'] = array(
  272. 'element' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  273. );
  274. $this->form->setOptions($options);
  275. $loader = $this->form->getPluginLoader('element');
  276. $paths = $loader->getPaths('Zend_Foo');
  277. $this->assertTrue(is_array($paths));
  278. $this->assertFalse(empty($paths));
  279. $this->assertContains('Foo', $paths[0]);
  280. }
  281. public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
  282. {
  283. $options = $this->getOptions();
  284. $options['prefixPath'] = array(
  285. array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  286. );
  287. $this->form->setOptions($options);
  288. $loader = $this->form->getPluginLoader('decorator');
  289. $paths = $loader->getPaths('Zend_Foo');
  290. $this->assertTrue(is_array($paths));
  291. $this->assertFalse(empty($paths));
  292. $this->assertContains('Foo', $paths[0]);
  293. }
  294. public function testSetOptionsSetsDisplayGroups()
  295. {
  296. $options = $this->getOptions();
  297. $options['displayGroups'] = array(
  298. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  299. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  300. array(
  301. 'name' => 'ghiabc',
  302. 'elements' => array('ghi', 'abc'),
  303. 'options' => array('order' => 15),
  304. ),
  305. );
  306. $options['elements'] = array(
  307. 'foo' => 'text',
  308. 'bar' => 'text',
  309. 'baz' => 'text',
  310. 'bat' => 'text',
  311. 'abc' => 'text',
  312. 'ghi' => 'text',
  313. 'jkl' => 'text',
  314. 'mno' => 'text',
  315. );
  316. $this->form->setOptions($options);
  317. $this->assertTrue(isset($this->form->barbat));
  318. $elements = $this->form->barbat->getElements();
  319. $expected = array('bar', 'bat');
  320. $this->assertEquals($expected, array_keys($elements));
  321. $this->assertEquals(20, $this->form->barbat->getOrder());
  322. $this->assertTrue(isset($this->form->foobaz));
  323. $elements = $this->form->foobaz->getElements();
  324. $expected = array('foo', 'baz');
  325. $this->assertEquals($expected, array_keys($elements));
  326. $this->assertEquals(10, $this->form->foobaz->getOrder());
  327. $this->assertTrue(isset($this->form->ghiabc));
  328. $elements = $this->form->ghiabc->getElements();
  329. $expected = array('ghi', 'abc');
  330. $this->assertEquals($expected, array_keys($elements));
  331. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  332. }
  333. /**
  334. * @group ZF-3250
  335. */
  336. public function testDisplayGroupOrderInConfigShouldNotMatter()
  337. {
  338. require_once 'Zend/Config/Xml.php';
  339. $config = new Zend_Config_Xml(dirname(__FILE__) . '/_files/config/zf3250.xml', 'sitearea', true);
  340. $form = new Zend_Form($config->test);
  341. // no assertions needed; throws error if order matters
  342. }
  343. /**
  344. * @group ZF-3112
  345. */
  346. public function testSetOptionsShouldCreateDisplayGroupsLast()
  347. {
  348. $options = array();
  349. $options['displayGroups'] = array(
  350. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  351. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  352. array(
  353. 'name' => 'ghiabc',
  354. 'elements' => array('ghi', 'abc'),
  355. 'options' => array('order' => 15),
  356. ),
  357. );
  358. $options = array_merge($options, $this->getOptions());
  359. $options['elements'] = array(
  360. 'foo' => 'text',
  361. 'bar' => 'text',
  362. 'baz' => 'text',
  363. 'bat' => 'text',
  364. 'abc' => 'text',
  365. 'ghi' => 'text',
  366. 'jkl' => 'text',
  367. 'mno' => 'text',
  368. );
  369. $this->form = new Zend_Form($options);
  370. $this->assertTrue(isset($this->form->barbat));
  371. $elements = $this->form->barbat->getElements();
  372. $expected = array('bar', 'bat');
  373. $this->assertEquals($expected, array_keys($elements));
  374. $this->assertEquals(20, $this->form->barbat->getOrder());
  375. $this->assertTrue(isset($this->form->foobaz));
  376. $elements = $this->form->foobaz->getElements();
  377. $expected = array('foo', 'baz');
  378. $this->assertEquals($expected, array_keys($elements));
  379. $this->assertEquals(10, $this->form->foobaz->getOrder());
  380. $this->assertTrue(isset($this->form->ghiabc));
  381. $elements = $this->form->ghiabc->getElements();
  382. $expected = array('ghi', 'abc');
  383. $this->assertEquals($expected, array_keys($elements));
  384. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  385. }
  386. public function testSetConfigSetsObjectState()
  387. {
  388. $config = new Zend_Config($this->getOptions());
  389. $this->form->setConfig($config);
  390. $this->assertEquals('foo', $this->form->getName());
  391. $this->assertEquals('someform', $this->form->getAttrib('class'));
  392. $this->assertEquals('/foo/bar', $this->form->getAction());
  393. $this->assertEquals('put', $this->form->getMethod());
  394. }
  395. public function testCanSetObjectStateByPassingConfigObjectToConstructor()
  396. {
  397. $config = new Zend_Config($this->getOptions());
  398. $form = new Zend_Form($config);
  399. $this->assertEquals('foo', $form->getName());
  400. $this->assertEquals('someform', $form->getAttrib('class'));
  401. $this->assertEquals('/foo/bar', $form->getAction());
  402. $this->assertEquals('put', $form->getMethod());
  403. }
  404. // Attribs:
  405. public function testAttribsArrayInitiallyEmpty()
  406. {
  407. $attribs = $this->form->getAttribs();
  408. $this->assertTrue(is_array($attribs));
  409. $this->assertTrue(empty($attribs));
  410. }
  411. public function testRetrievingUndefinedAttribReturnsNull()
  412. {
  413. $this->assertNull($this->form->getAttrib('foo'));
  414. }
  415. public function testCanAddAndRetrieveSingleAttribs()
  416. {
  417. $this->testRetrievingUndefinedAttribReturnsNull();
  418. $this->form->setAttrib('foo', 'bar');
  419. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  420. }
  421. public function testCanAddAndRetrieveMultipleAttribs()
  422. {
  423. $this->form->setAttrib('foo', 'bar');
  424. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  425. $this->form->addAttribs(array(
  426. 'bar' => 'baz',
  427. 'baz' => 'bat',
  428. 'bat' => 'foo'
  429. ));
  430. $test = $this->form->getAttribs();
  431. $attribs = array(
  432. 'foo' => 'bar',
  433. 'bar' => 'baz',
  434. 'baz' => 'bat',
  435. 'bat' => 'foo'
  436. );
  437. $this->assertSame($attribs, $test);
  438. }
  439. public function testSetAttribsOverwritesExistingAttribs()
  440. {
  441. $this->testCanAddAndRetrieveMultipleAttribs();
  442. $array = array('bogus' => 'value', 'not' => 'real');
  443. $this->form->setAttribs($array);
  444. $this->assertSame($array, $this->form->getAttribs());
  445. }
  446. public function testCanRemoveSingleAttrib()
  447. {
  448. $this->testCanAddAndRetrieveSingleAttribs();
  449. $this->assertTrue($this->form->removeAttrib('foo'));
  450. $this->assertNull($this->form->getAttrib('foo'));
  451. }
  452. public function testRemoveAttribReturnsFalseIfAttribDoesNotExist()
  453. {
  454. $this->assertFalse($this->form->removeAttrib('foo'));
  455. }
  456. public function testCanClearAllAttribs()
  457. {
  458. $this->testCanAddAndRetrieveMultipleAttribs();
  459. $this->form->clearAttribs();
  460. $attribs = $this->form->getAttribs();
  461. $this->assertTrue(is_array($attribs));
  462. $this->assertTrue(empty($attribs));
  463. }
  464. public function testNameIsInitiallyNull()
  465. {
  466. $this->assertNull($this->form->getName());
  467. }
  468. public function testCanSetName()
  469. {
  470. $this->testNameIsInitiallyNull();
  471. $this->form->setName('foo');
  472. $this->assertEquals('foo', $this->form->getName());
  473. }
  474. public function testZeroAsNameIsAllowed()
  475. {
  476. try {
  477. $this->form->setName(0);
  478. $this->assertEquals(0, $this->form->getName());
  479. } catch (Zend_Form_Exception $e) {
  480. $this->fail('Should allow zero as form name');
  481. }
  482. }
  483. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  484. {
  485. $this->form->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  486. $this->assertEquals('foobar', $this->form->getName());
  487. try {
  488. $this->form->setName('%\^&*)\(%$#@!.}{;-,');
  489. $this->fail('Empty names should raise exception');
  490. } catch (Zend_Form_Exception $e) {
  491. $this->assertContains('Invalid name provided', $e->getMessage());
  492. }
  493. }
  494. public function testActionDefaultsToEmptyString()
  495. {
  496. $this->assertSame('', $this->form->getAction());
  497. }
  498. public function testCanSetAction()
  499. {
  500. $this->testActionDefaultsToEmptyString();
  501. $this->form->setAction('/foo/bar');
  502. $this->assertEquals('/foo/bar', $this->form->getAction());
  503. }
  504. /**
  505. * @issue ZF-7067
  506. */
  507. public function testCanSetActionWithGetParams()
  508. {
  509. $this->testActionDefaultsToEmptyString();
  510. $this->form->setAction('/foo.php?bar')
  511. ->setView(new Zend_View);
  512. $html = $this->form->render();
  513. $this->assertContains('action="/foo.php?bar"', $html);
  514. $this->assertEquals('/foo.php?bar', $this->form->getAction());
  515. }
  516. public function testMethodDefaultsToPost()
  517. {
  518. $this->assertEquals('post', $this->form->getMethod());
  519. }
  520. public function testCanSetMethod()
  521. {
  522. $this->testMethodDefaultsToPost();
  523. $this->form->setMethod('get');
  524. $this->assertEquals('get', $this->form->getMethod());
  525. }
  526. public function testMethodLimitedToGetPostPutAndDelete()
  527. {
  528. foreach (array('get', 'post', 'put', 'delete') as $method) {
  529. $this->form->setMethod($method);
  530. $this->assertEquals($method, $this->form->getMethod());
  531. }
  532. try {
  533. $this->form->setMethod('bogus');
  534. $this->fail('Invalid method type should throw exception');
  535. } catch (Zend_Form_Exception $e) {
  536. $this->assertContains('invalid', $e->getMessage());
  537. }
  538. }
  539. public function testEnctypeDefaultsToUrlEncoded()
  540. {
  541. $this->assertEquals(Zend_Form::ENCTYPE_URLENCODED, $this->form->getEnctype());
  542. }
  543. public function testCanSetEnctype()
  544. {
  545. $this->testEnctypeDefaultsToUrlEncoded();
  546. $this->form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  547. $this->assertEquals(Zend_Form::ENCTYPE_MULTIPART, $this->form->getEnctype());
  548. }
  549. public function testLegendInitiallyNull()
  550. {
  551. $this->assertNull($this->form->getLegend());
  552. }
  553. public function testCanSetLegend()
  554. {
  555. $this->testLegendInitiallyNull();
  556. $legend = "This is a legend";
  557. $this->form->setLegend($legend);
  558. $this->assertEquals($legend, $this->form->getLegend());
  559. }
  560. public function testDescriptionInitiallyNull()
  561. {
  562. $this->assertNull($this->form->getDescription());
  563. }
  564. public function testCanSetDescription()
  565. {
  566. $this->testDescriptionInitiallyNull();
  567. $description = "This is a description";
  568. $this->form->setDescription($description);
  569. $this->assertEquals($description, $this->form->getDescription());
  570. }
  571. // Plugin loaders
  572. public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
  573. {
  574. $loader = $this->form->getPluginLoader('decorator');
  575. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  576. $paths = $loader->getPaths('Zend_Form_Decorator');
  577. $this->assertTrue(is_array($paths), var_export($loader, 1));
  578. $this->assertTrue(0 < count($paths));
  579. $this->assertContains('Form', $paths[0]);
  580. $this->assertContains('Decorator', $paths[0]);
  581. }
  582. public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
  583. {
  584. $loader = new Zend_Loader_PluginLoader();
  585. try {
  586. $this->form->setPluginLoader($loader, 'foo');
  587. $this->fail('Invalid plugin loader type should raise exception');
  588. } catch (Zend_Form_Exception $e) {
  589. $this->assertContains('Invalid type', $e->getMessage());
  590. }
  591. }
  592. public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
  593. {
  594. try {
  595. $this->form->getPluginLoader('foo');
  596. $this->fail('Invalid plugin loader type should raise exception');
  597. } catch (Zend_Form_Exception $e) {
  598. $this->assertContains('Invalid type', $e->getMessage());
  599. }
  600. }
  601. public function testCanSetCustomDecoratorPluginLoader()
  602. {
  603. $loader = new Zend_Loader_PluginLoader();
  604. $this->form->setPluginLoader($loader, 'decorator');
  605. $test = $this->form->getPluginLoader('decorator');
  606. $this->assertSame($loader, $test);
  607. }
  608. public function testPassingInvalidTypeToAddPrefixPathThrowsException()
  609. {
  610. try {
  611. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
  612. $this->fail('Passing invalid loader type to addPrefixPath() should raise exception');
  613. } catch (Zend_Form_Exception $e) {
  614. $this->assertContains('Invalid type', $e->getMessage());
  615. }
  616. }
  617. public function testCanAddDecoratorPluginLoaderPrefixPath()
  618. {
  619. $loader = $this->form->getPluginLoader('decorator');
  620. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  621. $paths = $loader->getPaths('Zend_Foo');
  622. $this->assertTrue(is_array($paths));
  623. $this->assertContains('Foo', $paths[0]);
  624. }
  625. public function testUpdatedDecoratorPrefixPathUsedForNewElements()
  626. {
  627. $loader = $this->form->getPluginLoader('decorator');
  628. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  629. $foo = new Zend_Form_Element_Text('foo');
  630. $this->form->addElement($foo);
  631. $loader = $foo->getPluginLoader('decorator');
  632. $paths = $loader->getPaths('Zend_Foo');
  633. $this->assertTrue(is_array($paths));
  634. $this->assertContains('Foo', $paths[0]);
  635. $this->form->addElement('text', 'bar');
  636. $bar = $this->form->bar;
  637. $loader = $bar->getPluginLoader('decorator');
  638. $paths = $loader->getPaths('Zend_Foo');
  639. $this->assertTrue(is_array($paths));
  640. $this->assertContains('Foo', $paths[0]);
  641. }
  642. public function testUpdatedDecoratorPrefixPathUsedForNewDisplayGroups()
  643. {
  644. $loader = $this->form->getPluginLoader('decorator');
  645. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  646. $this->setupElements();
  647. $foo = $this->form->foo;
  648. $loader = $foo->getPluginLoader('decorator');
  649. $paths = $loader->getPaths('Zend_Foo');
  650. $this->assertTrue(is_array($paths));
  651. $this->assertContains('Foo', $paths[0]);
  652. }
  653. public function testUpdatedPrefixPathUsedForNewSubForms()
  654. {
  655. $loader = $this->form->getPluginLoader('decorator');
  656. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  657. $this->setupSubForm();
  658. $loader = $this->form->sub->getPluginLoader('decorator');
  659. $paths = $loader->getPaths('Zend_Foo');
  660. $this->assertTrue(is_array($paths));
  661. $this->assertContains('Foo', $paths[0]);
  662. }
  663. public function testGetPluginLoaderRetrievesDefaultElementPluginLoader()
  664. {
  665. $loader = $this->form->getPluginLoader('element');
  666. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  667. $paths = $loader->getPaths('Zend_Form_Element');
  668. $this->assertTrue(is_array($paths), var_export($loader, 1));
  669. $this->assertTrue(0 < count($paths));
  670. $this->assertContains('Form', $paths[0]);
  671. $this->assertContains('Element', $paths[0]);
  672. }
  673. public function testCanSetCustomDecoratorElementLoader()
  674. {
  675. $loader = new Zend_Loader_PluginLoader();
  676. $this->form->setPluginLoader($loader, 'element');
  677. $test = $this->form->getPluginLoader('element');
  678. $this->assertSame($loader, $test);
  679. }
  680. public function testCanAddElementPluginLoaderPrefixPath()
  681. {
  682. $loader = $this->form->getPluginLoader('element');
  683. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'element');
  684. $paths = $loader->getPaths('Zend_Foo');
  685. $this->assertTrue(is_array($paths));
  686. $this->assertContains('Foo', $paths[0]);
  687. }
  688. public function testAddAllPluginLoaderPrefixPathsSimultaneously()
  689. {
  690. $decoratorLoader = new Zend_Loader_PluginLoader();
  691. $elementLoader = new Zend_Loader_PluginLoader();
  692. $this->form->setPluginLoader($decoratorLoader, 'decorator')
  693. ->setPluginLoader($elementLoader, 'element')
  694. ->addPrefixPath('Zend', 'Zend/');
  695. $paths = $decoratorLoader->getPaths('Zend_Decorator');
  696. $this->assertTrue(is_array($paths), var_export($paths, 1));
  697. $this->assertContains('Decorator', $paths[0]);
  698. $paths = $elementLoader->getPaths('Zend_Element');
  699. $this->assertTrue(is_array($paths), var_export($paths, 1));
  700. $this->assertContains('Element', $paths[0]);
  701. }
  702. // Elements:
  703. public function testCanAddAndRetrieveSingleElements()
  704. {
  705. $element = new Zend_Form_Element('foo');
  706. $this->form->addElement($element);
  707. $this->assertSame($element, $this->form->getElement('foo'));
  708. }
  709. public function testGetElementReturnsNullForUnregisteredElement()
  710. {
  711. $this->assertNull($this->form->getElement('foo'));
  712. }
  713. public function testCanAddAndRetrieveSingleElementsByStringType()
  714. {
  715. $this->form->addElement('text', 'foo');
  716. $element = $this->form->getElement('foo');
  717. $this->assertTrue($element instanceof Zend_Form_Element);
  718. $this->assertTrue($element instanceof Zend_Form_Element_Text);
  719. $this->assertEquals('foo', $element->getName());
  720. }
  721. public function testAddElementAsStringElementThrowsExceptionWhenNoNameProvided()
  722. {
  723. try {
  724. $this->form->addElement('text');
  725. $this->fail('Should not be able to specify string element type without name');
  726. } catch (Zend_Form_Exception $e) {
  727. $this->assertContains('must have', $e->getMessage());
  728. }
  729. }
  730. public function testCreateElementReturnsNewElement()
  731. {
  732. $element = $this->form->createElement('text', 'foo');
  733. $this->assertTrue($element instanceof Zend_Form_Element);
  734. }
  735. public function testCreateElementDoesNotAttachElementToForm()
  736. {
  737. $element = $this->form->createElement('text', 'foo');
  738. $this->assertTrue($element instanceof Zend_Form_Element);
  739. $this->assertNull($this->form->foo);
  740. }
  741. public function testCanAddAndRetrieveMultipleElements()
  742. {
  743. $this->form->addElements(array(
  744. 'foo' => 'text',
  745. array('text', 'bar'),
  746. array('text', 'baz', array('foo' => 'bar')),
  747. new Zend_Form_Element_Text('bat'),
  748. ));
  749. $elements = $this->form->getElements();
  750. $names = array('foo', 'bar', 'baz', 'bat');
  751. $this->assertEquals($names, array_keys($elements));
  752. $foo = $elements['foo'];
  753. $this->assertTrue($foo instanceof Zend_Form_Element_Text);
  754. $bar = $elements['bar'];
  755. $this->assertTrue($bar instanceof Zend_Form_Element_Text);
  756. $baz = $elements['baz'];
  757. $this->assertTrue($baz instanceof Zend_Form_Element_Text);
  758. $this->assertEquals('bar', $baz->foo, var_export($baz->getAttribs(), 1));
  759. $bat = $elements['bat'];
  760. $this->assertTrue($bat instanceof Zend_Form_Element_Text);
  761. }
  762. public function testSetElementsOverwritesExistingElements()
  763. {
  764. $this->testCanAddAndRetrieveMultipleElements();
  765. $this->form->setElements(array(
  766. 'bogus' => 'text'
  767. ));
  768. $elements = $this->form->getElements();
  769. $names = array('bogus');
  770. $this->assertEquals($names, array_keys($elements));
  771. }
  772. public function testCanRemoveSingleElement()
  773. {
  774. $this->testCanAddAndRetrieveMultipleElements();
  775. $this->assertTrue($this->form->removeElement('bar'));
  776. $this->assertNull($this->form->getElement('bar'));
  777. }
  778. public function testRemoveElementReturnsFalseWhenElementNotRegistered()
  779. {
  780. $this->assertFalse($this->form->removeElement('bogus'));
  781. }
  782. public function testCanClearAllElements()
  783. {
  784. $this->testCanAddAndRetrieveMultipleElements();
  785. $this->form->clearElements();
  786. $elements = $this->form->getElements();
  787. $this->assertTrue(is_array($elements));
  788. $this->assertTrue(empty($elements));
  789. }
  790. public function testGetValueReturnsNullForUndefinedElements()
  791. {
  792. $this->assertNull($this->form->getValue('foo'));
  793. }
  794. public function testCanSetElementDefaultValues()
  795. {
  796. $this->testCanAddAndRetrieveMultipleElements();
  797. $values = array(
  798. 'foo' => 'foovalue',
  799. 'bar' => 'barvalue',
  800. 'baz' => 'bazvalue',
  801. 'bat' => 'batvalue'
  802. );
  803. $this->form->setDefaults($values);
  804. $elements = $this->form->getElements();
  805. foreach (array_keys($values) as $name) {
  806. $this->assertEquals($name . 'value', $elements[$name]->getValue(), var_export($elements[$name], 1));
  807. }
  808. }
  809. public function testSettingElementDefaultsDoesNotSetElementValuesToNullIfNotInDefaultsArray()
  810. {
  811. $this->testCanAddAndRetrieveMultipleElements();
  812. $this->form->baz->setValue('testing');
  813. $this->form->bar->setValue('testing');
  814. $values = array(
  815. 'foo' => 'foovalue',
  816. 'bat' => 'batvalue'
  817. );
  818. $this->form->setDefaults($values);
  819. $this->assertEquals('foovalue', $this->form->foo->getValue());
  820. $this->assertEquals('batvalue', $this->form->bat->getValue());
  821. $this->assertNotNull($this->form->baz->getValue());
  822. $this->assertNotNull($this->form->bar->getValue());
  823. }
  824. public function testCanRetrieveSingleElementValue()
  825. {
  826. $this->form->addElement('text', 'foo', array('value' => 'foovalue'));
  827. $this->assertEquals('foovalue', $this->form->getValue('foo'));
  828. }
  829. public function testCanRetrieveAllElementValues()
  830. {
  831. $this->testCanAddAndRetrieveMultipleElements();
  832. $values = array(
  833. 'foo' => 'foovalue',
  834. 'bar' => 'barvalue',
  835. 'baz' => 'bazvalue',
  836. 'bat' => 'batvalue'
  837. );
  838. $this->form->setDefaults($values);
  839. $test = $this->form->getValues();
  840. $elements = $this->form->getElements();
  841. foreach (array_keys($values) as $name) {
  842. $this->assertEquals($values[$name], $test[$name]);
  843. }
  844. }
  845. public function testRetrievingAllElementValuesSkipsThoseFlaggedAsIgnore()
  846. {
  847. $this->form->addElements(array(
  848. 'foo' => 'text',
  849. 'bar' => 'text',
  850. 'baz' => 'text'
  851. ));
  852. $this->form->setDefaults(array(
  853. 'foo' => 'Foo Value',
  854. 'bar' => 'Bar Value',
  855. 'baz' => 'Baz Value',
  856. ));
  857. $this->form->bar->setIgnore(true);
  858. $test = $this->form->getValues();
  859. $this->assertFalse(array_key_exists('bar', $test));
  860. $this->assertTrue(array_key_exists('foo', $test));
  861. $this->assertTrue(array_key_exists('baz', $test));
  862. }
  863. public function testCanRetrieveSingleUnfilteredElementValue()
  864. {
  865. $foo = new Zend_Form_Element_Text('foo');
  866. $foo->addFilter('StringToUpper')
  867. ->setValue('foovalue');
  868. $this->form->addElement($foo);
  869. $this->assertEquals('FOOVALUE', $this->form->getValue('foo'));
  870. $this->assertEquals('foovalue', $this->form->getUnfilteredValue('foo'));
  871. }
  872. public function testCanRetrieveAllUnfilteredElementValues()
  873. {
  874. $foo = new Zend_Form_Element_Text('foo');
  875. $foo->addFilter('StringToUpper')
  876. ->setValue('foovalue');
  877. $bar = new Zend_Form_Element_Text('bar');
  878. $bar->addFilter('StringToUpper')
  879. ->setValue('barvalue');
  880. $this->form->addElements(array($foo, $bar));
  881. $values = $this->form->getValues();
  882. $unfiltered = $this->form->getUnfilteredValues();
  883. foreach (array('foo', 'bar') as $key) {
  884. $value = $key . 'value';
  885. $this->assertEquals(strtoupper($value), $values[$key]);
  886. $this->assertEquals($value, $unfiltered[$key]);
  887. }
  888. }
  889. public function testOverloadingElements()
  890. {
  891. $this->form->addElement('text', 'foo');
  892. $this->assertTrue(isset($this->form->foo));
  893. $element = $this->form->foo;
  894. $this->assertTrue($element instanceof Zend_Form_Element);
  895. unset($this->form->foo);
  896. $this->assertFalse(isset($this->form->foo));
  897. $bar = new Zend_Form_Element_Text('bar');
  898. $this->form->bar = $bar;
  899. $this->assertTrue(isset($this->form->bar));
  900. $element = $this->form->bar;
  901. $this->assertSame($bar, $element);
  902. }
  903. public function testOverloadingGetReturnsNullForUndefinedFormItems()
  904. {
  905. $this->assertNull($this->form->bogus);
  906. }
  907. public function testOverloadingSetThrowsExceptionForInvalidTypes()
  908. {
  909. try {
  910. $this->form->foo = true;
  911. $this->fail('Overloading should not allow scalars');
  912. } catch (Zend_Form_Exception $e) {
  913. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  914. }
  915. try {
  916. $this->form->foo = new Zend_Config(array());
  917. $this->fail('Overloading should not allow arbitrary object types');
  918. } catch (Zend_Form_Exception $e) {
  919. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  920. $this->assertContains('Zend_Config', $e->getMessage());
  921. }
  922. }
  923. public function testFormIsNotAnArrayByDefault()
  924. {
  925. $this->assertFalse($this->form->isArray());
  926. }
  927. public function testCanSetArrayFlag()
  928. {
  929. $this->testFormIsNotAnArrayByDefault();
  930. $this->form->setIsArray(true);
  931. $this->assertTrue($this->form->isArray());
  932. $this->form->setIsArray(false);
  933. $this->assertFalse($this->form->isArray());
  934. }
  935. public function testElementsBelongToReturnsFormNameWhenFormIsArray()
  936. {
  937. $this->form->setName('foo')
  938. ->setIsArray(true);
  939. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  940. }
  941. public function testElementsInitiallyBelongToNoArrays()
  942. {
  943. $this->assertNull($this->form->getElementsBelongTo());
  944. }
  945. public function testCanSetArrayToWhichElementsBelong()
  946. {
  947. $this->testElementsInitiallyBelongToNoArrays();
  948. $this->form->setElementsBelongTo('foo');
  949. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  950. }
  951. public function testSettingArrayToWhichElementsBelongSetsArrayFlag()
  952. {
  953. $this->testFormIsNotAnArrayByDefault();
  954. $this->testCanSetArrayToWhichElementsBelong();
  955. $this->assertTrue($this->form->isArray());
  956. }
  957. public function testArrayToWhichElementsBelongCanConsistOfValidVariableCharsOnly()
  958. {
  959. $this->testElementsInitiallyBelongToNoArrays();
  960. $this->form->setElementsBelongTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
  961. $this->assertEquals('foobar', $this->form->getElementsBelongTo());
  962. }
  963. public function testSettingArrayToWhichElementsBelongEmptyClearsIt()
  964. {
  965. $this->testCanSetArrayToWhichElementsBelong();
  966. $this->form->setElementsBelongTo('');
  967. $this->assertNull($this->form->getElementsBelongTo());
  968. }
  969. public function testSettingArrayToWhichElementsBelongEmptySetsArrayFlagToFalse()
  970. {
  971. $this->testSettingArrayToWhichElementsBelongEmptyClearsIt();
  972. $this->assertFalse($this->form->isArray());
  973. }
  974. /**
  975. * @group ZF-3146
  976. */
  977. public function testSetElementsBelongToShouldApplyToBothExistingAndFutureElements()
  978. {
  979. $this->form->addElement('text', 'testBelongsTo');
  980. $this->form->setElementsBelongTo('foo');
  981. $this->assertEquals('foo', $this->form->testBelongsTo->getBelongsTo(), 'Failed determining testBelongsTo belongs to array');
  982. $this->setupElements();
  983. foreach ($this->form->getElements() as $element) {
  984. $message = sprintf('Failed determining element "%s" belongs to foo', $element->getName());
  985. $this->assertEquals('foo', $element->getBelongsTo(), $message);
  986. }
  987. }
  988. /**
  989. * @group ZF-3742
  990. */
  991. public function testElementsInDisplayGroupsShouldInheritFormElementsBelongToSetting()
  992. {
  993. $subForm = new Zend_Form_SubForm();
  994. $subForm->addElements(array(
  995. new Zend_Form_Element_Text('foo'),
  996. new Zend_Form_Element_Text('bar'),
  997. new Zend_Form_Element_Text('baz'),
  998. new Zend_Form_Element_Text('bat'),
  999. ))
  1000. ->addDisplayGroup(array('bar', 'baz'), 'barbaz');
  1001. $this->form->addSubForm($subForm, 'sub')
  1002. ->setElementsBelongTo('myform')
  1003. ->setView(new Zend_View);
  1004. $html = $this->form->render();
  1005. foreach (array('foo', 'bar', 'baz', 'bat') as $test) {
  1006. $this->assertContains('id="myform-sub-' . $test . '"', $html);
  1007. $this->assertContains('name="myform[sub][' . $test . ']"', $html);
  1008. }
  1009. }
  1010. public function testIsValidWithOneLevelElementsBelongTo()
  1011. {
  1012. $this->form->addElement('text', 'test')->test
  1013. ->addValidator('Identical', false, array('Test Value'));
  1014. $this->form->setElementsBelongTo('foo');
  1015. $data = array(
  1016. 'foo' => array(
  1017. 'test' => 'Test Value',
  1018. ),
  1019. );
  1020. $this->assertTrue($this->form->isValid($data));
  1021. }
  1022. public function testIsValidWithMultiLevelElementsBelongTo()
  1023. {
  1024. $this->form->addElement('text', 'test')->test
  1025. ->addValidator('Identical', false, array('Test Value'));
  1026. $this->form->setElementsBelongTo('foo[bar][zot]');
  1027. $data = array(
  1028. 'foo' => array(
  1029. 'bar' => array(
  1030. 'zot' => array(
  1031. 'test' => 'Test Value',
  1032. ),
  1033. ),
  1034. ),
  1035. );
  1036. $this->assertTrue($this->form->isValid($data));
  1037. }
  1038. // Sub forms
  1039. public function testCanAddAndRetrieveSingleSubForm()
  1040. {
  1041. $subForm = new Zend_Form_SubForm;
  1042. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1043. $this->form->addSubForm($subForm, 'page1');
  1044. $test = $this->form->getSubForm('page1');
  1045. $this->assertSame($subForm, $test);
  1046. }
  1047. public function testAddingSubFormSetsSubFormName()
  1048. {
  1049. $subForm = new Zend_Form_SubForm;
  1050. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1051. $this->form->addSubForm($subForm, 'page1');
  1052. $this->assertEquals('page1', $subForm->getName());
  1053. }
  1054. public function testGetSubFormReturnsNullForUnregisteredSubForm()
  1055. {
  1056. $this->assertNull($this->form->getSubForm('foo'));
  1057. }
  1058. public function testCanAddAndRetrieveMultipleSubForms()
  1059. {
  1060. $page1 = new Zend_Form_SubForm();
  1061. $page2 = new Zend_Form_SubForm();
  1062. $page3 = new Zend_Form_SubForm();
  1063. $this->form->addSubForms(array(
  1064. 'page1' => $page1,
  1065. array($page2, 'page2'),
  1066. array($page3, 'page3', 3)
  1067. ));
  1068. $subforms = $this->form->getSubForms();
  1069. $keys = array('page1', 'page2', 'page3');
  1070. $this->assertEquals($keys, array_keys($subforms));
  1071. $this->assertSame($page1, $subforms['page1']);
  1072. $this->assertSame($page2, $subforms['page2']);
  1073. $this->assertSame($page3, $subforms['page3']);
  1074. }
  1075. public function testSetSubFormsOverwritesExistingSubForms()
  1076. {
  1077. $this->testCanAddAndRetrieveMultipleSubForms();
  1078. $foo = new Zend_Form_SubForm();
  1079. $this->form->setSubForms(array('foo' => $foo));
  1080. $subforms = $this->form->getSubForms();
  1081. $keys = array('foo');
  1082. $this->assertEquals($keys, array_keys($subforms));
  1083. $this->assertSame($foo, $subforms['foo']);
  1084. }
  1085. public function testCanRemoveSingleSubForm()
  1086. {
  1087. $this->testCanAddAndRetrieveMultipleSubForms();
  1088. $this->assertTrue($this->form->removeSubForm('page2'));
  1089. $this->assertNull($this->form->getSubForm('page2'));
  1090. }
  1091. public function testRemoveSubFormReturnsFalseForNonexistantSubForm()
  1092. {
  1093. $this->assertFalse($this->form->removeSubForm('foo'));
  1094. }
  1095. public function testCanClearAllSubForms()
  1096. {
  1097. $this->testCanAddAndRetrieveMultipleSubForms();
  1098. $this->form->clearSubForms();
  1099. $subforms = $this->form->getSubForms();
  1100. $this->assertTrue(is_array($subforms));
  1101. $this->assertTrue(empty($subforms));
  1102. }
  1103. public function testOverloadingSubForms()
  1104. {
  1105. $foo = new Zend_Form_SubForm;
  1106. $this->form->addSubForm($foo, 'foo');
  1107. $this->assertTrue(isset($this->form->foo));
  1108. $subform = $this->form->foo;
  1109. $this->assertSame($foo, $subform);
  1110. unset($this->form->foo);
  1111. $this->assertFalse(isset($this->form->foo));
  1112. $bar = new Zend_Form_SubForm();
  1113. $this->form->bar = $bar;
  1114. $this->assertTrue(isset($this->form->bar));
  1115. $subform = $this->form->bar;
  1116. $this->assertSame($bar, $subform);
  1117. }
  1118. public function testCanSetDefaultsForSubFormElementsFromForm()
  1119. {
  1120. $subForm = new Zend_Form_SubForm;
  1121. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1122. $this->form->addSubForm($subForm, 'page1');
  1123. $data = array('foo' => 'foo value', 'bar' => 'bar value');
  1124. $this->form->setDefaults($data);
  1125. $this->assertEquals($data['foo'], $subForm->foo->getValue());
  1126. $this->assertEquals($data['bar'], $subForm->bar->getValue());
  1127. }
  1128. public function testCanSetDefaultsForSubFormElementsFromFormWithArray()
  1129. {
  1130. $subForm = new Zend_Form_SubForm;
  1131. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1132. $this->form->addSubForm($subForm, 'page1');
  1133. $data = array( 'page1' => array(
  1134. 'foo' => 'foo value',
  1135. 'bar' => 'bar value'
  1136. ));
  1137. $this->form->setDefaults($data);
  1138. $this->assertEquals($data['page1']['foo'], $subForm->foo->getValue());
  1139. $this->assertEquals($data['page1']['bar'], $subForm->bar->getValue());
  1140. }
  1141. public function testGetValuesReturnsSubFormValues()
  1142. {
  1143. $subForm = new Zend_Form_SubForm;
  1144. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1145. $subForm->foo->setValue('foo value');
  1146. $subForm->bar->setValue('bar value');
  1147. $this->form->addSubForm($subForm, 'page1');
  1148. $values = $this->form->getValues();
  1149. $this->assertTrue(isset($values['page1']));
  1150. $this->assertTrue(isset($values['page1']['foo']));
  1151. $this->assertTrue(isset($values['page1']['bar']));
  1152. $this->assertEquals($subForm->foo->getValue(), $values['page1']['foo']);
  1153. $this->assertEquals($subForm->bar->getValue(), $values['page1']['bar']);
  1154. }
  1155. public function testGetValuesReturnsSubFormValuesFromArrayToWhichElementsBelong()
  1156. {
  1157. $subForm = new Zend_Form_SubForm;
  1158. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1159. ->setElementsBelongTo('subform');
  1160. $subForm->foo->setValue('foo value');
  1161. $subForm->bar->setValue('bar value');
  1162. $this->form->addSubForm($subForm, 'page1');
  1163. $values = $this->form->getValues();
  1164. $this->assertTrue(isset($values['subform']), var_export($values, 1));
  1165. $this->assertTrue(isset($values['subform']['foo']));
  1166. $this->assertTrue(isset($values['subform']['bar']));
  1167. $this->assertEquals($subForm->foo->getValue(), $values['subform']['foo']);
  1168. $this->assertEquals($subForm->bar->getValue(), $values['subform']['bar']);
  1169. }
  1170. public function testGetValuesReturnsNestedSubFormValuesFromArraysToWhichElementsBelong()
  1171. {
  1172. $form = new Zend_Form();
  1173. $form->setElementsBelongTo('foobar');
  1174. $form->addElement('text', 'firstName')
  1175. ->getElement('firstName')
  1176. ->setRequired(true);
  1177. $form->addElement('text', 'lastName')
  1178. ->getElement('lastName')
  1179. ->setRequired(true);
  1180. $subForm = new Zend_Form_SubForm();
  1181. $subForm->setElementsBelongTo('baz[quux]');
  1182. $subForm->addElement('text', 'email')
  1183. ->getElement('email')->setRequired(true);
  1184. $subSubForm = new Zend_Form_SubForm();
  1185. $subSubForm->setElementsBelongTo('bat');
  1186. $subSubForm->addElement('checkbox', 'home')
  1187. ->getElement('home')->setRequired(true);
  1188. $subForm->addSubForm($subSubForm, 'subSub');
  1189. $form->addSubForm($subForm, 'sub')
  1190. ->addElement('submit', 'save', array('value' => 'submit', 'ignore' => true));
  1191. $data = array('foobar' => array(
  1192. 'firstName' => 'Mabel',
  1193. 'lastName' => 'Cow',
  1194. 'baz' => array(
  1195. 'quux' => array(
  1196. 'email' => 'mabel@cow.org',
  1197. 'bat' => array(
  1198. 'home' => 1,
  1199. )
  1200. ),
  1201. )
  1202. ));
  1203. $this->assertTrue($form->isValid($data));
  1204. $values = $form->getValues();
  1205. $this->assertEquals($data, $values);
  1206. }
  1207. public function testGetValueCanReturnSubFormValues()
  1208. {
  1209. $subForm = new Zend_Form_SubForm;
  1210. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1211. $subForm->foo->setValue('foo value');
  1212. $subForm->bar->setValue('bar value');
  1213. $this->form->addSubForm($subForm, 'page1');
  1214. $values = $this->form->getValue('page1');
  1215. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1216. $this->assertTrue(isset($values['bar']));
  1217. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1218. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1219. }
  1220. public function testGetValueCanReturnSubFormValuesFromArrayToWhichElementsBelong()
  1221. {
  1222. $subForm = new Zend_Form_SubForm;
  1223. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1224. ->setElementsBelongTo('subform');
  1225. $subForm->foo->setValue('foo value');
  1226. $subForm->bar->setValue('bar value');
  1227. $this->form->addSubForm($subForm, 'page1');
  1228. $values = $this->form->getValue('subform');
  1229. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1230. $this->assertTrue(isset($values['bar']));
  1231. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1232. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1233. }
  1234. public function testIsValidCanValidateSubFormsWithArbitraryElementsBelong()
  1235. {
  1236. $subForm = new Zend_Form_SubForm();
  1237. $subForm->addElement('text', 'test')->test
  1238. ->setRequired(true)->addValidator('Identical', false, array('Test Value'));
  1239. $this->form->addSubForm($subForm, 'sub');
  1240. $this->form->setElementsBelongTo('foo[bar]');
  1241. $subForm->setElementsBelongTo('my[subform]');
  1242. $data = array(
  1243. 'foo' => array(
  1244. 'bar' => array(
  1245. 'my' => array(
  1246. 'subform' => array(
  1247. 'test' => 'Test Value',
  1248. ),
  1249. ),
  1250. ),
  1251. ),
  1252. );
  1253. $this->assertTrue($this->form->isValid($data));
  1254. }
  1255. public function testIsValidCanValidateNestedSubFormsWithArbitraryElementsBelong()
  1256. {
  1257. $subForm = new Zend_Form_SubForm();
  1258. $subForm->addElement('text', 'test1')->test1
  1259. ->setRequired(true)->addValidator('Identical', false, array('Test1 Value'));
  1260. $this->form->addSubForm($subForm, 'sub');
  1261. $subSubForm = new Zend_Form_SubForm();
  1262. $subSubForm->addElement('text', 'test2')->test2
  1263. ->setRequired(true)->addValidator('Identical', false, array('Test2 Value'));
  1264. $subForm->addSubForm($subSubForm, 'subSub');
  1265. $this->form->setElementsBelongTo('form[first]');
  1266. // Notice we skipped subForm, to mix manual and auto elementsBelongTo.
  1267. $subSubForm->setElementsBelongTo('subsubform[first]');
  1268. $data = array(
  1269. 'form' => array(
  1270. 'first' => array(
  1271. 'sub' => array(
  1272. 'test1' => 'Test1 Value',
  1273. 'subsubform' => array(
  1274. 'first' => array(
  1275. 'test2' => 'Test2 Value',
  1276. ),
  1277. ),
  1278. ),
  1279. ),
  1280. ),
  1281. );
  1282. $this->assertTrue($this->form->isValid($data));
  1283. }
  1284. // Display groups
  1285. public function testCanAddAndRetrieveSingleDisplayGroups()
  1286. {
  1287. $this->testCanAddAndRetrieveMultipleElements();
  1288. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  1289. $group = $this->form->getDisplayGroup('barbat');
  1290. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1291. $elements = $group->getElements();
  1292. $expected = array('bar' => $this->form->bar, 'bat' => $this->form->bat);
  1293. $this->assertEquals($expected, $elements);
  1294. }
  1295. public function testDisplayGroupsMustContainAtLeastOneElement()
  1296. {
  1297. try {
  1298. $this->form->addDisplayGroup(array(), 'foo');
  1299. $this->fail('Empty display group should raise exception');
  1300. } catch (Zend_Form_Exception $e) {
  1301. $this->assertContains('No valid elements', $e->getMessage());
  1302. }
  1303. }
  1304. public function testCanAddAndRetrieveMultipleDisplayGroups()
  1305. {
  1306. $this->testCanAddAndRetrieveMultipleElements();
  1307. $this->form->addDisplayGroups(array(
  1308. array(array('bar', 'bat'), 'barbat'),
  1309. 'foobaz' => array('baz', 'foo')
  1310. ));
  1311. $groups = $this->form->getDisplayGroups();
  1312. $expected = array(
  1313. 'barbat' => array('bar' => $this->form->bar, 'bat' => $this->form->bat),
  1314. 'foobaz' => array('baz' => $this->form->baz, 'foo' => $this->form->foo),
  1315. );
  1316. foreach ($groups as $group) {
  1317. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1318. }
  1319. $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
  1320. $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
  1321. }
  1322. public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
  1323. {
  1324. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1325. $this->form->setDisplayGroups(array('foobar' => array('bar', 'foo')));
  1326. $groups = $this->form->getDisplayGroups();
  1327. $expected = array('bar' => $this->form->bar, 'foo' => $this->form->foo);
  1328. $this->assertEquals(1, count($groups));
  1329. $this->assertTrue(isset($groups['foobar']));
  1330. $this->assertEquals($expected, $groups['foobar']->getElements());
  1331. }
  1332. public function testCanRemoveSingleDisplayGroup()
  1333. {
  1334. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1335. $this->assertTrue($this->form->removeDisplayGroup('barbat'));
  1336. $this->assertNull($this->form->getDisplayGroup('barbat'));
  1337. }
  1338. public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
  1339. {
  1340. $this->assertFalse($this->form->removeDisplayGroup('bogus'));
  1341. }
  1342. public function testCanClearAllDisplayGroups()
  1343. {
  1344. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1345. $this->form->clearDisplayGroups();
  1346. $groups = $this->form->getDisplayGroups();
  1347. $this->assertTrue(is_array($groups));
  1348. $this->assertTrue(empty($groups));
  1349. }
  1350. public function testOverloadingDisplayGroups()
  1351. {
  1352. $this->testCanAddAndRetrieveMultipleElements();
  1353. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1354. $this->assertTrue(isset($this->form->foobar));
  1355. $group = $this->form->foobar;
  1356. $expected = array('foo' => $this->form->foo, 'bar' => $this->form->bar);
  1357. $this->assertEquals($expected, $group->getElements());
  1358. unset($this->form->foobar);
  1359. $this->assertFalse(isset($this->form->foobar));
  1360. $this->form->barbaz = array('bar', 'baz');
  1361. $this->assertTrue(isset($this->form->barbaz));
  1362. $group = $this->form->barbaz;
  1363. $expected = array('bar' => $this->form->bar, 'baz' => $this->form->baz);
  1364. $this->assertSame($expected, $group->getElements());
  1365. }
  1366. public function testDefaultDisplayGroupClassExists()
  1367. {
  1368. $this->assertEquals('Zend_Form_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1369. }
  1370. public function testCanSetDefaultDisplayGroupClass()
  1371. {
  1372. $this->testDefaultDisplayGroupClassExists();
  1373. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1374. $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1375. }
  1376. public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
  1377. {
  1378. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1379. $this->setupElements();
  1380. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1381. $displayGroup = $this->form->getDisplayGroup('foobar');
  1382. $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup);
  1383. }
  1384. public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
  1385. {
  1386. $this->setupElements();
  1387. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
  1388. $this->assertTrue($this->form->foobar instanceof Zend_Form_FormTest_DisplayGroup);
  1389. }
  1390. /**
  1391. * @group ZF-3254
  1392. */
  1393. public function testAddingDisplayGroupShouldPassOptions()
  1394. {
  1395. $this->testCanAddAndRetrieveMultipleElements();
  1396. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
  1397. $group = $this->form->getDisplayGroup('barbat');
  1398. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1399. $decorators = $group->getDecorators();
  1400. $this->assertTrue(is_array($decorators));
  1401. $this->assertTrue(empty($decorators));
  1402. }
  1403. // Processing
  1404. public function testPopulateProxiesToSetDefaults()
  1405. {
  1406. $this->testCanAddAndRetrieveMultipleElements();
  1407. $values = array(
  1408. 'foo' => 'foovalue',
  1409. 'bar' => 'barvalue',
  1410. 'baz' => 'bazvalue',
  1411. 'bat' => 'batvalue'
  1412. );
  1413. $this->form->populate($values);
  1414. $test = $this->form->getValues();
  1415. $elements = $this->form->getElements();
  1416. foreach (array_keys($values) as $name) {
  1417. $this->assertEquals($values[$name], $test[$name]);
  1418. }
  1419. }
  1420. public function setupElements()
  1421. {
  1422. $foo = new Zend_Form_Element_Text('foo');
  1423. $foo->addValidator('NotEmpty')
  1424. ->addValidator('Alpha');
  1425. $bar = new Zend_Form_Element_Text('bar');
  1426. $bar->addValidator('NotEmpty')
  1427. ->addValidator('Digits');
  1428. $baz = new Zend_Form_Element_Text('baz');
  1429. $baz->addValidator('NotEmpty')
  1430. ->addValidator('Alnum');
  1431. $this->form->addElements(array($foo, $bar, $baz));
  1432. $this->elementValues = array(
  1433. 'foo' => 'fooBarBAZ',
  1434. 'bar' => '123456789',
  1435. 'baz' => 'foo123BAR',
  1436. );
  1437. }
  1438. public function testIsValidShouldThrowExceptionWithNonArrayArgument()
  1439. {
  1440. try {
  1441. $this->form->isValid(true);
  1442. $this->fail('isValid() should raise exception with non-array argument');
  1443. } catch (Zend_Form_Exception $e) {
  1444. $this->assertContains('expects an array', $e->getMessage());
  1445. }
  1446. }
  1447. public function testCanValidateFullFormContainingOnlyElements()
  1448. {
  1449. $this->_checkZf2794();
  1450. $this->setupElements();
  1451. $this->assertTrue($this->form->isValid($this->elementValues));
  1452. $values = array(
  1453. 'foo' => '12345',
  1454. 'bar' => 'abc',
  1455. 'baz' => 'abc-123'
  1456. );
  1457. $this->assertFalse($this->form->isValid($values));
  1458. $validator = $this->form->foo->getValidator('alpha');
  1459. $this->assertEquals('12345', $validator->value);
  1460. $validator = $this->form->bar->getValidator('digits');
  1461. $this->assertEquals('abc', $validator->value);
  1462. $validator = $this->form->baz->getValidator('alnum');
  1463. $this->assertEquals('abc-123', $validator->value);
  1464. }
  1465. public function testValidationTakesElementRequiredFlagsIntoAccount()
  1466. {
  1467. $this->_checkZf2794();
  1468. $this->setupElements();
  1469. $this->assertTrue($this->form->isValid(array()));
  1470. $this->form->getElement('foo')->setRequired(true);
  1471. $this->assertTrue($this->form->isValid(array(
  1472. 'foo' => 'abc',
  1473. 'baz' => 'abc123'
  1474. )));
  1475. $this->assertFalse($this->form->isValid(array(
  1476. 'baz' => 'abc123'
  1477. )));
  1478. }
  1479. public function testCanValidatePartialFormContainingOnlyElements()
  1480. {
  1481. $this->_checkZf2794();
  1482. $this->setupElements();
  1483. $this->form->getElement('foo')->setRequired(true);
  1484. $this->form->getElement('bar')->setRequired(true);
  1485. $this->form->getElement('baz')->setRequired(true);
  1486. $this->assertTrue($this->form->isValidPartial(array(
  1487. 'foo' => 'abc',
  1488. 'baz' => 'abc123'
  1489. )));
  1490. $this->assertFalse($this->form->isValidPartial(array(
  1491. 'foo' => '123',
  1492. 'baz' => 'abc-123'
  1493. )));
  1494. }
  1495. public function setupSubForm()
  1496. {
  1497. $subForm = new Zend_Form_SubForm();
  1498. $foo = new Zend_Form_Element_Text('subfoo');
  1499. $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
  1500. $bar = new Zend_Form_Element_Text('subbar');
  1501. $bar->addValidators(array('NotEmpty', 'Digits'));
  1502. $baz = new Zend_Form_Element_Text('subbaz');
  1503. $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
  1504. $subForm->addElements(array($foo, $bar, $baz));
  1505. $this->form->addSubForm($subForm, 'sub');
  1506. }
  1507. public function testFullDataArrayUsedToValidateSubFormByDefault()
  1508. {
  1509. $this->_checkZf2794();
  1510. $this->setupElements();
  1511. $this->setupSubForm();
  1512. $data = array(
  1513. 'foo' => 'abcdef',
  1514. 'bar' => '123456',
  1515. 'baz' => '123abc',
  1516. 'subfoo' => 'abcdef',
  1517. 'subbar' => '123456',
  1518. 'subbaz' => '123abc',
  1519. );
  1520. $this->assertTrue($this->form->isValid($data));
  1521. $data = array(
  1522. 'foo' => 'abcdef',
  1523. 'bar' => '123456',
  1524. 'baz' => '123abc',
  1525. 'subfoo' => '123',
  1526. 'subbar' => 'abc',
  1527. 'subbaz' => '123-abc',
  1528. );
  1529. $this->assertFalse($this->form->isValid($data));
  1530. $data = array(
  1531. 'foo' => 'abcdef',
  1532. 'bar' => '123456',
  1533. 'baz' => '123abc',
  1534. 'subfoo' => 'abc',
  1535. 'subbaz' => '123abc',
  1536. );
  1537. $this->assertTrue($this->form->isValid($data));
  1538. $data = array(
  1539. 'foo' => 'abcdef',
  1540. 'bar' => '123456',
  1541. 'baz' => '123abc',
  1542. 'subbar' => '123',
  1543. 'subbaz' => '123abc',
  1544. );
  1545. $this->assertFalse($this->form->isValid($data));
  1546. }
  1547. public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
  1548. {
  1549. $this->_checkZf2794();
  1550. $this->setupElements();
  1551. $this->setupSubForm();
  1552. $data = array(
  1553. 'foo' => 'abcdef',
  1554. 'bar' => '123456',
  1555. 'baz' => '123abc',
  1556. 'sub' => array(
  1557. 'subfoo' => 'abcdef',
  1558. 'subbar' => '123456',
  1559. 'subbaz' => '123abc',
  1560. ),
  1561. );
  1562. $this->assertTrue($this->form->isValid($data));
  1563. $data = array(
  1564. 'foo' => 'abcdef',
  1565. 'bar' => '123456',
  1566. 'baz' => '123abc',
  1567. 'sub' => array(
  1568. 'subfoo' => '123',
  1569. 'subbar' => 'abc',
  1570. 'subbaz' => '123-abc',
  1571. )
  1572. );
  1573. $this->assertFalse($this->form->isValid($data));
  1574. $data = array(
  1575. 'foo' => 'abcdef',
  1576. 'bar' => '123456',
  1577. 'baz' => '123abc',
  1578. 'sub' => array(
  1579. 'subfoo' => 'abc',
  1580. 'subbaz' => '123abc',
  1581. )
  1582. );
  1583. $this->assertTrue($this->form->isValid($data));
  1584. $data = array(
  1585. 'foo' => 'abcdef',
  1586. 'bar' => '123456',
  1587. 'baz' => '123abc',
  1588. 'sub' => array(
  1589. 'subbar' => '123',
  1590. 'subbaz' => '123abc',
  1591. )
  1592. );
  1593. $this->assertFalse($this->form->isValid($data));
  1594. }
  1595. public function testCanValidateNestedFormsWithElementsBelongingToArrays()
  1596. {
  1597. $form = new Zend_Form();
  1598. $form->setElementsBelongTo('foobar');
  1599. $form->addElement('text', 'firstName')
  1600. ->getElement('firstName')
  1601. ->setRequired(true);
  1602. $form->addElement('text', 'lastName')
  1603. ->getElement('lastName')
  1604. ->setRequired(true);
  1605. $subForm = new Zend_Form_SubForm();
  1606. $subForm->setElementsBelongTo('baz');
  1607. $subForm->addElement('text', 'email')
  1608. ->getElement('email')->setRequired(true);
  1609. $subSubForm = new Zend_Form_SubForm();
  1610. $subSubForm->setElementsBelongTo('bat');
  1611. $subSubForm->addElement('checkbox', 'home')
  1612. ->getElement('home')->setRequired(true);
  1613. $subForm->addSubForm($subSubForm, 'subSub');
  1614. $form->addSubForm($subForm, 'sub')
  1615. ->addElement('submit', 'save', array('value' => 'submit'));
  1616. $data = array('foobar' => array(
  1617. 'firstName' => 'Mabel',
  1618. 'lastName' => 'Cow',
  1619. 'baz' => array(
  1620. 'email' => 'mabel@cow.org',
  1621. 'bat' => array(
  1622. 'home' => 1,
  1623. )
  1624. )
  1625. ));
  1626. $this->assertTrue($form->isValid($data));
  1627. $this->assertEquals('Mabel', $form->firstName->getValue());
  1628. $this->assertEquals('Cow', $form->lastName->getValue());
  1629. $this->assertEquals('mabel@cow.org', $form->sub->email->getValue());
  1630. $this->assertEquals(1, $form->sub->subSub->home->getValue());
  1631. }
  1632. public function testCanValidatePartialFormContainingSubForms()
  1633. {
  1634. $this->_checkZf2794();
  1635. $this->setupElements();
  1636. $this->setupSubForm();
  1637. $data = array(
  1638. 'subfoo' => 'abcdef',
  1639. 'subbar' => '123456',
  1640. );
  1641. $this->assertTrue($this->form->isValidPartial($data));
  1642. $data = array(
  1643. 'foo' => 'abcdef',
  1644. 'baz' => '123abc',
  1645. 'sub' => array(
  1646. 'subbar' => '123',
  1647. )
  1648. );
  1649. $this->assertTrue($this->form->isValidPartial($data));
  1650. $data = array(
  1651. 'foo' => 'abcdef',
  1652. 'bar' => '123456',
  1653. 'baz' => '123abc',
  1654. 'sub' => array(
  1655. 'subfoo' => '123',
  1656. )
  1657. );
  1658. $this->assertFalse($this->form->isValidPartial($data));
  1659. }
  1660. public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
  1661. {
  1662. $this->_checkZf2794();
  1663. $form = new Zend_Form();
  1664. $form->setElementsBelongTo('foobar');
  1665. $form->addElement('text', 'firstName')
  1666. ->getElement('firstName')
  1667. ->setRequired(false);
  1668. $form->addElement('text', 'lastName')
  1669. ->getElement('lastName')
  1670. ->setRequired(true);
  1671. $subForm = new Zend_Form_SubForm();
  1672. $subForm->setElementsBelongTo('baz');
  1673. $subForm->addElement('text', 'email')
  1674. ->getElement('email')
  1675. ->setRequired(true)
  1676. ->addValidator('NotEmpty');
  1677. $subSubForm = new Zend_Form_SubForm();
  1678. $subSubForm->setElementsBelongTo('bat');
  1679. $subSubForm->addElement('checkbox', 'home')
  1680. ->getElement('home')
  1681. ->setRequired(true)
  1682. ->addValidator('InArray', false, array(array('1')));
  1683. $subForm->addSubForm($subSubForm, 'subSub');
  1684. $form->addSubForm($subForm, 'sub')
  1685. ->addElement('submit', 'save', array('value' => 'submit'));
  1686. $data = array('foobar' => array(
  1687. 'lastName' => 'Cow',
  1688. ));
  1689. $this->assertTrue($form->isValidPartial($data));
  1690. $this->assertEquals('Cow', $form->lastName->getValue());
  1691. $firstName = $form->firstName->getValue();
  1692. $email = $form->sub->email->getValue();
  1693. $home = $form->sub->subSub->home->getValue();
  1694. $this->assertTrue(empty($firstName));
  1695. $this->assertTrue(empty($email));
  1696. $this->assertTrue(empty($home));
  1697. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1698. $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
  1699. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1700. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1701. $messages = $form->getMessages();
  1702. $this->assertFalse(empty($messages));
  1703. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
  1704. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
  1705. }
  1706. public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
  1707. {
  1708. $this->_checkZf2794();
  1709. $form = new Zend_Form();
  1710. $form->setElementsBelongTo('foo[bar]');
  1711. $form->addElement('text', 'firstName')
  1712. ->getElement('firstName')
  1713. ->setRequired(false);
  1714. $form->addElement('text', 'lastName')
  1715. ->getElement('lastName')
  1716. ->setRequired(true);
  1717. $subForm = new Zend_Form_SubForm();
  1718. $subForm->setElementsBelongTo('baz');
  1719. $subForm->addElement('text', 'email')
  1720. ->getElement('email')
  1721. ->setRequired(true)
  1722. ->addValidator('NotEmpty');
  1723. $subSubForm = new Zend_Form_SubForm();
  1724. $subSubForm->setElementsBelongTo('bat[quux]');
  1725. $subSubForm->addElement('checkbox', 'home')
  1726. ->getElement('home')
  1727. ->setRequired(true)
  1728. ->addValidator('InArray', false, array(array('1')));
  1729. $subForm->addSubForm($subSubForm, 'subSub');
  1730. $form->addSubForm($subForm, 'sub')
  1731. ->addElement('submit', 'save', array('value' => 'submit'));
  1732. $data = array('foo' => array(
  1733. 'bar' => array(
  1734. 'lastName' => 'Cow',
  1735. ),
  1736. ));
  1737. $this->assertTrue($form->isValidPartial($data));
  1738. $this->assertEquals('Cow', $form->lastName->getValue());
  1739. $firstName = $form->firstName->getValue();
  1740. $email = $form->sub->email->getValue();
  1741. $home = $form->sub->subSub->home->getValue();
  1742. $this->assertTrue(empty($firstName));
  1743. $this->assertTrue(empty($email));
  1744. $this->assertTrue(empty($home));
  1745. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1746. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1747. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1748. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1749. }
  1750. public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
  1751. {
  1752. $this->_checkZf2794();
  1753. $form = new Zend_Form();
  1754. $form->setElementsBelongTo('foo[bar]');
  1755. $form->addElement('text', 'firstName')
  1756. ->getElement('firstName')
  1757. ->setRequired(false);
  1758. $form->addElement('text', 'lastName')
  1759. ->getElement('lastName')
  1760. ->setRequired(true);
  1761. $subForm = new Zend_Form_SubForm();
  1762. $subForm->setElementsBelongTo('baz');
  1763. $subForm->addElement('text', 'email')
  1764. ->getElement('email')
  1765. ->setRequired(true)
  1766. ->addValidator('NotEmpty');
  1767. $subSubForm = new Zend_Form_SubForm();
  1768. $subSubForm->setElementsBelongTo('bat[quux]');
  1769. $subSubForm->addElement('checkbox', 'home')
  1770. ->getElement('home')
  1771. ->setRequired(true)
  1772. ->addValidator('InArray', false, array(array('1')));
  1773. $subForm->addSubForm($subSubForm, 'subSub');
  1774. $form->addSubForm($subForm, 'sub')
  1775. ->addElement('submit', 'save', array('value' => 'submit'));
  1776. $data = array('foo' => array(
  1777. 'bar' => array(
  1778. 'lastName' => 'Cow',
  1779. ),
  1780. ));
  1781. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1782. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1783. $form->isValidPartial($data);
  1784. $messages = $form->getMessages();
  1785. $this->assertFalse(empty($messages));
  1786. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
  1787. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
  1788. }
  1789. public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
  1790. {
  1791. $this->setupElements();
  1792. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1793. $this->assertTrue($this->form->isValid($this->elementValues));
  1794. $this->assertFalse($this->form->isValid(array(
  1795. 'foo' => '123',
  1796. 'bar' => 'abc',
  1797. 'baz' => 'abc-123'
  1798. )));
  1799. }
  1800. public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
  1801. {
  1802. $this->setupElements();
  1803. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1804. $this->assertTrue($this->form->isValid(array(
  1805. 'foo' => 'abc',
  1806. 'baz' => 'abc123'
  1807. )));
  1808. $this->assertFalse($this->form->isValid(array(
  1809. 'foo' => '123',
  1810. 'baz' => 'abc-123'
  1811. )));
  1812. }
  1813. public function testProcessAjaxReturnsJsonTrueForValidForm()
  1814. {
  1815. $this->setupElements();
  1816. $return = $this->form->processAjax($this->elementValues);
  1817. $this->assertTrue(Zend_Json::decode($return));
  1818. }
  1819. public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
  1820. {
  1821. $this->setupElements();
  1822. $data = array('foo' => 'abcdef', 'baz' => 'abc123');
  1823. $return = $this->form->processAjax($data);
  1824. $this->assertTrue(Zend_Json::decode($return));
  1825. }
  1826. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
  1827. {
  1828. $this->setupElements();
  1829. $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
  1830. $return = Zend_Json::decode($this->form->processAjax($data));
  1831. $this->assertTrue(is_array($return));
  1832. $this->assertEquals(array_keys($data), array_keys($return));
  1833. }
  1834. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
  1835. {
  1836. $this->setupElements();
  1837. $data = array('baz' => 'abc-123');
  1838. $return = Zend_Json::decode($this->form->processAjax($data));
  1839. $this->assertTrue(is_array($return));
  1840. $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
  1841. }
  1842. public function testPersistDataStoresDataInSession()
  1843. {
  1844. $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
  1845. }
  1846. public function testCanCheckIfErrorsAreRegistered()
  1847. {
  1848. $this->assertFalse($this->form->isErrors());
  1849. $this->testCanValidateFullFormContainingOnlyElements();
  1850. $this->assertTrue($this->form->isErrors());
  1851. }
  1852. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  1853. {
  1854. $this->_checkZf2794();
  1855. $this->testCanValidateFullFormContainingOnlyElements();
  1856. $codes = $this->form->getErrors();
  1857. $keys = array('foo', 'bar', 'baz');
  1858. $this->assertEquals($keys, array_keys($codes));
  1859. }
  1860. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  1861. {
  1862. $this->_checkZf2794();
  1863. $this->testCanValidateFullFormContainingOnlyElements();
  1864. $codes = $this->form->getErrors();
  1865. $keys = array('foo', 'bar', 'baz');
  1866. $errors = $this->form->getErrors('foo');
  1867. $foo = $this->form->foo;
  1868. $this->assertEquals($foo->getErrors(), $errors);
  1869. }
  1870. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  1871. {
  1872. $this->_checkZf2794();
  1873. $this->testCanValidateFullFormContainingOnlyElements();
  1874. $codes = $this->form->getMessages();
  1875. $keys = array('foo', 'bar', 'baz');
  1876. $this->assertEquals($keys, array_keys($codes));
  1877. }
  1878. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  1879. {
  1880. $this->_checkZf2794();
  1881. $this->testCanValidateFullFormContainingOnlyElements();
  1882. $codes = $this->form->getMessages();
  1883. $keys = array('foo', 'bar', 'baz');
  1884. $messages = $this->form->getMessages('foo');
  1885. $foo = $this->form->foo;
  1886. $this->assertEquals($foo->getMessages(), $messages);
  1887. }
  1888. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  1889. {
  1890. $this->_checkZf2794();
  1891. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1892. $codes = $this->form->getErrors();
  1893. $this->assertTrue(array_key_exists('sub', $codes));
  1894. $this->assertTrue(is_array($codes['sub']));
  1895. $keys = array('subfoo', 'subbar', 'subbaz');
  1896. $this->assertEquals($keys, array_keys($codes['sub']));
  1897. }
  1898. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  1899. {
  1900. $this->_checkZf2794();
  1901. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1902. $codes = $this->form->getErrors('sub');
  1903. $this->assertTrue(is_array($codes));
  1904. $this->assertFalse(empty($codes));
  1905. $keys = array('subfoo', 'subbar', 'subbaz');
  1906. $this->assertEquals($keys, array_keys($codes));
  1907. }
  1908. public function testGetErrorsHonorsElementsBelongTo()
  1909. {
  1910. $this->_checkZf2794();
  1911. $subForm = new Zend_Form_SubForm();
  1912. $subForm->setElementsBelongTo('foo[bar]');
  1913. $subForm->addElement('text', 'test')->test
  1914. ->setRequired(true);
  1915. $this->form->addSubForm($subForm, 'sub');
  1916. $data = array('foo' => array(
  1917. 'bar' => array(
  1918. 'test' => '',
  1919. ),
  1920. ));
  1921. $this->form->isValid($data);
  1922. $codes = $this->form->getErrors();
  1923. $this->assertFalse(empty($codes['foo']['bar']['test']));
  1924. }
  1925. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  1926. {
  1927. $this->_checkZf2794();
  1928. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1929. $data = array(
  1930. 'foo' => 'abcdef',
  1931. 'bar' => '123456',
  1932. 'baz' => '123abc',
  1933. 'subfoo' => '123',
  1934. 'subbar' => 'abc',
  1935. 'subbaz' => '123-abc',
  1936. );
  1937. $this->assertFalse($this->form->isValid($data));
  1938. $codes = $this->form->getMessages();
  1939. $this->assertTrue(array_key_exists('sub', $codes));
  1940. $this->assertTrue(is_array($codes['sub']));
  1941. $keys = array('subfoo', 'subbar', 'subbaz');
  1942. $this->assertEquals($keys, array_keys($codes['sub']));
  1943. }
  1944. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  1945. {
  1946. $this->_checkZf2794();
  1947. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1948. $data = array(
  1949. 'foo' => 'abcdef',
  1950. 'bar' => '123456',
  1951. 'baz' => '123abc',
  1952. 'subfoo' => '123',
  1953. 'subbar' => 'abc',
  1954. 'subbaz' => '123-abc',
  1955. );
  1956. $this->assertFalse($this->form->isValid($data));
  1957. $codes = $this->form->getMessages('sub');
  1958. $this->assertTrue(is_array($codes));
  1959. $this->assertFalse(empty($codes));
  1960. $keys = array('subfoo', 'subbar', 'subbaz');
  1961. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  1962. }
  1963. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  1964. {
  1965. $this->_checkZf2794();
  1966. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  1967. $translate = new Zend_Translate('array', $translations, 'en');
  1968. $translate->setLocale('en');
  1969. $this->form->addElements(array(
  1970. 'foo' => array(
  1971. 'type' => 'text',
  1972. 'options' => array(
  1973. 'required' => true,
  1974. 'validators' => array('NotEmpty')
  1975. )
  1976. ),
  1977. 'bar' => array(
  1978. 'type' => 'text',
  1979. 'options' => array(
  1980. 'required' => true,
  1981. 'validators' => array('Digits')
  1982. )
  1983. ),
  1984. ))
  1985. ->setTranslator($translate);
  1986. $data = array(
  1987. 'foo' => '',
  1988. 'bar' => 'abc',
  1989. );
  1990. if ($this->form->isValid($data)) {
  1991. $this->fail('Form should not validate');
  1992. }
  1993. $messages = $this->form->getMessages();
  1994. $this->assertTrue(isset($messages['foo']));
  1995. $this->assertTrue(isset($messages['bar']));
  1996. foreach ($messages['foo'] as $key => $message) {
  1997. if (array_key_exists($key, $translations)) {
  1998. $this->assertEquals($translations[$key], $message);
  1999. } else {
  2000. $this->fail('Translation for ' . $key . ' does not exist?');
  2001. }
  2002. }
  2003. foreach ($messages['bar'] as $key => $message) {
  2004. if (array_key_exists($key, $translations)) {
  2005. $this->assertEquals($translations[$key], $message);
  2006. } else {
  2007. $this->fail('Translation for ' . $key . ' does not exist?');
  2008. }
  2009. }
  2010. }
  2011. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2012. {
  2013. $this->_checkZf2794();
  2014. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2015. $translate = new Zend_Translate('array', $translations, 'en');
  2016. $translate->setLocale('en');
  2017. $this->form->addElements(array(
  2018. 'foo' => array(
  2019. 'type' => 'text',
  2020. 'options' => array(
  2021. 'required' => true,
  2022. 'validators' => array('NotEmpty')
  2023. )
  2024. ),
  2025. 'bar' => array(
  2026. 'type' => 'text',
  2027. 'options' => array(
  2028. 'required' => true,
  2029. 'validators' => array('Digits')
  2030. )
  2031. ),
  2032. ))
  2033. ->setTranslator($translate);
  2034. $data = array(
  2035. 'foo' => '',
  2036. );
  2037. if ($this->form->isValidPartial($data)) {
  2038. $this->fail('Form should not validate');
  2039. }
  2040. $messages = $this->form->getMessages();
  2041. $this->assertTrue(isset($messages['foo']));
  2042. $this->assertFalse(isset($messages['bar']));
  2043. foreach ($messages['foo'] as $key => $message) {
  2044. if (array_key_exists($key, $translations)) {
  2045. $this->assertEquals($translations[$key], $message);
  2046. } else {
  2047. $this->fail('Translation for ' . $key . ' does not exist?');
  2048. }
  2049. }
  2050. }
  2051. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2052. {
  2053. $this->_checkZf2794();
  2054. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2055. $translate = new Zend_Translate('array', $translations, 'en');
  2056. $translate->setLocale('en');
  2057. $this->form->addElements(array(
  2058. 'foo' => array(
  2059. 'type' => 'text',
  2060. 'options' => array(
  2061. 'required' => true,
  2062. 'validators' => array('NotEmpty')
  2063. )
  2064. ),
  2065. 'bar' => array(
  2066. 'type' => 'text',
  2067. 'options' => array(
  2068. 'required' => true,
  2069. 'validators' => array('Digits')
  2070. )
  2071. ),
  2072. ))
  2073. ->setTranslator($translate);
  2074. $data = array(
  2075. 'foo' => '',
  2076. );
  2077. $return = $this->form->processAjax($data);
  2078. $messages = Zend_Json::decode($return);
  2079. $this->assertTrue(is_array($messages));
  2080. $this->assertTrue(isset($messages['foo']));
  2081. $this->assertFalse(isset($messages['bar']));
  2082. foreach ($messages['foo'] as $key => $message) {
  2083. if (array_key_exists($key, $translations)) {
  2084. $this->assertEquals($translations[$key], $message);
  2085. } else {
  2086. $this->fail('Translation for ' . $key . ' does not exist?');
  2087. }
  2088. }
  2089. }
  2090. /**#@+
  2091. * @group ZF-2988
  2092. */
  2093. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2094. {
  2095. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2096. $this->form->addErrorMessage('Invalid values entered');
  2097. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2098. $messages = $this->form->getMessages();
  2099. $this->assertEquals(1, count($messages));
  2100. $this->assertEquals('Invalid values entered', array_shift($messages));
  2101. }
  2102. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2103. {
  2104. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2105. $this->form->addErrorMessage('Invalid values entered');
  2106. $this->form->addErrorMessage('Really, they are not valid');
  2107. $messages = $this->form->getErrorMessages();
  2108. $this->assertEquals(2, count($messages));
  2109. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2110. $messages = $this->form->getMessages();
  2111. $this->assertEquals(2, count($messages));
  2112. $this->assertEquals('Invalid values entered', array_shift($messages));
  2113. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2114. }
  2115. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2116. {
  2117. $set1 = array('foo', 'bar', 'baz');
  2118. $this->form->addErrorMessages($set1);
  2119. $this->assertSame($set1, $this->form->getErrorMessages());
  2120. }
  2121. public function testSetErrorMessagesShouldOverwriteMessages()
  2122. {
  2123. $set1 = array('foo', 'bar', 'baz');
  2124. $set2 = array('bat', 'cat');
  2125. $this->form->addErrorMessages($set1);
  2126. $this->assertSame($set1, $this->form->getErrorMessages());
  2127. $this->form->setErrorMessages($set2);
  2128. $this->assertSame($set2, $this->form->getErrorMessages());
  2129. }
  2130. public function testCustomErrorMessageStackShouldBeClearable()
  2131. {
  2132. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2133. $this->form->clearErrorMessages();
  2134. $messages = $this->form->getErrorMessages();
  2135. $this->assertTrue(empty($messages));
  2136. }
  2137. public function testCustomErrorMessagesShouldBeTranslated()
  2138. {
  2139. $translations = array(
  2140. 'foo' => 'Foo message',
  2141. );
  2142. $translate = new Zend_Translate('array', $translations);
  2143. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2144. $this->form->setTranslator($translate)
  2145. ->addErrorMessage('foo');
  2146. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2147. $messages = $this->form->getMessages();
  2148. $this->assertEquals(1, count($messages));
  2149. $this->assertEquals('Foo message', array_shift($messages));
  2150. }
  2151. public function testShouldAllowMarkingFormAsInvalid()
  2152. {
  2153. $this->form->addErrorMessage('Invalid values entered');
  2154. $this->assertFalse($this->form->isErrors());
  2155. $this->form->markAsError();
  2156. $this->assertTrue($this->form->isErrors());
  2157. $messages = $this->form->getMessages();
  2158. $this->assertEquals(1, count($messages));
  2159. $this->assertEquals('Invalid values entered', array_shift($messages));
  2160. }
  2161. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2162. {
  2163. $this->assertFalse($this->form->isErrors());
  2164. $this->form->setErrors(array('Error 1', 'Error 2'))
  2165. ->addError('Error 3')
  2166. ->addErrors(array('Error 4', 'Error 5'));
  2167. $this->assertTrue($this->form->isErrors());
  2168. $messages = $this->form->getMessages();
  2169. $this->assertEquals(5, count($messages));
  2170. foreach (range(1, 5) as $id) {
  2171. $message = 'Error ' . $id;
  2172. $this->assertContains($message, $messages);
  2173. }
  2174. }
  2175. /**#@-*/
  2176. // View object
  2177. public function getView()
  2178. {
  2179. $view = new Zend_View();
  2180. $libPath = dirname(__FILE__) . '/../../../library';
  2181. $view->addHelperPath($libPath . '/Zend/View/Helper');
  2182. return $view;
  2183. }
  2184. public function testGetViewRetrievesFromViewRendererByDefault()
  2185. {
  2186. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2187. $viewRenderer->initView();
  2188. $view = $viewRenderer->view;
  2189. $test = $this->form->getView();
  2190. $this->assertSame($view, $test);
  2191. }
  2192. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2193. {
  2194. $this->assertNull($this->form->getView());
  2195. }
  2196. public function testCanSetView()
  2197. {
  2198. $view = new Zend_View();
  2199. $this->assertNull($this->form->getView());
  2200. $this->form->setView($view);
  2201. $received = $this->form->getView();
  2202. $this->assertSame($view, $received);
  2203. }
  2204. // Decorators
  2205. public function testFormDecoratorRegisteredByDefault()
  2206. {
  2207. $this->_checkZf2794();
  2208. $decorator = $this->form->getDecorator('form');
  2209. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2210. }
  2211. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2212. {
  2213. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2214. $decorators = $form->getDecorators();
  2215. $this->assertEquals(array(), $decorators);
  2216. }
  2217. public function testCanAddSingleDecoratorAsString()
  2218. {
  2219. $this->_checkZf2794();
  2220. $this->form->clearDecorators();
  2221. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2222. $this->form->addDecorator('viewHelper');
  2223. $decorator = $this->form->getDecorator('viewHelper');
  2224. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2225. }
  2226. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2227. {
  2228. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2229. }
  2230. public function testCanAddSingleDecoratorAsDecoratorObject()
  2231. {
  2232. $this->form->clearDecorators();
  2233. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2234. $decorator = new Zend_Form_Decorator_ViewHelper;
  2235. $this->form->addDecorator($decorator);
  2236. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2237. $this->assertSame($decorator, $test);
  2238. }
  2239. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2240. {
  2241. $this->_checkZf2794();
  2242. $this->form->clearDecorators();
  2243. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2244. $decorator = new Zend_Form_Decorator_ViewHelper;
  2245. $this->form->addDecorator($decorator);
  2246. $test = $this->form->getDecorator('viewHelper');
  2247. $this->assertSame($decorator, $test);
  2248. }
  2249. public function testCanAddMultipleDecorators()
  2250. {
  2251. $this->_checkZf2794();
  2252. $this->form->clearDecorators();
  2253. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2254. $testDecorator = new Zend_Form_Decorator_Errors;
  2255. $this->form->addDecorators(array(
  2256. 'ViewHelper',
  2257. $testDecorator
  2258. ));
  2259. $viewHelper = $this->form->getDecorator('viewHelper');
  2260. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2261. $decorator = $this->form->getDecorator('errors');
  2262. $this->assertSame($testDecorator, $decorator);
  2263. }
  2264. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2265. {
  2266. $this->_checkZf2794();
  2267. $this->assertFalse($this->form->removeDecorator('foobar'));
  2268. }
  2269. public function testCanRemoveDecorator()
  2270. {
  2271. $this->_checkZf2794();
  2272. $this->testFormDecoratorRegisteredByDefault();
  2273. $this->form->removeDecorator('form');
  2274. $this->assertFalse($this->form->getDecorator('form'));
  2275. }
  2276. /**
  2277. * @group ZF-3069
  2278. */
  2279. public function testRemovingNamedDecoratorShouldWork()
  2280. {
  2281. $this->_checkZf2794();
  2282. $this->form->setDecorators(array(
  2283. 'FormElements',
  2284. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2285. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2286. ));
  2287. $decorators = $this->form->getDecorators();
  2288. $this->assertTrue(array_key_exists('div', $decorators));
  2289. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2290. $this->form->removeDecorator('div');
  2291. $decorators = $this->form->getDecorators();
  2292. $this->assertFalse(array_key_exists('div', $decorators));
  2293. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2294. }
  2295. public function testCanClearAllDecorators()
  2296. {
  2297. $this->_checkZf2794();
  2298. $this->testCanAddMultipleDecorators();
  2299. $this->form->clearDecorators();
  2300. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2301. $this->assertFalse($this->form->getDecorator('fieldset'));
  2302. }
  2303. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2304. {
  2305. $this->_checkZf2794();
  2306. $this->form->setDecorators(array(
  2307. array('HtmlTag', array('tag' => 'div')),
  2308. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2309. ));
  2310. $decorator = $this->form->getDecorator('FooBar');
  2311. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2312. $this->assertEquals('dd', $decorator->getOption('tag'));
  2313. $decorator = $this->form->getDecorator('HtmlTag');
  2314. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2315. $this->assertEquals('div', $decorator->getOption('tag'));
  2316. }
  2317. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2318. {
  2319. $this->form->setDecorators(array(
  2320. 'FormElements',
  2321. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2322. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2323. 'Form',
  2324. ));
  2325. $decorator = $this->form->getDecorator('fieldset');
  2326. $decorators = $this->form->getDecorators();
  2327. $i = 0;
  2328. $order = array();
  2329. foreach (array_keys($decorators) as $name) {
  2330. $order[$name] = $i;
  2331. ++$i;
  2332. }
  2333. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2334. }
  2335. // Rendering
  2336. public function checkMarkup($html)
  2337. {
  2338. $this->assertFalse(empty($html));
  2339. $this->assertContains('<form', $html);
  2340. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2341. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2342. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2343. $this->assertContains('</form>', $html);
  2344. }
  2345. public function testRenderReturnsMarkup()
  2346. {
  2347. $this->setupElements();
  2348. $html = $this->form->render($this->getView());
  2349. $this->checkMarkup($html);
  2350. }
  2351. public function testRenderReturnsMarkupRepresentingAllElements()
  2352. {
  2353. $this->testRenderReturnsMarkup();
  2354. $html = $this->form->render();
  2355. foreach ($this->form->getElements() as $key => $element) {
  2356. $this->assertFalse(empty($key));
  2357. $this->assertFalse(is_numeric($key));
  2358. $this->assertContains('<input', $html);
  2359. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2360. }
  2361. }
  2362. public function testRenderReturnsMarkupContainingSubForms()
  2363. {
  2364. $this->setupElements();
  2365. $this->setupSubForm();
  2366. $this->form->setView($this->getView());
  2367. $html = $this->form->render();
  2368. $this->assertRegexp('/<fieldset/', $html);
  2369. $this->assertContains('</fieldset>', $html);
  2370. foreach ($this->form->sub as $key => $item) {
  2371. $this->assertFalse(empty($key));
  2372. $this->assertFalse(is_numeric($key));
  2373. $this->assertContains('<input', $html);
  2374. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2375. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2376. }
  2377. }
  2378. public function testRenderReturnsMarkupContainingDisplayGroups()
  2379. {
  2380. $this->setupElements();
  2381. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2382. $this->form->setView($this->getView());
  2383. $html = $this->html = $this->form->render();
  2384. $this->assertRegexp('/<fieldset/', $html);
  2385. $this->assertContains('</fieldset>', $html);
  2386. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2387. $dom = new DOMDocument();
  2388. $dom->loadHTML($html);
  2389. $fieldsets = $dom->getElementsByTagName('fieldset');
  2390. $this->assertTrue(0 < $fieldsets->length);
  2391. $fieldset = $fieldsets->item(0);
  2392. $nodes = $fieldset->childNodes;
  2393. $this->assertNotNull($nodes);
  2394. for ($i = 0; $i < $nodes->length; ++$i) {
  2395. $node = $nodes->item($i);
  2396. if ('input' != $node->nodeName) {
  2397. continue;
  2398. }
  2399. $this->assertTrue($node->hasAttribute('name'));
  2400. $nameNode = $node->getAttributeNode('name');
  2401. switch ($i) {
  2402. case 0:
  2403. $this->assertEquals('foo', $nameNode->nodeValue);
  2404. break;
  2405. case 1:
  2406. $this->assertEquals('baz', $nameNode->nodeValue);
  2407. break;
  2408. default:
  2409. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2410. }
  2411. }
  2412. }
  2413. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2414. {
  2415. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2416. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2417. $this->fail("Should find foo element in rendered form");
  2418. }
  2419. $this->assertEquals(1, count($matches));
  2420. $this->assertEquals(1, count($matches[0]));
  2421. }
  2422. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2423. {
  2424. $this->setupElements();
  2425. $this->form->setElementsBelongTo('anArray');
  2426. $html = $this->form->render($this->getView());
  2427. $this->assertContains('name="anArray[foo]"', $html);
  2428. $this->assertContains('name="anArray[bar]"', $html);
  2429. $this->assertContains('name="anArray[baz]"', $html);
  2430. $this->assertContains('id="anArray-foo"', $html);
  2431. $this->assertContains('id="anArray-bar"', $html);
  2432. $this->assertContains('id="anArray-baz"', $html);
  2433. }
  2434. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2435. {
  2436. $this->setupElements();
  2437. $this->form->setElementsBelongTo('data[foo]');
  2438. $html = $this->form->render($this->getView());
  2439. $this->assertContains('name="data[foo][foo]"', $html);
  2440. $this->assertContains('name="data[foo][bar]"', $html);
  2441. $this->assertContains('name="data[foo][baz]"', $html);
  2442. $this->assertContains('id="data-foo-foo"', $html);
  2443. $this->assertContains('id="data-foo-bar"', $html);
  2444. $this->assertContains('id="data-foo-baz"', $html);
  2445. }
  2446. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2447. {
  2448. $this->setupElements();
  2449. $this->form->setName('data')
  2450. ->setIsArray(true);
  2451. $html = $this->form->render($this->getView());
  2452. $this->assertContains('name="data[foo]"', $html);
  2453. $this->assertContains('name="data[bar]"', $html);
  2454. $this->assertContains('name="data[baz]"', $html);
  2455. $this->assertContains('id="data-foo"', $html);
  2456. $this->assertContains('id="data-bar"', $html);
  2457. $this->assertContains('id="data-baz"', $html);
  2458. }
  2459. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2460. {
  2461. $this->form->setName('data')
  2462. ->setIsArray(true);
  2463. $subForm = new Zend_Form_SubForm();
  2464. $subForm->setElementsBelongTo('billing[info]');
  2465. $subForm->addElement('text', 'name');
  2466. $subForm->addElement('text', 'number');
  2467. $this->form->addSubForm($subForm, 'sub');
  2468. $html = $this->form->render($this->getView());
  2469. $this->assertContains('name="data[billing][info][name]', $html);
  2470. $this->assertContains('name="data[billing][info][number]', $html);
  2471. $this->assertContains('id="data-billing-info-name"', $html);
  2472. $this->assertContains('id="data-billing-info-number"', $html);
  2473. }
  2474. public function testToStringProxiesToRender()
  2475. {
  2476. $this->setupElements();
  2477. $this->form->setView($this->getView());
  2478. $html = $this->form->__toString();
  2479. $this->checkMarkup($html);
  2480. }
  2481. public function raiseDecoratorException($content, $element, $options)
  2482. {
  2483. throw new Exception('Raising exception in decorator callback');
  2484. }
  2485. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2486. {
  2487. $this->error = $errstr;
  2488. }
  2489. public function testToStringRaisesErrorWhenExceptionCaught()
  2490. {
  2491. $this->form->setDecorators(array(
  2492. array(
  2493. 'decorator' => 'Callback',
  2494. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2495. ),
  2496. ));
  2497. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2498. $text = $this->form->__toString();
  2499. restore_error_handler();
  2500. $this->assertTrue(empty($text));
  2501. $this->assertTrue(isset($this->error));
  2502. $this->assertContains('Raising exception in decorator callback', $this->error);
  2503. }
  2504. /**
  2505. * ZF-2718
  2506. */
  2507. public function testHiddenElementsGroupedWhenRendered()
  2508. {
  2509. $this->markTestIncomplete('Scheduling for future release');
  2510. $this->form->addElements(array(
  2511. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2512. array('type' => 'text', 'name' => 'testone'),
  2513. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2514. array('type' => 'text', 'name' => 'testtwo'),
  2515. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2516. array('type' => 'text', 'name' => 'testthree'),
  2517. ));
  2518. $html = $this->form->render($this->getView());
  2519. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2520. $this->fail('Hidden elements should be grouped');
  2521. }
  2522. foreach (array('first', 'second', 'third') as $which) {
  2523. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2524. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2525. }
  2526. }
  2527. // Localization
  2528. public function testTranslatorIsNullByDefault()
  2529. {
  2530. $this->assertNull($this->form->getTranslator());
  2531. }
  2532. public function testCanSetTranslator()
  2533. {
  2534. require_once 'Zend/Translate/Adapter/Array.php';
  2535. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2536. $this->form->setTranslator($translator);
  2537. $received = $this->form->getTranslator($translator);
  2538. $this->assertSame($translator->getAdapter(), $received);
  2539. }
  2540. public function testCanSetDefaultGlobalTranslator()
  2541. {
  2542. $this->assertNull($this->form->getTranslator());
  2543. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2544. Zend_Form::setDefaultTranslator($translator);
  2545. $received = Zend_Form::getDefaultTranslator();
  2546. $this->assertSame($translator->getAdapter(), $received);
  2547. $received = $this->form->getTranslator();
  2548. $this->assertSame($translator->getAdapter(), $received);
  2549. $form = new Zend_Form();
  2550. $received = $form->getTranslator();
  2551. $this->assertSame($translator->getAdapter(), $received);
  2552. }
  2553. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2554. {
  2555. $this->assertNull($this->form->getTranslator());
  2556. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2557. Zend_Form::setDefaultTranslator($translatorDefault);
  2558. $received = $this->form->getTranslator();
  2559. $this->assertSame($translatorDefault->getAdapter(), $received);
  2560. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2561. $this->form->setTranslator($translator);
  2562. $received = $this->form->getTranslator();
  2563. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2564. $this->assertSame($translator->getAdapter(), $received);
  2565. }
  2566. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2567. {
  2568. $this->assertNull($this->form->getTranslator());
  2569. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2570. Zend_Registry::set('Zend_Translate', $translator);
  2571. $received = Zend_Form::getDefaultTranslator();
  2572. $this->assertSame($translator->getAdapter(), $received);
  2573. $received = $this->form->getTranslator();
  2574. $this->assertSame($translator->getAdapter(), $received);
  2575. $form = new Zend_Form();
  2576. $received = $form->getTranslator();
  2577. $this->assertSame($translator->getAdapter(), $received);
  2578. }
  2579. public function testCanDisableTranslation()
  2580. {
  2581. $this->testCanSetDefaultGlobalTranslator();
  2582. $this->form->setDisableTranslator(true);
  2583. $this->assertNull($this->form->getTranslator());
  2584. }
  2585. // Iteration
  2586. public function testFormObjectIsIterableAndIteratesElements()
  2587. {
  2588. $this->setupElements();
  2589. $expected = array('foo', 'bar', 'baz');
  2590. $received = array();
  2591. foreach ($this->form as $key => $value) {
  2592. $received[] = $key;
  2593. }
  2594. $this->assertSame($expected, $received);
  2595. }
  2596. public function testFormObjectIteratesElementsInExpectedOrder()
  2597. {
  2598. $this->setupElements();
  2599. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2600. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2601. $received = array();
  2602. foreach ($this->form as $key => $value) {
  2603. $received[] = $key;
  2604. $this->assertTrue($value instanceof Zend_Form_Element);
  2605. }
  2606. $this->assertSame($expected, $received);
  2607. }
  2608. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2609. {
  2610. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2611. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2612. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2613. $expected = array('name', 'email', 'submit');
  2614. $received = array();
  2615. foreach ($this->form as $key => $value) {
  2616. $received[] = $key;
  2617. $this->assertTrue($value instanceof Zend_Form_Element);
  2618. }
  2619. $this->assertSame($expected, $received);
  2620. }
  2621. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2622. {
  2623. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2624. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2625. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2626. ->setView($this->getView());
  2627. $test = $this->form->render();
  2628. $this->assertContains('name="a"', $test);
  2629. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2630. $this->fail('Expected markup not found');
  2631. }
  2632. $order = array();
  2633. foreach ($matches[1] as $element) {
  2634. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2635. $order[] = $m[1];
  2636. }
  2637. }
  2638. $this->assertSame(array('b', 'c', 'a'), $order);
  2639. }
  2640. public function testFormObjectIteratesElementsAndSubforms()
  2641. {
  2642. $this->setupElements();
  2643. $this->setupSubForm();
  2644. $expected = array('foo', 'bar', 'baz', 'sub');
  2645. $received = array();
  2646. foreach ($this->form as $key => $value) {
  2647. $received[] = $key;
  2648. $this->assertTrue(($value instanceof Zend_Form_Element)
  2649. or ($value instanceof Zend_Form_SubForm));
  2650. }
  2651. $this->assertSame($expected, $received);
  2652. }
  2653. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  2654. {
  2655. $this->setupElements();
  2656. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2657. $expected = array('bar', 'foobaz');
  2658. $received = array();
  2659. foreach ($this->form as $key => $value) {
  2660. $received[] = $key;
  2661. $this->assertTrue(($value instanceof Zend_Form_Element)
  2662. or ($value instanceof Zend_Form_DisplayGroup));
  2663. }
  2664. $this->assertSame($expected, $received);
  2665. }
  2666. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  2667. {
  2668. $this->setupElements();
  2669. $bar = $this->form->bar;
  2670. $this->form->removeElement('bar');
  2671. try {
  2672. foreach ($this->form as $item) {
  2673. }
  2674. } catch (Exception $e) {
  2675. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2676. }
  2677. $this->form->addElement($bar);
  2678. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  2679. $this->form->removeDisplayGroup('bazbar');
  2680. try {
  2681. foreach ($this->form as $item) {
  2682. }
  2683. } catch (Exception $e) {
  2684. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2685. }
  2686. $subForm = new Zend_Form_SubForm;
  2687. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  2688. $this->form->addSubForm($subForm, 'page1');
  2689. $this->form->removeSubForm('page1');
  2690. try {
  2691. foreach ($this->form as $item) {
  2692. }
  2693. } catch (Exception $e) {
  2694. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2695. }
  2696. }
  2697. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  2698. {
  2699. $form = new Zend_Form();
  2700. $form->addElements(array(
  2701. 'username' => 'text',
  2702. 'password' => 'text',
  2703. ));
  2704. $form->clearElements();
  2705. try {
  2706. foreach ($form as $item) {
  2707. }
  2708. } catch (Zend_Form_Exception $e) {
  2709. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  2710. . $e->getMessage();
  2711. $this->fail($message);
  2712. }
  2713. $form->addElements(array(
  2714. 'username' => 'text',
  2715. 'password' => 'text',
  2716. ))
  2717. ->addDisplayGroup(array('username', 'password'), 'login');
  2718. $form->clearDisplayGroups();
  2719. try {
  2720. foreach ($form as $item) {
  2721. }
  2722. } catch (Zend_Form_Exception $e) {
  2723. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  2724. . $e->getMessage();
  2725. $this->fail($message);
  2726. }
  2727. $subForm = new Zend_Form_SubForm();
  2728. $form->addSubForm($subForm, 'foo');
  2729. $form->clearSubForms();
  2730. try {
  2731. foreach ($form as $item) {
  2732. }
  2733. } catch (Zend_Form_Exception $e) {
  2734. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  2735. . $e->getMessage();
  2736. $this->fail($message);
  2737. }
  2738. }
  2739. // Countable
  2740. public function testCanCountFormObject()
  2741. {
  2742. $this->setupElements();
  2743. $this->assertEquals(3, count($this->form));
  2744. }
  2745. public function testCountingFormObjectCountsSubForms()
  2746. {
  2747. $this->setupElements();
  2748. $this->setupSubForm();
  2749. $this->assertEquals(4, count($this->form));
  2750. }
  2751. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  2752. {
  2753. $this->testCountingFormObjectCountsSubForms();
  2754. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2755. $this->assertEquals(3, count($this->form));
  2756. }
  2757. // Element decorators and plugin paths
  2758. public function testCanSetAllElementDecoratorsAtOnce()
  2759. {
  2760. $this->_checkZf2794();
  2761. $this->setupElements();
  2762. $this->form->setElementDecorators(array(
  2763. array('ViewHelper'),
  2764. array('Label'),
  2765. array('Fieldset'),
  2766. ));
  2767. foreach ($this->form->getElements() as $element) {
  2768. $this->assertFalse($element->getDecorator('Errors'));
  2769. $this->assertFalse($element->getDecorator('HtmlTag'));
  2770. $decorator = $element->getDecorator('ViewHelper');
  2771. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2772. $decorator = $element->getDecorator('Label');
  2773. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  2774. $decorator = $element->getDecorator('Fieldset');
  2775. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2776. }
  2777. }
  2778. /**
  2779. * @group ZF-3597
  2780. */
  2781. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  2782. {
  2783. $this->form->setDecorators(array(
  2784. 'FormElements',
  2785. array('HtmlTag', array('tag' => 'table')),
  2786. 'Form',
  2787. ));
  2788. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  2789. $this->form->addElement('text', 'test', array(
  2790. 'label' => 'Foo',
  2791. 'description' => 'sample description',
  2792. ));
  2793. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  2794. $decorator = new My_Decorator_TableRow();
  2795. $this->form->setElementDecorators(array(
  2796. 'ViewHelper',
  2797. $decorator,
  2798. ));
  2799. $html = $this->form->render($this->getView());
  2800. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  2801. }
  2802. /**#@+
  2803. * @group ZF-3228
  2804. */
  2805. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  2806. {
  2807. $this->_checkZf2794();
  2808. $this->setupElements();
  2809. $this->form->setElementDecorators(
  2810. array(
  2811. 'Description',
  2812. 'Form',
  2813. 'Fieldset',
  2814. ),
  2815. array(
  2816. 'bar',
  2817. )
  2818. );
  2819. $element = $this->form->bar;
  2820. $this->assertFalse($element->getDecorator('ViewHelper'));
  2821. $this->assertFalse($element->getDecorator('Errors'));
  2822. $this->assertFalse($element->getDecorator('Label'));
  2823. $this->assertFalse($element->getDecorator('HtmlTag'));
  2824. $decorator = $element->getDecorator('Description');
  2825. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2826. $decorator = $element->getDecorator('Form');
  2827. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2828. $decorator = $element->getDecorator('Fieldset');
  2829. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2830. foreach (array('foo', 'baz') as $name) {
  2831. $element = $this->form->$name;
  2832. $this->assertFalse($element->getDecorator('Form'));
  2833. $this->assertFalse($element->getDecorator('Fieldset'));
  2834. }
  2835. }
  2836. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  2837. {
  2838. $this->_checkZf2794();
  2839. $this->setupElements();
  2840. $this->form->setElementDecorators(
  2841. array(
  2842. 'Description',
  2843. 'Form',
  2844. 'Fieldset',
  2845. ),
  2846. array(
  2847. 'foo',
  2848. 'baz',
  2849. ),
  2850. false
  2851. );
  2852. $element = $this->form->bar;
  2853. $this->assertFalse($element->getDecorator('ViewHelper'));
  2854. $this->assertFalse($element->getDecorator('Errors'));
  2855. $this->assertFalse($element->getDecorator('Label'));
  2856. $this->assertFalse($element->getDecorator('HtmlTag'));
  2857. $decorator = $element->getDecorator('Description');
  2858. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2859. $decorator = $element->getDecorator('Form');
  2860. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2861. $decorator = $element->getDecorator('Fieldset');
  2862. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2863. foreach (array('foo', 'baz') as $name) {
  2864. $element = $this->form->$name;
  2865. $this->assertFalse($element->getDecorator('Form'));
  2866. $this->assertFalse($element->getDecorator('Fieldset'));
  2867. }
  2868. }
  2869. /**#@-*/
  2870. public function testCanSetAllElementFiltersAtOnce()
  2871. {
  2872. $this->_checkZf2794();
  2873. $this->setupElements();
  2874. $this->form->setElementFilters(array(
  2875. 'Alnum',
  2876. 'StringToLower'
  2877. ));
  2878. foreach ($this->form->getElements() as $element) {
  2879. $filter = $element->getFilter('Alnum');
  2880. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  2881. $filter = $element->getFilter('StringToLower');
  2882. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  2883. }
  2884. }
  2885. public function testCanSetGlobalElementPrefixPath()
  2886. {
  2887. $this->setupElements();
  2888. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2889. $this->form->addElement('text', 'prefixTest');
  2890. foreach ($this->form->getElements() as $element) {
  2891. $loader = $element->getPluginLoader('validate');
  2892. $paths = $loader->getPaths('Zend_Foo_Validate');
  2893. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  2894. $this->assertContains('Foo', $paths[0]);
  2895. $this->assertContains('Validate', $paths[0]);
  2896. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  2897. $this->assertFalse(empty($paths));
  2898. $this->assertContains('Foo', $paths[0]);
  2899. $this->assertContains('Filter', $paths[0]);
  2900. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  2901. $this->assertFalse(empty($paths));
  2902. $this->assertContains('Foo', $paths[0]);
  2903. $this->assertContains('Decorator', $paths[0]);
  2904. }
  2905. }
  2906. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  2907. {
  2908. $this->_checkZf2794();
  2909. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  2910. $this->form->addElement('text', 'prefixTest');
  2911. $element = $this->form->prefixTest;
  2912. $label = $element->getDecorator('Label');
  2913. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  2914. }
  2915. /**
  2916. * @group ZF-3093
  2917. */
  2918. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  2919. {
  2920. $subForm = new Zend_Form_SubForm();
  2921. $subForm->addElement('text', 'foo');
  2922. $this->form->addSubForm($subForm, 'subForm');
  2923. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2924. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  2925. $paths = $loader->getPaths('Zend_Foo_Decorator');
  2926. $this->assertFalse(empty($paths));
  2927. $this->assertContains('Foo', $paths[0]);
  2928. $this->assertContains('Decorator', $paths[0]);
  2929. }
  2930. public function testCanSetElementValidatorPrefixPath()
  2931. {
  2932. $this->setupElements();
  2933. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  2934. $this->form->addElement('text', 'prefixTest');
  2935. foreach ($this->form->getElements() as $element) {
  2936. $loader = $element->getPluginLoader('validate');
  2937. $paths = $loader->getPaths('Zend_Foo');
  2938. $this->assertFalse(empty($paths));
  2939. $this->assertContains('Foo', $paths[0]);
  2940. $this->assertNotContains('Validate', $paths[0]);
  2941. }
  2942. }
  2943. public function testCanSetElementFilterPrefixPath()
  2944. {
  2945. $this->setupElements();
  2946. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  2947. $this->form->addElement('text', 'prefixTest');
  2948. foreach ($this->form->getElements() as $element) {
  2949. $loader = $element->getPluginLoader('filter');
  2950. $paths = $loader->getPaths('Zend_Foo');
  2951. $this->assertFalse(empty($paths));
  2952. $this->assertContains('Foo', $paths[0]);
  2953. $this->assertNotContains('Filter', $paths[0]);
  2954. }
  2955. }
  2956. public function testCanSetElementDecoratorPrefixPath()
  2957. {
  2958. $this->setupElements();
  2959. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  2960. $this->form->addElement('text', 'prefixTest');
  2961. foreach ($this->form->getElements() as $element) {
  2962. $loader = $element->getPluginLoader('decorator');
  2963. $paths = $loader->getPaths('Zend_Foo');
  2964. $this->assertFalse(empty($paths));
  2965. $this->assertContains('Foo', $paths[0]);
  2966. $this->assertNotContains('Decorator', $paths[0]);
  2967. }
  2968. }
  2969. // Display Group decorators and plugin paths
  2970. public function setupDisplayGroups()
  2971. {
  2972. $this->testCanAddAndRetrieveMultipleElements();
  2973. $this->form->addElements(array(
  2974. 'test1' => 'text',
  2975. 'test2' => 'text',
  2976. 'test3' => 'text',
  2977. 'test4' => 'text'
  2978. ));
  2979. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  2980. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2981. }
  2982. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  2983. {
  2984. $this->_checkZf2794();
  2985. $this->setupDisplayGroups();
  2986. $this->form->setDisplayGroupDecorators(array(
  2987. array('Callback', array('callback' => 'strip_tags')),
  2988. ));
  2989. foreach ($this->form->getDisplayGroups() as $element) {
  2990. $this->assertFalse($element->getDecorator('FormElements'));
  2991. $this->assertFalse($element->getDecorator('HtmlTag'));
  2992. $this->assertFalse($element->getDecorator('Fieldset'));
  2993. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  2994. $decorator = $element->getDecorator('Callback');
  2995. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  2996. }
  2997. }
  2998. public function testCanSetDisplayGroupPrefixPath()
  2999. {
  3000. $this->setupDisplayGroups();
  3001. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3002. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3003. foreach ($this->form->getDisplayGroups() as $group) {
  3004. $loader = $group->getPluginLoader();
  3005. $paths = $loader->getPaths('Zend_Foo');
  3006. $this->assertFalse(empty($paths));
  3007. $this->assertContains('Foo', $paths[0]);
  3008. }
  3009. }
  3010. /**
  3011. * @group ZF-3213
  3012. */
  3013. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3014. {
  3015. require_once 'Zend/Config/Ini.php';
  3016. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3017. $form = new Zend_Form($config);
  3018. $dg = $form->foofoo;
  3019. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3020. $this->assertTrue($paths !== false);
  3021. }
  3022. // Subform decorators
  3023. public function testCanSetAllSubFormDecoratorsAtOnce()
  3024. {
  3025. $this->_checkZf2794();
  3026. $this->setupSubForm();
  3027. $this->form->setSubFormDecorators(array(
  3028. array('Callback', array('callback' => 'strip_tags')),
  3029. ));
  3030. foreach ($this->form->getSubForms() as $subForm) {
  3031. $this->assertFalse($subForm->getDecorator('FormElements'));
  3032. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3033. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3034. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3035. $decorator = $subForm->getDecorator('Callback');
  3036. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3037. }
  3038. }
  3039. // Extension
  3040. public function testInitCalledPriorToLoadingDefaultDecorators()
  3041. {
  3042. $form = new Zend_Form_FormTest_FormExtension();
  3043. $decorators = $form->getDecorators();
  3044. $this->assertTrue(empty($decorators));
  3045. }
  3046. // Clone
  3047. /**
  3048. * @group ZF-3819
  3049. */
  3050. public function testCloningShouldCloneAllChildren()
  3051. {
  3052. $form = new Zend_Form();
  3053. $foo = new Zend_Form_SubForm(array(
  3054. 'name' => 'foo',
  3055. 'elements' => array(
  3056. 'one' => 'text',
  3057. 'two' => 'text',
  3058. ),
  3059. ));
  3060. $form->addElement('text', 'bar')
  3061. ->addElement('text', 'baz')
  3062. ->addElement('text', 'bat')
  3063. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3064. ->addSubForm($foo, 'foo');
  3065. $bar = $form->bar;
  3066. $baz = $form->baz;
  3067. $bat = $form->bat;
  3068. $barbat = $form->barbat;
  3069. $cloned = clone $form;
  3070. $this->assertNotSame($foo, $cloned->foo);
  3071. $this->assertNotSame($bar, $cloned->bar);
  3072. $this->assertNotSame($baz, $cloned->baz);
  3073. $this->assertNotSame($bat, $cloned->bat);
  3074. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3075. $this->assertNotSame($foo->one, $cloned->foo->one);
  3076. $this->assertNotSame($foo->two, $cloned->foo->two);
  3077. }
  3078. // Reset
  3079. /**
  3080. * @group ZF-3227
  3081. */
  3082. public function testFormsShouldAllowResetting()
  3083. {
  3084. $form = new Zend_Form();
  3085. $foo = new Zend_Form_SubForm(array(
  3086. 'name' => 'foo',
  3087. 'elements' => array(
  3088. 'one' => 'text',
  3089. 'two' => 'text',
  3090. ),
  3091. ));
  3092. $form->addElement('text', 'bar')
  3093. ->addElement('text', 'baz')
  3094. ->addElement('text', 'bat')
  3095. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3096. ->addSubForm($foo, 'foo');
  3097. $values = array(
  3098. 'bar' => 'Bar Value',
  3099. 'baz' => 'Baz Value',
  3100. 'bat' => 'Bat Value',
  3101. 'foo' => array(
  3102. 'one' => 'One Value',
  3103. 'two' => 'Two Value',
  3104. ),
  3105. );
  3106. $form->populate($values);
  3107. $test = $form->getValues();
  3108. $this->assertEquals($values, $test);
  3109. $form->reset();
  3110. $test = $form->getValues();
  3111. $this->assertNotEquals($values, $test);
  3112. $this->assertEquals(0, array_sum($test));
  3113. }
  3114. /**
  3115. * @group ZF-3217
  3116. */
  3117. public function testFormShouldOverloadToRenderDecorators()
  3118. {
  3119. $this->setupElements();
  3120. $this->form->setView($this->getView());
  3121. $html = $this->form->renderFormElements();
  3122. foreach ($this->form->getElements() as $element) {
  3123. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3124. }
  3125. $this->assertNotContains('<dl', $html);
  3126. $this->assertNotContains('<form', $html);
  3127. $html = $this->form->renderForm('this is the content');
  3128. $this->assertContains('<form', $html);
  3129. $this->assertContains('</form>', $html);
  3130. $this->assertContains('this is the content', $html);
  3131. }
  3132. /**
  3133. * @group ZF-3217
  3134. * @expectedException Zend_Form_Exception
  3135. */
  3136. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3137. {
  3138. $html = $this->form->bogusMethodCall();
  3139. }
  3140. /**
  3141. * @group ZF-2950
  3142. */
  3143. public function testDtDdElementsWithLabelGetUniqueId()
  3144. {
  3145. $form = new Zend_Form();
  3146. $form->setView($this->getView());
  3147. $fooElement = new Zend_Form_Element_Text('foo');
  3148. $fooElement->setLabel('Foo');
  3149. $form->addElement($fooElement);
  3150. $html = $form->render();
  3151. $this->assertContains('<dt id="foo-label">', $html);
  3152. $this->assertContains('<dd id="foo-element">', $html);
  3153. }
  3154. /**
  3155. * @group ZF-2950
  3156. */
  3157. public function testDtDdElementsWithoutLabelGetUniqueId()
  3158. {
  3159. $form = new Zend_Form();
  3160. $form->setView($this->getView())
  3161. ->addElement(new Zend_Form_Element_Text('foo'));
  3162. $html = $form->render();
  3163. $this->assertContains('<dt id="foo-label">&nbsp;</dt>', $html);
  3164. $this->assertContains('<dd id="foo-element">', $html);
  3165. }
  3166. /**
  3167. * @group ZF-2950
  3168. */
  3169. public function testSubFormGetsUniqueIdWithName()
  3170. {
  3171. $form = new Zend_Form();
  3172. $form->setView($this->getView())
  3173. ->setName('testform')
  3174. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3175. $html = $form->render();
  3176. $this->assertContains('<dt id="testform-label">&nbsp;</dt>', $html);
  3177. $this->assertContains('<dd id="testform-element">', $html);
  3178. }
  3179. /**
  3180. * @group ZF-5370
  3181. */
  3182. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3183. {
  3184. $file = new Zend_Form_Element_File('txt');
  3185. $this->form->addElement($file);
  3186. $html = $this->form->render($this->getView());
  3187. $this->assertFalse(empty($html));
  3188. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3189. }
  3190. /**
  3191. * @group ZF-5370
  3192. */
  3193. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3194. {
  3195. $subForm = new Zend_Form_SubForm();
  3196. $subForm->addElement('file', 'txt');
  3197. $this->form->addSubForm($subForm, 'page1')
  3198. ->setView(new Zend_View);
  3199. $html = $this->form->render();
  3200. $this->assertContains('id="txt"', $html);
  3201. $this->assertContains('name="txt"', $html);
  3202. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3203. }
  3204. /**
  3205. * @group ZF-5370
  3206. */
  3207. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3208. {
  3209. $this->form->addElement('file', 'txt')
  3210. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3211. ->setView(new Zend_View);
  3212. $html = $this->form->render();
  3213. $this->assertContains('id="txt"', $html);
  3214. $this->assertContains('name="txt"', $html);
  3215. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3216. }
  3217. /**
  3218. * @group ZF-6070
  3219. */
  3220. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3221. {
  3222. $this->form->setOptions(array(
  3223. 'elementDecorators' => array(
  3224. 'ViewHelper',
  3225. 'Label',
  3226. ),
  3227. 'elements' => array(
  3228. 'foo' => array(
  3229. 'type' => 'text',
  3230. 'options' => array(
  3231. 'decorators' => array(
  3232. 'Errors',
  3233. 'ViewHelper',
  3234. ),
  3235. ),
  3236. ),
  3237. ),
  3238. ));
  3239. $element = $this->form->getElement('foo');
  3240. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3241. $actual = array();
  3242. foreach ($element->getDecorators() as $decorator) {
  3243. $actual[] = get_class($decorator);
  3244. }
  3245. $this->assertSame($expected, $actual);
  3246. }
  3247. /**
  3248. * @group ZF-5150
  3249. */
  3250. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3251. {
  3252. $this->form->addError('Error');
  3253. $this->assertFalse($this->form->isValid(array()));
  3254. }
  3255. /**
  3256. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3257. *
  3258. * @link http://framework.zend.com/issues/browse/ZF-2794
  3259. * @return void
  3260. */
  3261. protected function _checkZf2794()
  3262. {
  3263. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3264. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3265. }
  3266. }
  3267. }
  3268. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  3269. {
  3270. }
  3271. class Zend_Form_FormTest_FormExtension extends Zend_Form
  3272. {
  3273. public function init()
  3274. {
  3275. $this->setDisableLoadDefaultDecorators(true);
  3276. }
  3277. }
  3278. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  3279. Zend_Form_FormTest::main();
  3280. }