FormHelperTest.php 267 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package cake
  46. * @package Cake.Test.Case.View.Helper
  47. */
  48. class Contact extends CakeTestModel {
  49. /**
  50. * useTable property
  51. *
  52. * @var bool false
  53. */
  54. public $useTable = false;
  55. /**
  56. * Default schema
  57. *
  58. * @var array
  59. */
  60. protected $_schema = array(
  61. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  62. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  67. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  68. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  69. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  70. );
  71. /**
  72. * validate property
  73. *
  74. * @var array
  75. */
  76. public $validate = array(
  77. 'non_existing' => array(),
  78. 'idontexist' => array(),
  79. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  80. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  81. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  82. 'imrequiredonboth' => array(
  83. 'required' => array('rule' => 'alphaNumeric'),
  84. ),
  85. 'string_required' => 'notEmpty',
  86. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  87. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  88. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  89. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  90. 'imalsonotrequired' => array(
  91. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  92. 'between' => array('rule' => array('between', 5, 30)),
  93. ),
  94. 'imalsonotrequired2' => array(
  95. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  96. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  97. ),
  98. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  99. 'iamrequiredalways' => array(
  100. 'email' => array('rule' => 'email'),
  101. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  102. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  103. ),
  104. 'boolean_field' => array('rule' => 'boolean')
  105. );
  106. /**
  107. * schema method
  108. *
  109. * @return void
  110. */
  111. public function setSchema($schema) {
  112. $this->_schema = $schema;
  113. }
  114. /**
  115. * hasAndBelongsToMany property
  116. *
  117. * @var array
  118. */
  119. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  120. /**
  121. * hasAndBelongsToMany property
  122. *
  123. * @var array
  124. */
  125. public $belongsTo = array('User' => array('className' => 'UserForm'));
  126. }
  127. /**
  128. * ContactTagsContact class
  129. *
  130. * @package cake
  131. * @package Cake.Test.Case.View.Helper
  132. */
  133. class ContactTagsContact extends CakeTestModel {
  134. /**
  135. * useTable property
  136. *
  137. * @var bool false
  138. */
  139. public $useTable = false;
  140. /**
  141. * Default schema
  142. *
  143. * @var array
  144. */
  145. protected $_schema = array(
  146. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  147. 'contact_tag_id' => array(
  148. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  149. )
  150. );
  151. /**
  152. * schema method
  153. *
  154. * @return void
  155. */
  156. public function setSchema($schema) {
  157. $this->_schema = $schema;
  158. }
  159. }
  160. /**
  161. * ContactNonStandardPk class
  162. *
  163. * @package cake
  164. * @package Cake.Test.Case.View.Helper
  165. */
  166. class ContactNonStandardPk extends Contact {
  167. /**
  168. * primaryKey property
  169. *
  170. * @var string 'pk'
  171. */
  172. public $primaryKey = 'pk';
  173. /**
  174. * schema method
  175. *
  176. * @return void
  177. */
  178. public function schema($field = false) {
  179. $this->_schema = parent::schema();
  180. $this->_schema['pk'] = $this->_schema['id'];
  181. unset($this->_schema['id']);
  182. return $this->_schema;
  183. }
  184. }
  185. /**
  186. * ContactTag class
  187. *
  188. * @package cake
  189. * @package Cake.Test.Case.View.Helper
  190. */
  191. class ContactTag extends Model {
  192. /**
  193. * useTable property
  194. *
  195. * @var bool false
  196. */
  197. public $useTable = false;
  198. /**
  199. * schema definition
  200. *
  201. * @var array
  202. */
  203. protected $_schema = array(
  204. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  205. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  206. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  207. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  208. );
  209. }
  210. /**
  211. * UserForm class
  212. *
  213. * @package cake
  214. * @package Cake.Test.Case.View.Helper
  215. */
  216. class UserForm extends CakeTestModel {
  217. /**
  218. * useTable property
  219. *
  220. * @var bool false
  221. */
  222. public $useTable = false;
  223. /**
  224. * hasMany property
  225. *
  226. * @var array
  227. */
  228. public $hasMany = array(
  229. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  230. ));
  231. /**
  232. * schema definition
  233. *
  234. * @var array
  235. */
  236. protected $_schema = array(
  237. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  238. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  239. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  240. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  241. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  242. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  243. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  244. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  245. );
  246. }
  247. /**
  248. * OpenidUrl class
  249. *
  250. * @package cake
  251. * @package Cake.Test.Case.View.Helper
  252. */
  253. class OpenidUrl extends CakeTestModel {
  254. /**
  255. * useTable property
  256. *
  257. * @var bool false
  258. */
  259. public $useTable = false;
  260. /**
  261. * belongsTo property
  262. *
  263. * @var array
  264. */
  265. public $belongsTo = array('UserForm' => array(
  266. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  267. ));
  268. /**
  269. * validate property
  270. *
  271. * @var array
  272. */
  273. public $validate = array('openid_not_registered' => array());
  274. /**
  275. * schema method
  276. *
  277. * @var array
  278. */
  279. protected $_schema = array(
  280. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  281. 'user_form_id' => array(
  282. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  283. ),
  284. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  285. );
  286. /**
  287. * beforeValidate method
  288. *
  289. * @return void
  290. */
  291. public function beforeValidate($options = array()) {
  292. $this->invalidate('openid_not_registered');
  293. return true;
  294. }
  295. }
  296. /**
  297. * ValidateUser class
  298. *
  299. * @package cake
  300. * @package Cake.Test.Case.View.Helper
  301. */
  302. class ValidateUser extends CakeTestModel {
  303. /**
  304. * useTable property
  305. *
  306. * @var bool false
  307. */
  308. public $useTable = false;
  309. /**
  310. * hasOne property
  311. *
  312. * @var array
  313. */
  314. public $hasOne = array('ValidateProfile' => array(
  315. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  316. ));
  317. /**
  318. * schema method
  319. *
  320. * @var array
  321. */
  322. protected $_schema = array(
  323. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  324. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  325. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  327. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  328. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('email');
  337. return false;
  338. }
  339. }
  340. /**
  341. * ValidateProfile class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateProfile extends CakeTestModel {
  347. /**
  348. * useTable property
  349. *
  350. * @var bool false
  351. */
  352. public $useTable = false;
  353. /**
  354. * schema property
  355. *
  356. * @var array
  357. */
  358. protected $_schema = array(
  359. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  360. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  362. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  364. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  365. );
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateItem' => array(
  372. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  373. ));
  374. /**
  375. * belongsTo property
  376. *
  377. * @var array
  378. */
  379. public $belongsTo = array('ValidateUser' => array(
  380. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * beforeValidate method
  384. *
  385. * @return void
  386. */
  387. public function beforeValidate($options = array()) {
  388. $this->invalidate('full_name');
  389. $this->invalidate('city');
  390. return false;
  391. }
  392. }
  393. /**
  394. * ValidateItem class
  395. *
  396. * @package cake
  397. * @package Cake.Test.Case.View.Helper
  398. */
  399. class ValidateItem extends CakeTestModel {
  400. /**
  401. * useTable property
  402. *
  403. * @var bool false
  404. */
  405. public $useTable = false;
  406. /**
  407. * schema property
  408. *
  409. * @var array
  410. */
  411. protected $_schema = array(
  412. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  413. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  415. 'description' => array(
  416. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  417. ),
  418. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  419. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  420. );
  421. /**
  422. * belongsTo property
  423. *
  424. * @var array
  425. */
  426. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  427. /**
  428. * beforeValidate method
  429. *
  430. * @return void
  431. */
  432. public function beforeValidate($options = array()) {
  433. $this->invalidate('description');
  434. return false;
  435. }
  436. }
  437. /**
  438. * TestMail class
  439. *
  440. * @package cake
  441. * @package Cake.Test.Case.View.Helper
  442. */
  443. class TestMail extends CakeTestModel {
  444. /**
  445. * useTable property
  446. *
  447. * @var bool false
  448. */
  449. public $useTable = false;
  450. }
  451. /**
  452. * FormHelperTest class
  453. *
  454. * @package cake
  455. * @subpackage Cake.Test.Case.View.Helper
  456. * @property FormHelper $Form
  457. */
  458. class FormHelperTest extends CakeTestCase {
  459. /**
  460. * Fixtures to be used
  461. *
  462. * @var array
  463. */
  464. public $fixtures = array('core.post');
  465. /**
  466. * Do not load the fixtures by default
  467. *
  468. * @var boolean
  469. */
  470. public $autoFixtures = false;
  471. /**
  472. * setUp method
  473. *
  474. * @return void
  475. */
  476. public function setUp() {
  477. parent::setUp();
  478. Configure::write('App.base', '');
  479. $this->Controller = new ContactTestController();
  480. $this->View = new View($this->Controller);
  481. $this->Form = new FormHelper($this->View);
  482. $this->Form->Html = new HtmlHelper($this->View);
  483. $this->Form->request = new CakeRequest('contacts/add', false);
  484. $this->Form->request->here = '/contacts/add';
  485. $this->Form->request['action'] = 'add';
  486. $this->Form->request->webroot = '';
  487. $this->Form->request->base = '';
  488. ClassRegistry::addObject('Contact', new Contact());
  489. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  490. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  491. ClassRegistry::addObject('User', new UserForm());
  492. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  493. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  494. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  495. $this->oldSalt = Configure::read('Security.salt');
  496. $this->dateRegex = array(
  497. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  498. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  499. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  500. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  502. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  503. );
  504. Configure::write('Security.salt', 'foo!');
  505. }
  506. /**
  507. * tearDown method
  508. *
  509. * @return void
  510. */
  511. public function tearDown() {
  512. parent::tearDown();
  513. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  514. Configure::write('Security.salt', $this->oldSalt);
  515. }
  516. /**
  517. * testFormCreateWithSecurity method
  518. *
  519. * Test form->create() with security key.
  520. *
  521. * @return void
  522. */
  523. public function testCreateWithSecurity() {
  524. $this->Form->request['_Token'] = array('key' => 'testKey');
  525. $encoding = strtolower(Configure::read('App.encoding'));
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  527. $expected = array(
  528. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  529. 'div' => array('style' => 'display:none;'),
  530. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  531. array('input' => array(
  532. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  533. )),
  534. '/div'
  535. );
  536. $this->assertTags($result, $expected);
  537. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  538. $expected['form']['id'] = 'MyForm';
  539. $this->assertTags($result, $expected);
  540. }
  541. /**
  542. * testFormCreateGetNoSecurity method
  543. *
  544. * Test form->create() with no security key as its a get form
  545. *
  546. * @return void
  547. */
  548. public function testCreateEndGetNoSecurity() {
  549. $this->Form->request['_Token'] = array('key' => 'testKey');
  550. $encoding = strtolower(Configure::read('App.encoding'));
  551. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  552. $this->assertNotContains('Token', $result);
  553. $result = $this->Form->end('Save');
  554. $this->assertNotContains('Token', $result);
  555. }
  556. /**
  557. * test that create() clears the fields property so it starts fresh
  558. *
  559. * @return void
  560. */
  561. public function testCreateClearingFields() {
  562. $this->Form->fields = array('model_id');
  563. $this->Form->create('Contact');
  564. $this->assertEquals(array(), $this->Form->fields);
  565. }
  566. /**
  567. * Tests form hash generation with model-less data
  568. *
  569. * @return void
  570. */
  571. public function testValidateHashNoModel() {
  572. $this->Form->request['_Token'] = array('key' => 'foo');
  573. $result = $this->Form->secure(array('anything'));
  574. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  575. }
  576. /**
  577. * Tests that models with identical field names get resolved properly
  578. *
  579. * @return void
  580. */
  581. public function testDuplicateFieldNameResolution() {
  582. $result = $this->Form->create('ValidateUser');
  583. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  584. $result = $this->Form->input('ValidateItem.name');
  585. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  586. $result = $this->Form->input('ValidateUser.name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. $result = $this->Form->input('ValidateItem.name');
  591. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  592. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  593. $this->assertRegExp('/<textarea/', $result);
  594. $result = $this->Form->input('name');
  595. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  596. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  597. $this->assertRegExp('/type="text"/', $result);
  598. }
  599. /**
  600. * Tests that hidden fields generated for checkboxes don't get locked
  601. *
  602. * @return void
  603. */
  604. public function testNoCheckboxLocking() {
  605. $this->Form->request['_Token'] = array('key' => 'foo');
  606. $this->assertSame(array(), $this->Form->fields);
  607. $this->Form->checkbox('check', array('value' => '1'));
  608. $this->assertSame($this->Form->fields, array('check'));
  609. }
  610. /**
  611. * testFormSecurityFields method
  612. *
  613. * Test generation of secure form hash generation.
  614. *
  615. * @return void
  616. */
  617. public function testFormSecurityFields() {
  618. $key = 'testKey';
  619. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  620. $this->Form->request['_Token'] = array('key' => $key);
  621. $result = $this->Form->secure($fields);
  622. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  623. $hash .= ':' . 'Model.valid';
  624. $hash = urlencode($hash);
  625. $expected = array(
  626. 'div' => array('style' => 'display:none;'),
  627. array('input' => array(
  628. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  629. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  630. )),
  631. array('input' => array(
  632. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  633. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  639. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  640. $result = $this->Form->secure($fields);
  641. $expected = array(
  642. 'div' => array('class' => 'hidden'),
  643. array('input' => array(
  644. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  645. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  646. )),
  647. array('input' => array(
  648. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  649. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  650. )),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Tests correct generation of number fields for double and float fields
  657. *
  658. * @return void
  659. */
  660. public function testTextFieldGenerationForFloats() {
  661. $model = ClassRegistry::getObject('Contact');
  662. $model->setSchema(array('foo' => array(
  663. 'type' => 'float',
  664. 'null' => false,
  665. 'default' => null,
  666. 'length' => null
  667. )));
  668. $this->Form->create('Contact');
  669. $result = $this->Form->input('foo');
  670. $expected = array(
  671. 'div' => array('class' => 'input number'),
  672. 'label' => array('for' => 'ContactFoo'),
  673. 'Foo',
  674. '/label',
  675. array('input' => array(
  676. 'type' => 'number',
  677. 'name' => 'data[Contact][foo]',
  678. 'id' => 'ContactFoo',
  679. 'step' => 'any'
  680. )),
  681. '/div'
  682. );
  683. $this->assertTags($result, $expected);
  684. $result = $this->Form->input('foo', array('step' => 0.5));
  685. $expected = array(
  686. 'div' => array('class' => 'input number'),
  687. 'label' => array('for' => 'ContactFoo'),
  688. 'Foo',
  689. '/label',
  690. array('input' => array(
  691. 'type' => 'number',
  692. 'name' => 'data[Contact][foo]',
  693. 'id' => 'ContactFoo',
  694. 'step' => '0.5'
  695. )),
  696. '/div'
  697. );
  698. $this->assertTags($result, $expected);
  699. }
  700. /**
  701. * Tests correct generation of number fields for integer fields
  702. *
  703. * @access public
  704. * @return void
  705. */
  706. public function testTextFieldTypeNumberGenerationForIntegers() {
  707. $model = ClassRegistry::getObject('Contact');
  708. $model->setSchema(array('foo' => array(
  709. 'type' => 'integer',
  710. 'null' => false,
  711. 'default' => null,
  712. 'length' => null
  713. )));
  714. $this->Form->create('Contact');
  715. $result = $this->Form->input('foo');
  716. $expected = array(
  717. 'div' => array('class' => 'input number'),
  718. 'label' => array('for' => 'ContactFoo'),
  719. 'Foo',
  720. '/label',
  721. array('input' => array(
  722. 'type' => 'number', 'name' => 'data[Contact][foo]',
  723. 'id' => 'ContactFoo'
  724. )),
  725. '/div'
  726. );
  727. $this->assertTags($result, $expected);
  728. }
  729. /**
  730. * testFormSecurityMultipleFields method
  731. *
  732. * Test secure() with multiple row form. Ensure hash is correct.
  733. *
  734. * @return void
  735. */
  736. public function testFormSecurityMultipleFields() {
  737. $key = 'testKey';
  738. $fields = array(
  739. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  740. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  741. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  742. );
  743. $this->Form->request['_Token'] = array('key' => $key);
  744. $result = $this->Form->secure($fields);
  745. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  746. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  747. $expected = array(
  748. 'div' => array('style' => 'display:none;'),
  749. array('input' => array(
  750. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  751. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  752. )),
  753. array('input' => array(
  754. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  755. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  756. )),
  757. '/div'
  758. );
  759. $this->assertTags($result, $expected);
  760. }
  761. /**
  762. * testFormSecurityMultipleSubmitButtons
  763. *
  764. * test form submit generation and ensure that _Token is only created on end()
  765. *
  766. * @return void
  767. */
  768. public function testFormSecurityMultipleSubmitButtons() {
  769. $key = 'testKey';
  770. $this->Form->request['_Token'] = array('key' => $key);
  771. $this->Form->create('Addresses');
  772. $this->Form->input('Address.title');
  773. $this->Form->input('Address.first_name');
  774. $result = $this->Form->submit('Save', array('name' => 'save'));
  775. $expected = array(
  776. 'div' => array('class' => 'submit'),
  777. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  778. '/div',
  779. );
  780. $this->assertTags($result, $expected);
  781. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  782. $expected = array(
  783. 'div' => array('class' => 'submit'),
  784. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  785. '/div',
  786. );
  787. $this->assertTags($result, $expected);
  788. $result = $this->Form->end(null);
  789. $expected = array(
  790. 'div' => array('style' => 'display:none;'),
  791. array('input' => array(
  792. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  793. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  794. )),
  795. array('input' => array(
  796. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  797. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  798. )),
  799. '/div'
  800. );
  801. $this->assertTags($result, $expected);
  802. }
  803. /**
  804. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  805. *
  806. * @return void
  807. */
  808. public function testSecurityButtonNestedNamed() {
  809. $key = 'testKey';
  810. $this->Form->request['_Token'] = array('key' => $key);
  811. $this->Form->create('Addresses');
  812. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  813. $result = $this->Form->unlockField();
  814. $this->assertEquals(array('Address.button'), $result);
  815. }
  816. /**
  817. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  818. *
  819. * @return void
  820. */
  821. public function testSecuritySubmitNestedNamed() {
  822. $key = 'testKey';
  823. $this->Form->request['_Token'] = array('key' => $key);
  824. $this->Form->create('Addresses');
  825. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  826. $result = $this->Form->unlockField();
  827. $this->assertEquals(array('Address.button'), $result);
  828. }
  829. /**
  830. * Test that the correct fields are unlocked for image submits with no names.
  831. *
  832. * @return void
  833. */
  834. public function testSecuritySubmitImageNoName() {
  835. $key = 'testKey';
  836. $this->Form->request['_Token'] = array('key' => $key);
  837. $this->Form->create('User');
  838. $result = $this->Form->submit('save.png');
  839. $expected = array(
  840. 'div' => array('class' => 'submit'),
  841. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  842. '/div'
  843. );
  844. $this->assertTags($result, $expected);
  845. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  846. }
  847. /**
  848. * Test that the correct fields are unlocked for image submits with names.
  849. *
  850. * @return void
  851. */
  852. public function testSecuritySubmitImageName() {
  853. $key = 'testKey';
  854. $this->Form->request['_Token'] = array('key' => $key);
  855. $this->Form->create('User');
  856. $result = $this->Form->submit('save.png', array('name' => 'test'));
  857. $expected = array(
  858. 'div' => array('class' => 'submit'),
  859. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  860. '/div'
  861. );
  862. $this->assertTags($result, $expected);
  863. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  864. }
  865. /**
  866. * testFormSecurityMultipleInputFields method
  867. *
  868. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  869. *
  870. * @return void
  871. */
  872. public function testFormSecurityMultipleInputFields() {
  873. $key = 'testKey';
  874. $this->Form->request['_Token'] = array('key' => $key);
  875. $this->Form->create('Addresses');
  876. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  877. $this->Form->input('Addresses.0.title');
  878. $this->Form->input('Addresses.0.first_name');
  879. $this->Form->input('Addresses.0.last_name');
  880. $this->Form->input('Addresses.0.address');
  881. $this->Form->input('Addresses.0.city');
  882. $this->Form->input('Addresses.0.phone');
  883. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  884. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  885. $this->Form->input('Addresses.1.title');
  886. $this->Form->input('Addresses.1.first_name');
  887. $this->Form->input('Addresses.1.last_name');
  888. $this->Form->input('Addresses.1.address');
  889. $this->Form->input('Addresses.1.city');
  890. $this->Form->input('Addresses.1.phone');
  891. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  892. $result = $this->Form->secure($this->Form->fields);
  893. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  894. $expected = array(
  895. 'div' => array('style' => 'display:none;'),
  896. array('input' => array(
  897. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  898. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  899. )),
  900. array('input' => array(
  901. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  902. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  903. )),
  904. '/div'
  905. );
  906. $this->assertTags($result, $expected);
  907. }
  908. /**
  909. * Test form security with Model.field.0 style inputs
  910. *
  911. * @return void
  912. */
  913. public function testFormSecurityArrayFields() {
  914. $key = 'testKey';
  915. $this->Form->request->params['_Token']['key'] = $key;
  916. $this->Form->create('Address');
  917. $this->Form->input('Address.primary.1');
  918. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  919. $this->Form->input('Address.secondary.1.0');
  920. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  921. }
  922. /**
  923. * testFormSecurityMultipleInputDisabledFields method
  924. *
  925. * test secure form generation with multiple records and disabled fields.
  926. *
  927. * @return void
  928. */
  929. public function testFormSecurityMultipleInputDisabledFields() {
  930. $key = 'testKey';
  931. $this->Form->request->params['_Token'] = array(
  932. 'key' => $key,
  933. 'unlockedFields' => array('first_name', 'address')
  934. );
  935. $this->Form->create();
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  944. $this->Form->input('Addresses.1.title');
  945. $this->Form->input('Addresses.1.first_name');
  946. $this->Form->input('Addresses.1.last_name');
  947. $this->Form->input('Addresses.1.address');
  948. $this->Form->input('Addresses.1.city');
  949. $this->Form->input('Addresses.1.phone');
  950. $result = $this->Form->secure($this->Form->fields);
  951. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  952. $expected = array(
  953. 'div' => array('style' => 'display:none;'),
  954. array('input' => array(
  955. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  956. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  957. )),
  958. array('input' => array(
  959. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  960. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  961. )),
  962. '/div'
  963. );
  964. $this->assertTags($result, $expected);
  965. }
  966. /**
  967. * testFormSecurityInputDisabledFields method
  968. *
  969. * Test single record form with disabled fields.
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityInputUnlockedFields() {
  974. $key = 'testKey';
  975. $this->Form->request['_Token'] = array(
  976. 'key' => $key,
  977. 'unlockedFields' => array('first_name', 'address')
  978. );
  979. $this->Form->create();
  980. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  981. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  982. $this->Form->input('Addresses.title');
  983. $this->Form->input('Addresses.first_name');
  984. $this->Form->input('Addresses.last_name');
  985. $this->Form->input('Addresses.address');
  986. $this->Form->input('Addresses.city');
  987. $this->Form->input('Addresses.phone');
  988. $result = $this->Form->fields;
  989. $expected = array(
  990. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  991. 'Addresses.city', 'Addresses.phone'
  992. );
  993. $this->assertEquals($expected, $result);
  994. $result = $this->Form->secure($expected);
  995. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  996. $expected = array(
  997. 'div' => array('style' => 'display:none;'),
  998. array('input' => array(
  999. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1000. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1001. )),
  1002. array('input' => array(
  1003. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1004. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1005. )),
  1006. '/div'
  1007. );
  1008. $this->assertTags($result, $expected);
  1009. }
  1010. /**
  1011. * test securing inputs with custom name attributes.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecureWithCustomNameAttribute() {
  1016. $this->Form->request->params['_Token']['key'] = 'testKey';
  1017. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1018. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1019. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1020. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1021. }
  1022. /**
  1023. * testFormSecuredInput method
  1024. *
  1025. * Test generation of entire secure form, assertions made on input() output.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testFormSecuredInput() {
  1030. $this->Form->request['_Token'] = array('key' => 'testKey');
  1031. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1032. $encoding = strtolower(Configure::read('App.encoding'));
  1033. $expected = array(
  1034. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1035. 'div' => array('style' => 'display:none;'),
  1036. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1037. array('input' => array(
  1038. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1039. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1040. )),
  1041. '/div'
  1042. );
  1043. $this->assertTags($result, $expected);
  1044. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1045. $expected = array(
  1046. 'div' => array('class' => 'input text'),
  1047. 'label' => array('for' => 'UserFormPublished'),
  1048. 'Published',
  1049. '/label',
  1050. array('input' => array(
  1051. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1052. 'id' => 'UserFormPublished'
  1053. )),
  1054. '/div'
  1055. );
  1056. $this->assertTags($result, $expected);
  1057. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1058. $expected = array(
  1059. 'div' => array('class' => 'input text'),
  1060. 'label' => array('for' => 'UserFormOther'),
  1061. 'Other',
  1062. '/label',
  1063. array('input' => array(
  1064. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1065. 'id' => 'UserFormOther'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. $result = $this->Form->hidden('UserForm.stuff');
  1071. $expected = array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1073. 'id' => 'UserFormStuff'
  1074. ));
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1077. $expected = array('input' => array(
  1078. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1079. 'value' => '0', 'id' => 'UserFormHidden'
  1080. ));
  1081. $this->assertTags($result, $expected);
  1082. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1083. $expected = array(
  1084. 'div' => array('class' => 'input checkbox'),
  1085. array('input' => array(
  1086. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1087. 'value' => '0', 'id' => 'UserFormSomething_'
  1088. )),
  1089. array('input' => array(
  1090. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1091. 'value' => '1', 'id' => 'UserFormSomething'
  1092. )),
  1093. 'label' => array('for' => 'UserFormSomething'),
  1094. 'Something',
  1095. '/label',
  1096. '/div'
  1097. );
  1098. $this->assertTags($result, $expected);
  1099. $result = $this->Form->fields;
  1100. $expected = array(
  1101. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1102. 'UserForm.hidden' => '0', 'UserForm.something'
  1103. );
  1104. $this->assertEquals($expected, $result);
  1105. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1106. $result = $this->Form->secure($this->Form->fields);
  1107. $expected = array(
  1108. 'div' => array('style' => 'display:none;'),
  1109. array('input' => array(
  1110. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1111. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1112. )),
  1113. array('input' => array(
  1114. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1115. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1116. )),
  1117. '/div'
  1118. );
  1119. $this->assertTags($result, $expected);
  1120. }
  1121. /**
  1122. * Tests that the correct keys are added to the field hash index
  1123. *
  1124. * @return void
  1125. */
  1126. public function testFormSecuredFileInput() {
  1127. $this->Form->request['_Token'] = array('key' => 'testKey');
  1128. $this->assertEquals(array(), $this->Form->fields);
  1129. $this->Form->file('Attachment.file');
  1130. $expected = array(
  1131. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1132. 'Attachment.file.error', 'Attachment.file.size'
  1133. );
  1134. $this->assertEquals($expected, $this->Form->fields);
  1135. }
  1136. /**
  1137. * test that multiple selects keys are added to field hash
  1138. *
  1139. * @return void
  1140. */
  1141. public function testFormSecuredMultipleSelect() {
  1142. $this->Form->request['_Token'] = array('key' => 'testKey');
  1143. $this->assertEquals(array(), $this->Form->fields);
  1144. $options = array('1' => 'one', '2' => 'two');
  1145. $this->Form->select('Model.select', $options);
  1146. $expected = array('Model.select');
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. $this->Form->fields = array();
  1149. $this->Form->select('Model.select', $options, array('multiple' => true));
  1150. $this->assertEquals($expected, $this->Form->fields);
  1151. }
  1152. /**
  1153. * testFormSecuredRadio method
  1154. *
  1155. * @return void
  1156. */
  1157. public function testFormSecuredRadio() {
  1158. $this->Form->request['_Token'] = array('key' => 'testKey');
  1159. $this->assertEquals(array(), $this->Form->fields);
  1160. $options = array('1' => 'option1', '2' => 'option2');
  1161. $this->Form->radio('Test.test', $options);
  1162. $expected = array('Test.test');
  1163. $this->assertEquals($expected, $this->Form->fields);
  1164. }
  1165. /**
  1166. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1167. * hashing.
  1168. *
  1169. * @return void
  1170. */
  1171. public function testFormSecuredAndDisabled() {
  1172. $this->Form->request['_Token'] = array('key' => 'testKey');
  1173. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1174. $this->Form->text('Model.text', array('disabled' => true));
  1175. $this->Form->password('Model.text', array('disabled' => true));
  1176. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1177. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1178. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1179. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1180. $this->Form->month('Model.month', array('disabled' => true));
  1181. $this->Form->day('Model.day', array('disabled' => true));
  1182. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1183. $this->Form->minute('Model.minute', array('disabled' => true));
  1184. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1185. $expected = array(
  1186. 'Model.radio' => ''
  1187. );
  1188. $this->assertEquals($expected, $this->Form->fields);
  1189. }
  1190. /**
  1191. * testDisableSecurityUsingForm method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testDisableSecurityUsingForm() {
  1196. $this->Form->request['_Token'] = array(
  1197. 'key' => 'testKey',
  1198. 'disabledFields' => array()
  1199. );
  1200. $this->Form->create();
  1201. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1202. $this->Form->input('Addresses.title');
  1203. $this->Form->input('Addresses.first_name', array('secure' => false));
  1204. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1205. $this->Form->input('Addresses.zip', array(
  1206. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1207. ));
  1208. $result = $this->Form->fields;
  1209. $expected = array(
  1210. 'Addresses.id' => '123456', 'Addresses.title',
  1211. );
  1212. $this->assertEquals($expected, $result);
  1213. }
  1214. /**
  1215. * test disableField
  1216. *
  1217. * @return void
  1218. */
  1219. public function testUnlockFieldAddsToList() {
  1220. $this->Form->request['_Token'] = array(
  1221. 'key' => 'testKey',
  1222. 'unlockedFields' => array()
  1223. );
  1224. $this->Form->create('Contact');
  1225. $this->Form->unlockField('Contact.name');
  1226. $this->Form->text('Contact.name');
  1227. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. }
  1230. /**
  1231. * test unlockField removing from fields array.
  1232. *
  1233. * @return void
  1234. */
  1235. public function testUnlockFieldRemovingFromFields() {
  1236. $this->Form->request['_Token'] = array(
  1237. 'key' => 'testKey',
  1238. 'unlockedFields' => array()
  1239. );
  1240. $this->Form->create('Contact');
  1241. $this->Form->hidden('Contact.id', array('value' => 1));
  1242. $this->Form->text('Contact.name');
  1243. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1244. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1245. $this->Form->unlockField('Contact.name');
  1246. $this->Form->unlockField('Contact.id');
  1247. $this->assertEquals(array(), $this->Form->fields);
  1248. }
  1249. /**
  1250. * testTagIsInvalid method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testTagIsInvalid() {
  1255. $Contact = ClassRegistry::getObject('Contact');
  1256. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1257. $this->Form->setEntity('Contact.0.email');
  1258. $result = $this->Form->tagIsInvalid();
  1259. $expected = array('Please provide an email');
  1260. $this->assertEquals($expected, $result);
  1261. $this->Form->setEntity('Contact.1.email');
  1262. $result = $this->Form->tagIsInvalid();
  1263. $expected = false;
  1264. $this->assertSame($expected, $result);
  1265. $this->Form->setEntity('Contact.0.name');
  1266. $result = $this->Form->tagIsInvalid();
  1267. $expected = false;
  1268. $this->assertSame($expected, $result);
  1269. }
  1270. /**
  1271. * Test validation errors.
  1272. *
  1273. * @return void
  1274. */
  1275. public function testPasswordValidation() {
  1276. $Contact = ClassRegistry::getObject('Contact');
  1277. $Contact->validationErrors['password'] = array('Please provide a password');
  1278. $result = $this->Form->input('Contact.password');
  1279. $expected = array(
  1280. 'div' => array('class' => 'input password error'),
  1281. 'label' => array('for' => 'ContactPassword'),
  1282. 'Password',
  1283. '/label',
  1284. 'input' => array(
  1285. 'type' => 'password', 'name' => 'data[Contact][password]',
  1286. 'id' => 'ContactPassword', 'class' => 'form-error'
  1287. ),
  1288. array('div' => array('class' => 'error-message')),
  1289. 'Please provide a password',
  1290. '/div',
  1291. '/div'
  1292. );
  1293. $this->assertTags($result, $expected);
  1294. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1295. $expected = array(
  1296. 'div' => array('class' => 'input password error'),
  1297. 'label' => array('for' => 'ContactPassword'),
  1298. 'Password',
  1299. '/label',
  1300. 'input' => array(
  1301. 'type' => 'password', 'name' => 'data[Contact][password]',
  1302. 'id' => 'ContactPassword', 'class' => 'form-error'
  1303. ),
  1304. '/div'
  1305. );
  1306. $this->assertTags($result, $expected);
  1307. }
  1308. /**
  1309. * Test validation errors, when validation message is an empty string.
  1310. *
  1311. * @access public
  1312. * @return void
  1313. */
  1314. public function testEmptyErrorValidation() {
  1315. $this->Form->validationErrors['Contact']['password'] = '';
  1316. $result = $this->Form->input('Contact.password');
  1317. $expected = array(
  1318. 'div' => array('class' => 'input password error'),
  1319. 'label' => array('for' => 'ContactPassword'),
  1320. 'Password',
  1321. '/label',
  1322. 'input' => array(
  1323. 'type' => 'password', 'name' => 'data[Contact][password]',
  1324. 'id' => 'ContactPassword', 'class' => 'form-error'
  1325. ),
  1326. array('div' => array('class' => 'error-message')),
  1327. array(),
  1328. '/div',
  1329. '/div'
  1330. );
  1331. $this->assertTags($result, $expected);
  1332. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1333. $expected = array(
  1334. 'div' => array('class' => 'input password error'),
  1335. 'label' => array('for' => 'ContactPassword'),
  1336. 'Password',
  1337. '/label',
  1338. 'input' => array(
  1339. 'type' => 'password', 'name' => 'data[Contact][password]',
  1340. 'id' => 'ContactPassword', 'class' => 'form-error'
  1341. ),
  1342. '/div'
  1343. );
  1344. $this->assertTags($result, $expected);
  1345. }
  1346. /**
  1347. * Test validation errors, when calling input() overriding validation message by an empty string.
  1348. *
  1349. * @access public
  1350. * @return void
  1351. */
  1352. public function testEmptyInputErrorValidation() {
  1353. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1354. $result = $this->Form->input('Contact.password', array('error' => ''));
  1355. $expected = array(
  1356. 'div' => array('class' => 'input password error'),
  1357. 'label' => array('for' => 'ContactPassword'),
  1358. 'Password',
  1359. '/label',
  1360. 'input' => array(
  1361. 'type' => 'password', 'name' => 'data[Contact][password]',
  1362. 'id' => 'ContactPassword', 'class' => 'form-error'
  1363. ),
  1364. array('div' => array('class' => 'error-message')),
  1365. array(),
  1366. '/div',
  1367. '/div'
  1368. );
  1369. $this->assertTags($result, $expected);
  1370. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1371. $expected = array(
  1372. 'div' => array('class' => 'input password error'),
  1373. 'label' => array('for' => 'ContactPassword'),
  1374. 'Password',
  1375. '/label',
  1376. 'input' => array(
  1377. 'type' => 'password', 'name' => 'data[Contact][password]',
  1378. 'id' => 'ContactPassword', 'class' => 'form-error'
  1379. ),
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testFormValidationAssociated method
  1386. *
  1387. * test display of form errors in conjunction with model::validates.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testFormValidationAssociated() {
  1392. $this->UserForm = ClassRegistry::getObject('UserForm');
  1393. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1394. $data = array(
  1395. 'UserForm' => array('name' => 'user'),
  1396. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1397. );
  1398. $result = $this->UserForm->OpenidUrl->create($data);
  1399. $this->assertFalse(empty($result));
  1400. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1401. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1402. $encoding = strtolower(Configure::read('App.encoding'));
  1403. $expected = array(
  1404. 'form' => array(
  1405. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1406. 'accept-charset' => $encoding
  1407. ),
  1408. 'div' => array('style' => 'display:none;'),
  1409. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1410. '/div'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->error(
  1414. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1415. );
  1416. $this->assertEquals('Error, not registered', $result);
  1417. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1418. }
  1419. /**
  1420. * testFormValidationAssociatedFirstLevel method
  1421. *
  1422. * test form error display with associated model.
  1423. *
  1424. * @return void
  1425. */
  1426. public function testFormValidationAssociatedFirstLevel() {
  1427. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1428. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1429. $data = array(
  1430. 'ValidateUser' => array('name' => 'mariano'),
  1431. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1432. );
  1433. $result = $this->ValidateUser->create($data);
  1434. $this->assertFalse(empty($result));
  1435. $this->assertFalse($this->ValidateUser->validates());
  1436. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1437. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1441. 'div' => array('style' => 'display:none;'),
  1442. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1443. '/div'
  1444. );
  1445. $this->assertTags($result, $expected);
  1446. $result = $this->Form->error(
  1447. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1448. );
  1449. $this->assertEquals('Invalid email', $result);
  1450. $result = $this->Form->error(
  1451. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1452. );
  1453. $this->assertEquals('Invalid name', $result);
  1454. $result = $this->Form->error(
  1455. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Invalid city', $result);
  1458. unset($this->ValidateUser->ValidateProfile);
  1459. unset($this->ValidateUser);
  1460. }
  1461. /**
  1462. * testFormValidationAssociatedSecondLevel method
  1463. *
  1464. * test form error display with associated model.
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormValidationAssociatedSecondLevel() {
  1469. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1470. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1471. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1472. $data = array(
  1473. 'ValidateUser' => array('name' => 'mariano'),
  1474. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1475. 'ValidateItem' => array('name' => 'Item')
  1476. );
  1477. $result = $this->ValidateUser->create($data);
  1478. $this->assertFalse(empty($result));
  1479. $this->assertFalse($this->ValidateUser->validates());
  1480. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1481. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1482. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1483. $encoding = strtolower(Configure::read('App.encoding'));
  1484. $expected = array(
  1485. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1486. 'div' => array('style' => 'display:none;'),
  1487. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1488. '/div'
  1489. );
  1490. $this->assertTags($result, $expected);
  1491. $result = $this->Form->error(
  1492. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid email', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid name', $result);
  1499. $result = $this->Form->error(
  1500. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1501. );
  1502. $result = $this->Form->error(
  1503. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1504. );
  1505. $this->assertEquals('Invalid description', $result);
  1506. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1507. unset($this->ValidateUser->ValidateProfile);
  1508. unset($this->ValidateUser);
  1509. }
  1510. /**
  1511. * testFormValidationMultiRecord method
  1512. *
  1513. * test form error display with multiple records.
  1514. *
  1515. * @return void
  1516. */
  1517. public function testFormValidationMultiRecord() {
  1518. $Contact = ClassRegistry::getObject('Contact');
  1519. $Contact->validationErrors[2] = array(
  1520. 'name' => array('This field cannot be left blank')
  1521. );
  1522. $result = $this->Form->input('Contact.2.name');
  1523. $expected = array(
  1524. 'div' => array('class' => 'input text error'),
  1525. 'label' => array('for' => 'Contact2Name'),
  1526. 'Name',
  1527. '/label',
  1528. 'input' => array(
  1529. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1530. 'class' => 'form-error', 'maxlength' => 255
  1531. ),
  1532. array('div' => array('class' => 'error-message')),
  1533. 'This field cannot be left blank',
  1534. '/div',
  1535. '/div'
  1536. );
  1537. $this->assertTags($result, $expected);
  1538. }
  1539. /**
  1540. * testMultipleInputValidation method
  1541. *
  1542. * test multiple record form validation error display.
  1543. *
  1544. * @return void
  1545. */
  1546. public function testMultipleInputValidation() {
  1547. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1548. $Address->validationErrors[0] = array(
  1549. 'title' => array('This field cannot be empty'),
  1550. 'first_name' => array('This field cannot be empty')
  1551. );
  1552. $Address->validationErrors[1] = array(
  1553. 'last_name' => array('You must have a last name')
  1554. );
  1555. $this->Form->create();
  1556. $result = $this->Form->input('Address.0.title');
  1557. $expected = array(
  1558. 'div' => array('class'),
  1559. 'label' => array('for'),
  1560. 'preg:/[^<]+/',
  1561. '/label',
  1562. 'input' => array(
  1563. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1564. ),
  1565. array('div' => array('class' => 'error-message')),
  1566. 'This field cannot be empty',
  1567. '/div',
  1568. '/div'
  1569. );
  1570. $this->assertTags($result, $expected);
  1571. $result = $this->Form->input('Address.0.first_name');
  1572. $expected = array(
  1573. 'div' => array('class'),
  1574. 'label' => array('for'),
  1575. 'preg:/[^<]+/',
  1576. '/label',
  1577. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1578. array('div' => array('class' => 'error-message')),
  1579. 'This field cannot be empty',
  1580. '/div',
  1581. '/div'
  1582. );
  1583. $this->assertTags($result, $expected);
  1584. $result = $this->Form->input('Address.0.last_name');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'preg:/[^<]+/',
  1589. '/label',
  1590. 'input' => array('type' => 'text', 'name', 'id'),
  1591. '/div'
  1592. );
  1593. $this->assertTags($result, $expected);
  1594. $result = $this->Form->input('Address.1.last_name');
  1595. $expected = array(
  1596. 'div' => array('class'),
  1597. 'label' => array('for'),
  1598. 'preg:/[^<]+/',
  1599. '/label',
  1600. 'input' => array(
  1601. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1602. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'You must have a last name',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. }
  1611. /**
  1612. * testInput method
  1613. *
  1614. * Test various incarnations of input().
  1615. *
  1616. * @return void
  1617. */
  1618. public function testInput() {
  1619. $result = $this->Form->input('ValidateUser.balance');
  1620. $expected = array(
  1621. 'div' => array('class'),
  1622. 'label' => array('for'),
  1623. 'Balance',
  1624. '/label',
  1625. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1626. '/div',
  1627. );
  1628. $this->assertTags($result, $expected);
  1629. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1630. $expected = array(
  1631. 'div' => array('class' => 'input email'),
  1632. 'label' => array('for' => 'custom'),
  1633. 'Email',
  1634. '/label',
  1635. array('input' => array(
  1636. 'type' => 'email', 'name' => 'data[Contact][email]',
  1637. 'id' => 'custom', 'maxlength' => 255
  1638. )),
  1639. '/div'
  1640. );
  1641. $this->assertTags($result, $expected);
  1642. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1643. $expected = array(
  1644. '<div',
  1645. 'label' => array('for' => 'ContactEmail'),
  1646. 'Email',
  1647. '/label',
  1648. array('input' => array(
  1649. 'type' => 'email', 'name' => 'data[Contact][email]',
  1650. 'id' => 'ContactEmail', 'maxlength' => 255
  1651. )),
  1652. '/div'
  1653. );
  1654. $this->assertTags($result, $expected);
  1655. $result = $this->Form->hidden('Contact.idontexist');
  1656. $expected = array('input' => array(
  1657. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1658. 'id' => 'ContactIdontexist'
  1659. ));
  1660. $this->assertTags($result, $expected);
  1661. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1662. $expected = array(
  1663. 'div' => array('class' => 'input text'),
  1664. 'label' => array('for' => 'ContactEmail'),
  1665. 'Email',
  1666. '/label',
  1667. array('input' => array(
  1668. 'type' => 'text', 'name' => 'data[Contact][email]',
  1669. 'id' => 'ContactEmail'
  1670. )),
  1671. '/div'
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1675. $expected = array(
  1676. 'div' => array('class' => 'input text'),
  1677. 'label' => array('for' => 'Contact5Email'),
  1678. 'Email',
  1679. '/label',
  1680. array('input' => array(
  1681. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1682. 'id' => 'Contact5Email'
  1683. )),
  1684. '/div'
  1685. );
  1686. $this->assertTags($result, $expected);
  1687. $result = $this->Form->input('Contact.password');
  1688. $expected = array(
  1689. 'div' => array('class' => 'input password'),
  1690. 'label' => array('for' => 'ContactPassword'),
  1691. 'Password',
  1692. '/label',
  1693. array('input' => array(
  1694. 'type' => 'password', 'name' => 'data[Contact][password]',
  1695. 'id' => 'ContactPassword'
  1696. )),
  1697. '/div'
  1698. );
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array(
  1701. 'type' => 'file', 'class' => 'textbox'
  1702. ));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input file'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1716. $result = $this->Form->input('Contact.phone');
  1717. $expected = array(
  1718. 'div' => array('class' => 'input tel'),
  1719. 'label' => array('for' => 'ContactPhone'),
  1720. 'Phone',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1724. 'value' => 'Hello &amp; World &gt; weird chars',
  1725. 'id' => 'ContactPhone', 'maxlength' => 255
  1726. )),
  1727. '/div'
  1728. );
  1729. $this->assertTags($result, $expected);
  1730. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1731. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1732. $expected = array(
  1733. 'div' => array('class' => 'input text'),
  1734. 'label' => array('for' => 'myId'),
  1735. 'Field',
  1736. '/label',
  1737. 'input' => array(
  1738. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1739. 'value' => 'My value', 'id' => 'myId'
  1740. ),
  1741. '/div'
  1742. );
  1743. $this->assertTags($result, $expected);
  1744. unset($this->Form->request->data);
  1745. $Contact = ClassRegistry::getObject('Contact');
  1746. $Contact->validationErrors['field'] = array('Badness!');
  1747. $result = $this->Form->input('Contact.field');
  1748. $expected = array(
  1749. 'div' => array('class' => 'input text error'),
  1750. 'label' => array('for' => 'ContactField'),
  1751. 'Field',
  1752. '/label',
  1753. 'input' => array(
  1754. 'type' => 'text', 'name' => 'data[Contact][field]',
  1755. 'id' => 'ContactField', 'class' => 'form-error'
  1756. ),
  1757. array('div' => array('class' => 'error-message')),
  1758. 'Badness!',
  1759. '/div',
  1760. '/div'
  1761. );
  1762. $this->assertTags($result, $expected);
  1763. $result = $this->Form->input('Contact.field', array(
  1764. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1765. ));
  1766. $expected = array(
  1767. 'label' => array('for' => 'ContactField'),
  1768. 'Field',
  1769. '/label',
  1770. 'input' => array(
  1771. 'type' => 'text', 'name' => 'data[Contact][field]',
  1772. 'id' => 'ContactField', 'class' => 'form-error'
  1773. ),
  1774. array('span' => array('class' => 'error-message')),
  1775. 'Badness!',
  1776. '/span'
  1777. );
  1778. $this->assertTags($result, $expected);
  1779. $result = $this->Form->input('Contact.field', array(
  1780. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1781. ));
  1782. $expected = array(
  1783. 'div' => array('class' => 'input text error'),
  1784. 'label' => array('for' => 'ContactField'),
  1785. 'Field',
  1786. '/label',
  1787. 'input' => array(
  1788. 'type' => 'text', 'name' => 'data[Contact][field]',
  1789. 'id' => 'ContactField', 'class' => 'form-error'
  1790. ),
  1791. array('div' => array('class' => 'error')),
  1792. 'Badness!',
  1793. '/div'
  1794. );
  1795. $this->assertTags($result, $expected);
  1796. $result = $this->Form->input('Contact.field', array(
  1797. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1798. ));
  1799. $expected = array(
  1800. 'span' => array('class' => 'input text error'),
  1801. 'label' => array('for' => 'ContactField'),
  1802. 'Field',
  1803. '/label',
  1804. 'input' => array(
  1805. 'type' => 'text', 'name' => 'data[Contact][field]',
  1806. 'id' => 'ContactField', 'class' => 'form-error'
  1807. ),
  1808. 'Badness!',
  1809. '/span'
  1810. );
  1811. $this->assertTags($result, $expected);
  1812. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1813. $expected = array(
  1814. 'div' => array('class' => 'input text error'),
  1815. 'label' => array('for' => 'ContactField'),
  1816. 'Field',
  1817. '/label',
  1818. 'input' => array(
  1819. 'type' => 'text', 'name' => 'data[Contact][field]',
  1820. 'id' => 'ContactField', 'class' => 'form-error'
  1821. ),
  1822. 'A message to you, Rudy',
  1823. array('div' => array('class' => 'error-message')),
  1824. 'Badness!',
  1825. '/div',
  1826. '/div'
  1827. );
  1828. $this->assertTags($result, $expected);
  1829. $this->Form->setEntity(null);
  1830. $this->Form->setEntity('Contact.field');
  1831. $result = $this->Form->input('Contact.field', array(
  1832. 'after' => 'A message to you, Rudy', 'error' => false
  1833. ));
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1840. 'A message to you, Rudy',
  1841. '/div'
  1842. );
  1843. $this->assertTags($result, $expected);
  1844. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1845. $expected = array(
  1846. 'div' => array('class' => 'input text'),
  1847. 'label' => array('for' => 'ObjectField'),
  1848. 'Field',
  1849. '/label',
  1850. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1851. 'A message to you, Rudy',
  1852. '/div'
  1853. );
  1854. $this->assertTags($result, $expected);
  1855. $Contact->validationErrors['field'] = array('minLength');
  1856. $result = $this->Form->input('Contact.field', array(
  1857. 'error' => array(
  1858. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1859. 'maxLength' => 'login too large'
  1860. )
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text error'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1868. array('div' => array('class' => 'error-message')),
  1869. 'Le login doit contenir au moins 2 caractères',
  1870. '/div',
  1871. '/div'
  1872. );
  1873. $this->assertTags($result, $expected);
  1874. $Contact->validationErrors['field'] = array('maxLength');
  1875. $result = $this->Form->input('Contact.field', array(
  1876. 'error' => array(
  1877. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1878. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1879. 'maxLength' => 'login too large',
  1880. )
  1881. ));
  1882. $expected = array(
  1883. 'div' => array('class' => 'input text error'),
  1884. 'label' => array('for' => 'ContactField'),
  1885. 'Field',
  1886. '/label',
  1887. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1888. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1889. 'login too large',
  1890. '/span',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. }
  1895. /**
  1896. * Test that inputs with 0 can be created.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testInputZero() {
  1901. $this->Form->create('User');
  1902. $result = $this->Form->input('0');
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text'),
  1905. 'label' => array('for' => 'User0'), '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. }
  1911. /**
  1912. * test input() with checkbox creation
  1913. *
  1914. * @return void
  1915. */
  1916. public function testInputCheckbox() {
  1917. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input checkbox'),
  1920. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1921. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1922. '/div'
  1923. );
  1924. $this->assertTags($result, $expected);
  1925. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1926. $expected = array(
  1927. 'div' => array('class' => 'input checkbox'),
  1928. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1929. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1934. $expected = array(
  1935. 'div' => array('class' => 'input checkbox'),
  1936. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1937. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. }
  1942. /**
  1943. * test form->input() with time types.
  1944. *
  1945. */
  1946. public function testInputTime() {
  1947. extract($this->dateRegex);
  1948. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1949. $result = explode(':', $result);
  1950. $this->assertRegExp('/option value="23"/', $result[0]);
  1951. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1952. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1953. $result = explode(':', $result);
  1954. $this->assertRegExp('/option value="23"/', $result[0]);
  1955. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1956. $result = $this->Form->input('Model.field', array(
  1957. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1958. ));
  1959. $result = explode(':', $result);
  1960. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1961. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1962. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1963. $result = $this->Form->input('Model.field', array(
  1964. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1965. ));
  1966. $result = explode(':', $result);
  1967. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1968. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1969. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1970. $result = $this->Form->input('prueba', array(
  1971. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1972. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1973. ));
  1974. $result = explode(':', $result);
  1975. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1976. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1977. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1978. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1979. $result = $this->Form->input('Random.start_time', array(
  1980. 'type' => 'time',
  1981. 'selected' => '18:15'
  1982. ));
  1983. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1984. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1985. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1986. }
  1987. /**
  1988. * Test interval + selected near the hour roll over.
  1989. *
  1990. * @return void
  1991. */
  1992. public function testTimeSelectedWithInterval() {
  1993. $result = $this->Form->input('Model.start_time', array(
  1994. 'type' => 'time',
  1995. 'interval' => 15,
  1996. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  1997. ));
  1998. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  1999. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2000. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2001. $result = $this->Form->input('Model.start_time', array(
  2002. 'type' => 'time',
  2003. 'interval' => 15,
  2004. 'selected' => '2012-10-23 15:57:00'
  2005. ));
  2006. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2007. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2008. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2009. $result = $this->Form->input('Model.start_time', array(
  2010. 'timeFormat' => 24,
  2011. 'type' => 'time',
  2012. 'interval' => 15,
  2013. 'selected' => '15:57'
  2014. ));
  2015. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2016. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2017. $result = $this->Form->input('Model.start_time', array(
  2018. 'timeFormat' => 24,
  2019. 'type' => 'time',
  2020. 'interval' => 15,
  2021. 'selected' => '23:57'
  2022. ));
  2023. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2024. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2025. $result = $this->Form->input('Model.created', array(
  2026. 'timeFormat' => 24,
  2027. 'type' => 'datetime',
  2028. 'interval' => 15,
  2029. 'selected' => '2012-09-30 23:56'
  2030. ));
  2031. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2032. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2033. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2034. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. }
  2037. /**
  2038. * test form->input() with datetime, date and time types
  2039. *
  2040. * @return void
  2041. */
  2042. public function testInputDatetime() {
  2043. extract($this->dateRegex);
  2044. $result = $this->Form->input('prueba', array(
  2045. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2046. 'maxYear' => date('Y') + 1, 'interval' => 15
  2047. ));
  2048. $result = explode(':', $result);
  2049. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2050. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2051. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2052. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2053. //related to ticket #5013
  2054. $result = $this->Form->input('Contact.date', array(
  2055. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2056. ));
  2057. $this->assertRegExp('/class="customClass"/', $result);
  2058. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2059. $result = $this->Form->input('Contact.date', array(
  2060. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2061. ));
  2062. $this->assertRegExp('/id="customIdDay"/', $result);
  2063. $this->assertRegExp('/id="customIdMonth"/', $result);
  2064. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2065. $result = $this->Form->input('Model.field', array(
  2066. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2067. ));
  2068. $this->assertRegExp('/id="customIDDay"/', $result);
  2069. $this->assertRegExp('/id="customIDHour"/', $result);
  2070. $result = explode('</select><select', $result);
  2071. $result = explode(':', $result[1]);
  2072. $this->assertRegExp('/option value="23"/', $result[0]);
  2073. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2074. $result = $this->Form->input('Model.field', array(
  2075. 'type' => 'datetime', 'timeFormat' => 12
  2076. ));
  2077. $result = explode('</select><select', $result);
  2078. $result = explode(':', $result[1]);
  2079. $this->assertRegExp('/option value="12"/', $result[0]);
  2080. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2081. $this->Form->request->data = array('Contact' => array('created' => null));
  2082. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2083. $expected = array(
  2084. 'div' => array('class' => 'input date'),
  2085. 'label' => array('for' => 'ContactCreatedMonth'),
  2086. 'Created',
  2087. '/label',
  2088. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2089. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2090. $monthsRegex,
  2091. '/select', '-',
  2092. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2093. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2094. $daysRegex,
  2095. '/select', '-',
  2096. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2097. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2098. $yearsRegex,
  2099. '/select',
  2100. '/div'
  2101. );
  2102. $this->assertTags($result, $expected);
  2103. $this->Form->request->data = array('Contact' => array('created' => null));
  2104. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2105. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2106. $this->Form->request->data = array('Contact' => array('created' => null));
  2107. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2108. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2109. $result = $this->Form->input('Contact.created', array(
  2110. 'type' => 'date',
  2111. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2112. 'timeFormat' => 'NONE'
  2113. ));
  2114. $this->assertRegExp('/for\="created-month"/', $result);
  2115. }
  2116. /**
  2117. * Test generating checkboxes in a loop.
  2118. *
  2119. * @return void
  2120. */
  2121. public function testInputCheckboxesInLoop() {
  2122. for ($i = 1; $i < 5; $i++) {
  2123. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2124. $expected = array(
  2125. 'div' => array('class' => 'input checkbox'),
  2126. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2127. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2128. 'label' => array('for' => "Contact{$i}Email"),
  2129. 'Email',
  2130. '/label',
  2131. '/div'
  2132. );
  2133. $this->assertTags($result, $expected);
  2134. }
  2135. }
  2136. /**
  2137. * Test generating checkboxes with disabled elements.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testInputCheckboxWithDisabledElements() {
  2142. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2143. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2144. $expected = array(
  2145. array('div' => array('class' => 'input select')),
  2146. array('label' => array('for' => "ContactMultiple")),
  2147. 'Multiple',
  2148. '/label',
  2149. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2150. array('div' => array('class' => 'checkbox')),
  2151. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2152. array('label' => array('for' => "ContactMultiple1")),
  2153. 'One',
  2154. '/label',
  2155. '/div',
  2156. array('div' => array('class' => 'checkbox')),
  2157. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2158. array('label' => array('for' => "ContactMultiple2")),
  2159. 'Two',
  2160. '/label',
  2161. '/div',
  2162. array('div' => array('class' => 'checkbox')),
  2163. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2164. array('label' => array('for' => "ContactMultiple3")),
  2165. 'Three',
  2166. '/label',
  2167. '/div',
  2168. '/div'
  2169. );
  2170. $this->assertTags($result, $expected);
  2171. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2172. $this->assertTags($result, $expected);
  2173. $disabled = array('2', 3);
  2174. $expected = array(
  2175. array('div' => array('class' => 'input select')),
  2176. array('label' => array('for' => "ContactMultiple")),
  2177. 'Multiple',
  2178. '/label',
  2179. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2180. array('div' => array('class' => 'checkbox')),
  2181. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2182. array('label' => array('for' => "ContactMultiple1")),
  2183. 'One',
  2184. '/label',
  2185. '/div',
  2186. array('div' => array('class' => 'checkbox')),
  2187. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2188. array('label' => array('for' => "ContactMultiple2")),
  2189. 'Two',
  2190. '/label',
  2191. '/div',
  2192. array('div' => array('class' => 'checkbox')),
  2193. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2194. array('label' => array('for' => "ContactMultiple3")),
  2195. 'Three',
  2196. '/label',
  2197. '/div',
  2198. '/div'
  2199. );
  2200. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2201. $this->assertTags($result, $expected);
  2202. }
  2203. /**
  2204. * test input name with leading integer, ensure attributes are generated correctly.
  2205. *
  2206. * @return void
  2207. */
  2208. public function testInputWithLeadingInteger() {
  2209. $result = $this->Form->text('0.Node.title');
  2210. $expected = array(
  2211. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2212. );
  2213. $this->assertTags($result, $expected);
  2214. }
  2215. /**
  2216. * test form->input() with select type inputs.
  2217. *
  2218. * @return void
  2219. */
  2220. public function testInputSelectType() {
  2221. $result = $this->Form->input('email', array(
  2222. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2223. );
  2224. $expected = array(
  2225. 'div' => array('class' => 'input select'),
  2226. 'label' => array('for' => 'email'),
  2227. 'Email',
  2228. '/label',
  2229. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2230. array('option' => array('value' => '')),
  2231. '/option',
  2232. array('option' => array('value' => 'è')),
  2233. 'Firést',
  2234. '/option',
  2235. array('option' => array('value' => 'é')),
  2236. 'Secoènd',
  2237. '/option',
  2238. '/select',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $result = $this->Form->input('email', array(
  2243. 'options' => array('First', 'Second'), 'empty' => true)
  2244. );
  2245. $expected = array(
  2246. 'div' => array('class' => 'input select'),
  2247. 'label' => array('for' => 'email'),
  2248. 'Email',
  2249. '/label',
  2250. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2251. array('option' => array('value' => '')),
  2252. '/option',
  2253. array('option' => array('value' => '0')),
  2254. 'First',
  2255. '/option',
  2256. array('option' => array('value' => '1')),
  2257. 'Second',
  2258. '/option',
  2259. '/select',
  2260. '/div'
  2261. );
  2262. $this->assertTags($result, $expected);
  2263. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2264. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2265. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2266. $expected = array(
  2267. 'div' => array('class' => 'input select'),
  2268. 'label' => array('for' => 'ModelUserId'),
  2269. 'User',
  2270. '/label',
  2271. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2272. array('option' => array('value' => '')),
  2273. '/option',
  2274. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2275. 'good',
  2276. '/option',
  2277. array('option' => array('value' => 'other')),
  2278. 'bad',
  2279. '/option',
  2280. '/select',
  2281. '/div'
  2282. );
  2283. $this->assertTags($result, $expected);
  2284. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2285. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2286. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2287. $expected = array(
  2288. 'div' => array('class' => 'input select'),
  2289. 'label' => array('for' => 'ThingUserId'),
  2290. 'User',
  2291. '/label',
  2292. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2293. array('option' => array('value' => '')),
  2294. 'Some Empty',
  2295. '/option',
  2296. array('option' => array('value' => 'value')),
  2297. 'good',
  2298. '/option',
  2299. array('option' => array('value' => 'other')),
  2300. 'bad',
  2301. '/option',
  2302. '/select',
  2303. '/div'
  2304. );
  2305. $this->assertTags($result, $expected);
  2306. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2307. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2308. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2309. $expected = array(
  2310. 'div' => array('class' => 'input select'),
  2311. 'label' => array('for' => 'ThingUserId'),
  2312. 'User',
  2313. '/label',
  2314. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2315. array('option' => array('value' => '')),
  2316. 'Some Empty',
  2317. '/option',
  2318. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2319. 'good',
  2320. '/option',
  2321. array('option' => array('value' => 'other')),
  2322. 'bad',
  2323. '/option',
  2324. '/select',
  2325. '/div'
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2329. $this->Form->request->data = array('User' => array('User' => array('value')));
  2330. $result = $this->Form->input('User.User', array('empty' => true));
  2331. $expected = array(
  2332. 'div' => array('class' => 'input select'),
  2333. 'label' => array('for' => 'UserUser'),
  2334. 'User',
  2335. '/label',
  2336. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2337. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2338. array('option' => array('value' => '')),
  2339. '/option',
  2340. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2341. 'good',
  2342. '/option',
  2343. array('option' => array('value' => 'other')),
  2344. 'bad',
  2345. '/option',
  2346. '/select',
  2347. '/div'
  2348. );
  2349. $this->assertTags($result, $expected);
  2350. $this->Form->data = array();
  2351. $result = $this->Form->input('Publisher.id', array(
  2352. 'label' => 'Publisher',
  2353. 'type' => 'select',
  2354. 'multiple' => 'checkbox',
  2355. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2356. ));
  2357. $expected = array(
  2358. array('div' => array('class' => 'input select')),
  2359. array('label' => array('for' => 'PublisherId')),
  2360. 'Publisher',
  2361. '/label',
  2362. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2363. array('div' => array('class' => 'checkbox')),
  2364. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2365. array('label' => array('for' => 'PublisherIdValue1')),
  2366. 'Label 1',
  2367. '/label',
  2368. '/div',
  2369. array('div' => array('class' => 'checkbox')),
  2370. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2371. array('label' => array('for' => 'PublisherIdValue2')),
  2372. 'Label 2',
  2373. '/label',
  2374. '/div',
  2375. '/div'
  2376. );
  2377. $this->assertTags($result, $expected);
  2378. }
  2379. /**
  2380. * test that input() and a non standard primary key makes a hidden input by default.
  2381. *
  2382. * @return void
  2383. */
  2384. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2385. $this->Form->create('User');
  2386. $this->Form->fieldset = array(
  2387. 'User' => array(
  2388. 'fields' => array(
  2389. 'model_id' => array('type' => 'integer')
  2390. ),
  2391. 'validates' => array(),
  2392. 'key' => 'model_id'
  2393. )
  2394. );
  2395. $result = $this->Form->input('model_id');
  2396. $expected = array(
  2397. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. }
  2401. /**
  2402. * test that overriding the magic select type widget is possible
  2403. *
  2404. * @return void
  2405. */
  2406. public function testInputOverridingMagicSelectType() {
  2407. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2408. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2409. $expected = array(
  2410. 'div' => array('class' => 'input text'),
  2411. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2412. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2413. '/div'
  2414. );
  2415. $this->assertTags($result, $expected);
  2416. //Check that magic types still work for plural/singular vars
  2417. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2418. $result = $this->Form->input('Model.type');
  2419. $expected = array(
  2420. 'div' => array('class' => 'input select'),
  2421. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2422. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2423. array('option' => array('value' => 'value')), 'good', '/option',
  2424. array('option' => array('value' => 'other')), 'bad', '/option',
  2425. '/select',
  2426. '/div'
  2427. );
  2428. $this->assertTags($result, $expected);
  2429. }
  2430. /**
  2431. * Test that magic input() selects are created for type=number
  2432. *
  2433. * @return void
  2434. */
  2435. public function testInputMagicSelectForTypeNumber() {
  2436. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2437. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2438. $result = $this->Form->input('ValidateUser.balance');
  2439. $expected = array(
  2440. 'div' => array('class' => 'input select'),
  2441. 'label' => array('for' => 'ValidateUserBalance'),
  2442. 'Balance',
  2443. '/label',
  2444. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2445. array('option' => array('value' => '0')),
  2446. 'nothing',
  2447. '/option',
  2448. array('option' => array('value' => '1', 'selected' => 'selected')),
  2449. 'some',
  2450. '/option',
  2451. array('option' => array('value' => '100')),
  2452. 'a lot',
  2453. '/option',
  2454. '/select',
  2455. '/div'
  2456. );
  2457. $this->assertTags($result, $expected);
  2458. }
  2459. /**
  2460. * Test that magic input() selects can easily be converted into radio types without error.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testInputMagicSelectChangeToRadio() {
  2465. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2466. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2467. $this->assertRegExp('/input type="radio"/', $result);
  2468. }
  2469. /**
  2470. * fields with the same name as the model should work.
  2471. *
  2472. * @return void
  2473. */
  2474. public function testInputWithMatchingFieldAndModelName() {
  2475. $this->Form->create('User');
  2476. $this->Form->fieldset = array(
  2477. 'User' => array(
  2478. 'fields' => array(
  2479. 'User' => array('type' => 'text')
  2480. ),
  2481. 'validates' => array(),
  2482. 'key' => 'id'
  2483. )
  2484. );
  2485. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2486. $result = $this->Form->input('User', array('type' => 'text'));
  2487. $expected = array(
  2488. 'div' => array('class' => 'input text'),
  2489. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2490. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2491. '/div'
  2492. );
  2493. $this->assertTags($result, $expected);
  2494. }
  2495. /**
  2496. * testFormInputs method
  2497. *
  2498. * test correct results from form::inputs().
  2499. *
  2500. * @return void
  2501. */
  2502. public function testFormInputs() {
  2503. $this->Form->create('Contact');
  2504. $result = $this->Form->inputs('The Legend');
  2505. $expected = array(
  2506. '<fieldset',
  2507. '<legend',
  2508. 'The Legend',
  2509. '/legend',
  2510. '*/fieldset',
  2511. );
  2512. $this->assertTags($result, $expected);
  2513. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2514. $expected = array(
  2515. 'fieldset' => array('class' => 'classy-stuff'),
  2516. '<legend',
  2517. 'Field of Dreams',
  2518. '/legend',
  2519. '*/fieldset'
  2520. );
  2521. $this->assertTags($result, $expected);
  2522. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2523. $this->assertTags($result, $expected);
  2524. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2525. $this->assertTags($result, $expected);
  2526. $this->Form->create('Contact');
  2527. $this->Form->request['prefix'] = 'admin';
  2528. $this->Form->request['action'] = 'admin_edit';
  2529. $result = $this->Form->inputs();
  2530. $expected = array(
  2531. '<fieldset',
  2532. '<legend',
  2533. 'Edit Contact',
  2534. '/legend',
  2535. '*/fieldset',
  2536. );
  2537. $this->assertTags($result, $expected);
  2538. $this->Form->create('Contact');
  2539. $result = $this->Form->inputs(false);
  2540. $expected = array(
  2541. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2542. array('div' => array('class' => 'input text')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input email')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input tel')),
  2547. '*/div',
  2548. array('div' => array('class' => 'input password')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input date')),
  2551. '*/div',
  2552. array('div' => array('class' => 'input date')),
  2553. '*/div',
  2554. array('div' => array('class' => 'input datetime')),
  2555. '*/div',
  2556. array('div' => array('class' => 'input number')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input select')),
  2559. '*/div',
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. $this->Form->create('Contact');
  2563. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2564. $expected = array(
  2565. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2566. array('div' => array('class' => 'input text')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input email')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input tel')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input password')),
  2573. '*/div',
  2574. array('div' => array('class' => 'input date')),
  2575. '*/div',
  2576. array('div' => array('class' => 'input date')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input datetime')),
  2579. '*/div',
  2580. array('div' => array('class' => 'input number')),
  2581. '*/div',
  2582. array('div' => array('class' => 'input select')),
  2583. '*/div',
  2584. );
  2585. $this->assertTags($result, $expected);
  2586. $this->Form->create('Contact');
  2587. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2588. $this->assertTags($result, $expected);
  2589. $this->Form->create('Contact');
  2590. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2591. $expected = array(
  2592. 'fieldset' => array(),
  2593. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2594. array('div' => array('class' => 'input text')),
  2595. '*/div',
  2596. array('div' => array('class' => 'input email')),
  2597. '*/div',
  2598. array('div' => array('class' => 'input tel')),
  2599. '*/div',
  2600. array('div' => array('class' => 'input password')),
  2601. '*/div',
  2602. array('div' => array('class' => 'input date')),
  2603. '*/div',
  2604. array('div' => array('class' => 'input date')),
  2605. '*/div',
  2606. array('div' => array('class' => 'input datetime')),
  2607. '*/div',
  2608. array('div' => array('class' => 'input number')),
  2609. '*/div',
  2610. array('div' => array('class' => 'input select')),
  2611. '*/div',
  2612. '/fieldset'
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->create('Contact');
  2616. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2617. $expected = array(
  2618. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2619. array('div' => array('class' => 'input text')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input email')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input tel')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input password')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input date')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input date')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input datetime')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input number')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input select')),
  2636. '*/div',
  2637. );
  2638. $this->assertTags($result, $expected);
  2639. $this->Form->create('Contact');
  2640. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2641. $this->assertTags($result, $expected);
  2642. $this->Form->create('Contact');
  2643. $result = $this->Form->inputs('Hello');
  2644. $expected = array(
  2645. 'fieldset' => array(),
  2646. 'legend' => array(),
  2647. 'Hello',
  2648. '/legend',
  2649. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2650. array('div' => array('class' => 'input text')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input email')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input tel')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input password')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input date')),
  2659. '*/div',
  2660. array('div' => array('class' => 'input date')),
  2661. '*/div',
  2662. array('div' => array('class' => 'input datetime')),
  2663. '*/div',
  2664. array('div' => array('class' => 'input number')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input select')),
  2667. '*/div',
  2668. '/fieldset'
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. $this->Form->create('Contact');
  2672. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2673. $expected = array(
  2674. 'fieldset' => array(),
  2675. 'legend' => array(),
  2676. 'Hello',
  2677. '/legend',
  2678. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2679. array('div' => array('class' => 'input text')),
  2680. '*/div',
  2681. array('div' => array('class' => 'input email')),
  2682. '*/div',
  2683. array('div' => array('class' => 'input tel')),
  2684. '*/div',
  2685. array('div' => array('class' => 'input password')),
  2686. '*/div',
  2687. array('div' => array('class' => 'input date')),
  2688. '*/div',
  2689. array('div' => array('class' => 'input date')),
  2690. '*/div',
  2691. array('div' => array('class' => 'input datetime')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input number')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input select')),
  2696. '*/div',
  2697. '/fieldset'
  2698. );
  2699. $this->assertTags($result, $expected);
  2700. $this->Form->create('Contact');
  2701. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2702. $this->assertTags($result, $expected);
  2703. $this->Form->end();
  2704. $this->Form->create(false);
  2705. $expected = array(
  2706. 'fieldset' => array(),
  2707. array('div' => array('class' => 'input text')),
  2708. 'label' => array('for' => 'foo'),
  2709. 'Foo',
  2710. '/label',
  2711. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2712. '*/div',
  2713. '/fieldset'
  2714. );
  2715. $result = $this->Form->inputs(
  2716. array('foo' => array('type' => 'text')),
  2717. array(),
  2718. array('legend' => false)
  2719. );
  2720. $this->assertTags($result, $expected);
  2721. }
  2722. /**
  2723. * Tests inputs() works with plugin models
  2724. *
  2725. * @return void
  2726. */
  2727. public function testInputsPluginModel() {
  2728. $this->loadFixtures('Post');
  2729. App::build(array(
  2730. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2731. ));
  2732. CakePlugin::load('TestPlugin');
  2733. $this->Form->request['models'] = array(
  2734. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2735. );
  2736. $this->Form->create('TestPlugin.TestPluginPost');
  2737. $result = $this->Form->inputs();
  2738. $this->assertContains('data[TestPluginPost][id]', $result);
  2739. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2740. $this->assertContains('data[TestPluginPost][title]', $result);
  2741. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2742. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2743. $this->assertEquals('TestPluginPost', $this->Form->model());
  2744. }
  2745. /**
  2746. * testSelectAsCheckbox method
  2747. *
  2748. * test multi-select widget with checkbox formatting.
  2749. *
  2750. * @return void
  2751. */
  2752. public function testSelectAsCheckbox() {
  2753. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2754. $expected = array(
  2755. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2756. array('div' => array('class' => 'checkbox')),
  2757. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2758. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2759. 'first',
  2760. '/label',
  2761. '/div',
  2762. array('div' => array('class' => 'checkbox')),
  2763. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2764. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2765. 'second',
  2766. '/label',
  2767. '/div',
  2768. array('div' => array('class' => 'checkbox')),
  2769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2770. array('label' => array('for' => 'ModelMultiField2')),
  2771. 'third',
  2772. '/label',
  2773. '/div',
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2777. $expected = array(
  2778. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2779. array('div' => array('class' => 'checkbox')),
  2780. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2781. array('label' => array('for' => 'ModelMultiField12')),
  2782. 'half',
  2783. '/label',
  2784. '/div',
  2785. );
  2786. $this->assertTags($result, $expected);
  2787. }
  2788. /**
  2789. * testLabel method
  2790. *
  2791. * test label generation.
  2792. *
  2793. * @return void
  2794. */
  2795. public function testLabel() {
  2796. $this->Form->text('Person.name');
  2797. $result = $this->Form->label();
  2798. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2799. $this->Form->text('Person.name');
  2800. $result = $this->Form->label();
  2801. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2802. $result = $this->Form->label('Person.first_name');
  2803. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2804. $result = $this->Form->label('Person.first_name', 'Your first name');
  2805. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2806. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2807. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2808. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2809. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2810. $result = $this->Form->label('Person.first_name', '');
  2811. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2812. $result = $this->Form->label('Person.2.name', '');
  2813. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2814. }
  2815. /**
  2816. * testTextbox method
  2817. *
  2818. * test textbox element generation
  2819. *
  2820. * @return void
  2821. */
  2822. public function testTextbox() {
  2823. $result = $this->Form->text('Model.field');
  2824. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2825. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2826. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2827. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2828. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2829. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2830. $result = $this->Form->text('Model.text');
  2831. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2832. $Contact = ClassRegistry::getObject('Contact');
  2833. $Contact->validationErrors['text'] = array(true);
  2834. $this->Form->request->data['Contact']['text'] = 'test';
  2835. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2836. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2837. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2838. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2839. $expected = array(
  2840. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2841. );
  2842. $this->assertTags($result, $expected);
  2843. }
  2844. /**
  2845. * testDefaultValue method
  2846. *
  2847. * Test default value setting
  2848. *
  2849. * @return void
  2850. */
  2851. public function testDefaultValue() {
  2852. $this->Form->request->data['Model']['field'] = 'test';
  2853. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2854. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2855. unset($this->Form->request->data['Model']['field']);
  2856. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2857. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2858. }
  2859. /**
  2860. * testCheckboxDefaultValue method
  2861. *
  2862. * Test default value setting on checkbox() method
  2863. *
  2864. * @return void
  2865. */
  2866. public function testCheckboxDefaultValue() {
  2867. $this->Form->request->data['Model']['field'] = false;
  2868. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2869. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2870. unset($this->Form->request->data['Model']['field']);
  2871. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2872. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2873. $this->Form->request->data['Model']['field'] = true;
  2874. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2875. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2876. unset($this->Form->request->data['Model']['field']);
  2877. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2878. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2879. }
  2880. /**
  2881. * testError method
  2882. *
  2883. * Test field error generation
  2884. *
  2885. * @return void
  2886. */
  2887. public function testError() {
  2888. $Contact = ClassRegistry::getObject('Contact');
  2889. $Contact->validationErrors['field'] = array(1);
  2890. $result = $this->Form->error('Contact.field');
  2891. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2892. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2893. $this->assertEquals('Error in field Field', $result);
  2894. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2895. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2896. $this->assertEquals('This field contains invalid input', $result);
  2897. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2898. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2899. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2900. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2901. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2902. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2903. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2904. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2905. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2906. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2907. $this->assertEquals('<strong>Badness!</strong>', $result);
  2908. $Contact->validationErrors['field'] = array("email");
  2909. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2910. $expected = array(
  2911. 'div' => array('class' => 'field-error'),
  2912. 'No good!',
  2913. '/div'
  2914. );
  2915. $this->assertTags($result, $expected);
  2916. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2917. $result = $this->Form->error('Contact.field', array(
  2918. 'notEmpty' => 'Cannot be empty',
  2919. 'email' => 'No good!'
  2920. ));
  2921. $expected = array(
  2922. 'div' => array('class' => 'error-message'),
  2923. 'ul' => array(),
  2924. '<li', 'Cannot be empty', '/li',
  2925. '<li', 'No good!', '/li',
  2926. '<li', 'Something else', '/li',
  2927. '/ul',
  2928. '/div'
  2929. );
  2930. $this->assertTags($result, $expected);
  2931. // Testing error messages list options
  2932. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2933. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2934. $expected = array(
  2935. 'div' => array('class' => 'error-message'),
  2936. 'ol' => array(),
  2937. '<li', 'notEmpty', '/li',
  2938. '<li', 'email', '/li',
  2939. '/ol',
  2940. '/div'
  2941. );
  2942. $this->assertTags($result, $expected);
  2943. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2944. $expected = array(
  2945. 'div' => array('class' => 'error-message'),
  2946. 'ol' => array(),
  2947. '<li', 'notEmpty', '/li',
  2948. '<li', 'email', '/li',
  2949. '/ol',
  2950. '/div'
  2951. );
  2952. $this->assertTags($result, $expected);
  2953. $result = $this->Form->error('Contact.field', null, array(
  2954. 'listOptions' => array(
  2955. 'class' => 'ul-class',
  2956. 'itemOptions' => array(
  2957. 'class' => 'li-class'
  2958. )
  2959. )
  2960. ));
  2961. $expected = array(
  2962. 'div' => array('class' => 'error-message'),
  2963. 'ul' => array('class' => 'ul-class'),
  2964. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2965. array('li' => array('class' => 'li-class')), 'email', '/li',
  2966. '/ul',
  2967. '/div'
  2968. );
  2969. $this->assertTags($result, $expected);
  2970. }
  2971. /**
  2972. * test error options when using form->input();
  2973. *
  2974. * @return void
  2975. */
  2976. public function testInputErrorEscape() {
  2977. $this->Form->create('ValidateProfile');
  2978. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2979. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2980. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2981. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2982. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2983. $this->assertRegExp('/required<br>/', $result);
  2984. }
  2985. /**
  2986. * testPassword method
  2987. *
  2988. * Test password element generation
  2989. *
  2990. * @return void
  2991. */
  2992. public function testPassword() {
  2993. $Contact = ClassRegistry::getObject('Contact');
  2994. $result = $this->Form->password('Contact.field');
  2995. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2996. $Contact->validationErrors['passwd'] = 1;
  2997. $this->Form->request->data['Contact']['passwd'] = 'test';
  2998. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2999. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3000. }
  3001. /**
  3002. * testRadio method
  3003. *
  3004. * Test radio element set generation
  3005. *
  3006. * @return void
  3007. */
  3008. public function testRadio() {
  3009. $result = $this->Form->radio('Model.field', array('option A'));
  3010. $expected = array(
  3011. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3012. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3013. 'label' => array('for' => 'ModelField0'),
  3014. 'option A',
  3015. '/label'
  3016. );
  3017. $this->assertTags($result, $expected);
  3018. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3019. $expected = array(
  3020. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3021. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3022. 'label' => array('for' => 'ModelField12'),
  3023. 'half',
  3024. '/label'
  3025. );
  3026. $this->assertTags($result, $expected);
  3027. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3028. $expected = array(
  3029. 'fieldset' => array(),
  3030. 'legend' => array(),
  3031. 'Field',
  3032. '/legend',
  3033. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3034. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3035. array('label' => array('for' => 'ModelField0')),
  3036. 'option A',
  3037. '/label',
  3038. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3039. array('label' => array('for' => 'ModelField1')),
  3040. 'option B',
  3041. '/label',
  3042. '/fieldset'
  3043. );
  3044. $this->assertTags($result, $expected);
  3045. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3046. $expected = array(
  3047. 'fieldset' => array(),
  3048. 'legend' => array(),
  3049. 'Field',
  3050. '/legend',
  3051. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3052. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3053. array('label' => array('for' => 'ModelField0')),
  3054. 'option A',
  3055. '/label',
  3056. 'br' => array(),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3058. array('label' => array('for' => 'ModelField1')),
  3059. 'option B',
  3060. '/label',
  3061. '/fieldset'
  3062. );
  3063. $this->assertTags($result, $expected);
  3064. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3065. $expected = array(
  3066. 'fieldset' => array(),
  3067. 'legend' => array(),
  3068. 'Gender',
  3069. '/legend',
  3070. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3071. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3072. array('label' => array('for' => 'EmployeeGenderMale')),
  3073. 'Male',
  3074. '/label',
  3075. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3076. array('label' => array('for' => 'EmployeeGenderFemale')),
  3077. 'Female',
  3078. '/label',
  3079. '/fieldset',
  3080. );
  3081. $this->assertTags($result, $expected);
  3082. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3083. $expected = array(
  3084. 'fieldset' => array(),
  3085. 'legend' => array(),
  3086. 'Gender',
  3087. '/legend',
  3088. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3089. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3090. array('label' => array('for' => 'OfficerGenderMale')),
  3091. 'Male',
  3092. '/label',
  3093. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3094. array('label' => array('for' => 'OfficerGenderFemale')),
  3095. 'Female',
  3096. '/label',
  3097. '/fieldset',
  3098. );
  3099. $this->assertTags($result, $expected);
  3100. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3101. $expected = array(
  3102. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3103. array('input' => array(
  3104. 'type' => 'radio',
  3105. 'name' => 'data[Contact][1][imrequired]',
  3106. 'value' => '0',
  3107. 'id' => 'Contact1Imrequired0',
  3108. 'required' => 'required'
  3109. )),
  3110. 'label' => array('for' => 'Contact1Imrequired0'),
  3111. 'option A',
  3112. '/label'
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. $result = $this->Form->radio('Model.1.field', array('option A'));
  3116. $expected = array(
  3117. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3118. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3119. 'label' => array('for' => 'Model1Field0'),
  3120. 'option A',
  3121. '/label'
  3122. );
  3123. $this->assertTags($result, $expected);
  3124. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3125. $expected = array(
  3126. 'fieldset' => array(),
  3127. 'legend' => array(),
  3128. 'Field',
  3129. '/legend',
  3130. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3131. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3132. array('label' => array('for' => 'ModelField0')),
  3133. 'option A',
  3134. '/label',
  3135. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3136. array('label' => array('for' => 'ModelField1')),
  3137. 'option B',
  3138. '/label',
  3139. '/fieldset'
  3140. );
  3141. $this->assertTags($result, $expected);
  3142. $result = $this->Form->radio(
  3143. 'Model.field',
  3144. array('option A', 'option B'),
  3145. array('between' => 'I am between')
  3146. );
  3147. $expected = array(
  3148. 'fieldset' => array(),
  3149. 'legend' => array(),
  3150. 'Field',
  3151. '/legend',
  3152. 'I am between',
  3153. 'input' => array(
  3154. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3155. 'value' => '', 'id' => 'ModelField_'
  3156. ),
  3157. array('input' => array(
  3158. 'type' => 'radio', 'name' => 'data[Model][field]',
  3159. 'value' => '0', 'id' => 'ModelField0'
  3160. )),
  3161. array('label' => array('for' => 'ModelField0')),
  3162. 'option A',
  3163. '/label',
  3164. array('input' => array(
  3165. 'type' => 'radio', 'name' => 'data[Model][field]',
  3166. 'value' => '1', 'id' => 'ModelField1'
  3167. )),
  3168. array('label' => array('for' => 'ModelField1')),
  3169. 'option B',
  3170. '/label',
  3171. '/fieldset'
  3172. );
  3173. $this->assertTags($result, $expected);
  3174. $result = $this->Form->radio(
  3175. 'Model.field',
  3176. array('option A', 'option B', 'option C'),
  3177. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3178. );
  3179. $expected = array(
  3180. 'fieldset' => array(),
  3181. 'legend' => array(),
  3182. 'Field',
  3183. '/legend',
  3184. 'input' => array(
  3185. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3186. 'value' => '', 'id' => 'ModelField_'
  3187. ),
  3188. array('input' => array(
  3189. 'type' => 'radio', 'name' => 'data[Model][field]',
  3190. 'value' => '0', 'id' => 'ModelField0'
  3191. )),
  3192. array('label' => array('for' => 'ModelField0')),
  3193. 'option A',
  3194. '/label',
  3195. 'between A',
  3196. '--separator--',
  3197. array('input' => array(
  3198. 'type' => 'radio', 'name' => 'data[Model][field]',
  3199. 'value' => '1', 'id' => 'ModelField1'
  3200. )),
  3201. array('label' => array('for' => 'ModelField1')),
  3202. 'option B',
  3203. '/label',
  3204. 'between B',
  3205. '--separator--',
  3206. array('input' => array(
  3207. 'type' => 'radio', 'name' => 'data[Model][field]',
  3208. 'value' => '2', 'id' => 'ModelField2'
  3209. )),
  3210. array('label' => array('for' => 'ModelField2')),
  3211. 'option C',
  3212. '/label',
  3213. 'between C',
  3214. '/fieldset'
  3215. );
  3216. $this->assertTags($result, $expected);
  3217. }
  3218. /**
  3219. * Test that radios with a 0 value are selected under the correct conditions.
  3220. * Also ensure that values that are booleanish are handled correctly.
  3221. *
  3222. * @return void
  3223. */
  3224. public function testRadioOptionWithBooleanishValues() {
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Field',
  3229. '/legend',
  3230. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3231. array('label' => array('for' => 'ModelField1')),
  3232. 'Yes',
  3233. '/label',
  3234. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3235. array('label' => array('for' => 'ModelField0')),
  3236. 'No',
  3237. '/label',
  3238. '/fieldset'
  3239. );
  3240. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3241. $this->assertTags($result, $expected);
  3242. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3243. $this->assertTags($result, $expected);
  3244. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3245. $this->assertTags($result, $expected);
  3246. $expected = array(
  3247. 'fieldset' => array(),
  3248. 'legend' => array(),
  3249. 'Field',
  3250. '/legend',
  3251. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3252. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3253. array('label' => array('for' => 'ModelField1')),
  3254. 'Yes',
  3255. '/label',
  3256. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3257. array('label' => array('for' => 'ModelField0')),
  3258. 'No',
  3259. '/label',
  3260. '/fieldset'
  3261. );
  3262. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3265. $this->assertTags($result, $expected);
  3266. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3267. $this->assertTags($result, $expected);
  3268. $expected = array(
  3269. 'fieldset' => array(),
  3270. 'legend' => array(),
  3271. 'Field',
  3272. '/legend',
  3273. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3274. array('label' => array('for' => 'ModelField1')),
  3275. 'Yes',
  3276. '/label',
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'No',
  3280. '/label',
  3281. '/fieldset'
  3282. );
  3283. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3284. $this->assertTags($result, $expected);
  3285. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3286. $this->assertTags($result, $expected);
  3287. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3288. $this->assertTags($result, $expected);
  3289. }
  3290. /**
  3291. * test disabled radio options
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioDisabled() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('disabled' => array('option A'), 'value' => '0')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3307. array('label' => array('for' => 'ModelField0')),
  3308. 'option A',
  3309. '/label',
  3310. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3311. array('label' => array('for' => 'ModelField1')),
  3312. 'option B',
  3313. '/label',
  3314. '/fieldset'
  3315. );
  3316. $this->assertTags($result, $expected);
  3317. $result = $this->Form->radio(
  3318. 'Model.field',
  3319. array('option A', 'option B'),
  3320. array('disabled' => true, 'value' => '0')
  3321. );
  3322. $expected = array(
  3323. 'fieldset' => array(),
  3324. 'legend' => array(),
  3325. 'Field',
  3326. '/legend',
  3327. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3328. array('label' => array('for' => 'ModelField0')),
  3329. 'option A',
  3330. '/label',
  3331. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3332. array('label' => array('for' => 'ModelField1')),
  3333. 'option B',
  3334. '/label',
  3335. '/fieldset'
  3336. );
  3337. $this->assertTags($result, $expected);
  3338. $result = $this->Form->radio(
  3339. 'Model.field',
  3340. array('option A', 'option B'),
  3341. array('disabled' => 'disabled', 'value' => '0')
  3342. );
  3343. $expected = array(
  3344. 'fieldset' => array(),
  3345. 'legend' => array(),
  3346. 'Field',
  3347. '/legend',
  3348. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3349. array('label' => array('for' => 'ModelField0')),
  3350. 'option A',
  3351. '/label',
  3352. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3353. array('label' => array('for' => 'ModelField1')),
  3354. 'option B',
  3355. '/label',
  3356. '/fieldset'
  3357. );
  3358. $this->assertTags($result, $expected);
  3359. $result = $this->Form->input('Model.field', array(
  3360. 'options' => array('1' => 'first', '2' => 'second'),
  3361. 'type' => 'radio',
  3362. 'before' => '--before--',
  3363. 'after' => '--after--',
  3364. 'separator' => '--separator--',
  3365. 'between' => array('--between first--', '--between second--')
  3366. ));
  3367. $expected = array(
  3368. 'div' => array('class' => 'input radio'),
  3369. '--before--',
  3370. 'fieldset' => array(),
  3371. 'legend' => array(),
  3372. 'Field',
  3373. '/legend',
  3374. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3375. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3376. array('label' => array('for' => 'ModelField1')),
  3377. 'first',
  3378. '/label',
  3379. '--between first--',
  3380. '--separator--',
  3381. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3382. array('label' => array('for' => 'ModelField2')),
  3383. 'second',
  3384. '/label',
  3385. '--between second--',
  3386. '/fieldset',
  3387. '--after--',
  3388. '/div'
  3389. );
  3390. $this->assertTags($result, $expected);
  3391. $result = $this->Form->input('Model.field', array(
  3392. 'options' => array('1' => 'first', '2' => 'second'),
  3393. 'type' => 'radio',
  3394. 'before' => '--before--',
  3395. 'after' => '--after--',
  3396. 'separator' => '--separator--',
  3397. 'between' => array('--between first--')
  3398. ));
  3399. $expected = array(
  3400. 'div' => array('class' => 'input radio'),
  3401. '--before--',
  3402. 'fieldset' => array(),
  3403. 'legend' => array(),
  3404. 'Field',
  3405. '/legend',
  3406. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3407. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3408. array('label' => array('for' => 'ModelField1')),
  3409. 'first',
  3410. '/label',
  3411. '--between first--',
  3412. '--separator--',
  3413. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3414. array('label' => array('for' => 'ModelField2')),
  3415. 'second',
  3416. '/label',
  3417. '/fieldset',
  3418. '--after--',
  3419. '/div'
  3420. );
  3421. }
  3422. /**
  3423. * test disabling the hidden input for radio buttons
  3424. *
  3425. * @return void
  3426. */
  3427. public function testRadioHiddenInputDisabling() {
  3428. $result = $this->Form->input('Model.1.field', array(
  3429. 'type' => 'radio',
  3430. 'options' => array('option A'),
  3431. 'hiddenField' => false
  3432. )
  3433. );
  3434. $expected = array(
  3435. 'div' => array('class' => 'input radio'),
  3436. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3437. 'label' => array('for' => 'Model1Field0'),
  3438. 'option A',
  3439. '/label',
  3440. '/div'
  3441. );
  3442. $this->assertTags($result, $expected);
  3443. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3444. $expected = array(
  3445. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3446. 'label' => array('for' => 'Model1Field0'),
  3447. 'option A',
  3448. '/label'
  3449. );
  3450. $this->assertTags($result, $expected);
  3451. }
  3452. /**
  3453. * test adding an empty option for radio buttons
  3454. *
  3455. * @return void
  3456. */
  3457. public function testRadioAddEmptyOption() {
  3458. $result = $this->Form->input('Model.1.field', array(
  3459. 'type' => 'radio',
  3460. 'options' => array('option A'),
  3461. 'empty' => true,
  3462. 'hiddenField' => false
  3463. ));
  3464. $expected = array(
  3465. 'div' => array('class' => 'input radio'),
  3466. 'fieldset' => array(),
  3467. 'legend' => array(),
  3468. 'Field',
  3469. '/legend',
  3470. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3471. array('label' => array('for' => 'Model1Field')),
  3472. __('empty'),
  3473. '/label',
  3474. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3475. array('label' => array('for' => 'Model1Field0')),
  3476. 'option A',
  3477. '/label',
  3478. '/fieldset',
  3479. '/div'
  3480. );
  3481. $this->assertTags($result, $expected);
  3482. $result = $this->Form->input('Model.1.field', array(
  3483. 'type' => 'radio',
  3484. 'options' => array('option A'),
  3485. 'empty' => 'CustomEmptyLabel',
  3486. 'hiddenField' => false
  3487. ));
  3488. $expected = array(
  3489. 'div' => array('class' => 'input radio'),
  3490. 'fieldset' => array(),
  3491. 'legend' => array(),
  3492. 'Field',
  3493. '/legend',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3495. array('label' => array('for' => 'Model1Field')),
  3496. 'CustomEmptyLabel',
  3497. '/label',
  3498. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3499. array('label' => array('for' => 'Model1Field0')),
  3500. 'option A',
  3501. '/label',
  3502. '/fieldset',
  3503. '/div'
  3504. );
  3505. $this->assertTags($result, $expected);
  3506. $result = $this->Form->input('Model.1.field', array(
  3507. 'type' => 'radio',
  3508. 'options' => array('option A'),
  3509. 'empty' => false,
  3510. 'hiddenField' => false
  3511. ));
  3512. $this->assertTextNotContains('"Model1Field"', $result);
  3513. }
  3514. /**
  3515. * testSelect method
  3516. *
  3517. * Test select element generation.
  3518. *
  3519. * @return void
  3520. */
  3521. public function testSelect() {
  3522. $result = $this->Form->select('Model.field', array());
  3523. $expected = array(
  3524. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3525. array('option' => array('value' => '')),
  3526. '/option',
  3527. '/select'
  3528. );
  3529. $this->assertTags($result, $expected);
  3530. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3531. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3532. $expected = array(
  3533. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3534. array('option' => array('value' => '')),
  3535. '/option',
  3536. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3537. 'good',
  3538. '/option',
  3539. array('option' => array('value' => 'other')),
  3540. 'bad',
  3541. '/option',
  3542. '/select'
  3543. );
  3544. $this->assertTags($result, $expected);
  3545. $this->Form->request->data = array();
  3546. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3547. $expected = array(
  3548. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3549. array('option' => array('value' => '')),
  3550. '/option',
  3551. array('option' => array('value' => 'value')),
  3552. 'good',
  3553. '/option',
  3554. array('option' => array('value' => 'other')),
  3555. 'bad',
  3556. '/option',
  3557. '/select'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. $result = $this->Form->select(
  3561. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3562. array('empty' => false)
  3563. );
  3564. $expected = array(
  3565. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3566. array('option' => array('value' => 'first')),
  3567. 'first &quot;html&quot; &lt;chars&gt;',
  3568. '/option',
  3569. array('option' => array('value' => 'second')),
  3570. 'value',
  3571. '/option',
  3572. '/select'
  3573. );
  3574. $this->assertTags($result, $expected);
  3575. $result = $this->Form->select(
  3576. 'Model.field',
  3577. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3578. array('escape' => false, 'empty' => false)
  3579. );
  3580. $expected = array(
  3581. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3582. array('option' => array('value' => 'first')),
  3583. 'first "html" <chars>',
  3584. '/option',
  3585. array('option' => array('value' => 'second')),
  3586. 'value',
  3587. '/option',
  3588. '/select'
  3589. );
  3590. $this->assertTags($result, $expected);
  3591. $options = array(
  3592. array('value' => 'first', 'name' => 'First'),
  3593. array('value' => 'first', 'name' => 'Another First'),
  3594. );
  3595. $result = $this->Form->select(
  3596. 'Model.field',
  3597. $options,
  3598. array('escape' => false, 'empty' => false)
  3599. );
  3600. $expected = array(
  3601. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3602. array('option' => array('value' => 'first')),
  3603. 'First',
  3604. '/option',
  3605. array('option' => array('value' => 'first')),
  3606. 'Another First',
  3607. '/option',
  3608. '/select'
  3609. );
  3610. $this->assertTags($result, $expected);
  3611. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3612. $result = $this->Form->select(
  3613. 'Model.contact_id',
  3614. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3615. array('escape' => false, 'empty' => 'pick something')
  3616. );
  3617. $expected = array(
  3618. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3619. array('option' => array('value' => '')), 'pick something', '/option',
  3620. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3621. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3622. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3623. '/select'
  3624. );
  3625. $this->assertTags($result, $expected);
  3626. $this->Form->request->data['Model']['field'] = 0;
  3627. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3628. $expected = array(
  3629. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3630. array('option' => array('value' => '')), '/option',
  3631. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3632. array('option' => array('value' => '1')), 'Yes', '/option',
  3633. '/select'
  3634. );
  3635. $this->assertTags($result, $expected);
  3636. $this->Form->request->data['Model']['field'] = 50;
  3637. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3638. $expected = array(
  3639. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3640. array('option' => array('value' => '')), '/option',
  3641. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3642. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3643. '/select'
  3644. );
  3645. $this->assertTags($result, $expected);
  3646. }
  3647. /**
  3648. * test that select() with optiongroups listens to the escape param.
  3649. *
  3650. * @return void
  3651. */
  3652. public function testSelectOptionGroupEscaping() {
  3653. $options = array(
  3654. '>< Key' => array(
  3655. 1 => 'One',
  3656. 2 => 'Two'
  3657. )
  3658. );
  3659. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3660. $expected = array(
  3661. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3662. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3663. array('option' => array('value' => '1')), 'One', '/option',
  3664. array('option' => array('value' => '2')), 'Two', '/option',
  3665. '/optgroup',
  3666. '/select'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. $options = array(
  3670. '>< Key' => array(
  3671. 1 => 'One',
  3672. 2 => 'Two'
  3673. )
  3674. );
  3675. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3676. $expected = array(
  3677. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3678. 'optgroup' => array('label' => '>< Key'),
  3679. array('option' => array('value' => '1')), 'One', '/option',
  3680. array('option' => array('value' => '2')), 'Two', '/option',
  3681. '/optgroup',
  3682. '/select'
  3683. );
  3684. $this->assertTags($result, $expected);
  3685. }
  3686. /**
  3687. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3688. *
  3689. * @return void
  3690. */
  3691. public function testSelectWithNullAttributes() {
  3692. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3693. $expected = array(
  3694. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3695. array('option' => array('value' => '0')),
  3696. 'first',
  3697. '/option',
  3698. array('option' => array('value' => '1')),
  3699. 'second',
  3700. '/option',
  3701. '/select'
  3702. );
  3703. $this->assertTags($result, $expected);
  3704. }
  3705. /**
  3706. * testNestedSelect method
  3707. *
  3708. * test select element generation with optgroups
  3709. *
  3710. * @return void
  3711. */
  3712. public function testNestedSelect() {
  3713. $result = $this->Form->select(
  3714. 'Model.field',
  3715. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3716. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3717. )), array('empty' => false)
  3718. );
  3719. $expected = array(
  3720. 'select' => array('name' => 'data[Model][field]',
  3721. 'id' => 'ModelField'),
  3722. array('option' => array('value' => 1)),
  3723. 'One',
  3724. '/option',
  3725. array('option' => array('value' => 2)),
  3726. 'Two',
  3727. '/option',
  3728. array('optgroup' => array('label' => 'Three')),
  3729. array('option' => array('value' => 4)),
  3730. 'Four',
  3731. '/option',
  3732. array('option' => array('value' => 5)),
  3733. 'Five',
  3734. '/option',
  3735. '/optgroup',
  3736. '/select'
  3737. );
  3738. $this->assertTags($result, $expected);
  3739. $result = $this->Form->select(
  3740. 'Model.field',
  3741. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3742. array('showParents' => true, 'empty' => false)
  3743. );
  3744. $expected = array(
  3745. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3746. array('option' => array('value' => 1)),
  3747. 'One',
  3748. '/option',
  3749. array('option' => array('value' => 2)),
  3750. 'Two',
  3751. '/option',
  3752. array('optgroup' => array('label' => 'Three')),
  3753. array('option' => array('value' => 3)),
  3754. 'Three',
  3755. '/option',
  3756. array('option' => array('value' => 4)),
  3757. 'Four',
  3758. '/option',
  3759. '/optgroup',
  3760. '/select'
  3761. );
  3762. $this->assertTags($result, $expected);
  3763. }
  3764. /**
  3765. * testSelectMultiple method
  3766. *
  3767. * test generation of multiple select elements
  3768. *
  3769. * @return void
  3770. */
  3771. public function testSelectMultiple() {
  3772. $options = array('first', 'second', 'third');
  3773. $result = $this->Form->select(
  3774. 'Model.multi_field', $options, array('multiple' => true)
  3775. );
  3776. $expected = array(
  3777. 'input' => array(
  3778. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3779. ),
  3780. 'select' => array(
  3781. 'name' => 'data[Model][multi_field][]',
  3782. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3783. ),
  3784. array('option' => array('value' => '0')),
  3785. 'first',
  3786. '/option',
  3787. array('option' => array('value' => '1')),
  3788. 'second',
  3789. '/option',
  3790. array('option' => array('value' => '2')),
  3791. 'third',
  3792. '/option',
  3793. '/select'
  3794. );
  3795. $this->assertTags($result, $expected);
  3796. $result = $this->Form->select(
  3797. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3798. );
  3799. $expected = array(
  3800. 'input' => array(
  3801. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3802. ),
  3803. 'select' => array(
  3804. 'name' => 'data[Model][multi_field][]',
  3805. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3806. ),
  3807. array('option' => array('value' => '0')),
  3808. 'first',
  3809. '/option',
  3810. array('option' => array('value' => '1')),
  3811. 'second',
  3812. '/option',
  3813. array('option' => array('value' => '2')),
  3814. 'third',
  3815. '/option',
  3816. '/select'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $result = $this->Form->select(
  3820. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3821. );
  3822. $expected = array(
  3823. 'input' => array(
  3824. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3825. ),
  3826. 'select' => array(
  3827. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3828. 'multiple' => 'multiple'
  3829. ),
  3830. array('option' => array('value' => '0', 'selected' => 'selected')),
  3831. 'first',
  3832. '/option',
  3833. array('option' => array('value' => '1', 'selected' => 'selected')),
  3834. 'second',
  3835. '/option',
  3836. array('option' => array('value' => '2')),
  3837. 'third',
  3838. '/option',
  3839. '/select'
  3840. );
  3841. $this->assertTags($result, $expected);
  3842. $result = $this->Form->select(
  3843. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3844. );
  3845. $expected = array(
  3846. 'select' => array(
  3847. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3848. ),
  3849. array('option' => array('value' => '0', 'selected' => 'selected')),
  3850. 'first',
  3851. '/option',
  3852. array('option' => array('value' => '1', 'selected' => 'selected')),
  3853. 'second',
  3854. '/option',
  3855. array('option' => array('value' => '2')),
  3856. 'third',
  3857. '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. }
  3862. /**
  3863. * test generation of habtm select boxes.
  3864. *
  3865. * @return void
  3866. */
  3867. public function testHabtmSelectBox() {
  3868. $this->View->viewVars['contactTags'] = array(
  3869. 1 => 'blue',
  3870. 2 => 'red',
  3871. 3 => 'green'
  3872. );
  3873. $this->Form->request->data = array(
  3874. 'Contact' => array(),
  3875. 'ContactTag' => array(
  3876. array(
  3877. 'id' => 1,
  3878. 'name' => 'blue'
  3879. ),
  3880. array(
  3881. 'id' => 3,
  3882. 'name' => 'green'
  3883. )
  3884. )
  3885. );
  3886. $this->Form->create('Contact');
  3887. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3888. $expected = array(
  3889. 'input' => array(
  3890. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3891. ),
  3892. 'select' => array(
  3893. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3894. 'multiple' => 'multiple'
  3895. ),
  3896. array('option' => array('value' => '1', 'selected' => 'selected')),
  3897. 'blue',
  3898. '/option',
  3899. array('option' => array('value' => '2')),
  3900. 'red',
  3901. '/option',
  3902. array('option' => array('value' => '3', 'selected' => 'selected')),
  3903. 'green',
  3904. '/option',
  3905. '/select'
  3906. );
  3907. $this->assertTags($result, $expected);
  3908. }
  3909. /**
  3910. * test generation of multi select elements in checkbox format
  3911. *
  3912. * @return void
  3913. */
  3914. public function testSelectMultipleCheckboxes() {
  3915. $result = $this->Form->select(
  3916. 'Model.multi_field',
  3917. array('first', 'second', 'third'),
  3918. array('multiple' => 'checkbox')
  3919. );
  3920. $expected = array(
  3921. 'input' => array(
  3922. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3923. ),
  3924. array('div' => array('class' => 'checkbox')),
  3925. array('input' => array(
  3926. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3927. 'value' => '0', 'id' => 'ModelMultiField0'
  3928. )),
  3929. array('label' => array('for' => 'ModelMultiField0')),
  3930. 'first',
  3931. '/label',
  3932. '/div',
  3933. array('div' => array('class' => 'checkbox')),
  3934. array('input' => array(
  3935. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3936. 'value' => '1', 'id' => 'ModelMultiField1'
  3937. )),
  3938. array('label' => array('for' => 'ModelMultiField1')),
  3939. 'second',
  3940. '/label',
  3941. '/div',
  3942. array('div' => array('class' => 'checkbox')),
  3943. array('input' => array(
  3944. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3945. 'value' => '2', 'id' => 'ModelMultiField2'
  3946. )),
  3947. array('label' => array('for' => 'ModelMultiField2')),
  3948. 'third',
  3949. '/label',
  3950. '/div'
  3951. );
  3952. $this->assertTags($result, $expected);
  3953. $result = $this->Form->select(
  3954. 'Model.multi_field',
  3955. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3956. array('multiple' => 'checkbox')
  3957. );
  3958. $expected = array(
  3959. 'input' => array(
  3960. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3961. ),
  3962. array('div' => array('class' => 'checkbox')),
  3963. array('input' => array(
  3964. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3965. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3966. )),
  3967. array('label' => array('for' => 'ModelMultiFieldA')),
  3968. 'first',
  3969. '/label',
  3970. '/div',
  3971. array('div' => array('class' => 'checkbox')),
  3972. array('input' => array(
  3973. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3974. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3975. )),
  3976. array('label' => array('for' => 'ModelMultiFieldB')),
  3977. 'second',
  3978. '/label',
  3979. '/div',
  3980. array('div' => array('class' => 'checkbox')),
  3981. array('input' => array(
  3982. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3983. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3984. )),
  3985. array('label' => array('for' => 'ModelMultiFieldC')),
  3986. 'third',
  3987. '/label',
  3988. '/div'
  3989. );
  3990. $this->assertTags($result, $expected);
  3991. $result = $this->Form->select(
  3992. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3993. );
  3994. $expected = array(
  3995. 'input' => array(
  3996. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3997. ),
  3998. array('div' => array('class' => 'checkbox')),
  3999. array('input' => array(
  4000. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4001. 'value' => '1', 'id' => 'ModelMultiField1'
  4002. )),
  4003. array('label' => array('for' => 'ModelMultiField1')),
  4004. 'first',
  4005. '/label',
  4006. '/div'
  4007. );
  4008. $this->assertTags($result, $expected);
  4009. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4010. $result = $this->Form->select(
  4011. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4012. );
  4013. $expected = array(
  4014. 'input' => array(
  4015. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4016. ),
  4017. array('div' => array('class' => 'checkbox')),
  4018. array('input' => array(
  4019. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4020. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4021. )),
  4022. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4023. 'first',
  4024. '/label',
  4025. '/div',
  4026. array('div' => array('class' => 'checkbox')),
  4027. array('input' => array(
  4028. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4029. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4030. )),
  4031. array('label' => array('for' => 'ModelTagsArray')),
  4032. 'Array',
  4033. '/label',
  4034. '/div'
  4035. );
  4036. $this->assertTags($result, $expected);
  4037. }
  4038. /**
  4039. * test multiple checkboxes with div styles.
  4040. *
  4041. * @return void
  4042. */
  4043. public function testSelectMultipleCheckboxDiv() {
  4044. $result = $this->Form->select(
  4045. 'Model.tags',
  4046. array('first', 'second'),
  4047. array('multiple' => 'checkbox', 'class' => 'my-class')
  4048. );
  4049. $expected = array(
  4050. 'input' => array(
  4051. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4052. ),
  4053. array('div' => array('class' => 'my-class')),
  4054. array('input' => array(
  4055. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4056. 'value' => '0', 'id' => 'ModelTags0'
  4057. )),
  4058. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4059. '/div',
  4060. array('div' => array('class' => 'my-class')),
  4061. array('input' => array(
  4062. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4063. 'value' => '1', 'id' => 'ModelTags1'
  4064. )),
  4065. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4066. '/div'
  4067. );
  4068. $this->assertTags($result, $expected);
  4069. $result = $this->Form->input('Model.tags', array(
  4070. 'options' => array('first', 'second'),
  4071. 'multiple' => 'checkbox',
  4072. 'class' => 'my-class',
  4073. 'div' => false,
  4074. 'label' => false
  4075. ));
  4076. $this->assertTags($result, $expected);
  4077. $Contact = ClassRegistry::getObject('Contact');
  4078. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4079. $result = $this->Form->input('Contact.tags', array(
  4080. 'options' => array('one'),
  4081. 'multiple' => 'checkbox',
  4082. 'label' => false,
  4083. 'div' => false
  4084. ));
  4085. $expected = array(
  4086. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4087. array('div' => array('class' => 'checkbox form-error')),
  4088. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4089. array('label' => array('for' => 'ContactTags0')),
  4090. 'one',
  4091. '/label',
  4092. '/div'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. $result = $this->Form->input('Contact.tags', array(
  4096. 'options' => array('one'),
  4097. 'multiple' => 'checkbox',
  4098. 'class' => 'mycheckbox',
  4099. 'label' => false,
  4100. 'div' => false
  4101. ));
  4102. $expected = array(
  4103. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4104. array('div' => array('class' => 'mycheckbox form-error')),
  4105. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4106. array('label' => array('for' => 'ContactTags0')),
  4107. 'one',
  4108. '/label',
  4109. '/div'
  4110. );
  4111. $this->assertTags($result, $expected);
  4112. }
  4113. /**
  4114. * Checks the security hash array generated for multiple-input checkbox elements
  4115. *
  4116. * @return void
  4117. */
  4118. public function testSelectMultipleCheckboxSecurity() {
  4119. $this->Form->request['_Token'] = array('key' => 'testKey');
  4120. $this->assertEquals(array(), $this->Form->fields);
  4121. $result = $this->Form->select(
  4122. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4123. array('multiple' => 'checkbox')
  4124. );
  4125. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4126. $result = $this->Form->secure($this->Form->fields);
  4127. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4128. $this->assertRegExp('/"' . $key . '"/', $result);
  4129. }
  4130. /**
  4131. * Multiple select elements should always be secured as they always participate
  4132. * in the POST data.
  4133. *
  4134. * @return void
  4135. */
  4136. public function testSelectMultipleSecureWithNoOptions() {
  4137. $this->Form->request['_Token'] = array('key' => 'testkey');
  4138. $this->assertEquals(array(), $this->Form->fields);
  4139. $this->Form->select(
  4140. 'Model.select',
  4141. array(),
  4142. array('multiple' => true)
  4143. );
  4144. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4145. }
  4146. /**
  4147. * When a select box has no options it should not be added to the fields list
  4148. * as it always fail post validation.
  4149. *
  4150. * @return void
  4151. */
  4152. public function testSelectNoSecureWithNoOptions() {
  4153. $this->Form->request['_Token'] = array('key' => 'testkey');
  4154. $this->assertEquals(array(), $this->Form->fields);
  4155. $this->Form->select(
  4156. 'Model.select',
  4157. array()
  4158. );
  4159. $this->assertEquals(array(), $this->Form->fields);
  4160. $this->Form->select(
  4161. 'Model.select',
  4162. array(),
  4163. array('empty' => true)
  4164. );
  4165. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4166. }
  4167. /**
  4168. * testInputMultipleCheckboxes method
  4169. *
  4170. * test input() resulting in multi select elements being generated.
  4171. *
  4172. * @return void
  4173. */
  4174. public function testInputMultipleCheckboxes() {
  4175. $result = $this->Form->input('Model.multi_field', array(
  4176. 'options' => array('first', 'second', 'third'),
  4177. 'multiple' => 'checkbox'
  4178. ));
  4179. $expected = array(
  4180. array('div' => array('class' => 'input select')),
  4181. array('label' => array('for' => 'ModelMultiField')),
  4182. 'Multi Field',
  4183. '/label',
  4184. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4185. array('div' => array('class' => 'checkbox')),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4187. array('label' => array('for' => 'ModelMultiField0')),
  4188. 'first',
  4189. '/label',
  4190. '/div',
  4191. array('div' => array('class' => 'checkbox')),
  4192. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4193. array('label' => array('for' => 'ModelMultiField1')),
  4194. 'second',
  4195. '/label',
  4196. '/div',
  4197. array('div' => array('class' => 'checkbox')),
  4198. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4199. array('label' => array('for' => 'ModelMultiField2')),
  4200. 'third',
  4201. '/label',
  4202. '/div',
  4203. '/div'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $result = $this->Form->input('Model.multi_field', array(
  4207. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4208. 'multiple' => 'checkbox'
  4209. ));
  4210. $expected = array(
  4211. array('div' => array('class' => 'input select')),
  4212. array('label' => array('for' => 'ModelMultiField')),
  4213. 'Multi Field',
  4214. '/label',
  4215. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4216. array('div' => array('class' => 'checkbox')),
  4217. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4218. array('label' => array('for' => 'ModelMultiFieldA')),
  4219. 'first',
  4220. '/label',
  4221. '/div',
  4222. array('div' => array('class' => 'checkbox')),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4224. array('label' => array('for' => 'ModelMultiFieldB')),
  4225. 'second',
  4226. '/label',
  4227. '/div',
  4228. array('div' => array('class' => 'checkbox')),
  4229. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4230. array('label' => array('for' => 'ModelMultiFieldC')),
  4231. 'third',
  4232. '/label',
  4233. '/div',
  4234. '/div'
  4235. );
  4236. $this->assertTags($result, $expected);
  4237. $result = $this->Form->input('Model.multi_field', array(
  4238. 'options' => array('1' => 'first'),
  4239. 'multiple' => 'checkbox',
  4240. 'label' => false,
  4241. 'div' => false
  4242. ));
  4243. $expected = array(
  4244. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4245. array('div' => array('class' => 'checkbox')),
  4246. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4247. array('label' => array('for' => 'ModelMultiField1')),
  4248. 'first',
  4249. '/label',
  4250. '/div'
  4251. );
  4252. $this->assertTags($result, $expected);
  4253. $result = $this->Form->input('Model.multi_field', array(
  4254. 'options' => array('2' => 'second'),
  4255. 'multiple' => 'checkbox',
  4256. 'label' => false,
  4257. 'div' => false
  4258. ));
  4259. $expected = array(
  4260. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4261. array('div' => array('class' => 'checkbox')),
  4262. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4263. array('label' => array('for' => 'ModelMultiField2')),
  4264. 'second',
  4265. '/label',
  4266. '/div'
  4267. );
  4268. $this->assertTags($result, $expected);
  4269. }
  4270. /**
  4271. * testSelectHiddenFieldOmission method
  4272. *
  4273. * test that select() with 'hiddenField' => false omits the hidden field
  4274. *
  4275. * @return void
  4276. */
  4277. public function testSelectHiddenFieldOmission() {
  4278. $result = $this->Form->select('Model.multi_field',
  4279. array('first', 'second'),
  4280. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4281. );
  4282. $expected = array(
  4283. array('div' => array('class' => 'checkbox')),
  4284. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4285. array('label' => array('for' => 'ModelMultiField0')),
  4286. 'first',
  4287. '/label',
  4288. '/div',
  4289. array('div' => array('class' => 'checkbox')),
  4290. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4291. array('label' => array('for' => 'ModelMultiField1')),
  4292. 'second',
  4293. '/label',
  4294. '/div'
  4295. );
  4296. $this->assertTags($result, $expected);
  4297. $result = $this->Form->input('Model.multi_field', array(
  4298. 'options' => array('first', 'second'),
  4299. 'multiple' => 'checkbox',
  4300. 'hiddenField' => false
  4301. ));
  4302. $expected = array(
  4303. array('div' => array('class' => 'input select')),
  4304. array('label' => array('for' => 'ModelMultiField')),
  4305. 'Multi Field',
  4306. '/label',
  4307. array('div' => array('class' => 'checkbox')),
  4308. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4309. array('label' => array('for' => 'ModelMultiField0')),
  4310. 'first',
  4311. '/label',
  4312. '/div',
  4313. array('div' => array('class' => 'checkbox')),
  4314. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4315. array('label' => array('for' => 'ModelMultiField1')),
  4316. 'second',
  4317. '/label',
  4318. '/div',
  4319. '/div'
  4320. );
  4321. $this->assertTags($result, $expected);
  4322. }
  4323. /**
  4324. * test that select() with multiple = checkbox works with overriding name attribute.
  4325. *
  4326. * @return void
  4327. */
  4328. public function testSelectCheckboxMultipleOverrideName() {
  4329. $result = $this->Form->input('category', array(
  4330. 'type' => 'select',
  4331. 'multiple' => 'checkbox',
  4332. 'name' => 'data[fish]',
  4333. 'options' => array('1', '2'),
  4334. 'div' => false,
  4335. 'label' => false,
  4336. ));
  4337. $expected = array(
  4338. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4339. array('div' => array('class' => 'checkbox')),
  4340. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4341. array('label' => array('for' => 'Category0')), '1', '/label',
  4342. '/div',
  4343. array('div' => array('class' => 'checkbox')),
  4344. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4345. array('label' => array('for' => 'Category1')), '2', '/label',
  4346. '/div'
  4347. );
  4348. $this->assertTags($result, $expected);
  4349. }
  4350. /**
  4351. * Test that 'id' overrides all the checkbox id's as well.
  4352. *
  4353. * @return void
  4354. */
  4355. public function testSelectCheckboxMultipleId() {
  4356. $result = $this->Form->select(
  4357. 'Model.multi_field',
  4358. array('first', 'second', 'third'),
  4359. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4360. );
  4361. $expected = array(
  4362. 'input' => array(
  4363. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4364. ),
  4365. array('div' => array('class' => 'checkbox')),
  4366. array('input' => array(
  4367. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4368. 'value' => '0', 'id' => 'CustomId0'
  4369. )),
  4370. array('label' => array('for' => 'CustomId0')),
  4371. 'first',
  4372. '/label',
  4373. '/div',
  4374. array('div' => array('class' => 'checkbox')),
  4375. array('input' => array(
  4376. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4377. 'value' => '1', 'id' => 'CustomId1'
  4378. )),
  4379. array('label' => array('for' => 'CustomId1')),
  4380. 'second',
  4381. '/label',
  4382. '/div',
  4383. array('div' => array('class' => 'checkbox')),
  4384. array('input' => array(
  4385. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4386. 'value' => '2', 'id' => 'CustomId2'
  4387. )),
  4388. array('label' => array('for' => 'CustomId2')),
  4389. 'third',
  4390. '/label',
  4391. '/div'
  4392. );
  4393. $this->assertTags($result, $expected);
  4394. }
  4395. /**
  4396. * testCheckbox method
  4397. *
  4398. * Test generation of checkboxes
  4399. *
  4400. * @return void
  4401. */
  4402. public function testCheckbox() {
  4403. $result = $this->Form->checkbox('Model.field');
  4404. $expected = array(
  4405. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4406. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4410. $expected = array(
  4411. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4412. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4413. );
  4414. $this->assertTags($result, $expected);
  4415. $Contact = ClassRegistry::getObject('Contact');
  4416. $Contact->validationErrors['field'] = 1;
  4417. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4418. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4419. $expected = array(
  4420. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4421. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4422. );
  4423. $this->assertTags($result, $expected);
  4424. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4425. $expected = array(
  4426. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4427. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4428. );
  4429. $this->assertTags($result, $expected);
  4430. $this->Form->request->data['Contact']['field'] = '';
  4431. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4432. $expected = array(
  4433. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4434. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4435. );
  4436. $this->assertTags($result, $expected);
  4437. $Contact->validationErrors = array();
  4438. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4439. $expected = array(
  4440. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4441. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4442. );
  4443. $this->assertTags($result, $expected);
  4444. $this->Form->request->data['Contact']['published'] = 1;
  4445. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4446. $expected = array(
  4447. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4448. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4449. );
  4450. $this->assertTags($result, $expected);
  4451. $this->Form->request->data['Contact']['published'] = 0;
  4452. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4453. $expected = array(
  4454. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4455. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4456. );
  4457. $this->assertTags($result, $expected);
  4458. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4459. $expected = array(
  4460. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4461. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4462. );
  4463. $this->assertTags($result, $expected);
  4464. $result = $this->Form->checkbox('CustomField.1.value');
  4465. $expected = array(
  4466. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4467. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4468. );
  4469. $this->assertTags($result, $expected);
  4470. }
  4471. /**
  4472. * test checkbox() with a custom name attribute
  4473. *
  4474. * @return void
  4475. */
  4476. public function testCheckboxCustomNameAttribute() {
  4477. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4478. $expected = array(
  4479. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4480. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4481. );
  4482. $this->assertTags($result, $expected);
  4483. }
  4484. /**
  4485. * test the checked option for checkboxes.
  4486. *
  4487. * @return void
  4488. */
  4489. public function testCheckboxCheckedOption() {
  4490. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4491. $expected = array(
  4492. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4493. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4494. );
  4495. $this->assertTags($result, $expected);
  4496. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4497. $expected = array(
  4498. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4499. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4500. );
  4501. $this->assertTags($result, $expected);
  4502. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4503. $expected = array(
  4504. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4505. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4506. );
  4507. $this->assertTags($result, $expected);
  4508. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4509. $expected = array(
  4510. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4511. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4512. );
  4513. $this->assertTags($result, $expected);
  4514. $this->Form->request->data['Model']['field'] = 1;
  4515. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4516. $expected = array(
  4517. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4518. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4519. );
  4520. $this->assertTags($result, $expected);
  4521. }
  4522. /**
  4523. * Test that disabled attribute works on both the checkbox and hidden input.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testCheckboxDisabling() {
  4528. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4529. $expected = array(
  4530. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4531. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4532. );
  4533. $this->assertTags($result, $expected);
  4534. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4535. $expected = array(
  4536. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4537. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4538. );
  4539. $this->assertTags($result, $expected);
  4540. }
  4541. /**
  4542. * Test that the hidden input for checkboxes can be omitted or set to a
  4543. * specific value.
  4544. *
  4545. * @return void
  4546. */
  4547. public function testCheckboxHiddenField() {
  4548. $result = $this->Form->input('UserForm.something', array(
  4549. 'type' => 'checkbox',
  4550. 'hiddenField' => false
  4551. ));
  4552. $expected = array(
  4553. 'div' => array('class' => 'input checkbox'),
  4554. array('input' => array(
  4555. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4556. 'value' => '1', 'id' => 'UserFormSomething'
  4557. )),
  4558. 'label' => array('for' => 'UserFormSomething'),
  4559. 'Something',
  4560. '/label',
  4561. '/div'
  4562. );
  4563. $this->assertTags($result, $expected);
  4564. $result = $this->Form->input('UserForm.something', array(
  4565. 'type' => 'checkbox',
  4566. 'value' => 'Y',
  4567. 'hiddenField' => 'N',
  4568. ));
  4569. $expected = array(
  4570. 'div' => array('class' => 'input checkbox'),
  4571. array('input' => array(
  4572. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4573. 'value' => 'N', 'id' => 'UserFormSomething_'
  4574. )),
  4575. array('input' => array(
  4576. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4577. 'value' => 'Y', 'id' => 'UserFormSomething'
  4578. )),
  4579. 'label' => array('for' => 'UserFormSomething'),
  4580. 'Something',
  4581. '/label',
  4582. '/div'
  4583. );
  4584. $this->assertTags($result, $expected);
  4585. }
  4586. /**
  4587. * testDateTime method
  4588. *
  4589. * Test generation of date/time select elements
  4590. *
  4591. * @return void
  4592. */
  4593. public function testDateTime() {
  4594. extract($this->dateRegex);
  4595. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4596. $now = strtotime('now');
  4597. $expected = array(
  4598. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4599. $daysRegex,
  4600. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4601. date('j', $now),
  4602. '/option',
  4603. '*/select',
  4604. '-',
  4605. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4606. $monthsRegex,
  4607. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4608. date('F', $now),
  4609. '/option',
  4610. '*/select',
  4611. '-',
  4612. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4613. $yearsRegex,
  4614. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4615. date('Y', $now),
  4616. '/option',
  4617. '*/select',
  4618. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4619. $hoursRegex,
  4620. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4621. date('g', $now),
  4622. '/option',
  4623. '*/select',
  4624. ':',
  4625. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4626. $minutesRegex,
  4627. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4628. date('i', $now),
  4629. '/option',
  4630. '*/select',
  4631. ' ',
  4632. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4633. $meridianRegex,
  4634. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4635. date('a', $now),
  4636. '/option',
  4637. '*/select'
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4641. $expected = array(
  4642. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4643. $daysRegex,
  4644. array('option' => array('value' => '')),
  4645. '/option',
  4646. '*/select',
  4647. '-',
  4648. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4649. $monthsRegex,
  4650. array('option' => array('value' => '')),
  4651. '/option',
  4652. '*/select',
  4653. '-',
  4654. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4655. $yearsRegex,
  4656. array('option' => array('value' => '')),
  4657. '/option',
  4658. '*/select',
  4659. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4660. $hoursRegex,
  4661. array('option' => array('value' => '')),
  4662. '/option',
  4663. '*/select',
  4664. ':',
  4665. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4666. $minutesRegex,
  4667. array('option' => array('value' => '')),
  4668. '/option',
  4669. '*/select',
  4670. ' ',
  4671. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4672. $meridianRegex,
  4673. array('option' => array('value' => '')),
  4674. '/option',
  4675. '*/select'
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4679. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4680. $this->assertTags($result, $expected);
  4681. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4682. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4683. $this->assertTags($result, $expected);
  4684. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4685. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4686. $expected = array(
  4687. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4688. $daysRegex,
  4689. array('option' => array('value' => '')),
  4690. '/option',
  4691. '*/select',
  4692. '-',
  4693. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4694. $monthsRegex,
  4695. array('option' => array('value' => '')),
  4696. '/option',
  4697. '*/select',
  4698. '-',
  4699. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4700. $yearsRegex,
  4701. array('option' => array('value' => '')),
  4702. '/option',
  4703. '*/select',
  4704. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4705. $hoursRegex,
  4706. array('option' => array('value' => '')),
  4707. '/option',
  4708. '*/select',
  4709. ':',
  4710. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4711. $minutesRegex,
  4712. array('option' => array('value' => '')),
  4713. '/option',
  4714. array('option' => array('value' => '00')),
  4715. '00',
  4716. '/option',
  4717. array('option' => array('value' => '05')),
  4718. '05',
  4719. '/option',
  4720. array('option' => array('value' => '10')),
  4721. '10',
  4722. '/option',
  4723. '*/select',
  4724. ' ',
  4725. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4726. $meridianRegex,
  4727. array('option' => array('value' => '')),
  4728. '/option',
  4729. '*/select'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4733. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4734. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4735. $this->Form->request->data['Contact']['data'] = null;
  4736. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4737. $expected = array(
  4738. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4739. $daysRegex,
  4740. array('option' => array('value' => '')),
  4741. '/option',
  4742. '*/select',
  4743. '-',
  4744. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4745. $monthsRegex,
  4746. array('option' => array('value' => '')),
  4747. '/option',
  4748. '*/select',
  4749. '-',
  4750. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4751. $yearsRegex,
  4752. array('option' => array('value' => '')),
  4753. '/option',
  4754. '*/select',
  4755. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4756. $hoursRegex,
  4757. array('option' => array('value' => '')),
  4758. '/option',
  4759. '*/select',
  4760. ':',
  4761. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4762. $minutesRegex,
  4763. array('option' => array('value' => '')),
  4764. '/option',
  4765. '*/select',
  4766. ' ',
  4767. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4768. $meridianRegex,
  4769. array('option' => array('value' => '')),
  4770. '/option',
  4771. '*/select'
  4772. );
  4773. $this->assertTags($result, $expected);
  4774. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4775. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4776. $now = strtotime($this->Form->data['Model']['field']);
  4777. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4778. $expected = array(
  4779. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4780. $daysRegex,
  4781. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4782. date('j', $now),
  4783. '/option',
  4784. '*/select',
  4785. '-',
  4786. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4787. $monthsRegex,
  4788. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4789. date('F', $now),
  4790. '/option',
  4791. '*/select',
  4792. '-',
  4793. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4794. $yearsRegex,
  4795. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4796. date('Y', $now),
  4797. '/option',
  4798. '*/select',
  4799. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4800. $hoursRegex,
  4801. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4802. date('g', $now),
  4803. '/option',
  4804. '*/select',
  4805. ':',
  4806. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4807. $minutesRegex,
  4808. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4809. date('i', $now),
  4810. '/option',
  4811. '*/select',
  4812. ' ',
  4813. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4814. $meridianRegex,
  4815. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4816. date('a', $now),
  4817. '/option',
  4818. '*/select'
  4819. );
  4820. $this->assertTags($result, $expected);
  4821. $selected = strtotime('2008-10-26 12:33:00');
  4822. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4823. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4824. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4825. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4826. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4827. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4828. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4829. $this->Form->create('Contact');
  4830. $result = $this->Form->input('published');
  4831. $now = strtotime('now');
  4832. $expected = array(
  4833. 'div' => array('class' => 'input date'),
  4834. 'label' => array('for' => 'ContactPublishedMonth'),
  4835. 'Published',
  4836. '/label',
  4837. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4838. $monthsRegex,
  4839. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4840. date('F', $now),
  4841. '/option',
  4842. '*/select',
  4843. '-',
  4844. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4845. $daysRegex,
  4846. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4847. date('j', $now),
  4848. '/option',
  4849. '*/select',
  4850. '-',
  4851. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4852. $yearsRegex,
  4853. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4854. date('Y', $now),
  4855. '/option',
  4856. '*/select',
  4857. '/div'
  4858. );
  4859. $this->assertTags($result, $expected);
  4860. $result = $this->Form->input('published2', array('type' => 'date'));
  4861. $now = strtotime('now');
  4862. $expected = array(
  4863. 'div' => array('class' => 'input date'),
  4864. 'label' => array('for' => 'ContactPublished2Month'),
  4865. 'Published2',
  4866. '/label',
  4867. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4868. $monthsRegex,
  4869. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4870. date('F', $now),
  4871. '/option',
  4872. '*/select',
  4873. '-',
  4874. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4875. $daysRegex,
  4876. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4877. date('j', $now),
  4878. '/option',
  4879. '*/select',
  4880. '-',
  4881. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4882. $yearsRegex,
  4883. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4884. date('Y', $now),
  4885. '/option',
  4886. '*/select',
  4887. '/div'
  4888. );
  4889. $this->assertTags($result, $expected);
  4890. $this->Form->create('Contact');
  4891. $result = $this->Form->input('published', array('monthNames' => false));
  4892. $now = strtotime('now');
  4893. $expected = array(
  4894. 'div' => array('class' => 'input date'),
  4895. 'label' => array('for' => 'ContactPublishedMonth'),
  4896. 'Published',
  4897. '/label',
  4898. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4899. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4900. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4901. date('m', $now),
  4902. '/option',
  4903. '*/select',
  4904. '-',
  4905. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4906. $daysRegex,
  4907. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4908. date('j', $now),
  4909. '/option',
  4910. '*/select',
  4911. '-',
  4912. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4913. $yearsRegex,
  4914. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4915. date('Y', $now),
  4916. '/option',
  4917. '*/select',
  4918. '/div'
  4919. );
  4920. $this->assertTags($result, $expected);
  4921. $result = $this->Form->input('published', array('type' => 'time'));
  4922. $now = strtotime('now');
  4923. $expected = array(
  4924. 'div' => array('class' => 'input time'),
  4925. 'label' => array('for' => 'ContactPublishedHour'),
  4926. 'Published',
  4927. '/label',
  4928. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4929. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4930. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4931. date('g', $now),
  4932. '/option',
  4933. '*/select',
  4934. ':',
  4935. );
  4936. $this->assertTags($result, $expected);
  4937. $result = $this->Form->input('published', array(
  4938. 'timeFormat' => 24,
  4939. 'interval' => 5,
  4940. 'selected' => strtotime('2009-09-03 13:37:00'),
  4941. 'type' => 'datetime'
  4942. ));
  4943. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4944. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4945. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4946. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4947. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4948. $this->assertNoErrors();
  4949. $this->Form->request->data['Contact'] = array(
  4950. 'date' => array(
  4951. 'day' => '',
  4952. 'month' => '',
  4953. 'year' => '',
  4954. 'hour' => '',
  4955. 'min' => '',
  4956. 'meridian' => ''
  4957. )
  4958. );
  4959. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4960. }
  4961. /**
  4962. * test that datetime() and default values work.
  4963. *
  4964. * @return void
  4965. */
  4966. public function testDatetimeWithDefault() {
  4967. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4968. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4969. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4970. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4971. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4972. 'default' => '2009-06-01 11:15:30'
  4973. ));
  4974. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4975. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4976. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4977. }
  4978. /**
  4979. * test that bogus non-date time data doesn't cause errors.
  4980. *
  4981. * @return void
  4982. */
  4983. public function testDateTimeWithBogusData() {
  4984. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4985. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4986. }
  4987. /**
  4988. * testDateTimeEmptyAsArray
  4989. *
  4990. * @return void
  4991. */
  4992. public function testDateTimeEmptyAsArray() {
  4993. $result = $this->Form->dateTime('Contact.date',
  4994. 'DMY',
  4995. '12',
  4996. array(
  4997. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4998. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4999. )
  5000. )
  5001. );
  5002. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5003. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5004. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5005. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5006. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5007. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5008. $result = $this->Form->dateTime('Contact.date',
  5009. 'DMY',
  5010. '12',
  5011. array(
  5012. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5013. )
  5014. );
  5015. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5016. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5017. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5018. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5019. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5020. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5021. }
  5022. /**
  5023. * testFormDateTimeMulti method
  5024. *
  5025. * test multiple datetime element generation
  5026. *
  5027. * @return void
  5028. */
  5029. public function testFormDateTimeMulti() {
  5030. extract($this->dateRegex);
  5031. $result = $this->Form->dateTime('Contact.1.updated');
  5032. $expected = array(
  5033. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5034. $daysRegex,
  5035. array('option' => array('value' => '')),
  5036. '/option',
  5037. '*/select',
  5038. '-',
  5039. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5040. $monthsRegex,
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. '*/select',
  5044. '-',
  5045. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5046. $yearsRegex,
  5047. array('option' => array('value' => '')),
  5048. '/option',
  5049. '*/select',
  5050. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5051. $hoursRegex,
  5052. array('option' => array('value' => '')),
  5053. '/option',
  5054. '*/select',
  5055. ':',
  5056. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5057. $minutesRegex,
  5058. array('option' => array('value' => '')),
  5059. '/option',
  5060. '*/select',
  5061. ' ',
  5062. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5063. $meridianRegex,
  5064. array('option' => array('value' => '')),
  5065. '/option',
  5066. '*/select'
  5067. );
  5068. $this->assertTags($result, $expected);
  5069. $result = $this->Form->dateTime('Contact.2.updated');
  5070. $expected = array(
  5071. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5072. $daysRegex,
  5073. array('option' => array('value' => '')),
  5074. '/option',
  5075. '*/select',
  5076. '-',
  5077. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5078. $monthsRegex,
  5079. array('option' => array('value' => '')),
  5080. '/option',
  5081. '*/select',
  5082. '-',
  5083. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5084. $yearsRegex,
  5085. array('option' => array('value' => '')),
  5086. '/option',
  5087. '*/select',
  5088. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5089. $hoursRegex,
  5090. array('option' => array('value' => '')),
  5091. '/option',
  5092. '*/select',
  5093. ':',
  5094. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5095. $minutesRegex,
  5096. array('option' => array('value' => '')),
  5097. '/option',
  5098. '*/select',
  5099. ' ',
  5100. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5101. $meridianRegex,
  5102. array('option' => array('value' => '')),
  5103. '/option',
  5104. '*/select'
  5105. );
  5106. $this->assertTags($result, $expected);
  5107. }
  5108. /**
  5109. * When changing the date format, the label should always focus the first select box when
  5110. * clicked.
  5111. *
  5112. * @return void
  5113. */
  5114. public function testDateTimeLabelIdMatchesFirstInput() {
  5115. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5116. $this->assertContains('label for="ModelDateMonth"', $result);
  5117. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5118. $this->assertContains('label for="ModelDateDay"', $result);
  5119. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5120. $this->assertContains('label for="ModelDateYear"', $result);
  5121. }
  5122. /**
  5123. * testMonth method
  5124. *
  5125. * @return void
  5126. */
  5127. public function testMonth() {
  5128. $result = $this->Form->month('Model.field');
  5129. $expected = array(
  5130. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5131. array('option' => array('value' => '')),
  5132. '/option',
  5133. array('option' => array('value' => '01')),
  5134. date('F', strtotime('2008-01-01 00:00:00')),
  5135. '/option',
  5136. array('option' => array('value' => '02')),
  5137. date('F', strtotime('2008-02-01 00:00:00')),
  5138. '/option',
  5139. '*/select',
  5140. );
  5141. $this->assertTags($result, $expected);
  5142. $result = $this->Form->month('Model.field', array('empty' => true));
  5143. $expected = array(
  5144. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5145. array('option' => array('value' => '')),
  5146. '/option',
  5147. array('option' => array('value' => '01')),
  5148. date('F', strtotime('2008-01-01 00:00:00')),
  5149. '/option',
  5150. array('option' => array('value' => '02')),
  5151. date('F', strtotime('2008-02-01 00:00:00')),
  5152. '/option',
  5153. '*/select',
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5157. $expected = array(
  5158. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5159. array('option' => array('value' => '')),
  5160. '/option',
  5161. array('option' => array('value' => '01')),
  5162. '01',
  5163. '/option',
  5164. array('option' => array('value' => '02')),
  5165. '02',
  5166. '/option',
  5167. '*/select',
  5168. );
  5169. $this->assertTags($result, $expected);
  5170. $monthNames = array(
  5171. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5172. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5173. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5174. $expected = array(
  5175. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5176. array('option' => array('value' => '')),
  5177. '/option',
  5178. array('option' => array('value' => '01')),
  5179. 'Jan',
  5180. '/option',
  5181. array('option' => array('value' => '02')),
  5182. 'Feb',
  5183. '/option',
  5184. '*/select',
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. }
  5188. /**
  5189. * testDay method
  5190. *
  5191. * @return void
  5192. */
  5193. public function testDay() {
  5194. extract($this->dateRegex);
  5195. $result = $this->Form->day('Model.field', array('value' => false));
  5196. $expected = array(
  5197. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5198. array('option' => array('value' => '')),
  5199. '/option',
  5200. array('option' => array('value' => '01')),
  5201. '1',
  5202. '/option',
  5203. array('option' => array('value' => '02')),
  5204. '2',
  5205. '/option',
  5206. $daysRegex,
  5207. '/select',
  5208. );
  5209. $this->assertTags($result, $expected);
  5210. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5211. $result = $this->Form->day('Model.field');
  5212. $expected = array(
  5213. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5214. array('option' => array('value' => '')),
  5215. '/option',
  5216. array('option' => array('value' => '01')),
  5217. '1',
  5218. '/option',
  5219. array('option' => array('value' => '02')),
  5220. '2',
  5221. '/option',
  5222. $daysRegex,
  5223. array('option' => array('value' => '10', 'selected' => 'selected')),
  5224. '10',
  5225. '/option',
  5226. $daysRegex,
  5227. '/select',
  5228. );
  5229. $this->assertTags($result, $expected);
  5230. $this->Form->request->data['Model']['field'] = '';
  5231. $result = $this->Form->day('Model.field', array('value' => '10'));
  5232. $expected = array(
  5233. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5234. array('option' => array('value' => '')),
  5235. '/option',
  5236. array('option' => array('value' => '01')),
  5237. '1',
  5238. '/option',
  5239. array('option' => array('value' => '02')),
  5240. '2',
  5241. '/option',
  5242. $daysRegex,
  5243. array('option' => array('value' => '10', 'selected' => 'selected')),
  5244. '10',
  5245. '/option',
  5246. $daysRegex,
  5247. '/select',
  5248. );
  5249. $this->assertTags($result, $expected);
  5250. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5251. $result = $this->Form->day('Model.field', array('value' => true));
  5252. $expected = array(
  5253. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5254. array('option' => array('value' => '')),
  5255. '/option',
  5256. array('option' => array('value' => '01')),
  5257. '1',
  5258. '/option',
  5259. array('option' => array('value' => '02')),
  5260. '2',
  5261. '/option',
  5262. $daysRegex,
  5263. array('option' => array('value' => '10', 'selected' => 'selected')),
  5264. '10',
  5265. '/option',
  5266. $daysRegex,
  5267. '/select',
  5268. );
  5269. $this->assertTags($result, $expected);
  5270. }
  5271. /**
  5272. * testMinute method
  5273. *
  5274. * @return void
  5275. */
  5276. public function testMinute() {
  5277. extract($this->dateRegex);
  5278. $result = $this->Form->minute('Model.field');
  5279. $expected = array(
  5280. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5281. array('option' => array('value' => '')),
  5282. '/option',
  5283. array('option' => array('value' => '00')),
  5284. '00',
  5285. '/option',
  5286. array('option' => array('value' => '01')),
  5287. '01',
  5288. '/option',
  5289. array('option' => array('value' => '02')),
  5290. '02',
  5291. '/option',
  5292. $minutesRegex,
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5297. $result = $this->Form->minute('Model.field');
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5300. array('option' => array('value' => '')),
  5301. '/option',
  5302. array('option' => array('value' => '00')),
  5303. '00',
  5304. '/option',
  5305. array('option' => array('value' => '01')),
  5306. '01',
  5307. '/option',
  5308. array('option' => array('value' => '02')),
  5309. '02',
  5310. '/option',
  5311. $minutesRegex,
  5312. array('option' => array('value' => '12', 'selected' => 'selected')),
  5313. '12',
  5314. '/option',
  5315. $minutesRegex,
  5316. '/select',
  5317. );
  5318. $this->assertTags($result, $expected);
  5319. $this->Form->request->data['Model']['field'] = '';
  5320. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5321. $expected = array(
  5322. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5323. array('option' => array('value' => '')),
  5324. '/option',
  5325. array('option' => array('value' => '00')),
  5326. '00',
  5327. '/option',
  5328. array('option' => array('value' => '05')),
  5329. '05',
  5330. '/option',
  5331. array('option' => array('value' => '10')),
  5332. '10',
  5333. '/option',
  5334. $minutesRegex,
  5335. '/select',
  5336. );
  5337. $this->assertTags($result, $expected);
  5338. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5339. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5340. $expected = array(
  5341. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5342. array('option' => array('value' => '')),
  5343. '/option',
  5344. array('option' => array('value' => '00')),
  5345. '00',
  5346. '/option',
  5347. array('option' => array('value' => '05')),
  5348. '05',
  5349. '/option',
  5350. array('option' => array('value' => '10', 'selected' => 'selected')),
  5351. '10',
  5352. '/option',
  5353. $minutesRegex,
  5354. '/select',
  5355. );
  5356. $this->assertTags($result, $expected);
  5357. }
  5358. /**
  5359. * testHour method
  5360. *
  5361. * @return void
  5362. */
  5363. public function testHour() {
  5364. extract($this->dateRegex);
  5365. $result = $this->Form->hour('Model.field', false);
  5366. $expected = array(
  5367. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5368. array('option' => array('value' => '')),
  5369. '/option',
  5370. array('option' => array('value' => '01')),
  5371. '1',
  5372. '/option',
  5373. array('option' => array('value' => '02')),
  5374. '2',
  5375. '/option',
  5376. $hoursRegex,
  5377. '/select',
  5378. );
  5379. $this->assertTags($result, $expected);
  5380. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5381. $result = $this->Form->hour('Model.field', false);
  5382. $expected = array(
  5383. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5384. array('option' => array('value' => '')),
  5385. '/option',
  5386. array('option' => array('value' => '01')),
  5387. '1',
  5388. '/option',
  5389. array('option' => array('value' => '02')),
  5390. '2',
  5391. '/option',
  5392. $hoursRegex,
  5393. array('option' => array('value' => '12', 'selected' => 'selected')),
  5394. '12',
  5395. '/option',
  5396. '/select',
  5397. );
  5398. $this->assertTags($result, $expected);
  5399. $this->Form->request->data['Model']['field'] = '';
  5400. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5401. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5402. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5403. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5404. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5405. $result = $this->Form->hour('Model.field', true);
  5406. $expected = array(
  5407. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5408. array('option' => array('value' => '')),
  5409. '/option',
  5410. array('option' => array('value' => '00', 'selected' => 'selected')),
  5411. '0',
  5412. '/option',
  5413. array('option' => array('value' => '01')),
  5414. '1',
  5415. '/option',
  5416. array('option' => array('value' => '02')),
  5417. '2',
  5418. '/option',
  5419. $hoursRegex,
  5420. '/select',
  5421. );
  5422. $this->assertTags($result, $expected);
  5423. unset($this->Form->request->data['Model']['field']);
  5424. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5425. $thisHour = date('H');
  5426. $optValue = date('G');
  5427. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5428. }
  5429. /**
  5430. * testYear method
  5431. *
  5432. * @return void
  5433. */
  5434. public function testYear() {
  5435. $result = $this->Form->year('Model.field', 2006, 2007);
  5436. $expected = array(
  5437. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5438. array('option' => array('value' => '')),
  5439. '/option',
  5440. array('option' => array('value' => '2007')),
  5441. '2007',
  5442. '/option',
  5443. array('option' => array('value' => '2006')),
  5444. '2006',
  5445. '/option',
  5446. '/select',
  5447. );
  5448. $this->assertTags($result, $expected);
  5449. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5450. $expected = array(
  5451. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5452. array('option' => array('value' => '')),
  5453. '/option',
  5454. array('option' => array('value' => '2006')),
  5455. '2006',
  5456. '/option',
  5457. array('option' => array('value' => '2007')),
  5458. '2007',
  5459. '/option',
  5460. '/select',
  5461. );
  5462. $this->assertTags($result, $expected);
  5463. $this->request->data['Contact']['published'] = '';
  5464. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5465. $expected = array(
  5466. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5467. array('option' => array('value' => '')),
  5468. '/option',
  5469. array('option' => array('value' => '2007')),
  5470. '2007',
  5471. '/option',
  5472. array('option' => array('value' => '2006')),
  5473. '2006',
  5474. '/option',
  5475. '/select',
  5476. );
  5477. $this->assertTags($result, $expected);
  5478. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5479. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5480. $expected = array(
  5481. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5482. array('option' => array('value' => '2007')),
  5483. '2007',
  5484. '/option',
  5485. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5486. '2006',
  5487. '/option',
  5488. '/select',
  5489. );
  5490. $this->assertTags($result, $expected);
  5491. $this->Form->request->data['Contact']['published'] = '';
  5492. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5493. $expected = array(
  5494. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5495. array('option' => array('value' => '')),
  5496. '/option',
  5497. array('option' => array('value' => '2007')),
  5498. '2007',
  5499. '/option',
  5500. array('option' => array('value' => '2006')),
  5501. '2006',
  5502. '/option',
  5503. '/select',
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5507. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5508. $expected = array(
  5509. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5510. array('option' => array('value' => '2007')),
  5511. '2007',
  5512. '/option',
  5513. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5514. '2006',
  5515. '/option',
  5516. '/select',
  5517. );
  5518. $this->assertTags($result, $expected);
  5519. $this->Form->request->data['Contact']['published'] = '';
  5520. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5521. $expected = array(
  5522. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5523. array('option' => array('value' => '')),
  5524. '/option',
  5525. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5526. '2007',
  5527. '/option',
  5528. array('option' => array('value' => '2006')),
  5529. '2006',
  5530. '/option',
  5531. '/select',
  5532. );
  5533. $this->assertTags($result, $expected);
  5534. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5535. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5536. $expected = array(
  5537. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5538. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5539. '2007',
  5540. '/option',
  5541. array('option' => array('value' => '2006')),
  5542. '2006',
  5543. '/option',
  5544. '/select',
  5545. );
  5546. $this->assertTags($result, $expected);
  5547. $this->Form->request->data['Contact']['published'] = '';
  5548. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5549. $expected = array(
  5550. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5551. array('option' => array('value' => '2008')),
  5552. '2008',
  5553. '/option',
  5554. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5555. '2007',
  5556. '/option',
  5557. array('option' => array('value' => '2006')),
  5558. '2006',
  5559. '/option',
  5560. '/select',
  5561. );
  5562. $this->assertTags($result, $expected);
  5563. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5564. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5565. $expected = array(
  5566. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5567. array('option' => array('value' => '2008')),
  5568. '2008',
  5569. '/option',
  5570. array('option' => array('value' => '2007')),
  5571. '2007',
  5572. '/option',
  5573. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5574. '2006',
  5575. '/option',
  5576. '/select',
  5577. );
  5578. $this->assertTags($result, $expected);
  5579. $this->Form->request->data = array();
  5580. $this->Form->create('Contact');
  5581. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5582. $expected = array(
  5583. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5584. array('option' => array('value' => '2008')),
  5585. '2008',
  5586. '/option',
  5587. array('option' => array('value' => '2007')),
  5588. '2007',
  5589. '/option',
  5590. array('option' => array('value' => '2006')),
  5591. '2006',
  5592. '/option',
  5593. '/select',
  5594. );
  5595. $this->assertTags($result, $expected);
  5596. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5597. $this->assertContains('data[Contact][published][year]', $result);
  5598. }
  5599. /**
  5600. * testTextArea method
  5601. *
  5602. * @return void
  5603. */
  5604. public function testTextArea() {
  5605. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5606. $result = $this->Form->textarea('Model.field');
  5607. $expected = array(
  5608. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5609. 'some test data',
  5610. '/textarea',
  5611. );
  5612. $this->assertTags($result, $expected);
  5613. $result = $this->Form->textarea('Model.tmp');
  5614. $expected = array(
  5615. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5616. '/textarea',
  5617. );
  5618. $this->assertTags($result, $expected);
  5619. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5620. $result = $this->Form->textarea('Model.field');
  5621. $expected = array(
  5622. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5623. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5624. '/textarea',
  5625. );
  5626. $this->assertTags($result, $expected);
  5627. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5628. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5629. $expected = array(
  5630. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5631. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5632. '/textarea',
  5633. );
  5634. $this->assertTags($result, $expected);
  5635. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5636. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5637. $expected = array(
  5638. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5639. '/textarea'
  5640. );
  5641. $this->assertTags($result, $expected);
  5642. }
  5643. /**
  5644. * testTextAreaWithStupidCharacters method
  5645. *
  5646. * test text area with non-ascii characters
  5647. *
  5648. * @return void
  5649. */
  5650. public function testTextAreaWithStupidCharacters() {
  5651. $this->loadFixtures('Post');
  5652. $result = $this->Form->input('Post.content', array(
  5653. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5654. ));
  5655. $expected = array(
  5656. 'div' => array('class' => 'input textarea'),
  5657. 'label' => array('for' => 'PostContent'),
  5658. 'Current Text',
  5659. '/label',
  5660. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5661. 'GREAT®',
  5662. '/textarea',
  5663. '/div'
  5664. );
  5665. $this->assertTags($result, $expected);
  5666. }
  5667. /**
  5668. * testHiddenField method
  5669. *
  5670. * @return void
  5671. */
  5672. public function testHiddenField() {
  5673. $Contact = ClassRegistry::getObject('Contact');
  5674. $Contact->validationErrors['field'] = 1;
  5675. $this->Form->request->data['Contact']['field'] = 'test';
  5676. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5677. $this->assertTags($result, array(
  5678. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5679. );
  5680. }
  5681. /**
  5682. * testFileUploadField method
  5683. *
  5684. * @return void
  5685. */
  5686. public function testFileUploadField() {
  5687. $result = $this->Form->file('Model.upload');
  5688. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5689. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5690. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5691. $expected = array(
  5692. 'div' => array('class' => 'input file'),
  5693. 'label' => array('for' => 'ModelUpload'),
  5694. 'Upload',
  5695. '/label',
  5696. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5697. '/div'
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5701. $result = $this->Form->file('Model.upload');
  5702. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5703. }
  5704. /**
  5705. * test File upload input on a model not used in create();
  5706. *
  5707. * @return void
  5708. */
  5709. public function testFileUploadOnOtherModel() {
  5710. $this->Form->create('ValidateUser', array('type' => 'file'));
  5711. $result = $this->Form->file('ValidateProfile.city');
  5712. $expected = array(
  5713. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. }
  5717. /**
  5718. * testButton method
  5719. *
  5720. * @return void
  5721. */
  5722. public function testButton() {
  5723. $result = $this->Form->button('Hi');
  5724. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5725. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5726. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5727. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5728. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5729. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5730. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5731. $result = $this->Form->button('No type', array('type' => false));
  5732. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5733. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5734. $this->assertNotRegExp('/\&039/', $result);
  5735. }
  5736. /**
  5737. * Test that button() makes unlocked fields by default.
  5738. *
  5739. * @return void
  5740. */
  5741. public function testButtonUnlockedByDefault() {
  5742. $this->Form->request->params['_Token']['key'] = 'secured';
  5743. $this->Form->button('Save', array('name' => 'save'));
  5744. $this->Form->button('Clear');
  5745. $result = $this->Form->unlockField();
  5746. $this->assertEquals(array('save'), $result);
  5747. }
  5748. /**
  5749. * testPostButton method
  5750. *
  5751. * @return void
  5752. */
  5753. public function testPostButton() {
  5754. $result = $this->Form->postButton('Hi', '/controller/action');
  5755. $this->assertTags($result, array(
  5756. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5757. 'div' => array('style' => 'display:none;'),
  5758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5759. '/div',
  5760. 'button' => array('type' => 'submit'),
  5761. 'Hi',
  5762. '/button',
  5763. '/form'
  5764. ));
  5765. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5766. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5767. }
  5768. /**
  5769. * Test that postButton adds _Token fields.
  5770. *
  5771. * @return void
  5772. */
  5773. public function testSecurePostButton() {
  5774. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5775. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5776. $expected = array(
  5777. 'form' => array(
  5778. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5779. ),
  5780. array('div' => array('style' => 'display:none;')),
  5781. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5782. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5783. '/div',
  5784. 'button' => array('type' => 'submit'),
  5785. 'Delete',
  5786. '/button',
  5787. array('div' => array('style' => 'display:none;')),
  5788. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5789. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5790. '/div',
  5791. '/form',
  5792. );
  5793. $this->assertTags($result, $expected);
  5794. }
  5795. /**
  5796. * testPostLink method
  5797. *
  5798. * @return void
  5799. */
  5800. public function testPostLink() {
  5801. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5802. $this->assertTags($result, array(
  5803. 'form' => array(
  5804. 'method' => 'post', 'action' => '/posts/delete/1',
  5805. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5806. ),
  5807. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5808. '/form',
  5809. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5810. 'Delete',
  5811. '/a'
  5812. ));
  5813. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5814. $this->assertTags($result, array(
  5815. 'form' => array(
  5816. 'method' => 'post', 'action' => '/posts/delete/1',
  5817. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5818. ),
  5819. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5820. '/form',
  5821. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5822. 'Delete',
  5823. '/a'
  5824. ));
  5825. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5826. $this->assertTags($result, array(
  5827. 'form' => array(
  5828. 'method' => 'post', 'action' => '/posts/delete/1',
  5829. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5830. ),
  5831. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5832. '/form',
  5833. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5834. 'Delete',
  5835. '/a'
  5836. ));
  5837. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5838. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5839. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5840. $this->assertTags($result, array(
  5841. 'form' => array(
  5842. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5843. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5844. ),
  5845. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5846. '/form',
  5847. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5848. 'Delete',
  5849. '/a'
  5850. ));
  5851. }
  5852. /**
  5853. * Test that postLink adds _Token fields.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testSecurePostLink() {
  5858. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5859. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5860. $expected = array(
  5861. 'form' => array(
  5862. 'method' => 'post', 'action' => '/posts/delete/1',
  5863. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5864. ),
  5865. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5866. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5867. 'div' => array('style' => 'display:none;'),
  5868. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5869. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5870. '/div',
  5871. '/form',
  5872. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5873. 'Delete',
  5874. '/a'
  5875. );
  5876. $this->assertTags($result, $expected);
  5877. }
  5878. /**
  5879. * testSubmitButton method
  5880. *
  5881. * @return void
  5882. */
  5883. public function testSubmitButton() {
  5884. $result = $this->Form->submit('');
  5885. $expected = array(
  5886. 'div' => array('class' => 'submit'),
  5887. 'input' => array('type' => 'submit', 'value' => ''),
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $result = $this->Form->submit('Test Submit');
  5892. $expected = array(
  5893. 'div' => array('class' => 'submit'),
  5894. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5895. '/div'
  5896. );
  5897. $this->assertTags($result, $expected);
  5898. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5899. $expected = array(
  5900. 'span' => array('class' => 'submit'),
  5901. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5902. '/span'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5906. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5907. $this->assertTags($result, $expected);
  5908. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5909. $expected = array(
  5910. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5911. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5912. '/div'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. $result = $this->Form->submit('Next >');
  5916. $expected = array(
  5917. 'div' => array('class' => 'submit'),
  5918. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5919. '/div'
  5920. );
  5921. $this->assertTags($result, $expected);
  5922. $result = $this->Form->submit('Next >', array('escape' => false));
  5923. $expected = array(
  5924. 'div' => array('class' => 'submit'),
  5925. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5926. '/div'
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5930. $expected = array(
  5931. 'div' => array('class' => 'submit'),
  5932. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5933. '/div'
  5934. );
  5935. $this->assertTags($result, $expected);
  5936. $before = '--before--';
  5937. $after = '--after--';
  5938. $result = $this->Form->submit('Test', array('before' => $before));
  5939. $expected = array(
  5940. 'div' => array('class' => 'submit'),
  5941. '--before--',
  5942. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5943. '/div'
  5944. );
  5945. $this->assertTags($result, $expected);
  5946. $result = $this->Form->submit('Test', array('after' => $after));
  5947. $expected = array(
  5948. 'div' => array('class' => 'submit'),
  5949. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5950. '--after--',
  5951. '/div'
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5955. $expected = array(
  5956. 'div' => array('class' => 'submit'),
  5957. '--before--',
  5958. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5959. '--after--',
  5960. '/div'
  5961. );
  5962. $this->assertTags($result, $expected);
  5963. }
  5964. /**
  5965. * test image submit types.
  5966. *
  5967. * @return void
  5968. */
  5969. public function testSubmitImage() {
  5970. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5971. $expected = array(
  5972. 'div' => array('class' => 'submit'),
  5973. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $result = $this->Form->submit('/relative/cake.power.gif');
  5978. $expected = array(
  5979. 'div' => array('class' => 'submit'),
  5980. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5981. '/div'
  5982. );
  5983. $this->assertTags($result, $expected);
  5984. $result = $this->Form->submit('cake.power.gif');
  5985. $expected = array(
  5986. 'div' => array('class' => 'submit'),
  5987. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5988. '/div'
  5989. );
  5990. $this->assertTags($result, $expected);
  5991. $result = $this->Form->submit('Not.an.image');
  5992. $expected = array(
  5993. 'div' => array('class' => 'submit'),
  5994. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5995. '/div'
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. $after = '--after--';
  5999. $before = '--before--';
  6000. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6001. $expected = array(
  6002. 'div' => array('class' => 'submit'),
  6003. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6004. '--after--',
  6005. '/div'
  6006. );
  6007. $this->assertTags($result, $expected);
  6008. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6009. $expected = array(
  6010. 'div' => array('class' => 'submit'),
  6011. '--before--',
  6012. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6013. '/div'
  6014. );
  6015. $this->assertTags($result, $expected);
  6016. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6017. $expected = array(
  6018. 'div' => array('class' => 'submit'),
  6019. '--before--',
  6020. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6021. '--after--',
  6022. '/div'
  6023. );
  6024. $this->assertTags($result, $expected);
  6025. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6026. $expected = array(
  6027. 'div' => array('class' => 'submit'),
  6028. '--before--',
  6029. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6030. '--after--',
  6031. '/div'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. }
  6035. /**
  6036. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6037. * be submitted at a time.
  6038. *
  6039. * @return void
  6040. */
  6041. public function testSubmitUnlockedByDefault() {
  6042. $this->Form->request->params['_Token']['key'] = 'secured';
  6043. $this->Form->submit('Go go');
  6044. $this->Form->submit('Save', array('name' => 'save'));
  6045. $result = $this->Form->unlockField();
  6046. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6047. }
  6048. /**
  6049. * Test submit image with timestamps.
  6050. *
  6051. * @return void
  6052. */
  6053. public function testSubmitImageTimestamp() {
  6054. Configure::write('Asset.timestamp', 'force');
  6055. $result = $this->Form->submit('cake.power.gif');
  6056. $expected = array(
  6057. 'div' => array('class' => 'submit'),
  6058. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6059. '/div'
  6060. );
  6061. $this->assertTags($result, $expected);
  6062. }
  6063. /**
  6064. * test the create() method
  6065. *
  6066. * @return void
  6067. */
  6068. public function testCreate() {
  6069. $result = $this->Form->create('Contact');
  6070. $encoding = strtolower(Configure::read('App.encoding'));
  6071. $expected = array(
  6072. 'form' => array(
  6073. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6074. 'accept-charset' => $encoding
  6075. ),
  6076. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6078. '/div'
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6082. $expected = array('form' => array(
  6083. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6084. 'accept-charset' => $encoding
  6085. ));
  6086. $this->assertTags($result, $expected);
  6087. $result = $this->Form->create('Contact', array('type' => 'get'));
  6088. $expected = array('form' => array(
  6089. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6090. 'accept-charset' => $encoding
  6091. ));
  6092. $this->assertTags($result, $expected);
  6093. $result = $this->Form->create('Contact', array('type' => 'put'));
  6094. $expected = array(
  6095. 'form' => array(
  6096. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6097. 'accept-charset' => $encoding
  6098. ),
  6099. 'div' => array('style' => 'display:none;'),
  6100. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6101. '/div'
  6102. );
  6103. $this->assertTags($result, $expected);
  6104. $result = $this->Form->create('Contact', array('type' => 'file'));
  6105. $expected = array(
  6106. 'form' => array(
  6107. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6108. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6109. ),
  6110. 'div' => array('style' => 'display:none;'),
  6111. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6112. '/div'
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. $this->Form->request->data['Contact']['id'] = 1;
  6116. $this->Form->request->here = '/contacts/edit/1';
  6117. $this->Form->request['action'] = 'edit';
  6118. $result = $this->Form->create('Contact');
  6119. $expected = array(
  6120. 'form' => array(
  6121. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6122. 'accept-charset' => $encoding
  6123. ),
  6124. 'div' => array('style' => 'display:none;'),
  6125. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6126. '/div'
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. $this->Form->request->data['Contact']['id'] = 1;
  6130. $this->Form->request->here = '/contacts/edit/1';
  6131. $this->Form->request['action'] = 'edit';
  6132. $result = $this->Form->create('Contact', array('type' => 'file'));
  6133. $expected = array(
  6134. 'form' => array(
  6135. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6136. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6137. ),
  6138. 'div' => array('style' => 'display:none;'),
  6139. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6140. '/div'
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6144. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6145. $expected = array(
  6146. 'form' => array(
  6147. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6148. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6149. ),
  6150. 'div' => array('style' => 'display:none;'),
  6151. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6152. '/div'
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6156. $expected = array(
  6157. 'form' => array(
  6158. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6159. 'accept-charset' => $encoding
  6160. ),
  6161. 'div' => array('style' => 'display:none;'),
  6162. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $this->Form->request['action'] = 'add';
  6167. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6168. $expected = array(
  6169. 'form' => array(
  6170. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6171. 'accept-charset' => $encoding
  6172. ),
  6173. 'div' => array('style' => 'display:none;'),
  6174. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6175. '/div'
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $result = $this->Form->create('User', array('action' => 'login'));
  6179. $expected = array(
  6180. 'form' => array(
  6181. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6182. 'accept-charset' => $encoding
  6183. ),
  6184. 'div' => array('style' => 'display:none;'),
  6185. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6186. '/div'
  6187. );
  6188. $this->assertTags($result, $expected);
  6189. $result = $this->Form->create('User', array('url' => '/users/login'));
  6190. $expected = array(
  6191. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6192. 'div' => array('style' => 'display:none;'),
  6193. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6194. '/div'
  6195. );
  6196. $this->assertTags($result, $expected);
  6197. $this->Form->request['controller'] = 'pages';
  6198. $result = $this->Form->create('User', array('action' => 'signup'));
  6199. $expected = array(
  6200. 'form' => array(
  6201. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6202. 'accept-charset' => $encoding
  6203. ),
  6204. 'div' => array('style' => 'display:none;'),
  6205. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6206. '/div'
  6207. );
  6208. $this->assertTags($result, $expected);
  6209. $this->Form->request->data = array();
  6210. $this->Form->request['controller'] = 'contacts';
  6211. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6212. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6213. $expected = array(
  6214. 'form' => array(
  6215. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6216. 'accept-charset' => 'utf-8'
  6217. ),
  6218. 'div' => array('style' => 'display:none;'),
  6219. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6220. '/div'
  6221. );
  6222. $this->assertTags($result, $expected);
  6223. }
  6224. /**
  6225. * Test the onsubmit option for create()
  6226. *
  6227. * @return void
  6228. */
  6229. public function testCreateOnSubmit() {
  6230. $this->Form->request->data = array();
  6231. $this->Form->request['controller'] = 'contacts';
  6232. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6233. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6234. $expected = array(
  6235. 'form' => array(
  6236. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6237. 'accept-charset' => 'utf-8'
  6238. ),
  6239. 'div' => array('style' => 'display:none;'),
  6240. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6241. '/div'
  6242. );
  6243. $this->assertTags($result, $expected);
  6244. $this->Form->request->data = array();
  6245. $this->Form->request['controller'] = 'contacts';
  6246. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6247. $result = $this->Form->create(array(
  6248. 'url' => array('action' => 'index', 'param'),
  6249. 'default' => false,
  6250. 'onsubmit' => 'someFunction();'
  6251. ));
  6252. $expected = array(
  6253. 'form' => array(
  6254. 'id' => 'ContactAddForm', 'method' => 'post',
  6255. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6256. 'action' => '/contacts/index/param',
  6257. 'accept-charset' => 'utf-8'
  6258. ),
  6259. 'div' => array('style' => 'display:none;'),
  6260. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6261. '/div'
  6262. );
  6263. $this->assertTags($result, $expected);
  6264. }
  6265. /**
  6266. * test create() with automatic url generation
  6267. *
  6268. * @return void
  6269. */
  6270. public function testCreateAutoUrl() {
  6271. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6272. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6273. $this->Form->request->base = '/base_url';
  6274. $result = $this->Form->create('Contact');
  6275. $expected = array(
  6276. 'form' => array(
  6277. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6278. 'accept-charset' => 'utf-8'
  6279. ),
  6280. 'div' => array('style' => 'display:none;'),
  6281. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6282. '/div'
  6283. );
  6284. $this->assertTags($result, $expected);
  6285. $this->Form->request['action'] = 'delete';
  6286. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6287. $this->Form->request->base = '/base_url';
  6288. $result = $this->Form->create('Contact');
  6289. $expected = array(
  6290. 'form' => array(
  6291. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6292. 'accept-charset' => 'utf-8'
  6293. ),
  6294. 'div' => array('style' => 'display:none;'),
  6295. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6296. '/div'
  6297. );
  6298. $this->assertTags($result, $expected);
  6299. }
  6300. /**
  6301. * test create() with a custom route
  6302. *
  6303. * @return void
  6304. */
  6305. public function testCreateCustomRoute() {
  6306. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6307. $encoding = strtolower(Configure::read('App.encoding'));
  6308. $result = $this->Form->create('User', array('action' => 'login'));
  6309. $expected = array(
  6310. 'form' => array(
  6311. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6312. 'accept-charset' => $encoding
  6313. ),
  6314. 'div' => array('style' => 'display:none;'),
  6315. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6316. '/div'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. }
  6320. /**
  6321. * test that inputDefaults are stored and used.
  6322. *
  6323. * @return void
  6324. */
  6325. public function testCreateWithInputDefaults() {
  6326. $this->Form->create('User', array(
  6327. 'inputDefaults' => array(
  6328. 'div' => false,
  6329. 'label' => false,
  6330. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6331. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6332. )
  6333. ));
  6334. $result = $this->Form->input('username');
  6335. $expected = array(
  6336. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6337. );
  6338. $this->assertTags($result, $expected);
  6339. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6340. $expected = array(
  6341. 'div' => array('class' => 'input text'),
  6342. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6343. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6344. '/div'
  6345. );
  6346. $this->assertTags($result, $expected);
  6347. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6348. $expected = array(
  6349. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6350. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $this->Form->create('User', array(
  6354. 'inputDefaults' => array(
  6355. 'div' => false,
  6356. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6357. )
  6358. ));
  6359. $result = $this->Form->input('username', array('div' => true));
  6360. $expected = array(
  6361. 'div' => array('class' => 'input text'),
  6362. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6363. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6364. '/div'
  6365. );
  6366. $this->assertTags($result, $expected);
  6367. }
  6368. /**
  6369. * test automatic accept-charset overriding
  6370. *
  6371. * @return void
  6372. */
  6373. public function testCreateWithAcceptCharset() {
  6374. $result = $this->Form->create('UserForm', array(
  6375. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6376. )
  6377. );
  6378. $expected = array(
  6379. 'form' => array(
  6380. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6381. 'accept-charset' => 'iso-8859-1'
  6382. ),
  6383. 'div' => array('style' => 'display:none;'),
  6384. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6385. '/div'
  6386. );
  6387. $this->assertTags($result, $expected);
  6388. }
  6389. /**
  6390. * Test base form url when url param is passed with multiple parameters (&)
  6391. *
  6392. */
  6393. public function testCreateQuerystringrequest() {
  6394. $encoding = strtolower(Configure::read('App.encoding'));
  6395. $result = $this->Form->create('Contact', array(
  6396. 'type' => 'post',
  6397. 'escape' => false,
  6398. 'url' => array(
  6399. 'controller' => 'controller',
  6400. 'action' => 'action',
  6401. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6402. )
  6403. ));
  6404. $expected = array(
  6405. 'form' => array(
  6406. 'id' => 'ContactAddForm',
  6407. 'method' => 'post',
  6408. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6409. 'accept-charset' => $encoding
  6410. ),
  6411. 'div' => array('style' => 'display:none;'),
  6412. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6413. '/div'
  6414. );
  6415. $this->assertTags($result, $expected);
  6416. $result = $this->Form->create('Contact', array(
  6417. 'type' => 'post',
  6418. 'url' => array(
  6419. 'controller' => 'controller',
  6420. 'action' => 'action',
  6421. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6422. )
  6423. ));
  6424. $expected = array(
  6425. 'form' => array(
  6426. 'id' => 'ContactAddForm',
  6427. 'method' => 'post',
  6428. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6429. 'accept-charset' => $encoding
  6430. ),
  6431. 'div' => array('style' => 'display:none;'),
  6432. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. }
  6437. /**
  6438. * test that create() doesn't cause errors by multiple id's being in the primary key
  6439. * as could happen with multiple select or checkboxes.
  6440. *
  6441. * @return void
  6442. */
  6443. public function testCreateWithMultipleIdInData() {
  6444. $encoding = strtolower(Configure::read('App.encoding'));
  6445. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6446. $result = $this->Form->create('Contact');
  6447. $expected = array(
  6448. 'form' => array(
  6449. 'id' => 'ContactAddForm',
  6450. 'method' => 'post',
  6451. 'action' => '/contacts/add',
  6452. 'accept-charset' => $encoding
  6453. ),
  6454. 'div' => array('style' => 'display:none;'),
  6455. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6456. '/div'
  6457. );
  6458. $this->assertTags($result, $expected);
  6459. }
  6460. /**
  6461. * test that create() doesn't add in extra passed params.
  6462. *
  6463. * @return void
  6464. */
  6465. public function testCreatePassedArgs() {
  6466. $encoding = strtolower(Configure::read('App.encoding'));
  6467. $this->Form->request->data['Contact']['id'] = 1;
  6468. $result = $this->Form->create('Contact', array(
  6469. 'type' => 'post',
  6470. 'escape' => false,
  6471. 'url' => array(
  6472. 'action' => 'edit',
  6473. 'myparam'
  6474. )
  6475. ));
  6476. $expected = array(
  6477. 'form' => array(
  6478. 'id' => 'ContactAddForm',
  6479. 'method' => 'post',
  6480. 'action' => '/contacts/edit/myparam',
  6481. 'accept-charset' => $encoding
  6482. ),
  6483. 'div' => array('style' => 'display:none;'),
  6484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6485. '/div'
  6486. );
  6487. $this->assertTags($result, $expected);
  6488. }
  6489. /**
  6490. * test creating a get form, and get form inputs.
  6491. *
  6492. * @return void
  6493. */
  6494. public function testGetFormCreate() {
  6495. $encoding = strtolower(Configure::read('App.encoding'));
  6496. $result = $this->Form->create('Contact', array('type' => 'get'));
  6497. $this->assertTags($result, array('form' => array(
  6498. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6499. 'accept-charset' => $encoding
  6500. )));
  6501. $result = $this->Form->text('Contact.name');
  6502. $this->assertTags($result, array('input' => array(
  6503. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6504. )));
  6505. $result = $this->Form->password('password');
  6506. $this->assertTags($result, array('input' => array(
  6507. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6508. )));
  6509. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6510. $result = $this->Form->text('user_form');
  6511. $this->assertTags($result, array('input' => array(
  6512. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6513. )));
  6514. }
  6515. /**
  6516. * test get form, and inputs when the model param is false
  6517. *
  6518. * @return void
  6519. */
  6520. public function testGetFormWithFalseModel() {
  6521. $encoding = strtolower(Configure::read('App.encoding'));
  6522. $this->Form->request['controller'] = 'contact_test';
  6523. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6524. $expected = array('form' => array(
  6525. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6526. 'accept-charset' => $encoding
  6527. ));
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->text('reason');
  6530. $expected = array(
  6531. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. }
  6535. /**
  6536. * test that datetime() works with GET style forms.
  6537. *
  6538. * @return void
  6539. */
  6540. public function testDateTimeWithGetForms() {
  6541. extract($this->dateRegex);
  6542. $this->Form->create('Contact', array('type' => 'get'));
  6543. $result = $this->Form->datetime('created');
  6544. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6545. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6546. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6547. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6548. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6549. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6550. }
  6551. /**
  6552. * testEditFormWithData method
  6553. *
  6554. * test auto populating form elements from submitted data.
  6555. *
  6556. * @return void
  6557. */
  6558. public function testEditFormWithData() {
  6559. $this->Form->request->data = array('Person' => array(
  6560. 'id' => 1,
  6561. 'first_name' => 'Nate',
  6562. 'last_name' => 'Abele',
  6563. 'email' => 'nate@example.com'
  6564. ));
  6565. $this->Form->request->addParams(array(
  6566. 'models' => array('Person'),
  6567. 'controller' => 'people',
  6568. 'action' => 'add'
  6569. ));
  6570. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6571. $this->Form->create();
  6572. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6573. $expected = array(
  6574. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6575. 'select' => array(
  6576. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6577. ),
  6578. array('option' => array('value' => 1)), 'Nate', '/option',
  6579. array('option' => array('value' => 2)), 'Garrett', '/option',
  6580. array('option' => array('value' => 3)), 'Larry', '/option',
  6581. '/select'
  6582. );
  6583. $this->assertTags($result, $expected);
  6584. }
  6585. /**
  6586. * Test that required fields are created for various types of validation.
  6587. *
  6588. * @return void
  6589. */
  6590. public function testFormInputRequiredDetection() {
  6591. $this->Form->create('Contact');
  6592. $result = $this->Form->input('Contact.non_existing');
  6593. $expected = array(
  6594. 'div' => array('class' => 'input text'),
  6595. 'label' => array('for' => 'ContactNonExisting'),
  6596. 'Non Existing',
  6597. '/label',
  6598. 'input' => array(
  6599. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6600. 'id' => 'ContactNonExisting'
  6601. ),
  6602. '/div'
  6603. );
  6604. $this->assertTags($result, $expected);
  6605. $result = $this->Form->input('Contact.imrequired');
  6606. $expected = array(
  6607. 'div' => array('class' => 'input text required'),
  6608. 'label' => array('for' => 'ContactImrequired'),
  6609. 'Imrequired',
  6610. '/label',
  6611. 'input' => array(
  6612. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6613. 'id' => 'ContactImrequired',
  6614. 'required' => 'required'
  6615. ),
  6616. '/div'
  6617. );
  6618. $this->assertTags($result, $expected);
  6619. $result = $this->Form->input('Contact.imalsorequired');
  6620. $expected = array(
  6621. 'div' => array('class' => 'input text required'),
  6622. 'label' => array('for' => 'ContactImalsorequired'),
  6623. 'Imalsorequired',
  6624. '/label',
  6625. 'input' => array(
  6626. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6627. 'id' => 'ContactImalsorequired',
  6628. 'required' => 'required'
  6629. ),
  6630. '/div'
  6631. );
  6632. $this->assertTags($result, $expected);
  6633. $result = $this->Form->input('Contact.imrequiredtoo');
  6634. $expected = array(
  6635. 'div' => array('class' => 'input text required'),
  6636. 'label' => array('for' => 'ContactImrequiredtoo'),
  6637. 'Imrequiredtoo',
  6638. '/label',
  6639. 'input' => array(
  6640. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6641. 'id' => 'ContactImrequiredtoo',
  6642. 'required' => 'required'
  6643. ),
  6644. '/div'
  6645. );
  6646. $this->assertTags($result, $expected);
  6647. $result = $this->Form->input('Contact.required_one');
  6648. $expected = array(
  6649. 'div' => array('class' => 'input text required'),
  6650. 'label' => array('for' => 'ContactRequiredOne'),
  6651. 'Required One',
  6652. '/label',
  6653. 'input' => array(
  6654. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6655. 'id' => 'ContactRequiredOne',
  6656. 'required' => 'required'
  6657. ),
  6658. '/div'
  6659. );
  6660. $this->assertTags($result, $expected);
  6661. $result = $this->Form->input('Contact.string_required');
  6662. $expected = array(
  6663. 'div' => array('class' => 'input text required'),
  6664. 'label' => array('for' => 'ContactStringRequired'),
  6665. 'String Required',
  6666. '/label',
  6667. 'input' => array(
  6668. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6669. 'id' => 'ContactStringRequired',
  6670. 'required' => 'required'
  6671. ),
  6672. '/div'
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $result = $this->Form->input('Contact.imnotrequired');
  6676. $expected = array(
  6677. 'div' => array('class' => 'input text'),
  6678. 'label' => array('for' => 'ContactImnotrequired'),
  6679. 'Imnotrequired',
  6680. '/label',
  6681. 'input' => array(
  6682. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6683. 'id' => 'ContactImnotrequired'
  6684. ),
  6685. '/div'
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. $result = $this->Form->input('Contact.imalsonotrequired');
  6689. $expected = array(
  6690. 'div' => array('class' => 'input text'),
  6691. 'label' => array('for' => 'ContactImalsonotrequired'),
  6692. 'Imalsonotrequired',
  6693. '/label',
  6694. 'input' => array(
  6695. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6696. 'id' => 'ContactImalsonotrequired'
  6697. ),
  6698. '/div'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. $result = $this->Form->input('Contact.imalsonotrequired2');
  6702. $expected = array(
  6703. 'div' => array('class' => 'input text'),
  6704. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6705. 'Imalsonotrequired2',
  6706. '/label',
  6707. 'input' => array(
  6708. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6709. 'id' => 'ContactImalsonotrequired2'
  6710. ),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. $result = $this->Form->input('Contact.imnotrequiredeither');
  6715. $expected = array(
  6716. 'div' => array('class' => 'input text'),
  6717. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6718. 'Imnotrequiredeither',
  6719. '/label',
  6720. 'input' => array(
  6721. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6722. 'id' => 'ContactImnotrequiredeither'
  6723. ),
  6724. '/div'
  6725. );
  6726. $this->assertTags($result, $expected);
  6727. $result = $this->Form->input('Contact.iamrequiredalways');
  6728. $expected = array(
  6729. 'div' => array('class' => 'input text required'),
  6730. 'label' => array('for' => 'ContactIamrequiredalways'),
  6731. 'Iamrequiredalways',
  6732. '/label',
  6733. 'input' => array(
  6734. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6735. 'id' => 'ContactIamrequiredalways',
  6736. 'required' => 'required'
  6737. ),
  6738. '/div'
  6739. );
  6740. $this->assertTags($result, $expected);
  6741. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6742. $expected = array(
  6743. 'div' => array('class' => 'input checkbox required'),
  6744. array('input' => array(
  6745. 'type' => 'hidden',
  6746. 'name' => 'data[Contact][boolean_field]',
  6747. 'id' => 'ContactBooleanField_',
  6748. 'value' => '0'
  6749. )),
  6750. array('input' => array(
  6751. 'type' => 'checkbox',
  6752. 'name' => 'data[Contact][boolean_field]',
  6753. 'value' => '1',
  6754. 'id' => 'ContactBooleanField'
  6755. )),
  6756. 'label' => array('for' => 'ContactBooleanField'),
  6757. 'Boolean Field',
  6758. '/label',
  6759. '/div'
  6760. );
  6761. $this->assertTags($result, $expected);
  6762. }
  6763. /**
  6764. * testFormMagicInput method
  6765. *
  6766. * @return void
  6767. */
  6768. public function testFormMagicInput() {
  6769. $encoding = strtolower(Configure::read('App.encoding'));
  6770. $result = $this->Form->create('Contact');
  6771. $expected = array(
  6772. 'form' => array(
  6773. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6774. 'accept-charset' => $encoding
  6775. ),
  6776. 'div' => array('style' => 'display:none;'),
  6777. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->input('name');
  6782. $expected = array(
  6783. 'div' => array('class' => 'input text'),
  6784. 'label' => array('for' => 'ContactName'),
  6785. 'Name',
  6786. '/label',
  6787. 'input' => array(
  6788. 'type' => 'text', 'name' => 'data[Contact][name]',
  6789. 'id' => 'ContactName', 'maxlength' => '255'
  6790. ),
  6791. '/div'
  6792. );
  6793. $this->assertTags($result, $expected);
  6794. $result = $this->Form->input('non_existing_field_in_contact_model');
  6795. $expected = array(
  6796. 'div' => array('class' => 'input text'),
  6797. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6798. 'Non Existing Field In Contact Model',
  6799. '/label',
  6800. 'input' => array(
  6801. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6802. 'id' => 'ContactNonExistingFieldInContactModel'
  6803. ),
  6804. '/div'
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. $result = $this->Form->input('Address.street');
  6808. $expected = array(
  6809. 'div' => array('class' => 'input text'),
  6810. 'label' => array('for' => 'AddressStreet'),
  6811. 'Street',
  6812. '/label',
  6813. 'input' => array(
  6814. 'type' => 'text', 'name' => 'data[Address][street]',
  6815. 'id' => 'AddressStreet'
  6816. ),
  6817. '/div'
  6818. );
  6819. $this->assertTags($result, $expected);
  6820. $result = $this->Form->input('Address.non_existing_field_in_model');
  6821. $expected = array(
  6822. 'div' => array('class' => 'input text'),
  6823. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6824. 'Non Existing Field In Model',
  6825. '/label',
  6826. 'input' => array(
  6827. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6828. 'id' => 'AddressNonExistingFieldInModel'
  6829. ),
  6830. '/div'
  6831. );
  6832. $this->assertTags($result, $expected);
  6833. $result = $this->Form->input('name', array('div' => false));
  6834. $expected = array(
  6835. 'label' => array('for' => 'ContactName'),
  6836. 'Name',
  6837. '/label',
  6838. 'input' => array(
  6839. 'type' => 'text', 'name' => 'data[Contact][name]',
  6840. 'id' => 'ContactName', 'maxlength' => '255'
  6841. )
  6842. );
  6843. $this->assertTags($result, $expected);
  6844. extract($this->dateRegex);
  6845. $now = strtotime('now');
  6846. $result = $this->Form->input('Contact.published', array('div' => false));
  6847. $expected = array(
  6848. 'label' => array('for' => 'ContactPublishedMonth'),
  6849. 'Published',
  6850. '/label',
  6851. array('select' => array(
  6852. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6853. )),
  6854. $monthsRegex,
  6855. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6856. date('F', $now),
  6857. '/option',
  6858. '*/select',
  6859. '-',
  6860. array('select' => array(
  6861. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6862. )),
  6863. $daysRegex,
  6864. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6865. date('j', $now),
  6866. '/option',
  6867. '*/select',
  6868. '-',
  6869. array('select' => array(
  6870. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6871. )),
  6872. $yearsRegex,
  6873. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6874. date('Y', $now),
  6875. '*/select'
  6876. );
  6877. $this->assertTags($result, $expected);
  6878. $result = $this->Form->input('Contact.updated', array('div' => false));
  6879. $expected = array(
  6880. 'label' => array('for' => 'ContactUpdatedMonth'),
  6881. 'Updated',
  6882. '/label',
  6883. array('select' => array(
  6884. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6885. )),
  6886. $monthsRegex,
  6887. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6888. date('F', $now),
  6889. '/option',
  6890. '*/select',
  6891. '-',
  6892. array('select' => array(
  6893. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6894. )),
  6895. $daysRegex,
  6896. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6897. date('j', $now),
  6898. '/option',
  6899. '*/select',
  6900. '-',
  6901. array('select' => array(
  6902. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6903. )),
  6904. $yearsRegex,
  6905. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6906. date('Y', $now),
  6907. '*/select'
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. $result = $this->Form->input('UserForm.stuff');
  6911. $expected = array(
  6912. 'div' => array('class' => 'input text'),
  6913. 'label' => array('for' => 'UserFormStuff'),
  6914. 'Stuff',
  6915. '/label',
  6916. 'input' => array(
  6917. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6918. 'id' => 'UserFormStuff', 'maxlength' => 10
  6919. ),
  6920. '/div'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. }
  6924. /**
  6925. * testForMagicInputNonExistingNorValidated method
  6926. *
  6927. * @return void
  6928. */
  6929. public function testForMagicInputNonExistingNorValidated() {
  6930. $encoding = strtolower(Configure::read('App.encoding'));
  6931. $result = $this->Form->create('Contact');
  6932. $expected = array(
  6933. 'form' => array(
  6934. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6935. 'accept-charset' => $encoding
  6936. ),
  6937. 'div' => array('style' => 'display:none;'),
  6938. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6939. '/div'
  6940. );
  6941. $this->assertTags($result, $expected);
  6942. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6943. $expected = array(
  6944. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6945. 'Non Existing Nor Validated',
  6946. '/label',
  6947. 'input' => array(
  6948. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6949. 'id' => 'ContactNonExistingNorValidated'
  6950. )
  6951. );
  6952. $this->assertTags($result, $expected);
  6953. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6954. 'div' => false, 'value' => 'my value'
  6955. ));
  6956. $expected = array(
  6957. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6958. 'Non Existing Nor Validated',
  6959. '/label',
  6960. 'input' => array(
  6961. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6962. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6963. )
  6964. );
  6965. $this->assertTags($result, $expected);
  6966. $this->Form->request->data = array(
  6967. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6968. ));
  6969. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6970. $expected = array(
  6971. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6972. 'Non Existing Nor Validated',
  6973. '/label',
  6974. 'input' => array(
  6975. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6976. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6977. )
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. }
  6981. /**
  6982. * testFormMagicInputLabel method
  6983. *
  6984. * @return void
  6985. */
  6986. public function testFormMagicInputLabel() {
  6987. $encoding = strtolower(Configure::read('App.encoding'));
  6988. $result = $this->Form->create('Contact');
  6989. $expected = array(
  6990. 'form' => array(
  6991. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6992. 'accept-charset' => $encoding
  6993. ),
  6994. 'div' => array('style' => 'display:none;'),
  6995. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6996. '/div'
  6997. );
  6998. $this->assertTags($result, $expected);
  6999. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7000. $this->assertTags($result, array('input' => array(
  7001. 'name' => 'data[Contact][name]', 'type' => 'text',
  7002. 'id' => 'ContactName', 'maxlength' => '255')
  7003. ));
  7004. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7005. $expected = array(
  7006. 'label' => array('for' => 'ContactName'),
  7007. 'My label',
  7008. '/label',
  7009. 'input' => array(
  7010. 'type' => 'text', 'name' => 'data[Contact][name]',
  7011. 'id' => 'ContactName', 'maxlength' => '255'
  7012. )
  7013. );
  7014. $this->assertTags($result, $expected);
  7015. $result = $this->Form->input('Contact.name', array(
  7016. 'div' => false, 'label' => array('class' => 'mandatory')
  7017. ));
  7018. $expected = array(
  7019. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7020. 'Name',
  7021. '/label',
  7022. 'input' => array(
  7023. 'type' => 'text', 'name' => 'data[Contact][name]',
  7024. 'id' => 'ContactName', 'maxlength' => '255'
  7025. )
  7026. );
  7027. $this->assertTags($result, $expected);
  7028. $result = $this->Form->input('Contact.name', array(
  7029. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7030. ));
  7031. $expected = array(
  7032. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7033. 'My label',
  7034. '/label',
  7035. 'input' => array(
  7036. 'type' => 'text', 'name' => 'data[Contact][name]',
  7037. 'id' => 'ContactName', 'maxlength' => '255'
  7038. )
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. $result = $this->Form->input('Contact.name', array(
  7042. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7043. ));
  7044. $expected = array(
  7045. 'label' => array('for' => 'my_id'),
  7046. 'Name',
  7047. '/label',
  7048. 'input' => array(
  7049. 'type' => 'text', 'name' => 'data[Contact][name]',
  7050. 'id' => 'my_id', 'maxlength' => '255'
  7051. )
  7052. );
  7053. $this->assertTags($result, $expected);
  7054. $result = $this->Form->input('1.id');
  7055. $this->assertTags($result, array('input' => array(
  7056. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7057. 'id' => 'Contact1Id'
  7058. )));
  7059. $result = $this->Form->input("1.name");
  7060. $expected = array(
  7061. 'div' => array('class' => 'input text'),
  7062. 'label' => array('for' => 'Contact1Name'),
  7063. 'Name',
  7064. '/label',
  7065. 'input' => array(
  7066. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7067. 'id' => 'Contact1Name', 'maxlength' => '255'
  7068. ),
  7069. '/div'
  7070. );
  7071. $this->assertTags($result, $expected);
  7072. $result = $this->Form->input('Contact.1.id');
  7073. $this->assertTags($result, array(
  7074. 'input' => array(
  7075. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7076. 'id' => 'Contact1Id'
  7077. )
  7078. ));
  7079. $result = $this->Form->input("Model.1.name");
  7080. $expected = array(
  7081. 'div' => array('class' => 'input text'),
  7082. 'label' => array('for' => 'Model1Name'),
  7083. 'Name',
  7084. '/label',
  7085. 'input' => array(
  7086. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7087. 'id' => 'Model1Name'
  7088. ),
  7089. '/div'
  7090. );
  7091. $this->assertTags($result, $expected);
  7092. }
  7093. /**
  7094. * testFormEnd method
  7095. *
  7096. * @return void
  7097. */
  7098. public function testFormEnd() {
  7099. $this->assertEquals('</form>', $this->Form->end());
  7100. $result = $this->Form->end('');
  7101. $expected = array(
  7102. 'div' => array('class' => 'submit'),
  7103. 'input' => array('type' => 'submit', 'value' => ''),
  7104. '/div',
  7105. '/form'
  7106. );
  7107. $this->assertTags($result, $expected);
  7108. $result = $this->Form->end(array('label' => ''));
  7109. $expected = array(
  7110. 'div' => array('class' => 'submit'),
  7111. 'input' => array('type' => 'submit', 'value' => ''),
  7112. '/div',
  7113. '/form'
  7114. );
  7115. $this->assertTags($result, $expected);
  7116. $result = $this->Form->end('save');
  7117. $expected = array(
  7118. 'div' => array('class' => 'submit'),
  7119. 'input' => array('type' => 'submit', 'value' => 'save'),
  7120. '/div',
  7121. '/form'
  7122. );
  7123. $this->assertTags($result, $expected);
  7124. $result = $this->Form->end(array('label' => 'save'));
  7125. $expected = array(
  7126. 'div' => array('class' => 'submit'),
  7127. 'input' => array('type' => 'submit', 'value' => 'save'),
  7128. '/div',
  7129. '/form'
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7133. $expected = array(
  7134. 'div' => array('class' => 'submit'),
  7135. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7136. '/div',
  7137. '/form'
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $result = $this->Form->end(array('name' => 'Whatever'));
  7141. $expected = array(
  7142. 'div' => array('class' => 'submit'),
  7143. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7144. '/div',
  7145. '/form'
  7146. );
  7147. $this->assertTags($result, $expected);
  7148. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7149. $expected = array(
  7150. 'div' => array('class' => 'good'),
  7151. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7152. '/div',
  7153. '/form'
  7154. );
  7155. $this->assertTags($result, $expected);
  7156. $result = $this->Form->end(array(
  7157. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7158. ));
  7159. $expected = array(
  7160. 'div' => array('class' => 'good'),
  7161. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7162. '/div',
  7163. '/form'
  7164. );
  7165. $this->assertTags($result, $expected);
  7166. }
  7167. /**
  7168. * testMultipleFormWithIdFields method
  7169. *
  7170. * @return void
  7171. */
  7172. public function testMultipleFormWithIdFields() {
  7173. $this->Form->create('UserForm');
  7174. $result = $this->Form->input('id');
  7175. $this->assertTags($result, array('input' => array(
  7176. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7177. )));
  7178. $result = $this->Form->input('ValidateItem.id');
  7179. $this->assertTags($result, array('input' => array(
  7180. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7181. 'id' => 'ValidateItemId'
  7182. )));
  7183. $result = $this->Form->input('ValidateUser.id');
  7184. $this->assertTags($result, array('input' => array(
  7185. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7186. 'id' => 'ValidateUserId'
  7187. )));
  7188. }
  7189. /**
  7190. * testDbLessModel method
  7191. *
  7192. * @return void
  7193. */
  7194. public function testDbLessModel() {
  7195. $this->Form->create('TestMail');
  7196. $result = $this->Form->input('name');
  7197. $expected = array(
  7198. 'div' => array('class' => 'input text'),
  7199. 'label' => array('for' => 'TestMailName'),
  7200. 'Name',
  7201. '/label',
  7202. 'input' => array(
  7203. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7204. 'id' => 'TestMailName'
  7205. ),
  7206. '/div'
  7207. );
  7208. $this->assertTags($result, $expected);
  7209. ClassRegistry::init('TestMail');
  7210. $this->Form->create('TestMail');
  7211. $result = $this->Form->input('name');
  7212. $expected = array(
  7213. 'div' => array('class' => 'input text'),
  7214. 'label' => array('for' => 'TestMailName'),
  7215. 'Name',
  7216. '/label',
  7217. 'input' => array(
  7218. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7219. 'id' => 'TestMailName'
  7220. ),
  7221. '/div'
  7222. );
  7223. $this->assertTags($result, $expected);
  7224. }
  7225. /**
  7226. * testBrokenness method
  7227. *
  7228. * @return void
  7229. */
  7230. public function testBrokenness() {
  7231. /*
  7232. * #4 This test has two parents and four children. By default (as of r7117) both
  7233. * parents are show but the first parent is missing a child. This is the inconsistency
  7234. * in the default behaviour - one parent has all children, the other does not - dependent
  7235. * on the data values.
  7236. */
  7237. $result = $this->Form->select('Model.field', array(
  7238. 'Fred' => array(
  7239. 'freds_son_1' => 'Fred',
  7240. 'freds_son_2' => 'Freddie'
  7241. ),
  7242. 'Bert' => array(
  7243. 'berts_son_1' => 'Albert',
  7244. 'berts_son_2' => 'Bertie')
  7245. ),
  7246. array('showParents' => true, 'empty' => false)
  7247. );
  7248. $expected = array(
  7249. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7250. array('optgroup' => array('label' => 'Fred')),
  7251. array('option' => array('value' => 'freds_son_1')),
  7252. 'Fred',
  7253. '/option',
  7254. array('option' => array('value' => 'freds_son_2')),
  7255. 'Freddie',
  7256. '/option',
  7257. '/optgroup',
  7258. array('optgroup' => array('label' => 'Bert')),
  7259. array('option' => array('value' => 'berts_son_1')),
  7260. 'Albert',
  7261. '/option',
  7262. array('option' => array('value' => 'berts_son_2')),
  7263. 'Bertie',
  7264. '/option',
  7265. '/optgroup',
  7266. '/select'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. /*
  7270. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7271. * changed, so we should expect the same select list data, just with a different name
  7272. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7273. * This is where data corruption can occur, because when a select value is missing from
  7274. * a list a form will substitute the first value in the list - without the user knowing.
  7275. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7276. * affect the availability of 3 => 'Three' as a valid option.
  7277. */
  7278. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7279. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7280. ));
  7281. $result = $this->Form->select(
  7282. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7283. );
  7284. $expected = array(
  7285. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7286. array('option' => array('value' => 1)),
  7287. 'One',
  7288. '/option',
  7289. array('option' => array('value' => 2)),
  7290. 'Two',
  7291. '/option',
  7292. array('optgroup' => array('label' => 'Three')),
  7293. array('option' => array('value' => 3)),
  7294. 'Three',
  7295. '/option',
  7296. array('option' => array('value' => 4)),
  7297. 'Four',
  7298. '/option',
  7299. array('option' => array('value' => 5)),
  7300. 'Five',
  7301. '/option',
  7302. '/optgroup',
  7303. '/select'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. }
  7307. /**
  7308. * Test the generation of fields for a multi record form.
  7309. *
  7310. * @return void
  7311. */
  7312. public function testMultiRecordForm() {
  7313. $this->Form->create('ValidateProfile');
  7314. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7315. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7316. $expected = array(
  7317. 'div' => array('class' => 'input textarea'),
  7318. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7319. 'Name',
  7320. '/label',
  7321. 'textarea' => array(
  7322. 'id' => 'ValidateProfile1ValidateItem2Name',
  7323. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7324. 'cols' => 30,
  7325. 'rows' => 6
  7326. ),
  7327. 'Value',
  7328. '/textarea',
  7329. '/div'
  7330. );
  7331. $this->assertTags($result, $expected);
  7332. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7333. $expected = array(
  7334. 'div' => array('class' => 'input date'),
  7335. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7336. 'Created',
  7337. '/label',
  7338. array('select' => array(
  7339. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7340. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7341. )
  7342. ),
  7343. array('option' => array('value' => '')), '/option',
  7344. $this->dateRegex['monthsRegex'],
  7345. '/select', '-',
  7346. array('select' => array(
  7347. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7348. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7349. )
  7350. ),
  7351. array('option' => array('value' => '')), '/option',
  7352. $this->dateRegex['daysRegex'],
  7353. '/select', '-',
  7354. array('select' => array(
  7355. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7356. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7357. )
  7358. ),
  7359. array('option' => array('value' => '')), '/option',
  7360. $this->dateRegex['yearsRegex'],
  7361. '/select',
  7362. '/div'
  7363. );
  7364. $this->assertTags($result, $expected);
  7365. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7366. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7367. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7368. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7369. $expected = array(
  7370. 'div' => array('class' => 'input select error'),
  7371. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7372. 'Profile',
  7373. '/label',
  7374. 'select' => array(
  7375. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7376. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7377. 'class' => 'form-error'
  7378. ),
  7379. '/select',
  7380. array('div' => array('class' => 'error-message')),
  7381. 'Error',
  7382. '/div',
  7383. '/div'
  7384. );
  7385. $this->assertTags($result, $expected);
  7386. }
  7387. /**
  7388. * test the correct display of multi-record form validation errors.
  7389. *
  7390. * @return void
  7391. */
  7392. public function testMultiRecordFormValidationErrors() {
  7393. $this->Form->create('ValidateProfile');
  7394. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7395. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7396. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7397. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7398. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7399. $result = $this->Form->error('ValidateProfile.2.city');
  7400. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7401. $result = $this->Form->error('2.city');
  7402. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7403. }
  7404. /**
  7405. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7406. *
  7407. * @return void
  7408. */
  7409. public function testInputTemplate() {
  7410. $result = $this->Form->input('Contact.email', array(
  7411. 'type' => 'text', 'format' => array('input')
  7412. ));
  7413. $expected = array(
  7414. 'div' => array('class' => 'input text'),
  7415. 'input' => array(
  7416. 'type' => 'text', 'name' => 'data[Contact][email]',
  7417. 'id' => 'ContactEmail'
  7418. ),
  7419. '/div'
  7420. );
  7421. $this->assertTags($result, $expected);
  7422. $result = $this->Form->input('Contact.email', array(
  7423. 'type' => 'text', 'format' => array('input', 'label'),
  7424. 'label' => '<em>Email (required)</em>'
  7425. ));
  7426. $expected = array(
  7427. 'div' => array('class' => 'input text'),
  7428. array('input' => array(
  7429. 'type' => 'text', 'name' => 'data[Contact][email]',
  7430. 'id' => 'ContactEmail'
  7431. )),
  7432. 'label' => array('for' => 'ContactEmail'),
  7433. 'em' => array(),
  7434. 'Email (required)',
  7435. '/em',
  7436. '/label',
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $result = $this->Form->input('Contact.email', array(
  7441. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7442. 'between' => '<div>Something in the middle</div>',
  7443. 'after' => '<span>Some text at the end</span>'
  7444. ));
  7445. $expected = array(
  7446. 'div' => array('class' => 'input text'),
  7447. array('input' => array(
  7448. 'type' => 'text', 'name' => 'data[Contact][email]',
  7449. 'id' => 'ContactEmail'
  7450. )),
  7451. array('div' => array()),
  7452. 'Something in the middle',
  7453. '/div',
  7454. 'label' => array('for' => 'ContactEmail'),
  7455. 'Email',
  7456. '/label',
  7457. 'span' => array(),
  7458. 'Some text at the end',
  7459. '/span',
  7460. '/div'
  7461. );
  7462. $this->assertTags($result, $expected);
  7463. $result = $this->Form->input('Contact.method', array(
  7464. 'type' => 'radio',
  7465. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7466. 'between' => 'I am between',
  7467. ));
  7468. $expected = array(
  7469. 'div' => array('class' => 'input radio'),
  7470. 'fieldset' => array(),
  7471. 'legend' => array(),
  7472. 'Method',
  7473. '/legend',
  7474. 'I am between',
  7475. 'input' => array(
  7476. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7477. 'value' => '', 'id' => 'ContactMethod_'
  7478. ),
  7479. array('input' => array(
  7480. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7481. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7482. )),
  7483. array('label' => array('for' => 'ContactMethodEmail')),
  7484. 'Email',
  7485. '/label',
  7486. array('input' => array(
  7487. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7488. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7489. )),
  7490. array('label' => array('for' => 'ContactMethodPigeon')),
  7491. 'Pigeon',
  7492. '/label',
  7493. '/fieldset',
  7494. '/div',
  7495. );
  7496. $this->assertTags($result, $expected);
  7497. }
  7498. /**
  7499. * test that some html5 inputs + FormHelper::__call() work
  7500. *
  7501. * @return void
  7502. */
  7503. public function testHtml5Inputs() {
  7504. $result = $this->Form->email('User.email');
  7505. $expected = array(
  7506. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7507. );
  7508. $this->assertTags($result, $expected);
  7509. $result = $this->Form->search('User.query');
  7510. $expected = array(
  7511. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7512. );
  7513. $this->assertTags($result, $expected);
  7514. $result = $this->Form->search('User.query', array('value' => 'test'));
  7515. $expected = array(
  7516. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7517. );
  7518. $this->assertTags($result, $expected);
  7519. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7520. $expected = array(
  7521. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7525. $expected = array(
  7526. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7527. );
  7528. $this->assertTags($result, $expected);
  7529. }
  7530. /**
  7531. *
  7532. * @expectedException CakeException
  7533. * @return void
  7534. */
  7535. public function testHtml5InputException() {
  7536. $this->Form->email();
  7537. }
  7538. /**
  7539. * Tests that a model can be loaded from the model names passed in the request object
  7540. *
  7541. * @return void
  7542. */
  7543. public function testIntrospectModelFromRequest() {
  7544. $this->loadFixtures('Post');
  7545. App::build(array(
  7546. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7547. ));
  7548. CakePlugin::load('TestPlugin');
  7549. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7550. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7551. $this->Form->create('TestPluginPost');
  7552. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7553. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7554. CakePlugin::unload();
  7555. App::build();
  7556. }
  7557. /**
  7558. * Tests that it is possible to set the validation errors directly in the helper for a field
  7559. *
  7560. * @return void
  7561. */
  7562. public function testCustomValidationErrors() {
  7563. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7564. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7565. $this->assertEquals('Badness!', $result);
  7566. }
  7567. /**
  7568. * Tests that the 'on' key validates as expected on create
  7569. *
  7570. * @return void
  7571. */
  7572. public function testRequiredOnCreate() {
  7573. $this->Form->create('Contact');
  7574. $result = $this->Form->input('Contact.imrequiredonupdate');
  7575. $expected = array(
  7576. 'div' => array('class' => 'input text'),
  7577. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7578. 'Imrequiredonupdate',
  7579. '/label',
  7580. 'input' => array(
  7581. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7582. 'id' => 'ContactImrequiredonupdate'
  7583. ),
  7584. '/div'
  7585. );
  7586. $this->assertTags($result, $expected);
  7587. $result = $this->Form->input('Contact.imrequiredoncreate');
  7588. $expected = array(
  7589. 'div' => array('class' => 'input text required'),
  7590. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7591. 'Imrequiredoncreate',
  7592. '/label',
  7593. 'input' => array(
  7594. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7595. 'id' => 'ContactImrequiredoncreate',
  7596. 'required' => 'required'
  7597. ),
  7598. '/div'
  7599. );
  7600. $this->assertTags($result, $expected);
  7601. $result = $this->Form->input('Contact.imrequiredonboth');
  7602. $expected = array(
  7603. 'div' => array('class' => 'input text required'),
  7604. 'label' => array('for' => 'ContactImrequiredonboth'),
  7605. 'Imrequiredonboth',
  7606. '/label',
  7607. 'input' => array(
  7608. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7609. 'id' => 'ContactImrequiredonboth',
  7610. 'required' => 'required'
  7611. ),
  7612. '/div'
  7613. );
  7614. $this->assertTags($result, $expected);
  7615. $this->Form->inputDefaults(array('required' => false));
  7616. $result = $this->Form->input('Contact.imrequired');
  7617. $expected = array(
  7618. 'div' => array('class' => 'input text required'),
  7619. 'label' => array('for' => 'ContactImrequired'),
  7620. 'Imrequired',
  7621. '/label',
  7622. 'input' => array(
  7623. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7624. 'id' => 'ContactImrequired'
  7625. ),
  7626. '/div'
  7627. );
  7628. $this->assertTags($result, $expected);
  7629. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7630. $this->assertTags($result, $expected);
  7631. }
  7632. /**
  7633. * Tests that the 'on' key validates as expected on update
  7634. *
  7635. * @return void
  7636. */
  7637. public function testRequiredOnUpdate() {
  7638. $this->Form->request->data['Contact']['id'] = 1;
  7639. $this->Form->create('Contact');
  7640. $result = $this->Form->input('Contact.imrequiredonupdate');
  7641. $expected = array(
  7642. 'div' => array('class' => 'input text required'),
  7643. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7644. 'Imrequiredonupdate',
  7645. '/label',
  7646. 'input' => array(
  7647. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7648. 'id' => 'ContactImrequiredonupdate',
  7649. 'required' => 'required'
  7650. ),
  7651. '/div'
  7652. );
  7653. $this->assertTags($result, $expected);
  7654. $result = $this->Form->input('Contact.imrequiredoncreate');
  7655. $expected = array(
  7656. 'div' => array('class' => 'input text'),
  7657. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7658. 'Imrequiredoncreate',
  7659. '/label',
  7660. 'input' => array(
  7661. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7662. 'id' => 'ContactImrequiredoncreate'
  7663. ),
  7664. '/div'
  7665. );
  7666. $this->assertTags($result, $expected);
  7667. $result = $this->Form->input('Contact.imrequiredonboth');
  7668. $expected = array(
  7669. 'div' => array('class' => 'input text required'),
  7670. 'label' => array('for' => 'ContactImrequiredonboth'),
  7671. 'Imrequiredonboth',
  7672. '/label',
  7673. 'input' => array(
  7674. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7675. 'id' => 'ContactImrequiredonboth',
  7676. 'required' => 'required'
  7677. ),
  7678. '/div'
  7679. );
  7680. $this->assertTags($result, $expected);
  7681. $result = $this->Form->input('Contact.imrequired');
  7682. $expected = array(
  7683. 'div' => array('class' => 'input text required'),
  7684. 'label' => array('for' => 'ContactImrequired'),
  7685. 'Imrequired',
  7686. '/label',
  7687. 'input' => array(
  7688. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7689. 'id' => 'ContactImrequired',
  7690. 'required' => 'required'
  7691. ),
  7692. '/div'
  7693. );
  7694. $this->assertTags($result, $expected);
  7695. }
  7696. /**
  7697. * Test inputDefaults setter and getter
  7698. *
  7699. * @return void
  7700. */
  7701. public function testInputDefaults() {
  7702. $this->Form->create('Contact');
  7703. $this->Form->inputDefaults(array(
  7704. 'label' => false,
  7705. 'div' => array(
  7706. 'style' => 'color: #000;'
  7707. )
  7708. ));
  7709. $result = $this->Form->input('Contact.field1');
  7710. $expected = array(
  7711. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7712. 'input' => array(
  7713. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7714. 'id' => 'ContactField1'
  7715. ),
  7716. '/div'
  7717. );
  7718. $this->assertTags($result, $expected);
  7719. $this->Form->inputDefaults(array(
  7720. 'div' => false,
  7721. 'label' => 'Label',
  7722. ));
  7723. $result = $this->Form->input('Contact.field1');
  7724. $expected = array(
  7725. 'label' => array('for' => 'ContactField1'),
  7726. 'Label',
  7727. '/label',
  7728. 'input' => array(
  7729. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7730. 'id' => 'ContactField1'
  7731. ),
  7732. );
  7733. $this->assertTags($result, $expected);
  7734. $this->Form->inputDefaults(array(
  7735. 'label' => false,
  7736. ), true);
  7737. $result = $this->Form->input('Contact.field1');
  7738. $expected = array(
  7739. 'input' => array(
  7740. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7741. 'id' => 'ContactField1'
  7742. ),
  7743. );
  7744. $this->assertTags($result, $expected);
  7745. $result = $this->Form->inputDefaults();
  7746. $expected = array(
  7747. 'div' => false,
  7748. 'label' => false,
  7749. );
  7750. $this->assertEqual($result, $expected);
  7751. }
  7752. }