FormTest.php 148 KB

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