FormTest.php 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829
  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. // Display groups
  1289. public function testCanAddAndRetrieveSingleDisplayGroups()
  1290. {
  1291. $this->testCanAddAndRetrieveMultipleElements();
  1292. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  1293. $group = $this->form->getDisplayGroup('barbat');
  1294. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1295. $elements = $group->getElements();
  1296. $expected = array('bar' => $this->form->bar, 'bat' => $this->form->bat);
  1297. $this->assertEquals($expected, $elements);
  1298. }
  1299. public function testDisplayGroupsMustContainAtLeastOneElement()
  1300. {
  1301. try {
  1302. $this->form->addDisplayGroup(array(), 'foo');
  1303. $this->fail('Empty display group should raise exception');
  1304. } catch (Zend_Form_Exception $e) {
  1305. $this->assertContains('No valid elements', $e->getMessage());
  1306. }
  1307. }
  1308. public function testCanAddAndRetrieveMultipleDisplayGroups()
  1309. {
  1310. $this->testCanAddAndRetrieveMultipleElements();
  1311. $this->form->addDisplayGroups(array(
  1312. array(array('bar', 'bat'), 'barbat'),
  1313. 'foobaz' => array('baz', 'foo')
  1314. ));
  1315. $groups = $this->form->getDisplayGroups();
  1316. $expected = array(
  1317. 'barbat' => array('bar' => $this->form->bar, 'bat' => $this->form->bat),
  1318. 'foobaz' => array('baz' => $this->form->baz, 'foo' => $this->form->foo),
  1319. );
  1320. foreach ($groups as $group) {
  1321. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1322. }
  1323. $this->assertEquals($expected['barbat'], $groups['barbat']->getElements());
  1324. $this->assertEquals($expected['foobaz'], $groups['foobaz']->getElements());
  1325. }
  1326. public function testSetDisplayGroupsOverwritesExistingDisplayGroups()
  1327. {
  1328. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1329. $this->form->setDisplayGroups(array('foobar' => array('bar', 'foo')));
  1330. $groups = $this->form->getDisplayGroups();
  1331. $expected = array('bar' => $this->form->bar, 'foo' => $this->form->foo);
  1332. $this->assertEquals(1, count($groups));
  1333. $this->assertTrue(isset($groups['foobar']));
  1334. $this->assertEquals($expected, $groups['foobar']->getElements());
  1335. }
  1336. public function testCanRemoveSingleDisplayGroup()
  1337. {
  1338. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1339. $this->assertTrue($this->form->removeDisplayGroup('barbat'));
  1340. $this->assertNull($this->form->getDisplayGroup('barbat'));
  1341. }
  1342. public function testRemoveDisplayGroupReturnsFalseForNonexistantGroup()
  1343. {
  1344. $this->assertFalse($this->form->removeDisplayGroup('bogus'));
  1345. }
  1346. public function testCanClearAllDisplayGroups()
  1347. {
  1348. $this->testCanAddAndRetrieveMultipleDisplayGroups();
  1349. $this->form->clearDisplayGroups();
  1350. $groups = $this->form->getDisplayGroups();
  1351. $this->assertTrue(is_array($groups));
  1352. $this->assertTrue(empty($groups));
  1353. }
  1354. public function testOverloadingDisplayGroups()
  1355. {
  1356. $this->testCanAddAndRetrieveMultipleElements();
  1357. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1358. $this->assertTrue(isset($this->form->foobar));
  1359. $group = $this->form->foobar;
  1360. $expected = array('foo' => $this->form->foo, 'bar' => $this->form->bar);
  1361. $this->assertEquals($expected, $group->getElements());
  1362. unset($this->form->foobar);
  1363. $this->assertFalse(isset($this->form->foobar));
  1364. $this->form->barbaz = array('bar', 'baz');
  1365. $this->assertTrue(isset($this->form->barbaz));
  1366. $group = $this->form->barbaz;
  1367. $expected = array('bar' => $this->form->bar, 'baz' => $this->form->baz);
  1368. $this->assertSame($expected, $group->getElements());
  1369. }
  1370. public function testDefaultDisplayGroupClassExists()
  1371. {
  1372. $this->assertEquals('Zend_Form_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1373. }
  1374. public function testCanSetDefaultDisplayGroupClass()
  1375. {
  1376. $this->testDefaultDisplayGroupClassExists();
  1377. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1378. $this->assertEquals('Zend_Form_FormTest_DisplayGroup', $this->form->getDefaultDisplayGroupClass());
  1379. }
  1380. public function testDefaultDisplayGroupClassUsedForNewDisplayGroups()
  1381. {
  1382. $this->form->setDefaultDisplayGroupClass('Zend_Form_FormTest_DisplayGroup');
  1383. $this->setupElements();
  1384. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar');
  1385. $displayGroup = $this->form->getDisplayGroup('foobar');
  1386. $this->assertTrue($displayGroup instanceof Zend_Form_FormTest_DisplayGroup);
  1387. }
  1388. public function testCanPassDisplayGroupClassWhenAddingDisplayGroup()
  1389. {
  1390. $this->setupElements();
  1391. $this->form->addDisplayGroup(array('foo', 'bar'), 'foobar', array('displayGroupClass' => 'Zend_Form_FormTest_DisplayGroup'));
  1392. $this->assertTrue($this->form->foobar instanceof Zend_Form_FormTest_DisplayGroup);
  1393. }
  1394. /**
  1395. * @group ZF-3254
  1396. */
  1397. public function testAddingDisplayGroupShouldPassOptions()
  1398. {
  1399. $this->testCanAddAndRetrieveMultipleElements();
  1400. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat', array('disableLoadDefaultDecorators' => true));
  1401. $group = $this->form->getDisplayGroup('barbat');
  1402. $this->assertTrue($group instanceof Zend_Form_DisplayGroup);
  1403. $decorators = $group->getDecorators();
  1404. $this->assertTrue(is_array($decorators));
  1405. $this->assertTrue(empty($decorators));
  1406. }
  1407. // Processing
  1408. public function testPopulateProxiesToSetDefaults()
  1409. {
  1410. $this->testCanAddAndRetrieveMultipleElements();
  1411. $values = array(
  1412. 'foo' => 'foovalue',
  1413. 'bar' => 'barvalue',
  1414. 'baz' => 'bazvalue',
  1415. 'bat' => 'batvalue'
  1416. );
  1417. $this->form->populate($values);
  1418. $test = $this->form->getValues();
  1419. $elements = $this->form->getElements();
  1420. foreach (array_keys($values) as $name) {
  1421. $this->assertEquals($values[$name], $test[$name]);
  1422. }
  1423. }
  1424. public function setupElements()
  1425. {
  1426. $foo = new Zend_Form_Element_Text('foo');
  1427. $foo->addValidator('NotEmpty')
  1428. ->addValidator('Alpha');
  1429. $bar = new Zend_Form_Element_Text('bar');
  1430. $bar->addValidator('NotEmpty')
  1431. ->addValidator('Digits');
  1432. $baz = new Zend_Form_Element_Text('baz');
  1433. $baz->addValidator('NotEmpty')
  1434. ->addValidator('Alnum');
  1435. $this->form->addElements(array($foo, $bar, $baz));
  1436. $this->elementValues = array(
  1437. 'foo' => 'fooBarBAZ',
  1438. 'bar' => '123456789',
  1439. 'baz' => 'foo123BAR',
  1440. );
  1441. }
  1442. public function testIsValidShouldThrowExceptionWithNonArrayArgument()
  1443. {
  1444. try {
  1445. $this->form->isValid(true);
  1446. $this->fail('isValid() should raise exception with non-array argument');
  1447. } catch (Zend_Form_Exception $e) {
  1448. $this->assertContains('expects an array', $e->getMessage());
  1449. }
  1450. }
  1451. public function testCanValidateFullFormContainingOnlyElements()
  1452. {
  1453. $this->_checkZf2794();
  1454. $this->setupElements();
  1455. $this->assertTrue($this->form->isValid($this->elementValues));
  1456. $values = array(
  1457. 'foo' => '12345',
  1458. 'bar' => 'abc',
  1459. 'baz' => 'abc-123'
  1460. );
  1461. $this->assertFalse($this->form->isValid($values));
  1462. $validator = $this->form->foo->getValidator('alpha');
  1463. $this->assertEquals('12345', $validator->value);
  1464. $validator = $this->form->bar->getValidator('digits');
  1465. $this->assertEquals('abc', $validator->value);
  1466. $validator = $this->form->baz->getValidator('alnum');
  1467. $this->assertEquals('abc-123', $validator->value);
  1468. }
  1469. public function testValidationTakesElementRequiredFlagsIntoAccount()
  1470. {
  1471. $this->_checkZf2794();
  1472. $this->setupElements();
  1473. $this->assertTrue($this->form->isValid(array()));
  1474. $this->form->getElement('foo')->setRequired(true);
  1475. $this->assertTrue($this->form->isValid(array(
  1476. 'foo' => 'abc',
  1477. 'baz' => 'abc123'
  1478. )));
  1479. $this->assertFalse($this->form->isValid(array(
  1480. 'baz' => 'abc123'
  1481. )));
  1482. }
  1483. public function testCanValidatePartialFormContainingOnlyElements()
  1484. {
  1485. $this->_checkZf2794();
  1486. $this->setupElements();
  1487. $this->form->getElement('foo')->setRequired(true);
  1488. $this->form->getElement('bar')->setRequired(true);
  1489. $this->form->getElement('baz')->setRequired(true);
  1490. $this->assertTrue($this->form->isValidPartial(array(
  1491. 'foo' => 'abc',
  1492. 'baz' => 'abc123'
  1493. )));
  1494. $this->assertFalse($this->form->isValidPartial(array(
  1495. 'foo' => '123',
  1496. 'baz' => 'abc-123'
  1497. )));
  1498. }
  1499. public function setupSubForm()
  1500. {
  1501. $subForm = new Zend_Form_SubForm();
  1502. $foo = new Zend_Form_Element_Text('subfoo');
  1503. $foo->addValidators(array('NotEmpty', 'Alpha'))->setRequired(true);
  1504. $bar = new Zend_Form_Element_Text('subbar');
  1505. $bar->addValidators(array('NotEmpty', 'Digits'));
  1506. $baz = new Zend_Form_Element_Text('subbaz');
  1507. $baz->addValidators(array('NotEmpty', 'Alnum'))->setRequired(true);
  1508. $subForm->addElements(array($foo, $bar, $baz));
  1509. $this->form->addSubForm($subForm, 'sub');
  1510. }
  1511. public function testFullDataArrayUsedToValidateSubFormByDefault()
  1512. {
  1513. $this->_checkZf2794();
  1514. $this->setupElements();
  1515. $this->setupSubForm();
  1516. $data = array(
  1517. 'foo' => 'abcdef',
  1518. 'bar' => '123456',
  1519. 'baz' => '123abc',
  1520. 'subfoo' => 'abcdef',
  1521. 'subbar' => '123456',
  1522. 'subbaz' => '123abc',
  1523. );
  1524. $this->assertTrue($this->form->isValid($data));
  1525. $data = array(
  1526. 'foo' => 'abcdef',
  1527. 'bar' => '123456',
  1528. 'baz' => '123abc',
  1529. 'subfoo' => '123',
  1530. 'subbar' => 'abc',
  1531. 'subbaz' => '123-abc',
  1532. );
  1533. $this->assertFalse($this->form->isValid($data));
  1534. $data = array(
  1535. 'foo' => 'abcdef',
  1536. 'bar' => '123456',
  1537. 'baz' => '123abc',
  1538. 'subfoo' => 'abc',
  1539. 'subbaz' => '123abc',
  1540. );
  1541. $this->assertTrue($this->form->isValid($data));
  1542. $data = array(
  1543. 'foo' => 'abcdef',
  1544. 'bar' => '123456',
  1545. 'baz' => '123abc',
  1546. 'subbar' => '123',
  1547. 'subbaz' => '123abc',
  1548. );
  1549. $this->assertFalse($this->form->isValid($data));
  1550. }
  1551. public function testDataKeyWithSameNameAsSubFormIsUsedForValidatingSubForm()
  1552. {
  1553. $this->_checkZf2794();
  1554. $this->setupElements();
  1555. $this->setupSubForm();
  1556. $data = array(
  1557. 'foo' => 'abcdef',
  1558. 'bar' => '123456',
  1559. 'baz' => '123abc',
  1560. 'sub' => array(
  1561. 'subfoo' => 'abcdef',
  1562. 'subbar' => '123456',
  1563. 'subbaz' => '123abc',
  1564. ),
  1565. );
  1566. $this->assertTrue($this->form->isValid($data));
  1567. $data = array(
  1568. 'foo' => 'abcdef',
  1569. 'bar' => '123456',
  1570. 'baz' => '123abc',
  1571. 'sub' => array(
  1572. 'subfoo' => '123',
  1573. 'subbar' => 'abc',
  1574. 'subbaz' => '123-abc',
  1575. )
  1576. );
  1577. $this->assertFalse($this->form->isValid($data));
  1578. $data = array(
  1579. 'foo' => 'abcdef',
  1580. 'bar' => '123456',
  1581. 'baz' => '123abc',
  1582. 'sub' => array(
  1583. 'subfoo' => 'abc',
  1584. 'subbaz' => '123abc',
  1585. )
  1586. );
  1587. $this->assertTrue($this->form->isValid($data));
  1588. $data = array(
  1589. 'foo' => 'abcdef',
  1590. 'bar' => '123456',
  1591. 'baz' => '123abc',
  1592. 'sub' => array(
  1593. 'subbar' => '123',
  1594. 'subbaz' => '123abc',
  1595. )
  1596. );
  1597. $this->assertFalse($this->form->isValid($data));
  1598. }
  1599. public function testCanValidateNestedFormsWithElementsBelongingToArrays()
  1600. {
  1601. $form = new Zend_Form();
  1602. $form->setElementsBelongTo('foobar');
  1603. $form->addElement('text', 'firstName')
  1604. ->getElement('firstName')
  1605. ->setRequired(true);
  1606. $form->addElement('text', 'lastName')
  1607. ->getElement('lastName')
  1608. ->setRequired(true);
  1609. $subForm = new Zend_Form_SubForm();
  1610. $subForm->setElementsBelongTo('baz');
  1611. $subForm->addElement('text', 'email')
  1612. ->getElement('email')->setRequired(true);
  1613. $subSubForm = new Zend_Form_SubForm();
  1614. $subSubForm->setElementsBelongTo('bat');
  1615. $subSubForm->addElement('checkbox', 'home')
  1616. ->getElement('home')->setRequired(true);
  1617. $subForm->addSubForm($subSubForm, 'subSub');
  1618. $form->addSubForm($subForm, 'sub')
  1619. ->addElement('submit', 'save', array('value' => 'submit'));
  1620. $data = array('foobar' => array(
  1621. 'firstName' => 'Mabel',
  1622. 'lastName' => 'Cow',
  1623. 'baz' => array(
  1624. 'email' => 'mabel@cow.org',
  1625. 'bat' => array(
  1626. 'home' => 1,
  1627. )
  1628. )
  1629. ));
  1630. $this->assertTrue($form->isValid($data));
  1631. $this->assertEquals('Mabel', $form->firstName->getValue());
  1632. $this->assertEquals('Cow', $form->lastName->getValue());
  1633. $this->assertEquals('mabel@cow.org', $form->sub->email->getValue());
  1634. $this->assertEquals(1, $form->sub->subSub->home->getValue());
  1635. }
  1636. public function testCanValidatePartialFormContainingSubForms()
  1637. {
  1638. $this->_checkZf2794();
  1639. $this->setupElements();
  1640. $this->setupSubForm();
  1641. $data = array(
  1642. 'subfoo' => 'abcdef',
  1643. 'subbar' => '123456',
  1644. );
  1645. $this->assertTrue($this->form->isValidPartial($data));
  1646. $data = array(
  1647. 'foo' => 'abcdef',
  1648. 'baz' => '123abc',
  1649. 'sub' => array(
  1650. 'subbar' => '123',
  1651. )
  1652. );
  1653. $this->assertTrue($this->form->isValidPartial($data));
  1654. $data = array(
  1655. 'foo' => 'abcdef',
  1656. 'bar' => '123456',
  1657. 'baz' => '123abc',
  1658. 'sub' => array(
  1659. 'subfoo' => '123',
  1660. )
  1661. );
  1662. $this->assertFalse($this->form->isValidPartial($data));
  1663. }
  1664. public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays()
  1665. {
  1666. $this->_checkZf2794();
  1667. $form = new Zend_Form();
  1668. $form->setElementsBelongTo('foobar');
  1669. $form->addElement('text', 'firstName')
  1670. ->getElement('firstName')
  1671. ->setRequired(false);
  1672. $form->addElement('text', 'lastName')
  1673. ->getElement('lastName')
  1674. ->setRequired(true);
  1675. $subForm = new Zend_Form_SubForm();
  1676. $subForm->setElementsBelongTo('baz');
  1677. $subForm->addElement('text', 'email')
  1678. ->getElement('email')
  1679. ->setRequired(true)
  1680. ->addValidator('NotEmpty');
  1681. $subSubForm = new Zend_Form_SubForm();
  1682. $subSubForm->setElementsBelongTo('bat');
  1683. $subSubForm->addElement('checkbox', 'home')
  1684. ->getElement('home')
  1685. ->setRequired(true)
  1686. ->addValidator('InArray', false, array(array('1')));
  1687. $subForm->addSubForm($subSubForm, 'subSub');
  1688. $form->addSubForm($subForm, 'sub')
  1689. ->addElement('submit', 'save', array('value' => 'submit'));
  1690. $data = array('foobar' => array(
  1691. 'lastName' => 'Cow',
  1692. ));
  1693. $this->assertTrue($form->isValidPartial($data));
  1694. $this->assertEquals('Cow', $form->lastName->getValue());
  1695. $firstName = $form->firstName->getValue();
  1696. $email = $form->sub->email->getValue();
  1697. $home = $form->sub->subSub->home->getValue();
  1698. $this->assertTrue(empty($firstName));
  1699. $this->assertTrue(empty($email));
  1700. $this->assertTrue(empty($home));
  1701. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1702. $data['foobar']['baz'] = array('bat' => array('home' => 'ab'));
  1703. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1704. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1705. $messages = $form->getMessages();
  1706. $this->assertFalse(empty($messages));
  1707. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1));
  1708. $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['notInArray']), var_export($messages, 1));
  1709. }
  1710. public function testCanValidatePartialNestedFormsWithMultiLevelElementsBelongingToArrays()
  1711. {
  1712. $this->_checkZf2794();
  1713. $form = new Zend_Form();
  1714. $form->setElementsBelongTo('foo[bar]');
  1715. $form->addElement('text', 'firstName')
  1716. ->getElement('firstName')
  1717. ->setRequired(false);
  1718. $form->addElement('text', 'lastName')
  1719. ->getElement('lastName')
  1720. ->setRequired(true);
  1721. $subForm = new Zend_Form_SubForm();
  1722. $subForm->setElementsBelongTo('baz');
  1723. $subForm->addElement('text', 'email')
  1724. ->getElement('email')
  1725. ->setRequired(true)
  1726. ->addValidator('NotEmpty');
  1727. $subSubForm = new Zend_Form_SubForm();
  1728. $subSubForm->setElementsBelongTo('bat[quux]');
  1729. $subSubForm->addElement('checkbox', 'home')
  1730. ->getElement('home')
  1731. ->setRequired(true)
  1732. ->addValidator('InArray', false, array(array('1')));
  1733. $subForm->addSubForm($subSubForm, 'subSub');
  1734. $form->addSubForm($subForm, 'sub')
  1735. ->addElement('submit', 'save', array('value' => 'submit'));
  1736. $data = array('foo' => array(
  1737. 'bar' => array(
  1738. 'lastName' => 'Cow',
  1739. ),
  1740. ));
  1741. $this->assertTrue($form->isValidPartial($data));
  1742. $this->assertEquals('Cow', $form->lastName->getValue());
  1743. $firstName = $form->firstName->getValue();
  1744. $email = $form->sub->email->getValue();
  1745. $home = $form->sub->subSub->home->getValue();
  1746. $this->assertTrue(empty($firstName));
  1747. $this->assertTrue(empty($email));
  1748. $this->assertTrue(empty($home));
  1749. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1750. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1751. $this->assertFalse($form->isValidPartial($data), var_export($data, 1));
  1752. $this->assertEquals('0', $form->sub->subSub->home->getValue());
  1753. }
  1754. public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays()
  1755. {
  1756. $this->_checkZf2794();
  1757. $form = new Zend_Form();
  1758. $form->setElementsBelongTo('foo[bar]');
  1759. $form->addElement('text', 'firstName')
  1760. ->getElement('firstName')
  1761. ->setRequired(false);
  1762. $form->addElement('text', 'lastName')
  1763. ->getElement('lastName')
  1764. ->setRequired(true);
  1765. $subForm = new Zend_Form_SubForm();
  1766. $subForm->setElementsBelongTo('baz');
  1767. $subForm->addElement('text', 'email')
  1768. ->getElement('email')
  1769. ->setRequired(true)
  1770. ->addValidator('NotEmpty');
  1771. $subSubForm = new Zend_Form_SubForm();
  1772. $subSubForm->setElementsBelongTo('bat[quux]');
  1773. $subSubForm->addElement('checkbox', 'home')
  1774. ->getElement('home')
  1775. ->setRequired(true)
  1776. ->addValidator('InArray', false, array(array('1')));
  1777. $subForm->addSubForm($subSubForm, 'subSub');
  1778. $form->addSubForm($subForm, 'sub')
  1779. ->addElement('submit', 'save', array('value' => 'submit'));
  1780. $data = array('foo' => array(
  1781. 'bar' => array(
  1782. 'lastName' => 'Cow',
  1783. ),
  1784. ));
  1785. $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6));
  1786. $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab')));
  1787. $form->isValidPartial($data);
  1788. $messages = $form->getMessages();
  1789. $this->assertFalse(empty($messages));
  1790. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1));
  1791. $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1));
  1792. }
  1793. public function testValidatingFormWithDisplayGroupsDoesSameAsWithout()
  1794. {
  1795. $this->setupElements();
  1796. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1797. $this->assertTrue($this->form->isValid($this->elementValues));
  1798. $this->assertFalse($this->form->isValid(array(
  1799. 'foo' => '123',
  1800. 'bar' => 'abc',
  1801. 'baz' => 'abc-123'
  1802. )));
  1803. }
  1804. public function testValidatePartialFormWithDisplayGroupsDoesSameAsWithout()
  1805. {
  1806. $this->setupElements();
  1807. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  1808. $this->assertTrue($this->form->isValid(array(
  1809. 'foo' => 'abc',
  1810. 'baz' => 'abc123'
  1811. )));
  1812. $this->assertFalse($this->form->isValid(array(
  1813. 'foo' => '123',
  1814. 'baz' => 'abc-123'
  1815. )));
  1816. }
  1817. public function testProcessAjaxReturnsJsonTrueForValidForm()
  1818. {
  1819. $this->setupElements();
  1820. $return = $this->form->processAjax($this->elementValues);
  1821. $this->assertTrue(Zend_Json::decode($return));
  1822. }
  1823. public function testProcessAjaxReturnsJsonTrueForValidPartialForm()
  1824. {
  1825. $this->setupElements();
  1826. $data = array('foo' => 'abcdef', 'baz' => 'abc123');
  1827. $return = $this->form->processAjax($data);
  1828. $this->assertTrue(Zend_Json::decode($return));
  1829. }
  1830. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidForm()
  1831. {
  1832. $this->setupElements();
  1833. $data = array('foo' => '123456', 'bar' => 'abcdef', 'baz' => 'abc-123');
  1834. $return = Zend_Json::decode($this->form->processAjax($data));
  1835. $this->assertTrue(is_array($return));
  1836. $this->assertEquals(array_keys($data), array_keys($return));
  1837. }
  1838. public function testProcessAjaxReturnsJsonWithAllErrorMessagesForInvalidPartialForm()
  1839. {
  1840. $this->setupElements();
  1841. $data = array('baz' => 'abc-123');
  1842. $return = Zend_Json::decode($this->form->processAjax($data));
  1843. $this->assertTrue(is_array($return));
  1844. $this->assertEquals(array_keys($data), array_keys($return), var_export($return, 1));
  1845. }
  1846. public function testPersistDataStoresDataInSession()
  1847. {
  1848. $this->markTestIncomplete('Zend_Form does not implement session storage at this time');
  1849. }
  1850. public function testCanCheckIfErrorsAreRegistered()
  1851. {
  1852. $this->assertFalse($this->form->isErrors());
  1853. $this->testCanValidateFullFormContainingOnlyElements();
  1854. $this->assertTrue($this->form->isErrors());
  1855. }
  1856. public function testCanRetrieveErrorCodesFromAllElementsAfterFailedValidation()
  1857. {
  1858. $this->_checkZf2794();
  1859. $this->testCanValidateFullFormContainingOnlyElements();
  1860. $codes = $this->form->getErrors();
  1861. $keys = array('foo', 'bar', 'baz');
  1862. $this->assertEquals($keys, array_keys($codes));
  1863. }
  1864. public function testCanRetrieveErrorCodesFromSingleElementAfterFailedValidation()
  1865. {
  1866. $this->_checkZf2794();
  1867. $this->testCanValidateFullFormContainingOnlyElements();
  1868. $codes = $this->form->getErrors();
  1869. $keys = array('foo', 'bar', 'baz');
  1870. $errors = $this->form->getErrors('foo');
  1871. $foo = $this->form->foo;
  1872. $this->assertEquals($foo->getErrors(), $errors);
  1873. }
  1874. public function testCanRetrieveErrorMessagesFromAllElementsAfterFailedValidation()
  1875. {
  1876. $this->_checkZf2794();
  1877. $this->testCanValidateFullFormContainingOnlyElements();
  1878. $codes = $this->form->getMessages();
  1879. $keys = array('foo', 'bar', 'baz');
  1880. $this->assertEquals($keys, array_keys($codes));
  1881. }
  1882. public function testCanRetrieveErrorMessagesFromSingleElementAfterFailedValidation()
  1883. {
  1884. $this->_checkZf2794();
  1885. $this->testCanValidateFullFormContainingOnlyElements();
  1886. $codes = $this->form->getMessages();
  1887. $keys = array('foo', 'bar', 'baz');
  1888. $messages = $this->form->getMessages('foo');
  1889. $foo = $this->form->foo;
  1890. $this->assertEquals($foo->getMessages(), $messages);
  1891. }
  1892. public function testErrorCodesFromSubFormReturnedInSeparateArray()
  1893. {
  1894. $this->_checkZf2794();
  1895. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1896. $codes = $this->form->getErrors();
  1897. $this->assertTrue(array_key_exists('sub', $codes));
  1898. $this->assertTrue(is_array($codes['sub']));
  1899. $keys = array('subfoo', 'subbar', 'subbaz');
  1900. $this->assertEquals($keys, array_keys($codes['sub']));
  1901. }
  1902. public function testCanRetrieveErrorCodesFromSingleSubFormAfterFailedValidation()
  1903. {
  1904. $this->_checkZf2794();
  1905. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1906. $codes = $this->form->getErrors('sub');
  1907. $this->assertTrue(is_array($codes));
  1908. $this->assertFalse(empty($codes));
  1909. $keys = array('subfoo', 'subbar', 'subbaz');
  1910. $this->assertEquals($keys, array_keys($codes));
  1911. }
  1912. public function testGetErrorsHonorsElementsBelongTo()
  1913. {
  1914. $this->_checkZf2794();
  1915. $subForm = new Zend_Form_SubForm();
  1916. $subForm->setElementsBelongTo('foo[bar]');
  1917. $subForm->addElement('text', 'test')->test
  1918. ->setRequired(true);
  1919. $this->form->addSubForm($subForm, 'sub');
  1920. $data = array('foo' => array(
  1921. 'bar' => array(
  1922. 'test' => '',
  1923. ),
  1924. ));
  1925. $this->form->isValid($data);
  1926. $codes = $this->form->getErrors();
  1927. $this->assertFalse(empty($codes['foo']['bar']['test']));
  1928. }
  1929. public function testErrorMessagesFromSubFormReturnedInSeparateArray()
  1930. {
  1931. $this->_checkZf2794();
  1932. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1933. $data = array(
  1934. 'foo' => 'abcdef',
  1935. 'bar' => '123456',
  1936. 'baz' => '123abc',
  1937. 'subfoo' => '123',
  1938. 'subbar' => 'abc',
  1939. 'subbaz' => '123-abc',
  1940. );
  1941. $this->assertFalse($this->form->isValid($data));
  1942. $codes = $this->form->getMessages();
  1943. $this->assertTrue(array_key_exists('sub', $codes));
  1944. $this->assertTrue(is_array($codes['sub']));
  1945. $keys = array('subfoo', 'subbar', 'subbaz');
  1946. $this->assertEquals($keys, array_keys($codes['sub']));
  1947. }
  1948. public function testCanRetrieveErrorMessagesFromSingleSubFormAfterFailedValidation()
  1949. {
  1950. $this->_checkZf2794();
  1951. $this->testFullDataArrayUsedToValidateSubFormByDefault();
  1952. $data = array(
  1953. 'foo' => 'abcdef',
  1954. 'bar' => '123456',
  1955. 'baz' => '123abc',
  1956. 'subfoo' => '123',
  1957. 'subbar' => 'abc',
  1958. 'subbaz' => '123-abc',
  1959. );
  1960. $this->assertFalse($this->form->isValid($data));
  1961. $codes = $this->form->getMessages('sub');
  1962. $this->assertTrue(is_array($codes));
  1963. $this->assertFalse(empty($codes));
  1964. $keys = array('subfoo', 'subbar', 'subbaz');
  1965. $this->assertEquals($keys, array_keys($codes), var_export($codes, 1));
  1966. }
  1967. public function testErrorMessagesAreLocalizedWhenTranslateAdapterPresent()
  1968. {
  1969. $this->_checkZf2794();
  1970. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  1971. $translate = new Zend_Translate('array', $translations, 'en');
  1972. $translate->setLocale('en');
  1973. $this->form->addElements(array(
  1974. 'foo' => array(
  1975. 'type' => 'text',
  1976. 'options' => array(
  1977. 'required' => true,
  1978. 'validators' => array('NotEmpty')
  1979. )
  1980. ),
  1981. 'bar' => array(
  1982. 'type' => 'text',
  1983. 'options' => array(
  1984. 'required' => true,
  1985. 'validators' => array('Digits')
  1986. )
  1987. ),
  1988. ))
  1989. ->setTranslator($translate);
  1990. $data = array(
  1991. 'foo' => '',
  1992. 'bar' => 'abc',
  1993. );
  1994. if ($this->form->isValid($data)) {
  1995. $this->fail('Form should not validate');
  1996. }
  1997. $messages = $this->form->getMessages();
  1998. $this->assertTrue(isset($messages['foo']));
  1999. $this->assertTrue(isset($messages['bar']));
  2000. foreach ($messages['foo'] as $key => $message) {
  2001. if (array_key_exists($key, $translations)) {
  2002. $this->assertEquals($translations[$key], $message);
  2003. } else {
  2004. $this->fail('Translation for ' . $key . ' does not exist?');
  2005. }
  2006. }
  2007. foreach ($messages['bar'] as $key => $message) {
  2008. if (array_key_exists($key, $translations)) {
  2009. $this->assertEquals($translations[$key], $message);
  2010. } else {
  2011. $this->fail('Translation for ' . $key . ' does not exist?');
  2012. }
  2013. }
  2014. }
  2015. public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent()
  2016. {
  2017. $this->_checkZf2794();
  2018. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2019. $translate = new Zend_Translate('array', $translations, 'en');
  2020. $translate->setLocale('en');
  2021. $this->form->addElements(array(
  2022. 'foo' => array(
  2023. 'type' => 'text',
  2024. 'options' => array(
  2025. 'required' => true,
  2026. 'validators' => array('NotEmpty')
  2027. )
  2028. ),
  2029. 'bar' => array(
  2030. 'type' => 'text',
  2031. 'options' => array(
  2032. 'required' => true,
  2033. 'validators' => array('Digits')
  2034. )
  2035. ),
  2036. ))
  2037. ->setTranslator($translate);
  2038. $data = array(
  2039. 'foo' => '',
  2040. );
  2041. if ($this->form->isValidPartial($data)) {
  2042. $this->fail('Form should not validate');
  2043. }
  2044. $messages = $this->form->getMessages();
  2045. $this->assertTrue(isset($messages['foo']));
  2046. $this->assertFalse(isset($messages['bar']));
  2047. foreach ($messages['foo'] as $key => $message) {
  2048. if (array_key_exists($key, $translations)) {
  2049. $this->assertEquals($translations[$key], $message);
  2050. } else {
  2051. $this->fail('Translation for ' . $key . ' does not exist?');
  2052. }
  2053. }
  2054. }
  2055. public function testErrorMessagesFromProcessAjaxAreLocalizedWhenTranslateAdapterPresent()
  2056. {
  2057. $this->_checkZf2794();
  2058. $translations = include dirname(__FILE__) . '/_files/locale/array.php';
  2059. $translate = new Zend_Translate('array', $translations, 'en');
  2060. $translate->setLocale('en');
  2061. $this->form->addElements(array(
  2062. 'foo' => array(
  2063. 'type' => 'text',
  2064. 'options' => array(
  2065. 'required' => true,
  2066. 'validators' => array('NotEmpty')
  2067. )
  2068. ),
  2069. 'bar' => array(
  2070. 'type' => 'text',
  2071. 'options' => array(
  2072. 'required' => true,
  2073. 'validators' => array('Digits')
  2074. )
  2075. ),
  2076. ))
  2077. ->setTranslator($translate);
  2078. $data = array(
  2079. 'foo' => '',
  2080. );
  2081. $return = $this->form->processAjax($data);
  2082. $messages = Zend_Json::decode($return);
  2083. $this->assertTrue(is_array($messages));
  2084. $this->assertTrue(isset($messages['foo']));
  2085. $this->assertFalse(isset($messages['bar']));
  2086. foreach ($messages['foo'] as $key => $message) {
  2087. if (array_key_exists($key, $translations)) {
  2088. $this->assertEquals($translations[$key], $message);
  2089. } else {
  2090. $this->fail('Translation for ' . $key . ' does not exist?');
  2091. }
  2092. }
  2093. }
  2094. /**#@+
  2095. * @group ZF-2988
  2096. */
  2097. public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
  2098. {
  2099. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2100. $this->form->addErrorMessage('Invalid values entered');
  2101. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2102. $messages = $this->form->getMessages();
  2103. $this->assertEquals(1, count($messages));
  2104. $this->assertEquals('Invalid values entered', array_shift($messages));
  2105. }
  2106. public function testCustomErrorMessagesShouldBeManagedInAStack()
  2107. {
  2108. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2109. $this->form->addErrorMessage('Invalid values entered');
  2110. $this->form->addErrorMessage('Really, they are not valid');
  2111. $messages = $this->form->getErrorMessages();
  2112. $this->assertEquals(2, count($messages));
  2113. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2114. $messages = $this->form->getMessages();
  2115. $this->assertEquals(2, count($messages));
  2116. $this->assertEquals('Invalid values entered', array_shift($messages));
  2117. $this->assertEquals('Really, they are not valid', array_shift($messages));
  2118. }
  2119. public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
  2120. {
  2121. $set1 = array('foo', 'bar', 'baz');
  2122. $this->form->addErrorMessages($set1);
  2123. $this->assertSame($set1, $this->form->getErrorMessages());
  2124. }
  2125. public function testSetErrorMessagesShouldOverwriteMessages()
  2126. {
  2127. $set1 = array('foo', 'bar', 'baz');
  2128. $set2 = array('bat', 'cat');
  2129. $this->form->addErrorMessages($set1);
  2130. $this->assertSame($set1, $this->form->getErrorMessages());
  2131. $this->form->setErrorMessages($set2);
  2132. $this->assertSame($set2, $this->form->getErrorMessages());
  2133. }
  2134. public function testCustomErrorMessageStackShouldBeClearable()
  2135. {
  2136. $this->testCustomErrorMessagesShouldBeManagedInAStack();
  2137. $this->form->clearErrorMessages();
  2138. $messages = $this->form->getErrorMessages();
  2139. $this->assertTrue(empty($messages));
  2140. }
  2141. public function testCustomErrorMessagesShouldBeTranslated()
  2142. {
  2143. $translations = array(
  2144. 'foo' => 'Foo message',
  2145. );
  2146. $translate = new Zend_Translate('array', $translations);
  2147. $this->form->addElement('text', 'foo', array('validators' => array('Alpha')));
  2148. $this->form->setTranslator($translate)
  2149. ->addErrorMessage('foo');
  2150. $this->assertFalse($this->form->isValid(array('foo' => 123)));
  2151. $messages = $this->form->getMessages();
  2152. $this->assertEquals(1, count($messages));
  2153. $this->assertEquals('Foo message', array_shift($messages));
  2154. }
  2155. public function testShouldAllowMarkingFormAsInvalid()
  2156. {
  2157. $this->form->addErrorMessage('Invalid values entered');
  2158. $this->assertFalse($this->form->isErrors());
  2159. $this->form->markAsError();
  2160. $this->assertTrue($this->form->isErrors());
  2161. $messages = $this->form->getMessages();
  2162. $this->assertEquals(1, count($messages));
  2163. $this->assertEquals('Invalid values entered', array_shift($messages));
  2164. }
  2165. public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
  2166. {
  2167. $this->assertFalse($this->form->isErrors());
  2168. $this->form->setErrors(array('Error 1', 'Error 2'))
  2169. ->addError('Error 3')
  2170. ->addErrors(array('Error 4', 'Error 5'));
  2171. $this->assertTrue($this->form->isErrors());
  2172. $messages = $this->form->getMessages();
  2173. $this->assertEquals(5, count($messages));
  2174. foreach (range(1, 5) as $id) {
  2175. $message = 'Error ' . $id;
  2176. $this->assertContains($message, $messages);
  2177. }
  2178. }
  2179. /**#@-*/
  2180. // View object
  2181. public function getView()
  2182. {
  2183. $view = new Zend_View();
  2184. $libPath = dirname(__FILE__) . '/../../../library';
  2185. $view->addHelperPath($libPath . '/Zend/View/Helper');
  2186. return $view;
  2187. }
  2188. public function testGetViewRetrievesFromViewRendererByDefault()
  2189. {
  2190. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2191. $viewRenderer->initView();
  2192. $view = $viewRenderer->view;
  2193. $test = $this->form->getView();
  2194. $this->assertSame($view, $test);
  2195. }
  2196. public function testGetViewReturnsNullWhenNoViewRegisteredWithViewRenderer()
  2197. {
  2198. $this->assertNull($this->form->getView());
  2199. }
  2200. public function testCanSetView()
  2201. {
  2202. $view = new Zend_View();
  2203. $this->assertNull($this->form->getView());
  2204. $this->form->setView($view);
  2205. $received = $this->form->getView();
  2206. $this->assertSame($view, $received);
  2207. }
  2208. // Decorators
  2209. public function testFormDecoratorRegisteredByDefault()
  2210. {
  2211. $this->_checkZf2794();
  2212. $decorator = $this->form->getDecorator('form');
  2213. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2214. }
  2215. public function testCanDisableRegisteringFormDecoratorsDuringInitialization()
  2216. {
  2217. $form = new Zend_Form(array('disableLoadDefaultDecorators' => true));
  2218. $decorators = $form->getDecorators();
  2219. $this->assertEquals(array(), $decorators);
  2220. }
  2221. public function testCanAddSingleDecoratorAsString()
  2222. {
  2223. $this->_checkZf2794();
  2224. $this->form->clearDecorators();
  2225. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2226. $this->form->addDecorator('viewHelper');
  2227. $decorator = $this->form->getDecorator('viewHelper');
  2228. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2229. }
  2230. public function testNotCanRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
  2231. {
  2232. $this->assertFalse($this->form->getDecorator('Zend_Form_Decorator_Form'));
  2233. }
  2234. public function testCanAddSingleDecoratorAsDecoratorObject()
  2235. {
  2236. $this->form->clearDecorators();
  2237. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2238. $decorator = new Zend_Form_Decorator_ViewHelper;
  2239. $this->form->addDecorator($decorator);
  2240. $test = $this->form->getDecorator('Zend_Form_Decorator_ViewHelper');
  2241. $this->assertSame($decorator, $test);
  2242. }
  2243. public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
  2244. {
  2245. $this->_checkZf2794();
  2246. $this->form->clearDecorators();
  2247. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2248. $decorator = new Zend_Form_Decorator_ViewHelper;
  2249. $this->form->addDecorator($decorator);
  2250. $test = $this->form->getDecorator('viewHelper');
  2251. $this->assertSame($decorator, $test);
  2252. }
  2253. public function testCanAddMultipleDecorators()
  2254. {
  2255. $this->_checkZf2794();
  2256. $this->form->clearDecorators();
  2257. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2258. $testDecorator = new Zend_Form_Decorator_Errors;
  2259. $this->form->addDecorators(array(
  2260. 'ViewHelper',
  2261. $testDecorator
  2262. ));
  2263. $viewHelper = $this->form->getDecorator('viewHelper');
  2264. $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
  2265. $decorator = $this->form->getDecorator('errors');
  2266. $this->assertSame($testDecorator, $decorator);
  2267. }
  2268. public function testRemoveDecoratorReturnsFalseForUnregisteredDecorators()
  2269. {
  2270. $this->_checkZf2794();
  2271. $this->assertFalse($this->form->removeDecorator('foobar'));
  2272. }
  2273. public function testCanRemoveDecorator()
  2274. {
  2275. $this->_checkZf2794();
  2276. $this->testFormDecoratorRegisteredByDefault();
  2277. $this->form->removeDecorator('form');
  2278. $this->assertFalse($this->form->getDecorator('form'));
  2279. }
  2280. /**
  2281. * @group ZF-3069
  2282. */
  2283. public function testRemovingNamedDecoratorShouldWork()
  2284. {
  2285. $this->_checkZf2794();
  2286. $this->form->setDecorators(array(
  2287. 'FormElements',
  2288. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2289. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2290. ));
  2291. $decorators = $this->form->getDecorators();
  2292. $this->assertTrue(array_key_exists('div', $decorators));
  2293. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2294. $this->form->removeDecorator('div');
  2295. $decorators = $this->form->getDecorators();
  2296. $this->assertFalse(array_key_exists('div', $decorators));
  2297. $this->assertTrue(array_key_exists('fieldset', $decorators));
  2298. }
  2299. public function testCanClearAllDecorators()
  2300. {
  2301. $this->_checkZf2794();
  2302. $this->testCanAddMultipleDecorators();
  2303. $this->form->clearDecorators();
  2304. $this->assertFalse($this->form->getDecorator('viewHelper'));
  2305. $this->assertFalse($this->form->getDecorator('fieldset'));
  2306. }
  2307. public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
  2308. {
  2309. $this->_checkZf2794();
  2310. $this->form->setDecorators(array(
  2311. array('HtmlTag', array('tag' => 'div')),
  2312. array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'dd')),
  2313. ));
  2314. $decorator = $this->form->getDecorator('FooBar');
  2315. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2316. $this->assertEquals('dd', $decorator->getOption('tag'));
  2317. $decorator = $this->form->getDecorator('HtmlTag');
  2318. $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
  2319. $this->assertEquals('div', $decorator->getOption('tag'));
  2320. }
  2321. public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
  2322. {
  2323. $this->form->setDecorators(array(
  2324. 'FormElements',
  2325. array(array('div' => 'HtmlTag'), array('tag' => 'div')),
  2326. array(array('fieldset' => 'HtmlTag'), array('tag' => 'fieldset')),
  2327. 'Form',
  2328. ));
  2329. $decorator = $this->form->getDecorator('fieldset');
  2330. $decorators = $this->form->getDecorators();
  2331. $i = 0;
  2332. $order = array();
  2333. foreach (array_keys($decorators) as $name) {
  2334. $order[$name] = $i;
  2335. ++$i;
  2336. }
  2337. $this->assertEquals(2, $order['fieldset'], var_export($order, 1));
  2338. }
  2339. // Rendering
  2340. public function checkMarkup($html)
  2341. {
  2342. $this->assertFalse(empty($html));
  2343. $this->assertContains('<form', $html);
  2344. $this->assertRegexp('/<form[^>]+action="' . $this->form->getAction() . '"/', $html);
  2345. $this->assertRegexp('/<form[^>]+method="' . $this->form->getMethod() . '"/i', $html);
  2346. $this->assertRegexp('#<form[^>]+enctype="application/x-www-form-urlencoded"#', $html);
  2347. $this->assertContains('</form>', $html);
  2348. }
  2349. public function testRenderReturnsMarkup()
  2350. {
  2351. $this->setupElements();
  2352. $html = $this->form->render($this->getView());
  2353. $this->checkMarkup($html);
  2354. }
  2355. public function testRenderReturnsMarkupRepresentingAllElements()
  2356. {
  2357. $this->testRenderReturnsMarkup();
  2358. $html = $this->form->render();
  2359. foreach ($this->form->getElements() as $key => $element) {
  2360. $this->assertFalse(empty($key));
  2361. $this->assertFalse(is_numeric($key));
  2362. $this->assertContains('<input', $html);
  2363. $this->assertRegexp('/<input type="text" name="' . $key . '"/', $html);
  2364. }
  2365. }
  2366. public function testRenderReturnsMarkupContainingSubForms()
  2367. {
  2368. $this->setupElements();
  2369. $this->setupSubForm();
  2370. $this->form->setView($this->getView());
  2371. $html = $this->form->render();
  2372. $this->assertRegexp('/<fieldset/', $html);
  2373. $this->assertContains('</fieldset>', $html);
  2374. foreach ($this->form->sub as $key => $item) {
  2375. $this->assertFalse(empty($key));
  2376. $this->assertFalse(is_numeric($key));
  2377. $this->assertContains('<input', $html);
  2378. $pattern = '/<input type="text" name="sub\[' . $key . '\]"/';
  2379. $this->assertRegexp($pattern, $html, 'Pattern: ' . $pattern . "\nHTML:\n" . $html);
  2380. }
  2381. }
  2382. public function testRenderReturnsMarkupContainingDisplayGroups()
  2383. {
  2384. $this->setupElements();
  2385. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz', array('legend' => 'Display Group'));
  2386. $this->form->setView($this->getView());
  2387. $html = $this->html = $this->form->render();
  2388. $this->assertRegexp('/<fieldset/', $html);
  2389. $this->assertContains('</fieldset>', $html);
  2390. $this->assertRegexp('#<legend>Display Group</legend>#', $html, $html);
  2391. $dom = new DOMDocument();
  2392. $dom->loadHTML($html);
  2393. $fieldsets = $dom->getElementsByTagName('fieldset');
  2394. $this->assertTrue(0 < $fieldsets->length);
  2395. $fieldset = $fieldsets->item(0);
  2396. $nodes = $fieldset->childNodes;
  2397. $this->assertNotNull($nodes);
  2398. for ($i = 0; $i < $nodes->length; ++$i) {
  2399. $node = $nodes->item($i);
  2400. if ('input' != $node->nodeName) {
  2401. continue;
  2402. }
  2403. $this->assertTrue($node->hasAttribute('name'));
  2404. $nameNode = $node->getAttributeNode('name');
  2405. switch ($i) {
  2406. case 0:
  2407. $this->assertEquals('foo', $nameNode->nodeValue);
  2408. break;
  2409. case 1:
  2410. $this->assertEquals('baz', $nameNode->nodeValue);
  2411. break;
  2412. default:
  2413. $this->fail('There should only be two input nodes in this display group: ' . $html);
  2414. }
  2415. }
  2416. }
  2417. public function testRenderDoesNotRepeatElementsInDisplayGroups()
  2418. {
  2419. $this->testRenderReturnsMarkupContainingDisplayGroups();
  2420. if (!preg_match_all('#<input[^>]+name="foo"#', $this->html, $matches)) {
  2421. $this->fail("Should find foo element in rendered form");
  2422. }
  2423. $this->assertEquals(1, count($matches));
  2424. $this->assertEquals(1, count($matches[0]));
  2425. }
  2426. public function testElementsRenderAsArrayMembersWhenElementsBelongToAnArray()
  2427. {
  2428. $this->setupElements();
  2429. $this->form->setElementsBelongTo('anArray');
  2430. $html = $this->form->render($this->getView());
  2431. $this->assertContains('name="anArray[foo]"', $html);
  2432. $this->assertContains('name="anArray[bar]"', $html);
  2433. $this->assertContains('name="anArray[baz]"', $html);
  2434. $this->assertContains('id="anArray-foo"', $html);
  2435. $this->assertContains('id="anArray-bar"', $html);
  2436. $this->assertContains('id="anArray-baz"', $html);
  2437. }
  2438. public function testElementsRenderAsSubArrayMembersWhenElementsBelongToASubArray()
  2439. {
  2440. $this->setupElements();
  2441. $this->form->setElementsBelongTo('data[foo]');
  2442. $html = $this->form->render($this->getView());
  2443. $this->assertContains('name="data[foo][foo]"', $html);
  2444. $this->assertContains('name="data[foo][bar]"', $html);
  2445. $this->assertContains('name="data[foo][baz]"', $html);
  2446. $this->assertContains('id="data-foo-foo"', $html);
  2447. $this->assertContains('id="data-foo-bar"', $html);
  2448. $this->assertContains('id="data-foo-baz"', $html);
  2449. }
  2450. public function testElementsRenderAsArrayMembersWhenRenderAsArrayToggled()
  2451. {
  2452. $this->setupElements();
  2453. $this->form->setName('data')
  2454. ->setIsArray(true);
  2455. $html = $this->form->render($this->getView());
  2456. $this->assertContains('name="data[foo]"', $html);
  2457. $this->assertContains('name="data[bar]"', $html);
  2458. $this->assertContains('name="data[baz]"', $html);
  2459. $this->assertContains('id="data-foo"', $html);
  2460. $this->assertContains('id="data-bar"', $html);
  2461. $this->assertContains('id="data-baz"', $html);
  2462. }
  2463. public function testElementsRenderAsMembersOfSubFormsWithElementsBelongTo()
  2464. {
  2465. $this->form->setName('data')
  2466. ->setIsArray(true);
  2467. $subForm = new Zend_Form_SubForm();
  2468. $subForm->setElementsBelongTo('billing[info]');
  2469. $subForm->addElement('text', 'name');
  2470. $subForm->addElement('text', 'number');
  2471. $this->form->addSubForm($subForm, 'sub');
  2472. $html = $this->form->render($this->getView());
  2473. $this->assertContains('name="data[billing][info][name]', $html);
  2474. $this->assertContains('name="data[billing][info][number]', $html);
  2475. $this->assertContains('id="data-billing-info-name"', $html);
  2476. $this->assertContains('id="data-billing-info-number"', $html);
  2477. }
  2478. public function testToStringProxiesToRender()
  2479. {
  2480. $this->setupElements();
  2481. $this->form->setView($this->getView());
  2482. $html = $this->form->__toString();
  2483. $this->checkMarkup($html);
  2484. }
  2485. public function raiseDecoratorException($content, $element, $options)
  2486. {
  2487. throw new Exception('Raising exception in decorator callback');
  2488. }
  2489. public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
  2490. {
  2491. $this->error = $errstr;
  2492. }
  2493. public function testToStringRaisesErrorWhenExceptionCaught()
  2494. {
  2495. $this->form->setDecorators(array(
  2496. array(
  2497. 'decorator' => 'Callback',
  2498. 'options' => array('callback' => array($this, 'raiseDecoratorException'))
  2499. ),
  2500. ));
  2501. $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
  2502. $text = $this->form->__toString();
  2503. restore_error_handler();
  2504. $this->assertTrue(empty($text));
  2505. $this->assertTrue(isset($this->error));
  2506. $this->assertContains('Raising exception in decorator callback', $this->error);
  2507. }
  2508. /**
  2509. * ZF-2718
  2510. */
  2511. public function testHiddenElementsGroupedWhenRendered()
  2512. {
  2513. $this->markTestIncomplete('Scheduling for future release');
  2514. $this->form->addElements(array(
  2515. array('type' => 'hidden', 'name' => 'first', 'options' => array('value' => 'first value')),
  2516. array('type' => 'text', 'name' => 'testone'),
  2517. array('type' => 'hidden', 'name' => 'second', 'options' => array('value' => 'second value')),
  2518. array('type' => 'text', 'name' => 'testtwo'),
  2519. array('type' => 'hidden', 'name' => 'third', 'options' => array('value' => 'third value')),
  2520. array('type' => 'text', 'name' => 'testthree'),
  2521. ));
  2522. $html = $this->form->render($this->getView());
  2523. if (!preg_match('#(<input type="hidden" name="[^>].*>\s*){3}#', $html, $matches)) {
  2524. $this->fail('Hidden elements should be grouped');
  2525. }
  2526. foreach (array('first', 'second', 'third') as $which) {
  2527. $this->assertRegexp('#<input[^]*name="' . $which . '"#', $matches[0]);
  2528. $this->assertRegexp('#<input[^]*value="' . $which . ' value"#', $matches[0]);
  2529. }
  2530. }
  2531. // Localization
  2532. public function testTranslatorIsNullByDefault()
  2533. {
  2534. $this->assertNull($this->form->getTranslator());
  2535. }
  2536. public function testCanSetTranslator()
  2537. {
  2538. require_once 'Zend/Translate/Adapter/Array.php';
  2539. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2540. $this->form->setTranslator($translator);
  2541. $received = $this->form->getTranslator($translator);
  2542. $this->assertSame($translator->getAdapter(), $received);
  2543. }
  2544. public function testCanSetDefaultGlobalTranslator()
  2545. {
  2546. $this->assertNull($this->form->getTranslator());
  2547. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2548. Zend_Form::setDefaultTranslator($translator);
  2549. $received = Zend_Form::getDefaultTranslator();
  2550. $this->assertSame($translator->getAdapter(), $received);
  2551. $received = $this->form->getTranslator();
  2552. $this->assertSame($translator->getAdapter(), $received);
  2553. $form = new Zend_Form();
  2554. $received = $form->getTranslator();
  2555. $this->assertSame($translator->getAdapter(), $received);
  2556. }
  2557. public function testLocalTranslatorPreferredOverDefaultGlobalTranslator()
  2558. {
  2559. $this->assertNull($this->form->getTranslator());
  2560. $translatorDefault = new Zend_Translate('array', array('foo' => 'bar'));
  2561. Zend_Form::setDefaultTranslator($translatorDefault);
  2562. $received = $this->form->getTranslator();
  2563. $this->assertSame($translatorDefault->getAdapter(), $received);
  2564. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2565. $this->form->setTranslator($translator);
  2566. $received = $this->form->getTranslator();
  2567. $this->assertNotSame($translatorDefault->getAdapter(), $received);
  2568. $this->assertSame($translator->getAdapter(), $received);
  2569. }
  2570. public function testTranslatorFromRegistryUsedWhenNoneRegistered()
  2571. {
  2572. $this->assertNull($this->form->getTranslator());
  2573. $translator = new Zend_Translate('array', array('foo' => 'bar'));
  2574. Zend_Registry::set('Zend_Translate', $translator);
  2575. $received = Zend_Form::getDefaultTranslator();
  2576. $this->assertSame($translator->getAdapter(), $received);
  2577. $received = $this->form->getTranslator();
  2578. $this->assertSame($translator->getAdapter(), $received);
  2579. $form = new Zend_Form();
  2580. $received = $form->getTranslator();
  2581. $this->assertSame($translator->getAdapter(), $received);
  2582. }
  2583. public function testCanDisableTranslation()
  2584. {
  2585. $this->testCanSetDefaultGlobalTranslator();
  2586. $this->form->setDisableTranslator(true);
  2587. $this->assertNull($this->form->getTranslator());
  2588. }
  2589. // Iteration
  2590. public function testFormObjectIsIterableAndIteratesElements()
  2591. {
  2592. $this->setupElements();
  2593. $expected = array('foo', 'bar', 'baz');
  2594. $received = array();
  2595. foreach ($this->form as $key => $value) {
  2596. $received[] = $key;
  2597. }
  2598. $this->assertSame($expected, $received);
  2599. }
  2600. public function testFormObjectIteratesElementsInExpectedOrder()
  2601. {
  2602. $this->setupElements();
  2603. $this->form->addElement('text', 'checkorder', array('order' => 2));
  2604. $expected = array('foo', 'bar', 'checkorder', 'baz');
  2605. $received = array();
  2606. foreach ($this->form as $key => $value) {
  2607. $received[] = $key;
  2608. $this->assertTrue($value instanceof Zend_Form_Element);
  2609. }
  2610. $this->assertSame($expected, $received);
  2611. }
  2612. public function testFormObjectIteratesElementsInExpectedOrderWhenAllElementsHaveOrder()
  2613. {
  2614. $this->form->addElement('submit', 'submit')->submit->setLabel('Submit')->setOrder(30);
  2615. $this->form->addElement('text', 'name')->name->setLabel('Name')->setOrder(10);
  2616. $this->form->addElement('text', 'email')->email->setLabel('E-mail')->setOrder(20);
  2617. $expected = array('name', 'email', 'submit');
  2618. $received = array();
  2619. foreach ($this->form as $key => $value) {
  2620. $received[] = $key;
  2621. $this->assertTrue($value instanceof Zend_Form_Element);
  2622. }
  2623. $this->assertSame($expected, $received);
  2624. }
  2625. public function testFormObjectIteratesElementsInExpectedOrderWhenFirstElementHasNoOrderSpecified()
  2626. {
  2627. $this->form->addElement(new Zend_Form_Element('a',array('label'=>'a')))
  2628. ->addElement(new Zend_Form_Element('b',array('label'=>'b', 'order' => 0)))
  2629. ->addElement(new Zend_Form_Element('c',array('label'=>'c', 'order' => 1)))
  2630. ->setView($this->getView());
  2631. $test = $this->form->render();
  2632. $this->assertContains('name="a"', $test);
  2633. if (!preg_match_all('/(<input[^>]+>)/', $test, $matches)) {
  2634. $this->fail('Expected markup not found');
  2635. }
  2636. $order = array();
  2637. foreach ($matches[1] as $element) {
  2638. if (preg_match('/name="(a|b|c)"/', $element, $m)) {
  2639. $order[] = $m[1];
  2640. }
  2641. }
  2642. $this->assertSame(array('b', 'c', 'a'), $order);
  2643. }
  2644. public function testFormObjectIteratesElementsAndSubforms()
  2645. {
  2646. $this->setupElements();
  2647. $this->setupSubForm();
  2648. $expected = array('foo', 'bar', 'baz', 'sub');
  2649. $received = array();
  2650. foreach ($this->form as $key => $value) {
  2651. $received[] = $key;
  2652. $this->assertTrue(($value instanceof Zend_Form_Element)
  2653. or ($value instanceof Zend_Form_SubForm));
  2654. }
  2655. $this->assertSame($expected, $received);
  2656. }
  2657. public function testFormObjectIteratesDisplayGroupsButSkipsDisplayGroupElements()
  2658. {
  2659. $this->setupElements();
  2660. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2661. $expected = array('bar', 'foobaz');
  2662. $received = array();
  2663. foreach ($this->form as $key => $value) {
  2664. $received[] = $key;
  2665. $this->assertTrue(($value instanceof Zend_Form_Element)
  2666. or ($value instanceof Zend_Form_DisplayGroup));
  2667. }
  2668. $this->assertSame($expected, $received);
  2669. }
  2670. public function testRemovingFormItemsShouldNotRaiseExceptionsDuringIteration()
  2671. {
  2672. $this->setupElements();
  2673. $bar = $this->form->bar;
  2674. $this->form->removeElement('bar');
  2675. try {
  2676. foreach ($this->form as $item) {
  2677. }
  2678. } catch (Exception $e) {
  2679. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2680. }
  2681. $this->form->addElement($bar);
  2682. $this->form->addDisplayGroup(array('baz', 'bar'), 'bazbar');
  2683. $this->form->removeDisplayGroup('bazbar');
  2684. try {
  2685. foreach ($this->form as $item) {
  2686. }
  2687. } catch (Exception $e) {
  2688. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2689. }
  2690. $subForm = new Zend_Form_SubForm;
  2691. $subForm->addElements(array('foo' => 'text', 'bar' => 'text'));
  2692. $this->form->addSubForm($subForm, 'page1');
  2693. $this->form->removeSubForm('page1');
  2694. try {
  2695. foreach ($this->form as $item) {
  2696. }
  2697. } catch (Exception $e) {
  2698. $this->fail('Exceptions should not be raised by iterator when elements are removed; error message: ' . $e->getMessage());
  2699. }
  2700. }
  2701. public function testClearingAttachedItemsShouldNotCauseIterationToRaiseExceptions()
  2702. {
  2703. $form = new Zend_Form();
  2704. $form->addElements(array(
  2705. 'username' => 'text',
  2706. 'password' => 'text',
  2707. ));
  2708. $form->clearElements();
  2709. try {
  2710. foreach ($form as $item) {
  2711. }
  2712. } catch (Zend_Form_Exception $e) {
  2713. $message = "Clearing elements prior to iteration should not cause iteration to fail;\n"
  2714. . $e->getMessage();
  2715. $this->fail($message);
  2716. }
  2717. $form->addElements(array(
  2718. 'username' => 'text',
  2719. 'password' => 'text',
  2720. ))
  2721. ->addDisplayGroup(array('username', 'password'), 'login');
  2722. $form->clearDisplayGroups();
  2723. try {
  2724. foreach ($form as $item) {
  2725. }
  2726. } catch (Zend_Form_Exception $e) {
  2727. $message = "Clearing display groups prior to iteration should not cause iteration to fail;\n"
  2728. . $e->getMessage();
  2729. $this->fail($message);
  2730. }
  2731. $subForm = new Zend_Form_SubForm();
  2732. $form->addSubForm($subForm, 'foo');
  2733. $form->clearSubForms();
  2734. try {
  2735. foreach ($form as $item) {
  2736. }
  2737. } catch (Zend_Form_Exception $e) {
  2738. $message = "Clearing sub forms prior to iteration should not cause iteration to fail;\n"
  2739. . $e->getMessage();
  2740. $this->fail($message);
  2741. }
  2742. }
  2743. // Countable
  2744. public function testCanCountFormObject()
  2745. {
  2746. $this->setupElements();
  2747. $this->assertEquals(3, count($this->form));
  2748. }
  2749. public function testCountingFormObjectCountsSubForms()
  2750. {
  2751. $this->setupElements();
  2752. $this->setupSubForm();
  2753. $this->assertEquals(4, count($this->form));
  2754. }
  2755. public function testCountingFormCountsDisplayGroupsButOmitsElementsInDisplayGroups()
  2756. {
  2757. $this->testCountingFormObjectCountsSubForms();
  2758. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2759. $this->assertEquals(3, count($this->form));
  2760. }
  2761. // Element decorators and plugin paths
  2762. public function testCanSetAllElementDecoratorsAtOnce()
  2763. {
  2764. $this->_checkZf2794();
  2765. $this->setupElements();
  2766. $this->form->setElementDecorators(array(
  2767. array('ViewHelper'),
  2768. array('Label'),
  2769. array('Fieldset'),
  2770. ));
  2771. foreach ($this->form->getElements() as $element) {
  2772. $this->assertFalse($element->getDecorator('Errors'));
  2773. $this->assertFalse($element->getDecorator('HtmlTag'));
  2774. $decorator = $element->getDecorator('ViewHelper');
  2775. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  2776. $decorator = $element->getDecorator('Label');
  2777. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
  2778. $decorator = $element->getDecorator('Fieldset');
  2779. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2780. }
  2781. }
  2782. /**
  2783. * @group ZF-3597
  2784. */
  2785. public function testSettingElementDecoratorsWithConcreteDecoratorShouldHonorOrder()
  2786. {
  2787. $this->form->setDecorators(array(
  2788. 'FormElements',
  2789. array('HtmlTag', array('tag' => 'table')),
  2790. 'Form',
  2791. ));
  2792. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators/', 'decorator');
  2793. $this->form->addElement('text', 'test', array(
  2794. 'label' => 'Foo',
  2795. 'description' => 'sample description',
  2796. ));
  2797. require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
  2798. $decorator = new My_Decorator_TableRow();
  2799. $this->form->setElementDecorators(array(
  2800. 'ViewHelper',
  2801. $decorator,
  2802. ));
  2803. $html = $this->form->render($this->getView());
  2804. $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
  2805. }
  2806. /**#@+
  2807. * @group ZF-3228
  2808. */
  2809. public function testShouldAllowSpecifyingSpecificElementsToDecorate()
  2810. {
  2811. $this->_checkZf2794();
  2812. $this->setupElements();
  2813. $this->form->setElementDecorators(
  2814. array(
  2815. 'Description',
  2816. 'Form',
  2817. 'Fieldset',
  2818. ),
  2819. array(
  2820. 'bar',
  2821. )
  2822. );
  2823. $element = $this->form->bar;
  2824. $this->assertFalse($element->getDecorator('ViewHelper'));
  2825. $this->assertFalse($element->getDecorator('Errors'));
  2826. $this->assertFalse($element->getDecorator('Label'));
  2827. $this->assertFalse($element->getDecorator('HtmlTag'));
  2828. $decorator = $element->getDecorator('Description');
  2829. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2830. $decorator = $element->getDecorator('Form');
  2831. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2832. $decorator = $element->getDecorator('Fieldset');
  2833. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2834. foreach (array('foo', 'baz') as $name) {
  2835. $element = $this->form->$name;
  2836. $this->assertFalse($element->getDecorator('Form'));
  2837. $this->assertFalse($element->getDecorator('Fieldset'));
  2838. }
  2839. }
  2840. public function testShouldAllowSpecifyingListOfElementsNotToDecorate()
  2841. {
  2842. $this->_checkZf2794();
  2843. $this->setupElements();
  2844. $this->form->setElementDecorators(
  2845. array(
  2846. 'Description',
  2847. 'Form',
  2848. 'Fieldset',
  2849. ),
  2850. array(
  2851. 'foo',
  2852. 'baz',
  2853. ),
  2854. false
  2855. );
  2856. $element = $this->form->bar;
  2857. $this->assertFalse($element->getDecorator('ViewHelper'));
  2858. $this->assertFalse($element->getDecorator('Errors'));
  2859. $this->assertFalse($element->getDecorator('Label'));
  2860. $this->assertFalse($element->getDecorator('HtmlTag'));
  2861. $decorator = $element->getDecorator('Description');
  2862. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
  2863. $decorator = $element->getDecorator('Form');
  2864. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
  2865. $decorator = $element->getDecorator('Fieldset');
  2866. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Fieldset);
  2867. foreach (array('foo', 'baz') as $name) {
  2868. $element = $this->form->$name;
  2869. $this->assertFalse($element->getDecorator('Form'));
  2870. $this->assertFalse($element->getDecorator('Fieldset'));
  2871. }
  2872. }
  2873. /**#@-*/
  2874. public function testCanSetAllElementFiltersAtOnce()
  2875. {
  2876. $this->_checkZf2794();
  2877. $this->setupElements();
  2878. $this->form->setElementFilters(array(
  2879. 'Alnum',
  2880. 'StringToLower'
  2881. ));
  2882. foreach ($this->form->getElements() as $element) {
  2883. $filter = $element->getFilter('Alnum');
  2884. $this->assertTrue($filter instanceof Zend_Filter_Alnum);
  2885. $filter = $element->getFilter('StringToLower');
  2886. $this->assertTrue($filter instanceof Zend_Filter_StringToLower);
  2887. }
  2888. }
  2889. public function testCanSetGlobalElementPrefixPath()
  2890. {
  2891. $this->setupElements();
  2892. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2893. $this->form->addElement('text', 'prefixTest');
  2894. foreach ($this->form->getElements() as $element) {
  2895. $loader = $element->getPluginLoader('validate');
  2896. $paths = $loader->getPaths('Zend_Foo_Validate');
  2897. $this->assertFalse(empty($paths), $element->getName() . ':' . var_export($loader->getPaths(), 1));
  2898. $this->assertContains('Foo', $paths[0]);
  2899. $this->assertContains('Validate', $paths[0]);
  2900. $paths = $element->getPluginLoader('filter')->getPaths('Zend_Foo_Filter');
  2901. $this->assertFalse(empty($paths));
  2902. $this->assertContains('Foo', $paths[0]);
  2903. $this->assertContains('Filter', $paths[0]);
  2904. $paths = $element->getPluginLoader('decorator')->getPaths('Zend_Foo_Decorator');
  2905. $this->assertFalse(empty($paths));
  2906. $this->assertContains('Foo', $paths[0]);
  2907. $this->assertContains('Decorator', $paths[0]);
  2908. }
  2909. }
  2910. public function testCustomGlobalElementPrefixPathUsedInNewlyCreatedElements()
  2911. {
  2912. $this->_checkZf2794();
  2913. $this->form->addElementPrefixPath('My_Decorator', dirname(__FILE__) . '/_files/decorators', 'decorator');
  2914. $this->form->addElement('text', 'prefixTest');
  2915. $element = $this->form->prefixTest;
  2916. $label = $element->getDecorator('Label');
  2917. $this->assertTrue($label instanceof My_Decorator_Label, get_class($label));
  2918. }
  2919. /**
  2920. * @group ZF-3093
  2921. */
  2922. public function testSettingElementPrefixPathPropagatesToAttachedSubForms()
  2923. {
  2924. $subForm = new Zend_Form_SubForm();
  2925. $subForm->addElement('text', 'foo');
  2926. $this->form->addSubForm($subForm, 'subForm');
  2927. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/');
  2928. $loader = $this->form->subForm->foo->getPluginLoader('decorator');
  2929. $paths = $loader->getPaths('Zend_Foo_Decorator');
  2930. $this->assertFalse(empty($paths));
  2931. $this->assertContains('Foo', $paths[0]);
  2932. $this->assertContains('Decorator', $paths[0]);
  2933. }
  2934. public function testCanSetElementValidatorPrefixPath()
  2935. {
  2936. $this->setupElements();
  2937. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'validate');
  2938. $this->form->addElement('text', 'prefixTest');
  2939. foreach ($this->form->getElements() as $element) {
  2940. $loader = $element->getPluginLoader('validate');
  2941. $paths = $loader->getPaths('Zend_Foo');
  2942. $this->assertFalse(empty($paths));
  2943. $this->assertContains('Foo', $paths[0]);
  2944. $this->assertNotContains('Validate', $paths[0]);
  2945. }
  2946. }
  2947. public function testCanSetElementFilterPrefixPath()
  2948. {
  2949. $this->setupElements();
  2950. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'filter');
  2951. $this->form->addElement('text', 'prefixTest');
  2952. foreach ($this->form->getElements() as $element) {
  2953. $loader = $element->getPluginLoader('filter');
  2954. $paths = $loader->getPaths('Zend_Foo');
  2955. $this->assertFalse(empty($paths));
  2956. $this->assertContains('Foo', $paths[0]);
  2957. $this->assertNotContains('Filter', $paths[0]);
  2958. }
  2959. }
  2960. public function testCanSetElementDecoratorPrefixPath()
  2961. {
  2962. $this->setupElements();
  2963. $this->form->addElementPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
  2964. $this->form->addElement('text', 'prefixTest');
  2965. foreach ($this->form->getElements() as $element) {
  2966. $loader = $element->getPluginLoader('decorator');
  2967. $paths = $loader->getPaths('Zend_Foo');
  2968. $this->assertFalse(empty($paths));
  2969. $this->assertContains('Foo', $paths[0]);
  2970. $this->assertNotContains('Decorator', $paths[0]);
  2971. }
  2972. }
  2973. // Display Group decorators and plugin paths
  2974. public function setupDisplayGroups()
  2975. {
  2976. $this->testCanAddAndRetrieveMultipleElements();
  2977. $this->form->addElements(array(
  2978. 'test1' => 'text',
  2979. 'test2' => 'text',
  2980. 'test3' => 'text',
  2981. 'test4' => 'text'
  2982. ));
  2983. $this->form->addDisplayGroup(array('bar', 'bat'), 'barbat');
  2984. $this->form->addDisplayGroup(array('foo', 'baz'), 'foobaz');
  2985. }
  2986. public function testCanSetAllDisplayGroupDecoratorsAtOnce()
  2987. {
  2988. $this->_checkZf2794();
  2989. $this->setupDisplayGroups();
  2990. $this->form->setDisplayGroupDecorators(array(
  2991. array('Callback', array('callback' => 'strip_tags')),
  2992. ));
  2993. foreach ($this->form->getDisplayGroups() as $element) {
  2994. $this->assertFalse($element->getDecorator('FormElements'));
  2995. $this->assertFalse($element->getDecorator('HtmlTag'));
  2996. $this->assertFalse($element->getDecorator('Fieldset'));
  2997. $this->assertFalse($element->getDecorator('DtDdWrapper'));
  2998. $decorator = $element->getDecorator('Callback');
  2999. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3000. }
  3001. }
  3002. public function testCanSetDisplayGroupPrefixPath()
  3003. {
  3004. $this->setupDisplayGroups();
  3005. $this->form->addDisplayGroupPrefixPath('Zend_Foo', 'Zend/Foo/');
  3006. $this->form->addDisplayGroup(array('test1', 'test2'), 'testgroup');
  3007. foreach ($this->form->getDisplayGroups() as $group) {
  3008. $loader = $group->getPluginLoader();
  3009. $paths = $loader->getPaths('Zend_Foo');
  3010. $this->assertFalse(empty($paths));
  3011. $this->assertContains('Foo', $paths[0]);
  3012. }
  3013. }
  3014. /**
  3015. * @group ZF-3213
  3016. */
  3017. public function testShouldAllowSettingDisplayGroupPrefixPathViaConfigOptions()
  3018. {
  3019. require_once 'Zend/Config/Ini.php';
  3020. $config = new Zend_Config_Ini(dirname(__FILE__) . '/_files/config/zf3213.ini', 'form');
  3021. $form = new Zend_Form($config);
  3022. $dg = $form->foofoo;
  3023. $paths = $dg->getPluginLoader()->getPaths('My_Decorator');
  3024. $this->assertTrue($paths !== false);
  3025. }
  3026. // Subform decorators
  3027. public function testCanSetAllSubFormDecoratorsAtOnce()
  3028. {
  3029. $this->_checkZf2794();
  3030. $this->setupSubForm();
  3031. $this->form->setSubFormDecorators(array(
  3032. array('Callback', array('callback' => 'strip_tags')),
  3033. ));
  3034. foreach ($this->form->getSubForms() as $subForm) {
  3035. $this->assertFalse($subForm->getDecorator('FormElements'));
  3036. $this->assertFalse($subForm->getDecorator('HtmlTag'));
  3037. $this->assertFalse($subForm->getDecorator('Fieldset'));
  3038. $this->assertFalse($subForm->getDecorator('DtDdWrapper'));
  3039. $decorator = $subForm->getDecorator('Callback');
  3040. $this->assertTrue($decorator instanceof Zend_Form_Decorator_Callback);
  3041. }
  3042. }
  3043. // Extension
  3044. public function testInitCalledPriorToLoadingDefaultDecorators()
  3045. {
  3046. $form = new Zend_Form_FormTest_FormExtension();
  3047. $decorators = $form->getDecorators();
  3048. $this->assertTrue(empty($decorators));
  3049. }
  3050. // Clone
  3051. /**
  3052. * @group ZF-3819
  3053. */
  3054. public function testCloningShouldCloneAllChildren()
  3055. {
  3056. $form = new Zend_Form();
  3057. $foo = new Zend_Form_SubForm(array(
  3058. 'name' => 'foo',
  3059. 'elements' => array(
  3060. 'one' => 'text',
  3061. 'two' => 'text',
  3062. ),
  3063. ));
  3064. $form->addElement('text', 'bar')
  3065. ->addElement('text', 'baz')
  3066. ->addElement('text', 'bat')
  3067. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3068. ->addSubForm($foo, 'foo');
  3069. $bar = $form->bar;
  3070. $baz = $form->baz;
  3071. $bat = $form->bat;
  3072. $barbat = $form->barbat;
  3073. $cloned = clone $form;
  3074. $this->assertNotSame($foo, $cloned->foo);
  3075. $this->assertNotSame($bar, $cloned->bar);
  3076. $this->assertNotSame($baz, $cloned->baz);
  3077. $this->assertNotSame($bat, $cloned->bat);
  3078. $this->assertNotSame($barbat, $cloned->getDisplayGroup('barbat'));
  3079. $this->assertNotSame($foo->one, $cloned->foo->one);
  3080. $this->assertNotSame($foo->two, $cloned->foo->two);
  3081. }
  3082. // Reset
  3083. /**
  3084. * @group ZF-3227
  3085. */
  3086. public function testFormsShouldAllowResetting()
  3087. {
  3088. $form = new Zend_Form();
  3089. $foo = new Zend_Form_SubForm(array(
  3090. 'name' => 'foo',
  3091. 'elements' => array(
  3092. 'one' => 'text',
  3093. 'two' => 'text',
  3094. ),
  3095. ));
  3096. $form->addElement('text', 'bar')
  3097. ->addElement('text', 'baz')
  3098. ->addElement('text', 'bat')
  3099. ->addDisplayGroup(array('bar', 'bat'), 'barbat')
  3100. ->addSubForm($foo, 'foo');
  3101. $values = array(
  3102. 'bar' => 'Bar Value',
  3103. 'baz' => 'Baz Value',
  3104. 'bat' => 'Bat Value',
  3105. 'foo' => array(
  3106. 'one' => 'One Value',
  3107. 'two' => 'Two Value',
  3108. ),
  3109. );
  3110. $form->populate($values);
  3111. $test = $form->getValues();
  3112. $this->assertEquals($values, $test);
  3113. $form->reset();
  3114. $test = $form->getValues();
  3115. $this->assertNotEquals($values, $test);
  3116. $this->assertEquals(0, array_sum($test));
  3117. }
  3118. /**
  3119. * @group ZF-3217
  3120. */
  3121. public function testFormShouldOverloadToRenderDecorators()
  3122. {
  3123. $this->setupElements();
  3124. $this->form->setView($this->getView());
  3125. $html = $this->form->renderFormElements();
  3126. foreach ($this->form->getElements() as $element) {
  3127. $this->assertContains('id="' . $element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
  3128. }
  3129. $this->assertNotContains('<dl', $html);
  3130. $this->assertNotContains('<form', $html);
  3131. $html = $this->form->renderForm('this is the content');
  3132. $this->assertContains('<form', $html);
  3133. $this->assertContains('</form>', $html);
  3134. $this->assertContains('this is the content', $html);
  3135. }
  3136. /**
  3137. * @group ZF-3217
  3138. * @expectedException Zend_Form_Exception
  3139. */
  3140. public function testOverloadingToInvalidMethodsShouldThrowAnException()
  3141. {
  3142. $html = $this->form->bogusMethodCall();
  3143. }
  3144. /**
  3145. * @group ZF-2950
  3146. */
  3147. public function testDtDdElementsWithLabelGetUniqueId()
  3148. {
  3149. $form = new Zend_Form();
  3150. $form->setView($this->getView());
  3151. $fooElement = new Zend_Form_Element_Text('foo');
  3152. $fooElement->setLabel('Foo');
  3153. $form->addElement($fooElement);
  3154. $html = $form->render();
  3155. $this->assertContains('<dt id="foo-label">', $html);
  3156. $this->assertContains('<dd id="foo-element">', $html);
  3157. }
  3158. /**
  3159. * @group ZF-2950
  3160. */
  3161. public function testDtDdElementsWithoutLabelGetUniqueId()
  3162. {
  3163. $form = new Zend_Form();
  3164. $form->setView($this->getView())
  3165. ->addElement(new Zend_Form_Element_Text('foo'));
  3166. $html = $form->render();
  3167. $this->assertContains('<dt id="foo-label">&nbsp;</dt>', $html);
  3168. $this->assertContains('<dd id="foo-element">', $html);
  3169. }
  3170. /**
  3171. * @group ZF-2950
  3172. */
  3173. public function testSubFormGetsUniqueIdWithName()
  3174. {
  3175. $form = new Zend_Form();
  3176. $form->setView($this->getView())
  3177. ->setName('testform')
  3178. ->addSubForm(new Zend_Form_SubForm(), 'testform');
  3179. $html = $form->render();
  3180. $this->assertContains('<dt id="testform-label">&nbsp;</dt>', $html);
  3181. $this->assertContains('<dd id="testform-element">', $html);
  3182. }
  3183. /**
  3184. * @group ZF-5370
  3185. */
  3186. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToForm()
  3187. {
  3188. $file = new Zend_Form_Element_File('txt');
  3189. $this->form->addElement($file);
  3190. $html = $this->form->render($this->getView());
  3191. $this->assertFalse(empty($html));
  3192. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html);
  3193. }
  3194. /**
  3195. * @group ZF-5370
  3196. */
  3197. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToSubForm()
  3198. {
  3199. $subForm = new Zend_Form_SubForm();
  3200. $subForm->addElement('file', 'txt');
  3201. $this->form->addSubForm($subForm, 'page1')
  3202. ->setView(new Zend_View);
  3203. $html = $this->form->render();
  3204. $this->assertContains('id="txt"', $html);
  3205. $this->assertContains('name="txt"', $html);
  3206. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3207. }
  3208. /**
  3209. * @group ZF-5370
  3210. */
  3211. public function testEnctypeDefaultsToMultipartWhenFileElementIsAttachedToDisplayGroup()
  3212. {
  3213. $this->form->addElement('file', 'txt')
  3214. ->addDisplayGroup(array('txt'), 'txtdisplay')
  3215. ->setView(new Zend_View);
  3216. $html = $this->form->render();
  3217. $this->assertContains('id="txt"', $html);
  3218. $this->assertContains('name="txt"', $html);
  3219. $this->assertRegexp('#<form[^>]+enctype="multipart/form-data"#', $html, $html);
  3220. }
  3221. /**
  3222. * @group ZF-6070
  3223. */
  3224. public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
  3225. {
  3226. $this->form->setOptions(array(
  3227. 'elementDecorators' => array(
  3228. 'ViewHelper',
  3229. 'Label',
  3230. ),
  3231. 'elements' => array(
  3232. 'foo' => array(
  3233. 'type' => 'text',
  3234. 'options' => array(
  3235. 'decorators' => array(
  3236. 'Errors',
  3237. 'ViewHelper',
  3238. ),
  3239. ),
  3240. ),
  3241. ),
  3242. ));
  3243. $element = $this->form->getElement('foo');
  3244. $expected = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
  3245. $actual = array();
  3246. foreach ($element->getDecorators() as $decorator) {
  3247. $actual[] = get_class($decorator);
  3248. }
  3249. $this->assertSame($expected, $actual);
  3250. }
  3251. /**
  3252. * @group ZF-5150
  3253. */
  3254. public function testIsValidShouldFailIfAddErrorHasBeenCalled()
  3255. {
  3256. $this->form->addError('Error');
  3257. $this->assertFalse($this->form->isValid(array()));
  3258. }
  3259. /**
  3260. * @group ZF-8494
  3261. */
  3262. public function testGetValidValues()
  3263. {
  3264. $data = array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement');
  3265. require_once "Zend/Validate/Int.php";
  3266. $validElement = new Zend_Form_Element("valid");
  3267. $validElement->addValidator(new Zend_Validate_Int());
  3268. $this->form->addElement($validElement);
  3269. $invalidElement = new Zend_Form_Element('invalid');
  3270. $invalidElement->addValidator(new Zend_Validate_Int());
  3271. $this->form->addElement($invalidElement);
  3272. $this->assertEquals(array('valid' => 1234), $this->form->getValidValues($data));
  3273. }
  3274. /**
  3275. * @group ZF-8494
  3276. */
  3277. public function testGetValidSubFormValues()
  3278. {
  3279. $data = array('sub' => array('valid' => 1234, 'invalid' => 'invalid', 'noElement' => 'noElement'));
  3280. require_once "Zend/Validate/Int.php";
  3281. $subForm = new Zend_Form_SubForm();
  3282. $validElement = new Zend_Form_Element("valid");
  3283. $validElement->addValidator(new Zend_Validate_Int());
  3284. $subForm->addElement($validElement);
  3285. $invalidElement = new Zend_Form_Element('invalid');
  3286. $invalidElement->addValidator(new Zend_Validate_Int());
  3287. $subForm->addElement($invalidElement);
  3288. $this->form->addSubForm($subForm, 'sub');
  3289. $this->assertEquals(array('sub' => array('valid' => 1234)), $this->form->getValidValues($data));
  3290. }
  3291. /**
  3292. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  3293. *
  3294. * @link http://framework.zend.com/issues/browse/ZF-2794
  3295. * @return void
  3296. */
  3297. protected function _checkZf2794()
  3298. {
  3299. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  3300. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  3301. }
  3302. }
  3303. }
  3304. class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup
  3305. {
  3306. }
  3307. class Zend_Form_FormTest_FormExtension extends Zend_Form
  3308. {
  3309. public function init()
  3310. {
  3311. $this->setDisableLoadDefaultDecorators(true);
  3312. }
  3313. }
  3314. if (PHPUnit_MAIN_METHOD == 'Zend_Form_FormTest::main') {
  3315. Zend_Form_FormTest::main();
  3316. }