FormTest.php 131 KB

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