FormTest.php 153 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298
  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-6741
  980. */
  981. public function testUseIdForDdTagByDefault()
  982. {
  983. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  984. ->bar->addElement('text', 'foo');
  985. $html = $this->form->setView($this->getView())->render();
  986. $this->assertRegexp('/<dd.*?bar-foo.*?>/', $html);
  987. }
  988. public function testUseIdForDtTagByDefault()
  989. {
  990. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  991. ->bar->addElement('text', 'foo');
  992. $html = $this->form->setView($this->getView())->render();
  993. $this->assertRegexp('/<dt.*?bar-foo.*?>/', $html);
  994. }
  995. /**
  996. * @group ZF-3146
  997. */
  998. public function testSetElementsBelongToShouldApplyToBothExistingAndFutureElements()
  999. {
  1000. $this->form->addElement('text', 'testBelongsTo');
  1001. $this->form->setElementsBelongTo('foo');
  1002. $this->assertEquals('foo', $this->form->testBelongsTo->getBelongsTo(), 'Failed determining testBelongsTo belongs to array');
  1003. $this->setupElements();
  1004. foreach ($this->form->getElements() as $element) {
  1005. $message = sprintf('Failed determining element "%s" belongs to foo', $element->getName());
  1006. $this->assertEquals('foo', $element->getBelongsTo(), $message);
  1007. }
  1008. }
  1009. /**
  1010. * @group ZF-3742
  1011. */
  1012. public function testElementsInDisplayGroupsShouldInheritFormElementsBelongToSetting()
  1013. {
  1014. $subForm = new Zend_Form_SubForm();
  1015. $subForm->addElements(array(
  1016. new Zend_Form_Element_Text('foo'),
  1017. new Zend_Form_Element_Text('bar'),
  1018. new Zend_Form_Element_Text('baz'),
  1019. new Zend_Form_Element_Text('bat'),
  1020. ))
  1021. ->addDisplayGroup(array('bar', 'baz'), 'barbaz');
  1022. $this->form->addSubForm($subForm, 'sub')
  1023. ->setElementsBelongTo('myform')
  1024. ->setView(new Zend_View);
  1025. $html = $this->form->render();
  1026. foreach (array('foo', 'bar', 'baz', 'bat') as $test) {
  1027. $this->assertContains('id="myform-sub-' . $test . '"', $html);
  1028. $this->assertContains('name="myform[sub][' . $test . ']"', $html);
  1029. }
  1030. }
  1031. public function testIsValidWithOneLevelElementsBelongTo()
  1032. {
  1033. $this->form->addElement('text', 'test')->test
  1034. ->addValidator('Identical', false, array('Test Value'));
  1035. $this->form->setElementsBelongTo('foo');
  1036. $data = array(
  1037. 'foo' => array(
  1038. 'test' => 'Test Value',
  1039. ),
  1040. );
  1041. $this->assertTrue($this->form->isValid($data));
  1042. }
  1043. public function testIsValidWithMultiLevelElementsBelongTo()
  1044. {
  1045. $this->form->addElement('text', 'test')->test
  1046. ->addValidator('Identical', false, array('Test Value'));
  1047. $this->form->setElementsBelongTo('foo[bar][zot]');
  1048. $data = array(
  1049. 'foo' => array(
  1050. 'bar' => array(
  1051. 'zot' => array(
  1052. 'test' => 'Test Value',
  1053. ),
  1054. ),
  1055. ),
  1056. );
  1057. $this->assertTrue($this->form->isValid($data));
  1058. }
  1059. // Sub forms
  1060. public function testCanAddAndRetrieveSingleSubForm()
  1061. {
  1062. $subForm = new Zend_Form_SubForm;
  1063. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1064. $this->form->addSubForm($subForm, 'page1');
  1065. $test = $this->form->getSubForm('page1');
  1066. $this->assertSame($subForm, $test);
  1067. }
  1068. public function testAddingSubFormSetsSubFormName()
  1069. {
  1070. $subForm = new Zend_Form_SubForm;
  1071. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1072. $this->form->addSubForm($subForm, 'page1');
  1073. $this->assertEquals('page1', $subForm->getName());
  1074. }
  1075. public function testGetSubFormReturnsNullForUnregisteredSubForm()
  1076. {
  1077. $this->assertNull($this->form->getSubForm('foo'));
  1078. }
  1079. public function testCanAddAndRetrieveMultipleSubForms()
  1080. {
  1081. $page1 = new Zend_Form_SubForm();
  1082. $page2 = new Zend_Form_SubForm();
  1083. $page3 = new Zend_Form_SubForm();
  1084. $this->form->addSubForms(array(
  1085. 'page1' => $page1,
  1086. array($page2, 'page2'),
  1087. array($page3, 'page3', 3)
  1088. ));
  1089. $subforms = $this->form->getSubForms();
  1090. $keys = array('page1', 'page2', 'page3');
  1091. $this->assertEquals($keys, array_keys($subforms));
  1092. $this->assertSame($page1, $subforms['page1']);
  1093. $this->assertSame($page2, $subforms['page2']);
  1094. $this->assertSame($page3, $subforms['page3']);
  1095. }
  1096. public function testSetSubFormsOverwritesExistingSubForms()
  1097. {
  1098. $this->testCanAddAndRetrieveMultipleSubForms();
  1099. $foo = new Zend_Form_SubForm();
  1100. $this->form->setSubForms(array('foo' => $foo));
  1101. $subforms = $this->form->getSubForms();
  1102. $keys = array('foo');
  1103. $this->assertEquals($keys, array_keys($subforms));
  1104. $this->assertSame($foo, $subforms['foo']);
  1105. }
  1106. public function testCanRemoveSingleSubForm()
  1107. {
  1108. $this->testCanAddAndRetrieveMultipleSubForms();
  1109. $this->assertTrue($this->form->removeSubForm('page2'));
  1110. $this->assertNull($this->form->getSubForm('page2'));
  1111. }
  1112. public function testRemoveSubFormReturnsFalseForNonexistantSubForm()
  1113. {
  1114. $this->assertFalse($this->form->removeSubForm('foo'));
  1115. }
  1116. public function testCanClearAllSubForms()
  1117. {
  1118. $this->testCanAddAndRetrieveMultipleSubForms();
  1119. $this->form->clearSubForms();
  1120. $subforms = $this->form->getSubForms();
  1121. $this->assertTrue(is_array($subforms));
  1122. $this->assertTrue(empty($subforms));
  1123. }
  1124. public function testOverloadingSubForms()
  1125. {
  1126. $foo = new Zend_Form_SubForm;
  1127. $this->form->addSubForm($foo, 'foo');
  1128. $this->assertTrue(isset($this->form->foo));
  1129. $subform = $this->form->foo;
  1130. $this->assertSame($foo, $subform);
  1131. unset($this->form->foo);
  1132. $this->assertFalse(isset($this->form->foo));
  1133. $bar = new Zend_Form_SubForm();
  1134. $this->form->bar = $bar;
  1135. $this->assertTrue(isset($this->form->bar));
  1136. $subform = $this->form->bar;
  1137. $this->assertSame($bar, $subform);
  1138. }
  1139. public function testCanSetDefaultsForSubFormElementsFromForm()
  1140. {
  1141. $subForm = new Zend_Form_SubForm;
  1142. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1143. $this->form->addSubForm($subForm, 'page1');
  1144. $data = array('foo' => 'foo value', 'bar' => 'bar value');
  1145. $this->form->setDefaults($data);
  1146. $this->assertEquals($data['foo'], $subForm->foo->getValue());
  1147. $this->assertEquals($data['bar'], $subForm->bar->getValue());
  1148. }
  1149. public function testCanSetDefaultsForSubFormElementsFromFormWithArray()
  1150. {
  1151. $subForm = new Zend_Form_SubForm;
  1152. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1153. $this->form->addSubForm($subForm, 'page1');
  1154. $data = array( 'page1' => array(
  1155. 'foo' => 'foo value',
  1156. 'bar' => 'bar value'
  1157. ));
  1158. $this->form->setDefaults($data);
  1159. $this->assertEquals($data['page1']['foo'], $subForm->foo->getValue());
  1160. $this->assertEquals($data['page1']['bar'], $subForm->bar->getValue());
  1161. }
  1162. public function testGetValuesReturnsSubFormValues()
  1163. {
  1164. $subForm = new Zend_Form_SubForm;
  1165. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1166. $subForm->foo->setValue('foo value');
  1167. $subForm->bar->setValue('bar value');
  1168. $this->form->addSubForm($subForm, 'page1');
  1169. $values = $this->form->getValues();
  1170. $this->assertTrue(isset($values['page1']));
  1171. $this->assertTrue(isset($values['page1']['foo']));
  1172. $this->assertTrue(isset($values['page1']['bar']));
  1173. $this->assertEquals($subForm->foo->getValue(), $values['page1']['foo']);
  1174. $this->assertEquals($subForm->bar->getValue(), $values['page1']['bar']);
  1175. }
  1176. public function testGetValuesReturnsSubFormValuesFromArrayToWhichElementsBelong()
  1177. {
  1178. $subForm = new Zend_Form_SubForm;
  1179. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1180. ->setElementsBelongTo('subform');
  1181. $subForm->foo->setValue('foo value');
  1182. $subForm->bar->setValue('bar value');
  1183. $this->form->addSubForm($subForm, 'page1');
  1184. $values = $this->form->getValues();
  1185. $this->assertTrue(isset($values['subform']), var_export($values, 1));
  1186. $this->assertTrue(isset($values['subform']['foo']));
  1187. $this->assertTrue(isset($values['subform']['bar']));
  1188. $this->assertEquals($subForm->foo->getValue(), $values['subform']['foo']);
  1189. $this->assertEquals($subForm->bar->getValue(), $values['subform']['bar']);
  1190. }
  1191. public function testGetValuesReturnsNestedSubFormValuesFromArraysToWhichElementsBelong()
  1192. {
  1193. $form = new Zend_Form();
  1194. $form->setElementsBelongTo('foobar');
  1195. $form->addElement('text', 'firstName')
  1196. ->getElement('firstName')
  1197. ->setRequired(true);
  1198. $form->addElement('text', 'lastName')
  1199. ->getElement('lastName')
  1200. ->setRequired(true);
  1201. $subForm = new Zend_Form_SubForm();
  1202. $subForm->setElementsBelongTo('baz[quux]');
  1203. $subForm->addElement('text', 'email')
  1204. ->getElement('email')->setRequired(true);
  1205. $subSubForm = new Zend_Form_SubForm();
  1206. $subSubForm->setElementsBelongTo('bat');
  1207. $subSubForm->addElement('checkbox', 'home')
  1208. ->getElement('home')->setRequired(true);
  1209. $subForm->addSubForm($subSubForm, 'subSub');
  1210. $form->addSubForm($subForm, 'sub')
  1211. ->addElement('submit', 'save', array('value' => 'submit', 'ignore' => true));
  1212. $data = array('foobar' => array(
  1213. 'firstName' => 'Mabel',
  1214. 'lastName' => 'Cow',
  1215. 'baz' => array(
  1216. 'quux' => array(
  1217. 'email' => 'mabel@cow.org',
  1218. 'bat' => array(
  1219. 'home' => 1,
  1220. )
  1221. ),
  1222. )
  1223. ));
  1224. $this->assertTrue($form->isValid($data));
  1225. $values = $form->getValues();
  1226. $this->assertEquals($data, $values);
  1227. }
  1228. public function testGetValueCanReturnSubFormValues()
  1229. {
  1230. $subForm = new Zend_Form_SubForm;
  1231. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  1232. $subForm->foo->setValue('foo value');
  1233. $subForm->bar->setValue('bar value');
  1234. $this->form->addSubForm($subForm, 'page1');
  1235. $values = $this->form->getValue('page1');
  1236. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1237. $this->assertTrue(isset($values['bar']));
  1238. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1239. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1240. }
  1241. public function testGetValueCanReturnSubFormValuesFromArrayToWhichElementsBelong()
  1242. {
  1243. $subForm = new Zend_Form_SubForm;
  1244. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'))
  1245. ->setElementsBelongTo('subform');
  1246. $subForm->foo->setValue('foo value');
  1247. $subForm->bar->setValue('bar value');
  1248. $this->form->addSubForm($subForm, 'page1');
  1249. $values = $this->form->getValue('subform');
  1250. $this->assertTrue(isset($values['foo']), var_export($values, 1));
  1251. $this->assertTrue(isset($values['bar']));
  1252. $this->assertEquals($subForm->foo->getValue(), $values['foo']);
  1253. $this->assertEquals($subForm->bar->getValue(), $values['bar']);
  1254. }
  1255. public function testIsValidCanValidateSubFormsWithArbitraryElementsBelong()
  1256. {
  1257. $subForm = new Zend_Form_SubForm();
  1258. $subForm->addElement('text', 'test')->test
  1259. ->setRequired(true)->addValidator('Identical', false, array('Test Value'));
  1260. $this->form->addSubForm($subForm, 'sub');
  1261. $this->form->setElementsBelongTo('foo[bar]');
  1262. $subForm->setElementsBelongTo('my[subform]');
  1263. $data = array(
  1264. 'foo' => array(
  1265. 'bar' => array(
  1266. 'my' => array(
  1267. 'subform' => array(
  1268. 'test' => 'Test Value',
  1269. ),
  1270. ),
  1271. ),
  1272. ),
  1273. );
  1274. $this->assertTrue($this->form->isValid($data));
  1275. }
  1276. public function testIsValidCanValidateNestedSubFormsWithArbitraryElementsBelong()
  1277. {
  1278. $subForm = new Zend_Form_SubForm();
  1279. $subForm->addElement('text', 'test1')->test1
  1280. ->setRequired(true)->addValidator('Identical', false, array('Test1 Value'));
  1281. $this->form->addSubForm($subForm, 'sub');
  1282. $subSubForm = new Zend_Form_SubForm();
  1283. $subSubForm->addElement('text', 'test2')->test2
  1284. ->setRequired(true)->addValidator('Identical', false, array('Test2 Value'));
  1285. $subForm->addSubForm($subSubForm, 'subSub');
  1286. $this->form->setElementsBelongTo('form[first]');
  1287. // Notice we skipped subForm, to mix manual and auto elementsBelongTo.
  1288. $subSubForm->setElementsBelongTo('subsubform[first]');
  1289. $data = array(
  1290. 'form' => array(
  1291. 'first' => array(
  1292. 'sub' => array(
  1293. 'test1' => 'Test1 Value',
  1294. 'subsubform' => array(
  1295. 'first' => array(
  1296. 'test2' => 'Test2 Value',
  1297. ),
  1298. ),
  1299. ),
  1300. ),
  1301. ),
  1302. );
  1303. $this->assertTrue($this->form->isValid($data));
  1304. }
  1305. /**
  1306. * @group ZF-9679
  1307. */
  1308. public function testIsValidDiscardsValidatedValues()
  1309. {
  1310. $this->form->addElement('text', 'foo');
  1311. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  1312. ->bar->addElement('text', 'foo')
  1313. ->foo->setAllowEmpty(true)
  1314. ->addValidator('Identical', true, '');
  1315. $this->assertTrue($this->form->isValid(array('foo' => 'foo Value')));
  1316. }
  1317. /**
  1318. * @group ZF-9666
  1319. */
  1320. public function testSetDefaultsDiscardsPopulatedValues()
  1321. {
  1322. $this->form->addElement('text', 'foo');
  1323. $this->form->addSubForm(new Zend_Form_SubForm(), 'bar')
  1324. ->bar->addElement('text', 'foo');
  1325. $this->form->populate(array('foo' => 'foo Value'));
  1326. $html = $this->form->setView($this->getView())
  1327. ->render();
  1328. $this->assertEquals(1, preg_match_all('/foo Value/', $html, $matches));
  1329. }
  1330. public function _setup9350()
  1331. {
  1332. $this->form->addSubForm(new Zend_Form_SubForm(), 'foo')
  1333. ->foo->setElementsBelongTo('foo[foo]') // foo[foo]
  1334. ->addSubForm(new Zend_Form_SubForm(), 'foo') // foo[foo][foo]
  1335. ->foo->setIsArray(false)
  1336. ->addElement('text', 'foo') // foo[foo][foo][foo]
  1337. ->foo->addValidator('Identical',
  1338. false,
  1339. array('foo Value'));
  1340. $this->form->foo->addSubForm(new Zend_Form_SubForm(), 'baz') // foo[foo][baz]
  1341. ->baz->setIsArray(false)
  1342. ->addSubForm(new Zend_Form_SubForm(), 'baz')
  1343. ->baz->setElementsBelongTo('baz[baz]') // foo[foo][baz][baz][baz]
  1344. ->addElement('text', 'baz') // foo[foo][baz][baz][baz][baz]
  1345. ->baz->addValidator('Identical',
  1346. false,
  1347. array('baz Value'));
  1348. // This is appending a different named SubForm and setting
  1349. // elementsBelongTo to a !isArray() Subform name from same level
  1350. $this->form->foo->addSubForm(new Zend_Form_SubForm(), 'quo')
  1351. ->quo->setElementsBelongTo('foo') // foo[foo][foo] !!!!
  1352. ->addElement('text', 'quo') // foo[foo][foo][quo]
  1353. ->quo->addValidator('Identical',
  1354. false,
  1355. array('quo Value'));
  1356. // This is setting elementsBelongTo point into the middle of
  1357. // a chain of another SubForms elementsBelongTo
  1358. $this->form->addSubForm(new Zend_Form_SubForm(), 'duh')
  1359. ->duh->setElementsBelongTo('foo[zoo]') // foo[zoo] !!!!
  1360. ->addElement('text', 'zoo') // foo[zoo][zoo]
  1361. ->zoo->addValidator('Identical',
  1362. false,
  1363. array('zoo Value'));
  1364. // This is !isArray SubForms Name equal to the last segment
  1365. // of another SubForms elementsBelongTo
  1366. $this->form->addSubForm(new Zend_Form_SubForm(), 'iek')
  1367. ->iek->setElementsBelongTo('foo') // foo !!!!
  1368. ->addSubForm(new Zend_Form_SubForm(), 'zoo') // foo[zoo] !!!!
  1369. ->zoo->setIsArray(false)
  1370. ->addElement('text', 'iek') // foo[zoo][iek]
  1371. ->iek->addValidator('Identical',
  1372. false,
  1373. array('iek Value'));
  1374. $data = array('valid' => array('foo' =>
  1375. array('foo' =>
  1376. array('foo' =>
  1377. array('foo' => 'foo Value',
  1378. 'quo' => 'quo Value'),
  1379. 'baz' =>
  1380. array('baz' =>
  1381. array('baz' =>
  1382. array('baz' => 'baz Value')))),
  1383. 'zoo' =>
  1384. array('zoo' => 'zoo Value',
  1385. 'iek' => 'iek Value'))),
  1386. 'invalid' => array('foo' =>
  1387. array('foo' =>
  1388. array('foo' =>
  1389. array('foo' => 'foo Invalid',
  1390. 'quo' => 'quo Value'),
  1391. 'baz' =>
  1392. array('baz' =>
  1393. array('baz' =>
  1394. array('baz' => 'baz Value')))),
  1395. 'zoo' =>
  1396. array('zoo' => 'zoo Value',
  1397. 'iek' => 'iek Invalid'))),
  1398. 'partial' => array('foo' =>
  1399. array('foo' =>
  1400. array('baz' =>
  1401. array('baz' =>
  1402. array('baz' =>
  1403. array('baz' => 'baz Value'))),
  1404. 'foo' =>
  1405. array('quo' => 'quo Value')),
  1406. 'zoo' =>
  1407. array('zoo' => 'zoo Value'))));
  1408. return $data;
  1409. }
  1410. public function testIsValidEqualSubFormAndElementName()
  1411. {
  1412. $data = $this->_setup9350();
  1413. $this->assertTrue($this->form->isValid($data['valid']));
  1414. }
  1415. public function testIsValidPartialEqualSubFormAndElementName()
  1416. {
  1417. $data = $this->_setup9350();
  1418. $this->assertTrue($this->form->isValidPartial($data['partial']));
  1419. }
  1420. public function testPopulateWithElementsBelongTo()
  1421. {
  1422. $data = $this->_setup9350();
  1423. $this->form->setView($this->getView())->populate($data['valid']);
  1424. $html = $this->form->render();
  1425. $this->assertRegexp('/value=.foo Value./', $html);
  1426. $this->assertRegexp('/value=.baz Value./', $html);
  1427. $this->assertRegexp('/value=.quo Value./', $html);
  1428. $this->assertRegexp('/value=.zoo Value./', $html);
  1429. $this->assertRegexp('/value=.iek Value./', $html);
  1430. }
  1431. public function testGetValidValuesWithElementsBelongTo()
  1432. {
  1433. $data = $this->_setup9350();
  1434. $this->assertSame($this->form->getValidValues($data['invalid']), $data['partial']);
  1435. }
  1436. public function testGetErrorsWithElementsBelongTo()
  1437. {
  1438. $data = $this->_setup9350();
  1439. $this->form->isValid($data['invalid']);
  1440. $errors = $this->form->getErrors();
  1441. $this->assertTrue(isset($errors['foo']['foo']['foo']['foo']));
  1442. $this->assertTrue(isset($errors['foo']['zoo']['iek']));
  1443. }
  1444. public function testGetValuesWithElementsBelongTo()
  1445. {
  1446. $data = $this->_setup9350();
  1447. $this->form->populate($data['valid']);
  1448. $this->assertSame($this->form->getValues(), $data['valid']);
  1449. }
  1450. public function testGetMessagesWithElementsBelongTo()
  1451. {
  1452. $data = $this->_setup9350();
  1453. $this->form->isValid($data['invalid']);
  1454. $msgs = $this->form->getMessages();
  1455. $this->assertTrue(isset($msgs['foo']['foo']['foo']['foo']));
  1456. $this->assertTrue(isset($msgs['foo']['zoo']['iek']));
  1457. }
  1458. public function _setup9401()
  1459. {
  1460. $sub0 = 0;
  1461. $this->form->addSubForm(new Zend_Form_SubForm(), $sub0)
  1462. ->$sub0->setElementsBelongTo('f[2]')
  1463. ->addElement('text', 'foo')
  1464. ->foo->addValidator('Identical',
  1465. false,
  1466. array('foo Value'));
  1467. $this->form->$sub0->addSubForm(new Zend_Form_SubForm(), $sub0)
  1468. ->$sub0->addElement('text', 'quo')
  1469. ->quo->addValidator('Identical',
  1470. false,
  1471. array('quo Value'));
  1472. $data = array('valid' => array('f' =>
  1473. array(2 =>
  1474. array('foo' => 'foo Value',
  1475. 0 =>
  1476. array('quo' => 'quo Value')))),
  1477. 'invalid' => array('f' =>
  1478. array(2 =>
  1479. array('foo' => 'foo Invalid',
  1480. 0 =>
  1481. array('quo' => 'quo Value')))),
  1482. 'partial' => array('f' =>
  1483. array(2 =>
  1484. array(0 =>
  1485. array('quo' => 'quo Value')))));
  1486. return $data;
  1487. }
  1488. public function testGetErrorsNumericalSubForms()
  1489. {
  1490. $data = $this->_setup9401();
  1491. $this->form->isValid($data['invalid']);
  1492. $err = $this->form->getErrors();
  1493. $this->assertTrue(is_array($err['f'][2]['foo']) && !empty($err['f'][2]['foo']));
  1494. }
  1495. public function testGetMessagesNumericalSubForms()
  1496. {
  1497. $data = $this->_setup9401();
  1498. $this->form->isValid($data['invalid']);
  1499. $msg = $this->form->getMessages();
  1500. $this->assertTrue(is_array($msg['f'][2]['foo']) && !empty($msg['f'][2]['foo']));
  1501. }
  1502. public function testGetValuesNumericalSubForms()
  1503. {
  1504. $data = $this->_setup9401();
  1505. $this->form->populate($data['valid']);
  1506. $this->assertEquals($this->form->getValues(), $data['valid']);
  1507. }
  1508. public function testGetValidValuesNumericalSubForms()
  1509. {
  1510. $data = $this->_setup9401();
  1511. $this->assertEquals($this->form->getValidValues($data['invalid']), $data['partial']);
  1512. }
  1513. public function _setup9607()
  1514. {
  1515. $this->form->addElement('text', 'foo')
  1516. ->foo->setBelongsTo('bar[quo]')
  1517. ->setRequired(true)
  1518. ->addValidator('Identical',
  1519. false,
  1520. 'foo Value');
  1521. $this->form->addElement('text', 'quo')
  1522. ->quo->setBelongsTo('bar[quo]')
  1523. ->addValidator('Identical',
  1524. false,
  1525. 'quo Value');
  1526. $data = array('valid' => array('bar' =>
  1527. array('quo' =>
  1528. array('foo' => 'foo Value',
  1529. 'quo' => 'quo Value'))),
  1530. 'invalid' => array('bar' =>
  1531. array('quo' =>
  1532. array('foo' => 'foo Invalid',
  1533. 'quo' => 'quo Value'))),
  1534. 'partial' => array('bar' =>
  1535. array('quo' =>
  1536. array('quo' => 'quo Value'))));
  1537. return $data;
  1538. }
  1539. public function testIsValidWithBelongsTo()
  1540. {
  1541. $data = $this->_setup9607();
  1542. $this->assertTrue($this->form->isValid($data['valid']));
  1543. }
  1544. public function testIsValidPartialWithBelongsTo()
  1545. {
  1546. $data = $this->_setup9607();
  1547. $this->assertTrue($this->form->isValidPartial($data['valid']));
  1548. $this->assertSame('foo Value', $this->form->foo->getValue());
  1549. }
  1550. public function testPopulateWithBelongsTo()
  1551. {
  1552. $data = $this->_setup9607();
  1553. $this->form->populate($data['valid']);
  1554. $this->assertSame('foo Value', $this->form->foo->getValue());
  1555. }
  1556. public function testGetValuesWithBelongsTo()
  1557. {
  1558. $data = $this->_setup9607();
  1559. $this->form->populate($data['valid']);
  1560. $this->assertSame($data['valid'], $this->form->getValues());
  1561. }
  1562. public function testGetValidValuesWithBelongsTo()
  1563. {
  1564. $data = $this->_setup9607();
  1565. $this->assertSame($data['partial'], $this->form->getValidValues($data['invalid']));
  1566. }
  1567. // Display groups
  1568. public function testCanAddAndRetrieveSingleDisplayGroups()
  1569. {
  1570. $this->testCanAddAndRetrieveMultipleElements();
  1571. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  1572. $group = $this->form->getDisplayGroup('barbat');
  1573. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1574. $elements = $group->getElements();
  1575. $expected = array('bar' => $this->form->bar, 'bat' => $this->form->bat);
  1576. $this->assertEquals($expected, $elements);
  1577. }
  1578. public function testDisplayGroupsMustContainAtLeastOneElement()
  1579. {
  1580. try {
  1581. $this->form->addDisplayGroup(array(), 'foo');
  1582. $this->fail('Empty display group should raise exception');
  1583. } catch (Zend_Form_Exception $e) {
  1584. $this->assertContains('No valid elements', $e->getMessage());
  1585. }
  1586. }
  1587. public function testCanAddAndRetrieveMultipleDisplayGroups()
  1588. {
  1589. $this->testCanAddAndRetrieveMultipleElements();
  1590. $this->form->addDisplayGroups(array(
  1591. array(array('bar', 'bat'), 'barbat'),
  1592. 'foobaz' => array('baz', 'foo')
  1593. ));
  1594. $groups = $this->form->getDisplayGroups();
  1595. $expected = array(
  1596. 'barbat' => array('bar' => $this->form->bar, 'bat' => $this->form->bat),
  1597. 'foobaz' => array('baz' => $this->form->baz, 'foo' => $this->form->foo),
  1598. );
  1599. foreach ($groups as $group) {
  1600. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1601. }
  1602. $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
  1603. $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
  1604. }
  1605. public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
  1606. {
  1607. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1608. $this->form->setDisplayGroups(array('foobar' => array('bar', 'foo')));
  1609. $groups = $this->form->getDisplayGroups();
  1610. $expected = array('bar' => $this->form->bar, 'foo' => $this->form->foo);
  1611. $this->assertEquals(1, count($groups));
  1612. $this->assertTrue(isset($groups['foobar']));
  1613. $this->assertEquals($expected, $groups['foobar']->getElements());
  1614. }
  1615. public function testCanRemoveSingleDisplayGroup()
  1616. {
  1617. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1618. $this->assertTrue($this->form->removeDisplayGroup('barbat'));
  1619. $this->assertNull($this->form->getDisplayGroup('barbat'));
  1620. }
  1621. public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
  1622. {
  1623. $this->assertFalse($this->form->removeDisplayGroup('bogus'));
  1624. }
  1625. public function testCanClearAllDisplayGroups()
  1626. {
  1627. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1628. $this->form->clearDisplayGroups();
  1629. $groups = $this->form->getDisplayGroups();
  1630. $this->assertTrue(is_array($groups));
  1631. $this->assertTrue(empty($groups));
  1632. }
  1633. public function testOverloadingDisplayGroups()
  1634. {
  1635. $this->testCanAddAndRetrieveMultipleElements();
  1636. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1637. $this->assertTrue(isset($this->form->foobar));
  1638. $group = $this->form->foobar;
  1639. $expected = array('foo' => $this->form->foo, 'bar' => $this->form->bar);
  1640. $this->assertEquals($expected, $group->getElements());
  1641. unset($this->form->foobar);
  1642. $this->assertFalse(isset($this->form->foobar));
  1643. $this->form->barbaz = array('bar', 'baz');
  1644. $this->assertTrue(isset($this->form->barbaz));
  1645. $group = $this->form->barbaz;
  1646. $expected = array('bar' => $this->form->bar, 'baz' => $this->form->baz);
  1647. $this->assertSame($expected, $group->getElements());
  1648. }
  1649. public function testDefaultDisplayGroupClassExists()
  1650. {
  1651. $this->assertEquals('Zend_Form_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1652. }
  1653. public function testCanSetDefaultDisplayGroupClass()
  1654. {
  1655. $this->testDefaultDisplayGroupClassExists();
  1656. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1657. $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1658. }
  1659. public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
  1660. {
  1661. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1662. $this->setupElements();
  1663. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1664. $displayGroup = $this->form->getDisplayGroup('foobar');
  1665. $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup);
  1666. }
  1667. public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
  1668. {
  1669. $this->setupElements();
  1670. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
  1671. $this->assertTrue($this->form->foobar instanceof Zend_Form_FormTest_DisplayGroup);
  1672. }
  1673. /**
  1674. * @group ZF-3254
  1675. */
  1676. public function testAddingDisplayGroupShouldPassOptions()
  1677. {
  1678. $this->testCanAddAndRetrieveMultipleElements();
  1679. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
  1680. $group = $this->form->getDisplayGroup('barbat');
  1681. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1682. $decorators = $group->getDecorators();
  1683. $this->assertTrue(is_array($decorators));
  1684. $this->assertTrue(empty($decorators));
  1685. }
  1686. // Processing
  1687. public function testPopulateProxiesToSetDefaults()
  1688. {
  1689. $this->testCanAddAndRetrieveMultipleElements();
  1690. $values = array(
  1691. 'foo' => 'foovalue',
  1692. 'bar' => 'barvalue',
  1693. 'baz' => 'bazvalue',
  1694. 'bat' => 'batvalue'
  1695. );
  1696. $this->form->populate($values);
  1697. $test = $this->form->getValues();
  1698. $elements = $this->form->getElements();
  1699. foreach (array_keys($values) as $name) {
  1700. $this->assertEquals($values[$name], $test[$name]);
  1701. }
  1702. }
  1703. public function setupElements()
  1704. {
  1705. $foo = new Zend_Form_Element_Text('foo');
  1706. $foo->addValidator('NotEmpty')
  1707. ->addValidator('Alpha');
  1708. $bar = new Zend_Form_Element_Text('bar');
  1709. $bar->addValidator('NotEmpty')
  1710. ->addValidator('Digits');
  1711. $baz = new Zend_Form_Element_Text('baz');
  1712. $baz->addValidator('NotEmpty')
  1713. ->addValidator('Alnum');
  1714. $this->form->addElements(array($foo, $bar, $baz));
  1715. $this->elementValues = array(
  1716. 'foo' => 'fooBarBAZ',
  1717. 'bar' => '123456789',
  1718. 'baz' => 'foo123BAR',
  1719. );
  1720. }
  1721. public function testIsValidShouldThrowExceptionWithNonArrayArgument()
  1722. {
  1723. try {
  1724. $this->form->isValid(true);
  1725. $this->fail('isValid() should raise exception with non-array argument');
  1726. } catch (Zend_Form_Exception $e) {
  1727. $this->assertContains('expects an array', $e->getMessage());
  1728. }
  1729. }
  1730. public function testCanValidateFullFormContainingOnlyElements()
  1731. {
  1732. $this->_checkZf2794();
  1733. $this->setupElements();
  1734. $this->assertTrue($this->form->isValid($this->elementValues));
  1735. $values = array(
  1736. 'foo' => '12345',
  1737. 'bar' => 'abc',
  1738. 'baz' => 'abc-123'
  1739. );
  1740. $this->assertFalse($this->form->isValid($values));
  1741. $validator = $this->form->foo->getValidator('alpha');
  1742. $this->assertEquals('12345', $validator->value);
  1743. $validator = $this->form->bar->getValidator('digits');
  1744. $this->assertEquals('abc', $validator->value);
  1745. $validator = $this->form->baz->getValidator('alnum');
  1746. $this->assertEquals('abc-123', $validator->value);
  1747. }
  1748. public function testValidationTakesElementRequiredFlagsIntoAccount()
  1749. {
  1750. $this->_checkZf2794();
  1751. $this->setupElements();
  1752. $this->assertTrue($this->form->isValid(array()));
  1753. $this->form->getElement('foo')->setRequired(true);
  1754. $this->assertTrue($this->form->isValid(array(
  1755. 'foo' => 'abc',
  1756. 'baz' => 'abc123'
  1757. )));
  1758. $this->assertFalse($this->form->isValid(array(
  1759. 'baz' => 'abc123'
  1760. )));
  1761. }
  1762. public function testCanValidatePartialFormContainingOnlyElements()
  1763. {
  1764. $this->_checkZf2794();
  1765. $this->setupElements();
  1766. $this->form->getElement('foo')->setRequired(true);
  1767. $this->form->getElement('bar')->setRequired(true);
  1768. $this->form->getElement('baz')->setRequired(true);
  1769. $this->assertTrue($this->form->isValidPartial(array(
  1770. 'foo' => 'abc',
  1771. 'baz' => 'abc123'
  1772. )));
  1773. $this->assertFalse($this->form->isValidPartial(array(
  1774. 'foo' => '123',
  1775. 'baz' => 'abc-123'
  1776. )));
  1777. }
  1778. public function setupSubForm()
  1779. {
  1780. $subForm = new Zend_Form_SubForm();
  1781. $foo = new Zend_Form_Element_Text('subfoo');
  1782. $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
  1783. $bar = new Zend_Form_Element_Text('subbar');
  1784. $bar->addValidators(array('NotEmpty', 'Digits'));
  1785. $baz = new Zend_Form_Element_Text('subbaz');
  1786. $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
  1787. $subForm->addElements(array($foo, $bar, $baz));
  1788. $this->form->addSubForm($subForm, 'sub');
  1789. }
  1790. public function testFullDataArrayUsedToValidateSubFormByDefault()
  1791. {
  1792. $this->_checkZf2794();
  1793. $this->setupElements();
  1794. $this->setupSubForm();
  1795. $data = array(
  1796. 'foo' => 'abcdef',
  1797. 'bar' => '123456',
  1798. 'baz' => '123abc',
  1799. 'subfoo' => 'abcdef',
  1800. 'subbar' => '123456',
  1801. 'subbaz' => '123abc',
  1802. );
  1803. $this->assertTrue($this->form->isValid($data));
  1804. $data = array(
  1805. 'foo' => 'abcdef',
  1806. 'bar' => '123456',
  1807. 'baz' => '123abc',
  1808. 'subfoo' => '123',
  1809. 'subbar' => 'abc',
  1810. 'subbaz' => '123-abc',
  1811. );
  1812. $this->assertFalse($this->form->isValid($data));
  1813. $data = array(
  1814. 'foo' => 'abcdef',
  1815. 'bar' => '123456',
  1816. 'baz' => '123abc',
  1817. 'subfoo' => 'abc',
  1818. 'subbaz' => '123abc',
  1819. );
  1820. $this->assertTrue($this->form->isValid($data));
  1821. $data = array(
  1822. 'foo' => 'abcdef',
  1823. 'bar' => '123456',
  1824. 'baz' => '123abc',
  1825. 'subbar' => '123',
  1826. 'subbaz' => '123abc',
  1827. );
  1828. $this->assertFalse($this->form->isValid($data));
  1829. }
  1830. public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
  1831. {
  1832. $this->_checkZf2794();
  1833. $this->setupElements();
  1834. $this->setupSubForm();
  1835. $data = array(
  1836. 'foo' => 'abcdef',
  1837. 'bar' => '123456',
  1838. 'baz' => '123abc',
  1839. 'sub' => array(
  1840. 'subfoo' => 'abcdef',
  1841. 'subbar' => '123456',
  1842. 'subbaz' => '123abc',
  1843. ),
  1844. );
  1845. $this->assertTrue($this->form->isValid($data));
  1846. $data = array(
  1847. 'foo' => 'abcdef',
  1848. 'bar' => '123456',
  1849. 'baz' => '123abc',
  1850. 'sub' => array(
  1851. 'subfoo' => '123',
  1852. 'subbar' => 'abc',
  1853. 'subbaz' => '123-abc',
  1854. )
  1855. );
  1856. $this->assertFalse($this->form->isValid($data));
  1857. $data = array(
  1858. 'foo' => 'abcdef',
  1859. 'bar' => '123456',
  1860. 'baz' => '123abc',
  1861. 'sub' => array(
  1862. 'subfoo' => 'abc',
  1863. 'subbaz' => '123abc',
  1864. )
  1865. );
  1866. $this->assertTrue($this->form->isValid($data));
  1867. $data = array(
  1868. 'foo' => 'abcdef',
  1869. 'bar' => '123456',
  1870. 'baz' => '123abc',
  1871. 'sub' => array(
  1872. 'subbar' => '123',
  1873. 'subbaz' => '123abc',
  1874. )
  1875. );
  1876. $this->assertFalse($this->form->isValid($data));
  1877. }
  1878. public function testCanValidateNestedFormsWithElementsBelongingToArrays()
  1879. {
  1880. $form = new Zend_Form();
  1881. $form->setElementsBelongTo('foobar');
  1882. $form->addElement('text', 'firstName')
  1883. ->getElement('firstName')
  1884. ->setRequired(true);
  1885. $form->addElement('text', 'lastName')
  1886. ->getElement('lastName')
  1887. ->setRequired(true);
  1888. $subForm = new Zend_Form_SubForm();
  1889. $subForm->setElementsBelongTo('baz');
  1890. $subForm->addElement('text', 'email')
  1891. ->getElement('email')->setRequired(true);
  1892. $subSubForm = new Zend_Form_SubForm();
  1893. $subSubForm->setElementsBelongTo('bat');
  1894. $subSubForm->addElement('checkbox', 'home')
  1895. ->getElement('home')->setRequired(true);
  1896. $subForm->addSubForm($subSubForm, 'subSub');
  1897. $form->addSubForm($subForm, 'sub')
  1898. ->addElement('submit', 'save', array('value' => 'submit'));
  1899. $data = array('foobar' => array(
  1900. 'firstName' => 'Mabel',
  1901. 'lastName' => 'Cow',
  1902. 'baz' => array(
  1903. 'email' => 'mabel@cow.org',
  1904. 'bat' => array(
  1905. 'home' => 1,
  1906. )
  1907. )
  1908. ));
  1909. $this->assertTrue($form->isValid($data));
  1910. $this->assertEquals('Mabel', $form->firstName->getValue());
  1911. $this->assertEquals('Cow', $form->lastName->getValue());
  1912. $this->assertEquals('mabel@cow.org', $form->sub->email->getValue());
  1913. $this->assertEquals(1, $form->sub->subSub->home->getValue());
  1914. }
  1915. public function testCanValidatePartialFormContainingSubForms()
  1916. {
  1917. $this->_checkZf2794();
  1918. $this->setupElements();
  1919. $this->setupSubForm();
  1920. $data = array(
  1921. 'subfoo' => 'abcdef',
  1922. 'subbar' => '123456',
  1923. );
  1924. $this->assertTrue($this->form->isValidPartial($data));
  1925. $data = array(
  1926. 'foo' => 'abcdef',
  1927. 'baz' => '123abc',
  1928. 'sub' => array(
  1929. 'subbar' => '123',
  1930. )
  1931. );
  1932. $this->assertTrue($this->form->isValidPartial($data));
  1933. $data = array(
  1934. 'foo' => 'abcdef',
  1935. 'bar' => '123456',
  1936. 'baz' => '123abc',
  1937. 'sub' => array(
  1938. 'subfoo' => '123',
  1939. )
  1940. );
  1941. $this->assertFalse($this->form->isValidPartial($data));
  1942. }
  1943. public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
  1944. {
  1945. $this->_checkZf2794();
  1946. $form = new Zend_Form();
  1947. $form->setElementsBelongTo('foobar');
  1948. $form->addElement('text', 'firstName')
  1949. ->getElement('firstName')
  1950. ->setRequired(false);
  1951. $form->addElement('text', 'lastName')
  1952. ->getElement('lastName')
  1953. ->setRequired(true);
  1954. $subForm = new Zend_Form_SubForm();
  1955. $subForm->setElementsBelongTo('baz');
  1956. $subForm->addElement('text', 'email')
  1957. ->getElement('email')
  1958. ->setRequired(true)
  1959. ->addValidator('NotEmpty');
  1960. $subSubForm = new Zend_Form_SubForm();
  1961. $subSubForm->setElementsBelongTo('bat');
  1962. $subSubForm->addElement('checkbox', 'home')
  1963. ->getElement('home')
  1964. ->setRequired(true)
  1965. ->addValidator('InArray', false, array(array('1')));
  1966. $subForm->addSubForm($subSubForm, 'subSub');
  1967. $form->addSubForm($subForm, 'sub')
  1968. ->addElement('submit', 'save', array('value' => 'submit'));
  1969. $data = array('foobar' => array(
  1970. 'lastName' => 'Cow',
  1971. ));
  1972. $this->assertTrue($form->isValidPartial($data));
  1973. $this->assertEquals('Cow', $form->lastName->getValue());
  1974. $firstName = $form->firstName->getValue();
  1975. $email = $form->sub->email->getValue();
  1976. $home = $form->sub->subSub->home->getValue();
  1977. $this->assertTrue(empty($firstName));
  1978. $this->assertTrue(empty($email));
  1979. $this->assertTrue(empty($home));
  1980. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1981. $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
  1982. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1983. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1984. $messages = $form->getMessages();
  1985. $this->assertFalse(empty($messages));
  1986. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
  1987. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
  1988. }
  1989. public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
  1990. {
  1991. $this->_checkZf2794();
  1992. $form = new Zend_Form();
  1993. $form->setElementsBelongTo('foo[bar]');
  1994. $form->addElement('text', 'firstName')
  1995. ->getElement('firstName')
  1996. ->setRequired(false);
  1997. $form->addElement('text', 'lastName')
  1998. ->getElement('lastName')
  1999. ->setRequired(true);
  2000. $subForm = new Zend_Form_SubForm();
  2001. $subForm->setElementsBelongTo('baz');
  2002. $subForm->addElement('text', 'email')
  2003. ->getElement('email')
  2004. ->setRequired(true)
  2005. ->addValidator('NotEmpty');
  2006. $subSubForm = new Zend_Form_SubForm();
  2007. $subSubForm->setElementsBelongTo('bat[quux]');
  2008. $subSubForm->addElement('checkbox', 'home')
  2009. ->getElement('home')
  2010. ->setRequired(true)
  2011. ->addValidator('InArray', false, array(array('1')));
  2012. $subForm->addSubForm($subSubForm, 'subSub');
  2013. $form->addSubForm($subForm, 'sub')
  2014. ->addElement('submit', 'save', array('value' => 'submit'));
  2015. $data = array('foo' => array(
  2016. 'bar' => array(
  2017. 'lastName' => 'Cow',
  2018. ),
  2019. ));
  2020. $this->assertTrue($form->isValidPartial($data));
  2021. $this->assertEquals('Cow', $form->lastName->getValue());
  2022. $firstName = $form->firstName->getValue();
  2023. $email = $form->sub->email->getValue();
  2024. $home = $form->sub->subSub->home->getValue();
  2025. $this->assertTrue(empty($firstName));
  2026. $this->assertTrue(empty($email));
  2027. $this->assertTrue(empty($home));
  2028. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  2029. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  2030. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  2031. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  2032. }
  2033. public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
  2034. {
  2035. $this->_checkZf2794();
  2036. $form = new Zend_Form();
  2037. $form->setElementsBelongTo('foo[bar]');
  2038. $form->addElement('text', 'firstName')
  2039. ->getElement('firstName')
  2040. ->setRequired(false);
  2041. $form->addElement('text', 'lastName')
  2042. ->getElement('lastName')
  2043. ->setRequired(true);
  2044. $subForm = new Zend_Form_SubForm();
  2045. $subForm->setElementsBelongTo('baz');
  2046. $subForm->addElement('text', 'email')
  2047. ->getElement('email')
  2048. ->setRequired(true)
  2049. ->addValidator('NotEmpty');
  2050. $subSubForm = new Zend_Form_SubForm();
  2051. $subSubForm->setElementsBelongTo('bat[quux]');
  2052. $subSubForm->addElement('checkbox', 'home')
  2053. ->getElement('home')
  2054. ->setRequired(true)
  2055. ->addValidator('InArray', false, array(array('1')));
  2056. $subForm->addSubForm($subSubForm, 'subSub');
  2057. $form->addSubForm($subForm, 'sub')
  2058. ->addElement('submit', 'save', array('value' => 'submit'));
  2059. $data = array('foo' => array(
  2060. 'bar' => array(
  2061. 'lastName' => 'Cow',
  2062. ),
  2063. ));
  2064. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  2065. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  2066. $form->isValidPartial($data);
  2067. $messages = $form->getMessages();
  2068. $this->assertFalse(empty($messages));
  2069. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
  2070. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
  2071. }
  2072. public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
  2073. {
  2074. $this->setupElements();
  2075. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2076. $this->assertTrue($this->form->isValid($this->elementValues));
  2077. $this->assertFalse($this->form->isValid(array(
  2078. 'foo' => '123',
  2079. 'bar' => 'abc',
  2080. 'baz' => 'abc-123'
  2081. )));
  2082. }
  2083. public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
  2084. {
  2085. $this->setupElements();
  2086. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2087. $this->assertTrue($this->form->isValid(array(
  2088. 'foo' => 'abc',
  2089. 'baz' => 'abc123'
  2090. )));
  2091. $this->assertFalse($this->form->isValid(array(
  2092. 'foo' => '123',
  2093. 'baz' => 'abc-123'
  2094. )));
  2095. }
  2096. public function testProcessAjaxReturnsJsonTrueForValidForm()
  2097. {
  2098. $this->setupElements();
  2099. $return = $this->form->processAjax($this->elementValues);
  2100. $this->assertTrue(Zend_Json::decode($return));
  2101. }
  2102. public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
  2103. {
  2104. $this->setupElements();
  2105. $data = array('foo' => 'abcdef', 'baz' => 'abc123');
  2106. $return = $this->form->processAjax($data);
  2107. $this->assertTrue(Zend_Json::decode($return));
  2108. }
  2109. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
  2110. {
  2111. $this->setupElements();
  2112. $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
  2113. $return = Zend_Json::decode($this->form->processAjax($data));
  2114. $this->assertTrue(is_array($return));
  2115. $this->assertEquals(array_keys($data), array_keys($return));
  2116. }
  2117. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
  2118. {
  2119. $this->setupElements();
  2120. $data = array('baz' => 'abc-123');
  2121. $return = Zend_Json::decode($this->form->processAjax($data));
  2122. $this->assertTrue(is_array($return));
  2123. $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
  2124. }
  2125. public function testPersistDataStoresDataInSession()
  2126. {
  2127. $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
  2128. }
  2129. public function testCanCheckIfErrorsAreRegistered()
  2130. {
  2131. $this->assertFalse($this->form->isErrors());
  2132. $this->testCanValidateFullFormContainingOnlyElements();
  2133. $this->assertTrue($this->form->isErrors());
  2134. }
  2135. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  2136. {
  2137. $this->_checkZf2794();
  2138. $this->testCanValidateFullFormContainingOnlyElements();
  2139. $codes = $this->form->getErrors();
  2140. $keys = array('foo', 'bar', 'baz');
  2141. $this->assertEquals($keys, array_keys($codes));
  2142. }
  2143. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  2144. {
  2145. $this->_checkZf2794();
  2146. $this->testCanValidateFullFormContainingOnlyElements();
  2147. $codes = $this->form->getErrors();
  2148. $keys = array('foo', 'bar', 'baz');
  2149. $errors = $this->form->getErrors('foo');
  2150. $foo = $this->form->foo;
  2151. $this->assertEquals($foo->getErrors(), $errors);
  2152. }
  2153. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  2154. {
  2155. $this->_checkZf2794();
  2156. $this->testCanValidateFullFormContainingOnlyElements();
  2157. $codes = $this->form->getMessages();
  2158. $keys = array('foo', 'bar', 'baz');
  2159. $this->assertEquals($keys, array_keys($codes));
  2160. }
  2161. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  2162. {
  2163. $this->_checkZf2794();
  2164. $this->testCanValidateFullFormContainingOnlyElements();
  2165. $codes = $this->form->getMessages();
  2166. $keys = array('foo', 'bar', 'baz');
  2167. $messages = $this->form->getMessages('foo');
  2168. $foo = $this->form->foo;
  2169. $this->assertEquals($foo->getMessages(), $messages);
  2170. }
  2171. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  2172. {
  2173. $this->_checkZf2794();
  2174. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2175. $codes = $this->form->getErrors();
  2176. $this->assertTrue(array_key_exists('sub', $codes));
  2177. $this->assertTrue(is_array($codes['sub']));
  2178. $keys = array('subfoo', 'subbar', 'subbaz');
  2179. $this->assertEquals($keys, array_keys($codes['sub']));
  2180. }
  2181. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  2182. {
  2183. $this->_checkZf2794();
  2184. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2185. $codes = $this->form->getErrors('sub');
  2186. $this->assertTrue(is_array($codes));
  2187. $this->assertFalse(empty($codes));
  2188. $keys = array('subfoo', 'subbar', 'subbaz');
  2189. $this->assertEquals($keys, array_keys($codes));
  2190. }
  2191. public function testGetErrorsHonorsElementsBelongTo()
  2192. {
  2193. $this->_checkZf2794();
  2194. $subForm = new Zend_Form_SubForm();
  2195. $subForm->setElementsBelongTo('foo[bar]');
  2196. $subForm->addElement('text', 'test')->test
  2197. ->setRequired(true);
  2198. $this->form->addSubForm($subForm, 'sub');
  2199. $data = array('foo' => array(
  2200. 'bar' => array(
  2201. 'test' => '',
  2202. ),
  2203. ));
  2204. $this->form->isValid($data);
  2205. $codes = $this->form->getErrors();
  2206. $this->assertFalse(empty($codes['foo']['bar']['test']));
  2207. }
  2208. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  2209. {
  2210. $this->_checkZf2794();
  2211. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2212. $data = array(
  2213. 'foo' => 'abcdef',
  2214. 'bar' => '123456',
  2215. 'baz' => '123abc',
  2216. 'subfoo' => '123',
  2217. 'subbar' => 'abc',
  2218. 'subbaz' => '123-abc',
  2219. );
  2220. $this->assertFalse($this->form->isValid($data));
  2221. $codes = $this->form->getMessages();
  2222. $this->assertTrue(array_key_exists('sub', $codes));
  2223. $this->assertTrue(is_array($codes['sub']));
  2224. $keys = array('subfoo', 'subbar', 'subbaz');
  2225. $this->assertEquals($keys, array_keys($codes['sub']));
  2226. }
  2227. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  2228. {
  2229. $this->_checkZf2794();
  2230. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  2231. $data = array(
  2232. 'foo' => 'abcdef',
  2233. 'bar' => '123456',
  2234. 'baz' => '123abc',
  2235. 'subfoo' => '123',
  2236. 'subbar' => 'abc',
  2237. 'subbaz' => '123-abc',
  2238. );
  2239. $this->assertFalse($this->form->isValid($data));
  2240. $codes = $this->form->getMessages('sub');
  2241. $this->assertTrue(is_array($codes));
  2242. $this->assertFalse(empty($codes));
  2243. $keys = array('subfoo', 'subbar', 'subbaz');
  2244. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  2245. }
  2246. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  2247. {
  2248. $this->_checkZf2794();
  2249. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2250. $translate = new Zend_Translate('array', $translations, 'en');
  2251. $translate->setLocale('en');
  2252. $this->form->addElements(array(
  2253. 'foo' => array(
  2254. 'type' => 'text',
  2255. 'options' => array(
  2256. 'required' => true,
  2257. 'validators' => array('NotEmpty')
  2258. )
  2259. ),
  2260. 'bar' => array(
  2261. 'type' => 'text',
  2262. 'options' => array(
  2263. 'required' => true,
  2264. 'validators' => array('Digits')
  2265. )
  2266. ),
  2267. ))
  2268. ->setTranslator($translate);
  2269. $data = array(
  2270. 'foo' => '',
  2271. 'bar' => 'abc',
  2272. );
  2273. if ($this->form->isValid($data)) {
  2274. $this->fail('Form should not validate');
  2275. }
  2276. $messages = $this->form->getMessages();
  2277. $this->assertTrue(isset($messages['foo']));
  2278. $this->assertTrue(isset($messages['bar']));
  2279. foreach ($messages['foo'] as $key => $message) {
  2280. if (array_key_exists($key, $translations)) {
  2281. $this->assertEquals($translations[$key], $message);
  2282. } else {
  2283. $this->fail('Translation for ' . $key . ' does not exist?');
  2284. }
  2285. }
  2286. foreach ($messages['bar'] as $key => $message) {
  2287. if (array_key_exists($key, $translations)) {
  2288. $this->assertEquals($translations[$key], $message);
  2289. } else {
  2290. $this->fail('Translation for ' . $key . ' does not exist?');
  2291. }
  2292. }
  2293. }
  2294. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2295. {
  2296. $this->_checkZf2794();
  2297. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2298. $translate = new Zend_Translate('array', $translations, 'en');
  2299. $translate->setLocale('en');
  2300. $this->form->addElements(array(
  2301. 'foo' => array(
  2302. 'type' => 'text',
  2303. 'options' => array(
  2304. 'required' => true,
  2305. 'validators' => array('NotEmpty')
  2306. )
  2307. ),
  2308. 'bar' => array(
  2309. 'type' => 'text',
  2310. 'options' => array(
  2311. 'required' => true,
  2312. 'validators' => array('Digits')
  2313. )
  2314. ),
  2315. ))
  2316. ->setTranslator($translate);
  2317. $data = array(
  2318. 'foo' => '',
  2319. );
  2320. if ($this->form->isValidPartial($data)) {
  2321. $this->fail('Form should not validate');
  2322. }
  2323. $messages = $this->form->getMessages();
  2324. $this->assertTrue(isset($messages['foo']));
  2325. $this->assertFalse(isset($messages['bar']));
  2326. foreach ($messages['foo'] as $key => $message) {
  2327. if (array_key_exists($key, $translations)) {
  2328. $this->assertEquals($translations[$key], $message);
  2329. } else {
  2330. $this->fail('Translation for ' . $key . ' does not exist?');
  2331. }
  2332. }
  2333. }
  2334. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2335. {
  2336. $this->_checkZf2794();
  2337. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2338. $translate = new Zend_Translate('array', $translations, 'en');
  2339. $translate->setLocale('en');
  2340. $this->form->addElements(array(
  2341. 'foo' => array(
  2342. 'type' => 'text',
  2343. 'options' => array(
  2344. 'required' => true,
  2345. 'validators' => array('NotEmpty')
  2346. )
  2347. ),
  2348. 'bar' => array(
  2349. 'type' => 'text',
  2350. 'options' => array(
  2351. 'required' => true,
  2352. 'validators' => array('Digits')
  2353. )
  2354. ),
  2355. ))
  2356. ->setTranslator($translate);
  2357. $data = array(
  2358. 'foo' => '',
  2359. );
  2360. $return = $this->form->processAjax($data);
  2361. $messages = Zend_Json::decode($return);
  2362. $this->assertTrue(is_array($messages));
  2363. $this->assertTrue(isset($messages['foo']));
  2364. $this->assertFalse(isset($messages['bar']));
  2365. foreach ($messages['foo'] as $key => $message) {
  2366. if (array_key_exists($key, $translations)) {
  2367. $this->assertEquals($translations[$key], $message);
  2368. } else {
  2369. $this->fail('Translation for ' . $key . ' does not exist?');
  2370. }
  2371. }
  2372. }
  2373. /**
  2374. * @Group ZF-9697
  2375. */
  2376. public function _setup9697()
  2377. {
  2378. $callback = create_function('$value, $options',
  2379. 'return (isset($options["bar"]["quo"]["foo"]) &&
  2380. "foo Value" === $options["bar"]["quo"]["foo"]);');
  2381. $this->form->addElement('text', 'foo')
  2382. ->foo->setBelongsTo('bar[quo]');
  2383. $this->form->addElement('text', 'quo')
  2384. ->quo->setBelongsTo('bar[quo]')
  2385. ->addValidator('Callback',
  2386. false,
  2387. $callback);
  2388. return array('bar' => array('quo' => array('foo' => 'foo Value',
  2389. 'quo' => 'quo Value')));
  2390. }
  2391. public function testIsValidKeepsContext()
  2392. {
  2393. $data = $this->_setup9697();
  2394. $this->assertTrue($this->form->isValid($data));
  2395. }
  2396. public function testIsValidPartialKeepsContext()
  2397. {
  2398. $data = $this->_setup9697();
  2399. $this->assertTrue($this->form->isValidPartial($data));
  2400. }
  2401. public function testGetValidValuesKeepsContext()
  2402. {
  2403. $data = $this->_setup9697();
  2404. $this->assertSame($data, $this->form->getValidValues($data));
  2405. }
  2406. /**#@+
  2407. * @group ZF-2988
  2408. */
  2409. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2410. {
  2411. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2412. $this->form->addErrorMessage('Invalid values entered');
  2413. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2414. $messages = $this->form->getMessages();
  2415. $this->assertEquals(1, count($messages));
  2416. $this->assertEquals('Invalid values entered', array_shift($messages));
  2417. }
  2418. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2419. {
  2420. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2421. $this->form->addErrorMessage('Invalid values entered');
  2422. $this->form->addErrorMessage('Really, they are not valid');
  2423. $messages = $this->form->getErrorMessages();
  2424. $this->assertEquals(2, count($messages));
  2425. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2426. $messages = $this->form->getMessages();
  2427. $this->assertEquals(2, count($messages));
  2428. $this->assertEquals('Invalid values entered', array_shift($messages));
  2429. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2430. }
  2431. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2432. {
  2433. $set1 = array('foo', 'bar', 'baz');
  2434. $this->form->addErrorMessages($set1);
  2435. $this->assertSame($set1, $this->form->getErrorMessages());
  2436. }
  2437. public function testSetErrorMessagesShouldOverwriteMessages()
  2438. {
  2439. $set1 = array('foo', 'bar', 'baz');
  2440. $set2 = array('bat', 'cat');
  2441. $this->form->addErrorMessages($set1);
  2442. $this->assertSame($set1, $this->form->getErrorMessages());
  2443. $this->form->setErrorMessages($set2);
  2444. $this->assertSame($set2, $this->form->getErrorMessages());
  2445. }
  2446. public function testCustomErrorMessageStackShouldBeClearable()
  2447. {
  2448. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2449. $this->form->clearErrorMessages();
  2450. $messages = $this->form->getErrorMessages();
  2451. $this->assertTrue(empty($messages));
  2452. }
  2453. public function testCustomErrorMessagesShouldBeTranslated()
  2454. {
  2455. $translations = array(
  2456. 'foo' => 'Foo message',
  2457. );
  2458. $translate = new Zend_Translate('array', $translations);
  2459. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2460. $this->form->setTranslator($translate)
  2461. ->addErrorMessage('foo');
  2462. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2463. $messages = $this->form->getMessages();
  2464. $this->assertEquals(1, count($messages));
  2465. $this->assertEquals('Foo message', array_shift($messages));
  2466. }
  2467. public function testShouldAllowMarkingFormAsInvalid()
  2468. {
  2469. $this->form->addErrorMessage('Invalid values entered');
  2470. $this->assertFalse($this->form->isErrors());
  2471. $this->form->markAsError();
  2472. $this->assertTrue($this->form->isErrors());
  2473. $messages = $this->form->getMessages();
  2474. $this->assertEquals(1, count($messages));
  2475. $this->assertEquals('Invalid values entered', array_shift($messages));
  2476. }
  2477. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2478. {
  2479. $this->assertFalse($this->form->isErrors());
  2480. $this->form->setErrors(array('Error 1', 'Error 2'))
  2481. ->addError('Error 3')
  2482. ->addErrors(array('Error 4', 'Error 5'));
  2483. $this->assertTrue($this->form->isErrors());
  2484. $messages = $this->form->getMessages();
  2485. $this->assertEquals(5, count($messages));
  2486. foreach (range(1, 5) as $id) {
  2487. $message = 'Error ' . $id;
  2488. $this->assertContains($message, $messages);
  2489. }
  2490. }
  2491. /**#@-*/
  2492. // View object
  2493. public function getView()
  2494. {
  2495. $view = new Zend_View();
  2496. $libPath = dirname(__FILE__) . '/../../../library';
  2497. $view->addHelperPath($libPath . '/Zend/View/Helper');
  2498. return $view;
  2499. }
  2500. public function testGetViewRetrievesFromViewRendererByDefault()
  2501. {
  2502. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2503. $viewRenderer->initView();
  2504. $view = $viewRenderer->view;
  2505. $test = $this->form->getView();
  2506. $this->assertSame($view, $test);
  2507. }
  2508. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2509. {
  2510. $this->assertNull($this->form->getView());
  2511. }
  2512. public function testCanSetView()
  2513. {
  2514. $view = new Zend_View();
  2515. $this->assertNull($this->form->getView());
  2516. $this->form->setView($view);
  2517. $received = $this->form->getView();
  2518. $this->assertSame($view, $received);
  2519. }
  2520. // Decorators
  2521. public function testFormDecoratorRegisteredByDefault()
  2522. {
  2523. $this->_checkZf2794();
  2524. $decorator = $this->form->getDecorator('form');
  2525. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2526. }
  2527. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2528. {
  2529. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2530. $decorators = $form->getDecorators();
  2531. $this->assertEquals(array(), $decorators);
  2532. }
  2533. public function testCanAddSingleDecoratorAsString()
  2534. {
  2535. $this->_checkZf2794();
  2536. $this->form->clearDecorators();
  2537. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2538. $this->form->addDecorator('viewHelper');
  2539. $decorator = $this->form->getDecorator('viewHelper');
  2540. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2541. }
  2542. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2543. {
  2544. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2545. }
  2546. public function testCanAddSingleDecoratorAsDecoratorObject()
  2547. {
  2548. $this->form->clearDecorators();
  2549. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2550. $decorator = new Zend_Form_Decorator_ViewHelper;
  2551. $this->form->addDecorator($decorator);
  2552. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2553. $this->assertSame($decorator, $test);
  2554. }
  2555. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2556. {
  2557. $this->_checkZf2794();
  2558. $this->form->clearDecorators();
  2559. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2560. $decorator = new Zend_Form_Decorator_ViewHelper;
  2561. $this->form->addDecorator($decorator);
  2562. $test = $this->form->getDecorator('viewHelper');
  2563. $this->assertSame($decorator, $test);
  2564. }
  2565. public function testCanAddMultipleDecorators()
  2566. {
  2567. $this->_checkZf2794();
  2568. $this->form->clearDecorators();
  2569. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2570. $testDecorator = new Zend_Form_Decorator_Errors;
  2571. $this->form->addDecorators(array(
  2572. 'ViewHelper',
  2573. $testDecorator
  2574. ));
  2575. $viewHelper = $this->form->getDecorator('viewHelper');
  2576. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2577. $decorator = $this->form->getDecorator('errors');
  2578. $this->assertSame($testDecorator, $decorator);
  2579. }
  2580. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2581. {
  2582. $this->_checkZf2794();
  2583. $this->assertFalse($this->form->removeDecorator('foobar'));
  2584. }
  2585. public function testCanRemoveDecorator()
  2586. {
  2587. $this->_checkZf2794();
  2588. $this->testFormDecoratorRegisteredByDefault();
  2589. $this->form->removeDecorator('form');
  2590. $this->assertFalse($this->form->getDecorator('form'));
  2591. }
  2592. /**
  2593. * @group ZF-3069
  2594. */
  2595. public function testRemovingNamedDecoratorShouldWork()
  2596. {
  2597. $this->_checkZf2794();
  2598. $this->form->setDecorators(array(
  2599. 'FormElements',
  2600. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2601. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2602. ));
  2603. $decorators = $this->form->getDecorators();
  2604. $this->assertTrue(array_key_exists('div', $decorators));
  2605. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2606. $this->form->removeDecorator('div');
  2607. $decorators = $this->form->getDecorators();
  2608. $this->assertFalse(array_key_exists('div', $decorators));
  2609. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2610. }
  2611. public function testCanClearAllDecorators()
  2612. {
  2613. $this->_checkZf2794();
  2614. $this->testCanAddMultipleDecorators();
  2615. $this->form->clearDecorators();
  2616. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2617. $this->assertFalse($this->form->getDecorator('fieldset'));
  2618. }
  2619. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2620. {
  2621. $this->_checkZf2794();
  2622. $this->form->setDecorators(array(
  2623. array('HtmlTag', array('tag' => 'div')),
  2624. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2625. ));
  2626. $decorator = $this->form->getDecorator('FooBar');
  2627. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2628. $this->assertEquals('dd', $decorator->getOption('tag'));
  2629. $decorator = $this->form->getDecorator('HtmlTag');
  2630. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2631. $this->assertEquals('div', $decorator->getOption('tag'));
  2632. }
  2633. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2634. {
  2635. $this->form->setDecorators(array(
  2636. 'FormElements',
  2637. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2638. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2639. 'Form',
  2640. ));
  2641. $decorator = $this->form->getDecorator('fieldset');
  2642. $decorators = $this->form->getDecorators();
  2643. $i = 0;
  2644. $order = array();
  2645. foreach (array_keys($decorators) as $name) {
  2646. $order[$name] = $i;
  2647. ++$i;
  2648. }
  2649. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2650. }
  2651. // Rendering
  2652. public function checkMarkup($html)
  2653. {
  2654. $this->assertFalse(empty($html));
  2655. $this->assertContains('<form', $html);
  2656. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2657. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2658. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2659. $this->assertContains('</form>', $html);
  2660. }
  2661. public function testRenderReturnsMarkup()
  2662. {
  2663. $this->setupElements();
  2664. $html = $this->form->render($this->getView());
  2665. $this->checkMarkup($html);
  2666. }
  2667. public function testRenderReturnsMarkupRepresentingAllElements()
  2668. {
  2669. $this->testRenderReturnsMarkup();
  2670. $html = $this->form->render();
  2671. foreach ($this->form->getElements() as $key => $element) {
  2672. $this->assertFalse(empty($key));
  2673. $this->assertFalse(is_numeric($key));
  2674. $this->assertContains('<input', $html);
  2675. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2676. }
  2677. }
  2678. public function testRenderReturnsMarkupContainingSubForms()
  2679. {
  2680. $this->setupElements();
  2681. $this->setupSubForm();
  2682. $this->form->setView($this->getView());
  2683. $html = $this->form->render();
  2684. $this->assertRegexp('/<fieldset/', $html);
  2685. $this->assertContains('</fieldset>', $html);
  2686. foreach ($this->form->sub as $key => $item) {
  2687. $this->assertFalse(empty($key));
  2688. $this->assertFalse(is_numeric($key));
  2689. $this->assertContains('<input', $html);
  2690. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2691. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2692. }
  2693. }
  2694. public function testRenderReturnsMarkupContainingDisplayGroups()
  2695. {
  2696. $this->setupElements();
  2697. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2698. $this->form->setView($this->getView());
  2699. $html = $this->html = $this->form->render();
  2700. $this->assertRegexp('/<fieldset/', $html);
  2701. $this->assertContains('</fieldset>', $html);
  2702. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2703. $dom = new DOMDocument();
  2704. $dom->loadHTML($html);
  2705. $fieldsets = $dom->getElementsByTagName('fieldset');
  2706. $this->assertTrue(0 < $fieldsets->length);
  2707. $fieldset = $fieldsets->item(0);
  2708. $nodes = $fieldset->childNodes;
  2709. $this->assertNotNull($nodes);
  2710. for ($i = 0; $i < $nodes->length; ++$i) {
  2711. $node = $nodes->item($i);
  2712. if ('input' != $node->nodeName) {
  2713. continue;
  2714. }
  2715. $this->assertTrue($node->hasAttribute('name'));
  2716. $nameNode = $node->getAttributeNode('name');
  2717. switch ($i) {
  2718. case 0:
  2719. $this->assertEquals('foo', $nameNode->nodeValue);
  2720. break;
  2721. case 1:
  2722. $this->assertEquals('baz', $nameNode->nodeValue);
  2723. break;
  2724. default:
  2725. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2726. }
  2727. }
  2728. }
  2729. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2730. {
  2731. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2732. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2733. $this->fail("Should find foo element in rendered form");
  2734. }
  2735. $this->assertEquals(1, count($matches));
  2736. $this->assertEquals(1, count($matches[0]));
  2737. }
  2738. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2739. {
  2740. $this->setupElements();
  2741. $this->form->setElementsBelongTo('anArray');
  2742. $html = $this->form->render($this->getView());
  2743. $this->assertContains('name="anArray[foo]"', $html);
  2744. $this->assertContains('name="anArray[bar]"', $html);
  2745. $this->assertContains('name="anArray[baz]"', $html);
  2746. $this->assertContains('id="anArray-foo"', $html);
  2747. $this->assertContains('id="anArray-bar"', $html);
  2748. $this->assertContains('id="anArray-baz"', $html);
  2749. }
  2750. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2751. {
  2752. $this->setupElements();
  2753. $this->form->setElementsBelongTo('data[foo]');
  2754. $html = $this->form->render($this->getView());
  2755. $this->assertContains('name="data[foo][foo]"', $html);
  2756. $this->assertContains('name="data[foo][bar]"', $html);
  2757. $this->assertContains('name="data[foo][baz]"', $html);
  2758. $this->assertContains('id="data-foo-foo"', $html);
  2759. $this->assertContains('id="data-foo-bar"', $html);
  2760. $this->assertContains('id="data-foo-baz"', $html);
  2761. }
  2762. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2763. {
  2764. $this->setupElements();
  2765. $this->form->setName('data')
  2766. ->setIsArray(true);
  2767. $html = $this->form->render($this->getView());
  2768. $this->assertContains('name="data[foo]"', $html);
  2769. $this->assertContains('name="data[bar]"', $html);
  2770. $this->assertContains('name="data[baz]"', $html);
  2771. $this->assertContains('id="data-foo"', $html);
  2772. $this->assertContains('id="data-bar"', $html);
  2773. $this->assertContains('id="data-baz"', $html);
  2774. }
  2775. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2776. {
  2777. $this->form->setName('data')
  2778. ->setIsArray(true);
  2779. $subForm = new Zend_Form_SubForm();
  2780. $subForm->setElementsBelongTo('billing[info]');
  2781. $subForm->addElement('text', 'name');
  2782. $subForm->addElement('text', 'number');
  2783. $this->form->addSubForm($subForm, 'sub');
  2784. $html = $this->form->render($this->getView());
  2785. $this->assertContains('name="data[billing][info][name]', $html);
  2786. $this->assertContains('name="data[billing][info][number]', $html);
  2787. $this->assertContains('id="data-billing-info-name"', $html);
  2788. $this->assertContains('id="data-billing-info-number"', $html);
  2789. }
  2790. public function testToStringProxiesToRender()
  2791. {
  2792. $this->setupElements();
  2793. $this->form->setView($this->getView());
  2794. $html = $this->form->__toString();
  2795. $this->checkMarkup($html);
  2796. }
  2797. public function raiseDecoratorException($content, $element, $options)
  2798. {
  2799. throw new Exception('Raising exception in decorator callback');
  2800. }
  2801. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2802. {
  2803. $this->error = $errstr;
  2804. }
  2805. public function testToStringRaisesErrorWhenExceptionCaught()
  2806. {
  2807. $this->form->setDecorators(array(
  2808. array(
  2809. 'decorator' => 'Callback',
  2810. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2811. ),
  2812. ));
  2813. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2814. $text = $this->form->__toString();
  2815. restore_error_handler();
  2816. $this->assertTrue(empty($text));
  2817. $this->assertTrue(isset($this->error));
  2818. $this->assertContains('Raising exception in decorator callback', $this->error);
  2819. }
  2820. /**
  2821. * ZF-2718
  2822. */
  2823. public function testHiddenElementsGroupedWhenRendered()
  2824. {
  2825. $this->markTestIncomplete('Scheduling for future release');
  2826. $this->form->addElements(array(
  2827. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2828. array('type' => 'text', 'name' => 'testone'),
  2829. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2830. array('type' => 'text', 'name' => 'testtwo'),
  2831. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2832. array('type' => 'text', 'name' => 'testthree'),
  2833. ));
  2834. $html = $this->form->render($this->getView());
  2835. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2836. $this->fail('Hidden elements should be grouped');
  2837. }
  2838. foreach (array('first', 'second', 'third') as $which) {
  2839. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2840. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2841. }
  2842. }
  2843. // Localization
  2844. public function testTranslatorIsNullByDefault()
  2845. {
  2846. $this->assertNull($this->form->getTranslator());
  2847. }
  2848. public function testCanSetTranslator()
  2849. {
  2850. require_once 'Zend/Translate/Adapter/Array.php';
  2851. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2852. $this->form->setTranslator($translator);
  2853. $received = $this->form->getTranslator($translator);
  2854. $this->assertSame($translator->getAdapter(), $received);
  2855. }
  2856. public function testCanSetDefaultGlobalTranslator()
  2857. {
  2858. $this->assertNull($this->form->getTranslator());
  2859. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2860. Zend_Form::setDefaultTranslator($translator);
  2861. $received = Zend_Form::getDefaultTranslator();
  2862. $this->assertSame($translator->getAdapter(), $received);
  2863. $received = $this->form->getTranslator();
  2864. $this->assertSame($translator->getAdapter(), $received);
  2865. $form = new Zend_Form();
  2866. $received = $form->getTranslator();
  2867. $this->assertSame($translator->getAdapter(), $received);
  2868. }
  2869. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2870. {
  2871. $this->assertNull($this->form->getTranslator());
  2872. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2873. Zend_Form::setDefaultTranslator($translatorDefault);
  2874. $received = $this->form->getTranslator();
  2875. $this->assertSame($translatorDefault->getAdapter(), $received);
  2876. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2877. $this->form->setTranslator($translator);
  2878. $received = $this->form->getTranslator();
  2879. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2880. $this->assertSame($translator->getAdapter(), $received);
  2881. }
  2882. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2883. {
  2884. $this->assertNull($this->form->getTranslator());
  2885. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2886. Zend_Registry::set('Zend_Translate', $translator);
  2887. $received = Zend_Form::getDefaultTranslator();
  2888. $this->assertSame($translator->getAdapter(), $received);
  2889. $received = $this->form->getTranslator();
  2890. $this->assertSame($translator->getAdapter(), $received);
  2891. $form = new Zend_Form();
  2892. $received = $form->getTranslator();
  2893. $this->assertSame($translator->getAdapter(), $received);
  2894. }
  2895. public function testCanDisableTranslation()
  2896. {
  2897. $this->testCanSetDefaultGlobalTranslator();
  2898. $this->form->setDisableTranslator(true);
  2899. $this->assertNull($this->form->getTranslator());
  2900. }
  2901. // Iteration
  2902. public function testFormObjectIsIterableAndIteratesElements()
  2903. {
  2904. $this->setupElements();
  2905. $expected = array('foo', 'bar', 'baz');
  2906. $received = array();
  2907. foreach ($this->form as $key => $value) {
  2908. $received[] = $key;
  2909. }
  2910. $this->assertSame($expected, $received);
  2911. }
  2912. public function testFormObjectIteratesElementsInExpectedOrder()
  2913. {
  2914. $this->setupElements();
  2915. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2916. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2917. $received = array();
  2918. foreach ($this->form as $key => $value) {
  2919. $received[] = $key;
  2920. $this->assertTrue($value instanceof Zend_Form_Element);
  2921. }
  2922. $this->assertSame($expected, $received);
  2923. }
  2924. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2925. {
  2926. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2927. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2928. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2929. $expected = array('name', 'email', 'submit');
  2930. $received = array();
  2931. foreach ($this->form as $key => $value) {
  2932. $received[] = $key;
  2933. $this->assertTrue($value instanceof Zend_Form_Element);
  2934. }
  2935. $this->assertSame($expected, $received);
  2936. }
  2937. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2938. {
  2939. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2940. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2941. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2942. ->setView($this->getView());
  2943. $test = $this->form->render();
  2944. $this->assertContains('name="a"', $test);
  2945. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2946. $this->fail('Expected markup not found');
  2947. }
  2948. $order = array();
  2949. foreach ($matches[1] as $element) {
  2950. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2951. $order[] = $m[1];
  2952. }
  2953. }
  2954. $this->assertSame(array('b', 'c', 'a'), $order);
  2955. }
  2956. public function testFormObjectIteratesElementsAndSubforms()
  2957. {
  2958. $this->setupElements();
  2959. $this->setupSubForm();
  2960. $expected = array('foo', 'bar', 'baz', 'sub');
  2961. $received = array();
  2962. foreach ($this->form as $key => $value) {
  2963. $received[] = $key;
  2964. $this->assertTrue(($value instanceof Zend_Form_Element)
  2965. or ($value instanceof Zend_Form_SubForm));
  2966. }
  2967. $this->assertSame($expected, $received);
  2968. }
  2969. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  2970. {
  2971. $this->setupElements();
  2972. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2973. $expected = array('bar', 'foobaz');
  2974. $received = array();
  2975. foreach ($this->form as $key => $value) {
  2976. $received[] = $key;
  2977. $this->assertTrue(($value instanceof Zend_Form_Element)
  2978. or ($value instanceof Zend_Form_DisplayGroup));
  2979. }
  2980. $this->assertSame($expected, $received);
  2981. }
  2982. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  2983. {
  2984. $this->setupElements();
  2985. $bar = $this->form->bar;
  2986. $this->form->removeElement('bar');
  2987. try {
  2988. foreach ($this->form as $item) {
  2989. }
  2990. } catch (Exception $e) {
  2991. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2992. }
  2993. $this->form->addElement($bar);
  2994. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  2995. $this->form->removeDisplayGroup('bazbar');
  2996. try {
  2997. foreach ($this->form as $item) {
  2998. }
  2999. } catch (Exception $e) {
  3000. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3001. }
  3002. $subForm = new Zend_Form_SubForm;
  3003. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  3004. $this->form->addSubForm($subForm, 'page1');
  3005. $this->form->removeSubForm('page1');
  3006. try {
  3007. foreach ($this->form as $item) {
  3008. }
  3009. } catch (Exception $e) {
  3010. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  3011. }
  3012. }
  3013. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  3014. {
  3015. $form = new Zend_Form();
  3016. $form->addElements(array(
  3017. 'username' => 'text',
  3018. 'password' => 'text',
  3019. ));
  3020. $form->clearElements();
  3021. try {
  3022. foreach ($form as $item) {
  3023. }
  3024. } catch (Zend_Form_Exception $e) {
  3025. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  3026. . $e->getMessage();
  3027. $this->fail($message);
  3028. }
  3029. $form->addElements(array(
  3030. 'username' => 'text',
  3031. 'password' => 'text',
  3032. ))
  3033. ->addDisplayGroup(array('username', 'password'), 'login');
  3034. $form->clearDisplayGroups();
  3035. try {
  3036. foreach ($form as $item) {
  3037. }
  3038. } catch (Zend_Form_Exception $e) {
  3039. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  3040. . $e->getMessage();
  3041. $this->fail($message);
  3042. }
  3043. $subForm = new Zend_Form_SubForm();
  3044. $form->addSubForm($subForm, 'foo');
  3045. $form->clearSubForms();
  3046. try {
  3047. foreach ($form as $item) {
  3048. }
  3049. } catch (Zend_Form_Exception $e) {
  3050. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  3051. . $e->getMessage();
  3052. $this->fail($message);
  3053. }
  3054. }
  3055. // Countable
  3056. public function testCanCountFormObject()
  3057. {
  3058. $this->setupElements();
  3059. $this->assertEquals(3, count($this->form));
  3060. }
  3061. public function testCountingFormObjectCountsSubForms()
  3062. {
  3063. $this->setupElements();
  3064. $this->setupSubForm();
  3065. $this->assertEquals(4, count($this->form));
  3066. }
  3067. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  3068. {
  3069. $this->testCountingFormObjectCountsSubForms();
  3070. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3071. $this->assertEquals(3, count($this->form));
  3072. }
  3073. // Element decorators and plugin paths
  3074. public function testCanSetAllElementDecoratorsAtOnce()
  3075. {
  3076. $this->_checkZf2794();
  3077. $this->setupElements();
  3078. $this->form->setElementDecorators(array(
  3079. array('ViewHelper'),
  3080. array('Label'),
  3081. array('Fieldset'),
  3082. ));
  3083. foreach ($this->form->getElements() as $element) {
  3084. $this->assertFalse($element->getDecorator('Errors'));
  3085. $this->assertFalse($element->getDecorator('HtmlTag'));
  3086. $decorator = $element->getDecorator('ViewHelper');
  3087. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  3088. $decorator = $element->getDecorator('Label');
  3089. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  3090. $decorator = $element->getDecorator('Fieldset');
  3091. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3092. }
  3093. }
  3094. /**
  3095. * @group ZF-3597
  3096. */
  3097. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  3098. {
  3099. $this->form->setDecorators(array(
  3100. 'FormElements',
  3101. array('HtmlTag', array('tag' => 'table')),
  3102. 'Form',
  3103. ));
  3104. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  3105. $this->form->addElement('text', 'test', array(
  3106. 'label' => 'Foo',
  3107. 'description' => 'sample description',
  3108. ));
  3109. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  3110. $decorator = new My_Decorator_TableRow();
  3111. $this->form->setElementDecorators(array(
  3112. 'ViewHelper',
  3113. $decorator,
  3114. ));
  3115. $html = $this->form->render($this->getView());
  3116. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  3117. }
  3118. /**#@+
  3119. * @group ZF-3228
  3120. */
  3121. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  3122. {
  3123. $this->_checkZf2794();
  3124. $this->setupElements();
  3125. $this->form->setElementDecorators(
  3126. array(
  3127. 'Description',
  3128. 'Form',
  3129. 'Fieldset',
  3130. ),
  3131. array(
  3132. 'bar',
  3133. )
  3134. );
  3135. $element = $this->form->bar;
  3136. $this->assertFalse($element->getDecorator('ViewHelper'));
  3137. $this->assertFalse($element->getDecorator('Errors'));
  3138. $this->assertFalse($element->getDecorator('Label'));
  3139. $this->assertFalse($element->getDecorator('HtmlTag'));
  3140. $decorator = $element->getDecorator('Description');
  3141. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3142. $decorator = $element->getDecorator('Form');
  3143. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3144. $decorator = $element->getDecorator('Fieldset');
  3145. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3146. foreach (array('foo', 'baz') as $name) {
  3147. $element = $this->form->$name;
  3148. $this->assertFalse($element->getDecorator('Form'));
  3149. $this->assertFalse($element->getDecorator('Fieldset'));
  3150. }
  3151. }
  3152. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  3153. {
  3154. $this->_checkZf2794();
  3155. $this->setupElements();
  3156. $this->form->setElementDecorators(
  3157. array(
  3158. 'Description',
  3159. 'Form',
  3160. 'Fieldset',
  3161. ),
  3162. array(
  3163. 'foo',
  3164. 'baz',
  3165. ),
  3166. false
  3167. );
  3168. $element = $this->form->bar;
  3169. $this->assertFalse($element->getDecorator('ViewHelper'));
  3170. $this->assertFalse($element->getDecorator('Errors'));
  3171. $this->assertFalse($element->getDecorator('Label'));
  3172. $this->assertFalse($element->getDecorator('HtmlTag'));
  3173. $decorator = $element->getDecorator('Description');
  3174. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  3175. $decorator = $element->getDecorator('Form');
  3176. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  3177. $decorator = $element->getDecorator('Fieldset');
  3178. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  3179. foreach (array('foo', 'baz') as $name) {
  3180. $element = $this->form->$name;
  3181. $this->assertFalse($element->getDecorator('Form'));
  3182. $this->assertFalse($element->getDecorator('Fieldset'));
  3183. }
  3184. }
  3185. /**#@-*/
  3186. public function testCanSetAllElementFiltersAtOnce()
  3187. {
  3188. $this->_checkZf2794();
  3189. $this->setupElements();
  3190. $this->form->setElementFilters(array(
  3191. 'Alnum',
  3192. 'StringToLower'
  3193. ));
  3194. foreach ($this->form->getElements() as $element) {
  3195. $filter = $element->getFilter('Alnum');
  3196. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  3197. $filter = $element->getFilter('StringToLower');
  3198. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  3199. }
  3200. }
  3201. public function testCanSetGlobalElementPrefixPath()
  3202. {
  3203. $this->setupElements();
  3204. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3205. $this->form->addElement('text', 'prefixTest');
  3206. foreach ($this->form->getElements() as $element) {
  3207. $loader = $element->getPluginLoader('validate');
  3208. $paths = $loader->getPaths('Zend_Foo_Validate');
  3209. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  3210. $this->assertContains('Foo', $paths[0]);
  3211. $this->assertContains('Validate', $paths[0]);
  3212. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  3213. $this->assertFalse(empty($paths));
  3214. $this->assertContains('Foo', $paths[0]);
  3215. $this->assertContains('Filter', $paths[0]);
  3216. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  3217. $this->assertFalse(empty($paths));
  3218. $this->assertContains('Foo', $paths[0]);
  3219. $this->assertContains('Decorator', $paths[0]);
  3220. }
  3221. }
  3222. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  3223. {
  3224. $this->_checkZf2794();
  3225. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  3226. $this->form->addElement('text', 'prefixTest');
  3227. $element = $this->form->prefixTest;
  3228. $label = $element->getDecorator('Label');
  3229. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  3230. }
  3231. /**
  3232. * @group ZF-3093
  3233. */
  3234. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  3235. {
  3236. $subForm = new Zend_Form_SubForm();
  3237. $subForm->addElement('text', 'foo');
  3238. $this->form->addSubForm($subForm, 'subForm');
  3239. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  3240. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  3241. $paths = $loader->getPaths('Zend_Foo_Decorator');
  3242. $this->assertFalse(empty($paths));
  3243. $this->assertContains('Foo', $paths[0]);
  3244. $this->assertContains('Decorator', $paths[0]);
  3245. }
  3246. public function testCanSetElementValidatorPrefixPath()
  3247. {
  3248. $this->setupElements();
  3249. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  3250. $this->form->addElement('text', 'prefixTest');
  3251. foreach ($this->form->getElements() as $element) {
  3252. $loader = $element->getPluginLoader('validate');
  3253. $paths = $loader->getPaths('Zend_Foo');
  3254. $this->assertFalse(empty($paths));
  3255. $this->assertContains('Foo', $paths[0]);
  3256. $this->assertNotContains('Validate', $paths[0]);
  3257. }
  3258. }
  3259. public function testCanSetElementFilterPrefixPath()
  3260. {
  3261. $this->setupElements();
  3262. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  3263. $this->form->addElement('text', 'prefixTest');
  3264. foreach ($this->form->getElements() as $element) {
  3265. $loader = $element->getPluginLoader('filter');
  3266. $paths = $loader->getPaths('Zend_Foo');
  3267. $this->assertFalse(empty($paths));
  3268. $this->assertContains('Foo', $paths[0]);
  3269. $this->assertNotContains('Filter', $paths[0]);
  3270. }
  3271. }
  3272. public function testCanSetElementDecoratorPrefixPath()
  3273. {
  3274. $this->setupElements();
  3275. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  3276. $this->form->addElement('text', 'prefixTest');
  3277. foreach ($this->form->getElements() as $element) {
  3278. $loader = $element->getPluginLoader('decorator');
  3279. $paths = $loader->getPaths('Zend_Foo');
  3280. $this->assertFalse(empty($paths));
  3281. $this->assertContains('Foo', $paths[0]);
  3282. $this->assertNotContains('Decorator', $paths[0]);
  3283. }
  3284. }
  3285. // Display Group decorators and plugin paths
  3286. public function setupDisplayGroups()
  3287. {
  3288. $this->testCanAddAndRetrieveMultipleElements();
  3289. $this->form->addElements(array(
  3290. 'test1' => 'text',
  3291. 'test2' => 'text',
  3292. 'test3' => 'text',
  3293. 'test4' => 'text'
  3294. ));
  3295. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  3296. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  3297. }
  3298. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  3299. {
  3300. $this->_checkZf2794();
  3301. $this->setupDisplayGroups();
  3302. $this->form->setDisplayGroupDecorators(array(
  3303. array('Callback', array('callback' => 'strip_tags')),
  3304. ));
  3305. foreach ($this->form->getDisplayGroups() as $element) {
  3306. $this->assertFalse($element->getDecorator('FormElements'));
  3307. $this->assertFalse($element->getDecorator('HtmlTag'));
  3308. $this->assertFalse($element->getDecorator('Fieldset'));
  3309. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  3310. $decorator = $element->getDecorator('Callback');
  3311. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3312. }
  3313. }
  3314. public function testCanSetDisplayGroupPrefixPath()
  3315. {
  3316. $this->setupDisplayGroups();
  3317. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3318. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3319. foreach ($this->form->getDisplayGroups() as $group) {
  3320. $loader = $group->getPluginLoader();
  3321. $paths = $loader->getPaths('Zend_Foo');
  3322. $this->assertFalse(empty($paths));
  3323. $this->assertContains('Foo', $paths[0]);
  3324. }
  3325. }
  3326. /**
  3327. * @group ZF-3213
  3328. */
  3329. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3330. {
  3331. require_once 'Zend/Config/Ini.php';
  3332. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3333. $form = new Zend_Form($config);
  3334. $dg = $form->foofoo;
  3335. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3336. $this->assertTrue($paths !== false);
  3337. }
  3338. // Subform decorators
  3339. public function testCanSetAllSubFormDecoratorsAtOnce()
  3340. {
  3341. $this->_checkZf2794();
  3342. $this->setupSubForm();
  3343. $this->form->setSubFormDecorators(array(
  3344. array('Callback', array('callback' => 'strip_tags')),
  3345. ));
  3346. foreach ($this->form->getSubForms() as $subForm) {
  3347. $this->assertFalse($subForm->getDecorator('FormElements'));
  3348. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3349. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3350. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3351. $decorator = $subForm->getDecorator('Callback');
  3352. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3353. }
  3354. }
  3355. // Extension
  3356. public function testInitCalledPriorToLoadingDefaultDecorators()
  3357. {
  3358. $form = new Zend_Form_FormTest_FormExtension();
  3359. $decorators = $form->getDecorators();
  3360. $this->assertTrue(empty($decorators));
  3361. }
  3362. // Clone
  3363. /**
  3364. * @group ZF-3819
  3365. */
  3366. public function testCloningShouldCloneAllChildren()
  3367. {
  3368. $form = new Zend_Form();
  3369. $foo = new Zend_Form_SubForm(array(
  3370. 'name' => 'foo',
  3371. 'elements' => array(
  3372. 'one' => 'text',
  3373. 'two' => 'text',
  3374. ),
  3375. ));
  3376. $form->addElement('text', 'bar')
  3377. ->addElement('text', 'baz')
  3378. ->addElement('text', 'bat')
  3379. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3380. ->addSubForm($foo, 'foo');
  3381. $bar = $form->bar;
  3382. $baz = $form->baz;
  3383. $bat = $form->bat;
  3384. $barbat = $form->barbat;
  3385. $cloned = clone $form;
  3386. $this->assertNotSame($foo, $cloned->foo);
  3387. $this->assertNotSame($bar, $cloned->bar);
  3388. $this->assertNotSame($baz, $cloned->baz);
  3389. $this->assertNotSame($bat, $cloned->bat);
  3390. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3391. $this->assertNotSame($foo->one, $cloned->foo->one);
  3392. $this->assertNotSame($foo->two, $cloned->foo->two);
  3393. }
  3394. // Reset
  3395. /**
  3396. * @group ZF-3227
  3397. */
  3398. public function testFormsShouldAllowResetting()
  3399. {
  3400. $form = new Zend_Form();
  3401. $foo = new Zend_Form_SubForm(array(
  3402. 'name' => 'foo',
  3403. 'elements' => array(
  3404. 'one' => 'text',
  3405. 'two' => 'text',
  3406. ),
  3407. ));
  3408. $form->addElement('text', 'bar')
  3409. ->addElement('text', 'baz')
  3410. ->addElement('text', 'bat')
  3411. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3412. ->addSubForm($foo, 'foo');
  3413. $values = array(
  3414. 'bar' => 'Bar Value',
  3415. 'baz' => 'Baz Value',
  3416. 'bat' => 'Bat Value',
  3417. 'foo' => array(
  3418. 'one' => 'One Value',
  3419. 'two' => 'Two Value',
  3420. ),
  3421. );
  3422. $form->populate($values);
  3423. $test = $form->getValues();
  3424. $this->assertEquals($values, $test);
  3425. $form->reset();
  3426. $test = $form->getValues();
  3427. $this->assertNotEquals($values, $test);
  3428. $this->assertEquals(0, array_sum($test));
  3429. }
  3430. /**
  3431. * @group ZF-3217
  3432. */
  3433. public function testFormShouldOverloadToRenderDecorators()
  3434. {
  3435. $this->setupElements();
  3436. $this->form->setView($this->getView());
  3437. $html = $this->form->renderFormElements();
  3438. foreach ($this->form->getElements() as $element) {
  3439. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3440. }
  3441. $this->assertNotContains('<dl', $html);
  3442. $this->assertNotContains('<form', $html);
  3443. $html = $this->form->renderForm('this is the content');
  3444. $this->assertContains('<form', $html);
  3445. $this->assertContains('</form>', $html);
  3446. $this->assertContains('this is the content', $html);
  3447. }
  3448. /**
  3449. * @group ZF-3217
  3450. * @expectedException Zend_Form_Exception
  3451. */
  3452. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3453. {
  3454. $html = $this->form->bogusMethodCall();
  3455. }
  3456. /**
  3457. * @group ZF-2950
  3458. */
  3459. public function testDtDdElementsWithLabelGetUniqueId()
  3460. {
  3461. $form = new Zend_Form();
  3462. $form->setView($this->getView());
  3463. $fooElement = new Zend_Form_Element_Text('foo');
  3464. $fooElement->setLabel('Foo');
  3465. $form->addElement($fooElement);
  3466. $html = $form->render();
  3467. $this->assertContains('<dt id="foo-label">', $html);
  3468. $this->assertContains('<dd id="foo-element">', $html);
  3469. }
  3470. /**
  3471. * @group ZF-2950
  3472. */
  3473. public function testDtDdElementsWithoutLabelGetUniqueId()
  3474. {
  3475. $form = new Zend_Form();
  3476. $form->setView($this->getView())
  3477. ->addElement(new Zend_Form_Element_Text('foo'));
  3478. $html = $form->render();
  3479. $this->assertContains('<dt id="foo-label">&nbsp;</dt>', $html);
  3480. $this->assertContains('<dd id="foo-element">', $html);
  3481. }
  3482. /**
  3483. * @group ZF-2950
  3484. */
  3485. public function testSubFormGetsUniqueIdWithName()
  3486. {
  3487. $form = new Zend_Form();
  3488. $form->setView($this->getView())
  3489. ->setName('testform')
  3490. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3491. $html = $form->render();
  3492. $this->assertContains('<dt id="testform-label">&nbsp;</dt>', $html);
  3493. $this->assertContains('<dd id="testform-element">', $html);
  3494. }
  3495. /**
  3496. * @group ZF-5370
  3497. */
  3498. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3499. {
  3500. $file = new Zend_Form_Element_File('txt');
  3501. $this->form->addElement($file);
  3502. $html = $this->form->render($this->getView());
  3503. $this->assertFalse(empty($html));
  3504. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3505. }
  3506. /**
  3507. * @group ZF-5370
  3508. */
  3509. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3510. {
  3511. $subForm = new Zend_Form_SubForm();
  3512. $subForm->addElement('file', 'txt');
  3513. $this->form->addSubForm($subForm, 'page1')
  3514. ->setView(new Zend_View);
  3515. $html = $this->form->render();
  3516. $this->assertContains('id="txt"', $html);
  3517. $this->assertContains('name="txt"', $html);
  3518. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3519. }
  3520. /**
  3521. * @group ZF-5370
  3522. */
  3523. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3524. {
  3525. $this->form->addElement('file', 'txt')
  3526. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3527. ->setView(new Zend_View);
  3528. $html = $this->form->render();
  3529. $this->assertContains('id="txt"', $html);
  3530. $this->assertContains('name="txt"', $html);
  3531. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3532. }
  3533. /**
  3534. * @group ZF-6070
  3535. */
  3536. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3537. {
  3538. $this->form->setOptions(array(
  3539. 'elementDecorators' => array(
  3540. 'ViewHelper',
  3541. 'Label',
  3542. ),
  3543. 'elements' => array(
  3544. 'foo' => array(
  3545. 'type' => 'text',
  3546. 'options' => array(
  3547. 'decorators' => array(
  3548. 'Errors',
  3549. 'ViewHelper',
  3550. ),
  3551. ),
  3552. ),
  3553. ),
  3554. ));
  3555. $element = $this->form->getElement('foo');
  3556. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3557. $actual = array();
  3558. foreach ($element->getDecorators() as $decorator) {
  3559. $actual[] = get_class($decorator);
  3560. }
  3561. $this->assertSame($expected, $actual);
  3562. }
  3563. /**
  3564. * @group ZF-5150
  3565. */
  3566. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3567. {
  3568. $this->form->addError('Error');
  3569. $this->assertFalse($this->form->isValid(array()));
  3570. }
  3571. /**
  3572. * @group ZF-8494
  3573. */
  3574. public function testGetValidValues()
  3575. {
  3576. $data = array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement');
  3577. require_once "Zend/Validate/Int.php";
  3578. $validElement = new Zend_Form_Element("valid");
  3579. $validElement->addValidator(new Zend_Validate_Int());
  3580. $this->form->addElement($validElement);
  3581. $invalidElement = new Zend_Form_Element('invalid');
  3582. $invalidElement->addValidator(new Zend_Validate_Int());
  3583. $this->form->addElement($invalidElement);
  3584. $this->assertEquals(array('valid' => 1234), $this->form->getValidValues($data));
  3585. }
  3586. /**
  3587. * @group ZF-8494
  3588. */
  3589. public function testGetValidSubFormValues()
  3590. {
  3591. $data = array('sub' => array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement'));
  3592. require_once "Zend/Validate/Int.php";
  3593. $subForm = new Zend_Form_SubForm();
  3594. $validElement = new Zend_Form_Element("valid");
  3595. $validElement->addValidator(new Zend_Validate_Int());
  3596. $subForm->addElement($validElement);
  3597. $invalidElement = new Zend_Form_Element('invalid');
  3598. $invalidElement->addValidator(new Zend_Validate_Int());
  3599. $subForm->addElement($invalidElement);
  3600. $this->form->addSubForm($subForm, 'sub');
  3601. $this->assertEquals(array('sub' => array('valid' => 1234)), $this->form->getValidValues($data));
  3602. }
  3603. /**
  3604. * @group ZF-9494
  3605. */
  3606. public function testElementTranslatorNotOveriddenbyFormTranslator()
  3607. {
  3608. $translations = array(
  3609. 'isEmpty' => 'Element message',
  3610. );
  3611. $translate = new Zend_Translate('array', $translations);
  3612. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$translate));
  3613. $this->assertFalse($this->form->isValid(array('foo'=>'')));
  3614. $messages = $this->form->getMessages();
  3615. $this->assertEquals(1, count($messages));
  3616. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3617. $this->assertFalse($this->form->isValidPartial(array('foo'=>'')));
  3618. $messages = $this->form->getMessages();
  3619. $this->assertEquals(1, count($messages));
  3620. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3621. }
  3622. /**
  3623. * @group ZF-9364
  3624. */
  3625. public function testElementTranslatorPreferredOverFormTranslator()
  3626. {
  3627. $formTanslations = array(
  3628. 'isEmpty' => 'Form message',
  3629. );
  3630. $elementTanslations = array(
  3631. 'isEmpty' => 'Element message',
  3632. );
  3633. $formTranslate = new Zend_Translate('array', $formTanslations);
  3634. $elementTranslate = new Zend_Translate('array', $elementTanslations);
  3635. $this->form->setTranslator($formTranslate);
  3636. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3637. $this->form->addElement('text', 'bar', array('required'=>true));
  3638. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3639. $messages = $this->form->getMessages();
  3640. $this->assertEquals(2, count($messages));
  3641. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3642. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3643. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3644. $messages = $this->form->getMessages();
  3645. $this->assertEquals(2, count($messages));
  3646. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3647. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3648. }
  3649. /**
  3650. * @group ZF-9364
  3651. */
  3652. public function testElementTranslatorPreferredOverDefaultTranslator()
  3653. {
  3654. $defaultTranslations = array(
  3655. 'isEmpty' => 'Default message',
  3656. );
  3657. $formTranslations = array(
  3658. 'isEmpty' => 'Form message',
  3659. );
  3660. $elementTranslations = array(
  3661. 'isEmpty' => 'Element message',
  3662. );
  3663. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3664. $formTranslate = new Zend_Translate('array', $formTranslations);
  3665. $elementTranslate = new Zend_Translate('array', $elementTranslations);
  3666. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3667. $this->form->setTranslator($formTranslate);
  3668. $this->form->addElement('text', 'foo', array('required'=>true, 'translator'=>$elementTranslate));
  3669. $this->form->addElement('text', 'bar', array('required'=>true));
  3670. $this->assertFalse($this->form->isValid(array('foo'=>'', 'bar'=>'')));
  3671. $messages = $this->form->getMessages();
  3672. $this->assertEquals(2, count($messages));
  3673. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3674. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3675. $this->assertFalse($this->form->isValidPartial(array('foo'=>'', 'bar'=>'')));
  3676. $messages = $this->form->getMessages();
  3677. $this->assertEquals(2, count($messages));
  3678. $this->assertEquals('Element message', $messages['foo']['isEmpty']);
  3679. $this->assertEquals('Form message', $messages['bar']['isEmpty']);
  3680. }
  3681. /**
  3682. * @group ZF-9540
  3683. */
  3684. public function testSubFormTranslatorPreferredOverDefaultTranslator()
  3685. {
  3686. $defaultTranslations = array('isEmpty' => 'Default message');
  3687. $subformTranslations = array('isEmpty' => 'SubForm message');
  3688. $defaultTranslate = new Zend_Translate('array', $defaultTranslations);
  3689. $subformTranslate = new Zend_Translate('array', $subformTranslations);
  3690. Zend_Registry::set('Zend_Translate', $defaultTranslate);
  3691. $this->form->addSubForm(new Zend_Form_SubForm(), 'subform');
  3692. $this->form->subform->setTranslator($subformTranslate);
  3693. $this->form->subform->addElement('text', 'foo', array('required'=>true));
  3694. $this->assertFalse($this->form->isValid(array('subform' => array('foo'=>''))));
  3695. $messages = $this->form->getMessages();
  3696. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3697. $this->assertFalse($this->form->isValidPartial(array('subform' => array('foo'=>''))));
  3698. $messages = $this->form->getMessages();
  3699. $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']);
  3700. }
  3701. /**
  3702. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3703. *
  3704. * @link http://framework.zend.com/issues/browse/ZF-2794
  3705. * @return void
  3706. */
  3707. protected function _checkZf2794()
  3708. {
  3709. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3710. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3711. }
  3712. }
  3713. }
  3714. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  3715. {
  3716. }
  3717. class Zend_Form_FormTest_FormExtension extends Zend_Form
  3718. {
  3719. public function init()
  3720. {
  3721. $this->setDisableLoadDefaultDecorators(true);
  3722. }
  3723. }
  3724. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  3725. Zend_Form_FormTest::main();
  3726. }