FormTest.php 153 KB

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