FormHelperTest.php 300 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426
  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. }
  3348. /**
  3349. * Test radio inputs with between as string or array. Also ensure
  3350. * that an array with less between elements works.
  3351. *
  3352. * @return void
  3353. */
  3354. public function testRadioBetween() {
  3355. $result = $this->Form->radio(
  3356. 'Model.field',
  3357. array('option A', 'option B'),
  3358. array('between' => 'I am between')
  3359. );
  3360. $expected = array(
  3361. 'fieldset' => array(),
  3362. 'legend' => array(),
  3363. 'Field',
  3364. '/legend',
  3365. 'I am between',
  3366. 'input' => array(
  3367. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3368. 'value' => '', 'id' => 'ModelField_'
  3369. ),
  3370. array('input' => array(
  3371. 'type' => 'radio', 'name' => 'data[Model][field]',
  3372. 'value' => '0', 'id' => 'ModelField0'
  3373. )),
  3374. array('label' => array('for' => 'ModelField0')),
  3375. 'option A',
  3376. '/label',
  3377. array('input' => array(
  3378. 'type' => 'radio', 'name' => 'data[Model][field]',
  3379. 'value' => '1', 'id' => 'ModelField1'
  3380. )),
  3381. array('label' => array('for' => 'ModelField1')),
  3382. 'option B',
  3383. '/label',
  3384. '/fieldset'
  3385. );
  3386. $this->assertTags($result, $expected);
  3387. $result = $this->Form->radio(
  3388. 'Model.field',
  3389. array('option A', 'option B', 'option C'),
  3390. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3391. );
  3392. $expected = array(
  3393. 'fieldset' => array(),
  3394. 'legend' => array(),
  3395. 'Field',
  3396. '/legend',
  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. 'between A',
  3409. '--separator--',
  3410. array('input' => array(
  3411. 'type' => 'radio', 'name' => 'data[Model][field]',
  3412. 'value' => '1', 'id' => 'ModelField1'
  3413. )),
  3414. array('label' => array('for' => 'ModelField1')),
  3415. 'option B',
  3416. '/label',
  3417. 'between B',
  3418. '--separator--',
  3419. array('input' => array(
  3420. 'type' => 'radio', 'name' => 'data[Model][field]',
  3421. 'value' => '2', 'id' => 'ModelField2'
  3422. )),
  3423. array('label' => array('for' => 'ModelField2')),
  3424. 'option C',
  3425. '/label',
  3426. 'between C',
  3427. '/fieldset'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $result = $this->Form->input('Model.field', array(
  3431. 'options' => array('1' => 'first', '2' => 'second'),
  3432. 'type' => 'radio',
  3433. 'before' => '--before--',
  3434. 'after' => '--after--',
  3435. 'separator' => '--separator--',
  3436. 'between' => array('--between first--', '--between second--')
  3437. ));
  3438. $expected = array(
  3439. 'div' => array('class' => 'input radio'),
  3440. '--before--',
  3441. 'fieldset' => array(),
  3442. 'legend' => array(),
  3443. 'Field',
  3444. '/legend',
  3445. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3446. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3447. array('label' => array('for' => 'ModelField1')),
  3448. 'first',
  3449. '/label',
  3450. '--between first--',
  3451. '--separator--',
  3452. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3453. array('label' => array('for' => 'ModelField2')),
  3454. 'second',
  3455. '/label',
  3456. '--between second--',
  3457. '/fieldset',
  3458. '--after--',
  3459. '/div'
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. $result = $this->Form->input('Model.field', array(
  3463. 'options' => array('1' => 'first', '2' => 'second'),
  3464. 'type' => 'radio',
  3465. 'before' => '--before--',
  3466. 'after' => '--after--',
  3467. 'separator' => '--separator--',
  3468. 'between' => array('--between first--')
  3469. ));
  3470. $expected = array(
  3471. 'div' => array('class' => 'input radio'),
  3472. '--before--',
  3473. 'fieldset' => array(),
  3474. 'legend' => array(),
  3475. 'Field',
  3476. '/legend',
  3477. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3478. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3479. array('label' => array('for' => 'ModelField1')),
  3480. 'first',
  3481. '/label',
  3482. '--between first--',
  3483. '--separator--',
  3484. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3485. array('label' => array('for' => 'ModelField2')),
  3486. 'second',
  3487. '/label',
  3488. '/fieldset',
  3489. '--after--',
  3490. '/div'
  3491. );
  3492. $this->assertTags($result, $expected);
  3493. }
  3494. /**
  3495. * Test that radios with a 0 value are selected under the correct conditions.
  3496. * Also ensure that values that are booleanish are handled correctly.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testRadioOptionWithBooleanishValues() {
  3501. $expected = array(
  3502. 'fieldset' => array(),
  3503. 'legend' => array(),
  3504. 'Field',
  3505. '/legend',
  3506. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3507. array('label' => array('for' => 'ModelField1')),
  3508. 'Yes',
  3509. '/label',
  3510. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3511. array('label' => array('for' => 'ModelField0')),
  3512. 'No',
  3513. '/label',
  3514. '/fieldset'
  3515. );
  3516. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3519. $this->assertTags($result, $expected);
  3520. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3521. $this->assertTags($result, $expected);
  3522. $expected = array(
  3523. 'fieldset' => array(),
  3524. 'legend' => array(),
  3525. 'Field',
  3526. '/legend',
  3527. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3528. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3529. array('label' => array('for' => 'ModelField1')),
  3530. 'Yes',
  3531. '/label',
  3532. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3533. array('label' => array('for' => 'ModelField0')),
  3534. 'No',
  3535. '/label',
  3536. '/fieldset'
  3537. );
  3538. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3539. $this->assertTags($result, $expected);
  3540. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3541. $this->assertTags($result, $expected);
  3542. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3543. $this->assertTags($result, $expected);
  3544. $expected = array(
  3545. 'fieldset' => array(),
  3546. 'legend' => array(),
  3547. 'Field',
  3548. '/legend',
  3549. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3550. array('label' => array('for' => 'ModelField1')),
  3551. 'Yes',
  3552. '/label',
  3553. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3554. array('label' => array('for' => 'ModelField0')),
  3555. 'No',
  3556. '/label',
  3557. '/fieldset'
  3558. );
  3559. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3560. $this->assertTags($result, $expected);
  3561. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3562. $this->assertTags($result, $expected);
  3563. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3564. $this->assertTags($result, $expected);
  3565. }
  3566. /**
  3567. * test disabled radio options
  3568. *
  3569. * @return void
  3570. */
  3571. public function testRadioDisabled() {
  3572. $result = $this->Form->radio(
  3573. 'Model.field',
  3574. array('option A', 'option B'),
  3575. array('disabled' => array(0), 'value' => '0')
  3576. );
  3577. $expected = array(
  3578. 'fieldset' => array(),
  3579. 'legend' => array(),
  3580. 'Field',
  3581. '/legend',
  3582. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3583. array('label' => array('for' => 'ModelField0')),
  3584. 'option A',
  3585. '/label',
  3586. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3587. array('label' => array('for' => 'ModelField1')),
  3588. 'option B',
  3589. '/label',
  3590. '/fieldset'
  3591. );
  3592. $this->assertTags($result, $expected);
  3593. $result = $this->Form->radio(
  3594. 'Model.field',
  3595. array('option A', 'option B'),
  3596. array('disabled' => true, 'value' => '0')
  3597. );
  3598. $expected = array(
  3599. 'fieldset' => array(),
  3600. 'legend' => array(),
  3601. 'Field',
  3602. '/legend',
  3603. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3604. array('label' => array('for' => 'ModelField0')),
  3605. 'option A',
  3606. '/label',
  3607. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3608. array('label' => array('for' => 'ModelField1')),
  3609. 'option B',
  3610. '/label',
  3611. '/fieldset'
  3612. );
  3613. $this->assertTags($result, $expected);
  3614. $result = $this->Form->radio(
  3615. 'Model.field',
  3616. array('option A', 'option B'),
  3617. array('disabled' => 'disabled', 'value' => '0')
  3618. );
  3619. $expected = array(
  3620. 'fieldset' => array(),
  3621. 'legend' => array(),
  3622. 'Field',
  3623. '/legend',
  3624. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3625. array('label' => array('for' => 'ModelField0')),
  3626. 'option A',
  3627. '/label',
  3628. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3629. array('label' => array('for' => 'ModelField1')),
  3630. 'option B',
  3631. '/label',
  3632. '/fieldset'
  3633. );
  3634. $this->assertTags($result, $expected);
  3635. $result = $this->Form->input('Model.field', array(
  3636. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3637. 'type' => 'radio',
  3638. 'disabled' => array(2, '3x'),
  3639. ));
  3640. $expected = array(
  3641. 'div' => array('class' => 'input radio'),
  3642. 'fieldset' => array(),
  3643. 'legend' => array(),
  3644. 'Field',
  3645. '/legend',
  3646. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3647. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3648. array('label' => array('for' => 'ModelField1')),
  3649. 'first',
  3650. '/label',
  3651. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3652. array('label' => array('for' => 'ModelField2')),
  3653. 'second',
  3654. '/label',
  3655. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3656. array('label' => array('for' => 'ModelField2x')),
  3657. '2x',
  3658. '/label',
  3659. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3660. array('label' => array('for' => 'ModelField3')),
  3661. 'third',
  3662. '/label',
  3663. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3664. array('label' => array('for' => 'ModelField3x')),
  3665. '3x',
  3666. '/label',
  3667. '/fieldset',
  3668. '/div'
  3669. );
  3670. $this->assertTags($result, $expected);
  3671. $result = $this->Form->input('Model.field', array(
  3672. 'type' => 'radio',
  3673. 'options' => array(
  3674. 1 => 'A',
  3675. 2 => 'B',
  3676. 3 => 'C'
  3677. ),
  3678. 'disabled' => array(1)
  3679. ));
  3680. $expected = array(
  3681. 'div' => array('class' => 'input radio'),
  3682. 'fieldset' => array(),
  3683. 'legend' => array(),
  3684. 'Field',
  3685. '/legend',
  3686. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3687. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3688. array('label' => array('for' => 'ModelField1')),
  3689. 'A',
  3690. '/label',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3692. array('label' => array('for' => 'ModelField2')),
  3693. 'B',
  3694. '/label',
  3695. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3696. array('label' => array('for' => 'ModelField3')),
  3697. 'C',
  3698. '/label',
  3699. '/fieldset',
  3700. '/div'
  3701. );
  3702. $this->assertTags($result, $expected);
  3703. }
  3704. /**
  3705. * test disabling the hidden input for radio buttons
  3706. *
  3707. * @return void
  3708. */
  3709. public function testRadioHiddenInputDisabling() {
  3710. $result = $this->Form->input('Model.1.field', array(
  3711. 'type' => 'radio',
  3712. 'options' => array('option A'),
  3713. 'hiddenField' => false
  3714. )
  3715. );
  3716. $expected = array(
  3717. 'div' => array('class' => 'input radio'),
  3718. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3719. 'label' => array('for' => 'Model1Field0'),
  3720. 'option A',
  3721. '/label',
  3722. '/div'
  3723. );
  3724. $this->assertTags($result, $expected);
  3725. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3726. $expected = array(
  3727. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3728. 'label' => array('for' => 'Model1Field0'),
  3729. 'option A',
  3730. '/label'
  3731. );
  3732. $this->assertTags($result, $expected);
  3733. }
  3734. /**
  3735. * test adding an empty option for radio buttons
  3736. *
  3737. * @return void
  3738. */
  3739. public function testRadioAddEmptyOption() {
  3740. $result = $this->Form->input('Model.1.field', array(
  3741. 'type' => 'radio',
  3742. 'options' => array('option A'),
  3743. 'empty' => true,
  3744. 'hiddenField' => false
  3745. ));
  3746. $expected = array(
  3747. 'div' => array('class' => 'input radio'),
  3748. 'fieldset' => array(),
  3749. 'legend' => array(),
  3750. 'Field',
  3751. '/legend',
  3752. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3753. array('label' => array('for' => 'Model1Field')),
  3754. __('empty'),
  3755. '/label',
  3756. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3757. array('label' => array('for' => 'Model1Field0')),
  3758. 'option A',
  3759. '/label',
  3760. '/fieldset',
  3761. '/div'
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. $result = $this->Form->input('Model.1.field', array(
  3765. 'type' => 'radio',
  3766. 'options' => array('option A'),
  3767. 'empty' => 'CustomEmptyLabel',
  3768. 'hiddenField' => false
  3769. ));
  3770. $expected = array(
  3771. 'div' => array('class' => 'input radio'),
  3772. 'fieldset' => array(),
  3773. 'legend' => array(),
  3774. 'Field',
  3775. '/legend',
  3776. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3777. array('label' => array('for' => 'Model1Field')),
  3778. 'CustomEmptyLabel',
  3779. '/label',
  3780. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3781. array('label' => array('for' => 'Model1Field0')),
  3782. 'option A',
  3783. '/label',
  3784. '/fieldset',
  3785. '/div'
  3786. );
  3787. $this->assertTags($result, $expected);
  3788. $result = $this->Form->input('Model.1.field', array(
  3789. 'type' => 'radio',
  3790. 'options' => array('option A'),
  3791. 'empty' => false,
  3792. 'hiddenField' => false
  3793. ));
  3794. $this->assertTextNotContains('"Model1Field"', $result);
  3795. }
  3796. /**
  3797. * Test that radio() accepts an array for label
  3798. *
  3799. * @return void
  3800. */
  3801. public function testRadioLabelArray() {
  3802. $result = $this->Form->input('Model.field', array(
  3803. 'type' => 'radio',
  3804. 'legend' => false,
  3805. 'label' => array(
  3806. 'class' => 'checkbox float-left',
  3807. ),
  3808. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3809. ));
  3810. $this->assertTextContains(
  3811. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3812. $result
  3813. );
  3814. }
  3815. /**
  3816. * Test that label id's match the input element id's when radio is called after create().
  3817. *
  3818. * @return void
  3819. */
  3820. public function testRadioWithCreate() {
  3821. $this->Form->create('Model');
  3822. $result = $this->Form->radio('recipient',
  3823. array('1' => '1', '2' => '2', '3' => '3'),
  3824. array('legend' => false, 'value' => '1')
  3825. );
  3826. $this->assertTextNotContains(
  3827. '<label for="ModelModelRecipient1">1</label>',
  3828. $result
  3829. );
  3830. $this->assertTextContains(
  3831. '<label for="ModelRecipient1">1</label>',
  3832. $result
  3833. );
  3834. }
  3835. /**
  3836. * testSelect method
  3837. *
  3838. * Test select element generation.
  3839. *
  3840. * @return void
  3841. */
  3842. public function testSelect() {
  3843. $result = $this->Form->select('Model.field', array());
  3844. $expected = array(
  3845. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3846. array('option' => array('value' => '')),
  3847. '/option',
  3848. '/select'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3852. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3853. $expected = array(
  3854. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3855. array('option' => array('value' => '')),
  3856. '/option',
  3857. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3858. 'good',
  3859. '/option',
  3860. array('option' => array('value' => 'other')),
  3861. 'bad',
  3862. '/option',
  3863. '/select'
  3864. );
  3865. $this->assertTags($result, $expected);
  3866. $this->Form->request->data = array();
  3867. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3868. $expected = array(
  3869. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3870. array('option' => array('value' => '')),
  3871. '/option',
  3872. array('option' => array('value' => 'value')),
  3873. 'good',
  3874. '/option',
  3875. array('option' => array('value' => 'other')),
  3876. 'bad',
  3877. '/option',
  3878. '/select'
  3879. );
  3880. $this->assertTags($result, $expected);
  3881. $result = $this->Form->select(
  3882. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3883. array('empty' => false)
  3884. );
  3885. $expected = array(
  3886. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3887. array('option' => array('value' => 'first')),
  3888. 'first &quot;html&quot; &lt;chars&gt;',
  3889. '/option',
  3890. array('option' => array('value' => 'second')),
  3891. 'value',
  3892. '/option',
  3893. '/select'
  3894. );
  3895. $this->assertTags($result, $expected);
  3896. $result = $this->Form->select(
  3897. 'Model.field',
  3898. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3899. array('escape' => false, 'empty' => false)
  3900. );
  3901. $expected = array(
  3902. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3903. array('option' => array('value' => 'first')),
  3904. 'first "html" <chars>',
  3905. '/option',
  3906. array('option' => array('value' => 'second')),
  3907. 'value',
  3908. '/option',
  3909. '/select'
  3910. );
  3911. $this->assertTags($result, $expected);
  3912. $options = array(
  3913. array('value' => 'first', 'name' => 'First'),
  3914. array('value' => 'first', 'name' => 'Another First'),
  3915. );
  3916. $result = $this->Form->select(
  3917. 'Model.field',
  3918. $options,
  3919. array('escape' => false, 'empty' => false)
  3920. );
  3921. $expected = array(
  3922. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3923. array('option' => array('value' => 'first')),
  3924. 'First',
  3925. '/option',
  3926. array('option' => array('value' => 'first')),
  3927. 'Another First',
  3928. '/option',
  3929. '/select'
  3930. );
  3931. $this->assertTags($result, $expected);
  3932. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3933. $result = $this->Form->select(
  3934. 'Model.contact_id',
  3935. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3936. array('escape' => false, 'empty' => 'pick something')
  3937. );
  3938. $expected = array(
  3939. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3940. array('option' => array('value' => '')), 'pick something', '/option',
  3941. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3942. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3943. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3944. '/select'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. $this->Form->request->data['Model']['field'] = 0;
  3948. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3949. $expected = array(
  3950. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3951. array('option' => array('value' => '')), '/option',
  3952. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3953. array('option' => array('value' => '1')), 'Yes', '/option',
  3954. '/select'
  3955. );
  3956. $this->assertTags($result, $expected);
  3957. $this->Form->request->data['Model']['field'] = 50;
  3958. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3959. $expected = array(
  3960. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3961. array('option' => array('value' => '')), '/option',
  3962. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3963. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3964. '/select'
  3965. );
  3966. $this->assertTags($result, $expected);
  3967. $result = $this->Form->select('Contact.required_one', array('option A'));
  3968. $expected = array(
  3969. 'select' => array(
  3970. 'name' => 'data[Contact][required_one]',
  3971. 'id' => 'ContactRequiredOne',
  3972. 'required' => 'required'
  3973. ),
  3974. array('option' => array('value' => '')), '/option',
  3975. array('option' => array('value' => '0')), 'option A', '/option',
  3976. '/select'
  3977. );
  3978. $this->assertTags($result, $expected);
  3979. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3980. $expected = array(
  3981. 'select' => array(
  3982. 'name' => 'data[Contact][required_one]',
  3983. 'id' => 'ContactRequiredOne',
  3984. 'disabled' => 'disabled'
  3985. ),
  3986. array('option' => array('value' => '')), '/option',
  3987. array('option' => array('value' => '0')), 'option A', '/option',
  3988. '/select'
  3989. );
  3990. $this->assertTags($result, $expected);
  3991. }
  3992. /**
  3993. * test that select() with optiongroups listens to the escape param.
  3994. *
  3995. * @return void
  3996. */
  3997. public function testSelectOptionGroupEscaping() {
  3998. $options = array(
  3999. '>< Key' => array(
  4000. 1 => 'One',
  4001. 2 => 'Two'
  4002. )
  4003. );
  4004. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4005. $expected = array(
  4006. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4007. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4008. array('option' => array('value' => '1')), 'One', '/option',
  4009. array('option' => array('value' => '2')), 'Two', '/option',
  4010. '/optgroup',
  4011. '/select'
  4012. );
  4013. $this->assertTags($result, $expected);
  4014. $options = array(
  4015. '>< Key' => array(
  4016. 1 => 'One',
  4017. 2 => 'Two'
  4018. )
  4019. );
  4020. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4021. $expected = array(
  4022. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4023. 'optgroup' => array('label' => '>< Key'),
  4024. array('option' => array('value' => '1')), 'One', '/option',
  4025. array('option' => array('value' => '2')), 'Two', '/option',
  4026. '/optgroup',
  4027. '/select'
  4028. );
  4029. $this->assertTags($result, $expected);
  4030. }
  4031. /**
  4032. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4033. *
  4034. * @return void
  4035. */
  4036. public function testSelectWithNullAttributes() {
  4037. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4038. $expected = array(
  4039. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4040. array('option' => array('value' => '0')),
  4041. 'first',
  4042. '/option',
  4043. array('option' => array('value' => '1')),
  4044. 'second',
  4045. '/option',
  4046. '/select'
  4047. );
  4048. $this->assertTags($result, $expected);
  4049. }
  4050. /**
  4051. * testNestedSelect method
  4052. *
  4053. * test select element generation with optgroups
  4054. *
  4055. * @return void
  4056. */
  4057. public function testNestedSelect() {
  4058. $result = $this->Form->select(
  4059. 'Model.field',
  4060. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4061. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4062. )), array('empty' => false)
  4063. );
  4064. $expected = array(
  4065. 'select' => array('name' => 'data[Model][field]',
  4066. 'id' => 'ModelField'),
  4067. array('option' => array('value' => 1)),
  4068. 'One',
  4069. '/option',
  4070. array('option' => array('value' => 2)),
  4071. 'Two',
  4072. '/option',
  4073. array('optgroup' => array('label' => 'Three')),
  4074. array('option' => array('value' => 4)),
  4075. 'Four',
  4076. '/option',
  4077. array('option' => array('value' => 5)),
  4078. 'Five',
  4079. '/option',
  4080. '/optgroup',
  4081. '/select'
  4082. );
  4083. $this->assertTags($result, $expected);
  4084. $result = $this->Form->select(
  4085. 'Model.field',
  4086. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4087. array('showParents' => true, 'empty' => false)
  4088. );
  4089. $expected = array(
  4090. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4091. array('option' => array('value' => 1)),
  4092. 'One',
  4093. '/option',
  4094. array('option' => array('value' => 2)),
  4095. 'Two',
  4096. '/option',
  4097. array('optgroup' => array('label' => 'Three')),
  4098. array('option' => array('value' => 3)),
  4099. 'Three',
  4100. '/option',
  4101. array('option' => array('value' => 4)),
  4102. 'Four',
  4103. '/option',
  4104. '/optgroup',
  4105. '/select'
  4106. );
  4107. $this->assertTags($result, $expected);
  4108. }
  4109. /**
  4110. * testSelectMultiple method
  4111. *
  4112. * test generation of multiple select elements
  4113. *
  4114. * @return void
  4115. */
  4116. public function testSelectMultiple() {
  4117. $options = array('first', 'second', 'third');
  4118. $result = $this->Form->select(
  4119. 'Model.multi_field', $options, array('multiple' => true)
  4120. );
  4121. $expected = array(
  4122. 'input' => array(
  4123. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4124. ),
  4125. 'select' => array(
  4126. 'name' => 'data[Model][multi_field][]',
  4127. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4128. ),
  4129. array('option' => array('value' => '0')),
  4130. 'first',
  4131. '/option',
  4132. array('option' => array('value' => '1')),
  4133. 'second',
  4134. '/option',
  4135. array('option' => array('value' => '2')),
  4136. 'third',
  4137. '/option',
  4138. '/select'
  4139. );
  4140. $this->assertTags($result, $expected);
  4141. $result = $this->Form->select(
  4142. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4143. );
  4144. $expected = array(
  4145. 'input' => array(
  4146. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4147. ),
  4148. 'select' => array(
  4149. 'name' => 'data[Model][multi_field][]',
  4150. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4151. ),
  4152. array('option' => array('value' => '0')),
  4153. 'first',
  4154. '/option',
  4155. array('option' => array('value' => '1')),
  4156. 'second',
  4157. '/option',
  4158. array('option' => array('value' => '2')),
  4159. 'third',
  4160. '/option',
  4161. '/select'
  4162. );
  4163. $this->assertTags($result, $expected);
  4164. $result = $this->Form->select(
  4165. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4166. );
  4167. $expected = array(
  4168. 'input' => array(
  4169. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4170. ),
  4171. 'select' => array(
  4172. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4173. 'multiple' => 'multiple'
  4174. ),
  4175. array('option' => array('value' => '0', 'selected' => 'selected')),
  4176. 'first',
  4177. '/option',
  4178. array('option' => array('value' => '1', 'selected' => 'selected')),
  4179. 'second',
  4180. '/option',
  4181. array('option' => array('value' => '2')),
  4182. 'third',
  4183. '/option',
  4184. '/select'
  4185. );
  4186. $this->assertTags($result, $expected);
  4187. $result = $this->Form->select(
  4188. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4189. );
  4190. $expected = array(
  4191. 'select' => array(
  4192. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4193. ),
  4194. array('option' => array('value' => '0', 'selected' => 'selected')),
  4195. 'first',
  4196. '/option',
  4197. array('option' => array('value' => '1', 'selected' => 'selected')),
  4198. 'second',
  4199. '/option',
  4200. array('option' => array('value' => '2')),
  4201. 'third',
  4202. '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4207. $selected = array('2', '3x');
  4208. $result = $this->Form->select(
  4209. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4210. );
  4211. $expected = array(
  4212. 'input' => array(
  4213. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4214. ),
  4215. 'select' => array(
  4216. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4217. ),
  4218. array('option' => array('value' => '1')),
  4219. 'One',
  4220. '/option',
  4221. array('option' => array('value' => '2', 'selected' => 'selected')),
  4222. 'Two',
  4223. '/option',
  4224. array('option' => array('value' => '3')),
  4225. 'Three',
  4226. '/option',
  4227. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4228. 'Stringy',
  4229. '/option',
  4230. '/select'
  4231. );
  4232. $this->assertTags($result, $expected);
  4233. $result = $this->Form->select('Contact.required_one', array(
  4234. '1' => 'option A',
  4235. '2' => 'option B'
  4236. ), array('multiple' => true));
  4237. $expected = array(
  4238. 'input' => array(
  4239. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4240. ),
  4241. 'select' => array(
  4242. 'name' => 'data[Contact][required_one][]',
  4243. 'id' => 'ContactRequiredOne',
  4244. 'required' => 'required',
  4245. 'multiple' => 'multiple'
  4246. ),
  4247. array('option' => array('value' => '1')), 'option A', '/option',
  4248. array('option' => array('value' => '2')), 'option B', '/option',
  4249. '/select'
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. }
  4253. /**
  4254. * Test generating multiple select with disabled elements.
  4255. *
  4256. * @return void
  4257. */
  4258. public function testSelectMultipleWithDisabledElements() {
  4259. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4260. $disabled = array(2, 3);
  4261. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4262. $expected = array(
  4263. 'input' => array(
  4264. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4265. ),
  4266. 'select' => array(
  4267. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4268. ),
  4269. array('option' => array('value' => '1')),
  4270. 'One',
  4271. '/option',
  4272. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4273. 'Two',
  4274. '/option',
  4275. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4276. 'Three',
  4277. '/option',
  4278. array('option' => array('value' => '3x')),
  4279. 'Stringy',
  4280. '/option',
  4281. '/select'
  4282. );
  4283. $this->assertTags($result, $expected);
  4284. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4285. $disabled = array('2', '3x');
  4286. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4287. $expected = array(
  4288. array('div' => array('class' => 'input select')),
  4289. array('label' => array('for' => 'ContactMultiple')),
  4290. 'Multiple',
  4291. '/label',
  4292. 'input' => array(
  4293. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4294. ),
  4295. 'select' => array(
  4296. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4297. ),
  4298. array('option' => array('value' => '1')),
  4299. 'One',
  4300. '/option',
  4301. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4302. 'Two',
  4303. '/option',
  4304. array('option' => array('value' => '3')),
  4305. 'Three',
  4306. '/option',
  4307. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4308. 'Stringy',
  4309. '/option',
  4310. '/select',
  4311. '/div'
  4312. );
  4313. $this->assertTags($result, $expected);
  4314. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4315. $disabled = true;
  4316. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4317. $expected = array(
  4318. array('div' => array('class' => 'input select')),
  4319. array('label' => array('for' => 'ContactMultiple')),
  4320. 'Multiple',
  4321. '/label',
  4322. 'input' => array(
  4323. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4324. ),
  4325. 'select' => array(
  4326. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4327. ),
  4328. array('option' => array('value' => '1')),
  4329. 'One',
  4330. '/option',
  4331. array('option' => array('value' => '2')),
  4332. 'Two',
  4333. '/option',
  4334. array('option' => array('value' => '3')),
  4335. 'Three',
  4336. '/option',
  4337. array('option' => array('value' => '3x')),
  4338. 'Stringy',
  4339. '/option',
  4340. '/select',
  4341. '/div'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. }
  4345. /**
  4346. * Test generating select with disabled elements.
  4347. *
  4348. * @return void
  4349. */
  4350. public function testSelectWithDisabledElements() {
  4351. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4352. $disabled = array(2, 3);
  4353. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4354. $expected = array(
  4355. 'select' => array(
  4356. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4357. ),
  4358. array('option' => array('value' => '')),
  4359. '/option',
  4360. array('option' => array('value' => '1')),
  4361. 'One',
  4362. '/option',
  4363. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4364. 'Two',
  4365. '/option',
  4366. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4367. 'Three',
  4368. '/option',
  4369. array('option' => array('value' => '3x')),
  4370. 'Stringy',
  4371. '/option',
  4372. '/select'
  4373. );
  4374. $this->assertTags($result, $expected);
  4375. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4376. $disabled = array('2', '3x');
  4377. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4378. $expected = array(
  4379. array('div' => array('class' => 'input select')),
  4380. array('label' => array('for' => 'ModelField')),
  4381. 'Field',
  4382. '/label',
  4383. 'select' => array(
  4384. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4385. ),
  4386. array('option' => array('value' => '1')),
  4387. 'One',
  4388. '/option',
  4389. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4390. 'Two',
  4391. '/option',
  4392. array('option' => array('value' => '3')),
  4393. 'Three',
  4394. '/option',
  4395. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4396. 'Stringy',
  4397. '/option',
  4398. '/select',
  4399. '/div'
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4403. $disabled = true;
  4404. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4405. $expected = array(
  4406. array('div' => array('class' => 'input select')),
  4407. array('label' => array('for' => 'ModelField')),
  4408. 'Field',
  4409. '/label',
  4410. 'select' => array(
  4411. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4412. ),
  4413. array('option' => array('value' => '1')),
  4414. 'One',
  4415. '/option',
  4416. array('option' => array('value' => '2')),
  4417. 'Two',
  4418. '/option',
  4419. array('option' => array('value' => '3')),
  4420. 'Three',
  4421. '/option',
  4422. array('option' => array('value' => '3x')),
  4423. 'Stringy',
  4424. '/option',
  4425. '/select',
  4426. '/div'
  4427. );
  4428. $this->assertTags($result, $expected);
  4429. }
  4430. /**
  4431. * test generation of habtm select boxes.
  4432. *
  4433. * @return void
  4434. */
  4435. public function testHabtmSelectBox() {
  4436. $this->View->viewVars['contactTags'] = array(
  4437. 1 => 'blue',
  4438. 2 => 'red',
  4439. 3 => 'green'
  4440. );
  4441. $this->Form->request->data = array(
  4442. 'Contact' => array(),
  4443. 'ContactTag' => array(
  4444. array(
  4445. 'id' => '1',
  4446. 'name' => 'blue'
  4447. ),
  4448. array(
  4449. 'id' => 3,
  4450. 'name' => 'green'
  4451. )
  4452. )
  4453. );
  4454. $this->Form->create('Contact');
  4455. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4456. $expected = array(
  4457. 'input' => array(
  4458. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4459. ),
  4460. 'select' => array(
  4461. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4462. 'multiple' => 'multiple'
  4463. ),
  4464. array('option' => array('value' => '1', 'selected' => 'selected')),
  4465. 'blue',
  4466. '/option',
  4467. array('option' => array('value' => '2')),
  4468. 'red',
  4469. '/option',
  4470. array('option' => array('value' => '3', 'selected' => 'selected')),
  4471. 'green',
  4472. '/option',
  4473. '/select'
  4474. );
  4475. $this->assertTags($result, $expected);
  4476. // make sure only 50 is selected, and not 50f5c0cf
  4477. $this->View->viewVars['contactTags'] = array(
  4478. '1' => 'blue',
  4479. '50f5c0cf' => 'red',
  4480. '50' => 'green'
  4481. );
  4482. $this->Form->request->data = array(
  4483. 'Contact' => array(),
  4484. 'ContactTag' => array(
  4485. array(
  4486. 'id' => 1,
  4487. 'name' => 'blue'
  4488. ),
  4489. array(
  4490. 'id' => 50,
  4491. 'name' => 'green'
  4492. )
  4493. )
  4494. );
  4495. $this->Form->create('Contact');
  4496. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4497. $expected = array(
  4498. 'input' => array(
  4499. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4500. ),
  4501. 'select' => array(
  4502. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4503. 'multiple' => 'multiple'
  4504. ),
  4505. array('option' => array('value' => '1', 'selected' => 'selected')),
  4506. 'blue',
  4507. '/option',
  4508. array('option' => array('value' => '50f5c0cf')),
  4509. 'red',
  4510. '/option',
  4511. array('option' => array('value' => '50', 'selected' => 'selected')),
  4512. 'green',
  4513. '/option',
  4514. '/select'
  4515. );
  4516. $this->assertTags($result, $expected);
  4517. }
  4518. /**
  4519. * test generation of multi select elements in checkbox format
  4520. *
  4521. * @return void
  4522. */
  4523. public function testSelectMultipleCheckboxes() {
  4524. $result = $this->Form->select(
  4525. 'Model.multi_field',
  4526. array('first', 'second', 'third'),
  4527. array('multiple' => 'checkbox')
  4528. );
  4529. $expected = array(
  4530. 'input' => array(
  4531. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4532. ),
  4533. array('div' => array('class' => 'checkbox')),
  4534. array('input' => array(
  4535. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4536. 'value' => '0', 'id' => 'ModelMultiField0'
  4537. )),
  4538. array('label' => array('for' => 'ModelMultiField0')),
  4539. 'first',
  4540. '/label',
  4541. '/div',
  4542. array('div' => array('class' => 'checkbox')),
  4543. array('input' => array(
  4544. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4545. 'value' => '1', 'id' => 'ModelMultiField1'
  4546. )),
  4547. array('label' => array('for' => 'ModelMultiField1')),
  4548. 'second',
  4549. '/label',
  4550. '/div',
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array(
  4553. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4554. 'value' => '2', 'id' => 'ModelMultiField2'
  4555. )),
  4556. array('label' => array('for' => 'ModelMultiField2')),
  4557. 'third',
  4558. '/label',
  4559. '/div'
  4560. );
  4561. $this->assertTags($result, $expected);
  4562. $result = $this->Form->select(
  4563. 'Model.multi_field',
  4564. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4565. array('multiple' => 'checkbox')
  4566. );
  4567. $expected = array(
  4568. 'input' => array(
  4569. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4570. ),
  4571. array('div' => array('class' => 'checkbox')),
  4572. array('input' => array(
  4573. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4574. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4575. )),
  4576. array('label' => array('for' => 'ModelMultiFieldA')),
  4577. 'first',
  4578. '/label',
  4579. '/div',
  4580. array('div' => array('class' => 'checkbox')),
  4581. array('input' => array(
  4582. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4583. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4584. )),
  4585. array('label' => array('for' => 'ModelMultiFieldB')),
  4586. 'second',
  4587. '/label',
  4588. '/div',
  4589. array('div' => array('class' => 'checkbox')),
  4590. array('input' => array(
  4591. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4592. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4593. )),
  4594. array('label' => array('for' => 'ModelMultiFieldC')),
  4595. 'third',
  4596. '/label',
  4597. '/div'
  4598. );
  4599. $this->assertTags($result, $expected);
  4600. $result = $this->Form->select(
  4601. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4602. );
  4603. $expected = array(
  4604. 'input' => array(
  4605. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4606. ),
  4607. array('div' => array('class' => 'checkbox')),
  4608. array('input' => array(
  4609. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4610. 'value' => '1', 'id' => 'ModelMultiField1'
  4611. )),
  4612. array('label' => array('for' => 'ModelMultiField1')),
  4613. 'first',
  4614. '/label',
  4615. '/div'
  4616. );
  4617. $this->assertTags($result, $expected);
  4618. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4619. $result = $this->Form->select(
  4620. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4621. );
  4622. $expected = array(
  4623. 'input' => array(
  4624. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4625. ),
  4626. array('div' => array('class' => 'checkbox')),
  4627. array('input' => array(
  4628. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4629. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4630. )),
  4631. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4632. 'first',
  4633. '/label',
  4634. '/div',
  4635. array('div' => array('class' => 'checkbox')),
  4636. array('input' => array(
  4637. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4638. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4639. )),
  4640. array('label' => array('for' => 'ModelTagsArray')),
  4641. 'Array',
  4642. '/label',
  4643. '/div'
  4644. );
  4645. $this->assertTags($result, $expected);
  4646. }
  4647. /**
  4648. * test multiple checkboxes with div styles.
  4649. *
  4650. * @return void
  4651. */
  4652. public function testSelectMultipleCheckboxDiv() {
  4653. $result = $this->Form->select(
  4654. 'Model.tags',
  4655. array('first', 'second'),
  4656. array('multiple' => 'checkbox', 'class' => 'my-class')
  4657. );
  4658. $expected = array(
  4659. 'input' => array(
  4660. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4661. ),
  4662. array('div' => array('class' => 'my-class')),
  4663. array('input' => array(
  4664. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4665. 'value' => '0', 'id' => 'ModelTags0'
  4666. )),
  4667. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4668. '/div',
  4669. array('div' => array('class' => 'my-class')),
  4670. array('input' => array(
  4671. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4672. 'value' => '1', 'id' => 'ModelTags1'
  4673. )),
  4674. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4675. '/div'
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. $result = $this->Form->input('Model.tags', array(
  4679. 'options' => array('first', 'second'),
  4680. 'multiple' => 'checkbox',
  4681. 'class' => 'my-class',
  4682. 'div' => false,
  4683. 'label' => false
  4684. ));
  4685. $this->assertTags($result, $expected);
  4686. $Contact = ClassRegistry::getObject('Contact');
  4687. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4688. $result = $this->Form->input('Contact.tags', array(
  4689. 'options' => array('one'),
  4690. 'multiple' => 'checkbox',
  4691. 'label' => false,
  4692. 'div' => false
  4693. ));
  4694. $expected = array(
  4695. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4696. array('div' => array('class' => 'checkbox form-error')),
  4697. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4698. array('label' => array('for' => 'ContactTags0')),
  4699. 'one',
  4700. '/label',
  4701. '/div'
  4702. );
  4703. $this->assertTags($result, $expected);
  4704. $result = $this->Form->input('Contact.tags', array(
  4705. 'options' => array('one'),
  4706. 'multiple' => 'checkbox',
  4707. 'class' => 'mycheckbox',
  4708. 'label' => false,
  4709. 'div' => false
  4710. ));
  4711. $expected = array(
  4712. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4713. array('div' => array('class' => 'mycheckbox form-error')),
  4714. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4715. array('label' => array('for' => 'ContactTags0')),
  4716. 'one',
  4717. '/label',
  4718. '/div'
  4719. );
  4720. $this->assertTags($result, $expected);
  4721. }
  4722. /**
  4723. * Checks the security hash array generated for multiple-input checkbox elements
  4724. *
  4725. * @return void
  4726. */
  4727. public function testSelectMultipleCheckboxSecurity() {
  4728. $this->Form->request['_Token'] = array('key' => 'testKey');
  4729. $this->assertEquals(array(), $this->Form->fields);
  4730. $result = $this->Form->select(
  4731. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4732. array('multiple' => 'checkbox')
  4733. );
  4734. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4735. $result = $this->Form->secure($this->Form->fields);
  4736. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4737. $this->assertRegExp('/"' . $key . '"/', $result);
  4738. }
  4739. /**
  4740. * Multiple select elements should always be secured as they always participate
  4741. * in the POST data.
  4742. *
  4743. * @return void
  4744. */
  4745. public function testSelectMultipleSecureWithNoOptions() {
  4746. $this->Form->request['_Token'] = array('key' => 'testkey');
  4747. $this->assertEquals(array(), $this->Form->fields);
  4748. $this->Form->select(
  4749. 'Model.select',
  4750. array(),
  4751. array('multiple' => true)
  4752. );
  4753. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4754. }
  4755. /**
  4756. * When a select box has no options it should not be added to the fields list
  4757. * as it always fail post validation.
  4758. *
  4759. * @return void
  4760. */
  4761. public function testSelectNoSecureWithNoOptions() {
  4762. $this->Form->request['_Token'] = array('key' => 'testkey');
  4763. $this->assertEquals(array(), $this->Form->fields);
  4764. $this->Form->select(
  4765. 'Model.select',
  4766. array()
  4767. );
  4768. $this->assertEquals(array(), $this->Form->fields);
  4769. $this->Form->select(
  4770. 'Model.select',
  4771. array(),
  4772. array('empty' => true)
  4773. );
  4774. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4775. }
  4776. /**
  4777. * testInputMultipleCheckboxes method
  4778. *
  4779. * test input() resulting in multi select elements being generated.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testInputMultipleCheckboxes() {
  4784. $result = $this->Form->input('Model.multi_field', array(
  4785. 'options' => array('first', 'second', 'third'),
  4786. 'multiple' => 'checkbox'
  4787. ));
  4788. $expected = array(
  4789. array('div' => array('class' => 'input select')),
  4790. array('label' => array('for' => 'ModelMultiField')),
  4791. 'Multi Field',
  4792. '/label',
  4793. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4794. array('div' => array('class' => 'checkbox')),
  4795. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4796. array('label' => array('for' => 'ModelMultiField0')),
  4797. 'first',
  4798. '/label',
  4799. '/div',
  4800. array('div' => array('class' => 'checkbox')),
  4801. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4802. array('label' => array('for' => 'ModelMultiField1')),
  4803. 'second',
  4804. '/label',
  4805. '/div',
  4806. array('div' => array('class' => 'checkbox')),
  4807. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4808. array('label' => array('for' => 'ModelMultiField2')),
  4809. 'third',
  4810. '/label',
  4811. '/div',
  4812. '/div'
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $result = $this->Form->input('Model.multi_field', array(
  4816. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4817. 'multiple' => 'checkbox'
  4818. ));
  4819. $expected = array(
  4820. array('div' => array('class' => 'input select')),
  4821. array('label' => array('for' => 'ModelMultiField')),
  4822. 'Multi Field',
  4823. '/label',
  4824. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4825. array('div' => array('class' => 'checkbox')),
  4826. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4827. array('label' => array('for' => 'ModelMultiFieldA')),
  4828. 'first',
  4829. '/label',
  4830. '/div',
  4831. array('div' => array('class' => 'checkbox')),
  4832. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4833. array('label' => array('for' => 'ModelMultiFieldB')),
  4834. 'second',
  4835. '/label',
  4836. '/div',
  4837. array('div' => array('class' => 'checkbox')),
  4838. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4839. array('label' => array('for' => 'ModelMultiFieldC')),
  4840. 'third',
  4841. '/label',
  4842. '/div',
  4843. '/div'
  4844. );
  4845. $this->assertTags($result, $expected);
  4846. $result = $this->Form->input('Model.multi_field', array(
  4847. 'options' => array('1' => 'first'),
  4848. 'multiple' => 'checkbox',
  4849. 'label' => false,
  4850. 'div' => false
  4851. ));
  4852. $expected = array(
  4853. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4854. array('div' => array('class' => 'checkbox')),
  4855. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4856. array('label' => array('for' => 'ModelMultiField1')),
  4857. 'first',
  4858. '/label',
  4859. '/div'
  4860. );
  4861. $this->assertTags($result, $expected);
  4862. $result = $this->Form->input('Model.multi_field', array(
  4863. 'options' => array('2' => 'second'),
  4864. 'multiple' => 'checkbox',
  4865. 'label' => false,
  4866. 'div' => false
  4867. ));
  4868. $expected = array(
  4869. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4870. array('div' => array('class' => 'checkbox')),
  4871. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4872. array('label' => array('for' => 'ModelMultiField2')),
  4873. 'second',
  4874. '/label',
  4875. '/div'
  4876. );
  4877. $this->assertTags($result, $expected);
  4878. }
  4879. /**
  4880. * testSelectHiddenFieldOmission method
  4881. *
  4882. * test that select() with 'hiddenField' => false omits the hidden field
  4883. *
  4884. * @return void
  4885. */
  4886. public function testSelectHiddenFieldOmission() {
  4887. $result = $this->Form->select('Model.multi_field',
  4888. array('first', 'second'),
  4889. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4890. );
  4891. $expected = array(
  4892. array('div' => array('class' => 'checkbox')),
  4893. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4894. array('label' => array('for' => 'ModelMultiField0')),
  4895. 'first',
  4896. '/label',
  4897. '/div',
  4898. array('div' => array('class' => 'checkbox')),
  4899. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4900. array('label' => array('for' => 'ModelMultiField1')),
  4901. 'second',
  4902. '/label',
  4903. '/div'
  4904. );
  4905. $this->assertTags($result, $expected);
  4906. $result = $this->Form->input('Model.multi_field', array(
  4907. 'options' => array('first', 'second'),
  4908. 'multiple' => 'checkbox',
  4909. 'hiddenField' => false
  4910. ));
  4911. $expected = array(
  4912. array('div' => array('class' => 'input select')),
  4913. array('label' => array('for' => 'ModelMultiField')),
  4914. 'Multi Field',
  4915. '/label',
  4916. array('div' => array('class' => 'checkbox')),
  4917. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4918. array('label' => array('for' => 'ModelMultiField0')),
  4919. 'first',
  4920. '/label',
  4921. '/div',
  4922. array('div' => array('class' => 'checkbox')),
  4923. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4924. array('label' => array('for' => 'ModelMultiField1')),
  4925. 'second',
  4926. '/label',
  4927. '/div',
  4928. '/div'
  4929. );
  4930. $this->assertTags($result, $expected);
  4931. }
  4932. /**
  4933. * test that select() with multiple = checkbox works with overriding name attribute.
  4934. *
  4935. * @return void
  4936. */
  4937. public function testSelectCheckboxMultipleOverrideName() {
  4938. $result = $this->Form->input('category', array(
  4939. 'type' => 'select',
  4940. 'multiple' => 'checkbox',
  4941. 'name' => 'data[fish]',
  4942. 'options' => array('1', '2'),
  4943. 'div' => false,
  4944. 'label' => false,
  4945. ));
  4946. $expected = array(
  4947. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4948. array('div' => array('class' => 'checkbox')),
  4949. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4950. array('label' => array('for' => 'Category0')), '1', '/label',
  4951. '/div',
  4952. array('div' => array('class' => 'checkbox')),
  4953. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4954. array('label' => array('for' => 'Category1')), '2', '/label',
  4955. '/div'
  4956. );
  4957. $this->assertTags($result, $expected);
  4958. }
  4959. /**
  4960. * Test that 'id' overrides all the checkbox id's as well.
  4961. *
  4962. * @return void
  4963. */
  4964. public function testSelectCheckboxMultipleId() {
  4965. $result = $this->Form->select(
  4966. 'Model.multi_field',
  4967. array('first', 'second', 'third'),
  4968. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4969. );
  4970. $expected = array(
  4971. 'input' => array(
  4972. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4973. ),
  4974. array('div' => array('class' => 'checkbox')),
  4975. array('input' => array(
  4976. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4977. 'value' => '0', 'id' => 'CustomId0'
  4978. )),
  4979. array('label' => array('for' => 'CustomId0')),
  4980. 'first',
  4981. '/label',
  4982. '/div',
  4983. array('div' => array('class' => 'checkbox')),
  4984. array('input' => array(
  4985. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4986. 'value' => '1', 'id' => 'CustomId1'
  4987. )),
  4988. array('label' => array('for' => 'CustomId1')),
  4989. 'second',
  4990. '/label',
  4991. '/div',
  4992. array('div' => array('class' => 'checkbox')),
  4993. array('input' => array(
  4994. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4995. 'value' => '2', 'id' => 'CustomId2'
  4996. )),
  4997. array('label' => array('for' => 'CustomId2')),
  4998. 'third',
  4999. '/label',
  5000. '/div'
  5001. );
  5002. $this->assertTags($result, $expected);
  5003. }
  5004. /**
  5005. * testCheckbox method
  5006. *
  5007. * Test generation of checkboxes
  5008. *
  5009. * @return void
  5010. */
  5011. public function testCheckbox() {
  5012. $result = $this->Form->checkbox('Model.field');
  5013. $expected = array(
  5014. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5015. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5016. );
  5017. $this->assertTags($result, $expected);
  5018. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5019. $expected = array(
  5020. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5021. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5022. );
  5023. $this->assertTags($result, $expected);
  5024. $Contact = ClassRegistry::getObject('Contact');
  5025. $Contact->validationErrors['field'] = 1;
  5026. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5027. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5028. $expected = array(
  5029. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5030. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5031. );
  5032. $this->assertTags($result, $expected);
  5033. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5034. $expected = array(
  5035. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5036. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5037. );
  5038. $this->assertTags($result, $expected);
  5039. $this->Form->request->data['Contact']['field'] = '';
  5040. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5041. $expected = array(
  5042. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5043. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. $Contact->validationErrors = array();
  5047. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5048. $expected = array(
  5049. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5050. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $this->Form->request->data['Contact']['published'] = 1;
  5054. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5055. $expected = array(
  5056. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5057. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5058. );
  5059. $this->assertTags($result, $expected);
  5060. $this->Form->request->data['Contact']['published'] = 0;
  5061. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5062. $expected = array(
  5063. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5064. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5065. );
  5066. $this->assertTags($result, $expected);
  5067. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5068. $expected = array(
  5069. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5070. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5071. );
  5072. $this->assertTags($result, $expected);
  5073. $result = $this->Form->checkbox('CustomField.1.value');
  5074. $expected = array(
  5075. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5076. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. }
  5080. /**
  5081. * test checkbox() with a custom name attribute
  5082. *
  5083. * @return void
  5084. */
  5085. public function testCheckboxCustomNameAttribute() {
  5086. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5087. $expected = array(
  5088. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5089. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. }
  5093. /**
  5094. * test the checked option for checkboxes.
  5095. *
  5096. * @return void
  5097. */
  5098. public function testCheckboxCheckedOption() {
  5099. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5100. $expected = array(
  5101. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5102. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5103. );
  5104. $this->assertTags($result, $expected);
  5105. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5106. $expected = array(
  5107. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5108. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5109. );
  5110. $this->assertTags($result, $expected);
  5111. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5112. $expected = array(
  5113. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5114. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5115. );
  5116. $this->assertTags($result, $expected);
  5117. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5118. $expected = array(
  5119. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5120. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5121. );
  5122. $this->assertTags($result, $expected);
  5123. $this->Form->request->data['Model']['field'] = 1;
  5124. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5125. $expected = array(
  5126. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5127. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5128. );
  5129. $this->assertTags($result, $expected);
  5130. }
  5131. /**
  5132. * Test that disabled attribute works on both the checkbox and hidden input.
  5133. *
  5134. * @return void
  5135. */
  5136. public function testCheckboxDisabling() {
  5137. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5138. $expected = array(
  5139. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5140. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5141. );
  5142. $this->assertTags($result, $expected);
  5143. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5144. $expected = array(
  5145. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5146. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5147. );
  5148. $this->assertTags($result, $expected);
  5149. }
  5150. /**
  5151. * Test that the hidden input for checkboxes can be omitted or set to a
  5152. * specific value.
  5153. *
  5154. * @return void
  5155. */
  5156. public function testCheckboxHiddenField() {
  5157. $result = $this->Form->input('UserForm.something', array(
  5158. 'type' => 'checkbox',
  5159. 'hiddenField' => false
  5160. ));
  5161. $expected = array(
  5162. 'div' => array('class' => 'input checkbox'),
  5163. array('input' => array(
  5164. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5165. 'value' => '1', 'id' => 'UserFormSomething'
  5166. )),
  5167. 'label' => array('for' => 'UserFormSomething'),
  5168. 'Something',
  5169. '/label',
  5170. '/div'
  5171. );
  5172. $this->assertTags($result, $expected);
  5173. $result = $this->Form->input('UserForm.something', array(
  5174. 'type' => 'checkbox',
  5175. 'value' => 'Y',
  5176. 'hiddenField' => 'N',
  5177. ));
  5178. $expected = array(
  5179. 'div' => array('class' => 'input checkbox'),
  5180. array('input' => array(
  5181. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5182. 'value' => 'N', 'id' => 'UserFormSomething_'
  5183. )),
  5184. array('input' => array(
  5185. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5186. 'value' => 'Y', 'id' => 'UserFormSomething'
  5187. )),
  5188. 'label' => array('for' => 'UserFormSomething'),
  5189. 'Something',
  5190. '/label',
  5191. '/div'
  5192. );
  5193. $this->assertTags($result, $expected);
  5194. }
  5195. /**
  5196. * testDateTime method
  5197. *
  5198. * Test generation of date/time select elements
  5199. *
  5200. * @return void
  5201. */
  5202. public function testDateTime() {
  5203. extract($this->dateRegex);
  5204. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5205. $now = strtotime('now');
  5206. $expected = array(
  5207. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5208. $daysRegex,
  5209. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5210. date('j', $now),
  5211. '/option',
  5212. '*/select',
  5213. '-',
  5214. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5215. $monthsRegex,
  5216. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5217. date('F', $now),
  5218. '/option',
  5219. '*/select',
  5220. '-',
  5221. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5222. $yearsRegex,
  5223. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5224. date('Y', $now),
  5225. '/option',
  5226. '*/select',
  5227. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5228. $hoursRegex,
  5229. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5230. date('g', $now),
  5231. '/option',
  5232. '*/select',
  5233. ':',
  5234. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5235. $minutesRegex,
  5236. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5237. date('i', $now),
  5238. '/option',
  5239. '*/select',
  5240. ' ',
  5241. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5242. $meridianRegex,
  5243. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5244. date('a', $now),
  5245. '/option',
  5246. '*/select'
  5247. );
  5248. $this->assertTags($result, $expected);
  5249. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5250. $expected = array(
  5251. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5252. $daysRegex,
  5253. array('option' => array('value' => '')),
  5254. '/option',
  5255. '*/select',
  5256. '-',
  5257. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5258. $monthsRegex,
  5259. array('option' => array('value' => '')),
  5260. '/option',
  5261. '*/select',
  5262. '-',
  5263. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5264. $yearsRegex,
  5265. array('option' => array('value' => '')),
  5266. '/option',
  5267. '*/select',
  5268. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5269. $hoursRegex,
  5270. array('option' => array('value' => '')),
  5271. '/option',
  5272. '*/select',
  5273. ':',
  5274. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5275. $minutesRegex,
  5276. array('option' => array('value' => '')),
  5277. '/option',
  5278. '*/select',
  5279. ' ',
  5280. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5281. $meridianRegex,
  5282. array('option' => array('value' => '')),
  5283. '/option',
  5284. '*/select'
  5285. );
  5286. $this->assertTags($result, $expected);
  5287. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5288. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5289. $this->assertTags($result, $expected);
  5290. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5291. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5292. $this->assertTags($result, $expected);
  5293. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5294. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5295. $expected = array(
  5296. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5297. $daysRegex,
  5298. array('option' => array('value' => '')),
  5299. '/option',
  5300. '*/select',
  5301. '-',
  5302. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5303. $monthsRegex,
  5304. array('option' => array('value' => '')),
  5305. '/option',
  5306. '*/select',
  5307. '-',
  5308. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5309. $yearsRegex,
  5310. array('option' => array('value' => '')),
  5311. '/option',
  5312. '*/select',
  5313. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5314. $hoursRegex,
  5315. array('option' => array('value' => '')),
  5316. '/option',
  5317. '*/select',
  5318. ':',
  5319. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5320. $minutesRegex,
  5321. array('option' => array('value' => '')),
  5322. '/option',
  5323. array('option' => array('value' => '00')),
  5324. '00',
  5325. '/option',
  5326. array('option' => array('value' => '05')),
  5327. '05',
  5328. '/option',
  5329. array('option' => array('value' => '10')),
  5330. '10',
  5331. '/option',
  5332. '*/select',
  5333. ' ',
  5334. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5335. $meridianRegex,
  5336. array('option' => array('value' => '')),
  5337. '/option',
  5338. '*/select'
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5342. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5343. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5344. $this->Form->request->data['Contact']['data'] = null;
  5345. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5346. $expected = array(
  5347. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5348. $daysRegex,
  5349. array('option' => array('value' => '')),
  5350. '/option',
  5351. '*/select',
  5352. '-',
  5353. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5354. $monthsRegex,
  5355. array('option' => array('value' => '')),
  5356. '/option',
  5357. '*/select',
  5358. '-',
  5359. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5360. $yearsRegex,
  5361. array('option' => array('value' => '')),
  5362. '/option',
  5363. '*/select',
  5364. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5365. $hoursRegex,
  5366. array('option' => array('value' => '')),
  5367. '/option',
  5368. '*/select',
  5369. ':',
  5370. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5371. $minutesRegex,
  5372. array('option' => array('value' => '')),
  5373. '/option',
  5374. '*/select',
  5375. ' ',
  5376. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5377. $meridianRegex,
  5378. array('option' => array('value' => '')),
  5379. '/option',
  5380. '*/select'
  5381. );
  5382. $this->assertTags($result, $expected);
  5383. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5384. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5385. $now = strtotime($this->Form->data['Model']['field']);
  5386. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5387. $expected = array(
  5388. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5389. $daysRegex,
  5390. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5391. date('j', $now),
  5392. '/option',
  5393. '*/select',
  5394. '-',
  5395. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5396. $monthsRegex,
  5397. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5398. date('F', $now),
  5399. '/option',
  5400. '*/select',
  5401. '-',
  5402. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5403. $yearsRegex,
  5404. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5405. date('Y', $now),
  5406. '/option',
  5407. '*/select',
  5408. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5409. $hoursRegex,
  5410. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5411. date('g', $now),
  5412. '/option',
  5413. '*/select',
  5414. ':',
  5415. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5416. $minutesRegex,
  5417. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5418. date('i', $now),
  5419. '/option',
  5420. '*/select',
  5421. ' ',
  5422. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5423. $meridianRegex,
  5424. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5425. date('a', $now),
  5426. '/option',
  5427. '*/select'
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $selected = strtotime('2008-10-26 12:33:00');
  5431. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5432. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5433. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5434. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5435. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5436. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5437. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5438. $this->Form->create('Contact');
  5439. $result = $this->Form->input('published');
  5440. $now = strtotime('now');
  5441. $expected = array(
  5442. 'div' => array('class' => 'input date'),
  5443. 'label' => array('for' => 'ContactPublishedMonth'),
  5444. 'Published',
  5445. '/label',
  5446. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5447. $monthsRegex,
  5448. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5449. date('F', $now),
  5450. '/option',
  5451. '*/select',
  5452. '-',
  5453. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5454. $daysRegex,
  5455. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5456. date('j', $now),
  5457. '/option',
  5458. '*/select',
  5459. '-',
  5460. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5461. $yearsRegex,
  5462. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5463. date('Y', $now),
  5464. '/option',
  5465. '*/select',
  5466. '/div'
  5467. );
  5468. $this->assertTags($result, $expected);
  5469. $result = $this->Form->input('published2', array('type' => 'date'));
  5470. $now = strtotime('now');
  5471. $expected = array(
  5472. 'div' => array('class' => 'input date'),
  5473. 'label' => array('for' => 'ContactPublished2Month'),
  5474. 'Published2',
  5475. '/label',
  5476. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5477. $monthsRegex,
  5478. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5479. date('F', $now),
  5480. '/option',
  5481. '*/select',
  5482. '-',
  5483. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5484. $daysRegex,
  5485. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5486. date('j', $now),
  5487. '/option',
  5488. '*/select',
  5489. '-',
  5490. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5491. $yearsRegex,
  5492. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5493. date('Y', $now),
  5494. '/option',
  5495. '*/select',
  5496. '/div'
  5497. );
  5498. $this->assertTags($result, $expected);
  5499. $this->Form->create('Contact');
  5500. $result = $this->Form->input('published', array('monthNames' => false));
  5501. $now = strtotime('now');
  5502. $expected = array(
  5503. 'div' => array('class' => 'input date'),
  5504. 'label' => array('for' => 'ContactPublishedMonth'),
  5505. 'Published',
  5506. '/label',
  5507. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5508. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5509. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5510. date('m', $now),
  5511. '/option',
  5512. '*/select',
  5513. '-',
  5514. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5515. $daysRegex,
  5516. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5517. date('j', $now),
  5518. '/option',
  5519. '*/select',
  5520. '-',
  5521. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5522. $yearsRegex,
  5523. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5524. date('Y', $now),
  5525. '/option',
  5526. '*/select',
  5527. '/div'
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. $result = $this->Form->input('published', array(
  5531. 'timeFormat' => 24,
  5532. 'interval' => 5,
  5533. 'selected' => strtotime('2009-09-03 13:37:00'),
  5534. 'type' => 'datetime'
  5535. ));
  5536. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5537. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5538. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5539. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5540. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5541. }
  5542. /**
  5543. * Test dateTime with rounding
  5544. *
  5545. * @return void
  5546. */
  5547. public function testDateTimeRounding() {
  5548. $this->Form->request->data['Contact'] = array(
  5549. 'date' => array(
  5550. 'day' => '13',
  5551. 'month' => '12',
  5552. 'year' => '2010',
  5553. 'hour' => '04',
  5554. 'min' => '19',
  5555. 'meridian' => 'AM'
  5556. )
  5557. );
  5558. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5559. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5560. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5561. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5562. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5563. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5564. }
  5565. /**
  5566. * Test that empty values don't trigger errors.
  5567. *
  5568. * @return void
  5569. */
  5570. public function testDateTimeNoErrorsOnEmptyData() {
  5571. $this->Form->request->data['Contact'] = array(
  5572. 'date' => array(
  5573. 'day' => '',
  5574. 'month' => '',
  5575. 'year' => '',
  5576. 'hour' => '',
  5577. 'min' => '',
  5578. 'meridian' => ''
  5579. )
  5580. );
  5581. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5582. $this->assertNotEmpty($result);
  5583. }
  5584. /**
  5585. * test that datetime() and default values work.
  5586. *
  5587. * @return void
  5588. */
  5589. public function testDatetimeWithDefault() {
  5590. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5591. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5592. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5593. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5594. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5595. 'default' => '2009-06-01 11:15:30'
  5596. ));
  5597. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5598. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5599. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5600. }
  5601. /**
  5602. * test that bogus non-date time data doesn't cause errors.
  5603. *
  5604. * @return void
  5605. */
  5606. public function testDateTimeWithBogusData() {
  5607. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5608. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5609. }
  5610. /**
  5611. * testDateTimeEmptyAsArray
  5612. *
  5613. * @return void
  5614. */
  5615. public function testDateTimeEmptyAsArray() {
  5616. $result = $this->Form->dateTime('Contact.date',
  5617. 'DMY',
  5618. '12',
  5619. array(
  5620. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5621. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5622. )
  5623. )
  5624. );
  5625. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5626. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5627. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5628. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5629. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5630. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5631. $result = $this->Form->dateTime('Contact.date',
  5632. 'DMY',
  5633. '12',
  5634. array(
  5635. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5636. )
  5637. );
  5638. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5639. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5640. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5641. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5642. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5643. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5644. }
  5645. /**
  5646. * testFormDateTimeMulti method
  5647. *
  5648. * test multiple datetime element generation
  5649. *
  5650. * @return void
  5651. */
  5652. public function testFormDateTimeMulti() {
  5653. extract($this->dateRegex);
  5654. $result = $this->Form->dateTime('Contact.1.updated');
  5655. $expected = array(
  5656. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5657. $daysRegex,
  5658. array('option' => array('value' => '')),
  5659. '/option',
  5660. '*/select',
  5661. '-',
  5662. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5663. $monthsRegex,
  5664. array('option' => array('value' => '')),
  5665. '/option',
  5666. '*/select',
  5667. '-',
  5668. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5669. $yearsRegex,
  5670. array('option' => array('value' => '')),
  5671. '/option',
  5672. '*/select',
  5673. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5674. $hoursRegex,
  5675. array('option' => array('value' => '')),
  5676. '/option',
  5677. '*/select',
  5678. ':',
  5679. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5680. $minutesRegex,
  5681. array('option' => array('value' => '')),
  5682. '/option',
  5683. '*/select',
  5684. ' ',
  5685. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5686. $meridianRegex,
  5687. array('option' => array('value' => '')),
  5688. '/option',
  5689. '*/select'
  5690. );
  5691. $this->assertTags($result, $expected);
  5692. $result = $this->Form->dateTime('Contact.2.updated');
  5693. $expected = array(
  5694. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5695. $daysRegex,
  5696. array('option' => array('value' => '')),
  5697. '/option',
  5698. '*/select',
  5699. '-',
  5700. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5701. $monthsRegex,
  5702. array('option' => array('value' => '')),
  5703. '/option',
  5704. '*/select',
  5705. '-',
  5706. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5707. $yearsRegex,
  5708. array('option' => array('value' => '')),
  5709. '/option',
  5710. '*/select',
  5711. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5712. $hoursRegex,
  5713. array('option' => array('value' => '')),
  5714. '/option',
  5715. '*/select',
  5716. ':',
  5717. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5718. $minutesRegex,
  5719. array('option' => array('value' => '')),
  5720. '/option',
  5721. '*/select',
  5722. ' ',
  5723. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5724. $meridianRegex,
  5725. array('option' => array('value' => '')),
  5726. '/option',
  5727. '*/select'
  5728. );
  5729. $this->assertTags($result, $expected);
  5730. }
  5731. /**
  5732. * When changing the date format, the label should always focus the first select box when
  5733. * clicked.
  5734. *
  5735. * @return void
  5736. */
  5737. public function testDateTimeLabelIdMatchesFirstInput() {
  5738. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5739. $this->assertContains('label for="ModelDateMonth"', $result);
  5740. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5741. $this->assertContains('label for="ModelDateDay"', $result);
  5742. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5743. $this->assertContains('label for="ModelDateYear"', $result);
  5744. }
  5745. /**
  5746. * testMonth method
  5747. *
  5748. * @return void
  5749. */
  5750. public function testMonth() {
  5751. $result = $this->Form->month('Model.field');
  5752. $expected = array(
  5753. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5754. array('option' => array('value' => '')),
  5755. '/option',
  5756. array('option' => array('value' => '01')),
  5757. date('F', strtotime('2008-01-01 00:00:00')),
  5758. '/option',
  5759. array('option' => array('value' => '02')),
  5760. date('F', strtotime('2008-02-01 00:00:00')),
  5761. '/option',
  5762. '*/select',
  5763. );
  5764. $this->assertTags($result, $expected);
  5765. $result = $this->Form->month('Model.field', array('empty' => true));
  5766. $expected = array(
  5767. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5768. array('option' => array('value' => '')),
  5769. '/option',
  5770. array('option' => array('value' => '01')),
  5771. date('F', strtotime('2008-01-01 00:00:00')),
  5772. '/option',
  5773. array('option' => array('value' => '02')),
  5774. date('F', strtotime('2008-02-01 00:00:00')),
  5775. '/option',
  5776. '*/select',
  5777. );
  5778. $this->assertTags($result, $expected);
  5779. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5780. $expected = array(
  5781. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5782. array('option' => array('value' => '')),
  5783. '/option',
  5784. array('option' => array('value' => '01')),
  5785. '01',
  5786. '/option',
  5787. array('option' => array('value' => '02')),
  5788. '02',
  5789. '/option',
  5790. '*/select',
  5791. );
  5792. $this->assertTags($result, $expected);
  5793. $monthNames = array(
  5794. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5795. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5796. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5797. $expected = array(
  5798. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5799. array('option' => array('value' => '')),
  5800. '/option',
  5801. array('option' => array('value' => '01')),
  5802. 'Jan',
  5803. '/option',
  5804. array('option' => array('value' => '02')),
  5805. 'Feb',
  5806. '/option',
  5807. '*/select',
  5808. );
  5809. $this->assertTags($result, $expected);
  5810. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5811. $result = $this->Form->month('Project.release');
  5812. $expected = array(
  5813. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5814. array('option' => array('value' => '')),
  5815. '/option',
  5816. array('option' => array('value' => '01')),
  5817. 'January',
  5818. '/option',
  5819. array('option' => array('value' => '02', 'selected' => 'selected')),
  5820. 'February',
  5821. '/option',
  5822. '*/select',
  5823. );
  5824. $this->assertTags($result, $expected);
  5825. }
  5826. /**
  5827. * testDay method
  5828. *
  5829. * @return void
  5830. */
  5831. public function testDay() {
  5832. extract($this->dateRegex);
  5833. $result = $this->Form->day('Model.field', array('value' => false));
  5834. $expected = array(
  5835. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5836. array('option' => array('value' => '')),
  5837. '/option',
  5838. array('option' => array('value' => '01')),
  5839. '1',
  5840. '/option',
  5841. array('option' => array('value' => '02')),
  5842. '2',
  5843. '/option',
  5844. $daysRegex,
  5845. '/select',
  5846. );
  5847. $this->assertTags($result, $expected);
  5848. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5849. $result = $this->Form->day('Model.field');
  5850. $expected = array(
  5851. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5852. array('option' => array('value' => '')),
  5853. '/option',
  5854. array('option' => array('value' => '01')),
  5855. '1',
  5856. '/option',
  5857. array('option' => array('value' => '02')),
  5858. '2',
  5859. '/option',
  5860. $daysRegex,
  5861. array('option' => array('value' => '10', 'selected' => 'selected')),
  5862. '10',
  5863. '/option',
  5864. $daysRegex,
  5865. '/select',
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $this->Form->request->data['Model']['field'] = '';
  5869. $result = $this->Form->day('Model.field', array('value' => '10'));
  5870. $expected = array(
  5871. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. array('option' => array('value' => '01')),
  5875. '1',
  5876. '/option',
  5877. array('option' => array('value' => '02')),
  5878. '2',
  5879. '/option',
  5880. $daysRegex,
  5881. array('option' => array('value' => '10', 'selected' => 'selected')),
  5882. '10',
  5883. '/option',
  5884. $daysRegex,
  5885. '/select',
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5889. $result = $this->Form->day('Model.field', array('value' => true));
  5890. $expected = array(
  5891. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5892. array('option' => array('value' => '')),
  5893. '/option',
  5894. array('option' => array('value' => '01')),
  5895. '1',
  5896. '/option',
  5897. array('option' => array('value' => '02')),
  5898. '2',
  5899. '/option',
  5900. $daysRegex,
  5901. array('option' => array('value' => '10', 'selected' => 'selected')),
  5902. '10',
  5903. '/option',
  5904. $daysRegex,
  5905. '/select',
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5909. $result = $this->Form->day('Project.release');
  5910. $expected = array(
  5911. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5912. array('option' => array('value' => '')),
  5913. '/option',
  5914. array('option' => array('value' => '01')),
  5915. '1',
  5916. '/option',
  5917. array('option' => array('value' => '02')),
  5918. '2',
  5919. '/option',
  5920. $daysRegex,
  5921. array('option' => array('value' => '10', 'selected' => 'selected')),
  5922. '10',
  5923. '/option',
  5924. $daysRegex,
  5925. '/select',
  5926. );
  5927. $this->assertTags($result, $expected);
  5928. }
  5929. /**
  5930. * testMinute method
  5931. *
  5932. * @return void
  5933. */
  5934. public function testMinute() {
  5935. extract($this->dateRegex);
  5936. $result = $this->Form->minute('Model.field');
  5937. $expected = array(
  5938. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5939. array('option' => array('value' => '')),
  5940. '/option',
  5941. array('option' => array('value' => '00')),
  5942. '00',
  5943. '/option',
  5944. array('option' => array('value' => '01')),
  5945. '01',
  5946. '/option',
  5947. array('option' => array('value' => '02')),
  5948. '02',
  5949. '/option',
  5950. $minutesRegex,
  5951. '/select',
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5955. $result = $this->Form->minute('Model.field');
  5956. $expected = array(
  5957. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5958. array('option' => array('value' => '')),
  5959. '/option',
  5960. array('option' => array('value' => '00')),
  5961. '00',
  5962. '/option',
  5963. array('option' => array('value' => '01')),
  5964. '01',
  5965. '/option',
  5966. array('option' => array('value' => '02')),
  5967. '02',
  5968. '/option',
  5969. $minutesRegex,
  5970. array('option' => array('value' => '12', 'selected' => 'selected')),
  5971. '12',
  5972. '/option',
  5973. $minutesRegex,
  5974. '/select',
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $this->Form->request->data['Model']['field'] = '';
  5978. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5979. $expected = array(
  5980. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5981. array('option' => array('value' => '')),
  5982. '/option',
  5983. array('option' => array('value' => '00')),
  5984. '00',
  5985. '/option',
  5986. array('option' => array('value' => '05')),
  5987. '05',
  5988. '/option',
  5989. array('option' => array('value' => '10')),
  5990. '10',
  5991. '/option',
  5992. $minutesRegex,
  5993. '/select',
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5997. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5998. $expected = array(
  5999. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6000. array('option' => array('value' => '')),
  6001. '/option',
  6002. array('option' => array('value' => '00')),
  6003. '00',
  6004. '/option',
  6005. array('option' => array('value' => '05')),
  6006. '05',
  6007. '/option',
  6008. array('option' => array('value' => '10', 'selected' => 'selected')),
  6009. '10',
  6010. '/option',
  6011. $minutesRegex,
  6012. '/select',
  6013. );
  6014. $this->assertTags($result, $expected);
  6015. }
  6016. /**
  6017. * testHour method
  6018. *
  6019. * @return void
  6020. */
  6021. public function testHour() {
  6022. extract($this->dateRegex);
  6023. $result = $this->Form->hour('Model.field', false);
  6024. $expected = array(
  6025. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6026. array('option' => array('value' => '')),
  6027. '/option',
  6028. array('option' => array('value' => '01')),
  6029. '1',
  6030. '/option',
  6031. array('option' => array('value' => '02')),
  6032. '2',
  6033. '/option',
  6034. $hoursRegex,
  6035. '/select',
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6039. $result = $this->Form->hour('Model.field', false);
  6040. $expected = array(
  6041. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6042. array('option' => array('value' => '')),
  6043. '/option',
  6044. array('option' => array('value' => '01')),
  6045. '1',
  6046. '/option',
  6047. array('option' => array('value' => '02')),
  6048. '2',
  6049. '/option',
  6050. $hoursRegex,
  6051. array('option' => array('value' => '12', 'selected' => 'selected')),
  6052. '12',
  6053. '/option',
  6054. '/select',
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. $this->Form->request->data['Model']['field'] = '';
  6058. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6059. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6060. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6061. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6062. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6063. $result = $this->Form->hour('Model.field', true);
  6064. $expected = array(
  6065. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6066. array('option' => array('value' => '')),
  6067. '/option',
  6068. array('option' => array('value' => '00', 'selected' => 'selected')),
  6069. '0',
  6070. '/option',
  6071. array('option' => array('value' => '01')),
  6072. '1',
  6073. '/option',
  6074. array('option' => array('value' => '02')),
  6075. '2',
  6076. '/option',
  6077. $hoursRegex,
  6078. '/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. unset($this->Form->request->data['Model']['field']);
  6082. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6083. $thisHour = date('H');
  6084. $optValue = date('G');
  6085. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6086. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6087. $result = $this->Form->hour('Model.field', true);
  6088. $expected = array(
  6089. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6090. array('option' => array('value' => '')),
  6091. '/option',
  6092. array('option' => array('value' => '00')),
  6093. '0',
  6094. '/option',
  6095. array('option' => array('value' => '01', 'selected' => 'selected')),
  6096. '1',
  6097. '/option',
  6098. array('option' => array('value' => '02')),
  6099. '2',
  6100. '/option',
  6101. $hoursRegex,
  6102. '/select',
  6103. );
  6104. $this->assertTags($result, $expected);
  6105. }
  6106. /**
  6107. * testYear method
  6108. *
  6109. * @return void
  6110. */
  6111. public function testYear() {
  6112. $result = $this->Form->year('Model.field', 2006, 2007);
  6113. $expected = array(
  6114. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6115. array('option' => array('value' => '')),
  6116. '/option',
  6117. array('option' => array('value' => '2007')),
  6118. '2007',
  6119. '/option',
  6120. array('option' => array('value' => '2006')),
  6121. '2006',
  6122. '/option',
  6123. '/select',
  6124. );
  6125. $this->assertTags($result, $expected);
  6126. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6127. $expected = array(
  6128. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6129. array('option' => array('value' => '')),
  6130. '/option',
  6131. array('option' => array('value' => '2006')),
  6132. '2006',
  6133. '/option',
  6134. array('option' => array('value' => '2007')),
  6135. '2007',
  6136. '/option',
  6137. '/select',
  6138. );
  6139. $this->assertTags($result, $expected);
  6140. $this->request->data['Contact']['published'] = '';
  6141. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6142. $expected = array(
  6143. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6144. array('option' => array('value' => '')),
  6145. '/option',
  6146. array('option' => array('value' => '2007')),
  6147. '2007',
  6148. '/option',
  6149. array('option' => array('value' => '2006')),
  6150. '2006',
  6151. '/option',
  6152. '/select',
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6156. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6157. $expected = array(
  6158. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6159. array('option' => array('value' => '2007')),
  6160. '2007',
  6161. '/option',
  6162. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6163. '2006',
  6164. '/option',
  6165. '/select',
  6166. );
  6167. $this->assertTags($result, $expected);
  6168. $this->Form->request->data['Contact']['published'] = '';
  6169. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6170. $expected = array(
  6171. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6172. array('option' => array('value' => '')),
  6173. '/option',
  6174. array('option' => array('value' => '2007')),
  6175. '2007',
  6176. '/option',
  6177. array('option' => array('value' => '2006')),
  6178. '2006',
  6179. '/option',
  6180. '/select',
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6184. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6185. $expected = array(
  6186. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6187. array('option' => array('value' => '2007')),
  6188. '2007',
  6189. '/option',
  6190. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6191. '2006',
  6192. '/option',
  6193. '/select',
  6194. );
  6195. $this->assertTags($result, $expected);
  6196. $this->Form->request->data['Contact']['published'] = '';
  6197. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6198. $expected = array(
  6199. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6200. array('option' => array('value' => '')),
  6201. '/option',
  6202. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6203. '2007',
  6204. '/option',
  6205. array('option' => array('value' => '2006')),
  6206. '2006',
  6207. '/option',
  6208. '/select',
  6209. );
  6210. $this->assertTags($result, $expected);
  6211. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6212. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6213. $expected = array(
  6214. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6215. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6216. '2007',
  6217. '/option',
  6218. array('option' => array('value' => '2006')),
  6219. '2006',
  6220. '/option',
  6221. '/select',
  6222. );
  6223. $this->assertTags($result, $expected);
  6224. $this->Form->request->data['Contact']['published'] = '';
  6225. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6226. $expected = array(
  6227. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6228. array('option' => array('value' => '2008')),
  6229. '2008',
  6230. '/option',
  6231. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6232. '2007',
  6233. '/option',
  6234. array('option' => array('value' => '2006')),
  6235. '2006',
  6236. '/option',
  6237. '/select',
  6238. );
  6239. $this->assertTags($result, $expected);
  6240. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6241. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6242. $expected = array(
  6243. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6244. array('option' => array('value' => '2008')),
  6245. '2008',
  6246. '/option',
  6247. array('option' => array('value' => '2007')),
  6248. '2007',
  6249. '/option',
  6250. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6251. '2006',
  6252. '/option',
  6253. '/select',
  6254. );
  6255. $this->assertTags($result, $expected);
  6256. $this->Form->request->data = array();
  6257. $this->Form->create('Contact');
  6258. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6259. $expected = array(
  6260. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6261. array('option' => array('value' => '2008')),
  6262. '2008',
  6263. '/option',
  6264. array('option' => array('value' => '2007')),
  6265. '2007',
  6266. '/option',
  6267. array('option' => array('value' => '2006')),
  6268. '2006',
  6269. '/option',
  6270. '/select',
  6271. );
  6272. $this->assertTags($result, $expected);
  6273. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6274. $this->assertContains('data[Contact][published][year]', $result);
  6275. }
  6276. /**
  6277. * testYearAutoExpandRange method
  6278. *
  6279. * @return void
  6280. */
  6281. public function testYearAutoExpandRange() {
  6282. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6283. $result = $this->Form->year('User.birthday');
  6284. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6285. $result = $matches[1];
  6286. $expected = range(date('Y') + 20, 1930);
  6287. $this->assertEquals($expected, $result);
  6288. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6289. $result = $this->Form->year('Project.release');
  6290. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6291. $result = $matches[1];
  6292. $expected = range(2050, date('Y') - 20);
  6293. $this->assertEquals($expected, $result);
  6294. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6295. $result = $this->Form->year('Project.release', 1890, 1900);
  6296. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6297. $result = $matches[1];
  6298. $expected = range(1900, 1881);
  6299. $this->assertEquals($expected, $result);
  6300. }
  6301. /**
  6302. * testInputDate method
  6303. *
  6304. * Test various inputs with type date and different dateFormat values.
  6305. * Failing to provide a dateFormat key should not error.
  6306. * It should simply not pre-select any value then.
  6307. *
  6308. * @return void
  6309. */
  6310. public function testInputDate() {
  6311. $this->Form->request->data = array(
  6312. 'User' => array(
  6313. 'month_year' => array('month' => date('m')),
  6314. 'just_year' => array('month' => date('m')),
  6315. 'just_month' => array('year' => date('Y')),
  6316. 'just_day' => array('month' => date('m')),
  6317. )
  6318. );
  6319. $this->Form->create('User');
  6320. $result = $this->Form->input('month_year',
  6321. array(
  6322. 'label' => false,
  6323. 'div' => false,
  6324. 'type' => 'date',
  6325. 'dateFormat' => 'MY',
  6326. 'minYear' => 2006,
  6327. 'maxYear' => 2008
  6328. )
  6329. );
  6330. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6331. $this->assertNotContains('value="2008" selected="selected"', $result);
  6332. $result = $this->Form->input('just_year',
  6333. array(
  6334. 'type' => 'date',
  6335. 'label' => false,
  6336. 'dateFormat' => 'Y',
  6337. 'minYear' => date('Y'),
  6338. 'maxYear' => date('Y', strtotime('+20 years'))
  6339. )
  6340. );
  6341. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6342. $result = $this->Form->input('just_month',
  6343. array(
  6344. 'type' => 'date',
  6345. 'label' => false,
  6346. 'dateFormat' => 'M',
  6347. 'empty' => false,
  6348. )
  6349. );
  6350. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6351. $result = $this->Form->input('just_day',
  6352. array(
  6353. 'type' => 'date',
  6354. 'label' => false,
  6355. 'dateFormat' => 'D',
  6356. 'empty' => false,
  6357. )
  6358. );
  6359. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6360. }
  6361. /**
  6362. * testInputDateMaxYear method
  6363. *
  6364. * Let's say we want to only allow users born from 2006 to 2008 to register
  6365. * This being the first singup page, we still don't have any data
  6366. *
  6367. * @return void
  6368. */
  6369. public function testInputDateMaxYear() {
  6370. $this->Form->request->data = array();
  6371. $this->Form->create('User');
  6372. $result = $this->Form->input('birthday',
  6373. array(
  6374. 'label' => false,
  6375. 'div' => false,
  6376. 'type' => 'date',
  6377. 'dateFormat' => 'DMY',
  6378. 'minYear' => 2006,
  6379. 'maxYear' => 2008
  6380. )
  6381. );
  6382. $this->assertContains('value="2008" selected="selected"', $result);
  6383. }
  6384. /**
  6385. * testTextArea method
  6386. *
  6387. * @return void
  6388. */
  6389. public function testTextArea() {
  6390. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6391. $result = $this->Form->textarea('Model.field');
  6392. $expected = array(
  6393. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6394. 'some test data',
  6395. '/textarea',
  6396. );
  6397. $this->assertTags($result, $expected);
  6398. $result = $this->Form->textarea('Model.tmp');
  6399. $expected = array(
  6400. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6401. '/textarea',
  6402. );
  6403. $this->assertTags($result, $expected);
  6404. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6405. $result = $this->Form->textarea('Model.field');
  6406. $expected = array(
  6407. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6408. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6409. '/textarea',
  6410. );
  6411. $this->assertTags($result, $expected);
  6412. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6413. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6414. $expected = array(
  6415. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6416. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6417. '/textarea',
  6418. );
  6419. $this->assertTags($result, $expected);
  6420. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6421. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6422. $expected = array(
  6423. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6424. '/textarea'
  6425. );
  6426. $this->assertTags($result, $expected);
  6427. }
  6428. /**
  6429. * testTextAreaWithStupidCharacters method
  6430. *
  6431. * test text area with non-ascii characters
  6432. *
  6433. * @return void
  6434. */
  6435. public function testTextAreaWithStupidCharacters() {
  6436. $this->loadFixtures('Post');
  6437. $result = $this->Form->input('Post.content', array(
  6438. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6439. ));
  6440. $expected = array(
  6441. 'div' => array('class' => 'input textarea'),
  6442. 'label' => array('for' => 'PostContent'),
  6443. 'Current Text',
  6444. '/label',
  6445. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6446. 'GREAT®',
  6447. '/textarea',
  6448. '/div'
  6449. );
  6450. $this->assertTags($result, $expected);
  6451. }
  6452. /**
  6453. * testHiddenField method
  6454. *
  6455. * @return void
  6456. */
  6457. public function testHiddenField() {
  6458. $Contact = ClassRegistry::getObject('Contact');
  6459. $Contact->validationErrors['field'] = 1;
  6460. $this->Form->request->data['Contact']['field'] = 'test';
  6461. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6462. $this->assertTags($result, array(
  6463. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6464. );
  6465. }
  6466. /**
  6467. * testFileUploadField method
  6468. *
  6469. * @return void
  6470. */
  6471. public function testFileUploadField() {
  6472. $result = $this->Form->file('Model.upload');
  6473. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6474. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6475. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6476. $expected = array(
  6477. 'div' => array('class' => 'input file'),
  6478. 'label' => array('for' => 'ModelUpload'),
  6479. 'Upload',
  6480. '/label',
  6481. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6482. '/div'
  6483. );
  6484. $this->assertTags($result, $expected);
  6485. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6486. $result = $this->Form->file('Model.upload');
  6487. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6488. }
  6489. /**
  6490. * test File upload input on a model not used in create();
  6491. *
  6492. * @return void
  6493. */
  6494. public function testFileUploadOnOtherModel() {
  6495. $this->Form->create('ValidateUser', array('type' => 'file'));
  6496. $result = $this->Form->file('ValidateProfile.city');
  6497. $expected = array(
  6498. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6499. );
  6500. $this->assertTags($result, $expected);
  6501. }
  6502. /**
  6503. * testButton method
  6504. *
  6505. * @return void
  6506. */
  6507. public function testButton() {
  6508. $result = $this->Form->button('Hi');
  6509. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6510. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6511. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6512. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6513. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6514. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6515. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6516. $result = $this->Form->button('No type', array('type' => false));
  6517. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6518. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6519. $this->assertNotRegExp('/\&039/', $result);
  6520. }
  6521. /**
  6522. * Test that button() makes unlocked fields by default.
  6523. *
  6524. * @return void
  6525. */
  6526. public function testButtonUnlockedByDefault() {
  6527. $this->Form->request->params['_Token']['key'] = 'secured';
  6528. $this->Form->button('Save', array('name' => 'save'));
  6529. $this->Form->button('Clear');
  6530. $result = $this->Form->unlockField();
  6531. $this->assertEquals(array('save'), $result);
  6532. }
  6533. /**
  6534. * testPostButton method
  6535. *
  6536. * @return void
  6537. */
  6538. public function testPostButton() {
  6539. $result = $this->Form->postButton('Hi', '/controller/action');
  6540. $this->assertTags($result, array(
  6541. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6542. 'div' => array('style' => 'display:none;'),
  6543. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6544. '/div',
  6545. 'button' => array('type' => 'submit'),
  6546. 'Hi',
  6547. '/button',
  6548. '/form'
  6549. ));
  6550. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6551. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6552. }
  6553. /**
  6554. * Test that postButton adds _Token fields.
  6555. *
  6556. * @return void
  6557. */
  6558. public function testSecurePostButton() {
  6559. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6560. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6561. $expected = array(
  6562. 'form' => array(
  6563. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6564. ),
  6565. array('div' => array('style' => 'display:none;')),
  6566. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6567. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6568. '/div',
  6569. 'button' => array('type' => 'submit'),
  6570. 'Delete',
  6571. '/button',
  6572. array('div' => array('style' => 'display:none;')),
  6573. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6574. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6575. '/div',
  6576. '/form',
  6577. );
  6578. $this->assertTags($result, $expected);
  6579. }
  6580. /**
  6581. * testPostLink method
  6582. *
  6583. * @return void
  6584. */
  6585. public function testPostLink() {
  6586. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6587. $this->assertTags($result, array(
  6588. 'form' => array(
  6589. 'method' => 'post', 'action' => '/posts/delete/1',
  6590. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6591. ),
  6592. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6593. '/form',
  6594. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6595. 'Delete',
  6596. '/a'
  6597. ));
  6598. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6599. $this->assertTags($result, array(
  6600. 'form' => array(
  6601. 'method' => 'post', 'action' => '/posts/delete/1',
  6602. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6603. ),
  6604. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6605. '/form',
  6606. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6607. 'Delete',
  6608. '/a'
  6609. ));
  6610. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6611. $this->assertTags($result, array(
  6612. 'form' => array(
  6613. 'method' => 'post', 'action' => '/posts/delete/1',
  6614. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6615. ),
  6616. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6617. '/form',
  6618. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6619. 'Delete',
  6620. '/a'
  6621. ));
  6622. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6623. $this->assertTags($result, array(
  6624. 'form' => array(
  6625. 'method' => 'post', 'action' => '/posts/delete/1',
  6626. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6627. ),
  6628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6629. '/form',
  6630. '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;/'),
  6631. 'Delete',
  6632. '/a'
  6633. ));
  6634. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6635. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6636. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6637. $this->assertTags($result, array(
  6638. 'form' => array(
  6639. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6640. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6641. ),
  6642. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6643. '/form',
  6644. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6645. 'Delete',
  6646. '/a'
  6647. ));
  6648. $result = $this->Form->postLink(
  6649. '',
  6650. array('controller' => 'items', 'action' => 'delete', 10),
  6651. array('class' => 'btn btn-danger', 'escape' => false),
  6652. 'Confirm thing'
  6653. );
  6654. $this->assertTags($result, array(
  6655. 'form' => array(
  6656. 'method' => 'post', 'action' => '/items/delete/10',
  6657. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6658. ),
  6659. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6660. '/form',
  6661. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6662. '/a'
  6663. ));
  6664. }
  6665. /**
  6666. * test creating postLinks after a GET form.
  6667. *
  6668. * @return void
  6669. */
  6670. public function testPostLinkAfterGetForm() {
  6671. $this->Form->request->params['_Token']['key'] = 'testkey';
  6672. $this->Form->create('User', array('type' => 'get'));
  6673. $this->Form->end();
  6674. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6675. $this->assertTags($result, array(
  6676. 'form' => array(
  6677. 'method' => 'post', 'action' => '/posts/delete/1',
  6678. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6679. ),
  6680. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6681. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6682. 'div' => array('style' => 'display:none;'),
  6683. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6684. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6685. '/div',
  6686. '/form',
  6687. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6688. 'Delete',
  6689. '/a'
  6690. ));
  6691. }
  6692. /**
  6693. * Test that postLink adds _Token fields.
  6694. *
  6695. * @return void
  6696. */
  6697. public function testSecurePostLink() {
  6698. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6699. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6700. $expected = array(
  6701. 'form' => array(
  6702. 'method' => 'post', 'action' => '/posts/delete/1',
  6703. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6704. ),
  6705. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6706. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6707. 'div' => array('style' => 'display:none;'),
  6708. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6709. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6710. '/div',
  6711. '/form',
  6712. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6713. 'Delete',
  6714. '/a'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. }
  6718. /**
  6719. * testSubmitButton method
  6720. *
  6721. * @return void
  6722. */
  6723. public function testSubmitButton() {
  6724. $result = $this->Form->submit('');
  6725. $expected = array(
  6726. 'div' => array('class' => 'submit'),
  6727. 'input' => array('type' => 'submit', 'value' => ''),
  6728. '/div'
  6729. );
  6730. $this->assertTags($result, $expected);
  6731. $result = $this->Form->submit('Test Submit');
  6732. $expected = array(
  6733. 'div' => array('class' => 'submit'),
  6734. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6735. '/div'
  6736. );
  6737. $this->assertTags($result, $expected);
  6738. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6739. $expected = array(
  6740. 'span' => array('class' => 'submit'),
  6741. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6742. '/span'
  6743. );
  6744. $this->assertTags($result, $expected);
  6745. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6746. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6747. $this->assertTags($result, $expected);
  6748. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6749. $expected = array(
  6750. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6751. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6752. '/div'
  6753. );
  6754. $this->assertTags($result, $expected);
  6755. $result = $this->Form->submit('Next >');
  6756. $expected = array(
  6757. 'div' => array('class' => 'submit'),
  6758. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6759. '/div'
  6760. );
  6761. $this->assertTags($result, $expected);
  6762. $result = $this->Form->submit('Next >', array('escape' => false));
  6763. $expected = array(
  6764. 'div' => array('class' => 'submit'),
  6765. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6766. '/div'
  6767. );
  6768. $this->assertTags($result, $expected);
  6769. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6770. $expected = array(
  6771. 'div' => array('class' => 'submit'),
  6772. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6773. '/div'
  6774. );
  6775. $this->assertTags($result, $expected);
  6776. $before = '--before--';
  6777. $after = '--after--';
  6778. $result = $this->Form->submit('Test', array('before' => $before));
  6779. $expected = array(
  6780. 'div' => array('class' => 'submit'),
  6781. '--before--',
  6782. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6783. '/div'
  6784. );
  6785. $this->assertTags($result, $expected);
  6786. $result = $this->Form->submit('Test', array('after' => $after));
  6787. $expected = array(
  6788. 'div' => array('class' => 'submit'),
  6789. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6790. '--after--',
  6791. '/div'
  6792. );
  6793. $this->assertTags($result, $expected);
  6794. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6795. $expected = array(
  6796. 'div' => array('class' => 'submit'),
  6797. '--before--',
  6798. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6799. '--after--',
  6800. '/div'
  6801. );
  6802. $this->assertTags($result, $expected);
  6803. }
  6804. /**
  6805. * test image submit types.
  6806. *
  6807. * @return void
  6808. */
  6809. public function testSubmitImage() {
  6810. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6811. $expected = array(
  6812. 'div' => array('class' => 'submit'),
  6813. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6814. '/div'
  6815. );
  6816. $this->assertTags($result, $expected);
  6817. $result = $this->Form->submit('/relative/cake.power.gif');
  6818. $expected = array(
  6819. 'div' => array('class' => 'submit'),
  6820. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6821. '/div'
  6822. );
  6823. $this->assertTags($result, $expected);
  6824. $result = $this->Form->submit('cake.power.gif');
  6825. $expected = array(
  6826. 'div' => array('class' => 'submit'),
  6827. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6828. '/div'
  6829. );
  6830. $this->assertTags($result, $expected);
  6831. $result = $this->Form->submit('Not.an.image');
  6832. $expected = array(
  6833. 'div' => array('class' => 'submit'),
  6834. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6835. '/div'
  6836. );
  6837. $this->assertTags($result, $expected);
  6838. $after = '--after--';
  6839. $before = '--before--';
  6840. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6841. $expected = array(
  6842. 'div' => array('class' => 'submit'),
  6843. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6844. '--after--',
  6845. '/div'
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6849. $expected = array(
  6850. 'div' => array('class' => 'submit'),
  6851. '--before--',
  6852. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6853. '/div'
  6854. );
  6855. $this->assertTags($result, $expected);
  6856. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6857. $expected = array(
  6858. 'div' => array('class' => 'submit'),
  6859. '--before--',
  6860. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6861. '--after--',
  6862. '/div'
  6863. );
  6864. $this->assertTags($result, $expected);
  6865. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6866. $expected = array(
  6867. 'div' => array('class' => 'submit'),
  6868. '--before--',
  6869. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6870. '--after--',
  6871. '/div'
  6872. );
  6873. $this->assertTags($result, $expected);
  6874. }
  6875. /**
  6876. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6877. * be submitted at a time.
  6878. *
  6879. * @return void
  6880. */
  6881. public function testSubmitUnlockedByDefault() {
  6882. $this->Form->request->params['_Token']['key'] = 'secured';
  6883. $this->Form->submit('Go go');
  6884. $this->Form->submit('Save', array('name' => 'save'));
  6885. $result = $this->Form->unlockField();
  6886. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6887. }
  6888. /**
  6889. * Test submit image with timestamps.
  6890. *
  6891. * @return void
  6892. */
  6893. public function testSubmitImageTimestamp() {
  6894. Configure::write('Asset.timestamp', 'force');
  6895. $result = $this->Form->submit('cake.power.gif');
  6896. $expected = array(
  6897. 'div' => array('class' => 'submit'),
  6898. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6899. '/div'
  6900. );
  6901. $this->assertTags($result, $expected);
  6902. }
  6903. /**
  6904. * test the create() method
  6905. *
  6906. * @return void
  6907. */
  6908. public function testCreate() {
  6909. $result = $this->Form->create('Contact');
  6910. $encoding = strtolower(Configure::read('App.encoding'));
  6911. $expected = array(
  6912. 'form' => array(
  6913. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6914. 'accept-charset' => $encoding
  6915. ),
  6916. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6917. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6918. '/div'
  6919. );
  6920. $this->assertTags($result, $expected);
  6921. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6922. $expected = array('form' => array(
  6923. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6924. 'accept-charset' => $encoding
  6925. ));
  6926. $this->assertTags($result, $expected);
  6927. $result = $this->Form->create('Contact', array('type' => 'get'));
  6928. $expected = array('form' => array(
  6929. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6930. 'accept-charset' => $encoding
  6931. ));
  6932. $this->assertTags($result, $expected);
  6933. $result = $this->Form->create('Contact', array('type' => 'put'));
  6934. $expected = array(
  6935. 'form' => array(
  6936. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6937. 'accept-charset' => $encoding
  6938. ),
  6939. 'div' => array('style' => 'display:none;'),
  6940. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6941. '/div'
  6942. );
  6943. $this->assertTags($result, $expected);
  6944. $result = $this->Form->create('Contact', array('type' => 'file'));
  6945. $expected = array(
  6946. 'form' => array(
  6947. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6948. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6949. ),
  6950. 'div' => array('style' => 'display:none;'),
  6951. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6952. '/div'
  6953. );
  6954. $this->assertTags($result, $expected);
  6955. $this->Form->request->data['Contact']['id'] = 1;
  6956. $this->Form->request->here = '/contacts/edit/1';
  6957. $this->Form->request['action'] = 'edit';
  6958. $result = $this->Form->create('Contact');
  6959. $expected = array(
  6960. 'form' => array(
  6961. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6962. 'accept-charset' => $encoding
  6963. ),
  6964. 'div' => array('style' => 'display:none;'),
  6965. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6966. '/div'
  6967. );
  6968. $this->assertTags($result, $expected);
  6969. $this->Form->request->data['Contact']['id'] = 1;
  6970. $this->Form->request->here = '/contacts/edit/1';
  6971. $this->Form->request['action'] = 'edit';
  6972. $result = $this->Form->create('Contact', array('type' => 'file'));
  6973. $expected = array(
  6974. 'form' => array(
  6975. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6976. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6977. ),
  6978. 'div' => array('style' => 'display:none;'),
  6979. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6980. '/div'
  6981. );
  6982. $this->assertTags($result, $expected);
  6983. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6984. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6985. $expected = array(
  6986. 'form' => array(
  6987. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6988. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6989. ),
  6990. 'div' => array('style' => 'display:none;'),
  6991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6992. '/div'
  6993. );
  6994. $this->assertTags($result, $expected);
  6995. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6996. $expected = array(
  6997. 'form' => array(
  6998. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6999. 'accept-charset' => $encoding
  7000. ),
  7001. 'div' => array('style' => 'display:none;'),
  7002. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7003. '/div'
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. $this->Form->request['action'] = 'add';
  7007. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7008. $expected = array(
  7009. 'form' => array(
  7010. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7011. 'accept-charset' => $encoding
  7012. ),
  7013. 'div' => array('style' => 'display:none;'),
  7014. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7015. '/div'
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. $result = $this->Form->create('User', array('action' => 'login'));
  7019. $expected = array(
  7020. 'form' => array(
  7021. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7022. 'accept-charset' => $encoding
  7023. ),
  7024. 'div' => array('style' => 'display:none;'),
  7025. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7026. '/div'
  7027. );
  7028. $this->assertTags($result, $expected);
  7029. $result = $this->Form->create('User', array('url' => '/users/login'));
  7030. $expected = array(
  7031. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7032. 'div' => array('style' => 'display:none;'),
  7033. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7034. '/div'
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. $this->Form->request['controller'] = 'pages';
  7038. $result = $this->Form->create('User', array('action' => 'signup'));
  7039. $expected = array(
  7040. 'form' => array(
  7041. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7042. 'accept-charset' => $encoding
  7043. ),
  7044. 'div' => array('style' => 'display:none;'),
  7045. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7046. '/div'
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. $this->Form->request->data = array();
  7050. $this->Form->request['controller'] = 'contacts';
  7051. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7052. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7053. $expected = array(
  7054. 'form' => array(
  7055. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7056. 'accept-charset' => 'utf-8'
  7057. ),
  7058. 'div' => array('style' => 'display:none;'),
  7059. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7060. '/div'
  7061. );
  7062. $this->assertTags($result, $expected);
  7063. }
  7064. /**
  7065. * Test the onsubmit option for create()
  7066. *
  7067. * @return void
  7068. */
  7069. public function testCreateOnSubmit() {
  7070. $this->Form->request->data = array();
  7071. $this->Form->request['controller'] = 'contacts';
  7072. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7073. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7074. $expected = array(
  7075. 'form' => array(
  7076. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7077. 'accept-charset' => 'utf-8'
  7078. ),
  7079. 'div' => array('style' => 'display:none;'),
  7080. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7081. '/div'
  7082. );
  7083. $this->assertTags($result, $expected);
  7084. $this->Form->request->data = array();
  7085. $this->Form->request['controller'] = 'contacts';
  7086. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7087. $result = $this->Form->create(array(
  7088. 'url' => array('action' => 'index', 'param'),
  7089. 'default' => false,
  7090. 'onsubmit' => 'someFunction();'
  7091. ));
  7092. $expected = array(
  7093. 'form' => array(
  7094. 'id' => 'ContactAddForm', 'method' => 'post',
  7095. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7096. 'action' => '/contacts/index/param',
  7097. 'accept-charset' => 'utf-8'
  7098. ),
  7099. 'div' => array('style' => 'display:none;'),
  7100. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7101. '/div'
  7102. );
  7103. $this->assertTags($result, $expected);
  7104. }
  7105. /**
  7106. * test create() with automatic url generation
  7107. *
  7108. * @return void
  7109. */
  7110. public function testCreateAutoUrl() {
  7111. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7112. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7113. $this->Form->request->base = '/base_url';
  7114. $result = $this->Form->create('Contact');
  7115. $expected = array(
  7116. 'form' => array(
  7117. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7118. 'accept-charset' => 'utf-8'
  7119. ),
  7120. 'div' => array('style' => 'display:none;'),
  7121. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7122. '/div'
  7123. );
  7124. $this->assertTags($result, $expected);
  7125. $this->Form->request['action'] = 'delete';
  7126. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7127. $this->Form->request->base = '/base_url';
  7128. $result = $this->Form->create('Contact');
  7129. $expected = array(
  7130. 'form' => array(
  7131. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7132. 'accept-charset' => 'utf-8'
  7133. ),
  7134. 'div' => array('style' => 'display:none;'),
  7135. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7136. '/div'
  7137. );
  7138. $this->assertTags($result, $expected);
  7139. }
  7140. /**
  7141. * test create() with a custom route
  7142. *
  7143. * @return void
  7144. */
  7145. public function testCreateCustomRoute() {
  7146. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7147. $encoding = strtolower(Configure::read('App.encoding'));
  7148. $result = $this->Form->create('User', array('action' => 'login'));
  7149. $expected = array(
  7150. 'form' => array(
  7151. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7152. 'accept-charset' => $encoding
  7153. ),
  7154. 'div' => array('style' => 'display:none;'),
  7155. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7156. '/div'
  7157. );
  7158. $this->assertTags($result, $expected);
  7159. }
  7160. /**
  7161. * test that inputDefaults are stored and used.
  7162. *
  7163. * @return void
  7164. */
  7165. public function testCreateWithInputDefaults() {
  7166. $this->Form->create('User', array(
  7167. 'inputDefaults' => array(
  7168. 'div' => false,
  7169. 'label' => false,
  7170. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7171. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7172. )
  7173. ));
  7174. $result = $this->Form->input('username');
  7175. $expected = array(
  7176. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7177. );
  7178. $this->assertTags($result, $expected);
  7179. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7180. $expected = array(
  7181. 'div' => array('class' => 'input text'),
  7182. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7183. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7184. '/div'
  7185. );
  7186. $this->assertTags($result, $expected);
  7187. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7188. $expected = array(
  7189. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7190. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7191. );
  7192. $this->assertTags($result, $expected);
  7193. $this->Form->create('User', array(
  7194. 'inputDefaults' => array(
  7195. 'div' => false,
  7196. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7197. )
  7198. ));
  7199. $result = $this->Form->input('username', array('div' => true));
  7200. $expected = array(
  7201. 'div' => array('class' => 'input text'),
  7202. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7203. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7204. '/div'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. }
  7208. /**
  7209. * test automatic accept-charset overriding
  7210. *
  7211. * @return void
  7212. */
  7213. public function testCreateWithAcceptCharset() {
  7214. $result = $this->Form->create('UserForm', array(
  7215. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7216. )
  7217. );
  7218. $expected = array(
  7219. 'form' => array(
  7220. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7221. 'accept-charset' => 'iso-8859-1'
  7222. ),
  7223. 'div' => array('style' => 'display:none;'),
  7224. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7225. '/div'
  7226. );
  7227. $this->assertTags($result, $expected);
  7228. }
  7229. /**
  7230. * Test base form URL when url param is passed with multiple parameters (&)
  7231. *
  7232. */
  7233. public function testCreateQuerystringrequest() {
  7234. $encoding = strtolower(Configure::read('App.encoding'));
  7235. $result = $this->Form->create('Contact', array(
  7236. 'type' => 'post',
  7237. 'escape' => false,
  7238. 'url' => array(
  7239. 'controller' => 'controller',
  7240. 'action' => 'action',
  7241. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7242. )
  7243. ));
  7244. $expected = array(
  7245. 'form' => array(
  7246. 'id' => 'ContactAddForm',
  7247. 'method' => 'post',
  7248. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7249. 'accept-charset' => $encoding
  7250. ),
  7251. 'div' => array('style' => 'display:none;'),
  7252. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7253. '/div'
  7254. );
  7255. $this->assertTags($result, $expected);
  7256. $result = $this->Form->create('Contact', array(
  7257. 'type' => 'post',
  7258. 'url' => array(
  7259. 'controller' => 'controller',
  7260. 'action' => 'action',
  7261. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7262. )
  7263. ));
  7264. $expected = array(
  7265. 'form' => array(
  7266. 'id' => 'ContactAddForm',
  7267. 'method' => 'post',
  7268. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7269. 'accept-charset' => $encoding
  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 that create() doesn't cause errors by multiple id's being in the primary key
  7279. * as could happen with multiple select or checkboxes.
  7280. *
  7281. * @return void
  7282. */
  7283. public function testCreateWithMultipleIdInData() {
  7284. $encoding = strtolower(Configure::read('App.encoding'));
  7285. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7286. $result = $this->Form->create('Contact');
  7287. $expected = array(
  7288. 'form' => array(
  7289. 'id' => 'ContactAddForm',
  7290. 'method' => 'post',
  7291. 'action' => '/contacts/add',
  7292. 'accept-charset' => $encoding
  7293. ),
  7294. 'div' => array('style' => 'display:none;'),
  7295. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7296. '/div'
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. }
  7300. /**
  7301. * test that create() doesn't add in extra passed params.
  7302. *
  7303. * @return void
  7304. */
  7305. public function testCreatePassedArgs() {
  7306. $encoding = strtolower(Configure::read('App.encoding'));
  7307. $this->Form->request->data['Contact']['id'] = 1;
  7308. $result = $this->Form->create('Contact', array(
  7309. 'type' => 'post',
  7310. 'escape' => false,
  7311. 'url' => array(
  7312. 'action' => 'edit',
  7313. 'myparam'
  7314. )
  7315. ));
  7316. $expected = array(
  7317. 'form' => array(
  7318. 'id' => 'ContactAddForm',
  7319. 'method' => 'post',
  7320. 'action' => '/contacts/edit/myparam',
  7321. 'accept-charset' => $encoding
  7322. ),
  7323. 'div' => array('style' => 'display:none;'),
  7324. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7325. '/div'
  7326. );
  7327. $this->assertTags($result, $expected);
  7328. }
  7329. /**
  7330. * test creating a get form, and get form inputs.
  7331. *
  7332. * @return void
  7333. */
  7334. public function testGetFormCreate() {
  7335. $encoding = strtolower(Configure::read('App.encoding'));
  7336. $result = $this->Form->create('Contact', array('type' => 'get'));
  7337. $this->assertTags($result, array('form' => array(
  7338. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7339. 'accept-charset' => $encoding
  7340. )));
  7341. $result = $this->Form->text('Contact.name');
  7342. $this->assertTags($result, array('input' => array(
  7343. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7344. )));
  7345. $result = $this->Form->password('password');
  7346. $this->assertTags($result, array('input' => array(
  7347. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7348. )));
  7349. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7350. $result = $this->Form->text('user_form');
  7351. $this->assertTags($result, array('input' => array(
  7352. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7353. )));
  7354. }
  7355. /**
  7356. * test get form, and inputs when the model param is false
  7357. *
  7358. * @return void
  7359. */
  7360. public function testGetFormWithFalseModel() {
  7361. $encoding = strtolower(Configure::read('App.encoding'));
  7362. $this->Form->request['controller'] = 'contact_test';
  7363. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7364. $expected = array('form' => array(
  7365. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7366. 'accept-charset' => $encoding
  7367. ));
  7368. $this->assertTags($result, $expected);
  7369. $result = $this->Form->text('reason');
  7370. $expected = array(
  7371. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7372. );
  7373. $this->assertTags($result, $expected);
  7374. }
  7375. /**
  7376. * test that datetime() works with GET style forms.
  7377. *
  7378. * @return void
  7379. */
  7380. public function testDateTimeWithGetForms() {
  7381. extract($this->dateRegex);
  7382. $this->Form->create('Contact', array('type' => 'get'));
  7383. $result = $this->Form->datetime('created');
  7384. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7385. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7386. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7387. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7388. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7389. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7390. }
  7391. /**
  7392. * testEditFormWithData method
  7393. *
  7394. * test auto populating form elements from submitted data.
  7395. *
  7396. * @return void
  7397. */
  7398. public function testEditFormWithData() {
  7399. $this->Form->request->data = array('Person' => array(
  7400. 'id' => 1,
  7401. 'first_name' => 'Nate',
  7402. 'last_name' => 'Abele',
  7403. 'email' => 'nate@example.com'
  7404. ));
  7405. $this->Form->request->addParams(array(
  7406. 'models' => array('Person'),
  7407. 'controller' => 'people',
  7408. 'action' => 'add'
  7409. ));
  7410. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7411. $this->Form->create();
  7412. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7413. $expected = array(
  7414. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7415. 'select' => array(
  7416. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7417. ),
  7418. array('option' => array('value' => 1)), 'Nate', '/option',
  7419. array('option' => array('value' => 2)), 'Garrett', '/option',
  7420. array('option' => array('value' => 3)), 'Larry', '/option',
  7421. '/select'
  7422. );
  7423. $this->assertTags($result, $expected);
  7424. }
  7425. /**
  7426. * Test that required fields are created for various types of validation.
  7427. *
  7428. * @return void
  7429. */
  7430. public function testFormInputRequiredDetection() {
  7431. $this->Form->create('Contact');
  7432. $result = $this->Form->input('Contact.non_existing');
  7433. $expected = array(
  7434. 'div' => array('class' => 'input text'),
  7435. 'label' => array('for' => 'ContactNonExisting'),
  7436. 'Non Existing',
  7437. '/label',
  7438. 'input' => array(
  7439. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7440. 'id' => 'ContactNonExisting'
  7441. ),
  7442. '/div'
  7443. );
  7444. $this->assertTags($result, $expected);
  7445. $result = $this->Form->input('Contact.imrequired');
  7446. $expected = array(
  7447. 'div' => array('class' => 'input text required'),
  7448. 'label' => array('for' => 'ContactImrequired'),
  7449. 'Imrequired',
  7450. '/label',
  7451. 'input' => array(
  7452. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7453. 'id' => 'ContactImrequired',
  7454. 'required' => 'required'
  7455. ),
  7456. '/div'
  7457. );
  7458. $this->assertTags($result, $expected);
  7459. $result = $this->Form->input('Contact.imalsorequired');
  7460. $expected = array(
  7461. 'div' => array('class' => 'input text required'),
  7462. 'label' => array('for' => 'ContactImalsorequired'),
  7463. 'Imalsorequired',
  7464. '/label',
  7465. 'input' => array(
  7466. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7467. 'id' => 'ContactImalsorequired',
  7468. 'required' => 'required'
  7469. ),
  7470. '/div'
  7471. );
  7472. $this->assertTags($result, $expected);
  7473. $result = $this->Form->input('Contact.imrequiredtoo');
  7474. $expected = array(
  7475. 'div' => array('class' => 'input text required'),
  7476. 'label' => array('for' => 'ContactImrequiredtoo'),
  7477. 'Imrequiredtoo',
  7478. '/label',
  7479. 'input' => array(
  7480. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7481. 'id' => 'ContactImrequiredtoo',
  7482. 'required' => 'required'
  7483. ),
  7484. '/div'
  7485. );
  7486. $this->assertTags($result, $expected);
  7487. $result = $this->Form->input('Contact.required_one');
  7488. $expected = array(
  7489. 'div' => array('class' => 'input text required'),
  7490. 'label' => array('for' => 'ContactRequiredOne'),
  7491. 'Required One',
  7492. '/label',
  7493. 'input' => array(
  7494. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7495. 'id' => 'ContactRequiredOne',
  7496. 'required' => 'required'
  7497. ),
  7498. '/div'
  7499. );
  7500. $this->assertTags($result, $expected);
  7501. $result = $this->Form->input('Contact.string_required');
  7502. $expected = array(
  7503. 'div' => array('class' => 'input text required'),
  7504. 'label' => array('for' => 'ContactStringRequired'),
  7505. 'String Required',
  7506. '/label',
  7507. 'input' => array(
  7508. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7509. 'id' => 'ContactStringRequired',
  7510. 'required' => 'required'
  7511. ),
  7512. '/div'
  7513. );
  7514. $this->assertTags($result, $expected);
  7515. $result = $this->Form->input('Contact.imnotrequired');
  7516. $expected = array(
  7517. 'div' => array('class' => 'input text'),
  7518. 'label' => array('for' => 'ContactImnotrequired'),
  7519. 'Imnotrequired',
  7520. '/label',
  7521. 'input' => array(
  7522. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7523. 'id' => 'ContactImnotrequired'
  7524. ),
  7525. '/div'
  7526. );
  7527. $this->assertTags($result, $expected);
  7528. $result = $this->Form->input('Contact.imalsonotrequired');
  7529. $expected = array(
  7530. 'div' => array('class' => 'input text'),
  7531. 'label' => array('for' => 'ContactImalsonotrequired'),
  7532. 'Imalsonotrequired',
  7533. '/label',
  7534. 'input' => array(
  7535. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7536. 'id' => 'ContactImalsonotrequired'
  7537. ),
  7538. '/div'
  7539. );
  7540. $this->assertTags($result, $expected);
  7541. $result = $this->Form->input('Contact.imalsonotrequired2');
  7542. $expected = array(
  7543. 'div' => array('class' => 'input text'),
  7544. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7545. 'Imalsonotrequired2',
  7546. '/label',
  7547. 'input' => array(
  7548. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7549. 'id' => 'ContactImalsonotrequired2'
  7550. ),
  7551. '/div'
  7552. );
  7553. $this->assertTags($result, $expected);
  7554. $result = $this->Form->input('Contact.imnotrequiredeither');
  7555. $expected = array(
  7556. 'div' => array('class' => 'input text'),
  7557. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7558. 'Imnotrequiredeither',
  7559. '/label',
  7560. 'input' => array(
  7561. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7562. 'id' => 'ContactImnotrequiredeither'
  7563. ),
  7564. '/div'
  7565. );
  7566. $this->assertTags($result, $expected);
  7567. $result = $this->Form->input('Contact.iamrequiredalways');
  7568. $expected = array(
  7569. 'div' => array('class' => 'input text required'),
  7570. 'label' => array('for' => 'ContactIamrequiredalways'),
  7571. 'Iamrequiredalways',
  7572. '/label',
  7573. 'input' => array(
  7574. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7575. 'id' => 'ContactIamrequiredalways',
  7576. 'required' => 'required'
  7577. ),
  7578. '/div'
  7579. );
  7580. $this->assertTags($result, $expected);
  7581. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7582. $expected = array(
  7583. 'div' => array('class' => 'input checkbox required'),
  7584. array('input' => array(
  7585. 'type' => 'hidden',
  7586. 'name' => 'data[Contact][boolean_field]',
  7587. 'id' => 'ContactBooleanField_',
  7588. 'value' => '0'
  7589. )),
  7590. array('input' => array(
  7591. 'type' => 'checkbox',
  7592. 'name' => 'data[Contact][boolean_field]',
  7593. 'value' => '1',
  7594. 'id' => 'ContactBooleanField'
  7595. )),
  7596. 'label' => array('for' => 'ContactBooleanField'),
  7597. 'Boolean Field',
  7598. '/label',
  7599. '/div'
  7600. );
  7601. $this->assertTags($result, $expected);
  7602. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7603. $expected = array(
  7604. 'div' => array('class' => 'input checkbox required'),
  7605. array('input' => array(
  7606. 'type' => 'hidden',
  7607. 'name' => 'data[Contact][boolean_field]',
  7608. 'id' => 'ContactBooleanField_',
  7609. 'value' => '0'
  7610. )),
  7611. array('input' => array(
  7612. 'type' => 'checkbox',
  7613. 'name' => 'data[Contact][boolean_field]',
  7614. 'value' => '1',
  7615. 'id' => 'ContactBooleanField',
  7616. 'required' => 'required'
  7617. )),
  7618. 'label' => array('for' => 'ContactBooleanField'),
  7619. 'Boolean Field',
  7620. '/label',
  7621. '/div'
  7622. );
  7623. $this->assertTags($result, $expected);
  7624. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7625. $expected = array(
  7626. 'div' => array('class' => 'input file required'),
  7627. 'label' => array('for' => 'ContactIamrequiredalways'),
  7628. 'Iamrequiredalways',
  7629. '/label',
  7630. 'input' => array(
  7631. 'type' => 'file',
  7632. 'name' => 'data[Contact][iamrequiredalways]',
  7633. 'id' => 'ContactIamrequiredalways',
  7634. 'required' => 'required'
  7635. ),
  7636. '/div'
  7637. );
  7638. $this->assertTags($result, $expected);
  7639. }
  7640. /**
  7641. * Test that required fields are created when only using ModelValidator::add().
  7642. *
  7643. * @return void
  7644. */
  7645. public function testFormInputRequiredDetectionModelValidator() {
  7646. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7647. $this->Form->create('ContactTag');
  7648. $result = $this->Form->input('ContactTag.iwillberequired');
  7649. $expected = array(
  7650. 'div' => array('class' => 'input text required'),
  7651. 'label' => array('for' => 'ContactTagIwillberequired'),
  7652. 'Iwillberequired',
  7653. '/label',
  7654. 'input' => array(
  7655. 'name' => 'data[ContactTag][iwillberequired]',
  7656. 'type' => 'text',
  7657. 'id' => 'ContactTagIwillberequired',
  7658. 'required' => 'required'
  7659. ),
  7660. '/div'
  7661. );
  7662. $this->assertTags($result, $expected);
  7663. }
  7664. /**
  7665. * testFormMagicInput method
  7666. *
  7667. * @return void
  7668. */
  7669. public function testFormMagicInput() {
  7670. $encoding = strtolower(Configure::read('App.encoding'));
  7671. $result = $this->Form->create('Contact');
  7672. $expected = array(
  7673. 'form' => array(
  7674. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7675. 'accept-charset' => $encoding
  7676. ),
  7677. 'div' => array('style' => 'display:none;'),
  7678. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7679. '/div'
  7680. );
  7681. $this->assertTags($result, $expected);
  7682. $result = $this->Form->input('name');
  7683. $expected = array(
  7684. 'div' => array('class' => 'input text'),
  7685. 'label' => array('for' => 'ContactName'),
  7686. 'Name',
  7687. '/label',
  7688. 'input' => array(
  7689. 'type' => 'text', 'name' => 'data[Contact][name]',
  7690. 'id' => 'ContactName', 'maxlength' => '255'
  7691. ),
  7692. '/div'
  7693. );
  7694. $this->assertTags($result, $expected);
  7695. $result = $this->Form->input('non_existing_field_in_contact_model');
  7696. $expected = array(
  7697. 'div' => array('class' => 'input text'),
  7698. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7699. 'Non Existing Field In Contact Model',
  7700. '/label',
  7701. 'input' => array(
  7702. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7703. 'id' => 'ContactNonExistingFieldInContactModel'
  7704. ),
  7705. '/div'
  7706. );
  7707. $this->assertTags($result, $expected);
  7708. $result = $this->Form->input('Address.street');
  7709. $expected = array(
  7710. 'div' => array('class' => 'input text'),
  7711. 'label' => array('for' => 'AddressStreet'),
  7712. 'Street',
  7713. '/label',
  7714. 'input' => array(
  7715. 'type' => 'text', 'name' => 'data[Address][street]',
  7716. 'id' => 'AddressStreet'
  7717. ),
  7718. '/div'
  7719. );
  7720. $this->assertTags($result, $expected);
  7721. $result = $this->Form->input('Address.non_existing_field_in_model');
  7722. $expected = array(
  7723. 'div' => array('class' => 'input text'),
  7724. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7725. 'Non Existing Field In Model',
  7726. '/label',
  7727. 'input' => array(
  7728. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7729. 'id' => 'AddressNonExistingFieldInModel'
  7730. ),
  7731. '/div'
  7732. );
  7733. $this->assertTags($result, $expected);
  7734. $result = $this->Form->input('name', array('div' => false));
  7735. $expected = array(
  7736. 'label' => array('for' => 'ContactName'),
  7737. 'Name',
  7738. '/label',
  7739. 'input' => array(
  7740. 'type' => 'text', 'name' => 'data[Contact][name]',
  7741. 'id' => 'ContactName', 'maxlength' => '255'
  7742. )
  7743. );
  7744. $this->assertTags($result, $expected);
  7745. extract($this->dateRegex);
  7746. $now = strtotime('now');
  7747. $result = $this->Form->input('Contact.published', array('div' => false));
  7748. $expected = array(
  7749. 'label' => array('for' => 'ContactPublishedMonth'),
  7750. 'Published',
  7751. '/label',
  7752. array('select' => array(
  7753. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7754. )),
  7755. $monthsRegex,
  7756. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7757. date('F', $now),
  7758. '/option',
  7759. '*/select',
  7760. '-',
  7761. array('select' => array(
  7762. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7763. )),
  7764. $daysRegex,
  7765. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7766. date('j', $now),
  7767. '/option',
  7768. '*/select',
  7769. '-',
  7770. array('select' => array(
  7771. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7772. )),
  7773. $yearsRegex,
  7774. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7775. date('Y', $now),
  7776. '*/select'
  7777. );
  7778. $this->assertTags($result, $expected);
  7779. $result = $this->Form->input('Contact.updated', array('div' => false));
  7780. $expected = array(
  7781. 'label' => array('for' => 'ContactUpdatedMonth'),
  7782. 'Updated',
  7783. '/label',
  7784. array('select' => array(
  7785. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7786. )),
  7787. $monthsRegex,
  7788. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7789. date('F', $now),
  7790. '/option',
  7791. '*/select',
  7792. '-',
  7793. array('select' => array(
  7794. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7795. )),
  7796. $daysRegex,
  7797. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7798. date('j', $now),
  7799. '/option',
  7800. '*/select',
  7801. '-',
  7802. array('select' => array(
  7803. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7804. )),
  7805. $yearsRegex,
  7806. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7807. date('Y', $now),
  7808. '*/select'
  7809. );
  7810. $this->assertTags($result, $expected);
  7811. $result = $this->Form->input('UserForm.stuff');
  7812. $expected = array(
  7813. 'div' => array('class' => 'input text'),
  7814. 'label' => array('for' => 'UserFormStuff'),
  7815. 'Stuff',
  7816. '/label',
  7817. 'input' => array(
  7818. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7819. 'id' => 'UserFormStuff', 'maxlength' => 10
  7820. ),
  7821. '/div'
  7822. );
  7823. $this->assertTags($result, $expected);
  7824. }
  7825. /**
  7826. * testForMagicInputNonExistingNorValidated method
  7827. *
  7828. * @return void
  7829. */
  7830. public function testForMagicInputNonExistingNorValidated() {
  7831. $encoding = strtolower(Configure::read('App.encoding'));
  7832. $result = $this->Form->create('Contact');
  7833. $expected = array(
  7834. 'form' => array(
  7835. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7836. 'accept-charset' => $encoding
  7837. ),
  7838. 'div' => array('style' => 'display:none;'),
  7839. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7840. '/div'
  7841. );
  7842. $this->assertTags($result, $expected);
  7843. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7844. $expected = array(
  7845. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7846. 'Non Existing Nor Validated',
  7847. '/label',
  7848. 'input' => array(
  7849. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7850. 'id' => 'ContactNonExistingNorValidated'
  7851. )
  7852. );
  7853. $this->assertTags($result, $expected);
  7854. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7855. 'div' => false, 'value' => 'my value'
  7856. ));
  7857. $expected = array(
  7858. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7859. 'Non Existing Nor Validated',
  7860. '/label',
  7861. 'input' => array(
  7862. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7863. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7864. )
  7865. );
  7866. $this->assertTags($result, $expected);
  7867. $this->Form->request->data = array(
  7868. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7869. ));
  7870. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7871. $expected = array(
  7872. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7873. 'Non Existing Nor Validated',
  7874. '/label',
  7875. 'input' => array(
  7876. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7877. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7878. )
  7879. );
  7880. $this->assertTags($result, $expected);
  7881. }
  7882. /**
  7883. * testFormMagicInputLabel method
  7884. *
  7885. * @return void
  7886. */
  7887. public function testFormMagicInputLabel() {
  7888. $encoding = strtolower(Configure::read('App.encoding'));
  7889. $result = $this->Form->create('Contact');
  7890. $expected = array(
  7891. 'form' => array(
  7892. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7893. 'accept-charset' => $encoding
  7894. ),
  7895. 'div' => array('style' => 'display:none;'),
  7896. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7897. '/div'
  7898. );
  7899. $this->assertTags($result, $expected);
  7900. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7901. $this->assertTags($result, array('input' => array(
  7902. 'name' => 'data[Contact][name]', 'type' => 'text',
  7903. 'id' => 'ContactName', 'maxlength' => '255')
  7904. ));
  7905. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7906. $expected = array(
  7907. 'label' => array('for' => 'ContactName'),
  7908. 'My label',
  7909. '/label',
  7910. 'input' => array(
  7911. 'type' => 'text', 'name' => 'data[Contact][name]',
  7912. 'id' => 'ContactName', 'maxlength' => '255'
  7913. )
  7914. );
  7915. $this->assertTags($result, $expected);
  7916. $result = $this->Form->input('Contact.name', array(
  7917. 'div' => false, 'label' => array('class' => 'mandatory')
  7918. ));
  7919. $expected = array(
  7920. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7921. 'Name',
  7922. '/label',
  7923. 'input' => array(
  7924. 'type' => 'text', 'name' => 'data[Contact][name]',
  7925. 'id' => 'ContactName', 'maxlength' => '255'
  7926. )
  7927. );
  7928. $this->assertTags($result, $expected);
  7929. $result = $this->Form->input('Contact.name', array(
  7930. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7931. ));
  7932. $expected = array(
  7933. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7934. 'My label',
  7935. '/label',
  7936. 'input' => array(
  7937. 'type' => 'text', 'name' => 'data[Contact][name]',
  7938. 'id' => 'ContactName', 'maxlength' => '255'
  7939. )
  7940. );
  7941. $this->assertTags($result, $expected);
  7942. $result = $this->Form->input('Contact.name', array(
  7943. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7944. ));
  7945. $expected = array(
  7946. 'label' => array('for' => 'my_id'),
  7947. 'Name',
  7948. '/label',
  7949. 'input' => array(
  7950. 'type' => 'text', 'name' => 'data[Contact][name]',
  7951. 'id' => 'my_id', 'maxlength' => '255'
  7952. )
  7953. );
  7954. $this->assertTags($result, $expected);
  7955. $result = $this->Form->input('1.id');
  7956. $this->assertTags($result, array('input' => array(
  7957. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7958. 'id' => 'Contact1Id'
  7959. )));
  7960. $result = $this->Form->input("1.name");
  7961. $expected = array(
  7962. 'div' => array('class' => 'input text'),
  7963. 'label' => array('for' => 'Contact1Name'),
  7964. 'Name',
  7965. '/label',
  7966. 'input' => array(
  7967. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7968. 'id' => 'Contact1Name', 'maxlength' => '255'
  7969. ),
  7970. '/div'
  7971. );
  7972. $this->assertTags($result, $expected);
  7973. $result = $this->Form->input('Contact.1.id');
  7974. $this->assertTags($result, array(
  7975. 'input' => array(
  7976. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7977. 'id' => 'Contact1Id'
  7978. )
  7979. ));
  7980. $result = $this->Form->input("Model.1.name");
  7981. $expected = array(
  7982. 'div' => array('class' => 'input text'),
  7983. 'label' => array('for' => 'Model1Name'),
  7984. 'Name',
  7985. '/label',
  7986. 'input' => array(
  7987. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7988. 'id' => 'Model1Name'
  7989. ),
  7990. '/div'
  7991. );
  7992. $this->assertTags($result, $expected);
  7993. }
  7994. /**
  7995. * testFormEnd method
  7996. *
  7997. * @return void
  7998. */
  7999. public function testFormEnd() {
  8000. $this->assertEquals('</form>', $this->Form->end());
  8001. $result = $this->Form->end('');
  8002. $expected = array(
  8003. 'div' => array('class' => 'submit'),
  8004. 'input' => array('type' => 'submit', 'value' => ''),
  8005. '/div',
  8006. '/form'
  8007. );
  8008. $this->assertTags($result, $expected);
  8009. $result = $this->Form->end(array('label' => ''));
  8010. $expected = array(
  8011. 'div' => array('class' => 'submit'),
  8012. 'input' => array('type' => 'submit', 'value' => ''),
  8013. '/div',
  8014. '/form'
  8015. );
  8016. $this->assertTags($result, $expected);
  8017. $result = $this->Form->end('save');
  8018. $expected = array(
  8019. 'div' => array('class' => 'submit'),
  8020. 'input' => array('type' => 'submit', 'value' => 'save'),
  8021. '/div',
  8022. '/form'
  8023. );
  8024. $this->assertTags($result, $expected);
  8025. $result = $this->Form->end(array('label' => 'save'));
  8026. $expected = array(
  8027. 'div' => array('class' => 'submit'),
  8028. 'input' => array('type' => 'submit', 'value' => 'save'),
  8029. '/div',
  8030. '/form'
  8031. );
  8032. $this->assertTags($result, $expected);
  8033. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8034. $expected = array(
  8035. 'div' => array('class' => 'submit'),
  8036. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8037. '/div',
  8038. '/form'
  8039. );
  8040. $this->assertTags($result, $expected);
  8041. $result = $this->Form->end(array('name' => 'Whatever'));
  8042. $expected = array(
  8043. 'div' => array('class' => 'submit'),
  8044. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8045. '/div',
  8046. '/form'
  8047. );
  8048. $this->assertTags($result, $expected);
  8049. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8050. $expected = array(
  8051. 'div' => array('class' => 'good'),
  8052. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8053. '/div',
  8054. '/form'
  8055. );
  8056. $this->assertTags($result, $expected);
  8057. $result = $this->Form->end(array(
  8058. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8059. ));
  8060. $expected = array(
  8061. 'div' => array('class' => 'good'),
  8062. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8063. '/div',
  8064. '/form'
  8065. );
  8066. $this->assertTags($result, $expected);
  8067. }
  8068. /**
  8069. * testMultipleFormWithIdFields method
  8070. *
  8071. * @return void
  8072. */
  8073. public function testMultipleFormWithIdFields() {
  8074. $this->Form->create('UserForm');
  8075. $result = $this->Form->input('id');
  8076. $this->assertTags($result, array('input' => array(
  8077. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8078. )));
  8079. $result = $this->Form->input('ValidateItem.id');
  8080. $this->assertTags($result, array('input' => array(
  8081. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8082. 'id' => 'ValidateItemId'
  8083. )));
  8084. $result = $this->Form->input('ValidateUser.id');
  8085. $this->assertTags($result, array('input' => array(
  8086. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8087. 'id' => 'ValidateUserId'
  8088. )));
  8089. }
  8090. /**
  8091. * testDbLessModel method
  8092. *
  8093. * @return void
  8094. */
  8095. public function testDbLessModel() {
  8096. $this->Form->create('TestMail');
  8097. $result = $this->Form->input('name');
  8098. $expected = array(
  8099. 'div' => array('class' => 'input text'),
  8100. 'label' => array('for' => 'TestMailName'),
  8101. 'Name',
  8102. '/label',
  8103. 'input' => array(
  8104. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8105. 'id' => 'TestMailName'
  8106. ),
  8107. '/div'
  8108. );
  8109. $this->assertTags($result, $expected);
  8110. ClassRegistry::init('TestMail');
  8111. $this->Form->create('TestMail');
  8112. $result = $this->Form->input('name');
  8113. $expected = array(
  8114. 'div' => array('class' => 'input text'),
  8115. 'label' => array('for' => 'TestMailName'),
  8116. 'Name',
  8117. '/label',
  8118. 'input' => array(
  8119. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8120. 'id' => 'TestMailName'
  8121. ),
  8122. '/div'
  8123. );
  8124. $this->assertTags($result, $expected);
  8125. }
  8126. /**
  8127. * testBrokenness method
  8128. *
  8129. * @return void
  8130. */
  8131. public function testBrokenness() {
  8132. /*
  8133. * #4 This test has two parents and four children. By default (as of r7117) both
  8134. * parents are show but the first parent is missing a child. This is the inconsistency
  8135. * in the default behaviour - one parent has all children, the other does not - dependent
  8136. * on the data values.
  8137. */
  8138. $result = $this->Form->select('Model.field', array(
  8139. 'Fred' => array(
  8140. 'freds_son_1' => 'Fred',
  8141. 'freds_son_2' => 'Freddie'
  8142. ),
  8143. 'Bert' => array(
  8144. 'berts_son_1' => 'Albert',
  8145. 'berts_son_2' => 'Bertie')
  8146. ),
  8147. array('showParents' => true, 'empty' => false)
  8148. );
  8149. $expected = array(
  8150. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8151. array('optgroup' => array('label' => 'Fred')),
  8152. array('option' => array('value' => 'freds_son_1')),
  8153. 'Fred',
  8154. '/option',
  8155. array('option' => array('value' => 'freds_son_2')),
  8156. 'Freddie',
  8157. '/option',
  8158. '/optgroup',
  8159. array('optgroup' => array('label' => 'Bert')),
  8160. array('option' => array('value' => 'berts_son_1')),
  8161. 'Albert',
  8162. '/option',
  8163. array('option' => array('value' => 'berts_son_2')),
  8164. 'Bertie',
  8165. '/option',
  8166. '/optgroup',
  8167. '/select'
  8168. );
  8169. $this->assertTags($result, $expected);
  8170. /*
  8171. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8172. * changed, so we should expect the same select list data, just with a different name
  8173. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8174. * This is where data corruption can occur, because when a select value is missing from
  8175. * a list a form will substitute the first value in the list - without the user knowing.
  8176. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8177. * affect the availability of 3 => 'Three' as a valid option.
  8178. */
  8179. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8180. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8181. ));
  8182. $result = $this->Form->select(
  8183. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8184. );
  8185. $expected = array(
  8186. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8187. array('option' => array('value' => 1)),
  8188. 'One',
  8189. '/option',
  8190. array('option' => array('value' => 2)),
  8191. 'Two',
  8192. '/option',
  8193. array('optgroup' => array('label' => 'Three')),
  8194. array('option' => array('value' => 3)),
  8195. 'Three',
  8196. '/option',
  8197. array('option' => array('value' => 4)),
  8198. 'Four',
  8199. '/option',
  8200. array('option' => array('value' => 5)),
  8201. 'Five',
  8202. '/option',
  8203. '/optgroup',
  8204. '/select'
  8205. );
  8206. $this->assertTags($result, $expected);
  8207. }
  8208. /**
  8209. * Test the generation of fields for a multi record form.
  8210. *
  8211. * @return void
  8212. */
  8213. public function testMultiRecordForm() {
  8214. $this->Form->create('ValidateProfile');
  8215. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8216. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8217. $expected = array(
  8218. 'div' => array('class' => 'input textarea'),
  8219. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8220. 'Name',
  8221. '/label',
  8222. 'textarea' => array(
  8223. 'id' => 'ValidateProfile1ValidateItem2Name',
  8224. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8225. 'cols' => 30,
  8226. 'rows' => 6
  8227. ),
  8228. 'Value',
  8229. '/textarea',
  8230. '/div'
  8231. );
  8232. $this->assertTags($result, $expected);
  8233. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8234. $expected = array(
  8235. 'div' => array('class' => 'input date'),
  8236. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8237. 'Created',
  8238. '/label',
  8239. array('select' => array(
  8240. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8241. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8242. )
  8243. ),
  8244. array('option' => array('value' => '')), '/option',
  8245. $this->dateRegex['monthsRegex'],
  8246. '/select', '-',
  8247. array('select' => array(
  8248. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8249. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8250. )
  8251. ),
  8252. array('option' => array('value' => '')), '/option',
  8253. $this->dateRegex['daysRegex'],
  8254. '/select', '-',
  8255. array('select' => array(
  8256. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8257. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8258. )
  8259. ),
  8260. array('option' => array('value' => '')), '/option',
  8261. $this->dateRegex['yearsRegex'],
  8262. '/select',
  8263. '/div'
  8264. );
  8265. $this->assertTags($result, $expected);
  8266. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8267. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8268. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8269. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8270. $expected = array(
  8271. 'div' => array('class' => 'input select error'),
  8272. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8273. 'Profile',
  8274. '/label',
  8275. 'select' => array(
  8276. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8277. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8278. 'class' => 'form-error'
  8279. ),
  8280. '/select',
  8281. array('div' => array('class' => 'error-message')),
  8282. 'Error',
  8283. '/div',
  8284. '/div'
  8285. );
  8286. $this->assertTags($result, $expected);
  8287. }
  8288. /**
  8289. * test the correct display of multi-record form validation errors.
  8290. *
  8291. * @return void
  8292. */
  8293. public function testMultiRecordFormValidationErrors() {
  8294. $this->Form->create('ValidateProfile');
  8295. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8296. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8297. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8298. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8299. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8300. $result = $this->Form->error('ValidateProfile.2.city');
  8301. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8302. $result = $this->Form->error('2.city');
  8303. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8304. }
  8305. /**
  8306. * test the correct display of multi-record form validation errors.
  8307. *
  8308. * @return void
  8309. */
  8310. public function testSaveManyRecordFormValidationErrors() {
  8311. $this->Form->create('ValidateUser');
  8312. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8313. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8314. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8315. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8316. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8317. $result = $this->Form->error('ValidateUser.0.city');
  8318. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8319. }
  8320. /**
  8321. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8322. *
  8323. * @return void
  8324. */
  8325. public function testInputTemplate() {
  8326. $result = $this->Form->input('Contact.email', array(
  8327. 'type' => 'text', 'format' => array('input')
  8328. ));
  8329. $expected = array(
  8330. 'div' => array('class' => 'input text'),
  8331. 'input' => array(
  8332. 'type' => 'text', 'name' => 'data[Contact][email]',
  8333. 'id' => 'ContactEmail'
  8334. ),
  8335. '/div'
  8336. );
  8337. $this->assertTags($result, $expected);
  8338. $result = $this->Form->input('Contact.email', array(
  8339. 'type' => 'text', 'format' => array('input', 'label'),
  8340. 'label' => '<em>Email (required)</em>'
  8341. ));
  8342. $expected = array(
  8343. 'div' => array('class' => 'input text'),
  8344. array('input' => array(
  8345. 'type' => 'text', 'name' => 'data[Contact][email]',
  8346. 'id' => 'ContactEmail'
  8347. )),
  8348. 'label' => array('for' => 'ContactEmail'),
  8349. 'em' => array(),
  8350. 'Email (required)',
  8351. '/em',
  8352. '/label',
  8353. '/div'
  8354. );
  8355. $this->assertTags($result, $expected);
  8356. $result = $this->Form->input('Contact.email', array(
  8357. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8358. 'between' => '<div>Something in the middle</div>',
  8359. 'after' => '<span>Some text at the end</span>'
  8360. ));
  8361. $expected = array(
  8362. 'div' => array('class' => 'input text'),
  8363. array('input' => array(
  8364. 'type' => 'text', 'name' => 'data[Contact][email]',
  8365. 'id' => 'ContactEmail'
  8366. )),
  8367. array('div' => array()),
  8368. 'Something in the middle',
  8369. '/div',
  8370. 'label' => array('for' => 'ContactEmail'),
  8371. 'Email',
  8372. '/label',
  8373. 'span' => array(),
  8374. 'Some text at the end',
  8375. '/span',
  8376. '/div'
  8377. );
  8378. $this->assertTags($result, $expected);
  8379. $result = $this->Form->input('Contact.method', array(
  8380. 'type' => 'radio',
  8381. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8382. 'between' => 'I am between',
  8383. ));
  8384. $expected = array(
  8385. 'div' => array('class' => 'input radio'),
  8386. 'fieldset' => array(),
  8387. 'legend' => array(),
  8388. 'Method',
  8389. '/legend',
  8390. 'I am between',
  8391. 'input' => array(
  8392. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8393. 'value' => '', 'id' => 'ContactMethod_'
  8394. ),
  8395. array('input' => array(
  8396. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8397. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8398. )),
  8399. array('label' => array('for' => 'ContactMethodEmail')),
  8400. 'Email',
  8401. '/label',
  8402. array('input' => array(
  8403. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8404. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8405. )),
  8406. array('label' => array('for' => 'ContactMethodPigeon')),
  8407. 'Pigeon',
  8408. '/label',
  8409. '/fieldset',
  8410. '/div',
  8411. );
  8412. $this->assertTags($result, $expected);
  8413. }
  8414. /**
  8415. * test that some html5 inputs + FormHelper::__call() work
  8416. *
  8417. * @return void
  8418. */
  8419. public function testHtml5Inputs() {
  8420. $result = $this->Form->email('User.email');
  8421. $expected = array(
  8422. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8423. );
  8424. $this->assertTags($result, $expected);
  8425. $result = $this->Form->search('User.query');
  8426. $expected = array(
  8427. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8428. );
  8429. $this->assertTags($result, $expected);
  8430. $result = $this->Form->search('User.query', array('value' => 'test'));
  8431. $expected = array(
  8432. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8433. );
  8434. $this->assertTags($result, $expected);
  8435. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8436. $expected = array(
  8437. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8438. );
  8439. $this->assertTags($result, $expected);
  8440. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8441. $expected = array(
  8442. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8443. );
  8444. $this->assertTags($result, $expected);
  8445. }
  8446. /**
  8447. *
  8448. * @expectedException CakeException
  8449. * @return void
  8450. */
  8451. public function testHtml5InputException() {
  8452. $this->Form->email();
  8453. }
  8454. /**
  8455. * Tests that a model can be loaded from the model names passed in the request object
  8456. *
  8457. * @return void
  8458. */
  8459. public function testIntrospectModelFromRequest() {
  8460. $this->loadFixtures('Post');
  8461. App::build(array(
  8462. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8463. ));
  8464. CakePlugin::load('TestPlugin');
  8465. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8466. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8467. $this->Form->create('TestPluginPost');
  8468. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8469. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8470. CakePlugin::unload();
  8471. App::build();
  8472. }
  8473. /**
  8474. * Tests that it is possible to set the validation errors directly in the helper for a field
  8475. *
  8476. * @return void
  8477. */
  8478. public function testCustomValidationErrors() {
  8479. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8480. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8481. $this->assertEquals('Badness!', $result);
  8482. }
  8483. /**
  8484. * Tests that the 'on' key validates as expected on create
  8485. *
  8486. * @return void
  8487. */
  8488. public function testRequiredOnCreate() {
  8489. $this->Form->create('Contact');
  8490. $result = $this->Form->input('Contact.imrequiredonupdate');
  8491. $expected = array(
  8492. 'div' => array('class' => 'input text'),
  8493. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8494. 'Imrequiredonupdate',
  8495. '/label',
  8496. 'input' => array(
  8497. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8498. 'id' => 'ContactImrequiredonupdate'
  8499. ),
  8500. '/div'
  8501. );
  8502. $this->assertTags($result, $expected);
  8503. $result = $this->Form->input('Contact.imrequiredoncreate');
  8504. $expected = array(
  8505. 'div' => array('class' => 'input text required'),
  8506. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8507. 'Imrequiredoncreate',
  8508. '/label',
  8509. 'input' => array(
  8510. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8511. 'id' => 'ContactImrequiredoncreate',
  8512. 'required' => 'required'
  8513. ),
  8514. '/div'
  8515. );
  8516. $this->assertTags($result, $expected);
  8517. $result = $this->Form->input('Contact.imrequiredonboth');
  8518. $expected = array(
  8519. 'div' => array('class' => 'input text required'),
  8520. 'label' => array('for' => 'ContactImrequiredonboth'),
  8521. 'Imrequiredonboth',
  8522. '/label',
  8523. 'input' => array(
  8524. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8525. 'id' => 'ContactImrequiredonboth',
  8526. 'required' => 'required'
  8527. ),
  8528. '/div'
  8529. );
  8530. $this->assertTags($result, $expected);
  8531. $this->Form->inputDefaults(array('required' => false));
  8532. $result = $this->Form->input('Contact.imrequired');
  8533. $expected = array(
  8534. 'div' => array('class' => 'input text'),
  8535. 'label' => array('for' => 'ContactImrequired'),
  8536. 'Imrequired',
  8537. '/label',
  8538. 'input' => array(
  8539. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8540. 'id' => 'ContactImrequired'
  8541. ),
  8542. '/div'
  8543. );
  8544. $this->assertTags($result, $expected);
  8545. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8546. $this->assertTags($result, $expected);
  8547. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8548. $expected = array(
  8549. 'div' => array('class' => 'input text required'),
  8550. 'label' => array('for' => 'ContactImrequired'),
  8551. 'Imrequired',
  8552. '/label',
  8553. 'input' => array(
  8554. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8555. 'id' => 'ContactImrequired'
  8556. ),
  8557. '/div'
  8558. );
  8559. $this->assertTags($result, $expected);
  8560. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8561. $this->assertTags($result, $expected);
  8562. }
  8563. /**
  8564. * Tests that the 'on' key validates as expected on update
  8565. *
  8566. * @return void
  8567. */
  8568. public function testRequiredOnUpdate() {
  8569. $this->Form->request->data['Contact']['id'] = 1;
  8570. $this->Form->create('Contact');
  8571. $result = $this->Form->input('Contact.imrequiredonupdate');
  8572. $expected = array(
  8573. 'div' => array('class' => 'input text required'),
  8574. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8575. 'Imrequiredonupdate',
  8576. '/label',
  8577. 'input' => array(
  8578. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8579. 'id' => 'ContactImrequiredonupdate',
  8580. 'required' => 'required'
  8581. ),
  8582. '/div'
  8583. );
  8584. $this->assertTags($result, $expected);
  8585. $result = $this->Form->input('Contact.imrequiredoncreate');
  8586. $expected = array(
  8587. 'div' => array('class' => 'input text'),
  8588. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8589. 'Imrequiredoncreate',
  8590. '/label',
  8591. 'input' => array(
  8592. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8593. 'id' => 'ContactImrequiredoncreate'
  8594. ),
  8595. '/div'
  8596. );
  8597. $this->assertTags($result, $expected);
  8598. $result = $this->Form->input('Contact.imrequiredonboth');
  8599. $expected = array(
  8600. 'div' => array('class' => 'input text required'),
  8601. 'label' => array('for' => 'ContactImrequiredonboth'),
  8602. 'Imrequiredonboth',
  8603. '/label',
  8604. 'input' => array(
  8605. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8606. 'id' => 'ContactImrequiredonboth',
  8607. 'required' => 'required'
  8608. ),
  8609. '/div'
  8610. );
  8611. $this->assertTags($result, $expected);
  8612. $result = $this->Form->input('Contact.imrequired');
  8613. $expected = array(
  8614. 'div' => array('class' => 'input text required'),
  8615. 'label' => array('for' => 'ContactImrequired'),
  8616. 'Imrequired',
  8617. '/label',
  8618. 'input' => array(
  8619. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8620. 'id' => 'ContactImrequired',
  8621. 'required' => 'required'
  8622. ),
  8623. '/div'
  8624. );
  8625. $this->assertTags($result, $expected);
  8626. }
  8627. /**
  8628. * Test inputDefaults setter and getter
  8629. *
  8630. * @return void
  8631. */
  8632. public function testInputDefaults() {
  8633. $this->Form->create('Contact');
  8634. $this->Form->inputDefaults(array(
  8635. 'label' => false,
  8636. 'div' => array(
  8637. 'style' => 'color: #000;'
  8638. )
  8639. ));
  8640. $result = $this->Form->input('Contact.field1');
  8641. $expected = array(
  8642. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8643. 'input' => array(
  8644. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8645. 'id' => 'ContactField1'
  8646. ),
  8647. '/div'
  8648. );
  8649. $this->assertTags($result, $expected);
  8650. $this->Form->inputDefaults(array(
  8651. 'div' => false,
  8652. 'label' => 'Label',
  8653. ));
  8654. $result = $this->Form->input('Contact.field1');
  8655. $expected = array(
  8656. 'label' => array('for' => 'ContactField1'),
  8657. 'Label',
  8658. '/label',
  8659. 'input' => array(
  8660. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8661. 'id' => 'ContactField1'
  8662. ),
  8663. );
  8664. $this->assertTags($result, $expected);
  8665. $this->Form->inputDefaults(array(
  8666. 'label' => false,
  8667. ), true);
  8668. $result = $this->Form->input('Contact.field1');
  8669. $expected = array(
  8670. 'input' => array(
  8671. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8672. 'id' => 'ContactField1'
  8673. ),
  8674. );
  8675. $this->assertTags($result, $expected);
  8676. $result = $this->Form->inputDefaults();
  8677. $expected = array(
  8678. 'div' => false,
  8679. 'label' => false,
  8680. );
  8681. $this->assertEquals($expected, $result);
  8682. }
  8683. }