FormTest.php 145 KB

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