FormHelperTest.php 177 KB

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