FormHelperTest.php 220 KB

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