FormHelperTest.php 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174
  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 that input() accepts a template file.
  1918. *
  1919. * @return void
  1920. */
  1921. public function testInputWithTemplateFile() {
  1922. $result = $this->Form->input('field', array(
  1923. 'templates' => 'htmlhelper_tags'
  1924. ));
  1925. $expected = array(
  1926. 'label' => array('for' => 'field'),
  1927. 'Field',
  1928. '/label',
  1929. 'input' => array(
  1930. 'type' => 'text', 'name' => 'field',
  1931. 'id' => 'field'
  1932. ),
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test id prefix
  1938. *
  1939. * @return void
  1940. */
  1941. public function testCreateIdPrefix() {
  1942. $this->Form->create(false, ['idPrefix' => 'prefix']);
  1943. $result = $this->Form->input('field');
  1944. $expected = [
  1945. 'div' => ['class' => 'input text'],
  1946. 'label' => ['for' => 'prefix-field'],
  1947. 'Field',
  1948. '/label',
  1949. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  1950. '/div'
  1951. ];
  1952. $this->assertTags($result, $expected);
  1953. $result = $this->Form->input('field', ['id' => 'custom-id']);
  1954. $expected = [
  1955. 'div' => ['class' => 'input text'],
  1956. 'label' => ['for' => 'custom-id'],
  1957. 'Field',
  1958. '/label',
  1959. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  1960. '/div'
  1961. ];
  1962. $this->assertTags($result, $expected);
  1963. $result = $this->Form->radio('Model.field', ['option A']);
  1964. $expected = [
  1965. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  1966. ['input' => [
  1967. 'type' => 'radio',
  1968. 'name' => 'Model[field]',
  1969. 'value' => '0',
  1970. 'id' => 'prefix-model-field-0'
  1971. ]],
  1972. 'label' => ['for' => 'prefix-model-field-0'],
  1973. 'option A',
  1974. '/label'
  1975. ];
  1976. $this->assertTags($result, $expected);
  1977. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  1978. $expected = [
  1979. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  1980. ['input' => [
  1981. 'type' => 'radio',
  1982. 'name' => 'Model[field]',
  1983. 'value' => '0',
  1984. 'id' => 'prefix-model-field-0'
  1985. ]],
  1986. 'label' => ['for' => 'prefix-model-field-0'],
  1987. 'option A',
  1988. '/label'
  1989. ];
  1990. $this->assertTags($result, $expected);
  1991. $result = $this->Form->select(
  1992. 'Model.multi_field',
  1993. ['first'],
  1994. ['multiple' => 'checkbox']
  1995. );
  1996. $expected = [
  1997. 'input' => [
  1998. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  1999. ],
  2000. ['div' => ['class' => 'checkbox']],
  2001. ['input' => [
  2002. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2003. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2004. ]],
  2005. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2006. 'first',
  2007. '/label',
  2008. '/div',
  2009. ];
  2010. $this->assertTags($result, $expected);
  2011. $this->Form->end();
  2012. $result = $this->Form->input('field');
  2013. $expected = [
  2014. 'div' => ['class' => 'input text'],
  2015. 'label' => ['for' => 'field'],
  2016. 'Field',
  2017. '/label',
  2018. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2019. '/div'
  2020. ];
  2021. $this->assertTags($result, $expected);
  2022. }
  2023. /**
  2024. * Test that inputs with 0 can be created.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testInputZero() {
  2029. TableRegistry::get('Contacts', [
  2030. 'className' => __NAMESPACE__ . '\ContactsTable'
  2031. ]);
  2032. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2033. $result = $this->Form->input('0');
  2034. $expected = array(
  2035. 'div' => array('class' => 'input text'),
  2036. 'label' => array('for' => '0'), '/label',
  2037. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2038. '/div'
  2039. );
  2040. $this->assertTags($result, $expected);
  2041. }
  2042. /**
  2043. * test input() with checkbox creation
  2044. *
  2045. * @return void
  2046. */
  2047. public function testInputCheckbox() {
  2048. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2049. $expected = array(
  2050. 'div' => array('class' => 'input checkbox'),
  2051. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2052. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2053. '/div'
  2054. );
  2055. $this->assertTags($result, $expected);
  2056. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2057. $expected = array(
  2058. 'div' => array('class' => 'input checkbox'),
  2059. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2060. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2061. '/div'
  2062. );
  2063. $this->assertTags($result, $expected);
  2064. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2065. $expected = array(
  2066. 'div' => array('class' => 'input checkbox'),
  2067. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2068. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2069. '/div'
  2070. );
  2071. $this->assertTags($result, $expected);
  2072. $result = $this->Form->input('User.disabled', array(
  2073. 'label' => 'Disabled',
  2074. 'type' => 'checkbox',
  2075. 'data-foo' => 'disabled'
  2076. ));
  2077. $expected = array(
  2078. 'div' => array('class' => 'input checkbox'),
  2079. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2080. array('input' => array(
  2081. 'type' => 'checkbox',
  2082. 'name' => 'User[disabled]',
  2083. 'value' => '1',
  2084. 'id' => 'user-disabled',
  2085. 'data-foo' => 'disabled'
  2086. )),
  2087. 'label' => array('for' => 'user-disabled'),
  2088. 'Disabled',
  2089. '/label',
  2090. '/div'
  2091. );
  2092. $this->assertTags($result, $expected);
  2093. }
  2094. /**
  2095. * Test that input() does not create wrapping div and label tag for hidden fields
  2096. *
  2097. * @return void
  2098. */
  2099. public function testInputHidden() {
  2100. TableRegistry::get('ValidateUsers', [
  2101. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2102. ]);
  2103. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2104. $result = $this->Form->input('ValidateUser.id');
  2105. $expected = array(
  2106. 'input' => array('name', 'type' => 'hidden', 'id')
  2107. );
  2108. $this->assertTags($result, $expected);
  2109. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2110. $expected = array(
  2111. 'input' => array('name', 'type' => 'hidden', 'id')
  2112. );
  2113. $this->assertTags($result, $expected);
  2114. }
  2115. /**
  2116. * test form->input() with datetime
  2117. *
  2118. * @return void
  2119. */
  2120. public function testInputDatetime() {
  2121. $this->Form = $this->getMock(
  2122. 'Cake\View\Helper\FormHelper',
  2123. ['datetime'],
  2124. [new View()]
  2125. );
  2126. $this->Form->expects($this->once())->method('datetime')
  2127. ->with('prueba', [
  2128. 'type' => 'datetime',
  2129. 'timeFormat' => 24,
  2130. 'minYear' => 2008,
  2131. 'maxYear' => 2011,
  2132. 'interval' => 15,
  2133. 'options' => null,
  2134. 'empty' => false,
  2135. 'id' => 'prueba',
  2136. 'required' => false,
  2137. ])
  2138. ->will($this->returnValue('This is it!'));
  2139. $result = $this->Form->input('prueba', array(
  2140. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2141. 'maxYear' => 2011, 'interval' => 15
  2142. ));
  2143. $expected = array(
  2144. 'div' => array('class' => 'input datetime'),
  2145. 'label' => array('for' => 'prueba'),
  2146. 'Prueba',
  2147. '/label',
  2148. 'This is it!',
  2149. '/div'
  2150. );
  2151. $this->assertTags($result, $expected);
  2152. }
  2153. /**
  2154. * test form->input() with datetime with id prefix
  2155. *
  2156. * @return void
  2157. */
  2158. public function testInputDatetimeIdPrefix() {
  2159. $this->Form = $this->getMock(
  2160. 'Cake\View\Helper\FormHelper',
  2161. ['datetime'],
  2162. [new View()]
  2163. );
  2164. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2165. $this->Form->expects($this->once())->method('datetime')
  2166. ->with('prueba', [
  2167. 'type' => 'datetime',
  2168. 'timeFormat' => 24,
  2169. 'minYear' => 2008,
  2170. 'maxYear' => 2011,
  2171. 'interval' => 15,
  2172. 'options' => null,
  2173. 'empty' => false,
  2174. 'id' => 'prefix-prueba',
  2175. 'required' => false,
  2176. ])
  2177. ->will($this->returnValue('This is it!'));
  2178. $result = $this->Form->input('prueba', array(
  2179. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2180. 'maxYear' => 2011, 'interval' => 15
  2181. ));
  2182. $expected = array(
  2183. 'div' => array('class' => 'input datetime'),
  2184. 'label' => array('for' => 'prefix-prueba'),
  2185. 'Prueba',
  2186. '/label',
  2187. 'This is it!',
  2188. '/div'
  2189. );
  2190. $this->assertTags($result, $expected);
  2191. }
  2192. /**
  2193. * Test generating checkboxes with disabled elements.
  2194. *
  2195. * @return void
  2196. */
  2197. public function testInputCheckboxWithDisabledElements() {
  2198. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2199. $result = $this->Form->input('Contact.multiple', array(
  2200. 'multiple' => 'checkbox',
  2201. 'disabled' => 'disabled',
  2202. 'options' => $options
  2203. ));
  2204. $expected = array(
  2205. array('div' => array('class' => 'input select')),
  2206. array('label' => array('for' => "contact-multiple")),
  2207. 'Multiple',
  2208. '/label',
  2209. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2210. array('div' => array('class' => 'checkbox')),
  2211. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  2212. array('label' => array('for' => "contact-multiple-1")),
  2213. 'One',
  2214. '/label',
  2215. '/div',
  2216. array('div' => array('class' => 'checkbox')),
  2217. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  2218. array('label' => array('for' => "contact-multiple-2")),
  2219. 'Two',
  2220. '/label',
  2221. '/div',
  2222. array('div' => array('class' => 'checkbox')),
  2223. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  2224. array('label' => array('for' => "contact-multiple-3")),
  2225. 'Three',
  2226. '/label',
  2227. '/div',
  2228. '/div'
  2229. );
  2230. $this->assertTags($result, $expected);
  2231. // make sure 50 does only disable 50, and not 50f5c0cf
  2232. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2233. $disabled = array(50);
  2234. $expected = array(
  2235. array('div' => array('class' => 'input select')),
  2236. array('label' => array('for' => "contact-multiple")),
  2237. 'Multiple',
  2238. '/label',
  2239. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2240. array('div' => array('class' => 'checkbox')),
  2241. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  2242. array('label' => array('for' => "contact-multiple-50")),
  2243. 'Fifty',
  2244. '/label',
  2245. '/div',
  2246. array('div' => array('class' => 'checkbox')),
  2247. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  2248. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2249. 'Stringy',
  2250. '/label',
  2251. '/div',
  2252. '/div'
  2253. );
  2254. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2255. $this->assertTags($result, $expected);
  2256. }
  2257. /**
  2258. * test input name with leading integer, ensure attributes are generated correctly.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputWithLeadingInteger() {
  2263. $result = $this->Form->text('0.Node.title');
  2264. $expected = array(
  2265. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2266. );
  2267. $this->assertTags($result, $expected);
  2268. }
  2269. /**
  2270. * test form->input() with select type inputs.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testInputSelectType() {
  2275. $result = $this->Form->input('email', array(
  2276. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2277. );
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'email'),
  2281. 'Email',
  2282. '/label',
  2283. array('select' => array('name' => 'email', 'id' => 'email')),
  2284. array('option' => array('value' => '')),
  2285. '/option',
  2286. array('option' => array('value' => 'è')),
  2287. 'Firést',
  2288. '/option',
  2289. array('option' => array('value' => 'é')),
  2290. 'Secoènd',
  2291. '/option',
  2292. '/select',
  2293. '/div'
  2294. );
  2295. $this->assertTags($result, $expected);
  2296. $result = $this->Form->input('email', array(
  2297. 'options' => array('First', 'Second'), 'empty' => true)
  2298. );
  2299. $expected = array(
  2300. 'div' => array('class' => 'input select'),
  2301. 'label' => array('for' => 'email'),
  2302. 'Email',
  2303. '/label',
  2304. array('select' => array('name' => 'email', 'id' => 'email')),
  2305. array('option' => array('value' => '')),
  2306. '/option',
  2307. array('option' => array('value' => '0')),
  2308. 'First',
  2309. '/option',
  2310. array('option' => array('value' => '1')),
  2311. 'Second',
  2312. '/option',
  2313. '/select',
  2314. '/div'
  2315. );
  2316. $this->assertTags($result, $expected);
  2317. $result = $this->Form->input('email', [
  2318. 'type' => 'select',
  2319. 'options' => new \ArrayObject(['First', 'Second']),
  2320. 'empty' => true
  2321. ]);
  2322. $this->assertTags($result, $expected);
  2323. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2324. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2325. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2326. $expected = array(
  2327. 'div' => array('class' => 'input select'),
  2328. 'label' => array('for' => 'model-user-id'),
  2329. 'User',
  2330. '/label',
  2331. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2332. array('option' => array('value' => '')),
  2333. '/option',
  2334. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2335. 'good',
  2336. '/option',
  2337. array('option' => array('value' => 'other')),
  2338. 'bad',
  2339. '/option',
  2340. '/select',
  2341. '/div'
  2342. );
  2343. $this->assertTags($result, $expected);
  2344. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2345. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2346. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2347. $expected = array(
  2348. 'div' => array('class' => 'input select'),
  2349. 'label' => array('for' => 'thing-user-id'),
  2350. 'User',
  2351. '/label',
  2352. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2353. array('option' => array('value' => '')),
  2354. 'Some Empty',
  2355. '/option',
  2356. array('option' => array('value' => 'value')),
  2357. 'good',
  2358. '/option',
  2359. array('option' => array('value' => 'other')),
  2360. 'bad',
  2361. '/option',
  2362. '/select',
  2363. '/div'
  2364. );
  2365. $this->assertTags($result, $expected);
  2366. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2367. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2368. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2369. $expected = array(
  2370. 'div' => array('class' => 'input select'),
  2371. 'label' => array('for' => 'thing-user-id'),
  2372. 'User',
  2373. '/label',
  2374. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2375. array('option' => array('value' => '')),
  2376. 'Some Empty',
  2377. '/option',
  2378. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2379. 'good',
  2380. '/option',
  2381. array('option' => array('value' => 'other')),
  2382. 'bad',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $this->Form->data = array();
  2389. $result = $this->Form->input('Publisher.id', array(
  2390. 'label' => 'Publisher',
  2391. 'type' => 'select',
  2392. 'multiple' => 'checkbox',
  2393. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2394. ));
  2395. $expected = array(
  2396. array('div' => array('class' => 'input select')),
  2397. array('label' => array('for' => 'publisher-id')),
  2398. 'Publisher',
  2399. '/label',
  2400. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2401. array('div' => array('class' => 'checkbox')),
  2402. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2403. array('label' => array('for' => 'publisher-id-value-1')),
  2404. 'Label 1',
  2405. '/label',
  2406. '/div',
  2407. array('div' => array('class' => 'checkbox')),
  2408. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2409. array('label' => array('for' => 'publisher-id-value-2')),
  2410. 'Label 2',
  2411. '/label',
  2412. '/div',
  2413. '/div'
  2414. );
  2415. $this->assertTags($result, $expected);
  2416. }
  2417. /**
  2418. * test that input() and a non standard primary key makes a hidden input by default.
  2419. *
  2420. * @return void
  2421. */
  2422. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2423. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2424. $this->Form->create($this->article);
  2425. $result = $this->Form->input('title');
  2426. $expected = array(
  2427. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2431. $this->Form->create($this->article);
  2432. $result = $this->Form->input('title');
  2433. $expected = array(
  2434. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2435. );
  2436. $this->assertTags($result, $expected);
  2437. $result = $this->Form->input('body');
  2438. $expected = array(
  2439. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2440. );
  2441. $this->assertTags($result, $expected);
  2442. }
  2443. /**
  2444. * test that overriding the magic select type widget is possible
  2445. *
  2446. * @return void
  2447. */
  2448. public function testInputOverridingMagicSelectType() {
  2449. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2450. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2451. $expected = array(
  2452. 'div' => array('class' => 'input text'),
  2453. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2454. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2455. '/div'
  2456. );
  2457. $this->assertTags($result, $expected);
  2458. //Check that magic types still work for plural/singular vars
  2459. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2460. $result = $this->Form->input('Model.type');
  2461. $expected = array(
  2462. 'div' => array('class' => 'input select'),
  2463. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2464. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2465. array('option' => array('value' => 'value')), 'good', '/option',
  2466. array('option' => array('value' => 'other')), 'bad', '/option',
  2467. '/select',
  2468. '/div'
  2469. );
  2470. $this->assertTags($result, $expected);
  2471. }
  2472. /**
  2473. * Test that inferred types do not override developer input
  2474. *
  2475. * @return void
  2476. */
  2477. public function testInputMagicTypeDoesNotOverride() {
  2478. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2479. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2480. $expected = array(
  2481. 'div' => array('class' => 'input checkbox'),
  2482. array('input' => array(
  2483. 'type' => 'hidden',
  2484. 'name' => 'Model[user]',
  2485. 'value' => 0,
  2486. )),
  2487. array('input' => array(
  2488. 'name' => 'Model[user]',
  2489. 'type' => 'checkbox',
  2490. 'id' => 'model-user',
  2491. 'value' => 1
  2492. )),
  2493. 'label' => array('for' => 'model-user'), 'User', '/label',
  2494. '/div'
  2495. );
  2496. $this->assertTags($result, $expected);
  2497. }
  2498. /**
  2499. * Test that magic input() selects are created for type=number
  2500. *
  2501. * @return void
  2502. */
  2503. public function testInputMagicSelectForTypeNumber() {
  2504. TableRegistry::get('ValidateUsers', [
  2505. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2506. ]);
  2507. $entity = new Entity(['balance' => 1]);
  2508. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2509. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2510. $result = $this->Form->input('balance');
  2511. $expected = array(
  2512. 'div' => array('class' => 'input select'),
  2513. 'label' => array('for' => 'balance'),
  2514. 'Balance',
  2515. '/label',
  2516. 'select' => array('name' => 'balance', 'id' => 'balance'),
  2517. array('option' => array('value' => '0')),
  2518. 'nothing',
  2519. '/option',
  2520. array('option' => array('value' => '1', 'selected' => 'selected')),
  2521. 'some',
  2522. '/option',
  2523. array('option' => array('value' => '100')),
  2524. 'a lot',
  2525. '/option',
  2526. '/select',
  2527. '/div'
  2528. );
  2529. $this->assertTags($result, $expected);
  2530. }
  2531. /**
  2532. * Test that magic input() selects can easily be converted into radio types without error.
  2533. *
  2534. * @return void
  2535. */
  2536. public function testInputMagicSelectChangeToRadio() {
  2537. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2538. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2539. $this->assertContains('input type="radio"', $result);
  2540. }
  2541. /**
  2542. * testFormInputs method
  2543. *
  2544. * test correct results from form::inputs().
  2545. *
  2546. * @return void
  2547. */
  2548. public function testFormInputsLegendFieldset() {
  2549. $this->Form->create($this->article);
  2550. $result = $this->Form->allInputs([], array('legend' => 'The Legend'));
  2551. $expected = array(
  2552. '<fieldset',
  2553. '<legend',
  2554. 'The Legend',
  2555. '/legend',
  2556. '*/fieldset',
  2557. );
  2558. $this->assertTags($result, $expected);
  2559. $result = $this->Form->allInputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
  2560. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2561. $this->assertContains('<fieldset>', $result);
  2562. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => false));
  2563. $this->assertNotContains('<legend>', $result);
  2564. $this->assertNotContains('<fieldset>', $result);
  2565. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => 'Hello'));
  2566. $this->assertNotContains('<legend>', $result);
  2567. $this->assertNotContains('<fieldset>', $result);
  2568. $this->Form->create($this->article);
  2569. $this->Form->request->params['prefix'] = 'admin';
  2570. $this->Form->request->params['action'] = 'admin_edit';
  2571. $this->Form->request->params['controller'] = 'articles';
  2572. $result = $this->Form->allInputs();
  2573. $expected = [
  2574. '<fieldset',
  2575. '<legend',
  2576. 'New Article',
  2577. '/legend',
  2578. '*/fieldset',
  2579. ];
  2580. $this->assertTags($result, $expected);
  2581. }
  2582. /**
  2583. * Test the inputs() method.
  2584. *
  2585. * @return void
  2586. */
  2587. public function testFormInputs() {
  2588. $this->Form->create($this->article);
  2589. $result = $this->Form->allInputs();
  2590. $expected = array(
  2591. '<fieldset',
  2592. '<legend', 'New Article', '/legend',
  2593. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2594. array('div' => array('class' => 'input select required')),
  2595. '*/div',
  2596. array('div' => array('class' => 'input text required')),
  2597. '*/div',
  2598. array('div' => array('class' => 'input text')),
  2599. '*/div',
  2600. array('div' => array('class' => 'input text')),
  2601. '*/div',
  2602. '/fieldset',
  2603. );
  2604. $this->assertTags($result, $expected);
  2605. $result = $this->Form->allInputs([
  2606. 'published' => ['type' => 'boolean']
  2607. ]);
  2608. $expected = array(
  2609. '<fieldset',
  2610. '<legend', 'New Article', '/legend',
  2611. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2612. array('div' => array('class' => 'input select required')),
  2613. '*/div',
  2614. array('div' => array('class' => 'input text required')),
  2615. '*/div',
  2616. array('div' => array('class' => 'input text')),
  2617. '*/div',
  2618. array('div' => array('class' => 'input boolean')),
  2619. '*/div',
  2620. '/fieldset',
  2621. );
  2622. $this->assertTags($result, $expected);
  2623. $this->Form->create($this->article);
  2624. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  2625. $expected = array(
  2626. 'fieldset' => array(),
  2627. 'legend' => array(),
  2628. 'Hello',
  2629. '/legend',
  2630. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2631. array('div' => array('class' => 'input select required')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input text required')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input text')),
  2636. '*/div',
  2637. array('div' => array('class' => 'input text')),
  2638. '*/div',
  2639. '/fieldset'
  2640. );
  2641. $this->assertTags($result, $expected);
  2642. $this->Form->create(false);
  2643. $expected = array(
  2644. 'fieldset' => array(),
  2645. array('div' => array('class' => 'input text')),
  2646. 'label' => array('for' => 'foo'),
  2647. 'Foo',
  2648. '/label',
  2649. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2650. '*/div',
  2651. '/fieldset'
  2652. );
  2653. $result = $this->Form->allInputs(
  2654. array('foo' => array('type' => 'text')),
  2655. array('legend' => false)
  2656. );
  2657. $this->assertTags($result, $expected);
  2658. }
  2659. /**
  2660. * testFormInputsBlacklist
  2661. *
  2662. * @return void
  2663. */
  2664. public function testFormInputsBlacklist() {
  2665. $this->Form->create($this->article);
  2666. $result = $this->Form->allInputs([
  2667. 'id' => false
  2668. ]);
  2669. $expected = array(
  2670. '<fieldset',
  2671. '<legend', 'New Article', '/legend',
  2672. array('div' => array('class' => 'input select required')),
  2673. '*/div',
  2674. array('div' => array('class' => 'input text required')),
  2675. '*/div',
  2676. array('div' => array('class' => 'input text')),
  2677. '*/div',
  2678. array('div' => array('class' => 'input text')),
  2679. '*/div',
  2680. '/fieldset',
  2681. );
  2682. $this->assertTags($result, $expected);
  2683. $this->Form->create($this->article);
  2684. $result = $this->Form->allInputs([
  2685. 'id' => []
  2686. ]);
  2687. $expected = array(
  2688. '<fieldset',
  2689. '<legend', 'New Article', '/legend',
  2690. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2691. array('div' => array('class' => 'input select required')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input text required')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input text')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input text')),
  2698. '*/div',
  2699. '/fieldset',
  2700. );
  2701. $this->assertTags($result, $expected, 'A falsey value (array) should not remove the input');
  2702. }
  2703. /**
  2704. * testSelectAsCheckbox method
  2705. *
  2706. * test multi-select widget with checkbox formatting.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testSelectAsCheckbox() {
  2711. $result = $this->Form->select(
  2712. 'Model.multi_field',
  2713. array('first', 'second', 'third'),
  2714. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2715. );
  2716. $expected = array(
  2717. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2718. array('div' => array('class' => 'checkbox')),
  2719. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2720. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2721. 'first',
  2722. '/label',
  2723. '/div',
  2724. array('div' => array('class' => 'checkbox')),
  2725. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2726. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2727. 'second',
  2728. '/label',
  2729. '/div',
  2730. array('div' => array('class' => 'checkbox')),
  2731. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2732. array('label' => array('for' => 'model-multi-field-2')),
  2733. 'third',
  2734. '/label',
  2735. '/div',
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. $result = $this->Form->select(
  2739. 'Model.multi_field',
  2740. array('1/2' => 'half'),
  2741. array('multiple' => 'checkbox')
  2742. );
  2743. $expected = array(
  2744. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2745. array('div' => array('class' => 'checkbox')),
  2746. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2747. array('label' => array('for' => 'model-multi-field-1-2')),
  2748. 'half',
  2749. '/label',
  2750. '/div',
  2751. );
  2752. $this->assertTags($result, $expected);
  2753. }
  2754. /**
  2755. * testLabel method
  2756. *
  2757. * test label generation.
  2758. *
  2759. * @return void
  2760. */
  2761. public function testLabel() {
  2762. $result = $this->Form->label('Person.name');
  2763. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2764. $result = $this->Form->label('Person.name');
  2765. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2766. $result = $this->Form->label('Person.first_name');
  2767. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2768. $result = $this->Form->label('Person.first_name', 'Your first name');
  2769. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2770. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2771. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2772. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2773. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2774. $result = $this->Form->label('Person.first_name', '');
  2775. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2776. $result = $this->Form->label('Person.2.name', '');
  2777. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2778. }
  2779. /**
  2780. * testTextbox method
  2781. *
  2782. * test textbox element generation
  2783. *
  2784. * @return void
  2785. */
  2786. public function testTextbox() {
  2787. $result = $this->Form->text('Model.field');
  2788. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2789. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2790. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2791. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2792. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2793. }
  2794. /**
  2795. * Test that text() hooks up with request data and error fields.
  2796. *
  2797. * @return void
  2798. */
  2799. public function testTextBoxDataAndError() {
  2800. $this->article['errors'] = [
  2801. 'Contact' => ['text' => 'wrong']
  2802. ];
  2803. $this->Form->create($this->article);
  2804. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2805. $result = $this->Form->text('Model.text');
  2806. $expected = [
  2807. 'input' => [
  2808. 'type' => 'text',
  2809. 'name' => 'Model[text]',
  2810. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2811. ]
  2812. ];
  2813. $this->assertTags($result, $expected);
  2814. $this->Form->request->data['Contact']['text'] = 'test';
  2815. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2816. $expected = [
  2817. 'input' => [
  2818. 'type' => 'text',
  2819. 'name' => 'Contact[text]',
  2820. 'value' => 'test',
  2821. 'id' => 'theID',
  2822. 'class' => 'form-error'
  2823. ]
  2824. ];
  2825. $this->assertTags($result, $expected);
  2826. }
  2827. /**
  2828. * testDefaultValue method
  2829. *
  2830. * Test default value setting
  2831. *
  2832. * @return void
  2833. */
  2834. public function testTextDefaultValue() {
  2835. $this->Form->request->data['Model']['field'] = 'test';
  2836. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2837. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2838. $this->assertTags($result, $expected);
  2839. unset($this->Form->request->data['Model']['field']);
  2840. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2841. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2842. $this->assertTags($result, $expected);
  2843. }
  2844. /**
  2845. * testError method
  2846. *
  2847. * Test field error generation
  2848. *
  2849. * @return void
  2850. */
  2851. public function testError() {
  2852. $this->article['errors'] = [
  2853. 'Article' => ['field' => 'email']
  2854. ];
  2855. $this->Form->create($this->article);
  2856. $result = $this->Form->error('Article.field');
  2857. $expected = [
  2858. ['div' => ['class' => 'error-message']],
  2859. 'email',
  2860. '/div',
  2861. ];
  2862. $this->assertTags($result, $expected);
  2863. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2864. $expected = [
  2865. ['div' => ['class' => 'error-message']],
  2866. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2867. '/div',
  2868. ];
  2869. $this->assertTags($result, $expected);
  2870. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  2871. $expected = [
  2872. ['div' => ['class' => 'error-message']],
  2873. '<strong', 'Badness!', '/strong',
  2874. '/div',
  2875. ];
  2876. $this->assertTags($result, $expected);
  2877. }
  2878. /**
  2879. * Test error with nested lists.
  2880. *
  2881. * @return void
  2882. */
  2883. public function testErrorMessages() {
  2884. $this->article['errors'] = [
  2885. 'Article' => ['field' => 'email']
  2886. ];
  2887. $this->Form->create($this->article);
  2888. $result = $this->Form->error('Article.field', array(
  2889. 'email' => 'No good!'
  2890. ));
  2891. $expected = array(
  2892. 'div' => array('class' => 'error-message'),
  2893. 'No good!',
  2894. '/div'
  2895. );
  2896. $this->assertTags($result, $expected);
  2897. }
  2898. /**
  2899. * test error() with multiple messages.
  2900. *
  2901. * @return void
  2902. */
  2903. public function testErrorMultipleMessages() {
  2904. $this->article['errors'] = [
  2905. 'field' => ['notEmpty', 'email', 'Something else']
  2906. ];
  2907. $this->Form->create($this->article);
  2908. $result = $this->Form->error('field', array(
  2909. 'notEmpty' => 'Cannot be empty',
  2910. 'email' => 'No good!'
  2911. ));
  2912. $expected = array(
  2913. 'div' => array('class' => 'error-message'),
  2914. 'ul' => array(),
  2915. '<li', 'Cannot be empty', '/li',
  2916. '<li', 'No good!', '/li',
  2917. '<li', 'Something else', '/li',
  2918. '/ul',
  2919. '/div'
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. }
  2923. /**
  2924. * testPassword method
  2925. *
  2926. * Test password element generation
  2927. *
  2928. * @return void
  2929. */
  2930. public function testPassword() {
  2931. $this->article['errors'] = [
  2932. 'Contact' => [
  2933. 'passwd' => 1
  2934. ]
  2935. ];
  2936. $this->Form->create($this->article);
  2937. $result = $this->Form->password('Contact.field');
  2938. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  2939. $this->Form->request->data['Contact']['passwd'] = 'test';
  2940. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2941. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2942. }
  2943. /**
  2944. * testRadio method
  2945. *
  2946. * Test radio element set generation
  2947. *
  2948. * @return void
  2949. */
  2950. public function testRadio() {
  2951. $result = $this->Form->radio('Model.field', array('option A'));
  2952. $expected = array(
  2953. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2954. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2955. 'label' => array('for' => 'model-field-0'),
  2956. 'option A',
  2957. '/label'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  2961. $this->assertTags($result, $expected);
  2962. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2963. $expected = array(
  2964. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2965. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2966. array('label' => array('for' => 'model-field-0')),
  2967. 'option A',
  2968. '/label',
  2969. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  2970. array('label' => array('for' => 'model-field-1')),
  2971. 'option B',
  2972. '/label',
  2973. );
  2974. $this->assertTags($result, $expected);
  2975. $result = $this->Form->radio(
  2976. 'Employee.gender',
  2977. array('male' => 'Male', 'female' => 'Female'),
  2978. ['form' => 'my-form']
  2979. );
  2980. $expected = array(
  2981. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'),
  2982. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form')),
  2983. array('label' => array('for' => 'employee-gender-male')),
  2984. 'Male',
  2985. '/label',
  2986. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form')),
  2987. array('label' => array('for' => 'employee-gender-female')),
  2988. 'Female',
  2989. '/label',
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  2993. $expected = array(
  2994. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  2995. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  2996. array('label' => array('for' => 'model-custom-0')),
  2997. 'option A',
  2998. '/label',
  2999. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3000. array('label' => array('for' => 'model-custom-1')),
  3001. 'option B',
  3002. '/label',
  3003. );
  3004. $this->assertTags($result, $expected);
  3005. }
  3006. /**
  3007. * test generating radio input inside label ala twitter bootstrap
  3008. *
  3009. * @return void
  3010. */
  3011. public function testRadioInputInsideLabel() {
  3012. $this->Form->templates([
  3013. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3014. 'radioWrapper' => '{{label}}'
  3015. ]);
  3016. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3017. $expected = [
  3018. ['input' => [
  3019. 'type' => 'hidden',
  3020. 'name' => 'Model[field]',
  3021. 'value' => ''
  3022. ]],
  3023. ['label' => ['for' => 'model-field-0']],
  3024. ['input' => [
  3025. 'type' => 'radio',
  3026. 'name' => 'Model[field]',
  3027. 'value' => '0',
  3028. 'id' => 'model-field-0'
  3029. ]],
  3030. 'option A',
  3031. '/label',
  3032. ['label' => ['for' => 'model-field-1']],
  3033. ['input' => [
  3034. 'type' => 'radio',
  3035. 'name' => 'Model[field]',
  3036. 'value' => '1',
  3037. 'id' => 'model-field-1'
  3038. ]],
  3039. 'option B',
  3040. '/label',
  3041. ];
  3042. $this->assertTags($result, $expected);
  3043. }
  3044. /**
  3045. * test disabling the hidden input for radio buttons
  3046. *
  3047. * @return void
  3048. */
  3049. public function testRadioHiddenInputDisabling() {
  3050. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3051. $expected = array(
  3052. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3053. 'label' => array('for' => 'model-1-field-0'),
  3054. 'option A',
  3055. '/label'
  3056. );
  3057. $this->assertTags($result, $expected);
  3058. }
  3059. /**
  3060. * testSelect method
  3061. *
  3062. * Test select element generation.
  3063. *
  3064. * @return void
  3065. */
  3066. public function testSelect() {
  3067. $result = $this->Form->select('Model.field', array());
  3068. $expected = array(
  3069. 'select' => array('name' => 'Model[field]'),
  3070. '/select'
  3071. );
  3072. $this->assertTags($result, $expected);
  3073. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3074. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3075. $expected = array(
  3076. 'select' => array('name' => 'Model[field]'),
  3077. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3078. 'good',
  3079. '/option',
  3080. array('option' => array('value' => 'other')),
  3081. 'bad',
  3082. '/option',
  3083. '/select'
  3084. );
  3085. $this->assertTags($result, $expected);
  3086. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3087. $this->assertTags($result, $expected);
  3088. $this->Form->request->data = array();
  3089. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3090. $expected = array(
  3091. 'select' => array('name' => 'Model[field]'),
  3092. array('option' => array('value' => 'value')),
  3093. 'good',
  3094. '/option',
  3095. array('option' => array('value' => 'other')),
  3096. 'bad',
  3097. '/option',
  3098. '/select'
  3099. );
  3100. $this->assertTags($result, $expected);
  3101. $options = array(
  3102. array('value' => 'first', 'text' => 'First'),
  3103. array('value' => 'first', 'text' => 'Another First'),
  3104. );
  3105. $result = $this->Form->select(
  3106. 'Model.field',
  3107. $options,
  3108. array('escape' => false, 'empty' => false)
  3109. );
  3110. $expected = array(
  3111. 'select' => array('name' => 'Model[field]'),
  3112. array('option' => array('value' => 'first')),
  3113. 'First',
  3114. '/option',
  3115. array('option' => array('value' => 'first')),
  3116. 'Another First',
  3117. '/option',
  3118. '/select'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3122. $result = $this->Form->select(
  3123. 'Model.contact_id',
  3124. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3125. array('escape' => false, 'empty' => 'pick something')
  3126. );
  3127. $expected = array(
  3128. 'select' => array('name' => 'Model[contact_id]'),
  3129. array('option' => array('value' => '')), 'pick something', '/option',
  3130. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3131. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3132. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3133. '/select'
  3134. );
  3135. $this->assertTags($result, $expected);
  3136. $this->Form->request->data['Model']['field'] = 0;
  3137. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3138. $expected = array(
  3139. 'select' => array('name' => 'Model[field]'),
  3140. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3141. array('option' => array('value' => '1')), 'Yes', '/option',
  3142. '/select'
  3143. );
  3144. $this->assertTags($result, $expected);
  3145. }
  3146. /**
  3147. * Test that select() escapes HTML.
  3148. *
  3149. * @return void
  3150. */
  3151. public function testSelectEscapeHtml() {
  3152. $result = $this->Form->select(
  3153. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3154. array('empty' => false)
  3155. );
  3156. $expected = array(
  3157. 'select' => array('name' => 'Model[field]'),
  3158. array('option' => array('value' => 'first')),
  3159. 'first &quot;html&quot; &lt;chars&gt;',
  3160. '/option',
  3161. array('option' => array('value' => 'second')),
  3162. 'value',
  3163. '/option',
  3164. '/select'
  3165. );
  3166. $this->assertTags($result, $expected);
  3167. $result = $this->Form->select(
  3168. 'Model.field',
  3169. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3170. array('escape' => false, 'empty' => false)
  3171. );
  3172. $expected = array(
  3173. 'select' => array('name' => 'Model[field]'),
  3174. array('option' => array('value' => 'first')),
  3175. 'first "html" <chars>',
  3176. '/option',
  3177. array('option' => array('value' => 'second')),
  3178. 'value',
  3179. '/option',
  3180. '/select'
  3181. );
  3182. $this->assertTags($result, $expected);
  3183. }
  3184. /**
  3185. * test select() with required and disabled attributes.
  3186. *
  3187. * @return void
  3188. */
  3189. public function testSelectRequired() {
  3190. $this->article['required'] = [
  3191. 'user_id' => true
  3192. ];
  3193. $this->Form->create($this->article);
  3194. $result = $this->Form->select('user_id', array('option A'));
  3195. $expected = array(
  3196. 'select' => array(
  3197. 'name' => 'user_id',
  3198. 'required' => 'required'
  3199. ),
  3200. array('option' => array('value' => '0')), 'option A', '/option',
  3201. '/select'
  3202. );
  3203. $this->assertTags($result, $expected);
  3204. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3205. $expected = array(
  3206. 'select' => array(
  3207. 'name' => 'user_id',
  3208. 'disabled' => 'disabled'
  3209. ),
  3210. array('option' => array('value' => '0')), 'option A', '/option',
  3211. '/select'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. }
  3215. /**
  3216. * testNestedSelect method
  3217. *
  3218. * test select element generation with optgroups
  3219. *
  3220. * @return void
  3221. */
  3222. public function testNestedSelect() {
  3223. $result = $this->Form->select(
  3224. 'Model.field',
  3225. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3226. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3227. )), array('empty' => false)
  3228. );
  3229. $expected = array(
  3230. 'select' => array('name' => 'Model[field]'),
  3231. array('option' => array('value' => 1)),
  3232. 'One',
  3233. '/option',
  3234. array('option' => array('value' => 2)),
  3235. 'Two',
  3236. '/option',
  3237. array('optgroup' => array('label' => 'Three')),
  3238. array('option' => array('value' => 3)),
  3239. 'Three',
  3240. '/option',
  3241. array('option' => array('value' => 4)),
  3242. 'Four',
  3243. '/option',
  3244. array('option' => array('value' => 5)),
  3245. 'Five',
  3246. '/option',
  3247. '/optgroup',
  3248. '/select'
  3249. );
  3250. $this->assertTags($result, $expected);
  3251. }
  3252. /**
  3253. * testSelectMultiple method
  3254. *
  3255. * test generation of multiple select elements
  3256. *
  3257. * @return void
  3258. */
  3259. public function testSelectMultiple() {
  3260. $options = array('first', 'second', 'third');
  3261. $result = $this->Form->select(
  3262. 'Model.multi_field',
  3263. $options,
  3264. ['form' => 'my-form', 'multiple' => true]
  3265. );
  3266. $expected = array(
  3267. 'input' => array(
  3268. 'type' => 'hidden',
  3269. 'name' => 'Model[multi_field]',
  3270. 'value' => '',
  3271. 'form' => 'my-form',
  3272. ),
  3273. 'select' => array(
  3274. 'name' => 'Model[multi_field][]',
  3275. 'multiple' => 'multiple',
  3276. 'form' => 'my-form',
  3277. ),
  3278. array('option' => array('value' => '0')),
  3279. 'first',
  3280. '/option',
  3281. array('option' => array('value' => '1')),
  3282. 'second',
  3283. '/option',
  3284. array('option' => array('value' => '2')),
  3285. 'third',
  3286. '/option',
  3287. '/select'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. $result = $this->Form->select(
  3291. 'Model.multi_field',
  3292. $options,
  3293. ['multiple' => 'multiple', 'form' => 'my-form']
  3294. );
  3295. $this->assertTags($result, $expected);
  3296. }
  3297. /**
  3298. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3299. *
  3300. * @return void
  3301. */
  3302. public function testCheckboxZeroValue() {
  3303. $result = $this->Form->input('User.get_spam', array(
  3304. 'type' => 'checkbox',
  3305. 'value' => '0',
  3306. 'hiddenField' => '1',
  3307. ));
  3308. $expected = array(
  3309. 'div' => array('class' => 'input checkbox'),
  3310. array('input' => array(
  3311. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3312. 'value' => '1'
  3313. )),
  3314. array('input' => array(
  3315. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3316. 'value' => '0', 'id' => 'user-get-spam'
  3317. )),
  3318. 'label' => array('for' => 'user-get-spam'),
  3319. 'Get Spam',
  3320. '/label',
  3321. '/div'
  3322. );
  3323. $this->assertTags($result, $expected);
  3324. }
  3325. /**
  3326. * test generation of habtm select boxes.
  3327. *
  3328. * @return void
  3329. */
  3330. public function testHabtmSelectBox() {
  3331. $this->loadFixtures('Article');
  3332. $options = array(
  3333. 1 => 'blue',
  3334. 2 => 'red',
  3335. 3 => 'green'
  3336. );
  3337. $tags = [
  3338. new Entity(['id' => 1, 'name' => 'blue']),
  3339. new Entity(['id' => 3, 'name' => 'green'])
  3340. ];
  3341. $article = new Article(['tags' => $tags]);
  3342. $this->Form->create($article);
  3343. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3344. $expected = array(
  3345. 'div' => array('class' => 'input select'),
  3346. 'label' => array('for' => 'tags-ids'),
  3347. 'Tags',
  3348. '/label',
  3349. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3350. 'select' => array(
  3351. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3352. 'multiple' => 'multiple'
  3353. ),
  3354. array('option' => array('value' => '1', 'selected' => 'selected')),
  3355. 'blue',
  3356. '/option',
  3357. array('option' => array('value' => '2')),
  3358. 'red',
  3359. '/option',
  3360. array('option' => array('value' => '3', 'selected' => 'selected')),
  3361. 'green',
  3362. '/option',
  3363. '/select',
  3364. '/div'
  3365. );
  3366. $this->assertTags($result, $expected);
  3367. // make sure only 50 is selected, and not 50f5c0cf
  3368. $options = array(
  3369. '1' => 'blue',
  3370. '50f5c0cf' => 'red',
  3371. '50' => 'green'
  3372. );
  3373. $tags = [
  3374. new Entity(['id' => 1, 'name' => 'blue']),
  3375. new Entity(['id' => 50, 'name' => 'green'])
  3376. ];
  3377. $article = new Article(['tags' => $tags]);
  3378. $this->Form->create($article);
  3379. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3380. $expected = array(
  3381. 'div' => array('class' => 'input select'),
  3382. 'label' => array('for' => 'tags-ids'),
  3383. 'Tags',
  3384. '/label',
  3385. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3386. 'select' => array(
  3387. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3388. 'multiple' => 'multiple'
  3389. ),
  3390. array('option' => array('value' => '1', 'selected' => 'selected')),
  3391. 'blue',
  3392. '/option',
  3393. array('option' => array('value' => '50f5c0cf')),
  3394. 'red',
  3395. '/option',
  3396. array('option' => array('value' => '50', 'selected' => 'selected')),
  3397. 'green',
  3398. '/option',
  3399. '/select',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. }
  3404. /**
  3405. * test generation of multi select elements in checkbox format
  3406. *
  3407. * @return void
  3408. */
  3409. public function testSelectMultipleCheckboxes() {
  3410. $result = $this->Form->select(
  3411. 'Model.multi_field',
  3412. array('first', 'second', 'third'),
  3413. array('multiple' => 'checkbox')
  3414. );
  3415. $expected = array(
  3416. 'input' => array(
  3417. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3418. ),
  3419. array('div' => array('class' => 'checkbox')),
  3420. array('input' => array(
  3421. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3422. 'value' => '0', 'id' => 'model-multi-field-0'
  3423. )),
  3424. array('label' => array('for' => 'model-multi-field-0')),
  3425. 'first',
  3426. '/label',
  3427. '/div',
  3428. array('div' => array('class' => 'checkbox')),
  3429. array('input' => array(
  3430. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3431. 'value' => '1', 'id' => 'model-multi-field-1'
  3432. )),
  3433. array('label' => array('for' => 'model-multi-field-1')),
  3434. 'second',
  3435. '/label',
  3436. '/div',
  3437. array('div' => array('class' => 'checkbox')),
  3438. array('input' => array(
  3439. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3440. 'value' => '2', 'id' => 'model-multi-field-2'
  3441. )),
  3442. array('label' => array('for' => 'model-multi-field-2')),
  3443. 'third',
  3444. '/label',
  3445. '/div'
  3446. );
  3447. $this->assertTags($result, $expected);
  3448. $result = $this->Form->select(
  3449. 'Model.multi_field',
  3450. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3451. array('multiple' => 'checkbox')
  3452. );
  3453. $expected = array(
  3454. 'input' => array(
  3455. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3456. ),
  3457. array('div' => array('class' => 'checkbox')),
  3458. array('input' => array(
  3459. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3460. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3461. )),
  3462. array('label' => array('for' => 'model-multi-field-a+')),
  3463. 'first',
  3464. '/label',
  3465. '/div',
  3466. array('div' => array('class' => 'checkbox')),
  3467. array('input' => array(
  3468. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3469. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3470. )),
  3471. array('label' => array('for' => 'model-multi-field-a++')),
  3472. 'second',
  3473. '/label',
  3474. '/div',
  3475. array('div' => array('class' => 'checkbox')),
  3476. array('input' => array(
  3477. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3478. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3479. )),
  3480. array('label' => array('for' => 'model-multi-field-a+++')),
  3481. 'third',
  3482. '/label',
  3483. '/div'
  3484. );
  3485. $this->assertTags($result, $expected);
  3486. $result = $this->Form->select(
  3487. 'Model.multi_field',
  3488. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3489. array('multiple' => 'checkbox')
  3490. );
  3491. $expected = array(
  3492. 'input' => array(
  3493. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3494. ),
  3495. array('div' => array('class' => 'checkbox')),
  3496. array('input' => array(
  3497. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3498. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3499. )),
  3500. array('label' => array('for' => 'model-multi-field-a-b')),
  3501. 'first',
  3502. '/label',
  3503. '/div',
  3504. array('div' => array('class' => 'checkbox')),
  3505. array('input' => array(
  3506. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3507. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3508. )),
  3509. array('label' => array('for' => 'model-multi-field-a-b1')),
  3510. 'second',
  3511. '/label',
  3512. '/div',
  3513. array('div' => array('class' => 'checkbox')),
  3514. array('input' => array(
  3515. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3516. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3517. )),
  3518. array('label' => array('for' => 'model-multi-field-a-b2')),
  3519. 'third',
  3520. '/label',
  3521. '/div'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * Ensure that multiCheckbox reads from the request data.
  3527. *
  3528. * @return void
  3529. */
  3530. public function testSelectMultipleCheckboxRequestData() {
  3531. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3532. $result = $this->Form->select(
  3533. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3534. );
  3535. $expected = array(
  3536. 'input' => array(
  3537. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3538. ),
  3539. array('div' => array('class' => 'checkbox')),
  3540. array('input' => array(
  3541. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3542. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3543. )),
  3544. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3545. 'first',
  3546. '/label',
  3547. '/div',
  3548. array('div' => array('class' => 'checkbox')),
  3549. array('input' => array(
  3550. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3551. 'value' => 'Array', 'id' => 'model-tags-array'
  3552. )),
  3553. array('label' => array('for' => 'model-tags-array')),
  3554. 'Array',
  3555. '/label',
  3556. '/div'
  3557. );
  3558. $this->assertTags($result, $expected);
  3559. }
  3560. /**
  3561. * Checks the security hash array generated for multiple-input checkbox elements
  3562. *
  3563. * @return void
  3564. */
  3565. public function testSelectMultipleCheckboxSecurity() {
  3566. $this->Form->request->params['_Token'] = 'testKey';
  3567. $this->assertEquals(array(), $this->Form->fields);
  3568. $result = $this->Form->select(
  3569. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3570. array('multiple' => 'checkbox')
  3571. );
  3572. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3573. $result = $this->Form->secure($this->Form->fields);
  3574. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3575. $this->assertRegExp('/"' . $key . '"/', $result);
  3576. }
  3577. /**
  3578. * Multiple select elements should always be secured as they always participate
  3579. * in the POST data.
  3580. *
  3581. * @return void
  3582. */
  3583. public function testSelectMultipleSecureWithNoOptions() {
  3584. $this->assertEquals(array(), $this->Form->fields);
  3585. $this->Form->select(
  3586. 'Model.select',
  3587. array(),
  3588. array('multiple' => true)
  3589. );
  3590. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3591. }
  3592. /**
  3593. * When a select box has no options it should not be added to the fields list
  3594. * as it always fail post validation.
  3595. *
  3596. * @return void
  3597. */
  3598. public function testSelectNoSecureWithNoOptions() {
  3599. $this->Form->request->params['_Token'] = 'testkey';
  3600. $this->assertEquals([], $this->Form->fields);
  3601. $this->Form->select(
  3602. 'Model.select',
  3603. []
  3604. );
  3605. $this->assertEquals([], $this->Form->fields);
  3606. $this->Form->select(
  3607. 'Model.user_id',
  3608. [],
  3609. ['empty' => true]
  3610. );
  3611. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3612. }
  3613. /**
  3614. * testInputMultipleCheckboxes method
  3615. *
  3616. * test input() resulting in multi select elements being generated.
  3617. *
  3618. * @return void
  3619. */
  3620. public function testInputMultipleCheckboxes() {
  3621. $result = $this->Form->input('Model.multi_field', array(
  3622. 'options' => array('first', 'second', 'third'),
  3623. 'multiple' => 'checkbox'
  3624. ));
  3625. $expected = array(
  3626. array('div' => array('class' => 'input select')),
  3627. array('label' => array('for' => 'model-multi-field')),
  3628. 'Multi Field',
  3629. '/label',
  3630. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3631. array('div' => array('class' => 'checkbox')),
  3632. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3633. array('label' => array('for' => 'model-multi-field-0')),
  3634. 'first',
  3635. '/label',
  3636. '/div',
  3637. array('div' => array('class' => 'checkbox')),
  3638. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3639. array('label' => array('for' => 'model-multi-field-1')),
  3640. 'second',
  3641. '/label',
  3642. '/div',
  3643. array('div' => array('class' => 'checkbox')),
  3644. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3645. array('label' => array('for' => 'model-multi-field-2')),
  3646. 'third',
  3647. '/label',
  3648. '/div',
  3649. '/div'
  3650. );
  3651. $this->assertTags($result, $expected);
  3652. $result = $this->Form->input('Model.multi_field', array(
  3653. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3654. 'multiple' => 'checkbox'
  3655. ));
  3656. $expected = array(
  3657. array('div' => array('class' => 'input select')),
  3658. array('label' => array('for' => 'model-multi-field')),
  3659. 'Multi Field',
  3660. '/label',
  3661. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3662. array('div' => array('class' => 'checkbox')),
  3663. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3664. array('label' => array('for' => 'model-multi-field-a')),
  3665. 'first',
  3666. '/label',
  3667. '/div',
  3668. array('div' => array('class' => 'checkbox')),
  3669. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3670. array('label' => array('for' => 'model-multi-field-b')),
  3671. 'second',
  3672. '/label',
  3673. '/div',
  3674. array('div' => array('class' => 'checkbox')),
  3675. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3676. array('label' => array('for' => 'model-multi-field-c')),
  3677. 'third',
  3678. '/label',
  3679. '/div',
  3680. '/div'
  3681. );
  3682. $this->assertTags($result, $expected);
  3683. }
  3684. /**
  3685. * testSelectHiddenFieldOmission method
  3686. *
  3687. * test that select() with 'hiddenField' => false omits the hidden field
  3688. *
  3689. * @return void
  3690. */
  3691. public function testSelectHiddenFieldOmission() {
  3692. $result = $this->Form->select('Model.multi_field',
  3693. array('first', 'second'),
  3694. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3695. );
  3696. $this->assertNotContains('type="hidden"', $result);
  3697. }
  3698. /**
  3699. * test that select() with multiple = checkbox works with overriding name attribute.
  3700. *
  3701. * @return void
  3702. */
  3703. public function testSelectCheckboxMultipleOverrideName() {
  3704. $result = $this->Form->select('category', ['1', '2'], [
  3705. 'multiple' => 'checkbox',
  3706. 'name' => 'fish',
  3707. ]);
  3708. $expected = array(
  3709. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3710. array('div' => array('class' => 'checkbox')),
  3711. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3712. array('label' => array('for' => 'fish-0')), '1', '/label',
  3713. '/div',
  3714. array('div' => array('class' => 'checkbox')),
  3715. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3716. array('label' => array('for' => 'fish-1')), '2', '/label',
  3717. '/div'
  3718. );
  3719. $this->assertTags($result, $expected);
  3720. $result = $this->Form->multiCheckbox(
  3721. 'category',
  3722. new Collection(['1', '2']),
  3723. ['name' => 'fish']
  3724. );
  3725. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3726. 'name' => 'fish',
  3727. ]);
  3728. $this->assertTags($result, $expected);
  3729. }
  3730. /**
  3731. * testCheckbox method
  3732. *
  3733. * Test generation of checkboxes
  3734. *
  3735. * @return void
  3736. */
  3737. public function testCheckbox() {
  3738. $result = $this->Form->checkbox('Model.field');
  3739. $expected = array(
  3740. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3741. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3742. );
  3743. $this->assertTags($result, $expected);
  3744. $result = $this->Form->checkbox('Model.field', array(
  3745. 'id' => 'theID',
  3746. 'value' => 'myvalue',
  3747. 'form' => 'my-form',
  3748. ));
  3749. $expected = array(
  3750. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'),
  3751. array('input' => array(
  3752. 'type' => 'checkbox', 'name' => 'Model[field]',
  3753. 'value' => 'myvalue', 'id' => 'theID',
  3754. 'form' => 'my-form',
  3755. ))
  3756. );
  3757. $this->assertTags($result, $expected);
  3758. }
  3759. /**
  3760. * testCheckboxDefaultValue method
  3761. *
  3762. * Test default value setting on checkbox() method
  3763. *
  3764. * @return void
  3765. */
  3766. public function testCheckboxDefaultValue() {
  3767. $this->Form->request->data['Model']['field'] = false;
  3768. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3769. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3770. unset($this->Form->request->data['Model']['field']);
  3771. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3772. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3773. $this->Form->request->data['Model']['field'] = true;
  3774. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3775. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3776. unset($this->Form->request->data['Model']['field']);
  3777. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3778. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3779. }
  3780. /**
  3781. * Test checkbox being checked or having errors.
  3782. *
  3783. * @return void
  3784. */
  3785. public function testCheckboxCheckedAndError() {
  3786. $this->article['errors'] = [
  3787. 'published' => true
  3788. ];
  3789. $this->Form->request->data['published'] = 'myvalue';
  3790. $this->Form->create($this->article);
  3791. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3792. $expected = array(
  3793. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3794. array('input' => array(
  3795. 'type' => 'checkbox',
  3796. 'name' => 'published',
  3797. 'value' => 'myvalue',
  3798. 'id' => 'theID',
  3799. 'checked' => 'checked',
  3800. 'class' => 'form-error'
  3801. ))
  3802. );
  3803. $this->assertTags($result, $expected);
  3804. $this->Form->request->data['published'] = '';
  3805. $result = $this->Form->checkbox('published');
  3806. $expected = array(
  3807. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3808. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3809. );
  3810. $this->assertTags($result, $expected);
  3811. }
  3812. /**
  3813. * test checkbox() with a custom name attribute
  3814. *
  3815. * @return void
  3816. */
  3817. public function testCheckboxCustomNameAttribute() {
  3818. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3819. $expected = array(
  3820. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3821. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3822. );
  3823. $this->assertTags($result, $expected);
  3824. }
  3825. /**
  3826. * Test that the hidden input for checkboxes can be omitted or set to a
  3827. * specific value.
  3828. *
  3829. * @return void
  3830. */
  3831. public function testCheckboxHiddenField() {
  3832. $result = $this->Form->checkbox('UserForm.something', array(
  3833. 'hiddenField' => false
  3834. ));
  3835. $expected = array(
  3836. 'input' => array(
  3837. 'type' => 'checkbox',
  3838. 'name' => 'UserForm[something]',
  3839. 'value' => '1'
  3840. ),
  3841. );
  3842. $this->assertTags($result, $expected);
  3843. $result = $this->Form->checkbox('UserForm.something', array(
  3844. 'value' => 'Y',
  3845. 'hiddenField' => 'N',
  3846. ));
  3847. $expected = array(
  3848. array('input' => array(
  3849. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3850. 'value' => 'N'
  3851. )),
  3852. array('input' => array(
  3853. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3854. 'value' => 'Y'
  3855. )),
  3856. );
  3857. $this->assertTags($result, $expected);
  3858. }
  3859. /**
  3860. * Test the time type.
  3861. *
  3862. * @return void
  3863. */
  3864. public function testTime() {
  3865. $result = $this->Form->time('start_time', array(
  3866. 'timeFormat' => 12,
  3867. 'interval' => 5,
  3868. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  3869. ));
  3870. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3871. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3872. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3873. $this->assertNotContains('year', $result);
  3874. $this->assertNotContains('month', $result);
  3875. $this->assertNotContains('day', $result);
  3876. $result = $this->Form->time('start_time', array(
  3877. 'timeFormat' => 12,
  3878. 'interval' => 5,
  3879. 'value' => '2014-03-08 16:30:00'
  3880. ));
  3881. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  3882. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3883. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  3884. $this->assertNotContains('year', $result);
  3885. $this->assertNotContains('month', $result);
  3886. $this->assertNotContains('day', $result);
  3887. }
  3888. /**
  3889. * Ensure that timeFormat=24 has no merdian.
  3890. *
  3891. * @return void.
  3892. */
  3893. public function testTimeFormat24NoMeridian() {
  3894. $result = $this->Form->time('start_time', array(
  3895. 'timeFormat' => 24,
  3896. 'interval' => 5,
  3897. 'value' => '2014-03-08 16:30:00'
  3898. ));
  3899. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  3900. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  3901. $this->assertNotContains('meridian', $result);
  3902. $this->assertNotContains('pm', $result);
  3903. $this->assertNotContains('year', $result);
  3904. $this->assertNotContains('month', $result);
  3905. $this->assertNotContains('day', $result);
  3906. }
  3907. /**
  3908. * Test the date type.
  3909. *
  3910. * @return void
  3911. */
  3912. public function testDate() {
  3913. $result = $this->Form->date('start_day', array(
  3914. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  3915. ));
  3916. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  3917. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  3918. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  3919. $this->assertNotContains('hour', $result);
  3920. $this->assertNotContains('minute', $result);
  3921. $this->assertNotContains('second', $result);
  3922. $this->assertNotContains('meridian', $result);
  3923. }
  3924. /**
  3925. * testDateTime method
  3926. *
  3927. * Test generation of date/time select elements
  3928. *
  3929. * @return void
  3930. */
  3931. public function testDateTime() {
  3932. extract($this->dateRegex);
  3933. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3934. $now = strtotime('now');
  3935. $expected = array(
  3936. array('select' => array('name' => 'Contact[date][year]')),
  3937. $yearsRegex,
  3938. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  3939. date('Y', $now),
  3940. '/option',
  3941. '*/select',
  3942. array('select' => array('name' => 'Contact[date][month]')),
  3943. $monthsRegex,
  3944. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3945. date('F', $now),
  3946. '/option',
  3947. '*/select',
  3948. array('select' => array('name' => 'Contact[date][day]')),
  3949. $daysRegex,
  3950. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  3951. date('j', $now),
  3952. '/option',
  3953. '*/select',
  3954. array('select' => array('name' => 'Contact[date][hour]')),
  3955. $hoursRegex,
  3956. array('option' => array('value' => date('H', $now), 'selected' => 'selected')),
  3957. date('G', $now),
  3958. '/option',
  3959. '*/select',
  3960. array('select' => array('name' => 'Contact[date][minute]')),
  3961. $minutesRegex,
  3962. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  3963. date('i', $now),
  3964. '/option',
  3965. '*/select',
  3966. );
  3967. $this->assertTags($result, $expected);
  3968. }
  3969. /**
  3970. * Test that datetime fields are added to protected fields list.
  3971. *
  3972. * @return void
  3973. */
  3974. public function testDateTimeSecured() {
  3975. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  3976. $this->Form->dateTime('Contact.date');
  3977. $expected = [
  3978. 'Contact.date.year',
  3979. 'Contact.date.month',
  3980. 'Contact.date.day',
  3981. 'Contact.date.hour',
  3982. 'Contact.date.minute',
  3983. 'Contact.date.meridian',
  3984. ];
  3985. $this->assertEquals($expected, $this->Form->fields);
  3986. $this->Form->fields = [];
  3987. $this->Form->date('Contact.published');
  3988. $expected = [
  3989. 'Contact.published.year',
  3990. 'Contact.published.month',
  3991. 'Contact.published.day',
  3992. ];
  3993. $this->assertEquals($expected, $this->Form->fields);
  3994. }
  3995. /**
  3996. * Test that datetime fields are added to protected fields list.
  3997. *
  3998. * @return void
  3999. */
  4000. public function testDateTimeSecuredDisabled() {
  4001. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4002. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4003. $expected = [];
  4004. $this->assertEquals($expected, $this->Form->fields);
  4005. $this->Form->fields = [];
  4006. $this->Form->date('Contact.published', ['secure' => false]);
  4007. $expected = [];
  4008. $this->assertEquals($expected, $this->Form->fields);
  4009. }
  4010. /**
  4011. * Test empty defaulting to true for datetime.
  4012. *
  4013. * @return void
  4014. */
  4015. public function testDatetimeEmpty() {
  4016. extract($this->dateRegex);
  4017. $now = strtotime('now');
  4018. $result = $this->Form->dateTime('Contact.date', array(
  4019. 'timeFormat' => 12,
  4020. 'empty' => true,
  4021. ));
  4022. $expected = array(
  4023. array('select' => array('name' => 'Contact[date][year]')),
  4024. $yearsRegex,
  4025. array('option' => array('value' => '')),
  4026. '/option',
  4027. '*/select',
  4028. array('select' => array('name' => 'Contact[date][month]')),
  4029. $monthsRegex,
  4030. array('option' => array('value' => '')),
  4031. '/option',
  4032. '*/select',
  4033. array('select' => array('name' => 'Contact[date][day]')),
  4034. $daysRegex,
  4035. array('option' => array('value' => '')),
  4036. '/option',
  4037. '*/select',
  4038. array('select' => array('name' => 'Contact[date][hour]')),
  4039. $hoursRegex,
  4040. array('option' => array('value' => '')),
  4041. '/option',
  4042. '*/select',
  4043. array('select' => array('name' => 'Contact[date][minute]')),
  4044. $minutesRegex,
  4045. array('option' => array('value' => '')),
  4046. '/option',
  4047. '*/select',
  4048. array('select' => array('name' => 'Contact[date][meridian]')),
  4049. $meridianRegex,
  4050. array('option' => array('value' => '')),
  4051. '/option',
  4052. '*/select'
  4053. );
  4054. $this->assertTags($result, $expected);
  4055. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4056. }
  4057. /**
  4058. * Test datetime with interval option.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testDatetimeMinuteInterval() {
  4063. extract($this->dateRegex);
  4064. $now = strtotime('now');
  4065. $result = $this->Form->dateTime('Contact.date', array(
  4066. 'interval' => 5,
  4067. 'value' => ''
  4068. ));
  4069. $expected = array(
  4070. array('select' => array('name' => 'Contact[date][year]')),
  4071. $yearsRegex,
  4072. array('option' => array('selected' => 'selected', 'value' => '')),
  4073. '/option',
  4074. '*/select',
  4075. array('select' => array('name' => 'Contact[date][month]')),
  4076. $monthsRegex,
  4077. array('option' => array('selected' => 'selected', 'value' => '')),
  4078. '/option',
  4079. '*/select',
  4080. array('select' => array('name' => 'Contact[date][day]')),
  4081. $daysRegex,
  4082. array('option' => array('selected' => 'selected', 'value' => '')),
  4083. '/option',
  4084. '*/select',
  4085. array('select' => array('name' => 'Contact[date][hour]')),
  4086. $hoursRegex,
  4087. array('option' => array('selected' => 'selected', 'value' => '')),
  4088. '/option',
  4089. '*/select',
  4090. array('select' => array('name' => 'Contact[date][minute]')),
  4091. $minutesRegex,
  4092. array('option' => array('selected' => 'selected', 'value' => '')),
  4093. '/option',
  4094. array('option' => array('value' => '00')),
  4095. '00',
  4096. '/option',
  4097. array('option' => array('value' => '05')),
  4098. '05',
  4099. '/option',
  4100. array('option' => array('value' => '10')),
  4101. '10',
  4102. '/option',
  4103. '*/select',
  4104. );
  4105. $this->assertTags($result, $expected);
  4106. }
  4107. /**
  4108. * Test dateTime with rounding
  4109. *
  4110. * @return void
  4111. */
  4112. public function testDateTimeRounding() {
  4113. $this->Form->request->data['Contact'] = array(
  4114. 'date' => array(
  4115. 'day' => '13',
  4116. 'month' => '12',
  4117. 'year' => '2010',
  4118. 'hour' => '04',
  4119. 'minute' => '19',
  4120. 'meridian' => 'AM'
  4121. )
  4122. );
  4123. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4124. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4125. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4126. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4127. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4128. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4129. }
  4130. /**
  4131. * test that datetime() and default values work.
  4132. *
  4133. * @return void
  4134. */
  4135. public function testDatetimeWithDefault() {
  4136. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4137. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4138. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4139. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4140. $result = $this->Form->dateTime('Contact.updated', array(
  4141. 'default' => '2009-06-01 11:15:30'
  4142. ));
  4143. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4144. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4145. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4146. }
  4147. /**
  4148. * testDateTime all zeros
  4149. *
  4150. * @return void
  4151. */
  4152. public function testDateTimeAllZeros() {
  4153. $result = $this->Form->dateTime('Contact.date', array(
  4154. 'timeFormat' => false,
  4155. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4156. 'value' => '0000-00-00'
  4157. ));
  4158. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4159. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4160. }
  4161. /**
  4162. * testDateTimeEmptyAsArray
  4163. *
  4164. * @return void
  4165. */
  4166. public function testDateTimeEmptyAsArray() {
  4167. $result = $this->Form->dateTime('Contact.date', array(
  4168. 'empty' => array(
  4169. 'day' => 'DAY',
  4170. 'month' => 'MONTH',
  4171. 'year' => 'YEAR',
  4172. 'hour' => 'HOUR',
  4173. 'minute' => 'MINUTE',
  4174. 'meridian' => false
  4175. )
  4176. ));
  4177. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4178. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4179. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4180. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4181. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4182. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4183. $result = $this->Form->dateTime('Contact.date', array(
  4184. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4185. ));
  4186. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4187. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4188. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4189. }
  4190. /**
  4191. * testFormDateTimeMulti method
  4192. *
  4193. * test multiple datetime element generation
  4194. *
  4195. * @return void
  4196. */
  4197. public function testFormDateTimeMulti() {
  4198. extract($this->dateRegex);
  4199. $result = $this->Form->dateTime('Contact.1.updated');
  4200. $this->assertContains('Contact[1][updated][month]', $result);
  4201. $this->assertContains('Contact[1][updated][day]', $result);
  4202. $this->assertContains('Contact[1][updated][year]', $result);
  4203. $this->assertContains('Contact[1][updated][hour]', $result);
  4204. $this->assertContains('Contact[1][updated][minute]', $result);
  4205. }
  4206. /**
  4207. * When changing the date format, the label should always focus the first select box when
  4208. * clicked.
  4209. *
  4210. * @return void
  4211. */
  4212. public function testDateTimeLabelIdMatchesFirstInput() {
  4213. $this->markTestIncomplete('Need to revisit once models work again.');
  4214. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4215. $this->assertContains('label for="ModelDateMonth"', $result);
  4216. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4217. $this->assertContains('label for="ModelDateDay"', $result);
  4218. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4219. $this->assertContains('label for="ModelDateYear"', $result);
  4220. }
  4221. /**
  4222. * testMonth method
  4223. *
  4224. * @return void
  4225. */
  4226. public function testMonth() {
  4227. $result = $this->Form->month('Model.field', ['value' => '']);
  4228. $expected = array(
  4229. array('select' => array('name' => 'Model[field][month]')),
  4230. array('option' => array('value' => '', 'selected' => 'selected')),
  4231. '/option',
  4232. array('option' => array('value' => '01')),
  4233. date('F', strtotime('2008-01-01 00:00:00')),
  4234. '/option',
  4235. array('option' => array('value' => '02')),
  4236. date('F', strtotime('2008-02-01 00:00:00')),
  4237. '/option',
  4238. '*/select',
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4242. $expected = array(
  4243. array('select' => array('name' => 'Model[field][month]')),
  4244. array('option' => array('selected' => 'selected', 'value' => '')),
  4245. '/option',
  4246. array('option' => array('value' => '01')),
  4247. date('F', strtotime('2008-01-01 00:00:00')),
  4248. '/option',
  4249. array('option' => array('value' => '02')),
  4250. date('F', strtotime('2008-02-01 00:00:00')),
  4251. '/option',
  4252. '*/select',
  4253. );
  4254. $this->assertTags($result, $expected);
  4255. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4256. $expected = array(
  4257. array('select' => array('name' => 'Model[field][month]')),
  4258. array('option' => array('selected' => 'selected', 'value' => '')),
  4259. '/option',
  4260. array('option' => array('value' => '01')),
  4261. '1',
  4262. '/option',
  4263. array('option' => array('value' => '02')),
  4264. '2',
  4265. '/option',
  4266. '*/select',
  4267. );
  4268. $this->assertTags($result, $expected);
  4269. $monthNames = [
  4270. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4271. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4272. ];
  4273. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4274. $expected = array(
  4275. array('select' => array('name' => 'Model[field][month]')),
  4276. array('option' => array('value' => '')),
  4277. '/option',
  4278. array('option' => array('value' => '01', 'selected' => 'selected')),
  4279. 'Jan',
  4280. '/option',
  4281. array('option' => array('value' => '02')),
  4282. 'Feb',
  4283. '/option',
  4284. '*/select',
  4285. );
  4286. $this->assertTags($result, $expected);
  4287. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4288. $result = $this->Form->month('Project.release');
  4289. $expected = array(
  4290. array('select' => array('name' => 'Project[release][month]')),
  4291. array('option' => array('value' => '')),
  4292. '/option',
  4293. array('option' => array('value' => '01')),
  4294. 'January',
  4295. '/option',
  4296. array('option' => array('value' => '02', 'selected' => 'selected')),
  4297. 'February',
  4298. '/option',
  4299. '*/select',
  4300. );
  4301. $this->assertTags($result, $expected);
  4302. }
  4303. /**
  4304. * testDay method
  4305. *
  4306. * @return void
  4307. */
  4308. public function testDay() {
  4309. extract($this->dateRegex);
  4310. $result = $this->Form->day('Model.field', array('value' => false));
  4311. $expected = array(
  4312. array('select' => array('name' => 'Model[field][day]')),
  4313. array('option' => array('selected' => 'selected', 'value' => '')),
  4314. '/option',
  4315. array('option' => array('value' => '01')),
  4316. '1',
  4317. '/option',
  4318. array('option' => array('value' => '02')),
  4319. '2',
  4320. '/option',
  4321. $daysRegex,
  4322. '/select',
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4326. $result = $this->Form->day('Model.field');
  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['Model']['field'] = '';
  4346. $result = $this->Form->day('Model.field', array('value' => '10'));
  4347. $expected = array(
  4348. array('select' => array('name' => 'Model[field][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. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4366. $result = $this->Form->day('Project.release');
  4367. $expected = array(
  4368. array('select' => array('name' => 'Project[release][day]')),
  4369. array('option' => array('value' => '')),
  4370. '/option',
  4371. array('option' => array('value' => '01')),
  4372. '1',
  4373. '/option',
  4374. array('option' => array('value' => '02')),
  4375. '2',
  4376. '/option',
  4377. $daysRegex,
  4378. array('option' => array('value' => '10', 'selected' => 'selected')),
  4379. '10',
  4380. '/option',
  4381. $daysRegex,
  4382. '/select',
  4383. );
  4384. $this->assertTags($result, $expected);
  4385. }
  4386. /**
  4387. * testMinute method
  4388. *
  4389. * @return void
  4390. */
  4391. public function testMinute() {
  4392. extract($this->dateRegex);
  4393. $result = $this->Form->minute('Model.field', ['value' => '']);
  4394. $expected = array(
  4395. array('select' => array('name' => 'Model[field][minute]')),
  4396. array('option' => array('selected' => 'selected', 'value' => '')),
  4397. '/option',
  4398. array('option' => array('value' => '00')),
  4399. '00',
  4400. '/option',
  4401. array('option' => array('value' => '01')),
  4402. '01',
  4403. '/option',
  4404. array('option' => array('value' => '02')),
  4405. '02',
  4406. '/option',
  4407. $minutesRegex,
  4408. '/select',
  4409. );
  4410. $this->assertTags($result, $expected);
  4411. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4412. $result = $this->Form->minute('Model.field');
  4413. $expected = array(
  4414. array('select' => array('name' => 'Model[field][minute]')),
  4415. array('option' => array('value' => '')),
  4416. '/option',
  4417. array('option' => array('value' => '00')),
  4418. '00',
  4419. '/option',
  4420. array('option' => array('value' => '01')),
  4421. '01',
  4422. '/option',
  4423. array('option' => array('value' => '02')),
  4424. '02',
  4425. '/option',
  4426. $minutesRegex,
  4427. array('option' => array('value' => '12', 'selected' => 'selected')),
  4428. '12',
  4429. '/option',
  4430. $minutesRegex,
  4431. '/select',
  4432. );
  4433. $this->assertTags($result, $expected);
  4434. $this->Form->request->data['Model']['field'] = '';
  4435. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4436. $expected = array(
  4437. array('select' => array('name' => 'Model[field][minute]')),
  4438. array('option' => array('selected' => 'selected', 'value' => '')),
  4439. '/option',
  4440. array('option' => array('value' => '00')),
  4441. '00',
  4442. '/option',
  4443. array('option' => array('value' => '05')),
  4444. '05',
  4445. '/option',
  4446. array('option' => array('value' => '10')),
  4447. '10',
  4448. '/option',
  4449. $minutesRegex,
  4450. '/select',
  4451. );
  4452. $this->assertTags($result, $expected);
  4453. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4454. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4455. $expected = array(
  4456. array('select' => array('name' => 'Model[field][minute]')),
  4457. array('option' => array('value' => '')),
  4458. '/option',
  4459. array('option' => array('value' => '00')),
  4460. '00',
  4461. '/option',
  4462. array('option' => array('value' => '05')),
  4463. '05',
  4464. '/option',
  4465. array('option' => array('value' => '10', 'selected' => 'selected')),
  4466. '10',
  4467. '/option',
  4468. $minutesRegex,
  4469. '/select',
  4470. );
  4471. $this->assertTags($result, $expected);
  4472. }
  4473. /**
  4474. * Test generating an input for the meridian.
  4475. *
  4476. * @return void
  4477. */
  4478. public function testMeridian() {
  4479. extract($this->dateRegex);
  4480. $now = time();
  4481. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4482. $expected = [
  4483. array('select' => array('name' => 'Model[field][meridian]')),
  4484. array('option' => array('value' => '')),
  4485. '/option',
  4486. $meridianRegex,
  4487. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4488. date('a', $now),
  4489. '/option',
  4490. '*/select'
  4491. ];
  4492. $this->assertTags($result, $expected);
  4493. }
  4494. /**
  4495. * testHour method
  4496. *
  4497. * @return void
  4498. */
  4499. public function testHour() {
  4500. extract($this->dateRegex);
  4501. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  4502. $expected = array(
  4503. array('select' => array('name' => 'Model[field][hour]')),
  4504. array('option' => array('selected' => 'selected', 'value' => '')),
  4505. '/option',
  4506. array('option' => array('value' => '01')),
  4507. '1',
  4508. '/option',
  4509. array('option' => array('value' => '02')),
  4510. '2',
  4511. '/option',
  4512. $hoursRegex,
  4513. '/select',
  4514. );
  4515. $this->assertTags($result, $expected);
  4516. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4517. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4518. $expected = array(
  4519. array('select' => array('name' => 'Model[field][hour]')),
  4520. array('option' => array('value' => '')),
  4521. '/option',
  4522. array('option' => array('value' => '01')),
  4523. '1',
  4524. '/option',
  4525. array('option' => array('value' => '02')),
  4526. '2',
  4527. '/option',
  4528. $hoursRegex,
  4529. array('option' => array('value' => '12', 'selected' => 'selected')),
  4530. '12',
  4531. '/option',
  4532. '/select',
  4533. );
  4534. $this->assertTags($result, $expected);
  4535. $this->Form->request->data['Model']['field'] = '';
  4536. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4537. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4538. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4539. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4540. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4541. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4542. $expected = array(
  4543. array('select' => array('name' => 'Model[field][hour]')),
  4544. array('option' => array('value' => '')),
  4545. '/option',
  4546. array('option' => array('value' => '00', 'selected' => 'selected')),
  4547. '0',
  4548. '/option',
  4549. array('option' => array('value' => '01')),
  4550. '1',
  4551. '/option',
  4552. array('option' => array('value' => '02')),
  4553. '2',
  4554. '/option',
  4555. $hoursRegex,
  4556. '/select',
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. unset($this->Form->request->data['Model']['field']);
  4560. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4561. $thisHour = date('H');
  4562. $optValue = date('G');
  4563. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4564. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4565. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4566. $expected = array(
  4567. array('select' => array('name' => 'Model[field][hour]')),
  4568. array('option' => array('value' => '')),
  4569. '/option',
  4570. array('option' => array('value' => '00')),
  4571. '0',
  4572. '/option',
  4573. array('option' => array('value' => '01', 'selected' => 'selected')),
  4574. '1',
  4575. '/option',
  4576. array('option' => array('value' => '02')),
  4577. '2',
  4578. '/option',
  4579. $hoursRegex,
  4580. '/select',
  4581. );
  4582. $this->assertTags($result, $expected);
  4583. }
  4584. /**
  4585. * testYear method
  4586. *
  4587. * @return void
  4588. */
  4589. public function testYear() {
  4590. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4591. $expected = array(
  4592. array('select' => array('name' => 'Model[field][year]')),
  4593. array('option' => array('selected' => 'selected', 'value' => '')),
  4594. '/option',
  4595. array('option' => array('value' => '2007')),
  4596. '2007',
  4597. '/option',
  4598. array('option' => array('value' => '2006')),
  4599. '2006',
  4600. '/option',
  4601. '/select',
  4602. );
  4603. $this->assertTags($result, $expected);
  4604. $result = $this->Form->year('Model.field', [
  4605. 'value' => '',
  4606. 'minYear' => 2006,
  4607. 'maxYear' => 2007,
  4608. 'orderYear' => 'asc'
  4609. ]);
  4610. $expected = array(
  4611. array('select' => array('name' => 'Model[field][year]')),
  4612. array('option' => array('selected' => 'selected', 'value' => '')),
  4613. '/option',
  4614. array('option' => array('value' => '2006')),
  4615. '2006',
  4616. '/option',
  4617. array('option' => array('value' => '2007')),
  4618. '2007',
  4619. '/option',
  4620. '/select',
  4621. );
  4622. $this->assertTags($result, $expected);
  4623. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4624. $result = $this->Form->year('Contact.published', [
  4625. 'empty' => false,
  4626. 'minYear' => 2006,
  4627. 'maxYear' => 2007,
  4628. ]);
  4629. $expected = array(
  4630. array('select' => array('name' => 'Contact[published][year]')),
  4631. array('option' => array('value' => '2007')),
  4632. '2007',
  4633. '/option',
  4634. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4635. '2006',
  4636. '/option',
  4637. '/select',
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. }
  4641. /**
  4642. * testYearAutoExpandRange method
  4643. *
  4644. * @return void
  4645. */
  4646. public function testYearAutoExpandRange() {
  4647. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4648. $result = $this->Form->year('User.birthday');
  4649. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4650. $result = $matches[1];
  4651. $expected = range(date('Y') + 5, 1930);
  4652. $this->assertEquals($expected, $result);
  4653. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4654. $result = $this->Form->year('Project.release');
  4655. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4656. $result = $matches[1];
  4657. $expected = range(2050, date('Y') - 5);
  4658. $this->assertEquals($expected, $result);
  4659. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4660. $result = $this->Form->year('Project.release', [
  4661. 'minYear' => 1890,
  4662. 'maxYear' => 1900
  4663. ]);
  4664. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4665. $result = $matches[1];
  4666. $expected = range(1900, 1881);
  4667. $this->assertEquals($expected, $result);
  4668. }
  4669. /**
  4670. * Test that input() accepts the type of date and passes options in.
  4671. *
  4672. * @return void
  4673. */
  4674. public function testInputDate() {
  4675. $this->Form->request->data = array(
  4676. 'month_year' => array('month' => date('m')),
  4677. );
  4678. $this->Form->create($this->article);
  4679. $result = $this->Form->input('month_year', array(
  4680. 'label' => false,
  4681. 'type' => 'date',
  4682. 'minYear' => 2006,
  4683. 'maxYear' => 2008
  4684. ));
  4685. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4686. $this->assertNotContains('value="2008" selected="selected"', $result);
  4687. }
  4688. /**
  4689. * testInputDateMaxYear method
  4690. *
  4691. * Let's say we want to only allow users born from 2006 to 2008 to register
  4692. * This being the first singup page, we still don't have any data
  4693. *
  4694. * @return void
  4695. */
  4696. public function testInputDateMaxYear() {
  4697. $this->Form->request->data = [];
  4698. $this->Form->create($this->article);
  4699. $result = $this->Form->input('birthday', array(
  4700. 'label' => false,
  4701. 'type' => 'date',
  4702. 'minYear' => 2006,
  4703. 'maxYear' => 2008
  4704. ));
  4705. $this->assertContains('value="2008" selected="selected"', $result);
  4706. }
  4707. /**
  4708. * testTextArea method
  4709. *
  4710. * @return void
  4711. */
  4712. public function testTextArea() {
  4713. $this->Form->request->data = array('field' => 'some test data');
  4714. $result = $this->Form->textarea('field');
  4715. $expected = array(
  4716. 'textarea' => array('name' => 'field'),
  4717. 'some test data',
  4718. '/textarea',
  4719. );
  4720. $this->assertTags($result, $expected);
  4721. $result = $this->Form->textarea('user.bio');
  4722. $expected = array(
  4723. 'textarea' => array('name' => 'user[bio]'),
  4724. '/textarea',
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4728. $result = $this->Form->textarea('field');
  4729. $expected = array(
  4730. 'textarea' => array('name' => 'field'),
  4731. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4732. '/textarea',
  4733. );
  4734. $this->assertTags($result, $expected);
  4735. $this->Form->request->data = [
  4736. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4737. ];
  4738. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4739. $expected = array(
  4740. 'textarea' => array('name' => 'Model[field]'),
  4741. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4742. '/textarea',
  4743. );
  4744. $this->assertTags($result, $expected);
  4745. $result = $this->Form->textarea('0.OtherModel.field');
  4746. $expected = array(
  4747. 'textarea' => array('name' => '0[OtherModel][field]'),
  4748. '/textarea'
  4749. );
  4750. $this->assertTags($result, $expected);
  4751. }
  4752. /**
  4753. * testTextAreaWithStupidCharacters method
  4754. *
  4755. * test text area with non-ascii characters
  4756. *
  4757. * @return void
  4758. */
  4759. public function testTextAreaWithStupidCharacters() {
  4760. $result = $this->Form->textarea('Post.content', [
  4761. 'value' => "GREAT®",
  4762. 'rows' => '15',
  4763. 'cols' => '75'
  4764. ]);
  4765. $expected = [
  4766. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4767. 'GREAT®',
  4768. '/textarea',
  4769. ];
  4770. $this->assertTags($result, $expected);
  4771. }
  4772. /**
  4773. * testHiddenField method
  4774. *
  4775. * @return void
  4776. */
  4777. public function testHiddenField() {
  4778. $this->article['errors'] = [
  4779. 'field' => true
  4780. ];
  4781. $this->Form->request->data['field'] = 'test';
  4782. $this->Form->create($this->article);
  4783. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4784. $this->assertTags($result, array(
  4785. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4786. );
  4787. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4788. $expected = [
  4789. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4790. ];
  4791. $this->assertTags($result, $expected);
  4792. }
  4793. /**
  4794. * testFileUploadField method
  4795. *
  4796. * @return void
  4797. */
  4798. public function testFileUploadField() {
  4799. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4800. $result = $this->Form->file('Model.upload');
  4801. $this->assertTags($result, $expected);
  4802. $this->Form->request->data['Model']['upload'] = [
  4803. 'name' => '', 'type' => '', 'tmp_name' => '',
  4804. 'error' => 4, 'size' => 0
  4805. ];
  4806. $result = $this->Form->file('Model.upload');
  4807. $this->assertTags($result, $expected);
  4808. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4809. $result = $this->Form->file('Model.upload');
  4810. $this->assertTags($result, $expected);
  4811. }
  4812. /**
  4813. * test File upload input on a model not used in create();
  4814. *
  4815. * @return void
  4816. */
  4817. public function testFileUploadOnOtherModel() {
  4818. $this->Form->create($this->article, array('type' => 'file'));
  4819. $result = $this->Form->file('ValidateProfile.city');
  4820. $expected = array(
  4821. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4822. );
  4823. $this->assertTags($result, $expected);
  4824. }
  4825. /**
  4826. * testButton method
  4827. *
  4828. * @return void
  4829. */
  4830. public function testButton() {
  4831. $result = $this->Form->button('Hi');
  4832. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4833. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4834. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4835. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4836. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4837. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4838. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4839. $result = $this->Form->button('No type', array('type' => false));
  4840. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4841. $result = $this->Form->button('Upload Text', array(
  4842. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4843. 'escape' => false
  4844. ));
  4845. $this->assertNotRegExp('/\&039/', $result);
  4846. }
  4847. /**
  4848. * Test that button() makes unlocked fields by default.
  4849. *
  4850. * @return void
  4851. */
  4852. public function testButtonUnlockedByDefault() {
  4853. $this->Form->request->params['_csrfToken'] = 'secured';
  4854. $this->Form->button('Save', array('name' => 'save'));
  4855. $this->Form->button('Clear');
  4856. $result = $this->Form->unlockField();
  4857. $this->assertEquals(array('save'), $result);
  4858. }
  4859. /**
  4860. * testPostButton method
  4861. *
  4862. * @return void
  4863. */
  4864. public function testPostButton() {
  4865. $result = $this->Form->postButton('Hi', '/controller/action');
  4866. $this->assertTags($result, array(
  4867. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4868. 'div' => array('style' => 'display:none;'),
  4869. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4870. '/div',
  4871. 'button' => array('type' => 'submit'),
  4872. 'Hi',
  4873. '/button',
  4874. '/form'
  4875. ));
  4876. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4877. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4878. }
  4879. /**
  4880. * Test using postButton with N dimensional data.
  4881. *
  4882. * @return void
  4883. */
  4884. public function testPostButtonNestedData() {
  4885. $data = array(
  4886. 'one' => array(
  4887. 'two' => array(
  4888. 3, 4, 5
  4889. )
  4890. )
  4891. );
  4892. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4893. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4894. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4895. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4896. }
  4897. /**
  4898. * Test that postButton adds _Token fields.
  4899. *
  4900. * @return void
  4901. */
  4902. public function testSecurePostButton() {
  4903. $this->Form->request->params['_csrfToken'] = 'testkey';
  4904. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4905. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4906. $expected = array(
  4907. 'form' => array(
  4908. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4909. ),
  4910. array('div' => array('style' => 'display:none;')),
  4911. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4912. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4913. '/div',
  4914. 'button' => array('type' => 'submit'),
  4915. 'Delete',
  4916. '/button',
  4917. array('div' => array('style' => 'display:none;')),
  4918. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4919. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4920. '/div',
  4921. '/form',
  4922. );
  4923. $this->assertTags($result, $expected);
  4924. }
  4925. /**
  4926. * testPostLink method
  4927. *
  4928. * @return void
  4929. */
  4930. public function testPostLink() {
  4931. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4932. $this->assertTags($result, array(
  4933. 'form' => array(
  4934. 'method' => 'post', 'action' => '/posts/delete/1',
  4935. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4936. ),
  4937. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4938. '/form',
  4939. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4940. 'Delete',
  4941. '/a'
  4942. ));
  4943. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4944. $this->assertTags($result, array(
  4945. 'form' => array(
  4946. 'method' => 'post', 'action' => '/posts/delete/1',
  4947. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4948. ),
  4949. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4950. '/form',
  4951. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4952. 'Delete',
  4953. '/a'
  4954. ));
  4955. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('confirm' => 'Confirm?'));
  4956. $this->assertTags($result, array(
  4957. 'form' => array(
  4958. 'method' => 'post', 'action' => '/posts/delete/1',
  4959. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4960. ),
  4961. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4962. '/form',
  4963. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4964. 'Delete',
  4965. '/a'
  4966. ));
  4967. $result = $this->Form->postLink(
  4968. 'Delete',
  4969. '/posts/delete/1',
  4970. array('escape' => false, 'confirm' => '\'Confirm\' this "deletion"?')
  4971. );
  4972. $this->assertTags($result, array(
  4973. 'form' => array(
  4974. 'method' => 'post', 'action' => '/posts/delete/1',
  4975. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4976. ),
  4977. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4978. '/form',
  4979. '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;/'),
  4980. 'Delete',
  4981. '/a'
  4982. ));
  4983. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  4984. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  4985. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  4986. $this->assertTags($result, array(
  4987. 'form' => array(
  4988. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  4989. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4990. ),
  4991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4992. '/form',
  4993. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4994. 'Delete',
  4995. '/a'
  4996. ));
  4997. $result = $this->Form->postLink(
  4998. '',
  4999. array('controller' => 'items', 'action' => 'delete', 10),
  5000. array('class' => 'btn btn-danger', 'escape' => false, 'confirm' => 'Confirm thing')
  5001. );
  5002. $this->assertTags($result, array(
  5003. 'form' => array(
  5004. 'method' => 'post', 'action' => '/items/delete/10',
  5005. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5006. ),
  5007. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5008. '/form',
  5009. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5010. '/a'
  5011. ));
  5012. }
  5013. /**
  5014. * Test that security hashes for postLink include the url.
  5015. *
  5016. * @return void
  5017. */
  5018. public function testPostLinkSecurityHash() {
  5019. $hash = Security::hash(
  5020. '/posts/delete/1' .
  5021. serialize(array()) .
  5022. '' .
  5023. Configure::read('Security.salt')
  5024. );
  5025. $hash .= '%3A';
  5026. $this->Form->request->params['_Token']['key'] = 'test';
  5027. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5028. $expected = array(
  5029. 'form' => array(
  5030. 'method' => 'post', 'action' => '/posts/delete/1',
  5031. 'name', 'style' => 'display:none;'
  5032. ),
  5033. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5034. 'div' => array('style' => 'display:none;'),
  5035. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash)),
  5036. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5037. '/div',
  5038. '/form',
  5039. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5040. 'Delete',
  5041. '/a'
  5042. );
  5043. $this->assertTags($result, $expected);
  5044. }
  5045. /**
  5046. * Test using postLink with N dimensional data.
  5047. *
  5048. * @return void
  5049. */
  5050. public function testPostLinkNestedData() {
  5051. $data = array(
  5052. 'one' => array(
  5053. 'two' => array(
  5054. 3, 4, 5
  5055. )
  5056. )
  5057. );
  5058. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5059. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5060. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5061. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5062. }
  5063. /**
  5064. * test creating postLinks after a GET form.
  5065. *
  5066. * @return void
  5067. */
  5068. public function testPostLinkAfterGetForm() {
  5069. $this->Form->request->params['_csrfToken'] = 'testkey';
  5070. $this->Form->request->params['_Token'] = 'val';
  5071. $this->Form->create($this->article, array('type' => 'get'));
  5072. $this->Form->end();
  5073. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5074. $this->assertTags($result, array(
  5075. 'form' => array(
  5076. 'method' => 'post', 'action' => '/posts/delete/1',
  5077. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5078. ),
  5079. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5080. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5081. 'div' => array('style' => 'display:none;'),
  5082. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5083. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5084. '/div',
  5085. '/form',
  5086. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5087. 'Delete',
  5088. '/a'
  5089. ));
  5090. }
  5091. /**
  5092. * Test that postLink adds form tags to view block
  5093. *
  5094. * @return void
  5095. */
  5096. public function testPostLinkFormBuffer() {
  5097. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
  5098. $this->assertTags($result, array(
  5099. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5100. 'Delete',
  5101. '/a'
  5102. ));
  5103. $result = $this->View->fetch('postLink');
  5104. $this->assertTags($result, array(
  5105. 'form' => array(
  5106. 'method' => 'post', 'action' => '/posts/delete/1',
  5107. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5108. ),
  5109. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5110. '/form'
  5111. ));
  5112. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5113. array('block' => true, 'method' => 'DELETE')
  5114. );
  5115. $this->assertTags($result, array(
  5116. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5117. 'Delete',
  5118. '/a'
  5119. ));
  5120. $result = $this->View->fetch('postLink');
  5121. $this->assertTags($result, array(
  5122. 'form' => array(
  5123. 'method' => 'post', 'action' => '/posts/delete/1',
  5124. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5125. ),
  5126. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5127. '/form',
  5128. array(
  5129. 'form' => array(
  5130. 'method' => 'post', 'action' => '/posts/delete/2',
  5131. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5132. ),
  5133. ),
  5134. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5135. '/form'
  5136. ));
  5137. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5138. $this->assertTags($result, array(
  5139. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5140. 'Delete',
  5141. '/a'
  5142. ));
  5143. $result = $this->View->fetch('foobar');
  5144. $this->assertTags($result, array(
  5145. 'form' => array(
  5146. 'method' => 'post', 'action' => '/posts/delete/1',
  5147. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5148. ),
  5149. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5150. '/form'
  5151. ));
  5152. }
  5153. /**
  5154. * testSubmitButton method
  5155. *
  5156. * @return void
  5157. */
  5158. public function testSubmitButton() {
  5159. $result = $this->Form->submit('');
  5160. $expected = array(
  5161. 'div' => array('class' => 'submit'),
  5162. 'input' => array('type' => 'submit', 'value' => ''),
  5163. '/div'
  5164. );
  5165. $this->assertTags($result, $expected);
  5166. $result = $this->Form->submit('Test Submit');
  5167. $expected = array(
  5168. 'div' => array('class' => 'submit'),
  5169. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5170. '/div'
  5171. );
  5172. $this->assertTags($result, $expected);
  5173. $result = $this->Form->submit('Next >');
  5174. $expected = array(
  5175. 'div' => array('class' => 'submit'),
  5176. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5177. '/div'
  5178. );
  5179. $this->assertTags($result, $expected);
  5180. $result = $this->Form->submit('Next >', array('escape' => false));
  5181. $expected = array(
  5182. 'div' => array('class' => 'submit'),
  5183. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5184. '/div'
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5188. $expected = array(
  5189. 'div' => array('class' => 'submit'),
  5190. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5191. '/div'
  5192. );
  5193. $this->assertTags($result, $expected);
  5194. }
  5195. /**
  5196. * test image submit types.
  5197. *
  5198. * @return void
  5199. */
  5200. public function testSubmitImage() {
  5201. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5202. $expected = array(
  5203. 'div' => array('class' => 'submit'),
  5204. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5205. '/div'
  5206. );
  5207. $this->assertTags($result, $expected);
  5208. $result = $this->Form->submit('/relative/cake.power.gif');
  5209. $expected = array(
  5210. 'div' => array('class' => 'submit'),
  5211. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5212. '/div'
  5213. );
  5214. $this->assertTags($result, $expected);
  5215. $result = $this->Form->submit('cake.power.gif');
  5216. $expected = array(
  5217. 'div' => array('class' => 'submit'),
  5218. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5219. '/div'
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $result = $this->Form->submit('Not.an.image');
  5223. $expected = array(
  5224. 'div' => array('class' => 'submit'),
  5225. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5226. '/div'
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. }
  5230. /**
  5231. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5232. * be submitted at a time.
  5233. *
  5234. * @return void
  5235. */
  5236. public function testSubmitUnlockedByDefault() {
  5237. $this->Form->request->params['_Token'] = 'secured';
  5238. $this->Form->submit('Go go');
  5239. $this->Form->submit('Save', array('name' => 'save'));
  5240. $result = $this->Form->unlockField();
  5241. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5242. }
  5243. /**
  5244. * Test submit image with timestamps.
  5245. *
  5246. * @return void
  5247. */
  5248. public function testSubmitImageTimestamp() {
  5249. Configure::write('Asset.timestamp', 'force');
  5250. $result = $this->Form->submit('cake.power.gif');
  5251. $expected = array(
  5252. 'div' => array('class' => 'submit'),
  5253. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5254. '/div'
  5255. );
  5256. $this->assertTags($result, $expected);
  5257. }
  5258. /**
  5259. * test that datetime() works with GET style forms.
  5260. *
  5261. * @return void
  5262. */
  5263. public function testDateTimeWithGetForms() {
  5264. extract($this->dateRegex);
  5265. $this->Form->create($this->article, array('type' => 'get'));
  5266. $result = $this->Form->datetime('created');
  5267. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5268. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5269. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5270. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5271. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5272. }
  5273. /**
  5274. * testForMagicInputNonExistingNorValidated method
  5275. *
  5276. * @return void
  5277. */
  5278. public function testForMagicInputNonExistingNorValidated() {
  5279. $result = $this->Form->create($this->article);
  5280. $this->Form->templates(['inputContainer' => '{{content}}']);
  5281. $result = $this->Form->input('non_existing_nor_validated');
  5282. $expected = array(
  5283. 'label' => array('for' => 'non-existing-nor-validated'),
  5284. 'Non Existing Nor Validated',
  5285. '/label',
  5286. 'input' => array(
  5287. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5288. 'id' => 'non-existing-nor-validated'
  5289. )
  5290. );
  5291. $this->assertTags($result, $expected);
  5292. $result = $this->Form->input('non_existing_nor_validated', array(
  5293. 'val' => 'my value'
  5294. ));
  5295. $expected = array(
  5296. 'label' => array('for' => 'non-existing-nor-validated'),
  5297. 'Non Existing Nor Validated',
  5298. '/label',
  5299. 'input' => array(
  5300. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5301. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5302. )
  5303. );
  5304. $this->assertTags($result, $expected);
  5305. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5306. $result = $this->Form->input('non_existing_nor_validated');
  5307. $expected = array(
  5308. 'label' => array('for' => 'non-existing-nor-validated'),
  5309. 'Non Existing Nor Validated',
  5310. '/label',
  5311. 'input' => array(
  5312. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5313. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5314. )
  5315. );
  5316. $this->assertTags($result, $expected);
  5317. }
  5318. /**
  5319. * testFormMagicInputLabel method
  5320. *
  5321. * @return void
  5322. */
  5323. public function testFormMagicInputLabel() {
  5324. TableRegistry::get('Contacts', [
  5325. 'className' => __NAMESPACE__ . '\ContactsTable'
  5326. ]);
  5327. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5328. $this->Form->templates(['inputContainer' => '{{content}}']);
  5329. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5330. $expected = array(
  5331. 'label' => array('for' => 'contacts-name'),
  5332. 'My label',
  5333. '/label',
  5334. 'input' => array(
  5335. 'type' => 'text', 'name' => 'Contacts[name]',
  5336. 'id' => 'contacts-name', 'maxlength' => '255'
  5337. )
  5338. );
  5339. $this->assertTags($result, $expected);
  5340. $result = $this->Form->input('name', array(
  5341. 'label' => array('class' => 'mandatory')
  5342. ));
  5343. $expected = array(
  5344. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5345. 'Name',
  5346. '/label',
  5347. 'input' => array(
  5348. 'type' => 'text', 'name' => 'name',
  5349. 'id' => 'name', 'maxlength' => '255'
  5350. )
  5351. );
  5352. $this->assertTags($result, $expected);
  5353. $result = $this->Form->input('name', array(
  5354. 'div' => false,
  5355. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5356. ));
  5357. $expected = array(
  5358. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5359. 'My label',
  5360. '/label',
  5361. 'input' => array(
  5362. 'type' => 'text', 'name' => 'name',
  5363. 'id' => 'name', 'maxlength' => '255'
  5364. )
  5365. );
  5366. $this->assertTags($result, $expected);
  5367. $result = $this->Form->input('Contact.name', array(
  5368. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5369. ));
  5370. $expected = array(
  5371. 'label' => array('for' => 'my_id'),
  5372. 'Name',
  5373. '/label',
  5374. 'input' => array(
  5375. 'type' => 'text', 'name' => 'Contact[name]',
  5376. 'id' => 'my_id', 'maxlength' => '255'
  5377. )
  5378. );
  5379. $this->assertTags($result, $expected);
  5380. $result = $this->Form->input('1.id');
  5381. $this->assertTags($result, array('input' => array(
  5382. 'type' => 'hidden', 'name' => '1[id]',
  5383. 'id' => '1-id'
  5384. )));
  5385. $result = $this->Form->input("1.name");
  5386. $expected = array(
  5387. 'label' => array('for' => '1-name'),
  5388. 'Name',
  5389. '/label',
  5390. 'input' => array(
  5391. 'type' => 'text', 'name' => '1[name]',
  5392. 'id' => '1-name', 'maxlength' => '255'
  5393. )
  5394. );
  5395. $this->assertTags($result, $expected);
  5396. }
  5397. /**
  5398. * testFormEnd method
  5399. *
  5400. * @return void
  5401. */
  5402. public function testFormEnd() {
  5403. $this->assertEquals('</form>', $this->Form->end());
  5404. }
  5405. /**
  5406. * Test the generation of fields for a multi record form.
  5407. *
  5408. * @return void
  5409. */
  5410. public function testMultiRecordForm() {
  5411. $this->loadFixtures('Article', 'Comment');
  5412. $articles = TableRegistry::get('Articles');
  5413. $articles->hasMany('Comments');
  5414. $comment = new Entity(['comment' => 'Value']);
  5415. $article = new Article(['comments' => [$comment]]);
  5416. $this->Form->create([$article]);
  5417. $result = $this->Form->input('0.comments.1.comment');
  5418. $expected = array(
  5419. 'div' => array('class' => 'input textarea'),
  5420. 'label' => array('for' => '0-comments-1-comment'),
  5421. 'Comment',
  5422. '/label',
  5423. 'textarea' => array(
  5424. 'name',
  5425. 'type',
  5426. 'id' => '0-comments-1-comment',
  5427. ),
  5428. '/textarea',
  5429. '/div'
  5430. );
  5431. $this->assertTags($result, $expected);
  5432. $result = $this->Form->input('0.comments.0.comment');
  5433. $expected = array(
  5434. 'div' => array('class' => 'input textarea'),
  5435. 'label' => array('for' => '0-comments-0-comment'),
  5436. 'Comment',
  5437. '/label',
  5438. 'textarea' => array(
  5439. 'name',
  5440. 'type',
  5441. 'id' => '0-comments-0-comment'
  5442. ),
  5443. 'Value',
  5444. '/textarea',
  5445. '/div'
  5446. );
  5447. $this->assertTags($result, $expected);
  5448. $comment->errors('comment', ['Not valid']);
  5449. $result = $this->Form->input('0.comments.0.comment');
  5450. $expected = array(
  5451. 'div' => array('class' => 'input textarea error'),
  5452. 'label' => array('for' => '0-comments-0-comment'),
  5453. 'Comment',
  5454. '/label',
  5455. 'textarea' => array(
  5456. 'name',
  5457. 'type',
  5458. 'class' => 'form-error',
  5459. 'id' => '0-comments-0-comment'
  5460. ),
  5461. 'Value',
  5462. '/textarea',
  5463. array('div' => array('class' => 'error-message')),
  5464. 'Not valid',
  5465. '/div',
  5466. '/div'
  5467. );
  5468. $this->assertTags($result, $expected);
  5469. TableRegistry::get('Comments')
  5470. ->validator('default')
  5471. ->allowEmpty('comment', false);
  5472. $result = $this->Form->input('0.comments.1.comment');
  5473. $expected = array(
  5474. 'div' => array('class' => 'input textarea required'),
  5475. 'label' => array('for' => '0-comments-1-comment'),
  5476. 'Comment',
  5477. '/label',
  5478. 'textarea' => array(
  5479. 'name',
  5480. 'type',
  5481. 'required' => 'required',
  5482. 'id' => '0-comments-1-comment'
  5483. ),
  5484. '/textarea',
  5485. '/div'
  5486. );
  5487. $this->assertTags($result, $expected);
  5488. }
  5489. /**
  5490. * test that some html5 inputs + FormHelper::__call() work
  5491. *
  5492. * @return void
  5493. */
  5494. public function testHtml5Inputs() {
  5495. $result = $this->Form->email('User.email');
  5496. $expected = array(
  5497. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5498. );
  5499. $this->assertTags($result, $expected);
  5500. $result = $this->Form->search('User.query');
  5501. $expected = array(
  5502. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5503. );
  5504. $this->assertTags($result, $expected);
  5505. $result = $this->Form->search('User.query', array('value' => 'test'));
  5506. $expected = array(
  5507. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5508. );
  5509. $this->assertTags($result, $expected);
  5510. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5511. $expected = array(
  5512. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5513. );
  5514. $this->assertTags($result, $expected);
  5515. }
  5516. /**
  5517. * Test accessing html5 inputs through input().
  5518. *
  5519. * @return void
  5520. */
  5521. public function testHtml5InputWithInput() {
  5522. $this->Form->create();
  5523. $this->Form->templates(['inputContainer' => '{{content}}']);
  5524. $result = $this->Form->input('website', array(
  5525. 'type' => 'url',
  5526. 'val' => 'http://domain.tld',
  5527. 'label' => false
  5528. ));
  5529. $expected = array(
  5530. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5531. );
  5532. $this->assertTags($result, $expected);
  5533. }
  5534. /**
  5535. * Test errors when field name is missing.
  5536. *
  5537. * @expectedException \Cake\Error\Exception
  5538. * @return void
  5539. */
  5540. public function testHtml5InputException() {
  5541. $this->Form->email();
  5542. }
  5543. /**
  5544. * Tests that formhelper sets required attributes.
  5545. *
  5546. * @return void
  5547. */
  5548. public function testRequiredAttribute() {
  5549. $this->article['required'] = [
  5550. 'title' => true,
  5551. 'body' => false,
  5552. ];
  5553. $this->Form->create($this->article);
  5554. $result = $this->Form->input('title');
  5555. $expected = array(
  5556. 'div' => array('class' => 'input text required'),
  5557. 'label' => array('for' => 'title'),
  5558. 'Title',
  5559. '/label',
  5560. 'input' => array(
  5561. 'type' => 'text',
  5562. 'name' => 'title',
  5563. 'id' => 'title',
  5564. 'required' => 'required',
  5565. ),
  5566. '/div'
  5567. );
  5568. $this->assertTags($result, $expected);
  5569. $result = $this->Form->input('title', ['required' => false]);
  5570. $this->assertNotContains('required', $result);
  5571. $result = $this->Form->input('body');
  5572. $expected = array(
  5573. 'div' => array('class' => 'input text'),
  5574. 'label' => array('for' => 'body'),
  5575. 'Body',
  5576. '/label',
  5577. 'input' => array(
  5578. 'type' => 'text',
  5579. 'name' => 'body',
  5580. 'id' => 'body',
  5581. ),
  5582. '/div'
  5583. );
  5584. $this->assertTags($result, $expected);
  5585. $result = $this->Form->input('body', ['required' => true]);
  5586. $this->assertContains('required', $result);
  5587. }
  5588. /**
  5589. * Tests that it is possible to nest inputs inside labels
  5590. *
  5591. * @return void
  5592. */
  5593. public function testNestInputInLabel() {
  5594. $this->Form->templates([
  5595. 'label' => '<label{{attrs}}>{{text}}{{input}}</label>',
  5596. 'formGroup' => '{{label}}'
  5597. ]);
  5598. $result = $this->Form->input('foo');
  5599. $expected = array(
  5600. 'div' => array('class' => 'input text'),
  5601. 'label' => array('for' => 'foo'),
  5602. 'Foo',
  5603. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  5604. '/label',
  5605. '/div'
  5606. );
  5607. $this->assertTags($result, $expected);
  5608. }
  5609. /**
  5610. * Test that *Container templates are used by input.
  5611. *
  5612. * @return void
  5613. */
  5614. public function testInputContainerTemplates() {
  5615. $this->Form->templates([
  5616. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  5617. 'radioContainer' => '<div class="rad">{{content}}</div>',
  5618. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  5619. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  5620. ]);
  5621. $this->article['errors'] = [
  5622. 'Article' => ['published' => 'error message']
  5623. ];
  5624. $this->Form->create($this->article);
  5625. $result = $this->Form->input('accept', [
  5626. 'type' => 'checkbox'
  5627. ]);
  5628. $expected = [
  5629. 'div' => ['class' => 'check'],
  5630. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  5631. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  5632. 'label' => ['for' => 'accept'],
  5633. 'Accept',
  5634. '/label',
  5635. '/div'
  5636. ];
  5637. $this->assertTags($result, $expected);
  5638. $result = $this->Form->input('accept', [
  5639. 'type' => 'radio',
  5640. 'options' => ['Y', 'N']
  5641. ]);
  5642. $this->assertContains('<div class="rad">', $result);
  5643. $result = $this->Form->input('Article.published', [
  5644. 'type' => 'radio',
  5645. 'options' => ['Y', 'N']
  5646. ]);
  5647. $this->assertContains('<div class="rad err">', $result);
  5648. $result = $this->Form->input('Article.created', [
  5649. 'type' => 'datetime',
  5650. ]);
  5651. $this->assertContains('<div class="dt">', $result);
  5652. }
  5653. /**
  5654. * Test resetting templates.
  5655. *
  5656. * @return void
  5657. */
  5658. public function testResetTemplates() {
  5659. $this->Form->templates(['input' => '<input>']);
  5660. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  5661. $this->assertNull($this->Form->resetTemplates());
  5662. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  5663. }
  5664. }