2
0

FormTest.php 169 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2012 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->isErrors());
  2161. $this->testCanValidateFullFormContainingOnlyElements();
  2162. $this->assertTrue($this->form->isErrors());
  2163. }
  2164. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  2165. {
  2166. $this->_checkZf2794();
  2167. $this->testCanValidateFullFormContainingOnlyElements();
  2168. $codes = $this->form->getErrors();
  2169. $keys = array('foo', 'bar', 'baz');
  2170. $this->assertEquals($keys, array_keys($codes));
  2171. }
  2172. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  2173. {
  2174. $this->_checkZf2794();
  2175. $this->testCanValidateFullFormContainingOnlyElements();
  2176. $codes = $this->form->getErrors();
  2177. $keys = array('foo', 'bar', 'baz');
  2178. $errors = $this->form->getErrors('foo');
  2179. $foo = $this->form->foo;
  2180. $this->assertEquals($foo->getErrors(), $errors);
  2181. }
  2182. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  2183. {
  2184. $this->_checkZf2794();
  2185. $this->testCanValidateFullFormContainingOnlyElements();
  2186. $codes = $this->form->getMessages();
  2187. $keys = array('foo', 'bar', 'baz');
  2188. $this->assertEquals($keys, array_keys($codes));
  2189. }
  2190. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  2191. {
  2192. $this->_checkZf2794();
  2193. $this->testCanValidateFullFormContainingOnlyElements();
  2194. $codes = $this->form->getMessages();
  2195. $keys = array('foo', 'bar', 'baz');
  2196. $messages = $this->form->getMessages('foo');
  2197. $foo = $this->form->foo;
  2198. $this->assertEquals($foo->getMessages(), $messages);
  2199. }
  2200. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  2201. {
  2202. $this->_checkZf2794();
  2203. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2204. $codes = $this->form->getErrors();
  2205. $this->assertTrue(array_key_exists('sub', $codes));
  2206. $this->assertTrue(is_array($codes['sub']));
  2207. $keys = array('subfoo', 'subbar', 'subbaz');
  2208. $this->assertEquals($keys, array_keys($codes['sub']));
  2209. }
  2210. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  2211. {
  2212. $this->_checkZf2794();
  2213. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2214. $codes = $this->form->getErrors('sub');
  2215. $this->assertTrue(is_array($codes));
  2216. $this->assertFalse(empty($codes));
  2217. $keys = array('subfoo', 'subbar', 'subbaz');
  2218. $this->assertEquals($keys, array_keys($codes));
  2219. }
  2220. public function testGetErrorsHonorsElementsBelongTo()
  2221. {
  2222. $this->_checkZf2794();
  2223. $subForm = new Zend_Form_SubForm();
  2224. $subForm->setElementsBelongTo('foo[bar]');
  2225. $subForm->addElement('text', 'test')->test
  2226. ->setRequired(true);
  2227. $this->form->addSubForm($subForm, 'sub');
  2228. $data = array('foo' => array(
  2229. 'bar' => array(
  2230. 'test' => '',
  2231. ),
  2232. ));
  2233. $this->form->isValid($data);
  2234. $codes = $this->form->getErrors();
  2235. $this->assertFalse(empty($codes['foo']['bar']['test']));
  2236. }
  2237. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  2238. {
  2239. $this->_checkZf2794();
  2240. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2241. $data = array(
  2242. 'foo' => 'abcdef',
  2243. 'bar' => '123456',
  2244. 'baz' => '123abc',
  2245. 'subfoo' => '123',
  2246. 'subbar' => 'abc',
  2247. 'subbaz' => '123-abc',
  2248. );
  2249. $this->assertFalse($this->form->isValid($data));
  2250. $codes = $this->form->getMessages();
  2251. $this->assertTrue(array_key_exists('sub', $codes));
  2252. $this->assertTrue(is_array($codes['sub']));
  2253. $keys = array('subfoo', 'subbar', 'subbaz');
  2254. $this->assertEquals($keys, array_keys($codes['sub']));
  2255. }
  2256. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  2257. {
  2258. $this->_checkZf2794();
  2259. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2260. $data = array(
  2261. 'foo' => 'abcdef',
  2262. 'bar' => '123456',
  2263. 'baz' => '123abc',
  2264. 'subfoo' => '123',
  2265. 'subbar' => 'abc',
  2266. 'subbaz' => '123-abc',
  2267. );
  2268. $this->assertFalse($this->form->isValid($data));
  2269. $codes = $this->form->getMessages('sub');
  2270. $this->assertTrue(is_array($codes));
  2271. $this->assertFalse(empty($codes));
  2272. $keys = array('subfoo', 'subbar', 'subbaz');
  2273. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  2274. }
  2275. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  2276. {
  2277. $this->_checkZf2794();
  2278. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2279. $translate = new Zend_Translate('array', $translations, 'en');
  2280. $translate->setLocale('en');
  2281. $this->form->addElements(array(
  2282. 'foo' => array(
  2283. 'type' => 'text',
  2284. 'options' => array(
  2285. 'required' => true,
  2286. 'validators' => array('NotEmpty')
  2287. )
  2288. ),
  2289. 'bar' => array(
  2290. 'type' => 'text',
  2291. 'options' => array(
  2292. 'required' => true,
  2293. 'validators' => array('Digits')
  2294. )
  2295. ),
  2296. ))
  2297. ->setTranslator($translate);
  2298. $data = array(
  2299. 'foo' => '',
  2300. 'bar' => 'abc',
  2301. );
  2302. if ($this->form->isValid($data)) {
  2303. $this->fail('Form should not validate');
  2304. }
  2305. $messages = $this->form->getMessages();
  2306. $this->assertTrue(isset($messages['foo']));
  2307. $this->assertTrue(isset($messages['bar']));
  2308. foreach ($messages['foo'] as $key => $message) {
  2309. if (array_key_exists($key, $translations)) {
  2310. $this->assertEquals($translations[$key], $message);
  2311. } else {
  2312. $this->fail('Translation for ' . $key . ' does not exist?');
  2313. }
  2314. }
  2315. foreach ($messages['bar'] as $key => $message) {
  2316. if (array_key_exists($key, $translations)) {
  2317. $this->assertEquals($translations[$key], $message);
  2318. } else {
  2319. $this->fail('Translation for ' . $key . ' does not exist?');
  2320. }
  2321. }
  2322. }
  2323. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2324. {
  2325. $this->_checkZf2794();
  2326. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2327. $translate = new Zend_Translate('array', $translations, 'en');
  2328. $translate->setLocale('en');
  2329. $this->form->addElements(array(
  2330. 'foo' => array(
  2331. 'type' => 'text',
  2332. 'options' => array(
  2333. 'required' => true,
  2334. 'validators' => array('NotEmpty')
  2335. )
  2336. ),
  2337. 'bar' => array(
  2338. 'type' => 'text',
  2339. 'options' => array(
  2340. 'required' => true,
  2341. 'validators' => array('Digits')
  2342. )
  2343. ),
  2344. ))
  2345. ->setTranslator($translate);
  2346. $data = array(
  2347. 'foo' => '',
  2348. );
  2349. if ($this->form->isValidPartial($data)) {
  2350. $this->fail('Form should not validate');
  2351. }
  2352. $messages = $this->form->getMessages();
  2353. $this->assertTrue(isset($messages['foo']));
  2354. $this->assertFalse(isset($messages['bar']));
  2355. foreach ($messages['foo'] as $key => $message) {
  2356. if (array_key_exists($key, $translations)) {
  2357. $this->assertEquals($translations[$key], $message);
  2358. } else {
  2359. $this->fail('Translation for ' . $key . ' does not exist?');
  2360. }
  2361. }
  2362. }
  2363. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2364. {
  2365. $this->_checkZf2794();
  2366. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2367. $translate = new Zend_Translate('array', $translations, 'en');
  2368. $translate->setLocale('en');
  2369. $this->form->addElements(array(
  2370. 'foo' => array(
  2371. 'type' => 'text',
  2372. 'options' => array(
  2373. 'required' => true,
  2374. 'validators' => array('NotEmpty')
  2375. )
  2376. ),
  2377. 'bar' => array(
  2378. 'type' => 'text',
  2379. 'options' => array(
  2380. 'required' => true,
  2381. 'validators' => array('Digits')
  2382. )
  2383. ),
  2384. ))
  2385. ->setTranslator($translate);
  2386. $data = array(
  2387. 'foo' => '',
  2388. );
  2389. $return = $this->form->processAjax($data);
  2390. $messages = Zend_Json::decode($return);
  2391. $this->assertTrue(is_array($messages));
  2392. $this->assertTrue(isset($messages['foo']));
  2393. $this->assertFalse(isset($messages['bar']));
  2394. foreach ($messages['foo'] as $key => $message) {
  2395. if (array_key_exists($key, $translations)) {
  2396. $this->assertEquals($translations[$key], $message);
  2397. } else {
  2398. $this->fail('Translation for ' . $key . ' does not exist?');
  2399. }
  2400. }
  2401. }
  2402. /**
  2403. * @Group ZF-9697
  2404. */
  2405. public function _setup9697()
  2406. {
  2407. $callback = create_function('$value, $options',
  2408. 'return (isset($options["bar"]["quo"]["foo"]) &&
  2409. "foo Value" === $options["bar"]["quo"]["foo"]);');
  2410. $this->form->addElement('text', 'foo')
  2411. ->foo->setBelongsTo('bar[quo]');
  2412. $this->form->addElement('text', 'quo')
  2413. ->quo->setBelongsTo('bar[quo]')
  2414. ->addValidator('Callback',
  2415. false,
  2416. $callback);
  2417. return array('bar' => array('quo' => array('foo' => 'foo Value',
  2418. 'quo' => 'quo Value')));
  2419. }
  2420. public function testIsValidKeepsContext()
  2421. {
  2422. $data = $this->_setup9697();
  2423. $this->assertTrue($this->form->isValid($data));
  2424. }
  2425. public function testIsValidPartialKeepsContext()
  2426. {
  2427. $data = $this->_setup9697();
  2428. $this->assertTrue($this->form->isValidPartial($data));
  2429. }
  2430. public function testGetValidValuesKeepsContext()
  2431. {
  2432. $data = $this->_setup9697();
  2433. $this->assertSame($data, $this->form->getValidValues($data));
  2434. }
  2435. /**
  2436. * @group ZF-2988
  2437. */
  2438. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2439. {
  2440. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2441. $this->form->addErrorMessage('Invalid values entered');
  2442. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2443. $messages = $this->form->getMessages();
  2444. $this->assertEquals(1, count($messages));
  2445. $this->assertEquals('Invalid values entered', array_shift($messages));
  2446. }
  2447. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2448. {
  2449. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2450. $this->form->addErrorMessage('Invalid values entered');
  2451. $this->form->addErrorMessage('Really, they are not valid');
  2452. $messages = $this->form->getErrorMessages();
  2453. $this->assertEquals(2, count($messages));
  2454. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2455. $messages = $this->form->getMessages();
  2456. $this->assertEquals(2, count($messages));
  2457. $this->assertEquals('Invalid values entered', array_shift($messages));
  2458. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2459. }
  2460. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2461. {
  2462. $set1 = array('foo', 'bar', 'baz');
  2463. $this->form->addErrorMessages($set1);
  2464. $this->assertSame($set1, $this->form->getErrorMessages());
  2465. }
  2466. public function testSetErrorMessagesShouldOverwriteMessages()
  2467. {
  2468. $set1 = array('foo', 'bar', 'baz');
  2469. $set2 = array('bat', 'cat');
  2470. $this->form->addErrorMessages($set1);
  2471. $this->assertSame($set1, $this->form->getErrorMessages());
  2472. $this->form->setErrorMessages($set2);
  2473. $this->assertSame($set2, $this->form->getErrorMessages());
  2474. }
  2475. public function testCustomErrorMessageStackShouldBeClearable()
  2476. {
  2477. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2478. $this->form->clearErrorMessages();
  2479. $messages = $this->form->getErrorMessages();
  2480. $this->assertTrue(empty($messages));
  2481. }
  2482. public function testCustomErrorMessagesShouldBeTranslated()
  2483. {
  2484. $translations = array(
  2485. 'foo' => 'Foo message',
  2486. );
  2487. $translate = new Zend_Translate('array', $translations);
  2488. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2489. $this->form->setTranslator($translate)
  2490. ->addErrorMessage('foo');
  2491. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2492. $messages = $this->form->getMessages();
  2493. $this->assertEquals(1, count($messages));
  2494. $this->assertEquals('Foo message', array_shift($messages));
  2495. }
  2496. public function testShouldAllowMarkingFormAsInvalid()
  2497. {
  2498. $this->form->addErrorMessage('Invalid values entered');
  2499. $this->assertFalse($this->form->isErrors());
  2500. $this->form->markAsError();
  2501. $this->assertTrue($this->form->isErrors());
  2502. $messages = $this->form->getMessages();
  2503. $this->assertEquals(1, count($messages));
  2504. $this->assertEquals('Invalid values entered', array_shift($messages));
  2505. }
  2506. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2507. {
  2508. $this->assertFalse($this->form->isErrors());
  2509. $this->form->setErrors(array('Error 1', 'Error 2'))
  2510. ->addError('Error 3')
  2511. ->addErrors(array('Error 4', 'Error 5'));
  2512. $this->assertTrue($this->form->isErrors());
  2513. $messages = $this->form->getMessages();
  2514. $this->assertEquals(5, count($messages));
  2515. foreach (range(1, 5) as $id) {
  2516. $message = 'Error ' . $id;
  2517. $this->assertContains($message, $messages);
  2518. }
  2519. }
  2520. /**#@-*/
  2521. // View object
  2522. public function getView()
  2523. {
  2524. $view = new Zend_View();
  2525. return $view;
  2526. }
  2527. public function testGetViewRetrievesFromViewRendererByDefault()
  2528. {
  2529. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2530. $viewRenderer->initView();
  2531. $view = $viewRenderer->view;
  2532. $test = $this->form->getView();
  2533. $this->assertSame($view, $test);
  2534. }
  2535. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2536. {
  2537. $this->assertNull($this->form->getView());
  2538. }
  2539. public function testCanSetView()
  2540. {
  2541. $view = new Zend_View();
  2542. $this->assertNull($this->form->getView());
  2543. $this->form->setView($view);
  2544. $received = $this->form->getView();
  2545. $this->assertSame($view, $received);
  2546. }
  2547. // Decorators
  2548. public function testFormDecoratorRegisteredByDefault()
  2549. {
  2550. $this->_checkZf2794();
  2551. $decorator = $this->form->getDecorator('form');
  2552. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2553. }
  2554. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2555. {
  2556. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2557. $decorators = $form->getDecorators();
  2558. $this->assertEquals(array(), $decorators);
  2559. }
  2560. public function testCanAddSingleDecoratorAsString()
  2561. {
  2562. $this->_checkZf2794();
  2563. $this->form->clearDecorators();
  2564. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2565. $this->form->addDecorator('viewHelper');
  2566. $decorator = $this->form->getDecorator('viewHelper');
  2567. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2568. }
  2569. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2570. {
  2571. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2572. }
  2573. public function testCanAddSingleDecoratorAsDecoratorObject()
  2574. {
  2575. $this->form->clearDecorators();
  2576. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2577. $decorator = new Zend_Form_Decorator_ViewHelper;
  2578. $this->form->addDecorator($decorator);
  2579. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2580. $this->assertSame($decorator, $test);
  2581. }
  2582. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2583. {
  2584. $this->_checkZf2794();
  2585. $this->form->clearDecorators();
  2586. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2587. $decorator = new Zend_Form_Decorator_ViewHelper;
  2588. $this->form->addDecorator($decorator);
  2589. $test = $this->form->getDecorator('viewHelper');
  2590. $this->assertSame($decorator, $test);
  2591. }
  2592. public function testCanAddMultipleDecorators()
  2593. {
  2594. $this->_checkZf2794();
  2595. $this->form->clearDecorators();
  2596. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2597. $testDecorator = new Zend_Form_Decorator_Errors;
  2598. $this->form->addDecorators(array(
  2599. 'ViewHelper',
  2600. $testDecorator
  2601. ));
  2602. $viewHelper = $this->form->getDecorator('viewHelper');
  2603. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2604. $decorator = $this->form->getDecorator('errors');
  2605. $this->assertSame($testDecorator, $decorator);
  2606. }
  2607. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2608. {
  2609. $this->_checkZf2794();
  2610. $this->assertFalse($this->form->removeDecorator('foobar'));
  2611. }
  2612. public function testCanRemoveDecorator()
  2613. {
  2614. $this->_checkZf2794();
  2615. $this->testFormDecoratorRegisteredByDefault();
  2616. $this->form->removeDecorator('form');
  2617. $this->assertFalse($this->form->getDecorator('form'));
  2618. }
  2619. /**
  2620. * @group ZF-3069
  2621. */
  2622. public function testRemovingNamedDecoratorShouldWork()
  2623. {
  2624. $this->_checkZf2794();
  2625. $this->form->setDecorators(array(
  2626. 'FormElements',
  2627. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2628. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2629. ));
  2630. $decorators = $this->form->getDecorators();
  2631. $this->assertTrue(array_key_exists('div', $decorators));
  2632. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2633. $this->form->removeDecorator('div');
  2634. $decorators = $this->form->getDecorators();
  2635. $this->assertFalse(array_key_exists('div', $decorators));
  2636. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2637. }
  2638. public function testCanClearAllDecorators()
  2639. {
  2640. $this->_checkZf2794();
  2641. $this->testCanAddMultipleDecorators();
  2642. $this->form->clearDecorators();
  2643. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2644. $this->assertFalse($this->form->getDecorator('fieldset'));
  2645. }
  2646. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2647. {
  2648. $this->_checkZf2794();
  2649. $this->form->setDecorators(array(
  2650. array('HtmlTag', array('tag' => 'div')),
  2651. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2652. ));
  2653. $decorator = $this->form->getDecorator('FooBar');
  2654. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2655. $this->assertEquals('dd', $decorator->getOption('tag'));
  2656. $decorator = $this->form->getDecorator('HtmlTag');
  2657. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2658. $this->assertEquals('div', $decorator->getOption('tag'));
  2659. }
  2660. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2661. {
  2662. $this->form->setDecorators(array(
  2663. 'FormElements',
  2664. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2665. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2666. 'Form',
  2667. ));
  2668. $decorator = $this->form->getDecorator('fieldset');
  2669. $decorators = $this->form->getDecorators();
  2670. $i = 0;
  2671. $order = array();
  2672. foreach (array_keys($decorators) as $name) {
  2673. $order[$name] = $i;
  2674. ++$i;
  2675. }
  2676. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2677. }
  2678. // Rendering
  2679. public function checkMarkup($html)
  2680. {
  2681. $this->assertFalse(empty($html));
  2682. $this->assertContains('<form', $html);
  2683. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2684. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2685. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2686. $this->assertContains('</form>', $html);
  2687. }
  2688. public function testRenderReturnsMarkup()
  2689. {
  2690. $this->setupElements();
  2691. $html = $this->form->render($this->getView());
  2692. $this->checkMarkup($html);
  2693. }
  2694. public function testRenderReturnsMarkupRepresentingAllElements()
  2695. {
  2696. $this->testRenderReturnsMarkup();
  2697. $html = $this->form->render();
  2698. foreach ($this->form->getElements() as $key => $element) {
  2699. $this->assertFalse(empty($key));
  2700. $this->assertFalse(is_numeric($key));
  2701. $this->assertContains('<input', $html);
  2702. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2703. }
  2704. }
  2705. public function testRenderReturnsMarkupContainingSubForms()
  2706. {
  2707. $this->setupElements();
  2708. $this->setupSubForm();
  2709. $this->form->setView($this->getView());
  2710. $html = $this->form->render();
  2711. $this->assertRegexp('/<fieldset/', $html);
  2712. $this->assertContains('</fieldset>', $html);
  2713. foreach ($this->form->sub as $key => $item) {
  2714. $this->assertFalse(empty($key));
  2715. $this->assertFalse(is_numeric($key));
  2716. $this->assertContains('<input', $html);
  2717. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2718. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2719. }
  2720. }
  2721. public function testRenderReturnsMarkupContainingDisplayGroups()
  2722. {
  2723. $this->setupElements();
  2724. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2725. $this->form->setView($this->getView());
  2726. $html = $this->html = $this->form->render();
  2727. $this->assertRegexp('/<fieldset/', $html);
  2728. $this->assertContains('</fieldset>', $html);
  2729. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2730. $dom = new DOMDocument();
  2731. $dom->loadHTML($html);
  2732. $fieldsets = $dom->getElementsByTagName('fieldset');
  2733. $this->assertTrue(0 < $fieldsets->length);
  2734. $fieldset = $fieldsets->item(0);
  2735. $nodes = $fieldset->childNodes;
  2736. $this->assertNotNull($nodes);
  2737. for ($i = 0; $i < $nodes->length; ++$i) {
  2738. $node = $nodes->item($i);
  2739. if ('input' != $node->nodeName) {
  2740. continue;
  2741. }
  2742. $this->assertTrue($node->hasAttribute('name'));
  2743. $nameNode = $node->getAttributeNode('name');
  2744. switch ($i) {
  2745. case 0:
  2746. $this->assertEquals('foo', $nameNode->nodeValue);
  2747. break;
  2748. case 1:
  2749. $this->assertEquals('baz', $nameNode->nodeValue);
  2750. break;
  2751. default:
  2752. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2753. }
  2754. }
  2755. }
  2756. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2757. {
  2758. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2759. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2760. $this->fail("Should find foo element in rendered form");
  2761. }
  2762. $this->assertEquals(1, count($matches));
  2763. $this->assertEquals(1, count($matches[0]));
  2764. }
  2765. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2766. {
  2767. $this->setupElements();
  2768. $this->form->setElementsBelongTo('anArray');
  2769. $html = $this->form->render($this->getView());
  2770. $this->assertContains('name="anArray[foo]"', $html);
  2771. $this->assertContains('name="anArray[bar]"', $html);
  2772. $this->assertContains('name="anArray[baz]"', $html);
  2773. $this->assertContains('id="anArray-foo"', $html);
  2774. $this->assertContains('id="anArray-bar"', $html);
  2775. $this->assertContains('id="anArray-baz"', $html);
  2776. }
  2777. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2778. {
  2779. $this->setupElements();
  2780. $this->form->setElementsBelongTo('data[foo]');
  2781. $html = $this->form->render($this->getView());
  2782. $this->assertContains('name="data[foo][foo]"', $html);
  2783. $this->assertContains('name="data[foo][bar]"', $html);
  2784. $this->assertContains('name="data[foo][baz]"', $html);
  2785. $this->assertContains('id="data-foo-foo"', $html);
  2786. $this->assertContains('id="data-foo-bar"', $html);
  2787. $this->assertContains('id="data-foo-baz"', $html);
  2788. }
  2789. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2790. {
  2791. $this->setupElements();
  2792. $this->form->setName('data')
  2793. ->setIsArray(true);
  2794. $html = $this->form->render($this->getView());
  2795. $this->assertContains('name="data[foo]"', $html);
  2796. $this->assertContains('name="data[bar]"', $html);
  2797. $this->assertContains('name="data[baz]"', $html);
  2798. $this->assertContains('id="data-foo"', $html);
  2799. $this->assertContains('id="data-bar"', $html);
  2800. $this->assertContains('id="data-baz"', $html);
  2801. }
  2802. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2803. {
  2804. $this->form->setName('data')
  2805. ->setIsArray(true);
  2806. $subForm = new Zend_Form_SubForm();
  2807. $subForm->setElementsBelongTo('billing[info]');
  2808. $subForm->addElement('text', 'name');
  2809. $subForm->addElement('text', 'number');
  2810. $this->form->addSubForm($subForm, 'sub');
  2811. $html = $this->form->render($this->getView());
  2812. $this->assertContains('name="data[billing][info][name]', $html);
  2813. $this->assertContains('name="data[billing][info][number]', $html);
  2814. $this->assertContains('id="data-billing-info-name"', $html);
  2815. $this->assertContains('id="data-billing-info-number"', $html);
  2816. }
  2817. public function testToStringProxiesToRender()
  2818. {
  2819. $this->setupElements();
  2820. $this->form->setView($this->getView());
  2821. $html = $this->form->__toString();
  2822. $this->checkMarkup($html);
  2823. }
  2824. public function raiseDecoratorException($content, $element, $options)
  2825. {
  2826. throw new Exception('Raising exception in decorator callback');
  2827. }
  2828. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2829. {
  2830. $this->error = $errstr;
  2831. }
  2832. public function testToStringRaisesErrorWhenExceptionCaught()
  2833. {
  2834. $this->form->setDecorators(array(
  2835. array(
  2836. 'decorator' => 'Callback',
  2837. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2838. ),
  2839. ));
  2840. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2841. $text = $this->form->__toString();
  2842. restore_error_handler();
  2843. $this->assertTrue(empty($text));
  2844. $this->assertTrue(isset($this->error));
  2845. $this->assertContains('Raising exception in decorator callback', $this->error);
  2846. }
  2847. /**
  2848. * ZF-2718
  2849. */
  2850. public function testHiddenElementsGroupedWhenRendered()
  2851. {
  2852. $this->markTestIncomplete('Scheduling for future release');
  2853. $this->form->addElements(array(
  2854. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2855. array('type' => 'text', 'name' => 'testone'),
  2856. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2857. array('type' => 'text', 'name' => 'testtwo'),
  2858. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2859. array('type' => 'text', 'name' => 'testthree'),
  2860. ));
  2861. $html = $this->form->render($this->getView());
  2862. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2863. $this->fail('Hidden elements should be grouped');
  2864. }
  2865. foreach (array('first', 'second', 'third') as $which) {
  2866. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2867. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2868. }
  2869. }
  2870. // Localization
  2871. public function testTranslatorIsNullByDefault()
  2872. {
  2873. $this->assertNull($this->form->getTranslator());
  2874. }
  2875. public function testCanSetTranslator()
  2876. {
  2877. require_once 'Zend/Translate/Adapter/Array.php';
  2878. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2879. $this->form->setTranslator($translator);
  2880. $received = $this->form->getTranslator($translator);
  2881. $this->assertSame($translator->getAdapter(), $received);
  2882. }
  2883. public function testCanSetDefaultGlobalTranslator()
  2884. {
  2885. $this->assertNull($this->form->getTranslator());
  2886. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2887. Zend_Form::setDefaultTranslator($translator);
  2888. $received = Zend_Form::getDefaultTranslator();
  2889. $this->assertSame($translator->getAdapter(), $received);
  2890. $received = $this->form->getTranslator();
  2891. $this->assertSame($translator->getAdapter(), $received);
  2892. $form = new Zend_Form();
  2893. $received = $form->getTranslator();
  2894. $this->assertSame($translator->getAdapter(), $received);
  2895. }
  2896. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2897. {
  2898. $this->assertNull($this->form->getTranslator());
  2899. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2900. Zend_Form::setDefaultTranslator($translatorDefault);
  2901. $received = $this->form->getTranslator();
  2902. $this->assertSame($translatorDefault->getAdapter(), $received);
  2903. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2904. $this->form->setTranslator($translator);
  2905. $received = $this->form->getTranslator();
  2906. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2907. $this->assertSame($translator->getAdapter(), $received);
  2908. }
  2909. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2910. {
  2911. $this->assertNull($this->form->getTranslator());
  2912. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2913. Zend_Registry::set('Zend_Translate', $translator);
  2914. $received = Zend_Form::getDefaultTranslator();
  2915. $this->assertSame($translator->getAdapter(), $received);
  2916. $received = $this->form->getTranslator();
  2917. $this->assertSame($translator->getAdapter(), $received);
  2918. $form = new Zend_Form();
  2919. $received = $form->getTranslator();
  2920. $this->assertSame($translator->getAdapter(), $received);
  2921. }
  2922. public function testCanDisableTranslation()
  2923. {
  2924. $this->testCanSetDefaultGlobalTranslator();
  2925. $this->form->setDisableTranslator(true);
  2926. $this->assertNull($this->form->getTranslator());
  2927. }
  2928. // Iteration
  2929. public function testFormObjectIsIterableAndIteratesElements()
  2930. {
  2931. $this->setupElements();
  2932. $expected = array('foo', 'bar', 'baz');
  2933. $received = array();
  2934. foreach ($this->form as $key => $value) {
  2935. $received[] = $key;
  2936. }
  2937. $this->assertSame($expected, $received);
  2938. }
  2939. public function testFormObjectIteratesElementsInExpectedOrder()
  2940. {
  2941. $this->setupElements();
  2942. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2943. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2944. $received = array();
  2945. foreach ($this->form as $key => $value) {
  2946. $received[] = $key;
  2947. $this->assertTrue($value instanceof Zend_Form_Element);
  2948. }
  2949. $this->assertSame($expected, $received);
  2950. }
  2951. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2952. {
  2953. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2954. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2955. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2956. $expected = array('name', 'email', 'submit');
  2957. $received = array();
  2958. foreach ($this->form as $key => $value) {
  2959. $received[] = $key;
  2960. $this->assertTrue($value instanceof Zend_Form_Element);
  2961. }
  2962. $this->assertSame($expected, $received);
  2963. }
  2964. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2965. {
  2966. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2967. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2968. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2969. ->setView($this->getView());
  2970. $test = $this->form->render();
  2971. $this->assertContains('name="a"', $test);
  2972. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2973. $this->fail('Expected markup not found');
  2974. }
  2975. $order = array();
  2976. foreach ($matches[1] as $element) {
  2977. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2978. $order[] = $m[1];
  2979. }
  2980. }
  2981. $this->assertSame(array('b', 'c', 'a'), $order);
  2982. }
  2983. public function testFormObjectIteratesElementsAndSubforms()
  2984. {
  2985. $this->setupElements();
  2986. $this->setupSubForm();
  2987. $expected = array('foo', 'bar', 'baz', 'sub');
  2988. $received = array();
  2989. foreach ($this->form as $key => $value) {
  2990. $received[] = $key;
  2991. $this->assertTrue(($value instanceof Zend_Form_Element)
  2992. or ($value instanceof Zend_Form_SubForm));
  2993. }
  2994. $this->assertSame($expected, $received);
  2995. }
  2996. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  2997. {
  2998. $this->setupElements();
  2999. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3000. $expected = array('bar', 'foobaz');
  3001. $received = array();
  3002. foreach ($this->form as $key => $value) {
  3003. $received[] = $key;
  3004. $this->assertTrue(($value instanceof Zend_Form_Element)
  3005. or ($value instanceof Zend_Form_DisplayGroup));
  3006. }
  3007. $this->assertSame($expected, $received);
  3008. }
  3009. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  3010. {
  3011. $this->setupElements();
  3012. $bar = $this->form->bar;
  3013. $this->form->removeElement('bar');
  3014. try {
  3015. foreach ($this->form as $item) {
  3016. }
  3017. } catch (Exception $e) {
  3018. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3019. }
  3020. $this->form->addElement($bar);
  3021. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  3022. $this->form->removeDisplayGroup('bazbar');
  3023. try {
  3024. foreach ($this->form as $item) {
  3025. }
  3026. } catch (Exception $e) {
  3027. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3028. }
  3029. $subForm = new Zend_Form_SubForm;
  3030. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  3031. $this->form->addSubForm($subForm, 'page1');
  3032. $this->form->removeSubForm('page1');
  3033. try {
  3034. foreach ($this->form as $item) {
  3035. }
  3036. } catch (Exception $e) {
  3037. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3038. }
  3039. }
  3040. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  3041. {
  3042. $form = new Zend_Form();
  3043. $form->addElements(array(
  3044. 'username' => 'text',
  3045. 'password' => 'text',
  3046. ));
  3047. $form->clearElements();
  3048. try {
  3049. foreach ($form as $item) {
  3050. }
  3051. } catch (Zend_Form_Exception $e) {
  3052. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  3053. . $e->getMessage();
  3054. $this->fail($message);
  3055. }
  3056. $form->addElements(array(
  3057. 'username' => 'text',
  3058. 'password' => 'text',
  3059. ))
  3060. ->addDisplayGroup(array('username', 'password'), 'login');
  3061. $form->clearDisplayGroups();
  3062. try {
  3063. foreach ($form as $item) {
  3064. }
  3065. } catch (Zend_Form_Exception $e) {
  3066. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  3067. . $e->getMessage();
  3068. $this->fail($message);
  3069. }
  3070. $subForm = new Zend_Form_SubForm();
  3071. $form->addSubForm($subForm, 'foo');
  3072. $form->clearSubForms();
  3073. try {
  3074. foreach ($form as $item) {
  3075. }
  3076. } catch (Zend_Form_Exception $e) {
  3077. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  3078. . $e->getMessage();
  3079. $this->fail($message);
  3080. }
  3081. }
  3082. // Countable
  3083. public function testCanCountFormObject()
  3084. {
  3085. $this->setupElements();
  3086. $this->assertEquals(3, count($this->form));
  3087. }
  3088. public function testCountingFormObjectCountsSubForms()
  3089. {
  3090. $this->setupElements();
  3091. $this->setupSubForm();
  3092. $this->assertEquals(4, count($this->form));
  3093. }
  3094. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  3095. {
  3096. $this->testCountingFormObjectCountsSubForms();
  3097. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3098. $this->assertEquals(3, count($this->form));
  3099. }
  3100. // Element decorators and plugin paths
  3101. public function testCanSetAllElementDecoratorsAtOnce()
  3102. {
  3103. $this->_checkZf2794();
  3104. $this->setupElements();
  3105. $this->form->setElementDecorators(array(
  3106. array('ViewHelper'),
  3107. array('Label'),
  3108. array('Fieldset'),
  3109. ));
  3110. foreach ($this->form->getElements() as $element) {
  3111. $this->assertFalse($element->getDecorator('Errors'));
  3112. $this->assertFalse($element->getDecorator('HtmlTag'));
  3113. $decorator = $element->getDecorator('ViewHelper');
  3114. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  3115. $decorator = $element->getDecorator('Label');
  3116. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  3117. $decorator = $element->getDecorator('Fieldset');
  3118. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3119. }
  3120. }
  3121. /**
  3122. * @group ZF-3597
  3123. */
  3124. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  3125. {
  3126. $this->form->setDecorators(array(
  3127. 'FormElements',
  3128. array('HtmlTag', array('tag' => 'table')),
  3129. 'Form',
  3130. ));
  3131. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  3132. $this->form->addElement('text', 'test', array(
  3133. 'label' => 'Foo',
  3134. 'description' => 'sample description',
  3135. ));
  3136. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  3137. $decorator = new My_Decorator_TableRow();
  3138. $this->form->setElementDecorators(array(
  3139. 'ViewHelper',
  3140. $decorator,
  3141. ));
  3142. $html = $this->form->render($this->getView());
  3143. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  3144. }
  3145. /**
  3146. * @group ZF-3228
  3147. */
  3148. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  3149. {
  3150. $this->_checkZf2794();
  3151. $this->setupElements();
  3152. $this->form->setElementDecorators(
  3153. array(
  3154. 'Description',
  3155. 'Form',
  3156. 'Fieldset',
  3157. ),
  3158. array(
  3159. 'bar',
  3160. )
  3161. );
  3162. $element = $this->form->bar;
  3163. $this->assertFalse($element->getDecorator('ViewHelper'));
  3164. $this->assertFalse($element->getDecorator('Errors'));
  3165. $this->assertFalse($element->getDecorator('Label'));
  3166. $this->assertFalse($element->getDecorator('HtmlTag'));
  3167. $decorator = $element->getDecorator('Description');
  3168. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3169. $decorator = $element->getDecorator('Form');
  3170. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3171. $decorator = $element->getDecorator('Fieldset');
  3172. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3173. foreach (array('foo', 'baz') as $name) {
  3174. $element = $this->form->$name;
  3175. $this->assertFalse($element->getDecorator('Form'));
  3176. $this->assertFalse($element->getDecorator('Fieldset'));
  3177. }
  3178. }
  3179. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  3180. {
  3181. $this->_checkZf2794();
  3182. $this->setupElements();
  3183. $this->form->setElementDecorators(
  3184. array(
  3185. 'Description',
  3186. 'Form',
  3187. 'Fieldset',
  3188. ),
  3189. array(
  3190. 'foo',
  3191. 'baz',
  3192. ),
  3193. false
  3194. );
  3195. $element = $this->form->bar;
  3196. $this->assertFalse($element->getDecorator('ViewHelper'));
  3197. $this->assertFalse($element->getDecorator('Errors'));
  3198. $this->assertFalse($element->getDecorator('Label'));
  3199. $this->assertFalse($element->getDecorator('HtmlTag'));
  3200. $decorator = $element->getDecorator('Description');
  3201. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3202. $decorator = $element->getDecorator('Form');
  3203. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3204. $decorator = $element->getDecorator('Fieldset');
  3205. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3206. foreach (array('foo', 'baz') as $name) {
  3207. $element = $this->form->$name;
  3208. $this->assertFalse($element->getDecorator('Form'));
  3209. $this->assertFalse($element->getDecorator('Fieldset'));
  3210. }
  3211. }
  3212. /**#@-*/
  3213. public function testCanSetAllElementFiltersAtOnce()
  3214. {
  3215. $this->_checkZf2794();
  3216. $this->setupElements();
  3217. $this->form->setElementFilters(array(
  3218. 'Alnum',
  3219. 'StringToLower'
  3220. ));
  3221. foreach ($this->form->getElements() as $element) {
  3222. $filter = $element->getFilter('Alnum');
  3223. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  3224. $filter = $element->getFilter('StringToLower');
  3225. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  3226. }
  3227. }
  3228. public function testCanSetGlobalElementPrefixPath()
  3229. {
  3230. $this->setupElements();
  3231. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3232. $this->form->addElement('text', 'prefixTest');
  3233. foreach ($this->form->getElements() as $element) {
  3234. $loader = $element->getPluginLoader('validate');
  3235. $paths = $loader->getPaths('Zend_Foo_Validate');
  3236. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  3237. $this->assertContains('Foo', $paths[0]);
  3238. $this->assertContains('Validate', $paths[0]);
  3239. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  3240. $this->assertFalse(empty($paths));
  3241. $this->assertContains('Foo', $paths[0]);
  3242. $this->assertContains('Filter', $paths[0]);
  3243. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  3244. $this->assertFalse(empty($paths));
  3245. $this->assertContains('Foo', $paths[0]);
  3246. $this->assertContains('Decorator', $paths[0]);
  3247. }
  3248. }
  3249. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  3250. {
  3251. $this->_checkZf2794();
  3252. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  3253. $this->form->addElement('text', 'prefixTest');
  3254. $element = $this->form->prefixTest;
  3255. $label = $element->getDecorator('Label');
  3256. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  3257. }
  3258. /**
  3259. * @group ZF-3093
  3260. */
  3261. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  3262. {
  3263. $subForm = new Zend_Form_SubForm();
  3264. $subForm->addElement('text', 'foo');
  3265. $this->form->addSubForm($subForm, 'subForm');
  3266. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3267. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  3268. $paths = $loader->getPaths('Zend_Foo_Decorator');
  3269. $this->assertFalse(empty($paths));
  3270. $this->assertContains('Foo', $paths[0]);
  3271. $this->assertContains('Decorator', $paths[0]);
  3272. }
  3273. public function testCanSetElementValidatorPrefixPath()
  3274. {
  3275. $this->setupElements();
  3276. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  3277. $this->form->addElement('text', 'prefixTest');
  3278. foreach ($this->form->getElements() as $element) {
  3279. $loader = $element->getPluginLoader('validate');
  3280. $paths = $loader->getPaths('Zend_Foo');
  3281. $this->assertFalse(empty($paths));
  3282. $this->assertContains('Foo', $paths[0]);
  3283. $this->assertNotContains('Validate', $paths[0]);
  3284. }
  3285. }
  3286. public function testCanSetElementFilterPrefixPath()
  3287. {
  3288. $this->setupElements();
  3289. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  3290. $this->form->addElement('text', 'prefixTest');
  3291. foreach ($this->form->getElements() as $element) {
  3292. $loader = $element->getPluginLoader('filter');
  3293. $paths = $loader->getPaths('Zend_Foo');
  3294. $this->assertFalse(empty($paths));
  3295. $this->assertContains('Foo', $paths[0]);
  3296. $this->assertNotContains('Filter', $paths[0]);
  3297. }
  3298. }
  3299. public function testCanSetElementDecoratorPrefixPath()
  3300. {
  3301. $this->setupElements();
  3302. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  3303. $this->form->addElement('text', 'prefixTest');
  3304. foreach ($this->form->getElements() as $element) {
  3305. $loader = $element->getPluginLoader('decorator');
  3306. $paths = $loader->getPaths('Zend_Foo');
  3307. $this->assertFalse(empty($paths));
  3308. $this->assertContains('Foo', $paths[0]);
  3309. $this->assertNotContains('Decorator', $paths[0]);
  3310. }
  3311. }
  3312. // Display Group decorators and plugin paths
  3313. public function setupDisplayGroups()
  3314. {
  3315. $this->testCanAddAndRetrieveMultipleElements();
  3316. $this->form->addElements(array(
  3317. 'test1' => 'text',
  3318. 'test2' => 'text',
  3319. 'test3' => 'text',
  3320. 'test4' => 'text'
  3321. ));
  3322. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  3323. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3324. }
  3325. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  3326. {
  3327. $this->_checkZf2794();
  3328. $this->setupDisplayGroups();
  3329. $this->form->setDisplayGroupDecorators(array(
  3330. array('Callback', array('callback' => 'strip_tags')),
  3331. ));
  3332. foreach ($this->form->getDisplayGroups() as $element) {
  3333. $this->assertFalse($element->getDecorator('FormElements'));
  3334. $this->assertFalse($element->getDecorator('HtmlTag'));
  3335. $this->assertFalse($element->getDecorator('Fieldset'));
  3336. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  3337. $decorator = $element->getDecorator('Callback');
  3338. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3339. }
  3340. }
  3341. public function testCanSetDisplayGroupPrefixPath()
  3342. {
  3343. $this->setupDisplayGroups();
  3344. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3345. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3346. foreach ($this->form->getDisplayGroups() as $group) {
  3347. $loader = $group->getPluginLoader();
  3348. $paths = $loader->getPaths('Zend_Foo');
  3349. $this->assertFalse(empty($paths));
  3350. $this->assertContains('Foo', $paths[0]);
  3351. }
  3352. }
  3353. /**
  3354. * @group ZF-3213
  3355. */
  3356. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3357. {
  3358. require_once 'Zend/Config/Ini.php';
  3359. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3360. $form = new Zend_Form($config);
  3361. $dg = $form->foofoo;
  3362. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3363. $this->assertTrue($paths !== false);
  3364. }
  3365. // Subform decorators
  3366. public function testCanSetAllSubFormDecoratorsAtOnce()
  3367. {
  3368. $this->_checkZf2794();
  3369. $this->setupSubForm();
  3370. $this->form->setSubFormDecorators(array(
  3371. array('Callback', array('callback' => 'strip_tags')),
  3372. ));
  3373. foreach ($this->form->getSubForms() as $subForm) {
  3374. $this->assertFalse($subForm->getDecorator('FormElements'));
  3375. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3376. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3377. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3378. $decorator = $subForm->getDecorator('Callback');
  3379. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3380. }
  3381. }
  3382. // Extension
  3383. public function testInitCalledPriorToLoadingDefaultDecorators()
  3384. {
  3385. $form = new Zend_Form_FormTest_FormExtension();
  3386. $decorators = $form->getDecorators();
  3387. $this->assertTrue(empty($decorators));
  3388. }
  3389. // Clone
  3390. /**
  3391. * @group ZF-3819
  3392. */
  3393. public function testCloningShouldCloneAllChildren()
  3394. {
  3395. $form = new Zend_Form();
  3396. $foo = new Zend_Form_SubForm(array(
  3397. 'name' => 'foo',
  3398. 'elements' => array(
  3399. 'one' => 'text',
  3400. 'two' => 'text',
  3401. ),
  3402. ));
  3403. $form->addElement('text', 'bar')
  3404. ->addElement('text', 'baz')
  3405. ->addElement('text', 'bat')
  3406. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3407. ->addSubForm($foo, 'foo');
  3408. $bar = $form->bar;
  3409. $baz = $form->baz;
  3410. $bat = $form->bat;
  3411. $barbat = $form->barbat;
  3412. $cloned = clone $form;
  3413. $this->assertNotSame($foo, $cloned->foo);
  3414. $this->assertNotSame($bar, $cloned->bar);
  3415. $this->assertNotSame($baz, $cloned->baz);
  3416. $this->assertNotSame($bat, $cloned->bat);
  3417. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3418. $this->assertNotSame($foo->one, $cloned->foo->one);
  3419. $this->assertNotSame($foo->two, $cloned->foo->two);
  3420. }
  3421. // Reset
  3422. /**
  3423. * @group ZF-3227
  3424. */
  3425. public function testFormsShouldAllowResetting()
  3426. {
  3427. $form = new Zend_Form();
  3428. $foo = new Zend_Form_SubForm(array(
  3429. 'name' => 'foo',
  3430. 'elements' => array(
  3431. 'one' => 'text',
  3432. 'two' => 'text',
  3433. ),
  3434. ));
  3435. $form->addElement('text', 'bar')
  3436. ->addElement('text', 'baz')
  3437. ->addElement('text', 'bat')
  3438. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3439. ->addSubForm($foo, 'foo');
  3440. $values = array(
  3441. 'bar' => 'Bar Value',
  3442. 'baz' => 'Baz Value',
  3443. 'bat' => 'Bat Value',
  3444. 'foo' => array(
  3445. 'one' => 'One Value',
  3446. 'two' => 'Two Value',
  3447. ),
  3448. );
  3449. $form->populate($values);
  3450. $test = $form->getValues();
  3451. $this->assertEquals($values, $test);
  3452. $form->reset();
  3453. $test = $form->getValues();
  3454. $this->assertNotEquals($values, $test);
  3455. $this->assertEquals(0, array_sum($test));
  3456. }
  3457. /**
  3458. * @group ZF-3217
  3459. */
  3460. public function testFormShouldOverloadToRenderDecorators()
  3461. {
  3462. $this->setupElements();
  3463. $this->form->setView($this->getView());
  3464. $html = $this->form->renderFormElements();
  3465. foreach ($this->form->getElements() as $element) {
  3466. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3467. }
  3468. $this->assertNotContains('<dl', $html);
  3469. $this->assertNotContains('<form', $html);
  3470. $html = $this->form->renderForm('this is the content');
  3471. $this->assertContains('<form', $html);
  3472. $this->assertContains('</form>', $html);
  3473. $this->assertContains('this is the content', $html);
  3474. }
  3475. /**
  3476. * @group ZF-3217
  3477. * @expectedException Zend_Form_Exception
  3478. */
  3479. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3480. {
  3481. $html = $this->form->bogusMethodCall();
  3482. }
  3483. /**
  3484. * @group ZF-2950
  3485. */
  3486. public function testDtDdElementsWithLabelGetUniqueId()
  3487. {
  3488. $form = new Zend_Form();
  3489. $form->setView($this->getView());
  3490. $fooElement = new Zend_Form_Element_Text('foo');
  3491. $fooElement->setLabel('Foo');
  3492. $form->addElement($fooElement);
  3493. $html = $form->render();
  3494. $this->assertContains('<dt id="foo-label">', $html);
  3495. $this->assertContains('<dd id="foo-element">', $html);
  3496. }
  3497. /**
  3498. * @group ZF-2950
  3499. */
  3500. public function testDtDdElementsWithoutLabelGetUniqueId()
  3501. {
  3502. $form = new Zend_Form();
  3503. $form->setView($this->getView())
  3504. ->addElement(new Zend_Form_Element_Text('foo'));
  3505. $html = $form->render();
  3506. $this->assertContains('<dt id="foo-label">&#160;</dt>', $html);
  3507. $this->assertContains('<dd id="foo-element">', $html);
  3508. }
  3509. /**
  3510. * @group ZF-2950
  3511. */
  3512. public function testSubFormGetsUniqueIdWithName()
  3513. {
  3514. $form = new Zend_Form();
  3515. $form->setView($this->getView())
  3516. ->setName('testform')
  3517. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3518. $html = $form->render();
  3519. $this->assertContains('<dt id="testform-label">&#160;</dt>', $html);
  3520. $this->assertContains('<dd id="testform-element">', $html);
  3521. }
  3522. /**
  3523. * @group ZF-5370
  3524. */
  3525. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3526. {
  3527. $file = new Zend_Form_Element_File('txt');
  3528. $this->form->addElement($file);
  3529. $html = $this->form->render($this->getView());
  3530. $this->assertFalse(empty($html));
  3531. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3532. }
  3533. /**
  3534. * @group ZF-5370
  3535. */
  3536. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3537. {
  3538. $subForm = new Zend_Form_SubForm();
  3539. $subForm->addElement('file', 'txt');
  3540. $this->form->addSubForm($subForm, 'page1')
  3541. ->setView(new Zend_View);
  3542. $html = $this->form->render();
  3543. $this->assertContains('id="txt"', $html);
  3544. $this->assertContains('name="txt"', $html);
  3545. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3546. }
  3547. /**
  3548. * @group ZF-5370
  3549. */
  3550. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3551. {
  3552. $this->form->addElement('file', 'txt')
  3553. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3554. ->setView(new Zend_View);
  3555. $html = $this->form->render();
  3556. $this->assertContains('id="txt"', $html);
  3557. $this->assertContains('name="txt"', $html);
  3558. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3559. }
  3560. /**
  3561. * @group ZF-6070
  3562. */
  3563. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3564. {
  3565. $this->form->setOptions(array(
  3566. 'elementDecorators' => array(
  3567. 'ViewHelper',
  3568. 'Label',
  3569. ),
  3570. 'elements' => array(
  3571. 'foo' => array(
  3572. 'type' => 'text',
  3573. 'options' => array(
  3574. 'decorators' => array(
  3575. 'Errors',
  3576. 'ViewHelper',
  3577. ),
  3578. ),
  3579. ),
  3580. ),
  3581. ));
  3582. $element = $this->form->getElement('foo');
  3583. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3584. $actual = array();
  3585. foreach ($element->getDecorators() as $decorator) {
  3586. $actual[] = get_class($decorator);
  3587. }
  3588. $this->assertSame($expected, $actual);
  3589. }
  3590. /**
  3591. * @group ZF-5150
  3592. */
  3593. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3594. {
  3595. $this->form->addError('Error');
  3596. $this->assertFalse($this->form->isValid(array()));
  3597. }
  3598. /**
  3599. * @group ZF-8494
  3600. */
  3601. public function testGetValidValues()
  3602. {
  3603. $data = array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement');
  3604. require_once "Zend/Validate/Int.php";
  3605. $validElement = new Zend_Form_Element("valid");
  3606. $validElement->addValidator(new Zend_Validate_Int());
  3607. $this->form->addElement($validElement);
  3608. $invalidElement = new Zend_Form_Element('invalid');
  3609. $invalidElement->addValidator(new Zend_Validate_Int());
  3610. $this->form->addElement($invalidElement);
  3611. $this->assertEquals(array('valid' => 1234), $this->form->getValidValues($data));
  3612. }
  3613. /**
  3614. * @group ZF-8494
  3615. */
  3616. public function testGetValidSubFormValues()
  3617. {
  3618. $data = array('sub' => array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement'));
  3619. require_once "Zend/Validate/Int.php";
  3620. $subForm = new Zend_Form_SubForm();
  3621. $validElement = new Zend_Form_Element("valid");
  3622. $validElement->addValidator(new Zend_Validate_Int());
  3623. $subForm->addElement($validElement);
  3624. $invalidElement = new Zend_Form_Element('invalid');
  3625. $invalidElement->addValidator(new Zend_Validate_Int());
  3626. $subForm->addElement($invalidElement);
  3627. $this->form->addSubForm($subForm, 'sub');
  3628. $this->assertEquals(array('sub' => array('valid' => 1234)), $this->form->getValidValues($data));
  3629. }
  3630. /**
  3631. * @group ZF-9275
  3632. */
  3633. public function testElementTranslatorNotOverriddenbyGlobalTranslatorDuringValidation()
  3634. {
  3635. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  3636. Zend_Registry::set('Zend_Translate', $translator);
  3637. $this->form->addElement('text', 'foo');
  3638. $this->form->isValid(array());
  3639. $received = $this->form->foo->hasTranslator();
  3640. $this->assertSame(false, $received);
  3641. }
  3642. /**
  3643. * @group ZF-9275
  3644. */
  3645. public function testZendValidateDefaultTranslatorOverridesZendTranslateDefaultTranslator()
  3646. {
  3647. $translate = new Zend_Translate('array', array('isEmpty' => 'translate'));
  3648. Zend_Registry::set('Zend_Translate', $translate);
  3649. $translateValidate = new Zend_Translate('array', array('isEmpty' => 'validate'));
  3650. Zend_Validate_Abstract::setDefaultTranslator($translateValidate);
  3651. $this->form->addElement('text', 'foo', array('required'=>1));
  3652. $this->form->isValid(array());
  3653. $this->assertSame(array('isEmpty' => 'validate'), $this->form->foo->getMessages());
  3654. }
  3655. /**
  3656. * @group ZF-9494
  3657. */
  3658. public function testElementTranslatorNotOveriddenbyFormTranslator()
  3659. {
  3660. $translations = array(
  3661. 'isEmpty' => 'Element message',
  3662. );
  3663. $translate = new Zend_Translate('array', $translations);
  3664. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$translate));
  3665. $this->assertFalse($this->form->isValid(array('foo'=>'')));
  3666. $messages = $this->form->getMessages();
  3667. $this->assertEquals(1, count($messages));
  3668. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3669. $this->assertFalse($this->form->isValidPartial(array('foo'=>'')));
  3670. $messages = $this->form->getMessages();
  3671. $this->assertEquals(1, count($messages));
  3672. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3673. }
  3674. /**
  3675. * @group ZF-9364
  3676. */
  3677. public function testElementTranslatorPreferredOverFormTranslator()
  3678. {
  3679. $formTanslations = array(
  3680. 'isEmpty' => 'Form message',
  3681. );
  3682. $elementTanslations = array(
  3683. 'isEmpty' => 'Element message',
  3684. );
  3685. $formTranslate = new Zend_Translate('array', $formTanslations);
  3686. $elementTranslate = new Zend_Translate('array', $elementTanslations);
  3687. $this->form->setTranslator($formTranslate);
  3688. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3689. $this->form->addElement('text', 'bar', array('required'=>true));
  3690. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3691. $messages = $this->form->getMessages();
  3692. $this->assertEquals(2, count($messages));
  3693. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3694. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3695. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3696. $messages = $this->form->getMessages();
  3697. $this->assertEquals(2, count($messages));
  3698. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3699. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3700. }
  3701. /**
  3702. * @group ZF-9364
  3703. */
  3704. public function testElementTranslatorPreferredOverDefaultTranslator()
  3705. {
  3706. $defaultTranslations = array(
  3707. 'isEmpty' => 'Default message',
  3708. );
  3709. $formTranslations = array(
  3710. 'isEmpty' => 'Form message',
  3711. );
  3712. $elementTranslations = array(
  3713. 'isEmpty' => 'Element message',
  3714. );
  3715. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3716. $formTranslate = new Zend_Translate('array', $formTranslations);
  3717. $elementTranslate = new Zend_Translate('array', $elementTranslations);
  3718. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3719. $this->form->setTranslator($formTranslate);
  3720. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3721. $this->form->addElement('text', 'bar', array('required'=>true));
  3722. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3723. $messages = $this->form->getMessages();
  3724. $this->assertEquals(2, count($messages));
  3725. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3726. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3727. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3728. $messages = $this->form->getMessages();
  3729. $this->assertEquals(2, count($messages));
  3730. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3731. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3732. }
  3733. /**
  3734. * @group ZF-9540
  3735. */
  3736. public function testSubFormTranslatorPreferredOverDefaultTranslator()
  3737. {
  3738. $defaultTranslations = array('isEmpty' => 'Default message');
  3739. $subformTranslations = array('isEmpty' => 'SubForm message');
  3740. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3741. $subformTranslate = new Zend_Translate('array', $subformTranslations);
  3742. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3743. $this->form->addSubForm(new Zend_Form_SubForm(), 'subform');
  3744. $this->form->subform->setTranslator($subformTranslate);
  3745. $this->form->subform->addElement('text', 'foo', array('required'=>true));
  3746. $this->assertFalse($this->form->isValid(array('subform' => array('foo'=>''))));
  3747. $messages = $this->form->getMessages();
  3748. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3749. $this->assertFalse($this->form->isValidPartial(array('subform' => array('foo'=>''))));
  3750. $messages = $this->form->getMessages();
  3751. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3752. }
  3753. /**
  3754. * @group ZF-12375
  3755. */
  3756. public function testElementTranslatorNotOveriddenbyFormTranslatorDuringRendering()
  3757. {
  3758. // Set translator for form
  3759. $this->form->setTranslator(
  3760. new Zend_Translate(
  3761. 'array',
  3762. array(
  3763. 'labelText' => 'Foo',
  3764. )
  3765. )
  3766. );
  3767. // Add element with his own translator
  3768. $this->form->addElement(
  3769. 'text',
  3770. 'foo',
  3771. array(
  3772. 'label' => 'labelText',
  3773. 'translator' => new Zend_Translate(
  3774. 'array',
  3775. array(
  3776. 'labelText' => 'Bar',
  3777. )
  3778. ),
  3779. 'decorators' => array(
  3780. 'Label',
  3781. ),
  3782. )
  3783. );
  3784. $this->form->setDecorators(array('FormElements'));
  3785. // Test
  3786. $this->assertSame(
  3787. PHP_EOL . '<label for="foo" class="optional">Bar</label>' . PHP_EOL,
  3788. $this->form->render(new Zend_View())
  3789. );
  3790. }
  3791. public function testNameAttributeOutputForXhtml()
  3792. {
  3793. // Create form
  3794. $form = new Zend_Form();
  3795. $form->setName('foo');
  3796. $form->setMethod(Zend_Form::METHOD_GET);
  3797. $form->removeDecorator('HtmlTag');
  3798. // Set doctype
  3799. $this->getView()->getHelper('doctype')->doctype(
  3800. Zend_View_Helper_Doctype::XHTML1_STRICT
  3801. );
  3802. $expected = '<form id="foo" method="get" action="">'
  3803. . PHP_EOL
  3804. . '</form>';
  3805. $this->assertSame(
  3806. $expected,
  3807. $form->render($this->getView())
  3808. );
  3809. }
  3810. /**
  3811. * @group ZF-5613
  3812. */
  3813. public function testAddSubFormsPerConfig()
  3814. {
  3815. // Create form
  3816. $form = new Zend_Form(
  3817. array(
  3818. 'subForms' => array(
  3819. array(
  3820. 'form' => array(
  3821. 'elements' => array(
  3822. 'foo' => array(
  3823. 'text',
  3824. array(
  3825. 'label' => 'Foo',
  3826. 'decorators' => array(
  3827. 'ViewHelper',
  3828. 'Label',
  3829. ),
  3830. ),
  3831. ),
  3832. ),
  3833. 'id' => 'subform1',
  3834. 'decorators' => array(
  3835. 'FormElements',
  3836. ),
  3837. ),
  3838. 'name' => 'subform1',
  3839. 'order' => 2,
  3840. ),
  3841. array(
  3842. 'form' => array(
  3843. 'elements' => array(
  3844. 'bar' => array(
  3845. 'text',
  3846. array(
  3847. 'label' => 'Bar',
  3848. 'decorators' => array(
  3849. 'ViewHelper',
  3850. 'Label',
  3851. ),
  3852. ),
  3853. ),
  3854. ),
  3855. 'id' => 'subform2',
  3856. 'decorators' => array(
  3857. 'FormElements',
  3858. ),
  3859. ),
  3860. 'name' => 'subform2',
  3861. 'order' => 1,
  3862. ),
  3863. ),
  3864. )
  3865. );
  3866. $form->removeDecorator('HtmlTag');
  3867. // Tests
  3868. $subForms = $form->getSubForms();
  3869. $subForm1 = current($subForms);
  3870. $subForm2 = next($subForms);
  3871. $this->assertSame(
  3872. array(
  3873. 'subform1',
  3874. 'subform2',
  3875. ),
  3876. array(
  3877. $subForm1->getName(),
  3878. $subForm2->getName(),
  3879. )
  3880. );
  3881. $expected = '<form enctype="application/x-www-form-urlencoded" action="" method="post">'
  3882. . PHP_EOL
  3883. . PHP_EOL
  3884. . '<label for="subform2-bar" class="optional">Bar</label>'
  3885. . PHP_EOL
  3886. . PHP_EOL
  3887. . '<input type="text" name="subform2[bar]" id="subform2-bar" value="" />'
  3888. . PHP_EOL
  3889. . PHP_EOL
  3890. . '<label for="subform1-foo" class="optional">Foo</label>'
  3891. . PHP_EOL
  3892. . PHP_EOL
  3893. . '<input type="text" name="subform1[foo]" id="subform1-foo" value="" />'
  3894. . '</form>';
  3895. $this->assertSame($expected, $form->render($this->getView()));
  3896. }
  3897. /**
  3898. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3899. *
  3900. * @link http://framework.zend.com/issues/browse/ZF-2794
  3901. * @return void
  3902. */
  3903. protected function _checkZf2794()
  3904. {
  3905. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3906. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3907. }
  3908. }
  3909. /**
  3910. * Prove the fluent interface on Zend_Form::loadDefaultDecorators
  3911. *
  3912. * @link http://framework.zend.com/issues/browse/ZF-9913
  3913. * @return void
  3914. */
  3915. public function testFluentInterfaceOnLoadDefaultDecorators()
  3916. {
  3917. $this->assertSame($this->form, $this->form->loadDefaultDecorators());
  3918. }
  3919. /**
  3920. * @group ZF-7552
  3921. */
  3922. public function testAddDecoratorsKeepsNonNumericKeyNames()
  3923. {
  3924. $this->form->addDecorators(array(array(array('td' => 'HtmlTag'),
  3925. array('tag' => 'td')),
  3926. array(array('tr' => 'HtmlTag'),
  3927. array('tag' => 'tr')),
  3928. array('HtmlTag', array('tag' => 'baz'))));
  3929. $t1 = $this->form->getDecorators();
  3930. $this->form->setDecorators($t1);
  3931. $t2 = $this->form->getDecorators();
  3932. $this->assertEquals($t1, $t2);
  3933. }
  3934. /**
  3935. * @group ZF-10411
  3936. */
  3937. public function testAddingElementToDisplayGroupManuallyShouldPreventRenderingByForm()
  3938. {
  3939. $form = new Zend_Form_FormTest_AddToDisplayGroup();
  3940. $html = $form->render($this->getView());
  3941. $this->assertEquals(1, substr_count($html, 'Customer Type'), $html);
  3942. }
  3943. /**
  3944. * @group ZF-10491
  3945. * @group ZF-10734
  3946. * @group ZF-10731
  3947. */
  3948. public function testAddElementToDisplayGroupByElementInstance()
  3949. {
  3950. $element = new Zend_Form_Element_Text('foo');
  3951. $elementTwo = new Zend_Form_Element_Text('baz-----');
  3952. $this->form->addElements(array($element, $elementTwo));
  3953. $this->form->addDisplayGroup(array($element, $elementTwo), 'bar');
  3954. $displayGroup = $this->form->getDisplayGroup('bar');
  3955. $this->assertNotNull($displayGroup->getElement('foo'));
  3956. $this->assertNotNull($displayGroup->getElement('baz'));
  3957. // clear display groups and elements
  3958. $this->form->clearDisplayGroups()
  3959. ->clearElements();
  3960. $this->form->addDisplayGroup(array($element, $elementTwo), 'bar');
  3961. $displayGroup = $this->form->getDisplayGroup('bar');
  3962. $this->assertNotNull($displayGroup->getElement('foo'));
  3963. $this->assertNotNull($displayGroup->getElement('baz'));
  3964. }
  3965. /**
  3966. * @group ZF-10149
  3967. */
  3968. public function testIfViewIsSetInTime()
  3969. {
  3970. try {
  3971. $form = new Zend_Form(array('view' => new MyTestView()));
  3972. $this->assertTrue($form->getView() instanceof MyTestView);
  3973. $form = new Zend_Form(array('view' => new StdClass()));
  3974. $this->assertNull($form->getView());
  3975. $result = $form->render();
  3976. }
  3977. catch (Zend_Form_Exception $e) {
  3978. $this->fail('Setting a view object using the options array should not throw an exception');
  3979. }
  3980. $this->assertNotEquals($result,'');
  3981. }
  3982. /**
  3983. * @group ZF-11088
  3984. */
  3985. public function testAddErrorOnElementMakesFormInvalidAndReturnsCustomError()
  3986. {
  3987. $element = new Zend_Form_Element_Text('foo');
  3988. $errorString = 'This element made a booboo';
  3989. $element->addError($errorString);
  3990. $errorMessages = $element->getErrorMessages();
  3991. $this->assertSame(1, count($errorMessages));
  3992. $this->assertSame($errorString, $errorMessages[0]);
  3993. $element2 = new Zend_Form_Element_Text('bar');
  3994. $this->form->addElement($element2);
  3995. $this->form->getElement('bar')->addError($errorString);
  3996. $errorMessages2 = $this->form->getElement('bar')->getErrorMessages();
  3997. $this->assertSame(1, count($errorMessages2));
  3998. $this->assertSame($errorString, $errorMessages2[0]);
  3999. }
  4000. /**
  4001. * @group ZF-10865
  4002. * @expectedException Zend_Form_Exception
  4003. */
  4004. public function testExceptionThrownWhenAddElementsIsGivenNullValue()
  4005. {
  4006. $form = new Zend_Form();
  4007. $form->addElement(NULL);
  4008. }
  4009. /**
  4010. * @group ZF-11729
  4011. */
  4012. public function testDashSeparatedElementsInDisplayGroupsShouldNotRenderOutsideDisplayGroup()
  4013. {
  4014. $form = new Zend_Form();
  4015. $form->addElement('text', 'random-element-name', array(
  4016. 'label' => 'This is weird',
  4017. 'value' => 'think its a bug',
  4018. ));
  4019. $form->addDisplayGroup(array('random-element-name'), 'foobar', array(
  4020. 'legend' => 'foobar',
  4021. ));
  4022. $html = $form->render($this->getView());
  4023. $count = substr_count($html, 'randomelementname-element');
  4024. $this->assertEquals(1, $count, $html);
  4025. }
  4026. /**
  4027. * @group ZF-11831
  4028. */
  4029. public function testElementsOfSubFormReceiveCorrectDefaultTranslator()
  4030. {
  4031. // Global default translator
  4032. $trDefault = new Zend_Translate(array(
  4033. 'adapter' => 'array',
  4034. 'content' => array(
  4035. Zend_Validate_NotEmpty::IS_EMPTY => 'Default'
  4036. ),
  4037. 'locale' => 'en'
  4038. ));
  4039. Zend_Registry::set('Zend_Translate', $trDefault);
  4040. // Translator to use for elements
  4041. $trElement = new Zend_Translate(array(
  4042. 'adapter' => 'array',
  4043. 'content' => array(
  4044. Zend_Validate_NotEmpty::IS_EMPTY =>'Element'
  4045. ),
  4046. 'locale' => 'en'
  4047. ));
  4048. Zend_Validate_Abstract::setDefaultTranslator($trElement);
  4049. // Change the form's translator
  4050. $form = new Zend_Form();
  4051. $form->addElement(new Zend_Form_Element_Text('foo', array(
  4052. 'required' => true,
  4053. 'validators' => array('NotEmpty')
  4054. )));
  4055. // Create a subform with it's own validator
  4056. $sf1 = new Zend_Form_SubForm();
  4057. $sf1->addElement(new Zend_Form_Element_Text('foosub', array(
  4058. 'required' => true,
  4059. 'validators' => array('NotEmpty')
  4060. )));
  4061. $form->addSubForm($sf1, 'Test1');
  4062. $form->isValid(array());
  4063. $messages = $form->getMessages();
  4064. $this->assertEquals(
  4065. 'Element',
  4066. @$messages['foo'][Zend_Validate_NotEmpty::IS_EMPTY],
  4067. 'Form element received wrong validator'
  4068. );
  4069. $this->assertEquals(
  4070. 'Element',
  4071. @$messages['Test1']['foosub'][Zend_Validate_NotEmpty::IS_EMPTY],
  4072. 'SubForm element received wrong validator'
  4073. );
  4074. }
  4075. /**
  4076. * @group ZF-12173
  4077. */
  4078. public function testAddingPrefixPathsWithBackslashWillLoadNamespacedPlugins()
  4079. {
  4080. if (version_compare(PHP_VERSION, '5.3.0', '<')) {
  4081. $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
  4082. return;
  4083. }
  4084. $form = new Zend_Form();
  4085. $form->addPrefixPath('Zf\Foo', 'Zf/Foo');
  4086. foreach (array('element', 'decorator') as $type) {
  4087. $loader = $form->getPluginLoader($type);
  4088. $paths = $loader->getPaths('Zf\Foo\\' . ucfirst($type));
  4089. $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
  4090. $this->assertFalse(empty($paths));
  4091. $this->assertContains('Foo', $paths[0]);
  4092. }
  4093. }
  4094. /**
  4095. * @group ZF-8942
  4096. */
  4097. public function testCreateElementIncludesElementDecorators()
  4098. {
  4099. // Init form
  4100. $form = new Zend_Form();
  4101. $form->setElementDecorators(
  4102. array(
  4103. new Zend_Form_Decorator_ViewHelper,
  4104. new Zend_Form_Decorator_Label,
  4105. )
  4106. );
  4107. $element = $form->createElement('text', 'foo');
  4108. // Test
  4109. $expected = array(
  4110. 'Zend_Form_Decorator_ViewHelper',
  4111. 'Zend_Form_Decorator_Label',
  4112. );
  4113. $actual = array();
  4114. foreach ($element->getDecorators() as $decorator) {
  4115. $actual[] = get_class($decorator);
  4116. }
  4117. $this->assertEquals($expected, $actual);
  4118. }
  4119. }
  4120. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  4121. {
  4122. }
  4123. class Zend_Form_FormTest_FormExtension extends Zend_Form
  4124. {
  4125. public function init()
  4126. {
  4127. $this->setDisableLoadDefaultDecorators(true);
  4128. }
  4129. }
  4130. class Zend_Form_FormTest_WithDisplayGroup extends Zend_Form
  4131. {
  4132. public function init()
  4133. {
  4134. $this->addElement('text', 'el1', array(
  4135. 'label' => 'Title',
  4136. 'required' => true,
  4137. ));
  4138. $this->addDisplayGroup(array('el1'), 'group1', array(
  4139. 'legend' => 'legend 1',
  4140. ));
  4141. }
  4142. }
  4143. class Zend_Form_FormTest_AddToDisplayGroup extends Zend_Form_FormTest_WithDisplayGroup
  4144. {
  4145. public function init()
  4146. {
  4147. parent::init();
  4148. $element = new Zend_Form_Element_Text('el2', array(
  4149. 'label' => 'Customer Type',
  4150. ));
  4151. $this->addElement($element);
  4152. $this->group1->addElement($element);
  4153. }
  4154. }
  4155. class MyTestView extends Zend_View
  4156. {
  4157. }
  4158. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  4159. Zend_Form_FormTest::main();
  4160. }