FormTest.php 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911
  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-2015 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 'Zend/Form.php';
  26. require_once 'Zend/Config.php';
  27. require_once 'Zend/Controller/Action/HelperBroker.php';
  28. require_once 'Zend/Form/Decorator/Form.php';
  29. require_once 'Zend/Form/DisplayGroup.php';
  30. require_once 'Zend/Form/Element.php';
  31. require_once 'Zend/Form/Element/Text.php';
  32. require_once 'Zend/Form/Element/File.php';
  33. require_once 'Zend/Form/SubForm.php';
  34. require_once 'Zend/Loader/PluginLoader.php';
  35. require_once 'Zend/Registry.php';
  36. require_once 'Zend/Translate.php';
  37. require_once 'Zend/View.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Form
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Form
  45. */
  46. class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * @var Zend_Form
  50. */
  51. public $form;
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_FormTest');
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. public function clearRegistry()
  58. {
  59. if (Zend_Registry::isRegistered('Zend_Translate')) {
  60. $registry = Zend_Registry::getInstance();
  61. unset($registry['Zend_Translate']);
  62. }
  63. }
  64. public function setUp()
  65. {
  66. $this->clearRegistry();
  67. Zend_Form::setDefaultTranslator(null);
  68. if (isset($this->error)) {
  69. unset($this->error);
  70. }
  71. Zend_Controller_Action_HelperBroker::resetHelpers();
  72. $this->form = new Zend_Form();
  73. }
  74. public function tearDown()
  75. {
  76. $this->clearRegistry();
  77. }
  78. public function testZendFormImplementsZendValidateInterface()
  79. {
  80. $this->assertTrue($this->form instanceof Zend_Validate_Interface);
  81. }
  82. // Configuration
  83. public function getOptions()
  84. {
  85. $options = array(
  86. 'name' => 'foo',
  87. 'class' => 'someform',
  88. 'action' => '/foo/bar',
  89. 'method' => 'put',
  90. );
  91. return $options;
  92. }
  93. public function testCanSetObjectStateViaSetOptions()
  94. {
  95. $options = $this->getOptions();
  96. $this->form->setOptions($options);
  97. $this->assertEquals('foo', $this->form->getName());
  98. $this->assertEquals('someform', $this->form->getAttrib('class'));
  99. $this->assertEquals('/foo/bar', $this->form->getAction());
  100. $this->assertEquals('put', $this->form->getMethod());
  101. }
  102. public function testCanSetObjectStateByPassingOptionsToConstructor()
  103. {
  104. $options = $this->getOptions();
  105. $form = new Zend_Form($options);
  106. $this->assertEquals('foo', $form->getName());
  107. $this->assertEquals('someform', $form->getAttrib('class'));
  108. $this->assertEquals('/foo/bar', $form->getAction());
  109. $this->assertEquals('put', $form->getMethod());
  110. }
  111. public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
  112. {
  113. $options = $this->getOptions();
  114. $config = new Zend_Config($options);
  115. $options['config'] = $config;
  116. $options['options'] = $config->toArray();
  117. $this->form->setOptions($options);
  118. }
  119. public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNonObjectPassed()
  120. {
  121. $options = $this->getOptions();
  122. $options['pluginLoader'] = true;
  123. $options['view'] = true;
  124. $options['translator'] = true;
  125. $options['default'] = true;
  126. $options['attrib'] = true;
  127. $this->form->setOptions($options);
  128. }
  129. public function testSetOptionsWithAttribsDoesNotOverwriteActionOrMethodOrName()
  130. {
  131. $attribs = $this->getOptions();
  132. unset($attribs['action'], $attribs['method']);
  133. $options = array(
  134. 'name' => 'MYFORM',
  135. 'action' => '/bar/baz',
  136. 'method' => 'GET',
  137. 'attribs' => $attribs,
  138. );
  139. $form = new Zend_Form($options);
  140. $this->assertEquals($options['name'], $form->getName());
  141. $this->assertEquals($options['action'], $form->getAction());
  142. $this->assertEquals(strtolower($options['method']), strtolower($form->getMethod()));
  143. }
  144. public function getElementOptions()
  145. {
  146. $elements = array(
  147. 'foo' => 'text',
  148. array('text', 'bar', array('class' => 'foobar')),
  149. array(
  150. 'options' => array('class' => 'barbaz'),
  151. 'type' => 'text',
  152. 'name' => 'baz',
  153. ),
  154. 'bat' => array(
  155. 'options' => array('class' => 'bazbat'),
  156. 'type' => 'text',
  157. ),
  158. 'lol' => array(
  159. 'text',
  160. array('class' => 'lolcat'),
  161. )
  162. );
  163. return $elements;
  164. }
  165. public function testSetOptionsSetsElements()
  166. {
  167. $options = $this->getOptions();
  168. $options['elements'] = $this->getElementOptions();
  169. $this->form->setOptions($options);
  170. $this->assertTrue(isset($this->form->foo));
  171. $this->assertTrue($this->form->foo instanceof Zend_Form_Element_Text);
  172. $this->assertTrue(isset($this->form->bar));
  173. $this->assertTrue($this->form->bar instanceof Zend_Form_Element_Text);
  174. $this->assertEquals('foobar', $this->form->bar->class);
  175. $this->assertTrue(isset($this->form->baz));
  176. $this->assertTrue($this->form->baz instanceof Zend_Form_Element_Text);
  177. $this->assertEquals('barbaz', $this->form->baz->class);
  178. $this->assertTrue(isset($this->form->bat));
  179. $this->assertTrue($this->form->bat instanceof Zend_Form_Element_Text);
  180. $this->assertEquals('bazbat', $this->form->bat->class);
  181. $this->assertTrue(isset($this->form->lol));
  182. $this->assertTrue($this->form->lol instanceof Zend_Form_Element_Text);
  183. $this->assertEquals('lolcat', $this->form->lol->class);
  184. }
  185. public function testSetOptionsSetsDefaultValues()
  186. {
  187. $options = $this->getOptions();
  188. $options['defaults'] = array(
  189. 'bar' => 'barvalue',
  190. 'bat' => 'batvalue',
  191. );
  192. $options['elements'] = $this->getElementOptions();
  193. $this->form->setOptions($options);
  194. $this->assertEquals('barvalue', $this->form->bar->getValue());
  195. $this->assertEquals('batvalue', $this->form->bat->getValue());
  196. }
  197. public function testSetOptionsSetsArrayOfStringDecorators()
  198. {
  199. $this->_checkZf2794();
  200. $options = $this->getOptions();
  201. $options['decorators'] = array('label', 'errors');
  202. $this->form->setOptions($options);
  203. $this->assertFalse($this->form->getDecorator('form'));
  204. $decorator = $this->form->getDecorator('label');
  205. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  206. $decorator = $this->form->getDecorator('errors');
  207. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  208. }
  209. public function testSetOptionsSetsArrayOfArrayDecorators()
  210. {
  211. $this->_checkZf2794();
  212. $options = $this->getOptions();
  213. $options['decorators'] = array(
  214. array('label', array('id' => 'mylabel')),
  215. array('errors', array('id' => 'errors')),
  216. );
  217. $this->form->setOptions($options);
  218. $this->assertFalse($this->form->getDecorator('form'));
  219. $decorator = $this->form->getDecorator('label');
  220. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  221. $options = $decorator->getOptions();
  222. $this->assertEquals('mylabel', $options['id']);
  223. $decorator = $this->form->getDecorator('errors');
  224. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  225. $options = $decorator->getOptions();
  226. $this->assertEquals('errors', $options['id']);
  227. }
  228. public function testSetOptionsSetsArrayOfAssocArrayDecorators()
  229. {
  230. $this->_checkZf2794();
  231. $options = $this->getOptions();
  232. $options['decorators'] = array(
  233. array(
  234. 'options' => array('id' => 'mylabel'),
  235. 'decorator' => 'label',
  236. ),
  237. array(
  238. 'options' => array('id' => 'errors'),
  239. 'decorator' => 'errors',
  240. ),
  241. );
  242. $this->form->setOptions($options);
  243. $this->assertFalse($this->form->getDecorator('form'));
  244. $decorator = $this->form->getDecorator('label');
  245. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  246. $options = $decorator->getOptions();
  247. $this->assertEquals('mylabel', $options['id']);
  248. $decorator = $this->form->getDecorator('errors');
  249. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
  250. $options = $decorator->getOptions();
  251. $this->assertEquals('errors', $options['id']);
  252. }
  253. public function testSetOptionsSetsGlobalPrefixPaths()
  254. {
  255. $options = $this->getOptions();
  256. $options['prefixPath'] = array(
  257. 'prefix' => 'Zend_Foo',
  258. 'path' => 'Zend/Foo/'
  259. );
  260. $this->form->setOptions($options);
  261. foreach (array('element', 'decorator') as $type) {
  262. $loader = $this->form->getPluginLoader($type);
  263. $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
  264. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  265. $this->assertFalse(empty($paths));
  266. $this->assertContains('Foo', $paths[0]);
  267. }
  268. }
  269. public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
  270. {
  271. $options = $this->getOptions();
  272. $options['prefixPath'] = array(
  273. 'element' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  274. );
  275. $this->form->setOptions($options);
  276. $loader = $this->form->getPluginLoader('element');
  277. $paths = $loader->getPaths('Zend_Foo');
  278. $this->assertTrue(is_array($paths));
  279. $this->assertFalse(empty($paths));
  280. $this->assertContains('Foo', $paths[0]);
  281. }
  282. public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
  283. {
  284. $options = $this->getOptions();
  285. $options['prefixPath'] = array(
  286. array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
  287. );
  288. $this->form->setOptions($options);
  289. $loader = $this->form->getPluginLoader('decorator');
  290. $paths = $loader->getPaths('Zend_Foo');
  291. $this->assertTrue(is_array($paths));
  292. $this->assertFalse(empty($paths));
  293. $this->assertContains('Foo', $paths[0]);
  294. }
  295. public function testSetOptionsSetsDisplayGroups()
  296. {
  297. $options = $this->getOptions();
  298. $options['displayGroups'] = array(
  299. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  300. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  301. array(
  302. 'name' => 'ghiabc',
  303. 'elements' => array('ghi', 'abc'),
  304. 'options' => array('order' => 15),
  305. ),
  306. );
  307. $options['elements'] = array(
  308. 'foo' => 'text',
  309. 'bar' => 'text',
  310. 'baz' => 'text',
  311. 'bat' => 'text',
  312. 'abc' => 'text',
  313. 'ghi' => 'text',
  314. 'jkl' => 'text',
  315. 'mno' => 'text',
  316. );
  317. $this->form->setOptions($options);
  318. $this->assertTrue(isset($this->form->barbat));
  319. $elements = $this->form->barbat->getElements();
  320. $expected = array('bar', 'bat');
  321. $this->assertEquals($expected, array_keys($elements));
  322. $this->assertEquals(20, $this->form->barbat->getOrder());
  323. $this->assertTrue(isset($this->form->foobaz));
  324. $elements = $this->form->foobaz->getElements();
  325. $expected = array('foo', 'baz');
  326. $this->assertEquals($expected, array_keys($elements));
  327. $this->assertEquals(10, $this->form->foobaz->getOrder());
  328. $this->assertTrue(isset($this->form->ghiabc));
  329. $elements = $this->form->ghiabc->getElements();
  330. $expected = array('ghi', 'abc');
  331. $this->assertEquals($expected, array_keys($elements));
  332. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  333. }
  334. /**
  335. * @group ZF-3250
  336. */
  337. public function testDisplayGroupOrderInConfigShouldNotMatter()
  338. {
  339. require_once 'Zend/Config/Xml.php';
  340. $config = new Zend_Config_Xml(dirname(__FILE__) . '/_files/config/zf3250.xml', 'sitearea', true);
  341. $form = new Zend_Form($config->test);
  342. // no assertions needed; throws error if order matters
  343. }
  344. /**
  345. * @group ZF-3112
  346. */
  347. public function testSetOptionsShouldCreateDisplayGroupsLast()
  348. {
  349. $options = array();
  350. $options['displayGroups'] = array(
  351. 'barbat' => array(array('bar', 'bat'), array('order' => 20)),
  352. array(array('foo', 'baz'), 'foobaz', array('order' => 10)),
  353. array(
  354. 'name' => 'ghiabc',
  355. 'elements' => array('ghi', 'abc'),
  356. 'options' => array('order' => 15),
  357. ),
  358. );
  359. $options = array_merge($options, $this->getOptions());
  360. $options['elements'] = array(
  361. 'foo' => 'text',
  362. 'bar' => 'text',
  363. 'baz' => 'text',
  364. 'bat' => 'text',
  365. 'abc' => 'text',
  366. 'ghi' => 'text',
  367. 'jkl' => 'text',
  368. 'mno' => 'text',
  369. );
  370. $this->form = new Zend_Form($options);
  371. $this->assertTrue(isset($this->form->barbat));
  372. $elements = $this->form->barbat->getElements();
  373. $expected = array('bar', 'bat');
  374. $this->assertEquals($expected, array_keys($elements));
  375. $this->assertEquals(20, $this->form->barbat->getOrder());
  376. $this->assertTrue(isset($this->form->foobaz));
  377. $elements = $this->form->foobaz->getElements();
  378. $expected = array('foo', 'baz');
  379. $this->assertEquals($expected, array_keys($elements));
  380. $this->assertEquals(10, $this->form->foobaz->getOrder());
  381. $this->assertTrue(isset($this->form->ghiabc));
  382. $elements = $this->form->ghiabc->getElements();
  383. $expected = array('ghi', 'abc');
  384. $this->assertEquals($expected, array_keys($elements));
  385. $this->assertEquals(15, $this->form->ghiabc->getOrder());
  386. }
  387. public function testSetConfigSetsObjectState()
  388. {
  389. $config = new Zend_Config($this->getOptions());
  390. $this->form->setConfig($config);
  391. $this->assertEquals('foo', $this->form->getName());
  392. $this->assertEquals('someform', $this->form->getAttrib('class'));
  393. $this->assertEquals('/foo/bar', $this->form->getAction());
  394. $this->assertEquals('put', $this->form->getMethod());
  395. }
  396. public function testCanSetObjectStateByPassingConfigObjectToConstructor()
  397. {
  398. $config = new Zend_Config($this->getOptions());
  399. $form = new Zend_Form($config);
  400. $this->assertEquals('foo', $form->getName());
  401. $this->assertEquals('someform', $form->getAttrib('class'));
  402. $this->assertEquals('/foo/bar', $form->getAction());
  403. $this->assertEquals('put', $form->getMethod());
  404. }
  405. // Attribs:
  406. public function testAttribsArrayInitiallyEmpty()
  407. {
  408. $attribs = $this->form->getAttribs();
  409. $this->assertTrue(is_array($attribs));
  410. $this->assertTrue(empty($attribs));
  411. }
  412. public function testRetrievingUndefinedAttribReturnsNull()
  413. {
  414. $this->assertNull($this->form->getAttrib('foo'));
  415. }
  416. public function testCanAddAndRetrieveSingleAttribs()
  417. {
  418. $this->testRetrievingUndefinedAttribReturnsNull();
  419. $this->form->setAttrib('foo', 'bar');
  420. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  421. }
  422. public function testCanAddAndRetrieveMultipleAttribs()
  423. {
  424. $this->form->setAttrib('foo', 'bar');
  425. $this->assertEquals('bar', $this->form->getAttrib('foo'));
  426. $this->form->addAttribs(array(
  427. 'bar' => 'baz',
  428. 'baz' => 'bat',
  429. 'bat' => 'foo'
  430. ));
  431. $test = $this->form->getAttribs();
  432. $attribs = array(
  433. 'foo' => 'bar',
  434. 'bar' => 'baz',
  435. 'baz' => 'bat',
  436. 'bat' => 'foo'
  437. );
  438. $this->assertSame($attribs, $test);
  439. }
  440. public function testSetAttribsOverwritesExistingAttribs()
  441. {
  442. $this->testCanAddAndRetrieveMultipleAttribs();
  443. $array = array('bogus' => 'value', 'not' => 'real');
  444. $this->form->setAttribs($array);
  445. $this->assertSame($array, $this->form->getAttribs());
  446. }
  447. public function testCanRemoveSingleAttrib()
  448. {
  449. $this->testCanAddAndRetrieveSingleAttribs();
  450. $this->assertTrue($this->form->removeAttrib('foo'));
  451. $this->assertNull($this->form->getAttrib('foo'));
  452. }
  453. public function testRemoveAttribReturnsFalseIfAttribDoesNotExist()
  454. {
  455. $this->assertFalse($this->form->removeAttrib('foo'));
  456. }
  457. public function testCanClearAllAttribs()
  458. {
  459. $this->testCanAddAndRetrieveMultipleAttribs();
  460. $this->form->clearAttribs();
  461. $attribs = $this->form->getAttribs();
  462. $this->assertTrue(is_array($attribs));
  463. $this->assertTrue(empty($attribs));
  464. }
  465. public function testNameIsInitiallyNull()
  466. {
  467. $this->assertNull($this->form->getName());
  468. }
  469. public function testCanSetName()
  470. {
  471. $this->testNameIsInitiallyNull();
  472. $this->form->setName('foo');
  473. $this->assertEquals('foo', $this->form->getName());
  474. }
  475. public function testZeroAsNameIsAllowed()
  476. {
  477. try {
  478. $this->form->setName(0);
  479. $this->assertEquals(0, $this->form->getName());
  480. } catch (Zend_Form_Exception $e) {
  481. $this->fail('Should allow zero as form name');
  482. }
  483. }
  484. public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
  485. {
  486. $this->form->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
  487. $this->assertEquals('foobar', $this->form->getName());
  488. try {
  489. $this->form->setName('%\^&*)\(%$#@!.}{;-,');
  490. $this->fail('Empty names should raise exception');
  491. } catch (Zend_Form_Exception $e) {
  492. $this->assertContains('Invalid name provided', $e->getMessage());
  493. }
  494. }
  495. public function testActionDefaultsToEmptyString()
  496. {
  497. $this->assertSame('', $this->form->getAction());
  498. }
  499. public function testCanSetAction()
  500. {
  501. $this->testActionDefaultsToEmptyString();
  502. $this->form->setAction('/foo/bar');
  503. $this->assertEquals('/foo/bar', $this->form->getAction());
  504. }
  505. /**
  506. * @group ZF-7067
  507. */
  508. public function testCanSetActionWithGetParams()
  509. {
  510. $this->testActionDefaultsToEmptyString();
  511. $this->form->setAction('/foo.php?bar')
  512. ->setView(new Zend_View);
  513. $html = $this->form->render();
  514. $this->assertContains('action="/foo.php?bar"', $html);
  515. $this->assertEquals('/foo.php?bar', $this->form->getAction());
  516. }
  517. public function testMethodDefaultsToPost()
  518. {
  519. $this->assertEquals('post', $this->form->getMethod());
  520. }
  521. public function testCanSetMethod()
  522. {
  523. $this->testMethodDefaultsToPost();
  524. $this->form->setMethod('get');
  525. $this->assertEquals('get', $this->form->getMethod());
  526. }
  527. public function testMethodLimitedToGetPostPutAndDelete()
  528. {
  529. foreach (array('get', 'post', 'put', 'delete') as $method) {
  530. $this->form->setMethod($method);
  531. $this->assertEquals($method, $this->form->getMethod());
  532. }
  533. try {
  534. $this->form->setMethod('bogus');
  535. $this->fail('Invalid method type should throw exception');
  536. } catch (Zend_Form_Exception $e) {
  537. $this->assertContains('invalid', $e->getMessage());
  538. }
  539. }
  540. public function testEnctypeDefaultsToUrlEncoded()
  541. {
  542. $this->assertEquals(Zend_Form::ENCTYPE_URLENCODED, $this->form->getEnctype());
  543. }
  544. public function testCanSetEnctype()
  545. {
  546. $this->testEnctypeDefaultsToUrlEncoded();
  547. $this->form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
  548. $this->assertEquals(Zend_Form::ENCTYPE_MULTIPART, $this->form->getEnctype());
  549. }
  550. public function testLegendInitiallyNull()
  551. {
  552. $this->assertNull($this->form->getLegend());
  553. }
  554. public function testCanSetLegend()
  555. {
  556. $this->testLegendInitiallyNull();
  557. $legend = "This is a legend";
  558. $this->form->setLegend($legend);
  559. $this->assertEquals($legend, $this->form->getLegend());
  560. }
  561. public function testDescriptionInitiallyNull()
  562. {
  563. $this->assertNull($this->form->getDescription());
  564. }
  565. public function testCanSetDescription()
  566. {
  567. $this->testDescriptionInitiallyNull();
  568. $description = "This is a description";
  569. $this->form->setDescription($description);
  570. $this->assertEquals($description, $this->form->getDescription());
  571. }
  572. // Plugin loaders
  573. public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
  574. {
  575. $loader = $this->form->getPluginLoader('decorator');
  576. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  577. $paths = $loader->getPaths('Zend_Form_Decorator');
  578. $this->assertTrue(is_array($paths), var_export($loader, 1));
  579. $this->assertTrue(0 < count($paths));
  580. $this->assertContains('Form', $paths[0]);
  581. $this->assertContains('Decorator', $paths[0]);
  582. }
  583. public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
  584. {
  585. $loader = new Zend_Loader_PluginLoader();
  586. try {
  587. $this->form->setPluginLoader($loader, 'foo');
  588. $this->fail('Invalid plugin loader type should raise exception');
  589. } catch (Zend_Form_Exception $e) {
  590. $this->assertContains('Invalid type', $e->getMessage());
  591. }
  592. }
  593. public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
  594. {
  595. try {
  596. $this->form->getPluginLoader('foo');
  597. $this->fail('Invalid plugin loader type should raise exception');
  598. } catch (Zend_Form_Exception $e) {
  599. $this->assertContains('Invalid type', $e->getMessage());
  600. }
  601. }
  602. public function testCanSetCustomDecoratorPluginLoader()
  603. {
  604. $loader = new Zend_Loader_PluginLoader();
  605. $this->form->setPluginLoader($loader, 'decorator');
  606. $test = $this->form->getPluginLoader('decorator');
  607. $this->assertSame($loader, $test);
  608. }
  609. public function testPassingInvalidTypeToAddPrefixPathThrowsException()
  610. {
  611. try {
  612. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
  613. $this->fail('Passing invalid loader type to addPrefixPath() should raise exception');
  614. } catch (Zend_Form_Exception $e) {
  615. $this->assertContains('Invalid type', $e->getMessage());
  616. }
  617. }
  618. public function testCanAddDecoratorPluginLoaderPrefixPath()
  619. {
  620. $loader = $this->form->getPluginLoader('decorator');
  621. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  622. $paths = $loader->getPaths('Zend_Foo');
  623. $this->assertTrue(is_array($paths));
  624. $this->assertContains('Foo', $paths[0]);
  625. }
  626. public function testUpdatedDecoratorPrefixPathUsedForNewElements()
  627. {
  628. $loader = $this->form->getPluginLoader('decorator');
  629. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  630. $foo = new Zend_Form_Element_Text('foo');
  631. $this->form->addElement($foo);
  632. $loader = $foo->getPluginLoader('decorator');
  633. $paths = $loader->getPaths('Zend_Foo');
  634. $this->assertTrue(is_array($paths));
  635. $this->assertContains('Foo', $paths[0]);
  636. $this->form->addElement('text', 'bar');
  637. $bar = $this->form->bar;
  638. $loader = $bar->getPluginLoader('decorator');
  639. $paths = $loader->getPaths('Zend_Foo');
  640. $this->assertTrue(is_array($paths));
  641. $this->assertContains('Foo', $paths[0]);
  642. }
  643. public function testUpdatedDecoratorPrefixPathUsedForNewDisplayGroups()
  644. {
  645. $loader = $this->form->getPluginLoader('decorator');
  646. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  647. $this->setupElements();
  648. $foo = $this->form->foo;
  649. $loader = $foo->getPluginLoader('decorator');
  650. $paths = $loader->getPaths('Zend_Foo');
  651. $this->assertTrue(is_array($paths));
  652. $this->assertContains('Foo', $paths[0]);
  653. }
  654. public function testUpdatedPrefixPathUsedForNewSubForms()
  655. {
  656. $loader = $this->form->getPluginLoader('decorator');
  657. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  658. $this->setupSubForm();
  659. $loader = $this->form->sub->getPluginLoader('decorator');
  660. $paths = $loader->getPaths('Zend_Foo');
  661. $this->assertTrue(is_array($paths));
  662. $this->assertContains('Foo', $paths[0]);
  663. }
  664. public function testGetPluginLoaderRetrievesDefaultElementPluginLoader()
  665. {
  666. $loader = $this->form->getPluginLoader('element');
  667. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  668. $paths = $loader->getPaths('Zend_Form_Element');
  669. $this->assertTrue(is_array($paths), var_export($loader, 1));
  670. $this->assertTrue(0 < count($paths));
  671. $this->assertContains('Form', $paths[0]);
  672. $this->assertContains('Element', $paths[0]);
  673. }
  674. public function testCanSetCustomDecoratorElementLoader()
  675. {
  676. $loader = new Zend_Loader_PluginLoader();
  677. $this->form->setPluginLoader($loader, 'element');
  678. $test = $this->form->getPluginLoader('element');
  679. $this->assertSame($loader, $test);
  680. }
  681. public function testCanAddElementPluginLoaderPrefixPath()
  682. {
  683. $loader = $this->form->getPluginLoader('element');
  684. $this->form->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'element');
  685. $paths = $loader->getPaths('Zend_Foo');
  686. $this->assertTrue(is_array($paths));
  687. $this->assertContains('Foo', $paths[0]);
  688. }
  689. public function testAddAllPluginLoaderPrefixPathsSimultaneously()
  690. {
  691. $decoratorLoader = new Zend_Loader_PluginLoader();
  692. $elementLoader = new Zend_Loader_PluginLoader();
  693. $this->form->setPluginLoader($decoratorLoader, 'decorator')
  694. ->setPluginLoader($elementLoader, 'element')
  695. ->addPrefixPath('Zend', 'Zend/');
  696. $paths = $decoratorLoader->getPaths('Zend_Decorator');
  697. $this->assertTrue(is_array($paths), var_export($paths, 1));
  698. $this->assertContains('Decorator', $paths[0]);
  699. $paths = $elementLoader->getPaths('Zend_Element');
  700. $this->assertTrue(is_array($paths), var_export($paths, 1));
  701. $this->assertContains('Element', $paths[0]);
  702. }
  703. // Elements:
  704. public function testCanAddAndRetrieveSingleElements()
  705. {
  706. $element = new Zend_Form_Element('foo');
  707. $this->form->addElement($element);
  708. $this->assertSame($element, $this->form->getElement('foo'));
  709. }
  710. public function testGetElementReturnsNullForUnregisteredElement()
  711. {
  712. $this->assertNull($this->form->getElement('foo'));
  713. }
  714. public function testCanAddAndRetrieveSingleElementsByStringType()
  715. {
  716. $this->form->addElement('text', 'foo');
  717. $element = $this->form->getElement('foo');
  718. $this->assertTrue($element instanceof Zend_Form_Element);
  719. $this->assertTrue($element instanceof Zend_Form_Element_Text);
  720. $this->assertEquals('foo', $element->getName());
  721. }
  722. public function testAddElementAsStringElementThrowsExceptionWhenNoNameProvided()
  723. {
  724. try {
  725. $this->form->addElement('text');
  726. $this->fail('Should not be able to specify string element type without name');
  727. } catch (Zend_Form_Exception $e) {
  728. $this->assertContains('must have', $e->getMessage());
  729. }
  730. }
  731. public function testCreateElementReturnsNewElement()
  732. {
  733. $element = $this->form->createElement('text', 'foo');
  734. $this->assertTrue($element instanceof Zend_Form_Element);
  735. }
  736. public function testCreateElementDoesNotAttachElementToForm()
  737. {
  738. $element = $this->form->createElement('text', 'foo');
  739. $this->assertTrue($element instanceof Zend_Form_Element);
  740. $this->assertNull($this->form->foo);
  741. }
  742. public function testCanAddAndRetrieveMultipleElements()
  743. {
  744. $this->form->addElements(array(
  745. 'foo' => 'text',
  746. array('text', 'bar'),
  747. array('text', 'baz', array('foo' => 'bar')),
  748. new Zend_Form_Element_Text('bat'),
  749. ));
  750. $elements = $this->form->getElements();
  751. $names = array('foo', 'bar', 'baz', 'bat');
  752. $this->assertEquals($names, array_keys($elements));
  753. $foo = $elements['foo'];
  754. $this->assertTrue($foo instanceof Zend_Form_Element_Text);
  755. $bar = $elements['bar'];
  756. $this->assertTrue($bar instanceof Zend_Form_Element_Text);
  757. $baz = $elements['baz'];
  758. $this->assertTrue($baz instanceof Zend_Form_Element_Text);
  759. $this->assertEquals('bar', $baz->foo, var_export($baz->getAttribs(), 1));
  760. $bat = $elements['bat'];
  761. $this->assertTrue($bat instanceof Zend_Form_Element_Text);
  762. }
  763. public function testSetElementsOverwritesExistingElements()
  764. {
  765. $this->testCanAddAndRetrieveMultipleElements();
  766. $this->form->setElements(array(
  767. 'bogus' => 'text'
  768. ));
  769. $elements = $this->form->getElements();
  770. $names = array('bogus');
  771. $this->assertEquals($names, array_keys($elements));
  772. }
  773. public function testCanRemoveSingleElement()
  774. {
  775. $this->testCanAddAndRetrieveMultipleElements();
  776. $this->assertTrue($this->form->removeElement('bar'));
  777. $this->assertNull($this->form->getElement('bar'));
  778. }
  779. public function testRemoveElementReturnsFalseWhenElementNotRegistered()
  780. {
  781. $this->assertFalse($this->form->removeElement('bogus'));
  782. }
  783. public function testCanClearAllElements()
  784. {
  785. $this->testCanAddAndRetrieveMultipleElements();
  786. $this->form->clearElements();
  787. $elements = $this->form->getElements();
  788. $this->assertTrue(is_array($elements));
  789. $this->assertTrue(empty($elements));
  790. }
  791. public function testGetValueReturnsNullForUndefinedElements()
  792. {
  793. $this->assertNull($this->form->getValue('foo'));
  794. }
  795. public function testCanSetElementDefaultValues()
  796. {
  797. $this->testCanAddAndRetrieveMultipleElements();
  798. $values = array(
  799. 'foo' => 'foovalue',
  800. 'bar' => 'barvalue',
  801. 'baz' => 'bazvalue',
  802. 'bat' => 'batvalue'
  803. );
  804. $this->form->setDefaults($values);
  805. $elements = $this->form->getElements();
  806. foreach (array_keys($values) as $name) {
  807. $this->assertEquals($name . 'value', $elements[$name]->getValue(), var_export($elements[$name], 1));
  808. }
  809. }
  810. public function testSettingElementDefaultsDoesNotSetElementValuesToNullIfNotInDefaultsArray()
  811. {
  812. $this->testCanAddAndRetrieveMultipleElements();
  813. $this->form->baz->setValue('testing');
  814. $this->form->bar->setValue('testing');
  815. $values = array(
  816. 'foo' => 'foovalue',
  817. 'bat' => 'batvalue'
  818. );
  819. $this->form->setDefaults($values);
  820. $this->assertEquals('foovalue', $this->form->foo->getValue());
  821. $this->assertEquals('batvalue', $this->form->bat->getValue());
  822. $this->assertNotNull($this->form->baz->getValue());
  823. $this->assertNotNull($this->form->bar->getValue());
  824. }
  825. public function testCanRetrieveSingleElementValue()
  826. {
  827. $this->form->addElement('text', 'foo', array('value' => 'foovalue'));
  828. $this->assertEquals('foovalue', $this->form->getValue('foo'));
  829. }
  830. public function testCanRetrieveAllElementValues()
  831. {
  832. $this->testCanAddAndRetrieveMultipleElements();
  833. $values = array(
  834. 'foo' => 'foovalue',
  835. 'bar' => 'barvalue',
  836. 'baz' => 'bazvalue',
  837. 'bat' => 'batvalue'
  838. );
  839. $this->form->setDefaults($values);
  840. $test = $this->form->getValues();
  841. $elements = $this->form->getElements();
  842. foreach (array_keys($values) as $name) {
  843. $this->assertEquals($values[$name], $test[$name]);
  844. }
  845. }
  846. public function testRetrievingAllElementValuesSkipsThoseFlaggedAsIgnore()
  847. {
  848. $this->form->addElements(array(
  849. 'foo' => 'text',
  850. 'bar' => 'text',
  851. 'baz' => 'text'
  852. ));
  853. $this->form->setDefaults(array(
  854. 'foo' => 'Foo Value',
  855. 'bar' => 'Bar Value',
  856. 'baz' => 'Baz Value',
  857. ));
  858. $this->form->bar->setIgnore(true);
  859. $test = $this->form->getValues();
  860. $this->assertFalse(array_key_exists('bar', $test));
  861. $this->assertTrue(array_key_exists('foo', $test));
  862. $this->assertTrue(array_key_exists('baz', $test));
  863. }
  864. public function testCanRetrieveSingleUnfilteredElementValue()
  865. {
  866. $foo = new Zend_Form_Element_Text('foo');
  867. $foo->addFilter('StringToUpper')
  868. ->setValue('foovalue');
  869. $this->form->addElement($foo);
  870. $this->assertEquals('FOOVALUE', $this->form->getValue('foo'));
  871. $this->assertEquals('foovalue', $this->form->getUnfilteredValue('foo'));
  872. }
  873. public function testCanRetrieveAllUnfilteredElementValues()
  874. {
  875. $foo = new Zend_Form_Element_Text('foo');
  876. $foo->addFilter('StringToUpper')
  877. ->setValue('foovalue');
  878. $bar = new Zend_Form_Element_Text('bar');
  879. $bar->addFilter('StringToUpper')
  880. ->setValue('barvalue');
  881. $this->form->addElements(array($foo, $bar));
  882. $values = $this->form->getValues();
  883. $unfiltered = $this->form->getUnfilteredValues();
  884. foreach (array('foo', 'bar') as $key) {
  885. $value = $key . 'value';
  886. $this->assertEquals(strtoupper($value), $values[$key]);
  887. $this->assertEquals($value, $unfiltered[$key]);
  888. }
  889. }
  890. public function testOverloadingElements()
  891. {
  892. $this->form->addElement('text', 'foo');
  893. $this->assertTrue(isset($this->form->foo));
  894. $element = $this->form->foo;
  895. $this->assertTrue($element instanceof Zend_Form_Element);
  896. unset($this->form->foo);
  897. $this->assertFalse(isset($this->form->foo));
  898. $bar = new Zend_Form_Element_Text('bar');
  899. $this->form->bar = $bar;
  900. $this->assertTrue(isset($this->form->bar));
  901. $element = $this->form->bar;
  902. $this->assertSame($bar, $element);
  903. }
  904. public function testOverloadingGetReturnsNullForUndefinedFormItems()
  905. {
  906. $this->assertNull($this->form->bogus);
  907. }
  908. public function testOverloadingSetThrowsExceptionForInvalidTypes()
  909. {
  910. try {
  911. $this->form->foo = true;
  912. $this->fail('Overloading should not allow scalars');
  913. } catch (Zend_Form_Exception $e) {
  914. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  915. }
  916. try {
  917. $this->form->foo = new Zend_Config(array());
  918. $this->fail('Overloading should not allow arbitrary object types');
  919. } catch (Zend_Form_Exception $e) {
  920. $this->assertContains('Only form elements and groups may be overloaded', $e->getMessage());
  921. $this->assertContains('Zend_Config', $e->getMessage());
  922. }
  923. }
  924. public function testFormIsNotAnArrayByDefault()
  925. {
  926. $this->assertFalse($this->form->isArray());
  927. }
  928. public function testCanSetArrayFlag()
  929. {
  930. $this->testFormIsNotAnArrayByDefault();
  931. $this->form->setIsArray(true);
  932. $this->assertTrue($this->form->isArray());
  933. $this->form->setIsArray(false);
  934. $this->assertFalse($this->form->isArray());
  935. }
  936. public function testElementsBelongToReturnsFormNameWhenFormIsArray()
  937. {
  938. $this->form->setName('foo')
  939. ->setIsArray(true);
  940. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  941. }
  942. public function testElementsInitiallyBelongToNoArrays()
  943. {
  944. $this->assertNull($this->form->getElementsBelongTo());
  945. }
  946. public function testCanSetArrayToWhichElementsBelong()
  947. {
  948. $this->testElementsInitiallyBelongToNoArrays();
  949. $this->form->setElementsBelongTo('foo');
  950. $this->assertEquals('foo', $this->form->getElementsBelongTo());
  951. }
  952. public function testSettingArrayToWhichElementsBelongSetsArrayFlag()
  953. {
  954. $this->testFormIsNotAnArrayByDefault();
  955. $this->testCanSetArrayToWhichElementsBelong();
  956. $this->assertTrue($this->form->isArray());
  957. }
  958. public function testArrayToWhichElementsBelongCanConsistOfValidVariableCharsOnly()
  959. {
  960. $this->testElementsInitiallyBelongToNoArrays();
  961. $this->form->setElementsBelongTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
  962. $this->assertEquals('foobar', $this->form->getElementsBelongTo());
  963. }
  964. public function testSettingArrayToWhichElementsBelongEmptyClearsIt()
  965. {
  966. $this->testCanSetArrayToWhichElementsBelong();
  967. $this->form->setElementsBelongTo('');
  968. $this->assertNull($this->form->getElementsBelongTo());
  969. }
  970. public function testSettingArrayToWhichElementsBelongEmptySetsArrayFlagToFalse()
  971. {
  972. $this->testSettingArrayToWhichElementsBelongEmptyClearsIt();
  973. $this->assertFalse($this->form->isArray());
  974. }
  975. /**
  976. * @group ZF-6741
  977. */
  978. public function testUseIdForDdTagByDefault()
  979. {
  980. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  981. ->bar->addElement('text', 'foo');
  982. $html = $this->form->setView($this->getView())->render();
  983. $this->assertRegexp('/<dd.*?bar-foo.*?>/', $html);
  984. }
  985. public function testUseIdForDtTagByDefault()
  986. {
  987. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  988. ->bar->addElement('text', 'foo');
  989. $html = $this->form->setView($this->getView())->render();
  990. $this->assertRegexp('/<dt.*?bar-foo.*?>/', $html);
  991. }
  992. /**
  993. * @group ZF-3146
  994. */
  995. public function testSetElementsBelongToShouldApplyToBothExistingAndFutureElements()
  996. {
  997. $this->form->addElement('text', 'testBelongsTo');
  998. $this->form->setElementsBelongTo('foo');
  999. $this->assertEquals('foo', $this->form->testBelongsTo->getBelongsTo(), 'Failed determining testBelongsTo belongs to array');
  1000. $this->setupElements();
  1001. foreach ($this->form->getElements() as $element) {
  1002. $message = sprintf('Failed determining element "%s" belongs to foo', $element->getName());
  1003. $this->assertEquals('foo', $element->getBelongsTo(), $message);
  1004. }
  1005. }
  1006. /**
  1007. * @group ZF-3742
  1008. */
  1009. public function testElementsInDisplayGroupsShouldInheritFormElementsBelongToSetting()
  1010. {
  1011. $subForm = new Zend_Form_SubForm();
  1012. $subForm->addElements(array(
  1013. new Zend_Form_Element_Text('foo'),
  1014. new Zend_Form_Element_Text('bar'),
  1015. new Zend_Form_Element_Text('baz'),
  1016. new Zend_Form_Element_Text('bat'),
  1017. ))
  1018. ->addDisplayGroup(array('bar', 'baz'), 'barbaz');
  1019. $this->form->addSubForm($subForm, 'sub')
  1020. ->setElementsBelongTo('myform')
  1021. ->setView(new Zend_View);
  1022. $html = $this->form->render();
  1023. foreach (array('foo', 'bar', 'baz', 'bat') as $test) {
  1024. $this->assertContains('id="myform-sub-' . $test . '"', $html);
  1025. $this->assertContains('name="myform[sub][' . $test . ']"', $html);
  1026. }
  1027. }
  1028. public function testIsValidWithOneLevelElementsBelongTo()
  1029. {
  1030. $this->form->addElement('text', 'test')->test
  1031. ->addValidator('Identical', false, array('Test Value'));
  1032. $this->form->setElementsBelongTo('foo');
  1033. $data = array(
  1034. 'foo' => array(
  1035. 'test' => 'Test Value',
  1036. ),
  1037. );
  1038. $this->assertTrue($this->form->isValid($data));
  1039. }
  1040. public function testIsValidWithMultiLevelElementsBelongTo()
  1041. {
  1042. $this->form->addElement('text', 'test')->test
  1043. ->addValidator('Identical', false, array('Test Value'));
  1044. $this->form->setElementsBelongTo('foo[bar][zot]');
  1045. $data = array(
  1046. 'foo' => array(
  1047. 'bar' => array(
  1048. 'zot' => array(
  1049. 'test' => 'Test Value',
  1050. ),
  1051. ),
  1052. ),
  1053. );
  1054. $this->assertTrue($this->form->isValid($data));
  1055. }
  1056. // Sub forms
  1057. public function testCanAddAndRetrieveSingleSubForm()
  1058. {
  1059. $subForm = new Zend_Form_SubForm;
  1060. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1061. $this->form->addSubForm($subForm, 'page1');
  1062. $test = $this->form->getSubForm('page1');
  1063. $this->assertSame($subForm, $test);
  1064. }
  1065. public function testAddingSubFormSetsSubFormName()
  1066. {
  1067. $subForm = new Zend_Form_SubForm;
  1068. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1069. $this->form->addSubForm($subForm, 'page1');
  1070. $this->assertEquals('page1', $subForm->getName());
  1071. }
  1072. public function testAddingSubFormResetsBelongsToWithDifferentSubFormName()
  1073. {
  1074. $subForm = new Zend_Form_SubForm;
  1075. $subForm->setName('quo')
  1076. ->addElement('text', 'foo');
  1077. $this->form->addSubForm($subForm, 'bar');
  1078. $this->assertEquals('bar', $subForm->foo->getBelongsTo());
  1079. }
  1080. public function testGetSubFormReturnsNullForUnregisteredSubForm()
  1081. {
  1082. $this->assertNull($this->form->getSubForm('foo'));
  1083. }
  1084. public function testCanAddAndRetrieveMultipleSubForms()
  1085. {
  1086. $page1 = new Zend_Form_SubForm();
  1087. $page2 = new Zend_Form_SubForm();
  1088. $page3 = new Zend_Form_SubForm();
  1089. $this->form->addSubForms(array(
  1090. 'page1' => $page1,
  1091. array($page2, 'page2'),
  1092. array($page3, 'page3', 3)
  1093. ));
  1094. $subforms = $this->form->getSubForms();
  1095. $keys = array('page1', 'page2', 'page3');
  1096. $this->assertEquals($keys, array_keys($subforms));
  1097. $this->assertSame($page1, $subforms['page1']);
  1098. $this->assertSame($page2, $subforms['page2']);
  1099. $this->assertSame($page3, $subforms['page3']);
  1100. }
  1101. public function testSetSubFormsOverwritesExistingSubForms()
  1102. {
  1103. $this->testCanAddAndRetrieveMultipleSubForms();
  1104. $foo = new Zend_Form_SubForm();
  1105. $this->form->setSubForms(array('foo' => $foo));
  1106. $subforms = $this->form->getSubForms();
  1107. $keys = array('foo');
  1108. $this->assertEquals($keys, array_keys($subforms));
  1109. $this->assertSame($foo, $subforms['foo']);
  1110. }
  1111. public function testCanRemoveSingleSubForm()
  1112. {
  1113. $this->testCanAddAndRetrieveMultipleSubForms();
  1114. $this->assertTrue($this->form->removeSubForm('page2'));
  1115. $this->assertNull($this->form->getSubForm('page2'));
  1116. }
  1117. public function testRemoveSubFormReturnsFalseForNonexistantSubForm()
  1118. {
  1119. $this->assertFalse($this->form->removeSubForm('foo'));
  1120. }
  1121. public function testCanClearAllSubForms()
  1122. {
  1123. $this->testCanAddAndRetrieveMultipleSubForms();
  1124. $this->form->clearSubForms();
  1125. $subforms = $this->form->getSubForms();
  1126. $this->assertTrue(is_array($subforms));
  1127. $this->assertTrue(empty($subforms));
  1128. }
  1129. public function testOverloadingSubForms()
  1130. {
  1131. $foo = new Zend_Form_SubForm;
  1132. $this->form->addSubForm($foo, 'foo');
  1133. $this->assertTrue(isset($this->form->foo));
  1134. $subform = $this->form->foo;
  1135. $this->assertSame($foo, $subform);
  1136. unset($this->form->foo);
  1137. $this->assertFalse(isset($this->form->foo));
  1138. $bar = new Zend_Form_SubForm();
  1139. $this->form->bar = $bar;
  1140. $this->assertTrue(isset($this->form->bar));
  1141. $subform = $this->form->bar;
  1142. $this->assertSame($bar, $subform);
  1143. }
  1144. public function testCanSetDefaultsForSubFormElementsFromForm()
  1145. {
  1146. $subForm = new Zend_Form_SubForm;
  1147. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1148. $this->form->addSubForm($subForm, 'page1');
  1149. $data = array('foo' => 'foo value', 'bar' => 'bar value');
  1150. $this->form->setDefaults($data);
  1151. $this->assertEquals($data['foo'], $subForm->foo->getValue());
  1152. $this->assertEquals($data['bar'], $subForm->bar->getValue());
  1153. }
  1154. public function testCanSetDefaultsForSubFormElementsFromFormWithArray()
  1155. {
  1156. $subForm = new Zend_Form_SubForm;
  1157. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1158. $this->form->addSubForm($subForm, 'page1');
  1159. $data = array( 'page1' => array(
  1160. 'foo' => 'foo value',
  1161. 'bar' => 'bar value'
  1162. ));
  1163. $this->form->setDefaults($data);
  1164. $this->assertEquals($data['page1']['foo'], $subForm->foo->getValue());
  1165. $this->assertEquals($data['page1']['bar'], $subForm->bar->getValue());
  1166. }
  1167. public function testGetValuesReturnsSubFormValues()
  1168. {
  1169. $subForm = new Zend_Form_SubForm;
  1170. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1171. $subForm->foo->setValue('foo value');
  1172. $subForm->bar->setValue('bar value');
  1173. $this->form->addSubForm($subForm, 'page1');
  1174. $values = $this->form->getValues();
  1175. $this->assertTrue(isset($values['page1']));
  1176. $this->assertTrue(isset($values['page1']['foo']));
  1177. $this->assertTrue(isset($values['page1']['bar']));
  1178. $this->assertEquals($subForm->foo->getValue(), $values['page1']['foo']);
  1179. $this->assertEquals($subForm->bar->getValue(), $values['page1']['bar']);
  1180. }
  1181. public function testGetValuesReturnsSubFormValuesFromArrayToWhichElementsBelong()
  1182. {
  1183. $subForm = new Zend_Form_SubForm;
  1184. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1185. ->setElementsBelongTo('subform');
  1186. $subForm->foo->setValue('foo value');
  1187. $subForm->bar->setValue('bar value');
  1188. $this->form->addSubForm($subForm, 'page1');
  1189. $values = $this->form->getValues();
  1190. $this->assertTrue(isset($values['subform']), var_export($values, 1));
  1191. $this->assertTrue(isset($values['subform']['foo']));
  1192. $this->assertTrue(isset($values['subform']['bar']));
  1193. $this->assertEquals($subForm->foo->getValue(), $values['subform']['foo']);
  1194. $this->assertEquals($subForm->bar->getValue(), $values['subform']['bar']);
  1195. }
  1196. public function testGetValuesReturnsNestedSubFormValuesFromArraysToWhichElementsBelong()
  1197. {
  1198. $form = new Zend_Form();
  1199. $form->setElementsBelongTo('foobar');
  1200. $form->addElement('text', 'firstName')
  1201. ->getElement('firstName')
  1202. ->setRequired(true);
  1203. $form->addElement('text', 'lastName')
  1204. ->getElement('lastName')
  1205. ->setRequired(true);
  1206. $subForm = new Zend_Form_SubForm();
  1207. $subForm->setElementsBelongTo('baz[quux]');
  1208. $subForm->addElement('text', 'email')
  1209. ->getElement('email')->setRequired(true);
  1210. $subSubForm = new Zend_Form_SubForm();
  1211. $subSubForm->setElementsBelongTo('bat');
  1212. $subSubForm->addElement('checkbox', 'home')
  1213. ->getElement('home')->setRequired(true);
  1214. $subForm->addSubForm($subSubForm, 'subSub');
  1215. $form->addSubForm($subForm, 'sub')
  1216. ->addElement('submit', 'save', array('value' => 'submit', 'ignore' => true));
  1217. $data = array('foobar' => array(
  1218. 'firstName' => 'Mabel',
  1219. 'lastName' => 'Cow',
  1220. 'baz' => array(
  1221. 'quux' => array(
  1222. 'email' => 'mabel@cow.org',
  1223. 'bat' => array(
  1224. 'home' => 1,
  1225. )
  1226. ),
  1227. )
  1228. ));
  1229. $this->assertTrue($form->isValid($data));
  1230. $values = $form->getValues();
  1231. $this->assertEquals($data, $values);
  1232. }
  1233. public function testGetValueCanReturnSubFormValues()
  1234. {
  1235. $subForm = new Zend_Form_SubForm;
  1236. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1237. $subForm->foo->setValue('foo value');
  1238. $subForm->bar->setValue('bar value');
  1239. $this->form->addSubForm($subForm, 'page1');
  1240. $values = $this->form->getValue('page1');
  1241. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1242. $this->assertTrue(isset($values['bar']));
  1243. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1244. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1245. }
  1246. public function testGetValueCanReturnSubFormValuesFromArrayToWhichElementsBelong()
  1247. {
  1248. $subForm = new Zend_Form_SubForm;
  1249. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1250. ->setElementsBelongTo('subform');
  1251. $subForm->foo->setValue('foo value');
  1252. $subForm->bar->setValue('bar value');
  1253. $this->form->addSubForm($subForm, 'page1');
  1254. $values = $this->form->getValue('subform');
  1255. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1256. $this->assertTrue(isset($values['bar']));
  1257. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1258. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1259. }
  1260. public function testIsValidCanValidateSubFormsWithArbitraryElementsBelong()
  1261. {
  1262. $subForm = new Zend_Form_SubForm();
  1263. $subForm->addElement('text', 'test')->test
  1264. ->setRequired(true)->addValidator('Identical', false, array('Test Value'));
  1265. $this->form->addSubForm($subForm, 'sub');
  1266. $this->form->setElementsBelongTo('foo[bar]');
  1267. $subForm->setElementsBelongTo('my[subform]');
  1268. $data = array(
  1269. 'foo' => array(
  1270. 'bar' => array(
  1271. 'my' => array(
  1272. 'subform' => array(
  1273. 'test' => 'Test Value',
  1274. ),
  1275. ),
  1276. ),
  1277. ),
  1278. );
  1279. $this->assertTrue($this->form->isValid($data));
  1280. }
  1281. public function testIsValidCanValidateNestedSubFormsWithArbitraryElementsBelong()
  1282. {
  1283. $subForm = new Zend_Form_SubForm();
  1284. $subForm->addElement('text', 'test1')->test1
  1285. ->setRequired(true)->addValidator('Identical', false, array('Test1 Value'));
  1286. $this->form->addSubForm($subForm, 'sub');
  1287. $subSubForm = new Zend_Form_SubForm();
  1288. $subSubForm->addElement('text', 'test2')->test2
  1289. ->setRequired(true)->addValidator('Identical', false, array('Test2 Value'));
  1290. $subForm->addSubForm($subSubForm, 'subSub');
  1291. $this->form->setElementsBelongTo('form[first]');
  1292. // Notice we skipped subForm, to mix manual and auto elementsBelongTo.
  1293. $subSubForm->setElementsBelongTo('subsubform[first]');
  1294. $data = array(
  1295. 'form' => array(
  1296. 'first' => array(
  1297. 'sub' => array(
  1298. 'test1' => 'Test1 Value',
  1299. 'subsubform' => array(
  1300. 'first' => array(
  1301. 'test2' => 'Test2 Value',
  1302. ),
  1303. ),
  1304. ),
  1305. ),
  1306. ),
  1307. );
  1308. $this->assertTrue($this->form->isValid($data));
  1309. }
  1310. /**
  1311. * @group ZF-9679
  1312. */
  1313. public function testIsValidDiscardsValidatedValues()
  1314. {
  1315. $this->form->addElement('text', 'foo');
  1316. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  1317. ->bar->addElement('text', 'foo')
  1318. ->foo->setAllowEmpty(true)
  1319. ->addValidator('Identical', true, '');
  1320. $this->assertTrue($this->form->isValid(array('foo' => 'foo Value')));
  1321. }
  1322. /**
  1323. * @group ZF-9666
  1324. */
  1325. public function testSetDefaultsDiscardsPopulatedValues()
  1326. {
  1327. $this->form->addElement('text', 'foo');
  1328. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  1329. ->bar->addElement('text', 'foo');
  1330. $this->form->populate(array('foo' => 'foo Value'));
  1331. $html = $this->form->setView($this->getView())
  1332. ->render();
  1333. $this->assertEquals(1, preg_match_all('/foo Value/', $html, $matches));
  1334. }
  1335. public function _setup9350()
  1336. {
  1337. $this->form->addSubForm(new Zend_Form_SubForm(), 'foo')
  1338. ->foo->setElementsBelongTo('foo[foo]') // foo[foo]
  1339. ->addSubForm(new Zend_Form_SubForm(), 'foo') // foo[foo][foo]
  1340. ->foo->setIsArray(false)
  1341. ->addElement('text', 'foo') // foo[foo][foo][foo]
  1342. ->foo->addValidator('Identical',
  1343. false,
  1344. array('foo Value'));
  1345. $this->form->foo->addSubForm(new Zend_Form_SubForm(), 'baz') // foo[foo][baz]
  1346. ->baz->setIsArray(false)
  1347. ->addSubForm(new Zend_Form_SubForm(), 'baz')
  1348. ->baz->setElementsBelongTo('baz[baz]') // foo[foo][baz][baz][baz]
  1349. ->addElement('text', 'baz') // foo[foo][baz][baz][baz][baz]
  1350. ->baz->addValidator('Identical',
  1351. false,
  1352. array('baz Value'));
  1353. // This is appending a different named SubForm and setting
  1354. // elementsBelongTo to a !isArray() Subform name from same level
  1355. $this->form->foo->addSubForm(new Zend_Form_SubForm(), 'quo')
  1356. ->quo->setElementsBelongTo('foo') // foo[foo][foo] !!!!
  1357. ->addElement('text', 'quo') // foo[foo][foo][quo]
  1358. ->quo->addValidator('Identical',
  1359. false,
  1360. array('quo Value'));
  1361. // This is setting elementsBelongTo point into the middle of
  1362. // a chain of another SubForms elementsBelongTo
  1363. $this->form->addSubForm(new Zend_Form_SubForm(), 'duh')
  1364. ->duh->setElementsBelongTo('foo[zoo]') // foo[zoo] !!!!
  1365. ->addElement('text', 'zoo') // foo[zoo][zoo]
  1366. ->zoo->addValidator('Identical',
  1367. false,
  1368. array('zoo Value'));
  1369. // This is !isArray SubForms Name equal to the last segment
  1370. // of another SubForms elementsBelongTo
  1371. $this->form->addSubForm(new Zend_Form_SubForm(), 'iek')
  1372. ->iek->setElementsBelongTo('foo') // foo !!!!
  1373. ->addSubForm(new Zend_Form_SubForm(), 'zoo') // foo[zoo] !!!!
  1374. ->zoo->setIsArray(false)
  1375. ->addElement('text', 'iek') // foo[zoo][iek]
  1376. ->iek->addValidator('Identical',
  1377. false,
  1378. array('iek Value'));
  1379. $data = array('valid' => array('foo' =>
  1380. array('foo' =>
  1381. array('foo' =>
  1382. array('foo' => 'foo Value',
  1383. 'quo' => 'quo Value'),
  1384. 'baz' =>
  1385. array('baz' =>
  1386. array('baz' =>
  1387. array('baz' => 'baz Value')))),
  1388. 'zoo' =>
  1389. array('zoo' => 'zoo Value',
  1390. 'iek' => 'iek Value'))),
  1391. 'invalid' => array('foo' =>
  1392. array('foo' =>
  1393. array('foo' =>
  1394. array('foo' => 'foo Invalid',
  1395. 'quo' => 'quo Value'),
  1396. 'baz' =>
  1397. array('baz' =>
  1398. array('baz' =>
  1399. array('baz' => 'baz Value')))),
  1400. 'zoo' =>
  1401. array('zoo' => 'zoo Value',
  1402. 'iek' => 'iek Invalid'))),
  1403. 'partial' => array('foo' =>
  1404. array('foo' =>
  1405. array('baz' =>
  1406. array('baz' =>
  1407. array('baz' =>
  1408. array('baz' => 'baz Value'))),
  1409. 'foo' =>
  1410. array('quo' => 'quo Value')),
  1411. 'zoo' =>
  1412. array('zoo' => 'zoo Value'))));
  1413. return $data;
  1414. }
  1415. public function testIsValidEqualSubFormAndElementName()
  1416. {
  1417. $data = $this->_setup9350();
  1418. $this->assertTrue($this->form->isValid($data['valid']));
  1419. }
  1420. public function testIsValidPartialEqualSubFormAndElementName()
  1421. {
  1422. $data = $this->_setup9350();
  1423. $this->assertTrue($this->form->isValidPartial($data['partial']));
  1424. }
  1425. public function testPopulateWithElementsBelongTo()
  1426. {
  1427. $data = $this->_setup9350();
  1428. $this->form->setView($this->getView())->populate($data['valid']);
  1429. $html = $this->form->render();
  1430. $this->assertRegexp('/value=.foo Value./', $html);
  1431. $this->assertRegexp('/value=.baz Value./', $html);
  1432. $this->assertRegexp('/value=.quo Value./', $html);
  1433. $this->assertRegexp('/value=.zoo Value./', $html);
  1434. $this->assertRegexp('/value=.iek Value./', $html);
  1435. }
  1436. public function testGetValidValuesWithElementsBelongTo()
  1437. {
  1438. $data = $this->_setup9350();
  1439. $this->assertSame($this->form->getValidValues($data['invalid']), $data['partial']);
  1440. }
  1441. public function testGetErrorsWithElementsBelongTo()
  1442. {
  1443. $data = $this->_setup9350();
  1444. $this->form->isValid($data['invalid']);
  1445. $errors = $this->form->getErrors();
  1446. $this->assertTrue(isset($errors['foo']['foo']['foo']['foo']));
  1447. $this->assertTrue(isset($errors['foo']['zoo']['iek']));
  1448. }
  1449. public function testGetValuesWithElementsBelongTo()
  1450. {
  1451. $data = $this->_setup9350();
  1452. $this->form->populate($data['valid']);
  1453. $this->assertSame($this->form->getValues(), $data['valid']);
  1454. }
  1455. public function testGetMessagesWithElementsBelongTo()
  1456. {
  1457. $data = $this->_setup9350();
  1458. $this->form->isValid($data['invalid']);
  1459. $msgs = $this->form->getMessages();
  1460. $this->assertTrue(isset($msgs['foo']['foo']['foo']['foo']));
  1461. $this->assertTrue(isset($msgs['foo']['zoo']['iek']));
  1462. }
  1463. public function _setup9401()
  1464. {
  1465. $sub0 = 0;
  1466. $this->form->addSubForm(new Zend_Form_SubForm(), $sub0)
  1467. ->$sub0->setElementsBelongTo('f[2]')
  1468. ->addElement('text', 'foo')
  1469. ->foo->addValidator('Identical',
  1470. false,
  1471. array('foo Value'));
  1472. $this->form->$sub0->addSubForm(new Zend_Form_SubForm(), $sub0)
  1473. ->$sub0->addElement('text', 'quo')
  1474. ->quo->addValidator('Identical',
  1475. false,
  1476. array('quo Value'));
  1477. $data = array('valid' => array('f' =>
  1478. array(2 =>
  1479. array('foo' => 'foo Value',
  1480. 0 =>
  1481. array('quo' => 'quo Value')))),
  1482. 'invalid' => array('f' =>
  1483. array(2 =>
  1484. array('foo' => 'foo Invalid',
  1485. 0 =>
  1486. array('quo' => 'quo Value')))),
  1487. 'partial' => array('f' =>
  1488. array(2 =>
  1489. array(0 =>
  1490. array('quo' => 'quo Value')))));
  1491. return $data;
  1492. }
  1493. public function testGetErrorsNumericalSubForms()
  1494. {
  1495. $data = $this->_setup9401();
  1496. $this->form->isValid($data['invalid']);
  1497. $err = $this->form->getErrors();
  1498. $this->assertTrue(is_array($err['f'][2]['foo']) && !empty($err['f'][2]['foo']));
  1499. }
  1500. public function testGetMessagesNumericalSubForms()
  1501. {
  1502. $data = $this->_setup9401();
  1503. $this->form->isValid($data['invalid']);
  1504. $msg = $this->form->getMessages();
  1505. $this->assertTrue(is_array($msg['f'][2]['foo']) && !empty($msg['f'][2]['foo']));
  1506. }
  1507. public function testGetValuesNumericalSubForms()
  1508. {
  1509. $data = $this->_setup9401();
  1510. $this->form->populate($data['valid']);
  1511. $this->assertEquals($this->form->getValues(), $data['valid']);
  1512. }
  1513. public function testGetValidValuesNumericalSubForms()
  1514. {
  1515. $data = $this->_setup9401();
  1516. $this->assertEquals($this->form->getValidValues($data['invalid']), $data['partial']);
  1517. }
  1518. public function _setup9607()
  1519. {
  1520. $this->form->addElement('text', 'foo')
  1521. ->foo->setBelongsTo('bar[quo]')
  1522. ->setRequired(true)
  1523. ->addValidator('Identical',
  1524. false,
  1525. 'foo Value');
  1526. $this->form->addElement('text', 'quo')
  1527. ->quo->setBelongsTo('bar[quo]')
  1528. ->addValidator('Identical',
  1529. false,
  1530. 'quo Value');
  1531. $data = array('valid' => array('bar' =>
  1532. array('quo' =>
  1533. array('foo' => 'foo Value',
  1534. 'quo' => 'quo Value'))),
  1535. 'invalid' => array('bar' =>
  1536. array('quo' =>
  1537. array('foo' => 'foo Invalid',
  1538. 'quo' => 'quo Value'))),
  1539. 'partial' => array('bar' =>
  1540. array('quo' =>
  1541. array('quo' => 'quo Value'))));
  1542. return $data;
  1543. }
  1544. public function testIsValidWithBelongsTo()
  1545. {
  1546. $data = $this->_setup9607();
  1547. $this->assertTrue($this->form->isValid($data['valid']));
  1548. }
  1549. public function testIsValidPartialWithBelongsTo()
  1550. {
  1551. $data = $this->_setup9607();
  1552. $this->assertTrue($this->form->isValidPartial($data['valid']));
  1553. $this->assertSame('foo Value', $this->form->foo->getValue());
  1554. }
  1555. public function testPopulateWithBelongsTo()
  1556. {
  1557. $data = $this->_setup9607();
  1558. $this->form->populate($data['valid']);
  1559. $this->assertSame('foo Value', $this->form->foo->getValue());
  1560. }
  1561. public function testGetValuesWithBelongsTo()
  1562. {
  1563. $data = $this->_setup9607();
  1564. $this->form->populate($data['valid']);
  1565. $this->assertSame($data['valid'], $this->form->getValues());
  1566. }
  1567. public function testGetValidValuesWithBelongsTo()
  1568. {
  1569. $data = $this->_setup9607();
  1570. $this->assertSame($data['partial'], $this->form->getValidValues($data['invalid']));
  1571. }
  1572. public function testZF9788_NumericArrayIndex()
  1573. {
  1574. $s = 2;
  1575. $e = 4;
  1576. $this->form->setName('f')
  1577. ->setIsArray(true)
  1578. ->addElement('text', (string)$e)
  1579. ->$e->setRequired(true);
  1580. $this->form->addSubForm(new Zend_Form_SubForm(), $s)
  1581. ->$s->addElement('text', (string)$e)
  1582. ->$e->setRequired(true);
  1583. $valid = array('f' => array($e => 1,
  1584. $s => array($e => 1)));
  1585. $this->form->populate($valid);
  1586. $this->assertEquals($valid, $this->form->getValues());
  1587. $vv = $this->form->getValidValues(array('f' => array($e => 1,
  1588. $s => array($e => 1))));
  1589. $this->assertEquals($valid, $vv);
  1590. $this->form->isValid(array());
  1591. $err = $this->form->getErrors();
  1592. $msg = $this->form->getMessages();
  1593. $this->assertTrue(is_array($err['f'][$e]) && is_array($err['f'][$s][$e]));
  1594. $this->assertTrue(is_array($msg['f'][$e]) && is_array($msg['f'][$s][$e]));
  1595. }
  1596. // Display groups
  1597. public function testCanAddAndRetrieveSingleDisplayGroups()
  1598. {
  1599. $this->testCanAddAndRetrieveMultipleElements();
  1600. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  1601. $group = $this->form->getDisplayGroup('barbat');
  1602. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1603. $elements = $group->getElements();
  1604. $expected = array('bar' => $this->form->bar, 'bat' => $this->form->bat);
  1605. $this->assertEquals($expected, $elements);
  1606. }
  1607. public function testDisplayGroupsMustContainAtLeastOneElement()
  1608. {
  1609. try {
  1610. $this->form->addDisplayGroup(array(), 'foo');
  1611. $this->fail('Empty display group should raise exception');
  1612. } catch (Zend_Form_Exception $e) {
  1613. $this->assertContains('No valid elements', $e->getMessage());
  1614. }
  1615. }
  1616. public function testCanAddAndRetrieveMultipleDisplayGroups()
  1617. {
  1618. $this->testCanAddAndRetrieveMultipleElements();
  1619. $this->form->addDisplayGroups(array(
  1620. array(array('bar', 'bat'), 'barbat'),
  1621. 'foobaz' => array('baz', 'foo')
  1622. ));
  1623. $groups = $this->form->getDisplayGroups();
  1624. $expected = array(
  1625. 'barbat' => array('bar' => $this->form->bar, 'bat' => $this->form->bat),
  1626. 'foobaz' => array('baz' => $this->form->baz, 'foo' => $this->form->foo),
  1627. );
  1628. foreach ($groups as $group) {
  1629. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1630. }
  1631. $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
  1632. $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
  1633. }
  1634. public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
  1635. {
  1636. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1637. $this->form->setDisplayGroups(array('foobar' => array('bar', 'foo')));
  1638. $groups = $this->form->getDisplayGroups();
  1639. $expected = array('bar' => $this->form->bar, 'foo' => $this->form->foo);
  1640. $this->assertEquals(1, count($groups));
  1641. $this->assertTrue(isset($groups['foobar']));
  1642. $this->assertEquals($expected, $groups['foobar']->getElements());
  1643. }
  1644. public function testCanRemoveSingleDisplayGroup()
  1645. {
  1646. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1647. $this->assertTrue($this->form->removeDisplayGroup('barbat'));
  1648. $this->assertNull($this->form->getDisplayGroup('barbat'));
  1649. }
  1650. public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
  1651. {
  1652. $this->assertFalse($this->form->removeDisplayGroup('bogus'));
  1653. }
  1654. public function testCanClearAllDisplayGroups()
  1655. {
  1656. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1657. $this->form->clearDisplayGroups();
  1658. $groups = $this->form->getDisplayGroups();
  1659. $this->assertTrue(is_array($groups));
  1660. $this->assertTrue(empty($groups));
  1661. }
  1662. public function testOverloadingDisplayGroups()
  1663. {
  1664. $this->testCanAddAndRetrieveMultipleElements();
  1665. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1666. $this->assertTrue(isset($this->form->foobar));
  1667. $group = $this->form->foobar;
  1668. $expected = array('foo' => $this->form->foo, 'bar' => $this->form->bar);
  1669. $this->assertEquals($expected, $group->getElements());
  1670. unset($this->form->foobar);
  1671. $this->assertFalse(isset($this->form->foobar));
  1672. $this->form->barbaz = array('bar', 'baz');
  1673. $this->assertTrue(isset($this->form->barbaz));
  1674. $group = $this->form->barbaz;
  1675. $expected = array('bar' => $this->form->bar, 'baz' => $this->form->baz);
  1676. $this->assertSame($expected, $group->getElements());
  1677. }
  1678. public function testDefaultDisplayGroupClassExists()
  1679. {
  1680. $this->assertEquals('Zend_Form_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1681. }
  1682. public function testCanSetDefaultDisplayGroupClass()
  1683. {
  1684. $this->testDefaultDisplayGroupClassExists();
  1685. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1686. $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1687. }
  1688. public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
  1689. {
  1690. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1691. $this->setupElements();
  1692. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1693. $displayGroup = $this->form->getDisplayGroup('foobar');
  1694. $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup);
  1695. }
  1696. public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
  1697. {
  1698. $this->setupElements();
  1699. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
  1700. $this->assertTrue($this->form->foobar instanceof Zend_Form_FormTest_DisplayGroup);
  1701. }
  1702. /**
  1703. * @group ZF-3254
  1704. */
  1705. public function testAddingDisplayGroupShouldPassOptions()
  1706. {
  1707. $this->testCanAddAndRetrieveMultipleElements();
  1708. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
  1709. $group = $this->form->getDisplayGroup('barbat');
  1710. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1711. $decorators = $group->getDecorators();
  1712. $this->assertTrue(is_array($decorators));
  1713. $this->assertTrue(empty($decorators));
  1714. }
  1715. // Processing
  1716. public function testPopulateProxiesToSetDefaults()
  1717. {
  1718. $this->testCanAddAndRetrieveMultipleElements();
  1719. $values = array(
  1720. 'foo' => 'foovalue',
  1721. 'bar' => 'barvalue',
  1722. 'baz' => 'bazvalue',
  1723. 'bat' => 'batvalue'
  1724. );
  1725. $this->form->populate($values);
  1726. $test = $this->form->getValues();
  1727. $elements = $this->form->getElements();
  1728. foreach (array_keys($values) as $name) {
  1729. $this->assertEquals($values[$name], $test[$name]);
  1730. }
  1731. }
  1732. public function setupElements()
  1733. {
  1734. $foo = new Zend_Form_Element_Text('foo');
  1735. $foo->addValidator('NotEmpty')
  1736. ->addValidator('Alpha');
  1737. $bar = new Zend_Form_Element_Text('bar');
  1738. $bar->addValidator('NotEmpty')
  1739. ->addValidator('Digits');
  1740. $baz = new Zend_Form_Element_Text('baz');
  1741. $baz->addValidator('NotEmpty')
  1742. ->addValidator('Alnum');
  1743. $this->form->addElements(array($foo, $bar, $baz));
  1744. $this->elementValues = array(
  1745. 'foo' => 'fooBarBAZ',
  1746. 'bar' => '123456789',
  1747. 'baz' => 'foo123BAR',
  1748. );
  1749. }
  1750. public function testIsValidShouldThrowExceptionWithNonArrayArgument()
  1751. {
  1752. try {
  1753. $this->form->isValid(true);
  1754. $this->fail('isValid() should raise exception with non-array argument');
  1755. } catch (Zend_Form_Exception $e) {
  1756. $this->assertContains('expects an array', $e->getMessage());
  1757. }
  1758. }
  1759. public function testCanValidateFullFormContainingOnlyElements()
  1760. {
  1761. $this->_checkZf2794();
  1762. $this->setupElements();
  1763. $this->assertTrue($this->form->isValid($this->elementValues));
  1764. $values = array(
  1765. 'foo' => '12345',
  1766. 'bar' => 'abc',
  1767. 'baz' => 'abc-123'
  1768. );
  1769. $this->assertFalse($this->form->isValid($values));
  1770. $validator = $this->form->foo->getValidator('alpha');
  1771. $this->assertEquals('12345', $validator->value);
  1772. $validator = $this->form->bar->getValidator('digits');
  1773. $this->assertEquals('abc', $validator->value);
  1774. $validator = $this->form->baz->getValidator('alnum');
  1775. $this->assertEquals('abc-123', $validator->value);
  1776. }
  1777. public function testValidationTakesElementRequiredFlagsIntoAccount()
  1778. {
  1779. $this->_checkZf2794();
  1780. $this->setupElements();
  1781. $this->assertTrue($this->form->isValid(array()));
  1782. $this->form->getElement('foo')->setRequired(true);
  1783. $this->assertTrue($this->form->isValid(array(
  1784. 'foo' => 'abc',
  1785. 'baz' => 'abc123'
  1786. )));
  1787. $this->assertFalse($this->form->isValid(array(
  1788. 'baz' => 'abc123'
  1789. )));
  1790. }
  1791. public function testCanValidatePartialFormContainingOnlyElements()
  1792. {
  1793. $this->_checkZf2794();
  1794. $this->setupElements();
  1795. $this->form->getElement('foo')->setRequired(true);
  1796. $this->form->getElement('bar')->setRequired(true);
  1797. $this->form->getElement('baz')->setRequired(true);
  1798. $this->assertTrue($this->form->isValidPartial(array(
  1799. 'foo' => 'abc',
  1800. 'baz' => 'abc123'
  1801. )));
  1802. $this->assertFalse($this->form->isValidPartial(array(
  1803. 'foo' => '123',
  1804. 'baz' => 'abc-123'
  1805. )));
  1806. }
  1807. public function setupSubForm()
  1808. {
  1809. $subForm = new Zend_Form_SubForm();
  1810. $foo = new Zend_Form_Element_Text('subfoo');
  1811. $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
  1812. $bar = new Zend_Form_Element_Text('subbar');
  1813. $bar->addValidators(array('NotEmpty', 'Digits'));
  1814. $baz = new Zend_Form_Element_Text('subbaz');
  1815. $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
  1816. $subForm->addElements(array($foo, $bar, $baz));
  1817. $this->form->addSubForm($subForm, 'sub');
  1818. }
  1819. public function testFullDataArrayUsedToValidateSubFormByDefault()
  1820. {
  1821. $this->_checkZf2794();
  1822. $this->setupElements();
  1823. $this->setupSubForm();
  1824. $data = array(
  1825. 'foo' => 'abcdef',
  1826. 'bar' => '123456',
  1827. 'baz' => '123abc',
  1828. 'subfoo' => 'abcdef',
  1829. 'subbar' => '123456',
  1830. 'subbaz' => '123abc',
  1831. );
  1832. $this->assertTrue($this->form->isValid($data));
  1833. $data = array(
  1834. 'foo' => 'abcdef',
  1835. 'bar' => '123456',
  1836. 'baz' => '123abc',
  1837. 'subfoo' => '123',
  1838. 'subbar' => 'abc',
  1839. 'subbaz' => '123-abc',
  1840. );
  1841. $this->assertFalse($this->form->isValid($data));
  1842. $data = array(
  1843. 'foo' => 'abcdef',
  1844. 'bar' => '123456',
  1845. 'baz' => '123abc',
  1846. 'subfoo' => 'abc',
  1847. 'subbaz' => '123abc',
  1848. );
  1849. $this->assertTrue($this->form->isValid($data));
  1850. $data = array(
  1851. 'foo' => 'abcdef',
  1852. 'bar' => '123456',
  1853. 'baz' => '123abc',
  1854. 'subbar' => '123',
  1855. 'subbaz' => '123abc',
  1856. );
  1857. $this->assertFalse($this->form->isValid($data));
  1858. }
  1859. public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
  1860. {
  1861. $this->_checkZf2794();
  1862. $this->setupElements();
  1863. $this->setupSubForm();
  1864. $data = array(
  1865. 'foo' => 'abcdef',
  1866. 'bar' => '123456',
  1867. 'baz' => '123abc',
  1868. 'sub' => array(
  1869. 'subfoo' => 'abcdef',
  1870. 'subbar' => '123456',
  1871. 'subbaz' => '123abc',
  1872. ),
  1873. );
  1874. $this->assertTrue($this->form->isValid($data));
  1875. $data = array(
  1876. 'foo' => 'abcdef',
  1877. 'bar' => '123456',
  1878. 'baz' => '123abc',
  1879. 'sub' => array(
  1880. 'subfoo' => '123',
  1881. 'subbar' => 'abc',
  1882. 'subbaz' => '123-abc',
  1883. )
  1884. );
  1885. $this->assertFalse($this->form->isValid($data));
  1886. $data = array(
  1887. 'foo' => 'abcdef',
  1888. 'bar' => '123456',
  1889. 'baz' => '123abc',
  1890. 'sub' => array(
  1891. 'subfoo' => 'abc',
  1892. 'subbaz' => '123abc',
  1893. )
  1894. );
  1895. $this->assertTrue($this->form->isValid($data));
  1896. $data = array(
  1897. 'foo' => 'abcdef',
  1898. 'bar' => '123456',
  1899. 'baz' => '123abc',
  1900. 'sub' => array(
  1901. 'subbar' => '123',
  1902. 'subbaz' => '123abc',
  1903. )
  1904. );
  1905. $this->assertFalse($this->form->isValid($data));
  1906. }
  1907. public function testCanValidateNestedFormsWithElementsBelongingToArrays()
  1908. {
  1909. $form = new Zend_Form();
  1910. $form->setElementsBelongTo('foobar');
  1911. $form->addElement('text', 'firstName')
  1912. ->getElement('firstName')
  1913. ->setRequired(true);
  1914. $form->addElement('text', 'lastName')
  1915. ->getElement('lastName')
  1916. ->setRequired(true);
  1917. $subForm = new Zend_Form_SubForm();
  1918. $subForm->setElementsBelongTo('baz');
  1919. $subForm->addElement('text', 'email')
  1920. ->getElement('email')->setRequired(true);
  1921. $subSubForm = new Zend_Form_SubForm();
  1922. $subSubForm->setElementsBelongTo('bat');
  1923. $subSubForm->addElement('checkbox', 'home')
  1924. ->getElement('home')->setRequired(true);
  1925. $subForm->addSubForm($subSubForm, 'subSub');
  1926. $form->addSubForm($subForm, 'sub')
  1927. ->addElement('submit', 'save', array('value' => 'submit'));
  1928. $data = array('foobar' => array(
  1929. 'firstName' => 'Mabel',
  1930. 'lastName' => 'Cow',
  1931. 'baz' => array(
  1932. 'email' => 'mabel@cow.org',
  1933. 'bat' => array(
  1934. 'home' => 1,
  1935. )
  1936. )
  1937. ));
  1938. $this->assertTrue($form->isValid($data));
  1939. $this->assertEquals('Mabel', $form->firstName->getValue());
  1940. $this->assertEquals('Cow', $form->lastName->getValue());
  1941. $this->assertEquals('mabel@cow.org', $form->sub->email->getValue());
  1942. $this->assertEquals(1, $form->sub->subSub->home->getValue());
  1943. }
  1944. public function testCanValidatePartialFormContainingSubForms()
  1945. {
  1946. $this->_checkZf2794();
  1947. $this->setupElements();
  1948. $this->setupSubForm();
  1949. $data = array(
  1950. 'subfoo' => 'abcdef',
  1951. 'subbar' => '123456',
  1952. );
  1953. $this->assertTrue($this->form->isValidPartial($data));
  1954. $data = array(
  1955. 'foo' => 'abcdef',
  1956. 'baz' => '123abc',
  1957. 'sub' => array(
  1958. 'subbar' => '123',
  1959. )
  1960. );
  1961. $this->assertTrue($this->form->isValidPartial($data));
  1962. $data = array(
  1963. 'foo' => 'abcdef',
  1964. 'bar' => '123456',
  1965. 'baz' => '123abc',
  1966. 'sub' => array(
  1967. 'subfoo' => '123',
  1968. )
  1969. );
  1970. $this->assertFalse($this->form->isValidPartial($data));
  1971. }
  1972. public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
  1973. {
  1974. $this->_checkZf2794();
  1975. $form = new Zend_Form();
  1976. $form->setElementsBelongTo('foobar');
  1977. $form->addElement('text', 'firstName')
  1978. ->getElement('firstName')
  1979. ->setRequired(false);
  1980. $form->addElement('text', 'lastName')
  1981. ->getElement('lastName')
  1982. ->setRequired(true);
  1983. $subForm = new Zend_Form_SubForm();
  1984. $subForm->setElementsBelongTo('baz');
  1985. $subForm->addElement('text', 'email')
  1986. ->getElement('email')
  1987. ->setRequired(true)
  1988. ->addValidator('NotEmpty');
  1989. $subSubForm = new Zend_Form_SubForm();
  1990. $subSubForm->setElementsBelongTo('bat');
  1991. $subSubForm->addElement('checkbox', 'home')
  1992. ->getElement('home')
  1993. ->setRequired(true)
  1994. ->addValidator('InArray', false, array(array('1')));
  1995. $subForm->addSubForm($subSubForm, 'subSub');
  1996. $form->addSubForm($subForm, 'sub')
  1997. ->addElement('submit', 'save', array('value' => 'submit'));
  1998. $data = array('foobar' => array(
  1999. 'lastName' => 'Cow',
  2000. ));
  2001. $this->assertTrue($form->isValidPartial($data));
  2002. $this->assertEquals('Cow', $form->lastName->getValue());
  2003. $firstName = $form->firstName->getValue();
  2004. $email = $form->sub->email->getValue();
  2005. $home = $form->sub->subSub->home->getValue();
  2006. $this->assertTrue(empty($firstName));
  2007. $this->assertTrue(empty($email));
  2008. $this->assertTrue(empty($home));
  2009. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  2010. $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
  2011. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  2012. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  2013. $messages = $form->getMessages();
  2014. $this->assertFalse(empty($messages));
  2015. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
  2016. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
  2017. }
  2018. public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
  2019. {
  2020. $this->_checkZf2794();
  2021. $form = new Zend_Form();
  2022. $form->setElementsBelongTo('foo[bar]');
  2023. $form->addElement('text', 'firstName')
  2024. ->getElement('firstName')
  2025. ->setRequired(false);
  2026. $form->addElement('text', 'lastName')
  2027. ->getElement('lastName')
  2028. ->setRequired(true);
  2029. $subForm = new Zend_Form_SubForm();
  2030. $subForm->setElementsBelongTo('baz');
  2031. $subForm->addElement('text', 'email')
  2032. ->getElement('email')
  2033. ->setRequired(true)
  2034. ->addValidator('NotEmpty');
  2035. $subSubForm = new Zend_Form_SubForm();
  2036. $subSubForm->setElementsBelongTo('bat[quux]');
  2037. $subSubForm->addElement('checkbox', 'home')
  2038. ->getElement('home')
  2039. ->setRequired(true)
  2040. ->addValidator('InArray', false, array(array('1')));
  2041. $subForm->addSubForm($subSubForm, 'subSub');
  2042. $form->addSubForm($subForm, 'sub')
  2043. ->addElement('submit', 'save', array('value' => 'submit'));
  2044. $data = array('foo' => array(
  2045. 'bar' => array(
  2046. 'lastName' => 'Cow',
  2047. ),
  2048. ));
  2049. $this->assertTrue($form->isValidPartial($data));
  2050. $this->assertEquals('Cow', $form->lastName->getValue());
  2051. $firstName = $form->firstName->getValue();
  2052. $email = $form->sub->email->getValue();
  2053. $home = $form->sub->subSub->home->getValue();
  2054. $this->assertTrue(empty($firstName));
  2055. $this->assertTrue(empty($email));
  2056. $this->assertTrue(empty($home));
  2057. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  2058. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  2059. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  2060. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  2061. }
  2062. public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
  2063. {
  2064. $this->_checkZf2794();
  2065. $form = new Zend_Form();
  2066. $form->setElementsBelongTo('foo[bar]');
  2067. $form->addElement('text', 'firstName')
  2068. ->getElement('firstName')
  2069. ->setRequired(false);
  2070. $form->addElement('text', 'lastName')
  2071. ->getElement('lastName')
  2072. ->setRequired(true);
  2073. $subForm = new Zend_Form_SubForm();
  2074. $subForm->setElementsBelongTo('baz');
  2075. $subForm->addElement('text', 'email')
  2076. ->getElement('email')
  2077. ->setRequired(true)
  2078. ->addValidator('NotEmpty');
  2079. $subSubForm = new Zend_Form_SubForm();
  2080. $subSubForm->setElementsBelongTo('bat[quux]');
  2081. $subSubForm->addElement('checkbox', 'home')
  2082. ->getElement('home')
  2083. ->setRequired(true)
  2084. ->addValidator('InArray', false, array(array('1')));
  2085. $subForm->addSubForm($subSubForm, 'subSub');
  2086. $form->addSubForm($subForm, 'sub')
  2087. ->addElement('submit', 'save', array('value' => 'submit'));
  2088. $data = array('foo' => array(
  2089. 'bar' => array(
  2090. 'lastName' => 'Cow',
  2091. ),
  2092. ));
  2093. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  2094. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  2095. $form->isValidPartial($data);
  2096. $messages = $form->getMessages();
  2097. $this->assertFalse(empty($messages));
  2098. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
  2099. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
  2100. }
  2101. public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
  2102. {
  2103. $this->setupElements();
  2104. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2105. $this->assertTrue($this->form->isValid($this->elementValues));
  2106. $this->assertFalse($this->form->isValid(array(
  2107. 'foo' => '123',
  2108. 'bar' => 'abc',
  2109. 'baz' => 'abc-123'
  2110. )));
  2111. }
  2112. public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
  2113. {
  2114. $this->setupElements();
  2115. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2116. $this->assertTrue($this->form->isValid(array(
  2117. 'foo' => 'abc',
  2118. 'baz' => 'abc123'
  2119. )));
  2120. $this->assertFalse($this->form->isValid(array(
  2121. 'foo' => '123',
  2122. 'baz' => 'abc-123'
  2123. )));
  2124. }
  2125. public function testProcessAjaxReturnsJsonTrueForValidForm()
  2126. {
  2127. $this->setupElements();
  2128. $return = $this->form->processAjax($this->elementValues);
  2129. $this->assertTrue(Zend_Json::decode($return));
  2130. }
  2131. public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
  2132. {
  2133. $this->setupElements();
  2134. $data = array('foo' => 'abcdef', 'baz' => 'abc123');
  2135. $return = $this->form->processAjax($data);
  2136. $this->assertTrue(Zend_Json::decode($return));
  2137. }
  2138. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
  2139. {
  2140. $this->setupElements();
  2141. $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
  2142. $return = Zend_Json::decode($this->form->processAjax($data));
  2143. $this->assertTrue(is_array($return));
  2144. $this->assertEquals(array_keys($data), array_keys($return));
  2145. }
  2146. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
  2147. {
  2148. $this->setupElements();
  2149. $data = array('baz' => 'abc-123');
  2150. $return = Zend_Json::decode($this->form->processAjax($data));
  2151. $this->assertTrue(is_array($return));
  2152. $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
  2153. }
  2154. public function testPersistDataStoresDataInSession()
  2155. {
  2156. $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
  2157. }
  2158. public function testCanCheckIfErrorsAreRegistered()
  2159. {
  2160. $this->assertFalse($this->form->hasErrors());
  2161. $this->testCanValidateFullFormContainingOnlyElements();
  2162. $this->assertTrue($this->form->hasErrors());
  2163. }
  2164. /**
  2165. * @group ZF-9914
  2166. */
  2167. public function testDeprecatedIsErrorsProxiesToHasErrors()
  2168. {
  2169. $this->testCanValidateFullFormContainingOnlyElements();
  2170. $this->assertEquals($this->form->isErrors(), $this->form->hasErrors());
  2171. }
  2172. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  2173. {
  2174. $this->_checkZf2794();
  2175. $this->testCanValidateFullFormContainingOnlyElements();
  2176. $codes = $this->form->getErrors();
  2177. $keys = array('foo', 'bar', 'baz');
  2178. $this->assertEquals($keys, array_keys($codes));
  2179. }
  2180. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  2181. {
  2182. $this->_checkZf2794();
  2183. $this->testCanValidateFullFormContainingOnlyElements();
  2184. $codes = $this->form->getErrors();
  2185. $keys = array('foo', 'bar', 'baz');
  2186. $errors = $this->form->getErrors('foo');
  2187. $foo = $this->form->foo;
  2188. $this->assertEquals($foo->getErrors(), $errors);
  2189. }
  2190. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  2191. {
  2192. $this->_checkZf2794();
  2193. $this->testCanValidateFullFormContainingOnlyElements();
  2194. $codes = $this->form->getMessages();
  2195. $keys = array('foo', 'bar', 'baz');
  2196. $this->assertEquals($keys, array_keys($codes));
  2197. }
  2198. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  2199. {
  2200. $this->_checkZf2794();
  2201. $this->testCanValidateFullFormContainingOnlyElements();
  2202. $codes = $this->form->getMessages();
  2203. $keys = array('foo', 'bar', 'baz');
  2204. $messages = $this->form->getMessages('foo');
  2205. $foo = $this->form->foo;
  2206. $this->assertEquals($foo->getMessages(), $messages);
  2207. }
  2208. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  2209. {
  2210. $this->_checkZf2794();
  2211. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2212. $codes = $this->form->getErrors();
  2213. $this->assertTrue(array_key_exists('sub', $codes));
  2214. $this->assertTrue(is_array($codes['sub']));
  2215. $keys = array('subfoo', 'subbar', 'subbaz');
  2216. $this->assertEquals($keys, array_keys($codes['sub']));
  2217. }
  2218. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  2219. {
  2220. $this->_checkZf2794();
  2221. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2222. $codes = $this->form->getErrors('sub');
  2223. $this->assertTrue(is_array($codes));
  2224. $this->assertFalse(empty($codes));
  2225. $keys = array('subfoo', 'subbar', 'subbaz');
  2226. $this->assertEquals($keys, array_keys($codes));
  2227. }
  2228. public function testGetErrorsHonorsElementsBelongTo()
  2229. {
  2230. $this->_checkZf2794();
  2231. $subForm = new Zend_Form_SubForm();
  2232. $subForm->setElementsBelongTo('foo[bar]');
  2233. $subForm->addElement('text', 'test')->test
  2234. ->setRequired(true);
  2235. $this->form->addSubForm($subForm, 'sub');
  2236. $data = array('foo' => array(
  2237. 'bar' => array(
  2238. 'test' => '',
  2239. ),
  2240. ));
  2241. $this->form->isValid($data);
  2242. $codes = $this->form->getErrors();
  2243. $this->assertFalse(empty($codes['foo']['bar']['test']));
  2244. }
  2245. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  2246. {
  2247. $this->_checkZf2794();
  2248. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2249. $data = array(
  2250. 'foo' => 'abcdef',
  2251. 'bar' => '123456',
  2252. 'baz' => '123abc',
  2253. 'subfoo' => '123',
  2254. 'subbar' => 'abc',
  2255. 'subbaz' => '123-abc',
  2256. );
  2257. $this->assertFalse($this->form->isValid($data));
  2258. $codes = $this->form->getMessages();
  2259. $this->assertTrue(array_key_exists('sub', $codes));
  2260. $this->assertTrue(is_array($codes['sub']));
  2261. $keys = array('subfoo', 'subbar', 'subbaz');
  2262. $this->assertEquals($keys, array_keys($codes['sub']));
  2263. }
  2264. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  2265. {
  2266. $this->_checkZf2794();
  2267. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2268. $data = array(
  2269. 'foo' => 'abcdef',
  2270. 'bar' => '123456',
  2271. 'baz' => '123abc',
  2272. 'subfoo' => '123',
  2273. 'subbar' => 'abc',
  2274. 'subbaz' => '123-abc',
  2275. );
  2276. $this->assertFalse($this->form->isValid($data));
  2277. $codes = $this->form->getMessages('sub');
  2278. $this->assertTrue(is_array($codes));
  2279. $this->assertFalse(empty($codes));
  2280. $keys = array('subfoo', 'subbar', 'subbaz');
  2281. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  2282. }
  2283. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  2284. {
  2285. $this->_checkZf2794();
  2286. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2287. $translate = new Zend_Translate('array', $translations, 'en');
  2288. $translate->setLocale('en');
  2289. $this->form->addElements(array(
  2290. 'foo' => array(
  2291. 'type' => 'text',
  2292. 'options' => array(
  2293. 'required' => true,
  2294. 'validators' => array('NotEmpty')
  2295. )
  2296. ),
  2297. 'bar' => array(
  2298. 'type' => 'text',
  2299. 'options' => array(
  2300. 'required' => true,
  2301. 'validators' => array('Digits')
  2302. )
  2303. ),
  2304. ))
  2305. ->setTranslator($translate);
  2306. $data = array(
  2307. 'foo' => '',
  2308. 'bar' => 'abc',
  2309. );
  2310. if ($this->form->isValid($data)) {
  2311. $this->fail('Form should not validate');
  2312. }
  2313. $messages = $this->form->getMessages();
  2314. $this->assertTrue(isset($messages['foo']));
  2315. $this->assertTrue(isset($messages['bar']));
  2316. foreach ($messages['foo'] as $key => $message) {
  2317. if (array_key_exists($key, $translations)) {
  2318. $this->assertEquals($translations[$key], $message);
  2319. } else {
  2320. $this->fail('Translation for ' . $key . ' does not exist?');
  2321. }
  2322. }
  2323. foreach ($messages['bar'] as $key => $message) {
  2324. if (array_key_exists($key, $translations)) {
  2325. $this->assertEquals($translations[$key], $message);
  2326. } else {
  2327. $this->fail('Translation for ' . $key . ' does not exist?');
  2328. }
  2329. }
  2330. }
  2331. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2332. {
  2333. $this->_checkZf2794();
  2334. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2335. $translate = new Zend_Translate('array', $translations, 'en');
  2336. $translate->setLocale('en');
  2337. $this->form->addElements(array(
  2338. 'foo' => array(
  2339. 'type' => 'text',
  2340. 'options' => array(
  2341. 'required' => true,
  2342. 'validators' => array('NotEmpty')
  2343. )
  2344. ),
  2345. 'bar' => array(
  2346. 'type' => 'text',
  2347. 'options' => array(
  2348. 'required' => true,
  2349. 'validators' => array('Digits')
  2350. )
  2351. ),
  2352. ))
  2353. ->setTranslator($translate);
  2354. $data = array(
  2355. 'foo' => '',
  2356. );
  2357. if ($this->form->isValidPartial($data)) {
  2358. $this->fail('Form should not validate');
  2359. }
  2360. $messages = $this->form->getMessages();
  2361. $this->assertTrue(isset($messages['foo']));
  2362. $this->assertFalse(isset($messages['bar']));
  2363. foreach ($messages['foo'] as $key => $message) {
  2364. if (array_key_exists($key, $translations)) {
  2365. $this->assertEquals($translations[$key], $message);
  2366. } else {
  2367. $this->fail('Translation for ' . $key . ' does not exist?');
  2368. }
  2369. }
  2370. }
  2371. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2372. {
  2373. $this->_checkZf2794();
  2374. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2375. $translate = new Zend_Translate('array', $translations, 'en');
  2376. $translate->setLocale('en');
  2377. $this->form->addElements(array(
  2378. 'foo' => array(
  2379. 'type' => 'text',
  2380. 'options' => array(
  2381. 'required' => true,
  2382. 'validators' => array('NotEmpty')
  2383. )
  2384. ),
  2385. 'bar' => array(
  2386. 'type' => 'text',
  2387. 'options' => array(
  2388. 'required' => true,
  2389. 'validators' => array('Digits')
  2390. )
  2391. ),
  2392. ))
  2393. ->setTranslator($translate);
  2394. $data = array(
  2395. 'foo' => '',
  2396. );
  2397. $return = $this->form->processAjax($data);
  2398. $messages = Zend_Json::decode($return);
  2399. $this->assertTrue(is_array($messages));
  2400. $this->assertTrue(isset($messages['foo']));
  2401. $this->assertFalse(isset($messages['bar']));
  2402. foreach ($messages['foo'] as $key => $message) {
  2403. if (array_key_exists($key, $translations)) {
  2404. $this->assertEquals($translations[$key], $message);
  2405. } else {
  2406. $this->fail('Translation for ' . $key . ' does not exist?');
  2407. }
  2408. }
  2409. }
  2410. /**
  2411. * @Group ZF-9697
  2412. */
  2413. public function _setup9697()
  2414. {
  2415. $callback = function($value, $options) {
  2416. return (isset($options["bar"]["quo"]["foo"]) && "foo Value" === $options["bar"]["quo"]["foo"]);
  2417. };
  2418. $this->form->addElement('text', 'foo')
  2419. ->foo->setBelongsTo('bar[quo]');
  2420. $this->form->addElement('text', 'quo')
  2421. ->quo->setBelongsTo('bar[quo]')
  2422. ->addValidator('Callback',
  2423. false,
  2424. $callback);
  2425. return array('bar' => array('quo' => array('foo' => 'foo Value',
  2426. 'quo' => 'quo Value')));
  2427. }
  2428. public function testIsValidKeepsContext()
  2429. {
  2430. $data = $this->_setup9697();
  2431. $this->assertTrue($this->form->isValid($data));
  2432. }
  2433. public function testIsValidPartialKeepsContext()
  2434. {
  2435. $data = $this->_setup9697();
  2436. $this->assertTrue($this->form->isValidPartial($data));
  2437. }
  2438. public function testGetValidValuesKeepsContext()
  2439. {
  2440. $data = $this->_setup9697();
  2441. $this->assertSame($data, $this->form->getValidValues($data));
  2442. }
  2443. /**
  2444. * @group ZF-2988
  2445. */
  2446. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2447. {
  2448. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2449. $this->form->addErrorMessage('Invalid values entered');
  2450. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2451. $messages = $this->form->getMessages();
  2452. $this->assertEquals(1, count($messages));
  2453. $this->assertEquals('Invalid values entered', array_shift($messages));
  2454. }
  2455. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2456. {
  2457. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2458. $this->form->addErrorMessage('Invalid values entered');
  2459. $this->form->addErrorMessage('Really, they are not valid');
  2460. $messages = $this->form->getErrorMessages();
  2461. $this->assertEquals(2, count($messages));
  2462. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2463. $messages = $this->form->getMessages();
  2464. $this->assertEquals(2, count($messages));
  2465. $this->assertEquals('Invalid values entered', array_shift($messages));
  2466. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2467. }
  2468. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2469. {
  2470. $set1 = array('foo', 'bar', 'baz');
  2471. $this->form->addErrorMessages($set1);
  2472. $this->assertSame($set1, $this->form->getErrorMessages());
  2473. }
  2474. public function testSetErrorMessagesShouldOverwriteMessages()
  2475. {
  2476. $set1 = array('foo', 'bar', 'baz');
  2477. $set2 = array('bat', 'cat');
  2478. $this->form->addErrorMessages($set1);
  2479. $this->assertSame($set1, $this->form->getErrorMessages());
  2480. $this->form->setErrorMessages($set2);
  2481. $this->assertSame($set2, $this->form->getErrorMessages());
  2482. }
  2483. public function testCustomErrorMessageStackShouldBeClearable()
  2484. {
  2485. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2486. $this->form->clearErrorMessages();
  2487. $messages = $this->form->getErrorMessages();
  2488. $this->assertTrue(empty($messages));
  2489. }
  2490. public function testCustomErrorMessagesShouldBeTranslated()
  2491. {
  2492. $translations = array(
  2493. 'foo' => 'Foo message',
  2494. );
  2495. $translate = new Zend_Translate('array', $translations);
  2496. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2497. $this->form->setTranslator($translate)
  2498. ->addErrorMessage('foo');
  2499. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2500. $messages = $this->form->getMessages();
  2501. $this->assertEquals(1, count($messages));
  2502. $this->assertEquals('Foo message', array_shift($messages));
  2503. }
  2504. public function testShouldAllowMarkingFormAsInvalid()
  2505. {
  2506. $this->form->addErrorMessage('Invalid values entered');
  2507. $this->assertFalse($this->form->hasErrors());
  2508. $this->form->markAsError();
  2509. $this->assertTrue($this->form->hasErrors());
  2510. $messages = $this->form->getMessages();
  2511. $this->assertEquals(1, count($messages));
  2512. $this->assertEquals('Invalid values entered', array_shift($messages));
  2513. }
  2514. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2515. {
  2516. $this->assertFalse($this->form->hasErrors());
  2517. $this->form->setErrors(array('Error 1', 'Error 2'))
  2518. ->addError('Error 3')
  2519. ->addErrors(array('Error 4', 'Error 5'));
  2520. $this->assertTrue($this->form->hasErrors());
  2521. $messages = $this->form->getMessages();
  2522. $this->assertEquals(5, count($messages));
  2523. foreach (range(1, 5) as $id) {
  2524. $message = 'Error ' . $id;
  2525. $this->assertContains($message, $messages);
  2526. }
  2527. }
  2528. /**#@-*/
  2529. // View object
  2530. public function getView()
  2531. {
  2532. $view = new Zend_View();
  2533. return $view;
  2534. }
  2535. public function testGetViewRetrievesFromViewRendererByDefault()
  2536. {
  2537. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2538. $viewRenderer->initView();
  2539. $view = $viewRenderer->view;
  2540. $test = $this->form->getView();
  2541. $this->assertSame($view, $test);
  2542. }
  2543. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2544. {
  2545. $this->assertNull($this->form->getView());
  2546. }
  2547. public function testCanSetView()
  2548. {
  2549. $view = new Zend_View();
  2550. $this->assertNull($this->form->getView());
  2551. $this->form->setView($view);
  2552. $received = $this->form->getView();
  2553. $this->assertSame($view, $received);
  2554. }
  2555. // Decorators
  2556. public function testFormDecoratorRegisteredByDefault()
  2557. {
  2558. $this->_checkZf2794();
  2559. $decorator = $this->form->getDecorator('form');
  2560. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2561. }
  2562. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2563. {
  2564. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2565. $decorators = $form->getDecorators();
  2566. $this->assertEquals(array(), $decorators);
  2567. }
  2568. public function testCanAddSingleDecoratorAsString()
  2569. {
  2570. $this->_checkZf2794();
  2571. $this->form->clearDecorators();
  2572. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2573. $this->form->addDecorator('viewHelper');
  2574. $decorator = $this->form->getDecorator('viewHelper');
  2575. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2576. }
  2577. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2578. {
  2579. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2580. }
  2581. public function testCanAddSingleDecoratorAsDecoratorObject()
  2582. {
  2583. $this->form->clearDecorators();
  2584. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2585. $decorator = new Zend_Form_Decorator_ViewHelper;
  2586. $this->form->addDecorator($decorator);
  2587. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2588. $this->assertSame($decorator, $test);
  2589. }
  2590. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2591. {
  2592. $this->_checkZf2794();
  2593. $this->form->clearDecorators();
  2594. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2595. $decorator = new Zend_Form_Decorator_ViewHelper;
  2596. $this->form->addDecorator($decorator);
  2597. $test = $this->form->getDecorator('viewHelper');
  2598. $this->assertSame($decorator, $test);
  2599. }
  2600. public function testCanAddMultipleDecorators()
  2601. {
  2602. $this->_checkZf2794();
  2603. $this->form->clearDecorators();
  2604. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2605. $testDecorator = new Zend_Form_Decorator_Errors;
  2606. $this->form->addDecorators(array(
  2607. 'ViewHelper',
  2608. $testDecorator
  2609. ));
  2610. $viewHelper = $this->form->getDecorator('viewHelper');
  2611. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2612. $decorator = $this->form->getDecorator('errors');
  2613. $this->assertSame($testDecorator, $decorator);
  2614. }
  2615. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2616. {
  2617. $this->_checkZf2794();
  2618. $this->assertFalse($this->form->removeDecorator('foobar'));
  2619. }
  2620. public function testCanRemoveDecorator()
  2621. {
  2622. $this->_checkZf2794();
  2623. $this->testFormDecoratorRegisteredByDefault();
  2624. $this->form->removeDecorator('form');
  2625. $this->assertFalse($this->form->getDecorator('form'));
  2626. }
  2627. /**
  2628. * @group ZF-3069
  2629. */
  2630. public function testRemovingNamedDecoratorShouldWork()
  2631. {
  2632. $this->_checkZf2794();
  2633. $this->form->setDecorators(array(
  2634. 'FormElements',
  2635. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2636. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2637. ));
  2638. $decorators = $this->form->getDecorators();
  2639. $this->assertTrue(array_key_exists('div', $decorators));
  2640. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2641. $this->form->removeDecorator('div');
  2642. $decorators = $this->form->getDecorators();
  2643. $this->assertFalse(array_key_exists('div', $decorators));
  2644. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2645. }
  2646. public function testCanClearAllDecorators()
  2647. {
  2648. $this->_checkZf2794();
  2649. $this->testCanAddMultipleDecorators();
  2650. $this->form->clearDecorators();
  2651. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2652. $this->assertFalse($this->form->getDecorator('fieldset'));
  2653. }
  2654. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2655. {
  2656. $this->_checkZf2794();
  2657. $this->form->setDecorators(array(
  2658. array('HtmlTag', array('tag' => 'div')),
  2659. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2660. ));
  2661. $decorator = $this->form->getDecorator('FooBar');
  2662. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2663. $this->assertEquals('dd', $decorator->getOption('tag'));
  2664. $decorator = $this->form->getDecorator('HtmlTag');
  2665. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2666. $this->assertEquals('div', $decorator->getOption('tag'));
  2667. }
  2668. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2669. {
  2670. $this->form->setDecorators(array(
  2671. 'FormElements',
  2672. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2673. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2674. 'Form',
  2675. ));
  2676. $decorator = $this->form->getDecorator('fieldset');
  2677. $decorators = $this->form->getDecorators();
  2678. $i = 0;
  2679. $order = array();
  2680. foreach (array_keys($decorators) as $name) {
  2681. $order[$name] = $i;
  2682. ++$i;
  2683. }
  2684. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2685. }
  2686. // Rendering
  2687. public function checkMarkup($html)
  2688. {
  2689. $this->assertFalse(empty($html));
  2690. $this->assertContains('<form', $html);
  2691. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2692. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2693. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2694. $this->assertContains('</form>', $html);
  2695. }
  2696. public function testRenderReturnsMarkup()
  2697. {
  2698. $this->setupElements();
  2699. $html = $this->form->render($this->getView());
  2700. $this->checkMarkup($html);
  2701. }
  2702. public function testRenderReturnsMarkupRepresentingAllElements()
  2703. {
  2704. $this->testRenderReturnsMarkup();
  2705. $html = $this->form->render();
  2706. foreach ($this->form->getElements() as $key => $element) {
  2707. $this->assertFalse(empty($key));
  2708. $this->assertFalse(is_numeric($key));
  2709. $this->assertContains('<input', $html);
  2710. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2711. }
  2712. }
  2713. public function testRenderReturnsMarkupContainingSubForms()
  2714. {
  2715. $this->setupElements();
  2716. $this->setupSubForm();
  2717. $this->form->setView($this->getView());
  2718. $html = $this->form->render();
  2719. $this->assertRegexp('/<fieldset/', $html);
  2720. $this->assertContains('</fieldset>', $html);
  2721. foreach ($this->form->sub as $key => $item) {
  2722. $this->assertFalse(empty($key));
  2723. $this->assertFalse(is_numeric($key));
  2724. $this->assertContains('<input', $html);
  2725. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2726. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2727. }
  2728. }
  2729. public function testRenderReturnsMarkupContainingDisplayGroups()
  2730. {
  2731. $this->setupElements();
  2732. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2733. $this->form->setView($this->getView());
  2734. $html = $this->html = $this->form->render();
  2735. $this->assertRegexp('/<fieldset/', $html);
  2736. $this->assertContains('</fieldset>', $html);
  2737. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2738. $dom = new DOMDocument();
  2739. $dom->loadHTML($html);
  2740. $fieldsets = $dom->getElementsByTagName('fieldset');
  2741. $this->assertTrue(0 < $fieldsets->length);
  2742. $fieldset = $fieldsets->item(0);
  2743. $nodes = $fieldset->childNodes;
  2744. $this->assertNotNull($nodes);
  2745. for ($i = 0; $i < $nodes->length; ++$i) {
  2746. $node = $nodes->item($i);
  2747. if ('input' != $node->nodeName) {
  2748. continue;
  2749. }
  2750. $this->assertTrue($node->hasAttribute('name'));
  2751. $nameNode = $node->getAttributeNode('name');
  2752. switch ($i) {
  2753. case 0:
  2754. $this->assertEquals('foo', $nameNode->nodeValue);
  2755. break;
  2756. case 1:
  2757. $this->assertEquals('baz', $nameNode->nodeValue);
  2758. break;
  2759. default:
  2760. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2761. }
  2762. }
  2763. }
  2764. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2765. {
  2766. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2767. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2768. $this->fail("Should find foo element in rendered form");
  2769. }
  2770. $this->assertEquals(1, count($matches));
  2771. $this->assertEquals(1, count($matches[0]));
  2772. }
  2773. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2774. {
  2775. $this->setupElements();
  2776. $this->form->setElementsBelongTo('anArray');
  2777. $html = $this->form->render($this->getView());
  2778. $this->assertContains('name="anArray[foo]"', $html);
  2779. $this->assertContains('name="anArray[bar]"', $html);
  2780. $this->assertContains('name="anArray[baz]"', $html);
  2781. $this->assertContains('id="anArray-foo"', $html);
  2782. $this->assertContains('id="anArray-bar"', $html);
  2783. $this->assertContains('id="anArray-baz"', $html);
  2784. }
  2785. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2786. {
  2787. $this->setupElements();
  2788. $this->form->setElementsBelongTo('data[foo]');
  2789. $html = $this->form->render($this->getView());
  2790. $this->assertContains('name="data[foo][foo]"', $html);
  2791. $this->assertContains('name="data[foo][bar]"', $html);
  2792. $this->assertContains('name="data[foo][baz]"', $html);
  2793. $this->assertContains('id="data-foo-foo"', $html);
  2794. $this->assertContains('id="data-foo-bar"', $html);
  2795. $this->assertContains('id="data-foo-baz"', $html);
  2796. }
  2797. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2798. {
  2799. $this->setupElements();
  2800. $this->form->setName('data')
  2801. ->setIsArray(true);
  2802. $html = $this->form->render($this->getView());
  2803. $this->assertContains('name="data[foo]"', $html);
  2804. $this->assertContains('name="data[bar]"', $html);
  2805. $this->assertContains('name="data[baz]"', $html);
  2806. $this->assertContains('id="data-foo"', $html);
  2807. $this->assertContains('id="data-bar"', $html);
  2808. $this->assertContains('id="data-baz"', $html);
  2809. }
  2810. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2811. {
  2812. $this->form->setName('data')
  2813. ->setIsArray(true);
  2814. $subForm = new Zend_Form_SubForm();
  2815. $subForm->setElementsBelongTo('billing[info]');
  2816. $subForm->addElement('text', 'name');
  2817. $subForm->addElement('text', 'number');
  2818. $this->form->addSubForm($subForm, 'sub');
  2819. $html = $this->form->render($this->getView());
  2820. $this->assertContains('name="data[billing][info][name]', $html);
  2821. $this->assertContains('name="data[billing][info][number]', $html);
  2822. $this->assertContains('id="data-billing-info-name"', $html);
  2823. $this->assertContains('id="data-billing-info-number"', $html);
  2824. }
  2825. public function testToStringProxiesToRender()
  2826. {
  2827. $this->setupElements();
  2828. $this->form->setView($this->getView());
  2829. $html = $this->form->__toString();
  2830. $this->checkMarkup($html);
  2831. }
  2832. public function raiseDecoratorException($content, $element, $options)
  2833. {
  2834. throw new Exception('Raising exception in decorator callback');
  2835. }
  2836. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2837. {
  2838. $this->error = $errstr;
  2839. }
  2840. public function testToStringRaisesErrorWhenExceptionCaught()
  2841. {
  2842. $this->form->setDecorators(array(
  2843. array(
  2844. 'decorator' => 'Callback',
  2845. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2846. ),
  2847. ));
  2848. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2849. $text = $this->form->__toString();
  2850. restore_error_handler();
  2851. $this->assertTrue(empty($text));
  2852. $this->assertTrue(isset($this->error));
  2853. $this->assertContains('Raising exception in decorator callback', $this->error);
  2854. }
  2855. /**
  2856. * ZF-2718
  2857. */
  2858. public function testHiddenElementsGroupedWhenRendered()
  2859. {
  2860. $this->markTestIncomplete('Scheduling for future release');
  2861. $this->form->addElements(array(
  2862. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2863. array('type' => 'text', 'name' => 'testone'),
  2864. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2865. array('type' => 'text', 'name' => 'testtwo'),
  2866. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2867. array('type' => 'text', 'name' => 'testthree'),
  2868. ));
  2869. $html = $this->form->render($this->getView());
  2870. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2871. $this->fail('Hidden elements should be grouped');
  2872. }
  2873. foreach (array('first', 'second', 'third') as $which) {
  2874. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2875. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2876. }
  2877. }
  2878. // Localization
  2879. public function testTranslatorIsNullByDefault()
  2880. {
  2881. $this->assertNull($this->form->getTranslator());
  2882. }
  2883. public function testCanSetTranslator()
  2884. {
  2885. require_once 'Zend/Translate/Adapter/Array.php';
  2886. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2887. $this->form->setTranslator($translator);
  2888. $received = $this->form->getTranslator($translator);
  2889. $this->assertSame($translator->getAdapter(), $received);
  2890. }
  2891. public function testCanSetDefaultGlobalTranslator()
  2892. {
  2893. $this->assertNull($this->form->getTranslator());
  2894. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2895. Zend_Form::setDefaultTranslator($translator);
  2896. $received = Zend_Form::getDefaultTranslator();
  2897. $this->assertSame($translator->getAdapter(), $received);
  2898. $received = $this->form->getTranslator();
  2899. $this->assertSame($translator->getAdapter(), $received);
  2900. $form = new Zend_Form();
  2901. $received = $form->getTranslator();
  2902. $this->assertSame($translator->getAdapter(), $received);
  2903. }
  2904. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2905. {
  2906. $this->assertNull($this->form->getTranslator());
  2907. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2908. Zend_Form::setDefaultTranslator($translatorDefault);
  2909. $received = $this->form->getTranslator();
  2910. $this->assertSame($translatorDefault->getAdapter(), $received);
  2911. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2912. $this->form->setTranslator($translator);
  2913. $received = $this->form->getTranslator();
  2914. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2915. $this->assertSame($translator->getAdapter(), $received);
  2916. }
  2917. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2918. {
  2919. $this->assertNull($this->form->getTranslator());
  2920. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2921. Zend_Registry::set('Zend_Translate', $translator);
  2922. $received = Zend_Form::getDefaultTranslator();
  2923. $this->assertSame($translator->getAdapter(), $received);
  2924. $received = $this->form->getTranslator();
  2925. $this->assertSame($translator->getAdapter(), $received);
  2926. $form = new Zend_Form();
  2927. $received = $form->getTranslator();
  2928. $this->assertSame($translator->getAdapter(), $received);
  2929. }
  2930. public function testCanDisableTranslation()
  2931. {
  2932. $this->testCanSetDefaultGlobalTranslator();
  2933. $this->form->setDisableTranslator(true);
  2934. $this->assertNull($this->form->getTranslator());
  2935. }
  2936. // Iteration
  2937. public function testFormObjectIsIterableAndIteratesElements()
  2938. {
  2939. $this->setupElements();
  2940. $expected = array('foo', 'bar', 'baz');
  2941. $received = array();
  2942. foreach ($this->form as $key => $value) {
  2943. $received[] = $key;
  2944. }
  2945. $this->assertSame($expected, $received);
  2946. }
  2947. public function testFormObjectIteratesElementsInExpectedOrder()
  2948. {
  2949. $this->setupElements();
  2950. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2951. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2952. $received = array();
  2953. foreach ($this->form as $key => $value) {
  2954. $received[] = $key;
  2955. $this->assertTrue($value instanceof Zend_Form_Element);
  2956. }
  2957. $this->assertSame($expected, $received);
  2958. }
  2959. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2960. {
  2961. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2962. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2963. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2964. $expected = array('name', 'email', 'submit');
  2965. $received = array();
  2966. foreach ($this->form as $key => $value) {
  2967. $received[] = $key;
  2968. $this->assertTrue($value instanceof Zend_Form_Element);
  2969. }
  2970. $this->assertSame($expected, $received);
  2971. }
  2972. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2973. {
  2974. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2975. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2976. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2977. ->setView($this->getView());
  2978. $test = $this->form->render();
  2979. $this->assertContains('name="a"', $test);
  2980. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2981. $this->fail('Expected markup not found');
  2982. }
  2983. $order = array();
  2984. foreach ($matches[1] as $element) {
  2985. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2986. $order[] = $m[1];
  2987. }
  2988. }
  2989. $this->assertSame(array('b', 'c', 'a'), $order);
  2990. }
  2991. public function testFormObjectIteratesElementsAndSubforms()
  2992. {
  2993. $this->setupElements();
  2994. $this->setupSubForm();
  2995. $expected = array('foo', 'bar', 'baz', 'sub');
  2996. $received = array();
  2997. foreach ($this->form as $key => $value) {
  2998. $received[] = $key;
  2999. $this->assertTrue(($value instanceof Zend_Form_Element)
  3000. or ($value instanceof Zend_Form_SubForm));
  3001. }
  3002. $this->assertSame($expected, $received);
  3003. }
  3004. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  3005. {
  3006. $this->setupElements();
  3007. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3008. $expected = array('bar', 'foobaz');
  3009. $received = array();
  3010. foreach ($this->form as $key => $value) {
  3011. $received[] = $key;
  3012. $this->assertTrue(($value instanceof Zend_Form_Element)
  3013. or ($value instanceof Zend_Form_DisplayGroup));
  3014. }
  3015. $this->assertSame($expected, $received);
  3016. }
  3017. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  3018. {
  3019. $this->setupElements();
  3020. $bar = $this->form->bar;
  3021. $this->form->removeElement('bar');
  3022. try {
  3023. foreach ($this->form as $item) {
  3024. }
  3025. } catch (Exception $e) {
  3026. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3027. }
  3028. $this->form->addElement($bar);
  3029. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  3030. $this->form->removeDisplayGroup('bazbar');
  3031. try {
  3032. foreach ($this->form as $item) {
  3033. }
  3034. } catch (Exception $e) {
  3035. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3036. }
  3037. $subForm = new Zend_Form_SubForm;
  3038. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  3039. $this->form->addSubForm($subForm, 'page1');
  3040. $this->form->removeSubForm('page1');
  3041. try {
  3042. foreach ($this->form as $item) {
  3043. }
  3044. } catch (Exception $e) {
  3045. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3046. }
  3047. }
  3048. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  3049. {
  3050. $form = new Zend_Form();
  3051. $form->addElements(array(
  3052. 'username' => 'text',
  3053. 'password' => 'text',
  3054. ));
  3055. $form->clearElements();
  3056. try {
  3057. foreach ($form as $item) {
  3058. }
  3059. } catch (Zend_Form_Exception $e) {
  3060. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  3061. . $e->getMessage();
  3062. $this->fail($message);
  3063. }
  3064. $form->addElements(array(
  3065. 'username' => 'text',
  3066. 'password' => 'text',
  3067. ))
  3068. ->addDisplayGroup(array('username', 'password'), 'login');
  3069. $form->clearDisplayGroups();
  3070. try {
  3071. foreach ($form as $item) {
  3072. }
  3073. } catch (Zend_Form_Exception $e) {
  3074. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  3075. . $e->getMessage();
  3076. $this->fail($message);
  3077. }
  3078. $subForm = new Zend_Form_SubForm();
  3079. $form->addSubForm($subForm, 'foo');
  3080. $form->clearSubForms();
  3081. try {
  3082. foreach ($form as $item) {
  3083. }
  3084. } catch (Zend_Form_Exception $e) {
  3085. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  3086. . $e->getMessage();
  3087. $this->fail($message);
  3088. }
  3089. }
  3090. // Countable
  3091. public function testCanCountFormObject()
  3092. {
  3093. $this->setupElements();
  3094. $this->assertEquals(3, count($this->form));
  3095. }
  3096. public function testCountingFormObjectCountsSubForms()
  3097. {
  3098. $this->setupElements();
  3099. $this->setupSubForm();
  3100. $this->assertEquals(4, count($this->form));
  3101. }
  3102. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  3103. {
  3104. $this->testCountingFormObjectCountsSubForms();
  3105. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3106. $this->assertEquals(3, count($this->form));
  3107. }
  3108. // Element decorators and plugin paths
  3109. public function testCanSetAllElementDecoratorsAtOnce()
  3110. {
  3111. $this->_checkZf2794();
  3112. $this->setupElements();
  3113. $this->form->setElementDecorators(array(
  3114. array('ViewHelper'),
  3115. array('Label'),
  3116. array('Fieldset'),
  3117. ));
  3118. foreach ($this->form->getElements() as $element) {
  3119. $this->assertFalse($element->getDecorator('Errors'));
  3120. $this->assertFalse($element->getDecorator('HtmlTag'));
  3121. $decorator = $element->getDecorator('ViewHelper');
  3122. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  3123. $decorator = $element->getDecorator('Label');
  3124. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  3125. $decorator = $element->getDecorator('Fieldset');
  3126. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3127. }
  3128. }
  3129. /**
  3130. * @group ZF-3597
  3131. */
  3132. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  3133. {
  3134. $this->form->setDecorators(array(
  3135. 'FormElements',
  3136. array('HtmlTag', array('tag' => 'table')),
  3137. 'Form',
  3138. ));
  3139. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  3140. $this->form->addElement('text', 'test', array(
  3141. 'label' => 'Foo',
  3142. 'description' => 'sample description',
  3143. ));
  3144. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  3145. $decorator = new My_Decorator_TableRow();
  3146. $this->form->setElementDecorators(array(
  3147. 'ViewHelper',
  3148. $decorator,
  3149. ));
  3150. $html = $this->form->render($this->getView());
  3151. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  3152. }
  3153. /**
  3154. * @group ZF-3228
  3155. */
  3156. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  3157. {
  3158. $this->_checkZf2794();
  3159. $this->setupElements();
  3160. $this->form->setElementDecorators(
  3161. array(
  3162. 'Description',
  3163. 'Form',
  3164. 'Fieldset',
  3165. ),
  3166. array(
  3167. 'bar',
  3168. )
  3169. );
  3170. $element = $this->form->bar;
  3171. $this->assertFalse($element->getDecorator('ViewHelper'));
  3172. $this->assertFalse($element->getDecorator('Errors'));
  3173. $this->assertFalse($element->getDecorator('Label'));
  3174. $this->assertFalse($element->getDecorator('HtmlTag'));
  3175. $decorator = $element->getDecorator('Description');
  3176. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3177. $decorator = $element->getDecorator('Form');
  3178. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3179. $decorator = $element->getDecorator('Fieldset');
  3180. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3181. foreach (array('foo', 'baz') as $name) {
  3182. $element = $this->form->$name;
  3183. $this->assertFalse($element->getDecorator('Form'));
  3184. $this->assertFalse($element->getDecorator('Fieldset'));
  3185. }
  3186. }
  3187. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  3188. {
  3189. $this->_checkZf2794();
  3190. $this->setupElements();
  3191. $this->form->setElementDecorators(
  3192. array(
  3193. 'Description',
  3194. 'Form',
  3195. 'Fieldset',
  3196. ),
  3197. array(
  3198. 'foo',
  3199. 'baz',
  3200. ),
  3201. false
  3202. );
  3203. $element = $this->form->bar;
  3204. $this->assertFalse($element->getDecorator('ViewHelper'));
  3205. $this->assertFalse($element->getDecorator('Errors'));
  3206. $this->assertFalse($element->getDecorator('Label'));
  3207. $this->assertFalse($element->getDecorator('HtmlTag'));
  3208. $decorator = $element->getDecorator('Description');
  3209. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3210. $decorator = $element->getDecorator('Form');
  3211. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3212. $decorator = $element->getDecorator('Fieldset');
  3213. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3214. foreach (array('foo', 'baz') as $name) {
  3215. $element = $this->form->$name;
  3216. $this->assertFalse($element->getDecorator('Form'));
  3217. $this->assertFalse($element->getDecorator('Fieldset'));
  3218. }
  3219. }
  3220. /**#@-*/
  3221. public function testCanSetAllElementFiltersAtOnce()
  3222. {
  3223. $this->_checkZf2794();
  3224. $this->setupElements();
  3225. $this->form->setElementFilters(array(
  3226. 'Alnum',
  3227. 'StringToLower'
  3228. ));
  3229. foreach ($this->form->getElements() as $element) {
  3230. $filter = $element->getFilter('Alnum');
  3231. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  3232. $filter = $element->getFilter('StringToLower');
  3233. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  3234. }
  3235. }
  3236. public function testCanSetGlobalElementPrefixPath()
  3237. {
  3238. $this->setupElements();
  3239. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3240. $this->form->addElement('text', 'prefixTest');
  3241. foreach ($this->form->getElements() as $element) {
  3242. $loader = $element->getPluginLoader('validate');
  3243. $paths = $loader->getPaths('Zend_Foo_Validate');
  3244. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  3245. $this->assertContains('Foo', $paths[0]);
  3246. $this->assertContains('Validate', $paths[0]);
  3247. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  3248. $this->assertFalse(empty($paths));
  3249. $this->assertContains('Foo', $paths[0]);
  3250. $this->assertContains('Filter', $paths[0]);
  3251. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  3252. $this->assertFalse(empty($paths));
  3253. $this->assertContains('Foo', $paths[0]);
  3254. $this->assertContains('Decorator', $paths[0]);
  3255. }
  3256. }
  3257. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  3258. {
  3259. $this->_checkZf2794();
  3260. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  3261. $this->form->addElement('text', 'prefixTest');
  3262. $element = $this->form->prefixTest;
  3263. $label = $element->getDecorator('Label');
  3264. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  3265. }
  3266. /**
  3267. * @group ZF-3093
  3268. */
  3269. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  3270. {
  3271. $subForm = new Zend_Form_SubForm();
  3272. $subForm->addElement('text', 'foo');
  3273. $this->form->addSubForm($subForm, 'subForm');
  3274. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3275. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  3276. $paths = $loader->getPaths('Zend_Foo_Decorator');
  3277. $this->assertFalse(empty($paths));
  3278. $this->assertContains('Foo', $paths[0]);
  3279. $this->assertContains('Decorator', $paths[0]);
  3280. }
  3281. public function testCanSetElementValidatorPrefixPath()
  3282. {
  3283. $this->setupElements();
  3284. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  3285. $this->form->addElement('text', 'prefixTest');
  3286. foreach ($this->form->getElements() as $element) {
  3287. $loader = $element->getPluginLoader('validate');
  3288. $paths = $loader->getPaths('Zend_Foo');
  3289. $this->assertFalse(empty($paths));
  3290. $this->assertContains('Foo', $paths[0]);
  3291. $this->assertNotContains('Validate', $paths[0]);
  3292. }
  3293. }
  3294. public function testCanSetElementFilterPrefixPath()
  3295. {
  3296. $this->setupElements();
  3297. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  3298. $this->form->addElement('text', 'prefixTest');
  3299. foreach ($this->form->getElements() as $element) {
  3300. $loader = $element->getPluginLoader('filter');
  3301. $paths = $loader->getPaths('Zend_Foo');
  3302. $this->assertFalse(empty($paths));
  3303. $this->assertContains('Foo', $paths[0]);
  3304. $this->assertNotContains('Filter', $paths[0]);
  3305. }
  3306. }
  3307. public function testCanSetElementDecoratorPrefixPath()
  3308. {
  3309. $this->setupElements();
  3310. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  3311. $this->form->addElement('text', 'prefixTest');
  3312. foreach ($this->form->getElements() as $element) {
  3313. $loader = $element->getPluginLoader('decorator');
  3314. $paths = $loader->getPaths('Zend_Foo');
  3315. $this->assertFalse(empty($paths));
  3316. $this->assertContains('Foo', $paths[0]);
  3317. $this->assertNotContains('Decorator', $paths[0]);
  3318. }
  3319. }
  3320. // Display Group decorators and plugin paths
  3321. public function setupDisplayGroups()
  3322. {
  3323. $this->testCanAddAndRetrieveMultipleElements();
  3324. $this->form->addElements(array(
  3325. 'test1' => 'text',
  3326. 'test2' => 'text',
  3327. 'test3' => 'text',
  3328. 'test4' => 'text'
  3329. ));
  3330. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  3331. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3332. }
  3333. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  3334. {
  3335. $this->_checkZf2794();
  3336. $this->setupDisplayGroups();
  3337. $this->form->setDisplayGroupDecorators(array(
  3338. array('Callback', array('callback' => 'strip_tags')),
  3339. ));
  3340. foreach ($this->form->getDisplayGroups() as $element) {
  3341. $this->assertFalse($element->getDecorator('FormElements'));
  3342. $this->assertFalse($element->getDecorator('HtmlTag'));
  3343. $this->assertFalse($element->getDecorator('Fieldset'));
  3344. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  3345. $decorator = $element->getDecorator('Callback');
  3346. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3347. }
  3348. }
  3349. public function testCanSetDisplayGroupPrefixPath()
  3350. {
  3351. $this->setupDisplayGroups();
  3352. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3353. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3354. foreach ($this->form->getDisplayGroups() as $group) {
  3355. $loader = $group->getPluginLoader();
  3356. $paths = $loader->getPaths('Zend_Foo');
  3357. $this->assertFalse(empty($paths));
  3358. $this->assertContains('Foo', $paths[0]);
  3359. }
  3360. }
  3361. /**
  3362. * @group ZF-3213
  3363. */
  3364. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3365. {
  3366. require_once 'Zend/Config/Ini.php';
  3367. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3368. $form = new Zend_Form($config);
  3369. $dg = $form->foofoo;
  3370. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3371. $this->assertTrue($paths !== false);
  3372. }
  3373. // Subform decorators
  3374. public function testCanSetAllSubFormDecoratorsAtOnce()
  3375. {
  3376. $this->_checkZf2794();
  3377. $this->setupSubForm();
  3378. $this->form->setSubFormDecorators(array(
  3379. array('Callback', array('callback' => 'strip_tags')),
  3380. ));
  3381. foreach ($this->form->getSubForms() as $subForm) {
  3382. $this->assertFalse($subForm->getDecorator('FormElements'));
  3383. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3384. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3385. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3386. $decorator = $subForm->getDecorator('Callback');
  3387. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3388. }
  3389. }
  3390. // Extension
  3391. public function testInitCalledPriorToLoadingDefaultDecorators()
  3392. {
  3393. $form = new Zend_Form_FormTest_FormExtension();
  3394. $decorators = $form->getDecorators();
  3395. $this->assertTrue(empty($decorators));
  3396. }
  3397. // Clone
  3398. /**
  3399. * @group ZF-3819
  3400. */
  3401. public function testCloningShouldCloneAllChildren()
  3402. {
  3403. $form = new Zend_Form();
  3404. $foo = new Zend_Form_SubForm(array(
  3405. 'name' => 'foo',
  3406. 'elements' => array(
  3407. 'one' => 'text',
  3408. 'two' => 'text',
  3409. ),
  3410. ));
  3411. $form->addElement('text', 'bar')
  3412. ->addElement('text', 'baz')
  3413. ->addElement('text', 'bat')
  3414. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3415. ->addSubForm($foo, 'foo');
  3416. $bar = $form->bar;
  3417. $baz = $form->baz;
  3418. $bat = $form->bat;
  3419. $barbat = $form->barbat;
  3420. $cloned = clone $form;
  3421. $this->assertNotSame($foo, $cloned->foo);
  3422. $this->assertNotSame($bar, $cloned->bar);
  3423. $this->assertNotSame($baz, $cloned->baz);
  3424. $this->assertNotSame($bat, $cloned->bat);
  3425. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3426. $this->assertNotSame($foo->one, $cloned->foo->one);
  3427. $this->assertNotSame($foo->two, $cloned->foo->two);
  3428. }
  3429. // Reset
  3430. /**
  3431. * @group ZF-3227
  3432. */
  3433. public function testFormsShouldAllowResetting()
  3434. {
  3435. $form = new Zend_Form();
  3436. $foo = new Zend_Form_SubForm(array(
  3437. 'name' => 'foo',
  3438. 'elements' => array(
  3439. 'one' => 'text',
  3440. 'two' => 'text',
  3441. ),
  3442. ));
  3443. $form->addElement('text', 'bar')
  3444. ->addElement('text', 'baz')
  3445. ->addElement('text', 'bat')
  3446. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3447. ->addSubForm($foo, 'foo');
  3448. $values = array(
  3449. 'bar' => 'Bar Value',
  3450. 'baz' => 'Baz Value',
  3451. 'bat' => 'Bat Value',
  3452. 'foo' => array(
  3453. 'one' => 'One Value',
  3454. 'two' => 'Two Value',
  3455. ),
  3456. );
  3457. $form->populate($values);
  3458. $test = $form->getValues();
  3459. $this->assertEquals($values, $test);
  3460. $form->reset();
  3461. $test = $form->getValues();
  3462. $this->assertNotEquals($values, $test);
  3463. $this->assertEquals(0, array_sum($test));
  3464. }
  3465. /**
  3466. * @group ZF-3217
  3467. */
  3468. public function testFormShouldOverloadToRenderDecorators()
  3469. {
  3470. $this->setupElements();
  3471. $this->form->setView($this->getView());
  3472. $html = $this->form->renderFormElements();
  3473. foreach ($this->form->getElements() as $element) {
  3474. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3475. }
  3476. $this->assertNotContains('<dl', $html);
  3477. $this->assertNotContains('<form', $html);
  3478. $html = $this->form->renderForm('this is the content');
  3479. $this->assertContains('<form', $html);
  3480. $this->assertContains('</form>', $html);
  3481. $this->assertContains('this is the content', $html);
  3482. }
  3483. /**
  3484. * @group ZF-3217
  3485. * @expectedException Zend_Form_Exception
  3486. */
  3487. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3488. {
  3489. $html = $this->form->bogusMethodCall();
  3490. }
  3491. /**
  3492. * @group ZF-2950
  3493. */
  3494. public function testDtDdElementsWithLabelGetUniqueId()
  3495. {
  3496. $form = new Zend_Form();
  3497. $form->setView($this->getView());
  3498. $fooElement = new Zend_Form_Element_Text('foo');
  3499. $fooElement->setLabel('Foo');
  3500. $form->addElement($fooElement);
  3501. $html = $form->render();
  3502. $this->assertContains('<dt id="foo-label">', $html);
  3503. $this->assertContains('<dd id="foo-element">', $html);
  3504. }
  3505. /**
  3506. * @group ZF-2950
  3507. */
  3508. public function testDtDdElementsWithoutLabelGetUniqueId()
  3509. {
  3510. $form = new Zend_Form();
  3511. $form->setView($this->getView())
  3512. ->addElement(new Zend_Form_Element_Text('foo'));
  3513. $html = $form->render();
  3514. $this->assertContains('<dt id="foo-label">&#160;</dt>', $html);
  3515. $this->assertContains('<dd id="foo-element">', $html);
  3516. }
  3517. /**
  3518. * @group ZF-2950
  3519. */
  3520. public function testSubFormGetsUniqueIdWithName()
  3521. {
  3522. $form = new Zend_Form();
  3523. $form->setView($this->getView())
  3524. ->setName('testform')
  3525. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3526. $html = $form->render();
  3527. $this->assertContains('<dt id="testform-label">&#160;</dt>', $html);
  3528. $this->assertContains('<dd id="testform-element">', $html);
  3529. }
  3530. /**
  3531. * @group ZF-5370
  3532. */
  3533. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3534. {
  3535. $file = new Zend_Form_Element_File('txt');
  3536. $this->form->addElement($file);
  3537. $html = $this->form->render($this->getView());
  3538. $this->assertFalse(empty($html));
  3539. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3540. }
  3541. /**
  3542. * @group ZF-5370
  3543. */
  3544. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3545. {
  3546. $subForm = new Zend_Form_SubForm();
  3547. $subForm->addElement('file', 'txt');
  3548. $this->form->addSubForm($subForm, 'page1')
  3549. ->setView(new Zend_View);
  3550. $html = $this->form->render();
  3551. $this->assertContains('id="txt"', $html);
  3552. $this->assertContains('name="txt"', $html);
  3553. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3554. }
  3555. /**
  3556. * @group ZF-5370
  3557. */
  3558. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3559. {
  3560. $this->form->addElement('file', 'txt')
  3561. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3562. ->setView(new Zend_View);
  3563. $html = $this->form->render();
  3564. $this->assertContains('id="txt"', $html);
  3565. $this->assertContains('name="txt"', $html);
  3566. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3567. }
  3568. /**
  3569. * @group ZF-6070
  3570. */
  3571. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3572. {
  3573. $this->form->setOptions(array(
  3574. 'elementDecorators' => array(
  3575. 'ViewHelper',
  3576. 'Label',
  3577. ),
  3578. 'elements' => array(
  3579. 'foo' => array(
  3580. 'type' => 'text',
  3581. 'options' => array(
  3582. 'decorators' => array(
  3583. 'Errors',
  3584. 'ViewHelper',
  3585. ),
  3586. ),
  3587. ),
  3588. ),
  3589. ));
  3590. $element = $this->form->getElement('foo');
  3591. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3592. $actual = array();
  3593. foreach ($element->getDecorators() as $decorator) {
  3594. $actual[] = get_class($decorator);
  3595. }
  3596. $this->assertSame($expected, $actual);
  3597. }
  3598. /**
  3599. * @group ZF-5150
  3600. */
  3601. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3602. {
  3603. $this->form->addError('Error');
  3604. $this->assertFalse($this->form->isValid(array()));
  3605. }
  3606. /**
  3607. * @group ZF-8494
  3608. */
  3609. public function testGetValidValues()
  3610. {
  3611. $data = array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement');
  3612. require_once "Zend/Validate/Int.php";
  3613. $validElement = new Zend_Form_Element("valid");
  3614. $validElement->addValidator(new Zend_Validate_Int());
  3615. $this->form->addElement($validElement);
  3616. $invalidElement = new Zend_Form_Element('invalid');
  3617. $invalidElement->addValidator(new Zend_Validate_Int());
  3618. $this->form->addElement($invalidElement);
  3619. $this->assertEquals(array('valid' => 1234), $this->form->getValidValues($data));
  3620. }
  3621. /**
  3622. * @group ZF-8494
  3623. */
  3624. public function testGetValidSubFormValues()
  3625. {
  3626. $data = array('sub' => array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement'));
  3627. require_once "Zend/Validate/Int.php";
  3628. $subForm = new Zend_Form_SubForm();
  3629. $validElement = new Zend_Form_Element("valid");
  3630. $validElement->addValidator(new Zend_Validate_Int());
  3631. $subForm->addElement($validElement);
  3632. $invalidElement = new Zend_Form_Element('invalid');
  3633. $invalidElement->addValidator(new Zend_Validate_Int());
  3634. $subForm->addElement($invalidElement);
  3635. $this->form->addSubForm($subForm, 'sub');
  3636. $this->assertEquals(array('sub' => array('valid' => 1234)), $this->form->getValidValues($data));
  3637. }
  3638. /**
  3639. * @group ZF-9275
  3640. */
  3641. public function testElementTranslatorNotOverriddenbyGlobalTranslatorDuringValidation()
  3642. {
  3643. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  3644. Zend_Registry::set('Zend_Translate', $translator);
  3645. $this->form->addElement('text', 'foo');
  3646. $this->form->isValid(array());
  3647. $received = $this->form->foo->hasTranslator();
  3648. $this->assertSame(false, $received);
  3649. }
  3650. /**
  3651. * @group ZF-9275
  3652. */
  3653. public function testZendValidateDefaultTranslatorOverridesZendTranslateDefaultTranslator()
  3654. {
  3655. $translate = new Zend_Translate('array', array('isEmpty' => 'translate'));
  3656. Zend_Registry::set('Zend_Translate', $translate);
  3657. $translateValidate = new Zend_Translate('array', array('isEmpty' => 'validate'));
  3658. Zend_Validate_Abstract::setDefaultTranslator($translateValidate);
  3659. $this->form->addElement('text', 'foo', array('required'=>1));
  3660. $this->form->isValid(array());
  3661. $this->assertSame(array('isEmpty' => 'validate'), $this->form->foo->getMessages());
  3662. }
  3663. /**
  3664. * @group ZF-9494
  3665. */
  3666. public function testElementTranslatorNotOveriddenbyFormTranslator()
  3667. {
  3668. $translations = array(
  3669. 'isEmpty' => 'Element message',
  3670. );
  3671. $translate = new Zend_Translate('array', $translations);
  3672. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$translate));
  3673. $this->assertFalse($this->form->isValid(array('foo'=>'')));
  3674. $messages = $this->form->getMessages();
  3675. $this->assertEquals(1, count($messages));
  3676. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3677. $this->assertFalse($this->form->isValidPartial(array('foo'=>'')));
  3678. $messages = $this->form->getMessages();
  3679. $this->assertEquals(1, count($messages));
  3680. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3681. }
  3682. /**
  3683. * @group ZF-9364
  3684. */
  3685. public function testElementTranslatorPreferredOverFormTranslator()
  3686. {
  3687. $formTanslations = array(
  3688. 'isEmpty' => 'Form message',
  3689. );
  3690. $elementTanslations = array(
  3691. 'isEmpty' => 'Element message',
  3692. );
  3693. $formTranslate = new Zend_Translate('array', $formTanslations);
  3694. $elementTranslate = new Zend_Translate('array', $elementTanslations);
  3695. $this->form->setTranslator($formTranslate);
  3696. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3697. $this->form->addElement('text', 'bar', array('required'=>true));
  3698. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3699. $messages = $this->form->getMessages();
  3700. $this->assertEquals(2, count($messages));
  3701. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3702. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3703. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3704. $messages = $this->form->getMessages();
  3705. $this->assertEquals(2, count($messages));
  3706. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3707. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3708. }
  3709. /**
  3710. * @group ZF-9364
  3711. */
  3712. public function testElementTranslatorPreferredOverDefaultTranslator()
  3713. {
  3714. $defaultTranslations = array(
  3715. 'isEmpty' => 'Default message',
  3716. );
  3717. $formTranslations = array(
  3718. 'isEmpty' => 'Form message',
  3719. );
  3720. $elementTranslations = array(
  3721. 'isEmpty' => 'Element message',
  3722. );
  3723. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3724. $formTranslate = new Zend_Translate('array', $formTranslations);
  3725. $elementTranslate = new Zend_Translate('array', $elementTranslations);
  3726. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3727. $this->form->setTranslator($formTranslate);
  3728. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3729. $this->form->addElement('text', 'bar', array('required'=>true));
  3730. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3731. $messages = $this->form->getMessages();
  3732. $this->assertEquals(2, count($messages));
  3733. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3734. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3735. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3736. $messages = $this->form->getMessages();
  3737. $this->assertEquals(2, count($messages));
  3738. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3739. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3740. }
  3741. /**
  3742. * @group ZF-9540
  3743. */
  3744. public function testSubFormTranslatorPreferredOverDefaultTranslator()
  3745. {
  3746. $defaultTranslations = array('isEmpty' => 'Default message');
  3747. $subformTranslations = array('isEmpty' => 'SubForm message');
  3748. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3749. $subformTranslate = new Zend_Translate('array', $subformTranslations);
  3750. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3751. $this->form->addSubForm(new Zend_Form_SubForm(), 'subform');
  3752. $this->form->subform->setTranslator($subformTranslate);
  3753. $this->form->subform->addElement('text', 'foo', array('required'=>true));
  3754. $this->assertFalse($this->form->isValid(array('subform' => array('foo'=>''))));
  3755. $messages = $this->form->getMessages();
  3756. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3757. $this->assertFalse($this->form->isValidPartial(array('subform' => array('foo'=>''))));
  3758. $messages = $this->form->getMessages();
  3759. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3760. }
  3761. /**
  3762. * @group ZF-12375
  3763. */
  3764. public function testElementTranslatorNotOveriddenbyFormTranslatorDuringRendering()
  3765. {
  3766. // Set translator for form
  3767. $this->form->setTranslator(
  3768. new Zend_Translate(
  3769. 'array',
  3770. array(
  3771. 'labelText' => 'Foo',
  3772. )
  3773. )
  3774. );
  3775. // Add element with his own translator
  3776. $this->form->addElement(
  3777. 'text',
  3778. 'foo',
  3779. array(
  3780. 'label' => 'labelText',
  3781. 'translator' => new Zend_Translate(
  3782. 'array',
  3783. array(
  3784. 'labelText' => 'Bar',
  3785. )
  3786. ),
  3787. 'decorators' => array(
  3788. 'Label',
  3789. ),
  3790. )
  3791. );
  3792. $this->form->setDecorators(array('FormElements'));
  3793. // Test
  3794. $this->assertSame(
  3795. PHP_EOL . '<label for="foo" class="optional">Bar</label>' . PHP_EOL,
  3796. $this->form->render(new Zend_View())
  3797. );
  3798. }
  3799. public function testNameAttributeOutputForXhtml()
  3800. {
  3801. // Create form
  3802. $form = new Zend_Form();
  3803. $form->setName('foo');
  3804. $form->setMethod(Zend_Form::METHOD_GET);
  3805. $form->removeDecorator('HtmlTag');
  3806. // Set doctype
  3807. $this->getView()->getHelper('doctype')->doctype(
  3808. Zend_View_Helper_Doctype::XHTML1_STRICT
  3809. );
  3810. $expected = '<form id="foo" method="get" action="">'
  3811. . PHP_EOL
  3812. . '</form>';
  3813. $this->assertSame(
  3814. $expected,
  3815. $form->render($this->getView())
  3816. );
  3817. }
  3818. /**
  3819. * @group ZF-5613
  3820. */
  3821. public function testAddSubFormsPerConfig()
  3822. {
  3823. // Create form
  3824. $form = new Zend_Form(
  3825. array(
  3826. 'subForms' => array(
  3827. array(
  3828. 'form' => array(
  3829. 'elements' => array(
  3830. 'foo' => array(
  3831. 'text',
  3832. array(
  3833. 'label' => 'Foo',
  3834. 'decorators' => array(
  3835. 'ViewHelper',
  3836. 'Label',
  3837. ),
  3838. ),
  3839. ),
  3840. ),
  3841. 'id' => 'subform1',
  3842. 'decorators' => array(
  3843. 'FormElements',
  3844. ),
  3845. ),
  3846. 'name' => 'subform1',
  3847. 'order' => 2,
  3848. ),
  3849. array(
  3850. 'form' => array(
  3851. 'elements' => array(
  3852. 'bar' => array(
  3853. 'text',
  3854. array(
  3855. 'label' => 'Bar',
  3856. 'decorators' => array(
  3857. 'ViewHelper',
  3858. 'Label',
  3859. ),
  3860. ),
  3861. ),
  3862. ),
  3863. 'id' => 'subform2',
  3864. 'decorators' => array(
  3865. 'FormElements',
  3866. ),
  3867. ),
  3868. 'name' => 'subform2',
  3869. 'order' => 1,
  3870. ),
  3871. ),
  3872. )
  3873. );
  3874. $form->removeDecorator('HtmlTag');
  3875. // Tests
  3876. $subForms = $form->getSubForms();
  3877. $subForm1 = current($subForms);
  3878. $subForm2 = next($subForms);
  3879. $this->assertSame(
  3880. array(
  3881. 'subform1',
  3882. 'subform2',
  3883. ),
  3884. array(
  3885. $subForm1->getName(),
  3886. $subForm2->getName(),
  3887. )
  3888. );
  3889. $expected = '<form enctype="application/x-www-form-urlencoded" action="" method="post">'
  3890. . PHP_EOL
  3891. . PHP_EOL
  3892. . '<label for="subform2-bar" class="optional">Bar</label>'
  3893. . PHP_EOL
  3894. . PHP_EOL
  3895. . '<input type="text" name="subform2[bar]" id="subform2-bar" value="" />'
  3896. . PHP_EOL
  3897. . PHP_EOL
  3898. . '<label for="subform1-foo" class="optional">Foo</label>'
  3899. . PHP_EOL
  3900. . PHP_EOL
  3901. . '<input type="text" name="subform1[foo]" id="subform1-foo" value="" />'
  3902. . '</form>';
  3903. $this->assertSame($expected, $form->render($this->getView()));
  3904. }
  3905. /**
  3906. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3907. *
  3908. * @link http://framework.zend.com/issues/browse/ZF-2794
  3909. * @return void
  3910. */
  3911. protected function _checkZf2794()
  3912. {
  3913. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3914. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3915. }
  3916. }
  3917. /**
  3918. * Prove the fluent interface on Zend_Form::loadDefaultDecorators
  3919. *
  3920. * @link http://framework.zend.com/issues/browse/ZF-9913
  3921. * @return void
  3922. */
  3923. public function testFluentInterfaceOnLoadDefaultDecorators()
  3924. {
  3925. $this->assertSame($this->form, $this->form->loadDefaultDecorators());
  3926. }
  3927. /**
  3928. * @group ZF-7552
  3929. */
  3930. public function testAddDecoratorsKeepsNonNumericKeyNames()
  3931. {
  3932. $this->form->addDecorators(array(array(array('td' => 'HtmlTag'),
  3933. array('tag' => 'td')),
  3934. array(array('tr' => 'HtmlTag'),
  3935. array('tag' => 'tr')),
  3936. array('HtmlTag', array('tag' => 'baz'))));
  3937. $t1 = $this->form->getDecorators();
  3938. $this->form->setDecorators($t1);
  3939. $t2 = $this->form->getDecorators();
  3940. $this->assertEquals($t1, $t2);
  3941. }
  3942. /**
  3943. * @group ZF-10411
  3944. */
  3945. public function testAddingElementToDisplayGroupManuallyShouldPreventRenderingByForm()
  3946. {
  3947. $form = new Zend_Form_FormTest_AddToDisplayGroup();
  3948. $html = $form->render($this->getView());
  3949. $this->assertEquals(1, substr_count($html, 'Customer Type'), $html);
  3950. }
  3951. /**
  3952. * @group ZF-10491
  3953. * @group ZF-10734
  3954. * @group ZF-10731
  3955. */
  3956. public function testAddElementToDisplayGroupByElementInstance()
  3957. {
  3958. $element = new Zend_Form_Element_Text('foo');
  3959. $elementTwo = new Zend_Form_Element_Text('baz-----');
  3960. $this->form->addElements(array($element, $elementTwo));
  3961. $this->form->addDisplayGroup(array($element, $elementTwo), 'bar');
  3962. $displayGroup = $this->form->getDisplayGroup('bar');
  3963. $this->assertNotNull($displayGroup->getElement('foo'));
  3964. $this->assertNotNull($displayGroup->getElement('baz'));
  3965. // clear display groups and elements
  3966. $this->form->clearDisplayGroups()
  3967. ->clearElements();
  3968. $this->form->addDisplayGroup(array($element, $elementTwo), 'bar');
  3969. $displayGroup = $this->form->getDisplayGroup('bar');
  3970. $this->assertNotNull($displayGroup->getElement('foo'));
  3971. $this->assertNotNull($displayGroup->getElement('baz'));
  3972. }
  3973. /**
  3974. * @group ZF-10149
  3975. */
  3976. public function testIfViewIsSetInTime()
  3977. {
  3978. try {
  3979. $form = new Zend_Form(array('view' => new MyTestView()));
  3980. $this->assertTrue($form->getView() instanceof MyTestView);
  3981. $form = new Zend_Form(array('view' => new StdClass()));
  3982. $this->assertNull($form->getView());
  3983. $result = $form->render();
  3984. }
  3985. catch (Zend_Form_Exception $e) {
  3986. $this->fail('Setting a view object using the options array should not throw an exception');
  3987. }
  3988. $this->assertNotEquals($result,'');
  3989. }
  3990. /**
  3991. * @group ZF-11088
  3992. */
  3993. public function testAddErrorOnElementMakesFormInvalidAndReturnsCustomError()
  3994. {
  3995. $element = new Zend_Form_Element_Text('foo');
  3996. $errorString = 'This element made a booboo';
  3997. $element->addError($errorString);
  3998. $errorMessages = $element->getErrorMessages();
  3999. $this->assertSame(1, count($errorMessages));
  4000. $this->assertSame($errorString, $errorMessages[0]);
  4001. $element2 = new Zend_Form_Element_Text('bar');
  4002. $this->form->addElement($element2);
  4003. $this->form->getElement('bar')->addError($errorString);
  4004. $errorMessages2 = $this->form->getElement('bar')->getErrorMessages();
  4005. $this->assertSame(1, count($errorMessages2));
  4006. $this->assertSame($errorString, $errorMessages2[0]);
  4007. }
  4008. /**
  4009. * @group ZF-10865
  4010. * @expectedException Zend_Form_Exception
  4011. */
  4012. public function testExceptionThrownWhenAddElementsIsGivenNullValue()
  4013. {
  4014. $form = new Zend_Form();
  4015. $form->addElement(null);
  4016. }
  4017. /**
  4018. * @group ZF-11729
  4019. */
  4020. public function testDashSeparatedElementsInDisplayGroupsShouldNotRenderOutsideDisplayGroup()
  4021. {
  4022. $form = new Zend_Form();
  4023. $form->addElement('text', 'random-element-name', array(
  4024. 'label' => 'This is weird',
  4025. 'value' => 'think its a bug',
  4026. ));
  4027. $form->addDisplayGroup(array('random-element-name'), 'foobar', array(
  4028. 'legend' => 'foobar',
  4029. ));
  4030. $html = $form->render($this->getView());
  4031. $count = substr_count($html, 'randomelementname-element');
  4032. $this->assertEquals(1, $count, $html);
  4033. }
  4034. /**
  4035. * @group ZF-11831
  4036. */
  4037. public function testElementsOfSubFormReceiveCorrectDefaultTranslator()
  4038. {
  4039. // Global default translator
  4040. $trDefault = new Zend_Translate(array(
  4041. 'adapter' => 'array',
  4042. 'content' => array(
  4043. Zend_Validate_NotEmpty::IS_EMPTY => 'Default'
  4044. ),
  4045. 'locale' => 'en'
  4046. ));
  4047. Zend_Registry::set('Zend_Translate', $trDefault);
  4048. // Translator to use for elements
  4049. $trElement = new Zend_Translate(array(
  4050. 'adapter' => 'array',
  4051. 'content' => array(
  4052. Zend_Validate_NotEmpty::IS_EMPTY =>'Element'
  4053. ),
  4054. 'locale' => 'en'
  4055. ));
  4056. Zend_Validate_Abstract::setDefaultTranslator($trElement);
  4057. // Change the form's translator
  4058. $form = new Zend_Form();
  4059. $form->addElement(new Zend_Form_Element_Text('foo', array(
  4060. 'required' => true,
  4061. 'validators' => array('NotEmpty')
  4062. )));
  4063. // Create a subform with it's own validator
  4064. $sf1 = new Zend_Form_SubForm();
  4065. $sf1->addElement(new Zend_Form_Element_Text('foosub', array(
  4066. 'required' => true,
  4067. 'validators' => array('NotEmpty')
  4068. )));
  4069. $form->addSubForm($sf1, 'Test1');
  4070. $form->isValid(array());
  4071. $messages = $form->getMessages();
  4072. $this->assertEquals(
  4073. 'Element',
  4074. @$messages['foo'][Zend_Validate_NotEmpty::IS_EMPTY],
  4075. 'Form element received wrong validator'
  4076. );
  4077. $this->assertEquals(
  4078. 'Element',
  4079. @$messages['Test1']['foosub'][Zend_Validate_NotEmpty::IS_EMPTY],
  4080. 'SubForm element received wrong validator'
  4081. );
  4082. }
  4083. /**
  4084. * @group ZF-12173
  4085. */
  4086. public function testAddingPrefixPathsWithBackslashWillLoadNamespacedPlugins()
  4087. {
  4088. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  4089. $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
  4090. return;
  4091. }
  4092. $form = new Zend_Form();
  4093. $form->addPrefixPath('Zf\Foo', 'Zf/Foo');
  4094. foreach (array('element', 'decorator') as $type) {
  4095. $loader = $form->getPluginLoader($type);
  4096. $paths = $loader->getPaths('Zf\Foo\\' . ucfirst($type));
  4097. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  4098. $this->assertFalse(empty($paths));
  4099. $this->assertContains('Foo', $paths[0]);
  4100. }
  4101. }
  4102. /**
  4103. * @group ZF-8942
  4104. */
  4105. public function testCreateElementIncludesElementDecorators()
  4106. {
  4107. // Init form
  4108. $form = new Zend_Form();
  4109. $form->setElementDecorators(
  4110. array(
  4111. new Zend_Form_Decorator_ViewHelper(),
  4112. new Zend_Form_Decorator_Label(),
  4113. )
  4114. );
  4115. $element = $form->createElement('text', 'foo');
  4116. // Test
  4117. $expected = array(
  4118. 'Zend_Form_Decorator_ViewHelper',
  4119. 'Zend_Form_Decorator_Label',
  4120. );
  4121. $actual = array();
  4122. foreach ($element->getDecorators() as $decorator) {
  4123. $actual[] = get_class($decorator);
  4124. }
  4125. $this->assertEquals($expected, $actual);
  4126. }
  4127. /**
  4128. * @group ZF-12387
  4129. */
  4130. public function testAddElementAsObjectWithDecoratorsShouldNotLoseDecorators()
  4131. {
  4132. // Init form
  4133. $form = new Zend_Form();
  4134. $form->setElementDecorators(
  4135. array(
  4136. new Zend_Form_Decorator_ViewHelper(),
  4137. new Zend_Form_Decorator_Label(),
  4138. )
  4139. );
  4140. $element = new Zend_Form_Element_Text('foo');
  4141. $element->setDecorators(array('Errors', 'Description'));
  4142. $form->addElement($element);
  4143. // Test
  4144. $expected = array(
  4145. 'Zend_Form_Decorator_Errors',
  4146. 'Zend_Form_Decorator_Description',
  4147. );
  4148. $actual = array();
  4149. foreach ($form->getElement('foo')->getDecorators() as $decorator) {
  4150. $actual[] = get_class($decorator);
  4151. }
  4152. $this->assertEquals($expected, $actual);
  4153. }
  4154. /**
  4155. * @group ZF-12387
  4156. */
  4157. public function testAddElementAsObjectGetsElementDecoratorsIfNoneIsExplicitlySet()
  4158. {
  4159. // Init form
  4160. $form = new Zend_Form();
  4161. $form->setElementDecorators(
  4162. array(
  4163. new Zend_Form_Decorator_ViewHelper(),
  4164. new Zend_Form_Decorator_Label(),
  4165. )
  4166. );
  4167. // Add element
  4168. $element = new Zend_Form_Element_Text(
  4169. 'foo',
  4170. array('disableLoadDefaultDecorators' => true)
  4171. );
  4172. $form->addElement($element);
  4173. // Test
  4174. $expected = array(
  4175. 'Zend_Form_Decorator_ViewHelper',
  4176. 'Zend_Form_Decorator_Label',
  4177. );
  4178. $actual = array();
  4179. foreach ($form->getElement('foo')->getDecorators() as $decorator) {
  4180. $actual[] = get_class($decorator);
  4181. }
  4182. $this->assertEquals($expected, $actual);
  4183. }
  4184. /**
  4185. * @group GH-319
  4186. */
  4187. public function testHasErrorsMethodShouldCheckAlsoElements()
  4188. {
  4189. // Init form
  4190. $form = new Zend_Form();
  4191. $element = new Zend_Form_Element_Text('foo');
  4192. $form->addElement($element);
  4193. $element->markAsError();
  4194. // Test form
  4195. $this->assertTrue($form->hasErrors());
  4196. $this->assertFalse($form->isValid(array('foo' => 1)));
  4197. // Test element
  4198. $this->assertTrue($element->hasErrors());
  4199. $this->assertFalse($element->isValid(1));
  4200. }
  4201. /**
  4202. * @group GH-319
  4203. */
  4204. public function testHasErrorsMethodShouldCheckAlsoSubForms()
  4205. {
  4206. // Init form
  4207. $form = new Zend_Form();
  4208. $subForm = new Zend_Form_SubForm();
  4209. $element = new Zend_Form_Element_Text('foo');
  4210. $subForm->addElement($element);
  4211. $form->addSubForm($subForm, 'subForm');
  4212. $element->markAsError();
  4213. // Test form
  4214. $this->assertTrue($form->hasErrors());
  4215. $this->assertFalse($form->isValid(array('foo' => 1)));
  4216. // Test element
  4217. $this->assertTrue($element->hasErrors());
  4218. $this->assertFalse($element->isValid(1));
  4219. }
  4220. }
  4221. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  4222. {
  4223. }
  4224. class Zend_Form_FormTest_FormExtension extends Zend_Form
  4225. {
  4226. public function init()
  4227. {
  4228. $this->setDisableLoadDefaultDecorators(true);
  4229. }
  4230. }
  4231. class Zend_Form_FormTest_WithDisplayGroup extends Zend_Form
  4232. {
  4233. public function init()
  4234. {
  4235. $this->addElement('text', 'el1', array(
  4236. 'label' => 'Title',
  4237. 'required' => true,
  4238. ));
  4239. $this->addDisplayGroup(array('el1'), 'group1', array(
  4240. 'legend' => 'legend 1',
  4241. ));
  4242. }
  4243. }
  4244. class Zend_Form_FormTest_AddToDisplayGroup extends Zend_Form_FormTest_WithDisplayGroup
  4245. {
  4246. public function init()
  4247. {
  4248. parent::init();
  4249. $element = new Zend_Form_Element_Text('el2', array(
  4250. 'label' => 'Customer Type',
  4251. ));
  4252. $this->addElement($element);
  4253. $this->group1->addElement($element);
  4254. }
  4255. }
  4256. class MyTestView extends Zend_View
  4257. {
  4258. }
  4259. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  4260. Zend_Form_FormTest::main();
  4261. }