FormHelperTest.php 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * Contact class
  39. *
  40. */
  41. class ContactsTable extends Table {
  42. /**
  43. * Default schema
  44. *
  45. * @var array
  46. */
  47. protected $_schema = array(
  48. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  49. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  50. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  51. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  52. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  53. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  54. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  55. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  56. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  57. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  58. );
  59. /**
  60. * Initializes the schema
  61. *
  62. * @return void
  63. */
  64. public function initialize(array $config) {
  65. $this->schema($this->_schema);
  66. }
  67. }
  68. /**
  69. * ValidateUser class
  70. *
  71. */
  72. class ValidateUsersTable extends Table {
  73. /**
  74. * schema method
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'balance' => array('type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2),
  83. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3),
  84. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6),
  85. 'population' => array('type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0),
  86. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  87. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  88. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  89. );
  90. /**
  91. * Initializes the schema
  92. *
  93. * @return void
  94. */
  95. public function initialize(array $config) {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase {
  105. /**
  106. * Fixtures to be used
  107. *
  108. * @var array
  109. */
  110. public $fixtures = array('core.article', 'core.comment');
  111. /**
  112. * Do not load the fixtures by default
  113. *
  114. * @var boolean
  115. */
  116. public $autoFixtures = false;
  117. /**
  118. * setUp method
  119. *
  120. * @return void
  121. */
  122. public function setUp() {
  123. parent::setUp();
  124. Configure::write('Config.language', 'eng');
  125. Configure::write('App.base', '');
  126. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  127. Configure::delete('Asset');
  128. $this->View = new View(null);
  129. $this->Form = new FormHelper($this->View);
  130. $this->Form->Html = new HtmlHelper($this->View);
  131. $this->Form->request = new Request('articles/add');
  132. $this->Form->request->here = '/articles/add';
  133. $this->Form->request['controller'] = 'articles';
  134. $this->Form->request['action'] = 'add';
  135. $this->Form->request->webroot = '';
  136. $this->Form->request->base = '';
  137. $this->dateRegex = array(
  138. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  139. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  140. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  142. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  143. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  144. );
  145. $this->article = [
  146. 'schema' => [
  147. 'id' => ['type' => 'integer'],
  148. 'author_id' => ['type' => 'integer', 'null' => true],
  149. 'title' => ['type' => 'string', 'null' => true],
  150. 'body' => 'text',
  151. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  152. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  153. ],
  154. 'required' => [
  155. 'author_id' => true,
  156. 'title' => true,
  157. ]
  158. ];
  159. Configure::write('Security.salt', 'foo!');
  160. Router::connect('/:controller', array('action' => 'index'));
  161. Router::connect('/:controller/:action/*');
  162. }
  163. /**
  164. * tearDown method
  165. *
  166. * @return void
  167. */
  168. public function tearDown() {
  169. parent::tearDown();
  170. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  171. TableRegistry::clear();
  172. }
  173. /**
  174. * Test registering a new widget class and rendering it.
  175. *
  176. * @return void
  177. */
  178. public function testAddWidgetAndRenderWidget() {
  179. $data = [
  180. 'val' => 1
  181. ];
  182. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  183. $this->assertNull($this->Form->addWidget('test', $mock));
  184. $mock->expects($this->once())
  185. ->method('render')
  186. ->with($data)
  187. ->will($this->returnValue('HTML'));
  188. $result = $this->Form->widget('test', $data);
  189. $this->assertEquals('HTML', $result);
  190. }
  191. /**
  192. * Test registering an invalid widget class.
  193. *
  194. * @expectedException \RuntimeException
  195. * @return void
  196. */
  197. public function testAddWidgetInvalid() {
  198. $mock = new \StdClass();
  199. $this->Form->addWidget('test', $mock);
  200. $this->Form->widget('test');
  201. }
  202. /**
  203. * Test adding a new context class.
  204. *
  205. * @return void
  206. */
  207. public function testAddContextProvider() {
  208. $context = 'My data';
  209. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  210. $this->assertInstanceOf('Cake\Network\Request', $request);
  211. $this->assertEquals($context, $data['entity']);
  212. return $this->getMock('Cake\View\Form\ContextInterface');
  213. });
  214. $this->Form->create($context);
  215. }
  216. /**
  217. * Test adding an invalid context class.
  218. *
  219. * @expectedException RuntimeException
  220. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  221. * @return void
  222. */
  223. public function testAddContextProviderInvalid() {
  224. $context = 'My data';
  225. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  226. return new \StdClass();
  227. });
  228. $this->Form->create($context);
  229. }
  230. /**
  231. * Provides context options for create().
  232. *
  233. * @return array
  234. */
  235. public function contextSelectionProvider() {
  236. $entity = new Article();
  237. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  238. $data = [
  239. 'schema' => [
  240. 'title' => ['type' => 'string']
  241. ]
  242. ];
  243. return [
  244. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  245. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  246. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  247. 'none' => [null, 'Cake\View\Form\NullContext'],
  248. 'false' => [false, 'Cake\View\Form\NullContext'],
  249. ];
  250. }
  251. /**
  252. * Test default context selection in create()
  253. *
  254. * @dataProvider contextSelectionProvider
  255. * @return void
  256. */
  257. public function testCreateContextSelectionBuiltIn($data, $class) {
  258. $this->loadFixtures('Article');
  259. $this->Form->create($data);
  260. $this->assertInstanceOf($class, $this->Form->context());
  261. }
  262. /**
  263. * Data provider for type option.
  264. *
  265. * @return array
  266. */
  267. public static function requestTypeProvider() {
  268. return [
  269. // type, method, override
  270. ['post', 'post', 'POST'],
  271. ['put', 'post', 'PUT'],
  272. ['patch', 'post', 'PATCH'],
  273. ['delete', 'post', 'DELETE'],
  274. ];
  275. }
  276. /**
  277. * Test creating file forms.
  278. *
  279. * @return void
  280. */
  281. public function testCreateFile() {
  282. $encoding = strtolower(Configure::read('App.encoding'));
  283. $result = $this->Form->create(false, array('type' => 'file'));
  284. $expected = array(
  285. 'form' => array(
  286. 'method' => 'post', 'action' => '/articles/add',
  287. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  288. ),
  289. 'div' => array('style' => 'display:none;'),
  290. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  291. '/div'
  292. );
  293. $this->assertTags($result, $expected);
  294. }
  295. /**
  296. * Test creating GET forms.
  297. *
  298. * @return void
  299. */
  300. public function testCreateGet() {
  301. $encoding = strtolower(Configure::read('App.encoding'));
  302. $result = $this->Form->create(false, array('type' => 'get'));
  303. $expected = array('form' => array(
  304. 'method' => 'get', 'action' => '/articles/add',
  305. 'accept-charset' => $encoding
  306. ));
  307. $this->assertTags($result, $expected);
  308. }
  309. /**
  310. * test the create() method
  311. *
  312. * @dataProvider requestTypeProvider
  313. * @return void
  314. */
  315. public function testCreateTypeOptions($type, $method, $override) {
  316. $encoding = strtolower(Configure::read('App.encoding'));
  317. $result = $this->Form->create(false, array('type' => $type));
  318. $expected = array(
  319. 'form' => array(
  320. 'method' => $method, 'action' => '/articles/add',
  321. 'accept-charset' => $encoding
  322. ),
  323. 'div' => array('style' => 'display:none;'),
  324. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  325. '/div'
  326. );
  327. $this->assertTags($result, $expected);
  328. }
  329. /**
  330. * Test opening a form for an update operation.
  331. *
  332. * @return void
  333. */
  334. public function testCreateUpdateForm() {
  335. $encoding = strtolower(Configure::read('App.encoding'));
  336. $this->Form->request->here = '/articles/edit/1';
  337. $this->Form->request['action'] = 'edit';
  338. $this->article['defaults']['id'] = 1;
  339. $result = $this->Form->create($this->article);
  340. $expected = array(
  341. 'form' => array(
  342. 'method' => 'post', 'action' => '/articles/edit/1',
  343. 'accept-charset' => $encoding
  344. ),
  345. 'div' => array('style' => 'display:none;'),
  346. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  347. '/div'
  348. );
  349. $this->assertTags($result, $expected);
  350. }
  351. /**
  352. * test create() with automatic url generation
  353. *
  354. * @return void
  355. */
  356. public function testCreateAutoUrl() {
  357. $encoding = strtolower(Configure::read('App.encoding'));
  358. $this->Form->request['action'] = 'delete';
  359. $this->Form->request->here = '/articles/delete/10';
  360. $this->Form->request->base = '';
  361. $result = $this->Form->create($this->article);
  362. $expected = array(
  363. 'form' => array(
  364. 'method' => 'post', 'action' => '/articles/delete/10',
  365. 'accept-charset' => $encoding
  366. ),
  367. 'div' => array('style' => 'display:none;'),
  368. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  369. '/div'
  370. );
  371. $this->assertTags($result, $expected);
  372. $this->article['defaults'] = ['id' => 1];
  373. $this->Form->request->here = '/articles/edit/1';
  374. $this->Form->request['action'] = 'delete';
  375. $result = $this->Form->create($this->article, ['action' => 'edit']);
  376. $expected = array(
  377. 'form' => array(
  378. 'method' => 'post',
  379. 'action' => '/articles/edit/1',
  380. 'accept-charset' => $encoding
  381. ),
  382. 'div' => array('style' => 'display:none;'),
  383. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  384. '/div'
  385. );
  386. $this->assertTags($result, $expected);
  387. $this->Form->request['action'] = 'add';
  388. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  389. $expected = array(
  390. 'form' => array(
  391. 'method' => 'post',
  392. 'action' => '/articles/publish/1',
  393. 'accept-charset' => $encoding
  394. ),
  395. 'div' => array('style' => 'display:none;'),
  396. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  397. '/div'
  398. );
  399. $this->assertTags($result, $expected);
  400. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  401. $expected = array(
  402. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  403. 'div' => array('style' => 'display:none;'),
  404. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  405. '/div'
  406. );
  407. $this->assertTags($result, $expected);
  408. $this->Form->request['controller'] = 'pages';
  409. $result = $this->Form->create($this->article, array('action' => 'signup'));
  410. $expected = array(
  411. 'form' => array(
  412. 'method' => 'post', 'action' => '/pages/signup/1',
  413. 'accept-charset' => $encoding
  414. ),
  415. 'div' => array('style' => 'display:none;'),
  416. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  417. '/div'
  418. );
  419. $this->assertTags($result, $expected);
  420. }
  421. /**
  422. * test create() with a custom route
  423. *
  424. * @return void
  425. */
  426. public function testCreateCustomRoute() {
  427. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $this->Form->request['controller'] = 'users';
  430. $result = $this->Form->create(false, array('action' => 'login'));
  431. $expected = array(
  432. 'form' => array(
  433. 'method' => 'post', 'action' => '/login',
  434. 'accept-charset' => $encoding
  435. ),
  436. 'div' => array('style' => 'display:none;'),
  437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  438. '/div'
  439. );
  440. $this->assertTags($result, $expected);
  441. }
  442. /**
  443. * test automatic accept-charset overriding
  444. *
  445. * @return void
  446. */
  447. public function testCreateWithAcceptCharset() {
  448. $result = $this->Form->create($this->article, array(
  449. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  450. )
  451. );
  452. $expected = array(
  453. 'form' => array(
  454. 'method' => 'post', 'action' => '/articles',
  455. 'accept-charset' => 'iso-8859-1'
  456. ),
  457. 'div' => array('style' => 'display:none;'),
  458. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  459. '/div'
  460. );
  461. $this->assertTags($result, $expected);
  462. }
  463. /**
  464. * Test base form URL when url param is passed with multiple parameters (&)
  465. *
  466. */
  467. public function testCreateQuerystringrequest() {
  468. $encoding = strtolower(Configure::read('App.encoding'));
  469. $result = $this->Form->create($this->article, array(
  470. 'type' => 'post',
  471. 'escape' => false,
  472. 'url' => array(
  473. 'controller' => 'controller',
  474. 'action' => 'action',
  475. '?' => array('param1' => 'value1', 'param2' => 'value2')
  476. )
  477. ));
  478. $expected = array(
  479. 'form' => array(
  480. 'method' => 'post',
  481. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  482. 'accept-charset' => $encoding
  483. ),
  484. 'div' => array('style' => 'display:none;'),
  485. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  486. '/div'
  487. );
  488. $this->assertTags($result, $expected);
  489. $result = $this->Form->create($this->article, array(
  490. 'type' => 'post',
  491. 'url' => array(
  492. 'controller' => 'controller',
  493. 'action' => 'action',
  494. '?' => array('param1' => 'value1', 'param2' => 'value2')
  495. )
  496. ));
  497. $this->assertTags($result, $expected);
  498. }
  499. /**
  500. * test that create() doesn't cause errors by multiple id's being in the primary key
  501. * as could happen with multiple select or checkboxes.
  502. *
  503. * @return void
  504. */
  505. public function testCreateWithMultipleIdInData() {
  506. $encoding = strtolower(Configure::read('App.encoding'));
  507. $this->Form->request->data['Article']['id'] = array(1, 2);
  508. $result = $this->Form->create($this->article);
  509. $expected = array(
  510. 'form' => array(
  511. 'method' => 'post',
  512. 'action' => '/articles/add',
  513. 'accept-charset' => $encoding
  514. ),
  515. 'div' => array('style' => 'display:none;'),
  516. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  517. '/div'
  518. );
  519. $this->assertTags($result, $expected);
  520. }
  521. /**
  522. * test that create() doesn't add in extra passed params.
  523. *
  524. * @return void
  525. */
  526. public function testCreatePassedArgs() {
  527. $encoding = strtolower(Configure::read('App.encoding'));
  528. $this->Form->request->data['Article']['id'] = 1;
  529. $result = $this->Form->create($this->article, array(
  530. 'type' => 'post',
  531. 'escape' => false,
  532. 'url' => array(
  533. 'action' => 'edit',
  534. 'myparam'
  535. )
  536. ));
  537. $expected = array(
  538. 'form' => array(
  539. 'method' => 'post',
  540. 'action' => '/articles/edit/myparam',
  541. 'accept-charset' => $encoding
  542. ),
  543. 'div' => array('style' => 'display:none;'),
  544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  545. '/div'
  546. );
  547. $this->assertTags($result, $expected);
  548. }
  549. /**
  550. * test creating a get form, and get form inputs.
  551. *
  552. * @return void
  553. */
  554. public function testGetFormCreate() {
  555. $encoding = strtolower(Configure::read('App.encoding'));
  556. $result = $this->Form->create($this->article, array('type' => 'get'));
  557. $this->assertTags($result, array('form' => array(
  558. 'method' => 'get', 'action' => '/articles/add',
  559. 'accept-charset' => $encoding
  560. )));
  561. $result = $this->Form->text('title');
  562. $this->assertTags($result, array('input' => array(
  563. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  564. )));
  565. $result = $this->Form->password('password');
  566. $this->assertTags($result, array('input' => array(
  567. 'name' => 'password', 'type' => 'password'
  568. )));
  569. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  570. $result = $this->Form->text('user_form');
  571. $this->assertTags($result, array('input' => array(
  572. 'name' => 'user_form', 'type' => 'text'
  573. )));
  574. }
  575. /**
  576. * test get form, and inputs when the model param is false
  577. *
  578. * @return void
  579. */
  580. public function testGetFormWithFalseModel() {
  581. $encoding = strtolower(Configure::read('App.encoding'));
  582. $this->Form->request['controller'] = 'contact_test';
  583. $result = $this->Form->create(false, array(
  584. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  585. ));
  586. $expected = array('form' => array(
  587. 'method' => 'get', 'action' => '/contact_test/add',
  588. 'accept-charset' => $encoding
  589. ));
  590. $this->assertTags($result, $expected);
  591. $result = $this->Form->text('reason');
  592. $expected = array(
  593. 'input' => array('type' => 'text', 'name' => 'reason')
  594. );
  595. $this->assertTags($result, $expected);
  596. }
  597. /**
  598. * testFormCreateWithSecurity method
  599. *
  600. * Test form->create() with security key.
  601. *
  602. * @return void
  603. */
  604. public function testCreateWithSecurity() {
  605. $this->Form->request->params['_csrfToken'] = 'testKey';
  606. $encoding = strtolower(Configure::read('App.encoding'));
  607. $result = $this->Form->create($this->article, [
  608. 'url' => '/articles/publish',
  609. ]);
  610. $expected = array(
  611. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  612. 'div' => array('style' => 'display:none;'),
  613. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  614. array('input' => array(
  615. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  616. )),
  617. '/div'
  618. );
  619. $this->assertTags($result, $expected);
  620. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  621. $expected['form']['id'] = 'MyForm';
  622. $this->assertTags($result, $expected);
  623. }
  624. /**
  625. * testFormCreateGetNoSecurity method
  626. *
  627. * Test form->create() with no security key as its a get form
  628. *
  629. * @return void
  630. */
  631. public function testCreateEndGetNoSecurity() {
  632. $this->Form->request->params['_csrfToken'] = 'testKey';
  633. $encoding = strtolower(Configure::read('App.encoding'));
  634. $article = new Article();
  635. $result = $this->Form->create($article, [
  636. 'type' => 'get',
  637. 'url' => '/contacts/add'
  638. ]);
  639. $this->assertNotContains('testKey', $result);
  640. $result = $this->Form->end();
  641. $this->assertNotContains('testKey', $result);
  642. }
  643. /**
  644. * test that create() clears the fields property so it starts fresh
  645. *
  646. * @return void
  647. */
  648. public function testCreateClearingFields() {
  649. $this->Form->fields = array('model_id');
  650. $this->Form->create($this->article);
  651. $this->assertEquals(array(), $this->Form->fields);
  652. }
  653. /**
  654. * Tests form hash generation with model-less data
  655. *
  656. * @return void
  657. */
  658. public function testValidateHashNoModel() {
  659. $this->Form->request->params['_Token'] = 'foo';
  660. $result = $this->Form->secure(array('anything'));
  661. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  662. }
  663. /**
  664. * Tests that hidden fields generated for checkboxes don't get locked
  665. *
  666. * @return void
  667. */
  668. public function testNoCheckboxLocking() {
  669. $this->Form->request->params['_Token'] = 'foo';
  670. $this->assertSame([], $this->Form->fields);
  671. $this->Form->checkbox('check', array('value' => '1'));
  672. $this->assertSame($this->Form->fields, array('check'));
  673. }
  674. /**
  675. * testFormSecurityFields method
  676. *
  677. * Test generation of secure form hash generation.
  678. *
  679. * @return void
  680. */
  681. public function testFormSecurityFields() {
  682. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  683. $this->Form->request->params['_Token'] = 'testKey';
  684. $result = $this->Form->secure($fields);
  685. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  686. $hash .= ':' . 'Model.valid';
  687. $hash = urlencode($hash);
  688. $expected = array(
  689. 'div' => array('style' => 'display:none;'),
  690. array('input' => array(
  691. 'type' => 'hidden',
  692. 'name' => '_Token[fields]',
  693. 'value' => $hash
  694. )),
  695. array('input' => array(
  696. 'type' => 'hidden',
  697. 'name' => '_Token[unlocked]',
  698. 'value' => '',
  699. )),
  700. '/div'
  701. );
  702. $this->assertTags($result, $expected);
  703. }
  704. /**
  705. * Tests correct generation of number fields for double and float fields
  706. *
  707. * @return void
  708. */
  709. public function testTextFieldGenerationForFloats() {
  710. $this->article['schema'] = [
  711. 'foo' => [
  712. 'type' => 'float',
  713. 'null' => false,
  714. 'default' => null,
  715. 'length' => 10
  716. ]
  717. ];
  718. $this->Form->create($this->article);
  719. $result = $this->Form->input('foo');
  720. $expected = array(
  721. 'div' => array('class' => 'input number'),
  722. 'label' => array('for' => 'foo'),
  723. 'Foo',
  724. '/label',
  725. array('input' => array(
  726. 'type' => 'number',
  727. 'name' => 'foo',
  728. 'id' => 'foo',
  729. 'step' => 'any'
  730. )),
  731. '/div'
  732. );
  733. $this->assertTags($result, $expected);
  734. $result = $this->Form->input('foo', array('step' => 0.5));
  735. $expected = array(
  736. 'div' => array('class' => 'input number'),
  737. 'label' => array('for' => 'foo'),
  738. 'Foo',
  739. '/label',
  740. array('input' => array(
  741. 'type' => 'number',
  742. 'name' => 'foo',
  743. 'id' => 'foo',
  744. 'step' => '0.5'
  745. )),
  746. '/div'
  747. );
  748. $this->assertTags($result, $expected);
  749. }
  750. /**
  751. * Tests correct generation of number fields for integer fields
  752. *
  753. * @return void
  754. */
  755. public function testTextFieldTypeNumberGenerationForIntegers() {
  756. TableRegistry::get('Contacts', [
  757. 'className' => __NAMESPACE__ . '\ContactsTable'
  758. ]);
  759. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  760. $result = $this->Form->input('age');
  761. $expected = array(
  762. 'div' => array('class' => 'input number'),
  763. 'label' => array('for' => 'age'),
  764. 'Age',
  765. '/label',
  766. array('input' => array(
  767. 'type' => 'number', 'name' => 'age',
  768. 'id' => 'age'
  769. )),
  770. '/div'
  771. );
  772. $this->assertTags($result, $expected);
  773. }
  774. /**
  775. * Tests correct generation of file upload fields for binary fields
  776. *
  777. * @return void
  778. */
  779. public function testFileUploadFieldTypeGenerationForBinaries() {
  780. $table = TableRegistry::get('Contacts', [
  781. 'className' => __NAMESPACE__ . '\ContactsTable'
  782. ]);
  783. $table->schema(array('foo' => array(
  784. 'type' => 'binary',
  785. 'null' => false,
  786. 'default' => null,
  787. 'length' => 1024
  788. )));
  789. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  790. $result = $this->Form->input('foo');
  791. $expected = array(
  792. 'div' => array('class' => 'input file'),
  793. 'label' => array('for' => 'foo'),
  794. 'Foo',
  795. '/label',
  796. array('input' => array(
  797. 'type' => 'file', 'name' => 'foo',
  798. 'id' => 'foo'
  799. )),
  800. '/div'
  801. );
  802. $this->assertTags($result, $expected);
  803. }
  804. /**
  805. * testFormSecurityMultipleFields method
  806. *
  807. * Test secure() with multiple row form. Ensure hash is correct.
  808. *
  809. * @return void
  810. */
  811. public function testFormSecurityMultipleFields() {
  812. $this->Form->request->params['_Token'] = 'foo';
  813. $fields = array(
  814. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  815. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  816. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  817. );
  818. $result = $this->Form->secure($fields);
  819. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  820. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  821. $expected = array(
  822. 'div' => array('style' => 'display:none;'),
  823. array('input' => array(
  824. 'type' => 'hidden', 'name' => '_Token[fields]',
  825. 'value' => $hash
  826. )),
  827. array('input' => array(
  828. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  829. 'value' => ''
  830. )),
  831. '/div'
  832. );
  833. $this->assertTags($result, $expected);
  834. }
  835. /**
  836. * testFormSecurityMultipleSubmitButtons
  837. *
  838. * test form submit generation and ensure that _Token is only created on end()
  839. *
  840. * @return void
  841. */
  842. public function testFormSecurityMultipleSubmitButtons() {
  843. $this->Form->request->params['_Token'] = 'testKey';
  844. $this->Form->create($this->article);
  845. $this->Form->text('Address.title');
  846. $this->Form->text('Address.first_name');
  847. $result = $this->Form->submit('Save', array('name' => 'save'));
  848. $expected = array(
  849. 'div' => array('class' => 'submit'),
  850. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  851. '/div',
  852. );
  853. $this->assertTags($result, $expected);
  854. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  855. $expected = array(
  856. 'div' => array('class' => 'submit'),
  857. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  858. '/div',
  859. );
  860. $this->assertTags($result, $expected);
  861. $result = $this->Form->end();
  862. $expected = array(
  863. 'div' => array('style' => 'display:none;'),
  864. array('input' => array(
  865. 'type' => 'hidden',
  866. 'name' => '_Token[fields]',
  867. 'value'
  868. )),
  869. array('input' => array(
  870. 'type' => 'hidden',
  871. 'name' => '_Token[unlocked]',
  872. 'value' => 'cancel%7Csave'
  873. )),
  874. '/div'
  875. );
  876. $this->assertTags($result, $expected);
  877. }
  878. /**
  879. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  880. *
  881. * @return void
  882. */
  883. public function testSecurityButtonNestedNamed() {
  884. $key = 'testKey';
  885. $this->Form->request->params['_csrfToken'] = $key;
  886. $this->Form->create('Addresses');
  887. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  888. $result = $this->Form->unlockField();
  889. $this->assertEquals(array('Address.button'), $result);
  890. }
  891. /**
  892. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  893. *
  894. * @return void
  895. */
  896. public function testSecuritySubmitNestedNamed() {
  897. $this->Form->request->params['_Token'] = 'testKey';
  898. $this->Form->create($this->article);
  899. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  900. $result = $this->Form->unlockField();
  901. $this->assertEquals(array('Address.button'), $result);
  902. }
  903. /**
  904. * Test that the correct fields are unlocked for image submits with no names.
  905. *
  906. * @return void
  907. */
  908. public function testSecuritySubmitImageNoName() {
  909. $key = 'testKey';
  910. $this->Form->request->params['_Token'] = 'testKey';
  911. $this->Form->create(false);
  912. $result = $this->Form->submit('save.png');
  913. $expected = array(
  914. 'div' => array('class' => 'submit'),
  915. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  916. '/div'
  917. );
  918. $this->assertTags($result, $expected);
  919. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  920. }
  921. /**
  922. * Test that the correct fields are unlocked for image submits with names.
  923. *
  924. * @return void
  925. */
  926. public function testSecuritySubmitImageName() {
  927. $this->Form->request->params['_Token'] = 'testKey';
  928. $this->Form->create(null);
  929. $result = $this->Form->submit('save.png', array('name' => 'test'));
  930. $expected = array(
  931. 'div' => array('class' => 'submit'),
  932. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  933. '/div'
  934. );
  935. $this->assertTags($result, $expected);
  936. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  937. }
  938. /**
  939. * testFormSecurityMultipleInputFields method
  940. *
  941. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  942. *
  943. * @return void
  944. */
  945. public function testFormSecurityMultipleInputFields() {
  946. $this->Form->request->params['_Token'] = 'testKey';
  947. $this->Form->create();
  948. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  949. $this->Form->input('Addresses.0.title');
  950. $this->Form->input('Addresses.0.first_name');
  951. $this->Form->input('Addresses.0.last_name');
  952. $this->Form->input('Addresses.0.address');
  953. $this->Form->input('Addresses.0.city');
  954. $this->Form->input('Addresses.0.phone');
  955. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  956. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  957. $this->Form->input('Addresses.1.title');
  958. $this->Form->input('Addresses.1.first_name');
  959. $this->Form->input('Addresses.1.last_name');
  960. $this->Form->input('Addresses.1.address');
  961. $this->Form->input('Addresses.1.city');
  962. $this->Form->input('Addresses.1.phone');
  963. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  964. $result = $this->Form->secure($this->Form->fields);
  965. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  966. $expected = array(
  967. 'div' => array('style' => 'display:none;'),
  968. array('input' => array(
  969. 'type' => 'hidden', 'name' => '_Token[fields]',
  970. 'value' => $hash
  971. )),
  972. array('input' => array(
  973. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  974. 'value' => ''
  975. )),
  976. '/div'
  977. );
  978. $this->assertTags($result, $expected);
  979. }
  980. /**
  981. * Test form security with Model.field.0 style inputs
  982. *
  983. * @return void
  984. */
  985. public function testFormSecurityArrayFields() {
  986. $this->Form->request->params['_Token'] = 'testKey';
  987. $this->Form->create();
  988. $this->Form->text('Address.primary.1');
  989. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  990. $this->Form->text('Address.secondary.1.0');
  991. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  992. }
  993. /**
  994. * testFormSecurityMultipleInputDisabledFields method
  995. *
  996. * test secure form generation with multiple records and disabled fields.
  997. *
  998. * @return void
  999. */
  1000. public function testFormSecurityMultipleInputDisabledFields() {
  1001. $this->Form->request->params['_Token'] = array(
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->text('Addresses.0.title');
  1007. $this->Form->text('Addresses.0.first_name');
  1008. $this->Form->text('Addresses.0.last_name');
  1009. $this->Form->text('Addresses.0.address');
  1010. $this->Form->text('Addresses.0.city');
  1011. $this->Form->text('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->text('Addresses.1.title');
  1014. $this->Form->text('Addresses.1.first_name');
  1015. $this->Form->text('Addresses.1.last_name');
  1016. $this->Form->text('Addresses.1.address');
  1017. $this->Form->text('Addresses.1.city');
  1018. $this->Form->text('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden',
  1025. 'name' => '_Token[fields]',
  1026. 'value' => $hash
  1027. )),
  1028. array('input' => array(
  1029. 'type' => 'hidden',
  1030. 'name' => '_Token[unlocked]',
  1031. 'value' => 'address%7Cfirst_name',
  1032. )),
  1033. '/div'
  1034. );
  1035. $this->assertTags($result, $expected);
  1036. }
  1037. /**
  1038. * testFormSecurityInputDisabledFields method
  1039. *
  1040. * Test single record form with disabled fields.
  1041. *
  1042. * @return void
  1043. */
  1044. public function testFormSecurityInputUnlockedFields() {
  1045. $this->Form->request['_Token'] = array(
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->text('Addresses.title');
  1052. $this->Form->text('Addresses.first_name');
  1053. $this->Form->text('Addresses.last_name');
  1054. $this->Form->text('Addresses.address');
  1055. $this->Form->text('Addresses.city');
  1056. $this->Form->text('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[fields]',
  1070. 'value' => $hash
  1071. )),
  1072. array('input' => array(
  1073. 'type' => 'hidden',
  1074. 'name' => '_Token[unlocked]',
  1075. 'value' => 'address%7Cfirst_name',
  1076. )),
  1077. '/div'
  1078. );
  1079. $this->assertTags($result, $expected);
  1080. }
  1081. /**
  1082. * test securing inputs with custom name attributes.
  1083. *
  1084. * @return void
  1085. */
  1086. public function testFormSecureWithCustomNameAttribute() {
  1087. $this->Form->request->params['_Token'] = 'testKey';
  1088. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1089. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1090. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1091. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1092. }
  1093. /**
  1094. * testFormSecuredInput method
  1095. *
  1096. * Test generation of entire secure form, assertions made on input() output.
  1097. *
  1098. * @return void
  1099. */
  1100. public function testFormSecuredInput() {
  1101. $this->Form->request->params['_csrfToken'] = 'testKey';
  1102. $this->Form->request->params['_Token'] = 'stuff';
  1103. $this->article['schema'] = [
  1104. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1105. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1106. ];
  1107. $result = $this->Form->create($this->article, array('url' => '/articles/add'));
  1108. $encoding = strtolower(Configure::read('App.encoding'));
  1109. $expected = array(
  1110. 'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
  1111. 'div' => array('style' => 'display:none;'),
  1112. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1113. array('input' => array(
  1114. 'type' => 'hidden',
  1115. 'name' => '_csrfToken',
  1116. 'value' => 'testKey'
  1117. )),
  1118. '/div'
  1119. );
  1120. $this->assertTags($result, $expected);
  1121. $result = $this->Form->input('ratio');
  1122. $expected = array(
  1123. 'div' => array('class'),
  1124. 'label' => array('for'),
  1125. 'Ratio',
  1126. '/label',
  1127. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1128. '/div',
  1129. );
  1130. $this->assertTags($result, $expected);
  1131. $result = $this->Form->input('population');
  1132. $expected = array(
  1133. 'div' => array('class'),
  1134. 'label' => array('for'),
  1135. 'Population',
  1136. '/label',
  1137. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1138. '/div',
  1139. );
  1140. $this->assertTags($result, $expected);
  1141. $result = $this->Form->input('published', array('type' => 'text'));
  1142. $expected = array(
  1143. 'div' => array('class' => 'input text'),
  1144. 'label' => array('for' => 'published'),
  1145. 'Published',
  1146. '/label',
  1147. array('input' => array(
  1148. 'type' => 'text',
  1149. 'name' => 'published',
  1150. 'id' => 'published'
  1151. )),
  1152. '/div'
  1153. );
  1154. $this->assertTags($result, $expected);
  1155. $result = $this->Form->input('other', array('type' => 'text'));
  1156. $expected = array(
  1157. 'div' => array('class' => 'input text'),
  1158. 'label' => array('for' => 'other'),
  1159. 'Other',
  1160. '/label',
  1161. array('input' => array(
  1162. 'type' => 'text',
  1163. 'name' => 'other',
  1164. 'id',
  1165. )),
  1166. '/div'
  1167. );
  1168. $this->assertTags($result, $expected);
  1169. $result = $this->Form->hidden('stuff');
  1170. $expected = array(
  1171. 'input' => array(
  1172. 'type' => 'hidden',
  1173. 'name' => 'stuff',
  1174. ));
  1175. $this->assertTags($result, $expected);
  1176. $result = $this->Form->hidden('hidden', array('value' => '0'));
  1177. $expected = array('input' => array(
  1178. 'type' => 'hidden',
  1179. 'name' => 'hidden',
  1180. 'value' => '0'
  1181. ));
  1182. $this->assertTags($result, $expected);
  1183. $result = $this->Form->input('something', array('type' => 'checkbox'));
  1184. $expected = array(
  1185. 'div' => array('class' => 'input checkbox'),
  1186. array('input' => array(
  1187. 'type' => 'hidden',
  1188. 'name' => 'something',
  1189. 'value' => '0'
  1190. )),
  1191. array('input' => array(
  1192. 'type' => 'checkbox',
  1193. 'name' => 'something',
  1194. 'value' => '1',
  1195. 'id' => 'something'
  1196. )),
  1197. 'label' => array('for' => 'something'),
  1198. 'Something',
  1199. '/label',
  1200. '/div'
  1201. );
  1202. $this->assertTags($result, $expected);
  1203. $result = $this->Form->fields;
  1204. $expected = array(
  1205. 'ratio', 'population', 'published', 'other',
  1206. 'stuff' => '',
  1207. 'hidden' => '0',
  1208. 'something'
  1209. );
  1210. $this->assertEquals($expected, $result);
  1211. $result = $this->Form->secure($this->Form->fields);
  1212. $expected = array(
  1213. 'div' => array('style' => 'display:none;'),
  1214. array('input' => array(
  1215. 'type' => 'hidden',
  1216. 'name' => '_Token[fields]',
  1217. 'value'
  1218. )),
  1219. array('input' => array(
  1220. 'type' => 'hidden',
  1221. 'name' => '_Token[unlocked]',
  1222. 'value' => ''
  1223. )),
  1224. '/div'
  1225. );
  1226. $this->assertTags($result, $expected);
  1227. }
  1228. /**
  1229. * Test secured inputs with custom names.
  1230. *
  1231. * @return void
  1232. */
  1233. public function testSecuredInputCustomName() {
  1234. $this->Form->request->params['_Token'] = 'testKey';
  1235. $this->assertEquals(array(), $this->Form->fields);
  1236. $this->Form->text('text_input', array(
  1237. 'name' => 'Option[General.default_role]',
  1238. ));
  1239. $expected = array('Option.General.default_role');
  1240. $this->assertEquals($expected, $this->Form->fields);
  1241. $this->Form->select('select_box', [1, 2], [
  1242. 'name' => 'Option[General.select_role]',
  1243. ]);
  1244. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1245. $this->assertEquals($expected, $this->Form->fields);
  1246. }
  1247. /**
  1248. * Tests that the correct keys are added to the field hash index
  1249. *
  1250. * @return void
  1251. */
  1252. public function testFormSecuredFileInput() {
  1253. $this->assertEquals(array(), $this->Form->fields);
  1254. $this->Form->file('Attachment.file');
  1255. $expected = array(
  1256. 'Attachment.file.name', 'Attachment.file.type',
  1257. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1258. 'Attachment.file.size'
  1259. );
  1260. $this->assertEquals($expected, $this->Form->fields);
  1261. }
  1262. /**
  1263. * test that multiple selects keys are added to field hash
  1264. *
  1265. * @return void
  1266. */
  1267. public function testFormSecuredMultipleSelect() {
  1268. $this->Form->request->params['_csrfToken'] = 'testKey';
  1269. $this->assertEquals(array(), $this->Form->fields);
  1270. $options = array('1' => 'one', '2' => 'two');
  1271. $this->Form->select('Model.select', $options);
  1272. $expected = array('Model.select');
  1273. $this->assertEquals($expected, $this->Form->fields);
  1274. $this->Form->fields = array();
  1275. $this->Form->select('Model.select', $options, array('multiple' => true));
  1276. $this->assertEquals($expected, $this->Form->fields);
  1277. }
  1278. /**
  1279. * testFormSecuredRadio method
  1280. *
  1281. * @return void
  1282. */
  1283. public function testFormSecuredRadio() {
  1284. $this->Form->request->params['_Token'] = 'testKey';
  1285. $this->assertEquals(array(), $this->Form->fields);
  1286. $options = array('1' => 'option1', '2' => 'option2');
  1287. $this->Form->radio('Test.test', $options);
  1288. $expected = array('Test.test');
  1289. $this->assertEquals($expected, $this->Form->fields);
  1290. }
  1291. /**
  1292. * Test that when disabled is in a list based attribute array it works.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testFormSecuredAndDisabledNotAssoc() {
  1297. $this->Form->request->params['_Token'] = 'testKey';
  1298. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1299. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1300. $this->Form->text('Model.text', array('disabled'));
  1301. $this->Form->textarea('Model.textarea', array('disabled'));
  1302. $this->Form->password('Model.password', array('disabled'));
  1303. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1304. $expected = array(
  1305. 'Model.radio' => ''
  1306. );
  1307. $this->assertEquals($expected, $this->Form->fields);
  1308. }
  1309. /**
  1310. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1311. * hashing.
  1312. *
  1313. * @return void
  1314. */
  1315. public function testFormSecuredAndDisabled() {
  1316. $this->Form->request->params['_Token'] = 'testKey';
  1317. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1318. $this->Form->text('Model.text', array('disabled' => true));
  1319. $this->Form->password('Model.text', array('disabled' => true));
  1320. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1321. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1322. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1323. $this->Form->year('Model.year', array('disabled' => true));
  1324. $this->Form->month('Model.month', array('disabled' => true));
  1325. $this->Form->day('Model.day', array('disabled' => true));
  1326. $this->Form->hour('Model.hour', array('disabled' => true));
  1327. $this->Form->minute('Model.minute', array('disabled' => true));
  1328. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1329. $expected = array(
  1330. 'Model.radio' => ''
  1331. );
  1332. $this->assertEquals($expected, $this->Form->fields);
  1333. }
  1334. /**
  1335. * testDisableSecurityUsingForm method
  1336. *
  1337. * @return void
  1338. */
  1339. public function testDisableSecurityUsingForm() {
  1340. $this->Form->request['_Token'] = [
  1341. 'disabledFields' => []
  1342. ];
  1343. $this->Form->create();
  1344. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1345. $this->Form->text('Addresses.title');
  1346. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1347. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1348. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1349. $result = $this->Form->fields;
  1350. $expected = [
  1351. 'Addresses.id' => '123456', 'Addresses.title',
  1352. ];
  1353. $this->assertEquals($expected, $result);
  1354. }
  1355. /**
  1356. * test disableField
  1357. *
  1358. * @return void
  1359. */
  1360. public function testUnlockFieldAddsToList() {
  1361. $this->Form->request['_Token'] = array(
  1362. 'unlockedFields' => array()
  1363. );
  1364. $this->Form->unlockField('Contact.name');
  1365. $this->Form->text('Contact.name');
  1366. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1367. $this->assertEquals(array(), $this->Form->fields);
  1368. }
  1369. /**
  1370. * test unlockField removing from fields array.
  1371. *
  1372. * @return void
  1373. */
  1374. public function testUnlockFieldRemovingFromFields() {
  1375. $this->Form->request['_Token'] = array(
  1376. 'unlockedFields' => array()
  1377. );
  1378. $this->Form->create($this->article);
  1379. $this->Form->hidden('Article.id', array('value' => 1));
  1380. $this->Form->text('Article.title');
  1381. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1382. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1383. $this->Form->unlockField('Article.title');
  1384. $this->Form->unlockField('Article.id');
  1385. $this->assertEquals(array(), $this->Form->fields);
  1386. }
  1387. /**
  1388. * test error message display
  1389. *
  1390. * @return void
  1391. */
  1392. public function testErrorMessageDisplay() {
  1393. $this->article['errors'] = [
  1394. 'Article' => ['title' => 'error message']
  1395. ];
  1396. $this->Form->create($this->article);
  1397. $result = $this->Form->input('Article.title');
  1398. $expected = [
  1399. 'div' => ['class' => 'input text error'],
  1400. 'label' => ['for' => 'article-title'],
  1401. 'Title',
  1402. '/label',
  1403. 'input' => [
  1404. 'type' => 'text', 'name' => 'Article[title]',
  1405. 'id' => 'article-title', 'class' => 'form-error'
  1406. ],
  1407. ['div' => ['class' => 'error-message']],
  1408. 'error message',
  1409. '/div',
  1410. '/div'
  1411. ];
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->input('Article.title', [
  1414. 'templates' => [
  1415. 'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1416. ]
  1417. ]);
  1418. $expected = [
  1419. 'div' => ['class' => 'input text error'],
  1420. 'label' => ['for' => 'article-title'],
  1421. 'Title',
  1422. '/label',
  1423. 'input' => [
  1424. 'type' => 'text', 'name' => 'Article[title]',
  1425. 'id' => 'article-title', 'class' => 'form-error'
  1426. ],
  1427. '/div'
  1428. ];
  1429. $this->assertTags($result, $expected);
  1430. }
  1431. /**
  1432. * Test validation errors, when validation message is an empty string.
  1433. *
  1434. * @return void
  1435. */
  1436. public function testEmptyErrorValidation() {
  1437. $this->article['errors'] = [
  1438. 'Article' => ['title' => '']
  1439. ];
  1440. $this->Form->create($this->article);
  1441. $result = $this->Form->input('Article.title');
  1442. $expected = [
  1443. 'div' => ['class' => 'input text error'],
  1444. 'label' => ['for' => 'article-title'],
  1445. 'Title',
  1446. '/label',
  1447. 'input' => [
  1448. 'type' => 'text', 'name' => 'Article[title]',
  1449. 'id' => 'article-title', 'class' => 'form-error'
  1450. ],
  1451. ['div' => ['class' => 'error-message']],
  1452. [],
  1453. '/div',
  1454. '/div'
  1455. ];
  1456. $this->assertTags($result, $expected);
  1457. }
  1458. /**
  1459. * Test validation errors, when calling input() overriding validation message by an empty string.
  1460. *
  1461. * @return void
  1462. */
  1463. public function testEmptyInputErrorValidation() {
  1464. $this->article['errors'] = [
  1465. 'Article' => ['title' => 'error message']
  1466. ];
  1467. $this->Form->create($this->article);
  1468. $result = $this->Form->input('Article.title', array('error' => ''));
  1469. $expected = [
  1470. 'div' => ['class' => 'input text error'],
  1471. 'label' => ['for' => 'article-title'],
  1472. 'Title',
  1473. '/label',
  1474. 'input' => [
  1475. 'type' => 'text', 'name' => 'Article[title]',
  1476. 'id' => 'article-title', 'class' => 'form-error'
  1477. ],
  1478. ['div' => ['class' => 'error-message']],
  1479. [],
  1480. '/div',
  1481. '/div'
  1482. ];
  1483. $this->assertTags($result, $expected);
  1484. }
  1485. /**
  1486. * Tests displaying errors for nested entities
  1487. *
  1488. * @return void
  1489. */
  1490. public function testFormValidationAssociated() {
  1491. $nested = new Entity(['foo' => 'bar']);
  1492. $nested->errors('foo', ['not a valid bar']);
  1493. $entity = new Entity(['nested' => $nested]);
  1494. $this->Form->create($entity);
  1495. $result = $this->Form->error('nested.foo');
  1496. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1497. }
  1498. /**
  1499. * testFormValidationAssociatedSecondLevel method
  1500. *
  1501. * test form error display with associated model.
  1502. *
  1503. * @return void
  1504. */
  1505. public function testFormValidationAssociatedSecondLevel() {
  1506. $inner = new Entity(['bar' => 'baz']);
  1507. $nested = new Entity(['foo' => $inner]);
  1508. $entity = new Entity(['nested' => $nested]);
  1509. $inner->errors('bar', ['not a valid one']);
  1510. $this->Form->create($entity);
  1511. $result = $this->Form->error('nested.foo.bar');
  1512. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1513. }
  1514. /**
  1515. * testFormValidationMultiRecord method
  1516. *
  1517. * test form error display with multiple records.
  1518. *
  1519. * @return void
  1520. */
  1521. public function testFormValidationMultiRecord() {
  1522. $one = new Entity;
  1523. $two = new Entity;
  1524. TableRegistry::get('Contacts', [
  1525. 'className' => __NAMESPACE__ . '\ContactsTable'
  1526. ]);
  1527. $one->errors('email', ['invalid email']);
  1528. $two->errors('name', ['This is wrong']);
  1529. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1530. $result = $this->Form->input('Contacts.0.email');
  1531. $expected = array(
  1532. 'div' => array('class' => 'input email error'),
  1533. 'label' => array('for' => 'contacts-0-email'),
  1534. 'Email',
  1535. '/label',
  1536. 'input' => array(
  1537. 'type' => 'text', 'name' => 'Contacts[0][email]', 'id' => 'contacts-0-email',
  1538. 'class' => 'form-error', 'maxlength' => 255
  1539. ),
  1540. array('div' => array('class' => 'error-message')),
  1541. 'invalid email',
  1542. '/div',
  1543. '/div'
  1544. );
  1545. $result = $this->Form->input('Contacts.1.name');
  1546. $expected = array(
  1547. 'div' => array('class' => 'input text error'),
  1548. 'label' => array('for' => 'contacts-1-name'),
  1549. 'Name',
  1550. '/label',
  1551. 'input' => array(
  1552. 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
  1553. 'class' => 'form-error', 'maxlength' => 255
  1554. ),
  1555. array('div' => array('class' => 'error-message')),
  1556. 'This is wrong',
  1557. '/div',
  1558. '/div'
  1559. );
  1560. $this->assertTags($result, $expected);
  1561. }
  1562. /**
  1563. * testInput method
  1564. *
  1565. * Test various incarnations of input().
  1566. *
  1567. * @return void
  1568. */
  1569. public function testInput() {
  1570. TableRegistry::get('ValidateUsers', [
  1571. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1572. ]);
  1573. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1574. $result = $this->Form->input('ValidateUsers.balance');
  1575. $expected = array(
  1576. 'div' => array('class'),
  1577. 'label' => array('for'),
  1578. 'Balance',
  1579. '/label',
  1580. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1581. '/div',
  1582. );
  1583. $this->assertTags($result, $expected);
  1584. $result = $this->Form->input('ValidateUser.cost_decimal');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'Cost Decimal',
  1589. '/label',
  1590. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1591. '/div',
  1592. );
  1593. $this->assertTags($result, $expected);
  1594. }
  1595. /**
  1596. * Tests the input method and passing custom options
  1597. *
  1598. * @return void
  1599. */
  1600. public function testInputCustomization() {
  1601. TableRegistry::get('Contacts', [
  1602. 'className' => __NAMESPACE__ . '\ContactsTable'
  1603. ]);
  1604. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1605. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1606. $expected = array(
  1607. 'div' => array('class' => 'input email'),
  1608. 'label' => array('for' => 'custom'),
  1609. 'Email',
  1610. '/label',
  1611. array('input' => array(
  1612. 'type' => 'email', 'name' => 'Contact[email]',
  1613. 'id' => 'custom', 'maxlength' => 255
  1614. )),
  1615. '/div'
  1616. );
  1617. $this->assertTags($result, $expected);
  1618. $result = $this->Form->input('Contact.email', array(
  1619. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  1620. ));
  1621. $expected = array(
  1622. '<div',
  1623. 'label' => array('for' => 'contact-email'),
  1624. 'Email',
  1625. '/label',
  1626. array('input' => array(
  1627. 'type' => 'email', 'name' => 'Contact[email]',
  1628. 'id' => 'contact-email', 'maxlength' => 255
  1629. )),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1634. $expected = array(
  1635. 'div' => array('class' => 'input text'),
  1636. 'label' => array('for' => 'contact-email'),
  1637. 'Email',
  1638. '/label',
  1639. array('input' => array(
  1640. 'type' => 'text', 'name' => 'Contact[email]',
  1641. 'id' => 'contact-email', 'maxlength' => '255'
  1642. )),
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1647. $expected = array(
  1648. 'div' => array('class' => 'input text'),
  1649. 'label' => array('for' => 'contact-5-email'),
  1650. 'Email',
  1651. '/label',
  1652. array('input' => array(
  1653. 'type' => 'text', 'name' => 'Contact[5][email]',
  1654. 'id' => 'contact-5-email', 'maxlength' => '255'
  1655. )),
  1656. '/div'
  1657. );
  1658. $this->assertTags($result, $expected);
  1659. $result = $this->Form->input('Contact.password');
  1660. $expected = array(
  1661. 'div' => array('class' => 'input password'),
  1662. 'label' => array('for' => 'contact-password'),
  1663. 'Password',
  1664. '/label',
  1665. array('input' => array(
  1666. 'type' => 'password', 'name' => 'Contact[password]',
  1667. 'id' => 'contact-password'
  1668. )),
  1669. '/div'
  1670. );
  1671. $this->assertTags($result, $expected);
  1672. $result = $this->Form->input('Contact.email', array(
  1673. 'type' => 'file', 'class' => 'textbox'
  1674. ));
  1675. $expected = array(
  1676. 'div' => array('class' => 'input file'),
  1677. 'label' => array('for' => 'contact-email'),
  1678. 'Email',
  1679. '/label',
  1680. array('input' => array(
  1681. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1682. 'id' => 'contact-email'
  1683. )),
  1684. '/div'
  1685. );
  1686. $this->assertTags($result, $expected);
  1687. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1688. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1689. $result = $this->Form->input('Contact.phone');
  1690. $expected = array(
  1691. 'div' => array('class' => 'input tel'),
  1692. 'label' => array('for' => 'contact-phone'),
  1693. 'Phone',
  1694. '/label',
  1695. array('input' => array(
  1696. 'type' => 'tel', 'name' => 'Contact[phone]',
  1697. 'value' => 'Hello &amp; World &gt; weird chars',
  1698. 'id' => 'contact-phone', 'maxlength' => 255
  1699. )),
  1700. '/div'
  1701. );
  1702. $this->assertTags($result, $expected);
  1703. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1704. $this->Form->create();
  1705. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1706. $expected = array(
  1707. 'div' => array('class' => 'input text'),
  1708. 'label' => array('for' => 'myId'),
  1709. 'Field',
  1710. '/label',
  1711. 'input' => array(
  1712. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1713. 'value' => 'My value', 'id' => 'myId'
  1714. ),
  1715. '/div'
  1716. );
  1717. $this->assertTags($result, $expected);
  1718. $this->Form->request->data = [];
  1719. $entity->errors('field', 'Badness!');
  1720. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1721. $result = $this->Form->input('Contact.field');
  1722. $expected = array(
  1723. 'div' => array('class' => 'input text error'),
  1724. 'label' => array('for' => 'contact-field'),
  1725. 'Field',
  1726. '/label',
  1727. 'input' => array(
  1728. 'type' => 'text', 'name' => 'Contact[field]',
  1729. 'id' => 'contact-field', 'class' => 'form-error'
  1730. ),
  1731. array('div' => array('class' => 'error-message')),
  1732. 'Badness!',
  1733. '/div',
  1734. '/div'
  1735. );
  1736. $this->assertTags($result, $expected);
  1737. $result = $this->Form->input('Contact.field', array(
  1738. 'templates' => [
  1739. 'groupContainerError' => '{{content}}{{error}}',
  1740. 'error' => '<span class="error-message">{{content}}</span>'
  1741. ]
  1742. ));
  1743. $expected = array(
  1744. 'label' => array('for' => 'contact-field'),
  1745. 'Field',
  1746. '/label',
  1747. 'input' => array(
  1748. 'type' => 'text', 'name' => 'Contact[field]',
  1749. 'id' => 'contact-field', 'class' => 'form-error'
  1750. ),
  1751. array('span' => array('class' => 'error-message')),
  1752. 'Badness!',
  1753. '/span'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $entity->errors('field', ['minLength']);
  1757. $result = $this->Form->input('Contact.field', array(
  1758. 'error' => array(
  1759. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1760. 'maxLength' => 'login too large'
  1761. )
  1762. ));
  1763. $expected = array(
  1764. 'div' => array('class' => 'input text error'),
  1765. 'label' => array('for' => 'contact-field'),
  1766. 'Field',
  1767. '/label',
  1768. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1769. array('div' => array('class' => 'error-message')),
  1770. 'Le login doit contenir au moins 2 caractères',
  1771. '/div',
  1772. '/div'
  1773. );
  1774. $this->assertTags($result, $expected);
  1775. $entity->errors('field', ['maxLength']);
  1776. $result = $this->Form->input('Contact.field', array(
  1777. 'error' => array(
  1778. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1779. 'maxLength' => 'login too large',
  1780. )
  1781. ));
  1782. $expected = array(
  1783. 'div' => array('class' => 'input text error'),
  1784. 'label' => array('for' => 'contact-field'),
  1785. 'Field',
  1786. '/label',
  1787. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  1788. array('div' => array('class' => 'error-message')),
  1789. 'login too large',
  1790. '/div',
  1791. '/div'
  1792. );
  1793. $this->assertTags($result, $expected);
  1794. }
  1795. /**
  1796. * Test that inputs with 0 can be created.
  1797. *
  1798. * @return void
  1799. */
  1800. public function testInputZero() {
  1801. TableRegistry::get('Contacts', [
  1802. 'className' => __NAMESPACE__ . '\ContactsTable'
  1803. ]);
  1804. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1805. $result = $this->Form->input('0');
  1806. $expected = array(
  1807. 'div' => array('class' => 'input text'),
  1808. 'label' => array('for' => '0'), '/label',
  1809. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  1810. '/div'
  1811. );
  1812. $this->assertTags($result, $expected);
  1813. }
  1814. /**
  1815. * test input() with checkbox creation
  1816. *
  1817. * @return void
  1818. */
  1819. public function testInputCheckbox() {
  1820. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input checkbox'),
  1823. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1824. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1825. '/div'
  1826. );
  1827. $this->assertTags($result, $expected);
  1828. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1829. $expected = array(
  1830. 'div' => array('class' => 'input checkbox'),
  1831. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1832. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1833. '/div'
  1834. );
  1835. $this->assertTags($result, $expected);
  1836. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1837. $expected = array(
  1838. 'div' => array('class' => 'input checkbox'),
  1839. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  1840. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  1841. '/div'
  1842. );
  1843. $this->assertTags($result, $expected);
  1844. $result = $this->Form->input('User.disabled', array(
  1845. 'label' => 'Disabled',
  1846. 'type' => 'checkbox',
  1847. 'data-foo' => 'disabled'
  1848. ));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input checkbox'),
  1851. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  1852. array('input' => array(
  1853. 'type' => 'checkbox',
  1854. 'name' => 'User[disabled]',
  1855. 'value' => '1',
  1856. 'id' => 'user-disabled',
  1857. 'data-foo' => 'disabled'
  1858. )),
  1859. 'label' => array('for' => 'user-disabled'),
  1860. 'Disabled',
  1861. '/label',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. }
  1866. /**
  1867. * test form->input() with datetime
  1868. *
  1869. * @return void
  1870. */
  1871. public function testInputDatetime() {
  1872. $this->Form = $this->getMock(
  1873. 'Cake\View\Helper\FormHelper',
  1874. ['datetime'],
  1875. [new View(null)]
  1876. );
  1877. $this->Form->expects($this->once())->method('datetime')
  1878. ->with('prueba', [
  1879. 'type' => 'datetime',
  1880. 'timeFormat' => 24,
  1881. 'minYear' => 2008,
  1882. 'maxYear' => 2011,
  1883. 'interval' => 15,
  1884. 'options' => null,
  1885. 'empty' => false,
  1886. 'id' => 'prueba',
  1887. 'required' => false,
  1888. ])
  1889. ->will($this->returnValue('This is it!'));
  1890. $result = $this->Form->input('prueba', array(
  1891. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  1892. 'maxYear' => 2011, 'interval' => 15
  1893. ));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input datetime'),
  1896. 'label' => array('for' => 'prueba'),
  1897. 'Prueba',
  1898. '/label',
  1899. 'This is it!',
  1900. '/div'
  1901. );
  1902. $this->assertTags($result, $expected);
  1903. }
  1904. /**
  1905. * Test generating checkboxes with disabled elements.
  1906. *
  1907. * @return void
  1908. */
  1909. public function testInputCheckboxWithDisabledElements() {
  1910. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  1911. $result = $this->Form->input('Contact.multiple', array(
  1912. 'multiple' => 'checkbox',
  1913. 'disabled' => 'disabled',
  1914. 'options' => $options
  1915. ));
  1916. $expected = array(
  1917. array('div' => array('class' => 'input select')),
  1918. array('label' => array('for' => "contact-multiple")),
  1919. 'Multiple',
  1920. '/label',
  1921. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  1922. array('div' => array('class' => 'checkbox')),
  1923. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  1924. array('label' => array('for' => "contact-multiple-1")),
  1925. 'One',
  1926. '/label',
  1927. '/div',
  1928. array('div' => array('class' => 'checkbox')),
  1929. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  1930. array('label' => array('for' => "contact-multiple-2")),
  1931. 'Two',
  1932. '/label',
  1933. '/div',
  1934. array('div' => array('class' => 'checkbox')),
  1935. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  1936. array('label' => array('for' => "contact-multiple-3")),
  1937. 'Three',
  1938. '/label',
  1939. '/div',
  1940. '/div'
  1941. );
  1942. $this->assertTags($result, $expected);
  1943. // make sure 50 does only disable 50, and not 50f5c0cf
  1944. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  1945. $disabled = array(50);
  1946. $expected = array(
  1947. array('div' => array('class' => 'input select')),
  1948. array('label' => array('for' => "contact-multiple")),
  1949. 'Multiple',
  1950. '/label',
  1951. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  1952. array('div' => array('class' => 'checkbox')),
  1953. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  1954. array('label' => array('for' => "contact-multiple-50")),
  1955. 'Fifty',
  1956. '/label',
  1957. '/div',
  1958. array('div' => array('class' => 'checkbox')),
  1959. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  1960. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  1961. 'Stringy',
  1962. '/label',
  1963. '/div',
  1964. '/div'
  1965. );
  1966. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  1967. $this->assertTags($result, $expected);
  1968. }
  1969. /**
  1970. * test input name with leading integer, ensure attributes are generated correctly.
  1971. *
  1972. * @return void
  1973. */
  1974. public function testInputWithLeadingInteger() {
  1975. $result = $this->Form->text('0.Node.title');
  1976. $expected = array(
  1977. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. }
  1981. /**
  1982. * test form->input() with select type inputs.
  1983. *
  1984. * @return void
  1985. */
  1986. public function testInputSelectType() {
  1987. $result = $this->Form->input('email', array(
  1988. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  1989. );
  1990. $expected = array(
  1991. 'div' => array('class' => 'input select'),
  1992. 'label' => array('for' => 'email'),
  1993. 'Email',
  1994. '/label',
  1995. array('select' => array('name' => 'email', 'id' => 'email')),
  1996. array('option' => array('value' => '')),
  1997. '/option',
  1998. array('option' => array('value' => 'è')),
  1999. 'Firést',
  2000. '/option',
  2001. array('option' => array('value' => 'é')),
  2002. 'Secoènd',
  2003. '/option',
  2004. '/select',
  2005. '/div'
  2006. );
  2007. $this->assertTags($result, $expected);
  2008. $result = $this->Form->input('email', array(
  2009. 'options' => array('First', 'Second'), 'empty' => true)
  2010. );
  2011. $expected = array(
  2012. 'div' => array('class' => 'input select'),
  2013. 'label' => array('for' => 'email'),
  2014. 'Email',
  2015. '/label',
  2016. array('select' => array('name' => 'email', 'id' => 'email')),
  2017. array('option' => array('value' => '')),
  2018. '/option',
  2019. array('option' => array('value' => '0')),
  2020. 'First',
  2021. '/option',
  2022. array('option' => array('value' => '1')),
  2023. 'Second',
  2024. '/option',
  2025. '/select',
  2026. '/div'
  2027. );
  2028. $this->assertTags($result, $expected);
  2029. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2030. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2031. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2032. $expected = array(
  2033. 'div' => array('class' => 'input select'),
  2034. 'label' => array('for' => 'model-user-id'),
  2035. 'User',
  2036. '/label',
  2037. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2038. array('option' => array('value' => '')),
  2039. '/option',
  2040. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2041. 'good',
  2042. '/option',
  2043. array('option' => array('value' => 'other')),
  2044. 'bad',
  2045. '/option',
  2046. '/select',
  2047. '/div'
  2048. );
  2049. $this->assertTags($result, $expected);
  2050. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2051. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2052. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2053. $expected = array(
  2054. 'div' => array('class' => 'input select'),
  2055. 'label' => array('for' => 'thing-user-id'),
  2056. 'User',
  2057. '/label',
  2058. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2059. array('option' => array('value' => '')),
  2060. 'Some Empty',
  2061. '/option',
  2062. array('option' => array('value' => 'value')),
  2063. 'good',
  2064. '/option',
  2065. array('option' => array('value' => 'other')),
  2066. 'bad',
  2067. '/option',
  2068. '/select',
  2069. '/div'
  2070. );
  2071. $this->assertTags($result, $expected);
  2072. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2073. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2074. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2075. $expected = array(
  2076. 'div' => array('class' => 'input select'),
  2077. 'label' => array('for' => 'thing-user-id'),
  2078. 'User',
  2079. '/label',
  2080. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2081. array('option' => array('value' => '')),
  2082. 'Some Empty',
  2083. '/option',
  2084. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2085. 'good',
  2086. '/option',
  2087. array('option' => array('value' => 'other')),
  2088. 'bad',
  2089. '/option',
  2090. '/select',
  2091. '/div'
  2092. );
  2093. $this->assertTags($result, $expected);
  2094. $this->Form->data = array();
  2095. $result = $this->Form->input('Publisher.id', array(
  2096. 'label' => 'Publisher',
  2097. 'type' => 'select',
  2098. 'multiple' => 'checkbox',
  2099. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2100. ));
  2101. $expected = array(
  2102. array('div' => array('class' => 'input select')),
  2103. array('label' => array('for' => 'publisher-id')),
  2104. 'Publisher',
  2105. '/label',
  2106. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2107. array('div' => array('class' => 'checkbox')),
  2108. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2109. array('label' => array('for' => 'publisher-id-value-1')),
  2110. 'Label 1',
  2111. '/label',
  2112. '/div',
  2113. array('div' => array('class' => 'checkbox')),
  2114. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2115. array('label' => array('for' => 'publisher-id-value-2')),
  2116. 'Label 2',
  2117. '/label',
  2118. '/div',
  2119. '/div'
  2120. );
  2121. $this->assertTags($result, $expected);
  2122. }
  2123. /**
  2124. * test that input() and a non standard primary key makes a hidden input by default.
  2125. *
  2126. * @return void
  2127. */
  2128. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2129. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2130. $this->Form->create($this->article);
  2131. $result = $this->Form->input('title');
  2132. $expected = array(
  2133. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2134. );
  2135. $this->assertTags($result, $expected);
  2136. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2137. $this->Form->create($this->article);
  2138. $result = $this->Form->input('title');
  2139. $expected = array(
  2140. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2141. );
  2142. $this->assertTags($result, $expected);
  2143. $result = $this->Form->input('body');
  2144. $expected = array(
  2145. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2146. );
  2147. $this->assertTags($result, $expected);
  2148. }
  2149. /**
  2150. * test that overriding the magic select type widget is possible
  2151. *
  2152. * @return void
  2153. */
  2154. public function testInputOverridingMagicSelectType() {
  2155. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2156. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2157. $expected = array(
  2158. 'div' => array('class' => 'input text'),
  2159. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2160. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2161. '/div'
  2162. );
  2163. $this->assertTags($result, $expected);
  2164. //Check that magic types still work for plural/singular vars
  2165. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2166. $result = $this->Form->input('Model.type');
  2167. $expected = array(
  2168. 'div' => array('class' => 'input select'),
  2169. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2170. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2171. array('option' => array('value' => 'value')), 'good', '/option',
  2172. array('option' => array('value' => 'other')), 'bad', '/option',
  2173. '/select',
  2174. '/div'
  2175. );
  2176. $this->assertTags($result, $expected);
  2177. }
  2178. /**
  2179. * Test that inferred types do not override developer input
  2180. *
  2181. * @return void
  2182. */
  2183. public function testInputMagicTypeDoesNotOverride() {
  2184. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2185. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2186. $expected = array(
  2187. 'div' => array('class' => 'input checkbox'),
  2188. array('input' => array(
  2189. 'type' => 'hidden',
  2190. 'name' => 'Model[user]',
  2191. 'value' => 0,
  2192. )),
  2193. array('input' => array(
  2194. 'name' => 'Model[user]',
  2195. 'type' => 'checkbox',
  2196. 'id' => 'model-user',
  2197. 'value' => 1
  2198. )),
  2199. 'label' => array('for' => 'model-user'), 'User', '/label',
  2200. '/div'
  2201. );
  2202. $this->assertTags($result, $expected);
  2203. }
  2204. /**
  2205. * Test that magic input() selects are created for type=number
  2206. *
  2207. * @return void
  2208. */
  2209. public function testInputMagicSelectForTypeNumber() {
  2210. TableRegistry::get('ValidateUsers', [
  2211. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2212. ]);
  2213. $entity = new Entity(['balance' => 1]);
  2214. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2215. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2216. $result = $this->Form->input('ValidateUser.balance');
  2217. $expected = array(
  2218. 'div' => array('class' => 'input select'),
  2219. 'label' => array('for' => 'validateuser-balance'),
  2220. 'Balance',
  2221. '/label',
  2222. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2223. array('option' => array('value' => '0')),
  2224. 'nothing',
  2225. '/option',
  2226. array('option' => array('value' => '1', 'selected' => 'selected')),
  2227. 'some',
  2228. '/option',
  2229. array('option' => array('value' => '100')),
  2230. 'a lot',
  2231. '/option',
  2232. '/select',
  2233. '/div'
  2234. );
  2235. $this->assertTags($result, $expected);
  2236. }
  2237. /**
  2238. * Test that magic input() selects can easily be converted into radio types without error.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputMagicSelectChangeToRadio() {
  2243. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2244. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2245. $this->assertRegExp('/input type="radio"/', $result);
  2246. }
  2247. /**
  2248. * testFormInputs method
  2249. *
  2250. * test correct results from form::inputs().
  2251. *
  2252. * @return void
  2253. */
  2254. public function testFormInputsLegendFieldset() {
  2255. $this->Form->create($this->article);
  2256. $result = $this->Form->inputs('The Legend');
  2257. $expected = array(
  2258. '<fieldset',
  2259. '<legend',
  2260. 'The Legend',
  2261. '/legend',
  2262. '*/fieldset',
  2263. );
  2264. $this->assertTags($result, $expected);
  2265. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => true));
  2266. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2267. $this->assertContains('<fieldset>', $result);
  2268. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => true));
  2269. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2270. $this->assertContains('<fieldset>', $result);
  2271. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => false));
  2272. $this->assertNotContains('<legend>', $result);
  2273. $this->assertNotContains('<fieldset>', $result);
  2274. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2275. $this->assertNotContains('<legend>', $result);
  2276. $this->assertNotContains('<fieldset>', $result);
  2277. $this->Form->create($this->article);
  2278. $this->Form->request->params['prefix'] = 'admin';
  2279. $this->Form->request->params['action'] = 'admin_edit';
  2280. $this->Form->request->params['controller'] = 'articles';
  2281. $result = $this->Form->inputs();
  2282. $expected = [
  2283. '<fieldset',
  2284. '<legend',
  2285. 'New Article',
  2286. '/legend',
  2287. '*/fieldset',
  2288. ];
  2289. $this->assertTags($result, $expected);
  2290. }
  2291. /**
  2292. * Test the inputs() method.
  2293. *
  2294. * @return void
  2295. */
  2296. public function testFormInputs() {
  2297. $this->Form->create($this->article);
  2298. $result = $this->Form->inputs();
  2299. $expected = array(
  2300. '<fieldset',
  2301. '<legend', 'New Article', '/legend',
  2302. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2303. array('div' => array('class' => 'input select required')),
  2304. '*/div',
  2305. array('div' => array('class' => 'input text required')),
  2306. '*/div',
  2307. array('div' => array('class' => 'input text')),
  2308. '*/div',
  2309. array('div' => array('class' => 'input text')),
  2310. '*/div',
  2311. '/fieldset',
  2312. );
  2313. $this->assertTags($result, $expected);
  2314. $result = $this->Form->inputs([
  2315. 'published' => ['type' => 'boolean']
  2316. ]);
  2317. $expected = array(
  2318. '<fieldset',
  2319. '<legend', 'New Article', '/legend',
  2320. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2321. array('div' => array('class' => 'input select required')),
  2322. '*/div',
  2323. array('div' => array('class' => 'input text required')),
  2324. '*/div',
  2325. array('div' => array('class' => 'input text')),
  2326. '*/div',
  2327. array('div' => array('class' => 'input boolean')),
  2328. '*/div',
  2329. '/fieldset',
  2330. );
  2331. $this->assertTags($result, $expected);
  2332. $this->Form->create($this->article);
  2333. $result = $this->Form->inputs('Hello');
  2334. $expected = array(
  2335. 'fieldset' => array(),
  2336. 'legend' => array(),
  2337. 'Hello',
  2338. '/legend',
  2339. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2340. array('div' => array('class' => 'input select required')),
  2341. '*/div',
  2342. array('div' => array('class' => 'input text required')),
  2343. '*/div',
  2344. array('div' => array('class' => 'input text')),
  2345. '*/div',
  2346. array('div' => array('class' => 'input text')),
  2347. '*/div',
  2348. '/fieldset'
  2349. );
  2350. $this->assertTags($result, $expected);
  2351. $this->Form->create(false);
  2352. $expected = array(
  2353. 'fieldset' => array(),
  2354. array('div' => array('class' => 'input text')),
  2355. 'label' => array('for' => 'foo'),
  2356. 'Foo',
  2357. '/label',
  2358. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2359. '*/div',
  2360. '/fieldset'
  2361. );
  2362. $result = $this->Form->inputs(
  2363. array('foo' => array('type' => 'text')),
  2364. array(),
  2365. array('legend' => false)
  2366. );
  2367. $this->assertTags($result, $expected);
  2368. }
  2369. /**
  2370. * testSelectAsCheckbox method
  2371. *
  2372. * test multi-select widget with checkbox formatting.
  2373. *
  2374. * @return void
  2375. */
  2376. public function testSelectAsCheckbox() {
  2377. $result = $this->Form->select(
  2378. 'Model.multi_field',
  2379. array('first', 'second', 'third'),
  2380. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2381. );
  2382. $expected = array(
  2383. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2384. array('div' => array('class' => 'checkbox')),
  2385. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2386. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2387. 'first',
  2388. '/label',
  2389. '/div',
  2390. array('div' => array('class' => 'checkbox')),
  2391. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2392. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2393. 'second',
  2394. '/label',
  2395. '/div',
  2396. array('div' => array('class' => 'checkbox')),
  2397. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2398. array('label' => array('for' => 'model-multi-field-2')),
  2399. 'third',
  2400. '/label',
  2401. '/div',
  2402. );
  2403. $this->assertTags($result, $expected);
  2404. $result = $this->Form->select(
  2405. 'Model.multi_field',
  2406. array('1/2' => 'half'),
  2407. array('multiple' => 'checkbox')
  2408. );
  2409. $expected = array(
  2410. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2411. array('div' => array('class' => 'checkbox')),
  2412. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2413. array('label' => array('for' => 'model-multi-field-1-2')),
  2414. 'half',
  2415. '/label',
  2416. '/div',
  2417. );
  2418. $this->assertTags($result, $expected);
  2419. }
  2420. /**
  2421. * testLabel method
  2422. *
  2423. * test label generation.
  2424. *
  2425. * @return void
  2426. */
  2427. public function testLabel() {
  2428. $result = $this->Form->label('Person.name');
  2429. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2430. $result = $this->Form->label('Person.name');
  2431. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2432. $result = $this->Form->label('Person.first_name');
  2433. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2434. $result = $this->Form->label('Person.first_name', 'Your first name');
  2435. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2436. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2437. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2438. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2439. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2440. $result = $this->Form->label('Person.first_name', '');
  2441. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2442. $result = $this->Form->label('Person.2.name', '');
  2443. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2444. }
  2445. /**
  2446. * testTextbox method
  2447. *
  2448. * test textbox element generation
  2449. *
  2450. * @return void
  2451. */
  2452. public function testTextbox() {
  2453. $result = $this->Form->text('Model.field');
  2454. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2455. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2456. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2457. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2458. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2459. }
  2460. /**
  2461. * Test that text() hooks up with request data and error fields.
  2462. *
  2463. * @return void
  2464. */
  2465. public function testTextBoxDataAndError() {
  2466. $this->article['errors'] = [
  2467. 'Contact' => ['text' => 'wrong']
  2468. ];
  2469. $this->Form->create($this->article);
  2470. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2471. $result = $this->Form->text('Model.text');
  2472. $expected = [
  2473. 'input' => [
  2474. 'type' => 'text',
  2475. 'name' => 'Model[text]',
  2476. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2477. ]
  2478. ];
  2479. $this->assertTags($result, $expected);
  2480. $this->Form->request->data['Contact']['text'] = 'test';
  2481. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2482. $expected = [
  2483. 'input' => [
  2484. 'type' => 'text',
  2485. 'name' => 'Contact[text]',
  2486. 'value' => 'test',
  2487. 'id' => 'theID',
  2488. 'class' => 'form-error'
  2489. ]
  2490. ];
  2491. $this->assertTags($result, $expected);
  2492. }
  2493. /**
  2494. * testDefaultValue method
  2495. *
  2496. * Test default value setting
  2497. *
  2498. * @return void
  2499. */
  2500. public function testTextDefaultValue() {
  2501. $this->Form->request->data['Model']['field'] = 'test';
  2502. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2503. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2504. $this->assertTags($result, $expected);
  2505. unset($this->Form->request->data['Model']['field']);
  2506. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2507. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2508. $this->assertTags($result, $expected);
  2509. }
  2510. /**
  2511. * testError method
  2512. *
  2513. * Test field error generation
  2514. *
  2515. * @return void
  2516. */
  2517. public function testError() {
  2518. $this->article['errors'] = [
  2519. 'Article' => ['field' => 'email']
  2520. ];
  2521. $this->Form->create($this->article);
  2522. $result = $this->Form->error('Article.field');
  2523. $expected = [
  2524. ['div' => ['class' => 'error-message']],
  2525. 'email',
  2526. '/div',
  2527. ];
  2528. $this->assertTags($result, $expected);
  2529. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2530. $expected = [
  2531. ['div' => ['class' => 'error-message']],
  2532. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2533. '/div',
  2534. ];
  2535. $this->assertTags($result, $expected);
  2536. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  2537. $expected = [
  2538. ['div' => ['class' => 'error-message']],
  2539. '<strong', 'Badness!', '/strong',
  2540. '/div',
  2541. ];
  2542. $this->assertTags($result, $expected);
  2543. }
  2544. /**
  2545. * Test error with nested lists.
  2546. *
  2547. * @return void
  2548. */
  2549. public function testErrorMessages() {
  2550. $this->article['errors'] = [
  2551. 'Article' => ['field' => 'email']
  2552. ];
  2553. $this->Form->create($this->article);
  2554. $result = $this->Form->error('Article.field', array(
  2555. 'email' => 'No good!'
  2556. ));
  2557. $expected = array(
  2558. 'div' => array('class' => 'error-message'),
  2559. 'No good!',
  2560. '/div'
  2561. );
  2562. $this->assertTags($result, $expected);
  2563. }
  2564. /**
  2565. * test error() with multiple messages.
  2566. *
  2567. * @return void
  2568. */
  2569. public function testErrorMultipleMessages() {
  2570. $this->article['errors'] = [
  2571. 'field' => ['notEmpty', 'email', 'Something else']
  2572. ];
  2573. $this->Form->create($this->article);
  2574. $result = $this->Form->error('field', array(
  2575. 'notEmpty' => 'Cannot be empty',
  2576. 'email' => 'No good!'
  2577. ));
  2578. $expected = array(
  2579. 'div' => array('class' => 'error-message'),
  2580. 'ul' => array(),
  2581. '<li', 'Cannot be empty', '/li',
  2582. '<li', 'No good!', '/li',
  2583. '<li', 'Something else', '/li',
  2584. '/ul',
  2585. '/div'
  2586. );
  2587. $this->assertTags($result, $expected);
  2588. }
  2589. /**
  2590. * testPassword method
  2591. *
  2592. * Test password element generation
  2593. *
  2594. * @return void
  2595. */
  2596. public function testPassword() {
  2597. $this->article['errors'] = [
  2598. 'Contact' => [
  2599. 'passwd' => 1
  2600. ]
  2601. ];
  2602. $this->Form->create($this->article);
  2603. $result = $this->Form->password('Contact.field');
  2604. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  2605. $this->Form->request->data['Contact']['passwd'] = 'test';
  2606. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2607. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2608. }
  2609. /**
  2610. * testRadio method
  2611. *
  2612. * Test radio element set generation
  2613. *
  2614. * @return void
  2615. */
  2616. public function testRadio() {
  2617. $result = $this->Form->radio('Model.field', array('option A'));
  2618. $expected = array(
  2619. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2620. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2621. 'label' => array('for' => 'model-field-0'),
  2622. 'option A',
  2623. '/label'
  2624. );
  2625. $this->assertTags($result, $expected);
  2626. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2627. $expected = array(
  2628. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2629. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2630. array('label' => array('for' => 'model-field-0')),
  2631. 'option A',
  2632. '/label',
  2633. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  2634. array('label' => array('for' => 'model-field-1')),
  2635. 'option B',
  2636. '/label',
  2637. );
  2638. $this->assertTags($result, $expected);
  2639. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2640. $expected = array(
  2641. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  2642. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  2643. array('label' => array('for' => 'employee-gender-male')),
  2644. 'Male',
  2645. '/label',
  2646. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  2647. array('label' => array('for' => 'employee-gender-female')),
  2648. 'Female',
  2649. '/label',
  2650. );
  2651. $this->assertTags($result, $expected);
  2652. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  2653. $expected = array(
  2654. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  2655. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  2656. array('label' => array('for' => 'model-custom-0')),
  2657. 'option A',
  2658. '/label',
  2659. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  2660. array('label' => array('for' => 'model-custom-1')),
  2661. 'option B',
  2662. '/label',
  2663. );
  2664. $this->assertTags($result, $expected);
  2665. }
  2666. /**
  2667. * test generating radio input inside label ala twitter bootstrap
  2668. *
  2669. * @return void
  2670. */
  2671. public function testRadioInputInsideLabel() {
  2672. $this->Form->templates([
  2673. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2674. 'radioContainer' => '{{label}}'
  2675. ]);
  2676. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  2677. $expected = [
  2678. ['input' => [
  2679. 'type' => 'hidden',
  2680. 'name' => 'Model[field]',
  2681. 'value' => ''
  2682. ]],
  2683. ['label' => ['for' => 'model-field-0']],
  2684. ['input' => [
  2685. 'type' => 'radio',
  2686. 'name' => 'Model[field]',
  2687. 'value' => '0',
  2688. 'id' => 'model-field-0'
  2689. ]],
  2690. 'option A',
  2691. '/label',
  2692. ['label' => ['for' => 'model-field-1']],
  2693. ['input' => [
  2694. 'type' => 'radio',
  2695. 'name' => 'Model[field]',
  2696. 'value' => '1',
  2697. 'id' => 'model-field-1'
  2698. ]],
  2699. 'option B',
  2700. '/label',
  2701. ];
  2702. $this->assertTags($result, $expected);
  2703. }
  2704. /**
  2705. * test disabling the hidden input for radio buttons
  2706. *
  2707. * @return void
  2708. */
  2709. public function testRadioHiddenInputDisabling() {
  2710. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  2711. $expected = array(
  2712. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  2713. 'label' => array('for' => 'model-1-field-0'),
  2714. 'option A',
  2715. '/label'
  2716. );
  2717. $this->assertTags($result, $expected);
  2718. }
  2719. /**
  2720. * testSelect method
  2721. *
  2722. * Test select element generation.
  2723. *
  2724. * @return void
  2725. */
  2726. public function testSelect() {
  2727. $result = $this->Form->select('Model.field', array());
  2728. $expected = array(
  2729. 'select' => array('name' => 'Model[field]'),
  2730. '/select'
  2731. );
  2732. $this->assertTags($result, $expected);
  2733. $this->Form->request->data = array('Model' => array('field' => 'value'));
  2734. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2735. $expected = array(
  2736. 'select' => array('name' => 'Model[field]'),
  2737. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2738. 'good',
  2739. '/option',
  2740. array('option' => array('value' => 'other')),
  2741. 'bad',
  2742. '/option',
  2743. '/select'
  2744. );
  2745. $this->assertTags($result, $expected);
  2746. $this->Form->request->data = array();
  2747. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2748. $expected = array(
  2749. 'select' => array('name' => 'Model[field]'),
  2750. array('option' => array('value' => 'value')),
  2751. 'good',
  2752. '/option',
  2753. array('option' => array('value' => 'other')),
  2754. 'bad',
  2755. '/option',
  2756. '/select'
  2757. );
  2758. $this->assertTags($result, $expected);
  2759. $result = $this->Form->select(
  2760. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  2761. array('empty' => false)
  2762. );
  2763. $expected = array(
  2764. 'select' => array('name' => 'Model[field]'),
  2765. array('option' => array('value' => 'first')),
  2766. 'first &quot;html&quot; &lt;chars&gt;',
  2767. '/option',
  2768. array('option' => array('value' => 'second')),
  2769. 'value',
  2770. '/option',
  2771. '/select'
  2772. );
  2773. $this->assertTags($result, $expected);
  2774. $result = $this->Form->select(
  2775. 'Model.field',
  2776. array('first' => 'first "html" <chars>', 'second' => 'value'),
  2777. array('escape' => false, 'empty' => false)
  2778. );
  2779. $expected = array(
  2780. 'select' => array('name' => 'Model[field]'),
  2781. array('option' => array('value' => 'first')),
  2782. 'first "html" <chars>',
  2783. '/option',
  2784. array('option' => array('value' => 'second')),
  2785. 'value',
  2786. '/option',
  2787. '/select'
  2788. );
  2789. $this->assertTags($result, $expected);
  2790. $options = array(
  2791. array('value' => 'first', 'text' => 'First'),
  2792. array('value' => 'first', 'text' => 'Another First'),
  2793. );
  2794. $result = $this->Form->select(
  2795. 'Model.field',
  2796. $options,
  2797. array('escape' => false, 'empty' => false)
  2798. );
  2799. $expected = array(
  2800. 'select' => array('name' => 'Model[field]'),
  2801. array('option' => array('value' => 'first')),
  2802. 'First',
  2803. '/option',
  2804. array('option' => array('value' => 'first')),
  2805. 'Another First',
  2806. '/option',
  2807. '/select'
  2808. );
  2809. $this->assertTags($result, $expected);
  2810. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  2811. $result = $this->Form->select(
  2812. 'Model.contact_id',
  2813. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  2814. array('escape' => false, 'empty' => 'pick something')
  2815. );
  2816. $expected = array(
  2817. 'select' => array('name' => 'Model[contact_id]'),
  2818. array('option' => array('value' => '')), 'pick something', '/option',
  2819. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  2820. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  2821. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  2822. '/select'
  2823. );
  2824. $this->assertTags($result, $expected);
  2825. $this->Form->request->data['Model']['field'] = 0;
  2826. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  2827. $expected = array(
  2828. 'select' => array('name' => 'Model[field]'),
  2829. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  2830. array('option' => array('value' => '1')), 'Yes', '/option',
  2831. '/select'
  2832. );
  2833. $this->assertTags($result, $expected);
  2834. }
  2835. /**
  2836. * test select() with required and disabled attributes.
  2837. *
  2838. * @return void
  2839. */
  2840. public function testSelectRequired() {
  2841. $this->article['required'] = [
  2842. 'user_id' => true
  2843. ];
  2844. $this->Form->create($this->article);
  2845. $result = $this->Form->select('user_id', array('option A'));
  2846. $expected = array(
  2847. 'select' => array(
  2848. 'name' => 'user_id',
  2849. 'required' => 'required'
  2850. ),
  2851. array('option' => array('value' => '0')), 'option A', '/option',
  2852. '/select'
  2853. );
  2854. $this->assertTags($result, $expected);
  2855. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  2856. $expected = array(
  2857. 'select' => array(
  2858. 'name' => 'user_id',
  2859. 'disabled' => 'disabled'
  2860. ),
  2861. array('option' => array('value' => '0')), 'option A', '/option',
  2862. '/select'
  2863. );
  2864. $this->assertTags($result, $expected);
  2865. }
  2866. /**
  2867. * testNestedSelect method
  2868. *
  2869. * test select element generation with optgroups
  2870. *
  2871. * @return void
  2872. */
  2873. public function testNestedSelect() {
  2874. $result = $this->Form->select(
  2875. 'Model.field',
  2876. array(1 => 'One', 2 => 'Two', 'Three' => array(
  2877. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  2878. )), array('empty' => false)
  2879. );
  2880. $expected = array(
  2881. 'select' => array('name' => 'Model[field]'),
  2882. array('option' => array('value' => 1)),
  2883. 'One',
  2884. '/option',
  2885. array('option' => array('value' => 2)),
  2886. 'Two',
  2887. '/option',
  2888. array('optgroup' => array('label' => 'Three')),
  2889. array('option' => array('value' => 3)),
  2890. 'Three',
  2891. '/option',
  2892. array('option' => array('value' => 4)),
  2893. 'Four',
  2894. '/option',
  2895. array('option' => array('value' => 5)),
  2896. 'Five',
  2897. '/option',
  2898. '/optgroup',
  2899. '/select'
  2900. );
  2901. $this->assertTags($result, $expected);
  2902. }
  2903. /**
  2904. * testSelectMultiple method
  2905. *
  2906. * test generation of multiple select elements
  2907. *
  2908. * @return void
  2909. */
  2910. public function testSelectMultiple() {
  2911. $options = array('first', 'second', 'third');
  2912. $result = $this->Form->select(
  2913. 'Model.multi_field',
  2914. $options,
  2915. array('multiple' => true)
  2916. );
  2917. $expected = array(
  2918. 'input' => array(
  2919. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2920. ),
  2921. 'select' => array(
  2922. 'name' => 'Model[multi_field][]',
  2923. 'multiple' => 'multiple'
  2924. ),
  2925. array('option' => array('value' => '0')),
  2926. 'first',
  2927. '/option',
  2928. array('option' => array('value' => '1')),
  2929. 'second',
  2930. '/option',
  2931. array('option' => array('value' => '2')),
  2932. 'third',
  2933. '/option',
  2934. '/select'
  2935. );
  2936. $this->assertTags($result, $expected);
  2937. $result = $this->Form->select(
  2938. 'Model.multi_field',
  2939. $options,
  2940. array('multiple' => 'multiple')
  2941. );
  2942. $this->assertTags($result, $expected);
  2943. }
  2944. /**
  2945. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  2946. *
  2947. * @return void
  2948. */
  2949. public function testCheckboxZeroValue() {
  2950. $result = $this->Form->input('User.get_spam', array(
  2951. 'type' => 'checkbox',
  2952. 'value' => '0',
  2953. 'hiddenField' => '1',
  2954. ));
  2955. $expected = array(
  2956. 'div' => array('class' => 'input checkbox'),
  2957. array('input' => array(
  2958. 'type' => 'hidden', 'name' => 'User[get_spam]',
  2959. 'value' => '1'
  2960. )),
  2961. array('input' => array(
  2962. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  2963. 'value' => '0', 'id' => 'user-get-spam'
  2964. )),
  2965. 'label' => array('for' => 'user-get-spam'),
  2966. 'Get Spam',
  2967. '/label',
  2968. '/div'
  2969. );
  2970. $this->assertTags($result, $expected);
  2971. }
  2972. /**
  2973. * test generation of habtm select boxes.
  2974. *
  2975. * @return void
  2976. */
  2977. public function testHabtmSelectBox() {
  2978. $this->loadFixtures('Article');
  2979. $options = array(
  2980. 1 => 'blue',
  2981. 2 => 'red',
  2982. 3 => 'green'
  2983. );
  2984. $tags = [
  2985. new Entity(['id' => 1, 'name' => 'blue']),
  2986. new Entity(['id' => 3, 'name' => 'green'])
  2987. ];
  2988. $article = new Article(['tags' => $tags]);
  2989. $this->Form->create($article);
  2990. $result = $this->Form->input('tags._ids', ['options' => $options]);
  2991. $expected = array(
  2992. 'div' => array('class' => 'input select'),
  2993. 'label' => array('for' => 'tags-ids'),
  2994. 'Tags',
  2995. '/label',
  2996. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  2997. 'select' => array(
  2998. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  2999. 'multiple' => 'multiple'
  3000. ),
  3001. array('option' => array('value' => '1', 'selected' => 'selected')),
  3002. 'blue',
  3003. '/option',
  3004. array('option' => array('value' => '2')),
  3005. 'red',
  3006. '/option',
  3007. array('option' => array('value' => '3', 'selected' => 'selected')),
  3008. 'green',
  3009. '/option',
  3010. '/select',
  3011. '/div'
  3012. );
  3013. $this->assertTags($result, $expected);
  3014. // make sure only 50 is selected, and not 50f5c0cf
  3015. $options = array(
  3016. '1' => 'blue',
  3017. '50f5c0cf' => 'red',
  3018. '50' => 'green'
  3019. );
  3020. $tags = [
  3021. new Entity(['id' => 1, 'name' => 'blue']),
  3022. new Entity(['id' => 50, 'name' => 'green'])
  3023. ];
  3024. $article = new Article(['tags' => $tags]);
  3025. $this->Form->create($article);
  3026. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3027. $expected = array(
  3028. 'div' => array('class' => 'input select'),
  3029. 'label' => array('for' => 'tags-ids'),
  3030. 'Tags',
  3031. '/label',
  3032. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3033. 'select' => array(
  3034. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3035. 'multiple' => 'multiple'
  3036. ),
  3037. array('option' => array('value' => '1', 'selected' => 'selected')),
  3038. 'blue',
  3039. '/option',
  3040. array('option' => array('value' => '50f5c0cf')),
  3041. 'red',
  3042. '/option',
  3043. array('option' => array('value' => '50', 'selected' => 'selected')),
  3044. 'green',
  3045. '/option',
  3046. '/select',
  3047. '/div'
  3048. );
  3049. $this->assertTags($result, $expected);
  3050. }
  3051. /**
  3052. * test generation of multi select elements in checkbox format
  3053. *
  3054. * @return void
  3055. */
  3056. public function testSelectMultipleCheckboxes() {
  3057. $result = $this->Form->select(
  3058. 'Model.multi_field',
  3059. array('first', 'second', 'third'),
  3060. array('multiple' => 'checkbox')
  3061. );
  3062. $expected = array(
  3063. 'input' => array(
  3064. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3065. ),
  3066. array('div' => array('class' => 'checkbox')),
  3067. array('input' => array(
  3068. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3069. 'value' => '0', 'id' => 'model-multi-field-0'
  3070. )),
  3071. array('label' => array('for' => 'model-multi-field-0')),
  3072. 'first',
  3073. '/label',
  3074. '/div',
  3075. array('div' => array('class' => 'checkbox')),
  3076. array('input' => array(
  3077. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3078. 'value' => '1', 'id' => 'model-multi-field-1'
  3079. )),
  3080. array('label' => array('for' => 'model-multi-field-1')),
  3081. 'second',
  3082. '/label',
  3083. '/div',
  3084. array('div' => array('class' => 'checkbox')),
  3085. array('input' => array(
  3086. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3087. 'value' => '2', 'id' => 'model-multi-field-2'
  3088. )),
  3089. array('label' => array('for' => 'model-multi-field-2')),
  3090. 'third',
  3091. '/label',
  3092. '/div'
  3093. );
  3094. $this->assertTags($result, $expected);
  3095. $result = $this->Form->select(
  3096. 'Model.multi_field',
  3097. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3098. array('multiple' => 'checkbox')
  3099. );
  3100. $expected = array(
  3101. 'input' => array(
  3102. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3103. ),
  3104. array('div' => array('class' => 'checkbox')),
  3105. array('input' => array(
  3106. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3107. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3108. )),
  3109. array('label' => array('for' => 'model-multi-field-a+')),
  3110. 'first',
  3111. '/label',
  3112. '/div',
  3113. array('div' => array('class' => 'checkbox')),
  3114. array('input' => array(
  3115. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3116. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3117. )),
  3118. array('label' => array('for' => 'model-multi-field-a++')),
  3119. 'second',
  3120. '/label',
  3121. '/div',
  3122. array('div' => array('class' => 'checkbox')),
  3123. array('input' => array(
  3124. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3125. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3126. )),
  3127. array('label' => array('for' => 'model-multi-field-a+++')),
  3128. 'third',
  3129. '/label',
  3130. '/div'
  3131. );
  3132. $this->assertTags($result, $expected);
  3133. $result = $this->Form->select(
  3134. 'Model.multi_field',
  3135. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3136. array('multiple' => 'checkbox')
  3137. );
  3138. $expected = array(
  3139. 'input' => array(
  3140. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3141. ),
  3142. array('div' => array('class' => 'checkbox')),
  3143. array('input' => array(
  3144. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3145. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3146. )),
  3147. array('label' => array('for' => 'model-multi-field-a-b')),
  3148. 'first',
  3149. '/label',
  3150. '/div',
  3151. array('div' => array('class' => 'checkbox')),
  3152. array('input' => array(
  3153. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3154. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3155. )),
  3156. array('label' => array('for' => 'model-multi-field-a-b1')),
  3157. 'second',
  3158. '/label',
  3159. '/div',
  3160. array('div' => array('class' => 'checkbox')),
  3161. array('input' => array(
  3162. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3163. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3164. )),
  3165. array('label' => array('for' => 'model-multi-field-a-b2')),
  3166. 'third',
  3167. '/label',
  3168. '/div'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. }
  3172. /**
  3173. * Ensure that multiCheckbox reads from the request data.
  3174. *
  3175. * @return void
  3176. */
  3177. public function testSelectMultipleCheckboxRequestData() {
  3178. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3179. $result = $this->Form->select(
  3180. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3181. );
  3182. $expected = array(
  3183. 'input' => array(
  3184. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3185. ),
  3186. array('div' => array('class' => 'checkbox')),
  3187. array('input' => array(
  3188. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3189. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3190. )),
  3191. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3192. 'first',
  3193. '/label',
  3194. '/div',
  3195. array('div' => array('class' => 'checkbox')),
  3196. array('input' => array(
  3197. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3198. 'value' => 'Array', 'id' => 'model-tags-array'
  3199. )),
  3200. array('label' => array('for' => 'model-tags-array')),
  3201. 'Array',
  3202. '/label',
  3203. '/div'
  3204. );
  3205. $this->assertTags($result, $expected);
  3206. }
  3207. /**
  3208. * Checks the security hash array generated for multiple-input checkbox elements
  3209. *
  3210. * @return void
  3211. */
  3212. public function testSelectMultipleCheckboxSecurity() {
  3213. $this->Form->request->params['_Token'] = 'testKey';
  3214. $this->assertEquals(array(), $this->Form->fields);
  3215. $result = $this->Form->select(
  3216. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3217. array('multiple' => 'checkbox')
  3218. );
  3219. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3220. $result = $this->Form->secure($this->Form->fields);
  3221. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3222. $this->assertRegExp('/"' . $key . '"/', $result);
  3223. }
  3224. /**
  3225. * Multiple select elements should always be secured as they always participate
  3226. * in the POST data.
  3227. *
  3228. * @return void
  3229. */
  3230. public function testSelectMultipleSecureWithNoOptions() {
  3231. $this->assertEquals(array(), $this->Form->fields);
  3232. $this->Form->select(
  3233. 'Model.select',
  3234. array(),
  3235. array('multiple' => true)
  3236. );
  3237. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3238. }
  3239. /**
  3240. * When a select box has no options it should not be added to the fields list
  3241. * as it always fail post validation.
  3242. *
  3243. * @return void
  3244. */
  3245. public function testSelectNoSecureWithNoOptions() {
  3246. $this->Form->request->params['_Token'] = 'testkey';
  3247. $this->assertEquals([], $this->Form->fields);
  3248. $this->Form->select(
  3249. 'Model.select',
  3250. []
  3251. );
  3252. $this->assertEquals([], $this->Form->fields);
  3253. $this->Form->select(
  3254. 'Model.user_id',
  3255. [],
  3256. ['empty' => true]
  3257. );
  3258. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3259. }
  3260. /**
  3261. * testInputMultipleCheckboxes method
  3262. *
  3263. * test input() resulting in multi select elements being generated.
  3264. *
  3265. * @return void
  3266. */
  3267. public function testInputMultipleCheckboxes() {
  3268. $result = $this->Form->input('Model.multi_field', array(
  3269. 'options' => array('first', 'second', 'third'),
  3270. 'multiple' => 'checkbox'
  3271. ));
  3272. $expected = array(
  3273. array('div' => array('class' => 'input select')),
  3274. array('label' => array('for' => 'model-multi-field')),
  3275. 'Multi Field',
  3276. '/label',
  3277. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3278. array('div' => array('class' => 'checkbox')),
  3279. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3280. array('label' => array('for' => 'model-multi-field-0')),
  3281. 'first',
  3282. '/label',
  3283. '/div',
  3284. array('div' => array('class' => 'checkbox')),
  3285. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3286. array('label' => array('for' => 'model-multi-field-1')),
  3287. 'second',
  3288. '/label',
  3289. '/div',
  3290. array('div' => array('class' => 'checkbox')),
  3291. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3292. array('label' => array('for' => 'model-multi-field-2')),
  3293. 'third',
  3294. '/label',
  3295. '/div',
  3296. '/div'
  3297. );
  3298. $this->assertTags($result, $expected);
  3299. $result = $this->Form->input('Model.multi_field', array(
  3300. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3301. 'multiple' => 'checkbox'
  3302. ));
  3303. $expected = array(
  3304. array('div' => array('class' => 'input select')),
  3305. array('label' => array('for' => 'model-multi-field')),
  3306. 'Multi Field',
  3307. '/label',
  3308. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3309. array('div' => array('class' => 'checkbox')),
  3310. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3311. array('label' => array('for' => 'model-multi-field-a')),
  3312. 'first',
  3313. '/label',
  3314. '/div',
  3315. array('div' => array('class' => 'checkbox')),
  3316. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3317. array('label' => array('for' => 'model-multi-field-b')),
  3318. 'second',
  3319. '/label',
  3320. '/div',
  3321. array('div' => array('class' => 'checkbox')),
  3322. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3323. array('label' => array('for' => 'model-multi-field-c')),
  3324. 'third',
  3325. '/label',
  3326. '/div',
  3327. '/div'
  3328. );
  3329. $this->assertTags($result, $expected);
  3330. }
  3331. /**
  3332. * testSelectHiddenFieldOmission method
  3333. *
  3334. * test that select() with 'hiddenField' => false omits the hidden field
  3335. *
  3336. * @return void
  3337. */
  3338. public function testSelectHiddenFieldOmission() {
  3339. $result = $this->Form->select('Model.multi_field',
  3340. array('first', 'second'),
  3341. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3342. );
  3343. $this->assertNotContains('type="hidden"', $result);
  3344. }
  3345. /**
  3346. * test that select() with multiple = checkbox works with overriding name attribute.
  3347. *
  3348. * @return void
  3349. */
  3350. public function testSelectCheckboxMultipleOverrideName() {
  3351. $result = $this->Form->select('category', ['1', '2'], [
  3352. 'multiple' => 'checkbox',
  3353. 'name' => 'fish',
  3354. ]);
  3355. $expected = array(
  3356. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3357. array('div' => array('class' => 'checkbox')),
  3358. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3359. array('label' => array('for' => 'fish-0')), '1', '/label',
  3360. '/div',
  3361. array('div' => array('class' => 'checkbox')),
  3362. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3363. array('label' => array('for' => 'fish-1')), '2', '/label',
  3364. '/div'
  3365. );
  3366. $this->assertTags($result, $expected);
  3367. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3368. 'name' => 'fish',
  3369. ]);
  3370. $this->assertTags($result, $expected);
  3371. }
  3372. /**
  3373. * testCheckbox method
  3374. *
  3375. * Test generation of checkboxes
  3376. *
  3377. * @return void
  3378. */
  3379. public function testCheckbox() {
  3380. $result = $this->Form->checkbox('Model.field');
  3381. $expected = array(
  3382. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3383. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3384. );
  3385. $this->assertTags($result, $expected);
  3386. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3387. $expected = array(
  3388. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3389. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  3390. );
  3391. $this->assertTags($result, $expected);
  3392. }
  3393. /**
  3394. * testCheckboxDefaultValue method
  3395. *
  3396. * Test default value setting on checkbox() method
  3397. *
  3398. * @return void
  3399. */
  3400. public function testCheckboxDefaultValue() {
  3401. $this->Form->request->data['Model']['field'] = false;
  3402. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3403. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3404. unset($this->Form->request->data['Model']['field']);
  3405. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3406. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3407. $this->Form->request->data['Model']['field'] = true;
  3408. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3409. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3410. unset($this->Form->request->data['Model']['field']);
  3411. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3412. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3413. }
  3414. /**
  3415. * Test checkbox being checked or having errors.
  3416. *
  3417. * @return void
  3418. */
  3419. public function testCheckboxCheckedAndError() {
  3420. $this->article['errors'] = [
  3421. 'published' => true
  3422. ];
  3423. $this->Form->request->data['published'] = 'myvalue';
  3424. $this->Form->create($this->article);
  3425. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3426. $expected = array(
  3427. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3428. array('input' => array(
  3429. 'type' => 'checkbox',
  3430. 'name' => 'published',
  3431. 'value' => 'myvalue',
  3432. 'id' => 'theID',
  3433. 'checked' => 'checked',
  3434. 'class' => 'form-error'
  3435. ))
  3436. );
  3437. $this->assertTags($result, $expected);
  3438. $this->Form->request->data['published'] = '';
  3439. $result = $this->Form->checkbox('published');
  3440. $expected = array(
  3441. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3442. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3443. );
  3444. $this->assertTags($result, $expected);
  3445. }
  3446. /**
  3447. * test checkbox() with a custom name attribute
  3448. *
  3449. * @return void
  3450. */
  3451. public function testCheckboxCustomNameAttribute() {
  3452. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3453. $expected = array(
  3454. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3455. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3456. );
  3457. $this->assertTags($result, $expected);
  3458. }
  3459. /**
  3460. * Test that the hidden input for checkboxes can be omitted or set to a
  3461. * specific value.
  3462. *
  3463. * @return void
  3464. */
  3465. public function testCheckboxHiddenField() {
  3466. $result = $this->Form->checkbox('UserForm.something', array(
  3467. 'hiddenField' => false
  3468. ));
  3469. $expected = array(
  3470. 'input' => array(
  3471. 'type' => 'checkbox',
  3472. 'name' => 'UserForm[something]',
  3473. 'value' => '1'
  3474. ),
  3475. );
  3476. $this->assertTags($result, $expected);
  3477. $result = $this->Form->checkbox('UserForm.something', array(
  3478. 'value' => 'Y',
  3479. 'hiddenField' => 'N',
  3480. ));
  3481. $expected = array(
  3482. array('input' => array(
  3483. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3484. 'value' => 'N'
  3485. )),
  3486. array('input' => array(
  3487. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3488. 'value' => 'Y'
  3489. )),
  3490. );
  3491. $this->assertTags($result, $expected);
  3492. }
  3493. /**
  3494. * Test the time type.
  3495. *
  3496. * @return void
  3497. */
  3498. public function testTime() {
  3499. $result = $this->Form->time('start_time', array(
  3500. 'timeFormat' => 12,
  3501. 'interval' => 5,
  3502. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  3503. ));
  3504. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3505. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3506. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3507. $this->assertNotContains('year', $result);
  3508. $this->assertNotContains('month', $result);
  3509. $this->assertNotContains('day', $result);
  3510. $result = $this->Form->time('start_time', array(
  3511. 'timeFormat' => 12,
  3512. 'interval' => 5,
  3513. 'value' => '2014-03-08 16:30:00'
  3514. ));
  3515. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3516. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3517. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3518. $this->assertNotContains('year', $result);
  3519. $this->assertNotContains('month', $result);
  3520. $this->assertNotContains('day', $result);
  3521. }
  3522. /**
  3523. * Test the date type.
  3524. *
  3525. * @return void
  3526. */
  3527. public function testDate() {
  3528. $result = $this->Form->date('start_day', array(
  3529. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  3530. ));
  3531. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  3532. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  3533. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  3534. $this->assertNotContains('hour', $result);
  3535. $this->assertNotContains('minute', $result);
  3536. $this->assertNotContains('second', $result);
  3537. $this->assertNotContains('meridian', $result);
  3538. }
  3539. /**
  3540. * testDateTime method
  3541. *
  3542. * Test generation of date/time select elements
  3543. *
  3544. * @return void
  3545. */
  3546. public function testDateTime() {
  3547. extract($this->dateRegex);
  3548. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3549. $now = strtotime('now');
  3550. $expected = array(
  3551. array('select' => array('name' => 'Contact[date][month]')),
  3552. $monthsRegex,
  3553. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3554. date('F', $now),
  3555. '/option',
  3556. '*/select',
  3557. array('select' => array('name' => 'Contact[date][day]')),
  3558. $daysRegex,
  3559. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  3560. date('j', $now),
  3561. '/option',
  3562. '*/select',
  3563. array('select' => array('name' => 'Contact[date][year]')),
  3564. $yearsRegex,
  3565. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  3566. date('Y', $now),
  3567. '/option',
  3568. '*/select',
  3569. array('select' => array('name' => 'Contact[date][hour]')),
  3570. $hoursRegex,
  3571. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  3572. date('g', $now),
  3573. '/option',
  3574. '*/select',
  3575. array('select' => array('name' => 'Contact[date][minute]')),
  3576. $minutesRegex,
  3577. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  3578. date('i', $now),
  3579. '/option',
  3580. '*/select',
  3581. array('select' => array('name' => 'Contact[date][meridian]')),
  3582. $meridianRegex,
  3583. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  3584. date('a', $now),
  3585. '/option',
  3586. '*/select'
  3587. );
  3588. $this->assertTags($result, $expected);
  3589. }
  3590. /**
  3591. * Test empty defaulting to true for datetime.
  3592. *
  3593. * @return void
  3594. */
  3595. public function testDatetimeEmpty() {
  3596. extract($this->dateRegex);
  3597. $now = strtotime('now');
  3598. $result = $this->Form->dateTime('Contact.date', array(
  3599. 'empty' => true,
  3600. ));
  3601. $expected = array(
  3602. array('select' => array('name' => 'Contact[date][month]')),
  3603. $monthsRegex,
  3604. array('option' => array('value' => '')),
  3605. '/option',
  3606. '*/select',
  3607. array('select' => array('name' => 'Contact[date][day]')),
  3608. $daysRegex,
  3609. array('option' => array('value' => '')),
  3610. '/option',
  3611. '*/select',
  3612. array('select' => array('name' => 'Contact[date][year]')),
  3613. $yearsRegex,
  3614. array('option' => array('value' => '')),
  3615. '/option',
  3616. '*/select',
  3617. array('select' => array('name' => 'Contact[date][hour]')),
  3618. $hoursRegex,
  3619. array('option' => array('value' => '')),
  3620. '/option',
  3621. '*/select',
  3622. array('select' => array('name' => 'Contact[date][minute]')),
  3623. $minutesRegex,
  3624. array('option' => array('value' => '')),
  3625. '/option',
  3626. '*/select',
  3627. array('select' => array('name' => 'Contact[date][meridian]')),
  3628. $meridianRegex,
  3629. array('option' => array('value' => '')),
  3630. '/option',
  3631. '*/select'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  3635. }
  3636. /**
  3637. * Test datetime with interval option.
  3638. *
  3639. * @return void
  3640. */
  3641. public function testDatetimeMinuteInterval() {
  3642. extract($this->dateRegex);
  3643. $now = strtotime('now');
  3644. $result = $this->Form->dateTime('Contact.date', array(
  3645. 'interval' => 5,
  3646. 'value' => ''
  3647. ));
  3648. $expected = array(
  3649. array('select' => array('name' => 'Contact[date][month]')),
  3650. $monthsRegex,
  3651. array('option' => array('selected' => 'selected', 'value' => '')),
  3652. '/option',
  3653. '*/select',
  3654. array('select' => array('name' => 'Contact[date][day]')),
  3655. $daysRegex,
  3656. array('option' => array('selected' => 'selected', 'value' => '')),
  3657. '/option',
  3658. '*/select',
  3659. array('select' => array('name' => 'Contact[date][year]')),
  3660. $yearsRegex,
  3661. array('option' => array('selected' => 'selected', 'value' => '')),
  3662. '/option',
  3663. '*/select',
  3664. array('select' => array('name' => 'Contact[date][hour]')),
  3665. $hoursRegex,
  3666. array('option' => array('selected' => 'selected', 'value' => '')),
  3667. '/option',
  3668. '*/select',
  3669. array('select' => array('name' => 'Contact[date][minute]')),
  3670. $minutesRegex,
  3671. array('option' => array('selected' => 'selected', 'value' => '')),
  3672. '/option',
  3673. array('option' => array('value' => '00')),
  3674. '00',
  3675. '/option',
  3676. array('option' => array('value' => '05')),
  3677. '05',
  3678. '/option',
  3679. array('option' => array('value' => '10')),
  3680. '10',
  3681. '/option',
  3682. '*/select',
  3683. );
  3684. $this->assertTags($result, $expected);
  3685. }
  3686. /**
  3687. * Test dateTime with rounding
  3688. *
  3689. * @return void
  3690. */
  3691. public function testDateTimeRounding() {
  3692. $this->Form->request->data['Contact'] = array(
  3693. 'date' => array(
  3694. 'day' => '13',
  3695. 'month' => '12',
  3696. 'year' => '2010',
  3697. 'hour' => '04',
  3698. 'minute' => '19',
  3699. 'meridian' => 'AM'
  3700. )
  3701. );
  3702. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  3703. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3704. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  3705. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  3706. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  3707. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3708. }
  3709. /**
  3710. * test that datetime() and default values work.
  3711. *
  3712. * @return void
  3713. */
  3714. public function testDatetimeWithDefault() {
  3715. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  3716. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3717. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3718. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3719. $result = $this->Form->dateTime('Contact.updated', array(
  3720. 'default' => '2009-06-01 11:15:30'
  3721. ));
  3722. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3723. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3724. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3725. }
  3726. /**
  3727. * testDateTime all zeros
  3728. *
  3729. * @return void
  3730. */
  3731. public function testDateTimeAllZeros() {
  3732. $result = $this->Form->dateTime('Contact.date', array(
  3733. 'timeFormat' => false,
  3734. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  3735. 'value' => '0000-00-00'
  3736. ));
  3737. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  3738. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  3739. }
  3740. /**
  3741. * testDateTimeEmptyAsArray
  3742. *
  3743. * @return void
  3744. */
  3745. public function testDateTimeEmptyAsArray() {
  3746. $result = $this->Form->dateTime('Contact.date', array(
  3747. 'empty' => array(
  3748. 'day' => 'DAY',
  3749. 'month' => 'MONTH',
  3750. 'year' => 'YEAR',
  3751. 'hour' => 'HOUR',
  3752. 'minute' => 'MINUTE',
  3753. 'meridian' => false
  3754. )
  3755. ));
  3756. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3757. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3758. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3759. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  3760. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  3761. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  3762. $result = $this->Form->dateTime('Contact.date', array(
  3763. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  3764. ));
  3765. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3766. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3767. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3768. }
  3769. /**
  3770. * testFormDateTimeMulti method
  3771. *
  3772. * test multiple datetime element generation
  3773. *
  3774. * @return void
  3775. */
  3776. public function testFormDateTimeMulti() {
  3777. extract($this->dateRegex);
  3778. $result = $this->Form->dateTime('Contact.1.updated');
  3779. $this->assertContains('Contact[1][updated][month]', $result);
  3780. $this->assertContains('Contact[1][updated][day]', $result);
  3781. $this->assertContains('Contact[1][updated][year]', $result);
  3782. $this->assertContains('Contact[1][updated][hour]', $result);
  3783. $this->assertContains('Contact[1][updated][minute]', $result);
  3784. $this->assertContains('Contact[1][updated][meridian]', $result);
  3785. }
  3786. /**
  3787. * When changing the date format, the label should always focus the first select box when
  3788. * clicked.
  3789. *
  3790. * @return void
  3791. */
  3792. public function testDateTimeLabelIdMatchesFirstInput() {
  3793. $this->markTestIncomplete('Need to revisit once models work again.');
  3794. $result = $this->Form->input('Model.date', array('type' => 'date'));
  3795. $this->assertContains('label for="ModelDateMonth"', $result);
  3796. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  3797. $this->assertContains('label for="ModelDateDay"', $result);
  3798. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  3799. $this->assertContains('label for="ModelDateYear"', $result);
  3800. }
  3801. /**
  3802. * testMonth method
  3803. *
  3804. * @return void
  3805. */
  3806. public function testMonth() {
  3807. $result = $this->Form->month('Model.field', ['value' => '']);
  3808. $expected = array(
  3809. array('select' => array('name' => 'Model[field][month]')),
  3810. array('option' => array('value' => '', 'selected' => 'selected')),
  3811. '/option',
  3812. array('option' => array('value' => '01')),
  3813. date('F', strtotime('2008-01-01 00:00:00')),
  3814. '/option',
  3815. array('option' => array('value' => '02')),
  3816. date('F', strtotime('2008-02-01 00:00:00')),
  3817. '/option',
  3818. '*/select',
  3819. );
  3820. $this->assertTags($result, $expected);
  3821. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  3822. $expected = array(
  3823. array('select' => array('name' => 'Model[field][month]')),
  3824. array('option' => array('selected' => 'selected', 'value' => '')),
  3825. '/option',
  3826. array('option' => array('value' => '01')),
  3827. date('F', strtotime('2008-01-01 00:00:00')),
  3828. '/option',
  3829. array('option' => array('value' => '02')),
  3830. date('F', strtotime('2008-02-01 00:00:00')),
  3831. '/option',
  3832. '*/select',
  3833. );
  3834. $this->assertTags($result, $expected);
  3835. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  3836. $expected = array(
  3837. array('select' => array('name' => 'Model[field][month]')),
  3838. array('option' => array('selected' => 'selected', 'value' => '')),
  3839. '/option',
  3840. array('option' => array('value' => '01')),
  3841. '1',
  3842. '/option',
  3843. array('option' => array('value' => '02')),
  3844. '2',
  3845. '/option',
  3846. '*/select',
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $monthNames = [
  3850. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  3851. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  3852. ];
  3853. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  3854. $expected = array(
  3855. array('select' => array('name' => 'Model[field][month]')),
  3856. array('option' => array('value' => '')),
  3857. '/option',
  3858. array('option' => array('value' => '01', 'selected' => 'selected')),
  3859. 'Jan',
  3860. '/option',
  3861. array('option' => array('value' => '02')),
  3862. 'Feb',
  3863. '/option',
  3864. '*/select',
  3865. );
  3866. $this->assertTags($result, $expected);
  3867. $this->Form->request->data['Project']['release'] = '2050-02-10';
  3868. $result = $this->Form->month('Project.release');
  3869. $expected = array(
  3870. array('select' => array('name' => 'Project[release][month]')),
  3871. array('option' => array('value' => '')),
  3872. '/option',
  3873. array('option' => array('value' => '01')),
  3874. 'January',
  3875. '/option',
  3876. array('option' => array('value' => '02', 'selected' => 'selected')),
  3877. 'February',
  3878. '/option',
  3879. '*/select',
  3880. );
  3881. $this->assertTags($result, $expected);
  3882. }
  3883. /**
  3884. * testDay method
  3885. *
  3886. * @return void
  3887. */
  3888. public function testDay() {
  3889. extract($this->dateRegex);
  3890. $result = $this->Form->day('Model.field', array('value' => false));
  3891. $expected = array(
  3892. array('select' => array('name' => 'Model[field][day]')),
  3893. array('option' => array('selected' => 'selected', 'value' => '')),
  3894. '/option',
  3895. array('option' => array('value' => '01')),
  3896. '1',
  3897. '/option',
  3898. array('option' => array('value' => '02')),
  3899. '2',
  3900. '/option',
  3901. $daysRegex,
  3902. '/select',
  3903. );
  3904. $this->assertTags($result, $expected);
  3905. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  3906. $result = $this->Form->day('Model.field');
  3907. $expected = array(
  3908. array('select' => array('name' => 'Model[field][day]')),
  3909. array('option' => array('value' => '')),
  3910. '/option',
  3911. array('option' => array('value' => '01')),
  3912. '1',
  3913. '/option',
  3914. array('option' => array('value' => '02')),
  3915. '2',
  3916. '/option',
  3917. $daysRegex,
  3918. array('option' => array('value' => '10', 'selected' => 'selected')),
  3919. '10',
  3920. '/option',
  3921. $daysRegex,
  3922. '/select',
  3923. );
  3924. $this->assertTags($result, $expected);
  3925. $this->Form->request->data['Model']['field'] = '';
  3926. $result = $this->Form->day('Model.field', array('value' => '10'));
  3927. $expected = array(
  3928. array('select' => array('name' => 'Model[field][day]')),
  3929. array('option' => array('value' => '')),
  3930. '/option',
  3931. array('option' => array('value' => '01')),
  3932. '1',
  3933. '/option',
  3934. array('option' => array('value' => '02')),
  3935. '2',
  3936. '/option',
  3937. $daysRegex,
  3938. array('option' => array('value' => '10', 'selected' => 'selected')),
  3939. '10',
  3940. '/option',
  3941. $daysRegex,
  3942. '/select',
  3943. );
  3944. $this->assertTags($result, $expected);
  3945. $this->Form->request->data['Project']['release'] = '2050-10-10';
  3946. $result = $this->Form->day('Project.release');
  3947. $expected = array(
  3948. array('select' => array('name' => 'Project[release][day]')),
  3949. array('option' => array('value' => '')),
  3950. '/option',
  3951. array('option' => array('value' => '01')),
  3952. '1',
  3953. '/option',
  3954. array('option' => array('value' => '02')),
  3955. '2',
  3956. '/option',
  3957. $daysRegex,
  3958. array('option' => array('value' => '10', 'selected' => 'selected')),
  3959. '10',
  3960. '/option',
  3961. $daysRegex,
  3962. '/select',
  3963. );
  3964. $this->assertTags($result, $expected);
  3965. }
  3966. /**
  3967. * testMinute method
  3968. *
  3969. * @return void
  3970. */
  3971. public function testMinute() {
  3972. extract($this->dateRegex);
  3973. $result = $this->Form->minute('Model.field', ['value' => '']);
  3974. $expected = array(
  3975. array('select' => array('name' => 'Model[field][minute]')),
  3976. array('option' => array('selected' => 'selected', 'value' => '')),
  3977. '/option',
  3978. array('option' => array('value' => '00')),
  3979. '00',
  3980. '/option',
  3981. array('option' => array('value' => '01')),
  3982. '01',
  3983. '/option',
  3984. array('option' => array('value' => '02')),
  3985. '02',
  3986. '/option',
  3987. $minutesRegex,
  3988. '/select',
  3989. );
  3990. $this->assertTags($result, $expected);
  3991. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  3992. $result = $this->Form->minute('Model.field');
  3993. $expected = array(
  3994. array('select' => array('name' => 'Model[field][minute]')),
  3995. array('option' => array('value' => '')),
  3996. '/option',
  3997. array('option' => array('value' => '00')),
  3998. '00',
  3999. '/option',
  4000. array('option' => array('value' => '01')),
  4001. '01',
  4002. '/option',
  4003. array('option' => array('value' => '02')),
  4004. '02',
  4005. '/option',
  4006. $minutesRegex,
  4007. array('option' => array('value' => '12', 'selected' => 'selected')),
  4008. '12',
  4009. '/option',
  4010. $minutesRegex,
  4011. '/select',
  4012. );
  4013. $this->assertTags($result, $expected);
  4014. $this->Form->request->data['Model']['field'] = '';
  4015. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4016. $expected = array(
  4017. array('select' => array('name' => 'Model[field][minute]')),
  4018. array('option' => array('selected' => 'selected', 'value' => '')),
  4019. '/option',
  4020. array('option' => array('value' => '00')),
  4021. '00',
  4022. '/option',
  4023. array('option' => array('value' => '05')),
  4024. '05',
  4025. '/option',
  4026. array('option' => array('value' => '10')),
  4027. '10',
  4028. '/option',
  4029. $minutesRegex,
  4030. '/select',
  4031. );
  4032. $this->assertTags($result, $expected);
  4033. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4034. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4035. $expected = array(
  4036. array('select' => array('name' => 'Model[field][minute]')),
  4037. array('option' => array('value' => '')),
  4038. '/option',
  4039. array('option' => array('value' => '00')),
  4040. '00',
  4041. '/option',
  4042. array('option' => array('value' => '05')),
  4043. '05',
  4044. '/option',
  4045. array('option' => array('value' => '10', 'selected' => 'selected')),
  4046. '10',
  4047. '/option',
  4048. $minutesRegex,
  4049. '/select',
  4050. );
  4051. $this->assertTags($result, $expected);
  4052. }
  4053. /**
  4054. * Test generating an input for the meridian.
  4055. *
  4056. * @return void
  4057. */
  4058. public function testMeridian() {
  4059. extract($this->dateRegex);
  4060. $now = time();
  4061. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4062. $expected = [
  4063. array('select' => array('name' => 'Model[field][meridian]')),
  4064. array('option' => array('value' => '')),
  4065. '/option',
  4066. $meridianRegex,
  4067. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4068. date('a', $now),
  4069. '/option',
  4070. '*/select'
  4071. ];
  4072. $this->assertTags($result, $expected);
  4073. }
  4074. /**
  4075. * testHour method
  4076. *
  4077. * @return void
  4078. */
  4079. public function testHour() {
  4080. extract($this->dateRegex);
  4081. $result = $this->Form->hour('Model.field', ['value' => '']);
  4082. $expected = array(
  4083. array('select' => array('name' => 'Model[field][hour]')),
  4084. array('option' => array('selected' => 'selected', 'value' => '')),
  4085. '/option',
  4086. array('option' => array('value' => '01')),
  4087. '1',
  4088. '/option',
  4089. array('option' => array('value' => '02')),
  4090. '2',
  4091. '/option',
  4092. $hoursRegex,
  4093. '/select',
  4094. );
  4095. $this->assertTags($result, $expected);
  4096. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4097. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4098. $expected = array(
  4099. array('select' => array('name' => 'Model[field][hour]')),
  4100. array('option' => array('value' => '')),
  4101. '/option',
  4102. array('option' => array('value' => '01')),
  4103. '1',
  4104. '/option',
  4105. array('option' => array('value' => '02')),
  4106. '2',
  4107. '/option',
  4108. $hoursRegex,
  4109. array('option' => array('value' => '12', 'selected' => 'selected')),
  4110. '12',
  4111. '/option',
  4112. '/select',
  4113. );
  4114. $this->assertTags($result, $expected);
  4115. $this->Form->request->data['Model']['field'] = '';
  4116. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4117. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4118. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4119. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4120. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4121. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4122. $expected = array(
  4123. array('select' => array('name' => 'Model[field][hour]')),
  4124. array('option' => array('value' => '')),
  4125. '/option',
  4126. array('option' => array('value' => '00', 'selected' => 'selected')),
  4127. '0',
  4128. '/option',
  4129. array('option' => array('value' => '01')),
  4130. '1',
  4131. '/option',
  4132. array('option' => array('value' => '02')),
  4133. '2',
  4134. '/option',
  4135. $hoursRegex,
  4136. '/select',
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. unset($this->Form->request->data['Model']['field']);
  4140. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4141. $thisHour = date('H');
  4142. $optValue = date('G');
  4143. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4144. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4145. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4146. $expected = array(
  4147. array('select' => array('name' => 'Model[field][hour]')),
  4148. array('option' => array('value' => '')),
  4149. '/option',
  4150. array('option' => array('value' => '00')),
  4151. '0',
  4152. '/option',
  4153. array('option' => array('value' => '01', 'selected' => 'selected')),
  4154. '1',
  4155. '/option',
  4156. array('option' => array('value' => '02')),
  4157. '2',
  4158. '/option',
  4159. $hoursRegex,
  4160. '/select',
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. }
  4164. /**
  4165. * testYear method
  4166. *
  4167. * @return void
  4168. */
  4169. public function testYear() {
  4170. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4171. $expected = array(
  4172. array('select' => array('name' => 'Model[field][year]')),
  4173. array('option' => array('selected' => 'selected', 'value' => '')),
  4174. '/option',
  4175. array('option' => array('value' => '2007')),
  4176. '2007',
  4177. '/option',
  4178. array('option' => array('value' => '2006')),
  4179. '2006',
  4180. '/option',
  4181. '/select',
  4182. );
  4183. $this->assertTags($result, $expected);
  4184. $result = $this->Form->year('Model.field', [
  4185. 'value' => '',
  4186. 'minYear' => 2006,
  4187. 'maxYear' => 2007,
  4188. 'orderYear' => 'asc'
  4189. ]);
  4190. $expected = array(
  4191. array('select' => array('name' => 'Model[field][year]')),
  4192. array('option' => array('selected' => 'selected', 'value' => '')),
  4193. '/option',
  4194. array('option' => array('value' => '2006')),
  4195. '2006',
  4196. '/option',
  4197. array('option' => array('value' => '2007')),
  4198. '2007',
  4199. '/option',
  4200. '/select',
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4204. $result = $this->Form->year('Contact.published', [
  4205. 'empty' => false,
  4206. 'minYear' => 2006,
  4207. 'maxYear' => 2007,
  4208. ]);
  4209. $expected = array(
  4210. array('select' => array('name' => 'Contact[published][year]')),
  4211. array('option' => array('value' => '2007')),
  4212. '2007',
  4213. '/option',
  4214. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4215. '2006',
  4216. '/option',
  4217. '/select',
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. }
  4221. /**
  4222. * testYearAutoExpandRange method
  4223. *
  4224. * @return void
  4225. */
  4226. public function testYearAutoExpandRange() {
  4227. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4228. $result = $this->Form->year('User.birthday');
  4229. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4230. $result = $matches[1];
  4231. $expected = range(date('Y') + 5, 1930);
  4232. $this->assertEquals($expected, $result);
  4233. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4234. $result = $this->Form->year('Project.release');
  4235. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4236. $result = $matches[1];
  4237. $expected = range(2050, date('Y') - 5);
  4238. $this->assertEquals($expected, $result);
  4239. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4240. $result = $this->Form->year('Project.release', [
  4241. 'minYear' => 1890,
  4242. 'maxYear' => 1900
  4243. ]);
  4244. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4245. $result = $matches[1];
  4246. $expected = range(1900, 1881);
  4247. $this->assertEquals($expected, $result);
  4248. }
  4249. /**
  4250. * Test that input() accepts the type of date and passes options in.
  4251. *
  4252. * @return void
  4253. */
  4254. public function testInputDate() {
  4255. $this->Form->request->data = array(
  4256. 'month_year' => array('month' => date('m')),
  4257. );
  4258. $this->Form->create($this->article);
  4259. $result = $this->Form->input('month_year', array(
  4260. 'label' => false,
  4261. 'type' => 'date',
  4262. 'minYear' => 2006,
  4263. 'maxYear' => 2008
  4264. ));
  4265. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4266. $this->assertNotContains('value="2008" selected="selected"', $result);
  4267. }
  4268. /**
  4269. * testInputDateMaxYear method
  4270. *
  4271. * Let's say we want to only allow users born from 2006 to 2008 to register
  4272. * This being the first singup page, we still don't have any data
  4273. *
  4274. * @return void
  4275. */
  4276. public function testInputDateMaxYear() {
  4277. $this->Form->request->data = [];
  4278. $this->Form->create($this->article);
  4279. $result = $this->Form->input('birthday', array(
  4280. 'label' => false,
  4281. 'type' => 'date',
  4282. 'minYear' => 2006,
  4283. 'maxYear' => 2008
  4284. ));
  4285. $this->assertContains('value="2008" selected="selected"', $result);
  4286. }
  4287. /**
  4288. * testTextArea method
  4289. *
  4290. * @return void
  4291. */
  4292. public function testTextArea() {
  4293. $this->Form->request->data = array('field' => 'some test data');
  4294. $result = $this->Form->textarea('field');
  4295. $expected = array(
  4296. 'textarea' => array('name' => 'field'),
  4297. 'some test data',
  4298. '/textarea',
  4299. );
  4300. $this->assertTags($result, $expected);
  4301. $result = $this->Form->textarea('user.bio');
  4302. $expected = array(
  4303. 'textarea' => array('name' => 'user[bio]'),
  4304. '/textarea',
  4305. );
  4306. $this->assertTags($result, $expected);
  4307. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4308. $result = $this->Form->textarea('field');
  4309. $expected = array(
  4310. 'textarea' => array('name' => 'field'),
  4311. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4312. '/textarea',
  4313. );
  4314. $this->assertTags($result, $expected);
  4315. $this->Form->request->data = [
  4316. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4317. ];
  4318. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4319. $expected = array(
  4320. 'textarea' => array('name' => 'Model[field]'),
  4321. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4322. '/textarea',
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. $result = $this->Form->textarea('0.OtherModel.field');
  4326. $expected = array(
  4327. 'textarea' => array('name' => '0[OtherModel][field]'),
  4328. '/textarea'
  4329. );
  4330. $this->assertTags($result, $expected);
  4331. }
  4332. /**
  4333. * testTextAreaWithStupidCharacters method
  4334. *
  4335. * test text area with non-ascii characters
  4336. *
  4337. * @return void
  4338. */
  4339. public function testTextAreaWithStupidCharacters() {
  4340. $result = $this->Form->textarea('Post.content', [
  4341. 'value' => "GREAT®",
  4342. 'rows' => '15',
  4343. 'cols' => '75'
  4344. ]);
  4345. $expected = [
  4346. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4347. 'GREAT®',
  4348. '/textarea',
  4349. ];
  4350. $this->assertTags($result, $expected);
  4351. }
  4352. /**
  4353. * testHiddenField method
  4354. *
  4355. * @return void
  4356. */
  4357. public function testHiddenField() {
  4358. $this->article['errors'] = [
  4359. 'field' => true
  4360. ];
  4361. $this->Form->request->data['field'] = 'test';
  4362. $this->Form->create($this->article);
  4363. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4364. $this->assertTags($result, array(
  4365. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4366. );
  4367. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4368. $expected = [
  4369. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4370. ];
  4371. $this->assertTags($result, $expected);
  4372. }
  4373. /**
  4374. * testFileUploadField method
  4375. *
  4376. * @return void
  4377. */
  4378. public function testFileUploadField() {
  4379. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4380. $result = $this->Form->file('Model.upload');
  4381. $this->assertTags($result, $expected);
  4382. $this->Form->request->data['Model']['upload'] = [
  4383. 'name' => '', 'type' => '', 'tmp_name' => '',
  4384. 'error' => 4, 'size' => 0
  4385. ];
  4386. $result = $this->Form->file('Model.upload');
  4387. $this->assertTags($result, $expected);
  4388. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4389. $result = $this->Form->file('Model.upload');
  4390. $this->assertTags($result, $expected);
  4391. }
  4392. /**
  4393. * test File upload input on a model not used in create();
  4394. *
  4395. * @return void
  4396. */
  4397. public function testFileUploadOnOtherModel() {
  4398. $this->Form->create($this->article, array('type' => 'file'));
  4399. $result = $this->Form->file('ValidateProfile.city');
  4400. $expected = array(
  4401. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4402. );
  4403. $this->assertTags($result, $expected);
  4404. }
  4405. /**
  4406. * testButton method
  4407. *
  4408. * @return void
  4409. */
  4410. public function testButton() {
  4411. $result = $this->Form->button('Hi');
  4412. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4413. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4414. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4415. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4416. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4417. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4418. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4419. $result = $this->Form->button('No type', array('type' => false));
  4420. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4421. $result = $this->Form->button('Upload Text', array(
  4422. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4423. 'escape' => false
  4424. ));
  4425. $this->assertNotRegExp('/\&039/', $result);
  4426. }
  4427. /**
  4428. * Test that button() makes unlocked fields by default.
  4429. *
  4430. * @return void
  4431. */
  4432. public function testButtonUnlockedByDefault() {
  4433. $this->Form->request->params['_csrfToken'] = 'secured';
  4434. $this->Form->button('Save', array('name' => 'save'));
  4435. $this->Form->button('Clear');
  4436. $result = $this->Form->unlockField();
  4437. $this->assertEquals(array('save'), $result);
  4438. }
  4439. /**
  4440. * testPostButton method
  4441. *
  4442. * @return void
  4443. */
  4444. public function testPostButton() {
  4445. $result = $this->Form->postButton('Hi', '/controller/action');
  4446. $this->assertTags($result, array(
  4447. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4448. 'div' => array('style' => 'display:none;'),
  4449. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4450. '/div',
  4451. 'button' => array('type' => 'submit'),
  4452. 'Hi',
  4453. '/button',
  4454. '/form'
  4455. ));
  4456. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4457. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4458. }
  4459. /**
  4460. * Test using postButton with N dimensional data.
  4461. *
  4462. * @return void
  4463. */
  4464. public function testPostButtonNestedData() {
  4465. $data = array(
  4466. 'one' => array(
  4467. 'two' => array(
  4468. 3, 4, 5
  4469. )
  4470. )
  4471. );
  4472. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4473. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4474. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4475. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4476. }
  4477. /**
  4478. * Test that postButton adds _Token fields.
  4479. *
  4480. * @return void
  4481. */
  4482. public function testSecurePostButton() {
  4483. $this->Form->request->params['_csrfToken'] = 'testkey';
  4484. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4485. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4486. $expected = array(
  4487. 'form' => array(
  4488. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4489. ),
  4490. array('div' => array('style' => 'display:none;')),
  4491. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4492. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4493. '/div',
  4494. 'button' => array('type' => 'submit'),
  4495. 'Delete',
  4496. '/button',
  4497. array('div' => array('style' => 'display:none;')),
  4498. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4499. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4500. '/div',
  4501. '/form',
  4502. );
  4503. $this->assertTags($result, $expected);
  4504. }
  4505. /**
  4506. * testPostLink method
  4507. *
  4508. * @return void
  4509. */
  4510. public function testPostLink() {
  4511. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4512. $this->assertTags($result, array(
  4513. 'form' => array(
  4514. 'method' => 'post', 'action' => '/posts/delete/1',
  4515. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4516. ),
  4517. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4518. '/form',
  4519. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4520. 'Delete',
  4521. '/a'
  4522. ));
  4523. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4524. $this->assertTags($result, array(
  4525. 'form' => array(
  4526. 'method' => 'post', 'action' => '/posts/delete/1',
  4527. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4528. ),
  4529. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4530. '/form',
  4531. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4532. 'Delete',
  4533. '/a'
  4534. ));
  4535. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  4536. $this->assertTags($result, array(
  4537. 'form' => array(
  4538. 'method' => 'post', 'action' => '/posts/delete/1',
  4539. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4540. ),
  4541. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4542. '/form',
  4543. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4544. 'Delete',
  4545. '/a'
  4546. ));
  4547. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  4548. $this->assertTags($result, array(
  4549. 'form' => array(
  4550. 'method' => 'post', 'action' => '/posts/delete/1',
  4551. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4552. ),
  4553. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4554. '/form',
  4555. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4556. 'Delete',
  4557. '/a'
  4558. ));
  4559. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  4560. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  4561. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  4562. $this->assertTags($result, array(
  4563. 'form' => array(
  4564. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  4565. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4566. ),
  4567. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4568. '/form',
  4569. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4570. 'Delete',
  4571. '/a'
  4572. ));
  4573. $result = $this->Form->postLink(
  4574. '',
  4575. array('controller' => 'items', 'action' => 'delete', 10),
  4576. array('class' => 'btn btn-danger', 'escape' => false),
  4577. 'Confirm thing'
  4578. );
  4579. $this->assertTags($result, array(
  4580. 'form' => array(
  4581. 'method' => 'post', 'action' => '/items/delete/10',
  4582. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4583. ),
  4584. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4585. '/form',
  4586. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4587. '/a'
  4588. ));
  4589. }
  4590. /**
  4591. * Test using postLink with N dimensional data.
  4592. *
  4593. * @return void
  4594. */
  4595. public function testPostLinkNestedData() {
  4596. $data = array(
  4597. 'one' => array(
  4598. 'two' => array(
  4599. 3, 4, 5
  4600. )
  4601. )
  4602. );
  4603. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  4604. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4605. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4606. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4607. }
  4608. /**
  4609. * test creating postLinks after a GET form.
  4610. *
  4611. * @return void
  4612. */
  4613. public function testPostLinkAfterGetForm() {
  4614. $this->Form->request->params['_csrfToken'] = 'testkey';
  4615. $this->Form->request->params['_Token'] = 'val';
  4616. $this->Form->create($this->article, array('type' => 'get'));
  4617. $this->Form->end();
  4618. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4619. $this->assertTags($result, array(
  4620. 'form' => array(
  4621. 'method' => 'post', 'action' => '/posts/delete/1',
  4622. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4623. ),
  4624. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4625. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4626. 'div' => array('style' => 'display:none;'),
  4627. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4628. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4629. '/div',
  4630. '/form',
  4631. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4632. 'Delete',
  4633. '/a'
  4634. ));
  4635. }
  4636. /**
  4637. * Test that postLink adds form tags to view block
  4638. *
  4639. * @return void
  4640. */
  4641. public function testPostLinkFormBuffer() {
  4642. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  4643. $this->assertTags($result, array(
  4644. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4645. 'Delete',
  4646. '/a'
  4647. ));
  4648. $result = $this->View->fetch('postLink');
  4649. $this->assertTags($result, array(
  4650. 'form' => array(
  4651. 'method' => 'post', 'action' => '/posts/delete/1',
  4652. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4653. ),
  4654. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4655. '/form'
  4656. ));
  4657. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  4658. array('inline' => false, 'method' => 'DELETE')
  4659. );
  4660. $this->assertTags($result, array(
  4661. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4662. 'Delete',
  4663. '/a'
  4664. ));
  4665. $result = $this->View->fetch('postLink');
  4666. $this->assertTags($result, array(
  4667. 'form' => array(
  4668. 'method' => 'post', 'action' => '/posts/delete/1',
  4669. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4670. ),
  4671. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4672. '/form',
  4673. array(
  4674. 'form' => array(
  4675. 'method' => 'post', 'action' => '/posts/delete/2',
  4676. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4677. ),
  4678. ),
  4679. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  4680. '/form'
  4681. ));
  4682. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  4683. $this->assertTags($result, array(
  4684. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4685. 'Delete',
  4686. '/a'
  4687. ));
  4688. $result = $this->View->fetch('foobar');
  4689. $this->assertTags($result, array(
  4690. 'form' => array(
  4691. 'method' => 'post', 'action' => '/posts/delete/1',
  4692. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4693. ),
  4694. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4695. '/form'
  4696. ));
  4697. }
  4698. /**
  4699. * testSubmitButton method
  4700. *
  4701. * @return void
  4702. */
  4703. public function testSubmitButton() {
  4704. $result = $this->Form->submit('');
  4705. $expected = array(
  4706. 'div' => array('class' => 'submit'),
  4707. 'input' => array('type' => 'submit', 'value' => ''),
  4708. '/div'
  4709. );
  4710. $this->assertTags($result, $expected);
  4711. $result = $this->Form->submit('Test Submit');
  4712. $expected = array(
  4713. 'div' => array('class' => 'submit'),
  4714. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4715. '/div'
  4716. );
  4717. $this->assertTags($result, $expected);
  4718. $result = $this->Form->submit('Next >');
  4719. $expected = array(
  4720. 'div' => array('class' => 'submit'),
  4721. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  4722. '/div'
  4723. );
  4724. $this->assertTags($result, $expected);
  4725. $result = $this->Form->submit('Next >', array('escape' => false));
  4726. $expected = array(
  4727. 'div' => array('class' => 'submit'),
  4728. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  4729. '/div'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  4733. $expected = array(
  4734. 'div' => array('class' => 'submit'),
  4735. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  4736. '/div'
  4737. );
  4738. $this->assertTags($result, $expected);
  4739. }
  4740. /**
  4741. * test image submit types.
  4742. *
  4743. * @return void
  4744. */
  4745. public function testSubmitImage() {
  4746. $result = $this->Form->submit('http://example.com/cake.power.gif');
  4747. $expected = array(
  4748. 'div' => array('class' => 'submit'),
  4749. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  4750. '/div'
  4751. );
  4752. $this->assertTags($result, $expected);
  4753. $result = $this->Form->submit('/relative/cake.power.gif');
  4754. $expected = array(
  4755. 'div' => array('class' => 'submit'),
  4756. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  4757. '/div'
  4758. );
  4759. $this->assertTags($result, $expected);
  4760. $result = $this->Form->submit('cake.power.gif');
  4761. $expected = array(
  4762. 'div' => array('class' => 'submit'),
  4763. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4764. '/div'
  4765. );
  4766. $this->assertTags($result, $expected);
  4767. $result = $this->Form->submit('Not.an.image');
  4768. $expected = array(
  4769. 'div' => array('class' => 'submit'),
  4770. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  4771. '/div'
  4772. );
  4773. $this->assertTags($result, $expected);
  4774. }
  4775. /**
  4776. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  4777. * be submitted at a time.
  4778. *
  4779. * @return void
  4780. */
  4781. public function testSubmitUnlockedByDefault() {
  4782. $this->Form->request->params['_Token'] = 'secured';
  4783. $this->Form->submit('Go go');
  4784. $this->Form->submit('Save', array('name' => 'save'));
  4785. $result = $this->Form->unlockField();
  4786. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  4787. }
  4788. /**
  4789. * Test submit image with timestamps.
  4790. *
  4791. * @return void
  4792. */
  4793. public function testSubmitImageTimestamp() {
  4794. Configure::write('Asset.timestamp', 'force');
  4795. $result = $this->Form->submit('cake.power.gif');
  4796. $expected = array(
  4797. 'div' => array('class' => 'submit'),
  4798. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  4799. '/div'
  4800. );
  4801. $this->assertTags($result, $expected);
  4802. }
  4803. /**
  4804. * test that datetime() works with GET style forms.
  4805. *
  4806. * @return void
  4807. */
  4808. public function testDateTimeWithGetForms() {
  4809. extract($this->dateRegex);
  4810. $this->Form->create($this->article, array('type' => 'get'));
  4811. $result = $this->Form->datetime('created');
  4812. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  4813. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  4814. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  4815. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  4816. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  4817. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  4818. }
  4819. /**
  4820. * testForMagicInputNonExistingNorValidated method
  4821. *
  4822. * @return void
  4823. */
  4824. public function testForMagicInputNonExistingNorValidated() {
  4825. $result = $this->Form->create($this->article);
  4826. $this->Form->templates(['groupContainer' => '{{content}}']);
  4827. $result = $this->Form->input('non_existing_nor_validated');
  4828. $expected = array(
  4829. 'label' => array('for' => 'non-existing-nor-validated'),
  4830. 'Non Existing Nor Validated',
  4831. '/label',
  4832. 'input' => array(
  4833. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4834. 'id' => 'non-existing-nor-validated'
  4835. )
  4836. );
  4837. $this->assertTags($result, $expected);
  4838. $result = $this->Form->input('non_existing_nor_validated', array(
  4839. 'val' => 'my value'
  4840. ));
  4841. $expected = array(
  4842. 'label' => array('for' => 'non-existing-nor-validated'),
  4843. 'Non Existing Nor Validated',
  4844. '/label',
  4845. 'input' => array(
  4846. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4847. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  4848. )
  4849. );
  4850. $this->assertTags($result, $expected);
  4851. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  4852. $result = $this->Form->input('non_existing_nor_validated');
  4853. $expected = array(
  4854. 'label' => array('for' => 'non-existing-nor-validated'),
  4855. 'Non Existing Nor Validated',
  4856. '/label',
  4857. 'input' => array(
  4858. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  4859. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  4860. )
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. }
  4864. /**
  4865. * testFormMagicInputLabel method
  4866. *
  4867. * @return void
  4868. */
  4869. public function testFormMagicInputLabel() {
  4870. TableRegistry::get('Contacts', [
  4871. 'className' => __NAMESPACE__ . '\ContactsTable'
  4872. ]);
  4873. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  4874. $this->Form->templates(['groupContainer' => '{{content}}']);
  4875. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  4876. $expected = array(
  4877. 'label' => array('for' => 'contacts-name'),
  4878. 'My label',
  4879. '/label',
  4880. 'input' => array(
  4881. 'type' => 'text', 'name' => 'Contacts[name]',
  4882. 'id' => 'contacts-name', 'maxlength' => '255'
  4883. )
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->input('name', array(
  4887. 'label' => array('class' => 'mandatory')
  4888. ));
  4889. $expected = array(
  4890. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  4891. 'Name',
  4892. '/label',
  4893. 'input' => array(
  4894. 'type' => 'text', 'name' => 'name',
  4895. 'id' => 'name', 'maxlength' => '255'
  4896. )
  4897. );
  4898. $this->assertTags($result, $expected);
  4899. $result = $this->Form->input('name', array(
  4900. 'div' => false,
  4901. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  4902. ));
  4903. $expected = array(
  4904. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  4905. 'My label',
  4906. '/label',
  4907. 'input' => array(
  4908. 'type' => 'text', 'name' => 'name',
  4909. 'id' => 'name', 'maxlength' => '255'
  4910. )
  4911. );
  4912. $this->assertTags($result, $expected);
  4913. $result = $this->Form->input('Contact.name', array(
  4914. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  4915. ));
  4916. $expected = array(
  4917. 'label' => array('for' => 'my_id'),
  4918. 'Name',
  4919. '/label',
  4920. 'input' => array(
  4921. 'type' => 'text', 'name' => 'Contact[name]',
  4922. 'id' => 'my_id', 'maxlength' => '255'
  4923. )
  4924. );
  4925. $this->assertTags($result, $expected);
  4926. $result = $this->Form->input('1.id');
  4927. $this->assertTags($result, array('input' => array(
  4928. 'type' => 'hidden', 'name' => '1[id]',
  4929. 'id' => '1-id'
  4930. )));
  4931. $result = $this->Form->input("1.name");
  4932. $expected = array(
  4933. 'label' => array('for' => '1-name'),
  4934. 'Name',
  4935. '/label',
  4936. 'input' => array(
  4937. 'type' => 'text', 'name' => '1[name]',
  4938. 'id' => '1-name', 'maxlength' => '255'
  4939. )
  4940. );
  4941. $this->assertTags($result, $expected);
  4942. }
  4943. /**
  4944. * testFormEnd method
  4945. *
  4946. * @return void
  4947. */
  4948. public function testFormEnd() {
  4949. $this->assertEquals('</form>', $this->Form->end());
  4950. }
  4951. /**
  4952. * Test the generation of fields for a multi record form.
  4953. *
  4954. * @return void
  4955. */
  4956. public function testMultiRecordForm() {
  4957. $this->loadFixtures('Article', 'Comment');
  4958. $articles = TableRegistry::get('Articles');
  4959. $articles->hasMany('Comments');
  4960. $comment = new Entity(['comment' => 'Value']);
  4961. $article = new Article(['comments' => [$comment]]);
  4962. $this->Form->create([$article]);
  4963. $result = $this->Form->input('0.comments.1.comment');
  4964. $expected = array(
  4965. 'div' => array('class' => 'input textarea'),
  4966. 'label' => array('for' => '0-comments-1-comment'),
  4967. 'Comment',
  4968. '/label',
  4969. 'textarea' => array(
  4970. 'name',
  4971. 'id' => '0-comments-1-comment'
  4972. ),
  4973. '/textarea',
  4974. '/div'
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. $result = $this->Form->input('0.comments.0.comment');
  4978. $expected = array(
  4979. 'div' => array('class' => 'input textarea'),
  4980. 'label' => array('for' => '0-comments-0-comment'),
  4981. 'Comment',
  4982. '/label',
  4983. 'textarea' => array(
  4984. 'name',
  4985. 'id' => '0-comments-0-comment'
  4986. ),
  4987. 'Value',
  4988. '/textarea',
  4989. '/div'
  4990. );
  4991. $this->assertTags($result, $expected);
  4992. $comment->errors('comment', ['Not valid']);
  4993. $result = $this->Form->input('0.comments.0.comment');
  4994. $expected = array(
  4995. 'div' => array('class' => 'input textarea error'),
  4996. 'label' => array('for' => '0-comments-0-comment'),
  4997. 'Comment',
  4998. '/label',
  4999. 'textarea' => array(
  5000. 'name',
  5001. 'class' => 'form-error',
  5002. 'id' => '0-comments-0-comment'
  5003. ),
  5004. 'Value',
  5005. '/textarea',
  5006. array('div' => array('class' => 'error-message')),
  5007. 'Not valid',
  5008. '/div',
  5009. '/div'
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. TableRegistry::get('Comments')
  5013. ->validator('default')
  5014. ->allowEmpty('comment', false);
  5015. $result = $this->Form->input('0.comments.1.comment');
  5016. $expected = array(
  5017. 'div' => array('class' => 'input textarea required'),
  5018. 'label' => array('for' => '0-comments-1-comment'),
  5019. 'Comment',
  5020. '/label',
  5021. 'textarea' => array(
  5022. 'name',
  5023. 'id' => '0-comments-1-comment'
  5024. ),
  5025. '/textarea',
  5026. '/div'
  5027. );
  5028. $this->assertTags($result, $expected);
  5029. }
  5030. /**
  5031. * test that some html5 inputs + FormHelper::__call() work
  5032. *
  5033. * @return void
  5034. */
  5035. public function testHtml5Inputs() {
  5036. $result = $this->Form->email('User.email');
  5037. $expected = array(
  5038. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $result = $this->Form->search('User.query');
  5042. $expected = array(
  5043. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. $result = $this->Form->search('User.query', array('value' => 'test'));
  5047. $expected = array(
  5048. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5049. );
  5050. $this->assertTags($result, $expected);
  5051. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5052. $expected = array(
  5053. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5054. );
  5055. $this->assertTags($result, $expected);
  5056. }
  5057. /**
  5058. * Test accessing html5 inputs through input().
  5059. *
  5060. * @return void
  5061. */
  5062. public function testHtml5InputWithInput() {
  5063. $this->Form->create();
  5064. $this->Form->templates(['groupContainer' => '{{content}}']);
  5065. $result = $this->Form->input('website', array(
  5066. 'type' => 'url',
  5067. 'val' => 'http://domain.tld',
  5068. 'label' => false
  5069. ));
  5070. $expected = array(
  5071. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. }
  5075. /**
  5076. * Test errors when field name is missing.
  5077. *
  5078. * @expectedException \Cake\Error\Exception
  5079. * @return void
  5080. */
  5081. public function testHtml5InputException() {
  5082. $this->Form->email();
  5083. }
  5084. /**
  5085. * Tests that formhelper sets required attributes.
  5086. *
  5087. * @return void
  5088. */
  5089. public function testRequiredAttribute() {
  5090. $this->article['required'] = [
  5091. 'title' => true,
  5092. 'body' => false,
  5093. ];
  5094. $this->Form->create($this->article);
  5095. $result = $this->Form->input('title');
  5096. $expected = array(
  5097. 'div' => array('class' => 'input text required'),
  5098. 'label' => array('for' => 'title'),
  5099. 'Title',
  5100. '/label',
  5101. 'input' => array(
  5102. 'type' => 'text',
  5103. 'name' => 'title',
  5104. 'id' => 'title',
  5105. 'required' => 'required',
  5106. ),
  5107. '/div'
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. $result = $this->Form->input('title', ['required' => false]);
  5111. $this->assertNotContains('required', $result);
  5112. $result = $this->Form->input('body');
  5113. $expected = array(
  5114. 'div' => array('class' => 'input text'),
  5115. 'label' => array('for' => 'body'),
  5116. 'Body',
  5117. '/label',
  5118. 'input' => array(
  5119. 'type' => 'text',
  5120. 'name' => 'body',
  5121. 'id' => 'body',
  5122. ),
  5123. '/div'
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. $result = $this->Form->input('body', ['required' => true]);
  5127. $this->assertContains('required', $result);
  5128. }
  5129. /**
  5130. * Tests that it is possible to nest inputs inside labels
  5131. *
  5132. * @return void
  5133. */
  5134. public function testNestInputInLabel() {
  5135. $this->Form->templates([
  5136. 'label' => '<label{{attrs}}>{{text}}{{input}}</label>',
  5137. 'formGroup' => '{{label}}'
  5138. ]);
  5139. $result = $this->Form->input('foo');
  5140. $expected = array(
  5141. 'div' => array('class' => 'input text'),
  5142. 'label' => array('for' => 'foo'),
  5143. 'Foo',
  5144. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  5145. '/label',
  5146. '/div'
  5147. );
  5148. $this->assertTags($result, $expected);
  5149. }
  5150. }