FormHelperTest.php 289 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package Cake.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var bool false
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var bool false
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var bool false
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var bool false
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var bool false
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  321. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  322. );
  323. /**
  324. * beforeValidate method
  325. *
  326. * @return void
  327. */
  328. public function beforeValidate($options = array()) {
  329. $this->invalidate('email');
  330. return false;
  331. }
  332. }
  333. /**
  334. * ValidateProfile class
  335. *
  336. * @package Cake.Test.Case.View.Helper
  337. */
  338. class ValidateProfile extends CakeTestModel {
  339. /**
  340. * useTable property
  341. *
  342. * @var bool false
  343. */
  344. public $useTable = false;
  345. /**
  346. * schema property
  347. *
  348. * @var array
  349. */
  350. protected $_schema = array(
  351. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  356. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  357. );
  358. /**
  359. * hasOne property
  360. *
  361. * @var array
  362. */
  363. public $hasOne = array('ValidateItem' => array(
  364. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  365. ));
  366. /**
  367. * belongsTo property
  368. *
  369. * @var array
  370. */
  371. public $belongsTo = array('ValidateUser' => array(
  372. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * beforeValidate method
  376. *
  377. * @return void
  378. */
  379. public function beforeValidate($options = array()) {
  380. $this->invalidate('full_name');
  381. $this->invalidate('city');
  382. return false;
  383. }
  384. }
  385. /**
  386. * ValidateItem class
  387. *
  388. * @package Cake.Test.Case.View.Helper
  389. */
  390. class ValidateItem extends CakeTestModel {
  391. /**
  392. * useTable property
  393. *
  394. * @var bool false
  395. */
  396. public $useTable = false;
  397. /**
  398. * schema property
  399. *
  400. * @var array
  401. */
  402. protected $_schema = array(
  403. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  406. 'description' => array(
  407. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  408. ),
  409. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  410. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  411. );
  412. /**
  413. * belongsTo property
  414. *
  415. * @var array
  416. */
  417. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  418. /**
  419. * beforeValidate method
  420. *
  421. * @return void
  422. */
  423. public function beforeValidate($options = array()) {
  424. $this->invalidate('description');
  425. return false;
  426. }
  427. }
  428. /**
  429. * TestMail class
  430. *
  431. * @package Cake.Test.Case.View.Helper
  432. */
  433. class TestMail extends CakeTestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var bool false
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @package Cake.Test.Case.View.Helper
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends CakeTestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. Configure::write('Config.language', 'eng');
  468. Configure::write('App.base', '');
  469. Configure::delete('Asset');
  470. $this->Controller = new ContactTestController();
  471. $this->View = new View($this->Controller);
  472. $this->Form = new FormHelper($this->View);
  473. $this->Form->Html = new HtmlHelper($this->View);
  474. $this->Form->request = new CakeRequest('contacts/add', false);
  475. $this->Form->request->here = '/contacts/add';
  476. $this->Form->request['action'] = 'add';
  477. $this->Form->request->webroot = '';
  478. $this->Form->request->base = '';
  479. ClassRegistry::addObject('Contact', new Contact());
  480. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  481. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  482. ClassRegistry::addObject('User', new UserForm());
  483. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  484. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  485. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  486. $this->oldSalt = Configure::read('Security.salt');
  487. $this->dateRegex = array(
  488. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  490. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  493. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  494. );
  495. Configure::write('Security.salt', 'foo!');
  496. }
  497. /**
  498. * tearDown method
  499. *
  500. * @return void
  501. */
  502. public function tearDown() {
  503. parent::tearDown();
  504. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  505. Configure::write('Security.salt', $this->oldSalt);
  506. }
  507. /**
  508. * testFormCreateWithSecurity method
  509. *
  510. * Test form->create() with security key.
  511. *
  512. * @return void
  513. */
  514. public function testCreateWithSecurity() {
  515. $this->Form->request['_Token'] = array('key' => 'testKey');
  516. $encoding = strtolower(Configure::read('App.encoding'));
  517. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  518. $expected = array(
  519. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  520. 'div' => array('style' => 'display:none;'),
  521. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  522. array('input' => array(
  523. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  524. )),
  525. '/div'
  526. );
  527. $this->assertTags($result, $expected);
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  529. $expected['form']['id'] = 'MyForm';
  530. $this->assertTags($result, $expected);
  531. }
  532. /**
  533. * testFormCreateGetNoSecurity method
  534. *
  535. * Test form->create() with no security key as its a get form
  536. *
  537. * @return void
  538. */
  539. public function testCreateEndGetNoSecurity() {
  540. $this->Form->request['_Token'] = array('key' => 'testKey');
  541. $encoding = strtolower(Configure::read('App.encoding'));
  542. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  543. $this->assertNotContains('Token', $result);
  544. $result = $this->Form->end('Save');
  545. $this->assertNotContains('Token', $result);
  546. }
  547. /**
  548. * test that create() clears the fields property so it starts fresh
  549. *
  550. * @return void
  551. */
  552. public function testCreateClearingFields() {
  553. $this->Form->fields = array('model_id');
  554. $this->Form->create('Contact');
  555. $this->assertEquals(array(), $this->Form->fields);
  556. }
  557. /**
  558. * Tests form hash generation with model-less data
  559. *
  560. * @return void
  561. */
  562. public function testValidateHashNoModel() {
  563. $this->Form->request['_Token'] = array('key' => 'foo');
  564. $result = $this->Form->secure(array('anything'));
  565. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  566. }
  567. /**
  568. * Tests that models with identical field names get resolved properly
  569. *
  570. * @return void
  571. */
  572. public function testDuplicateFieldNameResolution() {
  573. $result = $this->Form->create('ValidateUser');
  574. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateItem.name');
  576. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  577. $result = $this->Form->input('ValidateUser.name');
  578. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  579. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  580. $this->assertRegExp('/type="text"/', $result);
  581. $result = $this->Form->input('ValidateItem.name');
  582. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  583. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  584. $this->assertRegExp('/<textarea/', $result);
  585. $result = $this->Form->input('name');
  586. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  587. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  588. $this->assertRegExp('/type="text"/', $result);
  589. }
  590. /**
  591. * Tests that hidden fields generated for checkboxes don't get locked
  592. *
  593. * @return void
  594. */
  595. public function testNoCheckboxLocking() {
  596. $this->Form->request['_Token'] = array('key' => 'foo');
  597. $this->assertSame(array(), $this->Form->fields);
  598. $this->Form->checkbox('check', array('value' => '1'));
  599. $this->assertSame($this->Form->fields, array('check'));
  600. }
  601. /**
  602. * testFormSecurityFields method
  603. *
  604. * Test generation of secure form hash generation.
  605. *
  606. * @return void
  607. */
  608. public function testFormSecurityFields() {
  609. $key = 'testKey';
  610. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  611. $this->Form->request['_Token'] = array('key' => $key);
  612. $result = $this->Form->secure($fields);
  613. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  614. $hash .= ':' . 'Model.valid';
  615. $hash = urlencode($hash);
  616. $expected = array(
  617. 'div' => array('style' => 'display:none;'),
  618. array('input' => array(
  619. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  620. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  621. )),
  622. array('input' => array(
  623. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  624. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  630. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  631. $result = $this->Form->secure($fields);
  632. $expected = array(
  633. 'div' => array('class' => 'hidden'),
  634. array('input' => array(
  635. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  636. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  637. )),
  638. array('input' => array(
  639. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  640. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for double and float fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldGenerationForFloats() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'float',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => 10
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number',
  668. 'name' => 'data[Contact][foo]',
  669. 'id' => 'ContactFoo',
  670. 'step' => 'any'
  671. )),
  672. '/div'
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Form->input('foo', array('step' => 0.5));
  676. $expected = array(
  677. 'div' => array('class' => 'input number'),
  678. 'label' => array('for' => 'ContactFoo'),
  679. 'Foo',
  680. '/label',
  681. array('input' => array(
  682. 'type' => 'number',
  683. 'name' => 'data[Contact][foo]',
  684. 'id' => 'ContactFoo',
  685. 'step' => '0.5'
  686. )),
  687. '/div'
  688. );
  689. $this->assertTags($result, $expected);
  690. }
  691. /**
  692. * Tests correct generation of number fields for integer fields
  693. *
  694. * @access public
  695. * @return void
  696. */
  697. public function testTextFieldTypeNumberGenerationForIntegers() {
  698. $model = ClassRegistry::getObject('Contact');
  699. $model->setSchema(array('foo' => array(
  700. 'type' => 'integer',
  701. 'null' => false,
  702. 'default' => null,
  703. 'length' => null
  704. )));
  705. $this->Form->create('Contact');
  706. $result = $this->Form->input('foo');
  707. $expected = array(
  708. 'div' => array('class' => 'input number'),
  709. 'label' => array('for' => 'ContactFoo'),
  710. 'Foo',
  711. '/label',
  712. array('input' => array(
  713. 'type' => 'number', 'name' => 'data[Contact][foo]',
  714. 'id' => 'ContactFoo'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * testFormSecurityMultipleFields method
  722. *
  723. * Test secure() with multiple row form. Ensure hash is correct.
  724. *
  725. * @return void
  726. */
  727. public function testFormSecurityMultipleFields() {
  728. $key = 'testKey';
  729. $fields = array(
  730. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  731. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  732. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  733. );
  734. $this->Form->request['_Token'] = array('key' => $key);
  735. $result = $this->Form->secure($fields);
  736. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  737. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  738. $expected = array(
  739. 'div' => array('style' => 'display:none;'),
  740. array('input' => array(
  741. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  742. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  743. )),
  744. array('input' => array(
  745. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  746. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  747. )),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. }
  752. /**
  753. * testFormSecurityMultipleSubmitButtons
  754. *
  755. * test form submit generation and ensure that _Token is only created on end()
  756. *
  757. * @return void
  758. */
  759. public function testFormSecurityMultipleSubmitButtons() {
  760. $key = 'testKey';
  761. $this->Form->request['_Token'] = array('key' => $key);
  762. $this->Form->create('Addresses');
  763. $this->Form->input('Address.title');
  764. $this->Form->input('Address.first_name');
  765. $result = $this->Form->submit('Save', array('name' => 'save'));
  766. $expected = array(
  767. 'div' => array('class' => 'submit'),
  768. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  769. '/div',
  770. );
  771. $this->assertTags($result, $expected);
  772. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  773. $expected = array(
  774. 'div' => array('class' => 'submit'),
  775. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  776. '/div',
  777. );
  778. $this->assertTags($result, $expected);
  779. $result = $this->Form->end(null);
  780. $expected = array(
  781. 'div' => array('style' => 'display:none;'),
  782. array('input' => array(
  783. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  784. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  785. )),
  786. array('input' => array(
  787. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  788. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  789. )),
  790. '/div'
  791. );
  792. $this->assertTags($result, $expected);
  793. }
  794. /**
  795. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  796. *
  797. * @return void
  798. */
  799. public function testSecurityButtonNestedNamed() {
  800. $key = 'testKey';
  801. $this->Form->request['_Token'] = array('key' => $key);
  802. $this->Form->create('Addresses');
  803. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  804. $result = $this->Form->unlockField();
  805. $this->assertEquals(array('Address.button'), $result);
  806. }
  807. /**
  808. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  809. *
  810. * @return void
  811. */
  812. public function testSecuritySubmitNestedNamed() {
  813. $key = 'testKey';
  814. $this->Form->request['_Token'] = array('key' => $key);
  815. $this->Form->create('Addresses');
  816. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  817. $result = $this->Form->unlockField();
  818. $this->assertEquals(array('Address.button'), $result);
  819. }
  820. /**
  821. * Test that the correct fields are unlocked for image submits with no names.
  822. *
  823. * @return void
  824. */
  825. public function testSecuritySubmitImageNoName() {
  826. $key = 'testKey';
  827. $this->Form->request['_Token'] = array('key' => $key);
  828. $this->Form->create('User');
  829. $result = $this->Form->submit('save.png');
  830. $expected = array(
  831. 'div' => array('class' => 'submit'),
  832. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  833. '/div'
  834. );
  835. $this->assertTags($result, $expected);
  836. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  837. }
  838. /**
  839. * Test that the correct fields are unlocked for image submits with names.
  840. *
  841. * @return void
  842. */
  843. public function testSecuritySubmitImageName() {
  844. $key = 'testKey';
  845. $this->Form->request['_Token'] = array('key' => $key);
  846. $this->Form->create('User');
  847. $result = $this->Form->submit('save.png', array('name' => 'test'));
  848. $expected = array(
  849. 'div' => array('class' => 'submit'),
  850. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  855. }
  856. /**
  857. * testFormSecurityMultipleInputFields method
  858. *
  859. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  860. *
  861. * @return void
  862. */
  863. public function testFormSecurityMultipleInputFields() {
  864. $key = 'testKey';
  865. $this->Form->request['_Token'] = array('key' => $key);
  866. $this->Form->create('Addresses');
  867. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  868. $this->Form->input('Addresses.0.title');
  869. $this->Form->input('Addresses.0.first_name');
  870. $this->Form->input('Addresses.0.last_name');
  871. $this->Form->input('Addresses.0.address');
  872. $this->Form->input('Addresses.0.city');
  873. $this->Form->input('Addresses.0.phone');
  874. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  875. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  876. $this->Form->input('Addresses.1.title');
  877. $this->Form->input('Addresses.1.first_name');
  878. $this->Form->input('Addresses.1.last_name');
  879. $this->Form->input('Addresses.1.address');
  880. $this->Form->input('Addresses.1.city');
  881. $this->Form->input('Addresses.1.phone');
  882. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  883. $result = $this->Form->secure($this->Form->fields);
  884. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  885. $expected = array(
  886. 'div' => array('style' => 'display:none;'),
  887. array('input' => array(
  888. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  889. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  890. )),
  891. array('input' => array(
  892. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  893. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  894. )),
  895. '/div'
  896. );
  897. $this->assertTags($result, $expected);
  898. }
  899. /**
  900. * Test form security with Model.field.0 style inputs
  901. *
  902. * @return void
  903. */
  904. public function testFormSecurityArrayFields() {
  905. $key = 'testKey';
  906. $this->Form->request->params['_Token']['key'] = $key;
  907. $this->Form->create('Address');
  908. $this->Form->input('Address.primary.1');
  909. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  910. $this->Form->input('Address.secondary.1.0');
  911. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  912. }
  913. /**
  914. * testFormSecurityMultipleInputDisabledFields method
  915. *
  916. * test secure form generation with multiple records and disabled fields.
  917. *
  918. * @return void
  919. */
  920. public function testFormSecurityMultipleInputDisabledFields() {
  921. $key = 'testKey';
  922. $this->Form->request->params['_Token'] = array(
  923. 'key' => $key,
  924. 'unlockedFields' => array('first_name', 'address')
  925. );
  926. $this->Form->create();
  927. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  928. $this->Form->input('Addresses.0.title');
  929. $this->Form->input('Addresses.0.first_name');
  930. $this->Form->input('Addresses.0.last_name');
  931. $this->Form->input('Addresses.0.address');
  932. $this->Form->input('Addresses.0.city');
  933. $this->Form->input('Addresses.0.phone');
  934. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  935. $this->Form->input('Addresses.1.title');
  936. $this->Form->input('Addresses.1.first_name');
  937. $this->Form->input('Addresses.1.last_name');
  938. $this->Form->input('Addresses.1.address');
  939. $this->Form->input('Addresses.1.city');
  940. $this->Form->input('Addresses.1.phone');
  941. $result = $this->Form->secure($this->Form->fields);
  942. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  943. $expected = array(
  944. 'div' => array('style' => 'display:none;'),
  945. array('input' => array(
  946. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  947. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  948. )),
  949. array('input' => array(
  950. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  951. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  952. )),
  953. '/div'
  954. );
  955. $this->assertTags($result, $expected);
  956. }
  957. /**
  958. * testFormSecurityInputDisabledFields method
  959. *
  960. * Test single record form with disabled fields.
  961. *
  962. * @return void
  963. */
  964. public function testFormSecurityInputUnlockedFields() {
  965. $key = 'testKey';
  966. $this->Form->request['_Token'] = array(
  967. 'key' => $key,
  968. 'unlockedFields' => array('first_name', 'address')
  969. );
  970. $this->Form->create();
  971. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  972. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  973. $this->Form->input('Addresses.title');
  974. $this->Form->input('Addresses.first_name');
  975. $this->Form->input('Addresses.last_name');
  976. $this->Form->input('Addresses.address');
  977. $this->Form->input('Addresses.city');
  978. $this->Form->input('Addresses.phone');
  979. $result = $this->Form->fields;
  980. $expected = array(
  981. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  982. 'Addresses.city', 'Addresses.phone'
  983. );
  984. $this->assertEquals($expected, $result);
  985. $result = $this->Form->secure($expected);
  986. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  987. $expected = array(
  988. 'div' => array('style' => 'display:none;'),
  989. array('input' => array(
  990. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  991. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  992. )),
  993. array('input' => array(
  994. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  995. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  996. )),
  997. '/div'
  998. );
  999. $this->assertTags($result, $expected);
  1000. }
  1001. /**
  1002. * test securing inputs with custom name attributes.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testFormSecureWithCustomNameAttribute() {
  1007. $this->Form->request->params['_Token']['key'] = 'testKey';
  1008. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1009. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1010. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1011. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1012. }
  1013. /**
  1014. * testFormSecuredInput method
  1015. *
  1016. * Test generation of entire secure form, assertions made on input() output.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testFormSecuredInput() {
  1021. $this->Form->request['_Token'] = array('key' => 'testKey');
  1022. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1023. $encoding = strtolower(Configure::read('App.encoding'));
  1024. $expected = array(
  1025. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1026. 'div' => array('style' => 'display:none;'),
  1027. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1028. array('input' => array(
  1029. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1030. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertTags($result, $expected);
  1035. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1036. $expected = array(
  1037. 'div' => array('class' => 'input text'),
  1038. 'label' => array('for' => 'UserFormPublished'),
  1039. 'Published',
  1040. '/label',
  1041. array('input' => array(
  1042. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1043. 'id' => 'UserFormPublished'
  1044. )),
  1045. '/div'
  1046. );
  1047. $this->assertTags($result, $expected);
  1048. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1049. $expected = array(
  1050. 'div' => array('class' => 'input text'),
  1051. 'label' => array('for' => 'UserFormOther'),
  1052. 'Other',
  1053. '/label',
  1054. array('input' => array(
  1055. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1056. 'id' => 'UserFormOther'
  1057. )),
  1058. '/div'
  1059. );
  1060. $this->assertTags($result, $expected);
  1061. $result = $this->Form->hidden('UserForm.stuff');
  1062. $expected = array(
  1063. 'input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1065. 'id' => 'UserFormStuff'
  1066. ));
  1067. $this->assertTags($result, $expected);
  1068. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1069. $expected = array('input' => array(
  1070. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1071. 'value' => '0', 'id' => 'UserFormHidden'
  1072. ));
  1073. $this->assertTags($result, $expected);
  1074. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1075. $expected = array(
  1076. 'div' => array('class' => 'input checkbox'),
  1077. array('input' => array(
  1078. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1079. 'value' => '0', 'id' => 'UserFormSomething_'
  1080. )),
  1081. array('input' => array(
  1082. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1083. 'value' => '1', 'id' => 'UserFormSomething'
  1084. )),
  1085. 'label' => array('for' => 'UserFormSomething'),
  1086. 'Something',
  1087. '/label',
  1088. '/div'
  1089. );
  1090. $this->assertTags($result, $expected);
  1091. $result = $this->Form->fields;
  1092. $expected = array(
  1093. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1094. 'UserForm.hidden' => '0', 'UserForm.something'
  1095. );
  1096. $this->assertEquals($expected, $result);
  1097. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1098. $result = $this->Form->secure($this->Form->fields);
  1099. $expected = array(
  1100. 'div' => array('style' => 'display:none;'),
  1101. array('input' => array(
  1102. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1103. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1104. )),
  1105. array('input' => array(
  1106. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1107. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1108. )),
  1109. '/div'
  1110. );
  1111. $this->assertTags($result, $expected);
  1112. }
  1113. /**
  1114. * Test secured inputs with custom names.
  1115. *
  1116. * @return void
  1117. */
  1118. public function testSecuredInputCustomName() {
  1119. $this->Form->request['_Token'] = array('key' => 'testKey');
  1120. $this->assertEquals(array(), $this->Form->fields);
  1121. $this->Form->input('text_input', array(
  1122. 'name' => 'data[Option][General.default_role]',
  1123. ));
  1124. $expected = array('Option.General.default_role');
  1125. $this->assertEquals($expected, $this->Form->fields);
  1126. $this->Form->input('select_box', array(
  1127. 'name' => 'data[Option][General.select_role]',
  1128. 'type' => 'select',
  1129. 'options' => array(1, 2),
  1130. ));
  1131. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1132. $this->assertEquals($expected, $this->Form->fields);
  1133. }
  1134. /**
  1135. * Tests that the correct keys are added to the field hash index
  1136. *
  1137. * @return void
  1138. */
  1139. public function testFormSecuredFileInput() {
  1140. $this->Form->request['_Token'] = array('key' => 'testKey');
  1141. $this->assertEquals(array(), $this->Form->fields);
  1142. $this->Form->file('Attachment.file');
  1143. $expected = array(
  1144. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1145. 'Attachment.file.error', 'Attachment.file.size'
  1146. );
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * test that multiple selects keys are added to field hash
  1151. *
  1152. * @return void
  1153. */
  1154. public function testFormSecuredMultipleSelect() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $options = array('1' => 'one', '2' => 'two');
  1158. $this->Form->select('Model.select', $options);
  1159. $expected = array('Model.select');
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. $this->Form->fields = array();
  1162. $this->Form->select('Model.select', $options, array('multiple' => true));
  1163. $this->assertEquals($expected, $this->Form->fields);
  1164. }
  1165. /**
  1166. * testFormSecuredRadio method
  1167. *
  1168. * @return void
  1169. */
  1170. public function testFormSecuredRadio() {
  1171. $this->Form->request['_Token'] = array('key' => 'testKey');
  1172. $this->assertEquals(array(), $this->Form->fields);
  1173. $options = array('1' => 'option1', '2' => 'option2');
  1174. $this->Form->radio('Test.test', $options);
  1175. $expected = array('Test.test');
  1176. $this->assertEquals($expected, $this->Form->fields);
  1177. }
  1178. /**
  1179. * Test that when disabled is in a list based attribute array it works.
  1180. *
  1181. * @return void
  1182. */
  1183. public function testFormSecuredAndDisabledNotAssoc() {
  1184. $this->Form->request['_Token'] = array('key' => 'testKey');
  1185. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1186. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1187. $this->Form->text('Model.text', array('disabled'));
  1188. $this->Form->textarea('Model.textarea', array('disabled'));
  1189. $this->Form->password('Model.password', array('disabled'));
  1190. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1191. $expected = array(
  1192. 'Model.radio' => ''
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1198. * hashing.
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredAndDisabled() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1205. $this->Form->text('Model.text', array('disabled' => true));
  1206. $this->Form->password('Model.text', array('disabled' => true));
  1207. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1208. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1209. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1210. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1211. $this->Form->month('Model.month', array('disabled' => true));
  1212. $this->Form->day('Model.day', array('disabled' => true));
  1213. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1214. $this->Form->minute('Model.minute', array('disabled' => true));
  1215. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1216. $expected = array(
  1217. 'Model.radio' => ''
  1218. );
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testDisableSecurityUsingForm method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testDisableSecurityUsingForm() {
  1227. $this->Form->request['_Token'] = array(
  1228. 'key' => 'testKey',
  1229. 'disabledFields' => array()
  1230. );
  1231. $this->Form->create();
  1232. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1233. $this->Form->input('Addresses.title');
  1234. $this->Form->input('Addresses.first_name', array('secure' => false));
  1235. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1236. $this->Form->input('Addresses.zip', array(
  1237. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1238. ));
  1239. $result = $this->Form->fields;
  1240. $expected = array(
  1241. 'Addresses.id' => '123456', 'Addresses.title',
  1242. );
  1243. $this->assertEquals($expected, $result);
  1244. }
  1245. /**
  1246. * test disableField
  1247. *
  1248. * @return void
  1249. */
  1250. public function testUnlockFieldAddsToList() {
  1251. $this->Form->request['_Token'] = array(
  1252. 'key' => 'testKey',
  1253. 'unlockedFields' => array()
  1254. );
  1255. $this->Form->create('Contact');
  1256. $this->Form->unlockField('Contact.name');
  1257. $this->Form->text('Contact.name');
  1258. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1259. $this->assertEquals(array(), $this->Form->fields);
  1260. }
  1261. /**
  1262. * test unlockField removing from fields array.
  1263. *
  1264. * @return void
  1265. */
  1266. public function testUnlockFieldRemovingFromFields() {
  1267. $this->Form->request['_Token'] = array(
  1268. 'key' => 'testKey',
  1269. 'unlockedFields' => array()
  1270. );
  1271. $this->Form->create('Contact');
  1272. $this->Form->hidden('Contact.id', array('value' => 1));
  1273. $this->Form->text('Contact.name');
  1274. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1275. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1276. $this->Form->unlockField('Contact.name');
  1277. $this->Form->unlockField('Contact.id');
  1278. $this->assertEquals(array(), $this->Form->fields);
  1279. }
  1280. /**
  1281. * testTagIsInvalid method
  1282. *
  1283. * @return void
  1284. */
  1285. public function testTagIsInvalid() {
  1286. $Contact = ClassRegistry::getObject('Contact');
  1287. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1288. $this->Form->setEntity('Contact.0.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertEquals($expected, $result);
  1291. $this->Form->setEntity('Contact.1.email');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. $this->Form->setEntity('Contact.0.name');
  1295. $result = $this->Form->tagIsInvalid();
  1296. $this->assertFalse($result);
  1297. }
  1298. /**
  1299. * Test tagIsInvalid with validation errors from a saveMany
  1300. *
  1301. * @return void
  1302. */
  1303. public function testTagIsInvalidSaveMany() {
  1304. $Contact = ClassRegistry::getObject('Contact');
  1305. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1306. $this->Form->create('Contact');
  1307. $this->Form->setEntity('0.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. $this->Form->setEntity('0.Contact.email');
  1311. $result = $this->Form->tagIsInvalid();
  1312. $this->assertEquals($expected, $result);
  1313. }
  1314. /**
  1315. * Test validation errors.
  1316. *
  1317. * @return void
  1318. */
  1319. public function testPasswordValidation() {
  1320. $Contact = ClassRegistry::getObject('Contact');
  1321. $Contact->validationErrors['password'] = array('Please provide a password');
  1322. $result = $this->Form->input('Contact.password');
  1323. $expected = array(
  1324. 'div' => array('class' => 'input password error'),
  1325. 'label' => array('for' => 'ContactPassword'),
  1326. 'Password',
  1327. '/label',
  1328. 'input' => array(
  1329. 'type' => 'password', 'name' => 'data[Contact][password]',
  1330. 'id' => 'ContactPassword', 'class' => 'form-error'
  1331. ),
  1332. array('div' => array('class' => 'error-message')),
  1333. 'Please provide a password',
  1334. '/div',
  1335. '/div'
  1336. );
  1337. $this->assertTags($result, $expected);
  1338. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1339. $expected = array(
  1340. 'div' => array('class' => 'input password error'),
  1341. 'label' => array('for' => 'ContactPassword'),
  1342. 'Password',
  1343. '/label',
  1344. 'input' => array(
  1345. 'type' => 'password', 'name' => 'data[Contact][password]',
  1346. 'id' => 'ContactPassword', 'class' => 'form-error'
  1347. ),
  1348. '/div'
  1349. );
  1350. $this->assertTags($result, $expected);
  1351. }
  1352. /**
  1353. * Test validation errors, when validation message is an empty string.
  1354. *
  1355. * @access public
  1356. * @return void
  1357. */
  1358. public function testEmptyErrorValidation() {
  1359. $this->Form->validationErrors['Contact']['password'] = '';
  1360. $result = $this->Form->input('Contact.password');
  1361. $expected = array(
  1362. 'div' => array('class' => 'input password error'),
  1363. 'label' => array('for' => 'ContactPassword'),
  1364. 'Password',
  1365. '/label',
  1366. 'input' => array(
  1367. 'type' => 'password', 'name' => 'data[Contact][password]',
  1368. 'id' => 'ContactPassword', 'class' => 'form-error'
  1369. ),
  1370. array('div' => array('class' => 'error-message')),
  1371. array(),
  1372. '/div',
  1373. '/div'
  1374. );
  1375. $this->assertTags($result, $expected);
  1376. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. '/div'
  1387. );
  1388. $this->assertTags($result, $expected);
  1389. }
  1390. /**
  1391. * Test validation errors, when calling input() overriding validation message by an empty string.
  1392. *
  1393. * @access public
  1394. * @return void
  1395. */
  1396. public function testEmptyInputErrorValidation() {
  1397. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1398. $result = $this->Form->input('Contact.password', array('error' => ''));
  1399. $expected = array(
  1400. 'div' => array('class' => 'input password error'),
  1401. 'label' => array('for' => 'ContactPassword'),
  1402. 'Password',
  1403. '/label',
  1404. 'input' => array(
  1405. 'type' => 'password', 'name' => 'data[Contact][password]',
  1406. 'id' => 'ContactPassword', 'class' => 'form-error'
  1407. ),
  1408. array('div' => array('class' => 'error-message')),
  1409. array(),
  1410. '/div',
  1411. '/div'
  1412. );
  1413. $this->assertTags($result, $expected);
  1414. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1415. $expected = array(
  1416. 'div' => array('class' => 'input password error'),
  1417. 'label' => array('for' => 'ContactPassword'),
  1418. 'Password',
  1419. '/label',
  1420. 'input' => array(
  1421. 'type' => 'password', 'name' => 'data[Contact][password]',
  1422. 'id' => 'ContactPassword', 'class' => 'form-error'
  1423. ),
  1424. '/div'
  1425. );
  1426. $this->assertTags($result, $expected);
  1427. }
  1428. /**
  1429. * testFormValidationAssociated method
  1430. *
  1431. * test display of form errors in conjunction with model::validates.
  1432. *
  1433. * @return void
  1434. */
  1435. public function testFormValidationAssociated() {
  1436. $this->UserForm = ClassRegistry::getObject('UserForm');
  1437. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1438. $data = array(
  1439. 'UserForm' => array('name' => 'user'),
  1440. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1441. );
  1442. $result = $this->UserForm->OpenidUrl->create($data);
  1443. $this->assertFalse(empty($result));
  1444. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1445. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1446. $encoding = strtolower(Configure::read('App.encoding'));
  1447. $expected = array(
  1448. 'form' => array(
  1449. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1450. 'accept-charset' => $encoding
  1451. ),
  1452. 'div' => array('style' => 'display:none;'),
  1453. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1454. '/div'
  1455. );
  1456. $this->assertTags($result, $expected);
  1457. $result = $this->Form->error(
  1458. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1459. );
  1460. $this->assertEquals('Error, not registered', $result);
  1461. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1462. }
  1463. /**
  1464. * testFormValidationAssociatedFirstLevel method
  1465. *
  1466. * test form error display with associated model.
  1467. *
  1468. * @return void
  1469. */
  1470. public function testFormValidationAssociatedFirstLevel() {
  1471. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1472. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1473. $data = array(
  1474. 'ValidateUser' => array('name' => 'mariano'),
  1475. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1476. );
  1477. $result = $this->ValidateUser->create($data);
  1478. $this->assertFalse(empty($result));
  1479. $this->assertFalse($this->ValidateUser->validates());
  1480. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1481. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1482. $encoding = strtolower(Configure::read('App.encoding'));
  1483. $expected = array(
  1484. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1485. 'div' => array('style' => 'display:none;'),
  1486. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1487. '/div'
  1488. );
  1489. $this->assertTags($result, $expected);
  1490. $result = $this->Form->error(
  1491. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid email', $result);
  1494. $result = $this->Form->error(
  1495. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1496. );
  1497. $this->assertEquals('Invalid name', $result);
  1498. $result = $this->Form->error(
  1499. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1500. );
  1501. $this->assertEquals('Invalid city', $result);
  1502. unset($this->ValidateUser->ValidateProfile);
  1503. unset($this->ValidateUser);
  1504. }
  1505. /**
  1506. * testFormValidationAssociatedSecondLevel method
  1507. *
  1508. * test form error display with associated model.
  1509. *
  1510. * @return void
  1511. */
  1512. public function testFormValidationAssociatedSecondLevel() {
  1513. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1514. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1515. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1516. $data = array(
  1517. 'ValidateUser' => array('name' => 'mariano'),
  1518. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1519. 'ValidateItem' => array('name' => 'Item')
  1520. );
  1521. $result = $this->ValidateUser->create($data);
  1522. $this->assertFalse(empty($result));
  1523. $this->assertFalse($this->ValidateUser->validates());
  1524. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1525. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1526. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1527. $encoding = strtolower(Configure::read('App.encoding'));
  1528. $expected = array(
  1529. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1530. 'div' => array('style' => 'display:none;'),
  1531. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1532. '/div'
  1533. );
  1534. $this->assertTags($result, $expected);
  1535. $result = $this->Form->error(
  1536. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1537. );
  1538. $this->assertEquals('Invalid email', $result);
  1539. $result = $this->Form->error(
  1540. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1541. );
  1542. $this->assertEquals('Invalid name', $result);
  1543. $result = $this->Form->error(
  1544. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1545. );
  1546. $result = $this->Form->error(
  1547. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1548. );
  1549. $this->assertEquals('Invalid description', $result);
  1550. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1551. unset($this->ValidateUser->ValidateProfile);
  1552. unset($this->ValidateUser);
  1553. }
  1554. /**
  1555. * testFormValidationMultiRecord method
  1556. *
  1557. * test form error display with multiple records.
  1558. *
  1559. * @return void
  1560. */
  1561. public function testFormValidationMultiRecord() {
  1562. $Contact = ClassRegistry::getObject('Contact');
  1563. $Contact->validationErrors[2] = array(
  1564. 'name' => array('This field cannot be left blank')
  1565. );
  1566. $result = $this->Form->input('Contact.2.name');
  1567. $expected = array(
  1568. 'div' => array('class' => 'input text error'),
  1569. 'label' => array('for' => 'Contact2Name'),
  1570. 'Name',
  1571. '/label',
  1572. 'input' => array(
  1573. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1574. 'class' => 'form-error', 'maxlength' => 255
  1575. ),
  1576. array('div' => array('class' => 'error-message')),
  1577. 'This field cannot be left blank',
  1578. '/div',
  1579. '/div'
  1580. );
  1581. $this->assertTags($result, $expected);
  1582. }
  1583. /**
  1584. * testMultipleInputValidation method
  1585. *
  1586. * test multiple record form validation error display.
  1587. *
  1588. * @return void
  1589. */
  1590. public function testMultipleInputValidation() {
  1591. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1592. $Address->validationErrors[0] = array(
  1593. 'title' => array('This field cannot be empty'),
  1594. 'first_name' => array('This field cannot be empty')
  1595. );
  1596. $Address->validationErrors[1] = array(
  1597. 'last_name' => array('You must have a last name')
  1598. );
  1599. $this->Form->create();
  1600. $result = $this->Form->input('Address.0.title');
  1601. $expected = array(
  1602. 'div' => array('class'),
  1603. 'label' => array('for'),
  1604. 'preg:/[^<]+/',
  1605. '/label',
  1606. 'input' => array(
  1607. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1608. ),
  1609. array('div' => array('class' => 'error-message')),
  1610. 'This field cannot be empty',
  1611. '/div',
  1612. '/div'
  1613. );
  1614. $this->assertTags($result, $expected);
  1615. $result = $this->Form->input('Address.0.first_name');
  1616. $expected = array(
  1617. 'div' => array('class'),
  1618. 'label' => array('for'),
  1619. 'preg:/[^<]+/',
  1620. '/label',
  1621. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1622. array('div' => array('class' => 'error-message')),
  1623. 'This field cannot be empty',
  1624. '/div',
  1625. '/div'
  1626. );
  1627. $this->assertTags($result, $expected);
  1628. $result = $this->Form->input('Address.0.last_name');
  1629. $expected = array(
  1630. 'div' => array('class'),
  1631. 'label' => array('for'),
  1632. 'preg:/[^<]+/',
  1633. '/label',
  1634. 'input' => array('type' => 'text', 'name', 'id'),
  1635. '/div'
  1636. );
  1637. $this->assertTags($result, $expected);
  1638. $result = $this->Form->input('Address.1.last_name');
  1639. $expected = array(
  1640. 'div' => array('class'),
  1641. 'label' => array('for'),
  1642. 'preg:/[^<]+/',
  1643. '/label',
  1644. 'input' => array(
  1645. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1646. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1647. ),
  1648. array('div' => array('class' => 'error-message')),
  1649. 'You must have a last name',
  1650. '/div',
  1651. '/div'
  1652. );
  1653. $this->assertTags($result, $expected);
  1654. }
  1655. /**
  1656. * testInput method
  1657. *
  1658. * Test various incarnations of input().
  1659. *
  1660. * @return void
  1661. */
  1662. public function testInput() {
  1663. $result = $this->Form->input('ValidateUser.balance');
  1664. $expected = array(
  1665. 'div' => array('class'),
  1666. 'label' => array('for'),
  1667. 'Balance',
  1668. '/label',
  1669. 'input' => array('name', 'type' => 'number', 'id'),
  1670. '/div',
  1671. );
  1672. $this->assertTags($result, $expected);
  1673. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1674. $expected = array(
  1675. 'div' => array('class' => 'input email'),
  1676. 'label' => array('for' => 'custom'),
  1677. 'Email',
  1678. '/label',
  1679. array('input' => array(
  1680. 'type' => 'email', 'name' => 'data[Contact][email]',
  1681. 'id' => 'custom', 'maxlength' => 255
  1682. )),
  1683. '/div'
  1684. );
  1685. $this->assertTags($result, $expected);
  1686. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1687. $expected = array(
  1688. '<div',
  1689. 'label' => array('for' => 'ContactEmail'),
  1690. 'Email',
  1691. '/label',
  1692. array('input' => array(
  1693. 'type' => 'email', 'name' => 'data[Contact][email]',
  1694. 'id' => 'ContactEmail', 'maxlength' => 255
  1695. )),
  1696. '/div'
  1697. );
  1698. $this->assertTags($result, $expected);
  1699. $result = $this->Form->hidden('Contact.idontexist');
  1700. $expected = array('input' => array(
  1701. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1702. 'id' => 'ContactIdontexist'
  1703. ));
  1704. $this->assertTags($result, $expected);
  1705. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1706. $expected = array(
  1707. 'div' => array('class' => 'input text'),
  1708. 'label' => array('for' => 'ContactEmail'),
  1709. 'Email',
  1710. '/label',
  1711. array('input' => array(
  1712. 'type' => 'text', 'name' => 'data[Contact][email]',
  1713. 'id' => 'ContactEmail'
  1714. )),
  1715. '/div'
  1716. );
  1717. $this->assertTags($result, $expected);
  1718. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1719. $expected = array(
  1720. 'div' => array('class' => 'input text'),
  1721. 'label' => array('for' => 'Contact5Email'),
  1722. 'Email',
  1723. '/label',
  1724. array('input' => array(
  1725. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1726. 'id' => 'Contact5Email'
  1727. )),
  1728. '/div'
  1729. );
  1730. $this->assertTags($result, $expected);
  1731. $result = $this->Form->input('Contact.password');
  1732. $expected = array(
  1733. 'div' => array('class' => 'input password'),
  1734. 'label' => array('for' => 'ContactPassword'),
  1735. 'Password',
  1736. '/label',
  1737. array('input' => array(
  1738. 'type' => 'password', 'name' => 'data[Contact][password]',
  1739. 'id' => 'ContactPassword'
  1740. )),
  1741. '/div'
  1742. );
  1743. $this->assertTags($result, $expected);
  1744. $result = $this->Form->input('Contact.email', array(
  1745. 'type' => 'file', 'class' => 'textbox'
  1746. ));
  1747. $expected = array(
  1748. 'div' => array('class' => 'input file'),
  1749. 'label' => array('for' => 'ContactEmail'),
  1750. 'Email',
  1751. '/label',
  1752. array('input' => array(
  1753. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1754. 'id' => 'ContactEmail'
  1755. )),
  1756. '/div'
  1757. );
  1758. $this->assertTags($result, $expected);
  1759. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1760. $result = $this->Form->input('Contact.phone');
  1761. $expected = array(
  1762. 'div' => array('class' => 'input tel'),
  1763. 'label' => array('for' => 'ContactPhone'),
  1764. 'Phone',
  1765. '/label',
  1766. array('input' => array(
  1767. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1768. 'value' => 'Hello &amp; World &gt; weird chars',
  1769. 'id' => 'ContactPhone', 'maxlength' => 255
  1770. )),
  1771. '/div'
  1772. );
  1773. $this->assertTags($result, $expected);
  1774. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1775. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1776. $expected = array(
  1777. 'div' => array('class' => 'input text'),
  1778. 'label' => array('for' => 'myId'),
  1779. 'Field',
  1780. '/label',
  1781. 'input' => array(
  1782. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1783. 'value' => 'My value', 'id' => 'myId'
  1784. ),
  1785. '/div'
  1786. );
  1787. $this->assertTags($result, $expected);
  1788. unset($this->Form->request->data);
  1789. $Contact = ClassRegistry::getObject('Contact');
  1790. $Contact->validationErrors['field'] = array('Badness!');
  1791. $result = $this->Form->input('Contact.field');
  1792. $expected = array(
  1793. 'div' => array('class' => 'input text error'),
  1794. 'label' => array('for' => 'ContactField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'data[Contact][field]',
  1799. 'id' => 'ContactField', 'class' => 'form-error'
  1800. ),
  1801. array('div' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/div',
  1804. '/div'
  1805. );
  1806. $this->assertTags($result, $expected);
  1807. $result = $this->Form->input('Contact.field', array(
  1808. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1809. ));
  1810. $expected = array(
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array(
  1815. 'type' => 'text', 'name' => 'data[Contact][field]',
  1816. 'id' => 'ContactField', 'class' => 'form-error'
  1817. ),
  1818. array('span' => array('class' => 'error-message')),
  1819. 'Badness!',
  1820. '/span'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. $result = $this->Form->input('Contact.field', array(
  1824. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1825. ));
  1826. $expected = array(
  1827. 'div' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. array('div' => array('class' => 'error')),
  1836. 'Badness!',
  1837. '/div'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $result = $this->Form->input('Contact.field', array(
  1841. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1842. ));
  1843. $expected = array(
  1844. 'span' => array('class' => 'input text error'),
  1845. 'label' => array('for' => 'ContactField'),
  1846. 'Field',
  1847. '/label',
  1848. 'input' => array(
  1849. 'type' => 'text', 'name' => 'data[Contact][field]',
  1850. 'id' => 'ContactField', 'class' => 'form-error'
  1851. ),
  1852. 'Badness!',
  1853. '/span'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1857. $expected = array(
  1858. 'div' => array('class' => 'input text error'),
  1859. 'label' => array('for' => 'ContactField'),
  1860. 'Field',
  1861. '/label',
  1862. 'input' => array(
  1863. 'type' => 'text', 'name' => 'data[Contact][field]',
  1864. 'id' => 'ContactField', 'class' => 'form-error'
  1865. ),
  1866. 'A message to you, Rudy',
  1867. array('div' => array('class' => 'error-message')),
  1868. 'Badness!',
  1869. '/div',
  1870. '/div'
  1871. );
  1872. $this->assertTags($result, $expected);
  1873. $this->Form->setEntity(null);
  1874. $this->Form->setEntity('Contact.field');
  1875. $result = $this->Form->input('Contact.field', array(
  1876. 'after' => 'A message to you, Rudy', 'error' => false
  1877. ));
  1878. $expected = array(
  1879. 'div' => array('class' => 'input text'),
  1880. 'label' => array('for' => 'ContactField'),
  1881. 'Field',
  1882. '/label',
  1883. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1884. 'A message to you, Rudy',
  1885. '/div'
  1886. );
  1887. $this->assertTags($result, $expected);
  1888. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text'),
  1891. 'label' => array('for' => 'ObjectField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1895. 'A message to you, Rudy',
  1896. '/div'
  1897. );
  1898. $this->assertTags($result, $expected);
  1899. $Contact->validationErrors['field'] = array('minLength');
  1900. $result = $this->Form->input('Contact.field', array(
  1901. 'error' => array(
  1902. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1903. 'maxLength' => 'login too large'
  1904. )
  1905. ));
  1906. $expected = array(
  1907. 'div' => array('class' => 'input text error'),
  1908. 'label' => array('for' => 'ContactField'),
  1909. 'Field',
  1910. '/label',
  1911. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1912. array('div' => array('class' => 'error-message')),
  1913. 'Le login doit contenir au moins 2 caractères',
  1914. '/div',
  1915. '/div'
  1916. );
  1917. $this->assertTags($result, $expected);
  1918. $Contact->validationErrors['field'] = array('maxLength');
  1919. $result = $this->Form->input('Contact.field', array(
  1920. 'error' => array(
  1921. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1922. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1923. 'maxLength' => 'login too large',
  1924. )
  1925. ));
  1926. $expected = array(
  1927. 'div' => array('class' => 'input text error'),
  1928. 'label' => array('for' => 'ContactField'),
  1929. 'Field',
  1930. '/label',
  1931. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1932. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1933. 'login too large',
  1934. '/span',
  1935. '/div'
  1936. );
  1937. $this->assertTags($result, $expected);
  1938. }
  1939. /**
  1940. * Test that inputs with 0 can be created.
  1941. *
  1942. * @return void
  1943. */
  1944. public function testInputZero() {
  1945. $this->Form->create('User');
  1946. $result = $this->Form->input('0');
  1947. $expected = array(
  1948. 'div' => array('class' => 'input text'),
  1949. 'label' => array('for' => 'User0'), '/label',
  1950. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. }
  1955. /**
  1956. * test input() with checkbox creation
  1957. *
  1958. * @return void
  1959. */
  1960. public function testInputCheckbox() {
  1961. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input checkbox'),
  1964. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1965. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1966. '/div'
  1967. );
  1968. $this->assertTags($result, $expected);
  1969. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1970. $expected = array(
  1971. 'div' => array('class' => 'input checkbox'),
  1972. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1973. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1974. '/div'
  1975. );
  1976. $this->assertTags($result, $expected);
  1977. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1978. $expected = array(
  1979. 'div' => array('class' => 'input checkbox'),
  1980. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1981. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1982. '/div'
  1983. );
  1984. $this->assertTags($result, $expected);
  1985. $result = $this->Form->input('User.disabled', array(
  1986. 'label' => 'Disabled',
  1987. 'type' => 'checkbox',
  1988. 'data-foo' => 'disabled'
  1989. ));
  1990. $expected = array(
  1991. 'div' => array('class' => 'input checkbox'),
  1992. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1993. array('input' => array(
  1994. 'type' => 'checkbox',
  1995. 'name' => 'data[User][disabled]',
  1996. 'value' => '1',
  1997. 'id' => 'UserDisabled',
  1998. 'data-foo' => 'disabled'
  1999. )),
  2000. 'label' => array('for' => 'UserDisabled'),
  2001. 'Disabled',
  2002. '/label',
  2003. '/div'
  2004. );
  2005. $this->assertTags($result, $expected);
  2006. }
  2007. /**
  2008. * test form->input() with time types.
  2009. *
  2010. */
  2011. public function testInputTime() {
  2012. extract($this->dateRegex);
  2013. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2014. $result = explode(':', $result);
  2015. $this->assertRegExp('/option value="23"/', $result[0]);
  2016. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2017. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2018. $result = explode(':', $result);
  2019. $this->assertRegExp('/option value="23"/', $result[0]);
  2020. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2021. $result = $this->Form->input('Model.field', array(
  2022. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2023. ));
  2024. $result = explode(':', $result);
  2025. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2026. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2027. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2028. $result = $this->Form->input('Model.field', array(
  2029. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2030. ));
  2031. $result = explode(':', $result);
  2032. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2033. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2034. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2035. $result = $this->Form->input('prueba', array(
  2036. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2037. 'maxYear' => date('Y') + 1 , 'interval' => 15
  2038. ));
  2039. $result = explode(':', $result);
  2040. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2041. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2042. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2043. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2044. $result = $this->Form->input('Random.start_time', array(
  2045. 'type' => 'time',
  2046. 'selected' => '18:15'
  2047. ));
  2048. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2049. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2050. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2051. $result = $this->Form->input('published', array('type' => 'time'));
  2052. $now = strtotime('now');
  2053. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2054. $now = strtotime('2013-03-09 00:42:21');
  2055. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2056. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2057. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2058. }
  2059. /**
  2060. * Test interval + selected near the hour roll over.
  2061. *
  2062. * @return void
  2063. */
  2064. public function testTimeSelectedWithInterval() {
  2065. $result = $this->Form->input('Model.start_time', array(
  2066. 'type' => 'time',
  2067. 'interval' => 15,
  2068. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2069. ));
  2070. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2071. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2072. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2073. $result = $this->Form->input('Model.start_time', array(
  2074. 'type' => 'time',
  2075. 'interval' => 15,
  2076. 'selected' => '2012-10-23 15:57:00'
  2077. ));
  2078. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2079. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2080. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2081. $result = $this->Form->input('Model.start_time', array(
  2082. 'timeFormat' => 24,
  2083. 'type' => 'time',
  2084. 'interval' => 15,
  2085. 'selected' => '15:57'
  2086. ));
  2087. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2088. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2089. $result = $this->Form->input('Model.start_time', array(
  2090. 'timeFormat' => 24,
  2091. 'type' => 'time',
  2092. 'interval' => 15,
  2093. 'selected' => '23:57'
  2094. ));
  2095. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2096. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2097. $result = $this->Form->input('Model.created', array(
  2098. 'timeFormat' => 24,
  2099. 'type' => 'datetime',
  2100. 'interval' => 15,
  2101. 'selected' => '2012-09-30 23:56'
  2102. ));
  2103. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2104. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2105. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2106. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2107. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2108. }
  2109. /**
  2110. * Test interval & timeFormat = 12
  2111. *
  2112. * @return void
  2113. */
  2114. public function testInputTimeWithIntervalAnd12HourFormat() {
  2115. $result = $this->Form->input('Model.start_time', array(
  2116. 'type' => 'time',
  2117. 'timeFormat' => 12,
  2118. 'interval' => 5,
  2119. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2120. ));
  2121. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2122. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2123. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2124. $result = $this->Form->input('Model.start_time', array(
  2125. 'type' => 'time',
  2126. 'timeFormat' => '12',
  2127. 'interval' => 5,
  2128. 'selected' => '2013-04-19 16:30:00'
  2129. ));
  2130. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2131. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2132. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2133. $result = $this->Form->input('Model.start_time', array(
  2134. 'type' => 'time',
  2135. 'timeFormat' => '12',
  2136. 'interval' => 10,
  2137. 'selected' => '2013-05-19 00:33:00'
  2138. ));
  2139. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2140. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2141. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2142. $result = $this->Form->input('Model.start_time', array(
  2143. 'type' => 'time',
  2144. 'timeFormat' => '12',
  2145. 'interval' => 10,
  2146. 'selected' => '2013-05-19 13:33:00'
  2147. ));
  2148. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2149. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2150. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2151. $result = $this->Form->input('Model.start_time', array(
  2152. 'type' => 'time',
  2153. 'timeFormat' => '12',
  2154. 'interval' => 10,
  2155. 'selected' => '2013-05-19 01:33:00'
  2156. ));
  2157. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2158. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2159. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2160. }
  2161. /**
  2162. * test form->input() with datetime, date and time types
  2163. *
  2164. * @return void
  2165. */
  2166. public function testInputDatetime() {
  2167. extract($this->dateRegex);
  2168. $result = $this->Form->input('prueba', array(
  2169. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2170. 'maxYear' => date('Y') + 1, 'interval' => 15
  2171. ));
  2172. $result = explode(':', $result);
  2173. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2174. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2175. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2176. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2177. //related to ticket #5013
  2178. $result = $this->Form->input('Contact.date', array(
  2179. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2180. ));
  2181. $this->assertRegExp('/class="customClass"/', $result);
  2182. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2183. $result = $this->Form->input('Contact.date', array(
  2184. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2185. ));
  2186. $this->assertRegExp('/id="customIdDay"/', $result);
  2187. $this->assertRegExp('/id="customIdMonth"/', $result);
  2188. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2189. $result = $this->Form->input('Model.field', array(
  2190. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2191. ));
  2192. $this->assertRegExp('/id="customIDDay"/', $result);
  2193. $this->assertRegExp('/id="customIDHour"/', $result);
  2194. $result = explode('</select><select', $result);
  2195. $result = explode(':', $result[1]);
  2196. $this->assertRegExp('/option value="23"/', $result[0]);
  2197. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2198. $result = $this->Form->input('Model.field', array(
  2199. 'type' => 'datetime', 'timeFormat' => 12
  2200. ));
  2201. $result = explode('</select><select', $result);
  2202. $result = explode(':', $result[1]);
  2203. $this->assertRegExp('/option value="12"/', $result[0]);
  2204. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2205. $this->Form->request->data = array('Contact' => array('created' => null));
  2206. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2207. $expected = array(
  2208. 'div' => array('class' => 'input date'),
  2209. 'label' => array('for' => 'ContactCreatedMonth'),
  2210. 'Created',
  2211. '/label',
  2212. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2213. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2214. $monthsRegex,
  2215. '/select', '-',
  2216. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2217. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2218. $daysRegex,
  2219. '/select', '-',
  2220. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2221. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2222. $yearsRegex,
  2223. '/select',
  2224. '/div'
  2225. );
  2226. $this->assertTags($result, $expected);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2230. $this->Form->request->data = array('Contact' => array('created' => null));
  2231. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2232. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2233. $result = $this->Form->input('Contact.created', array(
  2234. 'type' => 'date',
  2235. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2236. 'timeFormat' => 'NONE'
  2237. ));
  2238. $this->assertRegExp('/for\="created-month"/', $result);
  2239. }
  2240. /**
  2241. * Test generating checkboxes in a loop.
  2242. *
  2243. * @return void
  2244. */
  2245. public function testInputCheckboxesInLoop() {
  2246. for ($i = 1; $i < 5; $i++) {
  2247. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2248. $expected = array(
  2249. 'div' => array('class' => 'input checkbox'),
  2250. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2251. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2252. 'label' => array('for' => "Contact{$i}Email"),
  2253. 'Email',
  2254. '/label',
  2255. '/div'
  2256. );
  2257. $this->assertTags($result, $expected);
  2258. }
  2259. }
  2260. /**
  2261. * Test generating checkboxes with disabled elements.
  2262. *
  2263. * @return void
  2264. */
  2265. public function testInputCheckboxWithDisabledElements() {
  2266. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2267. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2268. $expected = array(
  2269. array('div' => array('class' => 'input select')),
  2270. array('label' => array('for' => "ContactMultiple")),
  2271. 'Multiple',
  2272. '/label',
  2273. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2274. array('div' => array('class' => 'checkbox')),
  2275. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2276. array('label' => array('for' => "ContactMultiple1")),
  2277. 'One',
  2278. '/label',
  2279. '/div',
  2280. array('div' => array('class' => 'checkbox')),
  2281. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2282. array('label' => array('for' => "ContactMultiple2")),
  2283. 'Two',
  2284. '/label',
  2285. '/div',
  2286. array('div' => array('class' => 'checkbox')),
  2287. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2288. array('label' => array('for' => "ContactMultiple3")),
  2289. 'Three',
  2290. '/label',
  2291. '/div',
  2292. '/div'
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2296. $this->assertTags($result, $expected);
  2297. $disabled = array('2', 3);
  2298. $expected = array(
  2299. array('div' => array('class' => 'input select')),
  2300. array('label' => array('for' => "ContactMultiple")),
  2301. 'Multiple',
  2302. '/label',
  2303. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2304. array('div' => array('class' => 'checkbox')),
  2305. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2306. array('label' => array('for' => "ContactMultiple1")),
  2307. 'One',
  2308. '/label',
  2309. '/div',
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2312. array('label' => array('for' => "ContactMultiple2")),
  2313. 'Two',
  2314. '/label',
  2315. '/div',
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2318. array('label' => array('for' => "ContactMultiple3")),
  2319. 'Three',
  2320. '/label',
  2321. '/div',
  2322. '/div'
  2323. );
  2324. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2325. $this->assertTags($result, $expected);
  2326. // make sure 50 does only disable 50, and not 50f5c0cf
  2327. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2328. $disabled = array(50);
  2329. $expected = array(
  2330. array('div' => array('class' => 'input select')),
  2331. array('label' => array('for' => "ContactMultiple")),
  2332. 'Multiple',
  2333. '/label',
  2334. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2335. array('div' => array('class' => 'checkbox')),
  2336. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2337. array('label' => array('for' => "ContactMultiple50")),
  2338. 'Fifty',
  2339. '/label',
  2340. '/div',
  2341. array('div' => array('class' => 'checkbox')),
  2342. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2343. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2344. 'Stringy',
  2345. '/label',
  2346. '/div',
  2347. '/div'
  2348. );
  2349. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2350. $this->assertTags($result, $expected);
  2351. }
  2352. /**
  2353. * test input name with leading integer, ensure attributes are generated correctly.
  2354. *
  2355. * @return void
  2356. */
  2357. public function testInputWithLeadingInteger() {
  2358. $result = $this->Form->text('0.Node.title');
  2359. $expected = array(
  2360. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2361. );
  2362. $this->assertTags($result, $expected);
  2363. }
  2364. /**
  2365. * test form->input() with select type inputs.
  2366. *
  2367. * @return void
  2368. */
  2369. public function testInputSelectType() {
  2370. $result = $this->Form->input('email', array(
  2371. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2372. );
  2373. $expected = array(
  2374. 'div' => array('class' => 'input select'),
  2375. 'label' => array('for' => 'email'),
  2376. 'Email',
  2377. '/label',
  2378. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2379. array('option' => array('value' => '')),
  2380. '/option',
  2381. array('option' => array('value' => 'è')),
  2382. 'Firést',
  2383. '/option',
  2384. array('option' => array('value' => 'é')),
  2385. 'Secoènd',
  2386. '/option',
  2387. '/select',
  2388. '/div'
  2389. );
  2390. $this->assertTags($result, $expected);
  2391. $result = $this->Form->input('email', array(
  2392. 'options' => array('First', 'Second'), 'empty' => true)
  2393. );
  2394. $expected = array(
  2395. 'div' => array('class' => 'input select'),
  2396. 'label' => array('for' => 'email'),
  2397. 'Email',
  2398. '/label',
  2399. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2400. array('option' => array('value' => '')),
  2401. '/option',
  2402. array('option' => array('value' => '0')),
  2403. 'First',
  2404. '/option',
  2405. array('option' => array('value' => '1')),
  2406. 'Second',
  2407. '/option',
  2408. '/select',
  2409. '/div'
  2410. );
  2411. $this->assertTags($result, $expected);
  2412. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2413. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2414. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2415. $expected = array(
  2416. 'div' => array('class' => 'input select'),
  2417. 'label' => array('for' => 'ModelUserId'),
  2418. 'User',
  2419. '/label',
  2420. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2421. array('option' => array('value' => '')),
  2422. '/option',
  2423. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2424. 'good',
  2425. '/option',
  2426. array('option' => array('value' => 'other')),
  2427. 'bad',
  2428. '/option',
  2429. '/select',
  2430. '/div'
  2431. );
  2432. $this->assertTags($result, $expected);
  2433. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2434. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2435. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2436. $expected = array(
  2437. 'div' => array('class' => 'input select'),
  2438. 'label' => array('for' => 'ThingUserId'),
  2439. 'User',
  2440. '/label',
  2441. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2442. array('option' => array('value' => '')),
  2443. 'Some Empty',
  2444. '/option',
  2445. array('option' => array('value' => 'value')),
  2446. 'good',
  2447. '/option',
  2448. array('option' => array('value' => 'other')),
  2449. 'bad',
  2450. '/option',
  2451. '/select',
  2452. '/div'
  2453. );
  2454. $this->assertTags($result, $expected);
  2455. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2456. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2457. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2458. $expected = array(
  2459. 'div' => array('class' => 'input select'),
  2460. 'label' => array('for' => 'ThingUserId'),
  2461. 'User',
  2462. '/label',
  2463. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2464. array('option' => array('value' => '')),
  2465. 'Some Empty',
  2466. '/option',
  2467. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2468. 'good',
  2469. '/option',
  2470. array('option' => array('value' => 'other')),
  2471. 'bad',
  2472. '/option',
  2473. '/select',
  2474. '/div'
  2475. );
  2476. $this->assertTags($result, $expected);
  2477. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2478. $this->Form->request->data = array('User' => array('User' => array('value')));
  2479. $result = $this->Form->input('User.User', array('empty' => true));
  2480. $expected = array(
  2481. 'div' => array('class' => 'input select'),
  2482. 'label' => array('for' => 'UserUser'),
  2483. 'User',
  2484. '/label',
  2485. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2486. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2487. array('option' => array('value' => '')),
  2488. '/option',
  2489. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2490. 'good',
  2491. '/option',
  2492. array('option' => array('value' => 'other')),
  2493. 'bad',
  2494. '/option',
  2495. '/select',
  2496. '/div'
  2497. );
  2498. $this->assertTags($result, $expected);
  2499. $this->Form->data = array();
  2500. $result = $this->Form->input('Publisher.id', array(
  2501. 'label' => 'Publisher',
  2502. 'type' => 'select',
  2503. 'multiple' => 'checkbox',
  2504. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2505. ));
  2506. $expected = array(
  2507. array('div' => array('class' => 'input select')),
  2508. array('label' => array('for' => 'PublisherId')),
  2509. 'Publisher',
  2510. '/label',
  2511. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2512. array('div' => array('class' => 'checkbox')),
  2513. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2514. array('label' => array('for' => 'PublisherIdValue1')),
  2515. 'Label 1',
  2516. '/label',
  2517. '/div',
  2518. array('div' => array('class' => 'checkbox')),
  2519. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2520. array('label' => array('for' => 'PublisherIdValue2')),
  2521. 'Label 2',
  2522. '/label',
  2523. '/div',
  2524. '/div'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. }
  2528. /**
  2529. * test that input() and a non standard primary key makes a hidden input by default.
  2530. *
  2531. * @return void
  2532. */
  2533. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2534. $this->Form->create('User');
  2535. $this->Form->fieldset = array(
  2536. 'User' => array(
  2537. 'fields' => array(
  2538. 'model_id' => array('type' => 'integer')
  2539. ),
  2540. 'validates' => array(),
  2541. 'key' => 'model_id'
  2542. )
  2543. );
  2544. $result = $this->Form->input('model_id');
  2545. $expected = array(
  2546. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2547. );
  2548. $this->assertTags($result, $expected);
  2549. }
  2550. /**
  2551. * test that overriding the magic select type widget is possible
  2552. *
  2553. * @return void
  2554. */
  2555. public function testInputOverridingMagicSelectType() {
  2556. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2557. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2558. $expected = array(
  2559. 'div' => array('class' => 'input text'),
  2560. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2561. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2562. '/div'
  2563. );
  2564. $this->assertTags($result, $expected);
  2565. //Check that magic types still work for plural/singular vars
  2566. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2567. $result = $this->Form->input('Model.type');
  2568. $expected = array(
  2569. 'div' => array('class' => 'input select'),
  2570. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2571. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2572. array('option' => array('value' => 'value')), 'good', '/option',
  2573. array('option' => array('value' => 'other')), 'bad', '/option',
  2574. '/select',
  2575. '/div'
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. }
  2579. /**
  2580. * Test that magic input() selects are created for type=number
  2581. *
  2582. * @return void
  2583. */
  2584. public function testInputMagicSelectForTypeNumber() {
  2585. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2586. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2587. $result = $this->Form->input('ValidateUser.balance');
  2588. $expected = array(
  2589. 'div' => array('class' => 'input select'),
  2590. 'label' => array('for' => 'ValidateUserBalance'),
  2591. 'Balance',
  2592. '/label',
  2593. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2594. array('option' => array('value' => '0')),
  2595. 'nothing',
  2596. '/option',
  2597. array('option' => array('value' => '1', 'selected' => 'selected')),
  2598. 'some',
  2599. '/option',
  2600. array('option' => array('value' => '100')),
  2601. 'a lot',
  2602. '/option',
  2603. '/select',
  2604. '/div'
  2605. );
  2606. $this->assertTags($result, $expected);
  2607. }
  2608. /**
  2609. * Test that magic input() selects can easily be converted into radio types without error.
  2610. *
  2611. * @return void
  2612. */
  2613. public function testInputMagicSelectChangeToRadio() {
  2614. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2615. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2616. $this->assertRegExp('/input type="radio"/', $result);
  2617. }
  2618. /**
  2619. * fields with the same name as the model should work.
  2620. *
  2621. * @return void
  2622. */
  2623. public function testInputWithMatchingFieldAndModelName() {
  2624. $this->Form->create('User');
  2625. $this->Form->fieldset = array(
  2626. 'User' => array(
  2627. 'fields' => array(
  2628. 'User' => array('type' => 'text')
  2629. ),
  2630. 'validates' => array(),
  2631. 'key' => 'id'
  2632. )
  2633. );
  2634. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2635. $result = $this->Form->input('User', array('type' => 'text'));
  2636. $expected = array(
  2637. 'div' => array('class' => 'input text'),
  2638. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2639. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * testFormInputs method
  2646. *
  2647. * test correct results from form::inputs().
  2648. *
  2649. * @return void
  2650. */
  2651. public function testFormInputs() {
  2652. $this->Form->create('Contact');
  2653. $result = $this->Form->inputs('The Legend');
  2654. $expected = array(
  2655. '<fieldset',
  2656. '<legend',
  2657. 'The Legend',
  2658. '/legend',
  2659. '*/fieldset',
  2660. );
  2661. $this->assertTags($result, $expected);
  2662. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2663. $expected = array(
  2664. 'fieldset' => array('class' => 'classy-stuff'),
  2665. '<legend',
  2666. 'Field of Dreams',
  2667. '/legend',
  2668. '*/fieldset'
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2672. $this->assertTags($result, $expected);
  2673. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2674. $this->assertTags($result, $expected);
  2675. $this->Form->create('Contact');
  2676. $this->Form->request['prefix'] = 'admin';
  2677. $this->Form->request['action'] = 'admin_edit';
  2678. $result = $this->Form->inputs();
  2679. $expected = array(
  2680. '<fieldset',
  2681. '<legend',
  2682. 'Edit Contact',
  2683. '/legend',
  2684. '*/fieldset',
  2685. );
  2686. $this->assertTags($result, $expected);
  2687. $this->Form->create('Contact');
  2688. $result = $this->Form->inputs(false);
  2689. $expected = array(
  2690. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2691. array('div' => array('class' => 'input text')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input email')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input tel')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input password')),
  2698. '*/div',
  2699. array('div' => array('class' => 'input date')),
  2700. '*/div',
  2701. array('div' => array('class' => 'input date')),
  2702. '*/div',
  2703. array('div' => array('class' => 'input datetime')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input number')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input select')),
  2708. '*/div',
  2709. );
  2710. $this->assertTags($result, $expected);
  2711. $this->Form->create('Contact');
  2712. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2713. $expected = array(
  2714. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2715. array('div' => array('class' => 'input text')),
  2716. '*/div',
  2717. array('div' => array('class' => 'input email')),
  2718. '*/div',
  2719. array('div' => array('class' => 'input tel')),
  2720. '*/div',
  2721. array('div' => array('class' => 'input password')),
  2722. '*/div',
  2723. array('div' => array('class' => 'input date')),
  2724. '*/div',
  2725. array('div' => array('class' => 'input date')),
  2726. '*/div',
  2727. array('div' => array('class' => 'input datetime')),
  2728. '*/div',
  2729. array('div' => array('class' => 'input number')),
  2730. '*/div',
  2731. array('div' => array('class' => 'input select')),
  2732. '*/div',
  2733. );
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2737. $this->assertTags($result, $expected);
  2738. $this->Form->create('Contact');
  2739. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2740. $expected = array(
  2741. 'fieldset' => array(),
  2742. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2743. array('div' => array('class' => 'input text')),
  2744. '*/div',
  2745. array('div' => array('class' => 'input email')),
  2746. '*/div',
  2747. array('div' => array('class' => 'input tel')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input password')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input date')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input date')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input datetime')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input number')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input select')),
  2760. '*/div',
  2761. '/fieldset'
  2762. );
  2763. $this->assertTags($result, $expected);
  2764. $this->Form->create('Contact');
  2765. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2766. $expected = array(
  2767. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2768. array('div' => array('class' => 'input text')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input email')),
  2771. '*/div',
  2772. array('div' => array('class' => 'input tel')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input password')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input date')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input date')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input datetime')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input number')),
  2783. '*/div',
  2784. array('div' => array('class' => 'input select')),
  2785. '*/div',
  2786. );
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2790. $this->assertTags($result, $expected);
  2791. $this->Form->create('Contact');
  2792. $result = $this->Form->inputs('Hello');
  2793. $expected = array(
  2794. 'fieldset' => array(),
  2795. 'legend' => array(),
  2796. 'Hello',
  2797. '/legend',
  2798. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2799. array('div' => array('class' => 'input text')),
  2800. '*/div',
  2801. array('div' => array('class' => 'input email')),
  2802. '*/div',
  2803. array('div' => array('class' => 'input tel')),
  2804. '*/div',
  2805. array('div' => array('class' => 'input password')),
  2806. '*/div',
  2807. array('div' => array('class' => 'input date')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input date')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input datetime')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input number')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input select')),
  2816. '*/div',
  2817. '/fieldset'
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. $this->Form->create('Contact');
  2821. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2822. $expected = array(
  2823. 'fieldset' => array(),
  2824. 'legend' => array(),
  2825. 'Hello',
  2826. '/legend',
  2827. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2828. array('div' => array('class' => 'input text')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input email')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input tel')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input password')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input date')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input date')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input datetime')),
  2841. '*/div',
  2842. array('div' => array('class' => 'input number')),
  2843. '*/div',
  2844. array('div' => array('class' => 'input select')),
  2845. '*/div',
  2846. '/fieldset'
  2847. );
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->create('Contact');
  2850. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2851. $this->assertTags($result, $expected);
  2852. $this->Form->end();
  2853. $this->Form->create(false);
  2854. $expected = array(
  2855. 'fieldset' => array(),
  2856. array('div' => array('class' => 'input text')),
  2857. 'label' => array('for' => 'foo'),
  2858. 'Foo',
  2859. '/label',
  2860. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2861. '*/div',
  2862. '/fieldset'
  2863. );
  2864. $result = $this->Form->inputs(
  2865. array('foo' => array('type' => 'text')),
  2866. array(),
  2867. array('legend' => false)
  2868. );
  2869. $this->assertTags($result, $expected);
  2870. }
  2871. /**
  2872. * Tests inputs() works with plugin models
  2873. *
  2874. * @return void
  2875. */
  2876. public function testInputsPluginModel() {
  2877. $this->loadFixtures('Post');
  2878. App::build(array(
  2879. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2880. ));
  2881. CakePlugin::load('TestPlugin');
  2882. $this->Form->request['models'] = array(
  2883. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2884. );
  2885. $this->Form->create('TestPlugin.TestPluginPost');
  2886. $result = $this->Form->inputs();
  2887. $this->assertContains('data[TestPluginPost][id]', $result);
  2888. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2889. $this->assertContains('data[TestPluginPost][title]', $result);
  2890. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2891. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2892. $this->assertEquals('TestPluginPost', $this->Form->model());
  2893. }
  2894. /**
  2895. * testSelectAsCheckbox method
  2896. *
  2897. * test multi-select widget with checkbox formatting.
  2898. *
  2899. * @return void
  2900. */
  2901. public function testSelectAsCheckbox() {
  2902. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2903. $expected = array(
  2904. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2905. array('div' => array('class' => 'checkbox')),
  2906. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2907. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2908. 'first',
  2909. '/label',
  2910. '/div',
  2911. array('div' => array('class' => 'checkbox')),
  2912. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2913. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2914. 'second',
  2915. '/label',
  2916. '/div',
  2917. array('div' => array('class' => 'checkbox')),
  2918. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2919. array('label' => array('for' => 'ModelMultiField2')),
  2920. 'third',
  2921. '/label',
  2922. '/div',
  2923. );
  2924. $this->assertTags($result, $expected);
  2925. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2926. $expected = array(
  2927. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2928. array('div' => array('class' => 'checkbox')),
  2929. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2930. array('label' => array('for' => 'ModelMultiField12')),
  2931. 'half',
  2932. '/label',
  2933. '/div',
  2934. );
  2935. $this->assertTags($result, $expected);
  2936. }
  2937. /**
  2938. * testLabel method
  2939. *
  2940. * test label generation.
  2941. *
  2942. * @return void
  2943. */
  2944. public function testLabel() {
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $this->Form->text('Person.name');
  2949. $result = $this->Form->label();
  2950. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2951. $result = $this->Form->label('Person.first_name');
  2952. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2953. $result = $this->Form->label('Person.first_name', 'Your first name');
  2954. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2955. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2956. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2957. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2958. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2959. $result = $this->Form->label('Person.first_name', '');
  2960. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2961. $result = $this->Form->label('Person.2.name', '');
  2962. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2963. }
  2964. /**
  2965. * testTextbox method
  2966. *
  2967. * test textbox element generation
  2968. *
  2969. * @return void
  2970. */
  2971. public function testTextbox() {
  2972. $result = $this->Form->text('Model.field');
  2973. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2974. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2975. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2976. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2978. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2979. $result = $this->Form->text('Model.text');
  2980. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2981. $Contact = ClassRegistry::getObject('Contact');
  2982. $Contact->validationErrors['text'] = array(true);
  2983. $this->Form->request->data['Contact']['text'] = 'test';
  2984. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2985. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2986. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2987. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2988. $expected = array(
  2989. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. }
  2993. /**
  2994. * testDefaultValue method
  2995. *
  2996. * Test default value setting
  2997. *
  2998. * @return void
  2999. */
  3000. public function testDefaultValue() {
  3001. $this->Form->request->data['Model']['field'] = 'test';
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3004. unset($this->Form->request->data['Model']['field']);
  3005. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3006. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3007. }
  3008. /**
  3009. * testCheckboxDefaultValue method
  3010. *
  3011. * Test default value setting on checkbox() method
  3012. *
  3013. * @return void
  3014. */
  3015. public function testCheckboxDefaultValue() {
  3016. $this->Form->request->data['Model']['field'] = false;
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3019. unset($this->Form->request->data['Model']['field']);
  3020. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. $this->Form->request->data['Model']['field'] = true;
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3025. unset($this->Form->request->data['Model']['field']);
  3026. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3027. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3028. }
  3029. /**
  3030. * testError method
  3031. *
  3032. * Test field error generation
  3033. *
  3034. * @return void
  3035. */
  3036. public function testError() {
  3037. $Contact = ClassRegistry::getObject('Contact');
  3038. $Contact->validationErrors['field'] = array(1);
  3039. $result = $this->Form->error('Contact.field');
  3040. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('Error in field Field', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3045. $this->assertEquals('This field contains invalid input', $result);
  3046. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3047. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3048. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3049. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3050. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3051. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3052. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3053. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3054. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3055. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3056. $this->assertEquals('<strong>Badness!</strong>', $result);
  3057. $Contact->validationErrors['field'] = array("email");
  3058. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3059. $expected = array(
  3060. 'div' => array('class' => 'field-error'),
  3061. 'No good!',
  3062. '/div'
  3063. );
  3064. $this->assertTags($result, $expected);
  3065. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3066. $result = $this->Form->error('Contact.field', array(
  3067. 'notEmpty' => 'Cannot be empty',
  3068. 'email' => 'No good!'
  3069. ));
  3070. $expected = array(
  3071. 'div' => array('class' => 'error-message'),
  3072. 'ul' => array(),
  3073. '<li', 'Cannot be empty', '/li',
  3074. '<li', 'No good!', '/li',
  3075. '<li', 'Something else', '/li',
  3076. '/ul',
  3077. '/div'
  3078. );
  3079. $this->assertTags($result, $expected);
  3080. // Testing error messages list options
  3081. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3082. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3083. $expected = array(
  3084. 'div' => array('class' => 'error-message'),
  3085. 'ol' => array(),
  3086. '<li', 'notEmpty', '/li',
  3087. '<li', 'email', '/li',
  3088. '/ol',
  3089. '/div'
  3090. );
  3091. $this->assertTags($result, $expected);
  3092. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3093. $expected = array(
  3094. 'div' => array('class' => 'error-message'),
  3095. 'ol' => array(),
  3096. '<li', 'notEmpty', '/li',
  3097. '<li', 'email', '/li',
  3098. '/ol',
  3099. '/div'
  3100. );
  3101. $this->assertTags($result, $expected);
  3102. $result = $this->Form->error('Contact.field', null, array(
  3103. 'listOptions' => array(
  3104. 'class' => 'ul-class',
  3105. 'itemOptions' => array(
  3106. 'class' => 'li-class'
  3107. )
  3108. )
  3109. ));
  3110. $expected = array(
  3111. 'div' => array('class' => 'error-message'),
  3112. 'ul' => array('class' => 'ul-class'),
  3113. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3114. array('li' => array('class' => 'li-class')), 'email', '/li',
  3115. '/ul',
  3116. '/div'
  3117. );
  3118. $this->assertTags($result, $expected);
  3119. }
  3120. /**
  3121. * test error options when using form->input();
  3122. *
  3123. * @return void
  3124. */
  3125. public function testInputErrorEscape() {
  3126. $this->Form->create('ValidateProfile');
  3127. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3128. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3129. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3130. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3131. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3132. $this->assertRegExp('/required<br>/', $result);
  3133. }
  3134. /**
  3135. * testPassword method
  3136. *
  3137. * Test password element generation
  3138. *
  3139. * @return void
  3140. */
  3141. public function testPassword() {
  3142. $Contact = ClassRegistry::getObject('Contact');
  3143. $result = $this->Form->password('Contact.field');
  3144. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3145. $Contact->validationErrors['passwd'] = 1;
  3146. $this->Form->request->data['Contact']['passwd'] = 'test';
  3147. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3148. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3149. }
  3150. /**
  3151. * testRadio method
  3152. *
  3153. * Test radio element set generation
  3154. *
  3155. * @return void
  3156. */
  3157. public function testRadio() {
  3158. $result = $this->Form->radio('Model.field', array('option A'));
  3159. $expected = array(
  3160. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3161. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3162. 'label' => array('for' => 'ModelField0'),
  3163. 'option A',
  3164. '/label'
  3165. );
  3166. $this->assertTags($result, $expected);
  3167. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3168. $expected = array(
  3169. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3170. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3171. 'label' => array('for' => 'ModelField12'),
  3172. 'half',
  3173. '/label'
  3174. );
  3175. $this->assertTags($result, $expected);
  3176. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3177. $expected = array(
  3178. 'fieldset' => array(),
  3179. 'legend' => array(),
  3180. 'Field',
  3181. '/legend',
  3182. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3183. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3184. array('label' => array('for' => 'ModelField0')),
  3185. 'option A',
  3186. '/label',
  3187. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3188. array('label' => array('for' => 'ModelField1')),
  3189. 'option B',
  3190. '/label',
  3191. '/fieldset'
  3192. );
  3193. $this->assertTags($result, $expected);
  3194. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3195. $expected = array(
  3196. 'fieldset' => array(),
  3197. 'legend' => array(),
  3198. 'Field',
  3199. '/legend',
  3200. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3201. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3202. array('label' => array('for' => 'ModelField0')),
  3203. 'option A',
  3204. '/label',
  3205. 'br' => array(),
  3206. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3207. array('label' => array('for' => 'ModelField1')),
  3208. 'option B',
  3209. '/label',
  3210. '/fieldset'
  3211. );
  3212. $this->assertTags($result, $expected);
  3213. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3214. $expected = array(
  3215. 'fieldset' => array(),
  3216. 'legend' => array(),
  3217. 'Gender',
  3218. '/legend',
  3219. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3220. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3221. array('label' => array('for' => 'EmployeeGenderMale')),
  3222. 'Male',
  3223. '/label',
  3224. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3225. array('label' => array('for' => 'EmployeeGenderFemale')),
  3226. 'Female',
  3227. '/label',
  3228. '/fieldset',
  3229. );
  3230. $this->assertTags($result, $expected);
  3231. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3232. $expected = array(
  3233. 'fieldset' => array(),
  3234. 'legend' => array(),
  3235. 'Gender',
  3236. '/legend',
  3237. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3238. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3239. array('label' => array('for' => 'OfficerGenderMale')),
  3240. 'Male',
  3241. '/label',
  3242. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3243. array('label' => array('for' => 'OfficerGenderFemale')),
  3244. 'Female',
  3245. '/label',
  3246. '/fieldset',
  3247. );
  3248. $this->assertTags($result, $expected);
  3249. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3250. $expected = array(
  3251. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3252. array('input' => array(
  3253. 'type' => 'radio',
  3254. 'name' => 'data[Contact][1][imrequired]',
  3255. 'value' => '0',
  3256. 'id' => 'Contact1Imrequired0',
  3257. 'required' => 'required'
  3258. )),
  3259. 'label' => array('for' => 'Contact1Imrequired0'),
  3260. 'option A',
  3261. '/label'
  3262. );
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->radio('Model.1.field', array('option A'));
  3265. $expected = array(
  3266. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3267. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3268. 'label' => array('for' => 'Model1Field0'),
  3269. 'option A',
  3270. '/label'
  3271. );
  3272. $this->assertTags($result, $expected);
  3273. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3274. $expected = array(
  3275. 'fieldset' => array(),
  3276. 'legend' => array(),
  3277. 'Field',
  3278. '/legend',
  3279. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3281. array('label' => array('for' => 'ModelField0')),
  3282. 'option A',
  3283. '/label',
  3284. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3285. array('label' => array('for' => 'ModelField1')),
  3286. 'option B',
  3287. '/label',
  3288. '/fieldset'
  3289. );
  3290. $this->assertTags($result, $expected);
  3291. }
  3292. /**
  3293. * Test radio inputs with between as string or array. Also ensure
  3294. * that an array with less between elements works.
  3295. *
  3296. * @return void
  3297. */
  3298. public function testRadioBetween() {
  3299. $result = $this->Form->radio(
  3300. 'Model.field',
  3301. array('option A', 'option B'),
  3302. array('between' => 'I am between')
  3303. );
  3304. $expected = array(
  3305. 'fieldset' => array(),
  3306. 'legend' => array(),
  3307. 'Field',
  3308. '/legend',
  3309. 'I am between',
  3310. 'input' => array(
  3311. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3312. 'value' => '', 'id' => 'ModelField_'
  3313. ),
  3314. array('input' => array(
  3315. 'type' => 'radio', 'name' => 'data[Model][field]',
  3316. 'value' => '0', 'id' => 'ModelField0'
  3317. )),
  3318. array('label' => array('for' => 'ModelField0')),
  3319. 'option A',
  3320. '/label',
  3321. array('input' => array(
  3322. 'type' => 'radio', 'name' => 'data[Model][field]',
  3323. 'value' => '1', 'id' => 'ModelField1'
  3324. )),
  3325. array('label' => array('for' => 'ModelField1')),
  3326. 'option B',
  3327. '/label',
  3328. '/fieldset'
  3329. );
  3330. $this->assertTags($result, $expected);
  3331. $result = $this->Form->radio(
  3332. 'Model.field',
  3333. array('option A', 'option B', 'option C'),
  3334. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3335. );
  3336. $expected = array(
  3337. 'fieldset' => array(),
  3338. 'legend' => array(),
  3339. 'Field',
  3340. '/legend',
  3341. 'input' => array(
  3342. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3343. 'value' => '', 'id' => 'ModelField_'
  3344. ),
  3345. array('input' => array(
  3346. 'type' => 'radio', 'name' => 'data[Model][field]',
  3347. 'value' => '0', 'id' => 'ModelField0'
  3348. )),
  3349. array('label' => array('for' => 'ModelField0')),
  3350. 'option A',
  3351. '/label',
  3352. 'between A',
  3353. '--separator--',
  3354. array('input' => array(
  3355. 'type' => 'radio', 'name' => 'data[Model][field]',
  3356. 'value' => '1', 'id' => 'ModelField1'
  3357. )),
  3358. array('label' => array('for' => 'ModelField1')),
  3359. 'option B',
  3360. '/label',
  3361. 'between B',
  3362. '--separator--',
  3363. array('input' => array(
  3364. 'type' => 'radio', 'name' => 'data[Model][field]',
  3365. 'value' => '2', 'id' => 'ModelField2'
  3366. )),
  3367. array('label' => array('for' => 'ModelField2')),
  3368. 'option C',
  3369. '/label',
  3370. 'between C',
  3371. '/fieldset'
  3372. );
  3373. $this->assertTags($result, $expected);
  3374. $result = $this->Form->input('Model.field', array(
  3375. 'options' => array('1' => 'first', '2' => 'second'),
  3376. 'type' => 'radio',
  3377. 'before' => '--before--',
  3378. 'after' => '--after--',
  3379. 'separator' => '--separator--',
  3380. 'between' => array('--between first--', '--between second--')
  3381. ));
  3382. $expected = array(
  3383. 'div' => array('class' => 'input radio'),
  3384. '--before--',
  3385. 'fieldset' => array(),
  3386. 'legend' => array(),
  3387. 'Field',
  3388. '/legend',
  3389. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3390. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3391. array('label' => array('for' => 'ModelField1')),
  3392. 'first',
  3393. '/label',
  3394. '--between first--',
  3395. '--separator--',
  3396. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3397. array('label' => array('for' => 'ModelField2')),
  3398. 'second',
  3399. '/label',
  3400. '--between second--',
  3401. '/fieldset',
  3402. '--after--',
  3403. '/div'
  3404. );
  3405. $this->assertTags($result, $expected);
  3406. $result = $this->Form->input('Model.field', array(
  3407. 'options' => array('1' => 'first', '2' => 'second'),
  3408. 'type' => 'radio',
  3409. 'before' => '--before--',
  3410. 'after' => '--after--',
  3411. 'separator' => '--separator--',
  3412. 'between' => array('--between first--')
  3413. ));
  3414. $expected = array(
  3415. 'div' => array('class' => 'input radio'),
  3416. '--before--',
  3417. 'fieldset' => array(),
  3418. 'legend' => array(),
  3419. 'Field',
  3420. '/legend',
  3421. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3422. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3423. array('label' => array('for' => 'ModelField1')),
  3424. 'first',
  3425. '/label',
  3426. '--between first--',
  3427. '--separator--',
  3428. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3429. array('label' => array('for' => 'ModelField2')),
  3430. 'second',
  3431. '/label',
  3432. '/fieldset',
  3433. '--after--',
  3434. '/div'
  3435. );
  3436. $this->assertTags($result, $expected);
  3437. }
  3438. /**
  3439. * Test that radios with a 0 value are selected under the correct conditions.
  3440. * Also ensure that values that are booleanish are handled correctly.
  3441. *
  3442. * @return void
  3443. */
  3444. public function testRadioOptionWithBooleanishValues() {
  3445. $expected = array(
  3446. 'fieldset' => array(),
  3447. 'legend' => array(),
  3448. 'Field',
  3449. '/legend',
  3450. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3451. array('label' => array('for' => 'ModelField1')),
  3452. 'Yes',
  3453. '/label',
  3454. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3455. array('label' => array('for' => 'ModelField0')),
  3456. 'No',
  3457. '/label',
  3458. '/fieldset'
  3459. );
  3460. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3461. $this->assertTags($result, $expected);
  3462. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3463. $this->assertTags($result, $expected);
  3464. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3465. $this->assertTags($result, $expected);
  3466. $expected = array(
  3467. 'fieldset' => array(),
  3468. 'legend' => array(),
  3469. 'Field',
  3470. '/legend',
  3471. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3472. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3473. array('label' => array('for' => 'ModelField1')),
  3474. 'Yes',
  3475. '/label',
  3476. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3477. array('label' => array('for' => 'ModelField0')),
  3478. 'No',
  3479. '/label',
  3480. '/fieldset'
  3481. );
  3482. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3483. $this->assertTags($result, $expected);
  3484. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3485. $this->assertTags($result, $expected);
  3486. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3487. $this->assertTags($result, $expected);
  3488. $expected = array(
  3489. 'fieldset' => array(),
  3490. 'legend' => array(),
  3491. 'Field',
  3492. '/legend',
  3493. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3494. array('label' => array('for' => 'ModelField1')),
  3495. 'Yes',
  3496. '/label',
  3497. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3498. array('label' => array('for' => 'ModelField0')),
  3499. 'No',
  3500. '/label',
  3501. '/fieldset'
  3502. );
  3503. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3504. $this->assertTags($result, $expected);
  3505. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3506. $this->assertTags($result, $expected);
  3507. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3508. $this->assertTags($result, $expected);
  3509. }
  3510. /**
  3511. * test disabled radio options
  3512. *
  3513. * @return void
  3514. */
  3515. public function testRadioDisabled() {
  3516. $result = $this->Form->radio(
  3517. 'Model.field',
  3518. array('option A', 'option B'),
  3519. array('disabled' => array(0), 'value' => '0')
  3520. );
  3521. $expected = array(
  3522. 'fieldset' => array(),
  3523. 'legend' => array(),
  3524. 'Field',
  3525. '/legend',
  3526. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3527. array('label' => array('for' => 'ModelField0')),
  3528. 'option A',
  3529. '/label',
  3530. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3531. array('label' => array('for' => 'ModelField1')),
  3532. 'option B',
  3533. '/label',
  3534. '/fieldset'
  3535. );
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->radio(
  3538. 'Model.field',
  3539. array('option A', 'option B'),
  3540. array('disabled' => true, 'value' => '0')
  3541. );
  3542. $expected = array(
  3543. 'fieldset' => array(),
  3544. 'legend' => array(),
  3545. 'Field',
  3546. '/legend',
  3547. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3548. array('label' => array('for' => 'ModelField0')),
  3549. 'option A',
  3550. '/label',
  3551. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3552. array('label' => array('for' => 'ModelField1')),
  3553. 'option B',
  3554. '/label',
  3555. '/fieldset'
  3556. );
  3557. $this->assertTags($result, $expected);
  3558. $result = $this->Form->radio(
  3559. 'Model.field',
  3560. array('option A', 'option B'),
  3561. array('disabled' => 'disabled', 'value' => '0')
  3562. );
  3563. $expected = array(
  3564. 'fieldset' => array(),
  3565. 'legend' => array(),
  3566. 'Field',
  3567. '/legend',
  3568. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3569. array('label' => array('for' => 'ModelField0')),
  3570. 'option A',
  3571. '/label',
  3572. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3573. array('label' => array('for' => 'ModelField1')),
  3574. 'option B',
  3575. '/label',
  3576. '/fieldset'
  3577. );
  3578. $this->assertTags($result, $expected);
  3579. $result = $this->Form->input('Model.field', array(
  3580. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3581. 'type' => 'radio',
  3582. 'disabled' => array(2, '3x'),
  3583. ));
  3584. $expected = array(
  3585. 'div' => array('class' => 'input radio'),
  3586. 'fieldset' => array(),
  3587. 'legend' => array(),
  3588. 'Field',
  3589. '/legend',
  3590. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3591. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3592. array('label' => array('for' => 'ModelField1')),
  3593. 'first',
  3594. '/label',
  3595. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3596. array('label' => array('for' => 'ModelField2')),
  3597. 'second',
  3598. '/label',
  3599. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3600. array('label' => array('for' => 'ModelField2x')),
  3601. '2x',
  3602. '/label',
  3603. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3604. array('label' => array('for' => 'ModelField3')),
  3605. 'third',
  3606. '/label',
  3607. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3608. array('label' => array('for' => 'ModelField3x')),
  3609. '3x',
  3610. '/label',
  3611. '/fieldset',
  3612. '/div'
  3613. );
  3614. $this->assertTags($result, $expected);
  3615. }
  3616. /**
  3617. * test disabling the hidden input for radio buttons
  3618. *
  3619. * @return void
  3620. */
  3621. public function testRadioHiddenInputDisabling() {
  3622. $result = $this->Form->input('Model.1.field', array(
  3623. 'type' => 'radio',
  3624. 'options' => array('option A'),
  3625. 'hiddenField' => false
  3626. )
  3627. );
  3628. $expected = array(
  3629. 'div' => array('class' => 'input radio'),
  3630. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3631. 'label' => array('for' => 'Model1Field0'),
  3632. 'option A',
  3633. '/label',
  3634. '/div'
  3635. );
  3636. $this->assertTags($result, $expected);
  3637. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3638. $expected = array(
  3639. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3640. 'label' => array('for' => 'Model1Field0'),
  3641. 'option A',
  3642. '/label'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. }
  3646. /**
  3647. * test adding an empty option for radio buttons
  3648. *
  3649. * @return void
  3650. */
  3651. public function testRadioAddEmptyOption() {
  3652. $result = $this->Form->input('Model.1.field', array(
  3653. 'type' => 'radio',
  3654. 'options' => array('option A'),
  3655. 'empty' => true,
  3656. 'hiddenField' => false
  3657. ));
  3658. $expected = array(
  3659. 'div' => array('class' => 'input radio'),
  3660. 'fieldset' => array(),
  3661. 'legend' => array(),
  3662. 'Field',
  3663. '/legend',
  3664. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3665. array('label' => array('for' => 'Model1Field')),
  3666. __('empty'),
  3667. '/label',
  3668. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3669. array('label' => array('for' => 'Model1Field0')),
  3670. 'option A',
  3671. '/label',
  3672. '/fieldset',
  3673. '/div'
  3674. );
  3675. $this->assertTags($result, $expected);
  3676. $result = $this->Form->input('Model.1.field', array(
  3677. 'type' => 'radio',
  3678. 'options' => array('option A'),
  3679. 'empty' => 'CustomEmptyLabel',
  3680. 'hiddenField' => false
  3681. ));
  3682. $expected = array(
  3683. 'div' => array('class' => 'input radio'),
  3684. 'fieldset' => array(),
  3685. 'legend' => array(),
  3686. 'Field',
  3687. '/legend',
  3688. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3689. array('label' => array('for' => 'Model1Field')),
  3690. 'CustomEmptyLabel',
  3691. '/label',
  3692. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3693. array('label' => array('for' => 'Model1Field0')),
  3694. 'option A',
  3695. '/label',
  3696. '/fieldset',
  3697. '/div'
  3698. );
  3699. $this->assertTags($result, $expected);
  3700. $result = $this->Form->input('Model.1.field', array(
  3701. 'type' => 'radio',
  3702. 'options' => array('option A'),
  3703. 'empty' => false,
  3704. 'hiddenField' => false
  3705. ));
  3706. $this->assertTextNotContains('"Model1Field"', $result);
  3707. }
  3708. /**
  3709. * testSelect method
  3710. *
  3711. * Test select element generation.
  3712. *
  3713. * @return void
  3714. */
  3715. public function testSelect() {
  3716. $result = $this->Form->select('Model.field', array());
  3717. $expected = array(
  3718. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3719. array('option' => array('value' => '')),
  3720. '/option',
  3721. '/select'
  3722. );
  3723. $this->assertTags($result, $expected);
  3724. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3725. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3726. $expected = array(
  3727. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3728. array('option' => array('value' => '')),
  3729. '/option',
  3730. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3731. 'good',
  3732. '/option',
  3733. array('option' => array('value' => 'other')),
  3734. 'bad',
  3735. '/option',
  3736. '/select'
  3737. );
  3738. $this->assertTags($result, $expected);
  3739. $this->Form->request->data = array();
  3740. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3741. $expected = array(
  3742. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3743. array('option' => array('value' => '')),
  3744. '/option',
  3745. array('option' => array('value' => 'value')),
  3746. 'good',
  3747. '/option',
  3748. array('option' => array('value' => 'other')),
  3749. 'bad',
  3750. '/option',
  3751. '/select'
  3752. );
  3753. $this->assertTags($result, $expected);
  3754. $result = $this->Form->select(
  3755. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3756. array('empty' => false)
  3757. );
  3758. $expected = array(
  3759. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3760. array('option' => array('value' => 'first')),
  3761. 'first &quot;html&quot; &lt;chars&gt;',
  3762. '/option',
  3763. array('option' => array('value' => 'second')),
  3764. 'value',
  3765. '/option',
  3766. '/select'
  3767. );
  3768. $this->assertTags($result, $expected);
  3769. $result = $this->Form->select(
  3770. 'Model.field',
  3771. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3772. array('escape' => false, 'empty' => false)
  3773. );
  3774. $expected = array(
  3775. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3776. array('option' => array('value' => 'first')),
  3777. 'first "html" <chars>',
  3778. '/option',
  3779. array('option' => array('value' => 'second')),
  3780. 'value',
  3781. '/option',
  3782. '/select'
  3783. );
  3784. $this->assertTags($result, $expected);
  3785. $options = array(
  3786. array('value' => 'first', 'name' => 'First'),
  3787. array('value' => 'first', 'name' => 'Another First'),
  3788. );
  3789. $result = $this->Form->select(
  3790. 'Model.field',
  3791. $options,
  3792. array('escape' => false, 'empty' => false)
  3793. );
  3794. $expected = array(
  3795. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3796. array('option' => array('value' => 'first')),
  3797. 'First',
  3798. '/option',
  3799. array('option' => array('value' => 'first')),
  3800. 'Another First',
  3801. '/option',
  3802. '/select'
  3803. );
  3804. $this->assertTags($result, $expected);
  3805. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3806. $result = $this->Form->select(
  3807. 'Model.contact_id',
  3808. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3809. array('escape' => false, 'empty' => 'pick something')
  3810. );
  3811. $expected = array(
  3812. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3813. array('option' => array('value' => '')), 'pick something', '/option',
  3814. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3815. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3816. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3817. '/select'
  3818. );
  3819. $this->assertTags($result, $expected);
  3820. $this->Form->request->data['Model']['field'] = 0;
  3821. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3822. $expected = array(
  3823. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3824. array('option' => array('value' => '')), '/option',
  3825. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3826. array('option' => array('value' => '1')), 'Yes', '/option',
  3827. '/select'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. $this->Form->request->data['Model']['field'] = 50;
  3831. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3832. $expected = array(
  3833. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3834. array('option' => array('value' => '')), '/option',
  3835. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3836. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3837. '/select'
  3838. );
  3839. $this->assertTags($result, $expected);
  3840. }
  3841. /**
  3842. * test that select() with optiongroups listens to the escape param.
  3843. *
  3844. * @return void
  3845. */
  3846. public function testSelectOptionGroupEscaping() {
  3847. $options = array(
  3848. '>< Key' => array(
  3849. 1 => 'One',
  3850. 2 => 'Two'
  3851. )
  3852. );
  3853. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3854. $expected = array(
  3855. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3856. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3857. array('option' => array('value' => '1')), 'One', '/option',
  3858. array('option' => array('value' => '2')), 'Two', '/option',
  3859. '/optgroup',
  3860. '/select'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. $options = array(
  3864. '>< Key' => array(
  3865. 1 => 'One',
  3866. 2 => 'Two'
  3867. )
  3868. );
  3869. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3870. $expected = array(
  3871. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3872. 'optgroup' => array('label' => '>< Key'),
  3873. array('option' => array('value' => '1')), 'One', '/option',
  3874. array('option' => array('value' => '2')), 'Two', '/option',
  3875. '/optgroup',
  3876. '/select'
  3877. );
  3878. $this->assertTags($result, $expected);
  3879. }
  3880. /**
  3881. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3882. *
  3883. * @return void
  3884. */
  3885. public function testSelectWithNullAttributes() {
  3886. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3887. $expected = array(
  3888. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3889. array('option' => array('value' => '0')),
  3890. 'first',
  3891. '/option',
  3892. array('option' => array('value' => '1')),
  3893. 'second',
  3894. '/option',
  3895. '/select'
  3896. );
  3897. $this->assertTags($result, $expected);
  3898. }
  3899. /**
  3900. * testNestedSelect method
  3901. *
  3902. * test select element generation with optgroups
  3903. *
  3904. * @return void
  3905. */
  3906. public function testNestedSelect() {
  3907. $result = $this->Form->select(
  3908. 'Model.field',
  3909. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3910. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3911. )), array('empty' => false)
  3912. );
  3913. $expected = array(
  3914. 'select' => array('name' => 'data[Model][field]',
  3915. 'id' => 'ModelField'),
  3916. array('option' => array('value' => 1)),
  3917. 'One',
  3918. '/option',
  3919. array('option' => array('value' => 2)),
  3920. 'Two',
  3921. '/option',
  3922. array('optgroup' => array('label' => 'Three')),
  3923. array('option' => array('value' => 4)),
  3924. 'Four',
  3925. '/option',
  3926. array('option' => array('value' => 5)),
  3927. 'Five',
  3928. '/option',
  3929. '/optgroup',
  3930. '/select'
  3931. );
  3932. $this->assertTags($result, $expected);
  3933. $result = $this->Form->select(
  3934. 'Model.field',
  3935. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3936. array('showParents' => true, 'empty' => false)
  3937. );
  3938. $expected = array(
  3939. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3940. array('option' => array('value' => 1)),
  3941. 'One',
  3942. '/option',
  3943. array('option' => array('value' => 2)),
  3944. 'Two',
  3945. '/option',
  3946. array('optgroup' => array('label' => 'Three')),
  3947. array('option' => array('value' => 3)),
  3948. 'Three',
  3949. '/option',
  3950. array('option' => array('value' => 4)),
  3951. 'Four',
  3952. '/option',
  3953. '/optgroup',
  3954. '/select'
  3955. );
  3956. $this->assertTags($result, $expected);
  3957. }
  3958. /**
  3959. * testSelectMultiple method
  3960. *
  3961. * test generation of multiple select elements
  3962. *
  3963. * @return void
  3964. */
  3965. public function testSelectMultiple() {
  3966. $options = array('first', 'second', 'third');
  3967. $result = $this->Form->select(
  3968. 'Model.multi_field', $options, array('multiple' => true)
  3969. );
  3970. $expected = array(
  3971. 'input' => array(
  3972. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3973. ),
  3974. 'select' => array(
  3975. 'name' => 'data[Model][multi_field][]',
  3976. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3977. ),
  3978. array('option' => array('value' => '0')),
  3979. 'first',
  3980. '/option',
  3981. array('option' => array('value' => '1')),
  3982. 'second',
  3983. '/option',
  3984. array('option' => array('value' => '2')),
  3985. 'third',
  3986. '/option',
  3987. '/select'
  3988. );
  3989. $this->assertTags($result, $expected);
  3990. $result = $this->Form->select(
  3991. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3992. );
  3993. $expected = array(
  3994. 'input' => array(
  3995. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3996. ),
  3997. 'select' => array(
  3998. 'name' => 'data[Model][multi_field][]',
  3999. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4000. ),
  4001. array('option' => array('value' => '0')),
  4002. 'first',
  4003. '/option',
  4004. array('option' => array('value' => '1')),
  4005. 'second',
  4006. '/option',
  4007. array('option' => array('value' => '2')),
  4008. 'third',
  4009. '/option',
  4010. '/select'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. $result = $this->Form->select(
  4014. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4015. );
  4016. $expected = array(
  4017. 'input' => array(
  4018. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4019. ),
  4020. 'select' => array(
  4021. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4022. 'multiple' => 'multiple'
  4023. ),
  4024. array('option' => array('value' => '0', 'selected' => 'selected')),
  4025. 'first',
  4026. '/option',
  4027. array('option' => array('value' => '1', 'selected' => 'selected')),
  4028. 'second',
  4029. '/option',
  4030. array('option' => array('value' => '2')),
  4031. 'third',
  4032. '/option',
  4033. '/select'
  4034. );
  4035. $this->assertTags($result, $expected);
  4036. $result = $this->Form->select(
  4037. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4038. );
  4039. $expected = array(
  4040. 'select' => array(
  4041. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4042. ),
  4043. array('option' => array('value' => '0', 'selected' => 'selected')),
  4044. 'first',
  4045. '/option',
  4046. array('option' => array('value' => '1', 'selected' => 'selected')),
  4047. 'second',
  4048. '/option',
  4049. array('option' => array('value' => '2')),
  4050. 'third',
  4051. '/option',
  4052. '/select'
  4053. );
  4054. $this->assertTags($result, $expected);
  4055. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4056. $selected = array('2', '3x');
  4057. $result = $this->Form->select(
  4058. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4059. );
  4060. $expected = array(
  4061. 'input' => array(
  4062. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4063. ),
  4064. 'select' => array(
  4065. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4066. ),
  4067. array('option' => array('value' => '1')),
  4068. 'One',
  4069. '/option',
  4070. array('option' => array('value' => '2', 'selected' => 'selected')),
  4071. 'Two',
  4072. '/option',
  4073. array('option' => array('value' => '3')),
  4074. 'Three',
  4075. '/option',
  4076. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4077. 'Stringy',
  4078. '/option',
  4079. '/select'
  4080. );
  4081. $this->assertTags($result, $expected);
  4082. }
  4083. /**
  4084. * Test generating multiple select with disabled elements.
  4085. *
  4086. * @return void
  4087. */
  4088. public function testSelectMultipleWithDisabledElements() {
  4089. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4090. $disabled = array(2, 3);
  4091. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4092. $expected = array(
  4093. 'input' => array(
  4094. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4095. ),
  4096. 'select' => array(
  4097. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4098. ),
  4099. array('option' => array('value' => '1')),
  4100. 'One',
  4101. '/option',
  4102. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4103. 'Two',
  4104. '/option',
  4105. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4106. 'Three',
  4107. '/option',
  4108. array('option' => array('value' => '3x')),
  4109. 'Stringy',
  4110. '/option',
  4111. '/select'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4115. $disabled = array('2', '3x');
  4116. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4117. $expected = array(
  4118. array('div' => array('class' => 'input select')),
  4119. array('label' => array('for' => 'ContactMultiple')),
  4120. 'Multiple',
  4121. '/label',
  4122. 'input' => array(
  4123. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4124. ),
  4125. 'select' => array(
  4126. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4127. ),
  4128. array('option' => array('value' => '1')),
  4129. 'One',
  4130. '/option',
  4131. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4132. 'Two',
  4133. '/option',
  4134. array('option' => array('value' => '3')),
  4135. 'Three',
  4136. '/option',
  4137. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4138. 'Stringy',
  4139. '/option',
  4140. '/select',
  4141. '/div'
  4142. );
  4143. $this->assertTags($result, $expected);
  4144. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4145. $disabled = true;
  4146. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4147. $expected = array(
  4148. array('div' => array('class' => 'input select')),
  4149. array('label' => array('for' => 'ContactMultiple')),
  4150. 'Multiple',
  4151. '/label',
  4152. 'input' => array(
  4153. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4154. ),
  4155. 'select' => array(
  4156. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4157. ),
  4158. array('option' => array('value' => '1')),
  4159. 'One',
  4160. '/option',
  4161. array('option' => array('value' => '2')),
  4162. 'Two',
  4163. '/option',
  4164. array('option' => array('value' => '3')),
  4165. 'Three',
  4166. '/option',
  4167. array('option' => array('value' => '3x')),
  4168. 'Stringy',
  4169. '/option',
  4170. '/select',
  4171. '/div'
  4172. );
  4173. $this->assertTags($result, $expected);
  4174. }
  4175. /**
  4176. * Test generating select with disabled elements.
  4177. *
  4178. * @return void
  4179. */
  4180. public function testSelectWithDisabledElements() {
  4181. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4182. $disabled = array(2, 3);
  4183. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4184. $expected = array(
  4185. 'select' => array(
  4186. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4187. ),
  4188. array('option' => array('value' => '')),
  4189. '/option',
  4190. array('option' => array('value' => '1')),
  4191. 'One',
  4192. '/option',
  4193. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4194. 'Two',
  4195. '/option',
  4196. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4197. 'Three',
  4198. '/option',
  4199. array('option' => array('value' => '3x')),
  4200. 'Stringy',
  4201. '/option',
  4202. '/select'
  4203. );
  4204. $this->assertTags($result, $expected);
  4205. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4206. $disabled = array('2', '3x');
  4207. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4208. $expected = array(
  4209. array('div' => array('class' => 'input select')),
  4210. array('label' => array('for' => 'ModelField')),
  4211. 'Field',
  4212. '/label',
  4213. 'select' => array(
  4214. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4215. ),
  4216. array('option' => array('value' => '1')),
  4217. 'One',
  4218. '/option',
  4219. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4220. 'Two',
  4221. '/option',
  4222. array('option' => array('value' => '3')),
  4223. 'Three',
  4224. '/option',
  4225. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4226. 'Stringy',
  4227. '/option',
  4228. '/select',
  4229. '/div'
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4233. $disabled = true;
  4234. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4235. $expected = array(
  4236. array('div' => array('class' => 'input select')),
  4237. array('label' => array('for' => 'ModelField')),
  4238. 'Field',
  4239. '/label',
  4240. 'select' => array(
  4241. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4242. ),
  4243. array('option' => array('value' => '1')),
  4244. 'One',
  4245. '/option',
  4246. array('option' => array('value' => '2')),
  4247. 'Two',
  4248. '/option',
  4249. array('option' => array('value' => '3')),
  4250. 'Three',
  4251. '/option',
  4252. array('option' => array('value' => '3x')),
  4253. 'Stringy',
  4254. '/option',
  4255. '/select',
  4256. '/div'
  4257. );
  4258. $this->assertTags($result, $expected);
  4259. }
  4260. /**
  4261. * test generation of habtm select boxes.
  4262. *
  4263. * @return void
  4264. */
  4265. public function testHabtmSelectBox() {
  4266. $this->View->viewVars['contactTags'] = array(
  4267. 1 => 'blue',
  4268. 2 => 'red',
  4269. 3 => 'green'
  4270. );
  4271. $this->Form->request->data = array(
  4272. 'Contact' => array(),
  4273. 'ContactTag' => array(
  4274. array(
  4275. 'id' => '1',
  4276. 'name' => 'blue'
  4277. ),
  4278. array(
  4279. 'id' => 3,
  4280. 'name' => 'green'
  4281. )
  4282. )
  4283. );
  4284. $this->Form->create('Contact');
  4285. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4286. $expected = array(
  4287. 'input' => array(
  4288. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4289. ),
  4290. 'select' => array(
  4291. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4292. 'multiple' => 'multiple'
  4293. ),
  4294. array('option' => array('value' => '1', 'selected' => 'selected')),
  4295. 'blue',
  4296. '/option',
  4297. array('option' => array('value' => '2')),
  4298. 'red',
  4299. '/option',
  4300. array('option' => array('value' => '3', 'selected' => 'selected')),
  4301. 'green',
  4302. '/option',
  4303. '/select'
  4304. );
  4305. $this->assertTags($result, $expected);
  4306. // make sure only 50 is selected, and not 50f5c0cf
  4307. $this->View->viewVars['contactTags'] = array(
  4308. '1' => 'blue',
  4309. '50f5c0cf' => 'red',
  4310. '50' => 'green'
  4311. );
  4312. $this->Form->request->data = array(
  4313. 'Contact' => array(),
  4314. 'ContactTag' => array(
  4315. array(
  4316. 'id' => 1,
  4317. 'name' => 'blue'
  4318. ),
  4319. array(
  4320. 'id' => 50,
  4321. 'name' => 'green'
  4322. )
  4323. )
  4324. );
  4325. $this->Form->create('Contact');
  4326. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4327. $expected = array(
  4328. 'input' => array(
  4329. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4330. ),
  4331. 'select' => array(
  4332. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4333. 'multiple' => 'multiple'
  4334. ),
  4335. array('option' => array('value' => '1', 'selected' => 'selected')),
  4336. 'blue',
  4337. '/option',
  4338. array('option' => array('value' => '50f5c0cf')),
  4339. 'red',
  4340. '/option',
  4341. array('option' => array('value' => '50', 'selected' => 'selected')),
  4342. 'green',
  4343. '/option',
  4344. '/select'
  4345. );
  4346. $this->assertTags($result, $expected);
  4347. }
  4348. /**
  4349. * test generation of multi select elements in checkbox format
  4350. *
  4351. * @return void
  4352. */
  4353. public function testSelectMultipleCheckboxes() {
  4354. $result = $this->Form->select(
  4355. 'Model.multi_field',
  4356. array('first', 'second', 'third'),
  4357. array('multiple' => 'checkbox')
  4358. );
  4359. $expected = array(
  4360. 'input' => array(
  4361. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4362. ),
  4363. array('div' => array('class' => 'checkbox')),
  4364. array('input' => array(
  4365. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4366. 'value' => '0', 'id' => 'ModelMultiField0'
  4367. )),
  4368. array('label' => array('for' => 'ModelMultiField0')),
  4369. 'first',
  4370. '/label',
  4371. '/div',
  4372. array('div' => array('class' => 'checkbox')),
  4373. array('input' => array(
  4374. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4375. 'value' => '1', 'id' => 'ModelMultiField1'
  4376. )),
  4377. array('label' => array('for' => 'ModelMultiField1')),
  4378. 'second',
  4379. '/label',
  4380. '/div',
  4381. array('div' => array('class' => 'checkbox')),
  4382. array('input' => array(
  4383. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4384. 'value' => '2', 'id' => 'ModelMultiField2'
  4385. )),
  4386. array('label' => array('for' => 'ModelMultiField2')),
  4387. 'third',
  4388. '/label',
  4389. '/div'
  4390. );
  4391. $this->assertTags($result, $expected);
  4392. $result = $this->Form->select(
  4393. 'Model.multi_field',
  4394. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4395. array('multiple' => 'checkbox')
  4396. );
  4397. $expected = array(
  4398. 'input' => array(
  4399. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4400. ),
  4401. array('div' => array('class' => 'checkbox')),
  4402. array('input' => array(
  4403. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4404. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4405. )),
  4406. array('label' => array('for' => 'ModelMultiFieldA')),
  4407. 'first',
  4408. '/label',
  4409. '/div',
  4410. array('div' => array('class' => 'checkbox')),
  4411. array('input' => array(
  4412. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4413. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4414. )),
  4415. array('label' => array('for' => 'ModelMultiFieldB')),
  4416. 'second',
  4417. '/label',
  4418. '/div',
  4419. array('div' => array('class' => 'checkbox')),
  4420. array('input' => array(
  4421. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4422. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4423. )),
  4424. array('label' => array('for' => 'ModelMultiFieldC')),
  4425. 'third',
  4426. '/label',
  4427. '/div'
  4428. );
  4429. $this->assertTags($result, $expected);
  4430. $result = $this->Form->select(
  4431. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4432. );
  4433. $expected = array(
  4434. 'input' => array(
  4435. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4436. ),
  4437. array('div' => array('class' => 'checkbox')),
  4438. array('input' => array(
  4439. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4440. 'value' => '1', 'id' => 'ModelMultiField1'
  4441. )),
  4442. array('label' => array('for' => 'ModelMultiField1')),
  4443. 'first',
  4444. '/label',
  4445. '/div'
  4446. );
  4447. $this->assertTags($result, $expected);
  4448. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4449. $result = $this->Form->select(
  4450. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4451. );
  4452. $expected = array(
  4453. 'input' => array(
  4454. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4455. ),
  4456. array('div' => array('class' => 'checkbox')),
  4457. array('input' => array(
  4458. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4459. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4460. )),
  4461. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4462. 'first',
  4463. '/label',
  4464. '/div',
  4465. array('div' => array('class' => 'checkbox')),
  4466. array('input' => array(
  4467. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4468. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4469. )),
  4470. array('label' => array('for' => 'ModelTagsArray')),
  4471. 'Array',
  4472. '/label',
  4473. '/div'
  4474. );
  4475. $this->assertTags($result, $expected);
  4476. }
  4477. /**
  4478. * test multiple checkboxes with div styles.
  4479. *
  4480. * @return void
  4481. */
  4482. public function testSelectMultipleCheckboxDiv() {
  4483. $result = $this->Form->select(
  4484. 'Model.tags',
  4485. array('first', 'second'),
  4486. array('multiple' => 'checkbox', 'class' => 'my-class')
  4487. );
  4488. $expected = array(
  4489. 'input' => array(
  4490. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4491. ),
  4492. array('div' => array('class' => 'my-class')),
  4493. array('input' => array(
  4494. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4495. 'value' => '0', 'id' => 'ModelTags0'
  4496. )),
  4497. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4498. '/div',
  4499. array('div' => array('class' => 'my-class')),
  4500. array('input' => array(
  4501. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4502. 'value' => '1', 'id' => 'ModelTags1'
  4503. )),
  4504. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4505. '/div'
  4506. );
  4507. $this->assertTags($result, $expected);
  4508. $result = $this->Form->input('Model.tags', array(
  4509. 'options' => array('first', 'second'),
  4510. 'multiple' => 'checkbox',
  4511. 'class' => 'my-class',
  4512. 'div' => false,
  4513. 'label' => false
  4514. ));
  4515. $this->assertTags($result, $expected);
  4516. $Contact = ClassRegistry::getObject('Contact');
  4517. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4518. $result = $this->Form->input('Contact.tags', array(
  4519. 'options' => array('one'),
  4520. 'multiple' => 'checkbox',
  4521. 'label' => false,
  4522. 'div' => false
  4523. ));
  4524. $expected = array(
  4525. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4526. array('div' => array('class' => 'checkbox form-error')),
  4527. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4528. array('label' => array('for' => 'ContactTags0')),
  4529. 'one',
  4530. '/label',
  4531. '/div'
  4532. );
  4533. $this->assertTags($result, $expected);
  4534. $result = $this->Form->input('Contact.tags', array(
  4535. 'options' => array('one'),
  4536. 'multiple' => 'checkbox',
  4537. 'class' => 'mycheckbox',
  4538. 'label' => false,
  4539. 'div' => false
  4540. ));
  4541. $expected = array(
  4542. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4543. array('div' => array('class' => 'mycheckbox form-error')),
  4544. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4545. array('label' => array('for' => 'ContactTags0')),
  4546. 'one',
  4547. '/label',
  4548. '/div'
  4549. );
  4550. $this->assertTags($result, $expected);
  4551. }
  4552. /**
  4553. * Checks the security hash array generated for multiple-input checkbox elements
  4554. *
  4555. * @return void
  4556. */
  4557. public function testSelectMultipleCheckboxSecurity() {
  4558. $this->Form->request['_Token'] = array('key' => 'testKey');
  4559. $this->assertEquals(array(), $this->Form->fields);
  4560. $result = $this->Form->select(
  4561. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4562. array('multiple' => 'checkbox')
  4563. );
  4564. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4565. $result = $this->Form->secure($this->Form->fields);
  4566. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4567. $this->assertRegExp('/"' . $key . '"/', $result);
  4568. }
  4569. /**
  4570. * Multiple select elements should always be secured as they always participate
  4571. * in the POST data.
  4572. *
  4573. * @return void
  4574. */
  4575. public function testSelectMultipleSecureWithNoOptions() {
  4576. $this->Form->request['_Token'] = array('key' => 'testkey');
  4577. $this->assertEquals(array(), $this->Form->fields);
  4578. $this->Form->select(
  4579. 'Model.select',
  4580. array(),
  4581. array('multiple' => true)
  4582. );
  4583. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4584. }
  4585. /**
  4586. * When a select box has no options it should not be added to the fields list
  4587. * as it always fail post validation.
  4588. *
  4589. * @return void
  4590. */
  4591. public function testSelectNoSecureWithNoOptions() {
  4592. $this->Form->request['_Token'] = array('key' => 'testkey');
  4593. $this->assertEquals(array(), $this->Form->fields);
  4594. $this->Form->select(
  4595. 'Model.select',
  4596. array()
  4597. );
  4598. $this->assertEquals(array(), $this->Form->fields);
  4599. $this->Form->select(
  4600. 'Model.select',
  4601. array(),
  4602. array('empty' => true)
  4603. );
  4604. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4605. }
  4606. /**
  4607. * testInputMultipleCheckboxes method
  4608. *
  4609. * test input() resulting in multi select elements being generated.
  4610. *
  4611. * @return void
  4612. */
  4613. public function testInputMultipleCheckboxes() {
  4614. $result = $this->Form->input('Model.multi_field', array(
  4615. 'options' => array('first', 'second', 'third'),
  4616. 'multiple' => 'checkbox'
  4617. ));
  4618. $expected = array(
  4619. array('div' => array('class' => 'input select')),
  4620. array('label' => array('for' => 'ModelMultiField')),
  4621. 'Multi Field',
  4622. '/label',
  4623. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4624. array('div' => array('class' => 'checkbox')),
  4625. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4626. array('label' => array('for' => 'ModelMultiField0')),
  4627. 'first',
  4628. '/label',
  4629. '/div',
  4630. array('div' => array('class' => 'checkbox')),
  4631. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4632. array('label' => array('for' => 'ModelMultiField1')),
  4633. 'second',
  4634. '/label',
  4635. '/div',
  4636. array('div' => array('class' => 'checkbox')),
  4637. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4638. array('label' => array('for' => 'ModelMultiField2')),
  4639. 'third',
  4640. '/label',
  4641. '/div',
  4642. '/div'
  4643. );
  4644. $this->assertTags($result, $expected);
  4645. $result = $this->Form->input('Model.multi_field', array(
  4646. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4647. 'multiple' => 'checkbox'
  4648. ));
  4649. $expected = array(
  4650. array('div' => array('class' => 'input select')),
  4651. array('label' => array('for' => 'ModelMultiField')),
  4652. 'Multi Field',
  4653. '/label',
  4654. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4655. array('div' => array('class' => 'checkbox')),
  4656. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4657. array('label' => array('for' => 'ModelMultiFieldA')),
  4658. 'first',
  4659. '/label',
  4660. '/div',
  4661. array('div' => array('class' => 'checkbox')),
  4662. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4663. array('label' => array('for' => 'ModelMultiFieldB')),
  4664. 'second',
  4665. '/label',
  4666. '/div',
  4667. array('div' => array('class' => 'checkbox')),
  4668. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4669. array('label' => array('for' => 'ModelMultiFieldC')),
  4670. 'third',
  4671. '/label',
  4672. '/div',
  4673. '/div'
  4674. );
  4675. $this->assertTags($result, $expected);
  4676. $result = $this->Form->input('Model.multi_field', array(
  4677. 'options' => array('1' => 'first'),
  4678. 'multiple' => 'checkbox',
  4679. 'label' => false,
  4680. 'div' => false
  4681. ));
  4682. $expected = array(
  4683. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4684. array('div' => array('class' => 'checkbox')),
  4685. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4686. array('label' => array('for' => 'ModelMultiField1')),
  4687. 'first',
  4688. '/label',
  4689. '/div'
  4690. );
  4691. $this->assertTags($result, $expected);
  4692. $result = $this->Form->input('Model.multi_field', array(
  4693. 'options' => array('2' => 'second'),
  4694. 'multiple' => 'checkbox',
  4695. 'label' => false,
  4696. 'div' => false
  4697. ));
  4698. $expected = array(
  4699. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4700. array('div' => array('class' => 'checkbox')),
  4701. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4702. array('label' => array('for' => 'ModelMultiField2')),
  4703. 'second',
  4704. '/label',
  4705. '/div'
  4706. );
  4707. $this->assertTags($result, $expected);
  4708. }
  4709. /**
  4710. * testSelectHiddenFieldOmission method
  4711. *
  4712. * test that select() with 'hiddenField' => false omits the hidden field
  4713. *
  4714. * @return void
  4715. */
  4716. public function testSelectHiddenFieldOmission() {
  4717. $result = $this->Form->select('Model.multi_field',
  4718. array('first', 'second'),
  4719. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4720. );
  4721. $expected = array(
  4722. array('div' => array('class' => 'checkbox')),
  4723. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4724. array('label' => array('for' => 'ModelMultiField0')),
  4725. 'first',
  4726. '/label',
  4727. '/div',
  4728. array('div' => array('class' => 'checkbox')),
  4729. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4730. array('label' => array('for' => 'ModelMultiField1')),
  4731. 'second',
  4732. '/label',
  4733. '/div'
  4734. );
  4735. $this->assertTags($result, $expected);
  4736. $result = $this->Form->input('Model.multi_field', array(
  4737. 'options' => array('first', 'second'),
  4738. 'multiple' => 'checkbox',
  4739. 'hiddenField' => false
  4740. ));
  4741. $expected = array(
  4742. array('div' => array('class' => 'input select')),
  4743. array('label' => array('for' => 'ModelMultiField')),
  4744. 'Multi Field',
  4745. '/label',
  4746. array('div' => array('class' => 'checkbox')),
  4747. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4748. array('label' => array('for' => 'ModelMultiField0')),
  4749. 'first',
  4750. '/label',
  4751. '/div',
  4752. array('div' => array('class' => 'checkbox')),
  4753. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4754. array('label' => array('for' => 'ModelMultiField1')),
  4755. 'second',
  4756. '/label',
  4757. '/div',
  4758. '/div'
  4759. );
  4760. $this->assertTags($result, $expected);
  4761. }
  4762. /**
  4763. * test that select() with multiple = checkbox works with overriding name attribute.
  4764. *
  4765. * @return void
  4766. */
  4767. public function testSelectCheckboxMultipleOverrideName() {
  4768. $result = $this->Form->input('category', array(
  4769. 'type' => 'select',
  4770. 'multiple' => 'checkbox',
  4771. 'name' => 'data[fish]',
  4772. 'options' => array('1', '2'),
  4773. 'div' => false,
  4774. 'label' => false,
  4775. ));
  4776. $expected = array(
  4777. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4778. array('div' => array('class' => 'checkbox')),
  4779. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4780. array('label' => array('for' => 'Category0')), '1', '/label',
  4781. '/div',
  4782. array('div' => array('class' => 'checkbox')),
  4783. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4784. array('label' => array('for' => 'Category1')), '2', '/label',
  4785. '/div'
  4786. );
  4787. $this->assertTags($result, $expected);
  4788. }
  4789. /**
  4790. * Test that 'id' overrides all the checkbox id's as well.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testSelectCheckboxMultipleId() {
  4795. $result = $this->Form->select(
  4796. 'Model.multi_field',
  4797. array('first', 'second', 'third'),
  4798. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4799. );
  4800. $expected = array(
  4801. 'input' => array(
  4802. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4803. ),
  4804. array('div' => array('class' => 'checkbox')),
  4805. array('input' => array(
  4806. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4807. 'value' => '0', 'id' => 'CustomId0'
  4808. )),
  4809. array('label' => array('for' => 'CustomId0')),
  4810. 'first',
  4811. '/label',
  4812. '/div',
  4813. array('div' => array('class' => 'checkbox')),
  4814. array('input' => array(
  4815. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4816. 'value' => '1', 'id' => 'CustomId1'
  4817. )),
  4818. array('label' => array('for' => 'CustomId1')),
  4819. 'second',
  4820. '/label',
  4821. '/div',
  4822. array('div' => array('class' => 'checkbox')),
  4823. array('input' => array(
  4824. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4825. 'value' => '2', 'id' => 'CustomId2'
  4826. )),
  4827. array('label' => array('for' => 'CustomId2')),
  4828. 'third',
  4829. '/label',
  4830. '/div'
  4831. );
  4832. $this->assertTags($result, $expected);
  4833. }
  4834. /**
  4835. * testCheckbox method
  4836. *
  4837. * Test generation of checkboxes
  4838. *
  4839. * @return void
  4840. */
  4841. public function testCheckbox() {
  4842. $result = $this->Form->checkbox('Model.field');
  4843. $expected = array(
  4844. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4845. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4846. );
  4847. $this->assertTags($result, $expected);
  4848. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4849. $expected = array(
  4850. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4852. );
  4853. $this->assertTags($result, $expected);
  4854. $Contact = ClassRegistry::getObject('Contact');
  4855. $Contact->validationErrors['field'] = 1;
  4856. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4857. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4858. $expected = array(
  4859. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4860. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4864. $expected = array(
  4865. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4866. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4867. );
  4868. $this->assertTags($result, $expected);
  4869. $this->Form->request->data['Contact']['field'] = '';
  4870. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4871. $expected = array(
  4872. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4873. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4874. );
  4875. $this->assertTags($result, $expected);
  4876. $Contact->validationErrors = array();
  4877. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4878. $expected = array(
  4879. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4880. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4881. );
  4882. $this->assertTags($result, $expected);
  4883. $this->Form->request->data['Contact']['published'] = 1;
  4884. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4885. $expected = array(
  4886. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4887. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4888. );
  4889. $this->assertTags($result, $expected);
  4890. $this->Form->request->data['Contact']['published'] = 0;
  4891. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4892. $expected = array(
  4893. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4894. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4895. );
  4896. $this->assertTags($result, $expected);
  4897. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4898. $expected = array(
  4899. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4900. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4901. );
  4902. $this->assertTags($result, $expected);
  4903. $result = $this->Form->checkbox('CustomField.1.value');
  4904. $expected = array(
  4905. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4906. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4907. );
  4908. $this->assertTags($result, $expected);
  4909. }
  4910. /**
  4911. * test checkbox() with a custom name attribute
  4912. *
  4913. * @return void
  4914. */
  4915. public function testCheckboxCustomNameAttribute() {
  4916. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4917. $expected = array(
  4918. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4919. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4920. );
  4921. $this->assertTags($result, $expected);
  4922. }
  4923. /**
  4924. * test the checked option for checkboxes.
  4925. *
  4926. * @return void
  4927. */
  4928. public function testCheckboxCheckedOption() {
  4929. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4930. $expected = array(
  4931. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4932. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4933. );
  4934. $this->assertTags($result, $expected);
  4935. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4936. $expected = array(
  4937. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4938. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4939. );
  4940. $this->assertTags($result, $expected);
  4941. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4942. $expected = array(
  4943. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4944. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4945. );
  4946. $this->assertTags($result, $expected);
  4947. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4948. $expected = array(
  4949. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4950. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4951. );
  4952. $this->assertTags($result, $expected);
  4953. $this->Form->request->data['Model']['field'] = 1;
  4954. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4955. $expected = array(
  4956. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4957. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4958. );
  4959. $this->assertTags($result, $expected);
  4960. }
  4961. /**
  4962. * Test that disabled attribute works on both the checkbox and hidden input.
  4963. *
  4964. * @return void
  4965. */
  4966. public function testCheckboxDisabling() {
  4967. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4968. $expected = array(
  4969. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4970. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4971. );
  4972. $this->assertTags($result, $expected);
  4973. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4974. $expected = array(
  4975. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4976. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4977. );
  4978. $this->assertTags($result, $expected);
  4979. }
  4980. /**
  4981. * Test that the hidden input for checkboxes can be omitted or set to a
  4982. * specific value.
  4983. *
  4984. * @return void
  4985. */
  4986. public function testCheckboxHiddenField() {
  4987. $result = $this->Form->input('UserForm.something', array(
  4988. 'type' => 'checkbox',
  4989. 'hiddenField' => false
  4990. ));
  4991. $expected = array(
  4992. 'div' => array('class' => 'input checkbox'),
  4993. array('input' => array(
  4994. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4995. 'value' => '1', 'id' => 'UserFormSomething'
  4996. )),
  4997. 'label' => array('for' => 'UserFormSomething'),
  4998. 'Something',
  4999. '/label',
  5000. '/div'
  5001. );
  5002. $this->assertTags($result, $expected);
  5003. $result = $this->Form->input('UserForm.something', array(
  5004. 'type' => 'checkbox',
  5005. 'value' => 'Y',
  5006. 'hiddenField' => 'N',
  5007. ));
  5008. $expected = array(
  5009. 'div' => array('class' => 'input checkbox'),
  5010. array('input' => array(
  5011. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5012. 'value' => 'N', 'id' => 'UserFormSomething_'
  5013. )),
  5014. array('input' => array(
  5015. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5016. 'value' => 'Y', 'id' => 'UserFormSomething'
  5017. )),
  5018. 'label' => array('for' => 'UserFormSomething'),
  5019. 'Something',
  5020. '/label',
  5021. '/div'
  5022. );
  5023. $this->assertTags($result, $expected);
  5024. }
  5025. /**
  5026. * testDateTime method
  5027. *
  5028. * Test generation of date/time select elements
  5029. *
  5030. * @return void
  5031. */
  5032. public function testDateTime() {
  5033. extract($this->dateRegex);
  5034. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5035. $now = strtotime('now');
  5036. $expected = array(
  5037. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5038. $daysRegex,
  5039. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5040. date('j', $now),
  5041. '/option',
  5042. '*/select',
  5043. '-',
  5044. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5045. $monthsRegex,
  5046. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5047. date('F', $now),
  5048. '/option',
  5049. '*/select',
  5050. '-',
  5051. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5052. $yearsRegex,
  5053. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5054. date('Y', $now),
  5055. '/option',
  5056. '*/select',
  5057. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5058. $hoursRegex,
  5059. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5060. date('g', $now),
  5061. '/option',
  5062. '*/select',
  5063. ':',
  5064. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5065. $minutesRegex,
  5066. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5067. date('i', $now),
  5068. '/option',
  5069. '*/select',
  5070. ' ',
  5071. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5072. $meridianRegex,
  5073. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5074. date('a', $now),
  5075. '/option',
  5076. '*/select'
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5080. $expected = array(
  5081. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5082. $daysRegex,
  5083. array('option' => array('value' => '')),
  5084. '/option',
  5085. '*/select',
  5086. '-',
  5087. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5088. $monthsRegex,
  5089. array('option' => array('value' => '')),
  5090. '/option',
  5091. '*/select',
  5092. '-',
  5093. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5094. $yearsRegex,
  5095. array('option' => array('value' => '')),
  5096. '/option',
  5097. '*/select',
  5098. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5099. $hoursRegex,
  5100. array('option' => array('value' => '')),
  5101. '/option',
  5102. '*/select',
  5103. ':',
  5104. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5105. $minutesRegex,
  5106. array('option' => array('value' => '')),
  5107. '/option',
  5108. '*/select',
  5109. ' ',
  5110. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5111. $meridianRegex,
  5112. array('option' => array('value' => '')),
  5113. '/option',
  5114. '*/select'
  5115. );
  5116. $this->assertTags($result, $expected);
  5117. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5118. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5119. $this->assertTags($result, $expected);
  5120. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5121. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5122. $this->assertTags($result, $expected);
  5123. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5124. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5125. $expected = array(
  5126. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5127. $daysRegex,
  5128. array('option' => array('value' => '')),
  5129. '/option',
  5130. '*/select',
  5131. '-',
  5132. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5133. $monthsRegex,
  5134. array('option' => array('value' => '')),
  5135. '/option',
  5136. '*/select',
  5137. '-',
  5138. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5139. $yearsRegex,
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. '*/select',
  5143. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5144. $hoursRegex,
  5145. array('option' => array('value' => '')),
  5146. '/option',
  5147. '*/select',
  5148. ':',
  5149. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5150. $minutesRegex,
  5151. array('option' => array('value' => '')),
  5152. '/option',
  5153. array('option' => array('value' => '00')),
  5154. '00',
  5155. '/option',
  5156. array('option' => array('value' => '05')),
  5157. '05',
  5158. '/option',
  5159. array('option' => array('value' => '10')),
  5160. '10',
  5161. '/option',
  5162. '*/select',
  5163. ' ',
  5164. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5165. $meridianRegex,
  5166. array('option' => array('value' => '')),
  5167. '/option',
  5168. '*/select'
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5172. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5173. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5174. $this->Form->request->data['Contact']['data'] = null;
  5175. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5176. $expected = array(
  5177. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5178. $daysRegex,
  5179. array('option' => array('value' => '')),
  5180. '/option',
  5181. '*/select',
  5182. '-',
  5183. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5184. $monthsRegex,
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. '*/select',
  5188. '-',
  5189. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5190. $yearsRegex,
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. '*/select',
  5194. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5195. $hoursRegex,
  5196. array('option' => array('value' => '')),
  5197. '/option',
  5198. '*/select',
  5199. ':',
  5200. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5201. $minutesRegex,
  5202. array('option' => array('value' => '')),
  5203. '/option',
  5204. '*/select',
  5205. ' ',
  5206. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5207. $meridianRegex,
  5208. array('option' => array('value' => '')),
  5209. '/option',
  5210. '*/select'
  5211. );
  5212. $this->assertTags($result, $expected);
  5213. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5214. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5215. $now = strtotime($this->Form->data['Model']['field']);
  5216. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5217. $expected = array(
  5218. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5219. $daysRegex,
  5220. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5221. date('j', $now),
  5222. '/option',
  5223. '*/select',
  5224. '-',
  5225. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5226. $monthsRegex,
  5227. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5228. date('F', $now),
  5229. '/option',
  5230. '*/select',
  5231. '-',
  5232. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5233. $yearsRegex,
  5234. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5235. date('Y', $now),
  5236. '/option',
  5237. '*/select',
  5238. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5239. $hoursRegex,
  5240. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5241. date('g', $now),
  5242. '/option',
  5243. '*/select',
  5244. ':',
  5245. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5246. $minutesRegex,
  5247. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5248. date('i', $now),
  5249. '/option',
  5250. '*/select',
  5251. ' ',
  5252. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5253. $meridianRegex,
  5254. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5255. date('a', $now),
  5256. '/option',
  5257. '*/select'
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. $selected = strtotime('2008-10-26 12:33:00');
  5261. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5262. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5263. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5264. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5265. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5266. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5267. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5268. $this->Form->create('Contact');
  5269. $result = $this->Form->input('published');
  5270. $now = strtotime('now');
  5271. $expected = array(
  5272. 'div' => array('class' => 'input date'),
  5273. 'label' => array('for' => 'ContactPublishedMonth'),
  5274. 'Published',
  5275. '/label',
  5276. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5277. $monthsRegex,
  5278. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5279. date('F', $now),
  5280. '/option',
  5281. '*/select',
  5282. '-',
  5283. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5284. $daysRegex,
  5285. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5286. date('j', $now),
  5287. '/option',
  5288. '*/select',
  5289. '-',
  5290. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5291. $yearsRegex,
  5292. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5293. date('Y', $now),
  5294. '/option',
  5295. '*/select',
  5296. '/div'
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. $result = $this->Form->input('published2', array('type' => 'date'));
  5300. $now = strtotime('now');
  5301. $expected = array(
  5302. 'div' => array('class' => 'input date'),
  5303. 'label' => array('for' => 'ContactPublished2Month'),
  5304. 'Published2',
  5305. '/label',
  5306. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5307. $monthsRegex,
  5308. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5309. date('F', $now),
  5310. '/option',
  5311. '*/select',
  5312. '-',
  5313. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5314. $daysRegex,
  5315. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5316. date('j', $now),
  5317. '/option',
  5318. '*/select',
  5319. '-',
  5320. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5321. $yearsRegex,
  5322. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5323. date('Y', $now),
  5324. '/option',
  5325. '*/select',
  5326. '/div'
  5327. );
  5328. $this->assertTags($result, $expected);
  5329. $this->Form->create('Contact');
  5330. $result = $this->Form->input('published', array('monthNames' => false));
  5331. $now = strtotime('now');
  5332. $expected = array(
  5333. 'div' => array('class' => 'input date'),
  5334. 'label' => array('for' => 'ContactPublishedMonth'),
  5335. 'Published',
  5336. '/label',
  5337. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5338. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5339. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5340. date('m', $now),
  5341. '/option',
  5342. '*/select',
  5343. '-',
  5344. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5345. $daysRegex,
  5346. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5347. date('j', $now),
  5348. '/option',
  5349. '*/select',
  5350. '-',
  5351. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5352. $yearsRegex,
  5353. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5354. date('Y', $now),
  5355. '/option',
  5356. '*/select',
  5357. '/div'
  5358. );
  5359. $this->assertTags($result, $expected);
  5360. $result = $this->Form->input('published', array(
  5361. 'timeFormat' => 24,
  5362. 'interval' => 5,
  5363. 'selected' => strtotime('2009-09-03 13:37:00'),
  5364. 'type' => 'datetime'
  5365. ));
  5366. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5367. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5368. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5369. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5370. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5371. }
  5372. /**
  5373. * Test that empty values don't trigger errors.
  5374. *
  5375. * @return void
  5376. */
  5377. public function testDateTimeNoErrorsOnEmptyData() {
  5378. $this->Form->request->data['Contact'] = array(
  5379. 'date' => array(
  5380. 'day' => '',
  5381. 'month' => '',
  5382. 'year' => '',
  5383. 'hour' => '',
  5384. 'min' => '',
  5385. 'meridian' => ''
  5386. )
  5387. );
  5388. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5389. $this->assertNotEmpty($result);
  5390. }
  5391. /**
  5392. * test that datetime() and default values work.
  5393. *
  5394. * @return void
  5395. */
  5396. public function testDatetimeWithDefault() {
  5397. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5398. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5399. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5400. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5401. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5402. 'default' => '2009-06-01 11:15:30'
  5403. ));
  5404. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5405. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5406. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5407. }
  5408. /**
  5409. * test that bogus non-date time data doesn't cause errors.
  5410. *
  5411. * @return void
  5412. */
  5413. public function testDateTimeWithBogusData() {
  5414. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5415. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5416. }
  5417. /**
  5418. * testDateTimeEmptyAsArray
  5419. *
  5420. * @return void
  5421. */
  5422. public function testDateTimeEmptyAsArray() {
  5423. $result = $this->Form->dateTime('Contact.date',
  5424. 'DMY',
  5425. '12',
  5426. array(
  5427. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5428. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5429. )
  5430. )
  5431. );
  5432. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5433. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5434. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5435. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5436. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5437. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5438. $result = $this->Form->dateTime('Contact.date',
  5439. 'DMY',
  5440. '12',
  5441. array(
  5442. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5443. )
  5444. );
  5445. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5446. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5447. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5448. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5449. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5450. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5451. }
  5452. /**
  5453. * testFormDateTimeMulti method
  5454. *
  5455. * test multiple datetime element generation
  5456. *
  5457. * @return void
  5458. */
  5459. public function testFormDateTimeMulti() {
  5460. extract($this->dateRegex);
  5461. $result = $this->Form->dateTime('Contact.1.updated');
  5462. $expected = array(
  5463. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5464. $daysRegex,
  5465. array('option' => array('value' => '')),
  5466. '/option',
  5467. '*/select',
  5468. '-',
  5469. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5470. $monthsRegex,
  5471. array('option' => array('value' => '')),
  5472. '/option',
  5473. '*/select',
  5474. '-',
  5475. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5476. $yearsRegex,
  5477. array('option' => array('value' => '')),
  5478. '/option',
  5479. '*/select',
  5480. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5481. $hoursRegex,
  5482. array('option' => array('value' => '')),
  5483. '/option',
  5484. '*/select',
  5485. ':',
  5486. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5487. $minutesRegex,
  5488. array('option' => array('value' => '')),
  5489. '/option',
  5490. '*/select',
  5491. ' ',
  5492. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5493. $meridianRegex,
  5494. array('option' => array('value' => '')),
  5495. '/option',
  5496. '*/select'
  5497. );
  5498. $this->assertTags($result, $expected);
  5499. $result = $this->Form->dateTime('Contact.2.updated');
  5500. $expected = array(
  5501. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5502. $daysRegex,
  5503. array('option' => array('value' => '')),
  5504. '/option',
  5505. '*/select',
  5506. '-',
  5507. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5508. $monthsRegex,
  5509. array('option' => array('value' => '')),
  5510. '/option',
  5511. '*/select',
  5512. '-',
  5513. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5514. $yearsRegex,
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. '*/select',
  5518. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5519. $hoursRegex,
  5520. array('option' => array('value' => '')),
  5521. '/option',
  5522. '*/select',
  5523. ':',
  5524. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5525. $minutesRegex,
  5526. array('option' => array('value' => '')),
  5527. '/option',
  5528. '*/select',
  5529. ' ',
  5530. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5531. $meridianRegex,
  5532. array('option' => array('value' => '')),
  5533. '/option',
  5534. '*/select'
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. }
  5538. /**
  5539. * When changing the date format, the label should always focus the first select box when
  5540. * clicked.
  5541. *
  5542. * @return void
  5543. */
  5544. public function testDateTimeLabelIdMatchesFirstInput() {
  5545. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5546. $this->assertContains('label for="ModelDateMonth"', $result);
  5547. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5548. $this->assertContains('label for="ModelDateDay"', $result);
  5549. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5550. $this->assertContains('label for="ModelDateYear"', $result);
  5551. }
  5552. /**
  5553. * testMonth method
  5554. *
  5555. * @return void
  5556. */
  5557. public function testMonth() {
  5558. $result = $this->Form->month('Model.field');
  5559. $expected = array(
  5560. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5561. array('option' => array('value' => '')),
  5562. '/option',
  5563. array('option' => array('value' => '01')),
  5564. date('F', strtotime('2008-01-01 00:00:00')),
  5565. '/option',
  5566. array('option' => array('value' => '02')),
  5567. date('F', strtotime('2008-02-01 00:00:00')),
  5568. '/option',
  5569. '*/select',
  5570. );
  5571. $this->assertTags($result, $expected);
  5572. $result = $this->Form->month('Model.field', array('empty' => true));
  5573. $expected = array(
  5574. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5575. array('option' => array('value' => '')),
  5576. '/option',
  5577. array('option' => array('value' => '01')),
  5578. date('F', strtotime('2008-01-01 00:00:00')),
  5579. '/option',
  5580. array('option' => array('value' => '02')),
  5581. date('F', strtotime('2008-02-01 00:00:00')),
  5582. '/option',
  5583. '*/select',
  5584. );
  5585. $this->assertTags($result, $expected);
  5586. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5587. $expected = array(
  5588. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5589. array('option' => array('value' => '')),
  5590. '/option',
  5591. array('option' => array('value' => '01')),
  5592. '01',
  5593. '/option',
  5594. array('option' => array('value' => '02')),
  5595. '02',
  5596. '/option',
  5597. '*/select',
  5598. );
  5599. $this->assertTags($result, $expected);
  5600. $monthNames = array(
  5601. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5602. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5603. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5604. $expected = array(
  5605. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5606. array('option' => array('value' => '')),
  5607. '/option',
  5608. array('option' => array('value' => '01')),
  5609. 'Jan',
  5610. '/option',
  5611. array('option' => array('value' => '02')),
  5612. 'Feb',
  5613. '/option',
  5614. '*/select',
  5615. );
  5616. $this->assertTags($result, $expected);
  5617. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5618. $result = $this->Form->month('Project.release');
  5619. $expected = array(
  5620. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5621. array('option' => array('value' => '')),
  5622. '/option',
  5623. array('option' => array('value' => '01')),
  5624. 'January',
  5625. '/option',
  5626. array('option' => array('value' => '02', 'selected' => 'selected')),
  5627. 'February',
  5628. '/option',
  5629. '*/select',
  5630. );
  5631. $this->assertTags($result, $expected);
  5632. }
  5633. /**
  5634. * testDay method
  5635. *
  5636. * @return void
  5637. */
  5638. public function testDay() {
  5639. extract($this->dateRegex);
  5640. $result = $this->Form->day('Model.field', array('value' => false));
  5641. $expected = array(
  5642. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5643. array('option' => array('value' => '')),
  5644. '/option',
  5645. array('option' => array('value' => '01')),
  5646. '1',
  5647. '/option',
  5648. array('option' => array('value' => '02')),
  5649. '2',
  5650. '/option',
  5651. $daysRegex,
  5652. '/select',
  5653. );
  5654. $this->assertTags($result, $expected);
  5655. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5656. $result = $this->Form->day('Model.field');
  5657. $expected = array(
  5658. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5659. array('option' => array('value' => '')),
  5660. '/option',
  5661. array('option' => array('value' => '01')),
  5662. '1',
  5663. '/option',
  5664. array('option' => array('value' => '02')),
  5665. '2',
  5666. '/option',
  5667. $daysRegex,
  5668. array('option' => array('value' => '10', 'selected' => 'selected')),
  5669. '10',
  5670. '/option',
  5671. $daysRegex,
  5672. '/select',
  5673. );
  5674. $this->assertTags($result, $expected);
  5675. $this->Form->request->data['Model']['field'] = '';
  5676. $result = $this->Form->day('Model.field', array('value' => '10'));
  5677. $expected = array(
  5678. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5679. array('option' => array('value' => '')),
  5680. '/option',
  5681. array('option' => array('value' => '01')),
  5682. '1',
  5683. '/option',
  5684. array('option' => array('value' => '02')),
  5685. '2',
  5686. '/option',
  5687. $daysRegex,
  5688. array('option' => array('value' => '10', 'selected' => 'selected')),
  5689. '10',
  5690. '/option',
  5691. $daysRegex,
  5692. '/select',
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5696. $result = $this->Form->day('Model.field', array('value' => true));
  5697. $expected = array(
  5698. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5699. array('option' => array('value' => '')),
  5700. '/option',
  5701. array('option' => array('value' => '01')),
  5702. '1',
  5703. '/option',
  5704. array('option' => array('value' => '02')),
  5705. '2',
  5706. '/option',
  5707. $daysRegex,
  5708. array('option' => array('value' => '10', 'selected' => 'selected')),
  5709. '10',
  5710. '/option',
  5711. $daysRegex,
  5712. '/select',
  5713. );
  5714. $this->assertTags($result, $expected);
  5715. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5716. $result = $this->Form->day('Project.release');
  5717. $expected = array(
  5718. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5719. array('option' => array('value' => '')),
  5720. '/option',
  5721. array('option' => array('value' => '01')),
  5722. '1',
  5723. '/option',
  5724. array('option' => array('value' => '02')),
  5725. '2',
  5726. '/option',
  5727. $daysRegex,
  5728. array('option' => array('value' => '10', 'selected' => 'selected')),
  5729. '10',
  5730. '/option',
  5731. $daysRegex,
  5732. '/select',
  5733. );
  5734. $this->assertTags($result, $expected);
  5735. }
  5736. /**
  5737. * testMinute method
  5738. *
  5739. * @return void
  5740. */
  5741. public function testMinute() {
  5742. extract($this->dateRegex);
  5743. $result = $this->Form->minute('Model.field');
  5744. $expected = array(
  5745. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5746. array('option' => array('value' => '')),
  5747. '/option',
  5748. array('option' => array('value' => '00')),
  5749. '00',
  5750. '/option',
  5751. array('option' => array('value' => '01')),
  5752. '01',
  5753. '/option',
  5754. array('option' => array('value' => '02')),
  5755. '02',
  5756. '/option',
  5757. $minutesRegex,
  5758. '/select',
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5762. $result = $this->Form->minute('Model.field');
  5763. $expected = array(
  5764. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5765. array('option' => array('value' => '')),
  5766. '/option',
  5767. array('option' => array('value' => '00')),
  5768. '00',
  5769. '/option',
  5770. array('option' => array('value' => '01')),
  5771. '01',
  5772. '/option',
  5773. array('option' => array('value' => '02')),
  5774. '02',
  5775. '/option',
  5776. $minutesRegex,
  5777. array('option' => array('value' => '12', 'selected' => 'selected')),
  5778. '12',
  5779. '/option',
  5780. $minutesRegex,
  5781. '/select',
  5782. );
  5783. $this->assertTags($result, $expected);
  5784. $this->Form->request->data['Model']['field'] = '';
  5785. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5786. $expected = array(
  5787. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5788. array('option' => array('value' => '')),
  5789. '/option',
  5790. array('option' => array('value' => '00')),
  5791. '00',
  5792. '/option',
  5793. array('option' => array('value' => '05')),
  5794. '05',
  5795. '/option',
  5796. array('option' => array('value' => '10')),
  5797. '10',
  5798. '/option',
  5799. $minutesRegex,
  5800. '/select',
  5801. );
  5802. $this->assertTags($result, $expected);
  5803. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5804. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5805. $expected = array(
  5806. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5807. array('option' => array('value' => '')),
  5808. '/option',
  5809. array('option' => array('value' => '00')),
  5810. '00',
  5811. '/option',
  5812. array('option' => array('value' => '05')),
  5813. '05',
  5814. '/option',
  5815. array('option' => array('value' => '10', 'selected' => 'selected')),
  5816. '10',
  5817. '/option',
  5818. $minutesRegex,
  5819. '/select',
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. }
  5823. /**
  5824. * testHour method
  5825. *
  5826. * @return void
  5827. */
  5828. public function testHour() {
  5829. extract($this->dateRegex);
  5830. $result = $this->Form->hour('Model.field', false);
  5831. $expected = array(
  5832. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5833. array('option' => array('value' => '')),
  5834. '/option',
  5835. array('option' => array('value' => '01')),
  5836. '1',
  5837. '/option',
  5838. array('option' => array('value' => '02')),
  5839. '2',
  5840. '/option',
  5841. $hoursRegex,
  5842. '/select',
  5843. );
  5844. $this->assertTags($result, $expected);
  5845. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5846. $result = $this->Form->hour('Model.field', false);
  5847. $expected = array(
  5848. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5849. array('option' => array('value' => '')),
  5850. '/option',
  5851. array('option' => array('value' => '01')),
  5852. '1',
  5853. '/option',
  5854. array('option' => array('value' => '02')),
  5855. '2',
  5856. '/option',
  5857. $hoursRegex,
  5858. array('option' => array('value' => '12', 'selected' => 'selected')),
  5859. '12',
  5860. '/option',
  5861. '/select',
  5862. );
  5863. $this->assertTags($result, $expected);
  5864. $this->Form->request->data['Model']['field'] = '';
  5865. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5866. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5867. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5868. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5869. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5870. $result = $this->Form->hour('Model.field', true);
  5871. $expected = array(
  5872. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5873. array('option' => array('value' => '')),
  5874. '/option',
  5875. array('option' => array('value' => '00', 'selected' => 'selected')),
  5876. '0',
  5877. '/option',
  5878. array('option' => array('value' => '01')),
  5879. '1',
  5880. '/option',
  5881. array('option' => array('value' => '02')),
  5882. '2',
  5883. '/option',
  5884. $hoursRegex,
  5885. '/select',
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. unset($this->Form->request->data['Model']['field']);
  5889. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5890. $thisHour = date('H');
  5891. $optValue = date('G');
  5892. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5893. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5894. $result = $this->Form->hour('Model.field', true);
  5895. $expected = array(
  5896. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5897. array('option' => array('value' => '')),
  5898. '/option',
  5899. array('option' => array('value' => '00')),
  5900. '0',
  5901. '/option',
  5902. array('option' => array('value' => '01', 'selected' => 'selected')),
  5903. '1',
  5904. '/option',
  5905. array('option' => array('value' => '02')),
  5906. '2',
  5907. '/option',
  5908. $hoursRegex,
  5909. '/select',
  5910. );
  5911. $this->assertTags($result, $expected);
  5912. }
  5913. /**
  5914. * testYear method
  5915. *
  5916. * @return void
  5917. */
  5918. public function testYear() {
  5919. $result = $this->Form->year('Model.field', 2006, 2007);
  5920. $expected = array(
  5921. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5922. array('option' => array('value' => '')),
  5923. '/option',
  5924. array('option' => array('value' => '2007')),
  5925. '2007',
  5926. '/option',
  5927. array('option' => array('value' => '2006')),
  5928. '2006',
  5929. '/option',
  5930. '/select',
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5934. $expected = array(
  5935. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5936. array('option' => array('value' => '')),
  5937. '/option',
  5938. array('option' => array('value' => '2006')),
  5939. '2006',
  5940. '/option',
  5941. array('option' => array('value' => '2007')),
  5942. '2007',
  5943. '/option',
  5944. '/select',
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $this->request->data['Contact']['published'] = '';
  5948. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5949. $expected = array(
  5950. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5951. array('option' => array('value' => '')),
  5952. '/option',
  5953. array('option' => array('value' => '2007')),
  5954. '2007',
  5955. '/option',
  5956. array('option' => array('value' => '2006')),
  5957. '2006',
  5958. '/option',
  5959. '/select',
  5960. );
  5961. $this->assertTags($result, $expected);
  5962. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5963. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5964. $expected = array(
  5965. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5966. array('option' => array('value' => '2007')),
  5967. '2007',
  5968. '/option',
  5969. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5970. '2006',
  5971. '/option',
  5972. '/select',
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. $this->Form->request->data['Contact']['published'] = '';
  5976. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5977. $expected = array(
  5978. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5979. array('option' => array('value' => '')),
  5980. '/option',
  5981. array('option' => array('value' => '2007')),
  5982. '2007',
  5983. '/option',
  5984. array('option' => array('value' => '2006')),
  5985. '2006',
  5986. '/option',
  5987. '/select',
  5988. );
  5989. $this->assertTags($result, $expected);
  5990. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5991. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5992. $expected = array(
  5993. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5994. array('option' => array('value' => '2007')),
  5995. '2007',
  5996. '/option',
  5997. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5998. '2006',
  5999. '/option',
  6000. '/select',
  6001. );
  6002. $this->assertTags($result, $expected);
  6003. $this->Form->request->data['Contact']['published'] = '';
  6004. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6005. $expected = array(
  6006. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6007. array('option' => array('value' => '')),
  6008. '/option',
  6009. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6010. '2007',
  6011. '/option',
  6012. array('option' => array('value' => '2006')),
  6013. '2006',
  6014. '/option',
  6015. '/select',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6019. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6020. $expected = array(
  6021. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6022. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6023. '2007',
  6024. '/option',
  6025. array('option' => array('value' => '2006')),
  6026. '2006',
  6027. '/option',
  6028. '/select',
  6029. );
  6030. $this->assertTags($result, $expected);
  6031. $this->Form->request->data['Contact']['published'] = '';
  6032. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6033. $expected = array(
  6034. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6035. array('option' => array('value' => '2008')),
  6036. '2008',
  6037. '/option',
  6038. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6039. '2007',
  6040. '/option',
  6041. array('option' => array('value' => '2006')),
  6042. '2006',
  6043. '/option',
  6044. '/select',
  6045. );
  6046. $this->assertTags($result, $expected);
  6047. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6048. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6049. $expected = array(
  6050. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6051. array('option' => array('value' => '2008')),
  6052. '2008',
  6053. '/option',
  6054. array('option' => array('value' => '2007')),
  6055. '2007',
  6056. '/option',
  6057. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6058. '2006',
  6059. '/option',
  6060. '/select',
  6061. );
  6062. $this->assertTags($result, $expected);
  6063. $this->Form->request->data = array();
  6064. $this->Form->create('Contact');
  6065. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6066. $expected = array(
  6067. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6068. array('option' => array('value' => '2008')),
  6069. '2008',
  6070. '/option',
  6071. array('option' => array('value' => '2007')),
  6072. '2007',
  6073. '/option',
  6074. array('option' => array('value' => '2006')),
  6075. '2006',
  6076. '/option',
  6077. '/select',
  6078. );
  6079. $this->assertTags($result, $expected);
  6080. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6081. $this->assertContains('data[Contact][published][year]', $result);
  6082. }
  6083. /**
  6084. * testYearAutoExpandRange method
  6085. *
  6086. * @return void
  6087. */
  6088. public function testYearAutoExpandRange() {
  6089. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6090. $result = $this->Form->year('User.birthday');
  6091. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6092. $result = $matches[1];
  6093. $expected = range(date('Y') + 20, 1930);
  6094. $this->assertEquals($result, $expected);
  6095. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6096. $result = $this->Form->year('Project.release');
  6097. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6098. $result = $matches[1];
  6099. $expected = range(2050, date('Y') - 20);
  6100. $this->assertEquals($result, $expected);
  6101. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6102. $result = $this->Form->year('Project.release', 1890, 1900);
  6103. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6104. $result = $matches[1];
  6105. $expected = range(1900, 1881);
  6106. $this->assertEquals($result, $expected);
  6107. }
  6108. /**
  6109. * testInputDate method
  6110. *
  6111. * Test various inputs with type date and different dateFormat values.
  6112. * Failing to provide a dateFormat key should not error.
  6113. * It should simply not pre-select any value then.
  6114. *
  6115. * @return void
  6116. */
  6117. public function testInputDate() {
  6118. $this->Form->request->data = array(
  6119. 'User' => array(
  6120. 'month_year' => array('month' => date('m')),
  6121. 'just_year' => array('month' => date('m')),
  6122. 'just_month' => array('year' => date('Y')),
  6123. 'just_day' => array('month' => date('m')),
  6124. )
  6125. );
  6126. $this->Form->create('User');
  6127. $result = $this->Form->input('month_year',
  6128. array(
  6129. 'label' => false,
  6130. 'div' => false,
  6131. 'type' => 'date',
  6132. 'dateFormat' => 'MY',
  6133. 'minYear' => 2006,
  6134. 'maxYear' => 2008
  6135. )
  6136. );
  6137. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6138. $this->assertNotContains('value="2008" selected="selected"', $result);
  6139. $result = $this->Form->input('just_year',
  6140. array(
  6141. 'type' => 'date',
  6142. 'label' => false,
  6143. 'dateFormat' => 'Y',
  6144. 'minYear' => date('Y'),
  6145. 'maxYear' => date('Y', strtotime('+20 years'))
  6146. )
  6147. );
  6148. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6149. $result = $this->Form->input('just_month',
  6150. array(
  6151. 'type' => 'date',
  6152. 'label' => false,
  6153. 'dateFormat' => 'M',
  6154. 'empty' => false,
  6155. )
  6156. );
  6157. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6158. $result = $this->Form->input('just_day',
  6159. array(
  6160. 'type' => 'date',
  6161. 'label' => false,
  6162. 'dateFormat' => 'D',
  6163. 'empty' => false,
  6164. )
  6165. );
  6166. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6167. }
  6168. /**
  6169. * testInputDateMaxYear method
  6170. *
  6171. * Let's say we want to only allow users born from 2006 to 2008 to register
  6172. * This being the first singup page, we still don't have any data
  6173. *
  6174. * @return void
  6175. */
  6176. public function testInputDateMaxYear() {
  6177. $this->Form->request->data = array();
  6178. $this->Form->create('User');
  6179. $result = $this->Form->input('birthday',
  6180. array(
  6181. 'label' => false,
  6182. 'div' => false,
  6183. 'type' => 'date',
  6184. 'dateFormat' => 'DMY',
  6185. 'minYear' => 2006,
  6186. 'maxYear' => 2008
  6187. )
  6188. );
  6189. $this->assertContains('value="2008" selected="selected"', $result);
  6190. }
  6191. /**
  6192. * testTextArea method
  6193. *
  6194. * @return void
  6195. */
  6196. public function testTextArea() {
  6197. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6198. $result = $this->Form->textarea('Model.field');
  6199. $expected = array(
  6200. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6201. 'some test data',
  6202. '/textarea',
  6203. );
  6204. $this->assertTags($result, $expected);
  6205. $result = $this->Form->textarea('Model.tmp');
  6206. $expected = array(
  6207. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6208. '/textarea',
  6209. );
  6210. $this->assertTags($result, $expected);
  6211. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6212. $result = $this->Form->textarea('Model.field');
  6213. $expected = array(
  6214. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6215. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6216. '/textarea',
  6217. );
  6218. $this->assertTags($result, $expected);
  6219. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6220. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6221. $expected = array(
  6222. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6223. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6224. '/textarea',
  6225. );
  6226. $this->assertTags($result, $expected);
  6227. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6228. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6229. $expected = array(
  6230. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6231. '/textarea'
  6232. );
  6233. $this->assertTags($result, $expected);
  6234. }
  6235. /**
  6236. * testTextAreaWithStupidCharacters method
  6237. *
  6238. * test text area with non-ascii characters
  6239. *
  6240. * @return void
  6241. */
  6242. public function testTextAreaWithStupidCharacters() {
  6243. $this->loadFixtures('Post');
  6244. $result = $this->Form->input('Post.content', array(
  6245. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6246. ));
  6247. $expected = array(
  6248. 'div' => array('class' => 'input textarea'),
  6249. 'label' => array('for' => 'PostContent'),
  6250. 'Current Text',
  6251. '/label',
  6252. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6253. 'GREAT®',
  6254. '/textarea',
  6255. '/div'
  6256. );
  6257. $this->assertTags($result, $expected);
  6258. }
  6259. /**
  6260. * testHiddenField method
  6261. *
  6262. * @return void
  6263. */
  6264. public function testHiddenField() {
  6265. $Contact = ClassRegistry::getObject('Contact');
  6266. $Contact->validationErrors['field'] = 1;
  6267. $this->Form->request->data['Contact']['field'] = 'test';
  6268. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6269. $this->assertTags($result, array(
  6270. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6271. );
  6272. }
  6273. /**
  6274. * testFileUploadField method
  6275. *
  6276. * @return void
  6277. */
  6278. public function testFileUploadField() {
  6279. $result = $this->Form->file('Model.upload');
  6280. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6281. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6282. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6283. $expected = array(
  6284. 'div' => array('class' => 'input file'),
  6285. 'label' => array('for' => 'ModelUpload'),
  6286. 'Upload',
  6287. '/label',
  6288. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6289. '/div'
  6290. );
  6291. $this->assertTags($result, $expected);
  6292. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6293. $result = $this->Form->file('Model.upload');
  6294. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6295. }
  6296. /**
  6297. * test File upload input on a model not used in create();
  6298. *
  6299. * @return void
  6300. */
  6301. public function testFileUploadOnOtherModel() {
  6302. $this->Form->create('ValidateUser', array('type' => 'file'));
  6303. $result = $this->Form->file('ValidateProfile.city');
  6304. $expected = array(
  6305. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6306. );
  6307. $this->assertTags($result, $expected);
  6308. }
  6309. /**
  6310. * testButton method
  6311. *
  6312. * @return void
  6313. */
  6314. public function testButton() {
  6315. $result = $this->Form->button('Hi');
  6316. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6317. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6318. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6319. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6320. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6321. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6322. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6323. $result = $this->Form->button('No type', array('type' => false));
  6324. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6325. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6326. $this->assertNotRegExp('/\&039/', $result);
  6327. }
  6328. /**
  6329. * Test that button() makes unlocked fields by default.
  6330. *
  6331. * @return void
  6332. */
  6333. public function testButtonUnlockedByDefault() {
  6334. $this->Form->request->params['_Token']['key'] = 'secured';
  6335. $this->Form->button('Save', array('name' => 'save'));
  6336. $this->Form->button('Clear');
  6337. $result = $this->Form->unlockField();
  6338. $this->assertEquals(array('save'), $result);
  6339. }
  6340. /**
  6341. * testPostButton method
  6342. *
  6343. * @return void
  6344. */
  6345. public function testPostButton() {
  6346. $result = $this->Form->postButton('Hi', '/controller/action');
  6347. $this->assertTags($result, array(
  6348. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6349. 'div' => array('style' => 'display:none;'),
  6350. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6351. '/div',
  6352. 'button' => array('type' => 'submit'),
  6353. 'Hi',
  6354. '/button',
  6355. '/form'
  6356. ));
  6357. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6358. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6359. }
  6360. /**
  6361. * Test that postButton adds _Token fields.
  6362. *
  6363. * @return void
  6364. */
  6365. public function testSecurePostButton() {
  6366. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6367. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6368. $expected = array(
  6369. 'form' => array(
  6370. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6371. ),
  6372. array('div' => array('style' => 'display:none;')),
  6373. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6374. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6375. '/div',
  6376. 'button' => array('type' => 'submit'),
  6377. 'Delete',
  6378. '/button',
  6379. array('div' => array('style' => 'display:none;')),
  6380. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6381. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6382. '/div',
  6383. '/form',
  6384. );
  6385. $this->assertTags($result, $expected);
  6386. }
  6387. /**
  6388. * testPostLink method
  6389. *
  6390. * @return void
  6391. */
  6392. public function testPostLink() {
  6393. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6394. $this->assertTags($result, array(
  6395. 'form' => array(
  6396. 'method' => 'post', 'action' => '/posts/delete/1',
  6397. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6398. ),
  6399. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6400. '/form',
  6401. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6402. 'Delete',
  6403. '/a'
  6404. ));
  6405. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6406. $this->assertTags($result, array(
  6407. 'form' => array(
  6408. 'method' => 'post', 'action' => '/posts/delete/1',
  6409. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6410. ),
  6411. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6412. '/form',
  6413. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6414. 'Delete',
  6415. '/a'
  6416. ));
  6417. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6418. $this->assertTags($result, array(
  6419. 'form' => array(
  6420. 'method' => 'post', 'action' => '/posts/delete/1',
  6421. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6422. ),
  6423. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6424. '/form',
  6425. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6426. 'Delete',
  6427. '/a'
  6428. ));
  6429. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6430. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6431. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6432. $this->assertTags($result, array(
  6433. 'form' => array(
  6434. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6435. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6436. ),
  6437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6438. '/form',
  6439. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6440. 'Delete',
  6441. '/a'
  6442. ));
  6443. }
  6444. /**
  6445. * Test that postLink adds _Token fields.
  6446. *
  6447. * @return void
  6448. */
  6449. public function testSecurePostLink() {
  6450. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6451. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6452. $expected = array(
  6453. 'form' => array(
  6454. 'method' => 'post', 'action' => '/posts/delete/1',
  6455. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6456. ),
  6457. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6458. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6459. 'div' => array('style' => 'display:none;'),
  6460. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6461. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6462. '/div',
  6463. '/form',
  6464. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6465. 'Delete',
  6466. '/a'
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. }
  6470. /**
  6471. * testSubmitButton method
  6472. *
  6473. * @return void
  6474. */
  6475. public function testSubmitButton() {
  6476. $result = $this->Form->submit('');
  6477. $expected = array(
  6478. 'div' => array('class' => 'submit'),
  6479. 'input' => array('type' => 'submit', 'value' => ''),
  6480. '/div'
  6481. );
  6482. $this->assertTags($result, $expected);
  6483. $result = $this->Form->submit('Test Submit');
  6484. $expected = array(
  6485. 'div' => array('class' => 'submit'),
  6486. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6487. '/div'
  6488. );
  6489. $this->assertTags($result, $expected);
  6490. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6491. $expected = array(
  6492. 'span' => array('class' => 'submit'),
  6493. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6494. '/span'
  6495. );
  6496. $this->assertTags($result, $expected);
  6497. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6498. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6499. $this->assertTags($result, $expected);
  6500. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6501. $expected = array(
  6502. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6503. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6504. '/div'
  6505. );
  6506. $this->assertTags($result, $expected);
  6507. $result = $this->Form->submit('Next >');
  6508. $expected = array(
  6509. 'div' => array('class' => 'submit'),
  6510. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6511. '/div'
  6512. );
  6513. $this->assertTags($result, $expected);
  6514. $result = $this->Form->submit('Next >', array('escape' => false));
  6515. $expected = array(
  6516. 'div' => array('class' => 'submit'),
  6517. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6518. '/div'
  6519. );
  6520. $this->assertTags($result, $expected);
  6521. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6522. $expected = array(
  6523. 'div' => array('class' => 'submit'),
  6524. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6525. '/div'
  6526. );
  6527. $this->assertTags($result, $expected);
  6528. $before = '--before--';
  6529. $after = '--after--';
  6530. $result = $this->Form->submit('Test', array('before' => $before));
  6531. $expected = array(
  6532. 'div' => array('class' => 'submit'),
  6533. '--before--',
  6534. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6535. '/div'
  6536. );
  6537. $this->assertTags($result, $expected);
  6538. $result = $this->Form->submit('Test', array('after' => $after));
  6539. $expected = array(
  6540. 'div' => array('class' => 'submit'),
  6541. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6542. '--after--',
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6547. $expected = array(
  6548. 'div' => array('class' => 'submit'),
  6549. '--before--',
  6550. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6551. '--after--',
  6552. '/div'
  6553. );
  6554. $this->assertTags($result, $expected);
  6555. }
  6556. /**
  6557. * test image submit types.
  6558. *
  6559. * @return void
  6560. */
  6561. public function testSubmitImage() {
  6562. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6563. $expected = array(
  6564. 'div' => array('class' => 'submit'),
  6565. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6566. '/div'
  6567. );
  6568. $this->assertTags($result, $expected);
  6569. $result = $this->Form->submit('/relative/cake.power.gif');
  6570. $expected = array(
  6571. 'div' => array('class' => 'submit'),
  6572. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6573. '/div'
  6574. );
  6575. $this->assertTags($result, $expected);
  6576. $result = $this->Form->submit('cake.power.gif');
  6577. $expected = array(
  6578. 'div' => array('class' => 'submit'),
  6579. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6580. '/div'
  6581. );
  6582. $this->assertTags($result, $expected);
  6583. $result = $this->Form->submit('Not.an.image');
  6584. $expected = array(
  6585. 'div' => array('class' => 'submit'),
  6586. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6587. '/div'
  6588. );
  6589. $this->assertTags($result, $expected);
  6590. $after = '--after--';
  6591. $before = '--before--';
  6592. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6593. $expected = array(
  6594. 'div' => array('class' => 'submit'),
  6595. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6596. '--after--',
  6597. '/div'
  6598. );
  6599. $this->assertTags($result, $expected);
  6600. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6601. $expected = array(
  6602. 'div' => array('class' => 'submit'),
  6603. '--before--',
  6604. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6605. '/div'
  6606. );
  6607. $this->assertTags($result, $expected);
  6608. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6609. $expected = array(
  6610. 'div' => array('class' => 'submit'),
  6611. '--before--',
  6612. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6613. '--after--',
  6614. '/div'
  6615. );
  6616. $this->assertTags($result, $expected);
  6617. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6618. $expected = array(
  6619. 'div' => array('class' => 'submit'),
  6620. '--before--',
  6621. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6622. '--after--',
  6623. '/div'
  6624. );
  6625. $this->assertTags($result, $expected);
  6626. }
  6627. /**
  6628. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6629. * be submitted at a time.
  6630. *
  6631. * @return void
  6632. */
  6633. public function testSubmitUnlockedByDefault() {
  6634. $this->Form->request->params['_Token']['key'] = 'secured';
  6635. $this->Form->submit('Go go');
  6636. $this->Form->submit('Save', array('name' => 'save'));
  6637. $result = $this->Form->unlockField();
  6638. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6639. }
  6640. /**
  6641. * Test submit image with timestamps.
  6642. *
  6643. * @return void
  6644. */
  6645. public function testSubmitImageTimestamp() {
  6646. Configure::write('Asset.timestamp', 'force');
  6647. $result = $this->Form->submit('cake.power.gif');
  6648. $expected = array(
  6649. 'div' => array('class' => 'submit'),
  6650. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6651. '/div'
  6652. );
  6653. $this->assertTags($result, $expected);
  6654. }
  6655. /**
  6656. * test the create() method
  6657. *
  6658. * @return void
  6659. */
  6660. public function testCreate() {
  6661. $result = $this->Form->create('Contact');
  6662. $encoding = strtolower(Configure::read('App.encoding'));
  6663. $expected = array(
  6664. 'form' => array(
  6665. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6666. 'accept-charset' => $encoding
  6667. ),
  6668. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6669. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6670. '/div'
  6671. );
  6672. $this->assertTags($result, $expected);
  6673. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6674. $expected = array('form' => array(
  6675. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6676. 'accept-charset' => $encoding
  6677. ));
  6678. $this->assertTags($result, $expected);
  6679. $result = $this->Form->create('Contact', array('type' => 'get'));
  6680. $expected = array('form' => array(
  6681. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6682. 'accept-charset' => $encoding
  6683. ));
  6684. $this->assertTags($result, $expected);
  6685. $result = $this->Form->create('Contact', array('type' => 'put'));
  6686. $expected = array(
  6687. 'form' => array(
  6688. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6689. 'accept-charset' => $encoding
  6690. ),
  6691. 'div' => array('style' => 'display:none;'),
  6692. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6693. '/div'
  6694. );
  6695. $this->assertTags($result, $expected);
  6696. $result = $this->Form->create('Contact', array('type' => 'file'));
  6697. $expected = array(
  6698. 'form' => array(
  6699. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6700. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6701. ),
  6702. 'div' => array('style' => 'display:none;'),
  6703. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6704. '/div'
  6705. );
  6706. $this->assertTags($result, $expected);
  6707. $this->Form->request->data['Contact']['id'] = 1;
  6708. $this->Form->request->here = '/contacts/edit/1';
  6709. $this->Form->request['action'] = 'edit';
  6710. $result = $this->Form->create('Contact');
  6711. $expected = array(
  6712. 'form' => array(
  6713. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6714. 'accept-charset' => $encoding
  6715. ),
  6716. 'div' => array('style' => 'display:none;'),
  6717. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6718. '/div'
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. $this->Form->request->data['Contact']['id'] = 1;
  6722. $this->Form->request->here = '/contacts/edit/1';
  6723. $this->Form->request['action'] = 'edit';
  6724. $result = $this->Form->create('Contact', array('type' => 'file'));
  6725. $expected = array(
  6726. 'form' => array(
  6727. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6728. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6729. ),
  6730. 'div' => array('style' => 'display:none;'),
  6731. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6732. '/div'
  6733. );
  6734. $this->assertTags($result, $expected);
  6735. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6736. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6737. $expected = array(
  6738. 'form' => array(
  6739. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6740. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6741. ),
  6742. 'div' => array('style' => 'display:none;'),
  6743. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6748. $expected = array(
  6749. 'form' => array(
  6750. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6751. 'accept-charset' => $encoding
  6752. ),
  6753. 'div' => array('style' => 'display:none;'),
  6754. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6755. '/div'
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $this->Form->request['action'] = 'add';
  6759. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6760. $expected = array(
  6761. 'form' => array(
  6762. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6763. 'accept-charset' => $encoding
  6764. ),
  6765. 'div' => array('style' => 'display:none;'),
  6766. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6767. '/div'
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $result = $this->Form->create('User', array('action' => 'login'));
  6771. $expected = array(
  6772. 'form' => array(
  6773. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6774. 'accept-charset' => $encoding
  6775. ),
  6776. 'div' => array('style' => 'display:none;'),
  6777. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->create('User', array('url' => '/users/login'));
  6782. $expected = array(
  6783. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6784. 'div' => array('style' => 'display:none;'),
  6785. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6786. '/div'
  6787. );
  6788. $this->assertTags($result, $expected);
  6789. $this->Form->request['controller'] = 'pages';
  6790. $result = $this->Form->create('User', array('action' => 'signup'));
  6791. $expected = array(
  6792. 'form' => array(
  6793. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6794. 'accept-charset' => $encoding
  6795. ),
  6796. 'div' => array('style' => 'display:none;'),
  6797. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. $this->Form->request->data = array();
  6802. $this->Form->request['controller'] = 'contacts';
  6803. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6804. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6805. $expected = array(
  6806. 'form' => array(
  6807. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6808. 'accept-charset' => 'utf-8'
  6809. ),
  6810. 'div' => array('style' => 'display:none;'),
  6811. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6812. '/div'
  6813. );
  6814. $this->assertTags($result, $expected);
  6815. }
  6816. /**
  6817. * Test the onsubmit option for create()
  6818. *
  6819. * @return void
  6820. */
  6821. public function testCreateOnSubmit() {
  6822. $this->Form->request->data = array();
  6823. $this->Form->request['controller'] = 'contacts';
  6824. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6825. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6826. $expected = array(
  6827. 'form' => array(
  6828. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6829. 'accept-charset' => 'utf-8'
  6830. ),
  6831. 'div' => array('style' => 'display:none;'),
  6832. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6833. '/div'
  6834. );
  6835. $this->assertTags($result, $expected);
  6836. $this->Form->request->data = array();
  6837. $this->Form->request['controller'] = 'contacts';
  6838. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6839. $result = $this->Form->create(array(
  6840. 'url' => array('action' => 'index', 'param'),
  6841. 'default' => false,
  6842. 'onsubmit' => 'someFunction();'
  6843. ));
  6844. $expected = array(
  6845. 'form' => array(
  6846. 'id' => 'ContactAddForm', 'method' => 'post',
  6847. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6848. 'action' => '/contacts/index/param',
  6849. 'accept-charset' => 'utf-8'
  6850. ),
  6851. 'div' => array('style' => 'display:none;'),
  6852. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6853. '/div'
  6854. );
  6855. $this->assertTags($result, $expected);
  6856. }
  6857. /**
  6858. * test create() with automatic url generation
  6859. *
  6860. * @return void
  6861. */
  6862. public function testCreateAutoUrl() {
  6863. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6864. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6865. $this->Form->request->base = '/base_url';
  6866. $result = $this->Form->create('Contact');
  6867. $expected = array(
  6868. 'form' => array(
  6869. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6870. 'accept-charset' => 'utf-8'
  6871. ),
  6872. 'div' => array('style' => 'display:none;'),
  6873. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6874. '/div'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. $this->Form->request['action'] = 'delete';
  6878. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6879. $this->Form->request->base = '/base_url';
  6880. $result = $this->Form->create('Contact');
  6881. $expected = array(
  6882. 'form' => array(
  6883. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6884. 'accept-charset' => 'utf-8'
  6885. ),
  6886. 'div' => array('style' => 'display:none;'),
  6887. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6888. '/div'
  6889. );
  6890. $this->assertTags($result, $expected);
  6891. }
  6892. /**
  6893. * test create() with a custom route
  6894. *
  6895. * @return void
  6896. */
  6897. public function testCreateCustomRoute() {
  6898. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6899. $encoding = strtolower(Configure::read('App.encoding'));
  6900. $result = $this->Form->create('User', array('action' => 'login'));
  6901. $expected = array(
  6902. 'form' => array(
  6903. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6904. 'accept-charset' => $encoding
  6905. ),
  6906. 'div' => array('style' => 'display:none;'),
  6907. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6908. '/div'
  6909. );
  6910. $this->assertTags($result, $expected);
  6911. }
  6912. /**
  6913. * test that inputDefaults are stored and used.
  6914. *
  6915. * @return void
  6916. */
  6917. public function testCreateWithInputDefaults() {
  6918. $this->Form->create('User', array(
  6919. 'inputDefaults' => array(
  6920. 'div' => false,
  6921. 'label' => false,
  6922. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6923. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6924. )
  6925. ));
  6926. $result = $this->Form->input('username');
  6927. $expected = array(
  6928. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6929. );
  6930. $this->assertTags($result, $expected);
  6931. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6932. $expected = array(
  6933. 'div' => array('class' => 'input text'),
  6934. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6935. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6940. $expected = array(
  6941. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6942. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6943. );
  6944. $this->assertTags($result, $expected);
  6945. $this->Form->create('User', array(
  6946. 'inputDefaults' => array(
  6947. 'div' => false,
  6948. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6949. )
  6950. ));
  6951. $result = $this->Form->input('username', array('div' => true));
  6952. $expected = array(
  6953. 'div' => array('class' => 'input text'),
  6954. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6955. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6956. '/div'
  6957. );
  6958. $this->assertTags($result, $expected);
  6959. }
  6960. /**
  6961. * test automatic accept-charset overriding
  6962. *
  6963. * @return void
  6964. */
  6965. public function testCreateWithAcceptCharset() {
  6966. $result = $this->Form->create('UserForm', array(
  6967. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  6968. )
  6969. );
  6970. $expected = array(
  6971. 'form' => array(
  6972. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6973. 'accept-charset' => 'iso-8859-1'
  6974. ),
  6975. 'div' => array('style' => 'display:none;'),
  6976. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6977. '/div'
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. }
  6981. /**
  6982. * Test base form url when url param is passed with multiple parameters (&)
  6983. *
  6984. */
  6985. public function testCreateQuerystringrequest() {
  6986. $encoding = strtolower(Configure::read('App.encoding'));
  6987. $result = $this->Form->create('Contact', array(
  6988. 'type' => 'post',
  6989. 'escape' => false,
  6990. 'url' => array(
  6991. 'controller' => 'controller',
  6992. 'action' => 'action',
  6993. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6994. )
  6995. ));
  6996. $expected = array(
  6997. 'form' => array(
  6998. 'id' => 'ContactAddForm',
  6999. 'method' => 'post',
  7000. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7001. 'accept-charset' => $encoding
  7002. ),
  7003. 'div' => array('style' => 'display:none;'),
  7004. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7005. '/div'
  7006. );
  7007. $this->assertTags($result, $expected);
  7008. $result = $this->Form->create('Contact', array(
  7009. 'type' => 'post',
  7010. 'url' => array(
  7011. 'controller' => 'controller',
  7012. 'action' => 'action',
  7013. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7014. )
  7015. ));
  7016. $expected = array(
  7017. 'form' => array(
  7018. 'id' => 'ContactAddForm',
  7019. 'method' => 'post',
  7020. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7021. 'accept-charset' => $encoding
  7022. ),
  7023. 'div' => array('style' => 'display:none;'),
  7024. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7025. '/div'
  7026. );
  7027. $this->assertTags($result, $expected);
  7028. }
  7029. /**
  7030. * test that create() doesn't cause errors by multiple id's being in the primary key
  7031. * as could happen with multiple select or checkboxes.
  7032. *
  7033. * @return void
  7034. */
  7035. public function testCreateWithMultipleIdInData() {
  7036. $encoding = strtolower(Configure::read('App.encoding'));
  7037. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7038. $result = $this->Form->create('Contact');
  7039. $expected = array(
  7040. 'form' => array(
  7041. 'id' => 'ContactAddForm',
  7042. 'method' => 'post',
  7043. 'action' => '/contacts/add',
  7044. 'accept-charset' => $encoding
  7045. ),
  7046. 'div' => array('style' => 'display:none;'),
  7047. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7048. '/div'
  7049. );
  7050. $this->assertTags($result, $expected);
  7051. }
  7052. /**
  7053. * test that create() doesn't add in extra passed params.
  7054. *
  7055. * @return void
  7056. */
  7057. public function testCreatePassedArgs() {
  7058. $encoding = strtolower(Configure::read('App.encoding'));
  7059. $this->Form->request->data['Contact']['id'] = 1;
  7060. $result = $this->Form->create('Contact', array(
  7061. 'type' => 'post',
  7062. 'escape' => false,
  7063. 'url' => array(
  7064. 'action' => 'edit',
  7065. 'myparam'
  7066. )
  7067. ));
  7068. $expected = array(
  7069. 'form' => array(
  7070. 'id' => 'ContactAddForm',
  7071. 'method' => 'post',
  7072. 'action' => '/contacts/edit/myparam',
  7073. 'accept-charset' => $encoding
  7074. ),
  7075. 'div' => array('style' => 'display:none;'),
  7076. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. }
  7081. /**
  7082. * test creating a get form, and get form inputs.
  7083. *
  7084. * @return void
  7085. */
  7086. public function testGetFormCreate() {
  7087. $encoding = strtolower(Configure::read('App.encoding'));
  7088. $result = $this->Form->create('Contact', array('type' => 'get'));
  7089. $this->assertTags($result, array('form' => array(
  7090. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7091. 'accept-charset' => $encoding
  7092. )));
  7093. $result = $this->Form->text('Contact.name');
  7094. $this->assertTags($result, array('input' => array(
  7095. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7096. )));
  7097. $result = $this->Form->password('password');
  7098. $this->assertTags($result, array('input' => array(
  7099. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7100. )));
  7101. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7102. $result = $this->Form->text('user_form');
  7103. $this->assertTags($result, array('input' => array(
  7104. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7105. )));
  7106. }
  7107. /**
  7108. * test get form, and inputs when the model param is false
  7109. *
  7110. * @return void
  7111. */
  7112. public function testGetFormWithFalseModel() {
  7113. $encoding = strtolower(Configure::read('App.encoding'));
  7114. $this->Form->request['controller'] = 'contact_test';
  7115. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7116. $expected = array('form' => array(
  7117. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7118. 'accept-charset' => $encoding
  7119. ));
  7120. $this->assertTags($result, $expected);
  7121. $result = $this->Form->text('reason');
  7122. $expected = array(
  7123. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7124. );
  7125. $this->assertTags($result, $expected);
  7126. }
  7127. /**
  7128. * test that datetime() works with GET style forms.
  7129. *
  7130. * @return void
  7131. */
  7132. public function testDateTimeWithGetForms() {
  7133. extract($this->dateRegex);
  7134. $this->Form->create('Contact', array('type' => 'get'));
  7135. $result = $this->Form->datetime('created');
  7136. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7137. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7138. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7139. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7140. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7141. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7142. }
  7143. /**
  7144. * testEditFormWithData method
  7145. *
  7146. * test auto populating form elements from submitted data.
  7147. *
  7148. * @return void
  7149. */
  7150. public function testEditFormWithData() {
  7151. $this->Form->request->data = array('Person' => array(
  7152. 'id' => 1,
  7153. 'first_name' => 'Nate',
  7154. 'last_name' => 'Abele',
  7155. 'email' => 'nate@example.com'
  7156. ));
  7157. $this->Form->request->addParams(array(
  7158. 'models' => array('Person'),
  7159. 'controller' => 'people',
  7160. 'action' => 'add'
  7161. ));
  7162. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7163. $this->Form->create();
  7164. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7165. $expected = array(
  7166. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7167. 'select' => array(
  7168. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7169. ),
  7170. array('option' => array('value' => 1)), 'Nate', '/option',
  7171. array('option' => array('value' => 2)), 'Garrett', '/option',
  7172. array('option' => array('value' => 3)), 'Larry', '/option',
  7173. '/select'
  7174. );
  7175. $this->assertTags($result, $expected);
  7176. }
  7177. /**
  7178. * Test that required fields are created for various types of validation.
  7179. *
  7180. * @return void
  7181. */
  7182. public function testFormInputRequiredDetection() {
  7183. $this->Form->create('Contact');
  7184. $result = $this->Form->input('Contact.non_existing');
  7185. $expected = array(
  7186. 'div' => array('class' => 'input text'),
  7187. 'label' => array('for' => 'ContactNonExisting'),
  7188. 'Non Existing',
  7189. '/label',
  7190. 'input' => array(
  7191. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7192. 'id' => 'ContactNonExisting'
  7193. ),
  7194. '/div'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->input('Contact.imrequired');
  7198. $expected = array(
  7199. 'div' => array('class' => 'input text required'),
  7200. 'label' => array('for' => 'ContactImrequired'),
  7201. 'Imrequired',
  7202. '/label',
  7203. 'input' => array(
  7204. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7205. 'id' => 'ContactImrequired',
  7206. 'required' => 'required'
  7207. ),
  7208. '/div'
  7209. );
  7210. $this->assertTags($result, $expected);
  7211. $result = $this->Form->input('Contact.imalsorequired');
  7212. $expected = array(
  7213. 'div' => array('class' => 'input text required'),
  7214. 'label' => array('for' => 'ContactImalsorequired'),
  7215. 'Imalsorequired',
  7216. '/label',
  7217. 'input' => array(
  7218. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7219. 'id' => 'ContactImalsorequired',
  7220. 'required' => 'required'
  7221. ),
  7222. '/div'
  7223. );
  7224. $this->assertTags($result, $expected);
  7225. $result = $this->Form->input('Contact.imrequiredtoo');
  7226. $expected = array(
  7227. 'div' => array('class' => 'input text required'),
  7228. 'label' => array('for' => 'ContactImrequiredtoo'),
  7229. 'Imrequiredtoo',
  7230. '/label',
  7231. 'input' => array(
  7232. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7233. 'id' => 'ContactImrequiredtoo',
  7234. 'required' => 'required'
  7235. ),
  7236. '/div'
  7237. );
  7238. $this->assertTags($result, $expected);
  7239. $result = $this->Form->input('Contact.required_one');
  7240. $expected = array(
  7241. 'div' => array('class' => 'input text required'),
  7242. 'label' => array('for' => 'ContactRequiredOne'),
  7243. 'Required One',
  7244. '/label',
  7245. 'input' => array(
  7246. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7247. 'id' => 'ContactRequiredOne',
  7248. 'required' => 'required'
  7249. ),
  7250. '/div'
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. $result = $this->Form->input('Contact.string_required');
  7254. $expected = array(
  7255. 'div' => array('class' => 'input text required'),
  7256. 'label' => array('for' => 'ContactStringRequired'),
  7257. 'String Required',
  7258. '/label',
  7259. 'input' => array(
  7260. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7261. 'id' => 'ContactStringRequired',
  7262. 'required' => 'required'
  7263. ),
  7264. '/div'
  7265. );
  7266. $this->assertTags($result, $expected);
  7267. $result = $this->Form->input('Contact.imnotrequired');
  7268. $expected = array(
  7269. 'div' => array('class' => 'input text'),
  7270. 'label' => array('for' => 'ContactImnotrequired'),
  7271. 'Imnotrequired',
  7272. '/label',
  7273. 'input' => array(
  7274. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7275. 'id' => 'ContactImnotrequired'
  7276. ),
  7277. '/div'
  7278. );
  7279. $this->assertTags($result, $expected);
  7280. $result = $this->Form->input('Contact.imalsonotrequired');
  7281. $expected = array(
  7282. 'div' => array('class' => 'input text'),
  7283. 'label' => array('for' => 'ContactImalsonotrequired'),
  7284. 'Imalsonotrequired',
  7285. '/label',
  7286. 'input' => array(
  7287. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7288. 'id' => 'ContactImalsonotrequired'
  7289. ),
  7290. '/div'
  7291. );
  7292. $this->assertTags($result, $expected);
  7293. $result = $this->Form->input('Contact.imalsonotrequired2');
  7294. $expected = array(
  7295. 'div' => array('class' => 'input text'),
  7296. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7297. 'Imalsonotrequired2',
  7298. '/label',
  7299. 'input' => array(
  7300. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7301. 'id' => 'ContactImalsonotrequired2'
  7302. ),
  7303. '/div'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. $result = $this->Form->input('Contact.imnotrequiredeither');
  7307. $expected = array(
  7308. 'div' => array('class' => 'input text'),
  7309. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7310. 'Imnotrequiredeither',
  7311. '/label',
  7312. 'input' => array(
  7313. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7314. 'id' => 'ContactImnotrequiredeither'
  7315. ),
  7316. '/div'
  7317. );
  7318. $this->assertTags($result, $expected);
  7319. $result = $this->Form->input('Contact.iamrequiredalways');
  7320. $expected = array(
  7321. 'div' => array('class' => 'input text required'),
  7322. 'label' => array('for' => 'ContactIamrequiredalways'),
  7323. 'Iamrequiredalways',
  7324. '/label',
  7325. 'input' => array(
  7326. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7327. 'id' => 'ContactIamrequiredalways',
  7328. 'required' => 'required'
  7329. ),
  7330. '/div'
  7331. );
  7332. $this->assertTags($result, $expected);
  7333. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7334. $expected = array(
  7335. 'div' => array('class' => 'input checkbox required'),
  7336. array('input' => array(
  7337. 'type' => 'hidden',
  7338. 'name' => 'data[Contact][boolean_field]',
  7339. 'id' => 'ContactBooleanField_',
  7340. 'value' => '0'
  7341. )),
  7342. array('input' => array(
  7343. 'type' => 'checkbox',
  7344. 'name' => 'data[Contact][boolean_field]',
  7345. 'value' => '1',
  7346. 'id' => 'ContactBooleanField'
  7347. )),
  7348. 'label' => array('for' => 'ContactBooleanField'),
  7349. 'Boolean Field',
  7350. '/label',
  7351. '/div'
  7352. );
  7353. $this->assertTags($result, $expected);
  7354. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7355. $expected = array(
  7356. 'div' => array('class' => 'input checkbox required'),
  7357. array('input' => array(
  7358. 'type' => 'hidden',
  7359. 'name' => 'data[Contact][boolean_field]',
  7360. 'id' => 'ContactBooleanField_',
  7361. 'value' => '0'
  7362. )),
  7363. array('input' => array(
  7364. 'type' => 'checkbox',
  7365. 'name' => 'data[Contact][boolean_field]',
  7366. 'value' => '1',
  7367. 'id' => 'ContactBooleanField',
  7368. 'required' => 'required'
  7369. )),
  7370. 'label' => array('for' => 'ContactBooleanField'),
  7371. 'Boolean Field',
  7372. '/label',
  7373. '/div'
  7374. );
  7375. $this->assertTags($result, $expected);
  7376. }
  7377. /**
  7378. * testFormMagicInput method
  7379. *
  7380. * @return void
  7381. */
  7382. public function testFormMagicInput() {
  7383. $encoding = strtolower(Configure::read('App.encoding'));
  7384. $result = $this->Form->create('Contact');
  7385. $expected = array(
  7386. 'form' => array(
  7387. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7388. 'accept-charset' => $encoding
  7389. ),
  7390. 'div' => array('style' => 'display:none;'),
  7391. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7392. '/div'
  7393. );
  7394. $this->assertTags($result, $expected);
  7395. $result = $this->Form->input('name');
  7396. $expected = array(
  7397. 'div' => array('class' => 'input text'),
  7398. 'label' => array('for' => 'ContactName'),
  7399. 'Name',
  7400. '/label',
  7401. 'input' => array(
  7402. 'type' => 'text', 'name' => 'data[Contact][name]',
  7403. 'id' => 'ContactName', 'maxlength' => '255'
  7404. ),
  7405. '/div'
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $result = $this->Form->input('non_existing_field_in_contact_model');
  7409. $expected = array(
  7410. 'div' => array('class' => 'input text'),
  7411. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7412. 'Non Existing Field In Contact Model',
  7413. '/label',
  7414. 'input' => array(
  7415. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7416. 'id' => 'ContactNonExistingFieldInContactModel'
  7417. ),
  7418. '/div'
  7419. );
  7420. $this->assertTags($result, $expected);
  7421. $result = $this->Form->input('Address.street');
  7422. $expected = array(
  7423. 'div' => array('class' => 'input text'),
  7424. 'label' => array('for' => 'AddressStreet'),
  7425. 'Street',
  7426. '/label',
  7427. 'input' => array(
  7428. 'type' => 'text', 'name' => 'data[Address][street]',
  7429. 'id' => 'AddressStreet'
  7430. ),
  7431. '/div'
  7432. );
  7433. $this->assertTags($result, $expected);
  7434. $result = $this->Form->input('Address.non_existing_field_in_model');
  7435. $expected = array(
  7436. 'div' => array('class' => 'input text'),
  7437. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7438. 'Non Existing Field In Model',
  7439. '/label',
  7440. 'input' => array(
  7441. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7442. 'id' => 'AddressNonExistingFieldInModel'
  7443. ),
  7444. '/div'
  7445. );
  7446. $this->assertTags($result, $expected);
  7447. $result = $this->Form->input('name', array('div' => false));
  7448. $expected = array(
  7449. 'label' => array('for' => 'ContactName'),
  7450. 'Name',
  7451. '/label',
  7452. 'input' => array(
  7453. 'type' => 'text', 'name' => 'data[Contact][name]',
  7454. 'id' => 'ContactName', 'maxlength' => '255'
  7455. )
  7456. );
  7457. $this->assertTags($result, $expected);
  7458. extract($this->dateRegex);
  7459. $now = strtotime('now');
  7460. $result = $this->Form->input('Contact.published', array('div' => false));
  7461. $expected = array(
  7462. 'label' => array('for' => 'ContactPublishedMonth'),
  7463. 'Published',
  7464. '/label',
  7465. array('select' => array(
  7466. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7467. )),
  7468. $monthsRegex,
  7469. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7470. date('F', $now),
  7471. '/option',
  7472. '*/select',
  7473. '-',
  7474. array('select' => array(
  7475. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7476. )),
  7477. $daysRegex,
  7478. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7479. date('j', $now),
  7480. '/option',
  7481. '*/select',
  7482. '-',
  7483. array('select' => array(
  7484. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7485. )),
  7486. $yearsRegex,
  7487. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7488. date('Y', $now),
  7489. '*/select'
  7490. );
  7491. $this->assertTags($result, $expected);
  7492. $result = $this->Form->input('Contact.updated', array('div' => false));
  7493. $expected = array(
  7494. 'label' => array('for' => 'ContactUpdatedMonth'),
  7495. 'Updated',
  7496. '/label',
  7497. array('select' => array(
  7498. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7499. )),
  7500. $monthsRegex,
  7501. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7502. date('F', $now),
  7503. '/option',
  7504. '*/select',
  7505. '-',
  7506. array('select' => array(
  7507. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7508. )),
  7509. $daysRegex,
  7510. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7511. date('j', $now),
  7512. '/option',
  7513. '*/select',
  7514. '-',
  7515. array('select' => array(
  7516. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7517. )),
  7518. $yearsRegex,
  7519. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7520. date('Y', $now),
  7521. '*/select'
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. $result = $this->Form->input('UserForm.stuff');
  7525. $expected = array(
  7526. 'div' => array('class' => 'input text'),
  7527. 'label' => array('for' => 'UserFormStuff'),
  7528. 'Stuff',
  7529. '/label',
  7530. 'input' => array(
  7531. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7532. 'id' => 'UserFormStuff', 'maxlength' => 10
  7533. ),
  7534. '/div'
  7535. );
  7536. $this->assertTags($result, $expected);
  7537. }
  7538. /**
  7539. * testForMagicInputNonExistingNorValidated method
  7540. *
  7541. * @return void
  7542. */
  7543. public function testForMagicInputNonExistingNorValidated() {
  7544. $encoding = strtolower(Configure::read('App.encoding'));
  7545. $result = $this->Form->create('Contact');
  7546. $expected = array(
  7547. 'form' => array(
  7548. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7549. 'accept-charset' => $encoding
  7550. ),
  7551. 'div' => array('style' => 'display:none;'),
  7552. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7553. '/div'
  7554. );
  7555. $this->assertTags($result, $expected);
  7556. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7557. $expected = array(
  7558. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7559. 'Non Existing Nor Validated',
  7560. '/label',
  7561. 'input' => array(
  7562. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7563. 'id' => 'ContactNonExistingNorValidated'
  7564. )
  7565. );
  7566. $this->assertTags($result, $expected);
  7567. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7568. 'div' => false, 'value' => 'my value'
  7569. ));
  7570. $expected = array(
  7571. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7572. 'Non Existing Nor Validated',
  7573. '/label',
  7574. 'input' => array(
  7575. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7576. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7577. )
  7578. );
  7579. $this->assertTags($result, $expected);
  7580. $this->Form->request->data = array(
  7581. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7582. ));
  7583. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7584. $expected = array(
  7585. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7586. 'Non Existing Nor Validated',
  7587. '/label',
  7588. 'input' => array(
  7589. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7590. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7591. )
  7592. );
  7593. $this->assertTags($result, $expected);
  7594. }
  7595. /**
  7596. * testFormMagicInputLabel method
  7597. *
  7598. * @return void
  7599. */
  7600. public function testFormMagicInputLabel() {
  7601. $encoding = strtolower(Configure::read('App.encoding'));
  7602. $result = $this->Form->create('Contact');
  7603. $expected = array(
  7604. 'form' => array(
  7605. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7606. 'accept-charset' => $encoding
  7607. ),
  7608. 'div' => array('style' => 'display:none;'),
  7609. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7610. '/div'
  7611. );
  7612. $this->assertTags($result, $expected);
  7613. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7614. $this->assertTags($result, array('input' => array(
  7615. 'name' => 'data[Contact][name]', 'type' => 'text',
  7616. 'id' => 'ContactName', 'maxlength' => '255')
  7617. ));
  7618. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7619. $expected = array(
  7620. 'label' => array('for' => 'ContactName'),
  7621. 'My label',
  7622. '/label',
  7623. 'input' => array(
  7624. 'type' => 'text', 'name' => 'data[Contact][name]',
  7625. 'id' => 'ContactName', 'maxlength' => '255'
  7626. )
  7627. );
  7628. $this->assertTags($result, $expected);
  7629. $result = $this->Form->input('Contact.name', array(
  7630. 'div' => false, 'label' => array('class' => 'mandatory')
  7631. ));
  7632. $expected = array(
  7633. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7634. 'Name',
  7635. '/label',
  7636. 'input' => array(
  7637. 'type' => 'text', 'name' => 'data[Contact][name]',
  7638. 'id' => 'ContactName', 'maxlength' => '255'
  7639. )
  7640. );
  7641. $this->assertTags($result, $expected);
  7642. $result = $this->Form->input('Contact.name', array(
  7643. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7644. ));
  7645. $expected = array(
  7646. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7647. 'My label',
  7648. '/label',
  7649. 'input' => array(
  7650. 'type' => 'text', 'name' => 'data[Contact][name]',
  7651. 'id' => 'ContactName', 'maxlength' => '255'
  7652. )
  7653. );
  7654. $this->assertTags($result, $expected);
  7655. $result = $this->Form->input('Contact.name', array(
  7656. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7657. ));
  7658. $expected = array(
  7659. 'label' => array('for' => 'my_id'),
  7660. 'Name',
  7661. '/label',
  7662. 'input' => array(
  7663. 'type' => 'text', 'name' => 'data[Contact][name]',
  7664. 'id' => 'my_id', 'maxlength' => '255'
  7665. )
  7666. );
  7667. $this->assertTags($result, $expected);
  7668. $result = $this->Form->input('1.id');
  7669. $this->assertTags($result, array('input' => array(
  7670. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7671. 'id' => 'Contact1Id'
  7672. )));
  7673. $result = $this->Form->input("1.name");
  7674. $expected = array(
  7675. 'div' => array('class' => 'input text'),
  7676. 'label' => array('for' => 'Contact1Name'),
  7677. 'Name',
  7678. '/label',
  7679. 'input' => array(
  7680. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7681. 'id' => 'Contact1Name', 'maxlength' => '255'
  7682. ),
  7683. '/div'
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. $result = $this->Form->input('Contact.1.id');
  7687. $this->assertTags($result, array(
  7688. 'input' => array(
  7689. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7690. 'id' => 'Contact1Id'
  7691. )
  7692. ));
  7693. $result = $this->Form->input("Model.1.name");
  7694. $expected = array(
  7695. 'div' => array('class' => 'input text'),
  7696. 'label' => array('for' => 'Model1Name'),
  7697. 'Name',
  7698. '/label',
  7699. 'input' => array(
  7700. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7701. 'id' => 'Model1Name'
  7702. ),
  7703. '/div'
  7704. );
  7705. $this->assertTags($result, $expected);
  7706. }
  7707. /**
  7708. * testFormEnd method
  7709. *
  7710. * @return void
  7711. */
  7712. public function testFormEnd() {
  7713. $this->assertEquals('</form>', $this->Form->end());
  7714. $result = $this->Form->end('');
  7715. $expected = array(
  7716. 'div' => array('class' => 'submit'),
  7717. 'input' => array('type' => 'submit', 'value' => ''),
  7718. '/div',
  7719. '/form'
  7720. );
  7721. $this->assertTags($result, $expected);
  7722. $result = $this->Form->end(array('label' => ''));
  7723. $expected = array(
  7724. 'div' => array('class' => 'submit'),
  7725. 'input' => array('type' => 'submit', 'value' => ''),
  7726. '/div',
  7727. '/form'
  7728. );
  7729. $this->assertTags($result, $expected);
  7730. $result = $this->Form->end('save');
  7731. $expected = array(
  7732. 'div' => array('class' => 'submit'),
  7733. 'input' => array('type' => 'submit', 'value' => 'save'),
  7734. '/div',
  7735. '/form'
  7736. );
  7737. $this->assertTags($result, $expected);
  7738. $result = $this->Form->end(array('label' => 'save'));
  7739. $expected = array(
  7740. 'div' => array('class' => 'submit'),
  7741. 'input' => array('type' => 'submit', 'value' => 'save'),
  7742. '/div',
  7743. '/form'
  7744. );
  7745. $this->assertTags($result, $expected);
  7746. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7747. $expected = array(
  7748. 'div' => array('class' => 'submit'),
  7749. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7750. '/div',
  7751. '/form'
  7752. );
  7753. $this->assertTags($result, $expected);
  7754. $result = $this->Form->end(array('name' => 'Whatever'));
  7755. $expected = array(
  7756. 'div' => array('class' => 'submit'),
  7757. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7758. '/div',
  7759. '/form'
  7760. );
  7761. $this->assertTags($result, $expected);
  7762. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7763. $expected = array(
  7764. 'div' => array('class' => 'good'),
  7765. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7766. '/div',
  7767. '/form'
  7768. );
  7769. $this->assertTags($result, $expected);
  7770. $result = $this->Form->end(array(
  7771. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7772. ));
  7773. $expected = array(
  7774. 'div' => array('class' => 'good'),
  7775. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7776. '/div',
  7777. '/form'
  7778. );
  7779. $this->assertTags($result, $expected);
  7780. }
  7781. /**
  7782. * testMultipleFormWithIdFields method
  7783. *
  7784. * @return void
  7785. */
  7786. public function testMultipleFormWithIdFields() {
  7787. $this->Form->create('UserForm');
  7788. $result = $this->Form->input('id');
  7789. $this->assertTags($result, array('input' => array(
  7790. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7791. )));
  7792. $result = $this->Form->input('ValidateItem.id');
  7793. $this->assertTags($result, array('input' => array(
  7794. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7795. 'id' => 'ValidateItemId'
  7796. )));
  7797. $result = $this->Form->input('ValidateUser.id');
  7798. $this->assertTags($result, array('input' => array(
  7799. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7800. 'id' => 'ValidateUserId'
  7801. )));
  7802. }
  7803. /**
  7804. * testDbLessModel method
  7805. *
  7806. * @return void
  7807. */
  7808. public function testDbLessModel() {
  7809. $this->Form->create('TestMail');
  7810. $result = $this->Form->input('name');
  7811. $expected = array(
  7812. 'div' => array('class' => 'input text'),
  7813. 'label' => array('for' => 'TestMailName'),
  7814. 'Name',
  7815. '/label',
  7816. 'input' => array(
  7817. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7818. 'id' => 'TestMailName'
  7819. ),
  7820. '/div'
  7821. );
  7822. $this->assertTags($result, $expected);
  7823. ClassRegistry::init('TestMail');
  7824. $this->Form->create('TestMail');
  7825. $result = $this->Form->input('name');
  7826. $expected = array(
  7827. 'div' => array('class' => 'input text'),
  7828. 'label' => array('for' => 'TestMailName'),
  7829. 'Name',
  7830. '/label',
  7831. 'input' => array(
  7832. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7833. 'id' => 'TestMailName'
  7834. ),
  7835. '/div'
  7836. );
  7837. $this->assertTags($result, $expected);
  7838. }
  7839. /**
  7840. * testBrokenness method
  7841. *
  7842. * @return void
  7843. */
  7844. public function testBrokenness() {
  7845. /*
  7846. * #4 This test has two parents and four children. By default (as of r7117) both
  7847. * parents are show but the first parent is missing a child. This is the inconsistency
  7848. * in the default behaviour - one parent has all children, the other does not - dependent
  7849. * on the data values.
  7850. */
  7851. $result = $this->Form->select('Model.field', array(
  7852. 'Fred' => array(
  7853. 'freds_son_1' => 'Fred',
  7854. 'freds_son_2' => 'Freddie'
  7855. ),
  7856. 'Bert' => array(
  7857. 'berts_son_1' => 'Albert',
  7858. 'berts_son_2' => 'Bertie')
  7859. ),
  7860. array('showParents' => true, 'empty' => false)
  7861. );
  7862. $expected = array(
  7863. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7864. array('optgroup' => array('label' => 'Fred')),
  7865. array('option' => array('value' => 'freds_son_1')),
  7866. 'Fred',
  7867. '/option',
  7868. array('option' => array('value' => 'freds_son_2')),
  7869. 'Freddie',
  7870. '/option',
  7871. '/optgroup',
  7872. array('optgroup' => array('label' => 'Bert')),
  7873. array('option' => array('value' => 'berts_son_1')),
  7874. 'Albert',
  7875. '/option',
  7876. array('option' => array('value' => 'berts_son_2')),
  7877. 'Bertie',
  7878. '/option',
  7879. '/optgroup',
  7880. '/select'
  7881. );
  7882. $this->assertTags($result, $expected);
  7883. /*
  7884. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7885. * changed, so we should expect the same select list data, just with a different name
  7886. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7887. * This is where data corruption can occur, because when a select value is missing from
  7888. * a list a form will substitute the first value in the list - without the user knowing.
  7889. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7890. * affect the availability of 3 => 'Three' as a valid option.
  7891. */
  7892. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7893. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7894. ));
  7895. $result = $this->Form->select(
  7896. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7897. );
  7898. $expected = array(
  7899. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7900. array('option' => array('value' => 1)),
  7901. 'One',
  7902. '/option',
  7903. array('option' => array('value' => 2)),
  7904. 'Two',
  7905. '/option',
  7906. array('optgroup' => array('label' => 'Three')),
  7907. array('option' => array('value' => 3)),
  7908. 'Three',
  7909. '/option',
  7910. array('option' => array('value' => 4)),
  7911. 'Four',
  7912. '/option',
  7913. array('option' => array('value' => 5)),
  7914. 'Five',
  7915. '/option',
  7916. '/optgroup',
  7917. '/select'
  7918. );
  7919. $this->assertTags($result, $expected);
  7920. }
  7921. /**
  7922. * Test the generation of fields for a multi record form.
  7923. *
  7924. * @return void
  7925. */
  7926. public function testMultiRecordForm() {
  7927. $this->Form->create('ValidateProfile');
  7928. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7929. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7930. $expected = array(
  7931. 'div' => array('class' => 'input textarea'),
  7932. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7933. 'Name',
  7934. '/label',
  7935. 'textarea' => array(
  7936. 'id' => 'ValidateProfile1ValidateItem2Name',
  7937. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7938. 'cols' => 30,
  7939. 'rows' => 6
  7940. ),
  7941. 'Value',
  7942. '/textarea',
  7943. '/div'
  7944. );
  7945. $this->assertTags($result, $expected);
  7946. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  7947. $expected = array(
  7948. 'div' => array('class' => 'input date'),
  7949. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7950. 'Created',
  7951. '/label',
  7952. array('select' => array(
  7953. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7954. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7955. )
  7956. ),
  7957. array('option' => array('value' => '')), '/option',
  7958. $this->dateRegex['monthsRegex'],
  7959. '/select', '-',
  7960. array('select' => array(
  7961. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7962. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7963. )
  7964. ),
  7965. array('option' => array('value' => '')), '/option',
  7966. $this->dateRegex['daysRegex'],
  7967. '/select', '-',
  7968. array('select' => array(
  7969. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7970. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7971. )
  7972. ),
  7973. array('option' => array('value' => '')), '/option',
  7974. $this->dateRegex['yearsRegex'],
  7975. '/select',
  7976. '/div'
  7977. );
  7978. $this->assertTags($result, $expected);
  7979. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7980. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7981. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7982. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7983. $expected = array(
  7984. 'div' => array('class' => 'input select error'),
  7985. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7986. 'Profile',
  7987. '/label',
  7988. 'select' => array(
  7989. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7990. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7991. 'class' => 'form-error'
  7992. ),
  7993. '/select',
  7994. array('div' => array('class' => 'error-message')),
  7995. 'Error',
  7996. '/div',
  7997. '/div'
  7998. );
  7999. $this->assertTags($result, $expected);
  8000. }
  8001. /**
  8002. * test the correct display of multi-record form validation errors.
  8003. *
  8004. * @return void
  8005. */
  8006. public function testMultiRecordFormValidationErrors() {
  8007. $this->Form->create('ValidateProfile');
  8008. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8009. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8010. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8011. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8012. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8013. $result = $this->Form->error('ValidateProfile.2.city');
  8014. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8015. $result = $this->Form->error('2.city');
  8016. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8017. }
  8018. /**
  8019. * test the correct display of multi-record form validation errors.
  8020. *
  8021. * @return void
  8022. */
  8023. public function testSaveManyRecordFormValidationErrors() {
  8024. $this->Form->create('ValidateUser');
  8025. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8026. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8027. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8028. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8029. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8030. $result = $this->Form->error('ValidateUser.0.city');
  8031. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8032. }
  8033. /**
  8034. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8035. *
  8036. * @return void
  8037. */
  8038. public function testInputTemplate() {
  8039. $result = $this->Form->input('Contact.email', array(
  8040. 'type' => 'text', 'format' => array('input')
  8041. ));
  8042. $expected = array(
  8043. 'div' => array('class' => 'input text'),
  8044. 'input' => array(
  8045. 'type' => 'text', 'name' => 'data[Contact][email]',
  8046. 'id' => 'ContactEmail'
  8047. ),
  8048. '/div'
  8049. );
  8050. $this->assertTags($result, $expected);
  8051. $result = $this->Form->input('Contact.email', array(
  8052. 'type' => 'text', 'format' => array('input', 'label'),
  8053. 'label' => '<em>Email (required)</em>'
  8054. ));
  8055. $expected = array(
  8056. 'div' => array('class' => 'input text'),
  8057. array('input' => array(
  8058. 'type' => 'text', 'name' => 'data[Contact][email]',
  8059. 'id' => 'ContactEmail'
  8060. )),
  8061. 'label' => array('for' => 'ContactEmail'),
  8062. 'em' => array(),
  8063. 'Email (required)',
  8064. '/em',
  8065. '/label',
  8066. '/div'
  8067. );
  8068. $this->assertTags($result, $expected);
  8069. $result = $this->Form->input('Contact.email', array(
  8070. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8071. 'between' => '<div>Something in the middle</div>',
  8072. 'after' => '<span>Some text at the end</span>'
  8073. ));
  8074. $expected = array(
  8075. 'div' => array('class' => 'input text'),
  8076. array('input' => array(
  8077. 'type' => 'text', 'name' => 'data[Contact][email]',
  8078. 'id' => 'ContactEmail'
  8079. )),
  8080. array('div' => array()),
  8081. 'Something in the middle',
  8082. '/div',
  8083. 'label' => array('for' => 'ContactEmail'),
  8084. 'Email',
  8085. '/label',
  8086. 'span' => array(),
  8087. 'Some text at the end',
  8088. '/span',
  8089. '/div'
  8090. );
  8091. $this->assertTags($result, $expected);
  8092. $result = $this->Form->input('Contact.method', array(
  8093. 'type' => 'radio',
  8094. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8095. 'between' => 'I am between',
  8096. ));
  8097. $expected = array(
  8098. 'div' => array('class' => 'input radio'),
  8099. 'fieldset' => array(),
  8100. 'legend' => array(),
  8101. 'Method',
  8102. '/legend',
  8103. 'I am between',
  8104. 'input' => array(
  8105. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8106. 'value' => '', 'id' => 'ContactMethod_'
  8107. ),
  8108. array('input' => array(
  8109. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8110. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8111. )),
  8112. array('label' => array('for' => 'ContactMethodEmail')),
  8113. 'Email',
  8114. '/label',
  8115. array('input' => array(
  8116. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8117. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8118. )),
  8119. array('label' => array('for' => 'ContactMethodPigeon')),
  8120. 'Pigeon',
  8121. '/label',
  8122. '/fieldset',
  8123. '/div',
  8124. );
  8125. $this->assertTags($result, $expected);
  8126. }
  8127. /**
  8128. * test that some html5 inputs + FormHelper::__call() work
  8129. *
  8130. * @return void
  8131. */
  8132. public function testHtml5Inputs() {
  8133. $result = $this->Form->email('User.email');
  8134. $expected = array(
  8135. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8136. );
  8137. $this->assertTags($result, $expected);
  8138. $result = $this->Form->search('User.query');
  8139. $expected = array(
  8140. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8141. );
  8142. $this->assertTags($result, $expected);
  8143. $result = $this->Form->search('User.query', array('value' => 'test'));
  8144. $expected = array(
  8145. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8146. );
  8147. $this->assertTags($result, $expected);
  8148. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8149. $expected = array(
  8150. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8151. );
  8152. $this->assertTags($result, $expected);
  8153. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8154. $expected = array(
  8155. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8156. );
  8157. $this->assertTags($result, $expected);
  8158. }
  8159. /**
  8160. *
  8161. * @expectedException CakeException
  8162. * @return void
  8163. */
  8164. public function testHtml5InputException() {
  8165. $this->Form->email();
  8166. }
  8167. /**
  8168. * Tests that a model can be loaded from the model names passed in the request object
  8169. *
  8170. * @return void
  8171. */
  8172. public function testIntrospectModelFromRequest() {
  8173. $this->loadFixtures('Post');
  8174. App::build(array(
  8175. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8176. ));
  8177. CakePlugin::load('TestPlugin');
  8178. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8179. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8180. $this->Form->create('TestPluginPost');
  8181. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8182. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8183. CakePlugin::unload();
  8184. App::build();
  8185. }
  8186. /**
  8187. * Tests that it is possible to set the validation errors directly in the helper for a field
  8188. *
  8189. * @return void
  8190. */
  8191. public function testCustomValidationErrors() {
  8192. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8193. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8194. $this->assertEquals('Badness!', $result);
  8195. }
  8196. /**
  8197. * Tests that the 'on' key validates as expected on create
  8198. *
  8199. * @return void
  8200. */
  8201. public function testRequiredOnCreate() {
  8202. $this->Form->create('Contact');
  8203. $result = $this->Form->input('Contact.imrequiredonupdate');
  8204. $expected = array(
  8205. 'div' => array('class' => 'input text'),
  8206. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8207. 'Imrequiredonupdate',
  8208. '/label',
  8209. 'input' => array(
  8210. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8211. 'id' => 'ContactImrequiredonupdate'
  8212. ),
  8213. '/div'
  8214. );
  8215. $this->assertTags($result, $expected);
  8216. $result = $this->Form->input('Contact.imrequiredoncreate');
  8217. $expected = array(
  8218. 'div' => array('class' => 'input text required'),
  8219. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8220. 'Imrequiredoncreate',
  8221. '/label',
  8222. 'input' => array(
  8223. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8224. 'id' => 'ContactImrequiredoncreate',
  8225. 'required' => 'required'
  8226. ),
  8227. '/div'
  8228. );
  8229. $this->assertTags($result, $expected);
  8230. $result = $this->Form->input('Contact.imrequiredonboth');
  8231. $expected = array(
  8232. 'div' => array('class' => 'input text required'),
  8233. 'label' => array('for' => 'ContactImrequiredonboth'),
  8234. 'Imrequiredonboth',
  8235. '/label',
  8236. 'input' => array(
  8237. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8238. 'id' => 'ContactImrequiredonboth',
  8239. 'required' => 'required'
  8240. ),
  8241. '/div'
  8242. );
  8243. $this->assertTags($result, $expected);
  8244. $this->Form->inputDefaults(array('required' => false));
  8245. $result = $this->Form->input('Contact.imrequired');
  8246. $expected = array(
  8247. 'div' => array('class' => 'input text'),
  8248. 'label' => array('for' => 'ContactImrequired'),
  8249. 'Imrequired',
  8250. '/label',
  8251. 'input' => array(
  8252. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8253. 'id' => 'ContactImrequired'
  8254. ),
  8255. '/div'
  8256. );
  8257. $this->assertTags($result, $expected);
  8258. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8259. $this->assertTags($result, $expected);
  8260. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8261. $expected = array(
  8262. 'div' => array('class' => 'input text required'),
  8263. 'label' => array('for' => 'ContactImrequired'),
  8264. 'Imrequired',
  8265. '/label',
  8266. 'input' => array(
  8267. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8268. 'id' => 'ContactImrequired'
  8269. ),
  8270. '/div'
  8271. );
  8272. $this->assertTags($result, $expected);
  8273. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8274. $this->assertTags($result, $expected);
  8275. }
  8276. /**
  8277. * Tests that the 'on' key validates as expected on update
  8278. *
  8279. * @return void
  8280. */
  8281. public function testRequiredOnUpdate() {
  8282. $this->Form->request->data['Contact']['id'] = 1;
  8283. $this->Form->create('Contact');
  8284. $result = $this->Form->input('Contact.imrequiredonupdate');
  8285. $expected = array(
  8286. 'div' => array('class' => 'input text required'),
  8287. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8288. 'Imrequiredonupdate',
  8289. '/label',
  8290. 'input' => array(
  8291. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8292. 'id' => 'ContactImrequiredonupdate',
  8293. 'required' => 'required'
  8294. ),
  8295. '/div'
  8296. );
  8297. $this->assertTags($result, $expected);
  8298. $result = $this->Form->input('Contact.imrequiredoncreate');
  8299. $expected = array(
  8300. 'div' => array('class' => 'input text'),
  8301. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8302. 'Imrequiredoncreate',
  8303. '/label',
  8304. 'input' => array(
  8305. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8306. 'id' => 'ContactImrequiredoncreate'
  8307. ),
  8308. '/div'
  8309. );
  8310. $this->assertTags($result, $expected);
  8311. $result = $this->Form->input('Contact.imrequiredonboth');
  8312. $expected = array(
  8313. 'div' => array('class' => 'input text required'),
  8314. 'label' => array('for' => 'ContactImrequiredonboth'),
  8315. 'Imrequiredonboth',
  8316. '/label',
  8317. 'input' => array(
  8318. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8319. 'id' => 'ContactImrequiredonboth',
  8320. 'required' => 'required'
  8321. ),
  8322. '/div'
  8323. );
  8324. $this->assertTags($result, $expected);
  8325. $result = $this->Form->input('Contact.imrequired');
  8326. $expected = array(
  8327. 'div' => array('class' => 'input text required'),
  8328. 'label' => array('for' => 'ContactImrequired'),
  8329. 'Imrequired',
  8330. '/label',
  8331. 'input' => array(
  8332. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8333. 'id' => 'ContactImrequired',
  8334. 'required' => 'required'
  8335. ),
  8336. '/div'
  8337. );
  8338. $this->assertTags($result, $expected);
  8339. }
  8340. /**
  8341. * Test inputDefaults setter and getter
  8342. *
  8343. * @return void
  8344. */
  8345. public function testInputDefaults() {
  8346. $this->Form->create('Contact');
  8347. $this->Form->inputDefaults(array(
  8348. 'label' => false,
  8349. 'div' => array(
  8350. 'style' => 'color: #000;'
  8351. )
  8352. ));
  8353. $result = $this->Form->input('Contact.field1');
  8354. $expected = array(
  8355. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8356. 'input' => array(
  8357. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8358. 'id' => 'ContactField1'
  8359. ),
  8360. '/div'
  8361. );
  8362. $this->assertTags($result, $expected);
  8363. $this->Form->inputDefaults(array(
  8364. 'div' => false,
  8365. 'label' => 'Label',
  8366. ));
  8367. $result = $this->Form->input('Contact.field1');
  8368. $expected = array(
  8369. 'label' => array('for' => 'ContactField1'),
  8370. 'Label',
  8371. '/label',
  8372. 'input' => array(
  8373. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8374. 'id' => 'ContactField1'
  8375. ),
  8376. );
  8377. $this->assertTags($result, $expected);
  8378. $this->Form->inputDefaults(array(
  8379. 'label' => false,
  8380. ), true);
  8381. $result = $this->Form->input('Contact.field1');
  8382. $expected = array(
  8383. 'input' => array(
  8384. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8385. 'id' => 'ContactField1'
  8386. ),
  8387. );
  8388. $this->assertTags($result, $expected);
  8389. $result = $this->Form->inputDefaults();
  8390. $expected = array(
  8391. 'div' => false,
  8392. 'label' => false,
  8393. );
  8394. $this->assertEqual($result, $expected);
  8395. }
  8396. }