FormTest.php 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996
  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. // Display groups
  1332. public function testCanAddAndRetrieveSingleDisplayGroups()
  1333. {
  1334. $this->testCanAddAndRetrieveMultipleElements();
  1335. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  1336. $group = $this->form->getDisplayGroup('barbat');
  1337. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1338. $elements = $group->getElements();
  1339. $expected = array('bar' => $this->form->bar, 'bat' => $this->form->bat);
  1340. $this->assertEquals($expected, $elements);
  1341. }
  1342. public function testDisplayGroupsMustContainAtLeastOneElement()
  1343. {
  1344. try {
  1345. $this->form->addDisplayGroup(array(), 'foo');
  1346. $this->fail('Empty display group should raise exception');
  1347. } catch (Zend_Form_Exception $e) {
  1348. $this->assertContains('No valid elements', $e->getMessage());
  1349. }
  1350. }
  1351. public function testCanAddAndRetrieveMultipleDisplayGroups()
  1352. {
  1353. $this->testCanAddAndRetrieveMultipleElements();
  1354. $this->form->addDisplayGroups(array(
  1355. array(array('bar', 'bat'), 'barbat'),
  1356. 'foobaz' => array('baz', 'foo')
  1357. ));
  1358. $groups = $this->form->getDisplayGroups();
  1359. $expected = array(
  1360. 'barbat' => array('bar' => $this->form->bar, 'bat' => $this->form->bat),
  1361. 'foobaz' => array('baz' => $this->form->baz, 'foo' => $this->form->foo),
  1362. );
  1363. foreach ($groups as $group) {
  1364. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1365. }
  1366. $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
  1367. $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
  1368. }
  1369. public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
  1370. {
  1371. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1372. $this->form->setDisplayGroups(array('foobar' => array('bar', 'foo')));
  1373. $groups = $this->form->getDisplayGroups();
  1374. $expected = array('bar' => $this->form->bar, 'foo' => $this->form->foo);
  1375. $this->assertEquals(1, count($groups));
  1376. $this->assertTrue(isset($groups['foobar']));
  1377. $this->assertEquals($expected, $groups['foobar']->getElements());
  1378. }
  1379. public function testCanRemoveSingleDisplayGroup()
  1380. {
  1381. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1382. $this->assertTrue($this->form->removeDisplayGroup('barbat'));
  1383. $this->assertNull($this->form->getDisplayGroup('barbat'));
  1384. }
  1385. public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
  1386. {
  1387. $this->assertFalse($this->form->removeDisplayGroup('bogus'));
  1388. }
  1389. public function testCanClearAllDisplayGroups()
  1390. {
  1391. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1392. $this->form->clearDisplayGroups();
  1393. $groups = $this->form->getDisplayGroups();
  1394. $this->assertTrue(is_array($groups));
  1395. $this->assertTrue(empty($groups));
  1396. }
  1397. public function testOverloadingDisplayGroups()
  1398. {
  1399. $this->testCanAddAndRetrieveMultipleElements();
  1400. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1401. $this->assertTrue(isset($this->form->foobar));
  1402. $group = $this->form->foobar;
  1403. $expected = array('foo' => $this->form->foo, 'bar' => $this->form->bar);
  1404. $this->assertEquals($expected, $group->getElements());
  1405. unset($this->form->foobar);
  1406. $this->assertFalse(isset($this->form->foobar));
  1407. $this->form->barbaz = array('bar', 'baz');
  1408. $this->assertTrue(isset($this->form->barbaz));
  1409. $group = $this->form->barbaz;
  1410. $expected = array('bar' => $this->form->bar, 'baz' => $this->form->baz);
  1411. $this->assertSame($expected, $group->getElements());
  1412. }
  1413. public function testDefaultDisplayGroupClassExists()
  1414. {
  1415. $this->assertEquals('Zend_Form_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1416. }
  1417. public function testCanSetDefaultDisplayGroupClass()
  1418. {
  1419. $this->testDefaultDisplayGroupClassExists();
  1420. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1421. $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1422. }
  1423. public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
  1424. {
  1425. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1426. $this->setupElements();
  1427. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1428. $displayGroup = $this->form->getDisplayGroup('foobar');
  1429. $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup);
  1430. }
  1431. public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
  1432. {
  1433. $this->setupElements();
  1434. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
  1435. $this->assertTrue($this->form->foobar instanceof Zend_Form_FormTest_DisplayGroup);
  1436. }
  1437. /**
  1438. * @group ZF-3254
  1439. */
  1440. public function testAddingDisplayGroupShouldPassOptions()
  1441. {
  1442. $this->testCanAddAndRetrieveMultipleElements();
  1443. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
  1444. $group = $this->form->getDisplayGroup('barbat');
  1445. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1446. $decorators = $group->getDecorators();
  1447. $this->assertTrue(is_array($decorators));
  1448. $this->assertTrue(empty($decorators));
  1449. }
  1450. // Processing
  1451. public function testPopulateProxiesToSetDefaults()
  1452. {
  1453. $this->testCanAddAndRetrieveMultipleElements();
  1454. $values = array(
  1455. 'foo' => 'foovalue',
  1456. 'bar' => 'barvalue',
  1457. 'baz' => 'bazvalue',
  1458. 'bat' => 'batvalue'
  1459. );
  1460. $this->form->populate($values);
  1461. $test = $this->form->getValues();
  1462. $elements = $this->form->getElements();
  1463. foreach (array_keys($values) as $name) {
  1464. $this->assertEquals($values[$name], $test[$name]);
  1465. }
  1466. }
  1467. public function setupElements()
  1468. {
  1469. $foo = new Zend_Form_Element_Text('foo');
  1470. $foo->addValidator('NotEmpty')
  1471. ->addValidator('Alpha');
  1472. $bar = new Zend_Form_Element_Text('bar');
  1473. $bar->addValidator('NotEmpty')
  1474. ->addValidator('Digits');
  1475. $baz = new Zend_Form_Element_Text('baz');
  1476. $baz->addValidator('NotEmpty')
  1477. ->addValidator('Alnum');
  1478. $this->form->addElements(array($foo, $bar, $baz));
  1479. $this->elementValues = array(
  1480. 'foo' => 'fooBarBAZ',
  1481. 'bar' => '123456789',
  1482. 'baz' => 'foo123BAR',
  1483. );
  1484. }
  1485. public function testIsValidShouldThrowExceptionWithNonArrayArgument()
  1486. {
  1487. try {
  1488. $this->form->isValid(true);
  1489. $this->fail('isValid() should raise exception with non-array argument');
  1490. } catch (Zend_Form_Exception $e) {
  1491. $this->assertContains('expects an array', $e->getMessage());
  1492. }
  1493. }
  1494. public function testCanValidateFullFormContainingOnlyElements()
  1495. {
  1496. $this->_checkZf2794();
  1497. $this->setupElements();
  1498. $this->assertTrue($this->form->isValid($this->elementValues));
  1499. $values = array(
  1500. 'foo' => '12345',
  1501. 'bar' => 'abc',
  1502. 'baz' => 'abc-123'
  1503. );
  1504. $this->assertFalse($this->form->isValid($values));
  1505. $validator = $this->form->foo->getValidator('alpha');
  1506. $this->assertEquals('12345', $validator->value);
  1507. $validator = $this->form->bar->getValidator('digits');
  1508. $this->assertEquals('abc', $validator->value);
  1509. $validator = $this->form->baz->getValidator('alnum');
  1510. $this->assertEquals('abc-123', $validator->value);
  1511. }
  1512. public function testValidationTakesElementRequiredFlagsIntoAccount()
  1513. {
  1514. $this->_checkZf2794();
  1515. $this->setupElements();
  1516. $this->assertTrue($this->form->isValid(array()));
  1517. $this->form->getElement('foo')->setRequired(true);
  1518. $this->assertTrue($this->form->isValid(array(
  1519. 'foo' => 'abc',
  1520. 'baz' => 'abc123'
  1521. )));
  1522. $this->assertFalse($this->form->isValid(array(
  1523. 'baz' => 'abc123'
  1524. )));
  1525. }
  1526. public function testCanValidatePartialFormContainingOnlyElements()
  1527. {
  1528. $this->_checkZf2794();
  1529. $this->setupElements();
  1530. $this->form->getElement('foo')->setRequired(true);
  1531. $this->form->getElement('bar')->setRequired(true);
  1532. $this->form->getElement('baz')->setRequired(true);
  1533. $this->assertTrue($this->form->isValidPartial(array(
  1534. 'foo' => 'abc',
  1535. 'baz' => 'abc123'
  1536. )));
  1537. $this->assertFalse($this->form->isValidPartial(array(
  1538. 'foo' => '123',
  1539. 'baz' => 'abc-123'
  1540. )));
  1541. }
  1542. public function setupSubForm()
  1543. {
  1544. $subForm = new Zend_Form_SubForm();
  1545. $foo = new Zend_Form_Element_Text('subfoo');
  1546. $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
  1547. $bar = new Zend_Form_Element_Text('subbar');
  1548. $bar->addValidators(array('NotEmpty', 'Digits'));
  1549. $baz = new Zend_Form_Element_Text('subbaz');
  1550. $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
  1551. $subForm->addElements(array($foo, $bar, $baz));
  1552. $this->form->addSubForm($subForm, 'sub');
  1553. }
  1554. public function testFullDataArrayUsedToValidateSubFormByDefault()
  1555. {
  1556. $this->_checkZf2794();
  1557. $this->setupElements();
  1558. $this->setupSubForm();
  1559. $data = array(
  1560. 'foo' => 'abcdef',
  1561. 'bar' => '123456',
  1562. 'baz' => '123abc',
  1563. 'subfoo' => 'abcdef',
  1564. 'subbar' => '123456',
  1565. 'subbaz' => '123abc',
  1566. );
  1567. $this->assertTrue($this->form->isValid($data));
  1568. $data = array(
  1569. 'foo' => 'abcdef',
  1570. 'bar' => '123456',
  1571. 'baz' => '123abc',
  1572. 'subfoo' => '123',
  1573. 'subbar' => 'abc',
  1574. 'subbaz' => '123-abc',
  1575. );
  1576. $this->assertFalse($this->form->isValid($data));
  1577. $data = array(
  1578. 'foo' => 'abcdef',
  1579. 'bar' => '123456',
  1580. 'baz' => '123abc',
  1581. 'subfoo' => 'abc',
  1582. 'subbaz' => '123abc',
  1583. );
  1584. $this->assertTrue($this->form->isValid($data));
  1585. $data = array(
  1586. 'foo' => 'abcdef',
  1587. 'bar' => '123456',
  1588. 'baz' => '123abc',
  1589. 'subbar' => '123',
  1590. 'subbaz' => '123abc',
  1591. );
  1592. $this->assertFalse($this->form->isValid($data));
  1593. }
  1594. public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
  1595. {
  1596. $this->_checkZf2794();
  1597. $this->setupElements();
  1598. $this->setupSubForm();
  1599. $data = array(
  1600. 'foo' => 'abcdef',
  1601. 'bar' => '123456',
  1602. 'baz' => '123abc',
  1603. 'sub' => array(
  1604. 'subfoo' => 'abcdef',
  1605. 'subbar' => '123456',
  1606. 'subbaz' => '123abc',
  1607. ),
  1608. );
  1609. $this->assertTrue($this->form->isValid($data));
  1610. $data = array(
  1611. 'foo' => 'abcdef',
  1612. 'bar' => '123456',
  1613. 'baz' => '123abc',
  1614. 'sub' => array(
  1615. 'subfoo' => '123',
  1616. 'subbar' => 'abc',
  1617. 'subbaz' => '123-abc',
  1618. )
  1619. );
  1620. $this->assertFalse($this->form->isValid($data));
  1621. $data = array(
  1622. 'foo' => 'abcdef',
  1623. 'bar' => '123456',
  1624. 'baz' => '123abc',
  1625. 'sub' => array(
  1626. 'subfoo' => 'abc',
  1627. 'subbaz' => '123abc',
  1628. )
  1629. );
  1630. $this->assertTrue($this->form->isValid($data));
  1631. $data = array(
  1632. 'foo' => 'abcdef',
  1633. 'bar' => '123456',
  1634. 'baz' => '123abc',
  1635. 'sub' => array(
  1636. 'subbar' => '123',
  1637. 'subbaz' => '123abc',
  1638. )
  1639. );
  1640. $this->assertFalse($this->form->isValid($data));
  1641. }
  1642. public function testCanValidateNestedFormsWithElementsBelongingToArrays()
  1643. {
  1644. $form = new Zend_Form();
  1645. $form->setElementsBelongTo('foobar');
  1646. $form->addElement('text', 'firstName')
  1647. ->getElement('firstName')
  1648. ->setRequired(true);
  1649. $form->addElement('text', 'lastName')
  1650. ->getElement('lastName')
  1651. ->setRequired(true);
  1652. $subForm = new Zend_Form_SubForm();
  1653. $subForm->setElementsBelongTo('baz');
  1654. $subForm->addElement('text', 'email')
  1655. ->getElement('email')->setRequired(true);
  1656. $subSubForm = new Zend_Form_SubForm();
  1657. $subSubForm->setElementsBelongTo('bat');
  1658. $subSubForm->addElement('checkbox', 'home')
  1659. ->getElement('home')->setRequired(true);
  1660. $subForm->addSubForm($subSubForm, 'subSub');
  1661. $form->addSubForm($subForm, 'sub')
  1662. ->addElement('submit', 'save', array('value' => 'submit'));
  1663. $data = array('foobar' => array(
  1664. 'firstName' => 'Mabel',
  1665. 'lastName' => 'Cow',
  1666. 'baz' => array(
  1667. 'email' => 'mabel@cow.org',
  1668. 'bat' => array(
  1669. 'home' => 1,
  1670. )
  1671. )
  1672. ));
  1673. $this->assertTrue($form->isValid($data));
  1674. $this->assertEquals('Mabel', $form->firstName->getValue());
  1675. $this->assertEquals('Cow', $form->lastName->getValue());
  1676. $this->assertEquals('mabel@cow.org', $form->sub->email->getValue());
  1677. $this->assertEquals(1, $form->sub->subSub->home->getValue());
  1678. }
  1679. public function testCanValidatePartialFormContainingSubForms()
  1680. {
  1681. $this->_checkZf2794();
  1682. $this->setupElements();
  1683. $this->setupSubForm();
  1684. $data = array(
  1685. 'subfoo' => 'abcdef',
  1686. 'subbar' => '123456',
  1687. );
  1688. $this->assertTrue($this->form->isValidPartial($data));
  1689. $data = array(
  1690. 'foo' => 'abcdef',
  1691. 'baz' => '123abc',
  1692. 'sub' => array(
  1693. 'subbar' => '123',
  1694. )
  1695. );
  1696. $this->assertTrue($this->form->isValidPartial($data));
  1697. $data = array(
  1698. 'foo' => 'abcdef',
  1699. 'bar' => '123456',
  1700. 'baz' => '123abc',
  1701. 'sub' => array(
  1702. 'subfoo' => '123',
  1703. )
  1704. );
  1705. $this->assertFalse($this->form->isValidPartial($data));
  1706. }
  1707. public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
  1708. {
  1709. $this->_checkZf2794();
  1710. $form = new Zend_Form();
  1711. $form->setElementsBelongTo('foobar');
  1712. $form->addElement('text', 'firstName')
  1713. ->getElement('firstName')
  1714. ->setRequired(false);
  1715. $form->addElement('text', 'lastName')
  1716. ->getElement('lastName')
  1717. ->setRequired(true);
  1718. $subForm = new Zend_Form_SubForm();
  1719. $subForm->setElementsBelongTo('baz');
  1720. $subForm->addElement('text', 'email')
  1721. ->getElement('email')
  1722. ->setRequired(true)
  1723. ->addValidator('NotEmpty');
  1724. $subSubForm = new Zend_Form_SubForm();
  1725. $subSubForm->setElementsBelongTo('bat');
  1726. $subSubForm->addElement('checkbox', 'home')
  1727. ->getElement('home')
  1728. ->setRequired(true)
  1729. ->addValidator('InArray', false, array(array('1')));
  1730. $subForm->addSubForm($subSubForm, 'subSub');
  1731. $form->addSubForm($subForm, 'sub')
  1732. ->addElement('submit', 'save', array('value' => 'submit'));
  1733. $data = array('foobar' => array(
  1734. 'lastName' => 'Cow',
  1735. ));
  1736. $this->assertTrue($form->isValidPartial($data));
  1737. $this->assertEquals('Cow', $form->lastName->getValue());
  1738. $firstName = $form->firstName->getValue();
  1739. $email = $form->sub->email->getValue();
  1740. $home = $form->sub->subSub->home->getValue();
  1741. $this->assertTrue(empty($firstName));
  1742. $this->assertTrue(empty($email));
  1743. $this->assertTrue(empty($home));
  1744. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1745. $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
  1746. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1747. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1748. $messages = $form->getMessages();
  1749. $this->assertFalse(empty($messages));
  1750. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
  1751. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
  1752. }
  1753. public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
  1754. {
  1755. $this->_checkZf2794();
  1756. $form = new Zend_Form();
  1757. $form->setElementsBelongTo('foo[bar]');
  1758. $form->addElement('text', 'firstName')
  1759. ->getElement('firstName')
  1760. ->setRequired(false);
  1761. $form->addElement('text', 'lastName')
  1762. ->getElement('lastName')
  1763. ->setRequired(true);
  1764. $subForm = new Zend_Form_SubForm();
  1765. $subForm->setElementsBelongTo('baz');
  1766. $subForm->addElement('text', 'email')
  1767. ->getElement('email')
  1768. ->setRequired(true)
  1769. ->addValidator('NotEmpty');
  1770. $subSubForm = new Zend_Form_SubForm();
  1771. $subSubForm->setElementsBelongTo('bat[quux]');
  1772. $subSubForm->addElement('checkbox', 'home')
  1773. ->getElement('home')
  1774. ->setRequired(true)
  1775. ->addValidator('InArray', false, array(array('1')));
  1776. $subForm->addSubForm($subSubForm, 'subSub');
  1777. $form->addSubForm($subForm, 'sub')
  1778. ->addElement('submit', 'save', array('value' => 'submit'));
  1779. $data = array('foo' => array(
  1780. 'bar' => array(
  1781. 'lastName' => 'Cow',
  1782. ),
  1783. ));
  1784. $this->assertTrue($form->isValidPartial($data));
  1785. $this->assertEquals('Cow', $form->lastName->getValue());
  1786. $firstName = $form->firstName->getValue();
  1787. $email = $form->sub->email->getValue();
  1788. $home = $form->sub->subSub->home->getValue();
  1789. $this->assertTrue(empty($firstName));
  1790. $this->assertTrue(empty($email));
  1791. $this->assertTrue(empty($home));
  1792. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1793. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1794. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1795. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1796. }
  1797. public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
  1798. {
  1799. $this->_checkZf2794();
  1800. $form = new Zend_Form();
  1801. $form->setElementsBelongTo('foo[bar]');
  1802. $form->addElement('text', 'firstName')
  1803. ->getElement('firstName')
  1804. ->setRequired(false);
  1805. $form->addElement('text', 'lastName')
  1806. ->getElement('lastName')
  1807. ->setRequired(true);
  1808. $subForm = new Zend_Form_SubForm();
  1809. $subForm->setElementsBelongTo('baz');
  1810. $subForm->addElement('text', 'email')
  1811. ->getElement('email')
  1812. ->setRequired(true)
  1813. ->addValidator('NotEmpty');
  1814. $subSubForm = new Zend_Form_SubForm();
  1815. $subSubForm->setElementsBelongTo('bat[quux]');
  1816. $subSubForm->addElement('checkbox', 'home')
  1817. ->getElement('home')
  1818. ->setRequired(true)
  1819. ->addValidator('InArray', false, array(array('1')));
  1820. $subForm->addSubForm($subSubForm, 'subSub');
  1821. $form->addSubForm($subForm, 'sub')
  1822. ->addElement('submit', 'save', array('value' => 'submit'));
  1823. $data = array('foo' => array(
  1824. 'bar' => array(
  1825. 'lastName' => 'Cow',
  1826. ),
  1827. ));
  1828. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1829. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1830. $form->isValidPartial($data);
  1831. $messages = $form->getMessages();
  1832. $this->assertFalse(empty($messages));
  1833. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
  1834. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
  1835. }
  1836. public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
  1837. {
  1838. $this->setupElements();
  1839. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1840. $this->assertTrue($this->form->isValid($this->elementValues));
  1841. $this->assertFalse($this->form->isValid(array(
  1842. 'foo' => '123',
  1843. 'bar' => 'abc',
  1844. 'baz' => 'abc-123'
  1845. )));
  1846. }
  1847. public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
  1848. {
  1849. $this->setupElements();
  1850. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1851. $this->assertTrue($this->form->isValid(array(
  1852. 'foo' => 'abc',
  1853. 'baz' => 'abc123'
  1854. )));
  1855. $this->assertFalse($this->form->isValid(array(
  1856. 'foo' => '123',
  1857. 'baz' => 'abc-123'
  1858. )));
  1859. }
  1860. public function testProcessAjaxReturnsJsonTrueForValidForm()
  1861. {
  1862. $this->setupElements();
  1863. $return = $this->form->processAjax($this->elementValues);
  1864. $this->assertTrue(Zend_Json::decode($return));
  1865. }
  1866. public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
  1867. {
  1868. $this->setupElements();
  1869. $data = array('foo' => 'abcdef', 'baz' => 'abc123');
  1870. $return = $this->form->processAjax($data);
  1871. $this->assertTrue(Zend_Json::decode($return));
  1872. }
  1873. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
  1874. {
  1875. $this->setupElements();
  1876. $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
  1877. $return = Zend_Json::decode($this->form->processAjax($data));
  1878. $this->assertTrue(is_array($return));
  1879. $this->assertEquals(array_keys($data), array_keys($return));
  1880. }
  1881. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
  1882. {
  1883. $this->setupElements();
  1884. $data = array('baz' => 'abc-123');
  1885. $return = Zend_Json::decode($this->form->processAjax($data));
  1886. $this->assertTrue(is_array($return));
  1887. $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
  1888. }
  1889. public function testPersistDataStoresDataInSession()
  1890. {
  1891. $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
  1892. }
  1893. public function testCanCheckIfErrorsAreRegistered()
  1894. {
  1895. $this->assertFalse($this->form->isErrors());
  1896. $this->testCanValidateFullFormContainingOnlyElements();
  1897. $this->assertTrue($this->form->isErrors());
  1898. }
  1899. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  1900. {
  1901. $this->_checkZf2794();
  1902. $this->testCanValidateFullFormContainingOnlyElements();
  1903. $codes = $this->form->getErrors();
  1904. $keys = array('foo', 'bar', 'baz');
  1905. $this->assertEquals($keys, array_keys($codes));
  1906. }
  1907. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  1908. {
  1909. $this->_checkZf2794();
  1910. $this->testCanValidateFullFormContainingOnlyElements();
  1911. $codes = $this->form->getErrors();
  1912. $keys = array('foo', 'bar', 'baz');
  1913. $errors = $this->form->getErrors('foo');
  1914. $foo = $this->form->foo;
  1915. $this->assertEquals($foo->getErrors(), $errors);
  1916. }
  1917. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  1918. {
  1919. $this->_checkZf2794();
  1920. $this->testCanValidateFullFormContainingOnlyElements();
  1921. $codes = $this->form->getMessages();
  1922. $keys = array('foo', 'bar', 'baz');
  1923. $this->assertEquals($keys, array_keys($codes));
  1924. }
  1925. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  1926. {
  1927. $this->_checkZf2794();
  1928. $this->testCanValidateFullFormContainingOnlyElements();
  1929. $codes = $this->form->getMessages();
  1930. $keys = array('foo', 'bar', 'baz');
  1931. $messages = $this->form->getMessages('foo');
  1932. $foo = $this->form->foo;
  1933. $this->assertEquals($foo->getMessages(), $messages);
  1934. }
  1935. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  1936. {
  1937. $this->_checkZf2794();
  1938. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1939. $codes = $this->form->getErrors();
  1940. $this->assertTrue(array_key_exists('sub', $codes));
  1941. $this->assertTrue(is_array($codes['sub']));
  1942. $keys = array('subfoo', 'subbar', 'subbaz');
  1943. $this->assertEquals($keys, array_keys($codes['sub']));
  1944. }
  1945. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  1946. {
  1947. $this->_checkZf2794();
  1948. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1949. $codes = $this->form->getErrors('sub');
  1950. $this->assertTrue(is_array($codes));
  1951. $this->assertFalse(empty($codes));
  1952. $keys = array('subfoo', 'subbar', 'subbaz');
  1953. $this->assertEquals($keys, array_keys($codes));
  1954. }
  1955. public function testGetErrorsHonorsElementsBelongTo()
  1956. {
  1957. $this->_checkZf2794();
  1958. $subForm = new Zend_Form_SubForm();
  1959. $subForm->setElementsBelongTo('foo[bar]');
  1960. $subForm->addElement('text', 'test')->test
  1961. ->setRequired(true);
  1962. $this->form->addSubForm($subForm, 'sub');
  1963. $data = array('foo' => array(
  1964. 'bar' => array(
  1965. 'test' => '',
  1966. ),
  1967. ));
  1968. $this->form->isValid($data);
  1969. $codes = $this->form->getErrors();
  1970. $this->assertFalse(empty($codes['foo']['bar']['test']));
  1971. }
  1972. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  1973. {
  1974. $this->_checkZf2794();
  1975. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1976. $data = array(
  1977. 'foo' => 'abcdef',
  1978. 'bar' => '123456',
  1979. 'baz' => '123abc',
  1980. 'subfoo' => '123',
  1981. 'subbar' => 'abc',
  1982. 'subbaz' => '123-abc',
  1983. );
  1984. $this->assertFalse($this->form->isValid($data));
  1985. $codes = $this->form->getMessages();
  1986. $this->assertTrue(array_key_exists('sub', $codes));
  1987. $this->assertTrue(is_array($codes['sub']));
  1988. $keys = array('subfoo', 'subbar', 'subbaz');
  1989. $this->assertEquals($keys, array_keys($codes['sub']));
  1990. }
  1991. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  1992. {
  1993. $this->_checkZf2794();
  1994. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1995. $data = array(
  1996. 'foo' => 'abcdef',
  1997. 'bar' => '123456',
  1998. 'baz' => '123abc',
  1999. 'subfoo' => '123',
  2000. 'subbar' => 'abc',
  2001. 'subbaz' => '123-abc',
  2002. );
  2003. $this->assertFalse($this->form->isValid($data));
  2004. $codes = $this->form->getMessages('sub');
  2005. $this->assertTrue(is_array($codes));
  2006. $this->assertFalse(empty($codes));
  2007. $keys = array('subfoo', 'subbar', 'subbaz');
  2008. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  2009. }
  2010. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  2011. {
  2012. $this->_checkZf2794();
  2013. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2014. $translate = new Zend_Translate('array', $translations, 'en');
  2015. $translate->setLocale('en');
  2016. $this->form->addElements(array(
  2017. 'foo' => array(
  2018. 'type' => 'text',
  2019. 'options' => array(
  2020. 'required' => true,
  2021. 'validators' => array('NotEmpty')
  2022. )
  2023. ),
  2024. 'bar' => array(
  2025. 'type' => 'text',
  2026. 'options' => array(
  2027. 'required' => true,
  2028. 'validators' => array('Digits')
  2029. )
  2030. ),
  2031. ))
  2032. ->setTranslator($translate);
  2033. $data = array(
  2034. 'foo' => '',
  2035. 'bar' => 'abc',
  2036. );
  2037. if ($this->form->isValid($data)) {
  2038. $this->fail('Form should not validate');
  2039. }
  2040. $messages = $this->form->getMessages();
  2041. $this->assertTrue(isset($messages['foo']));
  2042. $this->assertTrue(isset($messages['bar']));
  2043. foreach ($messages['foo'] as $key => $message) {
  2044. if (array_key_exists($key, $translations)) {
  2045. $this->assertEquals($translations[$key], $message);
  2046. } else {
  2047. $this->fail('Translation for ' . $key . ' does not exist?');
  2048. }
  2049. }
  2050. foreach ($messages['bar'] as $key => $message) {
  2051. if (array_key_exists($key, $translations)) {
  2052. $this->assertEquals($translations[$key], $message);
  2053. } else {
  2054. $this->fail('Translation for ' . $key . ' does not exist?');
  2055. }
  2056. }
  2057. }
  2058. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2059. {
  2060. $this->_checkZf2794();
  2061. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2062. $translate = new Zend_Translate('array', $translations, 'en');
  2063. $translate->setLocale('en');
  2064. $this->form->addElements(array(
  2065. 'foo' => array(
  2066. 'type' => 'text',
  2067. 'options' => array(
  2068. 'required' => true,
  2069. 'validators' => array('NotEmpty')
  2070. )
  2071. ),
  2072. 'bar' => array(
  2073. 'type' => 'text',
  2074. 'options' => array(
  2075. 'required' => true,
  2076. 'validators' => array('Digits')
  2077. )
  2078. ),
  2079. ))
  2080. ->setTranslator($translate);
  2081. $data = array(
  2082. 'foo' => '',
  2083. );
  2084. if ($this->form->isValidPartial($data)) {
  2085. $this->fail('Form should not validate');
  2086. }
  2087. $messages = $this->form->getMessages();
  2088. $this->assertTrue(isset($messages['foo']));
  2089. $this->assertFalse(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. }
  2098. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2099. {
  2100. $this->_checkZf2794();
  2101. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2102. $translate = new Zend_Translate('array', $translations, 'en');
  2103. $translate->setLocale('en');
  2104. $this->form->addElements(array(
  2105. 'foo' => array(
  2106. 'type' => 'text',
  2107. 'options' => array(
  2108. 'required' => true,
  2109. 'validators' => array('NotEmpty')
  2110. )
  2111. ),
  2112. 'bar' => array(
  2113. 'type' => 'text',
  2114. 'options' => array(
  2115. 'required' => true,
  2116. 'validators' => array('Digits')
  2117. )
  2118. ),
  2119. ))
  2120. ->setTranslator($translate);
  2121. $data = array(
  2122. 'foo' => '',
  2123. );
  2124. $return = $this->form->processAjax($data);
  2125. $messages = Zend_Json::decode($return);
  2126. $this->assertTrue(is_array($messages));
  2127. $this->assertTrue(isset($messages['foo']));
  2128. $this->assertFalse(isset($messages['bar']));
  2129. foreach ($messages['foo'] as $key => $message) {
  2130. if (array_key_exists($key, $translations)) {
  2131. $this->assertEquals($translations[$key], $message);
  2132. } else {
  2133. $this->fail('Translation for ' . $key . ' does not exist?');
  2134. }
  2135. }
  2136. }
  2137. /**#@+
  2138. * @group ZF-2988
  2139. */
  2140. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2141. {
  2142. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2143. $this->form->addErrorMessage('Invalid values entered');
  2144. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2145. $messages = $this->form->getMessages();
  2146. $this->assertEquals(1, count($messages));
  2147. $this->assertEquals('Invalid values entered', array_shift($messages));
  2148. }
  2149. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2150. {
  2151. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2152. $this->form->addErrorMessage('Invalid values entered');
  2153. $this->form->addErrorMessage('Really, they are not valid');
  2154. $messages = $this->form->getErrorMessages();
  2155. $this->assertEquals(2, count($messages));
  2156. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2157. $messages = $this->form->getMessages();
  2158. $this->assertEquals(2, count($messages));
  2159. $this->assertEquals('Invalid values entered', array_shift($messages));
  2160. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2161. }
  2162. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2163. {
  2164. $set1 = array('foo', 'bar', 'baz');
  2165. $this->form->addErrorMessages($set1);
  2166. $this->assertSame($set1, $this->form->getErrorMessages());
  2167. }
  2168. public function testSetErrorMessagesShouldOverwriteMessages()
  2169. {
  2170. $set1 = array('foo', 'bar', 'baz');
  2171. $set2 = array('bat', 'cat');
  2172. $this->form->addErrorMessages($set1);
  2173. $this->assertSame($set1, $this->form->getErrorMessages());
  2174. $this->form->setErrorMessages($set2);
  2175. $this->assertSame($set2, $this->form->getErrorMessages());
  2176. }
  2177. public function testCustomErrorMessageStackShouldBeClearable()
  2178. {
  2179. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2180. $this->form->clearErrorMessages();
  2181. $messages = $this->form->getErrorMessages();
  2182. $this->assertTrue(empty($messages));
  2183. }
  2184. public function testCustomErrorMessagesShouldBeTranslated()
  2185. {
  2186. $translations = array(
  2187. 'foo' => 'Foo message',
  2188. );
  2189. $translate = new Zend_Translate('array', $translations);
  2190. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2191. $this->form->setTranslator($translate)
  2192. ->addErrorMessage('foo');
  2193. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2194. $messages = $this->form->getMessages();
  2195. $this->assertEquals(1, count($messages));
  2196. $this->assertEquals('Foo message', array_shift($messages));
  2197. }
  2198. public function testShouldAllowMarkingFormAsInvalid()
  2199. {
  2200. $this->form->addErrorMessage('Invalid values entered');
  2201. $this->assertFalse($this->form->isErrors());
  2202. $this->form->markAsError();
  2203. $this->assertTrue($this->form->isErrors());
  2204. $messages = $this->form->getMessages();
  2205. $this->assertEquals(1, count($messages));
  2206. $this->assertEquals('Invalid values entered', array_shift($messages));
  2207. }
  2208. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2209. {
  2210. $this->assertFalse($this->form->isErrors());
  2211. $this->form->setErrors(array('Error 1', 'Error 2'))
  2212. ->addError('Error 3')
  2213. ->addErrors(array('Error 4', 'Error 5'));
  2214. $this->assertTrue($this->form->isErrors());
  2215. $messages = $this->form->getMessages();
  2216. $this->assertEquals(5, count($messages));
  2217. foreach (range(1, 5) as $id) {
  2218. $message = 'Error ' . $id;
  2219. $this->assertContains($message, $messages);
  2220. }
  2221. }
  2222. /**#@-*/
  2223. // View object
  2224. public function getView()
  2225. {
  2226. $view = new Zend_View();
  2227. $libPath = dirname(__FILE__) . '/../../../library';
  2228. $view->addHelperPath($libPath . '/Zend/View/Helper');
  2229. return $view;
  2230. }
  2231. public function testGetViewRetrievesFromViewRendererByDefault()
  2232. {
  2233. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2234. $viewRenderer->initView();
  2235. $view = $viewRenderer->view;
  2236. $test = $this->form->getView();
  2237. $this->assertSame($view, $test);
  2238. }
  2239. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2240. {
  2241. $this->assertNull($this->form->getView());
  2242. }
  2243. public function testCanSetView()
  2244. {
  2245. $view = new Zend_View();
  2246. $this->assertNull($this->form->getView());
  2247. $this->form->setView($view);
  2248. $received = $this->form->getView();
  2249. $this->assertSame($view, $received);
  2250. }
  2251. // Decorators
  2252. public function testFormDecoratorRegisteredByDefault()
  2253. {
  2254. $this->_checkZf2794();
  2255. $decorator = $this->form->getDecorator('form');
  2256. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2257. }
  2258. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2259. {
  2260. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2261. $decorators = $form->getDecorators();
  2262. $this->assertEquals(array(), $decorators);
  2263. }
  2264. public function testCanAddSingleDecoratorAsString()
  2265. {
  2266. $this->_checkZf2794();
  2267. $this->form->clearDecorators();
  2268. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2269. $this->form->addDecorator('viewHelper');
  2270. $decorator = $this->form->getDecorator('viewHelper');
  2271. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2272. }
  2273. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2274. {
  2275. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2276. }
  2277. public function testCanAddSingleDecoratorAsDecoratorObject()
  2278. {
  2279. $this->form->clearDecorators();
  2280. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2281. $decorator = new Zend_Form_Decorator_ViewHelper;
  2282. $this->form->addDecorator($decorator);
  2283. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2284. $this->assertSame($decorator, $test);
  2285. }
  2286. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2287. {
  2288. $this->_checkZf2794();
  2289. $this->form->clearDecorators();
  2290. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2291. $decorator = new Zend_Form_Decorator_ViewHelper;
  2292. $this->form->addDecorator($decorator);
  2293. $test = $this->form->getDecorator('viewHelper');
  2294. $this->assertSame($decorator, $test);
  2295. }
  2296. public function testCanAddMultipleDecorators()
  2297. {
  2298. $this->_checkZf2794();
  2299. $this->form->clearDecorators();
  2300. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2301. $testDecorator = new Zend_Form_Decorator_Errors;
  2302. $this->form->addDecorators(array(
  2303. 'ViewHelper',
  2304. $testDecorator
  2305. ));
  2306. $viewHelper = $this->form->getDecorator('viewHelper');
  2307. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2308. $decorator = $this->form->getDecorator('errors');
  2309. $this->assertSame($testDecorator, $decorator);
  2310. }
  2311. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2312. {
  2313. $this->_checkZf2794();
  2314. $this->assertFalse($this->form->removeDecorator('foobar'));
  2315. }
  2316. public function testCanRemoveDecorator()
  2317. {
  2318. $this->_checkZf2794();
  2319. $this->testFormDecoratorRegisteredByDefault();
  2320. $this->form->removeDecorator('form');
  2321. $this->assertFalse($this->form->getDecorator('form'));
  2322. }
  2323. /**
  2324. * @group ZF-3069
  2325. */
  2326. public function testRemovingNamedDecoratorShouldWork()
  2327. {
  2328. $this->_checkZf2794();
  2329. $this->form->setDecorators(array(
  2330. 'FormElements',
  2331. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2332. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2333. ));
  2334. $decorators = $this->form->getDecorators();
  2335. $this->assertTrue(array_key_exists('div', $decorators));
  2336. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2337. $this->form->removeDecorator('div');
  2338. $decorators = $this->form->getDecorators();
  2339. $this->assertFalse(array_key_exists('div', $decorators));
  2340. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2341. }
  2342. public function testCanClearAllDecorators()
  2343. {
  2344. $this->_checkZf2794();
  2345. $this->testCanAddMultipleDecorators();
  2346. $this->form->clearDecorators();
  2347. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2348. $this->assertFalse($this->form->getDecorator('fieldset'));
  2349. }
  2350. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2351. {
  2352. $this->_checkZf2794();
  2353. $this->form->setDecorators(array(
  2354. array('HtmlTag', array('tag' => 'div')),
  2355. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2356. ));
  2357. $decorator = $this->form->getDecorator('FooBar');
  2358. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2359. $this->assertEquals('dd', $decorator->getOption('tag'));
  2360. $decorator = $this->form->getDecorator('HtmlTag');
  2361. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2362. $this->assertEquals('div', $decorator->getOption('tag'));
  2363. }
  2364. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2365. {
  2366. $this->form->setDecorators(array(
  2367. 'FormElements',
  2368. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2369. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2370. 'Form',
  2371. ));
  2372. $decorator = $this->form->getDecorator('fieldset');
  2373. $decorators = $this->form->getDecorators();
  2374. $i = 0;
  2375. $order = array();
  2376. foreach (array_keys($decorators) as $name) {
  2377. $order[$name] = $i;
  2378. ++$i;
  2379. }
  2380. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2381. }
  2382. // Rendering
  2383. public function checkMarkup($html)
  2384. {
  2385. $this->assertFalse(empty($html));
  2386. $this->assertContains('<form', $html);
  2387. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2388. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2389. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2390. $this->assertContains('</form>', $html);
  2391. }
  2392. public function testRenderReturnsMarkup()
  2393. {
  2394. $this->setupElements();
  2395. $html = $this->form->render($this->getView());
  2396. $this->checkMarkup($html);
  2397. }
  2398. public function testRenderReturnsMarkupRepresentingAllElements()
  2399. {
  2400. $this->testRenderReturnsMarkup();
  2401. $html = $this->form->render();
  2402. foreach ($this->form->getElements() as $key => $element) {
  2403. $this->assertFalse(empty($key));
  2404. $this->assertFalse(is_numeric($key));
  2405. $this->assertContains('<input', $html);
  2406. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2407. }
  2408. }
  2409. public function testRenderReturnsMarkupContainingSubForms()
  2410. {
  2411. $this->setupElements();
  2412. $this->setupSubForm();
  2413. $this->form->setView($this->getView());
  2414. $html = $this->form->render();
  2415. $this->assertRegexp('/<fieldset/', $html);
  2416. $this->assertContains('</fieldset>', $html);
  2417. foreach ($this->form->sub as $key => $item) {
  2418. $this->assertFalse(empty($key));
  2419. $this->assertFalse(is_numeric($key));
  2420. $this->assertContains('<input', $html);
  2421. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2422. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2423. }
  2424. }
  2425. public function testRenderReturnsMarkupContainingDisplayGroups()
  2426. {
  2427. $this->setupElements();
  2428. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2429. $this->form->setView($this->getView());
  2430. $html = $this->html = $this->form->render();
  2431. $this->assertRegexp('/<fieldset/', $html);
  2432. $this->assertContains('</fieldset>', $html);
  2433. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2434. $dom = new DOMDocument();
  2435. $dom->loadHTML($html);
  2436. $fieldsets = $dom->getElementsByTagName('fieldset');
  2437. $this->assertTrue(0 < $fieldsets->length);
  2438. $fieldset = $fieldsets->item(0);
  2439. $nodes = $fieldset->childNodes;
  2440. $this->assertNotNull($nodes);
  2441. for ($i = 0; $i < $nodes->length; ++$i) {
  2442. $node = $nodes->item($i);
  2443. if ('input' != $node->nodeName) {
  2444. continue;
  2445. }
  2446. $this->assertTrue($node->hasAttribute('name'));
  2447. $nameNode = $node->getAttributeNode('name');
  2448. switch ($i) {
  2449. case 0:
  2450. $this->assertEquals('foo', $nameNode->nodeValue);
  2451. break;
  2452. case 1:
  2453. $this->assertEquals('baz', $nameNode->nodeValue);
  2454. break;
  2455. default:
  2456. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2457. }
  2458. }
  2459. }
  2460. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2461. {
  2462. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2463. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2464. $this->fail("Should find foo element in rendered form");
  2465. }
  2466. $this->assertEquals(1, count($matches));
  2467. $this->assertEquals(1, count($matches[0]));
  2468. }
  2469. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2470. {
  2471. $this->setupElements();
  2472. $this->form->setElementsBelongTo('anArray');
  2473. $html = $this->form->render($this->getView());
  2474. $this->assertContains('name="anArray[foo]"', $html);
  2475. $this->assertContains('name="anArray[bar]"', $html);
  2476. $this->assertContains('name="anArray[baz]"', $html);
  2477. $this->assertContains('id="anArray-foo"', $html);
  2478. $this->assertContains('id="anArray-bar"', $html);
  2479. $this->assertContains('id="anArray-baz"', $html);
  2480. }
  2481. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2482. {
  2483. $this->setupElements();
  2484. $this->form->setElementsBelongTo('data[foo]');
  2485. $html = $this->form->render($this->getView());
  2486. $this->assertContains('name="data[foo][foo]"', $html);
  2487. $this->assertContains('name="data[foo][bar]"', $html);
  2488. $this->assertContains('name="data[foo][baz]"', $html);
  2489. $this->assertContains('id="data-foo-foo"', $html);
  2490. $this->assertContains('id="data-foo-bar"', $html);
  2491. $this->assertContains('id="data-foo-baz"', $html);
  2492. }
  2493. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2494. {
  2495. $this->setupElements();
  2496. $this->form->setName('data')
  2497. ->setIsArray(true);
  2498. $html = $this->form->render($this->getView());
  2499. $this->assertContains('name="data[foo]"', $html);
  2500. $this->assertContains('name="data[bar]"', $html);
  2501. $this->assertContains('name="data[baz]"', $html);
  2502. $this->assertContains('id="data-foo"', $html);
  2503. $this->assertContains('id="data-bar"', $html);
  2504. $this->assertContains('id="data-baz"', $html);
  2505. }
  2506. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2507. {
  2508. $this->form->setName('data')
  2509. ->setIsArray(true);
  2510. $subForm = new Zend_Form_SubForm();
  2511. $subForm->setElementsBelongTo('billing[info]');
  2512. $subForm->addElement('text', 'name');
  2513. $subForm->addElement('text', 'number');
  2514. $this->form->addSubForm($subForm, 'sub');
  2515. $html = $this->form->render($this->getView());
  2516. $this->assertContains('name="data[billing][info][name]', $html);
  2517. $this->assertContains('name="data[billing][info][number]', $html);
  2518. $this->assertContains('id="data-billing-info-name"', $html);
  2519. $this->assertContains('id="data-billing-info-number"', $html);
  2520. }
  2521. public function testToStringProxiesToRender()
  2522. {
  2523. $this->setupElements();
  2524. $this->form->setView($this->getView());
  2525. $html = $this->form->__toString();
  2526. $this->checkMarkup($html);
  2527. }
  2528. public function raiseDecoratorException($content, $element, $options)
  2529. {
  2530. throw new Exception('Raising exception in decorator callback');
  2531. }
  2532. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2533. {
  2534. $this->error = $errstr;
  2535. }
  2536. public function testToStringRaisesErrorWhenExceptionCaught()
  2537. {
  2538. $this->form->setDecorators(array(
  2539. array(
  2540. 'decorator' => 'Callback',
  2541. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2542. ),
  2543. ));
  2544. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2545. $text = $this->form->__toString();
  2546. restore_error_handler();
  2547. $this->assertTrue(empty($text));
  2548. $this->assertTrue(isset($this->error));
  2549. $this->assertContains('Raising exception in decorator callback', $this->error);
  2550. }
  2551. /**
  2552. * ZF-2718
  2553. */
  2554. public function testHiddenElementsGroupedWhenRendered()
  2555. {
  2556. $this->markTestIncomplete('Scheduling for future release');
  2557. $this->form->addElements(array(
  2558. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2559. array('type' => 'text', 'name' => 'testone'),
  2560. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2561. array('type' => 'text', 'name' => 'testtwo'),
  2562. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2563. array('type' => 'text', 'name' => 'testthree'),
  2564. ));
  2565. $html = $this->form->render($this->getView());
  2566. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2567. $this->fail('Hidden elements should be grouped');
  2568. }
  2569. foreach (array('first', 'second', 'third') as $which) {
  2570. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2571. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2572. }
  2573. }
  2574. // Localization
  2575. public function testTranslatorIsNullByDefault()
  2576. {
  2577. $this->assertNull($this->form->getTranslator());
  2578. }
  2579. public function testCanSetTranslator()
  2580. {
  2581. require_once 'Zend/Translate/Adapter/Array.php';
  2582. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2583. $this->form->setTranslator($translator);
  2584. $received = $this->form->getTranslator($translator);
  2585. $this->assertSame($translator->getAdapter(), $received);
  2586. }
  2587. public function testCanSetDefaultGlobalTranslator()
  2588. {
  2589. $this->assertNull($this->form->getTranslator());
  2590. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2591. Zend_Form::setDefaultTranslator($translator);
  2592. $received = Zend_Form::getDefaultTranslator();
  2593. $this->assertSame($translator->getAdapter(), $received);
  2594. $received = $this->form->getTranslator();
  2595. $this->assertSame($translator->getAdapter(), $received);
  2596. $form = new Zend_Form();
  2597. $received = $form->getTranslator();
  2598. $this->assertSame($translator->getAdapter(), $received);
  2599. }
  2600. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2601. {
  2602. $this->assertNull($this->form->getTranslator());
  2603. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2604. Zend_Form::setDefaultTranslator($translatorDefault);
  2605. $received = $this->form->getTranslator();
  2606. $this->assertSame($translatorDefault->getAdapter(), $received);
  2607. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2608. $this->form->setTranslator($translator);
  2609. $received = $this->form->getTranslator();
  2610. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2611. $this->assertSame($translator->getAdapter(), $received);
  2612. }
  2613. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2614. {
  2615. $this->assertNull($this->form->getTranslator());
  2616. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2617. Zend_Registry::set('Zend_Translate', $translator);
  2618. $received = Zend_Form::getDefaultTranslator();
  2619. $this->assertSame($translator->getAdapter(), $received);
  2620. $received = $this->form->getTranslator();
  2621. $this->assertSame($translator->getAdapter(), $received);
  2622. $form = new Zend_Form();
  2623. $received = $form->getTranslator();
  2624. $this->assertSame($translator->getAdapter(), $received);
  2625. }
  2626. public function testCanDisableTranslation()
  2627. {
  2628. $this->testCanSetDefaultGlobalTranslator();
  2629. $this->form->setDisableTranslator(true);
  2630. $this->assertNull($this->form->getTranslator());
  2631. }
  2632. // Iteration
  2633. public function testFormObjectIsIterableAndIteratesElements()
  2634. {
  2635. $this->setupElements();
  2636. $expected = array('foo', 'bar', 'baz');
  2637. $received = array();
  2638. foreach ($this->form as $key => $value) {
  2639. $received[] = $key;
  2640. }
  2641. $this->assertSame($expected, $received);
  2642. }
  2643. public function testFormObjectIteratesElementsInExpectedOrder()
  2644. {
  2645. $this->setupElements();
  2646. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2647. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2648. $received = array();
  2649. foreach ($this->form as $key => $value) {
  2650. $received[] = $key;
  2651. $this->assertTrue($value instanceof Zend_Form_Element);
  2652. }
  2653. $this->assertSame($expected, $received);
  2654. }
  2655. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2656. {
  2657. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2658. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2659. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2660. $expected = array('name', 'email', 'submit');
  2661. $received = array();
  2662. foreach ($this->form as $key => $value) {
  2663. $received[] = $key;
  2664. $this->assertTrue($value instanceof Zend_Form_Element);
  2665. }
  2666. $this->assertSame($expected, $received);
  2667. }
  2668. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2669. {
  2670. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2671. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2672. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2673. ->setView($this->getView());
  2674. $test = $this->form->render();
  2675. $this->assertContains('name="a"', $test);
  2676. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2677. $this->fail('Expected markup not found');
  2678. }
  2679. $order = array();
  2680. foreach ($matches[1] as $element) {
  2681. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2682. $order[] = $m[1];
  2683. }
  2684. }
  2685. $this->assertSame(array('b', 'c', 'a'), $order);
  2686. }
  2687. public function testFormObjectIteratesElementsAndSubforms()
  2688. {
  2689. $this->setupElements();
  2690. $this->setupSubForm();
  2691. $expected = array('foo', 'bar', 'baz', 'sub');
  2692. $received = array();
  2693. foreach ($this->form as $key => $value) {
  2694. $received[] = $key;
  2695. $this->assertTrue(($value instanceof Zend_Form_Element)
  2696. or ($value instanceof Zend_Form_SubForm));
  2697. }
  2698. $this->assertSame($expected, $received);
  2699. }
  2700. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  2701. {
  2702. $this->setupElements();
  2703. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2704. $expected = array('bar', 'foobaz');
  2705. $received = array();
  2706. foreach ($this->form as $key => $value) {
  2707. $received[] = $key;
  2708. $this->assertTrue(($value instanceof Zend_Form_Element)
  2709. or ($value instanceof Zend_Form_DisplayGroup));
  2710. }
  2711. $this->assertSame($expected, $received);
  2712. }
  2713. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  2714. {
  2715. $this->setupElements();
  2716. $bar = $this->form->bar;
  2717. $this->form->removeElement('bar');
  2718. try {
  2719. foreach ($this->form as $item) {
  2720. }
  2721. } catch (Exception $e) {
  2722. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2723. }
  2724. $this->form->addElement($bar);
  2725. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  2726. $this->form->removeDisplayGroup('bazbar');
  2727. try {
  2728. foreach ($this->form as $item) {
  2729. }
  2730. } catch (Exception $e) {
  2731. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2732. }
  2733. $subForm = new Zend_Form_SubForm;
  2734. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  2735. $this->form->addSubForm($subForm, 'page1');
  2736. $this->form->removeSubForm('page1');
  2737. try {
  2738. foreach ($this->form as $item) {
  2739. }
  2740. } catch (Exception $e) {
  2741. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2742. }
  2743. }
  2744. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  2745. {
  2746. $form = new Zend_Form();
  2747. $form->addElements(array(
  2748. 'username' => 'text',
  2749. 'password' => 'text',
  2750. ));
  2751. $form->clearElements();
  2752. try {
  2753. foreach ($form as $item) {
  2754. }
  2755. } catch (Zend_Form_Exception $e) {
  2756. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  2757. . $e->getMessage();
  2758. $this->fail($message);
  2759. }
  2760. $form->addElements(array(
  2761. 'username' => 'text',
  2762. 'password' => 'text',
  2763. ))
  2764. ->addDisplayGroup(array('username', 'password'), 'login');
  2765. $form->clearDisplayGroups();
  2766. try {
  2767. foreach ($form as $item) {
  2768. }
  2769. } catch (Zend_Form_Exception $e) {
  2770. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  2771. . $e->getMessage();
  2772. $this->fail($message);
  2773. }
  2774. $subForm = new Zend_Form_SubForm();
  2775. $form->addSubForm($subForm, 'foo');
  2776. $form->clearSubForms();
  2777. try {
  2778. foreach ($form as $item) {
  2779. }
  2780. } catch (Zend_Form_Exception $e) {
  2781. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  2782. . $e->getMessage();
  2783. $this->fail($message);
  2784. }
  2785. }
  2786. // Countable
  2787. public function testCanCountFormObject()
  2788. {
  2789. $this->setupElements();
  2790. $this->assertEquals(3, count($this->form));
  2791. }
  2792. public function testCountingFormObjectCountsSubForms()
  2793. {
  2794. $this->setupElements();
  2795. $this->setupSubForm();
  2796. $this->assertEquals(4, count($this->form));
  2797. }
  2798. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  2799. {
  2800. $this->testCountingFormObjectCountsSubForms();
  2801. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2802. $this->assertEquals(3, count($this->form));
  2803. }
  2804. // Element decorators and plugin paths
  2805. public function testCanSetAllElementDecoratorsAtOnce()
  2806. {
  2807. $this->_checkZf2794();
  2808. $this->setupElements();
  2809. $this->form->setElementDecorators(array(
  2810. array('ViewHelper'),
  2811. array('Label'),
  2812. array('Fieldset'),
  2813. ));
  2814. foreach ($this->form->getElements() as $element) {
  2815. $this->assertFalse($element->getDecorator('Errors'));
  2816. $this->assertFalse($element->getDecorator('HtmlTag'));
  2817. $decorator = $element->getDecorator('ViewHelper');
  2818. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2819. $decorator = $element->getDecorator('Label');
  2820. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  2821. $decorator = $element->getDecorator('Fieldset');
  2822. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2823. }
  2824. }
  2825. /**
  2826. * @group ZF-3597
  2827. */
  2828. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  2829. {
  2830. $this->form->setDecorators(array(
  2831. 'FormElements',
  2832. array('HtmlTag', array('tag' => 'table')),
  2833. 'Form',
  2834. ));
  2835. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  2836. $this->form->addElement('text', 'test', array(
  2837. 'label' => 'Foo',
  2838. 'description' => 'sample description',
  2839. ));
  2840. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  2841. $decorator = new My_Decorator_TableRow();
  2842. $this->form->setElementDecorators(array(
  2843. 'ViewHelper',
  2844. $decorator,
  2845. ));
  2846. $html = $this->form->render($this->getView());
  2847. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  2848. }
  2849. /**#@+
  2850. * @group ZF-3228
  2851. */
  2852. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  2853. {
  2854. $this->_checkZf2794();
  2855. $this->setupElements();
  2856. $this->form->setElementDecorators(
  2857. array(
  2858. 'Description',
  2859. 'Form',
  2860. 'Fieldset',
  2861. ),
  2862. array(
  2863. 'bar',
  2864. )
  2865. );
  2866. $element = $this->form->bar;
  2867. $this->assertFalse($element->getDecorator('ViewHelper'));
  2868. $this->assertFalse($element->getDecorator('Errors'));
  2869. $this->assertFalse($element->getDecorator('Label'));
  2870. $this->assertFalse($element->getDecorator('HtmlTag'));
  2871. $decorator = $element->getDecorator('Description');
  2872. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2873. $decorator = $element->getDecorator('Form');
  2874. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2875. $decorator = $element->getDecorator('Fieldset');
  2876. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2877. foreach (array('foo', 'baz') as $name) {
  2878. $element = $this->form->$name;
  2879. $this->assertFalse($element->getDecorator('Form'));
  2880. $this->assertFalse($element->getDecorator('Fieldset'));
  2881. }
  2882. }
  2883. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  2884. {
  2885. $this->_checkZf2794();
  2886. $this->setupElements();
  2887. $this->form->setElementDecorators(
  2888. array(
  2889. 'Description',
  2890. 'Form',
  2891. 'Fieldset',
  2892. ),
  2893. array(
  2894. 'foo',
  2895. 'baz',
  2896. ),
  2897. false
  2898. );
  2899. $element = $this->form->bar;
  2900. $this->assertFalse($element->getDecorator('ViewHelper'));
  2901. $this->assertFalse($element->getDecorator('Errors'));
  2902. $this->assertFalse($element->getDecorator('Label'));
  2903. $this->assertFalse($element->getDecorator('HtmlTag'));
  2904. $decorator = $element->getDecorator('Description');
  2905. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2906. $decorator = $element->getDecorator('Form');
  2907. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2908. $decorator = $element->getDecorator('Fieldset');
  2909. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2910. foreach (array('foo', 'baz') as $name) {
  2911. $element = $this->form->$name;
  2912. $this->assertFalse($element->getDecorator('Form'));
  2913. $this->assertFalse($element->getDecorator('Fieldset'));
  2914. }
  2915. }
  2916. /**#@-*/
  2917. public function testCanSetAllElementFiltersAtOnce()
  2918. {
  2919. $this->_checkZf2794();
  2920. $this->setupElements();
  2921. $this->form->setElementFilters(array(
  2922. 'Alnum',
  2923. 'StringToLower'
  2924. ));
  2925. foreach ($this->form->getElements() as $element) {
  2926. $filter = $element->getFilter('Alnum');
  2927. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  2928. $filter = $element->getFilter('StringToLower');
  2929. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  2930. }
  2931. }
  2932. public function testCanSetGlobalElementPrefixPath()
  2933. {
  2934. $this->setupElements();
  2935. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2936. $this->form->addElement('text', 'prefixTest');
  2937. foreach ($this->form->getElements() as $element) {
  2938. $loader = $element->getPluginLoader('validate');
  2939. $paths = $loader->getPaths('Zend_Foo_Validate');
  2940. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  2941. $this->assertContains('Foo', $paths[0]);
  2942. $this->assertContains('Validate', $paths[0]);
  2943. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  2944. $this->assertFalse(empty($paths));
  2945. $this->assertContains('Foo', $paths[0]);
  2946. $this->assertContains('Filter', $paths[0]);
  2947. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  2948. $this->assertFalse(empty($paths));
  2949. $this->assertContains('Foo', $paths[0]);
  2950. $this->assertContains('Decorator', $paths[0]);
  2951. }
  2952. }
  2953. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  2954. {
  2955. $this->_checkZf2794();
  2956. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  2957. $this->form->addElement('text', 'prefixTest');
  2958. $element = $this->form->prefixTest;
  2959. $label = $element->getDecorator('Label');
  2960. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  2961. }
  2962. /**
  2963. * @group ZF-3093
  2964. */
  2965. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  2966. {
  2967. $subForm = new Zend_Form_SubForm();
  2968. $subForm->addElement('text', 'foo');
  2969. $this->form->addSubForm($subForm, 'subForm');
  2970. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2971. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  2972. $paths = $loader->getPaths('Zend_Foo_Decorator');
  2973. $this->assertFalse(empty($paths));
  2974. $this->assertContains('Foo', $paths[0]);
  2975. $this->assertContains('Decorator', $paths[0]);
  2976. }
  2977. public function testCanSetElementValidatorPrefixPath()
  2978. {
  2979. $this->setupElements();
  2980. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  2981. $this->form->addElement('text', 'prefixTest');
  2982. foreach ($this->form->getElements() as $element) {
  2983. $loader = $element->getPluginLoader('validate');
  2984. $paths = $loader->getPaths('Zend_Foo');
  2985. $this->assertFalse(empty($paths));
  2986. $this->assertContains('Foo', $paths[0]);
  2987. $this->assertNotContains('Validate', $paths[0]);
  2988. }
  2989. }
  2990. public function testCanSetElementFilterPrefixPath()
  2991. {
  2992. $this->setupElements();
  2993. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  2994. $this->form->addElement('text', 'prefixTest');
  2995. foreach ($this->form->getElements() as $element) {
  2996. $loader = $element->getPluginLoader('filter');
  2997. $paths = $loader->getPaths('Zend_Foo');
  2998. $this->assertFalse(empty($paths));
  2999. $this->assertContains('Foo', $paths[0]);
  3000. $this->assertNotContains('Filter', $paths[0]);
  3001. }
  3002. }
  3003. public function testCanSetElementDecoratorPrefixPath()
  3004. {
  3005. $this->setupElements();
  3006. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  3007. $this->form->addElement('text', 'prefixTest');
  3008. foreach ($this->form->getElements() as $element) {
  3009. $loader = $element->getPluginLoader('decorator');
  3010. $paths = $loader->getPaths('Zend_Foo');
  3011. $this->assertFalse(empty($paths));
  3012. $this->assertContains('Foo', $paths[0]);
  3013. $this->assertNotContains('Decorator', $paths[0]);
  3014. }
  3015. }
  3016. // Display Group decorators and plugin paths
  3017. public function setupDisplayGroups()
  3018. {
  3019. $this->testCanAddAndRetrieveMultipleElements();
  3020. $this->form->addElements(array(
  3021. 'test1' => 'text',
  3022. 'test2' => 'text',
  3023. 'test3' => 'text',
  3024. 'test4' => 'text'
  3025. ));
  3026. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  3027. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3028. }
  3029. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  3030. {
  3031. $this->_checkZf2794();
  3032. $this->setupDisplayGroups();
  3033. $this->form->setDisplayGroupDecorators(array(
  3034. array('Callback', array('callback' => 'strip_tags')),
  3035. ));
  3036. foreach ($this->form->getDisplayGroups() as $element) {
  3037. $this->assertFalse($element->getDecorator('FormElements'));
  3038. $this->assertFalse($element->getDecorator('HtmlTag'));
  3039. $this->assertFalse($element->getDecorator('Fieldset'));
  3040. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  3041. $decorator = $element->getDecorator('Callback');
  3042. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3043. }
  3044. }
  3045. public function testCanSetDisplayGroupPrefixPath()
  3046. {
  3047. $this->setupDisplayGroups();
  3048. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3049. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3050. foreach ($this->form->getDisplayGroups() as $group) {
  3051. $loader = $group->getPluginLoader();
  3052. $paths = $loader->getPaths('Zend_Foo');
  3053. $this->assertFalse(empty($paths));
  3054. $this->assertContains('Foo', $paths[0]);
  3055. }
  3056. }
  3057. /**
  3058. * @group ZF-3213
  3059. */
  3060. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3061. {
  3062. require_once 'Zend/Config/Ini.php';
  3063. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3064. $form = new Zend_Form($config);
  3065. $dg = $form->foofoo;
  3066. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3067. $this->assertTrue($paths !== false);
  3068. }
  3069. // Subform decorators
  3070. public function testCanSetAllSubFormDecoratorsAtOnce()
  3071. {
  3072. $this->_checkZf2794();
  3073. $this->setupSubForm();
  3074. $this->form->setSubFormDecorators(array(
  3075. array('Callback', array('callback' => 'strip_tags')),
  3076. ));
  3077. foreach ($this->form->getSubForms() as $subForm) {
  3078. $this->assertFalse($subForm->getDecorator('FormElements'));
  3079. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3080. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3081. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3082. $decorator = $subForm->getDecorator('Callback');
  3083. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3084. }
  3085. }
  3086. // Extension
  3087. public function testInitCalledPriorToLoadingDefaultDecorators()
  3088. {
  3089. $form = new Zend_Form_FormTest_FormExtension();
  3090. $decorators = $form->getDecorators();
  3091. $this->assertTrue(empty($decorators));
  3092. }
  3093. // Clone
  3094. /**
  3095. * @group ZF-3819
  3096. */
  3097. public function testCloningShouldCloneAllChildren()
  3098. {
  3099. $form = new Zend_Form();
  3100. $foo = new Zend_Form_SubForm(array(
  3101. 'name' => 'foo',
  3102. 'elements' => array(
  3103. 'one' => 'text',
  3104. 'two' => 'text',
  3105. ),
  3106. ));
  3107. $form->addElement('text', 'bar')
  3108. ->addElement('text', 'baz')
  3109. ->addElement('text', 'bat')
  3110. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3111. ->addSubForm($foo, 'foo');
  3112. $bar = $form->bar;
  3113. $baz = $form->baz;
  3114. $bat = $form->bat;
  3115. $barbat = $form->barbat;
  3116. $cloned = clone $form;
  3117. $this->assertNotSame($foo, $cloned->foo);
  3118. $this->assertNotSame($bar, $cloned->bar);
  3119. $this->assertNotSame($baz, $cloned->baz);
  3120. $this->assertNotSame($bat, $cloned->bat);
  3121. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3122. $this->assertNotSame($foo->one, $cloned->foo->one);
  3123. $this->assertNotSame($foo->two, $cloned->foo->two);
  3124. }
  3125. // Reset
  3126. /**
  3127. * @group ZF-3227
  3128. */
  3129. public function testFormsShouldAllowResetting()
  3130. {
  3131. $form = new Zend_Form();
  3132. $foo = new Zend_Form_SubForm(array(
  3133. 'name' => 'foo',
  3134. 'elements' => array(
  3135. 'one' => 'text',
  3136. 'two' => 'text',
  3137. ),
  3138. ));
  3139. $form->addElement('text', 'bar')
  3140. ->addElement('text', 'baz')
  3141. ->addElement('text', 'bat')
  3142. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3143. ->addSubForm($foo, 'foo');
  3144. $values = array(
  3145. 'bar' => 'Bar Value',
  3146. 'baz' => 'Baz Value',
  3147. 'bat' => 'Bat Value',
  3148. 'foo' => array(
  3149. 'one' => 'One Value',
  3150. 'two' => 'Two Value',
  3151. ),
  3152. );
  3153. $form->populate($values);
  3154. $test = $form->getValues();
  3155. $this->assertEquals($values, $test);
  3156. $form->reset();
  3157. $test = $form->getValues();
  3158. $this->assertNotEquals($values, $test);
  3159. $this->assertEquals(0, array_sum($test));
  3160. }
  3161. /**
  3162. * @group ZF-3217
  3163. */
  3164. public function testFormShouldOverloadToRenderDecorators()
  3165. {
  3166. $this->setupElements();
  3167. $this->form->setView($this->getView());
  3168. $html = $this->form->renderFormElements();
  3169. foreach ($this->form->getElements() as $element) {
  3170. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3171. }
  3172. $this->assertNotContains('<dl', $html);
  3173. $this->assertNotContains('<form', $html);
  3174. $html = $this->form->renderForm('this is the content');
  3175. $this->assertContains('<form', $html);
  3176. $this->assertContains('</form>', $html);
  3177. $this->assertContains('this is the content', $html);
  3178. }
  3179. /**
  3180. * @group ZF-3217
  3181. * @expectedException Zend_Form_Exception
  3182. */
  3183. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3184. {
  3185. $html = $this->form->bogusMethodCall();
  3186. }
  3187. /**
  3188. * @group ZF-2950
  3189. */
  3190. public function testDtDdElementsWithLabelGetUniqueId()
  3191. {
  3192. $form = new Zend_Form();
  3193. $form->setView($this->getView());
  3194. $fooElement = new Zend_Form_Element_Text('foo');
  3195. $fooElement->setLabel('Foo');
  3196. $form->addElement($fooElement);
  3197. $html = $form->render();
  3198. $this->assertContains('<dt id="foo-label">', $html);
  3199. $this->assertContains('<dd id="foo-element">', $html);
  3200. }
  3201. /**
  3202. * @group ZF-2950
  3203. */
  3204. public function testDtDdElementsWithoutLabelGetUniqueId()
  3205. {
  3206. $form = new Zend_Form();
  3207. $form->setView($this->getView())
  3208. ->addElement(new Zend_Form_Element_Text('foo'));
  3209. $html = $form->render();
  3210. $this->assertContains('<dt id="foo-label">&nbsp;</dt>', $html);
  3211. $this->assertContains('<dd id="foo-element">', $html);
  3212. }
  3213. /**
  3214. * @group ZF-2950
  3215. */
  3216. public function testSubFormGetsUniqueIdWithName()
  3217. {
  3218. $form = new Zend_Form();
  3219. $form->setView($this->getView())
  3220. ->setName('testform')
  3221. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3222. $html = $form->render();
  3223. $this->assertContains('<dt id="testform-label">&nbsp;</dt>', $html);
  3224. $this->assertContains('<dd id="testform-element">', $html);
  3225. }
  3226. /**
  3227. * @group ZF-5370
  3228. */
  3229. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3230. {
  3231. $file = new Zend_Form_Element_File('txt');
  3232. $this->form->addElement($file);
  3233. $html = $this->form->render($this->getView());
  3234. $this->assertFalse(empty($html));
  3235. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3236. }
  3237. /**
  3238. * @group ZF-5370
  3239. */
  3240. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3241. {
  3242. $subForm = new Zend_Form_SubForm();
  3243. $subForm->addElement('file', 'txt');
  3244. $this->form->addSubForm($subForm, 'page1')
  3245. ->setView(new Zend_View);
  3246. $html = $this->form->render();
  3247. $this->assertContains('id="txt"', $html);
  3248. $this->assertContains('name="txt"', $html);
  3249. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3250. }
  3251. /**
  3252. * @group ZF-5370
  3253. */
  3254. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3255. {
  3256. $this->form->addElement('file', 'txt')
  3257. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3258. ->setView(new Zend_View);
  3259. $html = $this->form->render();
  3260. $this->assertContains('id="txt"', $html);
  3261. $this->assertContains('name="txt"', $html);
  3262. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3263. }
  3264. /**
  3265. * @group ZF-6070
  3266. */
  3267. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3268. {
  3269. $this->form->setOptions(array(
  3270. 'elementDecorators' => array(
  3271. 'ViewHelper',
  3272. 'Label',
  3273. ),
  3274. 'elements' => array(
  3275. 'foo' => array(
  3276. 'type' => 'text',
  3277. 'options' => array(
  3278. 'decorators' => array(
  3279. 'Errors',
  3280. 'ViewHelper',
  3281. ),
  3282. ),
  3283. ),
  3284. ),
  3285. ));
  3286. $element = $this->form->getElement('foo');
  3287. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3288. $actual = array();
  3289. foreach ($element->getDecorators() as $decorator) {
  3290. $actual[] = get_class($decorator);
  3291. }
  3292. $this->assertSame($expected, $actual);
  3293. }
  3294. /**
  3295. * @group ZF-5150
  3296. */
  3297. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3298. {
  3299. $this->form->addError('Error');
  3300. $this->assertFalse($this->form->isValid(array()));
  3301. }
  3302. /**
  3303. * @group ZF-8494
  3304. */
  3305. public function testGetValidValues()
  3306. {
  3307. $data = array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement');
  3308. require_once "Zend/Validate/Int.php";
  3309. $validElement = new Zend_Form_Element("valid");
  3310. $validElement->addValidator(new Zend_Validate_Int());
  3311. $this->form->addElement($validElement);
  3312. $invalidElement = new Zend_Form_Element('invalid');
  3313. $invalidElement->addValidator(new Zend_Validate_Int());
  3314. $this->form->addElement($invalidElement);
  3315. $this->assertEquals(array('valid' => 1234), $this->form->getValidValues($data));
  3316. }
  3317. /**
  3318. * @group ZF-8494
  3319. */
  3320. public function testGetValidSubFormValues()
  3321. {
  3322. $data = array('sub' => array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement'));
  3323. require_once "Zend/Validate/Int.php";
  3324. $subForm = new Zend_Form_SubForm();
  3325. $validElement = new Zend_Form_Element("valid");
  3326. $validElement->addValidator(new Zend_Validate_Int());
  3327. $subForm->addElement($validElement);
  3328. $invalidElement = new Zend_Form_Element('invalid');
  3329. $invalidElement->addValidator(new Zend_Validate_Int());
  3330. $subForm->addElement($invalidElement);
  3331. $this->form->addSubForm($subForm, 'sub');
  3332. $this->assertEquals(array('sub' => array('valid' => 1234)), $this->form->getValidValues($data));
  3333. }
  3334. /**
  3335. * @group ZF-9494
  3336. */
  3337. public function testElementTranslatorNotOveriddenbyFormTranslator()
  3338. {
  3339. $translations = array(
  3340. 'isEmpty' => 'Element message',
  3341. );
  3342. $translate = new Zend_Translate('array', $translations);
  3343. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$translate));
  3344. $this->assertFalse($this->form->isValid(array('foo'=>'')));
  3345. $messages = $this->form->getMessages();
  3346. $this->assertEquals(1, count($messages));
  3347. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3348. $this->assertFalse($this->form->isValidPartial(array('foo'=>'')));
  3349. $messages = $this->form->getMessages();
  3350. $this->assertEquals(1, count($messages));
  3351. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3352. }
  3353. /**
  3354. * @group ZF-9364
  3355. */
  3356. public function testElementTranslatorPreferredOverFormTranslator()
  3357. {
  3358. $formTanslations = array(
  3359. 'isEmpty' => 'Form message',
  3360. );
  3361. $elementTanslations = array(
  3362. 'isEmpty' => 'Element message',
  3363. );
  3364. $formTranslate = new Zend_Translate('array', $formTanslations);
  3365. $elementTranslate = new Zend_Translate('array', $elementTanslations);
  3366. $this->form->setTranslator($formTranslate);
  3367. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3368. $this->form->addElement('text', 'bar', array('required'=>true));
  3369. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3370. $messages = $this->form->getMessages();
  3371. $this->assertEquals(2, count($messages));
  3372. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3373. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3374. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3375. $messages = $this->form->getMessages();
  3376. $this->assertEquals(2, count($messages));
  3377. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3378. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3379. }
  3380. /**
  3381. * @group ZF-9364
  3382. */
  3383. public function testElementTranslatorPreferredOverDefaultTranslator()
  3384. {
  3385. $defaultTranslations = array(
  3386. 'isEmpty' => 'Default message',
  3387. );
  3388. $formTranslations = array(
  3389. 'isEmpty' => 'Form message',
  3390. );
  3391. $elementTranslations = array(
  3392. 'isEmpty' => 'Element message',
  3393. );
  3394. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3395. $formTranslate = new Zend_Translate('array', $formTranslations);
  3396. $elementTranslate = new Zend_Translate('array', $elementTranslations);
  3397. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3398. $this->form->setTranslator($formTranslate);
  3399. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3400. $this->form->addElement('text', 'bar', array('required'=>true));
  3401. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3402. $messages = $this->form->getMessages();
  3403. $this->assertEquals(2, count($messages));
  3404. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3405. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3406. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3407. $messages = $this->form->getMessages();
  3408. $this->assertEquals(2, count($messages));
  3409. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3410. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3411. }
  3412. /**
  3413. * @group ZF-9540
  3414. */
  3415. public function testSubFormTranslatorPreferredOverDefaultTranslator()
  3416. {
  3417. $defaultTranslations = array('isEmpty' => 'Default message');
  3418. $subformTranslations = array('isEmpty' => 'SubForm message');
  3419. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3420. $subformTranslate = new Zend_Translate('array', $subformTranslations);
  3421. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3422. $this->form->addSubForm(new Zend_Form_SubForm(), 'subform');
  3423. $this->form->subform->setTranslator($subformTranslate);
  3424. $this->form->subform->addElement('text', 'foo', array('required'=>true));
  3425. $this->assertFalse($this->form->isValid(array('subform' => array('foo'=>''))));
  3426. $messages = $this->form->getMessages();
  3427. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3428. $this->assertFalse($this->form->isValidPartial(array('subform' => array('foo'=>''))));
  3429. $messages = $this->form->getMessages();
  3430. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3431. }
  3432. /**
  3433. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3434. *
  3435. * @link http://framework.zend.com/issues/browse/ZF-2794
  3436. * @return void
  3437. */
  3438. protected function _checkZf2794()
  3439. {
  3440. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3441. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3442. }
  3443. }
  3444. }
  3445. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  3446. {
  3447. }
  3448. class Zend_Form_FormTest_FormExtension extends Zend_Form
  3449. {
  3450. public function init()
  3451. {
  3452. $this->setDisableLoadDefaultDecorators(true);
  3453. }
  3454. }
  3455. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  3456. Zend_Form_FormTest::main();
  3457. }