FormTest.php 130 KB

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