FormTest.php 158 KB

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