FormTest.php 152 KB

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