FormHelperTest.php 180 KB

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