FormTest.php 139 KB

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