FormHelperTest.php 301 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483
  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 interval & timeFormat = 12
  2106. *
  2107. * @return void
  2108. */
  2109. public function testInputTimeWithIntervalAnd12HourFormat() {
  2110. $result = $this->Form->input('Model.start_time', array(
  2111. 'type' => 'time',
  2112. 'timeFormat' => 12,
  2113. 'interval' => 5,
  2114. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2115. ));
  2116. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2117. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2118. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2119. $result = $this->Form->input('Model.start_time', array(
  2120. 'type' => 'time',
  2121. 'timeFormat' => '12',
  2122. 'interval' => 5,
  2123. 'selected' => '2013-04-19 16:30:00'
  2124. ));
  2125. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2126. $this->assertContains('<option value="30" selected="selected">30</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' => 10,
  2132. 'selected' => '2013-05-19 00:33:00'
  2133. ));
  2134. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2135. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2136. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2137. $result = $this->Form->input('Model.start_time', array(
  2138. 'type' => 'time',
  2139. 'timeFormat' => '12',
  2140. 'interval' => 10,
  2141. 'selected' => '2013-05-19 13:33:00'
  2142. ));
  2143. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2144. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2145. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2146. $result = $this->Form->input('Model.start_time', array(
  2147. 'type' => 'time',
  2148. 'timeFormat' => '12',
  2149. 'interval' => 10,
  2150. 'selected' => '2013-05-19 01:33:00'
  2151. ));
  2152. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2153. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2154. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2155. }
  2156. /**
  2157. * test form->input() with datetime, date and time types
  2158. *
  2159. * @return void
  2160. */
  2161. public function testInputDatetime() {
  2162. extract($this->dateRegex);
  2163. $result = $this->Form->input('prueba', array(
  2164. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2165. 'maxYear' => date('Y') + 1, 'interval' => 15
  2166. ));
  2167. $result = explode(':', $result);
  2168. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2169. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2170. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2171. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2172. //related to ticket #5013
  2173. $result = $this->Form->input('Contact.date', array(
  2174. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2175. ));
  2176. $this->assertRegExp('/class="customClass"/', $result);
  2177. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2178. $result = $this->Form->input('Contact.date', array(
  2179. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2180. ));
  2181. $this->assertRegExp('/id="customIdDay"/', $result);
  2182. $this->assertRegExp('/id="customIdMonth"/', $result);
  2183. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2184. $result = $this->Form->input('Model.field', array(
  2185. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2186. ));
  2187. $this->assertRegExp('/id="customIDDay"/', $result);
  2188. $this->assertRegExp('/id="customIDHour"/', $result);
  2189. $result = explode('</select><select', $result);
  2190. $result = explode(':', $result[1]);
  2191. $this->assertRegExp('/option value="23"/', $result[0]);
  2192. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2193. $result = $this->Form->input('Model.field', array(
  2194. 'type' => 'datetime', 'timeFormat' => 12
  2195. ));
  2196. $result = explode('</select><select', $result);
  2197. $result = explode(':', $result[1]);
  2198. $this->assertRegExp('/option value="12"/', $result[0]);
  2199. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2200. $this->Form->request->data = array('Contact' => array('created' => null));
  2201. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2202. $expected = array(
  2203. 'div' => array('class' => 'input date'),
  2204. 'label' => array('for' => 'ContactCreatedMonth'),
  2205. 'Created',
  2206. '/label',
  2207. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2208. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2209. $monthsRegex,
  2210. '/select', '-',
  2211. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2212. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2213. $daysRegex,
  2214. '/select', '-',
  2215. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2216. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2217. $yearsRegex,
  2218. '/select',
  2219. '/div'
  2220. );
  2221. $this->assertTags($result, $expected);
  2222. $this->Form->request->data = array('Contact' => array('created' => null));
  2223. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2224. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2225. $this->Form->request->data = array('Contact' => array('created' => null));
  2226. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2227. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2228. $result = $this->Form->input('Contact.created', array(
  2229. 'type' => 'date',
  2230. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2231. 'timeFormat' => 'NONE'
  2232. ));
  2233. $this->assertRegExp('/for\="created-month"/', $result);
  2234. }
  2235. /**
  2236. * Test generating checkboxes in a loop.
  2237. *
  2238. * @return void
  2239. */
  2240. public function testInputCheckboxesInLoop() {
  2241. for ($i = 1; $i < 5; $i++) {
  2242. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2243. $expected = array(
  2244. 'div' => array('class' => 'input checkbox'),
  2245. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2246. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2247. 'label' => array('for' => "Contact{$i}Email"),
  2248. 'Email',
  2249. '/label',
  2250. '/div'
  2251. );
  2252. $this->assertTags($result, $expected);
  2253. }
  2254. }
  2255. /**
  2256. * Test generating checkboxes with disabled elements.
  2257. *
  2258. * @return void
  2259. */
  2260. public function testInputCheckboxWithDisabledElements() {
  2261. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2262. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2263. $expected = array(
  2264. array('div' => array('class' => 'input select')),
  2265. array('label' => array('for' => "ContactMultiple")),
  2266. 'Multiple',
  2267. '/label',
  2268. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2269. array('div' => array('class' => 'checkbox')),
  2270. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2271. array('label' => array('for' => "ContactMultiple1")),
  2272. 'One',
  2273. '/label',
  2274. '/div',
  2275. array('div' => array('class' => 'checkbox')),
  2276. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2277. array('label' => array('for' => "ContactMultiple2")),
  2278. 'Two',
  2279. '/label',
  2280. '/div',
  2281. array('div' => array('class' => 'checkbox')),
  2282. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2283. array('label' => array('for' => "ContactMultiple3")),
  2284. 'Three',
  2285. '/label',
  2286. '/div',
  2287. '/div'
  2288. );
  2289. $this->assertTags($result, $expected);
  2290. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2291. $this->assertTags($result, $expected);
  2292. $disabled = array('2', 3);
  2293. $expected = array(
  2294. array('div' => array('class' => 'input select')),
  2295. array('label' => array('for' => "ContactMultiple")),
  2296. 'Multiple',
  2297. '/label',
  2298. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2299. array('div' => array('class' => 'checkbox')),
  2300. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2301. array('label' => array('for' => "ContactMultiple1")),
  2302. 'One',
  2303. '/label',
  2304. '/div',
  2305. array('div' => array('class' => 'checkbox')),
  2306. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2307. array('label' => array('for' => "ContactMultiple2")),
  2308. 'Two',
  2309. '/label',
  2310. '/div',
  2311. array('div' => array('class' => 'checkbox')),
  2312. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2313. array('label' => array('for' => "ContactMultiple3")),
  2314. 'Three',
  2315. '/label',
  2316. '/div',
  2317. '/div'
  2318. );
  2319. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2320. $this->assertTags($result, $expected);
  2321. // make sure 50 does only disable 50, and not 50f5c0cf
  2322. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2323. $disabled = array(50);
  2324. $expected = array(
  2325. array('div' => array('class' => 'input select')),
  2326. array('label' => array('for' => "ContactMultiple")),
  2327. 'Multiple',
  2328. '/label',
  2329. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2330. array('div' => array('class' => 'checkbox')),
  2331. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2332. array('label' => array('for' => "ContactMultiple50")),
  2333. 'Fifty',
  2334. '/label',
  2335. '/div',
  2336. array('div' => array('class' => 'checkbox')),
  2337. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2338. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2339. 'Stringy',
  2340. '/label',
  2341. '/div',
  2342. '/div'
  2343. );
  2344. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2345. $this->assertTags($result, $expected);
  2346. }
  2347. /**
  2348. * test input name with leading integer, ensure attributes are generated correctly.
  2349. *
  2350. * @return void
  2351. */
  2352. public function testInputWithLeadingInteger() {
  2353. $result = $this->Form->text('0.Node.title');
  2354. $expected = array(
  2355. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2356. );
  2357. $this->assertTags($result, $expected);
  2358. }
  2359. /**
  2360. * test form->input() with select type inputs.
  2361. *
  2362. * @return void
  2363. */
  2364. public function testInputSelectType() {
  2365. $result = $this->Form->input('email', array(
  2366. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2367. );
  2368. $expected = array(
  2369. 'div' => array('class' => 'input select'),
  2370. 'label' => array('for' => 'email'),
  2371. 'Email',
  2372. '/label',
  2373. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2374. array('option' => array('value' => '')),
  2375. '/option',
  2376. array('option' => array('value' => 'è')),
  2377. 'Firést',
  2378. '/option',
  2379. array('option' => array('value' => 'é')),
  2380. 'Secoènd',
  2381. '/option',
  2382. '/select',
  2383. '/div'
  2384. );
  2385. $this->assertTags($result, $expected);
  2386. $result = $this->Form->input('email', array(
  2387. 'options' => array('First', 'Second'), 'empty' => true)
  2388. );
  2389. $expected = array(
  2390. 'div' => array('class' => 'input select'),
  2391. 'label' => array('for' => 'email'),
  2392. 'Email',
  2393. '/label',
  2394. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2395. array('option' => array('value' => '')),
  2396. '/option',
  2397. array('option' => array('value' => '0')),
  2398. 'First',
  2399. '/option',
  2400. array('option' => array('value' => '1')),
  2401. 'Second',
  2402. '/option',
  2403. '/select',
  2404. '/div'
  2405. );
  2406. $this->assertTags($result, $expected);
  2407. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2408. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2409. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2410. $expected = array(
  2411. 'div' => array('class' => 'input select'),
  2412. 'label' => array('for' => 'ModelUserId'),
  2413. 'User',
  2414. '/label',
  2415. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2416. array('option' => array('value' => '')),
  2417. '/option',
  2418. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2419. 'good',
  2420. '/option',
  2421. array('option' => array('value' => 'other')),
  2422. 'bad',
  2423. '/option',
  2424. '/select',
  2425. '/div'
  2426. );
  2427. $this->assertTags($result, $expected);
  2428. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2429. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2430. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2431. $expected = array(
  2432. 'div' => array('class' => 'input select'),
  2433. 'label' => array('for' => 'ThingUserId'),
  2434. 'User',
  2435. '/label',
  2436. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2437. array('option' => array('value' => '')),
  2438. 'Some Empty',
  2439. '/option',
  2440. array('option' => array('value' => 'value')),
  2441. 'good',
  2442. '/option',
  2443. array('option' => array('value' => 'other')),
  2444. 'bad',
  2445. '/option',
  2446. '/select',
  2447. '/div'
  2448. );
  2449. $this->assertTags($result, $expected);
  2450. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2451. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2452. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2453. $expected = array(
  2454. 'div' => array('class' => 'input select'),
  2455. 'label' => array('for' => 'ThingUserId'),
  2456. 'User',
  2457. '/label',
  2458. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2459. array('option' => array('value' => '')),
  2460. 'Some Empty',
  2461. '/option',
  2462. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2463. 'good',
  2464. '/option',
  2465. array('option' => array('value' => 'other')),
  2466. 'bad',
  2467. '/option',
  2468. '/select',
  2469. '/div'
  2470. );
  2471. $this->assertTags($result, $expected);
  2472. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2473. $this->Form->request->data = array('User' => array('User' => array('value')));
  2474. $result = $this->Form->input('User.User', array('empty' => true));
  2475. $expected = array(
  2476. 'div' => array('class' => 'input select'),
  2477. 'label' => array('for' => 'UserUser'),
  2478. 'User',
  2479. '/label',
  2480. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2481. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2482. array('option' => array('value' => '')),
  2483. '/option',
  2484. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2485. 'good',
  2486. '/option',
  2487. array('option' => array('value' => 'other')),
  2488. 'bad',
  2489. '/option',
  2490. '/select',
  2491. '/div'
  2492. );
  2493. $this->assertTags($result, $expected);
  2494. $this->Form->data = array();
  2495. $result = $this->Form->input('Publisher.id', array(
  2496. 'label' => 'Publisher',
  2497. 'type' => 'select',
  2498. 'multiple' => 'checkbox',
  2499. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2500. ));
  2501. $expected = array(
  2502. array('div' => array('class' => 'input select')),
  2503. array('label' => array('for' => 'PublisherId')),
  2504. 'Publisher',
  2505. '/label',
  2506. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2507. array('div' => array('class' => 'checkbox')),
  2508. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2509. array('label' => array('for' => 'PublisherIdValue1')),
  2510. 'Label 1',
  2511. '/label',
  2512. '/div',
  2513. array('div' => array('class' => 'checkbox')),
  2514. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2515. array('label' => array('for' => 'PublisherIdValue2')),
  2516. 'Label 2',
  2517. '/label',
  2518. '/div',
  2519. '/div'
  2520. );
  2521. $this->assertTags($result, $expected);
  2522. }
  2523. /**
  2524. * test that input() and a non standard primary key makes a hidden input by default.
  2525. *
  2526. * @return void
  2527. */
  2528. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2529. $this->Form->create('User');
  2530. $this->Form->fieldset = array(
  2531. 'User' => array(
  2532. 'fields' => array(
  2533. 'model_id' => array('type' => 'integer')
  2534. ),
  2535. 'validates' => array(),
  2536. 'key' => 'model_id'
  2537. )
  2538. );
  2539. $result = $this->Form->input('model_id');
  2540. $expected = array(
  2541. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2542. );
  2543. $this->assertTags($result, $expected);
  2544. }
  2545. /**
  2546. * test that overriding the magic select type widget is possible
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputOverridingMagicSelectType() {
  2551. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2552. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2553. $expected = array(
  2554. 'div' => array('class' => 'input text'),
  2555. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2556. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2557. '/div'
  2558. );
  2559. $this->assertTags($result, $expected);
  2560. //Check that magic types still work for plural/singular vars
  2561. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2562. $result = $this->Form->input('Model.type');
  2563. $expected = array(
  2564. 'div' => array('class' => 'input select'),
  2565. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2566. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2567. array('option' => array('value' => 'value')), 'good', '/option',
  2568. array('option' => array('value' => 'other')), 'bad', '/option',
  2569. '/select',
  2570. '/div'
  2571. );
  2572. $this->assertTags($result, $expected);
  2573. }
  2574. /**
  2575. * Test that magic input() selects are created for type=number
  2576. *
  2577. * @return void
  2578. */
  2579. public function testInputMagicSelectForTypeNumber() {
  2580. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2581. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2582. $result = $this->Form->input('ValidateUser.balance');
  2583. $expected = array(
  2584. 'div' => array('class' => 'input select'),
  2585. 'label' => array('for' => 'ValidateUserBalance'),
  2586. 'Balance',
  2587. '/label',
  2588. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2589. array('option' => array('value' => '0')),
  2590. 'nothing',
  2591. '/option',
  2592. array('option' => array('value' => '1', 'selected' => 'selected')),
  2593. 'some',
  2594. '/option',
  2595. array('option' => array('value' => '100')),
  2596. 'a lot',
  2597. '/option',
  2598. '/select',
  2599. '/div'
  2600. );
  2601. $this->assertTags($result, $expected);
  2602. }
  2603. /**
  2604. * Test that magic input() selects can easily be converted into radio types without error.
  2605. *
  2606. * @return void
  2607. */
  2608. public function testInputMagicSelectChangeToRadio() {
  2609. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2610. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2611. $this->assertRegExp('/input type="radio"/', $result);
  2612. }
  2613. /**
  2614. * fields with the same name as the model should work.
  2615. *
  2616. * @return void
  2617. */
  2618. public function testInputWithMatchingFieldAndModelName() {
  2619. $this->Form->create('User');
  2620. $this->Form->fieldset = array(
  2621. 'User' => array(
  2622. 'fields' => array(
  2623. 'User' => array('type' => 'text')
  2624. ),
  2625. 'validates' => array(),
  2626. 'key' => 'id'
  2627. )
  2628. );
  2629. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2630. $result = $this->Form->input('User', array('type' => 'text'));
  2631. $expected = array(
  2632. 'div' => array('class' => 'input text'),
  2633. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2634. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2635. '/div'
  2636. );
  2637. $this->assertTags($result, $expected);
  2638. }
  2639. /**
  2640. * testFormInputs method
  2641. *
  2642. * test correct results from form::inputs().
  2643. *
  2644. * @return void
  2645. */
  2646. public function testFormInputs() {
  2647. $this->Form->create('Contact');
  2648. $result = $this->Form->inputs('The Legend');
  2649. $expected = array(
  2650. '<fieldset',
  2651. '<legend',
  2652. 'The Legend',
  2653. '/legend',
  2654. '*/fieldset',
  2655. );
  2656. $this->assertTags($result, $expected);
  2657. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2658. $expected = array(
  2659. 'fieldset' => array('class' => 'classy-stuff'),
  2660. '<legend',
  2661. 'Field of Dreams',
  2662. '/legend',
  2663. '*/fieldset'
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $this->Form->create('Contact');
  2671. $this->Form->request['prefix'] = 'admin';
  2672. $this->Form->request['action'] = 'admin_edit';
  2673. $result = $this->Form->inputs();
  2674. $expected = array(
  2675. '<fieldset',
  2676. '<legend',
  2677. 'Edit Contact',
  2678. '/legend',
  2679. '*/fieldset',
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. $this->Form->create('Contact');
  2683. $result = $this->Form->inputs(false);
  2684. $expected = array(
  2685. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2686. array('div' => array('class' => 'input text')),
  2687. '*/div',
  2688. array('div' => array('class' => 'input email')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input tel')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input password')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input date')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input datetime')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input number')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input select')),
  2703. '*/div',
  2704. );
  2705. $this->assertTags($result, $expected);
  2706. $this->Form->create('Contact');
  2707. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2708. $expected = array(
  2709. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2710. array('div' => array('class' => 'input text')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input email')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input tel')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input password')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input date')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input datetime')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input number')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input select')),
  2727. '*/div',
  2728. );
  2729. $this->assertTags($result, $expected);
  2730. $this->Form->create('Contact');
  2731. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2732. $this->assertTags($result, $expected);
  2733. $this->Form->create('Contact');
  2734. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2735. $expected = array(
  2736. 'fieldset' => array(),
  2737. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2738. array('div' => array('class' => 'input text')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input email')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input tel')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input password')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input date')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input datetime')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input number')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input select')),
  2755. '*/div',
  2756. '/fieldset'
  2757. );
  2758. $this->assertTags($result, $expected);
  2759. $this->Form->create('Contact');
  2760. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2761. $expected = array(
  2762. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2763. array('div' => array('class' => 'input text')),
  2764. '*/div',
  2765. array('div' => array('class' => 'input email')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input tel')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input password')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input date')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input datetime')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input number')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input select')),
  2780. '*/div',
  2781. );
  2782. $this->assertTags($result, $expected);
  2783. $this->Form->create('Contact');
  2784. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs('Hello');
  2788. $expected = array(
  2789. 'fieldset' => array(),
  2790. 'legend' => array(),
  2791. 'Hello',
  2792. '/legend',
  2793. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2794. array('div' => array('class' => 'input text')),
  2795. '*/div',
  2796. array('div' => array('class' => 'input email')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input tel')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input password')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input date')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input datetime')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input number')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input select')),
  2811. '*/div',
  2812. '/fieldset'
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. $this->Form->create('Contact');
  2816. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2817. $expected = array(
  2818. 'fieldset' => array(),
  2819. 'legend' => array(),
  2820. 'Hello',
  2821. '/legend',
  2822. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2823. array('div' => array('class' => 'input text')),
  2824. '*/div',
  2825. array('div' => array('class' => 'input email')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input tel')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input password')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input date')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input datetime')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input number')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input select')),
  2840. '*/div',
  2841. '/fieldset'
  2842. );
  2843. $this->assertTags($result, $expected);
  2844. $this->Form->create('Contact');
  2845. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2846. $this->assertTags($result, $expected);
  2847. $this->Form->end();
  2848. $this->Form->create(false);
  2849. $expected = array(
  2850. 'fieldset' => array(),
  2851. array('div' => array('class' => 'input text')),
  2852. 'label' => array('for' => 'foo'),
  2853. 'Foo',
  2854. '/label',
  2855. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2856. '*/div',
  2857. '/fieldset'
  2858. );
  2859. $result = $this->Form->inputs(
  2860. array('foo' => array('type' => 'text')),
  2861. array(),
  2862. array('legend' => false)
  2863. );
  2864. $this->assertTags($result, $expected);
  2865. }
  2866. /**
  2867. * Tests inputs() works with plugin models
  2868. *
  2869. * @return void
  2870. */
  2871. public function testInputsPluginModel() {
  2872. $this->loadFixtures('Post');
  2873. App::build(array(
  2874. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2875. ));
  2876. CakePlugin::load('TestPlugin');
  2877. $this->Form->request['models'] = array(
  2878. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2879. );
  2880. $this->Form->create('TestPlugin.TestPluginPost');
  2881. $result = $this->Form->inputs();
  2882. $this->assertContains('data[TestPluginPost][id]', $result);
  2883. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2884. $this->assertContains('data[TestPluginPost][title]', $result);
  2885. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2886. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2887. $this->assertEquals('TestPluginPost', $this->Form->model());
  2888. }
  2889. /**
  2890. * testSelectAsCheckbox method
  2891. *
  2892. * test multi-select widget with checkbox formatting.
  2893. *
  2894. * @return void
  2895. */
  2896. public function testSelectAsCheckbox() {
  2897. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2898. $expected = array(
  2899. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2900. array('div' => array('class' => 'checkbox')),
  2901. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2902. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2903. 'first',
  2904. '/label',
  2905. '/div',
  2906. array('div' => array('class' => 'checkbox')),
  2907. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2908. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2909. 'second',
  2910. '/label',
  2911. '/div',
  2912. array('div' => array('class' => 'checkbox')),
  2913. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2914. array('label' => array('for' => 'ModelMultiField2')),
  2915. 'third',
  2916. '/label',
  2917. '/div',
  2918. );
  2919. $this->assertTags($result, $expected);
  2920. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2921. $expected = array(
  2922. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2923. array('div' => array('class' => 'checkbox')),
  2924. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/2')),
  2925. array('label' => array('for' => 'ModelMultiField1/2')),
  2926. 'half',
  2927. '/label',
  2928. '/div',
  2929. );
  2930. $this->assertTags($result, $expected);
  2931. }
  2932. /**
  2933. * testLabel method
  2934. *
  2935. * test label generation.
  2936. *
  2937. * @return void
  2938. */
  2939. public function testLabel() {
  2940. $this->Form->text('Person.name');
  2941. $result = $this->Form->label();
  2942. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2943. $this->Form->text('Person.name');
  2944. $result = $this->Form->label();
  2945. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2946. $result = $this->Form->label('Person.first_name');
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name', 'Your first name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', '');
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2956. $result = $this->Form->label('Person.2.name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2958. }
  2959. /**
  2960. * testTextbox method
  2961. *
  2962. * test textbox element generation
  2963. *
  2964. * @return void
  2965. */
  2966. public function testTextbox() {
  2967. $result = $this->Form->text('Model.field');
  2968. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2969. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2970. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2972. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2973. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2974. $result = $this->Form->text('Model.text');
  2975. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2976. $Contact = ClassRegistry::getObject('Contact');
  2977. $Contact->validationErrors['text'] = array(true);
  2978. $this->Form->request->data['Contact']['text'] = 'test';
  2979. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2980. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2981. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2982. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2983. $expected = array(
  2984. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2985. );
  2986. $this->assertTags($result, $expected);
  2987. }
  2988. /**
  2989. * testDefaultValue method
  2990. *
  2991. * Test default value setting
  2992. *
  2993. * @return void
  2994. */
  2995. public function testDefaultValue() {
  2996. $this->Form->request->data['Model']['field'] = 'test';
  2997. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2998. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2999. unset($this->Form->request->data['Model']['field']);
  3000. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3001. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3002. }
  3003. /**
  3004. * testCheckboxDefaultValue method
  3005. *
  3006. * Test default value setting on checkbox() method
  3007. *
  3008. * @return void
  3009. */
  3010. public function testCheckboxDefaultValue() {
  3011. $this->Form->request->data['Model']['field'] = false;
  3012. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3013. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3014. unset($this->Form->request->data['Model']['field']);
  3015. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3016. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3017. $this->Form->request->data['Model']['field'] = true;
  3018. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3019. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3020. unset($this->Form->request->data['Model']['field']);
  3021. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3022. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3023. }
  3024. /**
  3025. * testError method
  3026. *
  3027. * Test field error generation
  3028. *
  3029. * @return void
  3030. */
  3031. public function testError() {
  3032. $Contact = ClassRegistry::getObject('Contact');
  3033. $Contact->validationErrors['field'] = array(1);
  3034. $result = $this->Form->error('Contact.field');
  3035. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3036. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3037. $this->assertEquals('Error in field Field', $result);
  3038. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3039. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3040. $this->assertEquals('This field contains invalid input', $result);
  3041. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3042. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3043. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3044. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3046. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3047. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3051. $this->assertEquals('<strong>Badness!</strong>', $result);
  3052. $Contact->validationErrors['field'] = array("email");
  3053. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3054. $expected = array(
  3055. 'div' => array('class' => 'field-error'),
  3056. 'No good!',
  3057. '/div'
  3058. );
  3059. $this->assertTags($result, $expected);
  3060. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3061. $result = $this->Form->error('Contact.field', array(
  3062. 'notEmpty' => 'Cannot be empty',
  3063. 'email' => 'No good!'
  3064. ));
  3065. $expected = array(
  3066. 'div' => array('class' => 'error-message'),
  3067. 'ul' => array(),
  3068. '<li', 'Cannot be empty', '/li',
  3069. '<li', 'No good!', '/li',
  3070. '<li', 'Something else', '/li',
  3071. '/ul',
  3072. '/div'
  3073. );
  3074. $this->assertTags($result, $expected);
  3075. // Testing error messages list options
  3076. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3077. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3078. $expected = array(
  3079. 'div' => array('class' => 'error-message'),
  3080. 'ol' => array(),
  3081. '<li', 'notEmpty', '/li',
  3082. '<li', 'email', '/li',
  3083. '/ol',
  3084. '/div'
  3085. );
  3086. $this->assertTags($result, $expected);
  3087. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3088. $expected = array(
  3089. 'div' => array('class' => 'error-message'),
  3090. 'ol' => array(),
  3091. '<li', 'notEmpty', '/li',
  3092. '<li', 'email', '/li',
  3093. '/ol',
  3094. '/div'
  3095. );
  3096. $this->assertTags($result, $expected);
  3097. $result = $this->Form->error('Contact.field', null, array(
  3098. 'listOptions' => array(
  3099. 'class' => 'ul-class',
  3100. 'itemOptions' => array(
  3101. 'class' => 'li-class'
  3102. )
  3103. )
  3104. ));
  3105. $expected = array(
  3106. 'div' => array('class' => 'error-message'),
  3107. 'ul' => array('class' => 'ul-class'),
  3108. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3109. array('li' => array('class' => 'li-class')), 'email', '/li',
  3110. '/ul',
  3111. '/div'
  3112. );
  3113. $this->assertTags($result, $expected);
  3114. }
  3115. /**
  3116. * test error options when using form->input();
  3117. *
  3118. * @return void
  3119. */
  3120. public function testInputErrorEscape() {
  3121. $this->Form->create('ValidateProfile');
  3122. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3123. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3124. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3125. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3126. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3127. $this->assertRegExp('/required<br>/', $result);
  3128. }
  3129. /**
  3130. * testPassword method
  3131. *
  3132. * Test password element generation
  3133. *
  3134. * @return void
  3135. */
  3136. public function testPassword() {
  3137. $Contact = ClassRegistry::getObject('Contact');
  3138. $result = $this->Form->password('Contact.field');
  3139. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3140. $Contact->validationErrors['passwd'] = 1;
  3141. $this->Form->request->data['Contact']['passwd'] = 'test';
  3142. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3143. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3144. }
  3145. /**
  3146. * testRadio method
  3147. *
  3148. * Test radio element set generation
  3149. *
  3150. * @return void
  3151. */
  3152. public function testRadio() {
  3153. $result = $this->Form->radio('Model.field', array('option A'));
  3154. $expected = array(
  3155. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3156. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3157. 'label' => array('for' => 'ModelField0'),
  3158. 'option A',
  3159. '/label'
  3160. );
  3161. $this->assertTags($result, $expected);
  3162. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3163. $expected = array(
  3164. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3165. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3166. 'label' => array('for' => 'ModelField12'),
  3167. 'half',
  3168. '/label'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3172. $expected = array(
  3173. 'fieldset' => array(),
  3174. 'legend' => array(),
  3175. 'Field',
  3176. '/legend',
  3177. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3178. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3179. array('label' => array('for' => 'ModelField0')),
  3180. 'option A',
  3181. '/label',
  3182. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3183. array('label' => array('for' => 'ModelField1')),
  3184. 'option B',
  3185. '/label',
  3186. '/fieldset'
  3187. );
  3188. $this->assertTags($result, $expected);
  3189. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3190. $expected = array(
  3191. 'fieldset' => array(),
  3192. 'legend' => array(),
  3193. 'Field',
  3194. '/legend',
  3195. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3196. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3197. array('label' => array('for' => 'ModelField0')),
  3198. 'option A',
  3199. '/label',
  3200. 'br' => array(),
  3201. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3202. array('label' => array('for' => 'ModelField1')),
  3203. 'option B',
  3204. '/label',
  3205. '/fieldset'
  3206. );
  3207. $this->assertTags($result, $expected);
  3208. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3209. $expected = array(
  3210. 'fieldset' => array(),
  3211. 'legend' => array(),
  3212. 'Gender',
  3213. '/legend',
  3214. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3215. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3216. array('label' => array('for' => 'EmployeeGenderMale')),
  3217. 'Male',
  3218. '/label',
  3219. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3220. array('label' => array('for' => 'EmployeeGenderFemale')),
  3221. 'Female',
  3222. '/label',
  3223. '/fieldset',
  3224. );
  3225. $this->assertTags($result, $expected);
  3226. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3227. $expected = array(
  3228. 'fieldset' => array(),
  3229. 'legend' => array(),
  3230. 'Gender',
  3231. '/legend',
  3232. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3233. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3234. array('label' => array('for' => 'OfficerGenderMale')),
  3235. 'Male',
  3236. '/label',
  3237. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3238. array('label' => array('for' => 'OfficerGenderFemale')),
  3239. 'Female',
  3240. '/label',
  3241. '/fieldset',
  3242. );
  3243. $this->assertTags($result, $expected);
  3244. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3245. $expected = array(
  3246. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3247. array('input' => array(
  3248. 'type' => 'radio',
  3249. 'name' => 'data[Contact][1][imrequired]',
  3250. 'value' => '0',
  3251. 'id' => 'Contact1Imrequired0',
  3252. 'required' => 'required'
  3253. )),
  3254. 'label' => array('for' => 'Contact1Imrequired0'),
  3255. 'option A',
  3256. '/label'
  3257. );
  3258. $this->assertTags($result, $expected);
  3259. $result = $this->Form->radio('Model.1.field', array('option A'));
  3260. $expected = array(
  3261. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3262. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3263. 'label' => array('for' => 'Model1Field0'),
  3264. 'option A',
  3265. '/label'
  3266. );
  3267. $this->assertTags($result, $expected);
  3268. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3269. $expected = array(
  3270. 'fieldset' => array(),
  3271. 'legend' => array(),
  3272. 'Field',
  3273. '/legend',
  3274. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3275. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3276. array('label' => array('for' => 'ModelField0')),
  3277. 'option A',
  3278. '/label',
  3279. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3280. array('label' => array('for' => 'ModelField1')),
  3281. 'option B',
  3282. '/label',
  3283. '/fieldset'
  3284. );
  3285. $this->assertTags($result, $expected);
  3286. }
  3287. /**
  3288. * Test radio inputs with between as string or array. Also ensure
  3289. * that an array with less between elements works.
  3290. *
  3291. * @return void
  3292. */
  3293. public function testRadioBetween() {
  3294. $result = $this->Form->radio(
  3295. 'Model.field',
  3296. array('option A', 'option B'),
  3297. array('between' => 'I am between')
  3298. );
  3299. $expected = array(
  3300. 'fieldset' => array(),
  3301. 'legend' => array(),
  3302. 'Field',
  3303. '/legend',
  3304. 'I am between',
  3305. 'input' => array(
  3306. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3307. 'value' => '', 'id' => 'ModelField_'
  3308. ),
  3309. array('input' => array(
  3310. 'type' => 'radio', 'name' => 'data[Model][field]',
  3311. 'value' => '0', 'id' => 'ModelField0'
  3312. )),
  3313. array('label' => array('for' => 'ModelField0')),
  3314. 'option A',
  3315. '/label',
  3316. array('input' => array(
  3317. 'type' => 'radio', 'name' => 'data[Model][field]',
  3318. 'value' => '1', 'id' => 'ModelField1'
  3319. )),
  3320. array('label' => array('for' => 'ModelField1')),
  3321. 'option B',
  3322. '/label',
  3323. '/fieldset'
  3324. );
  3325. $this->assertTags($result, $expected);
  3326. $result = $this->Form->radio(
  3327. 'Model.field',
  3328. array('option A', 'option B', 'option C'),
  3329. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3330. );
  3331. $expected = array(
  3332. 'fieldset' => array(),
  3333. 'legend' => array(),
  3334. 'Field',
  3335. '/legend',
  3336. 'input' => array(
  3337. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3338. 'value' => '', 'id' => 'ModelField_'
  3339. ),
  3340. array('input' => array(
  3341. 'type' => 'radio', 'name' => 'data[Model][field]',
  3342. 'value' => '0', 'id' => 'ModelField0'
  3343. )),
  3344. array('label' => array('for' => 'ModelField0')),
  3345. 'option A',
  3346. '/label',
  3347. 'between A',
  3348. '--separator--',
  3349. array('input' => array(
  3350. 'type' => 'radio', 'name' => 'data[Model][field]',
  3351. 'value' => '1', 'id' => 'ModelField1'
  3352. )),
  3353. array('label' => array('for' => 'ModelField1')),
  3354. 'option B',
  3355. '/label',
  3356. 'between B',
  3357. '--separator--',
  3358. array('input' => array(
  3359. 'type' => 'radio', 'name' => 'data[Model][field]',
  3360. 'value' => '2', 'id' => 'ModelField2'
  3361. )),
  3362. array('label' => array('for' => 'ModelField2')),
  3363. 'option C',
  3364. '/label',
  3365. 'between C',
  3366. '/fieldset'
  3367. );
  3368. $this->assertTags($result, $expected);
  3369. $result = $this->Form->input('Model.field', array(
  3370. 'options' => array('1' => 'first', '2' => 'second'),
  3371. 'type' => 'radio',
  3372. 'before' => '--before--',
  3373. 'after' => '--after--',
  3374. 'separator' => '--separator--',
  3375. 'between' => array('--between first--', '--between second--')
  3376. ));
  3377. $expected = array(
  3378. 'div' => array('class' => 'input radio'),
  3379. '--before--',
  3380. 'fieldset' => array(),
  3381. 'legend' => array(),
  3382. 'Field',
  3383. '/legend',
  3384. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3385. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3386. array('label' => array('for' => 'ModelField1')),
  3387. 'first',
  3388. '/label',
  3389. '--between first--',
  3390. '--separator--',
  3391. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3392. array('label' => array('for' => 'ModelField2')),
  3393. 'second',
  3394. '/label',
  3395. '--between second--',
  3396. '/fieldset',
  3397. '--after--',
  3398. '/div'
  3399. );
  3400. $this->assertTags($result, $expected);
  3401. $result = $this->Form->input('Model.field', array(
  3402. 'options' => array('1' => 'first', '2' => 'second'),
  3403. 'type' => 'radio',
  3404. 'before' => '--before--',
  3405. 'after' => '--after--',
  3406. 'separator' => '--separator--',
  3407. 'between' => array('--between first--')
  3408. ));
  3409. $expected = array(
  3410. 'div' => array('class' => 'input radio'),
  3411. '--before--',
  3412. 'fieldset' => array(),
  3413. 'legend' => array(),
  3414. 'Field',
  3415. '/legend',
  3416. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3417. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3418. array('label' => array('for' => 'ModelField1')),
  3419. 'first',
  3420. '/label',
  3421. '--between first--',
  3422. '--separator--',
  3423. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3424. array('label' => array('for' => 'ModelField2')),
  3425. 'second',
  3426. '/label',
  3427. '/fieldset',
  3428. '--after--',
  3429. '/div'
  3430. );
  3431. $this->assertTags($result, $expected);
  3432. }
  3433. /**
  3434. * Test that radios with a 0 value are selected under the correct conditions.
  3435. * Also ensure that values that are booleanish are handled correctly.
  3436. *
  3437. * @return void
  3438. */
  3439. public function testRadioOptionWithBooleanishValues() {
  3440. $expected = array(
  3441. 'fieldset' => array(),
  3442. 'legend' => array(),
  3443. 'Field',
  3444. '/legend',
  3445. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3446. array('label' => array('for' => 'ModelField1')),
  3447. 'Yes',
  3448. '/label',
  3449. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3450. array('label' => array('for' => 'ModelField0')),
  3451. 'No',
  3452. '/label',
  3453. '/fieldset'
  3454. );
  3455. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3456. $this->assertTags($result, $expected);
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3460. $this->assertTags($result, $expected);
  3461. $expected = array(
  3462. 'fieldset' => array(),
  3463. 'legend' => array(),
  3464. 'Field',
  3465. '/legend',
  3466. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3467. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3468. array('label' => array('for' => 'ModelField1')),
  3469. 'Yes',
  3470. '/label',
  3471. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3472. array('label' => array('for' => 'ModelField0')),
  3473. 'No',
  3474. '/label',
  3475. '/fieldset'
  3476. );
  3477. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3478. $this->assertTags($result, $expected);
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3482. $this->assertTags($result, $expected);
  3483. $expected = array(
  3484. 'fieldset' => array(),
  3485. 'legend' => array(),
  3486. 'Field',
  3487. '/legend',
  3488. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3489. array('label' => array('for' => 'ModelField1')),
  3490. 'Yes',
  3491. '/label',
  3492. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3493. array('label' => array('for' => 'ModelField0')),
  3494. 'No',
  3495. '/label',
  3496. '/fieldset'
  3497. );
  3498. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3503. $this->assertTags($result, $expected);
  3504. }
  3505. /**
  3506. * test disabled radio options
  3507. *
  3508. * @return void
  3509. */
  3510. public function testRadioDisabled() {
  3511. $result = $this->Form->radio(
  3512. 'Model.field',
  3513. array('option A', 'option B'),
  3514. array('disabled' => array(0), 'value' => '0')
  3515. );
  3516. $expected = array(
  3517. 'fieldset' => array(),
  3518. 'legend' => array(),
  3519. 'Field',
  3520. '/legend',
  3521. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3522. array('label' => array('for' => 'ModelField0')),
  3523. 'option A',
  3524. '/label',
  3525. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3526. array('label' => array('for' => 'ModelField1')),
  3527. 'option B',
  3528. '/label',
  3529. '/fieldset'
  3530. );
  3531. $this->assertTags($result, $expected);
  3532. $result = $this->Form->radio(
  3533. 'Model.field',
  3534. array('option A', 'option B'),
  3535. array('disabled' => true, 'value' => '0')
  3536. );
  3537. $expected = array(
  3538. 'fieldset' => array(),
  3539. 'legend' => array(),
  3540. 'Field',
  3541. '/legend',
  3542. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3543. array('label' => array('for' => 'ModelField0')),
  3544. 'option A',
  3545. '/label',
  3546. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3547. array('label' => array('for' => 'ModelField1')),
  3548. 'option B',
  3549. '/label',
  3550. '/fieldset'
  3551. );
  3552. $this->assertTags($result, $expected);
  3553. $result = $this->Form->radio(
  3554. 'Model.field',
  3555. array('option A', 'option B'),
  3556. array('disabled' => 'disabled', 'value' => '0')
  3557. );
  3558. $expected = array(
  3559. 'fieldset' => array(),
  3560. 'legend' => array(),
  3561. 'Field',
  3562. '/legend',
  3563. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3564. array('label' => array('for' => 'ModelField0')),
  3565. 'option A',
  3566. '/label',
  3567. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3568. array('label' => array('for' => 'ModelField1')),
  3569. 'option B',
  3570. '/label',
  3571. '/fieldset'
  3572. );
  3573. $this->assertTags($result, $expected);
  3574. $result = $this->Form->input('Model.field', array(
  3575. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3576. 'type' => 'radio',
  3577. 'disabled' => array(2, '3x'),
  3578. ));
  3579. $expected = array(
  3580. 'div' => array('class' => 'input radio'),
  3581. 'fieldset' => array(),
  3582. 'legend' => array(),
  3583. 'Field',
  3584. '/legend',
  3585. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3586. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3587. array('label' => array('for' => 'ModelField1')),
  3588. 'first',
  3589. '/label',
  3590. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3591. array('label' => array('for' => 'ModelField2')),
  3592. 'second',
  3593. '/label',
  3594. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3595. array('label' => array('for' => 'ModelField2x')),
  3596. '2x',
  3597. '/label',
  3598. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3599. array('label' => array('for' => 'ModelField3')),
  3600. 'third',
  3601. '/label',
  3602. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3603. array('label' => array('for' => 'ModelField3x')),
  3604. '3x',
  3605. '/label',
  3606. '/fieldset',
  3607. '/div'
  3608. );
  3609. $this->assertTags($result, $expected);
  3610. $result = $this->Form->input('Model.field', array(
  3611. 'type' => 'radio',
  3612. 'options' => array(
  3613. 1 => 'A',
  3614. 2 => 'B',
  3615. 3 => 'C'
  3616. ),
  3617. 'disabled' => array(1)
  3618. ));
  3619. $expected = array(
  3620. 'div' => array('class' => 'input radio'),
  3621. 'fieldset' => array(),
  3622. 'legend' => array(),
  3623. 'Field',
  3624. '/legend',
  3625. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3626. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3627. array('label' => array('for' => 'ModelField1')),
  3628. 'A',
  3629. '/label',
  3630. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3631. array('label' => array('for' => 'ModelField2')),
  3632. 'B',
  3633. '/label',
  3634. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3635. array('label' => array('for' => 'ModelField3')),
  3636. 'C',
  3637. '/label',
  3638. '/fieldset',
  3639. '/div'
  3640. );
  3641. $this->assertTags($result, $expected);
  3642. }
  3643. /**
  3644. * test disabling the hidden input for radio buttons
  3645. *
  3646. * @return void
  3647. */
  3648. public function testRadioHiddenInputDisabling() {
  3649. $result = $this->Form->input('Model.1.field', array(
  3650. 'type' => 'radio',
  3651. 'options' => array('option A'),
  3652. 'hiddenField' => false
  3653. )
  3654. );
  3655. $expected = array(
  3656. 'div' => array('class' => 'input radio'),
  3657. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3658. 'label' => array('for' => 'Model1Field0'),
  3659. 'option A',
  3660. '/label',
  3661. '/div'
  3662. );
  3663. $this->assertTags($result, $expected);
  3664. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3665. $expected = array(
  3666. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3667. 'label' => array('for' => 'Model1Field0'),
  3668. 'option A',
  3669. '/label'
  3670. );
  3671. $this->assertTags($result, $expected);
  3672. }
  3673. /**
  3674. * test adding an empty option for radio buttons
  3675. *
  3676. * @return void
  3677. */
  3678. public function testRadioAddEmptyOption() {
  3679. $result = $this->Form->input('Model.1.field', array(
  3680. 'type' => 'radio',
  3681. 'options' => array('option A'),
  3682. 'empty' => true,
  3683. 'hiddenField' => false
  3684. ));
  3685. $expected = array(
  3686. 'div' => array('class' => 'input radio'),
  3687. 'fieldset' => array(),
  3688. 'legend' => array(),
  3689. 'Field',
  3690. '/legend',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3692. array('label' => array('for' => 'Model1Field')),
  3693. __('empty'),
  3694. '/label',
  3695. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3696. array('label' => array('for' => 'Model1Field0')),
  3697. 'option A',
  3698. '/label',
  3699. '/fieldset',
  3700. '/div'
  3701. );
  3702. $this->assertTags($result, $expected);
  3703. $result = $this->Form->input('Model.1.field', array(
  3704. 'type' => 'radio',
  3705. 'options' => array('option A'),
  3706. 'empty' => 'CustomEmptyLabel',
  3707. 'hiddenField' => false
  3708. ));
  3709. $expected = array(
  3710. 'div' => array('class' => 'input radio'),
  3711. 'fieldset' => array(),
  3712. 'legend' => array(),
  3713. 'Field',
  3714. '/legend',
  3715. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3716. array('label' => array('for' => 'Model1Field')),
  3717. 'CustomEmptyLabel',
  3718. '/label',
  3719. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3720. array('label' => array('for' => 'Model1Field0')),
  3721. 'option A',
  3722. '/label',
  3723. '/fieldset',
  3724. '/div'
  3725. );
  3726. $this->assertTags($result, $expected);
  3727. $result = $this->Form->input('Model.1.field', array(
  3728. 'type' => 'radio',
  3729. 'options' => array('option A'),
  3730. 'empty' => false,
  3731. 'hiddenField' => false
  3732. ));
  3733. $this->assertTextNotContains('"Model1Field"', $result);
  3734. }
  3735. /**
  3736. * Test that radio() accepts an array for label
  3737. *
  3738. * @return void
  3739. */
  3740. public function testRadioLabelArray() {
  3741. $result = $this->Form->input('Model.field', array(
  3742. 'type' => 'radio',
  3743. 'legend' => false,
  3744. 'label' => array(
  3745. 'class' => 'checkbox float-left',
  3746. ),
  3747. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3748. ));
  3749. $this->assertTextContains(
  3750. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3751. $result
  3752. );
  3753. }
  3754. /**
  3755. * Test that label id's match the input element id's when radio is called after create().
  3756. *
  3757. * @return void
  3758. */
  3759. public function testRadioWithCreate() {
  3760. $this->Form->create('Model');
  3761. $result = $this->Form->radio('recipient',
  3762. array('1' => '1', '2' => '2', '3' => '3'),
  3763. array('legend' => false, 'value' => '1')
  3764. );
  3765. $this->assertTextNotContains(
  3766. '<label for="ModelModelRecipient1">1</label>',
  3767. $result
  3768. );
  3769. $this->assertTextContains(
  3770. '<label for="ModelRecipient1">1</label>',
  3771. $result
  3772. );
  3773. }
  3774. /**
  3775. * testDomIdSuffix method
  3776. *
  3777. * @return void
  3778. */
  3779. public function testDomIdSuffix() {
  3780. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3781. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3782. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3783. $this->assertEquals('AbcX=FooY=Bar', $result);
  3784. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3785. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3786. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3787. $this->assertEquals('AbcXFooYBar', $result);
  3788. }
  3789. /**
  3790. * testDomIdSuffixCollisionResolvement()
  3791. *
  3792. * @return void
  3793. */
  3794. public function testDomIdSuffixCollisionResolvement() {
  3795. $result = $this->Form->domIdSuffix('a>b');
  3796. $this->assertEquals('AB', $result);
  3797. $result = $this->Form->domIdSuffix('a<b');
  3798. $this->assertEquals('AB1', $result);
  3799. $result = $this->Form->domIdSuffix('a\'b');
  3800. $this->assertEquals('AB2', $result);
  3801. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3802. $this->assertEquals('1StringWith1-dollar', $result);
  3803. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3804. $this->assertEquals('1StringWith1-dollar1', $result);
  3805. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3806. $this->assertEquals('1StringWith1-dollar2', $result);
  3807. }
  3808. /**
  3809. * testSelect method
  3810. *
  3811. * Test select element generation.
  3812. *
  3813. * @return void
  3814. */
  3815. public function testSelect() {
  3816. $result = $this->Form->select('Model.field', array());
  3817. $expected = array(
  3818. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3819. array('option' => array('value' => '')),
  3820. '/option',
  3821. '/select'
  3822. );
  3823. $this->assertTags($result, $expected);
  3824. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3825. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3826. $expected = array(
  3827. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3828. array('option' => array('value' => '')),
  3829. '/option',
  3830. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3831. 'good',
  3832. '/option',
  3833. array('option' => array('value' => 'other')),
  3834. 'bad',
  3835. '/option',
  3836. '/select'
  3837. );
  3838. $this->assertTags($result, $expected);
  3839. $this->Form->request->data = array();
  3840. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3841. $expected = array(
  3842. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3843. array('option' => array('value' => '')),
  3844. '/option',
  3845. array('option' => array('value' => 'value')),
  3846. 'good',
  3847. '/option',
  3848. array('option' => array('value' => 'other')),
  3849. 'bad',
  3850. '/option',
  3851. '/select'
  3852. );
  3853. $this->assertTags($result, $expected);
  3854. $result = $this->Form->select(
  3855. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3856. array('empty' => false)
  3857. );
  3858. $expected = array(
  3859. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3860. array('option' => array('value' => 'first')),
  3861. 'first &quot;html&quot; &lt;chars&gt;',
  3862. '/option',
  3863. array('option' => array('value' => 'second')),
  3864. 'value',
  3865. '/option',
  3866. '/select'
  3867. );
  3868. $this->assertTags($result, $expected);
  3869. $result = $this->Form->select(
  3870. 'Model.field',
  3871. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3872. array('escape' => false, 'empty' => false)
  3873. );
  3874. $expected = array(
  3875. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3876. array('option' => array('value' => 'first')),
  3877. 'first "html" <chars>',
  3878. '/option',
  3879. array('option' => array('value' => 'second')),
  3880. 'value',
  3881. '/option',
  3882. '/select'
  3883. );
  3884. $this->assertTags($result, $expected);
  3885. $options = array(
  3886. array('value' => 'first', 'name' => 'First'),
  3887. array('value' => 'first', 'name' => 'Another First'),
  3888. );
  3889. $result = $this->Form->select(
  3890. 'Model.field',
  3891. $options,
  3892. array('escape' => false, 'empty' => false)
  3893. );
  3894. $expected = array(
  3895. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3896. array('option' => array('value' => 'first')),
  3897. 'First',
  3898. '/option',
  3899. array('option' => array('value' => 'first')),
  3900. 'Another First',
  3901. '/option',
  3902. '/select'
  3903. );
  3904. $this->assertTags($result, $expected);
  3905. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3906. $result = $this->Form->select(
  3907. 'Model.contact_id',
  3908. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3909. array('escape' => false, 'empty' => 'pick something')
  3910. );
  3911. $expected = array(
  3912. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3913. array('option' => array('value' => '')), 'pick something', '/option',
  3914. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3915. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3916. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3917. '/select'
  3918. );
  3919. $this->assertTags($result, $expected);
  3920. $this->Form->request->data['Model']['field'] = 0;
  3921. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3922. $expected = array(
  3923. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3924. array('option' => array('value' => '')), '/option',
  3925. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3926. array('option' => array('value' => '1')), 'Yes', '/option',
  3927. '/select'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $this->Form->request->data['Model']['field'] = 50;
  3931. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3932. $expected = array(
  3933. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3934. array('option' => array('value' => '')), '/option',
  3935. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3936. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3937. '/select'
  3938. );
  3939. $this->assertTags($result, $expected);
  3940. $result = $this->Form->select('Contact.required_one', array('option A'));
  3941. $expected = array(
  3942. 'select' => array(
  3943. 'name' => 'data[Contact][required_one]',
  3944. 'id' => 'ContactRequiredOne',
  3945. 'required' => 'required'
  3946. ),
  3947. array('option' => array('value' => '')), '/option',
  3948. array('option' => array('value' => '0')), 'option A', '/option',
  3949. '/select'
  3950. );
  3951. $this->assertTags($result, $expected);
  3952. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3953. $expected = array(
  3954. 'select' => array(
  3955. 'name' => 'data[Contact][required_one]',
  3956. 'id' => 'ContactRequiredOne',
  3957. 'disabled' => 'disabled'
  3958. ),
  3959. array('option' => array('value' => '')), '/option',
  3960. array('option' => array('value' => '0')), 'option A', '/option',
  3961. '/select'
  3962. );
  3963. $this->assertTags($result, $expected);
  3964. }
  3965. /**
  3966. * test that select() with optiongroups listens to the escape param.
  3967. *
  3968. * @return void
  3969. */
  3970. public function testSelectOptionGroupEscaping() {
  3971. $options = array(
  3972. '>< Key' => array(
  3973. 1 => 'One',
  3974. 2 => 'Two'
  3975. )
  3976. );
  3977. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3978. $expected = array(
  3979. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3980. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3981. array('option' => array('value' => '1')), 'One', '/option',
  3982. array('option' => array('value' => '2')), 'Two', '/option',
  3983. '/optgroup',
  3984. '/select'
  3985. );
  3986. $this->assertTags($result, $expected);
  3987. $options = array(
  3988. '>< Key' => array(
  3989. 1 => 'One',
  3990. 2 => 'Two'
  3991. )
  3992. );
  3993. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3994. $expected = array(
  3995. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3996. 'optgroup' => array('label' => '>< Key'),
  3997. array('option' => array('value' => '1')), 'One', '/option',
  3998. array('option' => array('value' => '2')), 'Two', '/option',
  3999. '/optgroup',
  4000. '/select'
  4001. );
  4002. $this->assertTags($result, $expected);
  4003. }
  4004. /**
  4005. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4006. *
  4007. * @return void
  4008. */
  4009. public function testSelectWithNullAttributes() {
  4010. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4011. $expected = array(
  4012. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4013. array('option' => array('value' => '0')),
  4014. 'first',
  4015. '/option',
  4016. array('option' => array('value' => '1')),
  4017. 'second',
  4018. '/option',
  4019. '/select'
  4020. );
  4021. $this->assertTags($result, $expected);
  4022. }
  4023. /**
  4024. * testNestedSelect method
  4025. *
  4026. * test select element generation with optgroups
  4027. *
  4028. * @return void
  4029. */
  4030. public function testNestedSelect() {
  4031. $result = $this->Form->select(
  4032. 'Model.field',
  4033. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4034. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4035. )), array('empty' => false)
  4036. );
  4037. $expected = array(
  4038. 'select' => array('name' => 'data[Model][field]',
  4039. 'id' => 'ModelField'),
  4040. array('option' => array('value' => 1)),
  4041. 'One',
  4042. '/option',
  4043. array('option' => array('value' => 2)),
  4044. 'Two',
  4045. '/option',
  4046. array('optgroup' => array('label' => 'Three')),
  4047. array('option' => array('value' => 4)),
  4048. 'Four',
  4049. '/option',
  4050. array('option' => array('value' => 5)),
  4051. 'Five',
  4052. '/option',
  4053. '/optgroup',
  4054. '/select'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->select(
  4058. 'Model.field',
  4059. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4060. array('showParents' => true, 'empty' => false)
  4061. );
  4062. $expected = array(
  4063. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4064. array('option' => array('value' => 1)),
  4065. 'One',
  4066. '/option',
  4067. array('option' => array('value' => 2)),
  4068. 'Two',
  4069. '/option',
  4070. array('optgroup' => array('label' => 'Three')),
  4071. array('option' => array('value' => 3)),
  4072. 'Three',
  4073. '/option',
  4074. array('option' => array('value' => 4)),
  4075. 'Four',
  4076. '/option',
  4077. '/optgroup',
  4078. '/select'
  4079. );
  4080. $this->assertTags($result, $expected);
  4081. }
  4082. /**
  4083. * testSelectMultiple method
  4084. *
  4085. * test generation of multiple select elements
  4086. *
  4087. * @return void
  4088. */
  4089. public function testSelectMultiple() {
  4090. $options = array('first', 'second', 'third');
  4091. $result = $this->Form->select(
  4092. 'Model.multi_field', $options, array('multiple' => true)
  4093. );
  4094. $expected = array(
  4095. 'input' => array(
  4096. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4097. ),
  4098. 'select' => array(
  4099. 'name' => 'data[Model][multi_field][]',
  4100. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4101. ),
  4102. array('option' => array('value' => '0')),
  4103. 'first',
  4104. '/option',
  4105. array('option' => array('value' => '1')),
  4106. 'second',
  4107. '/option',
  4108. array('option' => array('value' => '2')),
  4109. 'third',
  4110. '/option',
  4111. '/select'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. $result = $this->Form->select(
  4115. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4116. );
  4117. $expected = array(
  4118. 'input' => array(
  4119. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4120. ),
  4121. 'select' => array(
  4122. 'name' => 'data[Model][multi_field][]',
  4123. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4124. ),
  4125. array('option' => array('value' => '0')),
  4126. 'first',
  4127. '/option',
  4128. array('option' => array('value' => '1')),
  4129. 'second',
  4130. '/option',
  4131. array('option' => array('value' => '2')),
  4132. 'third',
  4133. '/option',
  4134. '/select'
  4135. );
  4136. $this->assertTags($result, $expected);
  4137. $result = $this->Form->select(
  4138. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4139. );
  4140. $expected = array(
  4141. 'input' => array(
  4142. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4143. ),
  4144. 'select' => array(
  4145. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4146. 'multiple' => 'multiple'
  4147. ),
  4148. array('option' => array('value' => '0', 'selected' => 'selected')),
  4149. 'first',
  4150. '/option',
  4151. array('option' => array('value' => '1', 'selected' => 'selected')),
  4152. 'second',
  4153. '/option',
  4154. array('option' => array('value' => '2')),
  4155. 'third',
  4156. '/option',
  4157. '/select'
  4158. );
  4159. $this->assertTags($result, $expected);
  4160. $result = $this->Form->select(
  4161. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4162. );
  4163. $expected = array(
  4164. 'select' => array(
  4165. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4166. ),
  4167. array('option' => array('value' => '0', 'selected' => 'selected')),
  4168. 'first',
  4169. '/option',
  4170. array('option' => array('value' => '1', 'selected' => 'selected')),
  4171. 'second',
  4172. '/option',
  4173. array('option' => array('value' => '2')),
  4174. 'third',
  4175. '/option',
  4176. '/select'
  4177. );
  4178. $this->assertTags($result, $expected);
  4179. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4180. $selected = array('2', '3x');
  4181. $result = $this->Form->select(
  4182. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4183. );
  4184. $expected = array(
  4185. 'input' => array(
  4186. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4187. ),
  4188. 'select' => array(
  4189. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4190. ),
  4191. array('option' => array('value' => '1')),
  4192. 'One',
  4193. '/option',
  4194. array('option' => array('value' => '2', 'selected' => 'selected')),
  4195. 'Two',
  4196. '/option',
  4197. array('option' => array('value' => '3')),
  4198. 'Three',
  4199. '/option',
  4200. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4201. 'Stringy',
  4202. '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $result = $this->Form->select('Contact.required_one', array(
  4207. '1' => 'option A',
  4208. '2' => 'option B'
  4209. ), array('multiple' => true));
  4210. $expected = array(
  4211. 'input' => array(
  4212. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4213. ),
  4214. 'select' => array(
  4215. 'name' => 'data[Contact][required_one][]',
  4216. 'id' => 'ContactRequiredOne',
  4217. 'required' => 'required',
  4218. 'multiple' => 'multiple'
  4219. ),
  4220. array('option' => array('value' => '1')), 'option A', '/option',
  4221. array('option' => array('value' => '2')), 'option B', '/option',
  4222. '/select'
  4223. );
  4224. $this->assertTags($result, $expected);
  4225. }
  4226. /**
  4227. * Test generating multiple select with disabled elements.
  4228. *
  4229. * @return void
  4230. */
  4231. public function testSelectMultipleWithDisabledElements() {
  4232. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4233. $disabled = array(2, 3);
  4234. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4235. $expected = array(
  4236. 'input' => array(
  4237. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4238. ),
  4239. 'select' => array(
  4240. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4241. ),
  4242. array('option' => array('value' => '1')),
  4243. 'One',
  4244. '/option',
  4245. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4246. 'Two',
  4247. '/option',
  4248. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4249. 'Three',
  4250. '/option',
  4251. array('option' => array('value' => '3x')),
  4252. 'Stringy',
  4253. '/option',
  4254. '/select'
  4255. );
  4256. $this->assertTags($result, $expected);
  4257. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4258. $disabled = array('2', '3x');
  4259. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4260. $expected = array(
  4261. array('div' => array('class' => 'input select')),
  4262. array('label' => array('for' => 'ContactMultiple')),
  4263. 'Multiple',
  4264. '/label',
  4265. 'input' => array(
  4266. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4267. ),
  4268. 'select' => array(
  4269. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4270. ),
  4271. array('option' => array('value' => '1')),
  4272. 'One',
  4273. '/option',
  4274. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4275. 'Two',
  4276. '/option',
  4277. array('option' => array('value' => '3')),
  4278. 'Three',
  4279. '/option',
  4280. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4281. 'Stringy',
  4282. '/option',
  4283. '/select',
  4284. '/div'
  4285. );
  4286. $this->assertTags($result, $expected);
  4287. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4288. $disabled = true;
  4289. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4290. $expected = array(
  4291. array('div' => array('class' => 'input select')),
  4292. array('label' => array('for' => 'ContactMultiple')),
  4293. 'Multiple',
  4294. '/label',
  4295. 'input' => array(
  4296. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4297. ),
  4298. 'select' => array(
  4299. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4300. ),
  4301. array('option' => array('value' => '1')),
  4302. 'One',
  4303. '/option',
  4304. array('option' => array('value' => '2')),
  4305. 'Two',
  4306. '/option',
  4307. array('option' => array('value' => '3')),
  4308. 'Three',
  4309. '/option',
  4310. array('option' => array('value' => '3x')),
  4311. 'Stringy',
  4312. '/option',
  4313. '/select',
  4314. '/div'
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. }
  4318. /**
  4319. * Test generating select with disabled elements.
  4320. *
  4321. * @return void
  4322. */
  4323. public function testSelectWithDisabledElements() {
  4324. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4325. $disabled = array(2, 3);
  4326. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4327. $expected = array(
  4328. 'select' => array(
  4329. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4330. ),
  4331. array('option' => array('value' => '')),
  4332. '/option',
  4333. array('option' => array('value' => '1')),
  4334. 'One',
  4335. '/option',
  4336. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4337. 'Two',
  4338. '/option',
  4339. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4340. 'Three',
  4341. '/option',
  4342. array('option' => array('value' => '3x')),
  4343. 'Stringy',
  4344. '/option',
  4345. '/select'
  4346. );
  4347. $this->assertTags($result, $expected);
  4348. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4349. $disabled = array('2', '3x');
  4350. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4351. $expected = array(
  4352. array('div' => array('class' => 'input select')),
  4353. array('label' => array('for' => 'ModelField')),
  4354. 'Field',
  4355. '/label',
  4356. 'select' => array(
  4357. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4358. ),
  4359. array('option' => array('value' => '1')),
  4360. 'One',
  4361. '/option',
  4362. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4363. 'Two',
  4364. '/option',
  4365. array('option' => array('value' => '3')),
  4366. 'Three',
  4367. '/option',
  4368. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4369. 'Stringy',
  4370. '/option',
  4371. '/select',
  4372. '/div'
  4373. );
  4374. $this->assertTags($result, $expected);
  4375. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4376. $disabled = true;
  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', 'disabled' => 'disabled'
  4385. ),
  4386. array('option' => array('value' => '1')),
  4387. 'One',
  4388. '/option',
  4389. array('option' => array('value' => '2')),
  4390. 'Two',
  4391. '/option',
  4392. array('option' => array('value' => '3')),
  4393. 'Three',
  4394. '/option',
  4395. array('option' => array('value' => '3x')),
  4396. 'Stringy',
  4397. '/option',
  4398. '/select',
  4399. '/div'
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. }
  4403. /**
  4404. * test generation of habtm select boxes.
  4405. *
  4406. * @return void
  4407. */
  4408. public function testHabtmSelectBox() {
  4409. $this->View->viewVars['contactTags'] = array(
  4410. 1 => 'blue',
  4411. 2 => 'red',
  4412. 3 => 'green'
  4413. );
  4414. $this->Form->request->data = array(
  4415. 'Contact' => array(),
  4416. 'ContactTag' => array(
  4417. array(
  4418. 'id' => '1',
  4419. 'name' => 'blue'
  4420. ),
  4421. array(
  4422. 'id' => 3,
  4423. 'name' => 'green'
  4424. )
  4425. )
  4426. );
  4427. $this->Form->create('Contact');
  4428. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4429. $expected = array(
  4430. 'input' => array(
  4431. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4432. ),
  4433. 'select' => array(
  4434. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4435. 'multiple' => 'multiple'
  4436. ),
  4437. array('option' => array('value' => '1', 'selected' => 'selected')),
  4438. 'blue',
  4439. '/option',
  4440. array('option' => array('value' => '2')),
  4441. 'red',
  4442. '/option',
  4443. array('option' => array('value' => '3', 'selected' => 'selected')),
  4444. 'green',
  4445. '/option',
  4446. '/select'
  4447. );
  4448. $this->assertTags($result, $expected);
  4449. // make sure only 50 is selected, and not 50f5c0cf
  4450. $this->View->viewVars['contactTags'] = array(
  4451. '1' => 'blue',
  4452. '50f5c0cf' => 'red',
  4453. '50' => 'green'
  4454. );
  4455. $this->Form->request->data = array(
  4456. 'Contact' => array(),
  4457. 'ContactTag' => array(
  4458. array(
  4459. 'id' => 1,
  4460. 'name' => 'blue'
  4461. ),
  4462. array(
  4463. 'id' => 50,
  4464. 'name' => 'green'
  4465. )
  4466. )
  4467. );
  4468. $this->Form->create('Contact');
  4469. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4470. $expected = array(
  4471. 'input' => array(
  4472. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4473. ),
  4474. 'select' => array(
  4475. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4476. 'multiple' => 'multiple'
  4477. ),
  4478. array('option' => array('value' => '1', 'selected' => 'selected')),
  4479. 'blue',
  4480. '/option',
  4481. array('option' => array('value' => '50f5c0cf')),
  4482. 'red',
  4483. '/option',
  4484. array('option' => array('value' => '50', 'selected' => 'selected')),
  4485. 'green',
  4486. '/option',
  4487. '/select'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. }
  4491. /**
  4492. * test generation of multi select elements in checkbox format
  4493. *
  4494. * @return void
  4495. */
  4496. public function testSelectMultipleCheckboxes() {
  4497. $result = $this->Form->select(
  4498. 'Model.multi_field',
  4499. array('first', 'second', 'third'),
  4500. array('multiple' => 'checkbox')
  4501. );
  4502. $expected = array(
  4503. 'input' => array(
  4504. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4505. ),
  4506. array('div' => array('class' => 'checkbox')),
  4507. array('input' => array(
  4508. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4509. 'value' => '0', 'id' => 'ModelMultiField0'
  4510. )),
  4511. array('label' => array('for' => 'ModelMultiField0')),
  4512. 'first',
  4513. '/label',
  4514. '/div',
  4515. array('div' => array('class' => 'checkbox')),
  4516. array('input' => array(
  4517. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4518. 'value' => '1', 'id' => 'ModelMultiField1'
  4519. )),
  4520. array('label' => array('for' => 'ModelMultiField1')),
  4521. 'second',
  4522. '/label',
  4523. '/div',
  4524. array('div' => array('class' => 'checkbox')),
  4525. array('input' => array(
  4526. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4527. 'value' => '2', 'id' => 'ModelMultiField2'
  4528. )),
  4529. array('label' => array('for' => 'ModelMultiField2')),
  4530. 'third',
  4531. '/label',
  4532. '/div'
  4533. );
  4534. $this->assertTags($result, $expected);
  4535. $result = $this->Form->select(
  4536. 'Model.multi_field',
  4537. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4538. array('multiple' => 'checkbox')
  4539. );
  4540. $expected = array(
  4541. 'input' => array(
  4542. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4543. ),
  4544. array('div' => array('class' => 'checkbox')),
  4545. array('input' => array(
  4546. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4547. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4548. )),
  4549. array('label' => array('for' => 'ModelMultiFieldA')),
  4550. 'first',
  4551. '/label',
  4552. '/div',
  4553. array('div' => array('class' => 'checkbox')),
  4554. array('input' => array(
  4555. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4556. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4557. )),
  4558. array('label' => array('for' => 'ModelMultiFieldB')),
  4559. 'second',
  4560. '/label',
  4561. '/div',
  4562. array('div' => array('class' => 'checkbox')),
  4563. array('input' => array(
  4564. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4565. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4566. )),
  4567. array('label' => array('for' => 'ModelMultiFieldC')),
  4568. 'third',
  4569. '/label',
  4570. '/div'
  4571. );
  4572. $this->assertTags($result, $expected);
  4573. $result = $this->Form->select(
  4574. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4575. );
  4576. $expected = array(
  4577. 'input' => array(
  4578. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4579. ),
  4580. array('div' => array('class' => 'checkbox')),
  4581. array('input' => array(
  4582. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4583. 'value' => '1', 'id' => 'ModelMultiField1'
  4584. )),
  4585. array('label' => array('for' => 'ModelMultiField1')),
  4586. 'first',
  4587. '/label',
  4588. '/div'
  4589. );
  4590. $this->assertTags($result, $expected);
  4591. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4592. $result = $this->Form->select(
  4593. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4594. );
  4595. $expected = array(
  4596. 'input' => array(
  4597. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4598. ),
  4599. array('div' => array('class' => 'checkbox')),
  4600. array('input' => array(
  4601. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4602. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4603. )),
  4604. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4605. 'first',
  4606. '/label',
  4607. '/div',
  4608. array('div' => array('class' => 'checkbox')),
  4609. array('input' => array(
  4610. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4611. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4612. )),
  4613. array('label' => array('for' => 'ModelTagsArray')),
  4614. 'Array',
  4615. '/label',
  4616. '/div'
  4617. );
  4618. $this->assertTags($result, $expected);
  4619. $result = $this->Form->select(
  4620. 'Model.multi_field',
  4621. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4622. array('multiple' => 'checkbox')
  4623. );
  4624. $expected = array(
  4625. 'input' => array(
  4626. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4627. ),
  4628. array('div' => array('class' => 'checkbox')),
  4629. array('input' => array(
  4630. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4631. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4632. )),
  4633. array('label' => array('for' => 'ModelMultiFieldA+')),
  4634. 'first',
  4635. '/label',
  4636. '/div',
  4637. array('div' => array('class' => 'checkbox')),
  4638. array('input' => array(
  4639. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4640. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4641. )),
  4642. array('label' => array('for' => 'ModelMultiFieldA++')),
  4643. 'second',
  4644. '/label',
  4645. '/div',
  4646. array('div' => array('class' => 'checkbox')),
  4647. array('input' => array(
  4648. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4649. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4650. )),
  4651. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4652. 'third',
  4653. '/label',
  4654. '/div'
  4655. );
  4656. $this->assertTags($result, $expected);
  4657. $result = $this->Form->select(
  4658. 'Model.multi_field',
  4659. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4660. array('multiple' => 'checkbox')
  4661. );
  4662. $expected = array(
  4663. 'input' => array(
  4664. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4665. ),
  4666. array('div' => array('class' => 'checkbox')),
  4667. array('input' => array(
  4668. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4669. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4670. )),
  4671. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4672. 'first',
  4673. '/label',
  4674. '/div',
  4675. array('div' => array('class' => 'checkbox')),
  4676. array('input' => array(
  4677. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4678. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4679. )),
  4680. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4681. 'second',
  4682. '/label',
  4683. '/div',
  4684. array('div' => array('class' => 'checkbox')),
  4685. array('input' => array(
  4686. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4687. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4688. )),
  4689. array('label' => array('for' => 'ModelMultiFieldAB')),
  4690. 'third',
  4691. '/label',
  4692. '/div'
  4693. );
  4694. $this->assertTags($result, $expected);
  4695. }
  4696. /**
  4697. * test multiple checkboxes with div styles.
  4698. *
  4699. * @return void
  4700. */
  4701. public function testSelectMultipleCheckboxDiv() {
  4702. $result = $this->Form->select(
  4703. 'Model.tags',
  4704. array('first', 'second'),
  4705. array('multiple' => 'checkbox', 'class' => 'my-class')
  4706. );
  4707. $expected = array(
  4708. 'input' => array(
  4709. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4710. ),
  4711. array('div' => array('class' => 'my-class')),
  4712. array('input' => array(
  4713. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4714. 'value' => '0', 'id' => 'ModelTags0'
  4715. )),
  4716. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4717. '/div',
  4718. array('div' => array('class' => 'my-class')),
  4719. array('input' => array(
  4720. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4721. 'value' => '1', 'id' => 'ModelTags1'
  4722. )),
  4723. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4724. '/div'
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. $result = $this->Form->input('Model.tags', array(
  4728. 'options' => array('first', 'second'),
  4729. 'multiple' => 'checkbox',
  4730. 'class' => 'my-class',
  4731. 'div' => false,
  4732. 'label' => false
  4733. ));
  4734. $this->assertTags($result, $expected);
  4735. $Contact = ClassRegistry::getObject('Contact');
  4736. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4737. $result = $this->Form->input('Contact.tags', array(
  4738. 'options' => array('one'),
  4739. 'multiple' => 'checkbox',
  4740. 'label' => false,
  4741. 'div' => false
  4742. ));
  4743. $expected = array(
  4744. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4745. array('div' => array('class' => 'checkbox form-error')),
  4746. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4747. array('label' => array('for' => 'ContactTags0')),
  4748. 'one',
  4749. '/label',
  4750. '/div'
  4751. );
  4752. $this->assertTags($result, $expected);
  4753. $result = $this->Form->input('Contact.tags', array(
  4754. 'options' => array('one'),
  4755. 'multiple' => 'checkbox',
  4756. 'class' => 'mycheckbox',
  4757. 'label' => false,
  4758. 'div' => false
  4759. ));
  4760. $expected = array(
  4761. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4762. array('div' => array('class' => 'mycheckbox form-error')),
  4763. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4764. array('label' => array('for' => 'ContactTags0')),
  4765. 'one',
  4766. '/label',
  4767. '/div'
  4768. );
  4769. $this->assertTags($result, $expected);
  4770. }
  4771. /**
  4772. * Checks the security hash array generated for multiple-input checkbox elements
  4773. *
  4774. * @return void
  4775. */
  4776. public function testSelectMultipleCheckboxSecurity() {
  4777. $this->Form->request['_Token'] = array('key' => 'testKey');
  4778. $this->assertEquals(array(), $this->Form->fields);
  4779. $result = $this->Form->select(
  4780. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4781. array('multiple' => 'checkbox')
  4782. );
  4783. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4784. $result = $this->Form->secure($this->Form->fields);
  4785. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4786. $this->assertRegExp('/"' . $key . '"/', $result);
  4787. }
  4788. /**
  4789. * Multiple select elements should always be secured as they always participate
  4790. * in the POST data.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testSelectMultipleSecureWithNoOptions() {
  4795. $this->Form->request['_Token'] = array('key' => 'testkey');
  4796. $this->assertEquals(array(), $this->Form->fields);
  4797. $this->Form->select(
  4798. 'Model.select',
  4799. array(),
  4800. array('multiple' => true)
  4801. );
  4802. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4803. }
  4804. /**
  4805. * When a select box has no options it should not be added to the fields list
  4806. * as it always fail post validation.
  4807. *
  4808. * @return void
  4809. */
  4810. public function testSelectNoSecureWithNoOptions() {
  4811. $this->Form->request['_Token'] = array('key' => 'testkey');
  4812. $this->assertEquals(array(), $this->Form->fields);
  4813. $this->Form->select(
  4814. 'Model.select',
  4815. array()
  4816. );
  4817. $this->assertEquals(array(), $this->Form->fields);
  4818. $this->Form->select(
  4819. 'Model.select',
  4820. array(),
  4821. array('empty' => true)
  4822. );
  4823. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4824. }
  4825. /**
  4826. * testInputMultipleCheckboxes method
  4827. *
  4828. * test input() resulting in multi select elements being generated.
  4829. *
  4830. * @return void
  4831. */
  4832. public function testInputMultipleCheckboxes() {
  4833. $result = $this->Form->input('Model.multi_field', array(
  4834. 'options' => array('first', 'second', 'third'),
  4835. 'multiple' => 'checkbox'
  4836. ));
  4837. $expected = array(
  4838. array('div' => array('class' => 'input select')),
  4839. array('label' => array('for' => 'ModelMultiField')),
  4840. 'Multi Field',
  4841. '/label',
  4842. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4843. array('div' => array('class' => 'checkbox')),
  4844. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4845. array('label' => array('for' => 'ModelMultiField0')),
  4846. 'first',
  4847. '/label',
  4848. '/div',
  4849. array('div' => array('class' => 'checkbox')),
  4850. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4851. array('label' => array('for' => 'ModelMultiField1')),
  4852. 'second',
  4853. '/label',
  4854. '/div',
  4855. array('div' => array('class' => 'checkbox')),
  4856. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4857. array('label' => array('for' => 'ModelMultiField2')),
  4858. 'third',
  4859. '/label',
  4860. '/div',
  4861. '/div'
  4862. );
  4863. $this->assertTags($result, $expected);
  4864. $result = $this->Form->input('Model.multi_field', array(
  4865. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4866. 'multiple' => 'checkbox'
  4867. ));
  4868. $expected = array(
  4869. array('div' => array('class' => 'input select')),
  4870. array('label' => array('for' => 'ModelMultiField')),
  4871. 'Multi Field',
  4872. '/label',
  4873. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4874. array('div' => array('class' => 'checkbox')),
  4875. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4876. array('label' => array('for' => 'ModelMultiFieldA')),
  4877. 'first',
  4878. '/label',
  4879. '/div',
  4880. array('div' => array('class' => 'checkbox')),
  4881. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4882. array('label' => array('for' => 'ModelMultiFieldB')),
  4883. 'second',
  4884. '/label',
  4885. '/div',
  4886. array('div' => array('class' => 'checkbox')),
  4887. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4888. array('label' => array('for' => 'ModelMultiFieldC')),
  4889. 'third',
  4890. '/label',
  4891. '/div',
  4892. '/div'
  4893. );
  4894. $this->assertTags($result, $expected);
  4895. $result = $this->Form->input('Model.multi_field', array(
  4896. 'options' => array('1' => 'first'),
  4897. 'multiple' => 'checkbox',
  4898. 'label' => false,
  4899. 'div' => false
  4900. ));
  4901. $expected = array(
  4902. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4903. array('div' => array('class' => 'checkbox')),
  4904. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4905. array('label' => array('for' => 'ModelMultiField1')),
  4906. 'first',
  4907. '/label',
  4908. '/div'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. $result = $this->Form->input('Model.multi_field', array(
  4912. 'options' => array('2' => 'second'),
  4913. 'multiple' => 'checkbox',
  4914. 'label' => false,
  4915. 'div' => false
  4916. ));
  4917. $expected = array(
  4918. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4919. array('div' => array('class' => 'checkbox')),
  4920. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4921. array('label' => array('for' => 'ModelMultiField2')),
  4922. 'second',
  4923. '/label',
  4924. '/div'
  4925. );
  4926. $this->assertTags($result, $expected);
  4927. }
  4928. /**
  4929. * testSelectHiddenFieldOmission method
  4930. *
  4931. * test that select() with 'hiddenField' => false omits the hidden field
  4932. *
  4933. * @return void
  4934. */
  4935. public function testSelectHiddenFieldOmission() {
  4936. $result = $this->Form->select('Model.multi_field',
  4937. array('first', 'second'),
  4938. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4939. );
  4940. $expected = array(
  4941. array('div' => array('class' => 'checkbox')),
  4942. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4943. array('label' => array('for' => 'ModelMultiField0')),
  4944. 'first',
  4945. '/label',
  4946. '/div',
  4947. array('div' => array('class' => 'checkbox')),
  4948. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4949. array('label' => array('for' => 'ModelMultiField1')),
  4950. 'second',
  4951. '/label',
  4952. '/div'
  4953. );
  4954. $this->assertTags($result, $expected);
  4955. $result = $this->Form->input('Model.multi_field', array(
  4956. 'options' => array('first', 'second'),
  4957. 'multiple' => 'checkbox',
  4958. 'hiddenField' => false
  4959. ));
  4960. $expected = array(
  4961. array('div' => array('class' => 'input select')),
  4962. array('label' => array('for' => 'ModelMultiField')),
  4963. 'Multi Field',
  4964. '/label',
  4965. array('div' => array('class' => 'checkbox')),
  4966. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4967. array('label' => array('for' => 'ModelMultiField0')),
  4968. 'first',
  4969. '/label',
  4970. '/div',
  4971. array('div' => array('class' => 'checkbox')),
  4972. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4973. array('label' => array('for' => 'ModelMultiField1')),
  4974. 'second',
  4975. '/label',
  4976. '/div',
  4977. '/div'
  4978. );
  4979. $this->assertTags($result, $expected);
  4980. }
  4981. /**
  4982. * test that select() with multiple = checkbox works with overriding name attribute.
  4983. *
  4984. * @return void
  4985. */
  4986. public function testSelectCheckboxMultipleOverrideName() {
  4987. $result = $this->Form->input('category', array(
  4988. 'type' => 'select',
  4989. 'multiple' => 'checkbox',
  4990. 'name' => 'data[fish]',
  4991. 'options' => array('1', '2'),
  4992. 'div' => false,
  4993. 'label' => false,
  4994. ));
  4995. $expected = array(
  4996. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4997. array('div' => array('class' => 'checkbox')),
  4998. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4999. array('label' => array('for' => 'Category0')), '1', '/label',
  5000. '/div',
  5001. array('div' => array('class' => 'checkbox')),
  5002. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5003. array('label' => array('for' => 'Category1')), '2', '/label',
  5004. '/div'
  5005. );
  5006. $this->assertTags($result, $expected);
  5007. }
  5008. /**
  5009. * Test that 'id' overrides all the checkbox id's as well.
  5010. *
  5011. * @return void
  5012. */
  5013. public function testSelectCheckboxMultipleId() {
  5014. $result = $this->Form->select(
  5015. 'Model.multi_field',
  5016. array('first', 'second', 'third'),
  5017. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5018. );
  5019. $expected = array(
  5020. 'input' => array(
  5021. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5022. ),
  5023. array('div' => array('class' => 'checkbox')),
  5024. array('input' => array(
  5025. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5026. 'value' => '0', 'id' => 'CustomId0'
  5027. )),
  5028. array('label' => array('for' => 'CustomId0')),
  5029. 'first',
  5030. '/label',
  5031. '/div',
  5032. array('div' => array('class' => 'checkbox')),
  5033. array('input' => array(
  5034. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5035. 'value' => '1', 'id' => 'CustomId1'
  5036. )),
  5037. array('label' => array('for' => 'CustomId1')),
  5038. 'second',
  5039. '/label',
  5040. '/div',
  5041. array('div' => array('class' => 'checkbox')),
  5042. array('input' => array(
  5043. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5044. 'value' => '2', 'id' => 'CustomId2'
  5045. )),
  5046. array('label' => array('for' => 'CustomId2')),
  5047. 'third',
  5048. '/label',
  5049. '/div'
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. }
  5053. /**
  5054. * testCheckbox method
  5055. *
  5056. * Test generation of checkboxes
  5057. *
  5058. * @return void
  5059. */
  5060. public function testCheckbox() {
  5061. $result = $this->Form->checkbox('Model.field');
  5062. $expected = array(
  5063. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5064. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5065. );
  5066. $this->assertTags($result, $expected);
  5067. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5068. $expected = array(
  5069. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5070. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5071. );
  5072. $this->assertTags($result, $expected);
  5073. $Contact = ClassRegistry::getObject('Contact');
  5074. $Contact->validationErrors['field'] = 1;
  5075. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5076. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5077. $expected = array(
  5078. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5079. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5080. );
  5081. $this->assertTags($result, $expected);
  5082. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5083. $expected = array(
  5084. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5085. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5086. );
  5087. $this->assertTags($result, $expected);
  5088. $this->Form->request->data['Contact']['field'] = '';
  5089. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5090. $expected = array(
  5091. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5092. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. $Contact->validationErrors = array();
  5096. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5097. $expected = array(
  5098. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5099. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5100. );
  5101. $this->assertTags($result, $expected);
  5102. $this->Form->request->data['Contact']['published'] = 1;
  5103. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5104. $expected = array(
  5105. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5106. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5107. );
  5108. $this->assertTags($result, $expected);
  5109. $this->Form->request->data['Contact']['published'] = 0;
  5110. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5111. $expected = array(
  5112. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5113. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5114. );
  5115. $this->assertTags($result, $expected);
  5116. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5117. $expected = array(
  5118. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5119. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5120. );
  5121. $this->assertTags($result, $expected);
  5122. $result = $this->Form->checkbox('CustomField.1.value');
  5123. $expected = array(
  5124. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5125. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5126. );
  5127. $this->assertTags($result, $expected);
  5128. }
  5129. /**
  5130. * test checkbox() with a custom name attribute
  5131. *
  5132. * @return void
  5133. */
  5134. public function testCheckboxCustomNameAttribute() {
  5135. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5136. $expected = array(
  5137. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5138. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5139. );
  5140. $this->assertTags($result, $expected);
  5141. }
  5142. /**
  5143. * test the checked option for checkboxes.
  5144. *
  5145. * @return void
  5146. */
  5147. public function testCheckboxCheckedOption() {
  5148. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5149. $expected = array(
  5150. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5151. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5152. );
  5153. $this->assertTags($result, $expected);
  5154. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5155. $expected = array(
  5156. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5157. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5158. );
  5159. $this->assertTags($result, $expected);
  5160. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5161. $expected = array(
  5162. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5163. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5164. );
  5165. $this->assertTags($result, $expected);
  5166. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5167. $expected = array(
  5168. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5169. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5170. );
  5171. $this->assertTags($result, $expected);
  5172. $this->Form->request->data['Model']['field'] = 1;
  5173. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5174. $expected = array(
  5175. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5176. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5177. );
  5178. $this->assertTags($result, $expected);
  5179. }
  5180. /**
  5181. * Test that disabled attribute works on both the checkbox and hidden input.
  5182. *
  5183. * @return void
  5184. */
  5185. public function testCheckboxDisabling() {
  5186. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5187. $expected = array(
  5188. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5189. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5193. $expected = array(
  5194. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5195. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5196. );
  5197. $this->assertTags($result, $expected);
  5198. }
  5199. /**
  5200. * Test that the hidden input for checkboxes can be omitted or set to a
  5201. * specific value.
  5202. *
  5203. * @return void
  5204. */
  5205. public function testCheckboxHiddenField() {
  5206. $result = $this->Form->input('UserForm.something', array(
  5207. 'type' => 'checkbox',
  5208. 'hiddenField' => false
  5209. ));
  5210. $expected = array(
  5211. 'div' => array('class' => 'input checkbox'),
  5212. array('input' => array(
  5213. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5214. 'value' => '1', 'id' => 'UserFormSomething'
  5215. )),
  5216. 'label' => array('for' => 'UserFormSomething'),
  5217. 'Something',
  5218. '/label',
  5219. '/div'
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $result = $this->Form->input('UserForm.something', array(
  5223. 'type' => 'checkbox',
  5224. 'value' => 'Y',
  5225. 'hiddenField' => 'N',
  5226. ));
  5227. $expected = array(
  5228. 'div' => array('class' => 'input checkbox'),
  5229. array('input' => array(
  5230. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5231. 'value' => 'N', 'id' => 'UserFormSomething_'
  5232. )),
  5233. array('input' => array(
  5234. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5235. 'value' => 'Y', 'id' => 'UserFormSomething'
  5236. )),
  5237. 'label' => array('for' => 'UserFormSomething'),
  5238. 'Something',
  5239. '/label',
  5240. '/div'
  5241. );
  5242. $this->assertTags($result, $expected);
  5243. }
  5244. /**
  5245. * testDateTime method
  5246. *
  5247. * Test generation of date/time select elements
  5248. *
  5249. * @return void
  5250. */
  5251. public function testDateTime() {
  5252. extract($this->dateRegex);
  5253. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5254. $now = strtotime('now');
  5255. $expected = array(
  5256. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5257. $daysRegex,
  5258. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5259. date('j', $now),
  5260. '/option',
  5261. '*/select',
  5262. '-',
  5263. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5264. $monthsRegex,
  5265. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5266. date('F', $now),
  5267. '/option',
  5268. '*/select',
  5269. '-',
  5270. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5271. $yearsRegex,
  5272. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5273. date('Y', $now),
  5274. '/option',
  5275. '*/select',
  5276. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5277. $hoursRegex,
  5278. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5279. date('g', $now),
  5280. '/option',
  5281. '*/select',
  5282. ':',
  5283. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5284. $minutesRegex,
  5285. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5286. date('i', $now),
  5287. '/option',
  5288. '*/select',
  5289. ' ',
  5290. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5291. $meridianRegex,
  5292. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5293. date('a', $now),
  5294. '/option',
  5295. '*/select'
  5296. );
  5297. $this->assertTags($result, $expected);
  5298. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5299. $expected = array(
  5300. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5301. $daysRegex,
  5302. array('option' => array('value' => '')),
  5303. '/option',
  5304. '*/select',
  5305. '-',
  5306. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5307. $monthsRegex,
  5308. array('option' => array('value' => '')),
  5309. '/option',
  5310. '*/select',
  5311. '-',
  5312. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5313. $yearsRegex,
  5314. array('option' => array('value' => '')),
  5315. '/option',
  5316. '*/select',
  5317. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5318. $hoursRegex,
  5319. array('option' => array('value' => '')),
  5320. '/option',
  5321. '*/select',
  5322. ':',
  5323. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5324. $minutesRegex,
  5325. array('option' => array('value' => '')),
  5326. '/option',
  5327. '*/select',
  5328. ' ',
  5329. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5330. $meridianRegex,
  5331. array('option' => array('value' => '')),
  5332. '/option',
  5333. '*/select'
  5334. );
  5335. $this->assertTags($result, $expected);
  5336. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5337. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5338. $this->assertTags($result, $expected);
  5339. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5340. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5341. $this->assertTags($result, $expected);
  5342. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5343. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5344. $expected = array(
  5345. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5346. $daysRegex,
  5347. array('option' => array('value' => '')),
  5348. '/option',
  5349. '*/select',
  5350. '-',
  5351. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5352. $monthsRegex,
  5353. array('option' => array('value' => '')),
  5354. '/option',
  5355. '*/select',
  5356. '-',
  5357. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5358. $yearsRegex,
  5359. array('option' => array('value' => '')),
  5360. '/option',
  5361. '*/select',
  5362. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5363. $hoursRegex,
  5364. array('option' => array('value' => '')),
  5365. '/option',
  5366. '*/select',
  5367. ':',
  5368. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5369. $minutesRegex,
  5370. array('option' => array('value' => '')),
  5371. '/option',
  5372. array('option' => array('value' => '00')),
  5373. '00',
  5374. '/option',
  5375. array('option' => array('value' => '05')),
  5376. '05',
  5377. '/option',
  5378. array('option' => array('value' => '10')),
  5379. '10',
  5380. '/option',
  5381. '*/select',
  5382. ' ',
  5383. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5384. $meridianRegex,
  5385. array('option' => array('value' => '')),
  5386. '/option',
  5387. '*/select'
  5388. );
  5389. $this->assertTags($result, $expected);
  5390. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5391. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5392. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5393. $this->Form->request->data['Contact']['data'] = null;
  5394. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5395. $expected = array(
  5396. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5397. $daysRegex,
  5398. array('option' => array('value' => '')),
  5399. '/option',
  5400. '*/select',
  5401. '-',
  5402. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5403. $monthsRegex,
  5404. array('option' => array('value' => '')),
  5405. '/option',
  5406. '*/select',
  5407. '-',
  5408. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5409. $yearsRegex,
  5410. array('option' => array('value' => '')),
  5411. '/option',
  5412. '*/select',
  5413. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5414. $hoursRegex,
  5415. array('option' => array('value' => '')),
  5416. '/option',
  5417. '*/select',
  5418. ':',
  5419. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5420. $minutesRegex,
  5421. array('option' => array('value' => '')),
  5422. '/option',
  5423. '*/select',
  5424. ' ',
  5425. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5426. $meridianRegex,
  5427. array('option' => array('value' => '')),
  5428. '/option',
  5429. '*/select'
  5430. );
  5431. $this->assertTags($result, $expected);
  5432. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5433. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5434. $now = strtotime($this->Form->data['Model']['field']);
  5435. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5436. $expected = array(
  5437. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5438. $daysRegex,
  5439. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5440. date('j', $now),
  5441. '/option',
  5442. '*/select',
  5443. '-',
  5444. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5445. $monthsRegex,
  5446. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5447. date('F', $now),
  5448. '/option',
  5449. '*/select',
  5450. '-',
  5451. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5452. $yearsRegex,
  5453. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5454. date('Y', $now),
  5455. '/option',
  5456. '*/select',
  5457. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5458. $hoursRegex,
  5459. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5460. date('g', $now),
  5461. '/option',
  5462. '*/select',
  5463. ':',
  5464. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5465. $minutesRegex,
  5466. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5467. date('i', $now),
  5468. '/option',
  5469. '*/select',
  5470. ' ',
  5471. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5472. $meridianRegex,
  5473. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5474. date('a', $now),
  5475. '/option',
  5476. '*/select'
  5477. );
  5478. $this->assertTags($result, $expected);
  5479. $selected = strtotime('2008-10-26 12:33:00');
  5480. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5481. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5482. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5483. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5484. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5485. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5486. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5487. $this->Form->create('Contact');
  5488. $result = $this->Form->input('published');
  5489. $now = strtotime('now');
  5490. $expected = array(
  5491. 'div' => array('class' => 'input date'),
  5492. 'label' => array('for' => 'ContactPublishedMonth'),
  5493. 'Published',
  5494. '/label',
  5495. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5496. $monthsRegex,
  5497. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5498. date('F', $now),
  5499. '/option',
  5500. '*/select',
  5501. '-',
  5502. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5503. $daysRegex,
  5504. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5505. date('j', $now),
  5506. '/option',
  5507. '*/select',
  5508. '-',
  5509. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5510. $yearsRegex,
  5511. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5512. date('Y', $now),
  5513. '/option',
  5514. '*/select',
  5515. '/div'
  5516. );
  5517. $this->assertTags($result, $expected);
  5518. $result = $this->Form->input('published2', array('type' => 'date'));
  5519. $now = strtotime('now');
  5520. $expected = array(
  5521. 'div' => array('class' => 'input date'),
  5522. 'label' => array('for' => 'ContactPublished2Month'),
  5523. 'Published2',
  5524. '/label',
  5525. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5526. $monthsRegex,
  5527. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5528. date('F', $now),
  5529. '/option',
  5530. '*/select',
  5531. '-',
  5532. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5533. $daysRegex,
  5534. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5535. date('j', $now),
  5536. '/option',
  5537. '*/select',
  5538. '-',
  5539. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5540. $yearsRegex,
  5541. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5542. date('Y', $now),
  5543. '/option',
  5544. '*/select',
  5545. '/div'
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. $this->Form->create('Contact');
  5549. $result = $this->Form->input('published', array('monthNames' => false));
  5550. $now = strtotime('now');
  5551. $expected = array(
  5552. 'div' => array('class' => 'input date'),
  5553. 'label' => array('for' => 'ContactPublishedMonth'),
  5554. 'Published',
  5555. '/label',
  5556. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5557. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5558. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5559. date('m', $now),
  5560. '/option',
  5561. '*/select',
  5562. '-',
  5563. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5564. $daysRegex,
  5565. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5566. date('j', $now),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5571. $yearsRegex,
  5572. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5573. date('Y', $now),
  5574. '/option',
  5575. '*/select',
  5576. '/div'
  5577. );
  5578. $this->assertTags($result, $expected);
  5579. $result = $this->Form->input('published', array(
  5580. 'timeFormat' => 24,
  5581. 'interval' => 5,
  5582. 'selected' => strtotime('2009-09-03 13:37:00'),
  5583. 'type' => 'datetime'
  5584. ));
  5585. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5586. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5587. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5588. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5589. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5590. }
  5591. /**
  5592. * Test dateTime with rounding
  5593. *
  5594. * @return void
  5595. */
  5596. public function testDateTimeRounding() {
  5597. $this->Form->request->data['Contact'] = array(
  5598. 'date' => array(
  5599. 'day' => '13',
  5600. 'month' => '12',
  5601. 'year' => '2010',
  5602. 'hour' => '04',
  5603. 'min' => '19',
  5604. 'meridian' => 'AM'
  5605. )
  5606. );
  5607. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5608. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5609. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5610. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5611. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5612. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5613. }
  5614. /**
  5615. * Test that empty values don't trigger errors.
  5616. *
  5617. * @return void
  5618. */
  5619. public function testDateTimeNoErrorsOnEmptyData() {
  5620. $this->Form->request->data['Contact'] = array(
  5621. 'date' => array(
  5622. 'day' => '',
  5623. 'month' => '',
  5624. 'year' => '',
  5625. 'hour' => '',
  5626. 'min' => '',
  5627. 'meridian' => ''
  5628. )
  5629. );
  5630. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5631. $this->assertNotEmpty($result);
  5632. }
  5633. /**
  5634. * test that datetime() and default values work.
  5635. *
  5636. * @return void
  5637. */
  5638. public function testDatetimeWithDefault() {
  5639. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5640. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5641. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5642. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5643. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5644. 'default' => '2009-06-01 11:15:30'
  5645. ));
  5646. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5647. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5648. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5649. }
  5650. /**
  5651. * test that bogus non-date time data doesn't cause errors.
  5652. *
  5653. * @return void
  5654. */
  5655. public function testDateTimeWithBogusData() {
  5656. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5657. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5658. }
  5659. /**
  5660. * testDateTimeEmptyAsArray
  5661. *
  5662. * @return void
  5663. */
  5664. public function testDateTimeEmptyAsArray() {
  5665. $result = $this->Form->dateTime('Contact.date',
  5666. 'DMY',
  5667. '12',
  5668. array(
  5669. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5670. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5671. )
  5672. )
  5673. );
  5674. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5675. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5676. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5677. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5678. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5679. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5680. $result = $this->Form->dateTime('Contact.date',
  5681. 'DMY',
  5682. '12',
  5683. array(
  5684. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5685. )
  5686. );
  5687. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5688. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5689. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5690. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5691. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5692. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5693. }
  5694. /**
  5695. * testFormDateTimeMulti method
  5696. *
  5697. * test multiple datetime element generation
  5698. *
  5699. * @return void
  5700. */
  5701. public function testFormDateTimeMulti() {
  5702. extract($this->dateRegex);
  5703. $result = $this->Form->dateTime('Contact.1.updated');
  5704. $expected = array(
  5705. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5706. $daysRegex,
  5707. array('option' => array('value' => '')),
  5708. '/option',
  5709. '*/select',
  5710. '-',
  5711. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5712. $monthsRegex,
  5713. array('option' => array('value' => '')),
  5714. '/option',
  5715. '*/select',
  5716. '-',
  5717. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5718. $yearsRegex,
  5719. array('option' => array('value' => '')),
  5720. '/option',
  5721. '*/select',
  5722. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5723. $hoursRegex,
  5724. array('option' => array('value' => '')),
  5725. '/option',
  5726. '*/select',
  5727. ':',
  5728. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5729. $minutesRegex,
  5730. array('option' => array('value' => '')),
  5731. '/option',
  5732. '*/select',
  5733. ' ',
  5734. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5735. $meridianRegex,
  5736. array('option' => array('value' => '')),
  5737. '/option',
  5738. '*/select'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $result = $this->Form->dateTime('Contact.2.updated');
  5742. $expected = array(
  5743. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5744. $daysRegex,
  5745. array('option' => array('value' => '')),
  5746. '/option',
  5747. '*/select',
  5748. '-',
  5749. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5750. $monthsRegex,
  5751. array('option' => array('value' => '')),
  5752. '/option',
  5753. '*/select',
  5754. '-',
  5755. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5756. $yearsRegex,
  5757. array('option' => array('value' => '')),
  5758. '/option',
  5759. '*/select',
  5760. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5761. $hoursRegex,
  5762. array('option' => array('value' => '')),
  5763. '/option',
  5764. '*/select',
  5765. ':',
  5766. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5767. $minutesRegex,
  5768. array('option' => array('value' => '')),
  5769. '/option',
  5770. '*/select',
  5771. ' ',
  5772. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5773. $meridianRegex,
  5774. array('option' => array('value' => '')),
  5775. '/option',
  5776. '*/select'
  5777. );
  5778. $this->assertTags($result, $expected);
  5779. }
  5780. /**
  5781. * When changing the date format, the label should always focus the first select box when
  5782. * clicked.
  5783. *
  5784. * @return void
  5785. */
  5786. public function testDateTimeLabelIdMatchesFirstInput() {
  5787. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5788. $this->assertContains('label for="ModelDateMonth"', $result);
  5789. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5790. $this->assertContains('label for="ModelDateDay"', $result);
  5791. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5792. $this->assertContains('label for="ModelDateYear"', $result);
  5793. }
  5794. /**
  5795. * testMonth method
  5796. *
  5797. * @return void
  5798. */
  5799. public function testMonth() {
  5800. $result = $this->Form->month('Model.field');
  5801. $expected = array(
  5802. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5803. array('option' => array('value' => '')),
  5804. '/option',
  5805. array('option' => array('value' => '01')),
  5806. date('F', strtotime('2008-01-01 00:00:00')),
  5807. '/option',
  5808. array('option' => array('value' => '02')),
  5809. date('F', strtotime('2008-02-01 00:00:00')),
  5810. '/option',
  5811. '*/select',
  5812. );
  5813. $this->assertTags($result, $expected);
  5814. $result = $this->Form->month('Model.field', array('empty' => true));
  5815. $expected = array(
  5816. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5817. array('option' => array('value' => '')),
  5818. '/option',
  5819. array('option' => array('value' => '01')),
  5820. date('F', strtotime('2008-01-01 00:00:00')),
  5821. '/option',
  5822. array('option' => array('value' => '02')),
  5823. date('F', strtotime('2008-02-01 00:00:00')),
  5824. '/option',
  5825. '*/select',
  5826. );
  5827. $this->assertTags($result, $expected);
  5828. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5829. $expected = array(
  5830. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5831. array('option' => array('value' => '')),
  5832. '/option',
  5833. array('option' => array('value' => '01')),
  5834. '01',
  5835. '/option',
  5836. array('option' => array('value' => '02')),
  5837. '02',
  5838. '/option',
  5839. '*/select',
  5840. );
  5841. $this->assertTags($result, $expected);
  5842. $monthNames = array(
  5843. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5844. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5845. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5846. $expected = array(
  5847. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5848. array('option' => array('value' => '')),
  5849. '/option',
  5850. array('option' => array('value' => '01')),
  5851. 'Jan',
  5852. '/option',
  5853. array('option' => array('value' => '02')),
  5854. 'Feb',
  5855. '/option',
  5856. '*/select',
  5857. );
  5858. $this->assertTags($result, $expected);
  5859. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5860. $result = $this->Form->month('Project.release');
  5861. $expected = array(
  5862. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5863. array('option' => array('value' => '')),
  5864. '/option',
  5865. array('option' => array('value' => '01')),
  5866. 'January',
  5867. '/option',
  5868. array('option' => array('value' => '02', 'selected' => 'selected')),
  5869. 'February',
  5870. '/option',
  5871. '*/select',
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. }
  5875. /**
  5876. * testDay method
  5877. *
  5878. * @return void
  5879. */
  5880. public function testDay() {
  5881. extract($this->dateRegex);
  5882. $result = $this->Form->day('Model.field', array('value' => false));
  5883. $expected = array(
  5884. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5885. array('option' => array('value' => '')),
  5886. '/option',
  5887. array('option' => array('value' => '01')),
  5888. '1',
  5889. '/option',
  5890. array('option' => array('value' => '02')),
  5891. '2',
  5892. '/option',
  5893. $daysRegex,
  5894. '/select',
  5895. );
  5896. $this->assertTags($result, $expected);
  5897. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5898. $result = $this->Form->day('Model.field');
  5899. $expected = array(
  5900. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5901. array('option' => array('value' => '')),
  5902. '/option',
  5903. array('option' => array('value' => '01')),
  5904. '1',
  5905. '/option',
  5906. array('option' => array('value' => '02')),
  5907. '2',
  5908. '/option',
  5909. $daysRegex,
  5910. array('option' => array('value' => '10', 'selected' => 'selected')),
  5911. '10',
  5912. '/option',
  5913. $daysRegex,
  5914. '/select',
  5915. );
  5916. $this->assertTags($result, $expected);
  5917. $this->Form->request->data['Model']['field'] = '';
  5918. $result = $this->Form->day('Model.field', array('value' => '10'));
  5919. $expected = array(
  5920. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5921. array('option' => array('value' => '')),
  5922. '/option',
  5923. array('option' => array('value' => '01')),
  5924. '1',
  5925. '/option',
  5926. array('option' => array('value' => '02')),
  5927. '2',
  5928. '/option',
  5929. $daysRegex,
  5930. array('option' => array('value' => '10', 'selected' => 'selected')),
  5931. '10',
  5932. '/option',
  5933. $daysRegex,
  5934. '/select',
  5935. );
  5936. $this->assertTags($result, $expected);
  5937. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5938. $result = $this->Form->day('Model.field', array('value' => true));
  5939. $expected = array(
  5940. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5941. array('option' => array('value' => '')),
  5942. '/option',
  5943. array('option' => array('value' => '01')),
  5944. '1',
  5945. '/option',
  5946. array('option' => array('value' => '02')),
  5947. '2',
  5948. '/option',
  5949. $daysRegex,
  5950. array('option' => array('value' => '10', 'selected' => 'selected')),
  5951. '10',
  5952. '/option',
  5953. $daysRegex,
  5954. '/select',
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5958. $result = $this->Form->day('Project.release');
  5959. $expected = array(
  5960. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5961. array('option' => array('value' => '')),
  5962. '/option',
  5963. array('option' => array('value' => '01')),
  5964. '1',
  5965. '/option',
  5966. array('option' => array('value' => '02')),
  5967. '2',
  5968. '/option',
  5969. $daysRegex,
  5970. array('option' => array('value' => '10', 'selected' => 'selected')),
  5971. '10',
  5972. '/option',
  5973. $daysRegex,
  5974. '/select',
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. }
  5978. /**
  5979. * testMinute method
  5980. *
  5981. * @return void
  5982. */
  5983. public function testMinute() {
  5984. extract($this->dateRegex);
  5985. $result = $this->Form->minute('Model.field');
  5986. $expected = array(
  5987. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5988. array('option' => array('value' => '')),
  5989. '/option',
  5990. array('option' => array('value' => '00')),
  5991. '00',
  5992. '/option',
  5993. array('option' => array('value' => '01')),
  5994. '01',
  5995. '/option',
  5996. array('option' => array('value' => '02')),
  5997. '02',
  5998. '/option',
  5999. $minutesRegex,
  6000. '/select',
  6001. );
  6002. $this->assertTags($result, $expected);
  6003. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6004. $result = $this->Form->minute('Model.field');
  6005. $expected = array(
  6006. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6007. array('option' => array('value' => '')),
  6008. '/option',
  6009. array('option' => array('value' => '00')),
  6010. '00',
  6011. '/option',
  6012. array('option' => array('value' => '01')),
  6013. '01',
  6014. '/option',
  6015. array('option' => array('value' => '02')),
  6016. '02',
  6017. '/option',
  6018. $minutesRegex,
  6019. array('option' => array('value' => '12', 'selected' => 'selected')),
  6020. '12',
  6021. '/option',
  6022. $minutesRegex,
  6023. '/select',
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $this->Form->request->data['Model']['field'] = '';
  6027. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6028. $expected = array(
  6029. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6030. array('option' => array('value' => '')),
  6031. '/option',
  6032. array('option' => array('value' => '00')),
  6033. '00',
  6034. '/option',
  6035. array('option' => array('value' => '05')),
  6036. '05',
  6037. '/option',
  6038. array('option' => array('value' => '10')),
  6039. '10',
  6040. '/option',
  6041. $minutesRegex,
  6042. '/select',
  6043. );
  6044. $this->assertTags($result, $expected);
  6045. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6046. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6047. $expected = array(
  6048. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6049. array('option' => array('value' => '')),
  6050. '/option',
  6051. array('option' => array('value' => '00')),
  6052. '00',
  6053. '/option',
  6054. array('option' => array('value' => '05')),
  6055. '05',
  6056. '/option',
  6057. array('option' => array('value' => '10', 'selected' => 'selected')),
  6058. '10',
  6059. '/option',
  6060. $minutesRegex,
  6061. '/select',
  6062. );
  6063. $this->assertTags($result, $expected);
  6064. }
  6065. /**
  6066. * testHour method
  6067. *
  6068. * @return void
  6069. */
  6070. public function testHour() {
  6071. extract($this->dateRegex);
  6072. $result = $this->Form->hour('Model.field', false);
  6073. $expected = array(
  6074. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6075. array('option' => array('value' => '')),
  6076. '/option',
  6077. array('option' => array('value' => '01')),
  6078. '1',
  6079. '/option',
  6080. array('option' => array('value' => '02')),
  6081. '2',
  6082. '/option',
  6083. $hoursRegex,
  6084. '/select',
  6085. );
  6086. $this->assertTags($result, $expected);
  6087. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6088. $result = $this->Form->hour('Model.field', false);
  6089. $expected = array(
  6090. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6091. array('option' => array('value' => '')),
  6092. '/option',
  6093. array('option' => array('value' => '01')),
  6094. '1',
  6095. '/option',
  6096. array('option' => array('value' => '02')),
  6097. '2',
  6098. '/option',
  6099. $hoursRegex,
  6100. array('option' => array('value' => '12', 'selected' => 'selected')),
  6101. '12',
  6102. '/option',
  6103. '/select',
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $this->Form->request->data['Model']['field'] = '';
  6107. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6108. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6109. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6110. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6111. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6112. $result = $this->Form->hour('Model.field', true);
  6113. $expected = array(
  6114. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6115. array('option' => array('value' => '')),
  6116. '/option',
  6117. array('option' => array('value' => '00', 'selected' => 'selected')),
  6118. '0',
  6119. '/option',
  6120. array('option' => array('value' => '01')),
  6121. '1',
  6122. '/option',
  6123. array('option' => array('value' => '02')),
  6124. '2',
  6125. '/option',
  6126. $hoursRegex,
  6127. '/select',
  6128. );
  6129. $this->assertTags($result, $expected);
  6130. unset($this->Form->request->data['Model']['field']);
  6131. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6132. $thisHour = date('H');
  6133. $optValue = date('G');
  6134. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6135. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6136. $result = $this->Form->hour('Model.field', true);
  6137. $expected = array(
  6138. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6139. array('option' => array('value' => '')),
  6140. '/option',
  6141. array('option' => array('value' => '00')),
  6142. '0',
  6143. '/option',
  6144. array('option' => array('value' => '01', 'selected' => 'selected')),
  6145. '1',
  6146. '/option',
  6147. array('option' => array('value' => '02')),
  6148. '2',
  6149. '/option',
  6150. $hoursRegex,
  6151. '/select',
  6152. );
  6153. $this->assertTags($result, $expected);
  6154. }
  6155. /**
  6156. * testYear method
  6157. *
  6158. * @return void
  6159. */
  6160. public function testYear() {
  6161. $result = $this->Form->year('Model.field', 2006, 2007);
  6162. $expected = array(
  6163. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6164. array('option' => array('value' => '')),
  6165. '/option',
  6166. array('option' => array('value' => '2007')),
  6167. '2007',
  6168. '/option',
  6169. array('option' => array('value' => '2006')),
  6170. '2006',
  6171. '/option',
  6172. '/select',
  6173. );
  6174. $this->assertTags($result, $expected);
  6175. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6176. $expected = array(
  6177. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6178. array('option' => array('value' => '')),
  6179. '/option',
  6180. array('option' => array('value' => '2006')),
  6181. '2006',
  6182. '/option',
  6183. array('option' => array('value' => '2007')),
  6184. '2007',
  6185. '/option',
  6186. '/select',
  6187. );
  6188. $this->assertTags($result, $expected);
  6189. $this->request->data['Contact']['published'] = '';
  6190. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6191. $expected = array(
  6192. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6193. array('option' => array('value' => '')),
  6194. '/option',
  6195. array('option' => array('value' => '2007')),
  6196. '2007',
  6197. '/option',
  6198. array('option' => array('value' => '2006')),
  6199. '2006',
  6200. '/option',
  6201. '/select',
  6202. );
  6203. $this->assertTags($result, $expected);
  6204. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6205. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6206. $expected = array(
  6207. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6208. array('option' => array('value' => '2007')),
  6209. '2007',
  6210. '/option',
  6211. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6212. '2006',
  6213. '/option',
  6214. '/select',
  6215. );
  6216. $this->assertTags($result, $expected);
  6217. $this->Form->request->data['Contact']['published'] = '';
  6218. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6219. $expected = array(
  6220. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6221. array('option' => array('value' => '')),
  6222. '/option',
  6223. array('option' => array('value' => '2007')),
  6224. '2007',
  6225. '/option',
  6226. array('option' => array('value' => '2006')),
  6227. '2006',
  6228. '/option',
  6229. '/select',
  6230. );
  6231. $this->assertTags($result, $expected);
  6232. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6233. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6234. $expected = array(
  6235. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6236. array('option' => array('value' => '2007')),
  6237. '2007',
  6238. '/option',
  6239. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6240. '2006',
  6241. '/option',
  6242. '/select',
  6243. );
  6244. $this->assertTags($result, $expected);
  6245. $this->Form->request->data['Contact']['published'] = '';
  6246. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6247. $expected = array(
  6248. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6249. array('option' => array('value' => '')),
  6250. '/option',
  6251. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6252. '2007',
  6253. '/option',
  6254. array('option' => array('value' => '2006')),
  6255. '2006',
  6256. '/option',
  6257. '/select',
  6258. );
  6259. $this->assertTags($result, $expected);
  6260. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6261. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6262. $expected = array(
  6263. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6264. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6265. '2007',
  6266. '/option',
  6267. array('option' => array('value' => '2006')),
  6268. '2006',
  6269. '/option',
  6270. '/select',
  6271. );
  6272. $this->assertTags($result, $expected);
  6273. $this->Form->request->data['Contact']['published'] = '';
  6274. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6275. $expected = array(
  6276. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6277. array('option' => array('value' => '2008')),
  6278. '2008',
  6279. '/option',
  6280. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6281. '2007',
  6282. '/option',
  6283. array('option' => array('value' => '2006')),
  6284. '2006',
  6285. '/option',
  6286. '/select',
  6287. );
  6288. $this->assertTags($result, $expected);
  6289. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6290. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6291. $expected = array(
  6292. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6293. array('option' => array('value' => '2008')),
  6294. '2008',
  6295. '/option',
  6296. array('option' => array('value' => '2007')),
  6297. '2007',
  6298. '/option',
  6299. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6300. '2006',
  6301. '/option',
  6302. '/select',
  6303. );
  6304. $this->assertTags($result, $expected);
  6305. $this->Form->request->data = array();
  6306. $this->Form->create('Contact');
  6307. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6308. $expected = array(
  6309. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6310. array('option' => array('value' => '2008')),
  6311. '2008',
  6312. '/option',
  6313. array('option' => array('value' => '2007')),
  6314. '2007',
  6315. '/option',
  6316. array('option' => array('value' => '2006')),
  6317. '2006',
  6318. '/option',
  6319. '/select',
  6320. );
  6321. $this->assertTags($result, $expected);
  6322. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6323. $this->assertContains('data[Contact][published][year]', $result);
  6324. }
  6325. /**
  6326. * testYearAutoExpandRange method
  6327. *
  6328. * @return void
  6329. */
  6330. public function testYearAutoExpandRange() {
  6331. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6332. $result = $this->Form->year('User.birthday');
  6333. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6334. $result = $matches[1];
  6335. $expected = range(date('Y') + 20, 1930);
  6336. $this->assertEquals($expected, $result);
  6337. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6338. $result = $this->Form->year('Project.release');
  6339. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6340. $result = $matches[1];
  6341. $expected = range(2050, date('Y') - 20);
  6342. $this->assertEquals($expected, $result);
  6343. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6344. $result = $this->Form->year('Project.release', 1890, 1900);
  6345. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6346. $result = $matches[1];
  6347. $expected = range(1900, 1881);
  6348. $this->assertEquals($expected, $result);
  6349. }
  6350. /**
  6351. * testInputDate method
  6352. *
  6353. * Test various inputs with type date and different dateFormat values.
  6354. * Failing to provide a dateFormat key should not error.
  6355. * It should simply not pre-select any value then.
  6356. *
  6357. * @return void
  6358. */
  6359. public function testInputDate() {
  6360. $this->Form->request->data = array(
  6361. 'User' => array(
  6362. 'month_year' => array('month' => date('m')),
  6363. 'just_year' => array('month' => date('m')),
  6364. 'just_month' => array('year' => date('Y')),
  6365. 'just_day' => array('month' => date('m')),
  6366. )
  6367. );
  6368. $this->Form->create('User');
  6369. $result = $this->Form->input('month_year',
  6370. array(
  6371. 'label' => false,
  6372. 'div' => false,
  6373. 'type' => 'date',
  6374. 'dateFormat' => 'MY',
  6375. 'minYear' => 2006,
  6376. 'maxYear' => 2008
  6377. )
  6378. );
  6379. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6380. $this->assertNotContains('value="2008" selected="selected"', $result);
  6381. $result = $this->Form->input('just_year',
  6382. array(
  6383. 'type' => 'date',
  6384. 'label' => false,
  6385. 'dateFormat' => 'Y',
  6386. 'minYear' => date('Y'),
  6387. 'maxYear' => date('Y', strtotime('+20 years'))
  6388. )
  6389. );
  6390. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6391. $result = $this->Form->input('just_month',
  6392. array(
  6393. 'type' => 'date',
  6394. 'label' => false,
  6395. 'dateFormat' => 'M',
  6396. 'empty' => false,
  6397. )
  6398. );
  6399. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6400. $result = $this->Form->input('just_day',
  6401. array(
  6402. 'type' => 'date',
  6403. 'label' => false,
  6404. 'dateFormat' => 'D',
  6405. 'empty' => false,
  6406. )
  6407. );
  6408. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6409. }
  6410. /**
  6411. * testInputDateMaxYear method
  6412. *
  6413. * Let's say we want to only allow users born from 2006 to 2008 to register
  6414. * This being the first singup page, we still don't have any data
  6415. *
  6416. * @return void
  6417. */
  6418. public function testInputDateMaxYear() {
  6419. $this->Form->request->data = array();
  6420. $this->Form->create('User');
  6421. $result = $this->Form->input('birthday',
  6422. array(
  6423. 'label' => false,
  6424. 'div' => false,
  6425. 'type' => 'date',
  6426. 'dateFormat' => 'DMY',
  6427. 'minYear' => 2006,
  6428. 'maxYear' => 2008
  6429. )
  6430. );
  6431. $this->assertContains('value="2008" selected="selected"', $result);
  6432. }
  6433. /**
  6434. * testTextArea method
  6435. *
  6436. * @return void
  6437. */
  6438. public function testTextArea() {
  6439. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6440. $result = $this->Form->textarea('Model.field');
  6441. $expected = array(
  6442. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6443. 'some test data',
  6444. '/textarea',
  6445. );
  6446. $this->assertTags($result, $expected);
  6447. $result = $this->Form->textarea('Model.tmp');
  6448. $expected = array(
  6449. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6450. '/textarea',
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6454. $result = $this->Form->textarea('Model.field');
  6455. $expected = array(
  6456. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6457. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6458. '/textarea',
  6459. );
  6460. $this->assertTags($result, $expected);
  6461. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6462. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6463. $expected = array(
  6464. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6465. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6466. '/textarea',
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6470. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6471. $expected = array(
  6472. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6473. '/textarea'
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. }
  6477. /**
  6478. * testTextAreaWithStupidCharacters method
  6479. *
  6480. * test text area with non-ascii characters
  6481. *
  6482. * @return void
  6483. */
  6484. public function testTextAreaWithStupidCharacters() {
  6485. $this->loadFixtures('Post');
  6486. $result = $this->Form->input('Post.content', array(
  6487. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6488. ));
  6489. $expected = array(
  6490. 'div' => array('class' => 'input textarea'),
  6491. 'label' => array('for' => 'PostContent'),
  6492. 'Current Text',
  6493. '/label',
  6494. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6495. 'GREAT®',
  6496. '/textarea',
  6497. '/div'
  6498. );
  6499. $this->assertTags($result, $expected);
  6500. }
  6501. /**
  6502. * testHiddenField method
  6503. *
  6504. * @return void
  6505. */
  6506. public function testHiddenField() {
  6507. $Contact = ClassRegistry::getObject('Contact');
  6508. $Contact->validationErrors['field'] = 1;
  6509. $this->Form->request->data['Contact']['field'] = 'test';
  6510. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6511. $this->assertTags($result, array(
  6512. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6513. );
  6514. }
  6515. /**
  6516. * testFileUploadField method
  6517. *
  6518. * @return void
  6519. */
  6520. public function testFileUploadField() {
  6521. $result = $this->Form->file('Model.upload');
  6522. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6523. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6524. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6525. $expected = array(
  6526. 'div' => array('class' => 'input file'),
  6527. 'label' => array('for' => 'ModelUpload'),
  6528. 'Upload',
  6529. '/label',
  6530. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6531. '/div'
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6535. $result = $this->Form->file('Model.upload');
  6536. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6537. }
  6538. /**
  6539. * test File upload input on a model not used in create();
  6540. *
  6541. * @return void
  6542. */
  6543. public function testFileUploadOnOtherModel() {
  6544. $this->Form->create('ValidateUser', array('type' => 'file'));
  6545. $result = $this->Form->file('ValidateProfile.city');
  6546. $expected = array(
  6547. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6548. );
  6549. $this->assertTags($result, $expected);
  6550. }
  6551. /**
  6552. * testButton method
  6553. *
  6554. * @return void
  6555. */
  6556. public function testButton() {
  6557. $result = $this->Form->button('Hi');
  6558. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6559. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6560. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6561. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6562. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6563. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6564. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6565. $result = $this->Form->button('No type', array('type' => false));
  6566. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6567. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6568. $this->assertNotRegExp('/\&039/', $result);
  6569. }
  6570. /**
  6571. * Test that button() makes unlocked fields by default.
  6572. *
  6573. * @return void
  6574. */
  6575. public function testButtonUnlockedByDefault() {
  6576. $this->Form->request->params['_Token']['key'] = 'secured';
  6577. $this->Form->button('Save', array('name' => 'save'));
  6578. $this->Form->button('Clear');
  6579. $result = $this->Form->unlockField();
  6580. $this->assertEquals(array('save'), $result);
  6581. }
  6582. /**
  6583. * testPostButton method
  6584. *
  6585. * @return void
  6586. */
  6587. public function testPostButton() {
  6588. $result = $this->Form->postButton('Hi', '/controller/action');
  6589. $this->assertTags($result, array(
  6590. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6591. 'div' => array('style' => 'display:none;'),
  6592. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6593. '/div',
  6594. 'button' => array('type' => 'submit'),
  6595. 'Hi',
  6596. '/button',
  6597. '/form'
  6598. ));
  6599. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6600. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6601. }
  6602. /**
  6603. * Test that postButton adds _Token fields.
  6604. *
  6605. * @return void
  6606. */
  6607. public function testSecurePostButton() {
  6608. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6609. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6610. $expected = array(
  6611. 'form' => array(
  6612. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6613. ),
  6614. array('div' => array('style' => 'display:none;')),
  6615. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6616. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6617. '/div',
  6618. 'button' => array('type' => 'submit'),
  6619. 'Delete',
  6620. '/button',
  6621. array('div' => array('style' => 'display:none;')),
  6622. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6623. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6624. '/div',
  6625. '/form',
  6626. );
  6627. $this->assertTags($result, $expected);
  6628. }
  6629. /**
  6630. * testPostLink method
  6631. *
  6632. * @return void
  6633. */
  6634. public function testPostLink() {
  6635. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6636. $this->assertTags($result, array(
  6637. 'form' => array(
  6638. 'method' => 'post', 'action' => '/posts/delete/1',
  6639. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6640. ),
  6641. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6642. '/form',
  6643. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6644. 'Delete',
  6645. '/a'
  6646. ));
  6647. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6648. $this->assertTags($result, array(
  6649. 'form' => array(
  6650. 'method' => 'post', 'action' => '/posts/delete/1',
  6651. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6652. ),
  6653. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6654. '/form',
  6655. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6656. 'Delete',
  6657. '/a'
  6658. ));
  6659. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6660. $this->assertTags($result, array(
  6661. 'form' => array(
  6662. 'method' => 'post', 'action' => '/posts/delete/1',
  6663. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6664. ),
  6665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6666. '/form',
  6667. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6668. 'Delete',
  6669. '/a'
  6670. ));
  6671. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6672. $this->assertTags($result, array(
  6673. 'form' => array(
  6674. 'method' => 'post', 'action' => '/posts/delete/1',
  6675. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6676. ),
  6677. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6678. '/form',
  6679. '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;/'),
  6680. 'Delete',
  6681. '/a'
  6682. ));
  6683. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6684. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6685. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6686. $this->assertTags($result, array(
  6687. 'form' => array(
  6688. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6689. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6690. ),
  6691. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6692. '/form',
  6693. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6694. 'Delete',
  6695. '/a'
  6696. ));
  6697. $result = $this->Form->postLink(
  6698. '',
  6699. array('controller' => 'items', 'action' => 'delete', 10),
  6700. array('class' => 'btn btn-danger', 'escape' => false),
  6701. 'Confirm thing'
  6702. );
  6703. $this->assertTags($result, array(
  6704. 'form' => array(
  6705. 'method' => 'post', 'action' => '/items/delete/10',
  6706. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6707. ),
  6708. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6709. '/form',
  6710. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6711. '/a'
  6712. ));
  6713. }
  6714. /**
  6715. * test creating postLinks after a GET form.
  6716. *
  6717. * @return void
  6718. */
  6719. public function testPostLinkAfterGetForm() {
  6720. $this->Form->request->params['_Token']['key'] = 'testkey';
  6721. $this->Form->create('User', array('type' => 'get'));
  6722. $this->Form->end();
  6723. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6724. $this->assertTags($result, array(
  6725. 'form' => array(
  6726. 'method' => 'post', 'action' => '/posts/delete/1',
  6727. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6728. ),
  6729. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6730. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6731. 'div' => array('style' => 'display:none;'),
  6732. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6733. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6734. '/div',
  6735. '/form',
  6736. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6737. 'Delete',
  6738. '/a'
  6739. ));
  6740. }
  6741. /**
  6742. * Test that postLink adds _Token fields.
  6743. *
  6744. * @return void
  6745. */
  6746. public function testSecurePostLink() {
  6747. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6748. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6749. $expected = array(
  6750. 'form' => array(
  6751. 'method' => 'post', 'action' => '/posts/delete/1',
  6752. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6753. ),
  6754. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6755. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6756. 'div' => array('style' => 'display:none;'),
  6757. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6758. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6759. '/div',
  6760. '/form',
  6761. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6762. 'Delete',
  6763. '/a'
  6764. );
  6765. $this->assertTags($result, $expected);
  6766. }
  6767. /**
  6768. * testSubmitButton method
  6769. *
  6770. * @return void
  6771. */
  6772. public function testSubmitButton() {
  6773. $result = $this->Form->submit('');
  6774. $expected = array(
  6775. 'div' => array('class' => 'submit'),
  6776. 'input' => array('type' => 'submit', 'value' => ''),
  6777. '/div'
  6778. );
  6779. $this->assertTags($result, $expected);
  6780. $result = $this->Form->submit('Test Submit');
  6781. $expected = array(
  6782. 'div' => array('class' => 'submit'),
  6783. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6784. '/div'
  6785. );
  6786. $this->assertTags($result, $expected);
  6787. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6788. $expected = array(
  6789. 'span' => array('class' => 'submit'),
  6790. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6791. '/span'
  6792. );
  6793. $this->assertTags($result, $expected);
  6794. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6795. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6796. $this->assertTags($result, $expected);
  6797. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6798. $expected = array(
  6799. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6800. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6801. '/div'
  6802. );
  6803. $this->assertTags($result, $expected);
  6804. $result = $this->Form->submit('Next >');
  6805. $expected = array(
  6806. 'div' => array('class' => 'submit'),
  6807. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6808. '/div'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. $result = $this->Form->submit('Next >', array('escape' => false));
  6812. $expected = array(
  6813. 'div' => array('class' => 'submit'),
  6814. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6815. '/div'
  6816. );
  6817. $this->assertTags($result, $expected);
  6818. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6819. $expected = array(
  6820. 'div' => array('class' => 'submit'),
  6821. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6822. '/div'
  6823. );
  6824. $this->assertTags($result, $expected);
  6825. $before = '--before--';
  6826. $after = '--after--';
  6827. $result = $this->Form->submit('Test', array('before' => $before));
  6828. $expected = array(
  6829. 'div' => array('class' => 'submit'),
  6830. '--before--',
  6831. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6832. '/div'
  6833. );
  6834. $this->assertTags($result, $expected);
  6835. $result = $this->Form->submit('Test', array('after' => $after));
  6836. $expected = array(
  6837. 'div' => array('class' => 'submit'),
  6838. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6839. '--after--',
  6840. '/div'
  6841. );
  6842. $this->assertTags($result, $expected);
  6843. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6844. $expected = array(
  6845. 'div' => array('class' => 'submit'),
  6846. '--before--',
  6847. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6848. '--after--',
  6849. '/div'
  6850. );
  6851. $this->assertTags($result, $expected);
  6852. }
  6853. /**
  6854. * test image submit types.
  6855. *
  6856. * @return void
  6857. */
  6858. public function testSubmitImage() {
  6859. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6860. $expected = array(
  6861. 'div' => array('class' => 'submit'),
  6862. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6863. '/div'
  6864. );
  6865. $this->assertTags($result, $expected);
  6866. $result = $this->Form->submit('/relative/cake.power.gif');
  6867. $expected = array(
  6868. 'div' => array('class' => 'submit'),
  6869. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6870. '/div'
  6871. );
  6872. $this->assertTags($result, $expected);
  6873. $result = $this->Form->submit('cake.power.gif');
  6874. $expected = array(
  6875. 'div' => array('class' => 'submit'),
  6876. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6877. '/div'
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. $result = $this->Form->submit('Not.an.image');
  6881. $expected = array(
  6882. 'div' => array('class' => 'submit'),
  6883. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6884. '/div'
  6885. );
  6886. $this->assertTags($result, $expected);
  6887. $after = '--after--';
  6888. $before = '--before--';
  6889. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6890. $expected = array(
  6891. 'div' => array('class' => 'submit'),
  6892. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6893. '--after--',
  6894. '/div'
  6895. );
  6896. $this->assertTags($result, $expected);
  6897. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6898. $expected = array(
  6899. 'div' => array('class' => 'submit'),
  6900. '--before--',
  6901. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6902. '/div'
  6903. );
  6904. $this->assertTags($result, $expected);
  6905. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6906. $expected = array(
  6907. 'div' => array('class' => 'submit'),
  6908. '--before--',
  6909. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6910. '--after--',
  6911. '/div'
  6912. );
  6913. $this->assertTags($result, $expected);
  6914. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6915. $expected = array(
  6916. 'div' => array('class' => 'submit'),
  6917. '--before--',
  6918. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6919. '--after--',
  6920. '/div'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. }
  6924. /**
  6925. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6926. * be submitted at a time.
  6927. *
  6928. * @return void
  6929. */
  6930. public function testSubmitUnlockedByDefault() {
  6931. $this->Form->request->params['_Token']['key'] = 'secured';
  6932. $this->Form->submit('Go go');
  6933. $this->Form->submit('Save', array('name' => 'save'));
  6934. $result = $this->Form->unlockField();
  6935. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6936. }
  6937. /**
  6938. * Test submit image with timestamps.
  6939. *
  6940. * @return void
  6941. */
  6942. public function testSubmitImageTimestamp() {
  6943. Configure::write('Asset.timestamp', 'force');
  6944. $result = $this->Form->submit('cake.power.gif');
  6945. $expected = array(
  6946. 'div' => array('class' => 'submit'),
  6947. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6948. '/div'
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. }
  6952. /**
  6953. * test the create() method
  6954. *
  6955. * @return void
  6956. */
  6957. public function testCreate() {
  6958. $result = $this->Form->create('Contact');
  6959. $encoding = strtolower(Configure::read('App.encoding'));
  6960. $expected = array(
  6961. 'form' => array(
  6962. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6963. 'accept-charset' => $encoding
  6964. ),
  6965. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6966. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6967. '/div'
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6971. $expected = array('form' => array(
  6972. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6973. 'accept-charset' => $encoding
  6974. ));
  6975. $this->assertTags($result, $expected);
  6976. $result = $this->Form->create('Contact', array('type' => 'get'));
  6977. $expected = array('form' => array(
  6978. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6979. 'accept-charset' => $encoding
  6980. ));
  6981. $this->assertTags($result, $expected);
  6982. $result = $this->Form->create('Contact', array('type' => 'put'));
  6983. $expected = array(
  6984. 'form' => array(
  6985. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6986. 'accept-charset' => $encoding
  6987. ),
  6988. 'div' => array('style' => 'display:none;'),
  6989. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6990. '/div'
  6991. );
  6992. $this->assertTags($result, $expected);
  6993. $result = $this->Form->create('Contact', array('type' => 'file'));
  6994. $expected = array(
  6995. 'form' => array(
  6996. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6997. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6998. ),
  6999. 'div' => array('style' => 'display:none;'),
  7000. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7001. '/div'
  7002. );
  7003. $this->assertTags($result, $expected);
  7004. $this->Form->request->data['Contact']['id'] = 1;
  7005. $this->Form->request->here = '/contacts/edit/1';
  7006. $this->Form->request['action'] = 'edit';
  7007. $result = $this->Form->create('Contact');
  7008. $expected = array(
  7009. 'form' => array(
  7010. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7011. 'accept-charset' => $encoding
  7012. ),
  7013. 'div' => array('style' => 'display:none;'),
  7014. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7015. '/div'
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. $this->Form->request->data['Contact']['id'] = 1;
  7019. $this->Form->request->here = '/contacts/edit/1';
  7020. $this->Form->request['action'] = 'edit';
  7021. $result = $this->Form->create('Contact', array('type' => 'file'));
  7022. $expected = array(
  7023. 'form' => array(
  7024. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7025. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7026. ),
  7027. 'div' => array('style' => 'display:none;'),
  7028. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7029. '/div'
  7030. );
  7031. $this->assertTags($result, $expected);
  7032. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7033. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7034. $expected = array(
  7035. 'form' => array(
  7036. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7037. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7038. ),
  7039. 'div' => array('style' => 'display:none;'),
  7040. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7041. '/div'
  7042. );
  7043. $this->assertTags($result, $expected);
  7044. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7045. $expected = array(
  7046. 'form' => array(
  7047. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7048. 'accept-charset' => $encoding
  7049. ),
  7050. 'div' => array('style' => 'display:none;'),
  7051. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7052. '/div'
  7053. );
  7054. $this->assertTags($result, $expected);
  7055. $this->Form->request['action'] = 'add';
  7056. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7057. $expected = array(
  7058. 'form' => array(
  7059. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7060. 'accept-charset' => $encoding
  7061. ),
  7062. 'div' => array('style' => 'display:none;'),
  7063. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7064. '/div'
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. $result = $this->Form->create('User', array('action' => 'login'));
  7068. $expected = array(
  7069. 'form' => array(
  7070. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7071. 'accept-charset' => $encoding
  7072. ),
  7073. 'div' => array('style' => 'display:none;'),
  7074. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7075. '/div'
  7076. );
  7077. $this->assertTags($result, $expected);
  7078. $result = $this->Form->create('User', array('url' => '/users/login'));
  7079. $expected = array(
  7080. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7081. 'div' => array('style' => 'display:none;'),
  7082. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7083. '/div'
  7084. );
  7085. $this->assertTags($result, $expected);
  7086. $this->Form->request['controller'] = 'pages';
  7087. $result = $this->Form->create('User', array('action' => 'signup'));
  7088. $expected = array(
  7089. 'form' => array(
  7090. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7091. 'accept-charset' => $encoding
  7092. ),
  7093. 'div' => array('style' => 'display:none;'),
  7094. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7095. '/div'
  7096. );
  7097. $this->assertTags($result, $expected);
  7098. $this->Form->request->data = array();
  7099. $this->Form->request['controller'] = 'contacts';
  7100. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7101. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7102. $expected = array(
  7103. 'form' => array(
  7104. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7105. 'accept-charset' => 'utf-8'
  7106. ),
  7107. 'div' => array('style' => 'display:none;'),
  7108. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7109. '/div'
  7110. );
  7111. $this->assertTags($result, $expected);
  7112. }
  7113. /**
  7114. * Test the onsubmit option for create()
  7115. *
  7116. * @return void
  7117. */
  7118. public function testCreateOnSubmit() {
  7119. $this->Form->request->data = array();
  7120. $this->Form->request['controller'] = 'contacts';
  7121. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7122. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7123. $expected = array(
  7124. 'form' => array(
  7125. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7126. 'accept-charset' => 'utf-8'
  7127. ),
  7128. 'div' => array('style' => 'display:none;'),
  7129. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7130. '/div'
  7131. );
  7132. $this->assertTags($result, $expected);
  7133. $this->Form->request->data = array();
  7134. $this->Form->request['controller'] = 'contacts';
  7135. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7136. $result = $this->Form->create(array(
  7137. 'url' => array('action' => 'index', 'param'),
  7138. 'default' => false,
  7139. 'onsubmit' => 'someFunction();'
  7140. ));
  7141. $expected = array(
  7142. 'form' => array(
  7143. 'id' => 'ContactAddForm', 'method' => 'post',
  7144. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7145. 'action' => '/contacts/index/param',
  7146. 'accept-charset' => 'utf-8'
  7147. ),
  7148. 'div' => array('style' => 'display:none;'),
  7149. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7150. '/div'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. }
  7154. /**
  7155. * test create() with automatic url generation
  7156. *
  7157. * @return void
  7158. */
  7159. public function testCreateAutoUrl() {
  7160. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7161. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7162. $this->Form->request->base = '/base_url';
  7163. $result = $this->Form->create('Contact');
  7164. $expected = array(
  7165. 'form' => array(
  7166. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7167. 'accept-charset' => 'utf-8'
  7168. ),
  7169. 'div' => array('style' => 'display:none;'),
  7170. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7171. '/div'
  7172. );
  7173. $this->assertTags($result, $expected);
  7174. $this->Form->request['action'] = 'delete';
  7175. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7176. $this->Form->request->base = '/base_url';
  7177. $result = $this->Form->create('Contact');
  7178. $expected = array(
  7179. 'form' => array(
  7180. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7181. 'accept-charset' => 'utf-8'
  7182. ),
  7183. 'div' => array('style' => 'display:none;'),
  7184. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7185. '/div'
  7186. );
  7187. $this->assertTags($result, $expected);
  7188. }
  7189. /**
  7190. * test create() with a custom route
  7191. *
  7192. * @return void
  7193. */
  7194. public function testCreateCustomRoute() {
  7195. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7196. $encoding = strtolower(Configure::read('App.encoding'));
  7197. $result = $this->Form->create('User', array('action' => 'login'));
  7198. $expected = array(
  7199. 'form' => array(
  7200. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7201. 'accept-charset' => $encoding
  7202. ),
  7203. 'div' => array('style' => 'display:none;'),
  7204. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7205. '/div'
  7206. );
  7207. $this->assertTags($result, $expected);
  7208. }
  7209. /**
  7210. * test that inputDefaults are stored and used.
  7211. *
  7212. * @return void
  7213. */
  7214. public function testCreateWithInputDefaults() {
  7215. $this->Form->create('User', array(
  7216. 'inputDefaults' => array(
  7217. 'div' => false,
  7218. 'label' => false,
  7219. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7220. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7221. )
  7222. ));
  7223. $result = $this->Form->input('username');
  7224. $expected = array(
  7225. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7226. );
  7227. $this->assertTags($result, $expected);
  7228. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7229. $expected = array(
  7230. 'div' => array('class' => 'input text'),
  7231. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7232. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7233. '/div'
  7234. );
  7235. $this->assertTags($result, $expected);
  7236. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7237. $expected = array(
  7238. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7239. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7240. );
  7241. $this->assertTags($result, $expected);
  7242. $this->Form->create('User', array(
  7243. 'inputDefaults' => array(
  7244. 'div' => false,
  7245. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7246. )
  7247. ));
  7248. $result = $this->Form->input('username', array('div' => true));
  7249. $expected = array(
  7250. 'div' => array('class' => 'input text'),
  7251. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7252. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7253. '/div'
  7254. );
  7255. $this->assertTags($result, $expected);
  7256. }
  7257. /**
  7258. * test automatic accept-charset overriding
  7259. *
  7260. * @return void
  7261. */
  7262. public function testCreateWithAcceptCharset() {
  7263. $result = $this->Form->create('UserForm', array(
  7264. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7265. )
  7266. );
  7267. $expected = array(
  7268. 'form' => array(
  7269. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7270. 'accept-charset' => 'iso-8859-1'
  7271. ),
  7272. 'div' => array('style' => 'display:none;'),
  7273. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7274. '/div'
  7275. );
  7276. $this->assertTags($result, $expected);
  7277. }
  7278. /**
  7279. * Test base form URL when url param is passed with multiple parameters (&)
  7280. *
  7281. */
  7282. public function testCreateQuerystringrequest() {
  7283. $encoding = strtolower(Configure::read('App.encoding'));
  7284. $result = $this->Form->create('Contact', array(
  7285. 'type' => 'post',
  7286. 'escape' => false,
  7287. 'url' => array(
  7288. 'controller' => 'controller',
  7289. 'action' => 'action',
  7290. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7291. )
  7292. ));
  7293. $expected = array(
  7294. 'form' => array(
  7295. 'id' => 'ContactAddForm',
  7296. 'method' => 'post',
  7297. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7298. 'accept-charset' => $encoding
  7299. ),
  7300. 'div' => array('style' => 'display:none;'),
  7301. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7302. '/div'
  7303. );
  7304. $this->assertTags($result, $expected);
  7305. $result = $this->Form->create('Contact', array(
  7306. 'type' => 'post',
  7307. 'url' => array(
  7308. 'controller' => 'controller',
  7309. 'action' => 'action',
  7310. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7311. )
  7312. ));
  7313. $expected = array(
  7314. 'form' => array(
  7315. 'id' => 'ContactAddForm',
  7316. 'method' => 'post',
  7317. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7318. 'accept-charset' => $encoding
  7319. ),
  7320. 'div' => array('style' => 'display:none;'),
  7321. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7322. '/div'
  7323. );
  7324. $this->assertTags($result, $expected);
  7325. }
  7326. /**
  7327. * test that create() doesn't cause errors by multiple id's being in the primary key
  7328. * as could happen with multiple select or checkboxes.
  7329. *
  7330. * @return void
  7331. */
  7332. public function testCreateWithMultipleIdInData() {
  7333. $encoding = strtolower(Configure::read('App.encoding'));
  7334. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7335. $result = $this->Form->create('Contact');
  7336. $expected = array(
  7337. 'form' => array(
  7338. 'id' => 'ContactAddForm',
  7339. 'method' => 'post',
  7340. 'action' => '/contacts/add',
  7341. 'accept-charset' => $encoding
  7342. ),
  7343. 'div' => array('style' => 'display:none;'),
  7344. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7345. '/div'
  7346. );
  7347. $this->assertTags($result, $expected);
  7348. }
  7349. /**
  7350. * test that create() doesn't add in extra passed params.
  7351. *
  7352. * @return void
  7353. */
  7354. public function testCreatePassedArgs() {
  7355. $encoding = strtolower(Configure::read('App.encoding'));
  7356. $this->Form->request->data['Contact']['id'] = 1;
  7357. $result = $this->Form->create('Contact', array(
  7358. 'type' => 'post',
  7359. 'escape' => false,
  7360. 'url' => array(
  7361. 'action' => 'edit',
  7362. 'myparam'
  7363. )
  7364. ));
  7365. $expected = array(
  7366. 'form' => array(
  7367. 'id' => 'ContactAddForm',
  7368. 'method' => 'post',
  7369. 'action' => '/contacts/edit/myparam',
  7370. 'accept-charset' => $encoding
  7371. ),
  7372. 'div' => array('style' => 'display:none;'),
  7373. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7374. '/div'
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. }
  7378. /**
  7379. * test creating a get form, and get form inputs.
  7380. *
  7381. * @return void
  7382. */
  7383. public function testGetFormCreate() {
  7384. $encoding = strtolower(Configure::read('App.encoding'));
  7385. $result = $this->Form->create('Contact', array('type' => 'get'));
  7386. $this->assertTags($result, array('form' => array(
  7387. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7388. 'accept-charset' => $encoding
  7389. )));
  7390. $result = $this->Form->text('Contact.name');
  7391. $this->assertTags($result, array('input' => array(
  7392. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7393. )));
  7394. $result = $this->Form->password('password');
  7395. $this->assertTags($result, array('input' => array(
  7396. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7397. )));
  7398. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7399. $result = $this->Form->text('user_form');
  7400. $this->assertTags($result, array('input' => array(
  7401. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7402. )));
  7403. }
  7404. /**
  7405. * test get form, and inputs when the model param is false
  7406. *
  7407. * @return void
  7408. */
  7409. public function testGetFormWithFalseModel() {
  7410. $encoding = strtolower(Configure::read('App.encoding'));
  7411. $this->Form->request['controller'] = 'contact_test';
  7412. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7413. $expected = array('form' => array(
  7414. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7415. 'accept-charset' => $encoding
  7416. ));
  7417. $this->assertTags($result, $expected);
  7418. $result = $this->Form->text('reason');
  7419. $expected = array(
  7420. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7421. );
  7422. $this->assertTags($result, $expected);
  7423. }
  7424. /**
  7425. * test that datetime() works with GET style forms.
  7426. *
  7427. * @return void
  7428. */
  7429. public function testDateTimeWithGetForms() {
  7430. extract($this->dateRegex);
  7431. $this->Form->create('Contact', array('type' => 'get'));
  7432. $result = $this->Form->datetime('created');
  7433. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7434. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7435. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7436. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7437. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7438. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7439. }
  7440. /**
  7441. * testEditFormWithData method
  7442. *
  7443. * test auto populating form elements from submitted data.
  7444. *
  7445. * @return void
  7446. */
  7447. public function testEditFormWithData() {
  7448. $this->Form->request->data = array('Person' => array(
  7449. 'id' => 1,
  7450. 'first_name' => 'Nate',
  7451. 'last_name' => 'Abele',
  7452. 'email' => 'nate@example.com'
  7453. ));
  7454. $this->Form->request->addParams(array(
  7455. 'models' => array('Person'),
  7456. 'controller' => 'people',
  7457. 'action' => 'add'
  7458. ));
  7459. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7460. $this->Form->create();
  7461. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7462. $expected = array(
  7463. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7464. 'select' => array(
  7465. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7466. ),
  7467. array('option' => array('value' => 1)), 'Nate', '/option',
  7468. array('option' => array('value' => 2)), 'Garrett', '/option',
  7469. array('option' => array('value' => 3)), 'Larry', '/option',
  7470. '/select'
  7471. );
  7472. $this->assertTags($result, $expected);
  7473. }
  7474. /**
  7475. * Test that required fields are created for various types of validation.
  7476. *
  7477. * @return void
  7478. */
  7479. public function testFormInputRequiredDetection() {
  7480. $this->Form->create('Contact');
  7481. $result = $this->Form->input('Contact.non_existing');
  7482. $expected = array(
  7483. 'div' => array('class' => 'input text'),
  7484. 'label' => array('for' => 'ContactNonExisting'),
  7485. 'Non Existing',
  7486. '/label',
  7487. 'input' => array(
  7488. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7489. 'id' => 'ContactNonExisting'
  7490. ),
  7491. '/div'
  7492. );
  7493. $this->assertTags($result, $expected);
  7494. $result = $this->Form->input('Contact.imrequired');
  7495. $expected = array(
  7496. 'div' => array('class' => 'input text required'),
  7497. 'label' => array('for' => 'ContactImrequired'),
  7498. 'Imrequired',
  7499. '/label',
  7500. 'input' => array(
  7501. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7502. 'id' => 'ContactImrequired',
  7503. 'required' => 'required'
  7504. ),
  7505. '/div'
  7506. );
  7507. $this->assertTags($result, $expected);
  7508. $result = $this->Form->input('Contact.imalsorequired');
  7509. $expected = array(
  7510. 'div' => array('class' => 'input text required'),
  7511. 'label' => array('for' => 'ContactImalsorequired'),
  7512. 'Imalsorequired',
  7513. '/label',
  7514. 'input' => array(
  7515. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7516. 'id' => 'ContactImalsorequired',
  7517. 'required' => 'required'
  7518. ),
  7519. '/div'
  7520. );
  7521. $this->assertTags($result, $expected);
  7522. $result = $this->Form->input('Contact.imrequiredtoo');
  7523. $expected = array(
  7524. 'div' => array('class' => 'input text required'),
  7525. 'label' => array('for' => 'ContactImrequiredtoo'),
  7526. 'Imrequiredtoo',
  7527. '/label',
  7528. 'input' => array(
  7529. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7530. 'id' => 'ContactImrequiredtoo',
  7531. 'required' => 'required'
  7532. ),
  7533. '/div'
  7534. );
  7535. $this->assertTags($result, $expected);
  7536. $result = $this->Form->input('Contact.required_one');
  7537. $expected = array(
  7538. 'div' => array('class' => 'input text required'),
  7539. 'label' => array('for' => 'ContactRequiredOne'),
  7540. 'Required One',
  7541. '/label',
  7542. 'input' => array(
  7543. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7544. 'id' => 'ContactRequiredOne',
  7545. 'required' => 'required'
  7546. ),
  7547. '/div'
  7548. );
  7549. $this->assertTags($result, $expected);
  7550. $result = $this->Form->input('Contact.string_required');
  7551. $expected = array(
  7552. 'div' => array('class' => 'input text required'),
  7553. 'label' => array('for' => 'ContactStringRequired'),
  7554. 'String Required',
  7555. '/label',
  7556. 'input' => array(
  7557. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7558. 'id' => 'ContactStringRequired',
  7559. 'required' => 'required'
  7560. ),
  7561. '/div'
  7562. );
  7563. $this->assertTags($result, $expected);
  7564. $result = $this->Form->input('Contact.imnotrequired');
  7565. $expected = array(
  7566. 'div' => array('class' => 'input text'),
  7567. 'label' => array('for' => 'ContactImnotrequired'),
  7568. 'Imnotrequired',
  7569. '/label',
  7570. 'input' => array(
  7571. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7572. 'id' => 'ContactImnotrequired'
  7573. ),
  7574. '/div'
  7575. );
  7576. $this->assertTags($result, $expected);
  7577. $result = $this->Form->input('Contact.imalsonotrequired');
  7578. $expected = array(
  7579. 'div' => array('class' => 'input text'),
  7580. 'label' => array('for' => 'ContactImalsonotrequired'),
  7581. 'Imalsonotrequired',
  7582. '/label',
  7583. 'input' => array(
  7584. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7585. 'id' => 'ContactImalsonotrequired'
  7586. ),
  7587. '/div'
  7588. );
  7589. $this->assertTags($result, $expected);
  7590. $result = $this->Form->input('Contact.imalsonotrequired2');
  7591. $expected = array(
  7592. 'div' => array('class' => 'input text'),
  7593. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7594. 'Imalsonotrequired2',
  7595. '/label',
  7596. 'input' => array(
  7597. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7598. 'id' => 'ContactImalsonotrequired2'
  7599. ),
  7600. '/div'
  7601. );
  7602. $this->assertTags($result, $expected);
  7603. $result = $this->Form->input('Contact.imnotrequiredeither');
  7604. $expected = array(
  7605. 'div' => array('class' => 'input text'),
  7606. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7607. 'Imnotrequiredeither',
  7608. '/label',
  7609. 'input' => array(
  7610. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7611. 'id' => 'ContactImnotrequiredeither'
  7612. ),
  7613. '/div'
  7614. );
  7615. $this->assertTags($result, $expected);
  7616. $result = $this->Form->input('Contact.iamrequiredalways');
  7617. $expected = array(
  7618. 'div' => array('class' => 'input text required'),
  7619. 'label' => array('for' => 'ContactIamrequiredalways'),
  7620. 'Iamrequiredalways',
  7621. '/label',
  7622. 'input' => array(
  7623. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7624. 'id' => 'ContactIamrequiredalways',
  7625. 'required' => 'required'
  7626. ),
  7627. '/div'
  7628. );
  7629. $this->assertTags($result, $expected);
  7630. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7631. $expected = array(
  7632. 'div' => array('class' => 'input checkbox required'),
  7633. array('input' => array(
  7634. 'type' => 'hidden',
  7635. 'name' => 'data[Contact][boolean_field]',
  7636. 'id' => 'ContactBooleanField_',
  7637. 'value' => '0'
  7638. )),
  7639. array('input' => array(
  7640. 'type' => 'checkbox',
  7641. 'name' => 'data[Contact][boolean_field]',
  7642. 'value' => '1',
  7643. 'id' => 'ContactBooleanField'
  7644. )),
  7645. 'label' => array('for' => 'ContactBooleanField'),
  7646. 'Boolean Field',
  7647. '/label',
  7648. '/div'
  7649. );
  7650. $this->assertTags($result, $expected);
  7651. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7652. $expected = array(
  7653. 'div' => array('class' => 'input checkbox required'),
  7654. array('input' => array(
  7655. 'type' => 'hidden',
  7656. 'name' => 'data[Contact][boolean_field]',
  7657. 'id' => 'ContactBooleanField_',
  7658. 'value' => '0'
  7659. )),
  7660. array('input' => array(
  7661. 'type' => 'checkbox',
  7662. 'name' => 'data[Contact][boolean_field]',
  7663. 'value' => '1',
  7664. 'id' => 'ContactBooleanField',
  7665. 'required' => 'required'
  7666. )),
  7667. 'label' => array('for' => 'ContactBooleanField'),
  7668. 'Boolean Field',
  7669. '/label',
  7670. '/div'
  7671. );
  7672. $this->assertTags($result, $expected);
  7673. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7674. $expected = array(
  7675. 'div' => array('class' => 'input file required'),
  7676. 'label' => array('for' => 'ContactIamrequiredalways'),
  7677. 'Iamrequiredalways',
  7678. '/label',
  7679. 'input' => array(
  7680. 'type' => 'file',
  7681. 'name' => 'data[Contact][iamrequiredalways]',
  7682. 'id' => 'ContactIamrequiredalways',
  7683. 'required' => 'required'
  7684. ),
  7685. '/div'
  7686. );
  7687. $this->assertTags($result, $expected);
  7688. }
  7689. /**
  7690. * Test that required fields are created when only using ModelValidator::add().
  7691. *
  7692. * @return void
  7693. */
  7694. public function testFormInputRequiredDetectionModelValidator() {
  7695. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7696. $this->Form->create('ContactTag');
  7697. $result = $this->Form->input('ContactTag.iwillberequired');
  7698. $expected = array(
  7699. 'div' => array('class' => 'input text required'),
  7700. 'label' => array('for' => 'ContactTagIwillberequired'),
  7701. 'Iwillberequired',
  7702. '/label',
  7703. 'input' => array(
  7704. 'name' => 'data[ContactTag][iwillberequired]',
  7705. 'type' => 'text',
  7706. 'id' => 'ContactTagIwillberequired',
  7707. 'required' => 'required'
  7708. ),
  7709. '/div'
  7710. );
  7711. $this->assertTags($result, $expected);
  7712. }
  7713. /**
  7714. * testFormMagicInput method
  7715. *
  7716. * @return void
  7717. */
  7718. public function testFormMagicInput() {
  7719. $encoding = strtolower(Configure::read('App.encoding'));
  7720. $result = $this->Form->create('Contact');
  7721. $expected = array(
  7722. 'form' => array(
  7723. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7724. 'accept-charset' => $encoding
  7725. ),
  7726. 'div' => array('style' => 'display:none;'),
  7727. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7728. '/div'
  7729. );
  7730. $this->assertTags($result, $expected);
  7731. $result = $this->Form->input('name');
  7732. $expected = array(
  7733. 'div' => array('class' => 'input text'),
  7734. 'label' => array('for' => 'ContactName'),
  7735. 'Name',
  7736. '/label',
  7737. 'input' => array(
  7738. 'type' => 'text', 'name' => 'data[Contact][name]',
  7739. 'id' => 'ContactName', 'maxlength' => '255'
  7740. ),
  7741. '/div'
  7742. );
  7743. $this->assertTags($result, $expected);
  7744. $result = $this->Form->input('non_existing_field_in_contact_model');
  7745. $expected = array(
  7746. 'div' => array('class' => 'input text'),
  7747. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7748. 'Non Existing Field In Contact Model',
  7749. '/label',
  7750. 'input' => array(
  7751. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7752. 'id' => 'ContactNonExistingFieldInContactModel'
  7753. ),
  7754. '/div'
  7755. );
  7756. $this->assertTags($result, $expected);
  7757. $result = $this->Form->input('Address.street');
  7758. $expected = array(
  7759. 'div' => array('class' => 'input text'),
  7760. 'label' => array('for' => 'AddressStreet'),
  7761. 'Street',
  7762. '/label',
  7763. 'input' => array(
  7764. 'type' => 'text', 'name' => 'data[Address][street]',
  7765. 'id' => 'AddressStreet'
  7766. ),
  7767. '/div'
  7768. );
  7769. $this->assertTags($result, $expected);
  7770. $result = $this->Form->input('Address.non_existing_field_in_model');
  7771. $expected = array(
  7772. 'div' => array('class' => 'input text'),
  7773. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7774. 'Non Existing Field In Model',
  7775. '/label',
  7776. 'input' => array(
  7777. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7778. 'id' => 'AddressNonExistingFieldInModel'
  7779. ),
  7780. '/div'
  7781. );
  7782. $this->assertTags($result, $expected);
  7783. $result = $this->Form->input('name', array('div' => false));
  7784. $expected = array(
  7785. 'label' => array('for' => 'ContactName'),
  7786. 'Name',
  7787. '/label',
  7788. 'input' => array(
  7789. 'type' => 'text', 'name' => 'data[Contact][name]',
  7790. 'id' => 'ContactName', 'maxlength' => '255'
  7791. )
  7792. );
  7793. $this->assertTags($result, $expected);
  7794. extract($this->dateRegex);
  7795. $now = strtotime('now');
  7796. $result = $this->Form->input('Contact.published', array('div' => false));
  7797. $expected = array(
  7798. 'label' => array('for' => 'ContactPublishedMonth'),
  7799. 'Published',
  7800. '/label',
  7801. array('select' => array(
  7802. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7803. )),
  7804. $monthsRegex,
  7805. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7806. date('F', $now),
  7807. '/option',
  7808. '*/select',
  7809. '-',
  7810. array('select' => array(
  7811. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7812. )),
  7813. $daysRegex,
  7814. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7815. date('j', $now),
  7816. '/option',
  7817. '*/select',
  7818. '-',
  7819. array('select' => array(
  7820. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7821. )),
  7822. $yearsRegex,
  7823. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7824. date('Y', $now),
  7825. '*/select'
  7826. );
  7827. $this->assertTags($result, $expected);
  7828. $result = $this->Form->input('Contact.updated', array('div' => false));
  7829. $expected = array(
  7830. 'label' => array('for' => 'ContactUpdatedMonth'),
  7831. 'Updated',
  7832. '/label',
  7833. array('select' => array(
  7834. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7835. )),
  7836. $monthsRegex,
  7837. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7838. date('F', $now),
  7839. '/option',
  7840. '*/select',
  7841. '-',
  7842. array('select' => array(
  7843. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7844. )),
  7845. $daysRegex,
  7846. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7847. date('j', $now),
  7848. '/option',
  7849. '*/select',
  7850. '-',
  7851. array('select' => array(
  7852. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7853. )),
  7854. $yearsRegex,
  7855. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7856. date('Y', $now),
  7857. '*/select'
  7858. );
  7859. $this->assertTags($result, $expected);
  7860. $result = $this->Form->input('UserForm.stuff');
  7861. $expected = array(
  7862. 'div' => array('class' => 'input text'),
  7863. 'label' => array('for' => 'UserFormStuff'),
  7864. 'Stuff',
  7865. '/label',
  7866. 'input' => array(
  7867. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7868. 'id' => 'UserFormStuff', 'maxlength' => 10
  7869. ),
  7870. '/div'
  7871. );
  7872. $this->assertTags($result, $expected);
  7873. }
  7874. /**
  7875. * testForMagicInputNonExistingNorValidated method
  7876. *
  7877. * @return void
  7878. */
  7879. public function testForMagicInputNonExistingNorValidated() {
  7880. $encoding = strtolower(Configure::read('App.encoding'));
  7881. $result = $this->Form->create('Contact');
  7882. $expected = array(
  7883. 'form' => array(
  7884. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7885. 'accept-charset' => $encoding
  7886. ),
  7887. 'div' => array('style' => 'display:none;'),
  7888. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7889. '/div'
  7890. );
  7891. $this->assertTags($result, $expected);
  7892. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7893. $expected = array(
  7894. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7895. 'Non Existing Nor Validated',
  7896. '/label',
  7897. 'input' => array(
  7898. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7899. 'id' => 'ContactNonExistingNorValidated'
  7900. )
  7901. );
  7902. $this->assertTags($result, $expected);
  7903. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7904. 'div' => false, 'value' => 'my value'
  7905. ));
  7906. $expected = array(
  7907. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7908. 'Non Existing Nor Validated',
  7909. '/label',
  7910. 'input' => array(
  7911. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7912. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7913. )
  7914. );
  7915. $this->assertTags($result, $expected);
  7916. $this->Form->request->data = array(
  7917. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7918. ));
  7919. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7920. $expected = array(
  7921. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7922. 'Non Existing Nor Validated',
  7923. '/label',
  7924. 'input' => array(
  7925. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7926. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7927. )
  7928. );
  7929. $this->assertTags($result, $expected);
  7930. }
  7931. /**
  7932. * testFormMagicInputLabel method
  7933. *
  7934. * @return void
  7935. */
  7936. public function testFormMagicInputLabel() {
  7937. $encoding = strtolower(Configure::read('App.encoding'));
  7938. $result = $this->Form->create('Contact');
  7939. $expected = array(
  7940. 'form' => array(
  7941. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7942. 'accept-charset' => $encoding
  7943. ),
  7944. 'div' => array('style' => 'display:none;'),
  7945. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7946. '/div'
  7947. );
  7948. $this->assertTags($result, $expected);
  7949. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7950. $this->assertTags($result, array('input' => array(
  7951. 'name' => 'data[Contact][name]', 'type' => 'text',
  7952. 'id' => 'ContactName', 'maxlength' => '255')
  7953. ));
  7954. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7955. $expected = array(
  7956. 'label' => array('for' => 'ContactName'),
  7957. 'My label',
  7958. '/label',
  7959. 'input' => array(
  7960. 'type' => 'text', 'name' => 'data[Contact][name]',
  7961. 'id' => 'ContactName', 'maxlength' => '255'
  7962. )
  7963. );
  7964. $this->assertTags($result, $expected);
  7965. $result = $this->Form->input('Contact.name', array(
  7966. 'div' => false, 'label' => array('class' => 'mandatory')
  7967. ));
  7968. $expected = array(
  7969. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7970. 'Name',
  7971. '/label',
  7972. 'input' => array(
  7973. 'type' => 'text', 'name' => 'data[Contact][name]',
  7974. 'id' => 'ContactName', 'maxlength' => '255'
  7975. )
  7976. );
  7977. $this->assertTags($result, $expected);
  7978. $result = $this->Form->input('Contact.name', array(
  7979. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7980. ));
  7981. $expected = array(
  7982. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7983. 'My label',
  7984. '/label',
  7985. 'input' => array(
  7986. 'type' => 'text', 'name' => 'data[Contact][name]',
  7987. 'id' => 'ContactName', 'maxlength' => '255'
  7988. )
  7989. );
  7990. $this->assertTags($result, $expected);
  7991. $result = $this->Form->input('Contact.name', array(
  7992. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7993. ));
  7994. $expected = array(
  7995. 'label' => array('for' => 'my_id'),
  7996. 'Name',
  7997. '/label',
  7998. 'input' => array(
  7999. 'type' => 'text', 'name' => 'data[Contact][name]',
  8000. 'id' => 'my_id', 'maxlength' => '255'
  8001. )
  8002. );
  8003. $this->assertTags($result, $expected);
  8004. $result = $this->Form->input('1.id');
  8005. $this->assertTags($result, array('input' => array(
  8006. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8007. 'id' => 'Contact1Id'
  8008. )));
  8009. $result = $this->Form->input("1.name");
  8010. $expected = array(
  8011. 'div' => array('class' => 'input text'),
  8012. 'label' => array('for' => 'Contact1Name'),
  8013. 'Name',
  8014. '/label',
  8015. 'input' => array(
  8016. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8017. 'id' => 'Contact1Name', 'maxlength' => '255'
  8018. ),
  8019. '/div'
  8020. );
  8021. $this->assertTags($result, $expected);
  8022. $result = $this->Form->input('Contact.1.id');
  8023. $this->assertTags($result, array(
  8024. 'input' => array(
  8025. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8026. 'id' => 'Contact1Id'
  8027. )
  8028. ));
  8029. $result = $this->Form->input("Model.1.name");
  8030. $expected = array(
  8031. 'div' => array('class' => 'input text'),
  8032. 'label' => array('for' => 'Model1Name'),
  8033. 'Name',
  8034. '/label',
  8035. 'input' => array(
  8036. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8037. 'id' => 'Model1Name'
  8038. ),
  8039. '/div'
  8040. );
  8041. $this->assertTags($result, $expected);
  8042. }
  8043. /**
  8044. * testFormEnd method
  8045. *
  8046. * @return void
  8047. */
  8048. public function testFormEnd() {
  8049. $this->assertEquals('</form>', $this->Form->end());
  8050. $result = $this->Form->end('');
  8051. $expected = array(
  8052. 'div' => array('class' => 'submit'),
  8053. 'input' => array('type' => 'submit', 'value' => ''),
  8054. '/div',
  8055. '/form'
  8056. );
  8057. $this->assertTags($result, $expected);
  8058. $result = $this->Form->end(array('label' => ''));
  8059. $expected = array(
  8060. 'div' => array('class' => 'submit'),
  8061. 'input' => array('type' => 'submit', 'value' => ''),
  8062. '/div',
  8063. '/form'
  8064. );
  8065. $this->assertTags($result, $expected);
  8066. $result = $this->Form->end('save');
  8067. $expected = array(
  8068. 'div' => array('class' => 'submit'),
  8069. 'input' => array('type' => 'submit', 'value' => 'save'),
  8070. '/div',
  8071. '/form'
  8072. );
  8073. $this->assertTags($result, $expected);
  8074. $result = $this->Form->end(array('label' => 'save'));
  8075. $expected = array(
  8076. 'div' => array('class' => 'submit'),
  8077. 'input' => array('type' => 'submit', 'value' => 'save'),
  8078. '/div',
  8079. '/form'
  8080. );
  8081. $this->assertTags($result, $expected);
  8082. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8083. $expected = array(
  8084. 'div' => array('class' => 'submit'),
  8085. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8086. '/div',
  8087. '/form'
  8088. );
  8089. $this->assertTags($result, $expected);
  8090. $result = $this->Form->end(array('name' => 'Whatever'));
  8091. $expected = array(
  8092. 'div' => array('class' => 'submit'),
  8093. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8094. '/div',
  8095. '/form'
  8096. );
  8097. $this->assertTags($result, $expected);
  8098. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8099. $expected = array(
  8100. 'div' => array('class' => 'good'),
  8101. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8102. '/div',
  8103. '/form'
  8104. );
  8105. $this->assertTags($result, $expected);
  8106. $result = $this->Form->end(array(
  8107. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8108. ));
  8109. $expected = array(
  8110. 'div' => array('class' => 'good'),
  8111. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8112. '/div',
  8113. '/form'
  8114. );
  8115. $this->assertTags($result, $expected);
  8116. }
  8117. /**
  8118. * testMultipleFormWithIdFields method
  8119. *
  8120. * @return void
  8121. */
  8122. public function testMultipleFormWithIdFields() {
  8123. $this->Form->create('UserForm');
  8124. $result = $this->Form->input('id');
  8125. $this->assertTags($result, array('input' => array(
  8126. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8127. )));
  8128. $result = $this->Form->input('ValidateItem.id');
  8129. $this->assertTags($result, array('input' => array(
  8130. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8131. 'id' => 'ValidateItemId'
  8132. )));
  8133. $result = $this->Form->input('ValidateUser.id');
  8134. $this->assertTags($result, array('input' => array(
  8135. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8136. 'id' => 'ValidateUserId'
  8137. )));
  8138. }
  8139. /**
  8140. * testDbLessModel method
  8141. *
  8142. * @return void
  8143. */
  8144. public function testDbLessModel() {
  8145. $this->Form->create('TestMail');
  8146. $result = $this->Form->input('name');
  8147. $expected = array(
  8148. 'div' => array('class' => 'input text'),
  8149. 'label' => array('for' => 'TestMailName'),
  8150. 'Name',
  8151. '/label',
  8152. 'input' => array(
  8153. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8154. 'id' => 'TestMailName'
  8155. ),
  8156. '/div'
  8157. );
  8158. $this->assertTags($result, $expected);
  8159. ClassRegistry::init('TestMail');
  8160. $this->Form->create('TestMail');
  8161. $result = $this->Form->input('name');
  8162. $expected = array(
  8163. 'div' => array('class' => 'input text'),
  8164. 'label' => array('for' => 'TestMailName'),
  8165. 'Name',
  8166. '/label',
  8167. 'input' => array(
  8168. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8169. 'id' => 'TestMailName'
  8170. ),
  8171. '/div'
  8172. );
  8173. $this->assertTags($result, $expected);
  8174. }
  8175. /**
  8176. * testBrokenness method
  8177. *
  8178. * @return void
  8179. */
  8180. public function testBrokenness() {
  8181. /*
  8182. * #4 This test has two parents and four children. By default (as of r7117) both
  8183. * parents are show but the first parent is missing a child. This is the inconsistency
  8184. * in the default behaviour - one parent has all children, the other does not - dependent
  8185. * on the data values.
  8186. */
  8187. $result = $this->Form->select('Model.field', array(
  8188. 'Fred' => array(
  8189. 'freds_son_1' => 'Fred',
  8190. 'freds_son_2' => 'Freddie'
  8191. ),
  8192. 'Bert' => array(
  8193. 'berts_son_1' => 'Albert',
  8194. 'berts_son_2' => 'Bertie')
  8195. ),
  8196. array('showParents' => true, 'empty' => false)
  8197. );
  8198. $expected = array(
  8199. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8200. array('optgroup' => array('label' => 'Fred')),
  8201. array('option' => array('value' => 'freds_son_1')),
  8202. 'Fred',
  8203. '/option',
  8204. array('option' => array('value' => 'freds_son_2')),
  8205. 'Freddie',
  8206. '/option',
  8207. '/optgroup',
  8208. array('optgroup' => array('label' => 'Bert')),
  8209. array('option' => array('value' => 'berts_son_1')),
  8210. 'Albert',
  8211. '/option',
  8212. array('option' => array('value' => 'berts_son_2')),
  8213. 'Bertie',
  8214. '/option',
  8215. '/optgroup',
  8216. '/select'
  8217. );
  8218. $this->assertTags($result, $expected);
  8219. /*
  8220. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8221. * changed, so we should expect the same select list data, just with a different name
  8222. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8223. * This is where data corruption can occur, because when a select value is missing from
  8224. * a list a form will substitute the first value in the list - without the user knowing.
  8225. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8226. * affect the availability of 3 => 'Three' as a valid option.
  8227. */
  8228. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8229. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8230. ));
  8231. $result = $this->Form->select(
  8232. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8233. );
  8234. $expected = array(
  8235. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8236. array('option' => array('value' => 1)),
  8237. 'One',
  8238. '/option',
  8239. array('option' => array('value' => 2)),
  8240. 'Two',
  8241. '/option',
  8242. array('optgroup' => array('label' => 'Three')),
  8243. array('option' => array('value' => 3)),
  8244. 'Three',
  8245. '/option',
  8246. array('option' => array('value' => 4)),
  8247. 'Four',
  8248. '/option',
  8249. array('option' => array('value' => 5)),
  8250. 'Five',
  8251. '/option',
  8252. '/optgroup',
  8253. '/select'
  8254. );
  8255. $this->assertTags($result, $expected);
  8256. }
  8257. /**
  8258. * Test the generation of fields for a multi record form.
  8259. *
  8260. * @return void
  8261. */
  8262. public function testMultiRecordForm() {
  8263. $this->Form->create('ValidateProfile');
  8264. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8265. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8266. $expected = array(
  8267. 'div' => array('class' => 'input textarea'),
  8268. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8269. 'Name',
  8270. '/label',
  8271. 'textarea' => array(
  8272. 'id' => 'ValidateProfile1ValidateItem2Name',
  8273. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8274. 'cols' => 30,
  8275. 'rows' => 6
  8276. ),
  8277. 'Value',
  8278. '/textarea',
  8279. '/div'
  8280. );
  8281. $this->assertTags($result, $expected);
  8282. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8283. $expected = array(
  8284. 'div' => array('class' => 'input date'),
  8285. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8286. 'Created',
  8287. '/label',
  8288. array('select' => array(
  8289. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8290. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8291. )
  8292. ),
  8293. array('option' => array('value' => '')), '/option',
  8294. $this->dateRegex['monthsRegex'],
  8295. '/select', '-',
  8296. array('select' => array(
  8297. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8298. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8299. )
  8300. ),
  8301. array('option' => array('value' => '')), '/option',
  8302. $this->dateRegex['daysRegex'],
  8303. '/select', '-',
  8304. array('select' => array(
  8305. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8306. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8307. )
  8308. ),
  8309. array('option' => array('value' => '')), '/option',
  8310. $this->dateRegex['yearsRegex'],
  8311. '/select',
  8312. '/div'
  8313. );
  8314. $this->assertTags($result, $expected);
  8315. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8316. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8317. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8318. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8319. $expected = array(
  8320. 'div' => array('class' => 'input select error'),
  8321. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8322. 'Profile',
  8323. '/label',
  8324. 'select' => array(
  8325. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8326. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8327. 'class' => 'form-error'
  8328. ),
  8329. '/select',
  8330. array('div' => array('class' => 'error-message')),
  8331. 'Error',
  8332. '/div',
  8333. '/div'
  8334. );
  8335. $this->assertTags($result, $expected);
  8336. }
  8337. /**
  8338. * test the correct display of multi-record form validation errors.
  8339. *
  8340. * @return void
  8341. */
  8342. public function testMultiRecordFormValidationErrors() {
  8343. $this->Form->create('ValidateProfile');
  8344. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8345. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8346. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8347. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8348. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8349. $result = $this->Form->error('ValidateProfile.2.city');
  8350. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8351. $result = $this->Form->error('2.city');
  8352. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8353. }
  8354. /**
  8355. * test the correct display of multi-record form validation errors.
  8356. *
  8357. * @return void
  8358. */
  8359. public function testSaveManyRecordFormValidationErrors() {
  8360. $this->Form->create('ValidateUser');
  8361. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8362. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8363. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8364. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8365. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8366. $result = $this->Form->error('ValidateUser.0.city');
  8367. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8368. }
  8369. /**
  8370. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8371. *
  8372. * @return void
  8373. */
  8374. public function testInputTemplate() {
  8375. $result = $this->Form->input('Contact.email', array(
  8376. 'type' => 'text', 'format' => array('input')
  8377. ));
  8378. $expected = array(
  8379. 'div' => array('class' => 'input text'),
  8380. 'input' => array(
  8381. 'type' => 'text', 'name' => 'data[Contact][email]',
  8382. 'id' => 'ContactEmail'
  8383. ),
  8384. '/div'
  8385. );
  8386. $this->assertTags($result, $expected);
  8387. $result = $this->Form->input('Contact.email', array(
  8388. 'type' => 'text', 'format' => array('input', 'label'),
  8389. 'label' => '<em>Email (required)</em>'
  8390. ));
  8391. $expected = array(
  8392. 'div' => array('class' => 'input text'),
  8393. array('input' => array(
  8394. 'type' => 'text', 'name' => 'data[Contact][email]',
  8395. 'id' => 'ContactEmail'
  8396. )),
  8397. 'label' => array('for' => 'ContactEmail'),
  8398. 'em' => array(),
  8399. 'Email (required)',
  8400. '/em',
  8401. '/label',
  8402. '/div'
  8403. );
  8404. $this->assertTags($result, $expected);
  8405. $result = $this->Form->input('Contact.email', array(
  8406. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8407. 'between' => '<div>Something in the middle</div>',
  8408. 'after' => '<span>Some text at the end</span>'
  8409. ));
  8410. $expected = array(
  8411. 'div' => array('class' => 'input text'),
  8412. array('input' => array(
  8413. 'type' => 'text', 'name' => 'data[Contact][email]',
  8414. 'id' => 'ContactEmail'
  8415. )),
  8416. array('div' => array()),
  8417. 'Something in the middle',
  8418. '/div',
  8419. 'label' => array('for' => 'ContactEmail'),
  8420. 'Email',
  8421. '/label',
  8422. 'span' => array(),
  8423. 'Some text at the end',
  8424. '/span',
  8425. '/div'
  8426. );
  8427. $this->assertTags($result, $expected);
  8428. $result = $this->Form->input('Contact.method', array(
  8429. 'type' => 'radio',
  8430. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8431. 'between' => 'I am between',
  8432. ));
  8433. $expected = array(
  8434. 'div' => array('class' => 'input radio'),
  8435. 'fieldset' => array(),
  8436. 'legend' => array(),
  8437. 'Method',
  8438. '/legend',
  8439. 'I am between',
  8440. 'input' => array(
  8441. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8442. 'value' => '', 'id' => 'ContactMethod_'
  8443. ),
  8444. array('input' => array(
  8445. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8446. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8447. )),
  8448. array('label' => array('for' => 'ContactMethodEmail')),
  8449. 'Email',
  8450. '/label',
  8451. array('input' => array(
  8452. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8453. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8454. )),
  8455. array('label' => array('for' => 'ContactMethodPigeon')),
  8456. 'Pigeon',
  8457. '/label',
  8458. '/fieldset',
  8459. '/div',
  8460. );
  8461. $this->assertTags($result, $expected);
  8462. }
  8463. /**
  8464. * test that some html5 inputs + FormHelper::__call() work
  8465. *
  8466. * @return void
  8467. */
  8468. public function testHtml5Inputs() {
  8469. $result = $this->Form->email('User.email');
  8470. $expected = array(
  8471. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8472. );
  8473. $this->assertTags($result, $expected);
  8474. $result = $this->Form->search('User.query');
  8475. $expected = array(
  8476. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8477. );
  8478. $this->assertTags($result, $expected);
  8479. $result = $this->Form->search('User.query', array('value' => 'test'));
  8480. $expected = array(
  8481. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8482. );
  8483. $this->assertTags($result, $expected);
  8484. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8485. $expected = array(
  8486. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8487. );
  8488. $this->assertTags($result, $expected);
  8489. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8490. $expected = array(
  8491. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8492. );
  8493. $this->assertTags($result, $expected);
  8494. }
  8495. /**
  8496. *
  8497. * @expectedException CakeException
  8498. * @return void
  8499. */
  8500. public function testHtml5InputException() {
  8501. $this->Form->email();
  8502. }
  8503. /**
  8504. * Tests that a model can be loaded from the model names passed in the request object
  8505. *
  8506. * @return void
  8507. */
  8508. public function testIntrospectModelFromRequest() {
  8509. $this->loadFixtures('Post');
  8510. App::build(array(
  8511. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8512. ));
  8513. CakePlugin::load('TestPlugin');
  8514. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8515. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8516. $this->Form->create('TestPluginPost');
  8517. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8518. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8519. CakePlugin::unload();
  8520. App::build();
  8521. }
  8522. /**
  8523. * Tests that it is possible to set the validation errors directly in the helper for a field
  8524. *
  8525. * @return void
  8526. */
  8527. public function testCustomValidationErrors() {
  8528. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8529. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8530. $this->assertEquals('Badness!', $result);
  8531. }
  8532. /**
  8533. * Tests that the 'on' key validates as expected on create
  8534. *
  8535. * @return void
  8536. */
  8537. public function testRequiredOnCreate() {
  8538. $this->Form->create('Contact');
  8539. $result = $this->Form->input('Contact.imrequiredonupdate');
  8540. $expected = array(
  8541. 'div' => array('class' => 'input text'),
  8542. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8543. 'Imrequiredonupdate',
  8544. '/label',
  8545. 'input' => array(
  8546. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8547. 'id' => 'ContactImrequiredonupdate'
  8548. ),
  8549. '/div'
  8550. );
  8551. $this->assertTags($result, $expected);
  8552. $result = $this->Form->input('Contact.imrequiredoncreate');
  8553. $expected = array(
  8554. 'div' => array('class' => 'input text required'),
  8555. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8556. 'Imrequiredoncreate',
  8557. '/label',
  8558. 'input' => array(
  8559. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8560. 'id' => 'ContactImrequiredoncreate',
  8561. 'required' => 'required'
  8562. ),
  8563. '/div'
  8564. );
  8565. $this->assertTags($result, $expected);
  8566. $result = $this->Form->input('Contact.imrequiredonboth');
  8567. $expected = array(
  8568. 'div' => array('class' => 'input text required'),
  8569. 'label' => array('for' => 'ContactImrequiredonboth'),
  8570. 'Imrequiredonboth',
  8571. '/label',
  8572. 'input' => array(
  8573. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8574. 'id' => 'ContactImrequiredonboth',
  8575. 'required' => 'required'
  8576. ),
  8577. '/div'
  8578. );
  8579. $this->assertTags($result, $expected);
  8580. $this->Form->inputDefaults(array('required' => false));
  8581. $result = $this->Form->input('Contact.imrequired');
  8582. $expected = array(
  8583. 'div' => array('class' => 'input text'),
  8584. 'label' => array('for' => 'ContactImrequired'),
  8585. 'Imrequired',
  8586. '/label',
  8587. 'input' => array(
  8588. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8589. 'id' => 'ContactImrequired'
  8590. ),
  8591. '/div'
  8592. );
  8593. $this->assertTags($result, $expected);
  8594. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8595. $this->assertTags($result, $expected);
  8596. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8597. $expected = array(
  8598. 'div' => array('class' => 'input text required'),
  8599. 'label' => array('for' => 'ContactImrequired'),
  8600. 'Imrequired',
  8601. '/label',
  8602. 'input' => array(
  8603. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8604. 'id' => 'ContactImrequired'
  8605. ),
  8606. '/div'
  8607. );
  8608. $this->assertTags($result, $expected);
  8609. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8610. $this->assertTags($result, $expected);
  8611. }
  8612. /**
  8613. * Tests that the 'on' key validates as expected on update
  8614. *
  8615. * @return void
  8616. */
  8617. public function testRequiredOnUpdate() {
  8618. $this->Form->request->data['Contact']['id'] = 1;
  8619. $this->Form->create('Contact');
  8620. $result = $this->Form->input('Contact.imrequiredonupdate');
  8621. $expected = array(
  8622. 'div' => array('class' => 'input text required'),
  8623. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8624. 'Imrequiredonupdate',
  8625. '/label',
  8626. 'input' => array(
  8627. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8628. 'id' => 'ContactImrequiredonupdate',
  8629. 'required' => 'required'
  8630. ),
  8631. '/div'
  8632. );
  8633. $this->assertTags($result, $expected);
  8634. $result = $this->Form->input('Contact.imrequiredoncreate');
  8635. $expected = array(
  8636. 'div' => array('class' => 'input text'),
  8637. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8638. 'Imrequiredoncreate',
  8639. '/label',
  8640. 'input' => array(
  8641. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8642. 'id' => 'ContactImrequiredoncreate'
  8643. ),
  8644. '/div'
  8645. );
  8646. $this->assertTags($result, $expected);
  8647. $result = $this->Form->input('Contact.imrequiredonboth');
  8648. $expected = array(
  8649. 'div' => array('class' => 'input text required'),
  8650. 'label' => array('for' => 'ContactImrequiredonboth'),
  8651. 'Imrequiredonboth',
  8652. '/label',
  8653. 'input' => array(
  8654. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8655. 'id' => 'ContactImrequiredonboth',
  8656. 'required' => 'required'
  8657. ),
  8658. '/div'
  8659. );
  8660. $this->assertTags($result, $expected);
  8661. $result = $this->Form->input('Contact.imrequired');
  8662. $expected = array(
  8663. 'div' => array('class' => 'input text required'),
  8664. 'label' => array('for' => 'ContactImrequired'),
  8665. 'Imrequired',
  8666. '/label',
  8667. 'input' => array(
  8668. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8669. 'id' => 'ContactImrequired',
  8670. 'required' => 'required'
  8671. ),
  8672. '/div'
  8673. );
  8674. $this->assertTags($result, $expected);
  8675. }
  8676. /**
  8677. * Test inputDefaults setter and getter
  8678. *
  8679. * @return void
  8680. */
  8681. public function testInputDefaults() {
  8682. $this->Form->create('Contact');
  8683. $this->Form->inputDefaults(array(
  8684. 'label' => false,
  8685. 'div' => array(
  8686. 'style' => 'color: #000;'
  8687. )
  8688. ));
  8689. $result = $this->Form->input('Contact.field1');
  8690. $expected = array(
  8691. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8692. 'input' => array(
  8693. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8694. 'id' => 'ContactField1'
  8695. ),
  8696. '/div'
  8697. );
  8698. $this->assertTags($result, $expected);
  8699. $this->Form->inputDefaults(array(
  8700. 'div' => false,
  8701. 'label' => 'Label',
  8702. ));
  8703. $result = $this->Form->input('Contact.field1');
  8704. $expected = array(
  8705. 'label' => array('for' => 'ContactField1'),
  8706. 'Label',
  8707. '/label',
  8708. 'input' => array(
  8709. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8710. 'id' => 'ContactField1'
  8711. ),
  8712. );
  8713. $this->assertTags($result, $expected);
  8714. $this->Form->inputDefaults(array(
  8715. 'label' => false,
  8716. ), true);
  8717. $result = $this->Form->input('Contact.field1');
  8718. $expected = array(
  8719. 'input' => array(
  8720. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8721. 'id' => 'ContactField1'
  8722. ),
  8723. );
  8724. $this->assertTags($result, $expected);
  8725. $result = $this->Form->inputDefaults();
  8726. $expected = array(
  8727. 'div' => false,
  8728. 'label' => false,
  8729. );
  8730. $this->assertEquals($expected, $result);
  8731. }
  8732. }