FormHelperTest.php 292 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  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.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var bool false
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var bool false
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var bool false
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var bool false
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var bool false
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  321. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  322. );
  323. /**
  324. * beforeValidate method
  325. *
  326. * @return void
  327. */
  328. public function beforeValidate($options = array()) {
  329. $this->invalidate('email');
  330. return false;
  331. }
  332. }
  333. /**
  334. * ValidateProfile class
  335. *
  336. * @package Cake.Test.Case.View.Helper
  337. */
  338. class ValidateProfile extends CakeTestModel {
  339. /**
  340. * useTable property
  341. *
  342. * @var bool false
  343. */
  344. public $useTable = false;
  345. /**
  346. * schema property
  347. *
  348. * @var array
  349. */
  350. protected $_schema = array(
  351. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  356. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  357. );
  358. /**
  359. * hasOne property
  360. *
  361. * @var array
  362. */
  363. public $hasOne = array('ValidateItem' => array(
  364. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  365. ));
  366. /**
  367. * belongsTo property
  368. *
  369. * @var array
  370. */
  371. public $belongsTo = array('ValidateUser' => array(
  372. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * beforeValidate method
  376. *
  377. * @return void
  378. */
  379. public function beforeValidate($options = array()) {
  380. $this->invalidate('full_name');
  381. $this->invalidate('city');
  382. return false;
  383. }
  384. }
  385. /**
  386. * ValidateItem class
  387. *
  388. * @package Cake.Test.Case.View.Helper
  389. */
  390. class ValidateItem extends CakeTestModel {
  391. /**
  392. * useTable property
  393. *
  394. * @var bool false
  395. */
  396. public $useTable = false;
  397. /**
  398. * schema property
  399. *
  400. * @var array
  401. */
  402. protected $_schema = array(
  403. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  406. 'description' => array(
  407. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  408. ),
  409. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  410. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  411. );
  412. /**
  413. * belongsTo property
  414. *
  415. * @var array
  416. */
  417. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  418. /**
  419. * beforeValidate method
  420. *
  421. * @return void
  422. */
  423. public function beforeValidate($options = array()) {
  424. $this->invalidate('description');
  425. return false;
  426. }
  427. }
  428. /**
  429. * TestMail class
  430. *
  431. * @package Cake.Test.Case.View.Helper
  432. */
  433. class TestMail extends CakeTestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var bool false
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @package Cake.Test.Case.View.Helper
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends CakeTestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. Configure::write('Config.language', 'eng');
  468. Configure::write('App.base', '');
  469. Configure::delete('Asset');
  470. $this->Controller = new ContactTestController();
  471. $this->View = new View($this->Controller);
  472. $this->Form = new FormHelper($this->View);
  473. $this->Form->Html = new HtmlHelper($this->View);
  474. $this->Form->request = new CakeRequest('contacts/add', false);
  475. $this->Form->request->here = '/contacts/add';
  476. $this->Form->request['action'] = 'add';
  477. $this->Form->request->webroot = '';
  478. $this->Form->request->base = '';
  479. ClassRegistry::addObject('Contact', new Contact());
  480. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  481. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  482. ClassRegistry::addObject('User', new UserForm());
  483. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  484. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  485. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  486. $this->oldSalt = Configure::read('Security.salt');
  487. $this->dateRegex = array(
  488. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  490. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  493. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  494. );
  495. Configure::write('Security.salt', 'foo!');
  496. }
  497. /**
  498. * tearDown method
  499. *
  500. * @return void
  501. */
  502. public function tearDown() {
  503. parent::tearDown();
  504. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  505. Configure::write('Security.salt', $this->oldSalt);
  506. }
  507. /**
  508. * testFormCreateWithSecurity method
  509. *
  510. * Test form->create() with security key.
  511. *
  512. * @return void
  513. */
  514. public function testCreateWithSecurity() {
  515. $this->Form->request['_Token'] = array('key' => 'testKey');
  516. $encoding = strtolower(Configure::read('App.encoding'));
  517. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  518. $expected = array(
  519. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  520. 'div' => array('style' => 'display:none;'),
  521. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  522. array('input' => array(
  523. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  524. )),
  525. '/div'
  526. );
  527. $this->assertTags($result, $expected);
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  529. $expected['form']['id'] = 'MyForm';
  530. $this->assertTags($result, $expected);
  531. }
  532. /**
  533. * testFormCreateGetNoSecurity method
  534. *
  535. * Test form->create() with no security key as its a get form
  536. *
  537. * @return void
  538. */
  539. public function testCreateEndGetNoSecurity() {
  540. $this->Form->request['_Token'] = array('key' => 'testKey');
  541. $encoding = strtolower(Configure::read('App.encoding'));
  542. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  543. $this->assertNotContains('Token', $result);
  544. $result = $this->Form->end('Save');
  545. $this->assertNotContains('Token', $result);
  546. }
  547. /**
  548. * test that create() clears the fields property so it starts fresh
  549. *
  550. * @return void
  551. */
  552. public function testCreateClearingFields() {
  553. $this->Form->fields = array('model_id');
  554. $this->Form->create('Contact');
  555. $this->assertEquals(array(), $this->Form->fields);
  556. }
  557. /**
  558. * Tests form hash generation with model-less data
  559. *
  560. * @return void
  561. */
  562. public function testValidateHashNoModel() {
  563. $this->Form->request['_Token'] = array('key' => 'foo');
  564. $result = $this->Form->secure(array('anything'));
  565. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  566. }
  567. /**
  568. * Tests that models with identical field names get resolved properly
  569. *
  570. * @return void
  571. */
  572. public function testDuplicateFieldNameResolution() {
  573. $result = $this->Form->create('ValidateUser');
  574. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateItem.name');
  576. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  577. $result = $this->Form->input('ValidateUser.name');
  578. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  579. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  580. $this->assertRegExp('/type="text"/', $result);
  581. $result = $this->Form->input('ValidateItem.name');
  582. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  583. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  584. $this->assertRegExp('/<textarea/', $result);
  585. $result = $this->Form->input('name');
  586. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  587. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  588. $this->assertRegExp('/type="text"/', $result);
  589. }
  590. /**
  591. * Tests that hidden fields generated for checkboxes don't get locked
  592. *
  593. * @return void
  594. */
  595. public function testNoCheckboxLocking() {
  596. $this->Form->request['_Token'] = array('key' => 'foo');
  597. $this->assertSame(array(), $this->Form->fields);
  598. $this->Form->checkbox('check', array('value' => '1'));
  599. $this->assertSame($this->Form->fields, array('check'));
  600. }
  601. /**
  602. * testFormSecurityFields method
  603. *
  604. * Test generation of secure form hash generation.
  605. *
  606. * @return void
  607. */
  608. public function testFormSecurityFields() {
  609. $key = 'testKey';
  610. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  611. $this->Form->request['_Token'] = array('key' => $key);
  612. $result = $this->Form->secure($fields);
  613. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  614. $hash .= ':' . 'Model.valid';
  615. $hash = urlencode($hash);
  616. $expected = array(
  617. 'div' => array('style' => 'display:none;'),
  618. array('input' => array(
  619. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  620. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  621. )),
  622. array('input' => array(
  623. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  624. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  630. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  631. $result = $this->Form->secure($fields);
  632. $expected = array(
  633. 'div' => array('class' => 'hidden'),
  634. array('input' => array(
  635. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  636. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  637. )),
  638. array('input' => array(
  639. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  640. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for double and float fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldGenerationForFloats() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'float',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => 10
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number',
  668. 'name' => 'data[Contact][foo]',
  669. 'id' => 'ContactFoo',
  670. 'step' => 'any'
  671. )),
  672. '/div'
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Form->input('foo', array('step' => 0.5));
  676. $expected = array(
  677. 'div' => array('class' => 'input number'),
  678. 'label' => array('for' => 'ContactFoo'),
  679. 'Foo',
  680. '/label',
  681. array('input' => array(
  682. 'type' => 'number',
  683. 'name' => 'data[Contact][foo]',
  684. 'id' => 'ContactFoo',
  685. 'step' => '0.5'
  686. )),
  687. '/div'
  688. );
  689. $this->assertTags($result, $expected);
  690. }
  691. /**
  692. * Tests correct generation of number fields for integer fields
  693. *
  694. * @return void
  695. */
  696. public function testTextFieldTypeNumberGenerationForIntegers() {
  697. $model = ClassRegistry::getObject('Contact');
  698. $model->setSchema(array('foo' => array(
  699. 'type' => 'integer',
  700. 'null' => false,
  701. 'default' => null,
  702. 'length' => null
  703. )));
  704. $this->Form->create('Contact');
  705. $result = $this->Form->input('foo');
  706. $expected = array(
  707. 'div' => array('class' => 'input number'),
  708. 'label' => array('for' => 'ContactFoo'),
  709. 'Foo',
  710. '/label',
  711. array('input' => array(
  712. 'type' => 'number', 'name' => 'data[Contact][foo]',
  713. 'id' => 'ContactFoo'
  714. )),
  715. '/div'
  716. );
  717. $this->assertTags($result, $expected);
  718. }
  719. /**
  720. * testFormSecurityMultipleFields method
  721. *
  722. * Test secure() with multiple row form. Ensure hash is correct.
  723. *
  724. * @return void
  725. */
  726. public function testFormSecurityMultipleFields() {
  727. $key = 'testKey';
  728. $fields = array(
  729. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  730. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  731. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  732. );
  733. $this->Form->request['_Token'] = array('key' => $key);
  734. $result = $this->Form->secure($fields);
  735. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  736. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  737. $expected = array(
  738. 'div' => array('style' => 'display:none;'),
  739. array('input' => array(
  740. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  741. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  742. )),
  743. array('input' => array(
  744. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  745. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  746. )),
  747. '/div'
  748. );
  749. $this->assertTags($result, $expected);
  750. }
  751. /**
  752. * testFormSecurityMultipleSubmitButtons
  753. *
  754. * test form submit generation and ensure that _Token is only created on end()
  755. *
  756. * @return void
  757. */
  758. public function testFormSecurityMultipleSubmitButtons() {
  759. $key = 'testKey';
  760. $this->Form->request['_Token'] = array('key' => $key);
  761. $this->Form->create('Addresses');
  762. $this->Form->input('Address.title');
  763. $this->Form->input('Address.first_name');
  764. $result = $this->Form->submit('Save', array('name' => 'save'));
  765. $expected = array(
  766. 'div' => array('class' => 'submit'),
  767. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  768. '/div',
  769. );
  770. $this->assertTags($result, $expected);
  771. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  772. $expected = array(
  773. 'div' => array('class' => 'submit'),
  774. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  775. '/div',
  776. );
  777. $this->assertTags($result, $expected);
  778. $result = $this->Form->end(null);
  779. $expected = array(
  780. 'div' => array('style' => 'display:none;'),
  781. array('input' => array(
  782. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  783. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  784. )),
  785. array('input' => array(
  786. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  787. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  788. )),
  789. '/div'
  790. );
  791. $this->assertTags($result, $expected);
  792. }
  793. /**
  794. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  795. *
  796. * @return void
  797. */
  798. public function testSecurityButtonNestedNamed() {
  799. $key = 'testKey';
  800. $this->Form->request['_Token'] = array('key' => $key);
  801. $this->Form->create('Addresses');
  802. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  803. $result = $this->Form->unlockField();
  804. $this->assertEquals(array('Address.button'), $result);
  805. }
  806. /**
  807. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  808. *
  809. * @return void
  810. */
  811. public function testSecuritySubmitNestedNamed() {
  812. $key = 'testKey';
  813. $this->Form->request['_Token'] = array('key' => $key);
  814. $this->Form->create('Addresses');
  815. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  816. $result = $this->Form->unlockField();
  817. $this->assertEquals(array('Address.button'), $result);
  818. }
  819. /**
  820. * Test that the correct fields are unlocked for image submits with no names.
  821. *
  822. * @return void
  823. */
  824. public function testSecuritySubmitImageNoName() {
  825. $key = 'testKey';
  826. $this->Form->request['_Token'] = array('key' => $key);
  827. $this->Form->create('User');
  828. $result = $this->Form->submit('save.png');
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  832. '/div'
  833. );
  834. $this->assertTags($result, $expected);
  835. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  836. }
  837. /**
  838. * Test that the correct fields are unlocked for image submits with names.
  839. *
  840. * @return void
  841. */
  842. public function testSecuritySubmitImageName() {
  843. $key = 'testKey';
  844. $this->Form->request['_Token'] = array('key' => $key);
  845. $this->Form->create('User');
  846. $result = $this->Form->submit('save.png', array('name' => 'test'));
  847. $expected = array(
  848. 'div' => array('class' => 'submit'),
  849. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  850. '/div'
  851. );
  852. $this->assertTags($result, $expected);
  853. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  854. }
  855. /**
  856. * testFormSecurityMultipleInputFields method
  857. *
  858. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  859. *
  860. * @return void
  861. */
  862. public function testFormSecurityMultipleInputFields() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('Addresses');
  866. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  867. $this->Form->input('Addresses.0.title');
  868. $this->Form->input('Addresses.0.first_name');
  869. $this->Form->input('Addresses.0.last_name');
  870. $this->Form->input('Addresses.0.address');
  871. $this->Form->input('Addresses.0.city');
  872. $this->Form->input('Addresses.0.phone');
  873. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  874. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  875. $this->Form->input('Addresses.1.title');
  876. $this->Form->input('Addresses.1.first_name');
  877. $this->Form->input('Addresses.1.last_name');
  878. $this->Form->input('Addresses.1.address');
  879. $this->Form->input('Addresses.1.city');
  880. $this->Form->input('Addresses.1.phone');
  881. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  882. $result = $this->Form->secure($this->Form->fields);
  883. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  884. $expected = array(
  885. 'div' => array('style' => 'display:none;'),
  886. array('input' => array(
  887. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  888. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  889. )),
  890. array('input' => array(
  891. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  892. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  893. )),
  894. '/div'
  895. );
  896. $this->assertTags($result, $expected);
  897. }
  898. /**
  899. * Test form security with Model.field.0 style inputs
  900. *
  901. * @return void
  902. */
  903. public function testFormSecurityArrayFields() {
  904. $key = 'testKey';
  905. $this->Form->request->params['_Token']['key'] = $key;
  906. $this->Form->create('Address');
  907. $this->Form->input('Address.primary.1');
  908. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  909. $this->Form->input('Address.secondary.1.0');
  910. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  911. }
  912. /**
  913. * testFormSecurityMultipleInputDisabledFields method
  914. *
  915. * test secure form generation with multiple records and disabled fields.
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityMultipleInputDisabledFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token'] = array(
  922. 'key' => $key,
  923. 'unlockedFields' => array('first_name', 'address')
  924. );
  925. $this->Form->create();
  926. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  927. $this->Form->input('Addresses.0.title');
  928. $this->Form->input('Addresses.0.first_name');
  929. $this->Form->input('Addresses.0.last_name');
  930. $this->Form->input('Addresses.0.address');
  931. $this->Form->input('Addresses.0.city');
  932. $this->Form->input('Addresses.0.phone');
  933. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  934. $this->Form->input('Addresses.1.title');
  935. $this->Form->input('Addresses.1.first_name');
  936. $this->Form->input('Addresses.1.last_name');
  937. $this->Form->input('Addresses.1.address');
  938. $this->Form->input('Addresses.1.city');
  939. $this->Form->input('Addresses.1.phone');
  940. $result = $this->Form->secure($this->Form->fields);
  941. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  942. $expected = array(
  943. 'div' => array('style' => 'display:none;'),
  944. array('input' => array(
  945. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  946. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  947. )),
  948. array('input' => array(
  949. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  950. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  951. )),
  952. '/div'
  953. );
  954. $this->assertTags($result, $expected);
  955. }
  956. /**
  957. * testFormSecurityInputDisabledFields method
  958. *
  959. * Test single record form with disabled fields.
  960. *
  961. * @return void
  962. */
  963. public function testFormSecurityInputUnlockedFields() {
  964. $key = 'testKey';
  965. $this->Form->request['_Token'] = array(
  966. 'key' => $key,
  967. 'unlockedFields' => array('first_name', 'address')
  968. );
  969. $this->Form->create();
  970. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  971. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  972. $this->Form->input('Addresses.title');
  973. $this->Form->input('Addresses.first_name');
  974. $this->Form->input('Addresses.last_name');
  975. $this->Form->input('Addresses.address');
  976. $this->Form->input('Addresses.city');
  977. $this->Form->input('Addresses.phone');
  978. $result = $this->Form->fields;
  979. $expected = array(
  980. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  981. 'Addresses.city', 'Addresses.phone'
  982. );
  983. $this->assertEquals($expected, $result);
  984. $result = $this->Form->secure($expected);
  985. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  986. $expected = array(
  987. 'div' => array('style' => 'display:none;'),
  988. array('input' => array(
  989. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  990. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  991. )),
  992. array('input' => array(
  993. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  994. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  995. )),
  996. '/div'
  997. );
  998. $this->assertTags($result, $expected);
  999. }
  1000. /**
  1001. * test securing inputs with custom name attributes.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testFormSecureWithCustomNameAttribute() {
  1006. $this->Form->request->params['_Token']['key'] = 'testKey';
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1008. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1009. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1010. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1011. }
  1012. /**
  1013. * testFormSecuredInput method
  1014. *
  1015. * Test generation of entire secure form, assertions made on input() output.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testFormSecuredInput() {
  1020. $this->Form->request['_Token'] = array('key' => 'testKey');
  1021. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1022. $encoding = strtolower(Configure::read('App.encoding'));
  1023. $expected = array(
  1024. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1025. 'div' => array('style' => 'display:none;'),
  1026. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1029. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1035. $expected = array(
  1036. 'div' => array('class' => 'input text'),
  1037. 'label' => array('for' => 'UserFormPublished'),
  1038. 'Published',
  1039. '/label',
  1040. array('input' => array(
  1041. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1042. 'id' => 'UserFormPublished'
  1043. )),
  1044. '/div'
  1045. );
  1046. $this->assertTags($result, $expected);
  1047. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1048. $expected = array(
  1049. 'div' => array('class' => 'input text'),
  1050. 'label' => array('for' => 'UserFormOther'),
  1051. 'Other',
  1052. '/label',
  1053. array('input' => array(
  1054. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1055. 'id' => 'UserFormOther'
  1056. )),
  1057. '/div'
  1058. );
  1059. $this->assertTags($result, $expected);
  1060. $result = $this->Form->hidden('UserForm.stuff');
  1061. $expected = array(
  1062. 'input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1064. 'id' => 'UserFormStuff'
  1065. ));
  1066. $this->assertTags($result, $expected);
  1067. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1068. $expected = array('input' => array(
  1069. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1070. 'value' => '0', 'id' => 'UserFormHidden'
  1071. ));
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1074. $expected = array(
  1075. 'div' => array('class' => 'input checkbox'),
  1076. array('input' => array(
  1077. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1078. 'value' => '0', 'id' => 'UserFormSomething_'
  1079. )),
  1080. array('input' => array(
  1081. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1082. 'value' => '1', 'id' => 'UserFormSomething'
  1083. )),
  1084. 'label' => array('for' => 'UserFormSomething'),
  1085. 'Something',
  1086. '/label',
  1087. '/div'
  1088. );
  1089. $this->assertTags($result, $expected);
  1090. $result = $this->Form->fields;
  1091. $expected = array(
  1092. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1093. 'UserForm.hidden' => '0', 'UserForm.something'
  1094. );
  1095. $this->assertEquals($expected, $result);
  1096. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1097. $result = $this->Form->secure($this->Form->fields);
  1098. $expected = array(
  1099. 'div' => array('style' => 'display:none;'),
  1100. array('input' => array(
  1101. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1102. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1103. )),
  1104. array('input' => array(
  1105. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1106. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1107. )),
  1108. '/div'
  1109. );
  1110. $this->assertTags($result, $expected);
  1111. }
  1112. /**
  1113. * Test secured inputs with custom names.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testSecuredInputCustomName() {
  1118. $this->Form->request['_Token'] = array('key' => 'testKey');
  1119. $this->assertEquals(array(), $this->Form->fields);
  1120. $this->Form->input('text_input', array(
  1121. 'name' => 'data[Option][General.default_role]',
  1122. ));
  1123. $expected = array('Option.General.default_role');
  1124. $this->assertEquals($expected, $this->Form->fields);
  1125. $this->Form->input('select_box', array(
  1126. 'name' => 'data[Option][General.select_role]',
  1127. 'type' => 'select',
  1128. 'options' => array(1, 2),
  1129. ));
  1130. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1131. $this->assertEquals($expected, $this->Form->fields);
  1132. }
  1133. /**
  1134. * Tests that the correct keys are added to the field hash index
  1135. *
  1136. * @return void
  1137. */
  1138. public function testFormSecuredFileInput() {
  1139. $this->Form->request['_Token'] = array('key' => 'testKey');
  1140. $this->assertEquals(array(), $this->Form->fields);
  1141. $this->Form->file('Attachment.file');
  1142. $expected = array(
  1143. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1144. 'Attachment.file.error', 'Attachment.file.size'
  1145. );
  1146. $this->assertEquals($expected, $this->Form->fields);
  1147. }
  1148. /**
  1149. * test that multiple selects keys are added to field hash
  1150. *
  1151. * @return void
  1152. */
  1153. public function testFormSecuredMultipleSelect() {
  1154. $this->Form->request['_Token'] = array('key' => 'testKey');
  1155. $this->assertEquals(array(), $this->Form->fields);
  1156. $options = array('1' => 'one', '2' => 'two');
  1157. $this->Form->select('Model.select', $options);
  1158. $expected = array('Model.select');
  1159. $this->assertEquals($expected, $this->Form->fields);
  1160. $this->Form->fields = array();
  1161. $this->Form->select('Model.select', $options, array('multiple' => true));
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * testFormSecuredRadio method
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecuredRadio() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'option1', '2' => 'option2');
  1173. $this->Form->radio('Test.test', $options);
  1174. $expected = array('Test.test');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * Test that when disabled is in a list based attribute array it works.
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredAndDisabledNotAssoc() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1185. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1186. $this->Form->text('Model.text', array('disabled'));
  1187. $this->Form->textarea('Model.textarea', array('disabled'));
  1188. $this->Form->password('Model.password', array('disabled'));
  1189. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1190. $expected = array(
  1191. 'Model.radio' => ''
  1192. );
  1193. $this->assertEquals($expected, $this->Form->fields);
  1194. }
  1195. /**
  1196. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1197. * hashing.
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredAndDisabled() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1204. $this->Form->text('Model.text', array('disabled' => true));
  1205. $this->Form->password('Model.text', array('disabled' => true));
  1206. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1207. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1208. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1209. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1210. $this->Form->month('Model.month', array('disabled' => true));
  1211. $this->Form->day('Model.day', array('disabled' => true));
  1212. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1213. $this->Form->minute('Model.minute', array('disabled' => true));
  1214. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1215. $expected = array(
  1216. 'Model.radio' => ''
  1217. );
  1218. $this->assertEquals($expected, $this->Form->fields);
  1219. }
  1220. /**
  1221. * testDisableSecurityUsingForm method
  1222. *
  1223. * @return void
  1224. */
  1225. public function testDisableSecurityUsingForm() {
  1226. $this->Form->request['_Token'] = array(
  1227. 'key' => 'testKey',
  1228. 'disabledFields' => array()
  1229. );
  1230. $this->Form->create();
  1231. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1232. $this->Form->input('Addresses.title');
  1233. $this->Form->input('Addresses.first_name', array('secure' => false));
  1234. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1235. $this->Form->input('Addresses.zip', array(
  1236. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1237. ));
  1238. $result = $this->Form->fields;
  1239. $expected = array(
  1240. 'Addresses.id' => '123456', 'Addresses.title',
  1241. );
  1242. $this->assertEquals($expected, $result);
  1243. }
  1244. /**
  1245. * test disableField
  1246. *
  1247. * @return void
  1248. */
  1249. public function testUnlockFieldAddsToList() {
  1250. $this->Form->request['_Token'] = array(
  1251. 'key' => 'testKey',
  1252. 'unlockedFields' => array()
  1253. );
  1254. $this->Form->create('Contact');
  1255. $this->Form->unlockField('Contact.name');
  1256. $this->Form->text('Contact.name');
  1257. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1258. $this->assertEquals(array(), $this->Form->fields);
  1259. }
  1260. /**
  1261. * test unlockField removing from fields array.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUnlockFieldRemovingFromFields() {
  1266. $this->Form->request['_Token'] = array(
  1267. 'key' => 'testKey',
  1268. 'unlockedFields' => array()
  1269. );
  1270. $this->Form->create('Contact');
  1271. $this->Form->hidden('Contact.id', array('value' => 1));
  1272. $this->Form->text('Contact.name');
  1273. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1274. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1275. $this->Form->unlockField('Contact.name');
  1276. $this->Form->unlockField('Contact.id');
  1277. $this->assertEquals(array(), $this->Form->fields);
  1278. }
  1279. /**
  1280. * testTagIsInvalid method
  1281. *
  1282. * @return void
  1283. */
  1284. public function testTagIsInvalid() {
  1285. $Contact = ClassRegistry::getObject('Contact');
  1286. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1287. $this->Form->setEntity('Contact.0.email');
  1288. $result = $this->Form->tagIsInvalid();
  1289. $this->assertEquals($expected, $result);
  1290. $this->Form->setEntity('Contact.1.email');
  1291. $result = $this->Form->tagIsInvalid();
  1292. $this->assertFalse($result);
  1293. $this->Form->setEntity('Contact.0.name');
  1294. $result = $this->Form->tagIsInvalid();
  1295. $this->assertFalse($result);
  1296. }
  1297. /**
  1298. * Test tagIsInvalid with validation errors from a saveMany
  1299. *
  1300. * @return void
  1301. */
  1302. public function testTagIsInvalidSaveMany() {
  1303. $Contact = ClassRegistry::getObject('Contact');
  1304. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1305. $this->Form->create('Contact');
  1306. $this->Form->setEntity('0.email');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $this->assertEquals($expected, $result);
  1309. $this->Form->setEntity('0.Contact.email');
  1310. $result = $this->Form->tagIsInvalid();
  1311. $this->assertEquals($expected, $result);
  1312. }
  1313. /**
  1314. * Test validation errors.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testPasswordValidation() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors['password'] = array('Please provide a password');
  1321. $result = $this->Form->input('Contact.password');
  1322. $expected = array(
  1323. 'div' => array('class' => 'input password error'),
  1324. 'label' => array('for' => 'ContactPassword'),
  1325. 'Password',
  1326. '/label',
  1327. 'input' => array(
  1328. 'type' => 'password', 'name' => 'data[Contact][password]',
  1329. 'id' => 'ContactPassword', 'class' => 'form-error'
  1330. ),
  1331. array('div' => array('class' => 'error-message')),
  1332. 'Please provide a password',
  1333. '/div',
  1334. '/div'
  1335. );
  1336. $this->assertTags($result, $expected);
  1337. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1338. $expected = array(
  1339. 'div' => array('class' => 'input password error'),
  1340. 'label' => array('for' => 'ContactPassword'),
  1341. 'Password',
  1342. '/label',
  1343. 'input' => array(
  1344. 'type' => 'password', 'name' => 'data[Contact][password]',
  1345. 'id' => 'ContactPassword', 'class' => 'form-error'
  1346. ),
  1347. '/div'
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * Test validation errors, when validation message is an empty string.
  1353. *
  1354. * @return void
  1355. */
  1356. public function testEmptyErrorValidation() {
  1357. $this->Form->validationErrors['Contact']['password'] = '';
  1358. $result = $this->Form->input('Contact.password');
  1359. $expected = array(
  1360. 'div' => array('class' => 'input password error'),
  1361. 'label' => array('for' => 'ContactPassword'),
  1362. 'Password',
  1363. '/label',
  1364. 'input' => array(
  1365. 'type' => 'password', 'name' => 'data[Contact][password]',
  1366. 'id' => 'ContactPassword', 'class' => 'form-error'
  1367. ),
  1368. array('div' => array('class' => 'error-message')),
  1369. array(),
  1370. '/div',
  1371. '/div'
  1372. );
  1373. $this->assertTags($result, $expected);
  1374. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1375. $expected = array(
  1376. 'div' => array('class' => 'input password error'),
  1377. 'label' => array('for' => 'ContactPassword'),
  1378. 'Password',
  1379. '/label',
  1380. 'input' => array(
  1381. 'type' => 'password', 'name' => 'data[Contact][password]',
  1382. 'id' => 'ContactPassword', 'class' => 'form-error'
  1383. ),
  1384. '/div'
  1385. );
  1386. $this->assertTags($result, $expected);
  1387. }
  1388. /**
  1389. * Test validation errors, when calling input() overriding validation message by an empty string.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. '/div'
  1422. );
  1423. $this->assertTags($result, $expected);
  1424. }
  1425. /**
  1426. * testFormValidationAssociated method
  1427. *
  1428. * test display of form errors in conjunction with model::validates.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormValidationAssociated() {
  1433. $this->UserForm = ClassRegistry::getObject('UserForm');
  1434. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1435. $data = array(
  1436. 'UserForm' => array('name' => 'user'),
  1437. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1438. );
  1439. $result = $this->UserForm->OpenidUrl->create($data);
  1440. $this->assertFalse(empty($result));
  1441. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1442. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1443. $encoding = strtolower(Configure::read('App.encoding'));
  1444. $expected = array(
  1445. 'form' => array(
  1446. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1447. 'accept-charset' => $encoding
  1448. ),
  1449. 'div' => array('style' => 'display:none;'),
  1450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1451. '/div'
  1452. );
  1453. $this->assertTags($result, $expected);
  1454. $result = $this->Form->error(
  1455. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Error, not registered', $result);
  1458. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1459. }
  1460. /**
  1461. * testFormValidationAssociatedFirstLevel method
  1462. *
  1463. * test form error display with associated model.
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormValidationAssociatedFirstLevel() {
  1468. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1469. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1470. $data = array(
  1471. 'ValidateUser' => array('name' => 'mariano'),
  1472. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1473. );
  1474. $result = $this->ValidateUser->create($data);
  1475. $this->assertFalse(empty($result));
  1476. $this->assertFalse($this->ValidateUser->validates());
  1477. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1478. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1479. $encoding = strtolower(Configure::read('App.encoding'));
  1480. $expected = array(
  1481. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1482. 'div' => array('style' => 'display:none;'),
  1483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1484. '/div'
  1485. );
  1486. $this->assertTags($result, $expected);
  1487. $result = $this->Form->error(
  1488. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid email', $result);
  1491. $result = $this->Form->error(
  1492. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid name', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid city', $result);
  1499. unset($this->ValidateUser->ValidateProfile);
  1500. unset($this->ValidateUser);
  1501. }
  1502. /**
  1503. * testFormValidationAssociatedSecondLevel method
  1504. *
  1505. * test form error display with associated model.
  1506. *
  1507. * @return void
  1508. */
  1509. public function testFormValidationAssociatedSecondLevel() {
  1510. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1511. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1512. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1513. $data = array(
  1514. 'ValidateUser' => array('name' => 'mariano'),
  1515. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1516. 'ValidateItem' => array('name' => 'Item')
  1517. );
  1518. $result = $this->ValidateUser->create($data);
  1519. $this->assertFalse(empty($result));
  1520. $this->assertFalse($this->ValidateUser->validates());
  1521. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $result = $this->Form->error(
  1544. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1545. );
  1546. $this->assertEquals('Invalid description', $result);
  1547. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1548. unset($this->ValidateUser->ValidateProfile);
  1549. unset($this->ValidateUser);
  1550. }
  1551. /**
  1552. * testFormValidationMultiRecord method
  1553. *
  1554. * test form error display with multiple records.
  1555. *
  1556. * @return void
  1557. */
  1558. public function testFormValidationMultiRecord() {
  1559. $Contact = ClassRegistry::getObject('Contact');
  1560. $Contact->validationErrors[2] = array(
  1561. 'name' => array('This field cannot be left blank')
  1562. );
  1563. $result = $this->Form->input('Contact.2.name');
  1564. $expected = array(
  1565. 'div' => array('class' => 'input text error'),
  1566. 'label' => array('for' => 'Contact2Name'),
  1567. 'Name',
  1568. '/label',
  1569. 'input' => array(
  1570. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1571. 'class' => 'form-error', 'maxlength' => 255
  1572. ),
  1573. array('div' => array('class' => 'error-message')),
  1574. 'This field cannot be left blank',
  1575. '/div',
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. }
  1580. /**
  1581. * testMultipleInputValidation method
  1582. *
  1583. * test multiple record form validation error display.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testMultipleInputValidation() {
  1588. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1589. $Address->validationErrors[0] = array(
  1590. 'title' => array('This field cannot be empty'),
  1591. 'first_name' => array('This field cannot be empty')
  1592. );
  1593. $Address->validationErrors[1] = array(
  1594. 'last_name' => array('You must have a last name')
  1595. );
  1596. $this->Form->create();
  1597. $result = $this->Form->input('Address.0.title');
  1598. $expected = array(
  1599. 'div' => array('class'),
  1600. 'label' => array('for'),
  1601. 'preg:/[^<]+/',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1605. ),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.first_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1619. array('div' => array('class' => 'error-message')),
  1620. 'This field cannot be empty',
  1621. '/div',
  1622. '/div'
  1623. );
  1624. $this->assertTags($result, $expected);
  1625. $result = $this->Form->input('Address.0.last_name');
  1626. $expected = array(
  1627. 'div' => array('class'),
  1628. 'label' => array('for'),
  1629. 'preg:/[^<]+/',
  1630. '/label',
  1631. 'input' => array('type' => 'text', 'name', 'id'),
  1632. '/div'
  1633. );
  1634. $this->assertTags($result, $expected);
  1635. $result = $this->Form->input('Address.1.last_name');
  1636. $expected = array(
  1637. 'div' => array('class'),
  1638. 'label' => array('for'),
  1639. 'preg:/[^<]+/',
  1640. '/label',
  1641. 'input' => array(
  1642. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1643. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'You must have a last name',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. }
  1652. /**
  1653. * testInput method
  1654. *
  1655. * Test various incarnations of input().
  1656. *
  1657. * @return void
  1658. */
  1659. public function testInput() {
  1660. $result = $this->Form->input('ValidateUser.balance');
  1661. $expected = array(
  1662. 'div' => array('class'),
  1663. 'label' => array('for'),
  1664. 'Balance',
  1665. '/label',
  1666. 'input' => array('name', 'type' => 'number', 'id'),
  1667. '/div',
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1671. $expected = array(
  1672. 'div' => array('class' => 'input email'),
  1673. 'label' => array('for' => 'custom'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'data[Contact][email]',
  1678. 'id' => 'custom', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1684. $expected = array(
  1685. '<div',
  1686. 'label' => array('for' => 'ContactEmail'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'email', 'name' => 'data[Contact][email]',
  1691. 'id' => 'ContactEmail', 'maxlength' => 255
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->hidden('Contact.idontexist');
  1697. $expected = array('input' => array(
  1698. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1699. 'id' => 'ContactIdontexist'
  1700. ));
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][email]',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'Contact5Email'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1723. 'id' => 'Contact5Email'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.password');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input password'),
  1731. 'label' => array('for' => 'ContactPassword'),
  1732. 'Password',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'password', 'name' => 'data[Contact][password]',
  1736. 'id' => 'ContactPassword'
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array(
  1742. 'type' => 'file', 'class' => 'textbox'
  1743. ));
  1744. $expected = array(
  1745. 'div' => array('class' => 'input file'),
  1746. 'label' => array('for' => 'ContactEmail'),
  1747. 'Email',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1751. 'id' => 'ContactEmail'
  1752. )),
  1753. '/div'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1757. $result = $this->Form->input('Contact.phone');
  1758. $expected = array(
  1759. 'div' => array('class' => 'input tel'),
  1760. 'label' => array('for' => 'ContactPhone'),
  1761. 'Phone',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1765. 'value' => 'Hello &amp; World &gt; weird chars',
  1766. 'id' => 'ContactPhone', 'maxlength' => 255
  1767. )),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1772. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1773. $expected = array(
  1774. 'div' => array('class' => 'input text'),
  1775. 'label' => array('for' => 'myId'),
  1776. 'Field',
  1777. '/label',
  1778. 'input' => array(
  1779. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1780. 'value' => 'My value', 'id' => 'myId'
  1781. ),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. unset($this->Form->request->data);
  1786. $Contact = ClassRegistry::getObject('Contact');
  1787. $Contact->validationErrors['field'] = array('Badness!');
  1788. $result = $this->Form->input('Contact.field');
  1789. $expected = array(
  1790. 'div' => array('class' => 'input text error'),
  1791. 'label' => array('for' => 'ContactField'),
  1792. 'Field',
  1793. '/label',
  1794. 'input' => array(
  1795. 'type' => 'text', 'name' => 'data[Contact][field]',
  1796. 'id' => 'ContactField', 'class' => 'form-error'
  1797. ),
  1798. array('div' => array('class' => 'error-message')),
  1799. 'Badness!',
  1800. '/div',
  1801. '/div'
  1802. );
  1803. $this->assertTags($result, $expected);
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1806. ));
  1807. $expected = array(
  1808. 'label' => array('for' => 'ContactField'),
  1809. 'Field',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => 'data[Contact][field]',
  1813. 'id' => 'ContactField', 'class' => 'form-error'
  1814. ),
  1815. array('span' => array('class' => 'error-message')),
  1816. 'Badness!',
  1817. '/span'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $result = $this->Form->input('Contact.field', array(
  1821. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1822. ));
  1823. $expected = array(
  1824. 'div' => array('class' => 'input text error'),
  1825. 'label' => array('for' => 'ContactField'),
  1826. 'Field',
  1827. '/label',
  1828. 'input' => array(
  1829. 'type' => 'text', 'name' => 'data[Contact][field]',
  1830. 'id' => 'ContactField', 'class' => 'form-error'
  1831. ),
  1832. array('div' => array('class' => 'error')),
  1833. 'Badness!',
  1834. '/div'
  1835. );
  1836. $this->assertTags($result, $expected);
  1837. $result = $this->Form->input('Contact.field', array(
  1838. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1839. ));
  1840. $expected = array(
  1841. 'span' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'Badness!',
  1850. '/span'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1854. $expected = array(
  1855. 'div' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'A message to you, Rudy',
  1864. array('div' => array('class' => 'error-message')),
  1865. 'Badness!',
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $this->Form->setEntity(null);
  1871. $this->Form->setEntity('Contact.field');
  1872. $result = $this->Form->input('Contact.field', array(
  1873. 'after' => 'A message to you, Rudy', 'error' => false
  1874. ));
  1875. $expected = array(
  1876. 'div' => array('class' => 'input text'),
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1881. 'A message to you, Rudy',
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ObjectField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $Contact->validationErrors['field'] = array('minLength');
  1897. $result = $this->Form->input('Contact.field', array(
  1898. 'error' => array(
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large'
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Le login doit contenir au moins 2 caractères',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $Contact->validationErrors['field'] = array('maxLength');
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'error' => array(
  1918. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1919. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1920. 'maxLength' => 'login too large',
  1921. )
  1922. ));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1929. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1930. 'login too large',
  1931. '/span',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test that inputs with 0 can be created.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testInputZero() {
  1942. $this->Form->create('User');
  1943. $result = $this->Form->input('0');
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'User0'), '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1948. '/div'
  1949. );
  1950. $this->assertTags($result, $expected);
  1951. }
  1952. /**
  1953. * test input() with checkbox creation
  1954. *
  1955. * @return void
  1956. */
  1957. public function testInputCheckbox() {
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1975. $expected = array(
  1976. 'div' => array('class' => 'input checkbox'),
  1977. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1978. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $result = $this->Form->input('User.disabled', array(
  1983. 'label' => 'Disabled',
  1984. 'type' => 'checkbox',
  1985. 'data-foo' => 'disabled'
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1990. array('input' => array(
  1991. 'type' => 'checkbox',
  1992. 'name' => 'data[User][disabled]',
  1993. 'value' => '1',
  1994. 'id' => 'UserDisabled',
  1995. 'data-foo' => 'disabled'
  1996. )),
  1997. 'label' => array('for' => 'UserDisabled'),
  1998. 'Disabled',
  1999. '/label',
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. }
  2004. /**
  2005. * test form->input() with time types.
  2006. *
  2007. */
  2008. public function testInputTime() {
  2009. extract($this->dateRegex);
  2010. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2011. $result = explode(':', $result);
  2012. $this->assertRegExp('/option value="23"/', $result[0]);
  2013. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2014. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2015. $result = explode(':', $result);
  2016. $this->assertRegExp('/option value="23"/', $result[0]);
  2017. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('Model.field', array(
  2026. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2027. ));
  2028. $result = explode(':', $result);
  2029. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2030. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2032. $result = $this->Form->input('prueba', array(
  2033. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2034. 'maxYear' => date('Y') + 1 , 'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2041. $result = $this->Form->input('Random.start_time', array(
  2042. 'type' => 'time',
  2043. 'selected' => '18:15'
  2044. ));
  2045. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2046. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2047. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2048. $result = $this->Form->input('published', array('type' => 'time'));
  2049. $now = strtotime('now');
  2050. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2051. $now = strtotime('2013-03-09 00:42:21');
  2052. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2053. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2054. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2055. }
  2056. /**
  2057. * Test interval + selected near the hour roll over.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testTimeSelectedWithInterval() {
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'type' => 'time',
  2064. 'interval' => 15,
  2065. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2066. ));
  2067. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'type' => 'time',
  2072. 'interval' => 15,
  2073. 'selected' => '2012-10-23 15:57:00'
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => '15:57'
  2083. ));
  2084. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $result = $this->Form->input('Model.start_time', array(
  2087. 'timeFormat' => 24,
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '23:57'
  2091. ));
  2092. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $result = $this->Form->input('Model.created', array(
  2095. 'timeFormat' => 24,
  2096. 'type' => 'datetime',
  2097. 'interval' => 15,
  2098. 'selected' => '2012-09-30 23:56'
  2099. ));
  2100. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2101. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2102. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. }
  2106. /**
  2107. * Test interval & timeFormat = 12
  2108. *
  2109. * @return void
  2110. */
  2111. public function testInputTimeWithIntervalAnd12HourFormat() {
  2112. $result = $this->Form->input('Model.start_time', array(
  2113. 'type' => 'time',
  2114. 'timeFormat' => 12,
  2115. 'interval' => 5,
  2116. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2117. ));
  2118. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2119. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2120. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2121. $result = $this->Form->input('Model.start_time', array(
  2122. 'type' => 'time',
  2123. 'timeFormat' => '12',
  2124. 'interval' => 5,
  2125. 'selected' => '2013-04-19 16:30:00'
  2126. ));
  2127. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2128. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2129. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2130. $result = $this->Form->input('Model.start_time', array(
  2131. 'type' => 'time',
  2132. 'timeFormat' => '12',
  2133. 'interval' => 10,
  2134. 'selected' => '2013-05-19 00:33:00'
  2135. ));
  2136. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2137. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2138. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => '12',
  2142. 'interval' => 10,
  2143. 'selected' => '2013-05-19 13:33:00'
  2144. ));
  2145. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 10,
  2152. 'selected' => '2013-05-19 01:33:00'
  2153. ));
  2154. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2157. }
  2158. /**
  2159. * test form->input() with datetime, date and time types
  2160. *
  2161. * @return void
  2162. */
  2163. public function testInputDatetime() {
  2164. extract($this->dateRegex);
  2165. $result = $this->Form->input('prueba', array(
  2166. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2167. 'maxYear' => date('Y') + 1, 'interval' => 15
  2168. ));
  2169. $result = explode(':', $result);
  2170. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2171. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2172. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2174. //related to ticket #5013
  2175. $result = $this->Form->input('Contact.date', array(
  2176. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2177. ));
  2178. $this->assertRegExp('/class="customClass"/', $result);
  2179. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2180. $result = $this->Form->input('Contact.date', array(
  2181. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2182. ));
  2183. $this->assertRegExp('/id="customIdDay"/', $result);
  2184. $this->assertRegExp('/id="customIdMonth"/', $result);
  2185. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2186. $result = $this->Form->input('Model.field', array(
  2187. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2188. ));
  2189. $this->assertRegExp('/id="customIDDay"/', $result);
  2190. $this->assertRegExp('/id="customIDHour"/', $result);
  2191. $result = explode('</select><select', $result);
  2192. $result = explode(':', $result[1]);
  2193. $this->assertRegExp('/option value="23"/', $result[0]);
  2194. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2195. $result = $this->Form->input('Model.field', array(
  2196. 'type' => 'datetime', 'timeFormat' => 12
  2197. ));
  2198. $result = explode('</select><select', $result);
  2199. $result = explode(':', $result[1]);
  2200. $this->assertRegExp('/option value="12"/', $result[0]);
  2201. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2202. $this->Form->request->data = array('Contact' => array('created' => null));
  2203. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input date'),
  2206. 'label' => array('for' => 'ContactCreatedMonth'),
  2207. 'Created',
  2208. '/label',
  2209. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2210. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2211. $monthsRegex,
  2212. '/select', '-',
  2213. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2214. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2215. $daysRegex,
  2216. '/select', '-',
  2217. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2218. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2219. $yearsRegex,
  2220. '/select',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $this->Form->request->data = array('Contact' => array('created' => null));
  2225. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2226. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2230. $result = $this->Form->input('Contact.created', array(
  2231. 'type' => 'date',
  2232. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2233. 'timeFormat' => 'NONE'
  2234. ));
  2235. $this->assertRegExp('/for\="created-month"/', $result);
  2236. }
  2237. /**
  2238. * Test generating checkboxes in a loop.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputCheckboxesInLoop() {
  2243. for ($i = 1; $i < 5; $i++) {
  2244. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input checkbox'),
  2247. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2248. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2249. 'label' => array('for' => "Contact{$i}Email"),
  2250. 'Email',
  2251. '/label',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. }
  2257. /**
  2258. * Test generating checkboxes with disabled elements.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputCheckboxWithDisabledElements() {
  2263. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2264. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2265. $expected = array(
  2266. array('div' => array('class' => 'input select')),
  2267. array('label' => array('for' => "ContactMultiple")),
  2268. 'Multiple',
  2269. '/label',
  2270. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2273. array('label' => array('for' => "ContactMultiple1")),
  2274. 'One',
  2275. '/label',
  2276. '/div',
  2277. array('div' => array('class' => 'checkbox')),
  2278. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2279. array('label' => array('for' => "ContactMultiple2")),
  2280. 'Two',
  2281. '/label',
  2282. '/div',
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2285. array('label' => array('for' => "ContactMultiple3")),
  2286. 'Three',
  2287. '/label',
  2288. '/div',
  2289. '/div'
  2290. );
  2291. $this->assertTags($result, $expected);
  2292. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2293. $this->assertTags($result, $expected);
  2294. $disabled = array('2', 3);
  2295. $expected = array(
  2296. array('div' => array('class' => 'input select')),
  2297. array('label' => array('for' => "ContactMultiple")),
  2298. 'Multiple',
  2299. '/label',
  2300. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2301. array('div' => array('class' => 'checkbox')),
  2302. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2303. array('label' => array('for' => "ContactMultiple1")),
  2304. 'One',
  2305. '/label',
  2306. '/div',
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2309. array('label' => array('for' => "ContactMultiple2")),
  2310. 'Two',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2315. array('label' => array('for' => "ContactMultiple3")),
  2316. 'Three',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2322. $this->assertTags($result, $expected);
  2323. // make sure 50 does only disable 50, and not 50f5c0cf
  2324. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2325. $disabled = array(50);
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => "ContactMultiple")),
  2329. 'Multiple',
  2330. '/label',
  2331. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2334. array('label' => array('for' => "ContactMultiple50")),
  2335. 'Fifty',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2340. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2341. 'Stringy',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2347. $this->assertTags($result, $expected);
  2348. }
  2349. /**
  2350. * test input name with leading integer, ensure attributes are generated correctly.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testInputWithLeadingInteger() {
  2355. $result = $this->Form->text('0.Node.title');
  2356. $expected = array(
  2357. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test form->input() with select type inputs.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputSelectType() {
  2367. $result = $this->Form->input('email', array(
  2368. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2369. );
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'email'),
  2373. 'Email',
  2374. '/label',
  2375. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2376. array('option' => array('value' => '')),
  2377. '/option',
  2378. array('option' => array('value' => 'è')),
  2379. 'Firést',
  2380. '/option',
  2381. array('option' => array('value' => 'é')),
  2382. 'Secoènd',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $result = $this->Form->input('email', array(
  2389. 'options' => array('First', 'Second'), 'empty' => true)
  2390. );
  2391. $expected = array(
  2392. 'div' => array('class' => 'input select'),
  2393. 'label' => array('for' => 'email'),
  2394. 'Email',
  2395. '/label',
  2396. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2397. array('option' => array('value' => '')),
  2398. '/option',
  2399. array('option' => array('value' => '0')),
  2400. 'First',
  2401. '/option',
  2402. array('option' => array('value' => '1')),
  2403. 'Second',
  2404. '/option',
  2405. '/select',
  2406. '/div'
  2407. );
  2408. $this->assertTags($result, $expected);
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2411. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2412. $expected = array(
  2413. 'div' => array('class' => 'input select'),
  2414. 'label' => array('for' => 'ModelUserId'),
  2415. 'User',
  2416. '/label',
  2417. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2418. array('option' => array('value' => '')),
  2419. '/option',
  2420. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2421. 'good',
  2422. '/option',
  2423. array('option' => array('value' => 'other')),
  2424. 'bad',
  2425. '/option',
  2426. '/select',
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2431. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2432. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2433. $expected = array(
  2434. 'div' => array('class' => 'input select'),
  2435. 'label' => array('for' => 'ThingUserId'),
  2436. 'User',
  2437. '/label',
  2438. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2439. array('option' => array('value' => '')),
  2440. 'Some Empty',
  2441. '/option',
  2442. array('option' => array('value' => 'value')),
  2443. 'good',
  2444. '/option',
  2445. array('option' => array('value' => 'other')),
  2446. 'bad',
  2447. '/option',
  2448. '/select',
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2453. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2454. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2455. $expected = array(
  2456. 'div' => array('class' => 'input select'),
  2457. 'label' => array('for' => 'ThingUserId'),
  2458. 'User',
  2459. '/label',
  2460. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2461. array('option' => array('value' => '')),
  2462. 'Some Empty',
  2463. '/option',
  2464. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2465. 'good',
  2466. '/option',
  2467. array('option' => array('value' => 'other')),
  2468. 'bad',
  2469. '/option',
  2470. '/select',
  2471. '/div'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2475. $this->Form->request->data = array('User' => array('User' => array('value')));
  2476. $result = $this->Form->input('User.User', array('empty' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input select'),
  2479. 'label' => array('for' => 'UserUser'),
  2480. 'User',
  2481. '/label',
  2482. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2483. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2484. array('option' => array('value' => '')),
  2485. '/option',
  2486. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2487. 'good',
  2488. '/option',
  2489. array('option' => array('value' => 'other')),
  2490. 'bad',
  2491. '/option',
  2492. '/select',
  2493. '/div'
  2494. );
  2495. $this->assertTags($result, $expected);
  2496. $this->Form->data = array();
  2497. $result = $this->Form->input('Publisher.id', array(
  2498. 'label' => 'Publisher',
  2499. 'type' => 'select',
  2500. 'multiple' => 'checkbox',
  2501. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2502. ));
  2503. $expected = array(
  2504. array('div' => array('class' => 'input select')),
  2505. array('label' => array('for' => 'PublisherId')),
  2506. 'Publisher',
  2507. '/label',
  2508. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2509. array('div' => array('class' => 'checkbox')),
  2510. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2511. array('label' => array('for' => 'PublisherIdValue1')),
  2512. 'Label 1',
  2513. '/label',
  2514. '/div',
  2515. array('div' => array('class' => 'checkbox')),
  2516. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2517. array('label' => array('for' => 'PublisherIdValue2')),
  2518. 'Label 2',
  2519. '/label',
  2520. '/div',
  2521. '/div'
  2522. );
  2523. $this->assertTags($result, $expected);
  2524. }
  2525. /**
  2526. * test that input() and a non standard primary key makes a hidden input by default.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2531. $this->Form->create('User');
  2532. $this->Form->fieldset = array(
  2533. 'User' => array(
  2534. 'fields' => array(
  2535. 'model_id' => array('type' => 'integer')
  2536. ),
  2537. 'validates' => array(),
  2538. 'key' => 'model_id'
  2539. )
  2540. );
  2541. $result = $this->Form->input('model_id');
  2542. $expected = array(
  2543. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. }
  2547. /**
  2548. * test that overriding the magic select type widget is possible
  2549. *
  2550. * @return void
  2551. */
  2552. public function testInputOverridingMagicSelectType() {
  2553. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2554. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2555. $expected = array(
  2556. 'div' => array('class' => 'input text'),
  2557. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2558. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2559. '/div'
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. //Check that magic types still work for plural/singular vars
  2563. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2564. $result = $this->Form->input('Model.type');
  2565. $expected = array(
  2566. 'div' => array('class' => 'input select'),
  2567. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2568. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2569. array('option' => array('value' => 'value')), 'good', '/option',
  2570. array('option' => array('value' => 'other')), 'bad', '/option',
  2571. '/select',
  2572. '/div'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. }
  2576. /**
  2577. * Test that magic input() selects are created for type=number
  2578. *
  2579. * @return void
  2580. */
  2581. public function testInputMagicSelectForTypeNumber() {
  2582. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2583. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2584. $result = $this->Form->input('ValidateUser.balance');
  2585. $expected = array(
  2586. 'div' => array('class' => 'input select'),
  2587. 'label' => array('for' => 'ValidateUserBalance'),
  2588. 'Balance',
  2589. '/label',
  2590. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2591. array('option' => array('value' => '0')),
  2592. 'nothing',
  2593. '/option',
  2594. array('option' => array('value' => '1', 'selected' => 'selected')),
  2595. 'some',
  2596. '/option',
  2597. array('option' => array('value' => '100')),
  2598. 'a lot',
  2599. '/option',
  2600. '/select',
  2601. '/div'
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. }
  2605. /**
  2606. * Test that magic input() selects can easily be converted into radio types without error.
  2607. *
  2608. * @return void
  2609. */
  2610. public function testInputMagicSelectChangeToRadio() {
  2611. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2612. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2613. $this->assertRegExp('/input type="radio"/', $result);
  2614. }
  2615. /**
  2616. * fields with the same name as the model should work.
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputWithMatchingFieldAndModelName() {
  2621. $this->Form->create('User');
  2622. $this->Form->fieldset = array(
  2623. 'User' => array(
  2624. 'fields' => array(
  2625. 'User' => array('type' => 'text')
  2626. ),
  2627. 'validates' => array(),
  2628. 'key' => 'id'
  2629. )
  2630. );
  2631. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2632. $result = $this->Form->input('User', array('type' => 'text'));
  2633. $expected = array(
  2634. 'div' => array('class' => 'input text'),
  2635. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2636. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2637. '/div'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputs() {
  2649. $this->Form->create('Contact');
  2650. $result = $this->Form->inputs('The Legend');
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2660. $expected = array(
  2661. 'fieldset' => array('class' => 'classy-stuff'),
  2662. '<legend',
  2663. 'Field of Dreams',
  2664. '/legend',
  2665. '*/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $this->Form->request['prefix'] = 'admin';
  2674. $this->Form->request['action'] = 'admin_edit';
  2675. $result = $this->Form->inputs();
  2676. $expected = array(
  2677. '<fieldset',
  2678. '<legend',
  2679. 'Edit Contact',
  2680. '/legend',
  2681. '*/fieldset',
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $result = $this->Form->inputs(false);
  2686. $expected = array(
  2687. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2688. array('div' => array('class' => 'input text')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input email')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input tel')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input password')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input datetime')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input number')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input select')),
  2705. '*/div',
  2706. );
  2707. $this->assertTags($result, $expected);
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2710. $expected = array(
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. );
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2737. $expected = array(
  2738. 'fieldset' => array(),
  2739. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input email')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input tel')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input password')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input datetime')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input number')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input select')),
  2757. '*/div',
  2758. '/fieldset'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $this->Form->create('Contact');
  2762. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2763. $expected = array(
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input email')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input tel')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. );
  2784. $this->assertTags($result, $expected);
  2785. $this->Form->create('Contact');
  2786. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs('Hello');
  2790. $expected = array(
  2791. 'fieldset' => array(),
  2792. 'legend' => array(),
  2793. 'Hello',
  2794. '/legend',
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. '/fieldset'
  2815. );
  2816. $this->assertTags($result, $expected);
  2817. $this->Form->create('Contact');
  2818. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2819. $expected = array(
  2820. 'fieldset' => array(),
  2821. 'legend' => array(),
  2822. 'Hello',
  2823. '/legend',
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. '/fieldset'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->end();
  2850. $this->Form->create(false);
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. array('div' => array('class' => 'input text')),
  2854. 'label' => array('for' => 'foo'),
  2855. 'Foo',
  2856. '/label',
  2857. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2858. '*/div',
  2859. '/fieldset'
  2860. );
  2861. $result = $this->Form->inputs(
  2862. array('foo' => array('type' => 'text')),
  2863. array(),
  2864. array('legend' => false)
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. }
  2868. /**
  2869. * Tests inputs() works with plugin models
  2870. *
  2871. * @return void
  2872. */
  2873. public function testInputsPluginModel() {
  2874. $this->loadFixtures('Post');
  2875. App::build(array(
  2876. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2877. ));
  2878. CakePlugin::load('TestPlugin');
  2879. $this->Form->request['models'] = array(
  2880. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2881. );
  2882. $this->Form->create('TestPlugin.TestPluginPost');
  2883. $result = $this->Form->inputs();
  2884. $this->assertContains('data[TestPluginPost][id]', $result);
  2885. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2886. $this->assertContains('data[TestPluginPost][title]', $result);
  2887. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2888. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2889. $this->assertEquals('TestPluginPost', $this->Form->model());
  2890. }
  2891. /**
  2892. * testSelectAsCheckbox method
  2893. *
  2894. * test multi-select widget with checkbox formatting.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSelectAsCheckbox() {
  2899. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2902. array('div' => array('class' => 'checkbox')),
  2903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2904. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2905. 'first',
  2906. '/label',
  2907. '/div',
  2908. array('div' => array('class' => 'checkbox')),
  2909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2910. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2911. 'second',
  2912. '/label',
  2913. '/div',
  2914. array('div' => array('class' => 'checkbox')),
  2915. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2916. array('label' => array('for' => 'ModelMultiField2')),
  2917. 'third',
  2918. '/label',
  2919. '/div',
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2923. $expected = array(
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2927. array('label' => array('for' => 'ModelMultiField12')),
  2928. 'half',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. }
  2934. /**
  2935. * testLabel method
  2936. *
  2937. * test label generation.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testLabel() {
  2942. $this->Form->text('Person.name');
  2943. $result = $this->Form->label();
  2944. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2958. $result = $this->Form->label('Person.2.name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2960. }
  2961. /**
  2962. * testTextbox method
  2963. *
  2964. * test textbox element generation
  2965. *
  2966. * @return void
  2967. */
  2968. public function testTextbox() {
  2969. $result = $this->Form->text('Model.field');
  2970. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2974. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2975. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2976. $result = $this->Form->text('Model.text');
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2978. $Contact = ClassRegistry::getObject('Contact');
  2979. $Contact->validationErrors['text'] = array(true);
  2980. $this->Form->request->data['Contact']['text'] = 'test';
  2981. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2984. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2985. $expected = array(
  2986. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. }
  2990. /**
  2991. * testDefaultValue method
  2992. *
  2993. * Test default value setting
  2994. *
  2995. * @return void
  2996. */
  2997. public function testDefaultValue() {
  2998. $this->Form->request->data['Model']['field'] = 'test';
  2999. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3000. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3001. unset($this->Form->request->data['Model']['field']);
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3004. }
  3005. /**
  3006. * testCheckboxDefaultValue method
  3007. *
  3008. * Test default value setting on checkbox() method
  3009. *
  3010. * @return void
  3011. */
  3012. public function testCheckboxDefaultValue() {
  3013. $this->Form->request->data['Model']['field'] = false;
  3014. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3015. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3016. unset($this->Form->request->data['Model']['field']);
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3019. $this->Form->request->data['Model']['field'] = true;
  3020. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. unset($this->Form->request->data['Model']['field']);
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3025. }
  3026. /**
  3027. * testError method
  3028. *
  3029. * Test field error generation
  3030. *
  3031. * @return void
  3032. */
  3033. public function testError() {
  3034. $Contact = ClassRegistry::getObject('Contact');
  3035. $Contact->validationErrors['field'] = array(1);
  3036. $result = $this->Form->error('Contact.field');
  3037. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3038. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3039. $this->assertEquals('Error in field Field', $result);
  3040. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('This field contains invalid input', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3046. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3051. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3052. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3053. $this->assertEquals('<strong>Badness!</strong>', $result);
  3054. $Contact->validationErrors['field'] = array("email");
  3055. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3056. $expected = array(
  3057. 'div' => array('class' => 'field-error'),
  3058. 'No good!',
  3059. '/div'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3063. $result = $this->Form->error('Contact.field', array(
  3064. 'notEmpty' => 'Cannot be empty',
  3065. 'email' => 'No good!'
  3066. ));
  3067. $expected = array(
  3068. 'div' => array('class' => 'error-message'),
  3069. 'ul' => array(),
  3070. '<li', 'Cannot be empty', '/li',
  3071. '<li', 'No good!', '/li',
  3072. '<li', 'Something else', '/li',
  3073. '/ul',
  3074. '/div'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. // Testing error messages list options
  3078. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3079. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3080. $expected = array(
  3081. 'div' => array('class' => 'error-message'),
  3082. 'ol' => array(),
  3083. '<li', 'notEmpty', '/li',
  3084. '<li', 'email', '/li',
  3085. '/ol',
  3086. '/div'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3090. $expected = array(
  3091. 'div' => array('class' => 'error-message'),
  3092. 'ol' => array(),
  3093. '<li', 'notEmpty', '/li',
  3094. '<li', 'email', '/li',
  3095. '/ol',
  3096. '/div'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $result = $this->Form->error('Contact.field', null, array(
  3100. 'listOptions' => array(
  3101. 'class' => 'ul-class',
  3102. 'itemOptions' => array(
  3103. 'class' => 'li-class'
  3104. )
  3105. )
  3106. ));
  3107. $expected = array(
  3108. 'div' => array('class' => 'error-message'),
  3109. 'ul' => array('class' => 'ul-class'),
  3110. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3111. array('li' => array('class' => 'li-class')), 'email', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. }
  3117. /**
  3118. * test error options when using form->input();
  3119. *
  3120. * @return void
  3121. */
  3122. public function testInputErrorEscape() {
  3123. $this->Form->create('ValidateProfile');
  3124. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3125. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3126. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3127. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3128. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3129. $this->assertRegExp('/required<br>/', $result);
  3130. }
  3131. /**
  3132. * testPassword method
  3133. *
  3134. * Test password element generation
  3135. *
  3136. * @return void
  3137. */
  3138. public function testPassword() {
  3139. $Contact = ClassRegistry::getObject('Contact');
  3140. $result = $this->Form->password('Contact.field');
  3141. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3142. $Contact->validationErrors['passwd'] = 1;
  3143. $this->Form->request->data['Contact']['passwd'] = 'test';
  3144. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3145. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3146. }
  3147. /**
  3148. * testRadio method
  3149. *
  3150. * Test radio element set generation
  3151. *
  3152. * @return void
  3153. */
  3154. public function testRadio() {
  3155. $result = $this->Form->radio('Model.field', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. 'label' => array('for' => 'ModelField0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3168. 'label' => array('for' => 'ModelField12'),
  3169. 'half',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3199. array('label' => array('for' => 'ModelField0')),
  3200. 'option A',
  3201. '/label',
  3202. 'br' => array(),
  3203. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3204. array('label' => array('for' => 'ModelField1')),
  3205. 'option B',
  3206. '/label',
  3207. '/fieldset'
  3208. );
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Gender',
  3215. '/legend',
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3218. array('label' => array('for' => 'EmployeeGenderMale')),
  3219. 'Male',
  3220. '/label',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3222. array('label' => array('for' => 'EmployeeGenderFemale')),
  3223. 'Female',
  3224. '/label',
  3225. '/fieldset',
  3226. );
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Gender',
  3233. '/legend',
  3234. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3236. array('label' => array('for' => 'OfficerGenderMale')),
  3237. 'Male',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3240. array('label' => array('for' => 'OfficerGenderFemale')),
  3241. 'Female',
  3242. '/label',
  3243. '/fieldset',
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3247. $expected = array(
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3249. array('input' => array(
  3250. 'type' => 'radio',
  3251. 'name' => 'data[Contact][1][imrequired]',
  3252. 'value' => '0',
  3253. 'id' => 'Contact1Imrequired0',
  3254. 'required' => 'required'
  3255. )),
  3256. 'label' => array('for' => 'Contact1Imrequired0'),
  3257. 'option A',
  3258. '/label'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Model.1.field', array('option A'));
  3262. $expected = array(
  3263. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3271. $expected = array(
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'option A',
  3280. '/label',
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3282. array('label' => array('for' => 'ModelField1')),
  3283. 'option B',
  3284. '/label',
  3285. '/fieldset'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. }
  3289. /**
  3290. * Test radio inputs with between as string or array. Also ensure
  3291. * that an array with less between elements works.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioBetween() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('between' => 'I am between')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. 'I am between',
  3307. 'input' => array(
  3308. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3309. 'value' => '', 'id' => 'ModelField_'
  3310. ),
  3311. array('input' => array(
  3312. 'type' => 'radio', 'name' => 'data[Model][field]',
  3313. 'value' => '0', 'id' => 'ModelField0'
  3314. )),
  3315. array('label' => array('for' => 'ModelField0')),
  3316. 'option A',
  3317. '/label',
  3318. array('input' => array(
  3319. 'type' => 'radio', 'name' => 'data[Model][field]',
  3320. 'value' => '1', 'id' => 'ModelField1'
  3321. )),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B', 'option C'),
  3331. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. 'input' => array(
  3339. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3340. 'value' => '', 'id' => 'ModelField_'
  3341. ),
  3342. array('input' => array(
  3343. 'type' => 'radio', 'name' => 'data[Model][field]',
  3344. 'value' => '0', 'id' => 'ModelField0'
  3345. )),
  3346. array('label' => array('for' => 'ModelField0')),
  3347. 'option A',
  3348. '/label',
  3349. 'between A',
  3350. '--separator--',
  3351. array('input' => array(
  3352. 'type' => 'radio', 'name' => 'data[Model][field]',
  3353. 'value' => '1', 'id' => 'ModelField1'
  3354. )),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. 'between B',
  3359. '--separator--',
  3360. array('input' => array(
  3361. 'type' => 'radio', 'name' => 'data[Model][field]',
  3362. 'value' => '2', 'id' => 'ModelField2'
  3363. )),
  3364. array('label' => array('for' => 'ModelField2')),
  3365. 'option C',
  3366. '/label',
  3367. 'between C',
  3368. '/fieldset'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->input('Model.field', array(
  3372. 'options' => array('1' => 'first', '2' => 'second'),
  3373. 'type' => 'radio',
  3374. 'before' => '--before--',
  3375. 'after' => '--after--',
  3376. 'separator' => '--separator--',
  3377. 'between' => array('--between first--', '--between second--')
  3378. ));
  3379. $expected = array(
  3380. 'div' => array('class' => 'input radio'),
  3381. '--before--',
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3387. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3388. array('label' => array('for' => 'ModelField1')),
  3389. 'first',
  3390. '/label',
  3391. '--between first--',
  3392. '--separator--',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3394. array('label' => array('for' => 'ModelField2')),
  3395. 'second',
  3396. '/label',
  3397. '--between second--',
  3398. '/fieldset',
  3399. '--after--',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->input('Model.field', array(
  3404. 'options' => array('1' => 'first', '2' => 'second'),
  3405. 'type' => 'radio',
  3406. 'before' => '--before--',
  3407. 'after' => '--after--',
  3408. 'separator' => '--separator--',
  3409. 'between' => array('--between first--')
  3410. ));
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. '--before--',
  3414. 'fieldset' => array(),
  3415. 'legend' => array(),
  3416. 'Field',
  3417. '/legend',
  3418. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3420. array('label' => array('for' => 'ModelField1')),
  3421. 'first',
  3422. '/label',
  3423. '--between first--',
  3424. '--separator--',
  3425. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3426. array('label' => array('for' => 'ModelField2')),
  3427. 'second',
  3428. '/label',
  3429. '/fieldset',
  3430. '--after--',
  3431. '/div'
  3432. );
  3433. $this->assertTags($result, $expected);
  3434. }
  3435. /**
  3436. * Test that radios with a 0 value are selected under the correct conditions.
  3437. * Also ensure that values that are booleanish are handled correctly.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testRadioOptionWithBooleanishValues() {
  3442. $expected = array(
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'Yes',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3452. array('label' => array('for' => 'ModelField0')),
  3453. 'No',
  3454. '/label',
  3455. '/fieldset'
  3456. );
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3462. $this->assertTags($result, $expected);
  3463. $expected = array(
  3464. 'fieldset' => array(),
  3465. 'legend' => array(),
  3466. 'Field',
  3467. '/legend',
  3468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3469. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3470. array('label' => array('for' => 'ModelField1')),
  3471. 'Yes',
  3472. '/label',
  3473. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3474. array('label' => array('for' => 'ModelField0')),
  3475. 'No',
  3476. '/label',
  3477. '/fieldset'
  3478. );
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3484. $this->assertTags($result, $expected);
  3485. $expected = array(
  3486. 'fieldset' => array(),
  3487. 'legend' => array(),
  3488. 'Field',
  3489. '/legend',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3491. array('label' => array('for' => 'ModelField1')),
  3492. 'Yes',
  3493. '/label',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3495. array('label' => array('for' => 'ModelField0')),
  3496. 'No',
  3497. '/label',
  3498. '/fieldset'
  3499. );
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3503. $this->assertTags($result, $expected);
  3504. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3505. $this->assertTags($result, $expected);
  3506. }
  3507. /**
  3508. * test disabled radio options
  3509. *
  3510. * @return void
  3511. */
  3512. public function testRadioDisabled() {
  3513. $result = $this->Form->radio(
  3514. 'Model.field',
  3515. array('option A', 'option B'),
  3516. array('disabled' => array(0), 'value' => '0')
  3517. );
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3524. array('label' => array('for' => 'ModelField0')),
  3525. 'option A',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3528. array('label' => array('for' => 'ModelField1')),
  3529. 'option B',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->radio(
  3535. 'Model.field',
  3536. array('option A', 'option B'),
  3537. array('disabled' => true, 'value' => '0')
  3538. );
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3545. array('label' => array('for' => 'ModelField0')),
  3546. 'option A',
  3547. '/label',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'option B',
  3551. '/label',
  3552. '/fieldset'
  3553. );
  3554. $this->assertTags($result, $expected);
  3555. $result = $this->Form->radio(
  3556. 'Model.field',
  3557. array('option A', 'option B'),
  3558. array('disabled' => 'disabled', 'value' => '0')
  3559. );
  3560. $expected = array(
  3561. 'fieldset' => array(),
  3562. 'legend' => array(),
  3563. 'Field',
  3564. '/legend',
  3565. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3566. array('label' => array('for' => 'ModelField0')),
  3567. 'option A',
  3568. '/label',
  3569. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3570. array('label' => array('for' => 'ModelField1')),
  3571. 'option B',
  3572. '/label',
  3573. '/fieldset'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->input('Model.field', array(
  3577. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3578. 'type' => 'radio',
  3579. 'disabled' => array(2, '3x'),
  3580. ));
  3581. $expected = array(
  3582. 'div' => array('class' => 'input radio'),
  3583. 'fieldset' => array(),
  3584. 'legend' => array(),
  3585. 'Field',
  3586. '/legend',
  3587. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3588. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3589. array('label' => array('for' => 'ModelField1')),
  3590. 'first',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3593. array('label' => array('for' => 'ModelField2')),
  3594. 'second',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3597. array('label' => array('for' => 'ModelField2x')),
  3598. '2x',
  3599. '/label',
  3600. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3601. array('label' => array('for' => 'ModelField3')),
  3602. 'third',
  3603. '/label',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3605. array('label' => array('for' => 'ModelField3x')),
  3606. '3x',
  3607. '/label',
  3608. '/fieldset',
  3609. '/div'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. }
  3613. /**
  3614. * test disabling the hidden input for radio buttons
  3615. *
  3616. * @return void
  3617. */
  3618. public function testRadioHiddenInputDisabling() {
  3619. $result = $this->Form->input('Model.1.field', array(
  3620. 'type' => 'radio',
  3621. 'options' => array('option A'),
  3622. 'hiddenField' => false
  3623. )
  3624. );
  3625. $expected = array(
  3626. 'div' => array('class' => 'input radio'),
  3627. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3628. 'label' => array('for' => 'Model1Field0'),
  3629. 'option A',
  3630. '/label',
  3631. '/div'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3635. $expected = array(
  3636. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3637. 'label' => array('for' => 'Model1Field0'),
  3638. 'option A',
  3639. '/label'
  3640. );
  3641. $this->assertTags($result, $expected);
  3642. }
  3643. /**
  3644. * test adding an empty option for radio buttons
  3645. *
  3646. * @return void
  3647. */
  3648. public function testRadioAddEmptyOption() {
  3649. $result = $this->Form->input('Model.1.field', array(
  3650. 'type' => 'radio',
  3651. 'options' => array('option A'),
  3652. 'empty' => true,
  3653. 'hiddenField' => false
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'input radio'),
  3657. 'fieldset' => array(),
  3658. 'legend' => array(),
  3659. 'Field',
  3660. '/legend',
  3661. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3662. array('label' => array('for' => 'Model1Field')),
  3663. __('empty'),
  3664. '/label',
  3665. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3666. array('label' => array('for' => 'Model1Field0')),
  3667. 'option A',
  3668. '/label',
  3669. '/fieldset',
  3670. '/div'
  3671. );
  3672. $this->assertTags($result, $expected);
  3673. $result = $this->Form->input('Model.1.field', array(
  3674. 'type' => 'radio',
  3675. 'options' => array('option A'),
  3676. 'empty' => 'CustomEmptyLabel',
  3677. 'hiddenField' => false
  3678. ));
  3679. $expected = array(
  3680. 'div' => array('class' => 'input radio'),
  3681. 'fieldset' => array(),
  3682. 'legend' => array(),
  3683. 'Field',
  3684. '/legend',
  3685. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3686. array('label' => array('for' => 'Model1Field')),
  3687. 'CustomEmptyLabel',
  3688. '/label',
  3689. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3690. array('label' => array('for' => 'Model1Field0')),
  3691. 'option A',
  3692. '/label',
  3693. '/fieldset',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. $result = $this->Form->input('Model.1.field', array(
  3698. 'type' => 'radio',
  3699. 'options' => array('option A'),
  3700. 'empty' => false,
  3701. 'hiddenField' => false
  3702. ));
  3703. $this->assertTextNotContains('"Model1Field"', $result);
  3704. }
  3705. /**
  3706. * testSelect method
  3707. *
  3708. * Test select element generation.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testSelect() {
  3713. $result = $this->Form->select('Model.field', array());
  3714. $expected = array(
  3715. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3716. array('option' => array('value' => '')),
  3717. '/option',
  3718. '/select'
  3719. );
  3720. $this->assertTags($result, $expected);
  3721. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3722. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3723. $expected = array(
  3724. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3725. array('option' => array('value' => '')),
  3726. '/option',
  3727. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3728. 'good',
  3729. '/option',
  3730. array('option' => array('value' => 'other')),
  3731. 'bad',
  3732. '/option',
  3733. '/select'
  3734. );
  3735. $this->assertTags($result, $expected);
  3736. $this->Form->request->data = array();
  3737. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3740. array('option' => array('value' => '')),
  3741. '/option',
  3742. array('option' => array('value' => 'value')),
  3743. 'good',
  3744. '/option',
  3745. array('option' => array('value' => 'other')),
  3746. 'bad',
  3747. '/option',
  3748. '/select'
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->select(
  3752. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3753. array('empty' => false)
  3754. );
  3755. $expected = array(
  3756. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3757. array('option' => array('value' => 'first')),
  3758. 'first &quot;html&quot; &lt;chars&gt;',
  3759. '/option',
  3760. array('option' => array('value' => 'second')),
  3761. 'value',
  3762. '/option',
  3763. '/select'
  3764. );
  3765. $this->assertTags($result, $expected);
  3766. $result = $this->Form->select(
  3767. 'Model.field',
  3768. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3769. array('escape' => false, 'empty' => false)
  3770. );
  3771. $expected = array(
  3772. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3773. array('option' => array('value' => 'first')),
  3774. 'first "html" <chars>',
  3775. '/option',
  3776. array('option' => array('value' => 'second')),
  3777. 'value',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. $options = array(
  3783. array('value' => 'first', 'name' => 'First'),
  3784. array('value' => 'first', 'name' => 'Another First'),
  3785. );
  3786. $result = $this->Form->select(
  3787. 'Model.field',
  3788. $options,
  3789. array('escape' => false, 'empty' => false)
  3790. );
  3791. $expected = array(
  3792. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3793. array('option' => array('value' => 'first')),
  3794. 'First',
  3795. '/option',
  3796. array('option' => array('value' => 'first')),
  3797. 'Another First',
  3798. '/option',
  3799. '/select'
  3800. );
  3801. $this->assertTags($result, $expected);
  3802. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3803. $result = $this->Form->select(
  3804. 'Model.contact_id',
  3805. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3806. array('escape' => false, 'empty' => 'pick something')
  3807. );
  3808. $expected = array(
  3809. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3810. array('option' => array('value' => '')), 'pick something', '/option',
  3811. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3812. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3813. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3814. '/select'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $this->Form->request->data['Model']['field'] = 0;
  3818. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3819. $expected = array(
  3820. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3821. array('option' => array('value' => '')), '/option',
  3822. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3823. array('option' => array('value' => '1')), 'Yes', '/option',
  3824. '/select'
  3825. );
  3826. $this->assertTags($result, $expected);
  3827. $this->Form->request->data['Model']['field'] = 50;
  3828. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3829. $expected = array(
  3830. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3831. array('option' => array('value' => '')), '/option',
  3832. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3833. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3834. '/select'
  3835. );
  3836. $this->assertTags($result, $expected);
  3837. $result = $this->Form->select('Contact.required_one', array('option A'));
  3838. $expected = array(
  3839. 'select' => array(
  3840. 'name' => 'data[Contact][required_one]',
  3841. 'id' => 'ContactRequiredOne',
  3842. 'required' => 'required'
  3843. ),
  3844. array('option' => array('value' => '')), '/option',
  3845. array('option' => array('value' => '0')), 'option A', '/option',
  3846. '/select'
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3850. $expected = array(
  3851. 'select' => array(
  3852. 'name' => 'data[Contact][required_one]',
  3853. 'id' => 'ContactRequiredOne',
  3854. 'disabled' => 'disabled'
  3855. ),
  3856. array('option' => array('value' => '')), '/option',
  3857. array('option' => array('value' => '0')), 'option A', '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. }
  3862. /**
  3863. * test that select() with optiongroups listens to the escape param.
  3864. *
  3865. * @return void
  3866. */
  3867. public function testSelectOptionGroupEscaping() {
  3868. $options = array(
  3869. '>< Key' => array(
  3870. 1 => 'One',
  3871. 2 => 'Two'
  3872. )
  3873. );
  3874. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3875. $expected = array(
  3876. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3877. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3878. array('option' => array('value' => '1')), 'One', '/option',
  3879. array('option' => array('value' => '2')), 'Two', '/option',
  3880. '/optgroup',
  3881. '/select'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. $options = array(
  3885. '>< Key' => array(
  3886. 1 => 'One',
  3887. 2 => 'Two'
  3888. )
  3889. );
  3890. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3891. $expected = array(
  3892. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3893. 'optgroup' => array('label' => '>< Key'),
  3894. array('option' => array('value' => '1')), 'One', '/option',
  3895. array('option' => array('value' => '2')), 'Two', '/option',
  3896. '/optgroup',
  3897. '/select'
  3898. );
  3899. $this->assertTags($result, $expected);
  3900. }
  3901. /**
  3902. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3903. *
  3904. * @return void
  3905. */
  3906. public function testSelectWithNullAttributes() {
  3907. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3908. $expected = array(
  3909. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3910. array('option' => array('value' => '0')),
  3911. 'first',
  3912. '/option',
  3913. array('option' => array('value' => '1')),
  3914. 'second',
  3915. '/option',
  3916. '/select'
  3917. );
  3918. $this->assertTags($result, $expected);
  3919. }
  3920. /**
  3921. * testNestedSelect method
  3922. *
  3923. * test select element generation with optgroups
  3924. *
  3925. * @return void
  3926. */
  3927. public function testNestedSelect() {
  3928. $result = $this->Form->select(
  3929. 'Model.field',
  3930. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3931. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3932. )), array('empty' => false)
  3933. );
  3934. $expected = array(
  3935. 'select' => array('name' => 'data[Model][field]',
  3936. 'id' => 'ModelField'),
  3937. array('option' => array('value' => 1)),
  3938. 'One',
  3939. '/option',
  3940. array('option' => array('value' => 2)),
  3941. 'Two',
  3942. '/option',
  3943. array('optgroup' => array('label' => 'Three')),
  3944. array('option' => array('value' => 4)),
  3945. 'Four',
  3946. '/option',
  3947. array('option' => array('value' => 5)),
  3948. 'Five',
  3949. '/option',
  3950. '/optgroup',
  3951. '/select'
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. $result = $this->Form->select(
  3955. 'Model.field',
  3956. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3957. array('showParents' => true, 'empty' => false)
  3958. );
  3959. $expected = array(
  3960. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3961. array('option' => array('value' => 1)),
  3962. 'One',
  3963. '/option',
  3964. array('option' => array('value' => 2)),
  3965. 'Two',
  3966. '/option',
  3967. array('optgroup' => array('label' => 'Three')),
  3968. array('option' => array('value' => 3)),
  3969. 'Three',
  3970. '/option',
  3971. array('option' => array('value' => 4)),
  3972. 'Four',
  3973. '/option',
  3974. '/optgroup',
  3975. '/select'
  3976. );
  3977. $this->assertTags($result, $expected);
  3978. }
  3979. /**
  3980. * testSelectMultiple method
  3981. *
  3982. * test generation of multiple select elements
  3983. *
  3984. * @return void
  3985. */
  3986. public function testSelectMultiple() {
  3987. $options = array('first', 'second', 'third');
  3988. $result = $this->Form->select(
  3989. 'Model.multi_field', $options, array('multiple' => true)
  3990. );
  3991. $expected = array(
  3992. 'input' => array(
  3993. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3994. ),
  3995. 'select' => array(
  3996. 'name' => 'data[Model][multi_field][]',
  3997. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3998. ),
  3999. array('option' => array('value' => '0')),
  4000. 'first',
  4001. '/option',
  4002. array('option' => array('value' => '1')),
  4003. 'second',
  4004. '/option',
  4005. array('option' => array('value' => '2')),
  4006. 'third',
  4007. '/option',
  4008. '/select'
  4009. );
  4010. $this->assertTags($result, $expected);
  4011. $result = $this->Form->select(
  4012. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4013. );
  4014. $expected = array(
  4015. 'input' => array(
  4016. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4017. ),
  4018. 'select' => array(
  4019. 'name' => 'data[Model][multi_field][]',
  4020. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4021. ),
  4022. array('option' => array('value' => '0')),
  4023. 'first',
  4024. '/option',
  4025. array('option' => array('value' => '1')),
  4026. 'second',
  4027. '/option',
  4028. array('option' => array('value' => '2')),
  4029. 'third',
  4030. '/option',
  4031. '/select'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. $result = $this->Form->select(
  4035. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4036. );
  4037. $expected = array(
  4038. 'input' => array(
  4039. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4040. ),
  4041. 'select' => array(
  4042. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4043. 'multiple' => 'multiple'
  4044. ),
  4045. array('option' => array('value' => '0', 'selected' => 'selected')),
  4046. 'first',
  4047. '/option',
  4048. array('option' => array('value' => '1', 'selected' => 'selected')),
  4049. 'second',
  4050. '/option',
  4051. array('option' => array('value' => '2')),
  4052. 'third',
  4053. '/option',
  4054. '/select'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->select(
  4058. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4059. );
  4060. $expected = array(
  4061. 'select' => array(
  4062. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4063. ),
  4064. array('option' => array('value' => '0', 'selected' => 'selected')),
  4065. 'first',
  4066. '/option',
  4067. array('option' => array('value' => '1', 'selected' => 'selected')),
  4068. 'second',
  4069. '/option',
  4070. array('option' => array('value' => '2')),
  4071. 'third',
  4072. '/option',
  4073. '/select'
  4074. );
  4075. $this->assertTags($result, $expected);
  4076. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4077. $selected = array('2', '3x');
  4078. $result = $this->Form->select(
  4079. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4080. );
  4081. $expected = array(
  4082. 'input' => array(
  4083. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4084. ),
  4085. 'select' => array(
  4086. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4087. ),
  4088. array('option' => array('value' => '1')),
  4089. 'One',
  4090. '/option',
  4091. array('option' => array('value' => '2', 'selected' => 'selected')),
  4092. 'Two',
  4093. '/option',
  4094. array('option' => array('value' => '3')),
  4095. 'Three',
  4096. '/option',
  4097. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4098. 'Stringy',
  4099. '/option',
  4100. '/select'
  4101. );
  4102. $this->assertTags($result, $expected);
  4103. $result = $this->Form->select('Contact.required_one', array(
  4104. '1' => 'option A',
  4105. '2' => 'option B'
  4106. ), array('multiple' => true));
  4107. $expected = array(
  4108. 'input' => array(
  4109. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4110. ),
  4111. 'select' => array(
  4112. 'name' => 'data[Contact][required_one][]',
  4113. 'id' => 'ContactRequiredOne',
  4114. 'required' => 'required',
  4115. 'multiple' => 'multiple'
  4116. ),
  4117. array('option' => array('value' => '1')), 'option A', '/option',
  4118. array('option' => array('value' => '2')), 'option B', '/option',
  4119. '/select'
  4120. );
  4121. $this->assertTags($result, $expected);
  4122. }
  4123. /**
  4124. * Test generating multiple select with disabled elements.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testSelectMultipleWithDisabledElements() {
  4129. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4130. $disabled = array(2, 3);
  4131. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4132. $expected = array(
  4133. 'input' => array(
  4134. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4135. ),
  4136. 'select' => array(
  4137. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4138. ),
  4139. array('option' => array('value' => '1')),
  4140. 'One',
  4141. '/option',
  4142. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4143. 'Two',
  4144. '/option',
  4145. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4146. 'Three',
  4147. '/option',
  4148. array('option' => array('value' => '3x')),
  4149. 'Stringy',
  4150. '/option',
  4151. '/select'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4155. $disabled = array('2', '3x');
  4156. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4157. $expected = array(
  4158. array('div' => array('class' => 'input select')),
  4159. array('label' => array('for' => 'ContactMultiple')),
  4160. 'Multiple',
  4161. '/label',
  4162. 'input' => array(
  4163. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4164. ),
  4165. 'select' => array(
  4166. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4167. ),
  4168. array('option' => array('value' => '1')),
  4169. 'One',
  4170. '/option',
  4171. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4172. 'Two',
  4173. '/option',
  4174. array('option' => array('value' => '3')),
  4175. 'Three',
  4176. '/option',
  4177. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4178. 'Stringy',
  4179. '/option',
  4180. '/select',
  4181. '/div'
  4182. );
  4183. $this->assertTags($result, $expected);
  4184. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4185. $disabled = true;
  4186. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4187. $expected = array(
  4188. array('div' => array('class' => 'input select')),
  4189. array('label' => array('for' => 'ContactMultiple')),
  4190. 'Multiple',
  4191. '/label',
  4192. 'input' => array(
  4193. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4194. ),
  4195. 'select' => array(
  4196. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4197. ),
  4198. array('option' => array('value' => '1')),
  4199. 'One',
  4200. '/option',
  4201. array('option' => array('value' => '2')),
  4202. 'Two',
  4203. '/option',
  4204. array('option' => array('value' => '3')),
  4205. 'Three',
  4206. '/option',
  4207. array('option' => array('value' => '3x')),
  4208. 'Stringy',
  4209. '/option',
  4210. '/select',
  4211. '/div'
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. }
  4215. /**
  4216. * Test generating select with disabled elements.
  4217. *
  4218. * @return void
  4219. */
  4220. public function testSelectWithDisabledElements() {
  4221. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4222. $disabled = array(2, 3);
  4223. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4224. $expected = array(
  4225. 'select' => array(
  4226. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4227. ),
  4228. array('option' => array('value' => '')),
  4229. '/option',
  4230. array('option' => array('value' => '1')),
  4231. 'One',
  4232. '/option',
  4233. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4234. 'Two',
  4235. '/option',
  4236. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4237. 'Three',
  4238. '/option',
  4239. array('option' => array('value' => '3x')),
  4240. 'Stringy',
  4241. '/option',
  4242. '/select'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4246. $disabled = array('2', '3x');
  4247. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4248. $expected = array(
  4249. array('div' => array('class' => 'input select')),
  4250. array('label' => array('for' => 'ModelField')),
  4251. 'Field',
  4252. '/label',
  4253. 'select' => array(
  4254. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4255. ),
  4256. array('option' => array('value' => '1')),
  4257. 'One',
  4258. '/option',
  4259. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4260. 'Two',
  4261. '/option',
  4262. array('option' => array('value' => '3')),
  4263. 'Three',
  4264. '/option',
  4265. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4266. 'Stringy',
  4267. '/option',
  4268. '/select',
  4269. '/div'
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4273. $disabled = true;
  4274. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4275. $expected = array(
  4276. array('div' => array('class' => 'input select')),
  4277. array('label' => array('for' => 'ModelField')),
  4278. 'Field',
  4279. '/label',
  4280. 'select' => array(
  4281. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4282. ),
  4283. array('option' => array('value' => '1')),
  4284. 'One',
  4285. '/option',
  4286. array('option' => array('value' => '2')),
  4287. 'Two',
  4288. '/option',
  4289. array('option' => array('value' => '3')),
  4290. 'Three',
  4291. '/option',
  4292. array('option' => array('value' => '3x')),
  4293. 'Stringy',
  4294. '/option',
  4295. '/select',
  4296. '/div'
  4297. );
  4298. $this->assertTags($result, $expected);
  4299. }
  4300. /**
  4301. * test generation of habtm select boxes.
  4302. *
  4303. * @return void
  4304. */
  4305. public function testHabtmSelectBox() {
  4306. $this->View->viewVars['contactTags'] = array(
  4307. 1 => 'blue',
  4308. 2 => 'red',
  4309. 3 => 'green'
  4310. );
  4311. $this->Form->request->data = array(
  4312. 'Contact' => array(),
  4313. 'ContactTag' => array(
  4314. array(
  4315. 'id' => '1',
  4316. 'name' => 'blue'
  4317. ),
  4318. array(
  4319. 'id' => 3,
  4320. 'name' => 'green'
  4321. )
  4322. )
  4323. );
  4324. $this->Form->create('Contact');
  4325. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4326. $expected = array(
  4327. 'input' => array(
  4328. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4329. ),
  4330. 'select' => array(
  4331. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4332. 'multiple' => 'multiple'
  4333. ),
  4334. array('option' => array('value' => '1', 'selected' => 'selected')),
  4335. 'blue',
  4336. '/option',
  4337. array('option' => array('value' => '2')),
  4338. 'red',
  4339. '/option',
  4340. array('option' => array('value' => '3', 'selected' => 'selected')),
  4341. 'green',
  4342. '/option',
  4343. '/select'
  4344. );
  4345. $this->assertTags($result, $expected);
  4346. // make sure only 50 is selected, and not 50f5c0cf
  4347. $this->View->viewVars['contactTags'] = array(
  4348. '1' => 'blue',
  4349. '50f5c0cf' => 'red',
  4350. '50' => 'green'
  4351. );
  4352. $this->Form->request->data = array(
  4353. 'Contact' => array(),
  4354. 'ContactTag' => array(
  4355. array(
  4356. 'id' => 1,
  4357. 'name' => 'blue'
  4358. ),
  4359. array(
  4360. 'id' => 50,
  4361. 'name' => 'green'
  4362. )
  4363. )
  4364. );
  4365. $this->Form->create('Contact');
  4366. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4367. $expected = array(
  4368. 'input' => array(
  4369. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4370. ),
  4371. 'select' => array(
  4372. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4373. 'multiple' => 'multiple'
  4374. ),
  4375. array('option' => array('value' => '1', 'selected' => 'selected')),
  4376. 'blue',
  4377. '/option',
  4378. array('option' => array('value' => '50f5c0cf')),
  4379. 'red',
  4380. '/option',
  4381. array('option' => array('value' => '50', 'selected' => 'selected')),
  4382. 'green',
  4383. '/option',
  4384. '/select'
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. }
  4388. /**
  4389. * test generation of multi select elements in checkbox format
  4390. *
  4391. * @return void
  4392. */
  4393. public function testSelectMultipleCheckboxes() {
  4394. $result = $this->Form->select(
  4395. 'Model.multi_field',
  4396. array('first', 'second', 'third'),
  4397. array('multiple' => 'checkbox')
  4398. );
  4399. $expected = array(
  4400. 'input' => array(
  4401. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4402. ),
  4403. array('div' => array('class' => 'checkbox')),
  4404. array('input' => array(
  4405. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4406. 'value' => '0', 'id' => 'ModelMultiField0'
  4407. )),
  4408. array('label' => array('for' => 'ModelMultiField0')),
  4409. 'first',
  4410. '/label',
  4411. '/div',
  4412. array('div' => array('class' => 'checkbox')),
  4413. array('input' => array(
  4414. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4415. 'value' => '1', 'id' => 'ModelMultiField1'
  4416. )),
  4417. array('label' => array('for' => 'ModelMultiField1')),
  4418. 'second',
  4419. '/label',
  4420. '/div',
  4421. array('div' => array('class' => 'checkbox')),
  4422. array('input' => array(
  4423. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4424. 'value' => '2', 'id' => 'ModelMultiField2'
  4425. )),
  4426. array('label' => array('for' => 'ModelMultiField2')),
  4427. 'third',
  4428. '/label',
  4429. '/div'
  4430. );
  4431. $this->assertTags($result, $expected);
  4432. $result = $this->Form->select(
  4433. 'Model.multi_field',
  4434. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4435. array('multiple' => 'checkbox')
  4436. );
  4437. $expected = array(
  4438. 'input' => array(
  4439. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4440. ),
  4441. array('div' => array('class' => 'checkbox')),
  4442. array('input' => array(
  4443. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4444. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4445. )),
  4446. array('label' => array('for' => 'ModelMultiFieldA')),
  4447. 'first',
  4448. '/label',
  4449. '/div',
  4450. array('div' => array('class' => 'checkbox')),
  4451. array('input' => array(
  4452. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4453. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4454. )),
  4455. array('label' => array('for' => 'ModelMultiFieldB')),
  4456. 'second',
  4457. '/label',
  4458. '/div',
  4459. array('div' => array('class' => 'checkbox')),
  4460. array('input' => array(
  4461. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4462. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4463. )),
  4464. array('label' => array('for' => 'ModelMultiFieldC')),
  4465. 'third',
  4466. '/label',
  4467. '/div'
  4468. );
  4469. $this->assertTags($result, $expected);
  4470. $result = $this->Form->select(
  4471. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4472. );
  4473. $expected = array(
  4474. 'input' => array(
  4475. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4476. ),
  4477. array('div' => array('class' => 'checkbox')),
  4478. array('input' => array(
  4479. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4480. 'value' => '1', 'id' => 'ModelMultiField1'
  4481. )),
  4482. array('label' => array('for' => 'ModelMultiField1')),
  4483. 'first',
  4484. '/label',
  4485. '/div'
  4486. );
  4487. $this->assertTags($result, $expected);
  4488. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4489. $result = $this->Form->select(
  4490. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4491. );
  4492. $expected = array(
  4493. 'input' => array(
  4494. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4495. ),
  4496. array('div' => array('class' => 'checkbox')),
  4497. array('input' => array(
  4498. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4499. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4500. )),
  4501. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4502. 'first',
  4503. '/label',
  4504. '/div',
  4505. array('div' => array('class' => 'checkbox')),
  4506. array('input' => array(
  4507. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4508. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4509. )),
  4510. array('label' => array('for' => 'ModelTagsArray')),
  4511. 'Array',
  4512. '/label',
  4513. '/div'
  4514. );
  4515. $this->assertTags($result, $expected);
  4516. }
  4517. /**
  4518. * test multiple checkboxes with div styles.
  4519. *
  4520. * @return void
  4521. */
  4522. public function testSelectMultipleCheckboxDiv() {
  4523. $result = $this->Form->select(
  4524. 'Model.tags',
  4525. array('first', 'second'),
  4526. array('multiple' => 'checkbox', 'class' => 'my-class')
  4527. );
  4528. $expected = array(
  4529. 'input' => array(
  4530. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4531. ),
  4532. array('div' => array('class' => 'my-class')),
  4533. array('input' => array(
  4534. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4535. 'value' => '0', 'id' => 'ModelTags0'
  4536. )),
  4537. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4538. '/div',
  4539. array('div' => array('class' => 'my-class')),
  4540. array('input' => array(
  4541. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4542. 'value' => '1', 'id' => 'ModelTags1'
  4543. )),
  4544. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4545. '/div'
  4546. );
  4547. $this->assertTags($result, $expected);
  4548. $result = $this->Form->input('Model.tags', array(
  4549. 'options' => array('first', 'second'),
  4550. 'multiple' => 'checkbox',
  4551. 'class' => 'my-class',
  4552. 'div' => false,
  4553. 'label' => false
  4554. ));
  4555. $this->assertTags($result, $expected);
  4556. $Contact = ClassRegistry::getObject('Contact');
  4557. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4558. $result = $this->Form->input('Contact.tags', array(
  4559. 'options' => array('one'),
  4560. 'multiple' => 'checkbox',
  4561. 'label' => false,
  4562. 'div' => false
  4563. ));
  4564. $expected = array(
  4565. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4566. array('div' => array('class' => 'checkbox form-error')),
  4567. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4568. array('label' => array('for' => 'ContactTags0')),
  4569. 'one',
  4570. '/label',
  4571. '/div'
  4572. );
  4573. $this->assertTags($result, $expected);
  4574. $result = $this->Form->input('Contact.tags', array(
  4575. 'options' => array('one'),
  4576. 'multiple' => 'checkbox',
  4577. 'class' => 'mycheckbox',
  4578. 'label' => false,
  4579. 'div' => false
  4580. ));
  4581. $expected = array(
  4582. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4583. array('div' => array('class' => 'mycheckbox form-error')),
  4584. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4585. array('label' => array('for' => 'ContactTags0')),
  4586. 'one',
  4587. '/label',
  4588. '/div'
  4589. );
  4590. $this->assertTags($result, $expected);
  4591. }
  4592. /**
  4593. * Checks the security hash array generated for multiple-input checkbox elements
  4594. *
  4595. * @return void
  4596. */
  4597. public function testSelectMultipleCheckboxSecurity() {
  4598. $this->Form->request['_Token'] = array('key' => 'testKey');
  4599. $this->assertEquals(array(), $this->Form->fields);
  4600. $result = $this->Form->select(
  4601. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4602. array('multiple' => 'checkbox')
  4603. );
  4604. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4605. $result = $this->Form->secure($this->Form->fields);
  4606. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4607. $this->assertRegExp('/"' . $key . '"/', $result);
  4608. }
  4609. /**
  4610. * Multiple select elements should always be secured as they always participate
  4611. * in the POST data.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultipleSecureWithNoOptions() {
  4616. $this->Form->request['_Token'] = array('key' => 'testkey');
  4617. $this->assertEquals(array(), $this->Form->fields);
  4618. $this->Form->select(
  4619. 'Model.select',
  4620. array(),
  4621. array('multiple' => true)
  4622. );
  4623. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4624. }
  4625. /**
  4626. * When a select box has no options it should not be added to the fields list
  4627. * as it always fail post validation.
  4628. *
  4629. * @return void
  4630. */
  4631. public function testSelectNoSecureWithNoOptions() {
  4632. $this->Form->request['_Token'] = array('key' => 'testkey');
  4633. $this->assertEquals(array(), $this->Form->fields);
  4634. $this->Form->select(
  4635. 'Model.select',
  4636. array()
  4637. );
  4638. $this->assertEquals(array(), $this->Form->fields);
  4639. $this->Form->select(
  4640. 'Model.select',
  4641. array(),
  4642. array('empty' => true)
  4643. );
  4644. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4645. }
  4646. /**
  4647. * testInputMultipleCheckboxes method
  4648. *
  4649. * test input() resulting in multi select elements being generated.
  4650. *
  4651. * @return void
  4652. */
  4653. public function testInputMultipleCheckboxes() {
  4654. $result = $this->Form->input('Model.multi_field', array(
  4655. 'options' => array('first', 'second', 'third'),
  4656. 'multiple' => 'checkbox'
  4657. ));
  4658. $expected = array(
  4659. array('div' => array('class' => 'input select')),
  4660. array('label' => array('for' => 'ModelMultiField')),
  4661. 'Multi Field',
  4662. '/label',
  4663. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4664. array('div' => array('class' => 'checkbox')),
  4665. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4666. array('label' => array('for' => 'ModelMultiField0')),
  4667. 'first',
  4668. '/label',
  4669. '/div',
  4670. array('div' => array('class' => 'checkbox')),
  4671. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4672. array('label' => array('for' => 'ModelMultiField1')),
  4673. 'second',
  4674. '/label',
  4675. '/div',
  4676. array('div' => array('class' => 'checkbox')),
  4677. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4678. array('label' => array('for' => 'ModelMultiField2')),
  4679. 'third',
  4680. '/label',
  4681. '/div',
  4682. '/div'
  4683. );
  4684. $this->assertTags($result, $expected);
  4685. $result = $this->Form->input('Model.multi_field', array(
  4686. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4687. 'multiple' => 'checkbox'
  4688. ));
  4689. $expected = array(
  4690. array('div' => array('class' => 'input select')),
  4691. array('label' => array('for' => 'ModelMultiField')),
  4692. 'Multi Field',
  4693. '/label',
  4694. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4695. array('div' => array('class' => 'checkbox')),
  4696. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4697. array('label' => array('for' => 'ModelMultiFieldA')),
  4698. 'first',
  4699. '/label',
  4700. '/div',
  4701. array('div' => array('class' => 'checkbox')),
  4702. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4703. array('label' => array('for' => 'ModelMultiFieldB')),
  4704. 'second',
  4705. '/label',
  4706. '/div',
  4707. array('div' => array('class' => 'checkbox')),
  4708. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4709. array('label' => array('for' => 'ModelMultiFieldC')),
  4710. 'third',
  4711. '/label',
  4712. '/div',
  4713. '/div'
  4714. );
  4715. $this->assertTags($result, $expected);
  4716. $result = $this->Form->input('Model.multi_field', array(
  4717. 'options' => array('1' => 'first'),
  4718. 'multiple' => 'checkbox',
  4719. 'label' => false,
  4720. 'div' => false
  4721. ));
  4722. $expected = array(
  4723. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4724. array('div' => array('class' => 'checkbox')),
  4725. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4726. array('label' => array('for' => 'ModelMultiField1')),
  4727. 'first',
  4728. '/label',
  4729. '/div'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $result = $this->Form->input('Model.multi_field', array(
  4733. 'options' => array('2' => 'second'),
  4734. 'multiple' => 'checkbox',
  4735. 'label' => false,
  4736. 'div' => false
  4737. ));
  4738. $expected = array(
  4739. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4740. array('div' => array('class' => 'checkbox')),
  4741. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4742. array('label' => array('for' => 'ModelMultiField2')),
  4743. 'second',
  4744. '/label',
  4745. '/div'
  4746. );
  4747. $this->assertTags($result, $expected);
  4748. }
  4749. /**
  4750. * testSelectHiddenFieldOmission method
  4751. *
  4752. * test that select() with 'hiddenField' => false omits the hidden field
  4753. *
  4754. * @return void
  4755. */
  4756. public function testSelectHiddenFieldOmission() {
  4757. $result = $this->Form->select('Model.multi_field',
  4758. array('first', 'second'),
  4759. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4760. );
  4761. $expected = array(
  4762. array('div' => array('class' => 'checkbox')),
  4763. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4764. array('label' => array('for' => 'ModelMultiField0')),
  4765. 'first',
  4766. '/label',
  4767. '/div',
  4768. array('div' => array('class' => 'checkbox')),
  4769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4770. array('label' => array('for' => 'ModelMultiField1')),
  4771. 'second',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->input('Model.multi_field', array(
  4777. 'options' => array('first', 'second'),
  4778. 'multiple' => 'checkbox',
  4779. 'hiddenField' => false
  4780. ));
  4781. $expected = array(
  4782. array('div' => array('class' => 'input select')),
  4783. array('label' => array('for' => 'ModelMultiField')),
  4784. 'Multi Field',
  4785. '/label',
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4788. array('label' => array('for' => 'ModelMultiField0')),
  4789. 'first',
  4790. '/label',
  4791. '/div',
  4792. array('div' => array('class' => 'checkbox')),
  4793. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4794. array('label' => array('for' => 'ModelMultiField1')),
  4795. 'second',
  4796. '/label',
  4797. '/div',
  4798. '/div'
  4799. );
  4800. $this->assertTags($result, $expected);
  4801. }
  4802. /**
  4803. * test that select() with multiple = checkbox works with overriding name attribute.
  4804. *
  4805. * @return void
  4806. */
  4807. public function testSelectCheckboxMultipleOverrideName() {
  4808. $result = $this->Form->input('category', array(
  4809. 'type' => 'select',
  4810. 'multiple' => 'checkbox',
  4811. 'name' => 'data[fish]',
  4812. 'options' => array('1', '2'),
  4813. 'div' => false,
  4814. 'label' => false,
  4815. ));
  4816. $expected = array(
  4817. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4818. array('div' => array('class' => 'checkbox')),
  4819. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4820. array('label' => array('for' => 'Category0')), '1', '/label',
  4821. '/div',
  4822. array('div' => array('class' => 'checkbox')),
  4823. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4824. array('label' => array('for' => 'Category1')), '2', '/label',
  4825. '/div'
  4826. );
  4827. $this->assertTags($result, $expected);
  4828. }
  4829. /**
  4830. * Test that 'id' overrides all the checkbox id's as well.
  4831. *
  4832. * @return void
  4833. */
  4834. public function testSelectCheckboxMultipleId() {
  4835. $result = $this->Form->select(
  4836. 'Model.multi_field',
  4837. array('first', 'second', 'third'),
  4838. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4839. );
  4840. $expected = array(
  4841. 'input' => array(
  4842. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4843. ),
  4844. array('div' => array('class' => 'checkbox')),
  4845. array('input' => array(
  4846. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4847. 'value' => '0', 'id' => 'CustomId0'
  4848. )),
  4849. array('label' => array('for' => 'CustomId0')),
  4850. 'first',
  4851. '/label',
  4852. '/div',
  4853. array('div' => array('class' => 'checkbox')),
  4854. array('input' => array(
  4855. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4856. 'value' => '1', 'id' => 'CustomId1'
  4857. )),
  4858. array('label' => array('for' => 'CustomId1')),
  4859. 'second',
  4860. '/label',
  4861. '/div',
  4862. array('div' => array('class' => 'checkbox')),
  4863. array('input' => array(
  4864. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4865. 'value' => '2', 'id' => 'CustomId2'
  4866. )),
  4867. array('label' => array('for' => 'CustomId2')),
  4868. 'third',
  4869. '/label',
  4870. '/div'
  4871. );
  4872. $this->assertTags($result, $expected);
  4873. }
  4874. /**
  4875. * testCheckbox method
  4876. *
  4877. * Test generation of checkboxes
  4878. *
  4879. * @return void
  4880. */
  4881. public function testCheckbox() {
  4882. $result = $this->Form->checkbox('Model.field');
  4883. $expected = array(
  4884. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4885. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4886. );
  4887. $this->assertTags($result, $expected);
  4888. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4889. $expected = array(
  4890. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4891. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4892. );
  4893. $this->assertTags($result, $expected);
  4894. $Contact = ClassRegistry::getObject('Contact');
  4895. $Contact->validationErrors['field'] = 1;
  4896. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4897. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4898. $expected = array(
  4899. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4900. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4901. );
  4902. $this->assertTags($result, $expected);
  4903. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4904. $expected = array(
  4905. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4906. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4907. );
  4908. $this->assertTags($result, $expected);
  4909. $this->Form->request->data['Contact']['field'] = '';
  4910. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4911. $expected = array(
  4912. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4913. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. $Contact->validationErrors = array();
  4917. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4918. $expected = array(
  4919. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4920. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4921. );
  4922. $this->assertTags($result, $expected);
  4923. $this->Form->request->data['Contact']['published'] = 1;
  4924. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4925. $expected = array(
  4926. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4927. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4928. );
  4929. $this->assertTags($result, $expected);
  4930. $this->Form->request->data['Contact']['published'] = 0;
  4931. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4932. $expected = array(
  4933. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4934. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4935. );
  4936. $this->assertTags($result, $expected);
  4937. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4938. $expected = array(
  4939. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4940. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4941. );
  4942. $this->assertTags($result, $expected);
  4943. $result = $this->Form->checkbox('CustomField.1.value');
  4944. $expected = array(
  4945. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4946. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. }
  4950. /**
  4951. * test checkbox() with a custom name attribute
  4952. *
  4953. * @return void
  4954. */
  4955. public function testCheckboxCustomNameAttribute() {
  4956. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4957. $expected = array(
  4958. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4959. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4960. );
  4961. $this->assertTags($result, $expected);
  4962. }
  4963. /**
  4964. * test the checked option for checkboxes.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testCheckboxCheckedOption() {
  4969. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4970. $expected = array(
  4971. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4972. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4976. $expected = array(
  4977. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4978. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4979. );
  4980. $this->assertTags($result, $expected);
  4981. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4982. $expected = array(
  4983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4984. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4985. );
  4986. $this->assertTags($result, $expected);
  4987. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4988. $expected = array(
  4989. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4990. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4991. );
  4992. $this->assertTags($result, $expected);
  4993. $this->Form->request->data['Model']['field'] = 1;
  4994. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4995. $expected = array(
  4996. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4997. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. }
  5001. /**
  5002. * Test that disabled attribute works on both the checkbox and hidden input.
  5003. *
  5004. * @return void
  5005. */
  5006. public function testCheckboxDisabling() {
  5007. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5008. $expected = array(
  5009. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5010. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5014. $expected = array(
  5015. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5016. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. }
  5020. /**
  5021. * Test that the hidden input for checkboxes can be omitted or set to a
  5022. * specific value.
  5023. *
  5024. * @return void
  5025. */
  5026. public function testCheckboxHiddenField() {
  5027. $result = $this->Form->input('UserForm.something', array(
  5028. 'type' => 'checkbox',
  5029. 'hiddenField' => false
  5030. ));
  5031. $expected = array(
  5032. 'div' => array('class' => 'input checkbox'),
  5033. array('input' => array(
  5034. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5035. 'value' => '1', 'id' => 'UserFormSomething'
  5036. )),
  5037. 'label' => array('for' => 'UserFormSomething'),
  5038. 'Something',
  5039. '/label',
  5040. '/div'
  5041. );
  5042. $this->assertTags($result, $expected);
  5043. $result = $this->Form->input('UserForm.something', array(
  5044. 'type' => 'checkbox',
  5045. 'value' => 'Y',
  5046. 'hiddenField' => 'N',
  5047. ));
  5048. $expected = array(
  5049. 'div' => array('class' => 'input checkbox'),
  5050. array('input' => array(
  5051. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5052. 'value' => 'N', 'id' => 'UserFormSomething_'
  5053. )),
  5054. array('input' => array(
  5055. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5056. 'value' => 'Y', 'id' => 'UserFormSomething'
  5057. )),
  5058. 'label' => array('for' => 'UserFormSomething'),
  5059. 'Something',
  5060. '/label',
  5061. '/div'
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. }
  5065. /**
  5066. * testDateTime method
  5067. *
  5068. * Test generation of date/time select elements
  5069. *
  5070. * @return void
  5071. */
  5072. public function testDateTime() {
  5073. extract($this->dateRegex);
  5074. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5075. $now = strtotime('now');
  5076. $expected = array(
  5077. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5078. $daysRegex,
  5079. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5080. date('j', $now),
  5081. '/option',
  5082. '*/select',
  5083. '-',
  5084. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5085. $monthsRegex,
  5086. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5087. date('F', $now),
  5088. '/option',
  5089. '*/select',
  5090. '-',
  5091. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5092. $yearsRegex,
  5093. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5094. date('Y', $now),
  5095. '/option',
  5096. '*/select',
  5097. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5098. $hoursRegex,
  5099. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5100. date('g', $now),
  5101. '/option',
  5102. '*/select',
  5103. ':',
  5104. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5105. $minutesRegex,
  5106. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5107. date('i', $now),
  5108. '/option',
  5109. '*/select',
  5110. ' ',
  5111. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5112. $meridianRegex,
  5113. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5114. date('a', $now),
  5115. '/option',
  5116. '*/select'
  5117. );
  5118. $this->assertTags($result, $expected);
  5119. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5120. $expected = array(
  5121. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5122. $daysRegex,
  5123. array('option' => array('value' => '')),
  5124. '/option',
  5125. '*/select',
  5126. '-',
  5127. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5128. $monthsRegex,
  5129. array('option' => array('value' => '')),
  5130. '/option',
  5131. '*/select',
  5132. '-',
  5133. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5134. $yearsRegex,
  5135. array('option' => array('value' => '')),
  5136. '/option',
  5137. '*/select',
  5138. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5139. $hoursRegex,
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. '*/select',
  5143. ':',
  5144. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5145. $minutesRegex,
  5146. array('option' => array('value' => '')),
  5147. '/option',
  5148. '*/select',
  5149. ' ',
  5150. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5151. $meridianRegex,
  5152. array('option' => array('value' => '')),
  5153. '/option',
  5154. '*/select'
  5155. );
  5156. $this->assertTags($result, $expected);
  5157. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5158. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5159. $this->assertTags($result, $expected);
  5160. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5161. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5162. $this->assertTags($result, $expected);
  5163. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5164. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5165. $expected = array(
  5166. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5167. $daysRegex,
  5168. array('option' => array('value' => '')),
  5169. '/option',
  5170. '*/select',
  5171. '-',
  5172. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5173. $monthsRegex,
  5174. array('option' => array('value' => '')),
  5175. '/option',
  5176. '*/select',
  5177. '-',
  5178. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5179. $yearsRegex,
  5180. array('option' => array('value' => '')),
  5181. '/option',
  5182. '*/select',
  5183. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5184. $hoursRegex,
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. '*/select',
  5188. ':',
  5189. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5190. $minutesRegex,
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '00')),
  5194. '00',
  5195. '/option',
  5196. array('option' => array('value' => '05')),
  5197. '05',
  5198. '/option',
  5199. array('option' => array('value' => '10')),
  5200. '10',
  5201. '/option',
  5202. '*/select',
  5203. ' ',
  5204. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5205. $meridianRegex,
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. '*/select'
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5212. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5213. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5214. $this->Form->request->data['Contact']['data'] = null;
  5215. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5216. $expected = array(
  5217. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5218. $daysRegex,
  5219. array('option' => array('value' => '')),
  5220. '/option',
  5221. '*/select',
  5222. '-',
  5223. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5224. $monthsRegex,
  5225. array('option' => array('value' => '')),
  5226. '/option',
  5227. '*/select',
  5228. '-',
  5229. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5230. $yearsRegex,
  5231. array('option' => array('value' => '')),
  5232. '/option',
  5233. '*/select',
  5234. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5235. $hoursRegex,
  5236. array('option' => array('value' => '')),
  5237. '/option',
  5238. '*/select',
  5239. ':',
  5240. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5241. $minutesRegex,
  5242. array('option' => array('value' => '')),
  5243. '/option',
  5244. '*/select',
  5245. ' ',
  5246. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5247. $meridianRegex,
  5248. array('option' => array('value' => '')),
  5249. '/option',
  5250. '*/select'
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5254. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5255. $now = strtotime($this->Form->data['Model']['field']);
  5256. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5257. $expected = array(
  5258. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5259. $daysRegex,
  5260. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5261. date('j', $now),
  5262. '/option',
  5263. '*/select',
  5264. '-',
  5265. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5266. $monthsRegex,
  5267. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5268. date('F', $now),
  5269. '/option',
  5270. '*/select',
  5271. '-',
  5272. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5273. $yearsRegex,
  5274. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5275. date('Y', $now),
  5276. '/option',
  5277. '*/select',
  5278. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5279. $hoursRegex,
  5280. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5281. date('g', $now),
  5282. '/option',
  5283. '*/select',
  5284. ':',
  5285. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5286. $minutesRegex,
  5287. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5288. date('i', $now),
  5289. '/option',
  5290. '*/select',
  5291. ' ',
  5292. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5293. $meridianRegex,
  5294. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5295. date('a', $now),
  5296. '/option',
  5297. '*/select'
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $selected = strtotime('2008-10-26 12:33:00');
  5301. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5302. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5303. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5304. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5305. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5306. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5307. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5308. $this->Form->create('Contact');
  5309. $result = $this->Form->input('published');
  5310. $now = strtotime('now');
  5311. $expected = array(
  5312. 'div' => array('class' => 'input date'),
  5313. 'label' => array('for' => 'ContactPublishedMonth'),
  5314. 'Published',
  5315. '/label',
  5316. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5317. $monthsRegex,
  5318. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5319. date('F', $now),
  5320. '/option',
  5321. '*/select',
  5322. '-',
  5323. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5324. $daysRegex,
  5325. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5326. date('j', $now),
  5327. '/option',
  5328. '*/select',
  5329. '-',
  5330. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5331. $yearsRegex,
  5332. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5333. date('Y', $now),
  5334. '/option',
  5335. '*/select',
  5336. '/div'
  5337. );
  5338. $this->assertTags($result, $expected);
  5339. $result = $this->Form->input('published2', array('type' => 'date'));
  5340. $now = strtotime('now');
  5341. $expected = array(
  5342. 'div' => array('class' => 'input date'),
  5343. 'label' => array('for' => 'ContactPublished2Month'),
  5344. 'Published2',
  5345. '/label',
  5346. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5347. $monthsRegex,
  5348. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5349. date('F', $now),
  5350. '/option',
  5351. '*/select',
  5352. '-',
  5353. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5354. $daysRegex,
  5355. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5356. date('j', $now),
  5357. '/option',
  5358. '*/select',
  5359. '-',
  5360. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5361. $yearsRegex,
  5362. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5363. date('Y', $now),
  5364. '/option',
  5365. '*/select',
  5366. '/div'
  5367. );
  5368. $this->assertTags($result, $expected);
  5369. $this->Form->create('Contact');
  5370. $result = $this->Form->input('published', array('monthNames' => false));
  5371. $now = strtotime('now');
  5372. $expected = array(
  5373. 'div' => array('class' => 'input date'),
  5374. 'label' => array('for' => 'ContactPublishedMonth'),
  5375. 'Published',
  5376. '/label',
  5377. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5378. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5379. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5380. date('m', $now),
  5381. '/option',
  5382. '*/select',
  5383. '-',
  5384. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5385. $daysRegex,
  5386. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5387. date('j', $now),
  5388. '/option',
  5389. '*/select',
  5390. '-',
  5391. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5392. $yearsRegex,
  5393. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5394. date('Y', $now),
  5395. '/option',
  5396. '*/select',
  5397. '/div'
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. $result = $this->Form->input('published', array(
  5401. 'timeFormat' => 24,
  5402. 'interval' => 5,
  5403. 'selected' => strtotime('2009-09-03 13:37:00'),
  5404. 'type' => 'datetime'
  5405. ));
  5406. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5407. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5408. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5409. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5410. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5411. }
  5412. /**
  5413. * Test dateTime with rounding
  5414. *
  5415. * @return void
  5416. */
  5417. public function testDateTimeRounding() {
  5418. $this->Form->request->data['Contact'] = array(
  5419. 'date' => array(
  5420. 'day' => '13',
  5421. 'month' => '12',
  5422. 'year' => '2010',
  5423. 'hour' => '04',
  5424. 'min' => '19',
  5425. 'meridian' => 'AM'
  5426. )
  5427. );
  5428. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5429. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5430. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5431. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5432. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5433. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5434. }
  5435. /**
  5436. * Test that empty values don't trigger errors.
  5437. *
  5438. * @return void
  5439. */
  5440. public function testDateTimeNoErrorsOnEmptyData() {
  5441. $this->Form->request->data['Contact'] = array(
  5442. 'date' => array(
  5443. 'day' => '',
  5444. 'month' => '',
  5445. 'year' => '',
  5446. 'hour' => '',
  5447. 'min' => '',
  5448. 'meridian' => ''
  5449. )
  5450. );
  5451. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5452. $this->assertNotEmpty($result);
  5453. }
  5454. /**
  5455. * test that datetime() and default values work.
  5456. *
  5457. * @return void
  5458. */
  5459. public function testDatetimeWithDefault() {
  5460. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5461. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5462. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5463. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5464. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5465. 'default' => '2009-06-01 11:15:30'
  5466. ));
  5467. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5468. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5469. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5470. }
  5471. /**
  5472. * test that bogus non-date time data doesn't cause errors.
  5473. *
  5474. * @return void
  5475. */
  5476. public function testDateTimeWithBogusData() {
  5477. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5478. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5479. }
  5480. /**
  5481. * testDateTimeEmptyAsArray
  5482. *
  5483. * @return void
  5484. */
  5485. public function testDateTimeEmptyAsArray() {
  5486. $result = $this->Form->dateTime('Contact.date',
  5487. 'DMY',
  5488. '12',
  5489. array(
  5490. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5491. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5492. )
  5493. )
  5494. );
  5495. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5496. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5497. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5498. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5499. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5500. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5501. $result = $this->Form->dateTime('Contact.date',
  5502. 'DMY',
  5503. '12',
  5504. array(
  5505. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5506. )
  5507. );
  5508. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5509. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5510. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5511. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5512. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5513. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5514. }
  5515. /**
  5516. * testFormDateTimeMulti method
  5517. *
  5518. * test multiple datetime element generation
  5519. *
  5520. * @return void
  5521. */
  5522. public function testFormDateTimeMulti() {
  5523. extract($this->dateRegex);
  5524. $result = $this->Form->dateTime('Contact.1.updated');
  5525. $expected = array(
  5526. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5527. $daysRegex,
  5528. array('option' => array('value' => '')),
  5529. '/option',
  5530. '*/select',
  5531. '-',
  5532. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5533. $monthsRegex,
  5534. array('option' => array('value' => '')),
  5535. '/option',
  5536. '*/select',
  5537. '-',
  5538. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5539. $yearsRegex,
  5540. array('option' => array('value' => '')),
  5541. '/option',
  5542. '*/select',
  5543. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5544. $hoursRegex,
  5545. array('option' => array('value' => '')),
  5546. '/option',
  5547. '*/select',
  5548. ':',
  5549. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5550. $minutesRegex,
  5551. array('option' => array('value' => '')),
  5552. '/option',
  5553. '*/select',
  5554. ' ',
  5555. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5556. $meridianRegex,
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. '*/select'
  5560. );
  5561. $this->assertTags($result, $expected);
  5562. $result = $this->Form->dateTime('Contact.2.updated');
  5563. $expected = array(
  5564. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5565. $daysRegex,
  5566. array('option' => array('value' => '')),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5571. $monthsRegex,
  5572. array('option' => array('value' => '')),
  5573. '/option',
  5574. '*/select',
  5575. '-',
  5576. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5577. $yearsRegex,
  5578. array('option' => array('value' => '')),
  5579. '/option',
  5580. '*/select',
  5581. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5582. $hoursRegex,
  5583. array('option' => array('value' => '')),
  5584. '/option',
  5585. '*/select',
  5586. ':',
  5587. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5588. $minutesRegex,
  5589. array('option' => array('value' => '')),
  5590. '/option',
  5591. '*/select',
  5592. ' ',
  5593. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5594. $meridianRegex,
  5595. array('option' => array('value' => '')),
  5596. '/option',
  5597. '*/select'
  5598. );
  5599. $this->assertTags($result, $expected);
  5600. }
  5601. /**
  5602. * When changing the date format, the label should always focus the first select box when
  5603. * clicked.
  5604. *
  5605. * @return void
  5606. */
  5607. public function testDateTimeLabelIdMatchesFirstInput() {
  5608. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5609. $this->assertContains('label for="ModelDateMonth"', $result);
  5610. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5611. $this->assertContains('label for="ModelDateDay"', $result);
  5612. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5613. $this->assertContains('label for="ModelDateYear"', $result);
  5614. }
  5615. /**
  5616. * testMonth method
  5617. *
  5618. * @return void
  5619. */
  5620. public function testMonth() {
  5621. $result = $this->Form->month('Model.field');
  5622. $expected = array(
  5623. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5624. array('option' => array('value' => '')),
  5625. '/option',
  5626. array('option' => array('value' => '01')),
  5627. date('F', strtotime('2008-01-01 00:00:00')),
  5628. '/option',
  5629. array('option' => array('value' => '02')),
  5630. date('F', strtotime('2008-02-01 00:00:00')),
  5631. '/option',
  5632. '*/select',
  5633. );
  5634. $this->assertTags($result, $expected);
  5635. $result = $this->Form->month('Model.field', array('empty' => true));
  5636. $expected = array(
  5637. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5638. array('option' => array('value' => '')),
  5639. '/option',
  5640. array('option' => array('value' => '01')),
  5641. date('F', strtotime('2008-01-01 00:00:00')),
  5642. '/option',
  5643. array('option' => array('value' => '02')),
  5644. date('F', strtotime('2008-02-01 00:00:00')),
  5645. '/option',
  5646. '*/select',
  5647. );
  5648. $this->assertTags($result, $expected);
  5649. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5650. $expected = array(
  5651. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5652. array('option' => array('value' => '')),
  5653. '/option',
  5654. array('option' => array('value' => '01')),
  5655. '01',
  5656. '/option',
  5657. array('option' => array('value' => '02')),
  5658. '02',
  5659. '/option',
  5660. '*/select',
  5661. );
  5662. $this->assertTags($result, $expected);
  5663. $monthNames = array(
  5664. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5665. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5666. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5667. $expected = array(
  5668. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5669. array('option' => array('value' => '')),
  5670. '/option',
  5671. array('option' => array('value' => '01')),
  5672. 'Jan',
  5673. '/option',
  5674. array('option' => array('value' => '02')),
  5675. 'Feb',
  5676. '/option',
  5677. '*/select',
  5678. );
  5679. $this->assertTags($result, $expected);
  5680. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5681. $result = $this->Form->month('Project.release');
  5682. $expected = array(
  5683. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5684. array('option' => array('value' => '')),
  5685. '/option',
  5686. array('option' => array('value' => '01')),
  5687. 'January',
  5688. '/option',
  5689. array('option' => array('value' => '02', 'selected' => 'selected')),
  5690. 'February',
  5691. '/option',
  5692. '*/select',
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. }
  5696. /**
  5697. * testDay method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testDay() {
  5702. extract($this->dateRegex);
  5703. $result = $this->Form->day('Model.field', array('value' => false));
  5704. $expected = array(
  5705. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5706. array('option' => array('value' => '')),
  5707. '/option',
  5708. array('option' => array('value' => '01')),
  5709. '1',
  5710. '/option',
  5711. array('option' => array('value' => '02')),
  5712. '2',
  5713. '/option',
  5714. $daysRegex,
  5715. '/select',
  5716. );
  5717. $this->assertTags($result, $expected);
  5718. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5719. $result = $this->Form->day('Model.field');
  5720. $expected = array(
  5721. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5722. array('option' => array('value' => '')),
  5723. '/option',
  5724. array('option' => array('value' => '01')),
  5725. '1',
  5726. '/option',
  5727. array('option' => array('value' => '02')),
  5728. '2',
  5729. '/option',
  5730. $daysRegex,
  5731. array('option' => array('value' => '10', 'selected' => 'selected')),
  5732. '10',
  5733. '/option',
  5734. $daysRegex,
  5735. '/select',
  5736. );
  5737. $this->assertTags($result, $expected);
  5738. $this->Form->request->data['Model']['field'] = '';
  5739. $result = $this->Form->day('Model.field', array('value' => '10'));
  5740. $expected = array(
  5741. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5742. array('option' => array('value' => '')),
  5743. '/option',
  5744. array('option' => array('value' => '01')),
  5745. '1',
  5746. '/option',
  5747. array('option' => array('value' => '02')),
  5748. '2',
  5749. '/option',
  5750. $daysRegex,
  5751. array('option' => array('value' => '10', 'selected' => 'selected')),
  5752. '10',
  5753. '/option',
  5754. $daysRegex,
  5755. '/select',
  5756. );
  5757. $this->assertTags($result, $expected);
  5758. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5759. $result = $this->Form->day('Model.field', array('value' => true));
  5760. $expected = array(
  5761. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5762. array('option' => array('value' => '')),
  5763. '/option',
  5764. array('option' => array('value' => '01')),
  5765. '1',
  5766. '/option',
  5767. array('option' => array('value' => '02')),
  5768. '2',
  5769. '/option',
  5770. $daysRegex,
  5771. array('option' => array('value' => '10', 'selected' => 'selected')),
  5772. '10',
  5773. '/option',
  5774. $daysRegex,
  5775. '/select',
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5779. $result = $this->Form->day('Project.release');
  5780. $expected = array(
  5781. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5782. array('option' => array('value' => '')),
  5783. '/option',
  5784. array('option' => array('value' => '01')),
  5785. '1',
  5786. '/option',
  5787. array('option' => array('value' => '02')),
  5788. '2',
  5789. '/option',
  5790. $daysRegex,
  5791. array('option' => array('value' => '10', 'selected' => 'selected')),
  5792. '10',
  5793. '/option',
  5794. $daysRegex,
  5795. '/select',
  5796. );
  5797. $this->assertTags($result, $expected);
  5798. }
  5799. /**
  5800. * testMinute method
  5801. *
  5802. * @return void
  5803. */
  5804. public function testMinute() {
  5805. extract($this->dateRegex);
  5806. $result = $this->Form->minute('Model.field');
  5807. $expected = array(
  5808. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5809. array('option' => array('value' => '')),
  5810. '/option',
  5811. array('option' => array('value' => '00')),
  5812. '00',
  5813. '/option',
  5814. array('option' => array('value' => '01')),
  5815. '01',
  5816. '/option',
  5817. array('option' => array('value' => '02')),
  5818. '02',
  5819. '/option',
  5820. $minutesRegex,
  5821. '/select',
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5825. $result = $this->Form->minute('Model.field');
  5826. $expected = array(
  5827. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5828. array('option' => array('value' => '')),
  5829. '/option',
  5830. array('option' => array('value' => '00')),
  5831. '00',
  5832. '/option',
  5833. array('option' => array('value' => '01')),
  5834. '01',
  5835. '/option',
  5836. array('option' => array('value' => '02')),
  5837. '02',
  5838. '/option',
  5839. $minutesRegex,
  5840. array('option' => array('value' => '12', 'selected' => 'selected')),
  5841. '12',
  5842. '/option',
  5843. $minutesRegex,
  5844. '/select',
  5845. );
  5846. $this->assertTags($result, $expected);
  5847. $this->Form->request->data['Model']['field'] = '';
  5848. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5849. $expected = array(
  5850. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5851. array('option' => array('value' => '')),
  5852. '/option',
  5853. array('option' => array('value' => '00')),
  5854. '00',
  5855. '/option',
  5856. array('option' => array('value' => '05')),
  5857. '05',
  5858. '/option',
  5859. array('option' => array('value' => '10')),
  5860. '10',
  5861. '/option',
  5862. $minutesRegex,
  5863. '/select',
  5864. );
  5865. $this->assertTags($result, $expected);
  5866. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5867. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5868. $expected = array(
  5869. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5870. array('option' => array('value' => '')),
  5871. '/option',
  5872. array('option' => array('value' => '00')),
  5873. '00',
  5874. '/option',
  5875. array('option' => array('value' => '05')),
  5876. '05',
  5877. '/option',
  5878. array('option' => array('value' => '10', 'selected' => 'selected')),
  5879. '10',
  5880. '/option',
  5881. $minutesRegex,
  5882. '/select',
  5883. );
  5884. $this->assertTags($result, $expected);
  5885. }
  5886. /**
  5887. * testHour method
  5888. *
  5889. * @return void
  5890. */
  5891. public function testHour() {
  5892. extract($this->dateRegex);
  5893. $result = $this->Form->hour('Model.field', false);
  5894. $expected = array(
  5895. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5896. array('option' => array('value' => '')),
  5897. '/option',
  5898. array('option' => array('value' => '01')),
  5899. '1',
  5900. '/option',
  5901. array('option' => array('value' => '02')),
  5902. '2',
  5903. '/option',
  5904. $hoursRegex,
  5905. '/select',
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5909. $result = $this->Form->hour('Model.field', false);
  5910. $expected = array(
  5911. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5912. array('option' => array('value' => '')),
  5913. '/option',
  5914. array('option' => array('value' => '01')),
  5915. '1',
  5916. '/option',
  5917. array('option' => array('value' => '02')),
  5918. '2',
  5919. '/option',
  5920. $hoursRegex,
  5921. array('option' => array('value' => '12', 'selected' => 'selected')),
  5922. '12',
  5923. '/option',
  5924. '/select',
  5925. );
  5926. $this->assertTags($result, $expected);
  5927. $this->Form->request->data['Model']['field'] = '';
  5928. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5929. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5930. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5931. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5932. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5933. $result = $this->Form->hour('Model.field', true);
  5934. $expected = array(
  5935. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5936. array('option' => array('value' => '')),
  5937. '/option',
  5938. array('option' => array('value' => '00', 'selected' => 'selected')),
  5939. '0',
  5940. '/option',
  5941. array('option' => array('value' => '01')),
  5942. '1',
  5943. '/option',
  5944. array('option' => array('value' => '02')),
  5945. '2',
  5946. '/option',
  5947. $hoursRegex,
  5948. '/select',
  5949. );
  5950. $this->assertTags($result, $expected);
  5951. unset($this->Form->request->data['Model']['field']);
  5952. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5953. $thisHour = date('H');
  5954. $optValue = date('G');
  5955. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5956. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5957. $result = $this->Form->hour('Model.field', true);
  5958. $expected = array(
  5959. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5960. array('option' => array('value' => '')),
  5961. '/option',
  5962. array('option' => array('value' => '00')),
  5963. '0',
  5964. '/option',
  5965. array('option' => array('value' => '01', 'selected' => 'selected')),
  5966. '1',
  5967. '/option',
  5968. array('option' => array('value' => '02')),
  5969. '2',
  5970. '/option',
  5971. $hoursRegex,
  5972. '/select',
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. }
  5976. /**
  5977. * testYear method
  5978. *
  5979. * @return void
  5980. */
  5981. public function testYear() {
  5982. $result = $this->Form->year('Model.field', 2006, 2007);
  5983. $expected = array(
  5984. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5985. array('option' => array('value' => '')),
  5986. '/option',
  5987. array('option' => array('value' => '2007')),
  5988. '2007',
  5989. '/option',
  5990. array('option' => array('value' => '2006')),
  5991. '2006',
  5992. '/option',
  5993. '/select',
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5997. $expected = array(
  5998. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5999. array('option' => array('value' => '')),
  6000. '/option',
  6001. array('option' => array('value' => '2006')),
  6002. '2006',
  6003. '/option',
  6004. array('option' => array('value' => '2007')),
  6005. '2007',
  6006. '/option',
  6007. '/select',
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. $this->request->data['Contact']['published'] = '';
  6011. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6012. $expected = array(
  6013. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6014. array('option' => array('value' => '')),
  6015. '/option',
  6016. array('option' => array('value' => '2007')),
  6017. '2007',
  6018. '/option',
  6019. array('option' => array('value' => '2006')),
  6020. '2006',
  6021. '/option',
  6022. '/select',
  6023. );
  6024. $this->assertTags($result, $expected);
  6025. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6026. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6027. $expected = array(
  6028. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6029. array('option' => array('value' => '2007')),
  6030. '2007',
  6031. '/option',
  6032. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6033. '2006',
  6034. '/option',
  6035. '/select',
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request->data['Contact']['published'] = '';
  6039. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6040. $expected = array(
  6041. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6042. array('option' => array('value' => '')),
  6043. '/option',
  6044. array('option' => array('value' => '2007')),
  6045. '2007',
  6046. '/option',
  6047. array('option' => array('value' => '2006')),
  6048. '2006',
  6049. '/option',
  6050. '/select',
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6054. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6055. $expected = array(
  6056. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6057. array('option' => array('value' => '2007')),
  6058. '2007',
  6059. '/option',
  6060. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6061. '2006',
  6062. '/option',
  6063. '/select',
  6064. );
  6065. $this->assertTags($result, $expected);
  6066. $this->Form->request->data['Contact']['published'] = '';
  6067. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6073. '2007',
  6074. '/option',
  6075. array('option' => array('value' => '2006')),
  6076. '2006',
  6077. '/option',
  6078. '/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6082. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6083. $expected = array(
  6084. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6085. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6086. '2007',
  6087. '/option',
  6088. array('option' => array('value' => '2006')),
  6089. '2006',
  6090. '/option',
  6091. '/select',
  6092. );
  6093. $this->assertTags($result, $expected);
  6094. $this->Form->request->data['Contact']['published'] = '';
  6095. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6096. $expected = array(
  6097. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6098. array('option' => array('value' => '2008')),
  6099. '2008',
  6100. '/option',
  6101. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6102. '2007',
  6103. '/option',
  6104. array('option' => array('value' => '2006')),
  6105. '2006',
  6106. '/option',
  6107. '/select',
  6108. );
  6109. $this->assertTags($result, $expected);
  6110. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6111. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6112. $expected = array(
  6113. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6114. array('option' => array('value' => '2008')),
  6115. '2008',
  6116. '/option',
  6117. array('option' => array('value' => '2007')),
  6118. '2007',
  6119. '/option',
  6120. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6121. '2006',
  6122. '/option',
  6123. '/select',
  6124. );
  6125. $this->assertTags($result, $expected);
  6126. $this->Form->request->data = array();
  6127. $this->Form->create('Contact');
  6128. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6129. $expected = array(
  6130. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6131. array('option' => array('value' => '2008')),
  6132. '2008',
  6133. '/option',
  6134. array('option' => array('value' => '2007')),
  6135. '2007',
  6136. '/option',
  6137. array('option' => array('value' => '2006')),
  6138. '2006',
  6139. '/option',
  6140. '/select',
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6144. $this->assertContains('data[Contact][published][year]', $result);
  6145. }
  6146. /**
  6147. * testYearAutoExpandRange method
  6148. *
  6149. * @return void
  6150. */
  6151. public function testYearAutoExpandRange() {
  6152. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6153. $result = $this->Form->year('User.birthday');
  6154. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6155. $result = $matches[1];
  6156. $expected = range(date('Y') + 20, 1930);
  6157. $this->assertEquals($result, $expected);
  6158. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6159. $result = $this->Form->year('Project.release');
  6160. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6161. $result = $matches[1];
  6162. $expected = range(2050, date('Y') - 20);
  6163. $this->assertEquals($result, $expected);
  6164. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6165. $result = $this->Form->year('Project.release', 1890, 1900);
  6166. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6167. $result = $matches[1];
  6168. $expected = range(1900, 1881);
  6169. $this->assertEquals($result, $expected);
  6170. }
  6171. /**
  6172. * testInputDate method
  6173. *
  6174. * Test various inputs with type date and different dateFormat values.
  6175. * Failing to provide a dateFormat key should not error.
  6176. * It should simply not pre-select any value then.
  6177. *
  6178. * @return void
  6179. */
  6180. public function testInputDate() {
  6181. $this->Form->request->data = array(
  6182. 'User' => array(
  6183. 'month_year' => array('month' => date('m')),
  6184. 'just_year' => array('month' => date('m')),
  6185. 'just_month' => array('year' => date('Y')),
  6186. 'just_day' => array('month' => date('m')),
  6187. )
  6188. );
  6189. $this->Form->create('User');
  6190. $result = $this->Form->input('month_year',
  6191. array(
  6192. 'label' => false,
  6193. 'div' => false,
  6194. 'type' => 'date',
  6195. 'dateFormat' => 'MY',
  6196. 'minYear' => 2006,
  6197. 'maxYear' => 2008
  6198. )
  6199. );
  6200. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6201. $this->assertNotContains('value="2008" selected="selected"', $result);
  6202. $result = $this->Form->input('just_year',
  6203. array(
  6204. 'type' => 'date',
  6205. 'label' => false,
  6206. 'dateFormat' => 'Y',
  6207. 'minYear' => date('Y'),
  6208. 'maxYear' => date('Y', strtotime('+20 years'))
  6209. )
  6210. );
  6211. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6212. $result = $this->Form->input('just_month',
  6213. array(
  6214. 'type' => 'date',
  6215. 'label' => false,
  6216. 'dateFormat' => 'M',
  6217. 'empty' => false,
  6218. )
  6219. );
  6220. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6221. $result = $this->Form->input('just_day',
  6222. array(
  6223. 'type' => 'date',
  6224. 'label' => false,
  6225. 'dateFormat' => 'D',
  6226. 'empty' => false,
  6227. )
  6228. );
  6229. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6230. }
  6231. /**
  6232. * testInputDateMaxYear method
  6233. *
  6234. * Let's say we want to only allow users born from 2006 to 2008 to register
  6235. * This being the first singup page, we still don't have any data
  6236. *
  6237. * @return void
  6238. */
  6239. public function testInputDateMaxYear() {
  6240. $this->Form->request->data = array();
  6241. $this->Form->create('User');
  6242. $result = $this->Form->input('birthday',
  6243. array(
  6244. 'label' => false,
  6245. 'div' => false,
  6246. 'type' => 'date',
  6247. 'dateFormat' => 'DMY',
  6248. 'minYear' => 2006,
  6249. 'maxYear' => 2008
  6250. )
  6251. );
  6252. $this->assertContains('value="2008" selected="selected"', $result);
  6253. }
  6254. /**
  6255. * testTextArea method
  6256. *
  6257. * @return void
  6258. */
  6259. public function testTextArea() {
  6260. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6261. $result = $this->Form->textarea('Model.field');
  6262. $expected = array(
  6263. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6264. 'some test data',
  6265. '/textarea',
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. $result = $this->Form->textarea('Model.tmp');
  6269. $expected = array(
  6270. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6271. '/textarea',
  6272. );
  6273. $this->assertTags($result, $expected);
  6274. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6275. $result = $this->Form->textarea('Model.field');
  6276. $expected = array(
  6277. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6278. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6279. '/textarea',
  6280. );
  6281. $this->assertTags($result, $expected);
  6282. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6283. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6284. $expected = array(
  6285. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6286. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6287. '/textarea',
  6288. );
  6289. $this->assertTags($result, $expected);
  6290. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6291. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6292. $expected = array(
  6293. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6294. '/textarea'
  6295. );
  6296. $this->assertTags($result, $expected);
  6297. }
  6298. /**
  6299. * testTextAreaWithStupidCharacters method
  6300. *
  6301. * test text area with non-ascii characters
  6302. *
  6303. * @return void
  6304. */
  6305. public function testTextAreaWithStupidCharacters() {
  6306. $this->loadFixtures('Post');
  6307. $result = $this->Form->input('Post.content', array(
  6308. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6309. ));
  6310. $expected = array(
  6311. 'div' => array('class' => 'input textarea'),
  6312. 'label' => array('for' => 'PostContent'),
  6313. 'Current Text',
  6314. '/label',
  6315. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6316. 'GREAT®',
  6317. '/textarea',
  6318. '/div'
  6319. );
  6320. $this->assertTags($result, $expected);
  6321. }
  6322. /**
  6323. * testHiddenField method
  6324. *
  6325. * @return void
  6326. */
  6327. public function testHiddenField() {
  6328. $Contact = ClassRegistry::getObject('Contact');
  6329. $Contact->validationErrors['field'] = 1;
  6330. $this->Form->request->data['Contact']['field'] = 'test';
  6331. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6332. $this->assertTags($result, array(
  6333. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6334. );
  6335. }
  6336. /**
  6337. * testFileUploadField method
  6338. *
  6339. * @return void
  6340. */
  6341. public function testFileUploadField() {
  6342. $result = $this->Form->file('Model.upload');
  6343. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6344. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6345. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6346. $expected = array(
  6347. 'div' => array('class' => 'input file'),
  6348. 'label' => array('for' => 'ModelUpload'),
  6349. 'Upload',
  6350. '/label',
  6351. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6352. '/div'
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6356. $result = $this->Form->file('Model.upload');
  6357. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6358. }
  6359. /**
  6360. * test File upload input on a model not used in create();
  6361. *
  6362. * @return void
  6363. */
  6364. public function testFileUploadOnOtherModel() {
  6365. $this->Form->create('ValidateUser', array('type' => 'file'));
  6366. $result = $this->Form->file('ValidateProfile.city');
  6367. $expected = array(
  6368. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6369. );
  6370. $this->assertTags($result, $expected);
  6371. }
  6372. /**
  6373. * testButton method
  6374. *
  6375. * @return void
  6376. */
  6377. public function testButton() {
  6378. $result = $this->Form->button('Hi');
  6379. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6380. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6381. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6382. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6383. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6384. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6385. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6386. $result = $this->Form->button('No type', array('type' => false));
  6387. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6388. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6389. $this->assertNotRegExp('/\&039/', $result);
  6390. }
  6391. /**
  6392. * Test that button() makes unlocked fields by default.
  6393. *
  6394. * @return void
  6395. */
  6396. public function testButtonUnlockedByDefault() {
  6397. $this->Form->request->params['_Token']['key'] = 'secured';
  6398. $this->Form->button('Save', array('name' => 'save'));
  6399. $this->Form->button('Clear');
  6400. $result = $this->Form->unlockField();
  6401. $this->assertEquals(array('save'), $result);
  6402. }
  6403. /**
  6404. * testPostButton method
  6405. *
  6406. * @return void
  6407. */
  6408. public function testPostButton() {
  6409. $result = $this->Form->postButton('Hi', '/controller/action');
  6410. $this->assertTags($result, array(
  6411. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6412. 'div' => array('style' => 'display:none;'),
  6413. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6414. '/div',
  6415. 'button' => array('type' => 'submit'),
  6416. 'Hi',
  6417. '/button',
  6418. '/form'
  6419. ));
  6420. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6421. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6422. }
  6423. /**
  6424. * Test that postButton adds _Token fields.
  6425. *
  6426. * @return void
  6427. */
  6428. public function testSecurePostButton() {
  6429. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6430. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6431. $expected = array(
  6432. 'form' => array(
  6433. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6434. ),
  6435. array('div' => array('style' => 'display:none;')),
  6436. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6437. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6438. '/div',
  6439. 'button' => array('type' => 'submit'),
  6440. 'Delete',
  6441. '/button',
  6442. array('div' => array('style' => 'display:none;')),
  6443. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6444. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6445. '/div',
  6446. '/form',
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. }
  6450. /**
  6451. * testPostLink method
  6452. *
  6453. * @return void
  6454. */
  6455. public function testPostLink() {
  6456. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6457. $this->assertTags($result, array(
  6458. 'form' => array(
  6459. 'method' => 'post', 'action' => '/posts/delete/1',
  6460. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6461. ),
  6462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6463. '/form',
  6464. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6465. 'Delete',
  6466. '/a'
  6467. ));
  6468. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6469. $this->assertTags($result, array(
  6470. 'form' => array(
  6471. 'method' => 'post', 'action' => '/posts/delete/1',
  6472. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6473. ),
  6474. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6475. '/form',
  6476. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6477. 'Delete',
  6478. '/a'
  6479. ));
  6480. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6481. $this->assertTags($result, array(
  6482. 'form' => array(
  6483. 'method' => 'post', 'action' => '/posts/delete/1',
  6484. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6485. ),
  6486. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6487. '/form',
  6488. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6489. 'Delete',
  6490. '/a'
  6491. ));
  6492. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6493. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6494. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6495. $this->assertTags($result, array(
  6496. 'form' => array(
  6497. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6498. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6499. ),
  6500. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6501. '/form',
  6502. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6503. 'Delete',
  6504. '/a'
  6505. ));
  6506. }
  6507. /**
  6508. * Test that postLink adds _Token fields.
  6509. *
  6510. * @return void
  6511. */
  6512. public function testSecurePostLink() {
  6513. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6514. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6515. $expected = array(
  6516. 'form' => array(
  6517. 'method' => 'post', 'action' => '/posts/delete/1',
  6518. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6519. ),
  6520. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6521. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6522. 'div' => array('style' => 'display:none;'),
  6523. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6524. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6525. '/div',
  6526. '/form',
  6527. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6528. 'Delete',
  6529. '/a'
  6530. );
  6531. $this->assertTags($result, $expected);
  6532. }
  6533. /**
  6534. * testSubmitButton method
  6535. *
  6536. * @return void
  6537. */
  6538. public function testSubmitButton() {
  6539. $result = $this->Form->submit('');
  6540. $expected = array(
  6541. 'div' => array('class' => 'submit'),
  6542. 'input' => array('type' => 'submit', 'value' => ''),
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. $result = $this->Form->submit('Test Submit');
  6547. $expected = array(
  6548. 'div' => array('class' => 'submit'),
  6549. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6550. '/div'
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6554. $expected = array(
  6555. 'span' => array('class' => 'submit'),
  6556. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6557. '/span'
  6558. );
  6559. $this->assertTags($result, $expected);
  6560. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6561. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6562. $this->assertTags($result, $expected);
  6563. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6564. $expected = array(
  6565. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6566. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6567. '/div'
  6568. );
  6569. $this->assertTags($result, $expected);
  6570. $result = $this->Form->submit('Next >');
  6571. $expected = array(
  6572. 'div' => array('class' => 'submit'),
  6573. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6574. '/div'
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. $result = $this->Form->submit('Next >', array('escape' => false));
  6578. $expected = array(
  6579. 'div' => array('class' => 'submit'),
  6580. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6581. '/div'
  6582. );
  6583. $this->assertTags($result, $expected);
  6584. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6585. $expected = array(
  6586. 'div' => array('class' => 'submit'),
  6587. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6588. '/div'
  6589. );
  6590. $this->assertTags($result, $expected);
  6591. $before = '--before--';
  6592. $after = '--after--';
  6593. $result = $this->Form->submit('Test', array('before' => $before));
  6594. $expected = array(
  6595. 'div' => array('class' => 'submit'),
  6596. '--before--',
  6597. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6598. '/div'
  6599. );
  6600. $this->assertTags($result, $expected);
  6601. $result = $this->Form->submit('Test', array('after' => $after));
  6602. $expected = array(
  6603. 'div' => array('class' => 'submit'),
  6604. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6605. '--after--',
  6606. '/div'
  6607. );
  6608. $this->assertTags($result, $expected);
  6609. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6610. $expected = array(
  6611. 'div' => array('class' => 'submit'),
  6612. '--before--',
  6613. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6614. '--after--',
  6615. '/div'
  6616. );
  6617. $this->assertTags($result, $expected);
  6618. }
  6619. /**
  6620. * test image submit types.
  6621. *
  6622. * @return void
  6623. */
  6624. public function testSubmitImage() {
  6625. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6626. $expected = array(
  6627. 'div' => array('class' => 'submit'),
  6628. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6629. '/div'
  6630. );
  6631. $this->assertTags($result, $expected);
  6632. $result = $this->Form->submit('/relative/cake.power.gif');
  6633. $expected = array(
  6634. 'div' => array('class' => 'submit'),
  6635. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6636. '/div'
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. $result = $this->Form->submit('cake.power.gif');
  6640. $expected = array(
  6641. 'div' => array('class' => 'submit'),
  6642. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6643. '/div'
  6644. );
  6645. $this->assertTags($result, $expected);
  6646. $result = $this->Form->submit('Not.an.image');
  6647. $expected = array(
  6648. 'div' => array('class' => 'submit'),
  6649. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6650. '/div'
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. $after = '--after--';
  6654. $before = '--before--';
  6655. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6656. $expected = array(
  6657. 'div' => array('class' => 'submit'),
  6658. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6659. '--after--',
  6660. '/div'
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6664. $expected = array(
  6665. 'div' => array('class' => 'submit'),
  6666. '--before--',
  6667. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6668. '/div'
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6672. $expected = array(
  6673. 'div' => array('class' => 'submit'),
  6674. '--before--',
  6675. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6676. '--after--',
  6677. '/div'
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6681. $expected = array(
  6682. 'div' => array('class' => 'submit'),
  6683. '--before--',
  6684. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6685. '--after--',
  6686. '/div'
  6687. );
  6688. $this->assertTags($result, $expected);
  6689. }
  6690. /**
  6691. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6692. * be submitted at a time.
  6693. *
  6694. * @return void
  6695. */
  6696. public function testSubmitUnlockedByDefault() {
  6697. $this->Form->request->params['_Token']['key'] = 'secured';
  6698. $this->Form->submit('Go go');
  6699. $this->Form->submit('Save', array('name' => 'save'));
  6700. $result = $this->Form->unlockField();
  6701. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6702. }
  6703. /**
  6704. * Test submit image with timestamps.
  6705. *
  6706. * @return void
  6707. */
  6708. public function testSubmitImageTimestamp() {
  6709. Configure::write('Asset.timestamp', 'force');
  6710. $result = $this->Form->submit('cake.power.gif');
  6711. $expected = array(
  6712. 'div' => array('class' => 'submit'),
  6713. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. }
  6718. /**
  6719. * test the create() method
  6720. *
  6721. * @return void
  6722. */
  6723. public function testCreate() {
  6724. $result = $this->Form->create('Contact');
  6725. $encoding = strtolower(Configure::read('App.encoding'));
  6726. $expected = array(
  6727. 'form' => array(
  6728. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6729. 'accept-charset' => $encoding
  6730. ),
  6731. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6732. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6733. '/div'
  6734. );
  6735. $this->assertTags($result, $expected);
  6736. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6737. $expected = array('form' => array(
  6738. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6739. 'accept-charset' => $encoding
  6740. ));
  6741. $this->assertTags($result, $expected);
  6742. $result = $this->Form->create('Contact', array('type' => 'get'));
  6743. $expected = array('form' => array(
  6744. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6745. 'accept-charset' => $encoding
  6746. ));
  6747. $this->assertTags($result, $expected);
  6748. $result = $this->Form->create('Contact', array('type' => 'put'));
  6749. $expected = array(
  6750. 'form' => array(
  6751. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6752. 'accept-charset' => $encoding
  6753. ),
  6754. 'div' => array('style' => 'display:none;'),
  6755. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6756. '/div'
  6757. );
  6758. $this->assertTags($result, $expected);
  6759. $result = $this->Form->create('Contact', array('type' => 'file'));
  6760. $expected = array(
  6761. 'form' => array(
  6762. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6763. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6764. ),
  6765. 'div' => array('style' => 'display:none;'),
  6766. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6767. '/div'
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $this->Form->request->data['Contact']['id'] = 1;
  6771. $this->Form->request->here = '/contacts/edit/1';
  6772. $this->Form->request['action'] = 'edit';
  6773. $result = $this->Form->create('Contact');
  6774. $expected = array(
  6775. 'form' => array(
  6776. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6777. 'accept-charset' => $encoding
  6778. ),
  6779. 'div' => array('style' => 'display:none;'),
  6780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6781. '/div'
  6782. );
  6783. $this->assertTags($result, $expected);
  6784. $this->Form->request->data['Contact']['id'] = 1;
  6785. $this->Form->request->here = '/contacts/edit/1';
  6786. $this->Form->request['action'] = 'edit';
  6787. $result = $this->Form->create('Contact', array('type' => 'file'));
  6788. $expected = array(
  6789. 'form' => array(
  6790. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6791. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6792. ),
  6793. 'div' => array('style' => 'display:none;'),
  6794. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6795. '/div'
  6796. );
  6797. $this->assertTags($result, $expected);
  6798. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6799. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6800. $expected = array(
  6801. 'form' => array(
  6802. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6803. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6804. ),
  6805. 'div' => array('style' => 'display:none;'),
  6806. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6807. '/div'
  6808. );
  6809. $this->assertTags($result, $expected);
  6810. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6811. $expected = array(
  6812. 'form' => array(
  6813. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6814. 'accept-charset' => $encoding
  6815. ),
  6816. 'div' => array('style' => 'display:none;'),
  6817. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6818. '/div'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $this->Form->request['action'] = 'add';
  6822. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6823. $expected = array(
  6824. 'form' => array(
  6825. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6826. 'accept-charset' => $encoding
  6827. ),
  6828. 'div' => array('style' => 'display:none;'),
  6829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6830. '/div'
  6831. );
  6832. $this->assertTags($result, $expected);
  6833. $result = $this->Form->create('User', array('action' => 'login'));
  6834. $expected = array(
  6835. 'form' => array(
  6836. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6837. 'accept-charset' => $encoding
  6838. ),
  6839. 'div' => array('style' => 'display:none;'),
  6840. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6841. '/div'
  6842. );
  6843. $this->assertTags($result, $expected);
  6844. $result = $this->Form->create('User', array('url' => '/users/login'));
  6845. $expected = array(
  6846. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6847. 'div' => array('style' => 'display:none;'),
  6848. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6849. '/div'
  6850. );
  6851. $this->assertTags($result, $expected);
  6852. $this->Form->request['controller'] = 'pages';
  6853. $result = $this->Form->create('User', array('action' => 'signup'));
  6854. $expected = array(
  6855. 'form' => array(
  6856. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6857. 'accept-charset' => $encoding
  6858. ),
  6859. 'div' => array('style' => 'display:none;'),
  6860. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6861. '/div'
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. $this->Form->request->data = array();
  6865. $this->Form->request['controller'] = 'contacts';
  6866. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6867. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6868. $expected = array(
  6869. 'form' => array(
  6870. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6871. 'accept-charset' => 'utf-8'
  6872. ),
  6873. 'div' => array('style' => 'display:none;'),
  6874. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6875. '/div'
  6876. );
  6877. $this->assertTags($result, $expected);
  6878. }
  6879. /**
  6880. * Test the onsubmit option for create()
  6881. *
  6882. * @return void
  6883. */
  6884. public function testCreateOnSubmit() {
  6885. $this->Form->request->data = array();
  6886. $this->Form->request['controller'] = 'contacts';
  6887. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6888. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6889. $expected = array(
  6890. 'form' => array(
  6891. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6892. 'accept-charset' => 'utf-8'
  6893. ),
  6894. 'div' => array('style' => 'display:none;'),
  6895. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6896. '/div'
  6897. );
  6898. $this->assertTags($result, $expected);
  6899. $this->Form->request->data = array();
  6900. $this->Form->request['controller'] = 'contacts';
  6901. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6902. $result = $this->Form->create(array(
  6903. 'url' => array('action' => 'index', 'param'),
  6904. 'default' => false,
  6905. 'onsubmit' => 'someFunction();'
  6906. ));
  6907. $expected = array(
  6908. 'form' => array(
  6909. 'id' => 'ContactAddForm', 'method' => 'post',
  6910. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6911. 'action' => '/contacts/index/param',
  6912. 'accept-charset' => 'utf-8'
  6913. ),
  6914. 'div' => array('style' => 'display:none;'),
  6915. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6916. '/div'
  6917. );
  6918. $this->assertTags($result, $expected);
  6919. }
  6920. /**
  6921. * test create() with automatic url generation
  6922. *
  6923. * @return void
  6924. */
  6925. public function testCreateAutoUrl() {
  6926. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6927. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6928. $this->Form->request->base = '/base_url';
  6929. $result = $this->Form->create('Contact');
  6930. $expected = array(
  6931. 'form' => array(
  6932. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6933. 'accept-charset' => 'utf-8'
  6934. ),
  6935. 'div' => array('style' => 'display:none;'),
  6936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6937. '/div'
  6938. );
  6939. $this->assertTags($result, $expected);
  6940. $this->Form->request['action'] = 'delete';
  6941. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6942. $this->Form->request->base = '/base_url';
  6943. $result = $this->Form->create('Contact');
  6944. $expected = array(
  6945. 'form' => array(
  6946. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6947. 'accept-charset' => 'utf-8'
  6948. ),
  6949. 'div' => array('style' => 'display:none;'),
  6950. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6951. '/div'
  6952. );
  6953. $this->assertTags($result, $expected);
  6954. }
  6955. /**
  6956. * test create() with a custom route
  6957. *
  6958. * @return void
  6959. */
  6960. public function testCreateCustomRoute() {
  6961. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6962. $encoding = strtolower(Configure::read('App.encoding'));
  6963. $result = $this->Form->create('User', array('action' => 'login'));
  6964. $expected = array(
  6965. 'form' => array(
  6966. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6967. 'accept-charset' => $encoding
  6968. ),
  6969. 'div' => array('style' => 'display:none;'),
  6970. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6971. '/div'
  6972. );
  6973. $this->assertTags($result, $expected);
  6974. }
  6975. /**
  6976. * test that inputDefaults are stored and used.
  6977. *
  6978. * @return void
  6979. */
  6980. public function testCreateWithInputDefaults() {
  6981. $this->Form->create('User', array(
  6982. 'inputDefaults' => array(
  6983. 'div' => false,
  6984. 'label' => false,
  6985. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6986. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6987. )
  6988. ));
  6989. $result = $this->Form->input('username');
  6990. $expected = array(
  6991. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6992. );
  6993. $this->assertTags($result, $expected);
  6994. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6995. $expected = array(
  6996. 'div' => array('class' => 'input text'),
  6997. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6998. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6999. '/div'
  7000. );
  7001. $this->assertTags($result, $expected);
  7002. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7003. $expected = array(
  7004. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7005. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7006. );
  7007. $this->assertTags($result, $expected);
  7008. $this->Form->create('User', array(
  7009. 'inputDefaults' => array(
  7010. 'div' => false,
  7011. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7012. )
  7013. ));
  7014. $result = $this->Form->input('username', array('div' => true));
  7015. $expected = array(
  7016. 'div' => array('class' => 'input text'),
  7017. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7018. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7019. '/div'
  7020. );
  7021. $this->assertTags($result, $expected);
  7022. }
  7023. /**
  7024. * test automatic accept-charset overriding
  7025. *
  7026. * @return void
  7027. */
  7028. public function testCreateWithAcceptCharset() {
  7029. $result = $this->Form->create('UserForm', array(
  7030. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7031. )
  7032. );
  7033. $expected = array(
  7034. 'form' => array(
  7035. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7036. 'accept-charset' => 'iso-8859-1'
  7037. ),
  7038. 'div' => array('style' => 'display:none;'),
  7039. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7040. '/div'
  7041. );
  7042. $this->assertTags($result, $expected);
  7043. }
  7044. /**
  7045. * Test base form url when url param is passed with multiple parameters (&)
  7046. *
  7047. */
  7048. public function testCreateQuerystringrequest() {
  7049. $encoding = strtolower(Configure::read('App.encoding'));
  7050. $result = $this->Form->create('Contact', array(
  7051. 'type' => 'post',
  7052. 'escape' => false,
  7053. 'url' => array(
  7054. 'controller' => 'controller',
  7055. 'action' => 'action',
  7056. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7057. )
  7058. ));
  7059. $expected = array(
  7060. 'form' => array(
  7061. 'id' => 'ContactAddForm',
  7062. 'method' => 'post',
  7063. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7064. 'accept-charset' => $encoding
  7065. ),
  7066. 'div' => array('style' => 'display:none;'),
  7067. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7068. '/div'
  7069. );
  7070. $this->assertTags($result, $expected);
  7071. $result = $this->Form->create('Contact', array(
  7072. 'type' => 'post',
  7073. 'url' => array(
  7074. 'controller' => 'controller',
  7075. 'action' => 'action',
  7076. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7077. )
  7078. ));
  7079. $expected = array(
  7080. 'form' => array(
  7081. 'id' => 'ContactAddForm',
  7082. 'method' => 'post',
  7083. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7084. 'accept-charset' => $encoding
  7085. ),
  7086. 'div' => array('style' => 'display:none;'),
  7087. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7088. '/div'
  7089. );
  7090. $this->assertTags($result, $expected);
  7091. }
  7092. /**
  7093. * test that create() doesn't cause errors by multiple id's being in the primary key
  7094. * as could happen with multiple select or checkboxes.
  7095. *
  7096. * @return void
  7097. */
  7098. public function testCreateWithMultipleIdInData() {
  7099. $encoding = strtolower(Configure::read('App.encoding'));
  7100. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7101. $result = $this->Form->create('Contact');
  7102. $expected = array(
  7103. 'form' => array(
  7104. 'id' => 'ContactAddForm',
  7105. 'method' => 'post',
  7106. 'action' => '/contacts/add',
  7107. 'accept-charset' => $encoding
  7108. ),
  7109. 'div' => array('style' => 'display:none;'),
  7110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7111. '/div'
  7112. );
  7113. $this->assertTags($result, $expected);
  7114. }
  7115. /**
  7116. * test that create() doesn't add in extra passed params.
  7117. *
  7118. * @return void
  7119. */
  7120. public function testCreatePassedArgs() {
  7121. $encoding = strtolower(Configure::read('App.encoding'));
  7122. $this->Form->request->data['Contact']['id'] = 1;
  7123. $result = $this->Form->create('Contact', array(
  7124. 'type' => 'post',
  7125. 'escape' => false,
  7126. 'url' => array(
  7127. 'action' => 'edit',
  7128. 'myparam'
  7129. )
  7130. ));
  7131. $expected = array(
  7132. 'form' => array(
  7133. 'id' => 'ContactAddForm',
  7134. 'method' => 'post',
  7135. 'action' => '/contacts/edit/myparam',
  7136. 'accept-charset' => $encoding
  7137. ),
  7138. 'div' => array('style' => 'display:none;'),
  7139. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7140. '/div'
  7141. );
  7142. $this->assertTags($result, $expected);
  7143. }
  7144. /**
  7145. * test creating a get form, and get form inputs.
  7146. *
  7147. * @return void
  7148. */
  7149. public function testGetFormCreate() {
  7150. $encoding = strtolower(Configure::read('App.encoding'));
  7151. $result = $this->Form->create('Contact', array('type' => 'get'));
  7152. $this->assertTags($result, array('form' => array(
  7153. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7154. 'accept-charset' => $encoding
  7155. )));
  7156. $result = $this->Form->text('Contact.name');
  7157. $this->assertTags($result, array('input' => array(
  7158. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7159. )));
  7160. $result = $this->Form->password('password');
  7161. $this->assertTags($result, array('input' => array(
  7162. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7163. )));
  7164. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7165. $result = $this->Form->text('user_form');
  7166. $this->assertTags($result, array('input' => array(
  7167. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7168. )));
  7169. }
  7170. /**
  7171. * test get form, and inputs when the model param is false
  7172. *
  7173. * @return void
  7174. */
  7175. public function testGetFormWithFalseModel() {
  7176. $encoding = strtolower(Configure::read('App.encoding'));
  7177. $this->Form->request['controller'] = 'contact_test';
  7178. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7179. $expected = array('form' => array(
  7180. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7181. 'accept-charset' => $encoding
  7182. ));
  7183. $this->assertTags($result, $expected);
  7184. $result = $this->Form->text('reason');
  7185. $expected = array(
  7186. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7187. );
  7188. $this->assertTags($result, $expected);
  7189. }
  7190. /**
  7191. * test that datetime() works with GET style forms.
  7192. *
  7193. * @return void
  7194. */
  7195. public function testDateTimeWithGetForms() {
  7196. extract($this->dateRegex);
  7197. $this->Form->create('Contact', array('type' => 'get'));
  7198. $result = $this->Form->datetime('created');
  7199. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7200. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7201. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7202. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7203. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7204. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7205. }
  7206. /**
  7207. * testEditFormWithData method
  7208. *
  7209. * test auto populating form elements from submitted data.
  7210. *
  7211. * @return void
  7212. */
  7213. public function testEditFormWithData() {
  7214. $this->Form->request->data = array('Person' => array(
  7215. 'id' => 1,
  7216. 'first_name' => 'Nate',
  7217. 'last_name' => 'Abele',
  7218. 'email' => 'nate@example.com'
  7219. ));
  7220. $this->Form->request->addParams(array(
  7221. 'models' => array('Person'),
  7222. 'controller' => 'people',
  7223. 'action' => 'add'
  7224. ));
  7225. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7226. $this->Form->create();
  7227. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7228. $expected = array(
  7229. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7230. 'select' => array(
  7231. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7232. ),
  7233. array('option' => array('value' => 1)), 'Nate', '/option',
  7234. array('option' => array('value' => 2)), 'Garrett', '/option',
  7235. array('option' => array('value' => 3)), 'Larry', '/option',
  7236. '/select'
  7237. );
  7238. $this->assertTags($result, $expected);
  7239. }
  7240. /**
  7241. * Test that required fields are created for various types of validation.
  7242. *
  7243. * @return void
  7244. */
  7245. public function testFormInputRequiredDetection() {
  7246. $this->Form->create('Contact');
  7247. $result = $this->Form->input('Contact.non_existing');
  7248. $expected = array(
  7249. 'div' => array('class' => 'input text'),
  7250. 'label' => array('for' => 'ContactNonExisting'),
  7251. 'Non Existing',
  7252. '/label',
  7253. 'input' => array(
  7254. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7255. 'id' => 'ContactNonExisting'
  7256. ),
  7257. '/div'
  7258. );
  7259. $this->assertTags($result, $expected);
  7260. $result = $this->Form->input('Contact.imrequired');
  7261. $expected = array(
  7262. 'div' => array('class' => 'input text required'),
  7263. 'label' => array('for' => 'ContactImrequired'),
  7264. 'Imrequired',
  7265. '/label',
  7266. 'input' => array(
  7267. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7268. 'id' => 'ContactImrequired',
  7269. 'required' => 'required'
  7270. ),
  7271. '/div'
  7272. );
  7273. $this->assertTags($result, $expected);
  7274. $result = $this->Form->input('Contact.imalsorequired');
  7275. $expected = array(
  7276. 'div' => array('class' => 'input text required'),
  7277. 'label' => array('for' => 'ContactImalsorequired'),
  7278. 'Imalsorequired',
  7279. '/label',
  7280. 'input' => array(
  7281. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7282. 'id' => 'ContactImalsorequired',
  7283. 'required' => 'required'
  7284. ),
  7285. '/div'
  7286. );
  7287. $this->assertTags($result, $expected);
  7288. $result = $this->Form->input('Contact.imrequiredtoo');
  7289. $expected = array(
  7290. 'div' => array('class' => 'input text required'),
  7291. 'label' => array('for' => 'ContactImrequiredtoo'),
  7292. 'Imrequiredtoo',
  7293. '/label',
  7294. 'input' => array(
  7295. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7296. 'id' => 'ContactImrequiredtoo',
  7297. 'required' => 'required'
  7298. ),
  7299. '/div'
  7300. );
  7301. $this->assertTags($result, $expected);
  7302. $result = $this->Form->input('Contact.required_one');
  7303. $expected = array(
  7304. 'div' => array('class' => 'input text required'),
  7305. 'label' => array('for' => 'ContactRequiredOne'),
  7306. 'Required One',
  7307. '/label',
  7308. 'input' => array(
  7309. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7310. 'id' => 'ContactRequiredOne',
  7311. 'required' => 'required'
  7312. ),
  7313. '/div'
  7314. );
  7315. $this->assertTags($result, $expected);
  7316. $result = $this->Form->input('Contact.string_required');
  7317. $expected = array(
  7318. 'div' => array('class' => 'input text required'),
  7319. 'label' => array('for' => 'ContactStringRequired'),
  7320. 'String Required',
  7321. '/label',
  7322. 'input' => array(
  7323. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7324. 'id' => 'ContactStringRequired',
  7325. 'required' => 'required'
  7326. ),
  7327. '/div'
  7328. );
  7329. $this->assertTags($result, $expected);
  7330. $result = $this->Form->input('Contact.imnotrequired');
  7331. $expected = array(
  7332. 'div' => array('class' => 'input text'),
  7333. 'label' => array('for' => 'ContactImnotrequired'),
  7334. 'Imnotrequired',
  7335. '/label',
  7336. 'input' => array(
  7337. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7338. 'id' => 'ContactImnotrequired'
  7339. ),
  7340. '/div'
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. $result = $this->Form->input('Contact.imalsonotrequired');
  7344. $expected = array(
  7345. 'div' => array('class' => 'input text'),
  7346. 'label' => array('for' => 'ContactImalsonotrequired'),
  7347. 'Imalsonotrequired',
  7348. '/label',
  7349. 'input' => array(
  7350. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7351. 'id' => 'ContactImalsonotrequired'
  7352. ),
  7353. '/div'
  7354. );
  7355. $this->assertTags($result, $expected);
  7356. $result = $this->Form->input('Contact.imalsonotrequired2');
  7357. $expected = array(
  7358. 'div' => array('class' => 'input text'),
  7359. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7360. 'Imalsonotrequired2',
  7361. '/label',
  7362. 'input' => array(
  7363. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7364. 'id' => 'ContactImalsonotrequired2'
  7365. ),
  7366. '/div'
  7367. );
  7368. $this->assertTags($result, $expected);
  7369. $result = $this->Form->input('Contact.imnotrequiredeither');
  7370. $expected = array(
  7371. 'div' => array('class' => 'input text'),
  7372. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7373. 'Imnotrequiredeither',
  7374. '/label',
  7375. 'input' => array(
  7376. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7377. 'id' => 'ContactImnotrequiredeither'
  7378. ),
  7379. '/div'
  7380. );
  7381. $this->assertTags($result, $expected);
  7382. $result = $this->Form->input('Contact.iamrequiredalways');
  7383. $expected = array(
  7384. 'div' => array('class' => 'input text required'),
  7385. 'label' => array('for' => 'ContactIamrequiredalways'),
  7386. 'Iamrequiredalways',
  7387. '/label',
  7388. 'input' => array(
  7389. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7390. 'id' => 'ContactIamrequiredalways',
  7391. 'required' => 'required'
  7392. ),
  7393. '/div'
  7394. );
  7395. $this->assertTags($result, $expected);
  7396. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7397. $expected = array(
  7398. 'div' => array('class' => 'input checkbox required'),
  7399. array('input' => array(
  7400. 'type' => 'hidden',
  7401. 'name' => 'data[Contact][boolean_field]',
  7402. 'id' => 'ContactBooleanField_',
  7403. 'value' => '0'
  7404. )),
  7405. array('input' => array(
  7406. 'type' => 'checkbox',
  7407. 'name' => 'data[Contact][boolean_field]',
  7408. 'value' => '1',
  7409. 'id' => 'ContactBooleanField'
  7410. )),
  7411. 'label' => array('for' => 'ContactBooleanField'),
  7412. 'Boolean Field',
  7413. '/label',
  7414. '/div'
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7418. $expected = array(
  7419. 'div' => array('class' => 'input checkbox required'),
  7420. array('input' => array(
  7421. 'type' => 'hidden',
  7422. 'name' => 'data[Contact][boolean_field]',
  7423. 'id' => 'ContactBooleanField_',
  7424. 'value' => '0'
  7425. )),
  7426. array('input' => array(
  7427. 'type' => 'checkbox',
  7428. 'name' => 'data[Contact][boolean_field]',
  7429. 'value' => '1',
  7430. 'id' => 'ContactBooleanField',
  7431. 'required' => 'required'
  7432. )),
  7433. 'label' => array('for' => 'ContactBooleanField'),
  7434. 'Boolean Field',
  7435. '/label',
  7436. '/div'
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. }
  7440. /**
  7441. * Test that required fields are created when only using ModelValidator::add().
  7442. *
  7443. * @return void
  7444. */
  7445. public function testFormInputRequiredDetectionModelValidator() {
  7446. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7447. $this->Form->create('ContactTag');
  7448. $result = $this->Form->input('ContactTag.iwillberequired');
  7449. $expected = array(
  7450. 'div' => array('class' => 'input text required'),
  7451. 'label' => array('for' => 'ContactTagIwillberequired'),
  7452. 'Iwillberequired',
  7453. '/label',
  7454. 'input' => array(
  7455. 'name' => 'data[ContactTag][iwillberequired]',
  7456. 'type' => 'text',
  7457. 'id' => 'ContactTagIwillberequired',
  7458. 'required' => 'required'
  7459. ),
  7460. '/div'
  7461. );
  7462. $this->assertTags($result, $expected);
  7463. }
  7464. /**
  7465. * testFormMagicInput method
  7466. *
  7467. * @return void
  7468. */
  7469. public function testFormMagicInput() {
  7470. $encoding = strtolower(Configure::read('App.encoding'));
  7471. $result = $this->Form->create('Contact');
  7472. $expected = array(
  7473. 'form' => array(
  7474. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7475. 'accept-charset' => $encoding
  7476. ),
  7477. 'div' => array('style' => 'display:none;'),
  7478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7479. '/div'
  7480. );
  7481. $this->assertTags($result, $expected);
  7482. $result = $this->Form->input('name');
  7483. $expected = array(
  7484. 'div' => array('class' => 'input text'),
  7485. 'label' => array('for' => 'ContactName'),
  7486. 'Name',
  7487. '/label',
  7488. 'input' => array(
  7489. 'type' => 'text', 'name' => 'data[Contact][name]',
  7490. 'id' => 'ContactName', 'maxlength' => '255'
  7491. ),
  7492. '/div'
  7493. );
  7494. $this->assertTags($result, $expected);
  7495. $result = $this->Form->input('non_existing_field_in_contact_model');
  7496. $expected = array(
  7497. 'div' => array('class' => 'input text'),
  7498. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7499. 'Non Existing Field In Contact Model',
  7500. '/label',
  7501. 'input' => array(
  7502. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7503. 'id' => 'ContactNonExistingFieldInContactModel'
  7504. ),
  7505. '/div'
  7506. );
  7507. $this->assertTags($result, $expected);
  7508. $result = $this->Form->input('Address.street');
  7509. $expected = array(
  7510. 'div' => array('class' => 'input text'),
  7511. 'label' => array('for' => 'AddressStreet'),
  7512. 'Street',
  7513. '/label',
  7514. 'input' => array(
  7515. 'type' => 'text', 'name' => 'data[Address][street]',
  7516. 'id' => 'AddressStreet'
  7517. ),
  7518. '/div'
  7519. );
  7520. $this->assertTags($result, $expected);
  7521. $result = $this->Form->input('Address.non_existing_field_in_model');
  7522. $expected = array(
  7523. 'div' => array('class' => 'input text'),
  7524. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7525. 'Non Existing Field In Model',
  7526. '/label',
  7527. 'input' => array(
  7528. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7529. 'id' => 'AddressNonExistingFieldInModel'
  7530. ),
  7531. '/div'
  7532. );
  7533. $this->assertTags($result, $expected);
  7534. $result = $this->Form->input('name', array('div' => false));
  7535. $expected = array(
  7536. 'label' => array('for' => 'ContactName'),
  7537. 'Name',
  7538. '/label',
  7539. 'input' => array(
  7540. 'type' => 'text', 'name' => 'data[Contact][name]',
  7541. 'id' => 'ContactName', 'maxlength' => '255'
  7542. )
  7543. );
  7544. $this->assertTags($result, $expected);
  7545. extract($this->dateRegex);
  7546. $now = strtotime('now');
  7547. $result = $this->Form->input('Contact.published', array('div' => false));
  7548. $expected = array(
  7549. 'label' => array('for' => 'ContactPublishedMonth'),
  7550. 'Published',
  7551. '/label',
  7552. array('select' => array(
  7553. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7554. )),
  7555. $monthsRegex,
  7556. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7557. date('F', $now),
  7558. '/option',
  7559. '*/select',
  7560. '-',
  7561. array('select' => array(
  7562. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7563. )),
  7564. $daysRegex,
  7565. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7566. date('j', $now),
  7567. '/option',
  7568. '*/select',
  7569. '-',
  7570. array('select' => array(
  7571. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7572. )),
  7573. $yearsRegex,
  7574. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7575. date('Y', $now),
  7576. '*/select'
  7577. );
  7578. $this->assertTags($result, $expected);
  7579. $result = $this->Form->input('Contact.updated', array('div' => false));
  7580. $expected = array(
  7581. 'label' => array('for' => 'ContactUpdatedMonth'),
  7582. 'Updated',
  7583. '/label',
  7584. array('select' => array(
  7585. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7586. )),
  7587. $monthsRegex,
  7588. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7589. date('F', $now),
  7590. '/option',
  7591. '*/select',
  7592. '-',
  7593. array('select' => array(
  7594. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7595. )),
  7596. $daysRegex,
  7597. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7598. date('j', $now),
  7599. '/option',
  7600. '*/select',
  7601. '-',
  7602. array('select' => array(
  7603. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7604. )),
  7605. $yearsRegex,
  7606. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7607. date('Y', $now),
  7608. '*/select'
  7609. );
  7610. $this->assertTags($result, $expected);
  7611. $result = $this->Form->input('UserForm.stuff');
  7612. $expected = array(
  7613. 'div' => array('class' => 'input text'),
  7614. 'label' => array('for' => 'UserFormStuff'),
  7615. 'Stuff',
  7616. '/label',
  7617. 'input' => array(
  7618. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7619. 'id' => 'UserFormStuff', 'maxlength' => 10
  7620. ),
  7621. '/div'
  7622. );
  7623. $this->assertTags($result, $expected);
  7624. }
  7625. /**
  7626. * testForMagicInputNonExistingNorValidated method
  7627. *
  7628. * @return void
  7629. */
  7630. public function testForMagicInputNonExistingNorValidated() {
  7631. $encoding = strtolower(Configure::read('App.encoding'));
  7632. $result = $this->Form->create('Contact');
  7633. $expected = array(
  7634. 'form' => array(
  7635. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7636. 'accept-charset' => $encoding
  7637. ),
  7638. 'div' => array('style' => 'display:none;'),
  7639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7640. '/div'
  7641. );
  7642. $this->assertTags($result, $expected);
  7643. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7644. $expected = array(
  7645. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7646. 'Non Existing Nor Validated',
  7647. '/label',
  7648. 'input' => array(
  7649. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7650. 'id' => 'ContactNonExistingNorValidated'
  7651. )
  7652. );
  7653. $this->assertTags($result, $expected);
  7654. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7655. 'div' => false, 'value' => 'my value'
  7656. ));
  7657. $expected = array(
  7658. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7659. 'Non Existing Nor Validated',
  7660. '/label',
  7661. 'input' => array(
  7662. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7663. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7664. )
  7665. );
  7666. $this->assertTags($result, $expected);
  7667. $this->Form->request->data = array(
  7668. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7669. ));
  7670. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7671. $expected = array(
  7672. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7673. 'Non Existing Nor Validated',
  7674. '/label',
  7675. 'input' => array(
  7676. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7677. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7678. )
  7679. );
  7680. $this->assertTags($result, $expected);
  7681. }
  7682. /**
  7683. * testFormMagicInputLabel method
  7684. *
  7685. * @return void
  7686. */
  7687. public function testFormMagicInputLabel() {
  7688. $encoding = strtolower(Configure::read('App.encoding'));
  7689. $result = $this->Form->create('Contact');
  7690. $expected = array(
  7691. 'form' => array(
  7692. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7693. 'accept-charset' => $encoding
  7694. ),
  7695. 'div' => array('style' => 'display:none;'),
  7696. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7697. '/div'
  7698. );
  7699. $this->assertTags($result, $expected);
  7700. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7701. $this->assertTags($result, array('input' => array(
  7702. 'name' => 'data[Contact][name]', 'type' => 'text',
  7703. 'id' => 'ContactName', 'maxlength' => '255')
  7704. ));
  7705. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7706. $expected = array(
  7707. 'label' => array('for' => 'ContactName'),
  7708. 'My label',
  7709. '/label',
  7710. 'input' => array(
  7711. 'type' => 'text', 'name' => 'data[Contact][name]',
  7712. 'id' => 'ContactName', 'maxlength' => '255'
  7713. )
  7714. );
  7715. $this->assertTags($result, $expected);
  7716. $result = $this->Form->input('Contact.name', array(
  7717. 'div' => false, 'label' => array('class' => 'mandatory')
  7718. ));
  7719. $expected = array(
  7720. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7721. 'Name',
  7722. '/label',
  7723. 'input' => array(
  7724. 'type' => 'text', 'name' => 'data[Contact][name]',
  7725. 'id' => 'ContactName', 'maxlength' => '255'
  7726. )
  7727. );
  7728. $this->assertTags($result, $expected);
  7729. $result = $this->Form->input('Contact.name', array(
  7730. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7731. ));
  7732. $expected = array(
  7733. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7734. 'My label',
  7735. '/label',
  7736. 'input' => array(
  7737. 'type' => 'text', 'name' => 'data[Contact][name]',
  7738. 'id' => 'ContactName', 'maxlength' => '255'
  7739. )
  7740. );
  7741. $this->assertTags($result, $expected);
  7742. $result = $this->Form->input('Contact.name', array(
  7743. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7744. ));
  7745. $expected = array(
  7746. 'label' => array('for' => 'my_id'),
  7747. 'Name',
  7748. '/label',
  7749. 'input' => array(
  7750. 'type' => 'text', 'name' => 'data[Contact][name]',
  7751. 'id' => 'my_id', 'maxlength' => '255'
  7752. )
  7753. );
  7754. $this->assertTags($result, $expected);
  7755. $result = $this->Form->input('1.id');
  7756. $this->assertTags($result, array('input' => array(
  7757. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7758. 'id' => 'Contact1Id'
  7759. )));
  7760. $result = $this->Form->input("1.name");
  7761. $expected = array(
  7762. 'div' => array('class' => 'input text'),
  7763. 'label' => array('for' => 'Contact1Name'),
  7764. 'Name',
  7765. '/label',
  7766. 'input' => array(
  7767. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7768. 'id' => 'Contact1Name', 'maxlength' => '255'
  7769. ),
  7770. '/div'
  7771. );
  7772. $this->assertTags($result, $expected);
  7773. $result = $this->Form->input('Contact.1.id');
  7774. $this->assertTags($result, array(
  7775. 'input' => array(
  7776. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7777. 'id' => 'Contact1Id'
  7778. )
  7779. ));
  7780. $result = $this->Form->input("Model.1.name");
  7781. $expected = array(
  7782. 'div' => array('class' => 'input text'),
  7783. 'label' => array('for' => 'Model1Name'),
  7784. 'Name',
  7785. '/label',
  7786. 'input' => array(
  7787. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7788. 'id' => 'Model1Name'
  7789. ),
  7790. '/div'
  7791. );
  7792. $this->assertTags($result, $expected);
  7793. }
  7794. /**
  7795. * testFormEnd method
  7796. *
  7797. * @return void
  7798. */
  7799. public function testFormEnd() {
  7800. $this->assertEquals('</form>', $this->Form->end());
  7801. $result = $this->Form->end('');
  7802. $expected = array(
  7803. 'div' => array('class' => 'submit'),
  7804. 'input' => array('type' => 'submit', 'value' => ''),
  7805. '/div',
  7806. '/form'
  7807. );
  7808. $this->assertTags($result, $expected);
  7809. $result = $this->Form->end(array('label' => ''));
  7810. $expected = array(
  7811. 'div' => array('class' => 'submit'),
  7812. 'input' => array('type' => 'submit', 'value' => ''),
  7813. '/div',
  7814. '/form'
  7815. );
  7816. $this->assertTags($result, $expected);
  7817. $result = $this->Form->end('save');
  7818. $expected = array(
  7819. 'div' => array('class' => 'submit'),
  7820. 'input' => array('type' => 'submit', 'value' => 'save'),
  7821. '/div',
  7822. '/form'
  7823. );
  7824. $this->assertTags($result, $expected);
  7825. $result = $this->Form->end(array('label' => 'save'));
  7826. $expected = array(
  7827. 'div' => array('class' => 'submit'),
  7828. 'input' => array('type' => 'submit', 'value' => 'save'),
  7829. '/div',
  7830. '/form'
  7831. );
  7832. $this->assertTags($result, $expected);
  7833. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7834. $expected = array(
  7835. 'div' => array('class' => 'submit'),
  7836. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7837. '/div',
  7838. '/form'
  7839. );
  7840. $this->assertTags($result, $expected);
  7841. $result = $this->Form->end(array('name' => 'Whatever'));
  7842. $expected = array(
  7843. 'div' => array('class' => 'submit'),
  7844. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7845. '/div',
  7846. '/form'
  7847. );
  7848. $this->assertTags($result, $expected);
  7849. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7850. $expected = array(
  7851. 'div' => array('class' => 'good'),
  7852. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7853. '/div',
  7854. '/form'
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. $result = $this->Form->end(array(
  7858. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7859. ));
  7860. $expected = array(
  7861. 'div' => array('class' => 'good'),
  7862. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7863. '/div',
  7864. '/form'
  7865. );
  7866. $this->assertTags($result, $expected);
  7867. }
  7868. /**
  7869. * testMultipleFormWithIdFields method
  7870. *
  7871. * @return void
  7872. */
  7873. public function testMultipleFormWithIdFields() {
  7874. $this->Form->create('UserForm');
  7875. $result = $this->Form->input('id');
  7876. $this->assertTags($result, array('input' => array(
  7877. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7878. )));
  7879. $result = $this->Form->input('ValidateItem.id');
  7880. $this->assertTags($result, array('input' => array(
  7881. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7882. 'id' => 'ValidateItemId'
  7883. )));
  7884. $result = $this->Form->input('ValidateUser.id');
  7885. $this->assertTags($result, array('input' => array(
  7886. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7887. 'id' => 'ValidateUserId'
  7888. )));
  7889. }
  7890. /**
  7891. * testDbLessModel method
  7892. *
  7893. * @return void
  7894. */
  7895. public function testDbLessModel() {
  7896. $this->Form->create('TestMail');
  7897. $result = $this->Form->input('name');
  7898. $expected = array(
  7899. 'div' => array('class' => 'input text'),
  7900. 'label' => array('for' => 'TestMailName'),
  7901. 'Name',
  7902. '/label',
  7903. 'input' => array(
  7904. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7905. 'id' => 'TestMailName'
  7906. ),
  7907. '/div'
  7908. );
  7909. $this->assertTags($result, $expected);
  7910. ClassRegistry::init('TestMail');
  7911. $this->Form->create('TestMail');
  7912. $result = $this->Form->input('name');
  7913. $expected = array(
  7914. 'div' => array('class' => 'input text'),
  7915. 'label' => array('for' => 'TestMailName'),
  7916. 'Name',
  7917. '/label',
  7918. 'input' => array(
  7919. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7920. 'id' => 'TestMailName'
  7921. ),
  7922. '/div'
  7923. );
  7924. $this->assertTags($result, $expected);
  7925. }
  7926. /**
  7927. * testBrokenness method
  7928. *
  7929. * @return void
  7930. */
  7931. public function testBrokenness() {
  7932. /*
  7933. * #4 This test has two parents and four children. By default (as of r7117) both
  7934. * parents are show but the first parent is missing a child. This is the inconsistency
  7935. * in the default behaviour - one parent has all children, the other does not - dependent
  7936. * on the data values.
  7937. */
  7938. $result = $this->Form->select('Model.field', array(
  7939. 'Fred' => array(
  7940. 'freds_son_1' => 'Fred',
  7941. 'freds_son_2' => 'Freddie'
  7942. ),
  7943. 'Bert' => array(
  7944. 'berts_son_1' => 'Albert',
  7945. 'berts_son_2' => 'Bertie')
  7946. ),
  7947. array('showParents' => true, 'empty' => false)
  7948. );
  7949. $expected = array(
  7950. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7951. array('optgroup' => array('label' => 'Fred')),
  7952. array('option' => array('value' => 'freds_son_1')),
  7953. 'Fred',
  7954. '/option',
  7955. array('option' => array('value' => 'freds_son_2')),
  7956. 'Freddie',
  7957. '/option',
  7958. '/optgroup',
  7959. array('optgroup' => array('label' => 'Bert')),
  7960. array('option' => array('value' => 'berts_son_1')),
  7961. 'Albert',
  7962. '/option',
  7963. array('option' => array('value' => 'berts_son_2')),
  7964. 'Bertie',
  7965. '/option',
  7966. '/optgroup',
  7967. '/select'
  7968. );
  7969. $this->assertTags($result, $expected);
  7970. /*
  7971. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7972. * changed, so we should expect the same select list data, just with a different name
  7973. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7974. * This is where data corruption can occur, because when a select value is missing from
  7975. * a list a form will substitute the first value in the list - without the user knowing.
  7976. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7977. * affect the availability of 3 => 'Three' as a valid option.
  7978. */
  7979. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7980. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7981. ));
  7982. $result = $this->Form->select(
  7983. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7984. );
  7985. $expected = array(
  7986. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7987. array('option' => array('value' => 1)),
  7988. 'One',
  7989. '/option',
  7990. array('option' => array('value' => 2)),
  7991. 'Two',
  7992. '/option',
  7993. array('optgroup' => array('label' => 'Three')),
  7994. array('option' => array('value' => 3)),
  7995. 'Three',
  7996. '/option',
  7997. array('option' => array('value' => 4)),
  7998. 'Four',
  7999. '/option',
  8000. array('option' => array('value' => 5)),
  8001. 'Five',
  8002. '/option',
  8003. '/optgroup',
  8004. '/select'
  8005. );
  8006. $this->assertTags($result, $expected);
  8007. }
  8008. /**
  8009. * Test the generation of fields for a multi record form.
  8010. *
  8011. * @return void
  8012. */
  8013. public function testMultiRecordForm() {
  8014. $this->Form->create('ValidateProfile');
  8015. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8016. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8017. $expected = array(
  8018. 'div' => array('class' => 'input textarea'),
  8019. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8020. 'Name',
  8021. '/label',
  8022. 'textarea' => array(
  8023. 'id' => 'ValidateProfile1ValidateItem2Name',
  8024. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8025. 'cols' => 30,
  8026. 'rows' => 6
  8027. ),
  8028. 'Value',
  8029. '/textarea',
  8030. '/div'
  8031. );
  8032. $this->assertTags($result, $expected);
  8033. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8034. $expected = array(
  8035. 'div' => array('class' => 'input date'),
  8036. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8037. 'Created',
  8038. '/label',
  8039. array('select' => array(
  8040. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8041. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8042. )
  8043. ),
  8044. array('option' => array('value' => '')), '/option',
  8045. $this->dateRegex['monthsRegex'],
  8046. '/select', '-',
  8047. array('select' => array(
  8048. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8049. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8050. )
  8051. ),
  8052. array('option' => array('value' => '')), '/option',
  8053. $this->dateRegex['daysRegex'],
  8054. '/select', '-',
  8055. array('select' => array(
  8056. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8057. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8058. )
  8059. ),
  8060. array('option' => array('value' => '')), '/option',
  8061. $this->dateRegex['yearsRegex'],
  8062. '/select',
  8063. '/div'
  8064. );
  8065. $this->assertTags($result, $expected);
  8066. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8067. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8068. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8069. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8070. $expected = array(
  8071. 'div' => array('class' => 'input select error'),
  8072. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8073. 'Profile',
  8074. '/label',
  8075. 'select' => array(
  8076. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8077. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8078. 'class' => 'form-error'
  8079. ),
  8080. '/select',
  8081. array('div' => array('class' => 'error-message')),
  8082. 'Error',
  8083. '/div',
  8084. '/div'
  8085. );
  8086. $this->assertTags($result, $expected);
  8087. }
  8088. /**
  8089. * test the correct display of multi-record form validation errors.
  8090. *
  8091. * @return void
  8092. */
  8093. public function testMultiRecordFormValidationErrors() {
  8094. $this->Form->create('ValidateProfile');
  8095. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8096. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8097. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8098. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8099. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8100. $result = $this->Form->error('ValidateProfile.2.city');
  8101. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8102. $result = $this->Form->error('2.city');
  8103. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8104. }
  8105. /**
  8106. * test the correct display of multi-record form validation errors.
  8107. *
  8108. * @return void
  8109. */
  8110. public function testSaveManyRecordFormValidationErrors() {
  8111. $this->Form->create('ValidateUser');
  8112. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8113. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8114. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8115. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8116. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8117. $result = $this->Form->error('ValidateUser.0.city');
  8118. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8119. }
  8120. /**
  8121. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8122. *
  8123. * @return void
  8124. */
  8125. public function testInputTemplate() {
  8126. $result = $this->Form->input('Contact.email', array(
  8127. 'type' => 'text', 'format' => array('input')
  8128. ));
  8129. $expected = array(
  8130. 'div' => array('class' => 'input text'),
  8131. 'input' => array(
  8132. 'type' => 'text', 'name' => 'data[Contact][email]',
  8133. 'id' => 'ContactEmail'
  8134. ),
  8135. '/div'
  8136. );
  8137. $this->assertTags($result, $expected);
  8138. $result = $this->Form->input('Contact.email', array(
  8139. 'type' => 'text', 'format' => array('input', 'label'),
  8140. 'label' => '<em>Email (required)</em>'
  8141. ));
  8142. $expected = array(
  8143. 'div' => array('class' => 'input text'),
  8144. array('input' => array(
  8145. 'type' => 'text', 'name' => 'data[Contact][email]',
  8146. 'id' => 'ContactEmail'
  8147. )),
  8148. 'label' => array('for' => 'ContactEmail'),
  8149. 'em' => array(),
  8150. 'Email (required)',
  8151. '/em',
  8152. '/label',
  8153. '/div'
  8154. );
  8155. $this->assertTags($result, $expected);
  8156. $result = $this->Form->input('Contact.email', array(
  8157. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8158. 'between' => '<div>Something in the middle</div>',
  8159. 'after' => '<span>Some text at the end</span>'
  8160. ));
  8161. $expected = array(
  8162. 'div' => array('class' => 'input text'),
  8163. array('input' => array(
  8164. 'type' => 'text', 'name' => 'data[Contact][email]',
  8165. 'id' => 'ContactEmail'
  8166. )),
  8167. array('div' => array()),
  8168. 'Something in the middle',
  8169. '/div',
  8170. 'label' => array('for' => 'ContactEmail'),
  8171. 'Email',
  8172. '/label',
  8173. 'span' => array(),
  8174. 'Some text at the end',
  8175. '/span',
  8176. '/div'
  8177. );
  8178. $this->assertTags($result, $expected);
  8179. $result = $this->Form->input('Contact.method', array(
  8180. 'type' => 'radio',
  8181. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8182. 'between' => 'I am between',
  8183. ));
  8184. $expected = array(
  8185. 'div' => array('class' => 'input radio'),
  8186. 'fieldset' => array(),
  8187. 'legend' => array(),
  8188. 'Method',
  8189. '/legend',
  8190. 'I am between',
  8191. 'input' => array(
  8192. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8193. 'value' => '', 'id' => 'ContactMethod_'
  8194. ),
  8195. array('input' => array(
  8196. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8197. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8198. )),
  8199. array('label' => array('for' => 'ContactMethodEmail')),
  8200. 'Email',
  8201. '/label',
  8202. array('input' => array(
  8203. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8204. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8205. )),
  8206. array('label' => array('for' => 'ContactMethodPigeon')),
  8207. 'Pigeon',
  8208. '/label',
  8209. '/fieldset',
  8210. '/div',
  8211. );
  8212. $this->assertTags($result, $expected);
  8213. }
  8214. /**
  8215. * test that some html5 inputs + FormHelper::__call() work
  8216. *
  8217. * @return void
  8218. */
  8219. public function testHtml5Inputs() {
  8220. $result = $this->Form->email('User.email');
  8221. $expected = array(
  8222. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8223. );
  8224. $this->assertTags($result, $expected);
  8225. $result = $this->Form->search('User.query');
  8226. $expected = array(
  8227. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8228. );
  8229. $this->assertTags($result, $expected);
  8230. $result = $this->Form->search('User.query', array('value' => 'test'));
  8231. $expected = array(
  8232. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8233. );
  8234. $this->assertTags($result, $expected);
  8235. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8236. $expected = array(
  8237. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8238. );
  8239. $this->assertTags($result, $expected);
  8240. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8241. $expected = array(
  8242. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8243. );
  8244. $this->assertTags($result, $expected);
  8245. }
  8246. /**
  8247. *
  8248. * @expectedException CakeException
  8249. * @return void
  8250. */
  8251. public function testHtml5InputException() {
  8252. $this->Form->email();
  8253. }
  8254. /**
  8255. * Tests that a model can be loaded from the model names passed in the request object
  8256. *
  8257. * @return void
  8258. */
  8259. public function testIntrospectModelFromRequest() {
  8260. $this->loadFixtures('Post');
  8261. App::build(array(
  8262. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8263. ));
  8264. CakePlugin::load('TestPlugin');
  8265. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8266. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8267. $this->Form->create('TestPluginPost');
  8268. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8269. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8270. CakePlugin::unload();
  8271. App::build();
  8272. }
  8273. /**
  8274. * Tests that it is possible to set the validation errors directly in the helper for a field
  8275. *
  8276. * @return void
  8277. */
  8278. public function testCustomValidationErrors() {
  8279. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8280. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8281. $this->assertEquals('Badness!', $result);
  8282. }
  8283. /**
  8284. * Tests that the 'on' key validates as expected on create
  8285. *
  8286. * @return void
  8287. */
  8288. public function testRequiredOnCreate() {
  8289. $this->Form->create('Contact');
  8290. $result = $this->Form->input('Contact.imrequiredonupdate');
  8291. $expected = array(
  8292. 'div' => array('class' => 'input text'),
  8293. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8294. 'Imrequiredonupdate',
  8295. '/label',
  8296. 'input' => array(
  8297. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8298. 'id' => 'ContactImrequiredonupdate'
  8299. ),
  8300. '/div'
  8301. );
  8302. $this->assertTags($result, $expected);
  8303. $result = $this->Form->input('Contact.imrequiredoncreate');
  8304. $expected = array(
  8305. 'div' => array('class' => 'input text required'),
  8306. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8307. 'Imrequiredoncreate',
  8308. '/label',
  8309. 'input' => array(
  8310. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8311. 'id' => 'ContactImrequiredoncreate',
  8312. 'required' => 'required'
  8313. ),
  8314. '/div'
  8315. );
  8316. $this->assertTags($result, $expected);
  8317. $result = $this->Form->input('Contact.imrequiredonboth');
  8318. $expected = array(
  8319. 'div' => array('class' => 'input text required'),
  8320. 'label' => array('for' => 'ContactImrequiredonboth'),
  8321. 'Imrequiredonboth',
  8322. '/label',
  8323. 'input' => array(
  8324. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8325. 'id' => 'ContactImrequiredonboth',
  8326. 'required' => 'required'
  8327. ),
  8328. '/div'
  8329. );
  8330. $this->assertTags($result, $expected);
  8331. $this->Form->inputDefaults(array('required' => false));
  8332. $result = $this->Form->input('Contact.imrequired');
  8333. $expected = array(
  8334. 'div' => array('class' => 'input text'),
  8335. 'label' => array('for' => 'ContactImrequired'),
  8336. 'Imrequired',
  8337. '/label',
  8338. 'input' => array(
  8339. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8340. 'id' => 'ContactImrequired'
  8341. ),
  8342. '/div'
  8343. );
  8344. $this->assertTags($result, $expected);
  8345. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8346. $this->assertTags($result, $expected);
  8347. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8348. $expected = array(
  8349. 'div' => array('class' => 'input text required'),
  8350. 'label' => array('for' => 'ContactImrequired'),
  8351. 'Imrequired',
  8352. '/label',
  8353. 'input' => array(
  8354. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8355. 'id' => 'ContactImrequired'
  8356. ),
  8357. '/div'
  8358. );
  8359. $this->assertTags($result, $expected);
  8360. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8361. $this->assertTags($result, $expected);
  8362. }
  8363. /**
  8364. * Tests that the 'on' key validates as expected on update
  8365. *
  8366. * @return void
  8367. */
  8368. public function testRequiredOnUpdate() {
  8369. $this->Form->request->data['Contact']['id'] = 1;
  8370. $this->Form->create('Contact');
  8371. $result = $this->Form->input('Contact.imrequiredonupdate');
  8372. $expected = array(
  8373. 'div' => array('class' => 'input text required'),
  8374. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8375. 'Imrequiredonupdate',
  8376. '/label',
  8377. 'input' => array(
  8378. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8379. 'id' => 'ContactImrequiredonupdate',
  8380. 'required' => 'required'
  8381. ),
  8382. '/div'
  8383. );
  8384. $this->assertTags($result, $expected);
  8385. $result = $this->Form->input('Contact.imrequiredoncreate');
  8386. $expected = array(
  8387. 'div' => array('class' => 'input text'),
  8388. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8389. 'Imrequiredoncreate',
  8390. '/label',
  8391. 'input' => array(
  8392. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8393. 'id' => 'ContactImrequiredoncreate'
  8394. ),
  8395. '/div'
  8396. );
  8397. $this->assertTags($result, $expected);
  8398. $result = $this->Form->input('Contact.imrequiredonboth');
  8399. $expected = array(
  8400. 'div' => array('class' => 'input text required'),
  8401. 'label' => array('for' => 'ContactImrequiredonboth'),
  8402. 'Imrequiredonboth',
  8403. '/label',
  8404. 'input' => array(
  8405. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8406. 'id' => 'ContactImrequiredonboth',
  8407. 'required' => 'required'
  8408. ),
  8409. '/div'
  8410. );
  8411. $this->assertTags($result, $expected);
  8412. $result = $this->Form->input('Contact.imrequired');
  8413. $expected = array(
  8414. 'div' => array('class' => 'input text required'),
  8415. 'label' => array('for' => 'ContactImrequired'),
  8416. 'Imrequired',
  8417. '/label',
  8418. 'input' => array(
  8419. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8420. 'id' => 'ContactImrequired',
  8421. 'required' => 'required'
  8422. ),
  8423. '/div'
  8424. );
  8425. $this->assertTags($result, $expected);
  8426. }
  8427. /**
  8428. * Test inputDefaults setter and getter
  8429. *
  8430. * @return void
  8431. */
  8432. public function testInputDefaults() {
  8433. $this->Form->create('Contact');
  8434. $this->Form->inputDefaults(array(
  8435. 'label' => false,
  8436. 'div' => array(
  8437. 'style' => 'color: #000;'
  8438. )
  8439. ));
  8440. $result = $this->Form->input('Contact.field1');
  8441. $expected = array(
  8442. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8443. 'input' => array(
  8444. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8445. 'id' => 'ContactField1'
  8446. ),
  8447. '/div'
  8448. );
  8449. $this->assertTags($result, $expected);
  8450. $this->Form->inputDefaults(array(
  8451. 'div' => false,
  8452. 'label' => 'Label',
  8453. ));
  8454. $result = $this->Form->input('Contact.field1');
  8455. $expected = array(
  8456. 'label' => array('for' => 'ContactField1'),
  8457. 'Label',
  8458. '/label',
  8459. 'input' => array(
  8460. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8461. 'id' => 'ContactField1'
  8462. ),
  8463. );
  8464. $this->assertTags($result, $expected);
  8465. $this->Form->inputDefaults(array(
  8466. 'label' => false,
  8467. ), true);
  8468. $result = $this->Form->input('Contact.field1');
  8469. $expected = array(
  8470. 'input' => array(
  8471. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8472. 'id' => 'ContactField1'
  8473. ),
  8474. );
  8475. $this->assertTags($result, $expected);
  8476. $result = $this->Form->inputDefaults();
  8477. $expected = array(
  8478. 'div' => false,
  8479. 'label' => false,
  8480. );
  8481. $this->assertEqual($result, $expected);
  8482. }
  8483. }