FormHelperTest.php 187 KB

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