FormTest.php 154 KB

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