FormTest.php 143 KB

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