FormHelperTest.php 189 KB

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