FormHelperTest.php 306 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  319. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  320. );
  321. /**
  322. * beforeValidate method
  323. *
  324. * @return void
  325. */
  326. public function beforeValidate($options = array()) {
  327. $this->invalidate('email');
  328. return false;
  329. }
  330. }
  331. /**
  332. * ValidateProfile class
  333. *
  334. * @package Cake.Test.Case.View.Helper
  335. */
  336. class ValidateProfile extends CakeTestModel {
  337. /**
  338. * useTable property
  339. *
  340. * @var boolean
  341. */
  342. public $useTable = false;
  343. /**
  344. * schema property
  345. *
  346. * @var array
  347. */
  348. protected $_schema = array(
  349. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  350. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  351. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  352. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  353. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  354. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  355. );
  356. /**
  357. * hasOne property
  358. *
  359. * @var array
  360. */
  361. public $hasOne = array('ValidateItem' => array(
  362. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  363. ));
  364. /**
  365. * belongsTo property
  366. *
  367. * @var array
  368. */
  369. public $belongsTo = array('ValidateUser' => array(
  370. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  371. ));
  372. /**
  373. * beforeValidate method
  374. *
  375. * @return void
  376. */
  377. public function beforeValidate($options = array()) {
  378. $this->invalidate('full_name');
  379. $this->invalidate('city');
  380. return false;
  381. }
  382. }
  383. /**
  384. * ValidateItem class
  385. *
  386. * @package Cake.Test.Case.View.Helper
  387. */
  388. class ValidateItem extends CakeTestModel {
  389. /**
  390. * useTable property
  391. *
  392. * @var boolean
  393. */
  394. public $useTable = false;
  395. /**
  396. * schema property
  397. *
  398. * @var array
  399. */
  400. protected $_schema = array(
  401. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  402. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  403. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  404. 'description' => array(
  405. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  406. ),
  407. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  408. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  409. );
  410. /**
  411. * belongsTo property
  412. *
  413. * @var array
  414. */
  415. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  416. /**
  417. * beforeValidate method
  418. *
  419. * @return void
  420. */
  421. public function beforeValidate($options = array()) {
  422. $this->invalidate('description');
  423. return false;
  424. }
  425. }
  426. /**
  427. * TestMail class
  428. *
  429. * @package Cake.Test.Case.View.Helper
  430. */
  431. class TestMail extends CakeTestModel {
  432. /**
  433. * useTable property
  434. *
  435. * @var boolean
  436. */
  437. public $useTable = false;
  438. }
  439. /**
  440. * FormHelperTest class
  441. *
  442. * @package Cake.Test.Case.View.Helper
  443. * @property FormHelper $Form
  444. */
  445. class FormHelperTest extends CakeTestCase {
  446. /**
  447. * Fixtures to be used
  448. *
  449. * @var array
  450. */
  451. public $fixtures = array('core.post');
  452. /**
  453. * Do not load the fixtures by default
  454. *
  455. * @var boolean
  456. */
  457. public $autoFixtures = false;
  458. /**
  459. * setUp method
  460. *
  461. * @return void
  462. */
  463. public function setUp() {
  464. parent::setUp();
  465. Configure::write('Config.language', 'eng');
  466. Configure::write('App.base', '');
  467. Configure::delete('Asset');
  468. $this->Controller = new ContactTestController();
  469. $this->View = new View($this->Controller);
  470. $this->Form = new FormHelper($this->View);
  471. $this->Form->Html = new HtmlHelper($this->View);
  472. $this->Form->request = new CakeRequest('contacts/add', false);
  473. $this->Form->request->here = '/contacts/add';
  474. $this->Form->request['action'] = 'add';
  475. $this->Form->request->webroot = '';
  476. $this->Form->request->base = '';
  477. ClassRegistry::addObject('Contact', new Contact());
  478. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  479. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  480. ClassRegistry::addObject('User', new UserForm());
  481. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  482. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  483. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  484. $this->oldSalt = Configure::read('Security.salt');
  485. $this->dateRegex = array(
  486. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  487. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  488. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  491. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  492. );
  493. Configure::write('Security.salt', 'foo!');
  494. }
  495. /**
  496. * tearDown method
  497. *
  498. * @return void
  499. */
  500. public function tearDown() {
  501. parent::tearDown();
  502. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  503. Configure::write('Security.salt', $this->oldSalt);
  504. }
  505. /**
  506. * testFormCreateWithSecurity method
  507. *
  508. * Test form->create() with security key.
  509. *
  510. * @return void
  511. */
  512. public function testCreateWithSecurity() {
  513. $this->Form->request['_Token'] = array('key' => 'testKey');
  514. $encoding = strtolower(Configure::read('App.encoding'));
  515. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  516. $expected = array(
  517. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  518. 'div' => array('style' => 'display:none;'),
  519. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  520. array('input' => array(
  521. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  522. )),
  523. '/div'
  524. );
  525. $this->assertTags($result, $expected);
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  527. $expected['form']['id'] = 'MyForm';
  528. $this->assertTags($result, $expected);
  529. }
  530. /**
  531. * testFormCreateGetNoSecurity method
  532. *
  533. * Test form->create() with no security key as its a get form
  534. *
  535. * @return void
  536. */
  537. public function testCreateEndGetNoSecurity() {
  538. $this->Form->request['_Token'] = array('key' => 'testKey');
  539. $encoding = strtolower(Configure::read('App.encoding'));
  540. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  541. $this->assertNotContains('Token', $result);
  542. $result = $this->Form->end('Save');
  543. $this->assertNotContains('Token', $result);
  544. }
  545. /**
  546. * test that create() clears the fields property so it starts fresh
  547. *
  548. * @return void
  549. */
  550. public function testCreateClearingFields() {
  551. $this->Form->fields = array('model_id');
  552. $this->Form->create('Contact');
  553. $this->assertEquals(array(), $this->Form->fields);
  554. }
  555. /**
  556. * Tests form hash generation with model-less data
  557. *
  558. * @return void
  559. */
  560. public function testValidateHashNoModel() {
  561. $this->Form->request['_Token'] = array('key' => 'foo');
  562. $result = $this->Form->secure(array('anything'));
  563. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  564. }
  565. /**
  566. * Tests that models with identical field names get resolved properly
  567. *
  568. * @return void
  569. */
  570. public function testDuplicateFieldNameResolution() {
  571. $result = $this->Form->create('ValidateUser');
  572. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  573. $result = $this->Form->input('ValidateItem.name');
  574. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateUser.name');
  576. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  577. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  578. $this->assertRegExp('/type="text"/', $result);
  579. $result = $this->Form->input('ValidateItem.name');
  580. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  581. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  582. $this->assertRegExp('/<textarea/', $result);
  583. $result = $this->Form->input('name');
  584. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  585. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  586. $this->assertRegExp('/type="text"/', $result);
  587. }
  588. /**
  589. * Tests that hidden fields generated for checkboxes don't get locked
  590. *
  591. * @return void
  592. */
  593. public function testNoCheckboxLocking() {
  594. $this->Form->request['_Token'] = array('key' => 'foo');
  595. $this->assertSame(array(), $this->Form->fields);
  596. $this->Form->checkbox('check', array('value' => '1'));
  597. $this->assertSame($this->Form->fields, array('check'));
  598. }
  599. /**
  600. * testFormSecurityFields method
  601. *
  602. * Test generation of secure form hash generation.
  603. *
  604. * @return void
  605. */
  606. public function testFormSecurityFields() {
  607. $key = 'testKey';
  608. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  609. $this->Form->request['_Token'] = array('key' => $key);
  610. $result = $this->Form->secure($fields);
  611. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  612. $hash .= ':' . 'Model.valid';
  613. $hash = urlencode($hash);
  614. $expected = array(
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  618. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  619. )),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  622. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  623. )),
  624. '/div'
  625. );
  626. $this->assertTags($result, $expected);
  627. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  628. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  629. $result = $this->Form->secure($fields);
  630. $expected = array(
  631. 'div' => array('class' => 'hidden'),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  634. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  635. )),
  636. array('input' => array(
  637. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  638. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  639. )),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Tests correct generation of number fields for double and float fields
  646. *
  647. * @return void
  648. */
  649. public function testTextFieldGenerationForFloats() {
  650. $model = ClassRegistry::getObject('Contact');
  651. $model->setSchema(array('foo' => array(
  652. 'type' => 'float',
  653. 'null' => false,
  654. 'default' => null,
  655. 'length' => 10
  656. )));
  657. $this->Form->create('Contact');
  658. $result = $this->Form->input('foo');
  659. $expected = array(
  660. 'div' => array('class' => 'input number'),
  661. 'label' => array('for' => 'ContactFoo'),
  662. 'Foo',
  663. '/label',
  664. array('input' => array(
  665. 'type' => 'number',
  666. 'name' => 'data[Contact][foo]',
  667. 'id' => 'ContactFoo',
  668. 'step' => 'any'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. $result = $this->Form->input('foo', array('step' => 0.5));
  674. $expected = array(
  675. 'div' => array('class' => 'input number'),
  676. 'label' => array('for' => 'ContactFoo'),
  677. 'Foo',
  678. '/label',
  679. array('input' => array(
  680. 'type' => 'number',
  681. 'name' => 'data[Contact][foo]',
  682. 'id' => 'ContactFoo',
  683. 'step' => '0.5'
  684. )),
  685. '/div'
  686. );
  687. $this->assertTags($result, $expected);
  688. }
  689. /**
  690. * Tests correct generation of number fields for integer fields
  691. *
  692. * @return void
  693. */
  694. public function testTextFieldTypeNumberGenerationForIntegers() {
  695. $model = ClassRegistry::getObject('Contact');
  696. $model->setSchema(array('foo' => array(
  697. 'type' => 'integer',
  698. 'null' => false,
  699. 'default' => null,
  700. 'length' => null
  701. )));
  702. $this->Form->create('Contact');
  703. $result = $this->Form->input('foo');
  704. $expected = array(
  705. 'div' => array('class' => 'input number'),
  706. 'label' => array('for' => 'ContactFoo'),
  707. 'Foo',
  708. '/label',
  709. array('input' => array(
  710. 'type' => 'number', 'name' => 'data[Contact][foo]',
  711. 'id' => 'ContactFoo'
  712. )),
  713. '/div'
  714. );
  715. $this->assertTags($result, $expected);
  716. }
  717. /**
  718. * testFormSecurityMultipleFields method
  719. *
  720. * Test secure() with multiple row form. Ensure hash is correct.
  721. *
  722. * @return void
  723. */
  724. public function testFormSecurityMultipleFields() {
  725. $key = 'testKey';
  726. $fields = array(
  727. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  728. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  729. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  730. );
  731. $this->Form->request['_Token'] = array('key' => $key);
  732. $result = $this->Form->secure($fields);
  733. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  734. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  735. $expected = array(
  736. 'div' => array('style' => 'display:none;'),
  737. array('input' => array(
  738. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  739. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  740. )),
  741. array('input' => array(
  742. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  743. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  744. )),
  745. '/div'
  746. );
  747. $this->assertTags($result, $expected);
  748. }
  749. /**
  750. * testFormSecurityMultipleSubmitButtons
  751. *
  752. * test form submit generation and ensure that _Token is only created on end()
  753. *
  754. * @return void
  755. */
  756. public function testFormSecurityMultipleSubmitButtons() {
  757. $key = 'testKey';
  758. $this->Form->request['_Token'] = array('key' => $key);
  759. $this->Form->create('Addresses');
  760. $this->Form->input('Address.title');
  761. $this->Form->input('Address.first_name');
  762. $result = $this->Form->submit('Save', array('name' => 'save'));
  763. $expected = array(
  764. 'div' => array('class' => 'submit'),
  765. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  766. '/div',
  767. );
  768. $this->assertTags($result, $expected);
  769. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  770. $expected = array(
  771. 'div' => array('class' => 'submit'),
  772. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  773. '/div',
  774. );
  775. $this->assertTags($result, $expected);
  776. $result = $this->Form->end(null);
  777. $expected = array(
  778. 'div' => array('style' => 'display:none;'),
  779. array('input' => array(
  780. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  781. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  782. )),
  783. array('input' => array(
  784. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  785. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  786. )),
  787. '/div'
  788. );
  789. $this->assertTags($result, $expected);
  790. }
  791. /**
  792. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  793. *
  794. * @return void
  795. */
  796. public function testSecurityButtonNestedNamed() {
  797. $key = 'testKey';
  798. $this->Form->request['_Token'] = array('key' => $key);
  799. $this->Form->create('Addresses');
  800. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  801. $result = $this->Form->unlockField();
  802. $this->assertEquals(array('Address.button'), $result);
  803. }
  804. /**
  805. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecuritySubmitNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that the correct fields are unlocked for image submits with no names.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitImageNoName() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('User');
  826. $result = $this->Form->submit('save.png');
  827. $expected = array(
  828. 'div' => array('class' => 'submit'),
  829. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png', array('name' => 'test'));
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * testFormSecurityMultipleInputFields method
  855. *
  856. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  857. *
  858. * @return void
  859. */
  860. public function testFormSecurityMultipleInputFields() {
  861. $key = 'testKey';
  862. $this->Form->request['_Token'] = array('key' => $key);
  863. $this->Form->create('Addresses');
  864. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  865. $this->Form->input('Addresses.0.title');
  866. $this->Form->input('Addresses.0.first_name');
  867. $this->Form->input('Addresses.0.last_name');
  868. $this->Form->input('Addresses.0.address');
  869. $this->Form->input('Addresses.0.city');
  870. $this->Form->input('Addresses.0.phone');
  871. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  872. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  873. $this->Form->input('Addresses.1.title');
  874. $this->Form->input('Addresses.1.first_name');
  875. $this->Form->input('Addresses.1.last_name');
  876. $this->Form->input('Addresses.1.address');
  877. $this->Form->input('Addresses.1.city');
  878. $this->Form->input('Addresses.1.phone');
  879. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  880. $result = $this->Form->secure($this->Form->fields);
  881. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  882. $expected = array(
  883. 'div' => array('style' => 'display:none;'),
  884. array('input' => array(
  885. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  886. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  887. )),
  888. array('input' => array(
  889. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  890. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  891. )),
  892. '/div'
  893. );
  894. $this->assertTags($result, $expected);
  895. }
  896. /**
  897. * Test form security with Model.field.0 style inputs
  898. *
  899. * @return void
  900. */
  901. public function testFormSecurityArrayFields() {
  902. $key = 'testKey';
  903. $this->Form->request->params['_Token']['key'] = $key;
  904. $this->Form->create('Address');
  905. $this->Form->input('Address.primary.1');
  906. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  907. $this->Form->input('Address.secondary.1.0');
  908. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  909. }
  910. /**
  911. * testFormSecurityMultipleInputDisabledFields method
  912. *
  913. * test secure form generation with multiple records and disabled fields.
  914. *
  915. * @return void
  916. */
  917. public function testFormSecurityMultipleInputDisabledFields() {
  918. $key = 'testKey';
  919. $this->Form->request->params['_Token'] = array(
  920. 'key' => $key,
  921. 'unlockedFields' => array('first_name', 'address')
  922. );
  923. $this->Form->create();
  924. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  925. $this->Form->input('Addresses.0.title');
  926. $this->Form->input('Addresses.0.first_name');
  927. $this->Form->input('Addresses.0.last_name');
  928. $this->Form->input('Addresses.0.address');
  929. $this->Form->input('Addresses.0.city');
  930. $this->Form->input('Addresses.0.phone');
  931. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  932. $this->Form->input('Addresses.1.title');
  933. $this->Form->input('Addresses.1.first_name');
  934. $this->Form->input('Addresses.1.last_name');
  935. $this->Form->input('Addresses.1.address');
  936. $this->Form->input('Addresses.1.city');
  937. $this->Form->input('Addresses.1.phone');
  938. $result = $this->Form->secure($this->Form->fields);
  939. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  940. $expected = array(
  941. 'div' => array('style' => 'display:none;'),
  942. array('input' => array(
  943. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  944. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  945. )),
  946. array('input' => array(
  947. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  948. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  949. )),
  950. '/div'
  951. );
  952. $this->assertTags($result, $expected);
  953. }
  954. /**
  955. * testFormSecurityInputDisabledFields method
  956. *
  957. * Test single record form with disabled fields.
  958. *
  959. * @return void
  960. */
  961. public function testFormSecurityInputUnlockedFields() {
  962. $key = 'testKey';
  963. $this->Form->request['_Token'] = array(
  964. 'key' => $key,
  965. 'unlockedFields' => array('first_name', 'address')
  966. );
  967. $this->Form->create();
  968. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  969. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  970. $this->Form->input('Addresses.title');
  971. $this->Form->input('Addresses.first_name');
  972. $this->Form->input('Addresses.last_name');
  973. $this->Form->input('Addresses.address');
  974. $this->Form->input('Addresses.city');
  975. $this->Form->input('Addresses.phone');
  976. $result = $this->Form->fields;
  977. $expected = array(
  978. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  979. 'Addresses.city', 'Addresses.phone'
  980. );
  981. $this->assertEquals($expected, $result);
  982. $result = $this->Form->secure($expected);
  983. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  984. $expected = array(
  985. 'div' => array('style' => 'display:none;'),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  988. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  989. )),
  990. array('input' => array(
  991. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  992. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  993. )),
  994. '/div'
  995. );
  996. $this->assertTags($result, $expected);
  997. }
  998. /**
  999. * test securing inputs with custom name attributes.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testFormSecureWithCustomNameAttribute() {
  1004. $this->Form->request->params['_Token']['key'] = 'testKey';
  1005. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1006. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1008. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1009. }
  1010. /**
  1011. * testFormSecuredInput method
  1012. *
  1013. * Test generation of entire secure form, assertions made on input() output.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecuredInput() {
  1018. $this->Form->request['_Token'] = array('key' => 'testKey');
  1019. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1020. $encoding = strtolower(Configure::read('App.encoding'));
  1021. $expected = array(
  1022. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1023. 'div' => array('style' => 'display:none;'),
  1024. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1025. array('input' => array(
  1026. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1027. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1028. )),
  1029. '/div'
  1030. );
  1031. $this->assertTags($result, $expected);
  1032. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1033. $expected = array(
  1034. 'div' => array('class' => 'input text'),
  1035. 'label' => array('for' => 'UserFormPublished'),
  1036. 'Published',
  1037. '/label',
  1038. array('input' => array(
  1039. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1040. 'id' => 'UserFormPublished'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormOther'),
  1049. 'Other',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1053. 'id' => 'UserFormOther'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->hidden('UserForm.stuff');
  1059. $expected = array(
  1060. 'input' => array(
  1061. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1062. 'id' => 'UserFormStuff'
  1063. ));
  1064. $this->assertTags($result, $expected);
  1065. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1066. $expected = array('input' => array(
  1067. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1068. 'value' => '0', 'id' => 'UserFormHidden'
  1069. ));
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1072. $expected = array(
  1073. 'div' => array('class' => 'input checkbox'),
  1074. array('input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1076. 'value' => '0', 'id' => 'UserFormSomething_'
  1077. )),
  1078. array('input' => array(
  1079. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1080. 'value' => '1', 'id' => 'UserFormSomething'
  1081. )),
  1082. 'label' => array('for' => 'UserFormSomething'),
  1083. 'Something',
  1084. '/label',
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->fields;
  1089. $expected = array(
  1090. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1091. 'UserForm.hidden' => '0', 'UserForm.something'
  1092. );
  1093. $this->assertEquals($expected, $result);
  1094. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1095. $result = $this->Form->secure($this->Form->fields);
  1096. $expected = array(
  1097. 'div' => array('style' => 'display:none;'),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1100. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1101. )),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1104. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1105. )),
  1106. '/div'
  1107. );
  1108. $this->assertTags($result, $expected);
  1109. }
  1110. /**
  1111. * Test secured inputs with custom names.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testSecuredInputCustomName() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $this->assertEquals(array(), $this->Form->fields);
  1118. $this->Form->input('text_input', array(
  1119. 'name' => 'data[Option][General.default_role]',
  1120. ));
  1121. $expected = array('Option.General.default_role');
  1122. $this->assertEquals($expected, $this->Form->fields);
  1123. $this->Form->input('select_box', array(
  1124. 'name' => 'data[Option][General.select_role]',
  1125. 'type' => 'select',
  1126. 'options' => array(1, 2),
  1127. ));
  1128. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * Tests that the correct keys are added to the field hash index
  1133. *
  1134. * @return void
  1135. */
  1136. public function testFormSecuredFileInput() {
  1137. $this->Form->request['_Token'] = array('key' => 'testKey');
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $this->Form->file('Attachment.file');
  1140. $expected = array(
  1141. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1142. 'Attachment.file.error', 'Attachment.file.size'
  1143. );
  1144. $this->assertEquals($expected, $this->Form->fields);
  1145. }
  1146. /**
  1147. * test that multiple selects keys are added to field hash
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecuredMultipleSelect() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEquals(array(), $this->Form->fields);
  1154. $options = array('1' => 'one', '2' => 'two');
  1155. $this->Form->select('Model.select', $options);
  1156. $expected = array('Model.select');
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. $this->Form->fields = array();
  1159. $this->Form->select('Model.select', $options, array('multiple' => true));
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * testFormSecuredRadio method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredRadio() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $options = array('1' => 'option1', '2' => 'option2');
  1171. $this->Form->radio('Test.test', $options);
  1172. $expected = array('Test.test');
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * Test that when disabled is in a list based attribute array it works.
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredAndDisabledNotAssoc() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1183. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1184. $this->Form->text('Model.text', array('disabled'));
  1185. $this->Form->textarea('Model.textarea', array('disabled'));
  1186. $this->Form->password('Model.password', array('disabled'));
  1187. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1188. $expected = array(
  1189. 'Model.radio' => ''
  1190. );
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1195. * hashing.
  1196. *
  1197. * @return void
  1198. */
  1199. public function testFormSecuredAndDisabled() {
  1200. $this->Form->request['_Token'] = array('key' => 'testKey');
  1201. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1202. $this->Form->text('Model.text', array('disabled' => true));
  1203. $this->Form->password('Model.text', array('disabled' => true));
  1204. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1205. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1206. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1207. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1208. $this->Form->month('Model.month', array('disabled' => true));
  1209. $this->Form->day('Model.day', array('disabled' => true));
  1210. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1211. $this->Form->minute('Model.minute', array('disabled' => true));
  1212. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1213. $expected = array(
  1214. 'Model.radio' => ''
  1215. );
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. }
  1218. /**
  1219. * testDisableSecurityUsingForm method
  1220. *
  1221. * @return void
  1222. */
  1223. public function testDisableSecurityUsingForm() {
  1224. $this->Form->request['_Token'] = array(
  1225. 'key' => 'testKey',
  1226. 'disabledFields' => array()
  1227. );
  1228. $this->Form->create();
  1229. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1230. $this->Form->input('Addresses.title');
  1231. $this->Form->input('Addresses.first_name', array('secure' => false));
  1232. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1233. $this->Form->input('Addresses.zip', array(
  1234. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1235. ));
  1236. $result = $this->Form->fields;
  1237. $expected = array(
  1238. 'Addresses.id' => '123456', 'Addresses.title',
  1239. );
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * test disableField
  1244. *
  1245. * @return void
  1246. */
  1247. public function testUnlockFieldAddsToList() {
  1248. $this->Form->request['_Token'] = array(
  1249. 'key' => 'testKey',
  1250. 'unlockedFields' => array()
  1251. );
  1252. $this->Form->create('Contact');
  1253. $this->Form->unlockField('Contact.name');
  1254. $this->Form->text('Contact.name');
  1255. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1256. $this->assertEquals(array(), $this->Form->fields);
  1257. }
  1258. /**
  1259. * test unlockField removing from fields array.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testUnlockFieldRemovingFromFields() {
  1264. $this->Form->request['_Token'] = array(
  1265. 'key' => 'testKey',
  1266. 'unlockedFields' => array()
  1267. );
  1268. $this->Form->create('Contact');
  1269. $this->Form->hidden('Contact.id', array('value' => 1));
  1270. $this->Form->text('Contact.name');
  1271. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1272. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1273. $this->Form->unlockField('Contact.name');
  1274. $this->Form->unlockField('Contact.id');
  1275. $this->assertEquals(array(), $this->Form->fields);
  1276. }
  1277. /**
  1278. * testTagIsInvalid method
  1279. *
  1280. * @return void
  1281. */
  1282. public function testTagIsInvalid() {
  1283. $Contact = ClassRegistry::getObject('Contact');
  1284. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1285. $this->Form->setEntity('Contact.0.email');
  1286. $result = $this->Form->tagIsInvalid();
  1287. $this->assertEquals($expected, $result);
  1288. $this->Form->setEntity('Contact.1.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertFalse($result);
  1291. $this->Form->setEntity('Contact.0.name');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. }
  1295. /**
  1296. * Test tagIsInvalid with validation errors from a saveMany
  1297. *
  1298. * @return void
  1299. */
  1300. public function testTagIsInvalidSaveMany() {
  1301. $Contact = ClassRegistry::getObject('Contact');
  1302. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1303. $this->Form->create('Contact');
  1304. $this->Form->setEntity('0.email');
  1305. $result = $this->Form->tagIsInvalid();
  1306. $this->assertEquals($expected, $result);
  1307. $this->Form->setEntity('0.Contact.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. }
  1311. /**
  1312. * Test validation errors.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testPasswordValidation() {
  1317. $Contact = ClassRegistry::getObject('Contact');
  1318. $Contact->validationErrors['password'] = array('Please provide a password');
  1319. $result = $this->Form->input('Contact.password');
  1320. $expected = array(
  1321. 'div' => array('class' => 'input password error'),
  1322. 'label' => array('for' => 'ContactPassword'),
  1323. 'Password',
  1324. '/label',
  1325. 'input' => array(
  1326. 'type' => 'password', 'name' => 'data[Contact][password]',
  1327. 'id' => 'ContactPassword', 'class' => 'form-error'
  1328. ),
  1329. array('div' => array('class' => 'error-message')),
  1330. 'Please provide a password',
  1331. '/div',
  1332. '/div'
  1333. );
  1334. $this->assertTags($result, $expected);
  1335. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. '/div'
  1346. );
  1347. $this->assertTags($result, $expected);
  1348. }
  1349. /**
  1350. * Test validation errors, when validation message is an empty string.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testEmptyErrorValidation() {
  1355. $this->Form->validationErrors['Contact']['password'] = '';
  1356. $result = $this->Form->input('Contact.password');
  1357. $expected = array(
  1358. 'div' => array('class' => 'input password error'),
  1359. 'label' => array('for' => 'ContactPassword'),
  1360. 'Password',
  1361. '/label',
  1362. 'input' => array(
  1363. 'type' => 'password', 'name' => 'data[Contact][password]',
  1364. 'id' => 'ContactPassword', 'class' => 'form-error'
  1365. ),
  1366. array('div' => array('class' => 'error-message')),
  1367. array(),
  1368. '/div',
  1369. '/div'
  1370. );
  1371. $this->assertTags($result, $expected);
  1372. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1373. $expected = array(
  1374. 'div' => array('class' => 'input password error'),
  1375. 'label' => array('for' => 'ContactPassword'),
  1376. 'Password',
  1377. '/label',
  1378. 'input' => array(
  1379. 'type' => 'password', 'name' => 'data[Contact][password]',
  1380. 'id' => 'ContactPassword', 'class' => 'form-error'
  1381. ),
  1382. '/div'
  1383. );
  1384. $this->assertTags($result, $expected);
  1385. }
  1386. /**
  1387. * Test validation errors, when calling input() overriding validation message by an empty string.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testEmptyInputErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1393. $result = $this->Form->input('Contact.password', array('error' => ''));
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. '/div'
  1420. );
  1421. $this->assertTags($result, $expected);
  1422. }
  1423. /**
  1424. * testFormValidationAssociated method
  1425. *
  1426. * test display of form errors in conjunction with model::validates.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testFormValidationAssociated() {
  1431. $this->UserForm = ClassRegistry::getObject('UserForm');
  1432. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1433. $data = array(
  1434. 'UserForm' => array('name' => 'user'),
  1435. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1436. );
  1437. $result = $this->UserForm->OpenidUrl->create($data);
  1438. $this->assertFalse(empty($result));
  1439. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1440. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1441. $encoding = strtolower(Configure::read('App.encoding'));
  1442. $expected = array(
  1443. 'form' => array(
  1444. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1445. 'accept-charset' => $encoding
  1446. ),
  1447. 'div' => array('style' => 'display:none;'),
  1448. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1449. '/div'
  1450. );
  1451. $this->assertTags($result, $expected);
  1452. $result = $this->Form->error(
  1453. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1454. );
  1455. $this->assertEquals('Error, not registered', $result);
  1456. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1457. }
  1458. /**
  1459. * testFormValidationAssociatedFirstLevel method
  1460. *
  1461. * test form error display with associated model.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormValidationAssociatedFirstLevel() {
  1466. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1467. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1468. $data = array(
  1469. 'ValidateUser' => array('name' => 'mariano'),
  1470. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1471. );
  1472. $result = $this->ValidateUser->create($data);
  1473. $this->assertFalse(empty($result));
  1474. $this->assertFalse($this->ValidateUser->validates());
  1475. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1476. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1477. $encoding = strtolower(Configure::read('App.encoding'));
  1478. $expected = array(
  1479. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1480. 'div' => array('style' => 'display:none;'),
  1481. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1482. '/div'
  1483. );
  1484. $this->assertTags($result, $expected);
  1485. $result = $this->Form->error(
  1486. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1487. );
  1488. $this->assertEquals('Invalid email', $result);
  1489. $result = $this->Form->error(
  1490. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid name', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid city', $result);
  1497. unset($this->ValidateUser->ValidateProfile);
  1498. unset($this->ValidateUser);
  1499. }
  1500. /**
  1501. * testFormValidationAssociatedSecondLevel method
  1502. *
  1503. * test form error display with associated model.
  1504. *
  1505. * @return void
  1506. */
  1507. public function testFormValidationAssociatedSecondLevel() {
  1508. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1509. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1510. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1511. $data = array(
  1512. 'ValidateUser' => array('name' => 'mariano'),
  1513. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1514. 'ValidateItem' => array('name' => 'Item')
  1515. );
  1516. $result = $this->ValidateUser->create($data);
  1517. $this->assertFalse(empty($result));
  1518. $this->assertFalse($this->ValidateUser->validates());
  1519. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1520. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1521. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1522. $encoding = strtolower(Configure::read('App.encoding'));
  1523. $expected = array(
  1524. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1525. 'div' => array('style' => 'display:none;'),
  1526. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1527. '/div'
  1528. );
  1529. $this->assertTags($result, $expected);
  1530. $result = $this->Form->error(
  1531. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid email', $result);
  1534. $result = $this->Form->error(
  1535. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid name', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1540. );
  1541. $result = $this->Form->error(
  1542. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1543. );
  1544. $this->assertEquals('Invalid description', $result);
  1545. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1546. unset($this->ValidateUser->ValidateProfile);
  1547. unset($this->ValidateUser);
  1548. }
  1549. /**
  1550. * testFormValidationMultiRecord method
  1551. *
  1552. * test form error display with multiple records.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormValidationMultiRecord() {
  1557. $Contact = ClassRegistry::getObject('Contact');
  1558. $Contact->validationErrors[2] = array(
  1559. 'name' => array('This field cannot be left blank')
  1560. );
  1561. $result = $this->Form->input('Contact.2.name');
  1562. $expected = array(
  1563. 'div' => array('class' => 'input text error'),
  1564. 'label' => array('for' => 'Contact2Name'),
  1565. 'Name',
  1566. '/label',
  1567. 'input' => array(
  1568. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1569. 'class' => 'form-error', 'maxlength' => 255
  1570. ),
  1571. array('div' => array('class' => 'error-message')),
  1572. 'This field cannot be left blank',
  1573. '/div',
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. }
  1578. /**
  1579. * testMultipleInputValidation method
  1580. *
  1581. * test multiple record form validation error display.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testMultipleInputValidation() {
  1586. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1587. $Address->validationErrors[0] = array(
  1588. 'title' => array('This field cannot be empty'),
  1589. 'first_name' => array('This field cannot be empty')
  1590. );
  1591. $Address->validationErrors[1] = array(
  1592. 'last_name' => array('You must have a last name')
  1593. );
  1594. $this->Form->create();
  1595. $result = $this->Form->input('Address.0.title');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'This field cannot be empty',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->input('Address.0.first_name');
  1611. $expected = array(
  1612. 'div' => array('class'),
  1613. 'label' => array('for'),
  1614. 'preg:/[^<]+/',
  1615. '/label',
  1616. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1617. array('div' => array('class' => 'error-message')),
  1618. 'This field cannot be empty',
  1619. '/div',
  1620. '/div'
  1621. );
  1622. $this->assertTags($result, $expected);
  1623. $result = $this->Form->input('Address.0.last_name');
  1624. $expected = array(
  1625. 'div' => array('class'),
  1626. 'label' => array('for'),
  1627. 'preg:/[^<]+/',
  1628. '/label',
  1629. 'input' => array('type' => 'text', 'name', 'id'),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Address.1.last_name');
  1634. $expected = array(
  1635. 'div' => array('class'),
  1636. 'label' => array('for'),
  1637. 'preg:/[^<]+/',
  1638. '/label',
  1639. 'input' => array(
  1640. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1641. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1642. ),
  1643. array('div' => array('class' => 'error-message')),
  1644. 'You must have a last name',
  1645. '/div',
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. }
  1650. /**
  1651. * testInput method
  1652. *
  1653. * Test various incarnations of input().
  1654. *
  1655. * @return void
  1656. */
  1657. public function testInput() {
  1658. $result = $this->Form->input('ValidateUser.balance');
  1659. $expected = array(
  1660. 'div' => array('class'),
  1661. 'label' => array('for'),
  1662. 'Balance',
  1663. '/label',
  1664. 'input' => array('name', 'type' => 'number', 'id'),
  1665. '/div',
  1666. );
  1667. $this->assertTags($result, $expected);
  1668. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1669. $expected = array(
  1670. 'div' => array('class' => 'input email'),
  1671. 'label' => array('for' => 'custom'),
  1672. 'Email',
  1673. '/label',
  1674. array('input' => array(
  1675. 'type' => 'email', 'name' => 'data[Contact][email]',
  1676. 'id' => 'custom', 'maxlength' => 255
  1677. )),
  1678. '/div'
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1682. $expected = array(
  1683. '<div',
  1684. 'label' => array('for' => 'ContactEmail'),
  1685. 'Email',
  1686. '/label',
  1687. array('input' => array(
  1688. 'type' => 'email', 'name' => 'data[Contact][email]',
  1689. 'id' => 'ContactEmail', 'maxlength' => 255
  1690. )),
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->hidden('Contact.idontexist');
  1695. $expected = array('input' => array(
  1696. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1697. 'id' => 'ContactIdontexist'
  1698. ));
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1701. $expected = array(
  1702. 'div' => array('class' => 'input text'),
  1703. 'label' => array('for' => 'ContactEmail'),
  1704. 'Email',
  1705. '/label',
  1706. array('input' => array(
  1707. 'type' => 'text', 'name' => 'data[Contact][email]',
  1708. 'id' => 'ContactEmail'
  1709. )),
  1710. '/div'
  1711. );
  1712. $this->assertTags($result, $expected);
  1713. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input text'),
  1716. 'label' => array('for' => 'Contact5Email'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1721. 'id' => 'Contact5Email'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.password');
  1727. $expected = array(
  1728. 'div' => array('class' => 'input password'),
  1729. 'label' => array('for' => 'ContactPassword'),
  1730. 'Password',
  1731. '/label',
  1732. array('input' => array(
  1733. 'type' => 'password', 'name' => 'data[Contact][password]',
  1734. 'id' => 'ContactPassword'
  1735. )),
  1736. '/div'
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array(
  1740. 'type' => 'file', 'class' => 'textbox'
  1741. ));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input file'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1755. $result = $this->Form->input('Contact.phone');
  1756. $expected = array(
  1757. 'div' => array('class' => 'input tel'),
  1758. 'label' => array('for' => 'ContactPhone'),
  1759. 'Phone',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1763. 'value' => 'Hello &amp; World &gt; weird chars',
  1764. 'id' => 'ContactPhone', 'maxlength' => 255
  1765. )),
  1766. '/div'
  1767. );
  1768. $this->assertTags($result, $expected);
  1769. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1770. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1771. $expected = array(
  1772. 'div' => array('class' => 'input text'),
  1773. 'label' => array('for' => 'myId'),
  1774. 'Field',
  1775. '/label',
  1776. 'input' => array(
  1777. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1778. 'value' => 'My value', 'id' => 'myId'
  1779. ),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. unset($this->Form->request->data);
  1784. $Contact = ClassRegistry::getObject('Contact');
  1785. $Contact->validationErrors['field'] = array('Badness!');
  1786. $result = $this->Form->input('Contact.field');
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text error'),
  1789. 'label' => array('for' => 'ContactField'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Contact][field]',
  1794. 'id' => 'ContactField', 'class' => 'form-error'
  1795. ),
  1796. array('div' => array('class' => 'error-message')),
  1797. 'Badness!',
  1798. '/div',
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $result = $this->Form->input('Contact.field', array(
  1803. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1804. ));
  1805. $expected = array(
  1806. 'label' => array('for' => 'ContactField'),
  1807. 'Field',
  1808. '/label',
  1809. 'input' => array(
  1810. 'type' => 'text', 'name' => 'data[Contact][field]',
  1811. 'id' => 'ContactField', 'class' => 'form-error'
  1812. ),
  1813. array('span' => array('class' => 'error-message')),
  1814. 'Badness!',
  1815. '/span'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1820. ));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input text error'),
  1823. 'label' => array('for' => 'ContactField'),
  1824. 'Field',
  1825. '/label',
  1826. 'input' => array(
  1827. 'type' => 'text', 'name' => 'data[Contact][field]',
  1828. 'id' => 'ContactField', 'class' => 'form-error'
  1829. ),
  1830. array('div' => array('class' => 'error')),
  1831. 'Badness!',
  1832. '/div'
  1833. );
  1834. $this->assertTags($result, $expected);
  1835. $result = $this->Form->input('Contact.field', array(
  1836. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1837. ));
  1838. $expected = array(
  1839. 'span' => array('class' => 'input text error'),
  1840. 'label' => array('for' => 'ContactField'),
  1841. 'Field',
  1842. '/label',
  1843. 'input' => array(
  1844. 'type' => 'text', 'name' => 'data[Contact][field]',
  1845. 'id' => 'ContactField', 'class' => 'form-error'
  1846. ),
  1847. 'Badness!',
  1848. '/span'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1852. $expected = array(
  1853. 'div' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'A message to you, Rudy',
  1862. array('div' => array('class' => 'error-message')),
  1863. 'Badness!',
  1864. '/div',
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $this->Form->setEntity(null);
  1869. $this->Form->setEntity('Contact.field');
  1870. $result = $this->Form->input('Contact.field', array(
  1871. 'after' => 'A message to you, Rudy', 'error' => false
  1872. ));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ContactField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input text'),
  1886. 'label' => array('for' => 'ObjectField'),
  1887. 'Field',
  1888. '/label',
  1889. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1890. 'A message to you, Rudy',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. $Contact->validationErrors['field'] = array('minLength');
  1895. $result = $this->Form->input('Contact.field', array(
  1896. 'error' => array(
  1897. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1898. 'maxLength' => 'login too large'
  1899. )
  1900. ));
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1907. array('div' => array('class' => 'error-message')),
  1908. 'Le login doit contenir au moins 2 caractères',
  1909. '/div',
  1910. '/div'
  1911. );
  1912. $this->assertTags($result, $expected);
  1913. $Contact->validationErrors['field'] = array('maxLength');
  1914. $result = $this->Form->input('Contact.field', array(
  1915. 'error' => array(
  1916. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1917. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1918. 'maxLength' => 'login too large',
  1919. )
  1920. ));
  1921. $expected = array(
  1922. 'div' => array('class' => 'input text error'),
  1923. 'label' => array('for' => 'ContactField'),
  1924. 'Field',
  1925. '/label',
  1926. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1927. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1928. 'login too large',
  1929. '/span',
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. }
  1934. /**
  1935. * Test that inputs with 0 can be created.
  1936. *
  1937. * @return void
  1938. */
  1939. public function testInputZero() {
  1940. $this->Form->create('User');
  1941. $result = $this->Form->input('0');
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text'),
  1944. 'label' => array('for' => 'User0'), '/label',
  1945. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * test input() with checkbox creation
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputCheckbox() {
  1956. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1957. $expected = array(
  1958. 'div' => array('class' => 'input checkbox'),
  1959. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1960. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1961. '/div'
  1962. );
  1963. $this->assertTags($result, $expected);
  1964. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1965. $expected = array(
  1966. 'div' => array('class' => 'input checkbox'),
  1967. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1968. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1969. '/div'
  1970. );
  1971. $this->assertTags($result, $expected);
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.disabled', array(
  1981. 'label' => 'Disabled',
  1982. 'type' => 'checkbox',
  1983. 'data-foo' => 'disabled'
  1984. ));
  1985. $expected = array(
  1986. 'div' => array('class' => 'input checkbox'),
  1987. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1988. array('input' => array(
  1989. 'type' => 'checkbox',
  1990. 'name' => 'data[User][disabled]',
  1991. 'value' => '1',
  1992. 'id' => 'UserDisabled',
  1993. 'data-foo' => 'disabled'
  1994. )),
  1995. 'label' => array('for' => 'UserDisabled'),
  1996. 'Disabled',
  1997. '/label',
  1998. '/div'
  1999. );
  2000. $this->assertTags($result, $expected);
  2001. }
  2002. /**
  2003. * test form->input() with time types.
  2004. *
  2005. */
  2006. public function testInputTime() {
  2007. extract($this->dateRegex);
  2008. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2009. $result = explode(':', $result);
  2010. $this->assertRegExp('/option value="23"/', $result[0]);
  2011. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2012. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2013. $result = explode(':', $result);
  2014. $this->assertRegExp('/option value="23"/', $result[0]);
  2015. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2016. $result = $this->Form->input('Model.field', array(
  2017. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2018. ));
  2019. $result = explode(':', $result);
  2020. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2021. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2022. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2023. $result = $this->Form->input('Model.field', array(
  2024. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2025. ));
  2026. $result = explode(':', $result);
  2027. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2028. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2029. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2030. $result = $this->Form->input('prueba', array(
  2031. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2032. 'maxYear' => date('Y') + 1, 'interval' => 15
  2033. ));
  2034. $result = explode(':', $result);
  2035. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2036. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2037. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2039. $result = $this->Form->input('Random.start_time', array(
  2040. 'type' => 'time',
  2041. 'selected' => '18:15'
  2042. ));
  2043. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2044. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2045. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2046. $result = $this->Form->input('published', array('type' => 'time'));
  2047. $now = strtotime('now');
  2048. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2049. $now = strtotime('2013-03-09 00:42:21');
  2050. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2051. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2052. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2053. }
  2054. /**
  2055. * Test interval + selected near the hour roll over.
  2056. *
  2057. * @return void
  2058. */
  2059. public function testTimeSelectedWithInterval() {
  2060. $result = $this->Form->input('Model.start_time', array(
  2061. 'type' => 'time',
  2062. 'interval' => 15,
  2063. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2064. ));
  2065. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2066. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2067. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2068. $result = $this->Form->input('Model.start_time', array(
  2069. 'type' => 'time',
  2070. 'interval' => 15,
  2071. 'selected' => '2012-10-23 15:57:00'
  2072. ));
  2073. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2074. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2075. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2076. $result = $this->Form->input('Model.start_time', array(
  2077. 'timeFormat' => 24,
  2078. 'type' => 'time',
  2079. 'interval' => 15,
  2080. 'selected' => '15:57'
  2081. ));
  2082. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2083. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2084. $result = $this->Form->input('Model.start_time', array(
  2085. 'timeFormat' => 24,
  2086. 'type' => 'time',
  2087. 'interval' => 15,
  2088. 'selected' => '23:57'
  2089. ));
  2090. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2091. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2092. $result = $this->Form->input('Model.created', array(
  2093. 'timeFormat' => 24,
  2094. 'type' => 'datetime',
  2095. 'interval' => 15,
  2096. 'selected' => '2012-09-30 23:56'
  2097. ));
  2098. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2099. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2100. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2101. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2103. }
  2104. /**
  2105. * Test time with selected values around 12:xx:xx
  2106. *
  2107. * @return void
  2108. */
  2109. public function testTimeSelectedWithIntervalTwelve() {
  2110. $result = $this->Form->input('Model.start_time', array(
  2111. 'type' => 'time',
  2112. 'timeFormat' => 12,
  2113. 'interval' => 15,
  2114. 'selected' => '00:00:00'
  2115. ));
  2116. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2117. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2118. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2119. $result = $this->Form->input('Model.start_time', array(
  2120. 'type' => 'time',
  2121. 'timeFormat' => 12,
  2122. 'interval' => 15,
  2123. 'selected' => '12:00:00'
  2124. ));
  2125. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2126. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2127. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2128. $result = $this->Form->input('Model.start_time', array(
  2129. 'type' => 'time',
  2130. 'timeFormat' => 12,
  2131. 'interval' => 15,
  2132. 'selected' => '12:15:00'
  2133. ));
  2134. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2135. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2136. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2137. }
  2138. /**
  2139. * Test interval & timeFormat = 12
  2140. *
  2141. * @return void
  2142. */
  2143. public function testInputTimeWithIntervalAnd12HourFormat() {
  2144. $result = $this->Form->input('Model.start_time', array(
  2145. 'type' => 'time',
  2146. 'timeFormat' => 12,
  2147. 'interval' => 5,
  2148. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2149. ));
  2150. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2151. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2152. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2153. $result = $this->Form->input('Model.start_time', array(
  2154. 'type' => 'time',
  2155. 'timeFormat' => '12',
  2156. 'interval' => 5,
  2157. 'selected' => '2013-04-19 16:30:00'
  2158. ));
  2159. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2160. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2161. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2162. $result = $this->Form->input('Model.start_time', array(
  2163. 'type' => 'time',
  2164. 'timeFormat' => '12',
  2165. 'interval' => 10,
  2166. 'selected' => '2013-05-19 00:33:00'
  2167. ));
  2168. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2169. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2170. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2171. $result = $this->Form->input('Model.start_time', array(
  2172. 'type' => 'time',
  2173. 'timeFormat' => '12',
  2174. 'interval' => 10,
  2175. 'selected' => '2013-05-19 13:33:00'
  2176. ));
  2177. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2178. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2179. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2180. $result = $this->Form->input('Model.start_time', array(
  2181. 'type' => 'time',
  2182. 'timeFormat' => '12',
  2183. 'interval' => 10,
  2184. 'selected' => '2013-05-19 01:33:00'
  2185. ));
  2186. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2187. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2188. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2189. }
  2190. /**
  2191. * test form->input() with datetime, date and time types
  2192. *
  2193. * @return void
  2194. */
  2195. public function testInputDatetime() {
  2196. extract($this->dateRegex);
  2197. $result = $this->Form->input('prueba', array(
  2198. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2199. 'maxYear' => date('Y') + 1, 'interval' => 15
  2200. ));
  2201. $result = explode(':', $result);
  2202. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2203. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2204. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2205. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2206. //related to ticket #5013
  2207. $result = $this->Form->input('Contact.date', array(
  2208. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2209. ));
  2210. $this->assertRegExp('/class="customClass"/', $result);
  2211. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2212. $result = $this->Form->input('Contact.date', array(
  2213. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2214. ));
  2215. $this->assertRegExp('/id="customIdDay"/', $result);
  2216. $this->assertRegExp('/id="customIdMonth"/', $result);
  2217. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2218. $result = $this->Form->input('Model.field', array(
  2219. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2220. ));
  2221. $this->assertRegExp('/id="customIDDay"/', $result);
  2222. $this->assertRegExp('/id="customIDHour"/', $result);
  2223. $result = explode('</select><select', $result);
  2224. $result = explode(':', $result[1]);
  2225. $this->assertRegExp('/option value="23"/', $result[0]);
  2226. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2227. $result = $this->Form->input('Model.field', array(
  2228. 'type' => 'datetime', 'timeFormat' => 12
  2229. ));
  2230. $result = explode('</select><select', $result);
  2231. $result = explode(':', $result[1]);
  2232. $this->assertRegExp('/option value="12"/', $result[0]);
  2233. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2234. $this->Form->request->data = array('Contact' => array('created' => null));
  2235. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2236. $expected = array(
  2237. 'div' => array('class' => 'input date'),
  2238. 'label' => array('for' => 'ContactCreatedMonth'),
  2239. 'Created',
  2240. '/label',
  2241. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2242. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2243. $monthsRegex,
  2244. '/select', '-',
  2245. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2246. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2247. $daysRegex,
  2248. '/select', '-',
  2249. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2250. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2251. $yearsRegex,
  2252. '/select',
  2253. '/div'
  2254. );
  2255. $this->assertTags($result, $expected);
  2256. $this->Form->request->data = array('Contact' => array('created' => null));
  2257. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2258. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2259. $this->Form->request->data = array('Contact' => array('created' => null));
  2260. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2261. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2262. $result = $this->Form->input('Contact.created', array(
  2263. 'type' => 'date',
  2264. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2265. 'timeFormat' => 'NONE'
  2266. ));
  2267. $this->assertRegExp('/for\="created-month"/', $result);
  2268. }
  2269. /**
  2270. * Test generating checkboxes in a loop.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testInputCheckboxesInLoop() {
  2275. for ($i = 1; $i < 5; $i++) {
  2276. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2277. $expected = array(
  2278. 'div' => array('class' => 'input checkbox'),
  2279. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2280. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2281. 'label' => array('for' => "Contact{$i}Email"),
  2282. 'Email',
  2283. '/label',
  2284. '/div'
  2285. );
  2286. $this->assertTags($result, $expected);
  2287. }
  2288. }
  2289. /**
  2290. * Test generating checkboxes with disabled elements.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputCheckboxWithDisabledElements() {
  2295. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2296. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2297. $expected = array(
  2298. array('div' => array('class' => 'input select')),
  2299. array('label' => array('for' => "ContactMultiple")),
  2300. 'Multiple',
  2301. '/label',
  2302. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2303. array('div' => array('class' => 'checkbox')),
  2304. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2305. array('label' => array('for' => "ContactMultiple1")),
  2306. 'One',
  2307. '/label',
  2308. '/div',
  2309. array('div' => array('class' => 'checkbox')),
  2310. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2311. array('label' => array('for' => "ContactMultiple2")),
  2312. 'Two',
  2313. '/label',
  2314. '/div',
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2317. array('label' => array('for' => "ContactMultiple3")),
  2318. 'Three',
  2319. '/label',
  2320. '/div',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2325. $this->assertTags($result, $expected);
  2326. $disabled = array('2', 3);
  2327. $expected = array(
  2328. array('div' => array('class' => 'input select')),
  2329. array('label' => array('for' => "ContactMultiple")),
  2330. 'Multiple',
  2331. '/label',
  2332. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2333. array('div' => array('class' => 'checkbox')),
  2334. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2335. array('label' => array('for' => "ContactMultiple1")),
  2336. 'One',
  2337. '/label',
  2338. '/div',
  2339. array('div' => array('class' => 'checkbox')),
  2340. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2341. array('label' => array('for' => "ContactMultiple2")),
  2342. 'Two',
  2343. '/label',
  2344. '/div',
  2345. array('div' => array('class' => 'checkbox')),
  2346. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2347. array('label' => array('for' => "ContactMultiple3")),
  2348. 'Three',
  2349. '/label',
  2350. '/div',
  2351. '/div'
  2352. );
  2353. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2354. $this->assertTags($result, $expected);
  2355. // make sure 50 does only disable 50, and not 50f5c0cf
  2356. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2357. $disabled = array(50);
  2358. $expected = array(
  2359. array('div' => array('class' => 'input select')),
  2360. array('label' => array('for' => "ContactMultiple")),
  2361. 'Multiple',
  2362. '/label',
  2363. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2364. array('div' => array('class' => 'checkbox')),
  2365. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2366. array('label' => array('for' => "ContactMultiple50")),
  2367. 'Fifty',
  2368. '/label',
  2369. '/div',
  2370. array('div' => array('class' => 'checkbox')),
  2371. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2372. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2373. 'Stringy',
  2374. '/label',
  2375. '/div',
  2376. '/div'
  2377. );
  2378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2379. $this->assertTags($result, $expected);
  2380. }
  2381. /**
  2382. * test input name with leading integer, ensure attributes are generated correctly.
  2383. *
  2384. * @return void
  2385. */
  2386. public function testInputWithLeadingInteger() {
  2387. $result = $this->Form->text('0.Node.title');
  2388. $expected = array(
  2389. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2390. );
  2391. $this->assertTags($result, $expected);
  2392. }
  2393. /**
  2394. * test form->input() with select type inputs.
  2395. *
  2396. * @return void
  2397. */
  2398. public function testInputSelectType() {
  2399. $result = $this->Form->input('email', array(
  2400. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2401. );
  2402. $expected = array(
  2403. 'div' => array('class' => 'input select'),
  2404. 'label' => array('for' => 'email'),
  2405. 'Email',
  2406. '/label',
  2407. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2408. array('option' => array('value' => '')),
  2409. '/option',
  2410. array('option' => array('value' => 'è')),
  2411. 'Firést',
  2412. '/option',
  2413. array('option' => array('value' => 'é')),
  2414. 'Secoènd',
  2415. '/option',
  2416. '/select',
  2417. '/div'
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $result = $this->Form->input('email', array(
  2421. 'options' => array('First', 'Second'), 'empty' => true)
  2422. );
  2423. $expected = array(
  2424. 'div' => array('class' => 'input select'),
  2425. 'label' => array('for' => 'email'),
  2426. 'Email',
  2427. '/label',
  2428. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2429. array('option' => array('value' => '')),
  2430. '/option',
  2431. array('option' => array('value' => '0')),
  2432. 'First',
  2433. '/option',
  2434. array('option' => array('value' => '1')),
  2435. 'Second',
  2436. '/option',
  2437. '/select',
  2438. '/div'
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2442. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2443. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2444. $expected = array(
  2445. 'div' => array('class' => 'input select'),
  2446. 'label' => array('for' => 'ModelUserId'),
  2447. 'User',
  2448. '/label',
  2449. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2450. array('option' => array('value' => '')),
  2451. '/option',
  2452. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2453. 'good',
  2454. '/option',
  2455. array('option' => array('value' => 'other')),
  2456. 'bad',
  2457. '/option',
  2458. '/select',
  2459. '/div'
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2463. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2464. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2465. $expected = array(
  2466. 'div' => array('class' => 'input select'),
  2467. 'label' => array('for' => 'ThingUserId'),
  2468. 'User',
  2469. '/label',
  2470. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2471. array('option' => array('value' => '')),
  2472. 'Some Empty',
  2473. '/option',
  2474. array('option' => array('value' => 'value')),
  2475. 'good',
  2476. '/option',
  2477. array('option' => array('value' => 'other')),
  2478. 'bad',
  2479. '/option',
  2480. '/select',
  2481. '/div'
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2485. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2486. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2487. $expected = array(
  2488. 'div' => array('class' => 'input select'),
  2489. 'label' => array('for' => 'ThingUserId'),
  2490. 'User',
  2491. '/label',
  2492. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2493. array('option' => array('value' => '')),
  2494. 'Some Empty',
  2495. '/option',
  2496. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2497. 'good',
  2498. '/option',
  2499. array('option' => array('value' => 'other')),
  2500. 'bad',
  2501. '/option',
  2502. '/select',
  2503. '/div'
  2504. );
  2505. $this->assertTags($result, $expected);
  2506. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2507. $this->Form->request->data = array('User' => array('User' => array('value')));
  2508. $result = $this->Form->input('User.User', array('empty' => true));
  2509. $expected = array(
  2510. 'div' => array('class' => 'input select'),
  2511. 'label' => array('for' => 'UserUser'),
  2512. 'User',
  2513. '/label',
  2514. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2515. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2516. array('option' => array('value' => '')),
  2517. '/option',
  2518. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2519. 'good',
  2520. '/option',
  2521. array('option' => array('value' => 'other')),
  2522. 'bad',
  2523. '/option',
  2524. '/select',
  2525. '/div'
  2526. );
  2527. $this->assertTags($result, $expected);
  2528. $this->Form->data = array();
  2529. $result = $this->Form->input('Publisher.id', array(
  2530. 'label' => 'Publisher',
  2531. 'type' => 'select',
  2532. 'multiple' => 'checkbox',
  2533. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2534. ));
  2535. $expected = array(
  2536. array('div' => array('class' => 'input select')),
  2537. array('label' => array('for' => 'PublisherId')),
  2538. 'Publisher',
  2539. '/label',
  2540. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2541. array('div' => array('class' => 'checkbox')),
  2542. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2543. array('label' => array('for' => 'PublisherIdValue1')),
  2544. 'Label 1',
  2545. '/label',
  2546. '/div',
  2547. array('div' => array('class' => 'checkbox')),
  2548. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2549. array('label' => array('for' => 'PublisherIdValue2')),
  2550. 'Label 2',
  2551. '/label',
  2552. '/div',
  2553. '/div'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * test that input() and a non standard primary key makes a hidden input by default.
  2559. *
  2560. * @return void
  2561. */
  2562. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2563. $this->Form->create('User');
  2564. $this->Form->fieldset = array(
  2565. 'User' => array(
  2566. 'fields' => array(
  2567. 'model_id' => array('type' => 'integer')
  2568. ),
  2569. 'validates' => array(),
  2570. 'key' => 'model_id'
  2571. )
  2572. );
  2573. $result = $this->Form->input('model_id');
  2574. $expected = array(
  2575. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. }
  2579. /**
  2580. * test that overriding the magic select type widget is possible
  2581. *
  2582. * @return void
  2583. */
  2584. public function testInputOverridingMagicSelectType() {
  2585. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2586. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2587. $expected = array(
  2588. 'div' => array('class' => 'input text'),
  2589. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2590. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2591. '/div'
  2592. );
  2593. $this->assertTags($result, $expected);
  2594. //Check that magic types still work for plural/singular vars
  2595. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2596. $result = $this->Form->input('Model.type');
  2597. $expected = array(
  2598. 'div' => array('class' => 'input select'),
  2599. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2600. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2601. array('option' => array('value' => 'value')), 'good', '/option',
  2602. array('option' => array('value' => 'other')), 'bad', '/option',
  2603. '/select',
  2604. '/div'
  2605. );
  2606. $this->assertTags($result, $expected);
  2607. }
  2608. /**
  2609. * Test that inferred types do not override developer input
  2610. *
  2611. * @return void
  2612. */
  2613. public function testInputMagicTypeDoesNotOverride() {
  2614. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2615. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2616. $expected = array(
  2617. 'div' => array('class' => 'input checkbox'),
  2618. array('input' => array(
  2619. 'type' => 'hidden',
  2620. 'name' => 'data[Model][user]',
  2621. 'id' => 'ModelUser_',
  2622. 'value' => 0,
  2623. )),
  2624. array('input' => array(
  2625. 'name' => 'data[Model][user]',
  2626. 'type' => 'checkbox',
  2627. 'id' => 'ModelUser',
  2628. 'value' => 1
  2629. )),
  2630. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2631. '/div'
  2632. );
  2633. $this->assertTags($result, $expected);
  2634. }
  2635. /**
  2636. * Test that magic input() selects are created for type=number
  2637. *
  2638. * @return void
  2639. */
  2640. public function testInputMagicSelectForTypeNumber() {
  2641. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2642. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2643. $result = $this->Form->input('ValidateUser.balance');
  2644. $expected = array(
  2645. 'div' => array('class' => 'input select'),
  2646. 'label' => array('for' => 'ValidateUserBalance'),
  2647. 'Balance',
  2648. '/label',
  2649. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2650. array('option' => array('value' => '0')),
  2651. 'nothing',
  2652. '/option',
  2653. array('option' => array('value' => '1', 'selected' => 'selected')),
  2654. 'some',
  2655. '/option',
  2656. array('option' => array('value' => '100')),
  2657. 'a lot',
  2658. '/option',
  2659. '/select',
  2660. '/div'
  2661. );
  2662. $this->assertTags($result, $expected);
  2663. }
  2664. /**
  2665. * Test that magic input() selects can easily be converted into radio types without error.
  2666. *
  2667. * @return void
  2668. */
  2669. public function testInputMagicSelectChangeToRadio() {
  2670. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2671. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2672. $this->assertRegExp('/input type="radio"/', $result);
  2673. }
  2674. /**
  2675. * fields with the same name as the model should work.
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputWithMatchingFieldAndModelName() {
  2680. $this->Form->create('User');
  2681. $this->Form->fieldset = array(
  2682. 'User' => array(
  2683. 'fields' => array(
  2684. 'User' => array('type' => 'text')
  2685. ),
  2686. 'validates' => array(),
  2687. 'key' => 'id'
  2688. )
  2689. );
  2690. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2691. $result = $this->Form->input('User', array('type' => 'text'));
  2692. $expected = array(
  2693. 'div' => array('class' => 'input text'),
  2694. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2695. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2696. '/div'
  2697. );
  2698. $this->assertTags($result, $expected);
  2699. }
  2700. /**
  2701. * testFormInputs method
  2702. *
  2703. * test correct results from form::inputs().
  2704. *
  2705. * @return void
  2706. */
  2707. public function testFormInputs() {
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs('The Legend');
  2710. $expected = array(
  2711. '<fieldset',
  2712. '<legend',
  2713. 'The Legend',
  2714. '/legend',
  2715. '*/fieldset',
  2716. );
  2717. $this->assertTags($result, $expected);
  2718. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2719. $expected = array(
  2720. 'fieldset' => array('class' => 'classy-stuff'),
  2721. '<legend',
  2722. 'Field of Dreams',
  2723. '/legend',
  2724. '*/fieldset'
  2725. );
  2726. $this->assertTags($result, $expected);
  2727. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2728. $this->assertTags($result, $expected);
  2729. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2730. $this->assertTags($result, $expected);
  2731. $this->Form->create('Contact');
  2732. $this->Form->request['prefix'] = 'admin';
  2733. $this->Form->request['action'] = 'admin_edit';
  2734. $result = $this->Form->inputs();
  2735. $expected = array(
  2736. '<fieldset',
  2737. '<legend',
  2738. 'Edit Contact',
  2739. '/legend',
  2740. '*/fieldset',
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. $this->Form->create('Contact');
  2744. $result = $this->Form->inputs(false);
  2745. $expected = array(
  2746. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2747. array('div' => array('class' => 'input text')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input email')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input tel')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input password')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input date')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input date')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input datetime')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input number')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input select')),
  2764. '*/div',
  2765. );
  2766. $this->assertTags($result, $expected);
  2767. $this->Form->create('Contact');
  2768. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2769. $expected = array(
  2770. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2771. array('div' => array('class' => 'input text')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input email')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input tel')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input password')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input date')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input date')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input datetime')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input number')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input select')),
  2788. '*/div',
  2789. );
  2790. $this->assertTags($result, $expected);
  2791. $this->Form->create('Contact');
  2792. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2793. $this->assertTags($result, $expected);
  2794. $this->Form->create('Contact');
  2795. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2796. $expected = array(
  2797. 'fieldset' => array(),
  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('fieldset' => false, 'legend' => 'Hello'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2824. array('div' => array('class' => 'input text')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input email')),
  2827. '*/div',
  2828. array('div' => array('class' => 'input tel')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input password')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input date')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input date')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input datetime')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input number')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input select')),
  2841. '*/div',
  2842. );
  2843. $this->assertTags($result, $expected);
  2844. $this->Form->create('Contact');
  2845. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2846. $this->assertTags($result, $expected);
  2847. $this->Form->create('Contact');
  2848. $result = $this->Form->inputs('Hello');
  2849. $expected = array(
  2850. 'fieldset' => array(),
  2851. 'legend' => array(),
  2852. 'Hello',
  2853. '/legend',
  2854. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2855. array('div' => array('class' => 'input text')),
  2856. '*/div',
  2857. array('div' => array('class' => 'input email')),
  2858. '*/div',
  2859. array('div' => array('class' => 'input tel')),
  2860. '*/div',
  2861. array('div' => array('class' => 'input password')),
  2862. '*/div',
  2863. array('div' => array('class' => 'input date')),
  2864. '*/div',
  2865. array('div' => array('class' => 'input date')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input datetime')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input number')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input select')),
  2872. '*/div',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $this->Form->create('Contact');
  2877. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2878. $expected = array(
  2879. 'fieldset' => array(),
  2880. 'legend' => array(),
  2881. 'Hello',
  2882. '/legend',
  2883. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2884. array('div' => array('class' => 'input text')),
  2885. '*/div',
  2886. array('div' => array('class' => 'input email')),
  2887. '*/div',
  2888. array('div' => array('class' => 'input tel')),
  2889. '*/div',
  2890. array('div' => array('class' => 'input password')),
  2891. '*/div',
  2892. array('div' => array('class' => 'input date')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input date')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input datetime')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input number')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input select')),
  2901. '*/div',
  2902. '/fieldset'
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. $this->Form->create('Contact');
  2906. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2907. $this->assertTags($result, $expected);
  2908. $this->Form->end();
  2909. $this->Form->create(false);
  2910. $expected = array(
  2911. 'fieldset' => array(),
  2912. array('div' => array('class' => 'input text')),
  2913. 'label' => array('for' => 'foo'),
  2914. 'Foo',
  2915. '/label',
  2916. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2917. '*/div',
  2918. '/fieldset'
  2919. );
  2920. $result = $this->Form->inputs(
  2921. array('foo' => array('type' => 'text')),
  2922. array(),
  2923. array('legend' => false)
  2924. );
  2925. $this->assertTags($result, $expected);
  2926. }
  2927. /**
  2928. * Tests inputs() works with plugin models
  2929. *
  2930. * @return void
  2931. */
  2932. public function testInputsPluginModel() {
  2933. $this->loadFixtures('Post');
  2934. App::build(array(
  2935. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2936. ));
  2937. CakePlugin::load('TestPlugin');
  2938. $this->Form->request['models'] = array(
  2939. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2940. );
  2941. $this->Form->create('TestPlugin.TestPluginPost');
  2942. $result = $this->Form->inputs();
  2943. $this->assertContains('data[TestPluginPost][id]', $result);
  2944. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2945. $this->assertContains('data[TestPluginPost][title]', $result);
  2946. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2947. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2948. $this->assertEquals('TestPluginPost', $this->Form->model());
  2949. }
  2950. /**
  2951. * testSelectAsCheckbox method
  2952. *
  2953. * test multi-select widget with checkbox formatting.
  2954. *
  2955. * @return void
  2956. */
  2957. public function testSelectAsCheckbox() {
  2958. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2959. $expected = array(
  2960. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2961. array('div' => array('class' => 'checkbox')),
  2962. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2963. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2964. 'first',
  2965. '/label',
  2966. '/div',
  2967. array('div' => array('class' => 'checkbox')),
  2968. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2969. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2970. 'second',
  2971. '/label',
  2972. '/div',
  2973. array('div' => array('class' => 'checkbox')),
  2974. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2975. array('label' => array('for' => 'ModelMultiField2')),
  2976. 'third',
  2977. '/label',
  2978. '/div',
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2982. $expected = array(
  2983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2984. array('div' => array('class' => 'checkbox')),
  2985. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2986. array('label' => array('for' => 'ModelMultiField12')),
  2987. 'half',
  2988. '/label',
  2989. '/div',
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. }
  2993. /**
  2994. * testLabel method
  2995. *
  2996. * test label generation.
  2997. *
  2998. * @return void
  2999. */
  3000. public function testLabel() {
  3001. $this->Form->text('Person.name');
  3002. $result = $this->Form->label();
  3003. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3004. $this->Form->text('Person.name');
  3005. $result = $this->Form->label();
  3006. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3007. $result = $this->Form->label('Person.first_name');
  3008. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3009. $result = $this->Form->label('Person.first_name', 'Your first name');
  3010. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3011. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3012. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3013. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3014. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3015. $result = $this->Form->label('Person.first_name', '');
  3016. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3017. $result = $this->Form->label('Person.2.name', '');
  3018. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3019. }
  3020. /**
  3021. * testTextbox method
  3022. *
  3023. * test textbox element generation
  3024. *
  3025. * @return void
  3026. */
  3027. public function testTextbox() {
  3028. $result = $this->Form->text('Model.field');
  3029. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3030. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3031. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3032. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3033. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3034. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3035. $result = $this->Form->text('Model.text');
  3036. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3037. $Contact = ClassRegistry::getObject('Contact');
  3038. $Contact->validationErrors['text'] = array(true);
  3039. $this->Form->request->data['Contact']['text'] = 'test';
  3040. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3041. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3042. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3043. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3044. $expected = array(
  3045. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3046. );
  3047. $this->assertTags($result, $expected);
  3048. }
  3049. /**
  3050. * testDefaultValue method
  3051. *
  3052. * Test default value setting
  3053. *
  3054. * @return void
  3055. */
  3056. public function testDefaultValue() {
  3057. $this->Form->request->data['Model']['field'] = 'test';
  3058. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3059. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3060. unset($this->Form->request->data['Model']['field']);
  3061. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3062. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3063. }
  3064. /**
  3065. * testCheckboxDefaultValue method
  3066. *
  3067. * Test default value setting on checkbox() method
  3068. *
  3069. * @return void
  3070. */
  3071. public function testCheckboxDefaultValue() {
  3072. $this->Form->request->data['Model']['field'] = false;
  3073. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3074. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3075. unset($this->Form->request->data['Model']['field']);
  3076. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3077. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3078. $this->Form->request->data['Model']['field'] = true;
  3079. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3080. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3081. unset($this->Form->request->data['Model']['field']);
  3082. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3083. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3084. }
  3085. /**
  3086. * testError method
  3087. *
  3088. * Test field error generation
  3089. *
  3090. * @return void
  3091. */
  3092. public function testError() {
  3093. $Contact = ClassRegistry::getObject('Contact');
  3094. $Contact->validationErrors['field'] = array(1);
  3095. $result = $this->Form->error('Contact.field');
  3096. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3097. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3098. $this->assertEquals('Error in field Field', $result);
  3099. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3100. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3101. $this->assertEquals('This field contains invalid input', $result);
  3102. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3103. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3104. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3105. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3106. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3107. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3108. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3109. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3110. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3111. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3112. $this->assertEquals('<strong>Badness!</strong>', $result);
  3113. $Contact->validationErrors['field'] = array("email");
  3114. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3115. $expected = array(
  3116. 'div' => array('class' => 'field-error'),
  3117. 'No good!',
  3118. '/div'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3122. $result = $this->Form->error('Contact.field', array(
  3123. 'notEmpty' => 'Cannot be empty',
  3124. 'email' => 'No good!'
  3125. ));
  3126. $expected = array(
  3127. 'div' => array('class' => 'error-message'),
  3128. 'ul' => array(),
  3129. '<li', 'Cannot be empty', '/li',
  3130. '<li', 'No good!', '/li',
  3131. '<li', 'Something else', '/li',
  3132. '/ul',
  3133. '/div'
  3134. );
  3135. $this->assertTags($result, $expected);
  3136. // Testing error messages list options
  3137. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3138. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3139. $expected = array(
  3140. 'div' => array('class' => 'error-message'),
  3141. 'ol' => array(),
  3142. '<li', 'notEmpty', '/li',
  3143. '<li', 'email', '/li',
  3144. '/ol',
  3145. '/div'
  3146. );
  3147. $this->assertTags($result, $expected);
  3148. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3149. $expected = array(
  3150. 'div' => array('class' => 'error-message'),
  3151. 'ol' => array(),
  3152. '<li', 'notEmpty', '/li',
  3153. '<li', 'email', '/li',
  3154. '/ol',
  3155. '/div'
  3156. );
  3157. $this->assertTags($result, $expected);
  3158. $result = $this->Form->error('Contact.field', null, array(
  3159. 'listOptions' => array(
  3160. 'class' => 'ul-class',
  3161. 'itemOptions' => array(
  3162. 'class' => 'li-class'
  3163. )
  3164. )
  3165. ));
  3166. $expected = array(
  3167. 'div' => array('class' => 'error-message'),
  3168. 'ul' => array('class' => 'ul-class'),
  3169. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3170. array('li' => array('class' => 'li-class')), 'email', '/li',
  3171. '/ul',
  3172. '/div'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. }
  3176. /**
  3177. * test error options when using form->input();
  3178. *
  3179. * @return void
  3180. */
  3181. public function testInputErrorEscape() {
  3182. $this->Form->create('ValidateProfile');
  3183. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3184. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3185. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3186. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3187. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3188. $this->assertRegExp('/required<br>/', $result);
  3189. }
  3190. /**
  3191. * testPassword method
  3192. *
  3193. * Test password element generation
  3194. *
  3195. * @return void
  3196. */
  3197. public function testPassword() {
  3198. $Contact = ClassRegistry::getObject('Contact');
  3199. $result = $this->Form->password('Contact.field');
  3200. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3201. $Contact->validationErrors['passwd'] = 1;
  3202. $this->Form->request->data['Contact']['passwd'] = 'test';
  3203. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3204. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3205. }
  3206. /**
  3207. * testRadio method
  3208. *
  3209. * Test radio element set generation
  3210. *
  3211. * @return void
  3212. */
  3213. public function testRadio() {
  3214. $result = $this->Form->radio('Model.field', array('option A'));
  3215. $expected = array(
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3218. 'label' => array('for' => 'ModelField0'),
  3219. 'option A',
  3220. '/label'
  3221. );
  3222. $this->assertTags($result, $expected);
  3223. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3224. $expected = array(
  3225. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3226. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3227. 'label' => array('for' => 'ModelField12'),
  3228. 'half',
  3229. '/label'
  3230. );
  3231. $this->assertTags($result, $expected);
  3232. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3233. $expected = array(
  3234. 'fieldset' => array(),
  3235. 'legend' => array(),
  3236. 'Field',
  3237. '/legend',
  3238. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3240. array('label' => array('for' => 'ModelField0')),
  3241. 'option A',
  3242. '/label',
  3243. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3244. array('label' => array('for' => 'ModelField1')),
  3245. 'option B',
  3246. '/label',
  3247. '/fieldset'
  3248. );
  3249. $this->assertTags($result, $expected);
  3250. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3251. $expected = array(
  3252. 'fieldset' => array(),
  3253. 'legend' => array(),
  3254. 'Field',
  3255. '/legend',
  3256. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3257. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3258. array('label' => array('for' => 'ModelField0')),
  3259. 'option A',
  3260. '/label',
  3261. 'br' => array(),
  3262. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3263. array('label' => array('for' => 'ModelField1')),
  3264. 'option B',
  3265. '/label',
  3266. '/fieldset'
  3267. );
  3268. $this->assertTags($result, $expected);
  3269. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3270. $expected = array(
  3271. 'fieldset' => array(),
  3272. 'legend' => array(),
  3273. 'Gender',
  3274. '/legend',
  3275. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3276. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3277. array('label' => array('for' => 'EmployeeGenderMale')),
  3278. 'Male',
  3279. '/label',
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3281. array('label' => array('for' => 'EmployeeGenderFemale')),
  3282. 'Female',
  3283. '/label',
  3284. '/fieldset',
  3285. );
  3286. $this->assertTags($result, $expected);
  3287. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3288. $expected = array(
  3289. 'fieldset' => array(),
  3290. 'legend' => array(),
  3291. 'Gender',
  3292. '/legend',
  3293. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3294. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3295. array('label' => array('for' => 'OfficerGenderMale')),
  3296. 'Male',
  3297. '/label',
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3299. array('label' => array('for' => 'OfficerGenderFemale')),
  3300. 'Female',
  3301. '/label',
  3302. '/fieldset',
  3303. );
  3304. $this->assertTags($result, $expected);
  3305. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3306. $expected = array(
  3307. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3308. array('input' => array(
  3309. 'type' => 'radio',
  3310. 'name' => 'data[Contact][1][imrequired]',
  3311. 'value' => '0',
  3312. 'id' => 'Contact1Imrequired0',
  3313. 'required' => 'required'
  3314. )),
  3315. 'label' => array('for' => 'Contact1Imrequired0'),
  3316. 'option A',
  3317. '/label'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $result = $this->Form->radio('Model.1.field', array('option A'));
  3321. $expected = array(
  3322. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3323. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3324. 'label' => array('for' => 'Model1Field0'),
  3325. 'option A',
  3326. '/label'
  3327. );
  3328. $this->assertTags($result, $expected);
  3329. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3330. $expected = array(
  3331. 'fieldset' => array(),
  3332. 'legend' => array(),
  3333. 'Field',
  3334. '/legend',
  3335. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3336. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3337. array('label' => array('for' => 'ModelField0')),
  3338. 'option A',
  3339. '/label',
  3340. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3341. array('label' => array('for' => 'ModelField1')),
  3342. 'option B',
  3343. '/label',
  3344. '/fieldset'
  3345. );
  3346. $this->assertTags($result, $expected);
  3347. $result = $this->Form->radio(
  3348. 'Model.field',
  3349. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3350. );
  3351. $expected = array(
  3352. 'fieldset' => array(),
  3353. 'legend' => array(),
  3354. 'Field',
  3355. '/legend',
  3356. 'input' => array(
  3357. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3358. 'id' => 'ModelField_', 'value' => '',
  3359. ),
  3360. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3361. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3362. array('label' => array('for' => 'ModelFieldAB')),
  3363. 'first',
  3364. '/label',
  3365. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3366. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3367. array('label' => array('for' => 'ModelFieldAB1')),
  3368. 'second',
  3369. '/label',
  3370. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3371. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3372. array('label' => array('for' => 'ModelFieldAB2')),
  3373. 'third',
  3374. '/label',
  3375. '/fieldset'
  3376. );
  3377. $this->assertTags($result, $expected);
  3378. }
  3379. /**
  3380. * Test radio inputs with between as string or array. Also ensure
  3381. * that an array with less between elements works.
  3382. *
  3383. * @return void
  3384. */
  3385. public function testRadioBetween() {
  3386. $result = $this->Form->radio(
  3387. 'Model.field',
  3388. array('option A', 'option B'),
  3389. array('between' => 'I am between')
  3390. );
  3391. $expected = array(
  3392. 'fieldset' => array(),
  3393. 'legend' => array(),
  3394. 'Field',
  3395. '/legend',
  3396. 'I am between',
  3397. 'input' => array(
  3398. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3399. 'value' => '', 'id' => 'ModelField_'
  3400. ),
  3401. array('input' => array(
  3402. 'type' => 'radio', 'name' => 'data[Model][field]',
  3403. 'value' => '0', 'id' => 'ModelField0'
  3404. )),
  3405. array('label' => array('for' => 'ModelField0')),
  3406. 'option A',
  3407. '/label',
  3408. array('input' => array(
  3409. 'type' => 'radio', 'name' => 'data[Model][field]',
  3410. 'value' => '1', 'id' => 'ModelField1'
  3411. )),
  3412. array('label' => array('for' => 'ModelField1')),
  3413. 'option B',
  3414. '/label',
  3415. '/fieldset'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $result = $this->Form->radio(
  3419. 'Model.field',
  3420. array('option A', 'option B', 'option C'),
  3421. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3422. );
  3423. $expected = array(
  3424. 'fieldset' => array(),
  3425. 'legend' => array(),
  3426. 'Field',
  3427. '/legend',
  3428. 'input' => array(
  3429. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3430. 'value' => '', 'id' => 'ModelField_'
  3431. ),
  3432. array('input' => array(
  3433. 'type' => 'radio', 'name' => 'data[Model][field]',
  3434. 'value' => '0', 'id' => 'ModelField0'
  3435. )),
  3436. array('label' => array('for' => 'ModelField0')),
  3437. 'option A',
  3438. '/label',
  3439. 'between A',
  3440. '--separator--',
  3441. array('input' => array(
  3442. 'type' => 'radio', 'name' => 'data[Model][field]',
  3443. 'value' => '1', 'id' => 'ModelField1'
  3444. )),
  3445. array('label' => array('for' => 'ModelField1')),
  3446. 'option B',
  3447. '/label',
  3448. 'between B',
  3449. '--separator--',
  3450. array('input' => array(
  3451. 'type' => 'radio', 'name' => 'data[Model][field]',
  3452. 'value' => '2', 'id' => 'ModelField2'
  3453. )),
  3454. array('label' => array('for' => 'ModelField2')),
  3455. 'option C',
  3456. '/label',
  3457. 'between C',
  3458. '/fieldset'
  3459. );
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->input('Model.field', array(
  3462. 'options' => array('1' => 'first', '2' => 'second'),
  3463. 'type' => 'radio',
  3464. 'before' => '--before--',
  3465. 'after' => '--after--',
  3466. 'separator' => '--separator--',
  3467. 'between' => array('--between first--', '--between second--')
  3468. ));
  3469. $expected = array(
  3470. 'div' => array('class' => 'input radio'),
  3471. '--before--',
  3472. 'fieldset' => array(),
  3473. 'legend' => array(),
  3474. 'Field',
  3475. '/legend',
  3476. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3477. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3478. array('label' => array('for' => 'ModelField1')),
  3479. 'first',
  3480. '/label',
  3481. '--between first--',
  3482. '--separator--',
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3484. array('label' => array('for' => 'ModelField2')),
  3485. 'second',
  3486. '/label',
  3487. '--between second--',
  3488. '/fieldset',
  3489. '--after--',
  3490. '/div'
  3491. );
  3492. $this->assertTags($result, $expected);
  3493. $result = $this->Form->input('Model.field', array(
  3494. 'options' => array('1' => 'first', '2' => 'second'),
  3495. 'type' => 'radio',
  3496. 'before' => '--before--',
  3497. 'after' => '--after--',
  3498. 'separator' => '--separator--',
  3499. 'between' => array('--between first--')
  3500. ));
  3501. $expected = array(
  3502. 'div' => array('class' => 'input radio'),
  3503. '--before--',
  3504. 'fieldset' => array(),
  3505. 'legend' => array(),
  3506. 'Field',
  3507. '/legend',
  3508. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3509. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3510. array('label' => array('for' => 'ModelField1')),
  3511. 'first',
  3512. '/label',
  3513. '--between first--',
  3514. '--separator--',
  3515. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3516. array('label' => array('for' => 'ModelField2')),
  3517. 'second',
  3518. '/label',
  3519. '/fieldset',
  3520. '--after--',
  3521. '/div'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * Test that radios with a 0 value are selected under the correct conditions.
  3527. * Also ensure that values that are booleanish are handled correctly.
  3528. *
  3529. * @return void
  3530. */
  3531. public function testRadioOptionWithBooleanishValues() {
  3532. $expected = array(
  3533. 'fieldset' => array(),
  3534. 'legend' => array(),
  3535. 'Field',
  3536. '/legend',
  3537. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3538. array('label' => array('for' => 'ModelField1')),
  3539. 'Yes',
  3540. '/label',
  3541. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3542. array('label' => array('for' => 'ModelField0')),
  3543. 'No',
  3544. '/label',
  3545. '/fieldset'
  3546. );
  3547. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3548. $this->assertTags($result, $expected);
  3549. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3550. $this->assertTags($result, $expected);
  3551. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3552. $this->assertTags($result, $expected);
  3553. $expected = array(
  3554. 'fieldset' => array(),
  3555. 'legend' => array(),
  3556. 'Field',
  3557. '/legend',
  3558. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3559. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3560. array('label' => array('for' => 'ModelField1')),
  3561. 'Yes',
  3562. '/label',
  3563. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3564. array('label' => array('for' => 'ModelField0')),
  3565. 'No',
  3566. '/label',
  3567. '/fieldset'
  3568. );
  3569. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3570. $this->assertTags($result, $expected);
  3571. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3572. $this->assertTags($result, $expected);
  3573. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3574. $this->assertTags($result, $expected);
  3575. $expected = array(
  3576. 'fieldset' => array(),
  3577. 'legend' => array(),
  3578. 'Field',
  3579. '/legend',
  3580. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3581. array('label' => array('for' => 'ModelField1')),
  3582. 'Yes',
  3583. '/label',
  3584. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3585. array('label' => array('for' => 'ModelField0')),
  3586. 'No',
  3587. '/label',
  3588. '/fieldset'
  3589. );
  3590. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3591. $this->assertTags($result, $expected);
  3592. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3595. $this->assertTags($result, $expected);
  3596. }
  3597. /**
  3598. * test disabled radio options
  3599. *
  3600. * @return void
  3601. */
  3602. public function testRadioDisabled() {
  3603. $result = $this->Form->radio(
  3604. 'Model.field',
  3605. array('option A', 'option B'),
  3606. array('disabled' => array(0), 'value' => '0')
  3607. );
  3608. $expected = array(
  3609. 'fieldset' => array(),
  3610. 'legend' => array(),
  3611. 'Field',
  3612. '/legend',
  3613. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3614. array('label' => array('for' => 'ModelField0')),
  3615. 'option A',
  3616. '/label',
  3617. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3618. array('label' => array('for' => 'ModelField1')),
  3619. 'option B',
  3620. '/label',
  3621. '/fieldset'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. $result = $this->Form->radio(
  3625. 'Model.field',
  3626. array('option A', 'option B'),
  3627. array('disabled' => true, 'value' => '0')
  3628. );
  3629. $expected = array(
  3630. 'fieldset' => array(),
  3631. 'legend' => array(),
  3632. 'Field',
  3633. '/legend',
  3634. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3635. array('label' => array('for' => 'ModelField0')),
  3636. 'option A',
  3637. '/label',
  3638. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3639. array('label' => array('for' => 'ModelField1')),
  3640. 'option B',
  3641. '/label',
  3642. '/fieldset'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->radio(
  3646. 'Model.field',
  3647. array('option A', 'option B'),
  3648. array('disabled' => 'disabled', 'value' => '0')
  3649. );
  3650. $expected = array(
  3651. 'fieldset' => array(),
  3652. 'legend' => array(),
  3653. 'Field',
  3654. '/legend',
  3655. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3656. array('label' => array('for' => 'ModelField0')),
  3657. 'option A',
  3658. '/label',
  3659. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3660. array('label' => array('for' => 'ModelField1')),
  3661. 'option B',
  3662. '/label',
  3663. '/fieldset'
  3664. );
  3665. $this->assertTags($result, $expected);
  3666. $result = $this->Form->input('Model.field', array(
  3667. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3668. 'type' => 'radio',
  3669. 'disabled' => array(2, '3x'),
  3670. ));
  3671. $expected = array(
  3672. 'div' => array('class' => 'input radio'),
  3673. 'fieldset' => array(),
  3674. 'legend' => array(),
  3675. 'Field',
  3676. '/legend',
  3677. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3678. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3679. array('label' => array('for' => 'ModelField1')),
  3680. 'first',
  3681. '/label',
  3682. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3683. array('label' => array('for' => 'ModelField2')),
  3684. 'second',
  3685. '/label',
  3686. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3687. array('label' => array('for' => 'ModelField2x')),
  3688. '2x',
  3689. '/label',
  3690. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3691. array('label' => array('for' => 'ModelField3')),
  3692. 'third',
  3693. '/label',
  3694. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3695. array('label' => array('for' => 'ModelField3x')),
  3696. '3x',
  3697. '/label',
  3698. '/fieldset',
  3699. '/div'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. $result = $this->Form->input('Model.field', array(
  3703. 'type' => 'radio',
  3704. 'options' => array(
  3705. 1 => 'A',
  3706. 2 => 'B',
  3707. 3 => 'C'
  3708. ),
  3709. 'disabled' => array(1)
  3710. ));
  3711. $expected = array(
  3712. 'div' => array('class' => 'input radio'),
  3713. 'fieldset' => array(),
  3714. 'legend' => array(),
  3715. 'Field',
  3716. '/legend',
  3717. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3718. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3719. array('label' => array('for' => 'ModelField1')),
  3720. 'A',
  3721. '/label',
  3722. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3723. array('label' => array('for' => 'ModelField2')),
  3724. 'B',
  3725. '/label',
  3726. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3727. array('label' => array('for' => 'ModelField3')),
  3728. 'C',
  3729. '/label',
  3730. '/fieldset',
  3731. '/div'
  3732. );
  3733. $this->assertTags($result, $expected);
  3734. }
  3735. /**
  3736. * test disabling the hidden input for radio buttons
  3737. *
  3738. * @return void
  3739. */
  3740. public function testRadioHiddenInputDisabling() {
  3741. $result = $this->Form->input('Model.1.field', array(
  3742. 'type' => 'radio',
  3743. 'options' => array('option A'),
  3744. 'hiddenField' => false
  3745. )
  3746. );
  3747. $expected = array(
  3748. 'div' => array('class' => 'input radio'),
  3749. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3750. 'label' => array('for' => 'Model1Field0'),
  3751. 'option A',
  3752. '/label',
  3753. '/div'
  3754. );
  3755. $this->assertTags($result, $expected);
  3756. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3757. $expected = array(
  3758. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3759. 'label' => array('for' => 'Model1Field0'),
  3760. 'option A',
  3761. '/label'
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. }
  3765. /**
  3766. * test adding an empty option for radio buttons
  3767. *
  3768. * @return void
  3769. */
  3770. public function testRadioAddEmptyOption() {
  3771. $result = $this->Form->input('Model.1.field', array(
  3772. 'type' => 'radio',
  3773. 'options' => array('option A'),
  3774. 'empty' => true,
  3775. 'hiddenField' => false
  3776. ));
  3777. $expected = array(
  3778. 'div' => array('class' => 'input radio'),
  3779. 'fieldset' => array(),
  3780. 'legend' => array(),
  3781. 'Field',
  3782. '/legend',
  3783. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3784. array('label' => array('for' => 'Model1Field')),
  3785. __('empty'),
  3786. '/label',
  3787. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3788. array('label' => array('for' => 'Model1Field0')),
  3789. 'option A',
  3790. '/label',
  3791. '/fieldset',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->input('Model.1.field', array(
  3796. 'type' => 'radio',
  3797. 'options' => array('option A'),
  3798. 'empty' => 'CustomEmptyLabel',
  3799. 'hiddenField' => false
  3800. ));
  3801. $expected = array(
  3802. 'div' => array('class' => 'input radio'),
  3803. 'fieldset' => array(),
  3804. 'legend' => array(),
  3805. 'Field',
  3806. '/legend',
  3807. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3808. array('label' => array('for' => 'Model1Field')),
  3809. 'CustomEmptyLabel',
  3810. '/label',
  3811. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3812. array('label' => array('for' => 'Model1Field0')),
  3813. 'option A',
  3814. '/label',
  3815. '/fieldset',
  3816. '/div'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $result = $this->Form->input('Model.1.field', array(
  3820. 'type' => 'radio',
  3821. 'options' => array('option A'),
  3822. 'empty' => false,
  3823. 'hiddenField' => false
  3824. ));
  3825. $this->assertTextNotContains('"Model1Field"', $result);
  3826. }
  3827. /**
  3828. * Test that radio() accepts an array for label
  3829. *
  3830. * @return void
  3831. */
  3832. public function testRadioLabelArray() {
  3833. $result = $this->Form->input('Model.field', array(
  3834. 'type' => 'radio',
  3835. 'legend' => false,
  3836. 'label' => array(
  3837. 'class' => 'checkbox float-left',
  3838. ),
  3839. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3840. ));
  3841. $this->assertTextContains(
  3842. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3843. $result
  3844. );
  3845. }
  3846. /**
  3847. * Test that label id's match the input element id's when radio is called after create().
  3848. *
  3849. * @return void
  3850. */
  3851. public function testRadioWithCreate() {
  3852. $this->Form->create('Model');
  3853. $result = $this->Form->radio('recipient',
  3854. array('1' => '1', '2' => '2', '3' => '3'),
  3855. array('legend' => false, 'value' => '1')
  3856. );
  3857. $this->assertTextNotContains(
  3858. '<label for="ModelModelRecipient1">1</label>',
  3859. $result
  3860. );
  3861. $this->assertTextContains(
  3862. '<label for="ModelRecipient1">1</label>',
  3863. $result
  3864. );
  3865. }
  3866. /**
  3867. * testDomIdSuffix method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testDomIdSuffix() {
  3872. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3873. $this->assertEquals('1StringWith1DollarSigns', $result);
  3874. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3875. $this->assertEquals('AbcXFooYBar', $result);
  3876. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  3877. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3878. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  3879. $this->assertEquals('AbcX=FooY=Bar', $result);
  3880. }
  3881. /**
  3882. * testDomIdSuffixCollisionResolvement()
  3883. *
  3884. * @return void
  3885. */
  3886. public function testDomIdSuffixCollisionResolvement() {
  3887. $result = $this->Form->domIdSuffix('a>b');
  3888. $this->assertEquals('AB', $result);
  3889. $result = $this->Form->domIdSuffix('a<b');
  3890. $this->assertEquals('AB1', $result);
  3891. $result = $this->Form->domIdSuffix('a\'b');
  3892. $this->assertEquals('AB2', $result);
  3893. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3894. $this->assertEquals('1StringWith1Dollar', $result);
  3895. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3896. $this->assertEquals('1StringWith1Dollar1', $result);
  3897. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3898. $this->assertEquals('1StringWith1Dollar2', $result);
  3899. }
  3900. /**
  3901. * testSelect method
  3902. *
  3903. * Test select element generation.
  3904. *
  3905. * @return void
  3906. */
  3907. public function testSelect() {
  3908. $result = $this->Form->select('Model.field', array());
  3909. $expected = array(
  3910. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3911. array('option' => array('value' => '')),
  3912. '/option',
  3913. '/select'
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3917. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3918. $expected = array(
  3919. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3920. array('option' => array('value' => '')),
  3921. '/option',
  3922. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3923. 'good',
  3924. '/option',
  3925. array('option' => array('value' => 'other')),
  3926. 'bad',
  3927. '/option',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. $this->Form->request->data = array();
  3932. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3933. $expected = array(
  3934. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3935. array('option' => array('value' => '')),
  3936. '/option',
  3937. array('option' => array('value' => 'value')),
  3938. 'good',
  3939. '/option',
  3940. array('option' => array('value' => 'other')),
  3941. 'bad',
  3942. '/option',
  3943. '/select'
  3944. );
  3945. $this->assertTags($result, $expected);
  3946. $result = $this->Form->select(
  3947. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3948. array('empty' => false)
  3949. );
  3950. $expected = array(
  3951. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3952. array('option' => array('value' => 'first')),
  3953. 'first &quot;html&quot; &lt;chars&gt;',
  3954. '/option',
  3955. array('option' => array('value' => 'second')),
  3956. 'value',
  3957. '/option',
  3958. '/select'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->select(
  3962. 'Model.field',
  3963. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3964. array('escape' => false, 'empty' => false)
  3965. );
  3966. $expected = array(
  3967. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3968. array('option' => array('value' => 'first')),
  3969. 'first "html" <chars>',
  3970. '/option',
  3971. array('option' => array('value' => 'second')),
  3972. 'value',
  3973. '/option',
  3974. '/select'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. $options = array(
  3978. array('value' => 'first', 'name' => 'First'),
  3979. array('value' => 'first', 'name' => 'Another First'),
  3980. );
  3981. $result = $this->Form->select(
  3982. 'Model.field',
  3983. $options,
  3984. array('escape' => false, 'empty' => false)
  3985. );
  3986. $expected = array(
  3987. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3988. array('option' => array('value' => 'first')),
  3989. 'First',
  3990. '/option',
  3991. array('option' => array('value' => 'first')),
  3992. 'Another First',
  3993. '/option',
  3994. '/select'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3998. $result = $this->Form->select(
  3999. 'Model.contact_id',
  4000. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4001. array('escape' => false, 'empty' => 'pick something')
  4002. );
  4003. $expected = array(
  4004. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4005. array('option' => array('value' => '')), 'pick something', '/option',
  4006. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4007. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4008. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4009. '/select'
  4010. );
  4011. $this->assertTags($result, $expected);
  4012. $this->Form->request->data['Model']['field'] = 0;
  4013. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4014. $expected = array(
  4015. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4016. array('option' => array('value' => '')), '/option',
  4017. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4018. array('option' => array('value' => '1')), 'Yes', '/option',
  4019. '/select'
  4020. );
  4021. $this->assertTags($result, $expected);
  4022. $this->Form->request->data['Model']['field'] = 50;
  4023. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4024. $expected = array(
  4025. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4026. array('option' => array('value' => '')), '/option',
  4027. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4028. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4029. '/select'
  4030. );
  4031. $this->assertTags($result, $expected);
  4032. $result = $this->Form->select('Contact.required_one', array('option A'));
  4033. $expected = array(
  4034. 'select' => array(
  4035. 'name' => 'data[Contact][required_one]',
  4036. 'id' => 'ContactRequiredOne',
  4037. 'required' => 'required'
  4038. ),
  4039. array('option' => array('value' => '')), '/option',
  4040. array('option' => array('value' => '0')), 'option A', '/option',
  4041. '/select'
  4042. );
  4043. $this->assertTags($result, $expected);
  4044. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4045. $expected = array(
  4046. 'select' => array(
  4047. 'name' => 'data[Contact][required_one]',
  4048. 'id' => 'ContactRequiredOne',
  4049. 'disabled' => 'disabled'
  4050. ),
  4051. array('option' => array('value' => '')), '/option',
  4052. array('option' => array('value' => '0')), 'option A', '/option',
  4053. '/select'
  4054. );
  4055. $this->assertTags($result, $expected);
  4056. }
  4057. /**
  4058. * test that select() with optiongroups listens to the escape param.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testSelectOptionGroupEscaping() {
  4063. $options = array(
  4064. '>< Key' => array(
  4065. 1 => 'One',
  4066. 2 => 'Two'
  4067. )
  4068. );
  4069. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4070. $expected = array(
  4071. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4072. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4073. array('option' => array('value' => '1')), 'One', '/option',
  4074. array('option' => array('value' => '2')), 'Two', '/option',
  4075. '/optgroup',
  4076. '/select'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $options = array(
  4080. '>< Key' => array(
  4081. 1 => 'One',
  4082. 2 => 'Two'
  4083. )
  4084. );
  4085. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4086. $expected = array(
  4087. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4088. 'optgroup' => array('label' => '>< Key'),
  4089. array('option' => array('value' => '1')), 'One', '/option',
  4090. array('option' => array('value' => '2')), 'Two', '/option',
  4091. '/optgroup',
  4092. '/select'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. }
  4096. /**
  4097. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4098. *
  4099. * @return void
  4100. */
  4101. public function testSelectWithNullAttributes() {
  4102. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4103. $expected = array(
  4104. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4105. array('option' => array('value' => '0')),
  4106. 'first',
  4107. '/option',
  4108. array('option' => array('value' => '1')),
  4109. 'second',
  4110. '/option',
  4111. '/select'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. }
  4115. /**
  4116. * testNestedSelect method
  4117. *
  4118. * test select element generation with optgroups
  4119. *
  4120. * @return void
  4121. */
  4122. public function testNestedSelect() {
  4123. $result = $this->Form->select(
  4124. 'Model.field',
  4125. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4126. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4127. )), array('empty' => false)
  4128. );
  4129. $expected = array(
  4130. 'select' => array('name' => 'data[Model][field]',
  4131. 'id' => 'ModelField'),
  4132. array('option' => array('value' => 1)),
  4133. 'One',
  4134. '/option',
  4135. array('option' => array('value' => 2)),
  4136. 'Two',
  4137. '/option',
  4138. array('optgroup' => array('label' => 'Three')),
  4139. array('option' => array('value' => 4)),
  4140. 'Four',
  4141. '/option',
  4142. array('option' => array('value' => 5)),
  4143. 'Five',
  4144. '/option',
  4145. '/optgroup',
  4146. '/select'
  4147. );
  4148. $this->assertTags($result, $expected);
  4149. $result = $this->Form->select(
  4150. 'Model.field',
  4151. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4152. array('showParents' => true, 'empty' => false)
  4153. );
  4154. $expected = array(
  4155. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4156. array('option' => array('value' => 1)),
  4157. 'One',
  4158. '/option',
  4159. array('option' => array('value' => 2)),
  4160. 'Two',
  4161. '/option',
  4162. array('optgroup' => array('label' => 'Three')),
  4163. array('option' => array('value' => 3)),
  4164. 'Three',
  4165. '/option',
  4166. array('option' => array('value' => 4)),
  4167. 'Four',
  4168. '/option',
  4169. '/optgroup',
  4170. '/select'
  4171. );
  4172. $this->assertTags($result, $expected);
  4173. }
  4174. /**
  4175. * testSelectMultiple method
  4176. *
  4177. * test generation of multiple select elements
  4178. *
  4179. * @return void
  4180. */
  4181. public function testSelectMultiple() {
  4182. $options = array('first', 'second', 'third');
  4183. $result = $this->Form->select(
  4184. 'Model.multi_field', $options, array('multiple' => true)
  4185. );
  4186. $expected = array(
  4187. 'input' => array(
  4188. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4189. ),
  4190. 'select' => array(
  4191. 'name' => 'data[Model][multi_field][]',
  4192. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4193. ),
  4194. array('option' => array('value' => '0')),
  4195. 'first',
  4196. '/option',
  4197. array('option' => array('value' => '1')),
  4198. 'second',
  4199. '/option',
  4200. array('option' => array('value' => '2')),
  4201. 'third',
  4202. '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $result = $this->Form->select(
  4207. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4208. );
  4209. $expected = array(
  4210. 'input' => array(
  4211. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4212. ),
  4213. 'select' => array(
  4214. 'name' => 'data[Model][multi_field][]',
  4215. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4216. ),
  4217. array('option' => array('value' => '0')),
  4218. 'first',
  4219. '/option',
  4220. array('option' => array('value' => '1')),
  4221. 'second',
  4222. '/option',
  4223. array('option' => array('value' => '2')),
  4224. 'third',
  4225. '/option',
  4226. '/select'
  4227. );
  4228. $this->assertTags($result, $expected);
  4229. $result = $this->Form->select(
  4230. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4231. );
  4232. $expected = array(
  4233. 'input' => array(
  4234. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4235. ),
  4236. 'select' => array(
  4237. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4238. 'multiple' => 'multiple'
  4239. ),
  4240. array('option' => array('value' => '0', 'selected' => 'selected')),
  4241. 'first',
  4242. '/option',
  4243. array('option' => array('value' => '1', 'selected' => 'selected')),
  4244. 'second',
  4245. '/option',
  4246. array('option' => array('value' => '2')),
  4247. 'third',
  4248. '/option',
  4249. '/select'
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $result = $this->Form->select(
  4253. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4254. );
  4255. $expected = array(
  4256. 'select' => array(
  4257. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4258. ),
  4259. array('option' => array('value' => '0', 'selected' => 'selected')),
  4260. 'first',
  4261. '/option',
  4262. array('option' => array('value' => '1', 'selected' => 'selected')),
  4263. 'second',
  4264. '/option',
  4265. array('option' => array('value' => '2')),
  4266. 'third',
  4267. '/option',
  4268. '/select'
  4269. );
  4270. $this->assertTags($result, $expected);
  4271. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4272. $selected = array('2', '3x');
  4273. $result = $this->Form->select(
  4274. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4275. );
  4276. $expected = array(
  4277. 'input' => array(
  4278. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4279. ),
  4280. 'select' => array(
  4281. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4282. ),
  4283. array('option' => array('value' => '1')),
  4284. 'One',
  4285. '/option',
  4286. array('option' => array('value' => '2', 'selected' => 'selected')),
  4287. 'Two',
  4288. '/option',
  4289. array('option' => array('value' => '3')),
  4290. 'Three',
  4291. '/option',
  4292. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4293. 'Stringy',
  4294. '/option',
  4295. '/select'
  4296. );
  4297. $this->assertTags($result, $expected);
  4298. $result = $this->Form->select('Contact.required_one', array(
  4299. '1' => 'option A',
  4300. '2' => 'option B'
  4301. ), array('multiple' => true));
  4302. $expected = array(
  4303. 'input' => array(
  4304. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4305. ),
  4306. 'select' => array(
  4307. 'name' => 'data[Contact][required_one][]',
  4308. 'id' => 'ContactRequiredOne',
  4309. 'required' => 'required',
  4310. 'multiple' => 'multiple'
  4311. ),
  4312. array('option' => array('value' => '1')), 'option A', '/option',
  4313. array('option' => array('value' => '2')), 'option B', '/option',
  4314. '/select'
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. $result = $this->Form->select(
  4318. 'Model.multi_field',
  4319. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4320. array('multiple' => true)
  4321. );
  4322. $expected = array(
  4323. 'input' => array(
  4324. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4325. 'id' => 'ModelMultiField_'
  4326. ),
  4327. array('select' => array('name' => 'data[Model][multi_field][]',
  4328. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4329. )),
  4330. array('option' => array('value' => 'a&gt;b')),
  4331. 'first',
  4332. '/option',
  4333. array('option' => array('value' => 'a&lt;b')),
  4334. 'second',
  4335. '/option',
  4336. array('option' => array(
  4337. 'value' => 'a&quot;b'
  4338. )),
  4339. 'third',
  4340. '/option',
  4341. '/select'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. }
  4345. /**
  4346. * Test generating multiple select with disabled elements.
  4347. *
  4348. * @return void
  4349. */
  4350. public function testSelectMultipleWithDisabledElements() {
  4351. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4352. $disabled = array(2, 3);
  4353. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4354. $expected = array(
  4355. 'input' => array(
  4356. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4357. ),
  4358. 'select' => array(
  4359. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4360. ),
  4361. array('option' => array('value' => '1')),
  4362. 'One',
  4363. '/option',
  4364. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4365. 'Two',
  4366. '/option',
  4367. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4368. 'Three',
  4369. '/option',
  4370. array('option' => array('value' => '3x')),
  4371. 'Stringy',
  4372. '/option',
  4373. '/select'
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4377. $disabled = array('2', '3x');
  4378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4379. $expected = array(
  4380. array('div' => array('class' => 'input select')),
  4381. array('label' => array('for' => 'ContactMultiple')),
  4382. 'Multiple',
  4383. '/label',
  4384. 'input' => array(
  4385. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4386. ),
  4387. 'select' => array(
  4388. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4389. ),
  4390. array('option' => array('value' => '1')),
  4391. 'One',
  4392. '/option',
  4393. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4394. 'Two',
  4395. '/option',
  4396. array('option' => array('value' => '3')),
  4397. 'Three',
  4398. '/option',
  4399. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4400. 'Stringy',
  4401. '/option',
  4402. '/select',
  4403. '/div'
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4407. $disabled = true;
  4408. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4409. $expected = array(
  4410. array('div' => array('class' => 'input select')),
  4411. array('label' => array('for' => 'ContactMultiple')),
  4412. 'Multiple',
  4413. '/label',
  4414. 'input' => array(
  4415. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4416. ),
  4417. 'select' => array(
  4418. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4419. ),
  4420. array('option' => array('value' => '1')),
  4421. 'One',
  4422. '/option',
  4423. array('option' => array('value' => '2')),
  4424. 'Two',
  4425. '/option',
  4426. array('option' => array('value' => '3')),
  4427. 'Three',
  4428. '/option',
  4429. array('option' => array('value' => '3x')),
  4430. 'Stringy',
  4431. '/option',
  4432. '/select',
  4433. '/div'
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. }
  4437. /**
  4438. * Test generating select with disabled elements.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testSelectWithDisabledElements() {
  4443. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4444. $disabled = array(2, 3);
  4445. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4446. $expected = array(
  4447. 'select' => array(
  4448. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4449. ),
  4450. array('option' => array('value' => '')),
  4451. '/option',
  4452. array('option' => array('value' => '1')),
  4453. 'One',
  4454. '/option',
  4455. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4456. 'Two',
  4457. '/option',
  4458. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4459. 'Three',
  4460. '/option',
  4461. array('option' => array('value' => '3x')),
  4462. 'Stringy',
  4463. '/option',
  4464. '/select'
  4465. );
  4466. $this->assertTags($result, $expected);
  4467. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4468. $disabled = array('2', '3x');
  4469. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4470. $expected = array(
  4471. array('div' => array('class' => 'input select')),
  4472. array('label' => array('for' => 'ModelField')),
  4473. 'Field',
  4474. '/label',
  4475. 'select' => array(
  4476. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4477. ),
  4478. array('option' => array('value' => '1')),
  4479. 'One',
  4480. '/option',
  4481. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4482. 'Two',
  4483. '/option',
  4484. array('option' => array('value' => '3')),
  4485. 'Three',
  4486. '/option',
  4487. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4488. 'Stringy',
  4489. '/option',
  4490. '/select',
  4491. '/div'
  4492. );
  4493. $this->assertTags($result, $expected);
  4494. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4495. $disabled = true;
  4496. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4497. $expected = array(
  4498. array('div' => array('class' => 'input select')),
  4499. array('label' => array('for' => 'ModelField')),
  4500. 'Field',
  4501. '/label',
  4502. 'select' => array(
  4503. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4504. ),
  4505. array('option' => array('value' => '1')),
  4506. 'One',
  4507. '/option',
  4508. array('option' => array('value' => '2')),
  4509. 'Two',
  4510. '/option',
  4511. array('option' => array('value' => '3')),
  4512. 'Three',
  4513. '/option',
  4514. array('option' => array('value' => '3x')),
  4515. 'Stringy',
  4516. '/option',
  4517. '/select',
  4518. '/div'
  4519. );
  4520. $this->assertTags($result, $expected);
  4521. }
  4522. /**
  4523. * test generation of habtm select boxes.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testHabtmSelectBox() {
  4528. $this->View->viewVars['contactTags'] = array(
  4529. 1 => 'blue',
  4530. 2 => 'red',
  4531. 3 => 'green'
  4532. );
  4533. $this->Form->request->data = array(
  4534. 'Contact' => array(),
  4535. 'ContactTag' => array(
  4536. array(
  4537. 'id' => '1',
  4538. 'name' => 'blue'
  4539. ),
  4540. array(
  4541. 'id' => 3,
  4542. 'name' => 'green'
  4543. )
  4544. )
  4545. );
  4546. $this->Form->create('Contact');
  4547. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4548. $expected = array(
  4549. 'input' => array(
  4550. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4551. ),
  4552. 'select' => array(
  4553. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4554. 'multiple' => 'multiple'
  4555. ),
  4556. array('option' => array('value' => '1', 'selected' => 'selected')),
  4557. 'blue',
  4558. '/option',
  4559. array('option' => array('value' => '2')),
  4560. 'red',
  4561. '/option',
  4562. array('option' => array('value' => '3', 'selected' => 'selected')),
  4563. 'green',
  4564. '/option',
  4565. '/select'
  4566. );
  4567. $this->assertTags($result, $expected);
  4568. // make sure only 50 is selected, and not 50f5c0cf
  4569. $this->View->viewVars['contactTags'] = array(
  4570. '1' => 'blue',
  4571. '50f5c0cf' => 'red',
  4572. '50' => 'green'
  4573. );
  4574. $this->Form->request->data = array(
  4575. 'Contact' => array(),
  4576. 'ContactTag' => array(
  4577. array(
  4578. 'id' => 1,
  4579. 'name' => 'blue'
  4580. ),
  4581. array(
  4582. 'id' => 50,
  4583. 'name' => 'green'
  4584. )
  4585. )
  4586. );
  4587. $this->Form->create('Contact');
  4588. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4589. $expected = array(
  4590. 'input' => array(
  4591. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4592. ),
  4593. 'select' => array(
  4594. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4595. 'multiple' => 'multiple'
  4596. ),
  4597. array('option' => array('value' => '1', 'selected' => 'selected')),
  4598. 'blue',
  4599. '/option',
  4600. array('option' => array('value' => '50f5c0cf')),
  4601. 'red',
  4602. '/option',
  4603. array('option' => array('value' => '50', 'selected' => 'selected')),
  4604. 'green',
  4605. '/option',
  4606. '/select'
  4607. );
  4608. $this->assertTags($result, $expected);
  4609. }
  4610. /**
  4611. * test generation of multi select elements in checkbox format
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultipleCheckboxes() {
  4616. $result = $this->Form->select(
  4617. 'Model.multi_field',
  4618. array('first', 'second', 'third'),
  4619. array('multiple' => 'checkbox')
  4620. );
  4621. $expected = array(
  4622. 'input' => array(
  4623. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4624. ),
  4625. array('div' => array('class' => 'checkbox')),
  4626. array('input' => array(
  4627. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4628. 'value' => '0', 'id' => 'ModelMultiField0'
  4629. )),
  4630. array('label' => array('for' => 'ModelMultiField0')),
  4631. 'first',
  4632. '/label',
  4633. '/div',
  4634. array('div' => array('class' => 'checkbox')),
  4635. array('input' => array(
  4636. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4637. 'value' => '1', 'id' => 'ModelMultiField1'
  4638. )),
  4639. array('label' => array('for' => 'ModelMultiField1')),
  4640. 'second',
  4641. '/label',
  4642. '/div',
  4643. array('div' => array('class' => 'checkbox')),
  4644. array('input' => array(
  4645. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4646. 'value' => '2', 'id' => 'ModelMultiField2'
  4647. )),
  4648. array('label' => array('for' => 'ModelMultiField2')),
  4649. 'third',
  4650. '/label',
  4651. '/div'
  4652. );
  4653. $this->assertTags($result, $expected);
  4654. $result = $this->Form->select(
  4655. 'Model.multi_field',
  4656. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4657. array('multiple' => 'checkbox')
  4658. );
  4659. $expected = array(
  4660. 'input' => array(
  4661. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4662. ),
  4663. array('div' => array('class' => 'checkbox')),
  4664. array('input' => array(
  4665. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4666. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4667. )),
  4668. array('label' => array('for' => 'ModelMultiFieldA')),
  4669. 'first',
  4670. '/label',
  4671. '/div',
  4672. array('div' => array('class' => 'checkbox')),
  4673. array('input' => array(
  4674. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4675. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4676. )),
  4677. array('label' => array('for' => 'ModelMultiFieldB')),
  4678. 'second',
  4679. '/label',
  4680. '/div',
  4681. array('div' => array('class' => 'checkbox')),
  4682. array('input' => array(
  4683. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4684. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4685. )),
  4686. array('label' => array('for' => 'ModelMultiFieldC')),
  4687. 'third',
  4688. '/label',
  4689. '/div'
  4690. );
  4691. $this->assertTags($result, $expected);
  4692. $result = $this->Form->select(
  4693. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4694. );
  4695. $expected = array(
  4696. 'input' => array(
  4697. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4698. ),
  4699. array('div' => array('class' => 'checkbox')),
  4700. array('input' => array(
  4701. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4702. 'value' => '1', 'id' => 'ModelMultiField1'
  4703. )),
  4704. array('label' => array('for' => 'ModelMultiField1')),
  4705. 'first',
  4706. '/label',
  4707. '/div'
  4708. );
  4709. $this->assertTags($result, $expected);
  4710. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4711. $result = $this->Form->select(
  4712. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4713. );
  4714. $expected = array(
  4715. 'input' => array(
  4716. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4717. ),
  4718. array('div' => array('class' => 'checkbox')),
  4719. array('input' => array(
  4720. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4721. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4722. )),
  4723. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4724. 'first',
  4725. '/label',
  4726. '/div',
  4727. array('div' => array('class' => 'checkbox')),
  4728. array('input' => array(
  4729. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4730. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4731. )),
  4732. array('label' => array('for' => 'ModelTagsArray')),
  4733. 'Array',
  4734. '/label',
  4735. '/div'
  4736. );
  4737. $this->assertTags($result, $expected);
  4738. $result = $this->Form->select(
  4739. 'Model.multi_field',
  4740. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4741. array('multiple' => 'checkbox')
  4742. );
  4743. $expected = array(
  4744. 'input' => array(
  4745. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4746. ),
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array(
  4749. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4750. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4751. )),
  4752. array('label' => array('for' => 'ModelMultiFieldA2')),
  4753. 'first',
  4754. '/label',
  4755. '/div',
  4756. array('div' => array('class' => 'checkbox')),
  4757. array('input' => array(
  4758. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4759. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4760. )),
  4761. array('label' => array('for' => 'ModelMultiFieldA1')),
  4762. 'second',
  4763. '/label',
  4764. '/div',
  4765. array('div' => array('class' => 'checkbox')),
  4766. array('input' => array(
  4767. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4768. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4769. )),
  4770. array('label' => array('for' => 'ModelMultiFieldA')),
  4771. 'third',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->select(
  4777. 'Model.multi_field',
  4778. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4779. array('multiple' => 'checkbox')
  4780. );
  4781. $expected = array(
  4782. 'input' => array(
  4783. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4784. ),
  4785. array('div' => array('class' => 'checkbox')),
  4786. array('input' => array(
  4787. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4788. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4789. )),
  4790. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4791. 'first',
  4792. '/label',
  4793. '/div',
  4794. array('div' => array('class' => 'checkbox')),
  4795. array('input' => array(
  4796. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4797. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4798. )),
  4799. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4800. 'second',
  4801. '/label',
  4802. '/div',
  4803. array('div' => array('class' => 'checkbox')),
  4804. array('input' => array(
  4805. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4806. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4807. )),
  4808. array('label' => array('for' => 'ModelMultiFieldAB')),
  4809. 'third',
  4810. '/label',
  4811. '/div'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. }
  4815. /**
  4816. * test multiple checkboxes with div styles.
  4817. *
  4818. * @return void
  4819. */
  4820. public function testSelectMultipleCheckboxDiv() {
  4821. $result = $this->Form->select(
  4822. 'Model.tags',
  4823. array('first', 'second'),
  4824. array('multiple' => 'checkbox', 'class' => 'my-class')
  4825. );
  4826. $expected = array(
  4827. 'input' => array(
  4828. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4829. ),
  4830. array('div' => array('class' => 'my-class')),
  4831. array('input' => array(
  4832. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4833. 'value' => '0', 'id' => 'ModelTags0'
  4834. )),
  4835. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4836. '/div',
  4837. array('div' => array('class' => 'my-class')),
  4838. array('input' => array(
  4839. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4840. 'value' => '1', 'id' => 'ModelTags1'
  4841. )),
  4842. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4843. '/div'
  4844. );
  4845. $this->assertTags($result, $expected);
  4846. $result = $this->Form->input('Model.tags', array(
  4847. 'options' => array('first', 'second'),
  4848. 'multiple' => 'checkbox',
  4849. 'class' => 'my-class',
  4850. 'div' => false,
  4851. 'label' => false
  4852. ));
  4853. $this->assertTags($result, $expected);
  4854. $Contact = ClassRegistry::getObject('Contact');
  4855. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4856. $result = $this->Form->input('Contact.tags', array(
  4857. 'options' => array('one'),
  4858. 'multiple' => 'checkbox',
  4859. 'label' => false,
  4860. 'div' => false
  4861. ));
  4862. $expected = array(
  4863. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4864. array('div' => array('class' => 'checkbox form-error')),
  4865. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4866. array('label' => array('for' => 'ContactTags0')),
  4867. 'one',
  4868. '/label',
  4869. '/div'
  4870. );
  4871. $this->assertTags($result, $expected);
  4872. $result = $this->Form->input('Contact.tags', array(
  4873. 'options' => array('one'),
  4874. 'multiple' => 'checkbox',
  4875. 'class' => 'mycheckbox',
  4876. 'label' => false,
  4877. 'div' => false
  4878. ));
  4879. $expected = array(
  4880. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4881. array('div' => array('class' => 'mycheckbox form-error')),
  4882. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4883. array('label' => array('for' => 'ContactTags0')),
  4884. 'one',
  4885. '/label',
  4886. '/div'
  4887. );
  4888. $this->assertTags($result, $expected);
  4889. }
  4890. /**
  4891. * Checks the security hash array generated for multiple-input checkbox elements
  4892. *
  4893. * @return void
  4894. */
  4895. public function testSelectMultipleCheckboxSecurity() {
  4896. $this->Form->request['_Token'] = array('key' => 'testKey');
  4897. $this->assertEquals(array(), $this->Form->fields);
  4898. $result = $this->Form->select(
  4899. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4900. array('multiple' => 'checkbox')
  4901. );
  4902. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4903. $result = $this->Form->secure($this->Form->fields);
  4904. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4905. $this->assertRegExp('/"' . $key . '"/', $result);
  4906. }
  4907. /**
  4908. * Multiple select elements should always be secured as they always participate
  4909. * in the POST data.
  4910. *
  4911. * @return void
  4912. */
  4913. public function testSelectMultipleSecureWithNoOptions() {
  4914. $this->Form->request['_Token'] = array('key' => 'testkey');
  4915. $this->assertEquals(array(), $this->Form->fields);
  4916. $this->Form->select(
  4917. 'Model.select',
  4918. array(),
  4919. array('multiple' => true)
  4920. );
  4921. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4922. }
  4923. /**
  4924. * When a select box has no options it should not be added to the fields list
  4925. * as it always fail post validation.
  4926. *
  4927. * @return void
  4928. */
  4929. public function testSelectNoSecureWithNoOptions() {
  4930. $this->Form->request['_Token'] = array('key' => 'testkey');
  4931. $this->assertEquals(array(), $this->Form->fields);
  4932. $this->Form->select(
  4933. 'Model.select',
  4934. array()
  4935. );
  4936. $this->assertEquals(array(), $this->Form->fields);
  4937. $this->Form->select(
  4938. 'Model.select',
  4939. array(),
  4940. array('empty' => true)
  4941. );
  4942. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4943. }
  4944. /**
  4945. * testInputMultipleCheckboxes method
  4946. *
  4947. * test input() resulting in multi select elements being generated.
  4948. *
  4949. * @return void
  4950. */
  4951. public function testInputMultipleCheckboxes() {
  4952. $result = $this->Form->input('Model.multi_field', array(
  4953. 'options' => array('first', 'second', 'third'),
  4954. 'multiple' => 'checkbox'
  4955. ));
  4956. $expected = array(
  4957. array('div' => array('class' => 'input select')),
  4958. array('label' => array('for' => 'ModelMultiField')),
  4959. 'Multi Field',
  4960. '/label',
  4961. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4962. array('div' => array('class' => 'checkbox')),
  4963. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4964. array('label' => array('for' => 'ModelMultiField0')),
  4965. 'first',
  4966. '/label',
  4967. '/div',
  4968. array('div' => array('class' => 'checkbox')),
  4969. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4970. array('label' => array('for' => 'ModelMultiField1')),
  4971. 'second',
  4972. '/label',
  4973. '/div',
  4974. array('div' => array('class' => 'checkbox')),
  4975. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4976. array('label' => array('for' => 'ModelMultiField2')),
  4977. 'third',
  4978. '/label',
  4979. '/div',
  4980. '/div'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $result = $this->Form->input('Model.multi_field', array(
  4984. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4985. 'multiple' => 'checkbox'
  4986. ));
  4987. $expected = array(
  4988. array('div' => array('class' => 'input select')),
  4989. array('label' => array('for' => 'ModelMultiField')),
  4990. 'Multi Field',
  4991. '/label',
  4992. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4993. array('div' => array('class' => 'checkbox')),
  4994. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4995. array('label' => array('for' => 'ModelMultiFieldA')),
  4996. 'first',
  4997. '/label',
  4998. '/div',
  4999. array('div' => array('class' => 'checkbox')),
  5000. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5001. array('label' => array('for' => 'ModelMultiFieldB')),
  5002. 'second',
  5003. '/label',
  5004. '/div',
  5005. array('div' => array('class' => 'checkbox')),
  5006. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5007. array('label' => array('for' => 'ModelMultiFieldC')),
  5008. 'third',
  5009. '/label',
  5010. '/div',
  5011. '/div'
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $result = $this->Form->input('Model.multi_field', array(
  5015. 'options' => array('1' => 'first'),
  5016. 'multiple' => 'checkbox',
  5017. 'label' => false,
  5018. 'div' => false
  5019. ));
  5020. $expected = array(
  5021. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5022. array('div' => array('class' => 'checkbox')),
  5023. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5024. array('label' => array('for' => 'ModelMultiField1')),
  5025. 'first',
  5026. '/label',
  5027. '/div'
  5028. );
  5029. $this->assertTags($result, $expected);
  5030. $result = $this->Form->input('Model.multi_field', array(
  5031. 'options' => array('2' => 'second'),
  5032. 'multiple' => 'checkbox',
  5033. 'label' => false,
  5034. 'div' => false
  5035. ));
  5036. $expected = array(
  5037. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5038. array('div' => array('class' => 'checkbox')),
  5039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5040. array('label' => array('for' => 'ModelMultiField2')),
  5041. 'second',
  5042. '/label',
  5043. '/div'
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. }
  5047. /**
  5048. * testSelectHiddenFieldOmission method
  5049. *
  5050. * test that select() with 'hiddenField' => false omits the hidden field
  5051. *
  5052. * @return void
  5053. */
  5054. public function testSelectHiddenFieldOmission() {
  5055. $result = $this->Form->select('Model.multi_field',
  5056. array('first', 'second'),
  5057. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5058. );
  5059. $expected = array(
  5060. array('div' => array('class' => 'checkbox')),
  5061. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5062. array('label' => array('for' => 'ModelMultiField0')),
  5063. 'first',
  5064. '/label',
  5065. '/div',
  5066. array('div' => array('class' => 'checkbox')),
  5067. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5068. array('label' => array('for' => 'ModelMultiField1')),
  5069. 'second',
  5070. '/label',
  5071. '/div'
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. $result = $this->Form->input('Model.multi_field', array(
  5075. 'options' => array('first', 'second'),
  5076. 'multiple' => 'checkbox',
  5077. 'hiddenField' => false
  5078. ));
  5079. $expected = array(
  5080. array('div' => array('class' => 'input select')),
  5081. array('label' => array('for' => 'ModelMultiField')),
  5082. 'Multi Field',
  5083. '/label',
  5084. array('div' => array('class' => 'checkbox')),
  5085. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5086. array('label' => array('for' => 'ModelMultiField0')),
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. array('div' => array('class' => 'checkbox')),
  5091. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5092. array('label' => array('for' => 'ModelMultiField1')),
  5093. 'second',
  5094. '/label',
  5095. '/div',
  5096. '/div'
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. }
  5100. /**
  5101. * test that select() with multiple = checkbox works with overriding name attribute.
  5102. *
  5103. * @return void
  5104. */
  5105. public function testSelectCheckboxMultipleOverrideName() {
  5106. $result = $this->Form->input('category', array(
  5107. 'type' => 'select',
  5108. 'multiple' => 'checkbox',
  5109. 'name' => 'data[fish]',
  5110. 'options' => array('1', '2'),
  5111. 'div' => false,
  5112. 'label' => false,
  5113. ));
  5114. $expected = array(
  5115. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5116. array('div' => array('class' => 'checkbox')),
  5117. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5118. array('label' => array('for' => 'Category0')), '1', '/label',
  5119. '/div',
  5120. array('div' => array('class' => 'checkbox')),
  5121. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5122. array('label' => array('for' => 'Category1')), '2', '/label',
  5123. '/div'
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. }
  5127. /**
  5128. * Test that 'id' overrides all the checkbox id's as well.
  5129. *
  5130. * @return void
  5131. */
  5132. public function testSelectCheckboxMultipleId() {
  5133. $result = $this->Form->select(
  5134. 'Model.multi_field',
  5135. array('first', 'second', 'third'),
  5136. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5137. );
  5138. $expected = array(
  5139. 'input' => array(
  5140. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5141. ),
  5142. array('div' => array('class' => 'checkbox')),
  5143. array('input' => array(
  5144. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5145. 'value' => '0', 'id' => 'CustomId0'
  5146. )),
  5147. array('label' => array('for' => 'CustomId0')),
  5148. 'first',
  5149. '/label',
  5150. '/div',
  5151. array('div' => array('class' => 'checkbox')),
  5152. array('input' => array(
  5153. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5154. 'value' => '1', 'id' => 'CustomId1'
  5155. )),
  5156. array('label' => array('for' => 'CustomId1')),
  5157. 'second',
  5158. '/label',
  5159. '/div',
  5160. array('div' => array('class' => 'checkbox')),
  5161. array('input' => array(
  5162. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5163. 'value' => '2', 'id' => 'CustomId2'
  5164. )),
  5165. array('label' => array('for' => 'CustomId2')),
  5166. 'third',
  5167. '/label',
  5168. '/div'
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. }
  5172. /**
  5173. * testCheckbox method
  5174. *
  5175. * Test generation of checkboxes
  5176. *
  5177. * @return void
  5178. */
  5179. public function testCheckbox() {
  5180. $result = $this->Form->checkbox('Model.field');
  5181. $expected = array(
  5182. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5183. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5187. $expected = array(
  5188. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5189. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. $Contact = ClassRegistry::getObject('Contact');
  5193. $Contact->validationErrors['field'] = 1;
  5194. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5195. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5196. $expected = array(
  5197. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5198. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5199. );
  5200. $this->assertTags($result, $expected);
  5201. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5202. $expected = array(
  5203. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5204. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Contact']['field'] = '';
  5208. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5209. $expected = array(
  5210. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5211. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. $Contact->validationErrors = array();
  5215. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5216. $expected = array(
  5217. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5218. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $this->Form->request->data['Contact']['published'] = 1;
  5222. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5223. $expected = array(
  5224. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5225. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->Form->request->data['Contact']['published'] = 0;
  5229. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5230. $expected = array(
  5231. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5232. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5236. $expected = array(
  5237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. $result = $this->Form->checkbox('CustomField.1.value');
  5242. $expected = array(
  5243. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5244. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5245. );
  5246. $this->assertTags($result, $expected);
  5247. }
  5248. /**
  5249. * test checkbox() with a custom name attribute
  5250. *
  5251. * @return void
  5252. */
  5253. public function testCheckboxCustomNameAttribute() {
  5254. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5255. $expected = array(
  5256. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5257. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. }
  5261. /**
  5262. * test the checked option for checkboxes.
  5263. *
  5264. * @return void
  5265. */
  5266. public function testCheckboxCheckedOption() {
  5267. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5268. $expected = array(
  5269. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5270. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5274. $expected = array(
  5275. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5276. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5277. );
  5278. $this->assertTags($result, $expected);
  5279. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5280. $expected = array(
  5281. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5282. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5286. $expected = array(
  5287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5288. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $this->Form->request->data['Model']['field'] = 1;
  5292. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5293. $expected = array(
  5294. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5295. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5296. );
  5297. $this->assertTags($result, $expected);
  5298. }
  5299. /**
  5300. * Test that disabled attribute works on both the checkbox and hidden input.
  5301. *
  5302. * @return void
  5303. */
  5304. public function testCheckboxDisabling() {
  5305. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5306. $expected = array(
  5307. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5308. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5312. $expected = array(
  5313. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5314. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5315. );
  5316. $this->assertTags($result, $expected);
  5317. }
  5318. /**
  5319. * Test that the hidden input for checkboxes can be omitted or set to a
  5320. * specific value.
  5321. *
  5322. * @return void
  5323. */
  5324. public function testCheckboxHiddenField() {
  5325. $result = $this->Form->input('UserForm.something', array(
  5326. 'type' => 'checkbox',
  5327. 'hiddenField' => false
  5328. ));
  5329. $expected = array(
  5330. 'div' => array('class' => 'input checkbox'),
  5331. array('input' => array(
  5332. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5333. 'value' => '1', 'id' => 'UserFormSomething'
  5334. )),
  5335. 'label' => array('for' => 'UserFormSomething'),
  5336. 'Something',
  5337. '/label',
  5338. '/div'
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $result = $this->Form->input('UserForm.something', array(
  5342. 'type' => 'checkbox',
  5343. 'value' => 'Y',
  5344. 'hiddenField' => 'N',
  5345. ));
  5346. $expected = array(
  5347. 'div' => array('class' => 'input checkbox'),
  5348. array('input' => array(
  5349. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5350. 'value' => 'N', 'id' => 'UserFormSomething_'
  5351. )),
  5352. array('input' => array(
  5353. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5354. 'value' => 'Y', 'id' => 'UserFormSomething'
  5355. )),
  5356. 'label' => array('for' => 'UserFormSomething'),
  5357. 'Something',
  5358. '/label',
  5359. '/div'
  5360. );
  5361. $this->assertTags($result, $expected);
  5362. }
  5363. /**
  5364. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5365. *
  5366. * @return void
  5367. */
  5368. public function testCheckboxZeroValue() {
  5369. $result = $this->Form->input('User.get_spam', array(
  5370. 'type' => 'checkbox',
  5371. 'value' => '0',
  5372. 'hiddenField' => '1',
  5373. ));
  5374. $expected = array(
  5375. 'div' => array('class' => 'input checkbox'),
  5376. array('input' => array(
  5377. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5378. 'value' => '1', 'id' => 'UserGetSpam_'
  5379. )),
  5380. array('input' => array(
  5381. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5382. 'value' => '0', 'id' => 'UserGetSpam'
  5383. )),
  5384. 'label' => array('for' => 'UserGetSpam'),
  5385. 'Get Spam',
  5386. '/label',
  5387. '/div'
  5388. );
  5389. $this->assertTags($result, $expected);
  5390. }
  5391. /**
  5392. * testDateTime method
  5393. *
  5394. * Test generation of date/time select elements
  5395. *
  5396. * @return void
  5397. */
  5398. public function testDateTime() {
  5399. extract($this->dateRegex);
  5400. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5401. $now = strtotime('now');
  5402. $expected = array(
  5403. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5404. $daysRegex,
  5405. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5406. date('j', $now),
  5407. '/option',
  5408. '*/select',
  5409. '-',
  5410. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5411. $monthsRegex,
  5412. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5413. date('F', $now),
  5414. '/option',
  5415. '*/select',
  5416. '-',
  5417. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5418. $yearsRegex,
  5419. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5420. date('Y', $now),
  5421. '/option',
  5422. '*/select',
  5423. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5424. $hoursRegex,
  5425. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5426. date('g', $now),
  5427. '/option',
  5428. '*/select',
  5429. ':',
  5430. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5431. $minutesRegex,
  5432. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5433. date('i', $now),
  5434. '/option',
  5435. '*/select',
  5436. ' ',
  5437. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5438. $meridianRegex,
  5439. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5440. date('a', $now),
  5441. '/option',
  5442. '*/select'
  5443. );
  5444. $this->assertTags($result, $expected);
  5445. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5446. $expected = array(
  5447. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5448. $daysRegex,
  5449. array('option' => array('value' => '')),
  5450. '/option',
  5451. '*/select',
  5452. '-',
  5453. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5454. $monthsRegex,
  5455. array('option' => array('value' => '')),
  5456. '/option',
  5457. '*/select',
  5458. '-',
  5459. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5460. $yearsRegex,
  5461. array('option' => array('value' => '')),
  5462. '/option',
  5463. '*/select',
  5464. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5465. $hoursRegex,
  5466. array('option' => array('value' => '')),
  5467. '/option',
  5468. '*/select',
  5469. ':',
  5470. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5471. $minutesRegex,
  5472. array('option' => array('value' => '')),
  5473. '/option',
  5474. '*/select',
  5475. ' ',
  5476. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5477. $meridianRegex,
  5478. array('option' => array('value' => '')),
  5479. '/option',
  5480. '*/select'
  5481. );
  5482. $this->assertTags($result, $expected);
  5483. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5484. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5485. $this->assertTags($result, $expected);
  5486. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5487. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5488. $this->assertTags($result, $expected);
  5489. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5490. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5491. $expected = array(
  5492. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5493. $daysRegex,
  5494. array('option' => array('value' => '')),
  5495. '/option',
  5496. '*/select',
  5497. '-',
  5498. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5499. $monthsRegex,
  5500. array('option' => array('value' => '')),
  5501. '/option',
  5502. '*/select',
  5503. '-',
  5504. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5505. $yearsRegex,
  5506. array('option' => array('value' => '')),
  5507. '/option',
  5508. '*/select',
  5509. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5510. $hoursRegex,
  5511. array('option' => array('value' => '')),
  5512. '/option',
  5513. '*/select',
  5514. ':',
  5515. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5516. $minutesRegex,
  5517. array('option' => array('value' => '')),
  5518. '/option',
  5519. array('option' => array('value' => '00')),
  5520. '00',
  5521. '/option',
  5522. array('option' => array('value' => '05')),
  5523. '05',
  5524. '/option',
  5525. array('option' => array('value' => '10')),
  5526. '10',
  5527. '/option',
  5528. '*/select',
  5529. ' ',
  5530. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5531. $meridianRegex,
  5532. array('option' => array('value' => '')),
  5533. '/option',
  5534. '*/select'
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5538. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5539. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5540. $this->Form->request->data['Contact']['data'] = null;
  5541. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5542. $expected = array(
  5543. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5544. $daysRegex,
  5545. array('option' => array('value' => '')),
  5546. '/option',
  5547. '*/select',
  5548. '-',
  5549. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5550. $monthsRegex,
  5551. array('option' => array('value' => '')),
  5552. '/option',
  5553. '*/select',
  5554. '-',
  5555. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5556. $yearsRegex,
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. '*/select',
  5560. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5561. $hoursRegex,
  5562. array('option' => array('value' => '')),
  5563. '/option',
  5564. '*/select',
  5565. ':',
  5566. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5567. $minutesRegex,
  5568. array('option' => array('value' => '')),
  5569. '/option',
  5570. '*/select',
  5571. ' ',
  5572. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5573. $meridianRegex,
  5574. array('option' => array('value' => '')),
  5575. '/option',
  5576. '*/select'
  5577. );
  5578. $this->assertTags($result, $expected);
  5579. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5580. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5581. $now = strtotime($this->Form->data['Model']['field']);
  5582. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5583. $expected = array(
  5584. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5585. $daysRegex,
  5586. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5587. date('j', $now),
  5588. '/option',
  5589. '*/select',
  5590. '-',
  5591. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5592. $monthsRegex,
  5593. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5594. date('F', $now),
  5595. '/option',
  5596. '*/select',
  5597. '-',
  5598. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5599. $yearsRegex,
  5600. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5601. date('Y', $now),
  5602. '/option',
  5603. '*/select',
  5604. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5605. $hoursRegex,
  5606. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5607. date('g', $now),
  5608. '/option',
  5609. '*/select',
  5610. ':',
  5611. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5612. $minutesRegex,
  5613. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5614. date('i', $now),
  5615. '/option',
  5616. '*/select',
  5617. ' ',
  5618. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5619. $meridianRegex,
  5620. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5621. date('a', $now),
  5622. '/option',
  5623. '*/select'
  5624. );
  5625. $this->assertTags($result, $expected);
  5626. $selected = strtotime('2008-10-26 12:33:00');
  5627. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5628. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5629. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5630. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5631. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5632. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5633. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5634. $this->Form->create('Contact');
  5635. $result = $this->Form->input('published');
  5636. $now = strtotime('now');
  5637. $expected = array(
  5638. 'div' => array('class' => 'input date'),
  5639. 'label' => array('for' => 'ContactPublishedMonth'),
  5640. 'Published',
  5641. '/label',
  5642. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5643. $monthsRegex,
  5644. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5645. date('F', $now),
  5646. '/option',
  5647. '*/select',
  5648. '-',
  5649. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5650. $daysRegex,
  5651. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5652. date('j', $now),
  5653. '/option',
  5654. '*/select',
  5655. '-',
  5656. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5657. $yearsRegex,
  5658. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5659. date('Y', $now),
  5660. '/option',
  5661. '*/select',
  5662. '/div'
  5663. );
  5664. $this->assertTags($result, $expected);
  5665. $result = $this->Form->input('published2', array('type' => 'date'));
  5666. $now = strtotime('now');
  5667. $expected = array(
  5668. 'div' => array('class' => 'input date'),
  5669. 'label' => array('for' => 'ContactPublished2Month'),
  5670. 'Published2',
  5671. '/label',
  5672. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5673. $monthsRegex,
  5674. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5675. date('F', $now),
  5676. '/option',
  5677. '*/select',
  5678. '-',
  5679. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5680. $daysRegex,
  5681. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5682. date('j', $now),
  5683. '/option',
  5684. '*/select',
  5685. '-',
  5686. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5687. $yearsRegex,
  5688. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5689. date('Y', $now),
  5690. '/option',
  5691. '*/select',
  5692. '/div'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $this->Form->create('Contact');
  5696. $result = $this->Form->input('published', array('monthNames' => false));
  5697. $now = strtotime('now');
  5698. $expected = array(
  5699. 'div' => array('class' => 'input date'),
  5700. 'label' => array('for' => 'ContactPublishedMonth'),
  5701. 'Published',
  5702. '/label',
  5703. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5704. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5705. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5706. date('m', $now),
  5707. '/option',
  5708. '*/select',
  5709. '-',
  5710. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5711. $daysRegex,
  5712. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5713. date('j', $now),
  5714. '/option',
  5715. '*/select',
  5716. '-',
  5717. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5718. $yearsRegex,
  5719. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5720. date('Y', $now),
  5721. '/option',
  5722. '*/select',
  5723. '/div'
  5724. );
  5725. $this->assertTags($result, $expected);
  5726. $result = $this->Form->input('published', array(
  5727. 'timeFormat' => 24,
  5728. 'interval' => 5,
  5729. 'selected' => strtotime('2009-09-03 13:37:00'),
  5730. 'type' => 'datetime'
  5731. ));
  5732. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5733. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5734. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5735. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5736. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5737. }
  5738. /**
  5739. * Test dateTime with rounding
  5740. *
  5741. * @return void
  5742. */
  5743. public function testDateTimeRounding() {
  5744. $this->Form->request->data['Contact'] = array(
  5745. 'date' => array(
  5746. 'day' => '13',
  5747. 'month' => '12',
  5748. 'year' => '2010',
  5749. 'hour' => '04',
  5750. 'min' => '19',
  5751. 'meridian' => 'AM'
  5752. )
  5753. );
  5754. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5755. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5756. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5757. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5758. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5759. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5760. }
  5761. /**
  5762. * Test that empty values don't trigger errors.
  5763. *
  5764. * @return void
  5765. */
  5766. public function testDateTimeNoErrorsOnEmptyData() {
  5767. $this->Form->request->data['Contact'] = array(
  5768. 'date' => array(
  5769. 'day' => '',
  5770. 'month' => '',
  5771. 'year' => '',
  5772. 'hour' => '',
  5773. 'min' => '',
  5774. 'meridian' => ''
  5775. )
  5776. );
  5777. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5778. $this->assertNotEmpty($result);
  5779. }
  5780. /**
  5781. * test that datetime() and default values work.
  5782. *
  5783. * @return void
  5784. */
  5785. public function testDatetimeWithDefault() {
  5786. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5787. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5788. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5789. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5790. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5791. 'default' => '2009-06-01 11:15:30'
  5792. ));
  5793. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5794. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5795. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5796. }
  5797. /**
  5798. * test that bogus non-date time data doesn't cause errors.
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDateTimeWithBogusData() {
  5803. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5804. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5805. }
  5806. /**
  5807. * testDateTime all zeros
  5808. *
  5809. * @return void
  5810. */
  5811. public function testDateTimeAllZeros() {
  5812. $result = $this->Form->dateTime('Contact.date',
  5813. 'DMY',
  5814. false,
  5815. array(
  5816. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5817. 'value' => '0000-00-00'
  5818. )
  5819. );
  5820. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5821. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5822. }
  5823. /**
  5824. * testDateTimeEmptyAsArray
  5825. *
  5826. * @return void
  5827. */
  5828. public function testDateTimeEmptyAsArray() {
  5829. $result = $this->Form->dateTime('Contact.date',
  5830. 'DMY',
  5831. '12',
  5832. array(
  5833. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5834. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5835. )
  5836. )
  5837. );
  5838. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5839. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5840. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5841. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5842. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5843. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5844. $result = $this->Form->dateTime('Contact.date',
  5845. 'DMY',
  5846. '12',
  5847. array(
  5848. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5849. )
  5850. );
  5851. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5852. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5853. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5854. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5855. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5856. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5857. }
  5858. /**
  5859. * testFormDateTimeMulti method
  5860. *
  5861. * test multiple datetime element generation
  5862. *
  5863. * @return void
  5864. */
  5865. public function testFormDateTimeMulti() {
  5866. extract($this->dateRegex);
  5867. $result = $this->Form->dateTime('Contact.1.updated');
  5868. $expected = array(
  5869. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5870. $daysRegex,
  5871. array('option' => array('value' => '')),
  5872. '/option',
  5873. '*/select',
  5874. '-',
  5875. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5876. $monthsRegex,
  5877. array('option' => array('value' => '')),
  5878. '/option',
  5879. '*/select',
  5880. '-',
  5881. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5882. $yearsRegex,
  5883. array('option' => array('value' => '')),
  5884. '/option',
  5885. '*/select',
  5886. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5887. $hoursRegex,
  5888. array('option' => array('value' => '')),
  5889. '/option',
  5890. '*/select',
  5891. ':',
  5892. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5893. $minutesRegex,
  5894. array('option' => array('value' => '')),
  5895. '/option',
  5896. '*/select',
  5897. ' ',
  5898. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5899. $meridianRegex,
  5900. array('option' => array('value' => '')),
  5901. '/option',
  5902. '*/select'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->dateTime('Contact.2.updated');
  5906. $expected = array(
  5907. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5908. $daysRegex,
  5909. array('option' => array('value' => '')),
  5910. '/option',
  5911. '*/select',
  5912. '-',
  5913. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5914. $monthsRegex,
  5915. array('option' => array('value' => '')),
  5916. '/option',
  5917. '*/select',
  5918. '-',
  5919. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5920. $yearsRegex,
  5921. array('option' => array('value' => '')),
  5922. '/option',
  5923. '*/select',
  5924. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5925. $hoursRegex,
  5926. array('option' => array('value' => '')),
  5927. '/option',
  5928. '*/select',
  5929. ':',
  5930. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5931. $minutesRegex,
  5932. array('option' => array('value' => '')),
  5933. '/option',
  5934. '*/select',
  5935. ' ',
  5936. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5937. $meridianRegex,
  5938. array('option' => array('value' => '')),
  5939. '/option',
  5940. '*/select'
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. }
  5944. /**
  5945. * When changing the date format, the label should always focus the first select box when
  5946. * clicked.
  5947. *
  5948. * @return void
  5949. */
  5950. public function testDateTimeLabelIdMatchesFirstInput() {
  5951. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5952. $this->assertContains('label for="ModelDateMonth"', $result);
  5953. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5954. $this->assertContains('label for="ModelDateDay"', $result);
  5955. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5956. $this->assertContains('label for="ModelDateYear"', $result);
  5957. }
  5958. /**
  5959. * testMonth method
  5960. *
  5961. * @return void
  5962. */
  5963. public function testMonth() {
  5964. $result = $this->Form->month('Model.field');
  5965. $expected = array(
  5966. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5967. array('option' => array('value' => '')),
  5968. '/option',
  5969. array('option' => array('value' => '01')),
  5970. date('F', strtotime('2008-01-01 00:00:00')),
  5971. '/option',
  5972. array('option' => array('value' => '02')),
  5973. date('F', strtotime('2008-02-01 00:00:00')),
  5974. '/option',
  5975. '*/select',
  5976. );
  5977. $this->assertTags($result, $expected);
  5978. $result = $this->Form->month('Model.field', array('empty' => true));
  5979. $expected = array(
  5980. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5981. array('option' => array('value' => '')),
  5982. '/option',
  5983. array('option' => array('value' => '01')),
  5984. date('F', strtotime('2008-01-01 00:00:00')),
  5985. '/option',
  5986. array('option' => array('value' => '02')),
  5987. date('F', strtotime('2008-02-01 00:00:00')),
  5988. '/option',
  5989. '*/select',
  5990. );
  5991. $this->assertTags($result, $expected);
  5992. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5993. $expected = array(
  5994. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5995. array('option' => array('value' => '')),
  5996. '/option',
  5997. array('option' => array('value' => '01')),
  5998. '01',
  5999. '/option',
  6000. array('option' => array('value' => '02')),
  6001. '02',
  6002. '/option',
  6003. '*/select',
  6004. );
  6005. $this->assertTags($result, $expected);
  6006. $monthNames = array(
  6007. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6008. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6009. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6010. $expected = array(
  6011. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6012. array('option' => array('value' => '')),
  6013. '/option',
  6014. array('option' => array('value' => '01')),
  6015. 'Jan',
  6016. '/option',
  6017. array('option' => array('value' => '02')),
  6018. 'Feb',
  6019. '/option',
  6020. '*/select',
  6021. );
  6022. $this->assertTags($result, $expected);
  6023. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6024. $result = $this->Form->month('Project.release');
  6025. $expected = array(
  6026. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6027. array('option' => array('value' => '')),
  6028. '/option',
  6029. array('option' => array('value' => '01')),
  6030. 'January',
  6031. '/option',
  6032. array('option' => array('value' => '02', 'selected' => 'selected')),
  6033. 'February',
  6034. '/option',
  6035. '*/select',
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. }
  6039. /**
  6040. * testDay method
  6041. *
  6042. * @return void
  6043. */
  6044. public function testDay() {
  6045. extract($this->dateRegex);
  6046. $result = $this->Form->day('Model.field', array('value' => false));
  6047. $expected = array(
  6048. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6049. array('option' => array('value' => '')),
  6050. '/option',
  6051. array('option' => array('value' => '01')),
  6052. '1',
  6053. '/option',
  6054. array('option' => array('value' => '02')),
  6055. '2',
  6056. '/option',
  6057. $daysRegex,
  6058. '/select',
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6062. $result = $this->Form->day('Model.field');
  6063. $expected = array(
  6064. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6065. array('option' => array('value' => '')),
  6066. '/option',
  6067. array('option' => array('value' => '01')),
  6068. '1',
  6069. '/option',
  6070. array('option' => array('value' => '02')),
  6071. '2',
  6072. '/option',
  6073. $daysRegex,
  6074. array('option' => array('value' => '10', 'selected' => 'selected')),
  6075. '10',
  6076. '/option',
  6077. $daysRegex,
  6078. '/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->Form->request->data['Model']['field'] = '';
  6082. $result = $this->Form->day('Model.field', array('value' => '10'));
  6083. $expected = array(
  6084. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6085. array('option' => array('value' => '')),
  6086. '/option',
  6087. array('option' => array('value' => '01')),
  6088. '1',
  6089. '/option',
  6090. array('option' => array('value' => '02')),
  6091. '2',
  6092. '/option',
  6093. $daysRegex,
  6094. array('option' => array('value' => '10', 'selected' => 'selected')),
  6095. '10',
  6096. '/option',
  6097. $daysRegex,
  6098. '/select',
  6099. );
  6100. $this->assertTags($result, $expected);
  6101. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6102. $result = $this->Form->day('Model.field', array('value' => true));
  6103. $expected = array(
  6104. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6105. array('option' => array('value' => '')),
  6106. '/option',
  6107. array('option' => array('value' => '01')),
  6108. '1',
  6109. '/option',
  6110. array('option' => array('value' => '02')),
  6111. '2',
  6112. '/option',
  6113. $daysRegex,
  6114. array('option' => array('value' => '10', 'selected' => 'selected')),
  6115. '10',
  6116. '/option',
  6117. $daysRegex,
  6118. '/select',
  6119. );
  6120. $this->assertTags($result, $expected);
  6121. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6122. $result = $this->Form->day('Project.release');
  6123. $expected = array(
  6124. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6125. array('option' => array('value' => '')),
  6126. '/option',
  6127. array('option' => array('value' => '01')),
  6128. '1',
  6129. '/option',
  6130. array('option' => array('value' => '02')),
  6131. '2',
  6132. '/option',
  6133. $daysRegex,
  6134. array('option' => array('value' => '10', 'selected' => 'selected')),
  6135. '10',
  6136. '/option',
  6137. $daysRegex,
  6138. '/select',
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. }
  6142. /**
  6143. * testMinute method
  6144. *
  6145. * @return void
  6146. */
  6147. public function testMinute() {
  6148. extract($this->dateRegex);
  6149. $result = $this->Form->minute('Model.field');
  6150. $expected = array(
  6151. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6152. array('option' => array('value' => '')),
  6153. '/option',
  6154. array('option' => array('value' => '00')),
  6155. '00',
  6156. '/option',
  6157. array('option' => array('value' => '01')),
  6158. '01',
  6159. '/option',
  6160. array('option' => array('value' => '02')),
  6161. '02',
  6162. '/option',
  6163. $minutesRegex,
  6164. '/select',
  6165. );
  6166. $this->assertTags($result, $expected);
  6167. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6168. $result = $this->Form->minute('Model.field');
  6169. $expected = array(
  6170. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6171. array('option' => array('value' => '')),
  6172. '/option',
  6173. array('option' => array('value' => '00')),
  6174. '00',
  6175. '/option',
  6176. array('option' => array('value' => '01')),
  6177. '01',
  6178. '/option',
  6179. array('option' => array('value' => '02')),
  6180. '02',
  6181. '/option',
  6182. $minutesRegex,
  6183. array('option' => array('value' => '12', 'selected' => 'selected')),
  6184. '12',
  6185. '/option',
  6186. $minutesRegex,
  6187. '/select',
  6188. );
  6189. $this->assertTags($result, $expected);
  6190. $this->Form->request->data['Model']['field'] = '';
  6191. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6192. $expected = array(
  6193. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6194. array('option' => array('value' => '')),
  6195. '/option',
  6196. array('option' => array('value' => '00')),
  6197. '00',
  6198. '/option',
  6199. array('option' => array('value' => '05')),
  6200. '05',
  6201. '/option',
  6202. array('option' => array('value' => '10')),
  6203. '10',
  6204. '/option',
  6205. $minutesRegex,
  6206. '/select',
  6207. );
  6208. $this->assertTags($result, $expected);
  6209. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6210. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6211. $expected = array(
  6212. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6213. array('option' => array('value' => '')),
  6214. '/option',
  6215. array('option' => array('value' => '00')),
  6216. '00',
  6217. '/option',
  6218. array('option' => array('value' => '05')),
  6219. '05',
  6220. '/option',
  6221. array('option' => array('value' => '10', 'selected' => 'selected')),
  6222. '10',
  6223. '/option',
  6224. $minutesRegex,
  6225. '/select',
  6226. );
  6227. $this->assertTags($result, $expected);
  6228. }
  6229. /**
  6230. * testHour method
  6231. *
  6232. * @return void
  6233. */
  6234. public function testHour() {
  6235. extract($this->dateRegex);
  6236. $result = $this->Form->hour('Model.field', false);
  6237. $expected = array(
  6238. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6239. array('option' => array('value' => '')),
  6240. '/option',
  6241. array('option' => array('value' => '01')),
  6242. '1',
  6243. '/option',
  6244. array('option' => array('value' => '02')),
  6245. '2',
  6246. '/option',
  6247. $hoursRegex,
  6248. '/select',
  6249. );
  6250. $this->assertTags($result, $expected);
  6251. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6252. $result = $this->Form->hour('Model.field', false);
  6253. $expected = array(
  6254. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6255. array('option' => array('value' => '')),
  6256. '/option',
  6257. array('option' => array('value' => '01')),
  6258. '1',
  6259. '/option',
  6260. array('option' => array('value' => '02')),
  6261. '2',
  6262. '/option',
  6263. $hoursRegex,
  6264. array('option' => array('value' => '12', 'selected' => 'selected')),
  6265. '12',
  6266. '/option',
  6267. '/select',
  6268. );
  6269. $this->assertTags($result, $expected);
  6270. $this->Form->request->data['Model']['field'] = '';
  6271. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6272. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6273. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6274. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6275. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6276. $result = $this->Form->hour('Model.field', true);
  6277. $expected = array(
  6278. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6279. array('option' => array('value' => '')),
  6280. '/option',
  6281. array('option' => array('value' => '00', 'selected' => 'selected')),
  6282. '0',
  6283. '/option',
  6284. array('option' => array('value' => '01')),
  6285. '1',
  6286. '/option',
  6287. array('option' => array('value' => '02')),
  6288. '2',
  6289. '/option',
  6290. $hoursRegex,
  6291. '/select',
  6292. );
  6293. $this->assertTags($result, $expected);
  6294. unset($this->Form->request->data['Model']['field']);
  6295. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6296. $thisHour = date('H');
  6297. $optValue = date('G');
  6298. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6299. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6300. $result = $this->Form->hour('Model.field', true);
  6301. $expected = array(
  6302. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6303. array('option' => array('value' => '')),
  6304. '/option',
  6305. array('option' => array('value' => '00')),
  6306. '0',
  6307. '/option',
  6308. array('option' => array('value' => '01', 'selected' => 'selected')),
  6309. '1',
  6310. '/option',
  6311. array('option' => array('value' => '02')),
  6312. '2',
  6313. '/option',
  6314. $hoursRegex,
  6315. '/select',
  6316. );
  6317. $this->assertTags($result, $expected);
  6318. }
  6319. /**
  6320. * testYear method
  6321. *
  6322. * @return void
  6323. */
  6324. public function testYear() {
  6325. $result = $this->Form->year('Model.field', 2006, 2007);
  6326. $expected = array(
  6327. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6328. array('option' => array('value' => '')),
  6329. '/option',
  6330. array('option' => array('value' => '2007')),
  6331. '2007',
  6332. '/option',
  6333. array('option' => array('value' => '2006')),
  6334. '2006',
  6335. '/option',
  6336. '/select',
  6337. );
  6338. $this->assertTags($result, $expected);
  6339. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6340. $expected = array(
  6341. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6342. array('option' => array('value' => '')),
  6343. '/option',
  6344. array('option' => array('value' => '2006')),
  6345. '2006',
  6346. '/option',
  6347. array('option' => array('value' => '2007')),
  6348. '2007',
  6349. '/option',
  6350. '/select',
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $this->request->data['Contact']['published'] = '';
  6354. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6355. $expected = array(
  6356. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6357. array('option' => array('value' => '')),
  6358. '/option',
  6359. array('option' => array('value' => '2007')),
  6360. '2007',
  6361. '/option',
  6362. array('option' => array('value' => '2006')),
  6363. '2006',
  6364. '/option',
  6365. '/select',
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6369. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6370. $expected = array(
  6371. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6372. array('option' => array('value' => '2007')),
  6373. '2007',
  6374. '/option',
  6375. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6376. '2006',
  6377. '/option',
  6378. '/select',
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. $this->Form->request->data['Contact']['published'] = '';
  6382. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6383. $expected = array(
  6384. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6385. array('option' => array('value' => '')),
  6386. '/option',
  6387. array('option' => array('value' => '2007')),
  6388. '2007',
  6389. '/option',
  6390. array('option' => array('value' => '2006')),
  6391. '2006',
  6392. '/option',
  6393. '/select',
  6394. );
  6395. $this->assertTags($result, $expected);
  6396. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6397. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6398. $expected = array(
  6399. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6400. array('option' => array('value' => '2007')),
  6401. '2007',
  6402. '/option',
  6403. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6404. '2006',
  6405. '/option',
  6406. '/select',
  6407. );
  6408. $this->assertTags($result, $expected);
  6409. $this->Form->request->data['Contact']['published'] = '';
  6410. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6411. $expected = array(
  6412. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6413. array('option' => array('value' => '')),
  6414. '/option',
  6415. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6416. '2007',
  6417. '/option',
  6418. array('option' => array('value' => '2006')),
  6419. '2006',
  6420. '/option',
  6421. '/select',
  6422. );
  6423. $this->assertTags($result, $expected);
  6424. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6425. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6426. $expected = array(
  6427. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6428. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6429. '2007',
  6430. '/option',
  6431. array('option' => array('value' => '2006')),
  6432. '2006',
  6433. '/option',
  6434. '/select',
  6435. );
  6436. $this->assertTags($result, $expected);
  6437. $this->Form->request->data['Contact']['published'] = '';
  6438. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6439. $expected = array(
  6440. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6441. array('option' => array('value' => '2008')),
  6442. '2008',
  6443. '/option',
  6444. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6445. '2007',
  6446. '/option',
  6447. array('option' => array('value' => '2006')),
  6448. '2006',
  6449. '/option',
  6450. '/select',
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6454. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6455. $expected = array(
  6456. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6457. array('option' => array('value' => '2008')),
  6458. '2008',
  6459. '/option',
  6460. array('option' => array('value' => '2007')),
  6461. '2007',
  6462. '/option',
  6463. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6464. '2006',
  6465. '/option',
  6466. '/select',
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. $this->Form->request->data = array();
  6470. $this->Form->create('Contact');
  6471. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6472. $expected = array(
  6473. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6474. array('option' => array('value' => '2008')),
  6475. '2008',
  6476. '/option',
  6477. array('option' => array('value' => '2007')),
  6478. '2007',
  6479. '/option',
  6480. array('option' => array('value' => '2006')),
  6481. '2006',
  6482. '/option',
  6483. '/select',
  6484. );
  6485. $this->assertTags($result, $expected);
  6486. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6487. $this->assertContains('data[Contact][published][year]', $result);
  6488. }
  6489. /**
  6490. * testYearAutoExpandRange method
  6491. *
  6492. * @return void
  6493. */
  6494. public function testYearAutoExpandRange() {
  6495. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6496. $result = $this->Form->year('User.birthday');
  6497. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6498. $result = $matches[1];
  6499. $expected = range(date('Y') + 20, 1930);
  6500. $this->assertEquals($expected, $result);
  6501. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6502. $result = $this->Form->year('Project.release');
  6503. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6504. $result = $matches[1];
  6505. $expected = range(2050, date('Y') - 20);
  6506. $this->assertEquals($expected, $result);
  6507. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6508. $result = $this->Form->year('Project.release', 1890, 1900);
  6509. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6510. $result = $matches[1];
  6511. $expected = range(1900, 1881);
  6512. $this->assertEquals($expected, $result);
  6513. }
  6514. /**
  6515. * testInputDate method
  6516. *
  6517. * Test various inputs with type date and different dateFormat values.
  6518. * Failing to provide a dateFormat key should not error.
  6519. * It should simply not pre-select any value then.
  6520. *
  6521. * @return void
  6522. */
  6523. public function testInputDate() {
  6524. $this->Form->request->data = array(
  6525. 'User' => array(
  6526. 'month_year' => array('month' => date('m')),
  6527. 'just_year' => array('month' => date('m')),
  6528. 'just_month' => array('year' => date('Y')),
  6529. 'just_day' => array('month' => date('m')),
  6530. )
  6531. );
  6532. $this->Form->create('User');
  6533. $result = $this->Form->input('month_year',
  6534. array(
  6535. 'label' => false,
  6536. 'div' => false,
  6537. 'type' => 'date',
  6538. 'dateFormat' => 'MY',
  6539. 'minYear' => 2006,
  6540. 'maxYear' => 2008
  6541. )
  6542. );
  6543. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6544. $this->assertNotContains('value="2008" selected="selected"', $result);
  6545. $result = $this->Form->input('just_year',
  6546. array(
  6547. 'type' => 'date',
  6548. 'label' => false,
  6549. 'dateFormat' => 'Y',
  6550. 'minYear' => date('Y'),
  6551. 'maxYear' => date('Y', strtotime('+20 years'))
  6552. )
  6553. );
  6554. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6555. $result = $this->Form->input('just_month',
  6556. array(
  6557. 'type' => 'date',
  6558. 'label' => false,
  6559. 'dateFormat' => 'M',
  6560. 'empty' => false,
  6561. )
  6562. );
  6563. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6564. $result = $this->Form->input('just_day',
  6565. array(
  6566. 'type' => 'date',
  6567. 'label' => false,
  6568. 'dateFormat' => 'D',
  6569. 'empty' => false,
  6570. )
  6571. );
  6572. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6573. }
  6574. /**
  6575. * testInputDateMaxYear method
  6576. *
  6577. * Let's say we want to only allow users born from 2006 to 2008 to register
  6578. * This being the first singup page, we still don't have any data
  6579. *
  6580. * @return void
  6581. */
  6582. public function testInputDateMaxYear() {
  6583. $this->Form->request->data = array();
  6584. $this->Form->create('User');
  6585. $result = $this->Form->input('birthday',
  6586. array(
  6587. 'label' => false,
  6588. 'div' => false,
  6589. 'type' => 'date',
  6590. 'dateFormat' => 'DMY',
  6591. 'minYear' => 2006,
  6592. 'maxYear' => 2008
  6593. )
  6594. );
  6595. $this->assertContains('value="2008" selected="selected"', $result);
  6596. }
  6597. /**
  6598. * testTextArea method
  6599. *
  6600. * @return void
  6601. */
  6602. public function testTextArea() {
  6603. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6604. $result = $this->Form->textarea('Model.field');
  6605. $expected = array(
  6606. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6607. 'some test data',
  6608. '/textarea',
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. $result = $this->Form->textarea('Model.tmp');
  6612. $expected = array(
  6613. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6614. '/textarea',
  6615. );
  6616. $this->assertTags($result, $expected);
  6617. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6618. $result = $this->Form->textarea('Model.field');
  6619. $expected = array(
  6620. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6621. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6622. '/textarea',
  6623. );
  6624. $this->assertTags($result, $expected);
  6625. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6626. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6627. $expected = array(
  6628. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6629. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6630. '/textarea',
  6631. );
  6632. $this->assertTags($result, $expected);
  6633. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6634. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6635. $expected = array(
  6636. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6637. '/textarea'
  6638. );
  6639. $this->assertTags($result, $expected);
  6640. }
  6641. /**
  6642. * testTextAreaWithStupidCharacters method
  6643. *
  6644. * test text area with non-ascii characters
  6645. *
  6646. * @return void
  6647. */
  6648. public function testTextAreaWithStupidCharacters() {
  6649. $this->loadFixtures('Post');
  6650. $result = $this->Form->input('Post.content', array(
  6651. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6652. ));
  6653. $expected = array(
  6654. 'div' => array('class' => 'input textarea'),
  6655. 'label' => array('for' => 'PostContent'),
  6656. 'Current Text',
  6657. '/label',
  6658. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6659. 'GREAT®',
  6660. '/textarea',
  6661. '/div'
  6662. );
  6663. $this->assertTags($result, $expected);
  6664. }
  6665. /**
  6666. * testHiddenField method
  6667. *
  6668. * @return void
  6669. */
  6670. public function testHiddenField() {
  6671. $Contact = ClassRegistry::getObject('Contact');
  6672. $Contact->validationErrors['field'] = 1;
  6673. $this->Form->request->data['Contact']['field'] = 'test';
  6674. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6675. $this->assertTags($result, array(
  6676. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6677. );
  6678. }
  6679. /**
  6680. * testFileUploadField method
  6681. *
  6682. * @return void
  6683. */
  6684. public function testFileUploadField() {
  6685. $result = $this->Form->file('Model.upload');
  6686. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6687. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6688. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6689. $expected = array(
  6690. 'div' => array('class' => 'input file'),
  6691. 'label' => array('for' => 'ModelUpload'),
  6692. 'Upload',
  6693. '/label',
  6694. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6695. '/div'
  6696. );
  6697. $this->assertTags($result, $expected);
  6698. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6699. $result = $this->Form->file('Model.upload');
  6700. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6701. }
  6702. /**
  6703. * test File upload input on a model not used in create();
  6704. *
  6705. * @return void
  6706. */
  6707. public function testFileUploadOnOtherModel() {
  6708. $this->Form->create('ValidateUser', array('type' => 'file'));
  6709. $result = $this->Form->file('ValidateProfile.city');
  6710. $expected = array(
  6711. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. }
  6715. /**
  6716. * testButton method
  6717. *
  6718. * @return void
  6719. */
  6720. public function testButton() {
  6721. $result = $this->Form->button('Hi');
  6722. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6723. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6724. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6725. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6726. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6727. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6728. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6729. $result = $this->Form->button('No type', array('type' => false));
  6730. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6731. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6732. $this->assertNotRegExp('/\&039/', $result);
  6733. }
  6734. /**
  6735. * Test that button() makes unlocked fields by default.
  6736. *
  6737. * @return void
  6738. */
  6739. public function testButtonUnlockedByDefault() {
  6740. $this->Form->request->params['_Token']['key'] = 'secured';
  6741. $this->Form->button('Save', array('name' => 'save'));
  6742. $this->Form->button('Clear');
  6743. $result = $this->Form->unlockField();
  6744. $this->assertEquals(array('save'), $result);
  6745. }
  6746. /**
  6747. * testPostButton method
  6748. *
  6749. * @return void
  6750. */
  6751. public function testPostButton() {
  6752. $result = $this->Form->postButton('Hi', '/controller/action');
  6753. $this->assertTags($result, array(
  6754. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6755. 'div' => array('style' => 'display:none;'),
  6756. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6757. '/div',
  6758. 'button' => array('type' => 'submit'),
  6759. 'Hi',
  6760. '/button',
  6761. '/form'
  6762. ));
  6763. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6764. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6765. }
  6766. /**
  6767. * Test that postButton adds _Token fields.
  6768. *
  6769. * @return void
  6770. */
  6771. public function testSecurePostButton() {
  6772. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6773. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6774. $expected = array(
  6775. 'form' => array(
  6776. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6777. ),
  6778. array('div' => array('style' => 'display:none;')),
  6779. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6780. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6781. '/div',
  6782. 'button' => array('type' => 'submit'),
  6783. 'Delete',
  6784. '/button',
  6785. array('div' => array('style' => 'display:none;')),
  6786. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6787. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6788. '/div',
  6789. '/form',
  6790. );
  6791. $this->assertTags($result, $expected);
  6792. }
  6793. /**
  6794. * testPostLink method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testPostLink() {
  6799. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6800. $this->assertTags($result, array(
  6801. 'form' => array(
  6802. 'method' => 'post', 'action' => '/posts/delete/1',
  6803. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6804. ),
  6805. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6806. '/form',
  6807. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6808. 'Delete',
  6809. '/a'
  6810. ));
  6811. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6812. $this->assertTags($result, array(
  6813. 'form' => array(
  6814. 'method' => 'post', 'action' => '/posts/delete/1',
  6815. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6816. ),
  6817. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6818. '/form',
  6819. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6820. 'Delete',
  6821. '/a'
  6822. ));
  6823. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6824. $this->assertTags($result, array(
  6825. 'form' => array(
  6826. 'method' => 'post', 'action' => '/posts/delete/1',
  6827. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6828. ),
  6829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6830. '/form',
  6831. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6832. 'Delete',
  6833. '/a'
  6834. ));
  6835. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6836. $this->assertTags($result, array(
  6837. 'form' => array(
  6838. 'method' => 'post', 'action' => '/posts/delete/1',
  6839. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6840. ),
  6841. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6842. '/form',
  6843. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6844. 'Delete',
  6845. '/a'
  6846. ));
  6847. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6848. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6849. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6850. $this->assertTags($result, array(
  6851. 'form' => array(
  6852. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6853. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6854. ),
  6855. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6856. '/form',
  6857. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6858. 'Delete',
  6859. '/a'
  6860. ));
  6861. $result = $this->Form->postLink(
  6862. '',
  6863. array('controller' => 'items', 'action' => 'delete', 10),
  6864. array('class' => 'btn btn-danger', 'escape' => false),
  6865. 'Confirm thing'
  6866. );
  6867. $this->assertTags($result, array(
  6868. 'form' => array(
  6869. 'method' => 'post', 'action' => '/items/delete/10',
  6870. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6871. ),
  6872. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6873. '/form',
  6874. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6875. '/a'
  6876. ));
  6877. }
  6878. /**
  6879. * test creating postLinks after a GET form.
  6880. *
  6881. * @return void
  6882. */
  6883. public function testPostLinkAfterGetForm() {
  6884. $this->Form->request->params['_Token']['key'] = 'testkey';
  6885. $this->Form->create('User', array('type' => 'get'));
  6886. $this->Form->end();
  6887. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6888. $this->assertTags($result, array(
  6889. 'form' => array(
  6890. 'method' => 'post', 'action' => '/posts/delete/1',
  6891. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6892. ),
  6893. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6894. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6895. 'div' => array('style' => 'display:none;'),
  6896. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6897. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6898. '/div',
  6899. '/form',
  6900. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6901. 'Delete',
  6902. '/a'
  6903. ));
  6904. }
  6905. /**
  6906. * Test that postLink adds _Token fields.
  6907. *
  6908. * @return void
  6909. */
  6910. public function testSecurePostLink() {
  6911. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6912. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6913. $expected = array(
  6914. 'form' => array(
  6915. 'method' => 'post', 'action' => '/posts/delete/1',
  6916. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6917. ),
  6918. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6919. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6920. 'div' => array('style' => 'display:none;'),
  6921. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6922. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6923. '/div',
  6924. '/form',
  6925. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6926. 'Delete',
  6927. '/a'
  6928. );
  6929. $this->assertTags($result, $expected);
  6930. }
  6931. /**
  6932. * testSubmitButton method
  6933. *
  6934. * @return void
  6935. */
  6936. public function testSubmitButton() {
  6937. $result = $this->Form->submit('');
  6938. $expected = array(
  6939. 'div' => array('class' => 'submit'),
  6940. 'input' => array('type' => 'submit', 'value' => ''),
  6941. '/div'
  6942. );
  6943. $this->assertTags($result, $expected);
  6944. $result = $this->Form->submit('Test Submit');
  6945. $expected = array(
  6946. 'div' => array('class' => 'submit'),
  6947. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6948. '/div'
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6952. $expected = array(
  6953. 'span' => array('class' => 'submit'),
  6954. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6955. '/span'
  6956. );
  6957. $this->assertTags($result, $expected);
  6958. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6959. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6960. $this->assertTags($result, $expected);
  6961. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6962. $expected = array(
  6963. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6964. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6965. '/div'
  6966. );
  6967. $this->assertTags($result, $expected);
  6968. $result = $this->Form->submit('Next >');
  6969. $expected = array(
  6970. 'div' => array('class' => 'submit'),
  6971. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6972. '/div'
  6973. );
  6974. $this->assertTags($result, $expected);
  6975. $result = $this->Form->submit('Next >', array('escape' => false));
  6976. $expected = array(
  6977. 'div' => array('class' => 'submit'),
  6978. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6979. '/div'
  6980. );
  6981. $this->assertTags($result, $expected);
  6982. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6983. $expected = array(
  6984. 'div' => array('class' => 'submit'),
  6985. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6986. '/div'
  6987. );
  6988. $this->assertTags($result, $expected);
  6989. $before = '--before--';
  6990. $after = '--after--';
  6991. $result = $this->Form->submit('Test', array('before' => $before));
  6992. $expected = array(
  6993. 'div' => array('class' => 'submit'),
  6994. '--before--',
  6995. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6996. '/div'
  6997. );
  6998. $this->assertTags($result, $expected);
  6999. $result = $this->Form->submit('Test', array('after' => $after));
  7000. $expected = array(
  7001. 'div' => array('class' => 'submit'),
  7002. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7003. '--after--',
  7004. '/div'
  7005. );
  7006. $this->assertTags($result, $expected);
  7007. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7008. $expected = array(
  7009. 'div' => array('class' => 'submit'),
  7010. '--before--',
  7011. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7012. '--after--',
  7013. '/div'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. }
  7017. /**
  7018. * test image submit types.
  7019. *
  7020. * @return void
  7021. */
  7022. public function testSubmitImage() {
  7023. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7024. $expected = array(
  7025. 'div' => array('class' => 'submit'),
  7026. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7027. '/div'
  7028. );
  7029. $this->assertTags($result, $expected);
  7030. $result = $this->Form->submit('/relative/cake.power.gif');
  7031. $expected = array(
  7032. 'div' => array('class' => 'submit'),
  7033. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7034. '/div'
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. $result = $this->Form->submit('cake.power.gif');
  7038. $expected = array(
  7039. 'div' => array('class' => 'submit'),
  7040. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7041. '/div'
  7042. );
  7043. $this->assertTags($result, $expected);
  7044. $result = $this->Form->submit('Not.an.image');
  7045. $expected = array(
  7046. 'div' => array('class' => 'submit'),
  7047. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7048. '/div'
  7049. );
  7050. $this->assertTags($result, $expected);
  7051. $after = '--after--';
  7052. $before = '--before--';
  7053. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7054. $expected = array(
  7055. 'div' => array('class' => 'submit'),
  7056. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7057. '--after--',
  7058. '/div'
  7059. );
  7060. $this->assertTags($result, $expected);
  7061. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7062. $expected = array(
  7063. 'div' => array('class' => 'submit'),
  7064. '--before--',
  7065. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7066. '/div'
  7067. );
  7068. $this->assertTags($result, $expected);
  7069. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7070. $expected = array(
  7071. 'div' => array('class' => 'submit'),
  7072. '--before--',
  7073. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7074. '--after--',
  7075. '/div'
  7076. );
  7077. $this->assertTags($result, $expected);
  7078. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7079. $expected = array(
  7080. 'div' => array('class' => 'submit'),
  7081. '--before--',
  7082. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7083. '--after--',
  7084. '/div'
  7085. );
  7086. $this->assertTags($result, $expected);
  7087. }
  7088. /**
  7089. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7090. * be submitted at a time.
  7091. *
  7092. * @return void
  7093. */
  7094. public function testSubmitUnlockedByDefault() {
  7095. $this->Form->request->params['_Token']['key'] = 'secured';
  7096. $this->Form->submit('Go go');
  7097. $this->Form->submit('Save', array('name' => 'save'));
  7098. $result = $this->Form->unlockField();
  7099. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7100. }
  7101. /**
  7102. * Test submit image with timestamps.
  7103. *
  7104. * @return void
  7105. */
  7106. public function testSubmitImageTimestamp() {
  7107. Configure::write('Asset.timestamp', 'force');
  7108. $result = $this->Form->submit('cake.power.gif');
  7109. $expected = array(
  7110. 'div' => array('class' => 'submit'),
  7111. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7112. '/div'
  7113. );
  7114. $this->assertTags($result, $expected);
  7115. }
  7116. /**
  7117. * test the create() method
  7118. *
  7119. * @return void
  7120. */
  7121. public function testCreate() {
  7122. $result = $this->Form->create('Contact');
  7123. $encoding = strtolower(Configure::read('App.encoding'));
  7124. $expected = array(
  7125. 'form' => array(
  7126. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7127. 'accept-charset' => $encoding
  7128. ),
  7129. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7130. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7131. '/div'
  7132. );
  7133. $this->assertTags($result, $expected);
  7134. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7135. $expected = array('form' => array(
  7136. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7137. 'accept-charset' => $encoding
  7138. ));
  7139. $this->assertTags($result, $expected);
  7140. $result = $this->Form->create('Contact', array('type' => 'get'));
  7141. $expected = array('form' => array(
  7142. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7143. 'accept-charset' => $encoding
  7144. ));
  7145. $this->assertTags($result, $expected);
  7146. $result = $this->Form->create('Contact', array('type' => 'put'));
  7147. $expected = array(
  7148. 'form' => array(
  7149. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7150. 'accept-charset' => $encoding
  7151. ),
  7152. 'div' => array('style' => 'display:none;'),
  7153. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7154. '/div'
  7155. );
  7156. $this->assertTags($result, $expected);
  7157. $result = $this->Form->create('Contact', array('type' => 'file'));
  7158. $expected = array(
  7159. 'form' => array(
  7160. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7161. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7162. ),
  7163. 'div' => array('style' => 'display:none;'),
  7164. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7165. '/div'
  7166. );
  7167. $this->assertTags($result, $expected);
  7168. $this->Form->request->data['Contact']['id'] = 1;
  7169. $this->Form->request->here = '/contacts/edit/1';
  7170. $this->Form->request['action'] = 'edit';
  7171. $result = $this->Form->create('Contact');
  7172. $expected = array(
  7173. 'form' => array(
  7174. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7175. 'accept-charset' => $encoding
  7176. ),
  7177. 'div' => array('style' => 'display:none;'),
  7178. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7179. '/div'
  7180. );
  7181. $this->assertTags($result, $expected);
  7182. $this->Form->request->data['Contact']['id'] = 1;
  7183. $this->Form->request->here = '/contacts/edit/1';
  7184. $this->Form->request['action'] = 'edit';
  7185. $result = $this->Form->create('Contact', array('type' => 'file'));
  7186. $expected = array(
  7187. 'form' => array(
  7188. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7189. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7190. ),
  7191. 'div' => array('style' => 'display:none;'),
  7192. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7193. '/div'
  7194. );
  7195. $this->assertTags($result, $expected);
  7196. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7197. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7198. $expected = array(
  7199. 'form' => array(
  7200. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7201. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7202. ),
  7203. 'div' => array('style' => 'display:none;'),
  7204. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7205. '/div'
  7206. );
  7207. $this->assertTags($result, $expected);
  7208. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7209. $expected = array(
  7210. 'form' => array(
  7211. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7212. 'accept-charset' => $encoding
  7213. ),
  7214. 'div' => array('style' => 'display:none;'),
  7215. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7216. '/div'
  7217. );
  7218. $this->assertTags($result, $expected);
  7219. $this->Form->request['action'] = 'add';
  7220. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7221. $expected = array(
  7222. 'form' => array(
  7223. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7224. 'accept-charset' => $encoding
  7225. ),
  7226. 'div' => array('style' => 'display:none;'),
  7227. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7228. '/div'
  7229. );
  7230. $this->assertTags($result, $expected);
  7231. $result = $this->Form->create('User', array('action' => 'login'));
  7232. $expected = array(
  7233. 'form' => array(
  7234. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7235. 'accept-charset' => $encoding
  7236. ),
  7237. 'div' => array('style' => 'display:none;'),
  7238. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7239. '/div'
  7240. );
  7241. $this->assertTags($result, $expected);
  7242. $result = $this->Form->create('User', array('url' => '/users/login'));
  7243. $expected = array(
  7244. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7245. 'div' => array('style' => 'display:none;'),
  7246. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7247. '/div'
  7248. );
  7249. $this->assertTags($result, $expected);
  7250. $this->Form->request['controller'] = 'pages';
  7251. $result = $this->Form->create('User', array('action' => 'signup'));
  7252. $expected = array(
  7253. 'form' => array(
  7254. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7255. 'accept-charset' => $encoding
  7256. ),
  7257. 'div' => array('style' => 'display:none;'),
  7258. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7259. '/div'
  7260. );
  7261. $this->assertTags($result, $expected);
  7262. $this->Form->request->data = array();
  7263. $this->Form->request['controller'] = 'contacts';
  7264. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7265. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7266. $expected = array(
  7267. 'form' => array(
  7268. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7269. 'accept-charset' => 'utf-8'
  7270. ),
  7271. 'div' => array('style' => 'display:none;'),
  7272. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7273. '/div'
  7274. );
  7275. $this->assertTags($result, $expected);
  7276. }
  7277. /**
  7278. * Test the onsubmit option for create()
  7279. *
  7280. * @return void
  7281. */
  7282. public function testCreateOnSubmit() {
  7283. $this->Form->request->data = array();
  7284. $this->Form->request['controller'] = 'contacts';
  7285. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7286. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7287. $expected = array(
  7288. 'form' => array(
  7289. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7290. 'accept-charset' => 'utf-8'
  7291. ),
  7292. 'div' => array('style' => 'display:none;'),
  7293. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7294. '/div'
  7295. );
  7296. $this->assertTags($result, $expected);
  7297. $this->Form->request->data = array();
  7298. $this->Form->request['controller'] = 'contacts';
  7299. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7300. $result = $this->Form->create(array(
  7301. 'url' => array('action' => 'index', 'param'),
  7302. 'default' => false,
  7303. 'onsubmit' => 'someFunction();'
  7304. ));
  7305. $expected = array(
  7306. 'form' => array(
  7307. 'id' => 'ContactAddForm', 'method' => 'post',
  7308. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7309. 'action' => '/contacts/index/param',
  7310. 'accept-charset' => 'utf-8'
  7311. ),
  7312. 'div' => array('style' => 'display:none;'),
  7313. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7314. '/div'
  7315. );
  7316. $this->assertTags($result, $expected);
  7317. }
  7318. /**
  7319. * test create() with automatic url generation
  7320. *
  7321. * @return void
  7322. */
  7323. public function testCreateAutoUrl() {
  7324. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7325. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7326. $this->Form->request->base = '/base_url';
  7327. $result = $this->Form->create('Contact');
  7328. $expected = array(
  7329. 'form' => array(
  7330. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7331. 'accept-charset' => 'utf-8'
  7332. ),
  7333. 'div' => array('style' => 'display:none;'),
  7334. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7335. '/div'
  7336. );
  7337. $this->assertTags($result, $expected);
  7338. $this->Form->request['action'] = 'delete';
  7339. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7340. $this->Form->request->base = '/base_url';
  7341. $result = $this->Form->create('Contact');
  7342. $expected = array(
  7343. 'form' => array(
  7344. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7345. 'accept-charset' => 'utf-8'
  7346. ),
  7347. 'div' => array('style' => 'display:none;'),
  7348. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7349. '/div'
  7350. );
  7351. $this->assertTags($result, $expected);
  7352. }
  7353. /**
  7354. * test create() with a custom route
  7355. *
  7356. * @return void
  7357. */
  7358. public function testCreateCustomRoute() {
  7359. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7360. $encoding = strtolower(Configure::read('App.encoding'));
  7361. $result = $this->Form->create('User', array('action' => 'login'));
  7362. $expected = array(
  7363. 'form' => array(
  7364. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7365. 'accept-charset' => $encoding
  7366. ),
  7367. 'div' => array('style' => 'display:none;'),
  7368. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7369. '/div'
  7370. );
  7371. $this->assertTags($result, $expected);
  7372. }
  7373. /**
  7374. * test that inputDefaults are stored and used.
  7375. *
  7376. * @return void
  7377. */
  7378. public function testCreateWithInputDefaults() {
  7379. $this->Form->create('User', array(
  7380. 'inputDefaults' => array(
  7381. 'div' => false,
  7382. 'label' => false,
  7383. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7384. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7385. )
  7386. ));
  7387. $result = $this->Form->input('username');
  7388. $expected = array(
  7389. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7393. $expected = array(
  7394. 'div' => array('class' => 'input text'),
  7395. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7396. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7397. '/div'
  7398. );
  7399. $this->assertTags($result, $expected);
  7400. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7401. $expected = array(
  7402. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7403. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7404. );
  7405. $this->assertTags($result, $expected);
  7406. $this->Form->create('User', array(
  7407. 'inputDefaults' => array(
  7408. 'div' => false,
  7409. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7410. )
  7411. ));
  7412. $result = $this->Form->input('username', array('div' => true));
  7413. $expected = array(
  7414. 'div' => array('class' => 'input text'),
  7415. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7416. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7417. '/div'
  7418. );
  7419. $this->assertTags($result, $expected);
  7420. }
  7421. /**
  7422. * test automatic accept-charset overriding
  7423. *
  7424. * @return void
  7425. */
  7426. public function testCreateWithAcceptCharset() {
  7427. $result = $this->Form->create('UserForm', array(
  7428. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7429. )
  7430. );
  7431. $expected = array(
  7432. 'form' => array(
  7433. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7434. 'accept-charset' => 'iso-8859-1'
  7435. ),
  7436. 'div' => array('style' => 'display:none;'),
  7437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7438. '/div'
  7439. );
  7440. $this->assertTags($result, $expected);
  7441. }
  7442. /**
  7443. * Test base form URL when url param is passed with multiple parameters (&)
  7444. *
  7445. */
  7446. public function testCreateQuerystringrequest() {
  7447. $encoding = strtolower(Configure::read('App.encoding'));
  7448. $result = $this->Form->create('Contact', array(
  7449. 'type' => 'post',
  7450. 'escape' => false,
  7451. 'url' => array(
  7452. 'controller' => 'controller',
  7453. 'action' => 'action',
  7454. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7455. )
  7456. ));
  7457. $expected = array(
  7458. 'form' => array(
  7459. 'id' => 'ContactAddForm',
  7460. 'method' => 'post',
  7461. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7462. 'accept-charset' => $encoding
  7463. ),
  7464. 'div' => array('style' => 'display:none;'),
  7465. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7466. '/div'
  7467. );
  7468. $this->assertTags($result, $expected);
  7469. $result = $this->Form->create('Contact', array(
  7470. 'type' => 'post',
  7471. 'url' => array(
  7472. 'controller' => 'controller',
  7473. 'action' => 'action',
  7474. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7475. )
  7476. ));
  7477. $expected = array(
  7478. 'form' => array(
  7479. 'id' => 'ContactAddForm',
  7480. 'method' => 'post',
  7481. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7482. 'accept-charset' => $encoding
  7483. ),
  7484. 'div' => array('style' => 'display:none;'),
  7485. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7486. '/div'
  7487. );
  7488. $this->assertTags($result, $expected);
  7489. }
  7490. /**
  7491. * test that create() doesn't cause errors by multiple id's being in the primary key
  7492. * as could happen with multiple select or checkboxes.
  7493. *
  7494. * @return void
  7495. */
  7496. public function testCreateWithMultipleIdInData() {
  7497. $encoding = strtolower(Configure::read('App.encoding'));
  7498. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7499. $result = $this->Form->create('Contact');
  7500. $expected = array(
  7501. 'form' => array(
  7502. 'id' => 'ContactAddForm',
  7503. 'method' => 'post',
  7504. 'action' => '/contacts/add',
  7505. 'accept-charset' => $encoding
  7506. ),
  7507. 'div' => array('style' => 'display:none;'),
  7508. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7509. '/div'
  7510. );
  7511. $this->assertTags($result, $expected);
  7512. }
  7513. /**
  7514. * test that create() doesn't add in extra passed params.
  7515. *
  7516. * @return void
  7517. */
  7518. public function testCreatePassedArgs() {
  7519. $encoding = strtolower(Configure::read('App.encoding'));
  7520. $this->Form->request->data['Contact']['id'] = 1;
  7521. $result = $this->Form->create('Contact', array(
  7522. 'type' => 'post',
  7523. 'escape' => false,
  7524. 'url' => array(
  7525. 'action' => 'edit',
  7526. 'myparam'
  7527. )
  7528. ));
  7529. $expected = array(
  7530. 'form' => array(
  7531. 'id' => 'ContactAddForm',
  7532. 'method' => 'post',
  7533. 'action' => '/contacts/edit/myparam',
  7534. 'accept-charset' => $encoding
  7535. ),
  7536. 'div' => array('style' => 'display:none;'),
  7537. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7538. '/div'
  7539. );
  7540. $this->assertTags($result, $expected);
  7541. }
  7542. /**
  7543. * test creating a get form, and get form inputs.
  7544. *
  7545. * @return void
  7546. */
  7547. public function testGetFormCreate() {
  7548. $encoding = strtolower(Configure::read('App.encoding'));
  7549. $result = $this->Form->create('Contact', array('type' => 'get'));
  7550. $this->assertTags($result, array('form' => array(
  7551. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7552. 'accept-charset' => $encoding
  7553. )));
  7554. $result = $this->Form->text('Contact.name');
  7555. $this->assertTags($result, array('input' => array(
  7556. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7557. )));
  7558. $result = $this->Form->password('password');
  7559. $this->assertTags($result, array('input' => array(
  7560. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7561. )));
  7562. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7563. $result = $this->Form->text('user_form');
  7564. $this->assertTags($result, array('input' => array(
  7565. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7566. )));
  7567. }
  7568. /**
  7569. * test get form, and inputs when the model param is false
  7570. *
  7571. * @return void
  7572. */
  7573. public function testGetFormWithFalseModel() {
  7574. $encoding = strtolower(Configure::read('App.encoding'));
  7575. $this->Form->request['controller'] = 'contact_test';
  7576. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7577. $expected = array('form' => array(
  7578. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7579. 'accept-charset' => $encoding
  7580. ));
  7581. $this->assertTags($result, $expected);
  7582. $result = $this->Form->text('reason');
  7583. $expected = array(
  7584. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7585. );
  7586. $this->assertTags($result, $expected);
  7587. }
  7588. /**
  7589. * test that datetime() works with GET style forms.
  7590. *
  7591. * @return void
  7592. */
  7593. public function testDateTimeWithGetForms() {
  7594. extract($this->dateRegex);
  7595. $this->Form->create('Contact', array('type' => 'get'));
  7596. $result = $this->Form->datetime('created');
  7597. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7598. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7599. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7600. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7601. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7602. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7603. }
  7604. /**
  7605. * testEditFormWithData method
  7606. *
  7607. * test auto populating form elements from submitted data.
  7608. *
  7609. * @return void
  7610. */
  7611. public function testEditFormWithData() {
  7612. $this->Form->request->data = array('Person' => array(
  7613. 'id' => 1,
  7614. 'first_name' => 'Nate',
  7615. 'last_name' => 'Abele',
  7616. 'email' => 'nate@example.com'
  7617. ));
  7618. $this->Form->request->addParams(array(
  7619. 'models' => array('Person'),
  7620. 'controller' => 'people',
  7621. 'action' => 'add'
  7622. ));
  7623. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7624. $this->Form->create();
  7625. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7626. $expected = array(
  7627. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7628. 'select' => array(
  7629. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7630. ),
  7631. array('option' => array('value' => 1)), 'Nate', '/option',
  7632. array('option' => array('value' => 2)), 'Garrett', '/option',
  7633. array('option' => array('value' => 3)), 'Larry', '/option',
  7634. '/select'
  7635. );
  7636. $this->assertTags($result, $expected);
  7637. }
  7638. /**
  7639. * Test that required fields are created for various types of validation.
  7640. *
  7641. * @return void
  7642. */
  7643. public function testFormInputRequiredDetection() {
  7644. $this->Form->create('Contact');
  7645. $result = $this->Form->input('Contact.non_existing');
  7646. $expected = array(
  7647. 'div' => array('class' => 'input text'),
  7648. 'label' => array('for' => 'ContactNonExisting'),
  7649. 'Non Existing',
  7650. '/label',
  7651. 'input' => array(
  7652. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7653. 'id' => 'ContactNonExisting'
  7654. ),
  7655. '/div'
  7656. );
  7657. $this->assertTags($result, $expected);
  7658. $result = $this->Form->input('Contact.imrequired');
  7659. $expected = array(
  7660. 'div' => array('class' => 'input text required'),
  7661. 'label' => array('for' => 'ContactImrequired'),
  7662. 'Imrequired',
  7663. '/label',
  7664. 'input' => array(
  7665. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7666. 'id' => 'ContactImrequired',
  7667. 'required' => 'required'
  7668. ),
  7669. '/div'
  7670. );
  7671. $this->assertTags($result, $expected);
  7672. $result = $this->Form->input('Contact.imalsorequired');
  7673. $expected = array(
  7674. 'div' => array('class' => 'input text required'),
  7675. 'label' => array('for' => 'ContactImalsorequired'),
  7676. 'Imalsorequired',
  7677. '/label',
  7678. 'input' => array(
  7679. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7680. 'id' => 'ContactImalsorequired',
  7681. 'required' => 'required'
  7682. ),
  7683. '/div'
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. $result = $this->Form->input('Contact.imrequiredtoo');
  7687. $expected = array(
  7688. 'div' => array('class' => 'input text required'),
  7689. 'label' => array('for' => 'ContactImrequiredtoo'),
  7690. 'Imrequiredtoo',
  7691. '/label',
  7692. 'input' => array(
  7693. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7694. 'id' => 'ContactImrequiredtoo',
  7695. 'required' => 'required'
  7696. ),
  7697. '/div'
  7698. );
  7699. $this->assertTags($result, $expected);
  7700. $result = $this->Form->input('Contact.required_one');
  7701. $expected = array(
  7702. 'div' => array('class' => 'input text required'),
  7703. 'label' => array('for' => 'ContactRequiredOne'),
  7704. 'Required One',
  7705. '/label',
  7706. 'input' => array(
  7707. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7708. 'id' => 'ContactRequiredOne',
  7709. 'required' => 'required'
  7710. ),
  7711. '/div'
  7712. );
  7713. $this->assertTags($result, $expected);
  7714. $result = $this->Form->input('Contact.string_required');
  7715. $expected = array(
  7716. 'div' => array('class' => 'input text required'),
  7717. 'label' => array('for' => 'ContactStringRequired'),
  7718. 'String Required',
  7719. '/label',
  7720. 'input' => array(
  7721. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7722. 'id' => 'ContactStringRequired',
  7723. 'required' => 'required'
  7724. ),
  7725. '/div'
  7726. );
  7727. $this->assertTags($result, $expected);
  7728. $result = $this->Form->input('Contact.imnotrequired');
  7729. $expected = array(
  7730. 'div' => array('class' => 'input text'),
  7731. 'label' => array('for' => 'ContactImnotrequired'),
  7732. 'Imnotrequired',
  7733. '/label',
  7734. 'input' => array(
  7735. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7736. 'id' => 'ContactImnotrequired'
  7737. ),
  7738. '/div'
  7739. );
  7740. $this->assertTags($result, $expected);
  7741. $result = $this->Form->input('Contact.imalsonotrequired');
  7742. $expected = array(
  7743. 'div' => array('class' => 'input text'),
  7744. 'label' => array('for' => 'ContactImalsonotrequired'),
  7745. 'Imalsonotrequired',
  7746. '/label',
  7747. 'input' => array(
  7748. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7749. 'id' => 'ContactImalsonotrequired'
  7750. ),
  7751. '/div'
  7752. );
  7753. $this->assertTags($result, $expected);
  7754. $result = $this->Form->input('Contact.imalsonotrequired2');
  7755. $expected = array(
  7756. 'div' => array('class' => 'input text'),
  7757. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7758. 'Imalsonotrequired2',
  7759. '/label',
  7760. 'input' => array(
  7761. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7762. 'id' => 'ContactImalsonotrequired2'
  7763. ),
  7764. '/div'
  7765. );
  7766. $this->assertTags($result, $expected);
  7767. $result = $this->Form->input('Contact.imnotrequiredeither');
  7768. $expected = array(
  7769. 'div' => array('class' => 'input text'),
  7770. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7771. 'Imnotrequiredeither',
  7772. '/label',
  7773. 'input' => array(
  7774. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7775. 'id' => 'ContactImnotrequiredeither'
  7776. ),
  7777. '/div'
  7778. );
  7779. $this->assertTags($result, $expected);
  7780. $result = $this->Form->input('Contact.iamrequiredalways');
  7781. $expected = array(
  7782. 'div' => array('class' => 'input text required'),
  7783. 'label' => array('for' => 'ContactIamrequiredalways'),
  7784. 'Iamrequiredalways',
  7785. '/label',
  7786. 'input' => array(
  7787. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7788. 'id' => 'ContactIamrequiredalways',
  7789. 'required' => 'required'
  7790. ),
  7791. '/div'
  7792. );
  7793. $this->assertTags($result, $expected);
  7794. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7795. $expected = array(
  7796. 'div' => array('class' => 'input checkbox required'),
  7797. array('input' => array(
  7798. 'type' => 'hidden',
  7799. 'name' => 'data[Contact][boolean_field]',
  7800. 'id' => 'ContactBooleanField_',
  7801. 'value' => '0'
  7802. )),
  7803. array('input' => array(
  7804. 'type' => 'checkbox',
  7805. 'name' => 'data[Contact][boolean_field]',
  7806. 'value' => '1',
  7807. 'id' => 'ContactBooleanField'
  7808. )),
  7809. 'label' => array('for' => 'ContactBooleanField'),
  7810. 'Boolean Field',
  7811. '/label',
  7812. '/div'
  7813. );
  7814. $this->assertTags($result, $expected);
  7815. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7816. $expected = array(
  7817. 'div' => array('class' => 'input checkbox required'),
  7818. array('input' => array(
  7819. 'type' => 'hidden',
  7820. 'name' => 'data[Contact][boolean_field]',
  7821. 'id' => 'ContactBooleanField_',
  7822. 'value' => '0'
  7823. )),
  7824. array('input' => array(
  7825. 'type' => 'checkbox',
  7826. 'name' => 'data[Contact][boolean_field]',
  7827. 'value' => '1',
  7828. 'id' => 'ContactBooleanField',
  7829. 'required' => 'required'
  7830. )),
  7831. 'label' => array('for' => 'ContactBooleanField'),
  7832. 'Boolean Field',
  7833. '/label',
  7834. '/div'
  7835. );
  7836. $this->assertTags($result, $expected);
  7837. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7838. $expected = array(
  7839. 'div' => array('class' => 'input file required'),
  7840. 'label' => array('for' => 'ContactIamrequiredalways'),
  7841. 'Iamrequiredalways',
  7842. '/label',
  7843. 'input' => array(
  7844. 'type' => 'file',
  7845. 'name' => 'data[Contact][iamrequiredalways]',
  7846. 'id' => 'ContactIamrequiredalways',
  7847. 'required' => 'required'
  7848. ),
  7849. '/div'
  7850. );
  7851. $this->assertTags($result, $expected);
  7852. }
  7853. /**
  7854. * Test that required fields are created when only using ModelValidator::add().
  7855. *
  7856. * @return void
  7857. */
  7858. public function testFormInputRequiredDetectionModelValidator() {
  7859. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7860. $this->Form->create('ContactTag');
  7861. $result = $this->Form->input('ContactTag.iwillberequired');
  7862. $expected = array(
  7863. 'div' => array('class' => 'input text required'),
  7864. 'label' => array('for' => 'ContactTagIwillberequired'),
  7865. 'Iwillberequired',
  7866. '/label',
  7867. 'input' => array(
  7868. 'name' => 'data[ContactTag][iwillberequired]',
  7869. 'type' => 'text',
  7870. 'id' => 'ContactTagIwillberequired',
  7871. 'required' => 'required'
  7872. ),
  7873. '/div'
  7874. );
  7875. $this->assertTags($result, $expected);
  7876. }
  7877. /**
  7878. * testFormMagicInput method
  7879. *
  7880. * @return void
  7881. */
  7882. public function testFormMagicInput() {
  7883. $encoding = strtolower(Configure::read('App.encoding'));
  7884. $result = $this->Form->create('Contact');
  7885. $expected = array(
  7886. 'form' => array(
  7887. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7888. 'accept-charset' => $encoding
  7889. ),
  7890. 'div' => array('style' => 'display:none;'),
  7891. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7892. '/div'
  7893. );
  7894. $this->assertTags($result, $expected);
  7895. $result = $this->Form->input('name');
  7896. $expected = array(
  7897. 'div' => array('class' => 'input text'),
  7898. 'label' => array('for' => 'ContactName'),
  7899. 'Name',
  7900. '/label',
  7901. 'input' => array(
  7902. 'type' => 'text', 'name' => 'data[Contact][name]',
  7903. 'id' => 'ContactName', 'maxlength' => '255'
  7904. ),
  7905. '/div'
  7906. );
  7907. $this->assertTags($result, $expected);
  7908. $result = $this->Form->input('non_existing_field_in_contact_model');
  7909. $expected = array(
  7910. 'div' => array('class' => 'input text'),
  7911. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7912. 'Non Existing Field In Contact Model',
  7913. '/label',
  7914. 'input' => array(
  7915. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7916. 'id' => 'ContactNonExistingFieldInContactModel'
  7917. ),
  7918. '/div'
  7919. );
  7920. $this->assertTags($result, $expected);
  7921. $result = $this->Form->input('Address.street');
  7922. $expected = array(
  7923. 'div' => array('class' => 'input text'),
  7924. 'label' => array('for' => 'AddressStreet'),
  7925. 'Street',
  7926. '/label',
  7927. 'input' => array(
  7928. 'type' => 'text', 'name' => 'data[Address][street]',
  7929. 'id' => 'AddressStreet'
  7930. ),
  7931. '/div'
  7932. );
  7933. $this->assertTags($result, $expected);
  7934. $result = $this->Form->input('Address.non_existing_field_in_model');
  7935. $expected = array(
  7936. 'div' => array('class' => 'input text'),
  7937. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7938. 'Non Existing Field In Model',
  7939. '/label',
  7940. 'input' => array(
  7941. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7942. 'id' => 'AddressNonExistingFieldInModel'
  7943. ),
  7944. '/div'
  7945. );
  7946. $this->assertTags($result, $expected);
  7947. $result = $this->Form->input('name', array('div' => false));
  7948. $expected = array(
  7949. 'label' => array('for' => 'ContactName'),
  7950. 'Name',
  7951. '/label',
  7952. 'input' => array(
  7953. 'type' => 'text', 'name' => 'data[Contact][name]',
  7954. 'id' => 'ContactName', 'maxlength' => '255'
  7955. )
  7956. );
  7957. $this->assertTags($result, $expected);
  7958. extract($this->dateRegex);
  7959. $now = strtotime('now');
  7960. $result = $this->Form->input('Contact.published', array('div' => false));
  7961. $expected = array(
  7962. 'label' => array('for' => 'ContactPublishedMonth'),
  7963. 'Published',
  7964. '/label',
  7965. array('select' => array(
  7966. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7967. )),
  7968. $monthsRegex,
  7969. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7970. date('F', $now),
  7971. '/option',
  7972. '*/select',
  7973. '-',
  7974. array('select' => array(
  7975. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7976. )),
  7977. $daysRegex,
  7978. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7979. date('j', $now),
  7980. '/option',
  7981. '*/select',
  7982. '-',
  7983. array('select' => array(
  7984. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7985. )),
  7986. $yearsRegex,
  7987. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7988. date('Y', $now),
  7989. '*/select'
  7990. );
  7991. $this->assertTags($result, $expected);
  7992. $result = $this->Form->input('Contact.updated', array('div' => false));
  7993. $expected = array(
  7994. 'label' => array('for' => 'ContactUpdatedMonth'),
  7995. 'Updated',
  7996. '/label',
  7997. array('select' => array(
  7998. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7999. )),
  8000. $monthsRegex,
  8001. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8002. date('F', $now),
  8003. '/option',
  8004. '*/select',
  8005. '-',
  8006. array('select' => array(
  8007. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8008. )),
  8009. $daysRegex,
  8010. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8011. date('j', $now),
  8012. '/option',
  8013. '*/select',
  8014. '-',
  8015. array('select' => array(
  8016. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8017. )),
  8018. $yearsRegex,
  8019. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8020. date('Y', $now),
  8021. '*/select'
  8022. );
  8023. $this->assertTags($result, $expected);
  8024. $result = $this->Form->input('UserForm.stuff');
  8025. $expected = array(
  8026. 'div' => array('class' => 'input text'),
  8027. 'label' => array('for' => 'UserFormStuff'),
  8028. 'Stuff',
  8029. '/label',
  8030. 'input' => array(
  8031. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8032. 'id' => 'UserFormStuff', 'maxlength' => 10
  8033. ),
  8034. '/div'
  8035. );
  8036. $this->assertTags($result, $expected);
  8037. }
  8038. /**
  8039. * testForMagicInputNonExistingNorValidated method
  8040. *
  8041. * @return void
  8042. */
  8043. public function testForMagicInputNonExistingNorValidated() {
  8044. $encoding = strtolower(Configure::read('App.encoding'));
  8045. $result = $this->Form->create('Contact');
  8046. $expected = array(
  8047. 'form' => array(
  8048. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8049. 'accept-charset' => $encoding
  8050. ),
  8051. 'div' => array('style' => 'display:none;'),
  8052. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8053. '/div'
  8054. );
  8055. $this->assertTags($result, $expected);
  8056. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8057. $expected = array(
  8058. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8059. 'Non Existing Nor Validated',
  8060. '/label',
  8061. 'input' => array(
  8062. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8063. 'id' => 'ContactNonExistingNorValidated'
  8064. )
  8065. );
  8066. $this->assertTags($result, $expected);
  8067. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8068. 'div' => false, 'value' => 'my value'
  8069. ));
  8070. $expected = array(
  8071. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8072. 'Non Existing Nor Validated',
  8073. '/label',
  8074. 'input' => array(
  8075. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8076. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8077. )
  8078. );
  8079. $this->assertTags($result, $expected);
  8080. $this->Form->request->data = array(
  8081. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8082. ));
  8083. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8084. $expected = array(
  8085. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8086. 'Non Existing Nor Validated',
  8087. '/label',
  8088. 'input' => array(
  8089. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8090. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8091. )
  8092. );
  8093. $this->assertTags($result, $expected);
  8094. }
  8095. /**
  8096. * testFormMagicInputLabel method
  8097. *
  8098. * @return void
  8099. */
  8100. public function testFormMagicInputLabel() {
  8101. $encoding = strtolower(Configure::read('App.encoding'));
  8102. $result = $this->Form->create('Contact');
  8103. $expected = array(
  8104. 'form' => array(
  8105. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8106. 'accept-charset' => $encoding
  8107. ),
  8108. 'div' => array('style' => 'display:none;'),
  8109. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8110. '/div'
  8111. );
  8112. $this->assertTags($result, $expected);
  8113. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8114. $this->assertTags($result, array('input' => array(
  8115. 'name' => 'data[Contact][name]', 'type' => 'text',
  8116. 'id' => 'ContactName', 'maxlength' => '255')
  8117. ));
  8118. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8119. $expected = array(
  8120. 'label' => array('for' => 'ContactName'),
  8121. 'My label',
  8122. '/label',
  8123. 'input' => array(
  8124. 'type' => 'text', 'name' => 'data[Contact][name]',
  8125. 'id' => 'ContactName', 'maxlength' => '255'
  8126. )
  8127. );
  8128. $this->assertTags($result, $expected);
  8129. $result = $this->Form->input('Contact.name', array(
  8130. 'div' => false, 'label' => array('class' => 'mandatory')
  8131. ));
  8132. $expected = array(
  8133. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8134. 'Name',
  8135. '/label',
  8136. 'input' => array(
  8137. 'type' => 'text', 'name' => 'data[Contact][name]',
  8138. 'id' => 'ContactName', 'maxlength' => '255'
  8139. )
  8140. );
  8141. $this->assertTags($result, $expected);
  8142. $result = $this->Form->input('Contact.name', array(
  8143. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8144. ));
  8145. $expected = array(
  8146. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8147. 'My label',
  8148. '/label',
  8149. 'input' => array(
  8150. 'type' => 'text', 'name' => 'data[Contact][name]',
  8151. 'id' => 'ContactName', 'maxlength' => '255'
  8152. )
  8153. );
  8154. $this->assertTags($result, $expected);
  8155. $result = $this->Form->input('Contact.name', array(
  8156. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8157. ));
  8158. $expected = array(
  8159. 'label' => array('for' => 'my_id'),
  8160. 'Name',
  8161. '/label',
  8162. 'input' => array(
  8163. 'type' => 'text', 'name' => 'data[Contact][name]',
  8164. 'id' => 'my_id', 'maxlength' => '255'
  8165. )
  8166. );
  8167. $this->assertTags($result, $expected);
  8168. $result = $this->Form->input('1.id');
  8169. $this->assertTags($result, array('input' => array(
  8170. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8171. 'id' => 'Contact1Id'
  8172. )));
  8173. $result = $this->Form->input("1.name");
  8174. $expected = array(
  8175. 'div' => array('class' => 'input text'),
  8176. 'label' => array('for' => 'Contact1Name'),
  8177. 'Name',
  8178. '/label',
  8179. 'input' => array(
  8180. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8181. 'id' => 'Contact1Name', 'maxlength' => '255'
  8182. ),
  8183. '/div'
  8184. );
  8185. $this->assertTags($result, $expected);
  8186. $result = $this->Form->input('Contact.1.id');
  8187. $this->assertTags($result, array(
  8188. 'input' => array(
  8189. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8190. 'id' => 'Contact1Id'
  8191. )
  8192. ));
  8193. $result = $this->Form->input("Model.1.name");
  8194. $expected = array(
  8195. 'div' => array('class' => 'input text'),
  8196. 'label' => array('for' => 'Model1Name'),
  8197. 'Name',
  8198. '/label',
  8199. 'input' => array(
  8200. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8201. 'id' => 'Model1Name'
  8202. ),
  8203. '/div'
  8204. );
  8205. $this->assertTags($result, $expected);
  8206. }
  8207. /**
  8208. * testFormEnd method
  8209. *
  8210. * @return void
  8211. */
  8212. public function testFormEnd() {
  8213. $this->assertEquals('</form>', $this->Form->end());
  8214. $result = $this->Form->end('');
  8215. $expected = array(
  8216. 'div' => array('class' => 'submit'),
  8217. 'input' => array('type' => 'submit', 'value' => ''),
  8218. '/div',
  8219. '/form'
  8220. );
  8221. $this->assertTags($result, $expected);
  8222. $result = $this->Form->end(array('label' => ''));
  8223. $expected = array(
  8224. 'div' => array('class' => 'submit'),
  8225. 'input' => array('type' => 'submit', 'value' => ''),
  8226. '/div',
  8227. '/form'
  8228. );
  8229. $this->assertTags($result, $expected);
  8230. $result = $this->Form->end('save');
  8231. $expected = array(
  8232. 'div' => array('class' => 'submit'),
  8233. 'input' => array('type' => 'submit', 'value' => 'save'),
  8234. '/div',
  8235. '/form'
  8236. );
  8237. $this->assertTags($result, $expected);
  8238. $result = $this->Form->end(array('label' => 'save'));
  8239. $expected = array(
  8240. 'div' => array('class' => 'submit'),
  8241. 'input' => array('type' => 'submit', 'value' => 'save'),
  8242. '/div',
  8243. '/form'
  8244. );
  8245. $this->assertTags($result, $expected);
  8246. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8247. $expected = array(
  8248. 'div' => array('class' => 'submit'),
  8249. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8250. '/div',
  8251. '/form'
  8252. );
  8253. $this->assertTags($result, $expected);
  8254. $result = $this->Form->end(array('name' => 'Whatever'));
  8255. $expected = array(
  8256. 'div' => array('class' => 'submit'),
  8257. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8258. '/div',
  8259. '/form'
  8260. );
  8261. $this->assertTags($result, $expected);
  8262. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8263. $expected = array(
  8264. 'div' => array('class' => 'good'),
  8265. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8266. '/div',
  8267. '/form'
  8268. );
  8269. $this->assertTags($result, $expected);
  8270. $result = $this->Form->end(array(
  8271. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8272. ));
  8273. $expected = array(
  8274. 'div' => array('class' => 'good'),
  8275. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8276. '/div',
  8277. '/form'
  8278. );
  8279. $this->assertTags($result, $expected);
  8280. }
  8281. /**
  8282. * testMultipleFormWithIdFields method
  8283. *
  8284. * @return void
  8285. */
  8286. public function testMultipleFormWithIdFields() {
  8287. $this->Form->create('UserForm');
  8288. $result = $this->Form->input('id');
  8289. $this->assertTags($result, array('input' => array(
  8290. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8291. )));
  8292. $result = $this->Form->input('ValidateItem.id');
  8293. $this->assertTags($result, array('input' => array(
  8294. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8295. 'id' => 'ValidateItemId'
  8296. )));
  8297. $result = $this->Form->input('ValidateUser.id');
  8298. $this->assertTags($result, array('input' => array(
  8299. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8300. 'id' => 'ValidateUserId'
  8301. )));
  8302. }
  8303. /**
  8304. * testDbLessModel method
  8305. *
  8306. * @return void
  8307. */
  8308. public function testDbLessModel() {
  8309. $this->Form->create('TestMail');
  8310. $result = $this->Form->input('name');
  8311. $expected = array(
  8312. 'div' => array('class' => 'input text'),
  8313. 'label' => array('for' => 'TestMailName'),
  8314. 'Name',
  8315. '/label',
  8316. 'input' => array(
  8317. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8318. 'id' => 'TestMailName'
  8319. ),
  8320. '/div'
  8321. );
  8322. $this->assertTags($result, $expected);
  8323. ClassRegistry::init('TestMail');
  8324. $this->Form->create('TestMail');
  8325. $result = $this->Form->input('name');
  8326. $expected = array(
  8327. 'div' => array('class' => 'input text'),
  8328. 'label' => array('for' => 'TestMailName'),
  8329. 'Name',
  8330. '/label',
  8331. 'input' => array(
  8332. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8333. 'id' => 'TestMailName'
  8334. ),
  8335. '/div'
  8336. );
  8337. $this->assertTags($result, $expected);
  8338. }
  8339. /**
  8340. * testBrokenness method
  8341. *
  8342. * @return void
  8343. */
  8344. public function testBrokenness() {
  8345. /*
  8346. * #4 This test has two parents and four children. By default (as of r7117) both
  8347. * parents are show but the first parent is missing a child. This is the inconsistency
  8348. * in the default behaviour - one parent has all children, the other does not - dependent
  8349. * on the data values.
  8350. */
  8351. $result = $this->Form->select('Model.field', array(
  8352. 'Fred' => array(
  8353. 'freds_son_1' => 'Fred',
  8354. 'freds_son_2' => 'Freddie'
  8355. ),
  8356. 'Bert' => array(
  8357. 'berts_son_1' => 'Albert',
  8358. 'berts_son_2' => 'Bertie')
  8359. ),
  8360. array('showParents' => true, 'empty' => false)
  8361. );
  8362. $expected = array(
  8363. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8364. array('optgroup' => array('label' => 'Fred')),
  8365. array('option' => array('value' => 'freds_son_1')),
  8366. 'Fred',
  8367. '/option',
  8368. array('option' => array('value' => 'freds_son_2')),
  8369. 'Freddie',
  8370. '/option',
  8371. '/optgroup',
  8372. array('optgroup' => array('label' => 'Bert')),
  8373. array('option' => array('value' => 'berts_son_1')),
  8374. 'Albert',
  8375. '/option',
  8376. array('option' => array('value' => 'berts_son_2')),
  8377. 'Bertie',
  8378. '/option',
  8379. '/optgroup',
  8380. '/select'
  8381. );
  8382. $this->assertTags($result, $expected);
  8383. /*
  8384. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8385. * changed, so we should expect the same select list data, just with a different name
  8386. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8387. * This is where data corruption can occur, because when a select value is missing from
  8388. * a list a form will substitute the first value in the list - without the user knowing.
  8389. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8390. * affect the availability of 3 => 'Three' as a valid option.
  8391. */
  8392. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8393. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8394. ));
  8395. $result = $this->Form->select(
  8396. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8397. );
  8398. $expected = array(
  8399. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8400. array('option' => array('value' => 1)),
  8401. 'One',
  8402. '/option',
  8403. array('option' => array('value' => 2)),
  8404. 'Two',
  8405. '/option',
  8406. array('optgroup' => array('label' => 'Three')),
  8407. array('option' => array('value' => 3)),
  8408. 'Three',
  8409. '/option',
  8410. array('option' => array('value' => 4)),
  8411. 'Four',
  8412. '/option',
  8413. array('option' => array('value' => 5)),
  8414. 'Five',
  8415. '/option',
  8416. '/optgroup',
  8417. '/select'
  8418. );
  8419. $this->assertTags($result, $expected);
  8420. }
  8421. /**
  8422. * Test the generation of fields for a multi record form.
  8423. *
  8424. * @return void
  8425. */
  8426. public function testMultiRecordForm() {
  8427. $this->Form->create('ValidateProfile');
  8428. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8429. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8430. $expected = array(
  8431. 'div' => array('class' => 'input textarea'),
  8432. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8433. 'Name',
  8434. '/label',
  8435. 'textarea' => array(
  8436. 'id' => 'ValidateProfile1ValidateItem2Name',
  8437. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8438. 'cols' => 30,
  8439. 'rows' => 6
  8440. ),
  8441. 'Value',
  8442. '/textarea',
  8443. '/div'
  8444. );
  8445. $this->assertTags($result, $expected);
  8446. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8447. $expected = array(
  8448. 'div' => array('class' => 'input date'),
  8449. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8450. 'Created',
  8451. '/label',
  8452. array('select' => array(
  8453. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8454. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8455. )
  8456. ),
  8457. array('option' => array('value' => '')), '/option',
  8458. $this->dateRegex['monthsRegex'],
  8459. '/select', '-',
  8460. array('select' => array(
  8461. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8462. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8463. )
  8464. ),
  8465. array('option' => array('value' => '')), '/option',
  8466. $this->dateRegex['daysRegex'],
  8467. '/select', '-',
  8468. array('select' => array(
  8469. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8470. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8471. )
  8472. ),
  8473. array('option' => array('value' => '')), '/option',
  8474. $this->dateRegex['yearsRegex'],
  8475. '/select',
  8476. '/div'
  8477. );
  8478. $this->assertTags($result, $expected);
  8479. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8480. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8481. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8482. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8483. $expected = array(
  8484. 'div' => array('class' => 'input select error'),
  8485. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8486. 'Profile',
  8487. '/label',
  8488. 'select' => array(
  8489. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8490. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8491. 'class' => 'form-error'
  8492. ),
  8493. '/select',
  8494. array('div' => array('class' => 'error-message')),
  8495. 'Error',
  8496. '/div',
  8497. '/div'
  8498. );
  8499. $this->assertTags($result, $expected);
  8500. }
  8501. /**
  8502. * test the correct display of multi-record form validation errors.
  8503. *
  8504. * @return void
  8505. */
  8506. public function testMultiRecordFormValidationErrors() {
  8507. $this->Form->create('ValidateProfile');
  8508. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8509. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8510. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8511. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8512. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8513. $result = $this->Form->error('ValidateProfile.2.city');
  8514. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8515. $result = $this->Form->error('2.city');
  8516. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8517. }
  8518. /**
  8519. * test the correct display of multi-record form validation errors.
  8520. *
  8521. * @return void
  8522. */
  8523. public function testSaveManyRecordFormValidationErrors() {
  8524. $this->Form->create('ValidateUser');
  8525. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8526. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8527. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8528. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8529. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8530. $result = $this->Form->error('ValidateUser.0.city');
  8531. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8532. }
  8533. /**
  8534. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8535. *
  8536. * @return void
  8537. */
  8538. public function testInputTemplate() {
  8539. $result = $this->Form->input('Contact.email', array(
  8540. 'type' => 'text', 'format' => array('input')
  8541. ));
  8542. $expected = array(
  8543. 'div' => array('class' => 'input text'),
  8544. 'input' => array(
  8545. 'type' => 'text', 'name' => 'data[Contact][email]',
  8546. 'id' => 'ContactEmail'
  8547. ),
  8548. '/div'
  8549. );
  8550. $this->assertTags($result, $expected);
  8551. $result = $this->Form->input('Contact.email', array(
  8552. 'type' => 'text', 'format' => array('input', 'label'),
  8553. 'label' => '<em>Email (required)</em>'
  8554. ));
  8555. $expected = array(
  8556. 'div' => array('class' => 'input text'),
  8557. array('input' => array(
  8558. 'type' => 'text', 'name' => 'data[Contact][email]',
  8559. 'id' => 'ContactEmail'
  8560. )),
  8561. 'label' => array('for' => 'ContactEmail'),
  8562. 'em' => array(),
  8563. 'Email (required)',
  8564. '/em',
  8565. '/label',
  8566. '/div'
  8567. );
  8568. $this->assertTags($result, $expected);
  8569. $result = $this->Form->input('Contact.email', array(
  8570. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8571. 'between' => '<div>Something in the middle</div>',
  8572. 'after' => '<span>Some text at the end</span>'
  8573. ));
  8574. $expected = array(
  8575. 'div' => array('class' => 'input text'),
  8576. array('input' => array(
  8577. 'type' => 'text', 'name' => 'data[Contact][email]',
  8578. 'id' => 'ContactEmail'
  8579. )),
  8580. array('div' => array()),
  8581. 'Something in the middle',
  8582. '/div',
  8583. 'label' => array('for' => 'ContactEmail'),
  8584. 'Email',
  8585. '/label',
  8586. 'span' => array(),
  8587. 'Some text at the end',
  8588. '/span',
  8589. '/div'
  8590. );
  8591. $this->assertTags($result, $expected);
  8592. $result = $this->Form->input('Contact.method', array(
  8593. 'type' => 'radio',
  8594. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8595. 'between' => 'I am between',
  8596. ));
  8597. $expected = array(
  8598. 'div' => array('class' => 'input radio'),
  8599. 'fieldset' => array(),
  8600. 'legend' => array(),
  8601. 'Method',
  8602. '/legend',
  8603. 'I am between',
  8604. 'input' => array(
  8605. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8606. 'value' => '', 'id' => 'ContactMethod_'
  8607. ),
  8608. array('input' => array(
  8609. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8610. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8611. )),
  8612. array('label' => array('for' => 'ContactMethodEmail')),
  8613. 'Email',
  8614. '/label',
  8615. array('input' => array(
  8616. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8617. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8618. )),
  8619. array('label' => array('for' => 'ContactMethodPigeon')),
  8620. 'Pigeon',
  8621. '/label',
  8622. '/fieldset',
  8623. '/div',
  8624. );
  8625. $this->assertTags($result, $expected);
  8626. }
  8627. /**
  8628. * test that some html5 inputs + FormHelper::__call() work
  8629. *
  8630. * @return void
  8631. */
  8632. public function testHtml5Inputs() {
  8633. $result = $this->Form->email('User.email');
  8634. $expected = array(
  8635. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8636. );
  8637. $this->assertTags($result, $expected);
  8638. $result = $this->Form->search('User.query');
  8639. $expected = array(
  8640. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8641. );
  8642. $this->assertTags($result, $expected);
  8643. $result = $this->Form->search('User.query', array('value' => 'test'));
  8644. $expected = array(
  8645. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8646. );
  8647. $this->assertTags($result, $expected);
  8648. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8649. $expected = array(
  8650. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8651. );
  8652. $this->assertTags($result, $expected);
  8653. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8654. $expected = array(
  8655. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8656. );
  8657. $this->assertTags($result, $expected);
  8658. }
  8659. /**
  8660. *
  8661. * @expectedException CakeException
  8662. * @return void
  8663. */
  8664. public function testHtml5InputException() {
  8665. $this->Form->email();
  8666. }
  8667. /**
  8668. * Tests that a model can be loaded from the model names passed in the request object
  8669. *
  8670. * @return void
  8671. */
  8672. public function testIntrospectModelFromRequest() {
  8673. $this->loadFixtures('Post');
  8674. App::build(array(
  8675. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8676. ));
  8677. CakePlugin::load('TestPlugin');
  8678. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8679. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8680. $this->Form->create('TestPluginPost');
  8681. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8682. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8683. CakePlugin::unload();
  8684. App::build();
  8685. }
  8686. /**
  8687. * Tests that it is possible to set the validation errors directly in the helper for a field
  8688. *
  8689. * @return void
  8690. */
  8691. public function testCustomValidationErrors() {
  8692. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8693. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8694. $this->assertEquals('Badness!', $result);
  8695. }
  8696. /**
  8697. * Tests that the 'on' key validates as expected on create
  8698. *
  8699. * @return void
  8700. */
  8701. public function testRequiredOnCreate() {
  8702. $this->Form->create('Contact');
  8703. $result = $this->Form->input('Contact.imrequiredonupdate');
  8704. $expected = array(
  8705. 'div' => array('class' => 'input text'),
  8706. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8707. 'Imrequiredonupdate',
  8708. '/label',
  8709. 'input' => array(
  8710. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8711. 'id' => 'ContactImrequiredonupdate'
  8712. ),
  8713. '/div'
  8714. );
  8715. $this->assertTags($result, $expected);
  8716. $result = $this->Form->input('Contact.imrequiredoncreate');
  8717. $expected = array(
  8718. 'div' => array('class' => 'input text required'),
  8719. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8720. 'Imrequiredoncreate',
  8721. '/label',
  8722. 'input' => array(
  8723. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8724. 'id' => 'ContactImrequiredoncreate',
  8725. 'required' => 'required'
  8726. ),
  8727. '/div'
  8728. );
  8729. $this->assertTags($result, $expected);
  8730. $result = $this->Form->input('Contact.imrequiredonboth');
  8731. $expected = array(
  8732. 'div' => array('class' => 'input text required'),
  8733. 'label' => array('for' => 'ContactImrequiredonboth'),
  8734. 'Imrequiredonboth',
  8735. '/label',
  8736. 'input' => array(
  8737. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8738. 'id' => 'ContactImrequiredonboth',
  8739. 'required' => 'required'
  8740. ),
  8741. '/div'
  8742. );
  8743. $this->assertTags($result, $expected);
  8744. $this->Form->inputDefaults(array('required' => false));
  8745. $result = $this->Form->input('Contact.imrequired');
  8746. $expected = array(
  8747. 'div' => array('class' => 'input text'),
  8748. 'label' => array('for' => 'ContactImrequired'),
  8749. 'Imrequired',
  8750. '/label',
  8751. 'input' => array(
  8752. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8753. 'id' => 'ContactImrequired'
  8754. ),
  8755. '/div'
  8756. );
  8757. $this->assertTags($result, $expected);
  8758. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8759. $this->assertTags($result, $expected);
  8760. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8761. $expected = array(
  8762. 'div' => array('class' => 'input text required'),
  8763. 'label' => array('for' => 'ContactImrequired'),
  8764. 'Imrequired',
  8765. '/label',
  8766. 'input' => array(
  8767. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8768. 'id' => 'ContactImrequired'
  8769. ),
  8770. '/div'
  8771. );
  8772. $this->assertTags($result, $expected);
  8773. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8774. $this->assertTags($result, $expected);
  8775. }
  8776. /**
  8777. * Tests that the 'on' key validates as expected on update
  8778. *
  8779. * @return void
  8780. */
  8781. public function testRequiredOnUpdate() {
  8782. $this->Form->request->data['Contact']['id'] = 1;
  8783. $this->Form->create('Contact');
  8784. $result = $this->Form->input('Contact.imrequiredonupdate');
  8785. $expected = array(
  8786. 'div' => array('class' => 'input text required'),
  8787. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8788. 'Imrequiredonupdate',
  8789. '/label',
  8790. 'input' => array(
  8791. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8792. 'id' => 'ContactImrequiredonupdate',
  8793. 'required' => 'required'
  8794. ),
  8795. '/div'
  8796. );
  8797. $this->assertTags($result, $expected);
  8798. $result = $this->Form->input('Contact.imrequiredoncreate');
  8799. $expected = array(
  8800. 'div' => array('class' => 'input text'),
  8801. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8802. 'Imrequiredoncreate',
  8803. '/label',
  8804. 'input' => array(
  8805. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8806. 'id' => 'ContactImrequiredoncreate'
  8807. ),
  8808. '/div'
  8809. );
  8810. $this->assertTags($result, $expected);
  8811. $result = $this->Form->input('Contact.imrequiredonboth');
  8812. $expected = array(
  8813. 'div' => array('class' => 'input text required'),
  8814. 'label' => array('for' => 'ContactImrequiredonboth'),
  8815. 'Imrequiredonboth',
  8816. '/label',
  8817. 'input' => array(
  8818. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8819. 'id' => 'ContactImrequiredonboth',
  8820. 'required' => 'required'
  8821. ),
  8822. '/div'
  8823. );
  8824. $this->assertTags($result, $expected);
  8825. $result = $this->Form->input('Contact.imrequired');
  8826. $expected = array(
  8827. 'div' => array('class' => 'input text required'),
  8828. 'label' => array('for' => 'ContactImrequired'),
  8829. 'Imrequired',
  8830. '/label',
  8831. 'input' => array(
  8832. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8833. 'id' => 'ContactImrequired',
  8834. 'required' => 'required'
  8835. ),
  8836. '/div'
  8837. );
  8838. $this->assertTags($result, $expected);
  8839. }
  8840. /**
  8841. * Test inputDefaults setter and getter
  8842. *
  8843. * @return void
  8844. */
  8845. public function testInputDefaults() {
  8846. $this->Form->create('Contact');
  8847. $this->Form->inputDefaults(array(
  8848. 'label' => false,
  8849. 'div' => array(
  8850. 'style' => 'color: #000;'
  8851. )
  8852. ));
  8853. $result = $this->Form->input('Contact.field1');
  8854. $expected = array(
  8855. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8856. 'input' => array(
  8857. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8858. 'id' => 'ContactField1'
  8859. ),
  8860. '/div'
  8861. );
  8862. $this->assertTags($result, $expected);
  8863. $this->Form->inputDefaults(array(
  8864. 'div' => false,
  8865. 'label' => 'Label',
  8866. ));
  8867. $result = $this->Form->input('Contact.field1');
  8868. $expected = array(
  8869. 'label' => array('for' => 'ContactField1'),
  8870. 'Label',
  8871. '/label',
  8872. 'input' => array(
  8873. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8874. 'id' => 'ContactField1'
  8875. ),
  8876. );
  8877. $this->assertTags($result, $expected);
  8878. $this->Form->inputDefaults(array(
  8879. 'label' => false,
  8880. ), true);
  8881. $result = $this->Form->input('Contact.field1');
  8882. $expected = array(
  8883. 'input' => array(
  8884. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8885. 'id' => 'ContactField1'
  8886. ),
  8887. );
  8888. $this->assertTags($result, $expected);
  8889. $result = $this->Form->inputDefaults();
  8890. $expected = array(
  8891. 'div' => false,
  8892. 'label' => false,
  8893. );
  8894. $this->assertEquals($expected, $result);
  8895. }
  8896. }