FormHelperTest.php 300 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package Cake.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var boolean
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var boolean
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var boolean
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var boolean
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var boolean
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var boolean
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $this->Form->request['_Token'] = array('key' => $key);
  613. $result = $this->Form->secure($fields);
  614. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  615. $hash .= ':' . 'Model.valid';
  616. $hash = urlencode($hash);
  617. $expected = array(
  618. 'div' => array('style' => 'display:none;'),
  619. array('input' => array(
  620. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  621. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  622. )),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  625. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  631. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  632. $result = $this->Form->secure($fields);
  633. $expected = array(
  634. 'div' => array('class' => 'hidden'),
  635. array('input' => array(
  636. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  637. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  638. )),
  639. array('input' => array(
  640. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  641. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  642. )),
  643. '/div'
  644. );
  645. $this->assertTags($result, $expected);
  646. }
  647. /**
  648. * Tests correct generation of number fields for double and float fields
  649. *
  650. * @return void
  651. */
  652. public function testTextFieldGenerationForFloats() {
  653. $model = ClassRegistry::getObject('Contact');
  654. $model->setSchema(array('foo' => array(
  655. 'type' => 'float',
  656. 'null' => false,
  657. 'default' => null,
  658. 'length' => 10
  659. )));
  660. $this->Form->create('Contact');
  661. $result = $this->Form->input('foo');
  662. $expected = array(
  663. 'div' => array('class' => 'input number'),
  664. 'label' => array('for' => 'ContactFoo'),
  665. 'Foo',
  666. '/label',
  667. array('input' => array(
  668. 'type' => 'number',
  669. 'name' => 'data[Contact][foo]',
  670. 'id' => 'ContactFoo',
  671. 'step' => 'any'
  672. )),
  673. '/div'
  674. );
  675. $this->assertTags($result, $expected);
  676. $result = $this->Form->input('foo', array('step' => 0.5));
  677. $expected = array(
  678. 'div' => array('class' => 'input number'),
  679. 'label' => array('for' => 'ContactFoo'),
  680. 'Foo',
  681. '/label',
  682. array('input' => array(
  683. 'type' => 'number',
  684. 'name' => 'data[Contact][foo]',
  685. 'id' => 'ContactFoo',
  686. 'step' => '0.5'
  687. )),
  688. '/div'
  689. );
  690. $this->assertTags($result, $expected);
  691. }
  692. /**
  693. * Tests correct generation of number fields for integer fields
  694. *
  695. * @return void
  696. */
  697. public function testTextFieldTypeNumberGenerationForIntegers() {
  698. $model = ClassRegistry::getObject('Contact');
  699. $model->setSchema(array('foo' => array(
  700. 'type' => 'integer',
  701. 'null' => false,
  702. 'default' => null,
  703. 'length' => null
  704. )));
  705. $this->Form->create('Contact');
  706. $result = $this->Form->input('foo');
  707. $expected = array(
  708. 'div' => array('class' => 'input number'),
  709. 'label' => array('for' => 'ContactFoo'),
  710. 'Foo',
  711. '/label',
  712. array('input' => array(
  713. 'type' => 'number', 'name' => 'data[Contact][foo]',
  714. 'id' => 'ContactFoo'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * Tests correct generation of file upload fields for binary fields
  722. *
  723. * @return void
  724. */
  725. public function testFileUploadFieldTypeGenerationForBinaries() {
  726. $model = ClassRegistry::getObject('Contact');
  727. $model->setSchema(array('foo' => array(
  728. 'type' => 'binary',
  729. 'null' => false,
  730. 'default' => null,
  731. 'length' => 1024
  732. )));
  733. $this->Form->create('Contact');
  734. $result = $this->Form->input('foo');
  735. $expected = array(
  736. 'div' => array('class' => 'input file'),
  737. 'label' => array('for' => 'ContactFoo'),
  738. 'Foo',
  739. '/label',
  740. array('input' => array(
  741. 'type' => 'file', 'name' => 'data[Contact][foo]',
  742. 'id' => 'ContactFoo'
  743. )),
  744. '/div'
  745. );
  746. $this->assertTags($result, $expected);
  747. }
  748. /**
  749. * testFormSecurityMultipleFields method
  750. *
  751. * Test secure() with multiple row form. Ensure hash is correct.
  752. *
  753. * @return void
  754. */
  755. public function testFormSecurityMultipleFields() {
  756. $key = 'testKey';
  757. $fields = array(
  758. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  759. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  760. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  761. );
  762. $this->Form->request['_Token'] = array('key' => $key);
  763. $result = $this->Form->secure($fields);
  764. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  765. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  766. $expected = array(
  767. 'div' => array('style' => 'display:none;'),
  768. array('input' => array(
  769. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  770. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  771. )),
  772. array('input' => array(
  773. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  774. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  775. )),
  776. '/div'
  777. );
  778. $this->assertTags($result, $expected);
  779. }
  780. /**
  781. * testFormSecurityMultipleSubmitButtons
  782. *
  783. * test form submit generation and ensure that _Token is only created on end()
  784. *
  785. * @return void
  786. */
  787. public function testFormSecurityMultipleSubmitButtons() {
  788. $key = 'testKey';
  789. $this->Form->request['_Token'] = array('key' => $key);
  790. $this->Form->create('Addresses');
  791. $this->Form->input('Address.title');
  792. $this->Form->input('Address.first_name');
  793. $result = $this->Form->submit('Save', array('name' => 'save'));
  794. $expected = array(
  795. 'div' => array('class' => 'submit'),
  796. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  797. '/div',
  798. );
  799. $this->assertTags($result, $expected);
  800. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  801. $expected = array(
  802. 'div' => array('class' => 'submit'),
  803. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  804. '/div',
  805. );
  806. $this->assertTags($result, $expected);
  807. $result = $this->Form->end(null);
  808. $expected = array(
  809. 'div' => array('style' => 'display:none;'),
  810. array('input' => array(
  811. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  812. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  813. )),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  816. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  817. )),
  818. '/div'
  819. );
  820. $this->assertTags($result, $expected);
  821. }
  822. /**
  823. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  824. *
  825. * @return void
  826. */
  827. public function testSecurityButtonNestedNamed() {
  828. $key = 'testKey';
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $this->Form->create('Addresses');
  831. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  832. $result = $this->Form->unlockField();
  833. $this->assertEquals(array('Address.button'), $result);
  834. }
  835. /**
  836. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitNestedNamed() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('Addresses');
  844. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  845. $result = $this->Form->unlockField();
  846. $this->assertEquals(array('Address.button'), $result);
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with no names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageNoName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png');
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * Test that the correct fields are unlocked for image submits with names.
  868. *
  869. * @return void
  870. */
  871. public function testSecuritySubmitImageName() {
  872. $key = 'testKey';
  873. $this->Form->request['_Token'] = array('key' => $key);
  874. $this->Form->create('User');
  875. $result = $this->Form->submit('save.png', array('name' => 'test'));
  876. $expected = array(
  877. 'div' => array('class' => 'submit'),
  878. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  879. '/div'
  880. );
  881. $this->assertTags($result, $expected);
  882. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  883. }
  884. /**
  885. * testFormSecurityMultipleInputFields method
  886. *
  887. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  888. *
  889. * @return void
  890. */
  891. public function testFormSecurityMultipleInputFields() {
  892. $key = 'testKey';
  893. $this->Form->request['_Token'] = array('key' => $key);
  894. $this->Form->create('Addresses');
  895. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  896. $this->Form->input('Addresses.0.title');
  897. $this->Form->input('Addresses.0.first_name');
  898. $this->Form->input('Addresses.0.last_name');
  899. $this->Form->input('Addresses.0.address');
  900. $this->Form->input('Addresses.0.city');
  901. $this->Form->input('Addresses.0.phone');
  902. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  903. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  904. $this->Form->input('Addresses.1.title');
  905. $this->Form->input('Addresses.1.first_name');
  906. $this->Form->input('Addresses.1.last_name');
  907. $this->Form->input('Addresses.1.address');
  908. $this->Form->input('Addresses.1.city');
  909. $this->Form->input('Addresses.1.phone');
  910. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  911. $result = $this->Form->secure($this->Form->fields);
  912. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  913. $expected = array(
  914. 'div' => array('style' => 'display:none;'),
  915. array('input' => array(
  916. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  917. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  918. )),
  919. array('input' => array(
  920. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  921. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  922. )),
  923. '/div'
  924. );
  925. $this->assertTags($result, $expected);
  926. }
  927. /**
  928. * Test form security with Model.field.0 style inputs
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityArrayFields() {
  933. $key = 'testKey';
  934. $this->Form->request->params['_Token']['key'] = $key;
  935. $this->Form->create('Address');
  936. $this->Form->input('Address.primary.1');
  937. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  938. $this->Form->input('Address.secondary.1.0');
  939. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  940. }
  941. /**
  942. * testFormSecurityMultipleInputDisabledFields method
  943. *
  944. * test secure form generation with multiple records and disabled fields.
  945. *
  946. * @return void
  947. */
  948. public function testFormSecurityMultipleInputDisabledFields() {
  949. $key = 'testKey';
  950. $this->Form->request->params['_Token'] = array(
  951. 'key' => $key,
  952. 'unlockedFields' => array('first_name', 'address')
  953. );
  954. $this->Form->create();
  955. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  956. $this->Form->input('Addresses.0.title');
  957. $this->Form->input('Addresses.0.first_name');
  958. $this->Form->input('Addresses.0.last_name');
  959. $this->Form->input('Addresses.0.address');
  960. $this->Form->input('Addresses.0.city');
  961. $this->Form->input('Addresses.0.phone');
  962. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  963. $this->Form->input('Addresses.1.title');
  964. $this->Form->input('Addresses.1.first_name');
  965. $this->Form->input('Addresses.1.last_name');
  966. $this->Form->input('Addresses.1.address');
  967. $this->Form->input('Addresses.1.city');
  968. $this->Form->input('Addresses.1.phone');
  969. $result = $this->Form->secure($this->Form->fields);
  970. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  971. $expected = array(
  972. 'div' => array('style' => 'display:none;'),
  973. array('input' => array(
  974. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  975. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  976. )),
  977. array('input' => array(
  978. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  979. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  980. )),
  981. '/div'
  982. );
  983. $this->assertTags($result, $expected);
  984. }
  985. /**
  986. * testFormSecurityInputDisabledFields method
  987. *
  988. * Test single record form with disabled fields.
  989. *
  990. * @return void
  991. */
  992. public function testFormSecurityInputUnlockedFields() {
  993. $key = 'testKey';
  994. $this->Form->request['_Token'] = array(
  995. 'key' => $key,
  996. 'unlockedFields' => array('first_name', 'address')
  997. );
  998. $this->Form->create();
  999. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1000. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1001. $this->Form->input('Addresses.title');
  1002. $this->Form->input('Addresses.first_name');
  1003. $this->Form->input('Addresses.last_name');
  1004. $this->Form->input('Addresses.address');
  1005. $this->Form->input('Addresses.city');
  1006. $this->Form->input('Addresses.phone');
  1007. $result = $this->Form->fields;
  1008. $expected = array(
  1009. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1010. 'Addresses.city', 'Addresses.phone'
  1011. );
  1012. $this->assertEquals($expected, $result);
  1013. $result = $this->Form->secure($expected);
  1014. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1015. $expected = array(
  1016. 'div' => array('style' => 'display:none;'),
  1017. array('input' => array(
  1018. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1019. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1020. )),
  1021. array('input' => array(
  1022. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1023. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1024. )),
  1025. '/div'
  1026. );
  1027. $this->assertTags($result, $expected);
  1028. }
  1029. /**
  1030. * test securing inputs with custom name attributes.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFormSecureWithCustomNameAttribute() {
  1035. $this->Form->request->params['_Token']['key'] = 'testKey';
  1036. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1037. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1038. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1039. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1040. }
  1041. /**
  1042. * testFormSecuredInput method
  1043. *
  1044. * Test generation of entire secure form, assertions made on input() output.
  1045. *
  1046. * @return void
  1047. */
  1048. public function testFormSecuredInput() {
  1049. $this->Form->request['_Token'] = array('key' => 'testKey');
  1050. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1051. $encoding = strtolower(Configure::read('App.encoding'));
  1052. $expected = array(
  1053. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1054. 'div' => array('style' => 'display:none;'),
  1055. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1056. array('input' => array(
  1057. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1058. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1064. $expected = array(
  1065. 'div' => array('class' => 'input text'),
  1066. 'label' => array('for' => 'UserFormPublished'),
  1067. 'Published',
  1068. '/label',
  1069. array('input' => array(
  1070. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1071. 'id' => 'UserFormPublished'
  1072. )),
  1073. '/div'
  1074. );
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1077. $expected = array(
  1078. 'div' => array('class' => 'input text'),
  1079. 'label' => array('for' => 'UserFormOther'),
  1080. 'Other',
  1081. '/label',
  1082. array('input' => array(
  1083. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1084. 'id' => 'UserFormOther'
  1085. )),
  1086. '/div'
  1087. );
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->hidden('UserForm.stuff');
  1090. $expected = array(
  1091. 'input' => array(
  1092. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1093. 'id' => 'UserFormStuff'
  1094. ));
  1095. $this->assertTags($result, $expected);
  1096. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1097. $expected = array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1099. 'value' => '0', 'id' => 'UserFormHidden'
  1100. ));
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1103. $expected = array(
  1104. 'div' => array('class' => 'input checkbox'),
  1105. array('input' => array(
  1106. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1107. 'value' => '0', 'id' => 'UserFormSomething_'
  1108. )),
  1109. array('input' => array(
  1110. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1111. 'value' => '1', 'id' => 'UserFormSomething'
  1112. )),
  1113. 'label' => array('for' => 'UserFormSomething'),
  1114. 'Something',
  1115. '/label',
  1116. '/div'
  1117. );
  1118. $this->assertTags($result, $expected);
  1119. $result = $this->Form->fields;
  1120. $expected = array(
  1121. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1122. 'UserForm.hidden' => '0', 'UserForm.something'
  1123. );
  1124. $this->assertEquals($expected, $result);
  1125. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1126. $result = $this->Form->secure($this->Form->fields);
  1127. $expected = array(
  1128. 'div' => array('style' => 'display:none;'),
  1129. array('input' => array(
  1130. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1131. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1132. )),
  1133. array('input' => array(
  1134. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1135. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1136. )),
  1137. '/div'
  1138. );
  1139. $this->assertTags($result, $expected);
  1140. }
  1141. /**
  1142. * Test secured inputs with custom names.
  1143. *
  1144. * @return void
  1145. */
  1146. public function testSecuredInputCustomName() {
  1147. $this->Form->request['_Token'] = array('key' => 'testKey');
  1148. $this->assertEquals(array(), $this->Form->fields);
  1149. $this->Form->input('text_input', array(
  1150. 'name' => 'data[Option][General.default_role]',
  1151. ));
  1152. $expected = array('Option.General.default_role');
  1153. $this->assertEquals($expected, $this->Form->fields);
  1154. $this->Form->input('select_box', array(
  1155. 'name' => 'data[Option][General.select_role]',
  1156. 'type' => 'select',
  1157. 'options' => array(1, 2),
  1158. ));
  1159. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * Tests that the correct keys are added to the field hash index
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredFileInput() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $this->Form->file('Attachment.file');
  1171. $expected = array(
  1172. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1173. 'Attachment.file.error', 'Attachment.file.size'
  1174. );
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * test that multiple selects keys are added to field hash
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredMultipleSelect() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->assertEquals(array(), $this->Form->fields);
  1185. $options = array('1' => 'one', '2' => 'two');
  1186. $this->Form->select('Model.select', $options);
  1187. $expected = array('Model.select');
  1188. $this->assertEquals($expected, $this->Form->fields);
  1189. $this->Form->fields = array();
  1190. $this->Form->select('Model.select', $options, array('multiple' => true));
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * testFormSecuredRadio method
  1195. *
  1196. * @return void
  1197. */
  1198. public function testFormSecuredRadio() {
  1199. $this->Form->request['_Token'] = array('key' => 'testKey');
  1200. $this->assertEquals(array(), $this->Form->fields);
  1201. $options = array('1' => 'option1', '2' => 'option2');
  1202. $this->Form->radio('Test.test', $options);
  1203. $expected = array('Test.test');
  1204. $this->assertEquals($expected, $this->Form->fields);
  1205. }
  1206. /**
  1207. * Test that when disabled is in a list based attribute array it works.
  1208. *
  1209. * @return void
  1210. */
  1211. public function testFormSecuredAndDisabledNotAssoc() {
  1212. $this->Form->request['_Token'] = array('key' => 'testKey');
  1213. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1214. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1215. $this->Form->text('Model.text', array('disabled'));
  1216. $this->Form->textarea('Model.textarea', array('disabled'));
  1217. $this->Form->password('Model.password', array('disabled'));
  1218. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1219. $expected = array(
  1220. 'Model.radio' => ''
  1221. );
  1222. $this->assertEquals($expected, $this->Form->fields);
  1223. }
  1224. /**
  1225. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1226. * hashing.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testFormSecuredAndDisabled() {
  1231. $this->Form->request['_Token'] = array('key' => 'testKey');
  1232. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1233. $this->Form->text('Model.text', array('disabled' => true));
  1234. $this->Form->password('Model.text', array('disabled' => true));
  1235. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1236. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1237. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1238. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1239. $this->Form->month('Model.month', array('disabled' => true));
  1240. $this->Form->day('Model.day', array('disabled' => true));
  1241. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1242. $this->Form->minute('Model.minute', array('disabled' => true));
  1243. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1244. $expected = array(
  1245. 'Model.radio' => ''
  1246. );
  1247. $this->assertEquals($expected, $this->Form->fields);
  1248. }
  1249. /**
  1250. * testDisableSecurityUsingForm method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testDisableSecurityUsingForm() {
  1255. $this->Form->request['_Token'] = array(
  1256. 'key' => 'testKey',
  1257. 'disabledFields' => array()
  1258. );
  1259. $this->Form->create();
  1260. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1261. $this->Form->input('Addresses.title');
  1262. $this->Form->input('Addresses.first_name', array('secure' => false));
  1263. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1264. $this->Form->input('Addresses.zip', array(
  1265. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1266. ));
  1267. $result = $this->Form->fields;
  1268. $expected = array(
  1269. 'Addresses.id' => '123456', 'Addresses.title',
  1270. );
  1271. $this->assertEquals($expected, $result);
  1272. }
  1273. /**
  1274. * test disableField
  1275. *
  1276. * @return void
  1277. */
  1278. public function testUnlockFieldAddsToList() {
  1279. $this->Form->request['_Token'] = array(
  1280. 'key' => 'testKey',
  1281. 'unlockedFields' => array()
  1282. );
  1283. $this->Form->create('Contact');
  1284. $this->Form->unlockField('Contact.name');
  1285. $this->Form->text('Contact.name');
  1286. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1287. $this->assertEquals(array(), $this->Form->fields);
  1288. }
  1289. /**
  1290. * test unlockField removing from fields array.
  1291. *
  1292. * @return void
  1293. */
  1294. public function testUnlockFieldRemovingFromFields() {
  1295. $this->Form->request['_Token'] = array(
  1296. 'key' => 'testKey',
  1297. 'unlockedFields' => array()
  1298. );
  1299. $this->Form->create('Contact');
  1300. $this->Form->hidden('Contact.id', array('value' => 1));
  1301. $this->Form->text('Contact.name');
  1302. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1303. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1304. $this->Form->unlockField('Contact.name');
  1305. $this->Form->unlockField('Contact.id');
  1306. $this->assertEquals(array(), $this->Form->fields);
  1307. }
  1308. /**
  1309. * testTagIsInvalid method
  1310. *
  1311. * @return void
  1312. */
  1313. public function testTagIsInvalid() {
  1314. $Contact = ClassRegistry::getObject('Contact');
  1315. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1316. $this->Form->setEntity('Contact.0.email');
  1317. $result = $this->Form->tagIsInvalid();
  1318. $this->assertEquals($expected, $result);
  1319. $this->Form->setEntity('Contact.1.email');
  1320. $result = $this->Form->tagIsInvalid();
  1321. $this->assertFalse($result);
  1322. $this->Form->setEntity('Contact.0.name');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $this->assertFalse($result);
  1325. }
  1326. /**
  1327. * Test tagIsInvalid with validation errors from a saveMany
  1328. *
  1329. * @return void
  1330. */
  1331. public function testTagIsInvalidSaveMany() {
  1332. $Contact = ClassRegistry::getObject('Contact');
  1333. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1334. $this->Form->create('Contact');
  1335. $this->Form->setEntity('0.email');
  1336. $result = $this->Form->tagIsInvalid();
  1337. $this->assertEquals($expected, $result);
  1338. $this->Form->setEntity('0.Contact.email');
  1339. $result = $this->Form->tagIsInvalid();
  1340. $this->assertEquals($expected, $result);
  1341. }
  1342. /**
  1343. * Test validation errors.
  1344. *
  1345. * @return void
  1346. */
  1347. public function testPasswordValidation() {
  1348. $Contact = ClassRegistry::getObject('Contact');
  1349. $Contact->validationErrors['password'] = array('Please provide a password');
  1350. $result = $this->Form->input('Contact.password');
  1351. $expected = array(
  1352. 'div' => array('class' => 'input password error'),
  1353. 'label' => array('for' => 'ContactPassword'),
  1354. 'Password',
  1355. '/label',
  1356. 'input' => array(
  1357. 'type' => 'password', 'name' => 'data[Contact][password]',
  1358. 'id' => 'ContactPassword', 'class' => 'form-error'
  1359. ),
  1360. array('div' => array('class' => 'error-message')),
  1361. 'Please provide a password',
  1362. '/div',
  1363. '/div'
  1364. );
  1365. $this->assertTags($result, $expected);
  1366. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1367. $expected = array(
  1368. 'div' => array('class' => 'input password error'),
  1369. 'label' => array('for' => 'ContactPassword'),
  1370. 'Password',
  1371. '/label',
  1372. 'input' => array(
  1373. 'type' => 'password', 'name' => 'data[Contact][password]',
  1374. 'id' => 'ContactPassword', 'class' => 'form-error'
  1375. ),
  1376. '/div'
  1377. );
  1378. $this->assertTags($result, $expected);
  1379. }
  1380. /**
  1381. * Test validation errors, when validation message is an empty string.
  1382. *
  1383. * @return void
  1384. */
  1385. public function testEmptyErrorValidation() {
  1386. $this->Form->validationErrors['Contact']['password'] = '';
  1387. $result = $this->Form->input('Contact.password');
  1388. $expected = array(
  1389. 'div' => array('class' => 'input password error'),
  1390. 'label' => array('for' => 'ContactPassword'),
  1391. 'Password',
  1392. '/label',
  1393. 'input' => array(
  1394. 'type' => 'password', 'name' => 'data[Contact][password]',
  1395. 'id' => 'ContactPassword', 'class' => 'form-error'
  1396. ),
  1397. array('div' => array('class' => 'error-message')),
  1398. array(),
  1399. '/div',
  1400. '/div'
  1401. );
  1402. $this->assertTags($result, $expected);
  1403. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. '/div'
  1414. );
  1415. $this->assertTags($result, $expected);
  1416. }
  1417. /**
  1418. * Test validation errors, when calling input() overriding validation message by an empty string.
  1419. *
  1420. * @return void
  1421. */
  1422. public function testEmptyInputErrorValidation() {
  1423. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1424. $result = $this->Form->input('Contact.password', array('error' => ''));
  1425. $expected = array(
  1426. 'div' => array('class' => 'input password error'),
  1427. 'label' => array('for' => 'ContactPassword'),
  1428. 'Password',
  1429. '/label',
  1430. 'input' => array(
  1431. 'type' => 'password', 'name' => 'data[Contact][password]',
  1432. 'id' => 'ContactPassword', 'class' => 'form-error'
  1433. ),
  1434. array('div' => array('class' => 'error-message')),
  1435. array(),
  1436. '/div',
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. '/div'
  1451. );
  1452. $this->assertTags($result, $expected);
  1453. }
  1454. /**
  1455. * testFormValidationAssociated method
  1456. *
  1457. * test display of form errors in conjunction with model::validates.
  1458. *
  1459. * @return void
  1460. */
  1461. public function testFormValidationAssociated() {
  1462. $this->UserForm = ClassRegistry::getObject('UserForm');
  1463. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1464. $data = array(
  1465. 'UserForm' => array('name' => 'user'),
  1466. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1467. );
  1468. $result = $this->UserForm->OpenidUrl->create($data);
  1469. $this->assertFalse(empty($result));
  1470. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1471. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1472. $encoding = strtolower(Configure::read('App.encoding'));
  1473. $expected = array(
  1474. 'form' => array(
  1475. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1476. 'accept-charset' => $encoding
  1477. ),
  1478. 'div' => array('style' => 'display:none;'),
  1479. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1480. '/div'
  1481. );
  1482. $this->assertTags($result, $expected);
  1483. $result = $this->Form->error(
  1484. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1485. );
  1486. $this->assertEquals('Error, not registered', $result);
  1487. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1488. }
  1489. /**
  1490. * testFormValidationAssociatedFirstLevel method
  1491. *
  1492. * test form error display with associated model.
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormValidationAssociatedFirstLevel() {
  1497. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1498. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1502. );
  1503. $result = $this->ValidateUser->create($data);
  1504. $this->assertFalse(empty($result));
  1505. $this->assertFalse($this->ValidateUser->validates());
  1506. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1507. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1508. $encoding = strtolower(Configure::read('App.encoding'));
  1509. $expected = array(
  1510. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1511. 'div' => array('style' => 'display:none;'),
  1512. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1513. '/div'
  1514. );
  1515. $this->assertTags($result, $expected);
  1516. $result = $this->Form->error(
  1517. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1518. );
  1519. $this->assertEquals('Invalid email', $result);
  1520. $result = $this->Form->error(
  1521. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1522. );
  1523. $this->assertEquals('Invalid name', $result);
  1524. $result = $this->Form->error(
  1525. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1526. );
  1527. $this->assertEquals('Invalid city', $result);
  1528. unset($this->ValidateUser->ValidateProfile);
  1529. unset($this->ValidateUser);
  1530. }
  1531. /**
  1532. * testFormValidationAssociatedSecondLevel method
  1533. *
  1534. * test form error display with associated model.
  1535. *
  1536. * @return void
  1537. */
  1538. public function testFormValidationAssociatedSecondLevel() {
  1539. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1540. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1541. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1542. $data = array(
  1543. 'ValidateUser' => array('name' => 'mariano'),
  1544. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1545. 'ValidateItem' => array('name' => 'Item')
  1546. );
  1547. $result = $this->ValidateUser->create($data);
  1548. $this->assertFalse(empty($result));
  1549. $this->assertFalse($this->ValidateUser->validates());
  1550. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1551. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1552. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1553. $encoding = strtolower(Configure::read('App.encoding'));
  1554. $expected = array(
  1555. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1556. 'div' => array('style' => 'display:none;'),
  1557. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1558. '/div'
  1559. );
  1560. $this->assertTags($result, $expected);
  1561. $result = $this->Form->error(
  1562. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1563. );
  1564. $this->assertEquals('Invalid email', $result);
  1565. $result = $this->Form->error(
  1566. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1567. );
  1568. $this->assertEquals('Invalid name', $result);
  1569. $result = $this->Form->error(
  1570. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1571. );
  1572. $result = $this->Form->error(
  1573. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1574. );
  1575. $this->assertEquals('Invalid description', $result);
  1576. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1577. unset($this->ValidateUser->ValidateProfile);
  1578. unset($this->ValidateUser);
  1579. }
  1580. /**
  1581. * testFormValidationMultiRecord method
  1582. *
  1583. * test form error display with multiple records.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testFormValidationMultiRecord() {
  1588. $Contact = ClassRegistry::getObject('Contact');
  1589. $Contact->validationErrors[2] = array(
  1590. 'name' => array('This field cannot be left blank')
  1591. );
  1592. $result = $this->Form->input('Contact.2.name');
  1593. $expected = array(
  1594. 'div' => array('class' => 'input text error'),
  1595. 'label' => array('for' => 'Contact2Name'),
  1596. 'Name',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1600. 'class' => 'form-error', 'maxlength' => 255
  1601. ),
  1602. array('div' => array('class' => 'error-message')),
  1603. 'This field cannot be left blank',
  1604. '/div',
  1605. '/div'
  1606. );
  1607. $this->assertTags($result, $expected);
  1608. }
  1609. /**
  1610. * testMultipleInputValidation method
  1611. *
  1612. * test multiple record form validation error display.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testMultipleInputValidation() {
  1617. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1618. $Address->validationErrors[0] = array(
  1619. 'title' => array('This field cannot be empty'),
  1620. 'first_name' => array('This field cannot be empty')
  1621. );
  1622. $Address->validationErrors[1] = array(
  1623. 'last_name' => array('You must have a last name')
  1624. );
  1625. $this->Form->create();
  1626. $result = $this->Form->input('Address.0.title');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array(
  1633. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1634. ),
  1635. array('div' => array('class' => 'error-message')),
  1636. 'This field cannot be empty',
  1637. '/div',
  1638. '/div'
  1639. );
  1640. $this->assertTags($result, $expected);
  1641. $result = $this->Form->input('Address.0.first_name');
  1642. $expected = array(
  1643. 'div' => array('class'),
  1644. 'label' => array('for'),
  1645. 'preg:/[^<]+/',
  1646. '/label',
  1647. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1648. array('div' => array('class' => 'error-message')),
  1649. 'This field cannot be empty',
  1650. '/div',
  1651. '/div'
  1652. );
  1653. $this->assertTags($result, $expected);
  1654. $result = $this->Form->input('Address.0.last_name');
  1655. $expected = array(
  1656. 'div' => array('class'),
  1657. 'label' => array('for'),
  1658. 'preg:/[^<]+/',
  1659. '/label',
  1660. 'input' => array('type' => 'text', 'name', 'id'),
  1661. '/div'
  1662. );
  1663. $this->assertTags($result, $expected);
  1664. $result = $this->Form->input('Address.1.last_name');
  1665. $expected = array(
  1666. 'div' => array('class'),
  1667. 'label' => array('for'),
  1668. 'preg:/[^<]+/',
  1669. '/label',
  1670. 'input' => array(
  1671. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1672. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1673. ),
  1674. array('div' => array('class' => 'error-message')),
  1675. 'You must have a last name',
  1676. '/div',
  1677. '/div'
  1678. );
  1679. $this->assertTags($result, $expected);
  1680. }
  1681. /**
  1682. * testInput method
  1683. *
  1684. * Test various incarnations of input().
  1685. *
  1686. * @return void
  1687. */
  1688. public function testInput() {
  1689. $result = $this->Form->input('ValidateUser.balance');
  1690. $expected = array(
  1691. 'div' => array('class'),
  1692. 'label' => array('for'),
  1693. 'Balance',
  1694. '/label',
  1695. 'input' => array('name', 'type' => 'number', 'id'),
  1696. '/div',
  1697. );
  1698. $this->assertTags($result, $expected);
  1699. $result = $this->Form->input('ValidateUser.cost_decimal');
  1700. $expected = array(
  1701. 'div' => array('class'),
  1702. 'label' => array('for'),
  1703. 'Cost Decimal',
  1704. '/label',
  1705. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1706. '/div',
  1707. );
  1708. $this->assertTags($result, $expected);
  1709. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1710. $expected = array(
  1711. 'div' => array('class' => 'input email'),
  1712. 'label' => array('for' => 'custom'),
  1713. 'Email',
  1714. '/label',
  1715. array('input' => array(
  1716. 'type' => 'email', 'name' => 'data[Contact][email]',
  1717. 'id' => 'custom', 'maxlength' => 255
  1718. )),
  1719. '/div'
  1720. );
  1721. $this->assertTags($result, $expected);
  1722. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1723. $expected = array(
  1724. '<div',
  1725. 'label' => array('for' => 'ContactEmail'),
  1726. 'Email',
  1727. '/label',
  1728. array('input' => array(
  1729. 'type' => 'email', 'name' => 'data[Contact][email]',
  1730. 'id' => 'ContactEmail', 'maxlength' => 255
  1731. )),
  1732. '/div'
  1733. );
  1734. $this->assertTags($result, $expected);
  1735. $result = $this->Form->hidden('Contact.idontexist');
  1736. $expected = array('input' => array(
  1737. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1738. 'id' => 'ContactIdontexist'
  1739. ));
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input text'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'text', 'name' => 'data[Contact][email]',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1755. $expected = array(
  1756. 'div' => array('class' => 'input text'),
  1757. 'label' => array('for' => 'Contact5Email'),
  1758. 'Email',
  1759. '/label',
  1760. array('input' => array(
  1761. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1762. 'id' => 'Contact5Email'
  1763. )),
  1764. '/div'
  1765. );
  1766. $this->assertTags($result, $expected);
  1767. $result = $this->Form->input('Contact.password');
  1768. $expected = array(
  1769. 'div' => array('class' => 'input password'),
  1770. 'label' => array('for' => 'ContactPassword'),
  1771. 'Password',
  1772. '/label',
  1773. array('input' => array(
  1774. 'type' => 'password', 'name' => 'data[Contact][password]',
  1775. 'id' => 'ContactPassword'
  1776. )),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. $result = $this->Form->input('Contact.email', array(
  1781. 'type' => 'file', 'class' => 'textbox'
  1782. ));
  1783. $expected = array(
  1784. 'div' => array('class' => 'input file'),
  1785. 'label' => array('for' => 'ContactEmail'),
  1786. 'Email',
  1787. '/label',
  1788. array('input' => array(
  1789. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1790. 'id' => 'ContactEmail'
  1791. )),
  1792. '/div'
  1793. );
  1794. $this->assertTags($result, $expected);
  1795. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1796. $result = $this->Form->input('Contact.phone');
  1797. $expected = array(
  1798. 'div' => array('class' => 'input tel'),
  1799. 'label' => array('for' => 'ContactPhone'),
  1800. 'Phone',
  1801. '/label',
  1802. array('input' => array(
  1803. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1804. 'value' => 'Hello &amp; World &gt; weird chars',
  1805. 'id' => 'ContactPhone', 'maxlength' => 255
  1806. )),
  1807. '/div'
  1808. );
  1809. $this->assertTags($result, $expected);
  1810. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1811. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1812. $expected = array(
  1813. 'div' => array('class' => 'input text'),
  1814. 'label' => array('for' => 'myId'),
  1815. 'Field',
  1816. '/label',
  1817. 'input' => array(
  1818. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1819. 'value' => 'My value', 'id' => 'myId'
  1820. ),
  1821. '/div'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. unset($this->Form->request->data);
  1825. $Contact = ClassRegistry::getObject('Contact');
  1826. $Contact->validationErrors['field'] = array('Badness!');
  1827. $result = $this->Form->input('Contact.field');
  1828. $expected = array(
  1829. 'div' => array('class' => 'input text error'),
  1830. 'label' => array('for' => 'ContactField'),
  1831. 'Field',
  1832. '/label',
  1833. 'input' => array(
  1834. 'type' => 'text', 'name' => 'data[Contact][field]',
  1835. 'id' => 'ContactField', 'class' => 'form-error'
  1836. ),
  1837. array('div' => array('class' => 'error-message')),
  1838. 'Badness!',
  1839. '/div',
  1840. '/div'
  1841. );
  1842. $this->assertTags($result, $expected);
  1843. $result = $this->Form->input('Contact.field', array(
  1844. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1845. ));
  1846. $expected = array(
  1847. 'label' => array('for' => 'ContactField'),
  1848. 'Field',
  1849. '/label',
  1850. 'input' => array(
  1851. 'type' => 'text', 'name' => 'data[Contact][field]',
  1852. 'id' => 'ContactField', 'class' => 'form-error'
  1853. ),
  1854. array('span' => array('class' => 'error-message')),
  1855. 'Badness!',
  1856. '/span'
  1857. );
  1858. $this->assertTags($result, $expected);
  1859. $result = $this->Form->input('Contact.field', array(
  1860. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text error'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array(
  1868. 'type' => 'text', 'name' => 'data[Contact][field]',
  1869. 'id' => 'ContactField', 'class' => 'form-error'
  1870. ),
  1871. array('div' => array('class' => 'error')),
  1872. 'Badness!',
  1873. '/div'
  1874. );
  1875. $this->assertTags($result, $expected);
  1876. $result = $this->Form->input('Contact.field', array(
  1877. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1878. ));
  1879. $expected = array(
  1880. 'span' => array('class' => 'input text error'),
  1881. 'label' => array('for' => 'ContactField'),
  1882. 'Field',
  1883. '/label',
  1884. 'input' => array(
  1885. 'type' => 'text', 'name' => 'data[Contact][field]',
  1886. 'id' => 'ContactField', 'class' => 'form-error'
  1887. ),
  1888. 'Badness!',
  1889. '/span'
  1890. );
  1891. $this->assertTags($result, $expected);
  1892. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1893. $expected = array(
  1894. 'div' => array('class' => 'input text error'),
  1895. 'label' => array('for' => 'ContactField'),
  1896. 'Field',
  1897. '/label',
  1898. 'input' => array(
  1899. 'type' => 'text', 'name' => 'data[Contact][field]',
  1900. 'id' => 'ContactField', 'class' => 'form-error'
  1901. ),
  1902. 'A message to you, Rudy',
  1903. array('div' => array('class' => 'error-message')),
  1904. 'Badness!',
  1905. '/div',
  1906. '/div'
  1907. );
  1908. $this->assertTags($result, $expected);
  1909. $this->Form->setEntity(null);
  1910. $this->Form->setEntity('Contact.field');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'after' => 'A message to you, Rudy', 'error' => false
  1913. ));
  1914. $expected = array(
  1915. 'div' => array('class' => 'input text'),
  1916. 'label' => array('for' => 'ContactField'),
  1917. 'Field',
  1918. '/label',
  1919. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1920. 'A message to you, Rudy',
  1921. '/div'
  1922. );
  1923. $this->assertTags($result, $expected);
  1924. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1925. $expected = array(
  1926. 'div' => array('class' => 'input text'),
  1927. 'label' => array('for' => 'ObjectField'),
  1928. 'Field',
  1929. '/label',
  1930. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1931. 'A message to you, Rudy',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. $Contact->validationErrors['field'] = array('minLength');
  1936. $result = $this->Form->input('Contact.field', array(
  1937. 'error' => array(
  1938. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1939. 'maxLength' => 'login too large'
  1940. )
  1941. ));
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text error'),
  1944. 'label' => array('for' => 'ContactField'),
  1945. 'Field',
  1946. '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1948. array('div' => array('class' => 'error-message')),
  1949. 'Le login doit contenir au moins 2 caractères',
  1950. '/div',
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. $Contact->validationErrors['field'] = array('maxLength');
  1955. $result = $this->Form->input('Contact.field', array(
  1956. 'error' => array(
  1957. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1958. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1959. 'maxLength' => 'login too large',
  1960. )
  1961. ));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input text error'),
  1964. 'label' => array('for' => 'ContactField'),
  1965. 'Field',
  1966. '/label',
  1967. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1968. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1969. 'login too large',
  1970. '/span',
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. }
  1975. /**
  1976. * Test that inputs with 0 can be created.
  1977. *
  1978. * @return void
  1979. */
  1980. public function testInputZero() {
  1981. $this->Form->create('User');
  1982. $result = $this->Form->input('0');
  1983. $expected = array(
  1984. 'div' => array('class' => 'input text'),
  1985. 'label' => array('for' => 'User0'), '/label',
  1986. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1987. '/div'
  1988. );
  1989. $this->assertTags($result, $expected);
  1990. }
  1991. /**
  1992. * test input() with checkbox creation
  1993. *
  1994. * @return void
  1995. */
  1996. public function testInputCheckbox() {
  1997. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1998. $expected = array(
  1999. 'div' => array('class' => 'input checkbox'),
  2000. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2001. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2002. '/div'
  2003. );
  2004. $this->assertTags($result, $expected);
  2005. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2006. $expected = array(
  2007. 'div' => array('class' => 'input checkbox'),
  2008. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2009. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2010. '/div'
  2011. );
  2012. $this->assertTags($result, $expected);
  2013. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2014. $expected = array(
  2015. 'div' => array('class' => 'input checkbox'),
  2016. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2017. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2018. '/div'
  2019. );
  2020. $this->assertTags($result, $expected);
  2021. $result = $this->Form->input('User.disabled', array(
  2022. 'label' => 'Disabled',
  2023. 'type' => 'checkbox',
  2024. 'data-foo' => 'disabled'
  2025. ));
  2026. $expected = array(
  2027. 'div' => array('class' => 'input checkbox'),
  2028. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2029. array('input' => array(
  2030. 'type' => 'checkbox',
  2031. 'name' => 'data[User][disabled]',
  2032. 'value' => '1',
  2033. 'id' => 'UserDisabled',
  2034. 'data-foo' => 'disabled'
  2035. )),
  2036. 'label' => array('for' => 'UserDisabled'),
  2037. 'Disabled',
  2038. '/label',
  2039. '/div'
  2040. );
  2041. $this->assertTags($result, $expected);
  2042. }
  2043. /**
  2044. * test form->input() with time types.
  2045. *
  2046. */
  2047. public function testInputTime() {
  2048. extract($this->dateRegex);
  2049. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2050. $result = explode(':', $result);
  2051. $this->assertRegExp('/option value="23"/', $result[0]);
  2052. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2053. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2054. $result = explode(':', $result);
  2055. $this->assertRegExp('/option value="23"/', $result[0]);
  2056. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2057. $result = $this->Form->input('Model.field', array(
  2058. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2059. ));
  2060. $result = explode(':', $result);
  2061. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2062. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2063. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2064. $result = $this->Form->input('Model.field', array(
  2065. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2066. ));
  2067. $result = explode(':', $result);
  2068. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2069. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2070. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2071. $result = $this->Form->input('prueba', array(
  2072. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2073. 'maxYear' => date('Y') + 1, 'interval' => 15
  2074. ));
  2075. $result = explode(':', $result);
  2076. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2077. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2078. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2079. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2080. $result = $this->Form->input('Random.start_time', array(
  2081. 'type' => 'time',
  2082. 'selected' => '18:15'
  2083. ));
  2084. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2085. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2087. $result = $this->Form->input('published', array('type' => 'time'));
  2088. $now = strtotime('now');
  2089. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2090. $now = strtotime('2013-03-09 00:42:21');
  2091. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2092. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2093. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2094. }
  2095. /**
  2096. * Test interval + selected near the hour roll over.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testTimeSelectedWithInterval() {
  2101. $result = $this->Form->input('Model.start_time', array(
  2102. 'type' => 'time',
  2103. 'interval' => 15,
  2104. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2105. ));
  2106. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2107. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2108. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2109. $result = $this->Form->input('Model.start_time', array(
  2110. 'type' => 'time',
  2111. 'interval' => 15,
  2112. 'selected' => '2012-10-23 15:57:00'
  2113. ));
  2114. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2115. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2116. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2117. $result = $this->Form->input('Model.start_time', array(
  2118. 'timeFormat' => 24,
  2119. 'type' => 'time',
  2120. 'interval' => 15,
  2121. 'selected' => '15:57'
  2122. ));
  2123. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2124. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2125. $result = $this->Form->input('Model.start_time', array(
  2126. 'timeFormat' => 24,
  2127. 'type' => 'time',
  2128. 'interval' => 15,
  2129. 'selected' => '23:57'
  2130. ));
  2131. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2132. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2133. $result = $this->Form->input('Model.created', array(
  2134. 'timeFormat' => 24,
  2135. 'type' => 'datetime',
  2136. 'interval' => 15,
  2137. 'selected' => '2012-09-30 23:56'
  2138. ));
  2139. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2140. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2141. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2142. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2143. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2144. }
  2145. /**
  2146. * Test interval & timeFormat = 12
  2147. *
  2148. * @return void
  2149. */
  2150. public function testInputTimeWithIntervalAnd12HourFormat() {
  2151. $result = $this->Form->input('Model.start_time', array(
  2152. 'type' => 'time',
  2153. 'timeFormat' => 12,
  2154. 'interval' => 5,
  2155. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2156. ));
  2157. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2158. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2159. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2160. $result = $this->Form->input('Model.start_time', array(
  2161. 'type' => 'time',
  2162. 'timeFormat' => '12',
  2163. 'interval' => 5,
  2164. 'selected' => '2013-04-19 16:30:00'
  2165. ));
  2166. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2167. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2168. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2169. $result = $this->Form->input('Model.start_time', array(
  2170. 'type' => 'time',
  2171. 'timeFormat' => '12',
  2172. 'interval' => 10,
  2173. 'selected' => '2013-05-19 00:33:00'
  2174. ));
  2175. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2176. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2177. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2178. $result = $this->Form->input('Model.start_time', array(
  2179. 'type' => 'time',
  2180. 'timeFormat' => '12',
  2181. 'interval' => 10,
  2182. 'selected' => '2013-05-19 13:33:00'
  2183. ));
  2184. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2185. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2186. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2187. $result = $this->Form->input('Model.start_time', array(
  2188. 'type' => 'time',
  2189. 'timeFormat' => '12',
  2190. 'interval' => 10,
  2191. 'selected' => '2013-05-19 01:33:00'
  2192. ));
  2193. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2194. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2195. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2196. }
  2197. /**
  2198. * test form->input() with datetime, date and time types
  2199. *
  2200. * @return void
  2201. */
  2202. public function testInputDatetime() {
  2203. extract($this->dateRegex);
  2204. $result = $this->Form->input('prueba', array(
  2205. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2206. 'maxYear' => date('Y') + 1, 'interval' => 15
  2207. ));
  2208. $result = explode(':', $result);
  2209. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2210. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2211. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2212. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2213. //related to ticket #5013
  2214. $result = $this->Form->input('Contact.date', array(
  2215. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2216. ));
  2217. $this->assertRegExp('/class="customClass"/', $result);
  2218. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2219. $result = $this->Form->input('Contact.date', array(
  2220. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2221. ));
  2222. $this->assertRegExp('/id="customIdDay"/', $result);
  2223. $this->assertRegExp('/id="customIdMonth"/', $result);
  2224. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2225. $result = $this->Form->input('Model.field', array(
  2226. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2227. ));
  2228. $this->assertRegExp('/id="customIDDay"/', $result);
  2229. $this->assertRegExp('/id="customIDHour"/', $result);
  2230. $result = explode('</select><select', $result);
  2231. $result = explode(':', $result[1]);
  2232. $this->assertRegExp('/option value="23"/', $result[0]);
  2233. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2234. $result = $this->Form->input('Model.field', array(
  2235. 'type' => 'datetime', 'timeFormat' => 12
  2236. ));
  2237. $result = explode('</select><select', $result);
  2238. $result = explode(':', $result[1]);
  2239. $this->assertRegExp('/option value="12"/', $result[0]);
  2240. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2241. $this->Form->request->data = array('Contact' => array('created' => null));
  2242. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2243. $expected = array(
  2244. 'div' => array('class' => 'input date'),
  2245. 'label' => array('for' => 'ContactCreatedMonth'),
  2246. 'Created',
  2247. '/label',
  2248. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2249. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2250. $monthsRegex,
  2251. '/select', '-',
  2252. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2253. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2254. $daysRegex,
  2255. '/select', '-',
  2256. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2257. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2258. $yearsRegex,
  2259. '/select',
  2260. '/div'
  2261. );
  2262. $this->assertTags($result, $expected);
  2263. $this->Form->request->data = array('Contact' => array('created' => null));
  2264. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2265. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2266. $this->Form->request->data = array('Contact' => array('created' => null));
  2267. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2268. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2269. $result = $this->Form->input('Contact.created', array(
  2270. 'type' => 'date',
  2271. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2272. 'timeFormat' => 'NONE'
  2273. ));
  2274. $this->assertRegExp('/for\="created-month"/', $result);
  2275. }
  2276. /**
  2277. * Test generating checkboxes in a loop.
  2278. *
  2279. * @return void
  2280. */
  2281. public function testInputCheckboxesInLoop() {
  2282. for ($i = 1; $i < 5; $i++) {
  2283. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2284. $expected = array(
  2285. 'div' => array('class' => 'input checkbox'),
  2286. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2287. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2288. 'label' => array('for' => "Contact{$i}Email"),
  2289. 'Email',
  2290. '/label',
  2291. '/div'
  2292. );
  2293. $this->assertTags($result, $expected);
  2294. }
  2295. }
  2296. /**
  2297. * Test generating checkboxes with disabled elements.
  2298. *
  2299. * @return void
  2300. */
  2301. public function testInputCheckboxWithDisabledElements() {
  2302. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2303. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2304. $expected = array(
  2305. array('div' => array('class' => 'input select')),
  2306. array('label' => array('for' => "ContactMultiple")),
  2307. 'Multiple',
  2308. '/label',
  2309. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2312. array('label' => array('for' => "ContactMultiple1")),
  2313. 'One',
  2314. '/label',
  2315. '/div',
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2318. array('label' => array('for' => "ContactMultiple2")),
  2319. 'Two',
  2320. '/label',
  2321. '/div',
  2322. array('div' => array('class' => 'checkbox')),
  2323. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2324. array('label' => array('for' => "ContactMultiple3")),
  2325. 'Three',
  2326. '/label',
  2327. '/div',
  2328. '/div'
  2329. );
  2330. $this->assertTags($result, $expected);
  2331. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2332. $this->assertTags($result, $expected);
  2333. $disabled = array('2', 3);
  2334. $expected = array(
  2335. array('div' => array('class' => 'input select')),
  2336. array('label' => array('for' => "ContactMultiple")),
  2337. 'Multiple',
  2338. '/label',
  2339. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2342. array('label' => array('for' => "ContactMultiple1")),
  2343. 'One',
  2344. '/label',
  2345. '/div',
  2346. array('div' => array('class' => 'checkbox')),
  2347. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2348. array('label' => array('for' => "ContactMultiple2")),
  2349. 'Two',
  2350. '/label',
  2351. '/div',
  2352. array('div' => array('class' => 'checkbox')),
  2353. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2354. array('label' => array('for' => "ContactMultiple3")),
  2355. 'Three',
  2356. '/label',
  2357. '/div',
  2358. '/div'
  2359. );
  2360. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2361. $this->assertTags($result, $expected);
  2362. // make sure 50 does only disable 50, and not 50f5c0cf
  2363. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2364. $disabled = array(50);
  2365. $expected = array(
  2366. array('div' => array('class' => 'input select')),
  2367. array('label' => array('for' => "ContactMultiple")),
  2368. 'Multiple',
  2369. '/label',
  2370. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2371. array('div' => array('class' => 'checkbox')),
  2372. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2373. array('label' => array('for' => "ContactMultiple50")),
  2374. 'Fifty',
  2375. '/label',
  2376. '/div',
  2377. array('div' => array('class' => 'checkbox')),
  2378. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2379. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2380. 'Stringy',
  2381. '/label',
  2382. '/div',
  2383. '/div'
  2384. );
  2385. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2386. $this->assertTags($result, $expected);
  2387. }
  2388. /**
  2389. * test input name with leading integer, ensure attributes are generated correctly.
  2390. *
  2391. * @return void
  2392. */
  2393. public function testInputWithLeadingInteger() {
  2394. $result = $this->Form->text('0.Node.title');
  2395. $expected = array(
  2396. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2397. );
  2398. $this->assertTags($result, $expected);
  2399. }
  2400. /**
  2401. * test form->input() with select type inputs.
  2402. *
  2403. * @return void
  2404. */
  2405. public function testInputSelectType() {
  2406. $result = $this->Form->input('email', array(
  2407. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2408. );
  2409. $expected = array(
  2410. 'div' => array('class' => 'input select'),
  2411. 'label' => array('for' => 'email'),
  2412. 'Email',
  2413. '/label',
  2414. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2415. array('option' => array('value' => '')),
  2416. '/option',
  2417. array('option' => array('value' => 'è')),
  2418. 'Firést',
  2419. '/option',
  2420. array('option' => array('value' => 'é')),
  2421. 'Secoènd',
  2422. '/option',
  2423. '/select',
  2424. '/div'
  2425. );
  2426. $this->assertTags($result, $expected);
  2427. $result = $this->Form->input('email', array(
  2428. 'options' => array('First', 'Second'), 'empty' => true)
  2429. );
  2430. $expected = array(
  2431. 'div' => array('class' => 'input select'),
  2432. 'label' => array('for' => 'email'),
  2433. 'Email',
  2434. '/label',
  2435. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2436. array('option' => array('value' => '')),
  2437. '/option',
  2438. array('option' => array('value' => '0')),
  2439. 'First',
  2440. '/option',
  2441. array('option' => array('value' => '1')),
  2442. 'Second',
  2443. '/option',
  2444. '/select',
  2445. '/div'
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2449. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2450. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2451. $expected = array(
  2452. 'div' => array('class' => 'input select'),
  2453. 'label' => array('for' => 'ModelUserId'),
  2454. 'User',
  2455. '/label',
  2456. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2457. array('option' => array('value' => '')),
  2458. '/option',
  2459. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2460. 'good',
  2461. '/option',
  2462. array('option' => array('value' => 'other')),
  2463. 'bad',
  2464. '/option',
  2465. '/select',
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2470. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2471. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ThingUserId'),
  2475. 'User',
  2476. '/label',
  2477. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2478. array('option' => array('value' => '')),
  2479. 'Some Empty',
  2480. '/option',
  2481. array('option' => array('value' => 'value')),
  2482. 'good',
  2483. '/option',
  2484. array('option' => array('value' => 'other')),
  2485. 'bad',
  2486. '/option',
  2487. '/select',
  2488. '/div'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2492. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2493. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2494. $expected = array(
  2495. 'div' => array('class' => 'input select'),
  2496. 'label' => array('for' => 'ThingUserId'),
  2497. 'User',
  2498. '/label',
  2499. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2500. array('option' => array('value' => '')),
  2501. 'Some Empty',
  2502. '/option',
  2503. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2504. 'good',
  2505. '/option',
  2506. array('option' => array('value' => 'other')),
  2507. 'bad',
  2508. '/option',
  2509. '/select',
  2510. '/div'
  2511. );
  2512. $this->assertTags($result, $expected);
  2513. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2514. $this->Form->request->data = array('User' => array('User' => array('value')));
  2515. $result = $this->Form->input('User.User', array('empty' => true));
  2516. $expected = array(
  2517. 'div' => array('class' => 'input select'),
  2518. 'label' => array('for' => 'UserUser'),
  2519. 'User',
  2520. '/label',
  2521. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2522. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2523. array('option' => array('value' => '')),
  2524. '/option',
  2525. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2526. 'good',
  2527. '/option',
  2528. array('option' => array('value' => 'other')),
  2529. 'bad',
  2530. '/option',
  2531. '/select',
  2532. '/div'
  2533. );
  2534. $this->assertTags($result, $expected);
  2535. $this->Form->data = array();
  2536. $result = $this->Form->input('Publisher.id', array(
  2537. 'label' => 'Publisher',
  2538. 'type' => 'select',
  2539. 'multiple' => 'checkbox',
  2540. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2541. ));
  2542. $expected = array(
  2543. array('div' => array('class' => 'input select')),
  2544. array('label' => array('for' => 'PublisherId')),
  2545. 'Publisher',
  2546. '/label',
  2547. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2548. array('div' => array('class' => 'checkbox')),
  2549. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2550. array('label' => array('for' => 'PublisherIdValue1')),
  2551. 'Label 1',
  2552. '/label',
  2553. '/div',
  2554. array('div' => array('class' => 'checkbox')),
  2555. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2556. array('label' => array('for' => 'PublisherIdValue2')),
  2557. 'Label 2',
  2558. '/label',
  2559. '/div',
  2560. '/div'
  2561. );
  2562. $this->assertTags($result, $expected);
  2563. }
  2564. /**
  2565. * test that input() and a non standard primary key makes a hidden input by default.
  2566. *
  2567. * @return void
  2568. */
  2569. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2570. $this->Form->create('User');
  2571. $this->Form->fieldset = array(
  2572. 'User' => array(
  2573. 'fields' => array(
  2574. 'model_id' => array('type' => 'integer')
  2575. ),
  2576. 'validates' => array(),
  2577. 'key' => 'model_id'
  2578. )
  2579. );
  2580. $result = $this->Form->input('model_id');
  2581. $expected = array(
  2582. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2583. );
  2584. $this->assertTags($result, $expected);
  2585. }
  2586. /**
  2587. * test that overriding the magic select type widget is possible
  2588. *
  2589. * @return void
  2590. */
  2591. public function testInputOverridingMagicSelectType() {
  2592. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2593. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2594. $expected = array(
  2595. 'div' => array('class' => 'input text'),
  2596. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2597. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2598. '/div'
  2599. );
  2600. $this->assertTags($result, $expected);
  2601. //Check that magic types still work for plural/singular vars
  2602. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2603. $result = $this->Form->input('Model.type');
  2604. $expected = array(
  2605. 'div' => array('class' => 'input select'),
  2606. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2607. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2608. array('option' => array('value' => 'value')), 'good', '/option',
  2609. array('option' => array('value' => 'other')), 'bad', '/option',
  2610. '/select',
  2611. '/div'
  2612. );
  2613. $this->assertTags($result, $expected);
  2614. }
  2615. /**
  2616. * Test that magic input() selects are created for type=number
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputMagicSelectForTypeNumber() {
  2621. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2622. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2623. $result = $this->Form->input('ValidateUser.balance');
  2624. $expected = array(
  2625. 'div' => array('class' => 'input select'),
  2626. 'label' => array('for' => 'ValidateUserBalance'),
  2627. 'Balance',
  2628. '/label',
  2629. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2630. array('option' => array('value' => '0')),
  2631. 'nothing',
  2632. '/option',
  2633. array('option' => array('value' => '1', 'selected' => 'selected')),
  2634. 'some',
  2635. '/option',
  2636. array('option' => array('value' => '100')),
  2637. 'a lot',
  2638. '/option',
  2639. '/select',
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * Test that magic input() selects can easily be converted into radio types without error.
  2646. *
  2647. * @return void
  2648. */
  2649. public function testInputMagicSelectChangeToRadio() {
  2650. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2651. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2652. $this->assertRegExp('/input type="radio"/', $result);
  2653. }
  2654. /**
  2655. * fields with the same name as the model should work.
  2656. *
  2657. * @return void
  2658. */
  2659. public function testInputWithMatchingFieldAndModelName() {
  2660. $this->Form->create('User');
  2661. $this->Form->fieldset = array(
  2662. 'User' => array(
  2663. 'fields' => array(
  2664. 'User' => array('type' => 'text')
  2665. ),
  2666. 'validates' => array(),
  2667. 'key' => 'id'
  2668. )
  2669. );
  2670. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2671. $result = $this->Form->input('User', array('type' => 'text'));
  2672. $expected = array(
  2673. 'div' => array('class' => 'input text'),
  2674. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2675. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2676. '/div'
  2677. );
  2678. $this->assertTags($result, $expected);
  2679. }
  2680. /**
  2681. * testFormInputs method
  2682. *
  2683. * test correct results from form::inputs().
  2684. *
  2685. * @return void
  2686. */
  2687. public function testFormInputs() {
  2688. $this->Form->create('Contact');
  2689. $result = $this->Form->inputs('The Legend');
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend',
  2693. 'The Legend',
  2694. '/legend',
  2695. '*/fieldset',
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2699. $expected = array(
  2700. 'fieldset' => array('class' => 'classy-stuff'),
  2701. '<legend',
  2702. 'Field of Dreams',
  2703. '/legend',
  2704. '*/fieldset'
  2705. );
  2706. $this->assertTags($result, $expected);
  2707. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2708. $this->assertTags($result, $expected);
  2709. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2710. $this->assertTags($result, $expected);
  2711. $this->Form->create('Contact');
  2712. $this->Form->request['prefix'] = 'admin';
  2713. $this->Form->request['action'] = 'admin_edit';
  2714. $result = $this->Form->inputs();
  2715. $expected = array(
  2716. '<fieldset',
  2717. '<legend',
  2718. 'Edit Contact',
  2719. '/legend',
  2720. '*/fieldset',
  2721. );
  2722. $this->assertTags($result, $expected);
  2723. $this->Form->create('Contact');
  2724. $result = $this->Form->inputs(false);
  2725. $expected = array(
  2726. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2727. array('div' => array('class' => 'input text')),
  2728. '*/div',
  2729. array('div' => array('class' => 'input email')),
  2730. '*/div',
  2731. array('div' => array('class' => 'input tel')),
  2732. '*/div',
  2733. array('div' => array('class' => 'input password')),
  2734. '*/div',
  2735. array('div' => array('class' => 'input date')),
  2736. '*/div',
  2737. array('div' => array('class' => 'input date')),
  2738. '*/div',
  2739. array('div' => array('class' => 'input datetime')),
  2740. '*/div',
  2741. array('div' => array('class' => 'input number')),
  2742. '*/div',
  2743. array('div' => array('class' => 'input select')),
  2744. '*/div',
  2745. );
  2746. $this->assertTags($result, $expected);
  2747. $this->Form->create('Contact');
  2748. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2749. $expected = array(
  2750. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2751. array('div' => array('class' => 'input text')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input email')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input tel')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input password')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input date')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input date')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input datetime')),
  2764. '*/div',
  2765. array('div' => array('class' => 'input number')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input select')),
  2768. '*/div',
  2769. );
  2770. $this->assertTags($result, $expected);
  2771. $this->Form->create('Contact');
  2772. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2773. $this->assertTags($result, $expected);
  2774. $this->Form->create('Contact');
  2775. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2776. $expected = array(
  2777. 'fieldset' => array(),
  2778. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2779. array('div' => array('class' => 'input text')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input email')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input tel')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input password')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input date')),
  2788. '*/div',
  2789. array('div' => array('class' => 'input date')),
  2790. '*/div',
  2791. array('div' => array('class' => 'input datetime')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input number')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input select')),
  2796. '*/div',
  2797. '/fieldset'
  2798. );
  2799. $this->assertTags($result, $expected);
  2800. $this->Form->create('Contact');
  2801. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2802. $expected = array(
  2803. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2804. array('div' => array('class' => 'input text')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input email')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input tel')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input password')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input date')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input date')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input datetime')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input number')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input select')),
  2821. '*/div',
  2822. );
  2823. $this->assertTags($result, $expected);
  2824. $this->Form->create('Contact');
  2825. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2826. $this->assertTags($result, $expected);
  2827. $this->Form->create('Contact');
  2828. $result = $this->Form->inputs('Hello');
  2829. $expected = array(
  2830. 'fieldset' => array(),
  2831. 'legend' => array(),
  2832. 'Hello',
  2833. '/legend',
  2834. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2835. array('div' => array('class' => 'input text')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input email')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input tel')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input password')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input date')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input date')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input datetime')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input number')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input select')),
  2852. '*/div',
  2853. '/fieldset'
  2854. );
  2855. $this->assertTags($result, $expected);
  2856. $this->Form->create('Contact');
  2857. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2858. $expected = array(
  2859. 'fieldset' => array(),
  2860. 'legend' => array(),
  2861. 'Hello',
  2862. '/legend',
  2863. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2864. array('div' => array('class' => 'input text')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input email')),
  2867. '*/div',
  2868. array('div' => array('class' => 'input tel')),
  2869. '*/div',
  2870. array('div' => array('class' => 'input password')),
  2871. '*/div',
  2872. array('div' => array('class' => 'input date')),
  2873. '*/div',
  2874. array('div' => array('class' => 'input date')),
  2875. '*/div',
  2876. array('div' => array('class' => 'input datetime')),
  2877. '*/div',
  2878. array('div' => array('class' => 'input number')),
  2879. '*/div',
  2880. array('div' => array('class' => 'input select')),
  2881. '*/div',
  2882. '/fieldset'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. $this->Form->create('Contact');
  2886. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2887. $this->assertTags($result, $expected);
  2888. $this->Form->end();
  2889. $this->Form->create(false);
  2890. $expected = array(
  2891. 'fieldset' => array(),
  2892. array('div' => array('class' => 'input text')),
  2893. 'label' => array('for' => 'foo'),
  2894. 'Foo',
  2895. '/label',
  2896. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2897. '*/div',
  2898. '/fieldset'
  2899. );
  2900. $result = $this->Form->inputs(
  2901. array('foo' => array('type' => 'text')),
  2902. array(),
  2903. array('legend' => false)
  2904. );
  2905. $this->assertTags($result, $expected);
  2906. }
  2907. /**
  2908. * Tests inputs() works with plugin models
  2909. *
  2910. * @return void
  2911. */
  2912. public function testInputsPluginModel() {
  2913. $this->loadFixtures('Post');
  2914. App::build(array(
  2915. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2916. ));
  2917. CakePlugin::load('TestPlugin');
  2918. $this->Form->request['models'] = array(
  2919. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2920. );
  2921. $this->Form->create('TestPlugin.TestPluginPost');
  2922. $result = $this->Form->inputs();
  2923. $this->assertContains('data[TestPluginPost][id]', $result);
  2924. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2925. $this->assertContains('data[TestPluginPost][title]', $result);
  2926. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2927. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2928. $this->assertEquals('TestPluginPost', $this->Form->model());
  2929. }
  2930. /**
  2931. * testSelectAsCheckbox method
  2932. *
  2933. * test multi-select widget with checkbox formatting.
  2934. *
  2935. * @return void
  2936. */
  2937. public function testSelectAsCheckbox() {
  2938. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2939. $expected = array(
  2940. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2941. array('div' => array('class' => 'checkbox')),
  2942. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2943. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2944. 'first',
  2945. '/label',
  2946. '/div',
  2947. array('div' => array('class' => 'checkbox')),
  2948. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2949. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2950. 'second',
  2951. '/label',
  2952. '/div',
  2953. array('div' => array('class' => 'checkbox')),
  2954. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2955. array('label' => array('for' => 'ModelMultiField2')),
  2956. 'third',
  2957. '/label',
  2958. '/div',
  2959. );
  2960. $this->assertTags($result, $expected);
  2961. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2962. $expected = array(
  2963. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2964. array('div' => array('class' => 'checkbox')),
  2965. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2966. array('label' => array('for' => 'ModelMultiField12')),
  2967. 'half',
  2968. '/label',
  2969. '/div',
  2970. );
  2971. $this->assertTags($result, $expected);
  2972. }
  2973. /**
  2974. * testLabel method
  2975. *
  2976. * test label generation.
  2977. *
  2978. * @return void
  2979. */
  2980. public function testLabel() {
  2981. $this->Form->text('Person.name');
  2982. $result = $this->Form->label();
  2983. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2984. $this->Form->text('Person.name');
  2985. $result = $this->Form->label();
  2986. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2987. $result = $this->Form->label('Person.first_name');
  2988. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2989. $result = $this->Form->label('Person.first_name', 'Your first name');
  2990. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2991. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2992. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2993. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2994. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2995. $result = $this->Form->label('Person.first_name', '');
  2996. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2997. $result = $this->Form->label('Person.2.name', '');
  2998. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2999. }
  3000. /**
  3001. * testTextbox method
  3002. *
  3003. * test textbox element generation
  3004. *
  3005. * @return void
  3006. */
  3007. public function testTextbox() {
  3008. $result = $this->Form->text('Model.field');
  3009. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3010. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3011. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3012. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3013. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3014. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3015. $result = $this->Form->text('Model.text');
  3016. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3017. $Contact = ClassRegistry::getObject('Contact');
  3018. $Contact->validationErrors['text'] = array(true);
  3019. $this->Form->request->data['Contact']['text'] = 'test';
  3020. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3021. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3022. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3023. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3024. $expected = array(
  3025. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3026. );
  3027. $this->assertTags($result, $expected);
  3028. }
  3029. /**
  3030. * testDefaultValue method
  3031. *
  3032. * Test default value setting
  3033. *
  3034. * @return void
  3035. */
  3036. public function testDefaultValue() {
  3037. $this->Form->request->data['Model']['field'] = 'test';
  3038. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3039. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3040. unset($this->Form->request->data['Model']['field']);
  3041. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3042. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3043. }
  3044. /**
  3045. * testCheckboxDefaultValue method
  3046. *
  3047. * Test default value setting on checkbox() method
  3048. *
  3049. * @return void
  3050. */
  3051. public function testCheckboxDefaultValue() {
  3052. $this->Form->request->data['Model']['field'] = false;
  3053. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3054. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3055. unset($this->Form->request->data['Model']['field']);
  3056. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3057. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3058. $this->Form->request->data['Model']['field'] = true;
  3059. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3060. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3061. unset($this->Form->request->data['Model']['field']);
  3062. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3063. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3064. }
  3065. /**
  3066. * testError method
  3067. *
  3068. * Test field error generation
  3069. *
  3070. * @return void
  3071. */
  3072. public function testError() {
  3073. $Contact = ClassRegistry::getObject('Contact');
  3074. $Contact->validationErrors['field'] = array(1);
  3075. $result = $this->Form->error('Contact.field');
  3076. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3077. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3078. $this->assertEquals('Error in field Field', $result);
  3079. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3080. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3081. $this->assertEquals('This field contains invalid input', $result);
  3082. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3083. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3084. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3085. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3086. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3087. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3088. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3089. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3090. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3091. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3092. $this->assertEquals('<strong>Badness!</strong>', $result);
  3093. $Contact->validationErrors['field'] = array("email");
  3094. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3095. $expected = array(
  3096. 'div' => array('class' => 'field-error'),
  3097. 'No good!',
  3098. '/div'
  3099. );
  3100. $this->assertTags($result, $expected);
  3101. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3102. $result = $this->Form->error('Contact.field', array(
  3103. 'notEmpty' => 'Cannot be empty',
  3104. 'email' => 'No good!'
  3105. ));
  3106. $expected = array(
  3107. 'div' => array('class' => 'error-message'),
  3108. 'ul' => array(),
  3109. '<li', 'Cannot be empty', '/li',
  3110. '<li', 'No good!', '/li',
  3111. '<li', 'Something else', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. // Testing error messages list options
  3117. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3118. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3119. $expected = array(
  3120. 'div' => array('class' => 'error-message'),
  3121. 'ol' => array(),
  3122. '<li', 'notEmpty', '/li',
  3123. '<li', 'email', '/li',
  3124. '/ol',
  3125. '/div'
  3126. );
  3127. $this->assertTags($result, $expected);
  3128. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3129. $expected = array(
  3130. 'div' => array('class' => 'error-message'),
  3131. 'ol' => array(),
  3132. '<li', 'notEmpty', '/li',
  3133. '<li', 'email', '/li',
  3134. '/ol',
  3135. '/div'
  3136. );
  3137. $this->assertTags($result, $expected);
  3138. $result = $this->Form->error('Contact.field', null, array(
  3139. 'listOptions' => array(
  3140. 'class' => 'ul-class',
  3141. 'itemOptions' => array(
  3142. 'class' => 'li-class'
  3143. )
  3144. )
  3145. ));
  3146. $expected = array(
  3147. 'div' => array('class' => 'error-message'),
  3148. 'ul' => array('class' => 'ul-class'),
  3149. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3150. array('li' => array('class' => 'li-class')), 'email', '/li',
  3151. '/ul',
  3152. '/div'
  3153. );
  3154. $this->assertTags($result, $expected);
  3155. }
  3156. /**
  3157. * test error options when using form->input();
  3158. *
  3159. * @return void
  3160. */
  3161. public function testInputErrorEscape() {
  3162. $this->Form->create('ValidateProfile');
  3163. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3164. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3165. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3166. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3167. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3168. $this->assertRegExp('/required<br>/', $result);
  3169. }
  3170. /**
  3171. * testPassword method
  3172. *
  3173. * Test password element generation
  3174. *
  3175. * @return void
  3176. */
  3177. public function testPassword() {
  3178. $Contact = ClassRegistry::getObject('Contact');
  3179. $result = $this->Form->password('Contact.field');
  3180. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3181. $Contact->validationErrors['passwd'] = 1;
  3182. $this->Form->request->data['Contact']['passwd'] = 'test';
  3183. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3184. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3185. }
  3186. /**
  3187. * testRadio method
  3188. *
  3189. * Test radio element set generation
  3190. *
  3191. * @return void
  3192. */
  3193. public function testRadio() {
  3194. $result = $this->Form->radio('Model.field', array('option A'));
  3195. $expected = array(
  3196. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3197. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3198. 'label' => array('for' => 'ModelField0'),
  3199. 'option A',
  3200. '/label'
  3201. );
  3202. $this->assertTags($result, $expected);
  3203. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3204. $expected = array(
  3205. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3206. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3207. 'label' => array('for' => 'ModelField12'),
  3208. 'half',
  3209. '/label'
  3210. );
  3211. $this->assertTags($result, $expected);
  3212. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3213. $expected = array(
  3214. 'fieldset' => array(),
  3215. 'legend' => array(),
  3216. 'Field',
  3217. '/legend',
  3218. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3219. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3220. array('label' => array('for' => 'ModelField0')),
  3221. 'option A',
  3222. '/label',
  3223. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3224. array('label' => array('for' => 'ModelField1')),
  3225. 'option B',
  3226. '/label',
  3227. '/fieldset'
  3228. );
  3229. $this->assertTags($result, $expected);
  3230. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3231. $expected = array(
  3232. 'fieldset' => array(),
  3233. 'legend' => array(),
  3234. 'Field',
  3235. '/legend',
  3236. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3237. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3238. array('label' => array('for' => 'ModelField0')),
  3239. 'option A',
  3240. '/label',
  3241. 'br' => array(),
  3242. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3243. array('label' => array('for' => 'ModelField1')),
  3244. 'option B',
  3245. '/label',
  3246. '/fieldset'
  3247. );
  3248. $this->assertTags($result, $expected);
  3249. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3250. $expected = array(
  3251. 'fieldset' => array(),
  3252. 'legend' => array(),
  3253. 'Gender',
  3254. '/legend',
  3255. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3256. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3257. array('label' => array('for' => 'EmployeeGenderMale')),
  3258. 'Male',
  3259. '/label',
  3260. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3261. array('label' => array('for' => 'EmployeeGenderFemale')),
  3262. 'Female',
  3263. '/label',
  3264. '/fieldset',
  3265. );
  3266. $this->assertTags($result, $expected);
  3267. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3268. $expected = array(
  3269. 'fieldset' => array(),
  3270. 'legend' => array(),
  3271. 'Gender',
  3272. '/legend',
  3273. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3274. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3275. array('label' => array('for' => 'OfficerGenderMale')),
  3276. 'Male',
  3277. '/label',
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3279. array('label' => array('for' => 'OfficerGenderFemale')),
  3280. 'Female',
  3281. '/label',
  3282. '/fieldset',
  3283. );
  3284. $this->assertTags($result, $expected);
  3285. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3286. $expected = array(
  3287. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3288. array('input' => array(
  3289. 'type' => 'radio',
  3290. 'name' => 'data[Contact][1][imrequired]',
  3291. 'value' => '0',
  3292. 'id' => 'Contact1Imrequired0',
  3293. 'required' => 'required'
  3294. )),
  3295. 'label' => array('for' => 'Contact1Imrequired0'),
  3296. 'option A',
  3297. '/label'
  3298. );
  3299. $this->assertTags($result, $expected);
  3300. $result = $this->Form->radio('Model.1.field', array('option A'));
  3301. $expected = array(
  3302. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3303. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3304. 'label' => array('for' => 'Model1Field0'),
  3305. 'option A',
  3306. '/label'
  3307. );
  3308. $this->assertTags($result, $expected);
  3309. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3310. $expected = array(
  3311. 'fieldset' => array(),
  3312. 'legend' => array(),
  3313. 'Field',
  3314. '/legend',
  3315. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3316. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3317. array('label' => array('for' => 'ModelField0')),
  3318. 'option A',
  3319. '/label',
  3320. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3321. array('label' => array('for' => 'ModelField1')),
  3322. 'option B',
  3323. '/label',
  3324. '/fieldset'
  3325. );
  3326. $this->assertTags($result, $expected);
  3327. }
  3328. /**
  3329. * Test radio inputs with between as string or array. Also ensure
  3330. * that an array with less between elements works.
  3331. *
  3332. * @return void
  3333. */
  3334. public function testRadioBetween() {
  3335. $result = $this->Form->radio(
  3336. 'Model.field',
  3337. array('option A', 'option B'),
  3338. array('between' => 'I am between')
  3339. );
  3340. $expected = array(
  3341. 'fieldset' => array(),
  3342. 'legend' => array(),
  3343. 'Field',
  3344. '/legend',
  3345. 'I am between',
  3346. 'input' => array(
  3347. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3348. 'value' => '', 'id' => 'ModelField_'
  3349. ),
  3350. array('input' => array(
  3351. 'type' => 'radio', 'name' => 'data[Model][field]',
  3352. 'value' => '0', 'id' => 'ModelField0'
  3353. )),
  3354. array('label' => array('for' => 'ModelField0')),
  3355. 'option A',
  3356. '/label',
  3357. array('input' => array(
  3358. 'type' => 'radio', 'name' => 'data[Model][field]',
  3359. 'value' => '1', 'id' => 'ModelField1'
  3360. )),
  3361. array('label' => array('for' => 'ModelField1')),
  3362. 'option B',
  3363. '/label',
  3364. '/fieldset'
  3365. );
  3366. $this->assertTags($result, $expected);
  3367. $result = $this->Form->radio(
  3368. 'Model.field',
  3369. array('option A', 'option B', 'option C'),
  3370. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3371. );
  3372. $expected = array(
  3373. 'fieldset' => array(),
  3374. 'legend' => array(),
  3375. 'Field',
  3376. '/legend',
  3377. 'input' => array(
  3378. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3379. 'value' => '', 'id' => 'ModelField_'
  3380. ),
  3381. array('input' => array(
  3382. 'type' => 'radio', 'name' => 'data[Model][field]',
  3383. 'value' => '0', 'id' => 'ModelField0'
  3384. )),
  3385. array('label' => array('for' => 'ModelField0')),
  3386. 'option A',
  3387. '/label',
  3388. 'between A',
  3389. '--separator--',
  3390. array('input' => array(
  3391. 'type' => 'radio', 'name' => 'data[Model][field]',
  3392. 'value' => '1', 'id' => 'ModelField1'
  3393. )),
  3394. array('label' => array('for' => 'ModelField1')),
  3395. 'option B',
  3396. '/label',
  3397. 'between B',
  3398. '--separator--',
  3399. array('input' => array(
  3400. 'type' => 'radio', 'name' => 'data[Model][field]',
  3401. 'value' => '2', 'id' => 'ModelField2'
  3402. )),
  3403. array('label' => array('for' => 'ModelField2')),
  3404. 'option C',
  3405. '/label',
  3406. 'between C',
  3407. '/fieldset'
  3408. );
  3409. $this->assertTags($result, $expected);
  3410. $result = $this->Form->input('Model.field', array(
  3411. 'options' => array('1' => 'first', '2' => 'second'),
  3412. 'type' => 'radio',
  3413. 'before' => '--before--',
  3414. 'after' => '--after--',
  3415. 'separator' => '--separator--',
  3416. 'between' => array('--between first--', '--between second--')
  3417. ));
  3418. $expected = array(
  3419. 'div' => array('class' => 'input radio'),
  3420. '--before--',
  3421. 'fieldset' => array(),
  3422. 'legend' => array(),
  3423. 'Field',
  3424. '/legend',
  3425. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3426. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3427. array('label' => array('for' => 'ModelField1')),
  3428. 'first',
  3429. '/label',
  3430. '--between first--',
  3431. '--separator--',
  3432. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3433. array('label' => array('for' => 'ModelField2')),
  3434. 'second',
  3435. '/label',
  3436. '--between second--',
  3437. '/fieldset',
  3438. '--after--',
  3439. '/div'
  3440. );
  3441. $this->assertTags($result, $expected);
  3442. $result = $this->Form->input('Model.field', array(
  3443. 'options' => array('1' => 'first', '2' => 'second'),
  3444. 'type' => 'radio',
  3445. 'before' => '--before--',
  3446. 'after' => '--after--',
  3447. 'separator' => '--separator--',
  3448. 'between' => array('--between first--')
  3449. ));
  3450. $expected = array(
  3451. 'div' => array('class' => 'input radio'),
  3452. '--before--',
  3453. 'fieldset' => array(),
  3454. 'legend' => array(),
  3455. 'Field',
  3456. '/legend',
  3457. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3458. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3459. array('label' => array('for' => 'ModelField1')),
  3460. 'first',
  3461. '/label',
  3462. '--between first--',
  3463. '--separator--',
  3464. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3465. array('label' => array('for' => 'ModelField2')),
  3466. 'second',
  3467. '/label',
  3468. '/fieldset',
  3469. '--after--',
  3470. '/div'
  3471. );
  3472. $this->assertTags($result, $expected);
  3473. }
  3474. /**
  3475. * Test that radios with a 0 value are selected under the correct conditions.
  3476. * Also ensure that values that are booleanish are handled correctly.
  3477. *
  3478. * @return void
  3479. */
  3480. public function testRadioOptionWithBooleanishValues() {
  3481. $expected = array(
  3482. 'fieldset' => array(),
  3483. 'legend' => array(),
  3484. 'Field',
  3485. '/legend',
  3486. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3487. array('label' => array('for' => 'ModelField1')),
  3488. 'Yes',
  3489. '/label',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3491. array('label' => array('for' => 'ModelField0')),
  3492. 'No',
  3493. '/label',
  3494. '/fieldset'
  3495. );
  3496. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3497. $this->assertTags($result, $expected);
  3498. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3501. $this->assertTags($result, $expected);
  3502. $expected = array(
  3503. 'fieldset' => array(),
  3504. 'legend' => array(),
  3505. 'Field',
  3506. '/legend',
  3507. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3508. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3509. array('label' => array('for' => 'ModelField1')),
  3510. 'Yes',
  3511. '/label',
  3512. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3513. array('label' => array('for' => 'ModelField0')),
  3514. 'No',
  3515. '/label',
  3516. '/fieldset'
  3517. );
  3518. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3519. $this->assertTags($result, $expected);
  3520. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3521. $this->assertTags($result, $expected);
  3522. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3523. $this->assertTags($result, $expected);
  3524. $expected = array(
  3525. 'fieldset' => array(),
  3526. 'legend' => array(),
  3527. 'Field',
  3528. '/legend',
  3529. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3530. array('label' => array('for' => 'ModelField1')),
  3531. 'Yes',
  3532. '/label',
  3533. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3534. array('label' => array('for' => 'ModelField0')),
  3535. 'No',
  3536. '/label',
  3537. '/fieldset'
  3538. );
  3539. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3540. $this->assertTags($result, $expected);
  3541. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3542. $this->assertTags($result, $expected);
  3543. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3544. $this->assertTags($result, $expected);
  3545. }
  3546. /**
  3547. * test disabled radio options
  3548. *
  3549. * @return void
  3550. */
  3551. public function testRadioDisabled() {
  3552. $result = $this->Form->radio(
  3553. 'Model.field',
  3554. array('option A', 'option B'),
  3555. array('disabled' => array(0), 'value' => '0')
  3556. );
  3557. $expected = array(
  3558. 'fieldset' => array(),
  3559. 'legend' => array(),
  3560. 'Field',
  3561. '/legend',
  3562. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3563. array('label' => array('for' => 'ModelField0')),
  3564. 'option A',
  3565. '/label',
  3566. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3567. array('label' => array('for' => 'ModelField1')),
  3568. 'option B',
  3569. '/label',
  3570. '/fieldset'
  3571. );
  3572. $this->assertTags($result, $expected);
  3573. $result = $this->Form->radio(
  3574. 'Model.field',
  3575. array('option A', 'option B'),
  3576. array('disabled' => true, 'value' => '0')
  3577. );
  3578. $expected = array(
  3579. 'fieldset' => array(),
  3580. 'legend' => array(),
  3581. 'Field',
  3582. '/legend',
  3583. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3584. array('label' => array('for' => 'ModelField0')),
  3585. 'option A',
  3586. '/label',
  3587. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3588. array('label' => array('for' => 'ModelField1')),
  3589. 'option B',
  3590. '/label',
  3591. '/fieldset'
  3592. );
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->radio(
  3595. 'Model.field',
  3596. array('option A', 'option B'),
  3597. array('disabled' => 'disabled', 'value' => '0')
  3598. );
  3599. $expected = array(
  3600. 'fieldset' => array(),
  3601. 'legend' => array(),
  3602. 'Field',
  3603. '/legend',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3605. array('label' => array('for' => 'ModelField0')),
  3606. 'option A',
  3607. '/label',
  3608. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3609. array('label' => array('for' => 'ModelField1')),
  3610. 'option B',
  3611. '/label',
  3612. '/fieldset'
  3613. );
  3614. $this->assertTags($result, $expected);
  3615. $result = $this->Form->input('Model.field', array(
  3616. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3617. 'type' => 'radio',
  3618. 'disabled' => array(2, '3x'),
  3619. ));
  3620. $expected = array(
  3621. 'div' => array('class' => 'input radio'),
  3622. 'fieldset' => array(),
  3623. 'legend' => array(),
  3624. 'Field',
  3625. '/legend',
  3626. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3627. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3628. array('label' => array('for' => 'ModelField1')),
  3629. 'first',
  3630. '/label',
  3631. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3632. array('label' => array('for' => 'ModelField2')),
  3633. 'second',
  3634. '/label',
  3635. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3636. array('label' => array('for' => 'ModelField2x')),
  3637. '2x',
  3638. '/label',
  3639. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3640. array('label' => array('for' => 'ModelField3')),
  3641. 'third',
  3642. '/label',
  3643. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3644. array('label' => array('for' => 'ModelField3x')),
  3645. '3x',
  3646. '/label',
  3647. '/fieldset',
  3648. '/div'
  3649. );
  3650. $this->assertTags($result, $expected);
  3651. $result = $this->Form->input('Model.field', array(
  3652. 'type' => 'radio',
  3653. 'options' => array(
  3654. 1 => 'A',
  3655. 2 => 'B',
  3656. 3 => 'C'
  3657. ),
  3658. 'disabled' => array(1)
  3659. ));
  3660. $expected = array(
  3661. 'div' => array('class' => 'input radio'),
  3662. 'fieldset' => array(),
  3663. 'legend' => array(),
  3664. 'Field',
  3665. '/legend',
  3666. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3667. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3668. array('label' => array('for' => 'ModelField1')),
  3669. 'A',
  3670. '/label',
  3671. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3672. array('label' => array('for' => 'ModelField2')),
  3673. 'B',
  3674. '/label',
  3675. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3676. array('label' => array('for' => 'ModelField3')),
  3677. 'C',
  3678. '/label',
  3679. '/fieldset',
  3680. '/div'
  3681. );
  3682. $this->assertTags($result, $expected);
  3683. }
  3684. /**
  3685. * test disabling the hidden input for radio buttons
  3686. *
  3687. * @return void
  3688. */
  3689. public function testRadioHiddenInputDisabling() {
  3690. $result = $this->Form->input('Model.1.field', array(
  3691. 'type' => 'radio',
  3692. 'options' => array('option A'),
  3693. 'hiddenField' => false
  3694. )
  3695. );
  3696. $expected = array(
  3697. 'div' => array('class' => 'input radio'),
  3698. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3699. 'label' => array('for' => 'Model1Field0'),
  3700. 'option A',
  3701. '/label',
  3702. '/div'
  3703. );
  3704. $this->assertTags($result, $expected);
  3705. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3706. $expected = array(
  3707. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3708. 'label' => array('for' => 'Model1Field0'),
  3709. 'option A',
  3710. '/label'
  3711. );
  3712. $this->assertTags($result, $expected);
  3713. }
  3714. /**
  3715. * test adding an empty option for radio buttons
  3716. *
  3717. * @return void
  3718. */
  3719. public function testRadioAddEmptyOption() {
  3720. $result = $this->Form->input('Model.1.field', array(
  3721. 'type' => 'radio',
  3722. 'options' => array('option A'),
  3723. 'empty' => true,
  3724. 'hiddenField' => false
  3725. ));
  3726. $expected = array(
  3727. 'div' => array('class' => 'input radio'),
  3728. 'fieldset' => array(),
  3729. 'legend' => array(),
  3730. 'Field',
  3731. '/legend',
  3732. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3733. array('label' => array('for' => 'Model1Field')),
  3734. __('empty'),
  3735. '/label',
  3736. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3737. array('label' => array('for' => 'Model1Field0')),
  3738. 'option A',
  3739. '/label',
  3740. '/fieldset',
  3741. '/div'
  3742. );
  3743. $this->assertTags($result, $expected);
  3744. $result = $this->Form->input('Model.1.field', array(
  3745. 'type' => 'radio',
  3746. 'options' => array('option A'),
  3747. 'empty' => 'CustomEmptyLabel',
  3748. 'hiddenField' => false
  3749. ));
  3750. $expected = array(
  3751. 'div' => array('class' => 'input radio'),
  3752. 'fieldset' => array(),
  3753. 'legend' => array(),
  3754. 'Field',
  3755. '/legend',
  3756. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3757. array('label' => array('for' => 'Model1Field')),
  3758. 'CustomEmptyLabel',
  3759. '/label',
  3760. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3761. array('label' => array('for' => 'Model1Field0')),
  3762. 'option A',
  3763. '/label',
  3764. '/fieldset',
  3765. '/div'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->input('Model.1.field', array(
  3769. 'type' => 'radio',
  3770. 'options' => array('option A'),
  3771. 'empty' => false,
  3772. 'hiddenField' => false
  3773. ));
  3774. $this->assertTextNotContains('"Model1Field"', $result);
  3775. }
  3776. /**
  3777. * Test that radio() accepts an array for label
  3778. *
  3779. * @return void
  3780. */
  3781. public function testRadioLabelArray() {
  3782. $result = $this->Form->input('Model.field', array(
  3783. 'type' => 'radio',
  3784. 'legend' => false,
  3785. 'label' => array(
  3786. 'class' => 'checkbox float-left',
  3787. ),
  3788. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3789. ));
  3790. $this->assertTextContains(
  3791. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3792. $result
  3793. );
  3794. }
  3795. /**
  3796. * Test that label id's match the input element id's when radio is called after create().
  3797. *
  3798. * @return void
  3799. */
  3800. public function testRadioWithCreate() {
  3801. $this->Form->create('Model');
  3802. $result = $this->Form->radio('recipient',
  3803. array('1' => '1', '2' => '2', '3' => '3'),
  3804. array('legend' => false, 'value' => '1')
  3805. );
  3806. $this->assertTextNotContains(
  3807. '<label for="ModelModelRecipient1">1</label>',
  3808. $result
  3809. );
  3810. $this->assertTextContains(
  3811. '<label for="ModelRecipient1">1</label>',
  3812. $result
  3813. );
  3814. }
  3815. /**
  3816. * testSelect method
  3817. *
  3818. * Test select element generation.
  3819. *
  3820. * @return void
  3821. */
  3822. public function testSelect() {
  3823. $result = $this->Form->select('Model.field', array());
  3824. $expected = array(
  3825. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3826. array('option' => array('value' => '')),
  3827. '/option',
  3828. '/select'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3832. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3833. $expected = array(
  3834. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3835. array('option' => array('value' => '')),
  3836. '/option',
  3837. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3838. 'good',
  3839. '/option',
  3840. array('option' => array('value' => 'other')),
  3841. 'bad',
  3842. '/option',
  3843. '/select'
  3844. );
  3845. $this->assertTags($result, $expected);
  3846. $this->Form->request->data = array();
  3847. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3848. $expected = array(
  3849. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3850. array('option' => array('value' => '')),
  3851. '/option',
  3852. array('option' => array('value' => 'value')),
  3853. 'good',
  3854. '/option',
  3855. array('option' => array('value' => 'other')),
  3856. 'bad',
  3857. '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. $result = $this->Form->select(
  3862. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3863. array('empty' => false)
  3864. );
  3865. $expected = array(
  3866. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3867. array('option' => array('value' => 'first')),
  3868. 'first &quot;html&quot; &lt;chars&gt;',
  3869. '/option',
  3870. array('option' => array('value' => 'second')),
  3871. 'value',
  3872. '/option',
  3873. '/select'
  3874. );
  3875. $this->assertTags($result, $expected);
  3876. $result = $this->Form->select(
  3877. 'Model.field',
  3878. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3879. array('escape' => false, 'empty' => false)
  3880. );
  3881. $expected = array(
  3882. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3883. array('option' => array('value' => 'first')),
  3884. 'first "html" <chars>',
  3885. '/option',
  3886. array('option' => array('value' => 'second')),
  3887. 'value',
  3888. '/option',
  3889. '/select'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. $options = array(
  3893. array('value' => 'first', 'name' => 'First'),
  3894. array('value' => 'first', 'name' => 'Another First'),
  3895. );
  3896. $result = $this->Form->select(
  3897. 'Model.field',
  3898. $options,
  3899. array('escape' => false, 'empty' => false)
  3900. );
  3901. $expected = array(
  3902. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3903. array('option' => array('value' => 'first')),
  3904. 'First',
  3905. '/option',
  3906. array('option' => array('value' => 'first')),
  3907. 'Another First',
  3908. '/option',
  3909. '/select'
  3910. );
  3911. $this->assertTags($result, $expected);
  3912. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3913. $result = $this->Form->select(
  3914. 'Model.contact_id',
  3915. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3916. array('escape' => false, 'empty' => 'pick something')
  3917. );
  3918. $expected = array(
  3919. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3920. array('option' => array('value' => '')), 'pick something', '/option',
  3921. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3922. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3923. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3924. '/select'
  3925. );
  3926. $this->assertTags($result, $expected);
  3927. $this->Form->request->data['Model']['field'] = 0;
  3928. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3929. $expected = array(
  3930. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3931. array('option' => array('value' => '')), '/option',
  3932. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3933. array('option' => array('value' => '1')), 'Yes', '/option',
  3934. '/select'
  3935. );
  3936. $this->assertTags($result, $expected);
  3937. $this->Form->request->data['Model']['field'] = 50;
  3938. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3939. $expected = array(
  3940. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3941. array('option' => array('value' => '')), '/option',
  3942. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3943. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3944. '/select'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. $result = $this->Form->select('Contact.required_one', array('option A'));
  3948. $expected = array(
  3949. 'select' => array(
  3950. 'name' => 'data[Contact][required_one]',
  3951. 'id' => 'ContactRequiredOne',
  3952. 'required' => 'required'
  3953. ),
  3954. array('option' => array('value' => '')), '/option',
  3955. array('option' => array('value' => '0')), 'option A', '/option',
  3956. '/select'
  3957. );
  3958. $this->assertTags($result, $expected);
  3959. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3960. $expected = array(
  3961. 'select' => array(
  3962. 'name' => 'data[Contact][required_one]',
  3963. 'id' => 'ContactRequiredOne',
  3964. 'disabled' => 'disabled'
  3965. ),
  3966. array('option' => array('value' => '')), '/option',
  3967. array('option' => array('value' => '0')), 'option A', '/option',
  3968. '/select'
  3969. );
  3970. $this->assertTags($result, $expected);
  3971. }
  3972. /**
  3973. * test that select() with optiongroups listens to the escape param.
  3974. *
  3975. * @return void
  3976. */
  3977. public function testSelectOptionGroupEscaping() {
  3978. $options = array(
  3979. '>< Key' => array(
  3980. 1 => 'One',
  3981. 2 => 'Two'
  3982. )
  3983. );
  3984. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3985. $expected = array(
  3986. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3987. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3988. array('option' => array('value' => '1')), 'One', '/option',
  3989. array('option' => array('value' => '2')), 'Two', '/option',
  3990. '/optgroup',
  3991. '/select'
  3992. );
  3993. $this->assertTags($result, $expected);
  3994. $options = array(
  3995. '>< Key' => array(
  3996. 1 => 'One',
  3997. 2 => 'Two'
  3998. )
  3999. );
  4000. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4001. $expected = array(
  4002. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4003. 'optgroup' => array('label' => '>< Key'),
  4004. array('option' => array('value' => '1')), 'One', '/option',
  4005. array('option' => array('value' => '2')), 'Two', '/option',
  4006. '/optgroup',
  4007. '/select'
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. }
  4011. /**
  4012. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4013. *
  4014. * @return void
  4015. */
  4016. public function testSelectWithNullAttributes() {
  4017. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4018. $expected = array(
  4019. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4020. array('option' => array('value' => '0')),
  4021. 'first',
  4022. '/option',
  4023. array('option' => array('value' => '1')),
  4024. 'second',
  4025. '/option',
  4026. '/select'
  4027. );
  4028. $this->assertTags($result, $expected);
  4029. }
  4030. /**
  4031. * testNestedSelect method
  4032. *
  4033. * test select element generation with optgroups
  4034. *
  4035. * @return void
  4036. */
  4037. public function testNestedSelect() {
  4038. $result = $this->Form->select(
  4039. 'Model.field',
  4040. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4041. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4042. )), array('empty' => false)
  4043. );
  4044. $expected = array(
  4045. 'select' => array('name' => 'data[Model][field]',
  4046. 'id' => 'ModelField'),
  4047. array('option' => array('value' => 1)),
  4048. 'One',
  4049. '/option',
  4050. array('option' => array('value' => 2)),
  4051. 'Two',
  4052. '/option',
  4053. array('optgroup' => array('label' => 'Three')),
  4054. array('option' => array('value' => 4)),
  4055. 'Four',
  4056. '/option',
  4057. array('option' => array('value' => 5)),
  4058. 'Five',
  4059. '/option',
  4060. '/optgroup',
  4061. '/select'
  4062. );
  4063. $this->assertTags($result, $expected);
  4064. $result = $this->Form->select(
  4065. 'Model.field',
  4066. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4067. array('showParents' => true, 'empty' => false)
  4068. );
  4069. $expected = array(
  4070. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4071. array('option' => array('value' => 1)),
  4072. 'One',
  4073. '/option',
  4074. array('option' => array('value' => 2)),
  4075. 'Two',
  4076. '/option',
  4077. array('optgroup' => array('label' => 'Three')),
  4078. array('option' => array('value' => 3)),
  4079. 'Three',
  4080. '/option',
  4081. array('option' => array('value' => 4)),
  4082. 'Four',
  4083. '/option',
  4084. '/optgroup',
  4085. '/select'
  4086. );
  4087. $this->assertTags($result, $expected);
  4088. }
  4089. /**
  4090. * testSelectMultiple method
  4091. *
  4092. * test generation of multiple select elements
  4093. *
  4094. * @return void
  4095. */
  4096. public function testSelectMultiple() {
  4097. $options = array('first', 'second', 'third');
  4098. $result = $this->Form->select(
  4099. 'Model.multi_field', $options, array('multiple' => true)
  4100. );
  4101. $expected = array(
  4102. 'input' => array(
  4103. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4104. ),
  4105. 'select' => array(
  4106. 'name' => 'data[Model][multi_field][]',
  4107. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4108. ),
  4109. array('option' => array('value' => '0')),
  4110. 'first',
  4111. '/option',
  4112. array('option' => array('value' => '1')),
  4113. 'second',
  4114. '/option',
  4115. array('option' => array('value' => '2')),
  4116. 'third',
  4117. '/option',
  4118. '/select'
  4119. );
  4120. $this->assertTags($result, $expected);
  4121. $result = $this->Form->select(
  4122. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4123. );
  4124. $expected = array(
  4125. 'input' => array(
  4126. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4127. ),
  4128. 'select' => array(
  4129. 'name' => 'data[Model][multi_field][]',
  4130. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4131. ),
  4132. array('option' => array('value' => '0')),
  4133. 'first',
  4134. '/option',
  4135. array('option' => array('value' => '1')),
  4136. 'second',
  4137. '/option',
  4138. array('option' => array('value' => '2')),
  4139. 'third',
  4140. '/option',
  4141. '/select'
  4142. );
  4143. $this->assertTags($result, $expected);
  4144. $result = $this->Form->select(
  4145. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4146. );
  4147. $expected = array(
  4148. 'input' => array(
  4149. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4150. ),
  4151. 'select' => array(
  4152. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4153. 'multiple' => 'multiple'
  4154. ),
  4155. array('option' => array('value' => '0', 'selected' => 'selected')),
  4156. 'first',
  4157. '/option',
  4158. array('option' => array('value' => '1', 'selected' => 'selected')),
  4159. 'second',
  4160. '/option',
  4161. array('option' => array('value' => '2')),
  4162. 'third',
  4163. '/option',
  4164. '/select'
  4165. );
  4166. $this->assertTags($result, $expected);
  4167. $result = $this->Form->select(
  4168. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4169. );
  4170. $expected = array(
  4171. 'select' => array(
  4172. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4173. ),
  4174. array('option' => array('value' => '0', 'selected' => 'selected')),
  4175. 'first',
  4176. '/option',
  4177. array('option' => array('value' => '1', 'selected' => 'selected')),
  4178. 'second',
  4179. '/option',
  4180. array('option' => array('value' => '2')),
  4181. 'third',
  4182. '/option',
  4183. '/select'
  4184. );
  4185. $this->assertTags($result, $expected);
  4186. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4187. $selected = array('2', '3x');
  4188. $result = $this->Form->select(
  4189. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4190. );
  4191. $expected = array(
  4192. 'input' => array(
  4193. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4194. ),
  4195. 'select' => array(
  4196. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4197. ),
  4198. array('option' => array('value' => '1')),
  4199. 'One',
  4200. '/option',
  4201. array('option' => array('value' => '2', 'selected' => 'selected')),
  4202. 'Two',
  4203. '/option',
  4204. array('option' => array('value' => '3')),
  4205. 'Three',
  4206. '/option',
  4207. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4208. 'Stringy',
  4209. '/option',
  4210. '/select'
  4211. );
  4212. $this->assertTags($result, $expected);
  4213. $result = $this->Form->select('Contact.required_one', array(
  4214. '1' => 'option A',
  4215. '2' => 'option B'
  4216. ), array('multiple' => true));
  4217. $expected = array(
  4218. 'input' => array(
  4219. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4220. ),
  4221. 'select' => array(
  4222. 'name' => 'data[Contact][required_one][]',
  4223. 'id' => 'ContactRequiredOne',
  4224. 'required' => 'required',
  4225. 'multiple' => 'multiple'
  4226. ),
  4227. array('option' => array('value' => '1')), 'option A', '/option',
  4228. array('option' => array('value' => '2')), 'option B', '/option',
  4229. '/select'
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. }
  4233. /**
  4234. * Test generating multiple select with disabled elements.
  4235. *
  4236. * @return void
  4237. */
  4238. public function testSelectMultipleWithDisabledElements() {
  4239. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4240. $disabled = array(2, 3);
  4241. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4242. $expected = array(
  4243. 'input' => array(
  4244. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4245. ),
  4246. 'select' => array(
  4247. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4248. ),
  4249. array('option' => array('value' => '1')),
  4250. 'One',
  4251. '/option',
  4252. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4253. 'Two',
  4254. '/option',
  4255. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4256. 'Three',
  4257. '/option',
  4258. array('option' => array('value' => '3x')),
  4259. 'Stringy',
  4260. '/option',
  4261. '/select'
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4265. $disabled = array('2', '3x');
  4266. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4267. $expected = array(
  4268. array('div' => array('class' => 'input select')),
  4269. array('label' => array('for' => 'ContactMultiple')),
  4270. 'Multiple',
  4271. '/label',
  4272. 'input' => array(
  4273. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4274. ),
  4275. 'select' => array(
  4276. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4277. ),
  4278. array('option' => array('value' => '1')),
  4279. 'One',
  4280. '/option',
  4281. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4282. 'Two',
  4283. '/option',
  4284. array('option' => array('value' => '3')),
  4285. 'Three',
  4286. '/option',
  4287. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4288. 'Stringy',
  4289. '/option',
  4290. '/select',
  4291. '/div'
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4295. $disabled = true;
  4296. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4297. $expected = array(
  4298. array('div' => array('class' => 'input select')),
  4299. array('label' => array('for' => 'ContactMultiple')),
  4300. 'Multiple',
  4301. '/label',
  4302. 'input' => array(
  4303. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4304. ),
  4305. 'select' => array(
  4306. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4307. ),
  4308. array('option' => array('value' => '1')),
  4309. 'One',
  4310. '/option',
  4311. array('option' => array('value' => '2')),
  4312. 'Two',
  4313. '/option',
  4314. array('option' => array('value' => '3')),
  4315. 'Three',
  4316. '/option',
  4317. array('option' => array('value' => '3x')),
  4318. 'Stringy',
  4319. '/option',
  4320. '/select',
  4321. '/div'
  4322. );
  4323. $this->assertTags($result, $expected);
  4324. }
  4325. /**
  4326. * Test generating select with disabled elements.
  4327. *
  4328. * @return void
  4329. */
  4330. public function testSelectWithDisabledElements() {
  4331. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4332. $disabled = array(2, 3);
  4333. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4334. $expected = array(
  4335. 'select' => array(
  4336. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4337. ),
  4338. array('option' => array('value' => '')),
  4339. '/option',
  4340. array('option' => array('value' => '1')),
  4341. 'One',
  4342. '/option',
  4343. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4344. 'Two',
  4345. '/option',
  4346. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4347. 'Three',
  4348. '/option',
  4349. array('option' => array('value' => '3x')),
  4350. 'Stringy',
  4351. '/option',
  4352. '/select'
  4353. );
  4354. $this->assertTags($result, $expected);
  4355. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4356. $disabled = array('2', '3x');
  4357. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4358. $expected = array(
  4359. array('div' => array('class' => 'input select')),
  4360. array('label' => array('for' => 'ModelField')),
  4361. 'Field',
  4362. '/label',
  4363. 'select' => array(
  4364. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4365. ),
  4366. array('option' => array('value' => '1')),
  4367. 'One',
  4368. '/option',
  4369. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4370. 'Two',
  4371. '/option',
  4372. array('option' => array('value' => '3')),
  4373. 'Three',
  4374. '/option',
  4375. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4376. 'Stringy',
  4377. '/option',
  4378. '/select',
  4379. '/div'
  4380. );
  4381. $this->assertTags($result, $expected);
  4382. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4383. $disabled = true;
  4384. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4385. $expected = array(
  4386. array('div' => array('class' => 'input select')),
  4387. array('label' => array('for' => 'ModelField')),
  4388. 'Field',
  4389. '/label',
  4390. 'select' => array(
  4391. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4392. ),
  4393. array('option' => array('value' => '1')),
  4394. 'One',
  4395. '/option',
  4396. array('option' => array('value' => '2')),
  4397. 'Two',
  4398. '/option',
  4399. array('option' => array('value' => '3')),
  4400. 'Three',
  4401. '/option',
  4402. array('option' => array('value' => '3x')),
  4403. 'Stringy',
  4404. '/option',
  4405. '/select',
  4406. '/div'
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. }
  4410. /**
  4411. * test generation of habtm select boxes.
  4412. *
  4413. * @return void
  4414. */
  4415. public function testHabtmSelectBox() {
  4416. $this->View->viewVars['contactTags'] = array(
  4417. 1 => 'blue',
  4418. 2 => 'red',
  4419. 3 => 'green'
  4420. );
  4421. $this->Form->request->data = array(
  4422. 'Contact' => array(),
  4423. 'ContactTag' => array(
  4424. array(
  4425. 'id' => '1',
  4426. 'name' => 'blue'
  4427. ),
  4428. array(
  4429. 'id' => 3,
  4430. 'name' => 'green'
  4431. )
  4432. )
  4433. );
  4434. $this->Form->create('Contact');
  4435. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4436. $expected = array(
  4437. 'input' => array(
  4438. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4439. ),
  4440. 'select' => array(
  4441. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4442. 'multiple' => 'multiple'
  4443. ),
  4444. array('option' => array('value' => '1', 'selected' => 'selected')),
  4445. 'blue',
  4446. '/option',
  4447. array('option' => array('value' => '2')),
  4448. 'red',
  4449. '/option',
  4450. array('option' => array('value' => '3', 'selected' => 'selected')),
  4451. 'green',
  4452. '/option',
  4453. '/select'
  4454. );
  4455. $this->assertTags($result, $expected);
  4456. // make sure only 50 is selected, and not 50f5c0cf
  4457. $this->View->viewVars['contactTags'] = array(
  4458. '1' => 'blue',
  4459. '50f5c0cf' => 'red',
  4460. '50' => 'green'
  4461. );
  4462. $this->Form->request->data = array(
  4463. 'Contact' => array(),
  4464. 'ContactTag' => array(
  4465. array(
  4466. 'id' => 1,
  4467. 'name' => 'blue'
  4468. ),
  4469. array(
  4470. 'id' => 50,
  4471. 'name' => 'green'
  4472. )
  4473. )
  4474. );
  4475. $this->Form->create('Contact');
  4476. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4477. $expected = array(
  4478. 'input' => array(
  4479. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4480. ),
  4481. 'select' => array(
  4482. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4483. 'multiple' => 'multiple'
  4484. ),
  4485. array('option' => array('value' => '1', 'selected' => 'selected')),
  4486. 'blue',
  4487. '/option',
  4488. array('option' => array('value' => '50f5c0cf')),
  4489. 'red',
  4490. '/option',
  4491. array('option' => array('value' => '50', 'selected' => 'selected')),
  4492. 'green',
  4493. '/option',
  4494. '/select'
  4495. );
  4496. $this->assertTags($result, $expected);
  4497. }
  4498. /**
  4499. * test generation of multi select elements in checkbox format
  4500. *
  4501. * @return void
  4502. */
  4503. public function testSelectMultipleCheckboxes() {
  4504. $result = $this->Form->select(
  4505. 'Model.multi_field',
  4506. array('first', 'second', 'third'),
  4507. array('multiple' => 'checkbox')
  4508. );
  4509. $expected = array(
  4510. 'input' => array(
  4511. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4512. ),
  4513. array('div' => array('class' => 'checkbox')),
  4514. array('input' => array(
  4515. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4516. 'value' => '0', 'id' => 'ModelMultiField0'
  4517. )),
  4518. array('label' => array('for' => 'ModelMultiField0')),
  4519. 'first',
  4520. '/label',
  4521. '/div',
  4522. array('div' => array('class' => 'checkbox')),
  4523. array('input' => array(
  4524. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4525. 'value' => '1', 'id' => 'ModelMultiField1'
  4526. )),
  4527. array('label' => array('for' => 'ModelMultiField1')),
  4528. 'second',
  4529. '/label',
  4530. '/div',
  4531. array('div' => array('class' => 'checkbox')),
  4532. array('input' => array(
  4533. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4534. 'value' => '2', 'id' => 'ModelMultiField2'
  4535. )),
  4536. array('label' => array('for' => 'ModelMultiField2')),
  4537. 'third',
  4538. '/label',
  4539. '/div'
  4540. );
  4541. $this->assertTags($result, $expected);
  4542. $result = $this->Form->select(
  4543. 'Model.multi_field',
  4544. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4545. array('multiple' => 'checkbox')
  4546. );
  4547. $expected = array(
  4548. 'input' => array(
  4549. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4550. ),
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array(
  4553. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4554. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4555. )),
  4556. array('label' => array('for' => 'ModelMultiFieldA')),
  4557. 'first',
  4558. '/label',
  4559. '/div',
  4560. array('div' => array('class' => 'checkbox')),
  4561. array('input' => array(
  4562. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4563. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4564. )),
  4565. array('label' => array('for' => 'ModelMultiFieldB')),
  4566. 'second',
  4567. '/label',
  4568. '/div',
  4569. array('div' => array('class' => 'checkbox')),
  4570. array('input' => array(
  4571. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4572. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4573. )),
  4574. array('label' => array('for' => 'ModelMultiFieldC')),
  4575. 'third',
  4576. '/label',
  4577. '/div'
  4578. );
  4579. $this->assertTags($result, $expected);
  4580. $result = $this->Form->select(
  4581. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4582. );
  4583. $expected = array(
  4584. 'input' => array(
  4585. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4586. ),
  4587. array('div' => array('class' => 'checkbox')),
  4588. array('input' => array(
  4589. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4590. 'value' => '1', 'id' => 'ModelMultiField1'
  4591. )),
  4592. array('label' => array('for' => 'ModelMultiField1')),
  4593. 'first',
  4594. '/label',
  4595. '/div'
  4596. );
  4597. $this->assertTags($result, $expected);
  4598. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4599. $result = $this->Form->select(
  4600. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4601. );
  4602. $expected = array(
  4603. 'input' => array(
  4604. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4605. ),
  4606. array('div' => array('class' => 'checkbox')),
  4607. array('input' => array(
  4608. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4609. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4610. )),
  4611. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4612. 'first',
  4613. '/label',
  4614. '/div',
  4615. array('div' => array('class' => 'checkbox')),
  4616. array('input' => array(
  4617. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4618. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4619. )),
  4620. array('label' => array('for' => 'ModelTagsArray')),
  4621. 'Array',
  4622. '/label',
  4623. '/div'
  4624. );
  4625. $this->assertTags($result, $expected);
  4626. }
  4627. /**
  4628. * test multiple checkboxes with div styles.
  4629. *
  4630. * @return void
  4631. */
  4632. public function testSelectMultipleCheckboxDiv() {
  4633. $result = $this->Form->select(
  4634. 'Model.tags',
  4635. array('first', 'second'),
  4636. array('multiple' => 'checkbox', 'class' => 'my-class')
  4637. );
  4638. $expected = array(
  4639. 'input' => array(
  4640. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4641. ),
  4642. array('div' => array('class' => 'my-class')),
  4643. array('input' => array(
  4644. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4645. 'value' => '0', 'id' => 'ModelTags0'
  4646. )),
  4647. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4648. '/div',
  4649. array('div' => array('class' => 'my-class')),
  4650. array('input' => array(
  4651. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4652. 'value' => '1', 'id' => 'ModelTags1'
  4653. )),
  4654. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4655. '/div'
  4656. );
  4657. $this->assertTags($result, $expected);
  4658. $result = $this->Form->input('Model.tags', array(
  4659. 'options' => array('first', 'second'),
  4660. 'multiple' => 'checkbox',
  4661. 'class' => 'my-class',
  4662. 'div' => false,
  4663. 'label' => false
  4664. ));
  4665. $this->assertTags($result, $expected);
  4666. $Contact = ClassRegistry::getObject('Contact');
  4667. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4668. $result = $this->Form->input('Contact.tags', array(
  4669. 'options' => array('one'),
  4670. 'multiple' => 'checkbox',
  4671. 'label' => false,
  4672. 'div' => false
  4673. ));
  4674. $expected = array(
  4675. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4676. array('div' => array('class' => 'checkbox form-error')),
  4677. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4678. array('label' => array('for' => 'ContactTags0')),
  4679. 'one',
  4680. '/label',
  4681. '/div'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. $result = $this->Form->input('Contact.tags', array(
  4685. 'options' => array('one'),
  4686. 'multiple' => 'checkbox',
  4687. 'class' => 'mycheckbox',
  4688. 'label' => false,
  4689. 'div' => false
  4690. ));
  4691. $expected = array(
  4692. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4693. array('div' => array('class' => 'mycheckbox form-error')),
  4694. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4695. array('label' => array('for' => 'ContactTags0')),
  4696. 'one',
  4697. '/label',
  4698. '/div'
  4699. );
  4700. $this->assertTags($result, $expected);
  4701. }
  4702. /**
  4703. * Checks the security hash array generated for multiple-input checkbox elements
  4704. *
  4705. * @return void
  4706. */
  4707. public function testSelectMultipleCheckboxSecurity() {
  4708. $this->Form->request['_Token'] = array('key' => 'testKey');
  4709. $this->assertEquals(array(), $this->Form->fields);
  4710. $result = $this->Form->select(
  4711. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4712. array('multiple' => 'checkbox')
  4713. );
  4714. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4715. $result = $this->Form->secure($this->Form->fields);
  4716. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4717. $this->assertRegExp('/"' . $key . '"/', $result);
  4718. }
  4719. /**
  4720. * Multiple select elements should always be secured as they always participate
  4721. * in the POST data.
  4722. *
  4723. * @return void
  4724. */
  4725. public function testSelectMultipleSecureWithNoOptions() {
  4726. $this->Form->request['_Token'] = array('key' => 'testkey');
  4727. $this->assertEquals(array(), $this->Form->fields);
  4728. $this->Form->select(
  4729. 'Model.select',
  4730. array(),
  4731. array('multiple' => true)
  4732. );
  4733. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4734. }
  4735. /**
  4736. * When a select box has no options it should not be added to the fields list
  4737. * as it always fail post validation.
  4738. *
  4739. * @return void
  4740. */
  4741. public function testSelectNoSecureWithNoOptions() {
  4742. $this->Form->request['_Token'] = array('key' => 'testkey');
  4743. $this->assertEquals(array(), $this->Form->fields);
  4744. $this->Form->select(
  4745. 'Model.select',
  4746. array()
  4747. );
  4748. $this->assertEquals(array(), $this->Form->fields);
  4749. $this->Form->select(
  4750. 'Model.select',
  4751. array(),
  4752. array('empty' => true)
  4753. );
  4754. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4755. }
  4756. /**
  4757. * testInputMultipleCheckboxes method
  4758. *
  4759. * test input() resulting in multi select elements being generated.
  4760. *
  4761. * @return void
  4762. */
  4763. public function testInputMultipleCheckboxes() {
  4764. $result = $this->Form->input('Model.multi_field', array(
  4765. 'options' => array('first', 'second', 'third'),
  4766. 'multiple' => 'checkbox'
  4767. ));
  4768. $expected = array(
  4769. array('div' => array('class' => 'input select')),
  4770. array('label' => array('for' => 'ModelMultiField')),
  4771. 'Multi Field',
  4772. '/label',
  4773. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4774. array('div' => array('class' => 'checkbox')),
  4775. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4776. array('label' => array('for' => 'ModelMultiField0')),
  4777. 'first',
  4778. '/label',
  4779. '/div',
  4780. array('div' => array('class' => 'checkbox')),
  4781. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4782. array('label' => array('for' => 'ModelMultiField1')),
  4783. 'second',
  4784. '/label',
  4785. '/div',
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4788. array('label' => array('for' => 'ModelMultiField2')),
  4789. 'third',
  4790. '/label',
  4791. '/div',
  4792. '/div'
  4793. );
  4794. $this->assertTags($result, $expected);
  4795. $result = $this->Form->input('Model.multi_field', array(
  4796. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4797. 'multiple' => 'checkbox'
  4798. ));
  4799. $expected = array(
  4800. array('div' => array('class' => 'input select')),
  4801. array('label' => array('for' => 'ModelMultiField')),
  4802. 'Multi Field',
  4803. '/label',
  4804. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4805. array('div' => array('class' => 'checkbox')),
  4806. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4807. array('label' => array('for' => 'ModelMultiFieldA')),
  4808. 'first',
  4809. '/label',
  4810. '/div',
  4811. array('div' => array('class' => 'checkbox')),
  4812. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4813. array('label' => array('for' => 'ModelMultiFieldB')),
  4814. 'second',
  4815. '/label',
  4816. '/div',
  4817. array('div' => array('class' => 'checkbox')),
  4818. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4819. array('label' => array('for' => 'ModelMultiFieldC')),
  4820. 'third',
  4821. '/label',
  4822. '/div',
  4823. '/div'
  4824. );
  4825. $this->assertTags($result, $expected);
  4826. $result = $this->Form->input('Model.multi_field', array(
  4827. 'options' => array('1' => 'first'),
  4828. 'multiple' => 'checkbox',
  4829. 'label' => false,
  4830. 'div' => false
  4831. ));
  4832. $expected = array(
  4833. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4834. array('div' => array('class' => 'checkbox')),
  4835. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4836. array('label' => array('for' => 'ModelMultiField1')),
  4837. 'first',
  4838. '/label',
  4839. '/div'
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. $result = $this->Form->input('Model.multi_field', array(
  4843. 'options' => array('2' => 'second'),
  4844. 'multiple' => 'checkbox',
  4845. 'label' => false,
  4846. 'div' => false
  4847. ));
  4848. $expected = array(
  4849. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4850. array('div' => array('class' => 'checkbox')),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4852. array('label' => array('for' => 'ModelMultiField2')),
  4853. 'second',
  4854. '/label',
  4855. '/div'
  4856. );
  4857. $this->assertTags($result, $expected);
  4858. }
  4859. /**
  4860. * testSelectHiddenFieldOmission method
  4861. *
  4862. * test that select() with 'hiddenField' => false omits the hidden field
  4863. *
  4864. * @return void
  4865. */
  4866. public function testSelectHiddenFieldOmission() {
  4867. $result = $this->Form->select('Model.multi_field',
  4868. array('first', 'second'),
  4869. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4870. );
  4871. $expected = array(
  4872. array('div' => array('class' => 'checkbox')),
  4873. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4874. array('label' => array('for' => 'ModelMultiField0')),
  4875. 'first',
  4876. '/label',
  4877. '/div',
  4878. array('div' => array('class' => 'checkbox')),
  4879. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4880. array('label' => array('for' => 'ModelMultiField1')),
  4881. 'second',
  4882. '/label',
  4883. '/div'
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->input('Model.multi_field', array(
  4887. 'options' => array('first', 'second'),
  4888. 'multiple' => 'checkbox',
  4889. 'hiddenField' => false
  4890. ));
  4891. $expected = array(
  4892. array('div' => array('class' => 'input select')),
  4893. array('label' => array('for' => 'ModelMultiField')),
  4894. 'Multi Field',
  4895. '/label',
  4896. array('div' => array('class' => 'checkbox')),
  4897. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4898. array('label' => array('for' => 'ModelMultiField0')),
  4899. 'first',
  4900. '/label',
  4901. '/div',
  4902. array('div' => array('class' => 'checkbox')),
  4903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4904. array('label' => array('for' => 'ModelMultiField1')),
  4905. 'second',
  4906. '/label',
  4907. '/div',
  4908. '/div'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. }
  4912. /**
  4913. * test that select() with multiple = checkbox works with overriding name attribute.
  4914. *
  4915. * @return void
  4916. */
  4917. public function testSelectCheckboxMultipleOverrideName() {
  4918. $result = $this->Form->input('category', array(
  4919. 'type' => 'select',
  4920. 'multiple' => 'checkbox',
  4921. 'name' => 'data[fish]',
  4922. 'options' => array('1', '2'),
  4923. 'div' => false,
  4924. 'label' => false,
  4925. ));
  4926. $expected = array(
  4927. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4928. array('div' => array('class' => 'checkbox')),
  4929. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4930. array('label' => array('for' => 'Category0')), '1', '/label',
  4931. '/div',
  4932. array('div' => array('class' => 'checkbox')),
  4933. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4934. array('label' => array('for' => 'Category1')), '2', '/label',
  4935. '/div'
  4936. );
  4937. $this->assertTags($result, $expected);
  4938. }
  4939. /**
  4940. * Test that 'id' overrides all the checkbox id's as well.
  4941. *
  4942. * @return void
  4943. */
  4944. public function testSelectCheckboxMultipleId() {
  4945. $result = $this->Form->select(
  4946. 'Model.multi_field',
  4947. array('first', 'second', 'third'),
  4948. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4949. );
  4950. $expected = array(
  4951. 'input' => array(
  4952. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4953. ),
  4954. array('div' => array('class' => 'checkbox')),
  4955. array('input' => array(
  4956. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4957. 'value' => '0', 'id' => 'CustomId0'
  4958. )),
  4959. array('label' => array('for' => 'CustomId0')),
  4960. 'first',
  4961. '/label',
  4962. '/div',
  4963. array('div' => array('class' => 'checkbox')),
  4964. array('input' => array(
  4965. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4966. 'value' => '1', 'id' => 'CustomId1'
  4967. )),
  4968. array('label' => array('for' => 'CustomId1')),
  4969. 'second',
  4970. '/label',
  4971. '/div',
  4972. array('div' => array('class' => 'checkbox')),
  4973. array('input' => array(
  4974. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4975. 'value' => '2', 'id' => 'CustomId2'
  4976. )),
  4977. array('label' => array('for' => 'CustomId2')),
  4978. 'third',
  4979. '/label',
  4980. '/div'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. }
  4984. /**
  4985. * testCheckbox method
  4986. *
  4987. * Test generation of checkboxes
  4988. *
  4989. * @return void
  4990. */
  4991. public function testCheckbox() {
  4992. $result = $this->Form->checkbox('Model.field');
  4993. $expected = array(
  4994. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4995. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4996. );
  4997. $this->assertTags($result, $expected);
  4998. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4999. $expected = array(
  5000. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5001. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. $Contact = ClassRegistry::getObject('Contact');
  5005. $Contact->validationErrors['field'] = 1;
  5006. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5007. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5008. $expected = array(
  5009. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5010. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5014. $expected = array(
  5015. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5016. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. $this->Form->request->data['Contact']['field'] = '';
  5020. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5021. $expected = array(
  5022. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5023. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5024. );
  5025. $this->assertTags($result, $expected);
  5026. $Contact->validationErrors = array();
  5027. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5028. $expected = array(
  5029. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5030. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5031. );
  5032. $this->assertTags($result, $expected);
  5033. $this->Form->request->data['Contact']['published'] = 1;
  5034. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5035. $expected = array(
  5036. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5037. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5038. );
  5039. $this->assertTags($result, $expected);
  5040. $this->Form->request->data['Contact']['published'] = 0;
  5041. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5042. $expected = array(
  5043. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5044. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5045. );
  5046. $this->assertTags($result, $expected);
  5047. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5048. $expected = array(
  5049. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5050. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $result = $this->Form->checkbox('CustomField.1.value');
  5054. $expected = array(
  5055. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5056. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5057. );
  5058. $this->assertTags($result, $expected);
  5059. }
  5060. /**
  5061. * test checkbox() with a custom name attribute
  5062. *
  5063. * @return void
  5064. */
  5065. public function testCheckboxCustomNameAttribute() {
  5066. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5067. $expected = array(
  5068. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5069. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. }
  5073. /**
  5074. * test the checked option for checkboxes.
  5075. *
  5076. * @return void
  5077. */
  5078. public function testCheckboxCheckedOption() {
  5079. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5080. $expected = array(
  5081. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5082. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5086. $expected = array(
  5087. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5089. );
  5090. $this->assertTags($result, $expected);
  5091. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5092. $expected = array(
  5093. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5094. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5095. );
  5096. $this->assertTags($result, $expected);
  5097. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5098. $expected = array(
  5099. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5100. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5101. );
  5102. $this->assertTags($result, $expected);
  5103. $this->Form->request->data['Model']['field'] = 1;
  5104. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5105. $expected = array(
  5106. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5107. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. }
  5111. /**
  5112. * Test that disabled attribute works on both the checkbox and hidden input.
  5113. *
  5114. * @return void
  5115. */
  5116. public function testCheckboxDisabling() {
  5117. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5118. $expected = array(
  5119. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5120. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5121. );
  5122. $this->assertTags($result, $expected);
  5123. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5124. $expected = array(
  5125. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5126. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. }
  5130. /**
  5131. * Test that the hidden input for checkboxes can be omitted or set to a
  5132. * specific value.
  5133. *
  5134. * @return void
  5135. */
  5136. public function testCheckboxHiddenField() {
  5137. $result = $this->Form->input('UserForm.something', array(
  5138. 'type' => 'checkbox',
  5139. 'hiddenField' => false
  5140. ));
  5141. $expected = array(
  5142. 'div' => array('class' => 'input checkbox'),
  5143. array('input' => array(
  5144. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5145. 'value' => '1', 'id' => 'UserFormSomething'
  5146. )),
  5147. 'label' => array('for' => 'UserFormSomething'),
  5148. 'Something',
  5149. '/label',
  5150. '/div'
  5151. );
  5152. $this->assertTags($result, $expected);
  5153. $result = $this->Form->input('UserForm.something', array(
  5154. 'type' => 'checkbox',
  5155. 'value' => 'Y',
  5156. 'hiddenField' => 'N',
  5157. ));
  5158. $expected = array(
  5159. 'div' => array('class' => 'input checkbox'),
  5160. array('input' => array(
  5161. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5162. 'value' => 'N', 'id' => 'UserFormSomething_'
  5163. )),
  5164. array('input' => array(
  5165. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5166. 'value' => 'Y', 'id' => 'UserFormSomething'
  5167. )),
  5168. 'label' => array('for' => 'UserFormSomething'),
  5169. 'Something',
  5170. '/label',
  5171. '/div'
  5172. );
  5173. $this->assertTags($result, $expected);
  5174. }
  5175. /**
  5176. * testDateTime method
  5177. *
  5178. * Test generation of date/time select elements
  5179. *
  5180. * @return void
  5181. */
  5182. public function testDateTime() {
  5183. extract($this->dateRegex);
  5184. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5185. $now = strtotime('now');
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5188. $daysRegex,
  5189. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5190. date('j', $now),
  5191. '/option',
  5192. '*/select',
  5193. '-',
  5194. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5195. $monthsRegex,
  5196. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5197. date('F', $now),
  5198. '/option',
  5199. '*/select',
  5200. '-',
  5201. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5202. $yearsRegex,
  5203. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5204. date('Y', $now),
  5205. '/option',
  5206. '*/select',
  5207. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5208. $hoursRegex,
  5209. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5210. date('g', $now),
  5211. '/option',
  5212. '*/select',
  5213. ':',
  5214. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5215. $minutesRegex,
  5216. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5217. date('i', $now),
  5218. '/option',
  5219. '*/select',
  5220. ' ',
  5221. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5222. $meridianRegex,
  5223. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5224. date('a', $now),
  5225. '/option',
  5226. '*/select'
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5232. $daysRegex,
  5233. array('option' => array('value' => '')),
  5234. '/option',
  5235. '*/select',
  5236. '-',
  5237. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5238. $monthsRegex,
  5239. array('option' => array('value' => '')),
  5240. '/option',
  5241. '*/select',
  5242. '-',
  5243. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5244. $yearsRegex,
  5245. array('option' => array('value' => '')),
  5246. '/option',
  5247. '*/select',
  5248. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5249. $hoursRegex,
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. '*/select',
  5253. ':',
  5254. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5255. $minutesRegex,
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. '*/select',
  5259. ' ',
  5260. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5261. $meridianRegex,
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. '*/select'
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5268. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5269. $this->assertTags($result, $expected);
  5270. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5271. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5272. $this->assertTags($result, $expected);
  5273. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5274. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5275. $expected = array(
  5276. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5277. $daysRegex,
  5278. array('option' => array('value' => '')),
  5279. '/option',
  5280. '*/select',
  5281. '-',
  5282. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5283. $monthsRegex,
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. '*/select',
  5287. '-',
  5288. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5289. $yearsRegex,
  5290. array('option' => array('value' => '')),
  5291. '/option',
  5292. '*/select',
  5293. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5294. $hoursRegex,
  5295. array('option' => array('value' => '')),
  5296. '/option',
  5297. '*/select',
  5298. ':',
  5299. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5300. $minutesRegex,
  5301. array('option' => array('value' => '')),
  5302. '/option',
  5303. array('option' => array('value' => '00')),
  5304. '00',
  5305. '/option',
  5306. array('option' => array('value' => '05')),
  5307. '05',
  5308. '/option',
  5309. array('option' => array('value' => '10')),
  5310. '10',
  5311. '/option',
  5312. '*/select',
  5313. ' ',
  5314. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5315. $meridianRegex,
  5316. array('option' => array('value' => '')),
  5317. '/option',
  5318. '*/select'
  5319. );
  5320. $this->assertTags($result, $expected);
  5321. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5322. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5323. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5324. $this->Form->request->data['Contact']['data'] = null;
  5325. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5326. $expected = array(
  5327. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5328. $daysRegex,
  5329. array('option' => array('value' => '')),
  5330. '/option',
  5331. '*/select',
  5332. '-',
  5333. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5334. $monthsRegex,
  5335. array('option' => array('value' => '')),
  5336. '/option',
  5337. '*/select',
  5338. '-',
  5339. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5340. $yearsRegex,
  5341. array('option' => array('value' => '')),
  5342. '/option',
  5343. '*/select',
  5344. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5345. $hoursRegex,
  5346. array('option' => array('value' => '')),
  5347. '/option',
  5348. '*/select',
  5349. ':',
  5350. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5351. $minutesRegex,
  5352. array('option' => array('value' => '')),
  5353. '/option',
  5354. '*/select',
  5355. ' ',
  5356. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5357. $meridianRegex,
  5358. array('option' => array('value' => '')),
  5359. '/option',
  5360. '*/select'
  5361. );
  5362. $this->assertTags($result, $expected);
  5363. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5364. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5365. $now = strtotime($this->Form->data['Model']['field']);
  5366. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5367. $expected = array(
  5368. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5369. $daysRegex,
  5370. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5371. date('j', $now),
  5372. '/option',
  5373. '*/select',
  5374. '-',
  5375. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5376. $monthsRegex,
  5377. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5378. date('F', $now),
  5379. '/option',
  5380. '*/select',
  5381. '-',
  5382. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5383. $yearsRegex,
  5384. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5385. date('Y', $now),
  5386. '/option',
  5387. '*/select',
  5388. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5389. $hoursRegex,
  5390. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5391. date('g', $now),
  5392. '/option',
  5393. '*/select',
  5394. ':',
  5395. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5396. $minutesRegex,
  5397. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5398. date('i', $now),
  5399. '/option',
  5400. '*/select',
  5401. ' ',
  5402. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5403. $meridianRegex,
  5404. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5405. date('a', $now),
  5406. '/option',
  5407. '*/select'
  5408. );
  5409. $this->assertTags($result, $expected);
  5410. $selected = strtotime('2008-10-26 12:33:00');
  5411. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5412. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5413. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5414. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5415. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5416. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5417. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5418. $this->Form->create('Contact');
  5419. $result = $this->Form->input('published');
  5420. $now = strtotime('now');
  5421. $expected = array(
  5422. 'div' => array('class' => 'input date'),
  5423. 'label' => array('for' => 'ContactPublishedMonth'),
  5424. 'Published',
  5425. '/label',
  5426. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5427. $monthsRegex,
  5428. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5429. date('F', $now),
  5430. '/option',
  5431. '*/select',
  5432. '-',
  5433. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5434. $daysRegex,
  5435. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5436. date('j', $now),
  5437. '/option',
  5438. '*/select',
  5439. '-',
  5440. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5441. $yearsRegex,
  5442. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5443. date('Y', $now),
  5444. '/option',
  5445. '*/select',
  5446. '/div'
  5447. );
  5448. $this->assertTags($result, $expected);
  5449. $result = $this->Form->input('published2', array('type' => 'date'));
  5450. $now = strtotime('now');
  5451. $expected = array(
  5452. 'div' => array('class' => 'input date'),
  5453. 'label' => array('for' => 'ContactPublished2Month'),
  5454. 'Published2',
  5455. '/label',
  5456. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5457. $monthsRegex,
  5458. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5459. date('F', $now),
  5460. '/option',
  5461. '*/select',
  5462. '-',
  5463. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5464. $daysRegex,
  5465. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5466. date('j', $now),
  5467. '/option',
  5468. '*/select',
  5469. '-',
  5470. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5471. $yearsRegex,
  5472. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5473. date('Y', $now),
  5474. '/option',
  5475. '*/select',
  5476. '/div'
  5477. );
  5478. $this->assertTags($result, $expected);
  5479. $this->Form->create('Contact');
  5480. $result = $this->Form->input('published', array('monthNames' => false));
  5481. $now = strtotime('now');
  5482. $expected = array(
  5483. 'div' => array('class' => 'input date'),
  5484. 'label' => array('for' => 'ContactPublishedMonth'),
  5485. 'Published',
  5486. '/label',
  5487. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5488. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5489. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5490. date('m', $now),
  5491. '/option',
  5492. '*/select',
  5493. '-',
  5494. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5495. $daysRegex,
  5496. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5497. date('j', $now),
  5498. '/option',
  5499. '*/select',
  5500. '-',
  5501. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5502. $yearsRegex,
  5503. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5504. date('Y', $now),
  5505. '/option',
  5506. '*/select',
  5507. '/div'
  5508. );
  5509. $this->assertTags($result, $expected);
  5510. $result = $this->Form->input('published', array(
  5511. 'timeFormat' => 24,
  5512. 'interval' => 5,
  5513. 'selected' => strtotime('2009-09-03 13:37:00'),
  5514. 'type' => 'datetime'
  5515. ));
  5516. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5517. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5518. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5519. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5520. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5521. }
  5522. /**
  5523. * Test dateTime with rounding
  5524. *
  5525. * @return void
  5526. */
  5527. public function testDateTimeRounding() {
  5528. $this->Form->request->data['Contact'] = array(
  5529. 'date' => array(
  5530. 'day' => '13',
  5531. 'month' => '12',
  5532. 'year' => '2010',
  5533. 'hour' => '04',
  5534. 'min' => '19',
  5535. 'meridian' => 'AM'
  5536. )
  5537. );
  5538. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5539. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5540. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5541. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5542. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5543. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5544. }
  5545. /**
  5546. * Test that empty values don't trigger errors.
  5547. *
  5548. * @return void
  5549. */
  5550. public function testDateTimeNoErrorsOnEmptyData() {
  5551. $this->Form->request->data['Contact'] = array(
  5552. 'date' => array(
  5553. 'day' => '',
  5554. 'month' => '',
  5555. 'year' => '',
  5556. 'hour' => '',
  5557. 'min' => '',
  5558. 'meridian' => ''
  5559. )
  5560. );
  5561. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5562. $this->assertNotEmpty($result);
  5563. }
  5564. /**
  5565. * test that datetime() and default values work.
  5566. *
  5567. * @return void
  5568. */
  5569. public function testDatetimeWithDefault() {
  5570. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5571. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5572. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5573. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5574. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5575. 'default' => '2009-06-01 11:15:30'
  5576. ));
  5577. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5578. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5579. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5580. }
  5581. /**
  5582. * test that bogus non-date time data doesn't cause errors.
  5583. *
  5584. * @return void
  5585. */
  5586. public function testDateTimeWithBogusData() {
  5587. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5588. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5589. }
  5590. /**
  5591. * testDateTimeEmptyAsArray
  5592. *
  5593. * @return void
  5594. */
  5595. public function testDateTimeEmptyAsArray() {
  5596. $result = $this->Form->dateTime('Contact.date',
  5597. 'DMY',
  5598. '12',
  5599. array(
  5600. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5601. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5602. )
  5603. )
  5604. );
  5605. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5606. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5607. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5608. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5609. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5610. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5611. $result = $this->Form->dateTime('Contact.date',
  5612. 'DMY',
  5613. '12',
  5614. array(
  5615. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5616. )
  5617. );
  5618. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5619. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5620. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5621. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5622. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5623. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5624. }
  5625. /**
  5626. * testFormDateTimeMulti method
  5627. *
  5628. * test multiple datetime element generation
  5629. *
  5630. * @return void
  5631. */
  5632. public function testFormDateTimeMulti() {
  5633. extract($this->dateRegex);
  5634. $result = $this->Form->dateTime('Contact.1.updated');
  5635. $expected = array(
  5636. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5637. $daysRegex,
  5638. array('option' => array('value' => '')),
  5639. '/option',
  5640. '*/select',
  5641. '-',
  5642. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5643. $monthsRegex,
  5644. array('option' => array('value' => '')),
  5645. '/option',
  5646. '*/select',
  5647. '-',
  5648. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5649. $yearsRegex,
  5650. array('option' => array('value' => '')),
  5651. '/option',
  5652. '*/select',
  5653. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5654. $hoursRegex,
  5655. array('option' => array('value' => '')),
  5656. '/option',
  5657. '*/select',
  5658. ':',
  5659. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5660. $minutesRegex,
  5661. array('option' => array('value' => '')),
  5662. '/option',
  5663. '*/select',
  5664. ' ',
  5665. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5666. $meridianRegex,
  5667. array('option' => array('value' => '')),
  5668. '/option',
  5669. '*/select'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. $result = $this->Form->dateTime('Contact.2.updated');
  5673. $expected = array(
  5674. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5675. $daysRegex,
  5676. array('option' => array('value' => '')),
  5677. '/option',
  5678. '*/select',
  5679. '-',
  5680. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5681. $monthsRegex,
  5682. array('option' => array('value' => '')),
  5683. '/option',
  5684. '*/select',
  5685. '-',
  5686. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5687. $yearsRegex,
  5688. array('option' => array('value' => '')),
  5689. '/option',
  5690. '*/select',
  5691. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5692. $hoursRegex,
  5693. array('option' => array('value' => '')),
  5694. '/option',
  5695. '*/select',
  5696. ':',
  5697. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5698. $minutesRegex,
  5699. array('option' => array('value' => '')),
  5700. '/option',
  5701. '*/select',
  5702. ' ',
  5703. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5704. $meridianRegex,
  5705. array('option' => array('value' => '')),
  5706. '/option',
  5707. '*/select'
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. }
  5711. /**
  5712. * When changing the date format, the label should always focus the first select box when
  5713. * clicked.
  5714. *
  5715. * @return void
  5716. */
  5717. public function testDateTimeLabelIdMatchesFirstInput() {
  5718. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5719. $this->assertContains('label for="ModelDateMonth"', $result);
  5720. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5721. $this->assertContains('label for="ModelDateDay"', $result);
  5722. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5723. $this->assertContains('label for="ModelDateYear"', $result);
  5724. }
  5725. /**
  5726. * testMonth method
  5727. *
  5728. * @return void
  5729. */
  5730. public function testMonth() {
  5731. $result = $this->Form->month('Model.field');
  5732. $expected = array(
  5733. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5734. array('option' => array('value' => '')),
  5735. '/option',
  5736. array('option' => array('value' => '01')),
  5737. date('F', strtotime('2008-01-01 00:00:00')),
  5738. '/option',
  5739. array('option' => array('value' => '02')),
  5740. date('F', strtotime('2008-02-01 00:00:00')),
  5741. '/option',
  5742. '*/select',
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. $result = $this->Form->month('Model.field', array('empty' => true));
  5746. $expected = array(
  5747. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5748. array('option' => array('value' => '')),
  5749. '/option',
  5750. array('option' => array('value' => '01')),
  5751. date('F', strtotime('2008-01-01 00:00:00')),
  5752. '/option',
  5753. array('option' => array('value' => '02')),
  5754. date('F', strtotime('2008-02-01 00:00:00')),
  5755. '/option',
  5756. '*/select',
  5757. );
  5758. $this->assertTags($result, $expected);
  5759. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5760. $expected = array(
  5761. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5762. array('option' => array('value' => '')),
  5763. '/option',
  5764. array('option' => array('value' => '01')),
  5765. '01',
  5766. '/option',
  5767. array('option' => array('value' => '02')),
  5768. '02',
  5769. '/option',
  5770. '*/select',
  5771. );
  5772. $this->assertTags($result, $expected);
  5773. $monthNames = array(
  5774. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5775. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5776. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5777. $expected = array(
  5778. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5779. array('option' => array('value' => '')),
  5780. '/option',
  5781. array('option' => array('value' => '01')),
  5782. 'Jan',
  5783. '/option',
  5784. array('option' => array('value' => '02')),
  5785. 'Feb',
  5786. '/option',
  5787. '*/select',
  5788. );
  5789. $this->assertTags($result, $expected);
  5790. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5791. $result = $this->Form->month('Project.release');
  5792. $expected = array(
  5793. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5794. array('option' => array('value' => '')),
  5795. '/option',
  5796. array('option' => array('value' => '01')),
  5797. 'January',
  5798. '/option',
  5799. array('option' => array('value' => '02', 'selected' => 'selected')),
  5800. 'February',
  5801. '/option',
  5802. '*/select',
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. }
  5806. /**
  5807. * testDay method
  5808. *
  5809. * @return void
  5810. */
  5811. public function testDay() {
  5812. extract($this->dateRegex);
  5813. $result = $this->Form->day('Model.field', array('value' => false));
  5814. $expected = array(
  5815. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5816. array('option' => array('value' => '')),
  5817. '/option',
  5818. array('option' => array('value' => '01')),
  5819. '1',
  5820. '/option',
  5821. array('option' => array('value' => '02')),
  5822. '2',
  5823. '/option',
  5824. $daysRegex,
  5825. '/select',
  5826. );
  5827. $this->assertTags($result, $expected);
  5828. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5829. $result = $this->Form->day('Model.field');
  5830. $expected = array(
  5831. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5832. array('option' => array('value' => '')),
  5833. '/option',
  5834. array('option' => array('value' => '01')),
  5835. '1',
  5836. '/option',
  5837. array('option' => array('value' => '02')),
  5838. '2',
  5839. '/option',
  5840. $daysRegex,
  5841. array('option' => array('value' => '10', 'selected' => 'selected')),
  5842. '10',
  5843. '/option',
  5844. $daysRegex,
  5845. '/select',
  5846. );
  5847. $this->assertTags($result, $expected);
  5848. $this->Form->request->data['Model']['field'] = '';
  5849. $result = $this->Form->day('Model.field', array('value' => '10'));
  5850. $expected = array(
  5851. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5852. array('option' => array('value' => '')),
  5853. '/option',
  5854. array('option' => array('value' => '01')),
  5855. '1',
  5856. '/option',
  5857. array('option' => array('value' => '02')),
  5858. '2',
  5859. '/option',
  5860. $daysRegex,
  5861. array('option' => array('value' => '10', 'selected' => 'selected')),
  5862. '10',
  5863. '/option',
  5864. $daysRegex,
  5865. '/select',
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5869. $result = $this->Form->day('Model.field', array('value' => true));
  5870. $expected = array(
  5871. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5872. array('option' => array('value' => '')),
  5873. '/option',
  5874. array('option' => array('value' => '01')),
  5875. '1',
  5876. '/option',
  5877. array('option' => array('value' => '02')),
  5878. '2',
  5879. '/option',
  5880. $daysRegex,
  5881. array('option' => array('value' => '10', 'selected' => 'selected')),
  5882. '10',
  5883. '/option',
  5884. $daysRegex,
  5885. '/select',
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5889. $result = $this->Form->day('Project.release');
  5890. $expected = array(
  5891. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5892. array('option' => array('value' => '')),
  5893. '/option',
  5894. array('option' => array('value' => '01')),
  5895. '1',
  5896. '/option',
  5897. array('option' => array('value' => '02')),
  5898. '2',
  5899. '/option',
  5900. $daysRegex,
  5901. array('option' => array('value' => '10', 'selected' => 'selected')),
  5902. '10',
  5903. '/option',
  5904. $daysRegex,
  5905. '/select',
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. }
  5909. /**
  5910. * testMinute method
  5911. *
  5912. * @return void
  5913. */
  5914. public function testMinute() {
  5915. extract($this->dateRegex);
  5916. $result = $this->Form->minute('Model.field');
  5917. $expected = array(
  5918. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5919. array('option' => array('value' => '')),
  5920. '/option',
  5921. array('option' => array('value' => '00')),
  5922. '00',
  5923. '/option',
  5924. array('option' => array('value' => '01')),
  5925. '01',
  5926. '/option',
  5927. array('option' => array('value' => '02')),
  5928. '02',
  5929. '/option',
  5930. $minutesRegex,
  5931. '/select',
  5932. );
  5933. $this->assertTags($result, $expected);
  5934. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5935. $result = $this->Form->minute('Model.field');
  5936. $expected = array(
  5937. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5938. array('option' => array('value' => '')),
  5939. '/option',
  5940. array('option' => array('value' => '00')),
  5941. '00',
  5942. '/option',
  5943. array('option' => array('value' => '01')),
  5944. '01',
  5945. '/option',
  5946. array('option' => array('value' => '02')),
  5947. '02',
  5948. '/option',
  5949. $minutesRegex,
  5950. array('option' => array('value' => '12', 'selected' => 'selected')),
  5951. '12',
  5952. '/option',
  5953. $minutesRegex,
  5954. '/select',
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $this->Form->request->data['Model']['field'] = '';
  5958. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5959. $expected = array(
  5960. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5961. array('option' => array('value' => '')),
  5962. '/option',
  5963. array('option' => array('value' => '00')),
  5964. '00',
  5965. '/option',
  5966. array('option' => array('value' => '05')),
  5967. '05',
  5968. '/option',
  5969. array('option' => array('value' => '10')),
  5970. '10',
  5971. '/option',
  5972. $minutesRegex,
  5973. '/select',
  5974. );
  5975. $this->assertTags($result, $expected);
  5976. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5977. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5978. $expected = array(
  5979. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5980. array('option' => array('value' => '')),
  5981. '/option',
  5982. array('option' => array('value' => '00')),
  5983. '00',
  5984. '/option',
  5985. array('option' => array('value' => '05')),
  5986. '05',
  5987. '/option',
  5988. array('option' => array('value' => '10', 'selected' => 'selected')),
  5989. '10',
  5990. '/option',
  5991. $minutesRegex,
  5992. '/select',
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. }
  5996. /**
  5997. * testHour method
  5998. *
  5999. * @return void
  6000. */
  6001. public function testHour() {
  6002. extract($this->dateRegex);
  6003. $result = $this->Form->hour('Model.field', false);
  6004. $expected = array(
  6005. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6006. array('option' => array('value' => '')),
  6007. '/option',
  6008. array('option' => array('value' => '01')),
  6009. '1',
  6010. '/option',
  6011. array('option' => array('value' => '02')),
  6012. '2',
  6013. '/option',
  6014. $hoursRegex,
  6015. '/select',
  6016. );
  6017. $this->assertTags($result, $expected);
  6018. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6019. $result = $this->Form->hour('Model.field', false);
  6020. $expected = array(
  6021. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6022. array('option' => array('value' => '')),
  6023. '/option',
  6024. array('option' => array('value' => '01')),
  6025. '1',
  6026. '/option',
  6027. array('option' => array('value' => '02')),
  6028. '2',
  6029. '/option',
  6030. $hoursRegex,
  6031. array('option' => array('value' => '12', 'selected' => 'selected')),
  6032. '12',
  6033. '/option',
  6034. '/select',
  6035. );
  6036. $this->assertTags($result, $expected);
  6037. $this->Form->request->data['Model']['field'] = '';
  6038. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6039. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6040. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6041. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6042. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6043. $result = $this->Form->hour('Model.field', true);
  6044. $expected = array(
  6045. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6046. array('option' => array('value' => '')),
  6047. '/option',
  6048. array('option' => array('value' => '00', 'selected' => 'selected')),
  6049. '0',
  6050. '/option',
  6051. array('option' => array('value' => '01')),
  6052. '1',
  6053. '/option',
  6054. array('option' => array('value' => '02')),
  6055. '2',
  6056. '/option',
  6057. $hoursRegex,
  6058. '/select',
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. unset($this->Form->request->data['Model']['field']);
  6062. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6063. $thisHour = date('H');
  6064. $optValue = date('G');
  6065. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6066. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6067. $result = $this->Form->hour('Model.field', true);
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '00')),
  6073. '0',
  6074. '/option',
  6075. array('option' => array('value' => '01', 'selected' => 'selected')),
  6076. '1',
  6077. '/option',
  6078. array('option' => array('value' => '02')),
  6079. '2',
  6080. '/option',
  6081. $hoursRegex,
  6082. '/select',
  6083. );
  6084. $this->assertTags($result, $expected);
  6085. }
  6086. /**
  6087. * testYear method
  6088. *
  6089. * @return void
  6090. */
  6091. public function testYear() {
  6092. $result = $this->Form->year('Model.field', 2006, 2007);
  6093. $expected = array(
  6094. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6095. array('option' => array('value' => '')),
  6096. '/option',
  6097. array('option' => array('value' => '2007')),
  6098. '2007',
  6099. '/option',
  6100. array('option' => array('value' => '2006')),
  6101. '2006',
  6102. '/option',
  6103. '/select',
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6107. $expected = array(
  6108. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6109. array('option' => array('value' => '')),
  6110. '/option',
  6111. array('option' => array('value' => '2006')),
  6112. '2006',
  6113. '/option',
  6114. array('option' => array('value' => '2007')),
  6115. '2007',
  6116. '/option',
  6117. '/select',
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $this->request->data['Contact']['published'] = '';
  6121. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6122. $expected = array(
  6123. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6124. array('option' => array('value' => '')),
  6125. '/option',
  6126. array('option' => array('value' => '2007')),
  6127. '2007',
  6128. '/option',
  6129. array('option' => array('value' => '2006')),
  6130. '2006',
  6131. '/option',
  6132. '/select',
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6136. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6137. $expected = array(
  6138. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6139. array('option' => array('value' => '2007')),
  6140. '2007',
  6141. '/option',
  6142. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6143. '2006',
  6144. '/option',
  6145. '/select',
  6146. );
  6147. $this->assertTags($result, $expected);
  6148. $this->Form->request->data['Contact']['published'] = '';
  6149. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6150. $expected = array(
  6151. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6152. array('option' => array('value' => '')),
  6153. '/option',
  6154. array('option' => array('value' => '2007')),
  6155. '2007',
  6156. '/option',
  6157. array('option' => array('value' => '2006')),
  6158. '2006',
  6159. '/option',
  6160. '/select',
  6161. );
  6162. $this->assertTags($result, $expected);
  6163. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6164. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6165. $expected = array(
  6166. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6167. array('option' => array('value' => '2007')),
  6168. '2007',
  6169. '/option',
  6170. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6171. '2006',
  6172. '/option',
  6173. '/select',
  6174. );
  6175. $this->assertTags($result, $expected);
  6176. $this->Form->request->data['Contact']['published'] = '';
  6177. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6178. $expected = array(
  6179. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6180. array('option' => array('value' => '')),
  6181. '/option',
  6182. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6183. '2007',
  6184. '/option',
  6185. array('option' => array('value' => '2006')),
  6186. '2006',
  6187. '/option',
  6188. '/select',
  6189. );
  6190. $this->assertTags($result, $expected);
  6191. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6192. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6193. $expected = array(
  6194. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6195. array('option' => array('value' => '2007', 'selected' => 'selected')),
  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'] = '';
  6205. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6206. $expected = array(
  6207. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6208. array('option' => array('value' => '2008')),
  6209. '2008',
  6210. '/option',
  6211. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6212. '2007',
  6213. '/option',
  6214. array('option' => array('value' => '2006')),
  6215. '2006',
  6216. '/option',
  6217. '/select',
  6218. );
  6219. $this->assertTags($result, $expected);
  6220. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6221. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6222. $expected = array(
  6223. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6224. array('option' => array('value' => '2008')),
  6225. '2008',
  6226. '/option',
  6227. array('option' => array('value' => '2007')),
  6228. '2007',
  6229. '/option',
  6230. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6231. '2006',
  6232. '/option',
  6233. '/select',
  6234. );
  6235. $this->assertTags($result, $expected);
  6236. $this->Form->request->data = array();
  6237. $this->Form->create('Contact');
  6238. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6239. $expected = array(
  6240. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6241. array('option' => array('value' => '2008')),
  6242. '2008',
  6243. '/option',
  6244. array('option' => array('value' => '2007')),
  6245. '2007',
  6246. '/option',
  6247. array('option' => array('value' => '2006')),
  6248. '2006',
  6249. '/option',
  6250. '/select',
  6251. );
  6252. $this->assertTags($result, $expected);
  6253. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6254. $this->assertContains('data[Contact][published][year]', $result);
  6255. }
  6256. /**
  6257. * testYearAutoExpandRange method
  6258. *
  6259. * @return void
  6260. */
  6261. public function testYearAutoExpandRange() {
  6262. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6263. $result = $this->Form->year('User.birthday');
  6264. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6265. $result = $matches[1];
  6266. $expected = range(date('Y') + 20, 1930);
  6267. $this->assertEquals($expected, $result);
  6268. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6269. $result = $this->Form->year('Project.release');
  6270. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6271. $result = $matches[1];
  6272. $expected = range(2050, date('Y') - 20);
  6273. $this->assertEquals($expected, $result);
  6274. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6275. $result = $this->Form->year('Project.release', 1890, 1900);
  6276. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6277. $result = $matches[1];
  6278. $expected = range(1900, 1881);
  6279. $this->assertEquals($expected, $result);
  6280. }
  6281. /**
  6282. * testInputDate method
  6283. *
  6284. * Test various inputs with type date and different dateFormat values.
  6285. * Failing to provide a dateFormat key should not error.
  6286. * It should simply not pre-select any value then.
  6287. *
  6288. * @return void
  6289. */
  6290. public function testInputDate() {
  6291. $this->Form->request->data = array(
  6292. 'User' => array(
  6293. 'month_year' => array('month' => date('m')),
  6294. 'just_year' => array('month' => date('m')),
  6295. 'just_month' => array('year' => date('Y')),
  6296. 'just_day' => array('month' => date('m')),
  6297. )
  6298. );
  6299. $this->Form->create('User');
  6300. $result = $this->Form->input('month_year',
  6301. array(
  6302. 'label' => false,
  6303. 'div' => false,
  6304. 'type' => 'date',
  6305. 'dateFormat' => 'MY',
  6306. 'minYear' => 2006,
  6307. 'maxYear' => 2008
  6308. )
  6309. );
  6310. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6311. $this->assertNotContains('value="2008" selected="selected"', $result);
  6312. $result = $this->Form->input('just_year',
  6313. array(
  6314. 'type' => 'date',
  6315. 'label' => false,
  6316. 'dateFormat' => 'Y',
  6317. 'minYear' => date('Y'),
  6318. 'maxYear' => date('Y', strtotime('+20 years'))
  6319. )
  6320. );
  6321. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6322. $result = $this->Form->input('just_month',
  6323. array(
  6324. 'type' => 'date',
  6325. 'label' => false,
  6326. 'dateFormat' => 'M',
  6327. 'empty' => false,
  6328. )
  6329. );
  6330. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6331. $result = $this->Form->input('just_day',
  6332. array(
  6333. 'type' => 'date',
  6334. 'label' => false,
  6335. 'dateFormat' => 'D',
  6336. 'empty' => false,
  6337. )
  6338. );
  6339. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6340. }
  6341. /**
  6342. * testInputDateMaxYear method
  6343. *
  6344. * Let's say we want to only allow users born from 2006 to 2008 to register
  6345. * This being the first singup page, we still don't have any data
  6346. *
  6347. * @return void
  6348. */
  6349. public function testInputDateMaxYear() {
  6350. $this->Form->request->data = array();
  6351. $this->Form->create('User');
  6352. $result = $this->Form->input('birthday',
  6353. array(
  6354. 'label' => false,
  6355. 'div' => false,
  6356. 'type' => 'date',
  6357. 'dateFormat' => 'DMY',
  6358. 'minYear' => 2006,
  6359. 'maxYear' => 2008
  6360. )
  6361. );
  6362. $this->assertContains('value="2008" selected="selected"', $result);
  6363. }
  6364. /**
  6365. * testTextArea method
  6366. *
  6367. * @return void
  6368. */
  6369. public function testTextArea() {
  6370. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6371. $result = $this->Form->textarea('Model.field');
  6372. $expected = array(
  6373. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6374. 'some test data',
  6375. '/textarea',
  6376. );
  6377. $this->assertTags($result, $expected);
  6378. $result = $this->Form->textarea('Model.tmp');
  6379. $expected = array(
  6380. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6381. '/textarea',
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6385. $result = $this->Form->textarea('Model.field');
  6386. $expected = array(
  6387. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6388. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6389. '/textarea',
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6393. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6394. $expected = array(
  6395. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6396. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6397. '/textarea',
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6401. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6402. $expected = array(
  6403. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6404. '/textarea'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. }
  6408. /**
  6409. * testTextAreaWithStupidCharacters method
  6410. *
  6411. * test text area with non-ascii characters
  6412. *
  6413. * @return void
  6414. */
  6415. public function testTextAreaWithStupidCharacters() {
  6416. $this->loadFixtures('Post');
  6417. $result = $this->Form->input('Post.content', array(
  6418. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6419. ));
  6420. $expected = array(
  6421. 'div' => array('class' => 'input textarea'),
  6422. 'label' => array('for' => 'PostContent'),
  6423. 'Current Text',
  6424. '/label',
  6425. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6426. 'GREAT®',
  6427. '/textarea',
  6428. '/div'
  6429. );
  6430. $this->assertTags($result, $expected);
  6431. }
  6432. /**
  6433. * testHiddenField method
  6434. *
  6435. * @return void
  6436. */
  6437. public function testHiddenField() {
  6438. $Contact = ClassRegistry::getObject('Contact');
  6439. $Contact->validationErrors['field'] = 1;
  6440. $this->Form->request->data['Contact']['field'] = 'test';
  6441. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6442. $this->assertTags($result, array(
  6443. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6444. );
  6445. }
  6446. /**
  6447. * testFileUploadField method
  6448. *
  6449. * @return void
  6450. */
  6451. public function testFileUploadField() {
  6452. $result = $this->Form->file('Model.upload');
  6453. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6454. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6455. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6456. $expected = array(
  6457. 'div' => array('class' => 'input file'),
  6458. 'label' => array('for' => 'ModelUpload'),
  6459. 'Upload',
  6460. '/label',
  6461. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6462. '/div'
  6463. );
  6464. $this->assertTags($result, $expected);
  6465. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6466. $result = $this->Form->file('Model.upload');
  6467. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6468. }
  6469. /**
  6470. * test File upload input on a model not used in create();
  6471. *
  6472. * @return void
  6473. */
  6474. public function testFileUploadOnOtherModel() {
  6475. $this->Form->create('ValidateUser', array('type' => 'file'));
  6476. $result = $this->Form->file('ValidateProfile.city');
  6477. $expected = array(
  6478. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6479. );
  6480. $this->assertTags($result, $expected);
  6481. }
  6482. /**
  6483. * testButton method
  6484. *
  6485. * @return void
  6486. */
  6487. public function testButton() {
  6488. $result = $this->Form->button('Hi');
  6489. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6490. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6491. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6492. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6493. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6494. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6495. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6496. $result = $this->Form->button('No type', array('type' => false));
  6497. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6498. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6499. $this->assertNotRegExp('/\&039/', $result);
  6500. }
  6501. /**
  6502. * Test that button() makes unlocked fields by default.
  6503. *
  6504. * @return void
  6505. */
  6506. public function testButtonUnlockedByDefault() {
  6507. $this->Form->request->params['_Token']['key'] = 'secured';
  6508. $this->Form->button('Save', array('name' => 'save'));
  6509. $this->Form->button('Clear');
  6510. $result = $this->Form->unlockField();
  6511. $this->assertEquals(array('save'), $result);
  6512. }
  6513. /**
  6514. * testPostButton method
  6515. *
  6516. * @return void
  6517. */
  6518. public function testPostButton() {
  6519. $result = $this->Form->postButton('Hi', '/controller/action');
  6520. $this->assertTags($result, array(
  6521. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6522. 'div' => array('style' => 'display:none;'),
  6523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6524. '/div',
  6525. 'button' => array('type' => 'submit'),
  6526. 'Hi',
  6527. '/button',
  6528. '/form'
  6529. ));
  6530. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6531. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6532. }
  6533. /**
  6534. * Test that postButton adds _Token fields.
  6535. *
  6536. * @return void
  6537. */
  6538. public function testSecurePostButton() {
  6539. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6540. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6541. $expected = array(
  6542. 'form' => array(
  6543. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6544. ),
  6545. array('div' => array('style' => 'display:none;')),
  6546. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6547. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6548. '/div',
  6549. 'button' => array('type' => 'submit'),
  6550. 'Delete',
  6551. '/button',
  6552. array('div' => array('style' => 'display:none;')),
  6553. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6554. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6555. '/div',
  6556. '/form',
  6557. );
  6558. $this->assertTags($result, $expected);
  6559. }
  6560. /**
  6561. * testPostLink method
  6562. *
  6563. * @return void
  6564. */
  6565. public function testPostLink() {
  6566. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6567. $this->assertTags($result, array(
  6568. 'form' => array(
  6569. 'method' => 'post', 'action' => '/posts/delete/1',
  6570. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6571. ),
  6572. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6573. '/form',
  6574. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6575. 'Delete',
  6576. '/a'
  6577. ));
  6578. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6579. $this->assertTags($result, array(
  6580. 'form' => array(
  6581. 'method' => 'post', 'action' => '/posts/delete/1',
  6582. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6583. ),
  6584. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6585. '/form',
  6586. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6587. 'Delete',
  6588. '/a'
  6589. ));
  6590. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6591. $this->assertTags($result, array(
  6592. 'form' => array(
  6593. 'method' => 'post', 'action' => '/posts/delete/1',
  6594. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6595. ),
  6596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6597. '/form',
  6598. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6599. 'Delete',
  6600. '/a'
  6601. ));
  6602. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6603. $this->assertTags($result, array(
  6604. 'form' => array(
  6605. 'method' => 'post', 'action' => '/posts/delete/1',
  6606. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6607. ),
  6608. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6609. '/form',
  6610. '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;/'),
  6611. 'Delete',
  6612. '/a'
  6613. ));
  6614. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6615. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6616. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6617. $this->assertTags($result, array(
  6618. 'form' => array(
  6619. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6620. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6621. ),
  6622. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6623. '/form',
  6624. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6625. 'Delete',
  6626. '/a'
  6627. ));
  6628. $result = $this->Form->postLink(
  6629. '',
  6630. array('controller' => 'items', 'action' => 'delete', 10),
  6631. array('class' => 'btn btn-danger', 'escape' => false),
  6632. 'Confirm thing'
  6633. );
  6634. $this->assertTags($result, array(
  6635. 'form' => array(
  6636. 'method' => 'post', 'action' => '/items/delete/10',
  6637. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6638. ),
  6639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6640. '/form',
  6641. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6642. '/a'
  6643. ));
  6644. }
  6645. /**
  6646. * Test that postLink adds _Token fields.
  6647. *
  6648. * @return void
  6649. */
  6650. public function testSecurePostLink() {
  6651. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6652. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6653. $expected = array(
  6654. 'form' => array(
  6655. 'method' => 'post', 'action' => '/posts/delete/1',
  6656. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6657. ),
  6658. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6659. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6660. 'div' => array('style' => 'display:none;'),
  6661. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6662. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6663. '/div',
  6664. '/form',
  6665. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6666. 'Delete',
  6667. '/a'
  6668. );
  6669. $this->assertTags($result, $expected);
  6670. }
  6671. /**
  6672. * Test that postLink adds form tags to view block
  6673. *
  6674. * @return void
  6675. */
  6676. public function testPostLinkFormBuffer() {
  6677. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  6678. $this->assertTags($result, array(
  6679. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6680. 'Delete',
  6681. '/a'
  6682. ));
  6683. $result = $this->View->fetch('postLink');
  6684. $this->assertTags($result, array(
  6685. 'form' => array(
  6686. 'method' => 'post', 'action' => '/posts/delete/1',
  6687. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6688. ),
  6689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6690. '/form'
  6691. ));
  6692. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  6693. array('inline' => false, 'method' => 'DELETE')
  6694. );
  6695. $this->assertTags($result, array(
  6696. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6697. 'Delete',
  6698. '/a'
  6699. ));
  6700. $result = $this->View->fetch('postLink');
  6701. $this->assertTags($result, array(
  6702. 'form' => array(
  6703. 'method' => 'post', 'action' => '/posts/delete/1',
  6704. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6705. ),
  6706. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6707. '/form',
  6708. array(
  6709. 'form' => array(
  6710. 'method' => 'post', 'action' => '/posts/delete/2',
  6711. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6712. ),
  6713. ),
  6714. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  6715. '/form'
  6716. ));
  6717. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  6718. $this->assertTags($result, array(
  6719. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6720. 'Delete',
  6721. '/a'
  6722. ));
  6723. $result = $this->View->fetch('foobar');
  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. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6730. '/form'
  6731. ));
  6732. }
  6733. /**
  6734. * testSubmitButton method
  6735. *
  6736. * @return void
  6737. */
  6738. public function testSubmitButton() {
  6739. $result = $this->Form->submit('');
  6740. $expected = array(
  6741. 'div' => array('class' => 'submit'),
  6742. 'input' => array('type' => 'submit', 'value' => ''),
  6743. '/div'
  6744. );
  6745. $this->assertTags($result, $expected);
  6746. $result = $this->Form->submit('Test Submit');
  6747. $expected = array(
  6748. 'div' => array('class' => 'submit'),
  6749. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6750. '/div'
  6751. );
  6752. $this->assertTags($result, $expected);
  6753. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6754. $expected = array(
  6755. 'span' => array('class' => 'submit'),
  6756. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6757. '/span'
  6758. );
  6759. $this->assertTags($result, $expected);
  6760. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6761. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6762. $this->assertTags($result, $expected);
  6763. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6764. $expected = array(
  6765. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6766. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6767. '/div'
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $result = $this->Form->submit('Next >');
  6771. $expected = array(
  6772. 'div' => array('class' => 'submit'),
  6773. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6774. '/div'
  6775. );
  6776. $this->assertTags($result, $expected);
  6777. $result = $this->Form->submit('Next >', array('escape' => false));
  6778. $expected = array(
  6779. 'div' => array('class' => 'submit'),
  6780. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6781. '/div'
  6782. );
  6783. $this->assertTags($result, $expected);
  6784. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6785. $expected = array(
  6786. 'div' => array('class' => 'submit'),
  6787. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6788. '/div'
  6789. );
  6790. $this->assertTags($result, $expected);
  6791. $before = '--before--';
  6792. $after = '--after--';
  6793. $result = $this->Form->submit('Test', array('before' => $before));
  6794. $expected = array(
  6795. 'div' => array('class' => 'submit'),
  6796. '--before--',
  6797. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. $result = $this->Form->submit('Test', array('after' => $after));
  6802. $expected = array(
  6803. 'div' => array('class' => 'submit'),
  6804. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6805. '--after--',
  6806. '/div'
  6807. );
  6808. $this->assertTags($result, $expected);
  6809. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6810. $expected = array(
  6811. 'div' => array('class' => 'submit'),
  6812. '--before--',
  6813. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6814. '--after--',
  6815. '/div'
  6816. );
  6817. $this->assertTags($result, $expected);
  6818. }
  6819. /**
  6820. * test image submit types.
  6821. *
  6822. * @return void
  6823. */
  6824. public function testSubmitImage() {
  6825. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6826. $expected = array(
  6827. 'div' => array('class' => 'submit'),
  6828. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6829. '/div'
  6830. );
  6831. $this->assertTags($result, $expected);
  6832. $result = $this->Form->submit('/relative/cake.power.gif');
  6833. $expected = array(
  6834. 'div' => array('class' => 'submit'),
  6835. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6836. '/div'
  6837. );
  6838. $this->assertTags($result, $expected);
  6839. $result = $this->Form->submit('cake.power.gif');
  6840. $expected = array(
  6841. 'div' => array('class' => 'submit'),
  6842. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6843. '/div'
  6844. );
  6845. $this->assertTags($result, $expected);
  6846. $result = $this->Form->submit('Not.an.image');
  6847. $expected = array(
  6848. 'div' => array('class' => 'submit'),
  6849. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6850. '/div'
  6851. );
  6852. $this->assertTags($result, $expected);
  6853. $after = '--after--';
  6854. $before = '--before--';
  6855. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6856. $expected = array(
  6857. 'div' => array('class' => 'submit'),
  6858. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6859. '--after--',
  6860. '/div'
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6864. $expected = array(
  6865. 'div' => array('class' => 'submit'),
  6866. '--before--',
  6867. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6868. '/div'
  6869. );
  6870. $this->assertTags($result, $expected);
  6871. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6872. $expected = array(
  6873. 'div' => array('class' => 'submit'),
  6874. '--before--',
  6875. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6876. '--after--',
  6877. '/div'
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6881. $expected = array(
  6882. 'div' => array('class' => 'submit'),
  6883. '--before--',
  6884. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6885. '--after--',
  6886. '/div'
  6887. );
  6888. $this->assertTags($result, $expected);
  6889. }
  6890. /**
  6891. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6892. * be submitted at a time.
  6893. *
  6894. * @return void
  6895. */
  6896. public function testSubmitUnlockedByDefault() {
  6897. $this->Form->request->params['_Token']['key'] = 'secured';
  6898. $this->Form->submit('Go go');
  6899. $this->Form->submit('Save', array('name' => 'save'));
  6900. $result = $this->Form->unlockField();
  6901. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6902. }
  6903. /**
  6904. * Test submit image with timestamps.
  6905. *
  6906. * @return void
  6907. */
  6908. public function testSubmitImageTimestamp() {
  6909. Configure::write('Asset.timestamp', 'force');
  6910. $result = $this->Form->submit('cake.power.gif');
  6911. $expected = array(
  6912. 'div' => array('class' => 'submit'),
  6913. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6914. '/div'
  6915. );
  6916. $this->assertTags($result, $expected);
  6917. }
  6918. /**
  6919. * test the create() method
  6920. *
  6921. * @return void
  6922. */
  6923. public function testCreate() {
  6924. $result = $this->Form->create('Contact');
  6925. $encoding = strtolower(Configure::read('App.encoding'));
  6926. $expected = array(
  6927. 'form' => array(
  6928. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6929. 'accept-charset' => $encoding
  6930. ),
  6931. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6932. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6933. '/div'
  6934. );
  6935. $this->assertTags($result, $expected);
  6936. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6937. $expected = array('form' => array(
  6938. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6939. 'accept-charset' => $encoding
  6940. ));
  6941. $this->assertTags($result, $expected);
  6942. $result = $this->Form->create('Contact', array('type' => 'get'));
  6943. $expected = array('form' => array(
  6944. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6945. 'accept-charset' => $encoding
  6946. ));
  6947. $this->assertTags($result, $expected);
  6948. $result = $this->Form->create('Contact', array('type' => 'put'));
  6949. $expected = array(
  6950. 'form' => array(
  6951. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6952. 'accept-charset' => $encoding
  6953. ),
  6954. 'div' => array('style' => 'display:none;'),
  6955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6956. '/div'
  6957. );
  6958. $this->assertTags($result, $expected);
  6959. $result = $this->Form->create('Contact', array('type' => 'file'));
  6960. $expected = array(
  6961. 'form' => array(
  6962. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6963. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6964. ),
  6965. 'div' => array('style' => 'display:none;'),
  6966. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6967. '/div'
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $this->Form->request->data['Contact']['id'] = 1;
  6971. $this->Form->request->here = '/contacts/edit/1';
  6972. $this->Form->request['action'] = 'edit';
  6973. $result = $this->Form->create('Contact');
  6974. $expected = array(
  6975. 'form' => array(
  6976. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6977. 'accept-charset' => $encoding
  6978. ),
  6979. 'div' => array('style' => 'display:none;'),
  6980. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6981. '/div'
  6982. );
  6983. $this->assertTags($result, $expected);
  6984. $this->Form->request->data['Contact']['id'] = 1;
  6985. $this->Form->request->here = '/contacts/edit/1';
  6986. $this->Form->request['action'] = 'edit';
  6987. $result = $this->Form->create('Contact', array('type' => 'file'));
  6988. $expected = array(
  6989. 'form' => array(
  6990. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6991. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6992. ),
  6993. 'div' => array('style' => 'display:none;'),
  6994. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6995. '/div'
  6996. );
  6997. $this->assertTags($result, $expected);
  6998. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6999. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7000. $expected = array(
  7001. 'form' => array(
  7002. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7003. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7004. ),
  7005. 'div' => array('style' => 'display:none;'),
  7006. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7007. '/div'
  7008. );
  7009. $this->assertTags($result, $expected);
  7010. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7011. $expected = array(
  7012. 'form' => array(
  7013. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7014. 'accept-charset' => $encoding
  7015. ),
  7016. 'div' => array('style' => 'display:none;'),
  7017. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7018. '/div'
  7019. );
  7020. $this->assertTags($result, $expected);
  7021. $this->Form->request['action'] = 'add';
  7022. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7023. $expected = array(
  7024. 'form' => array(
  7025. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7026. 'accept-charset' => $encoding
  7027. ),
  7028. 'div' => array('style' => 'display:none;'),
  7029. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7030. '/div'
  7031. );
  7032. $this->assertTags($result, $expected);
  7033. $result = $this->Form->create('User', array('action' => 'login'));
  7034. $expected = array(
  7035. 'form' => array(
  7036. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7037. 'accept-charset' => $encoding
  7038. ),
  7039. 'div' => array('style' => 'display:none;'),
  7040. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7041. '/div'
  7042. );
  7043. $this->assertTags($result, $expected);
  7044. $result = $this->Form->create('User', array('url' => '/users/login'));
  7045. $expected = array(
  7046. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7047. 'div' => array('style' => 'display:none;'),
  7048. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7049. '/div'
  7050. );
  7051. $this->assertTags($result, $expected);
  7052. $this->Form->request['controller'] = 'pages';
  7053. $result = $this->Form->create('User', array('action' => 'signup'));
  7054. $expected = array(
  7055. 'form' => array(
  7056. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7057. 'accept-charset' => $encoding
  7058. ),
  7059. 'div' => array('style' => 'display:none;'),
  7060. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7061. '/div'
  7062. );
  7063. $this->assertTags($result, $expected);
  7064. $this->Form->request->data = array();
  7065. $this->Form->request['controller'] = 'contacts';
  7066. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7067. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7068. $expected = array(
  7069. 'form' => array(
  7070. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7071. 'accept-charset' => 'utf-8'
  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. }
  7079. /**
  7080. * Test the onsubmit option for create()
  7081. *
  7082. * @return void
  7083. */
  7084. public function testCreateOnSubmit() {
  7085. $this->Form->request->data = array();
  7086. $this->Form->request['controller'] = 'contacts';
  7087. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7088. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7089. $expected = array(
  7090. 'form' => array(
  7091. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7092. 'accept-charset' => 'utf-8'
  7093. ),
  7094. 'div' => array('style' => 'display:none;'),
  7095. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7096. '/div'
  7097. );
  7098. $this->assertTags($result, $expected);
  7099. $this->Form->request->data = array();
  7100. $this->Form->request['controller'] = 'contacts';
  7101. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7102. $result = $this->Form->create(array(
  7103. 'url' => array('action' => 'index', 'param'),
  7104. 'default' => false,
  7105. 'onsubmit' => 'someFunction();'
  7106. ));
  7107. $expected = array(
  7108. 'form' => array(
  7109. 'id' => 'ContactAddForm', 'method' => 'post',
  7110. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7111. 'action' => '/contacts/index/param',
  7112. 'accept-charset' => 'utf-8'
  7113. ),
  7114. 'div' => array('style' => 'display:none;'),
  7115. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7116. '/div'
  7117. );
  7118. $this->assertTags($result, $expected);
  7119. }
  7120. /**
  7121. * test create() with automatic url generation
  7122. *
  7123. * @return void
  7124. */
  7125. public function testCreateAutoUrl() {
  7126. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7127. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7128. $this->Form->request->base = '/base_url';
  7129. $result = $this->Form->create('Contact');
  7130. $expected = array(
  7131. 'form' => array(
  7132. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7133. 'accept-charset' => 'utf-8'
  7134. ),
  7135. 'div' => array('style' => 'display:none;'),
  7136. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7137. '/div'
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $this->Form->request['action'] = 'delete';
  7141. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7142. $this->Form->request->base = '/base_url';
  7143. $result = $this->Form->create('Contact');
  7144. $expected = array(
  7145. 'form' => array(
  7146. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7147. 'accept-charset' => 'utf-8'
  7148. ),
  7149. 'div' => array('style' => 'display:none;'),
  7150. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7151. '/div'
  7152. );
  7153. $this->assertTags($result, $expected);
  7154. }
  7155. /**
  7156. * test create() with a custom route
  7157. *
  7158. * @return void
  7159. */
  7160. public function testCreateCustomRoute() {
  7161. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7162. $encoding = strtolower(Configure::read('App.encoding'));
  7163. $result = $this->Form->create('User', array('action' => 'login'));
  7164. $expected = array(
  7165. 'form' => array(
  7166. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7167. 'accept-charset' => $encoding
  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. }
  7175. /**
  7176. * test that inputDefaults are stored and used.
  7177. *
  7178. * @return void
  7179. */
  7180. public function testCreateWithInputDefaults() {
  7181. $this->Form->create('User', array(
  7182. 'inputDefaults' => array(
  7183. 'div' => false,
  7184. 'label' => false,
  7185. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7186. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7187. )
  7188. ));
  7189. $result = $this->Form->input('username');
  7190. $expected = array(
  7191. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7192. );
  7193. $this->assertTags($result, $expected);
  7194. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7195. $expected = array(
  7196. 'div' => array('class' => 'input text'),
  7197. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7198. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7199. '/div'
  7200. );
  7201. $this->assertTags($result, $expected);
  7202. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7203. $expected = array(
  7204. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7205. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7206. );
  7207. $this->assertTags($result, $expected);
  7208. $this->Form->create('User', array(
  7209. 'inputDefaults' => array(
  7210. 'div' => false,
  7211. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7212. )
  7213. ));
  7214. $result = $this->Form->input('username', array('div' => true));
  7215. $expected = array(
  7216. 'div' => array('class' => 'input text'),
  7217. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7218. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7219. '/div'
  7220. );
  7221. $this->assertTags($result, $expected);
  7222. }
  7223. /**
  7224. * test automatic accept-charset overriding
  7225. *
  7226. * @return void
  7227. */
  7228. public function testCreateWithAcceptCharset() {
  7229. $result = $this->Form->create('UserForm', array(
  7230. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7231. )
  7232. );
  7233. $expected = array(
  7234. 'form' => array(
  7235. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7236. 'accept-charset' => 'iso-8859-1'
  7237. ),
  7238. 'div' => array('style' => 'display:none;'),
  7239. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7240. '/div'
  7241. );
  7242. $this->assertTags($result, $expected);
  7243. }
  7244. /**
  7245. * Test base form URL when url param is passed with multiple parameters (&)
  7246. *
  7247. */
  7248. public function testCreateQuerystringrequest() {
  7249. $encoding = strtolower(Configure::read('App.encoding'));
  7250. $result = $this->Form->create('Contact', array(
  7251. 'type' => 'post',
  7252. 'escape' => false,
  7253. 'url' => array(
  7254. 'controller' => 'controller',
  7255. 'action' => 'action',
  7256. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7257. )
  7258. ));
  7259. $expected = array(
  7260. 'form' => array(
  7261. 'id' => 'ContactAddForm',
  7262. 'method' => 'post',
  7263. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7264. 'accept-charset' => $encoding
  7265. ),
  7266. 'div' => array('style' => 'display:none;'),
  7267. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7268. '/div'
  7269. );
  7270. $this->assertTags($result, $expected);
  7271. $result = $this->Form->create('Contact', array(
  7272. 'type' => 'post',
  7273. 'url' => array(
  7274. 'controller' => 'controller',
  7275. 'action' => 'action',
  7276. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7277. )
  7278. ));
  7279. $expected = array(
  7280. 'form' => array(
  7281. 'id' => 'ContactAddForm',
  7282. 'method' => 'post',
  7283. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7284. 'accept-charset' => $encoding
  7285. ),
  7286. 'div' => array('style' => 'display:none;'),
  7287. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7288. '/div'
  7289. );
  7290. $this->assertTags($result, $expected);
  7291. }
  7292. /**
  7293. * test that create() doesn't cause errors by multiple id's being in the primary key
  7294. * as could happen with multiple select or checkboxes.
  7295. *
  7296. * @return void
  7297. */
  7298. public function testCreateWithMultipleIdInData() {
  7299. $encoding = strtolower(Configure::read('App.encoding'));
  7300. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7301. $result = $this->Form->create('Contact');
  7302. $expected = array(
  7303. 'form' => array(
  7304. 'id' => 'ContactAddForm',
  7305. 'method' => 'post',
  7306. 'action' => '/contacts/add',
  7307. 'accept-charset' => $encoding
  7308. ),
  7309. 'div' => array('style' => 'display:none;'),
  7310. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7311. '/div'
  7312. );
  7313. $this->assertTags($result, $expected);
  7314. }
  7315. /**
  7316. * test that create() doesn't add in extra passed params.
  7317. *
  7318. * @return void
  7319. */
  7320. public function testCreatePassedArgs() {
  7321. $encoding = strtolower(Configure::read('App.encoding'));
  7322. $this->Form->request->data['Contact']['id'] = 1;
  7323. $result = $this->Form->create('Contact', array(
  7324. 'type' => 'post',
  7325. 'escape' => false,
  7326. 'url' => array(
  7327. 'action' => 'edit',
  7328. 'myparam'
  7329. )
  7330. ));
  7331. $expected = array(
  7332. 'form' => array(
  7333. 'id' => 'ContactAddForm',
  7334. 'method' => 'post',
  7335. 'action' => '/contacts/edit/myparam',
  7336. 'accept-charset' => $encoding
  7337. ),
  7338. 'div' => array('style' => 'display:none;'),
  7339. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7340. '/div'
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. }
  7344. /**
  7345. * test creating a get form, and get form inputs.
  7346. *
  7347. * @return void
  7348. */
  7349. public function testGetFormCreate() {
  7350. $encoding = strtolower(Configure::read('App.encoding'));
  7351. $result = $this->Form->create('Contact', array('type' => 'get'));
  7352. $this->assertTags($result, array('form' => array(
  7353. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7354. 'accept-charset' => $encoding
  7355. )));
  7356. $result = $this->Form->text('Contact.name');
  7357. $this->assertTags($result, array('input' => array(
  7358. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7359. )));
  7360. $result = $this->Form->password('password');
  7361. $this->assertTags($result, array('input' => array(
  7362. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7363. )));
  7364. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7365. $result = $this->Form->text('user_form');
  7366. $this->assertTags($result, array('input' => array(
  7367. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7368. )));
  7369. }
  7370. /**
  7371. * test get form, and inputs when the model param is false
  7372. *
  7373. * @return void
  7374. */
  7375. public function testGetFormWithFalseModel() {
  7376. $encoding = strtolower(Configure::read('App.encoding'));
  7377. $this->Form->request['controller'] = 'contact_test';
  7378. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7379. $expected = array('form' => array(
  7380. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7381. 'accept-charset' => $encoding
  7382. ));
  7383. $this->assertTags($result, $expected);
  7384. $result = $this->Form->text('reason');
  7385. $expected = array(
  7386. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7387. );
  7388. $this->assertTags($result, $expected);
  7389. }
  7390. /**
  7391. * test that datetime() works with GET style forms.
  7392. *
  7393. * @return void
  7394. */
  7395. public function testDateTimeWithGetForms() {
  7396. extract($this->dateRegex);
  7397. $this->Form->create('Contact', array('type' => 'get'));
  7398. $result = $this->Form->datetime('created');
  7399. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7400. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7401. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7402. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7403. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7404. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7405. }
  7406. /**
  7407. * testEditFormWithData method
  7408. *
  7409. * test auto populating form elements from submitted data.
  7410. *
  7411. * @return void
  7412. */
  7413. public function testEditFormWithData() {
  7414. $this->Form->request->data = array('Person' => array(
  7415. 'id' => 1,
  7416. 'first_name' => 'Nate',
  7417. 'last_name' => 'Abele',
  7418. 'email' => 'nate@example.com'
  7419. ));
  7420. $this->Form->request->addParams(array(
  7421. 'models' => array('Person'),
  7422. 'controller' => 'people',
  7423. 'action' => 'add'
  7424. ));
  7425. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7426. $this->Form->create();
  7427. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7428. $expected = array(
  7429. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7430. 'select' => array(
  7431. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7432. ),
  7433. array('option' => array('value' => 1)), 'Nate', '/option',
  7434. array('option' => array('value' => 2)), 'Garrett', '/option',
  7435. array('option' => array('value' => 3)), 'Larry', '/option',
  7436. '/select'
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. }
  7440. /**
  7441. * Test that required fields are created for various types of validation.
  7442. *
  7443. * @return void
  7444. */
  7445. public function testFormInputRequiredDetection() {
  7446. $this->Form->create('Contact');
  7447. $result = $this->Form->input('Contact.non_existing');
  7448. $expected = array(
  7449. 'div' => array('class' => 'input text'),
  7450. 'label' => array('for' => 'ContactNonExisting'),
  7451. 'Non Existing',
  7452. '/label',
  7453. 'input' => array(
  7454. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7455. 'id' => 'ContactNonExisting'
  7456. ),
  7457. '/div'
  7458. );
  7459. $this->assertTags($result, $expected);
  7460. $result = $this->Form->input('Contact.imrequired');
  7461. $expected = array(
  7462. 'div' => array('class' => 'input text required'),
  7463. 'label' => array('for' => 'ContactImrequired'),
  7464. 'Imrequired',
  7465. '/label',
  7466. 'input' => array(
  7467. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7468. 'id' => 'ContactImrequired',
  7469. 'required' => 'required'
  7470. ),
  7471. '/div'
  7472. );
  7473. $this->assertTags($result, $expected);
  7474. $result = $this->Form->input('Contact.imalsorequired');
  7475. $expected = array(
  7476. 'div' => array('class' => 'input text required'),
  7477. 'label' => array('for' => 'ContactImalsorequired'),
  7478. 'Imalsorequired',
  7479. '/label',
  7480. 'input' => array(
  7481. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7482. 'id' => 'ContactImalsorequired',
  7483. 'required' => 'required'
  7484. ),
  7485. '/div'
  7486. );
  7487. $this->assertTags($result, $expected);
  7488. $result = $this->Form->input('Contact.imrequiredtoo');
  7489. $expected = array(
  7490. 'div' => array('class' => 'input text required'),
  7491. 'label' => array('for' => 'ContactImrequiredtoo'),
  7492. 'Imrequiredtoo',
  7493. '/label',
  7494. 'input' => array(
  7495. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7496. 'id' => 'ContactImrequiredtoo',
  7497. 'required' => 'required'
  7498. ),
  7499. '/div'
  7500. );
  7501. $this->assertTags($result, $expected);
  7502. $result = $this->Form->input('Contact.required_one');
  7503. $expected = array(
  7504. 'div' => array('class' => 'input text required'),
  7505. 'label' => array('for' => 'ContactRequiredOne'),
  7506. 'Required One',
  7507. '/label',
  7508. 'input' => array(
  7509. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7510. 'id' => 'ContactRequiredOne',
  7511. 'required' => 'required'
  7512. ),
  7513. '/div'
  7514. );
  7515. $this->assertTags($result, $expected);
  7516. $result = $this->Form->input('Contact.string_required');
  7517. $expected = array(
  7518. 'div' => array('class' => 'input text required'),
  7519. 'label' => array('for' => 'ContactStringRequired'),
  7520. 'String Required',
  7521. '/label',
  7522. 'input' => array(
  7523. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7524. 'id' => 'ContactStringRequired',
  7525. 'required' => 'required'
  7526. ),
  7527. '/div'
  7528. );
  7529. $this->assertTags($result, $expected);
  7530. $result = $this->Form->input('Contact.imnotrequired');
  7531. $expected = array(
  7532. 'div' => array('class' => 'input text'),
  7533. 'label' => array('for' => 'ContactImnotrequired'),
  7534. 'Imnotrequired',
  7535. '/label',
  7536. 'input' => array(
  7537. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7538. 'id' => 'ContactImnotrequired'
  7539. ),
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $result = $this->Form->input('Contact.imalsonotrequired');
  7544. $expected = array(
  7545. 'div' => array('class' => 'input text'),
  7546. 'label' => array('for' => 'ContactImalsonotrequired'),
  7547. 'Imalsonotrequired',
  7548. '/label',
  7549. 'input' => array(
  7550. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7551. 'id' => 'ContactImalsonotrequired'
  7552. ),
  7553. '/div'
  7554. );
  7555. $this->assertTags($result, $expected);
  7556. $result = $this->Form->input('Contact.imalsonotrequired2');
  7557. $expected = array(
  7558. 'div' => array('class' => 'input text'),
  7559. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7560. 'Imalsonotrequired2',
  7561. '/label',
  7562. 'input' => array(
  7563. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7564. 'id' => 'ContactImalsonotrequired2'
  7565. ),
  7566. '/div'
  7567. );
  7568. $this->assertTags($result, $expected);
  7569. $result = $this->Form->input('Contact.imnotrequiredeither');
  7570. $expected = array(
  7571. 'div' => array('class' => 'input text'),
  7572. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7573. 'Imnotrequiredeither',
  7574. '/label',
  7575. 'input' => array(
  7576. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7577. 'id' => 'ContactImnotrequiredeither'
  7578. ),
  7579. '/div'
  7580. );
  7581. $this->assertTags($result, $expected);
  7582. $result = $this->Form->input('Contact.iamrequiredalways');
  7583. $expected = array(
  7584. 'div' => array('class' => 'input text required'),
  7585. 'label' => array('for' => 'ContactIamrequiredalways'),
  7586. 'Iamrequiredalways',
  7587. '/label',
  7588. 'input' => array(
  7589. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7590. 'id' => 'ContactIamrequiredalways',
  7591. 'required' => 'required'
  7592. ),
  7593. '/div'
  7594. );
  7595. $this->assertTags($result, $expected);
  7596. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7597. $expected = array(
  7598. 'div' => array('class' => 'input checkbox required'),
  7599. array('input' => array(
  7600. 'type' => 'hidden',
  7601. 'name' => 'data[Contact][boolean_field]',
  7602. 'id' => 'ContactBooleanField_',
  7603. 'value' => '0'
  7604. )),
  7605. array('input' => array(
  7606. 'type' => 'checkbox',
  7607. 'name' => 'data[Contact][boolean_field]',
  7608. 'value' => '1',
  7609. 'id' => 'ContactBooleanField'
  7610. )),
  7611. 'label' => array('for' => 'ContactBooleanField'),
  7612. 'Boolean Field',
  7613. '/label',
  7614. '/div'
  7615. );
  7616. $this->assertTags($result, $expected);
  7617. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7618. $expected = array(
  7619. 'div' => array('class' => 'input checkbox required'),
  7620. array('input' => array(
  7621. 'type' => 'hidden',
  7622. 'name' => 'data[Contact][boolean_field]',
  7623. 'id' => 'ContactBooleanField_',
  7624. 'value' => '0'
  7625. )),
  7626. array('input' => array(
  7627. 'type' => 'checkbox',
  7628. 'name' => 'data[Contact][boolean_field]',
  7629. 'value' => '1',
  7630. 'id' => 'ContactBooleanField',
  7631. 'required' => 'required'
  7632. )),
  7633. 'label' => array('for' => 'ContactBooleanField'),
  7634. 'Boolean Field',
  7635. '/label',
  7636. '/div'
  7637. );
  7638. $this->assertTags($result, $expected);
  7639. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7640. $expected = array(
  7641. 'div' => array('class' => 'input file required'),
  7642. 'label' => array('for' => 'ContactIamrequiredalways'),
  7643. 'Iamrequiredalways',
  7644. '/label',
  7645. 'input' => array(
  7646. 'type' => 'file',
  7647. 'name' => 'data[Contact][iamrequiredalways]',
  7648. 'id' => 'ContactIamrequiredalways',
  7649. 'required' => 'required'
  7650. ),
  7651. '/div'
  7652. );
  7653. $this->assertTags($result, $expected);
  7654. }
  7655. /**
  7656. * Test that required fields are created when only using ModelValidator::add().
  7657. *
  7658. * @return void
  7659. */
  7660. public function testFormInputRequiredDetectionModelValidator() {
  7661. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7662. $this->Form->create('ContactTag');
  7663. $result = $this->Form->input('ContactTag.iwillberequired');
  7664. $expected = array(
  7665. 'div' => array('class' => 'input text required'),
  7666. 'label' => array('for' => 'ContactTagIwillberequired'),
  7667. 'Iwillberequired',
  7668. '/label',
  7669. 'input' => array(
  7670. 'name' => 'data[ContactTag][iwillberequired]',
  7671. 'type' => 'text',
  7672. 'id' => 'ContactTagIwillberequired',
  7673. 'required' => 'required'
  7674. ),
  7675. '/div'
  7676. );
  7677. $this->assertTags($result, $expected);
  7678. }
  7679. /**
  7680. * testFormMagicInput method
  7681. *
  7682. * @return void
  7683. */
  7684. public function testFormMagicInput() {
  7685. $encoding = strtolower(Configure::read('App.encoding'));
  7686. $result = $this->Form->create('Contact');
  7687. $expected = array(
  7688. 'form' => array(
  7689. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7690. 'accept-charset' => $encoding
  7691. ),
  7692. 'div' => array('style' => 'display:none;'),
  7693. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7694. '/div'
  7695. );
  7696. $this->assertTags($result, $expected);
  7697. $result = $this->Form->input('name');
  7698. $expected = array(
  7699. 'div' => array('class' => 'input text'),
  7700. 'label' => array('for' => 'ContactName'),
  7701. 'Name',
  7702. '/label',
  7703. 'input' => array(
  7704. 'type' => 'text', 'name' => 'data[Contact][name]',
  7705. 'id' => 'ContactName', 'maxlength' => '255'
  7706. ),
  7707. '/div'
  7708. );
  7709. $this->assertTags($result, $expected);
  7710. $result = $this->Form->input('non_existing_field_in_contact_model');
  7711. $expected = array(
  7712. 'div' => array('class' => 'input text'),
  7713. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7714. 'Non Existing Field In Contact Model',
  7715. '/label',
  7716. 'input' => array(
  7717. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7718. 'id' => 'ContactNonExistingFieldInContactModel'
  7719. ),
  7720. '/div'
  7721. );
  7722. $this->assertTags($result, $expected);
  7723. $result = $this->Form->input('Address.street');
  7724. $expected = array(
  7725. 'div' => array('class' => 'input text'),
  7726. 'label' => array('for' => 'AddressStreet'),
  7727. 'Street',
  7728. '/label',
  7729. 'input' => array(
  7730. 'type' => 'text', 'name' => 'data[Address][street]',
  7731. 'id' => 'AddressStreet'
  7732. ),
  7733. '/div'
  7734. );
  7735. $this->assertTags($result, $expected);
  7736. $result = $this->Form->input('Address.non_existing_field_in_model');
  7737. $expected = array(
  7738. 'div' => array('class' => 'input text'),
  7739. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7740. 'Non Existing Field In Model',
  7741. '/label',
  7742. 'input' => array(
  7743. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7744. 'id' => 'AddressNonExistingFieldInModel'
  7745. ),
  7746. '/div'
  7747. );
  7748. $this->assertTags($result, $expected);
  7749. $result = $this->Form->input('name', array('div' => false));
  7750. $expected = array(
  7751. 'label' => array('for' => 'ContactName'),
  7752. 'Name',
  7753. '/label',
  7754. 'input' => array(
  7755. 'type' => 'text', 'name' => 'data[Contact][name]',
  7756. 'id' => 'ContactName', 'maxlength' => '255'
  7757. )
  7758. );
  7759. $this->assertTags($result, $expected);
  7760. extract($this->dateRegex);
  7761. $now = strtotime('now');
  7762. $result = $this->Form->input('Contact.published', array('div' => false));
  7763. $expected = array(
  7764. 'label' => array('for' => 'ContactPublishedMonth'),
  7765. 'Published',
  7766. '/label',
  7767. array('select' => array(
  7768. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7769. )),
  7770. $monthsRegex,
  7771. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7772. date('F', $now),
  7773. '/option',
  7774. '*/select',
  7775. '-',
  7776. array('select' => array(
  7777. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7778. )),
  7779. $daysRegex,
  7780. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7781. date('j', $now),
  7782. '/option',
  7783. '*/select',
  7784. '-',
  7785. array('select' => array(
  7786. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7787. )),
  7788. $yearsRegex,
  7789. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7790. date('Y', $now),
  7791. '*/select'
  7792. );
  7793. $this->assertTags($result, $expected);
  7794. $result = $this->Form->input('Contact.updated', array('div' => false));
  7795. $expected = array(
  7796. 'label' => array('for' => 'ContactUpdatedMonth'),
  7797. 'Updated',
  7798. '/label',
  7799. array('select' => array(
  7800. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7801. )),
  7802. $monthsRegex,
  7803. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7804. date('F', $now),
  7805. '/option',
  7806. '*/select',
  7807. '-',
  7808. array('select' => array(
  7809. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7810. )),
  7811. $daysRegex,
  7812. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7813. date('j', $now),
  7814. '/option',
  7815. '*/select',
  7816. '-',
  7817. array('select' => array(
  7818. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7819. )),
  7820. $yearsRegex,
  7821. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7822. date('Y', $now),
  7823. '*/select'
  7824. );
  7825. $this->assertTags($result, $expected);
  7826. $result = $this->Form->input('UserForm.stuff');
  7827. $expected = array(
  7828. 'div' => array('class' => 'input text'),
  7829. 'label' => array('for' => 'UserFormStuff'),
  7830. 'Stuff',
  7831. '/label',
  7832. 'input' => array(
  7833. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7834. 'id' => 'UserFormStuff', 'maxlength' => 10
  7835. ),
  7836. '/div'
  7837. );
  7838. $this->assertTags($result, $expected);
  7839. }
  7840. /**
  7841. * testForMagicInputNonExistingNorValidated method
  7842. *
  7843. * @return void
  7844. */
  7845. public function testForMagicInputNonExistingNorValidated() {
  7846. $encoding = strtolower(Configure::read('App.encoding'));
  7847. $result = $this->Form->create('Contact');
  7848. $expected = array(
  7849. 'form' => array(
  7850. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7851. 'accept-charset' => $encoding
  7852. ),
  7853. 'div' => array('style' => 'display:none;'),
  7854. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7855. '/div'
  7856. );
  7857. $this->assertTags($result, $expected);
  7858. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7859. $expected = array(
  7860. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7861. 'Non Existing Nor Validated',
  7862. '/label',
  7863. 'input' => array(
  7864. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7865. 'id' => 'ContactNonExistingNorValidated'
  7866. )
  7867. );
  7868. $this->assertTags($result, $expected);
  7869. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7870. 'div' => false, 'value' => 'my value'
  7871. ));
  7872. $expected = array(
  7873. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7874. 'Non Existing Nor Validated',
  7875. '/label',
  7876. 'input' => array(
  7877. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7878. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7879. )
  7880. );
  7881. $this->assertTags($result, $expected);
  7882. $this->Form->request->data = array(
  7883. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7884. ));
  7885. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7886. $expected = array(
  7887. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7888. 'Non Existing Nor Validated',
  7889. '/label',
  7890. 'input' => array(
  7891. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7892. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7893. )
  7894. );
  7895. $this->assertTags($result, $expected);
  7896. }
  7897. /**
  7898. * testFormMagicInputLabel method
  7899. *
  7900. * @return void
  7901. */
  7902. public function testFormMagicInputLabel() {
  7903. $encoding = strtolower(Configure::read('App.encoding'));
  7904. $result = $this->Form->create('Contact');
  7905. $expected = array(
  7906. 'form' => array(
  7907. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7908. 'accept-charset' => $encoding
  7909. ),
  7910. 'div' => array('style' => 'display:none;'),
  7911. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7912. '/div'
  7913. );
  7914. $this->assertTags($result, $expected);
  7915. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7916. $this->assertTags($result, array('input' => array(
  7917. 'name' => 'data[Contact][name]', 'type' => 'text',
  7918. 'id' => 'ContactName', 'maxlength' => '255')
  7919. ));
  7920. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7921. $expected = array(
  7922. 'label' => array('for' => 'ContactName'),
  7923. 'My label',
  7924. '/label',
  7925. 'input' => array(
  7926. 'type' => 'text', 'name' => 'data[Contact][name]',
  7927. 'id' => 'ContactName', 'maxlength' => '255'
  7928. )
  7929. );
  7930. $this->assertTags($result, $expected);
  7931. $result = $this->Form->input('Contact.name', array(
  7932. 'div' => false, 'label' => array('class' => 'mandatory')
  7933. ));
  7934. $expected = array(
  7935. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7936. 'Name',
  7937. '/label',
  7938. 'input' => array(
  7939. 'type' => 'text', 'name' => 'data[Contact][name]',
  7940. 'id' => 'ContactName', 'maxlength' => '255'
  7941. )
  7942. );
  7943. $this->assertTags($result, $expected);
  7944. $result = $this->Form->input('Contact.name', array(
  7945. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7946. ));
  7947. $expected = array(
  7948. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7949. 'My label',
  7950. '/label',
  7951. 'input' => array(
  7952. 'type' => 'text', 'name' => 'data[Contact][name]',
  7953. 'id' => 'ContactName', 'maxlength' => '255'
  7954. )
  7955. );
  7956. $this->assertTags($result, $expected);
  7957. $result = $this->Form->input('Contact.name', array(
  7958. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7959. ));
  7960. $expected = array(
  7961. 'label' => array('for' => 'my_id'),
  7962. 'Name',
  7963. '/label',
  7964. 'input' => array(
  7965. 'type' => 'text', 'name' => 'data[Contact][name]',
  7966. 'id' => 'my_id', 'maxlength' => '255'
  7967. )
  7968. );
  7969. $this->assertTags($result, $expected);
  7970. $result = $this->Form->input('1.id');
  7971. $this->assertTags($result, array('input' => array(
  7972. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7973. 'id' => 'Contact1Id'
  7974. )));
  7975. $result = $this->Form->input("1.name");
  7976. $expected = array(
  7977. 'div' => array('class' => 'input text'),
  7978. 'label' => array('for' => 'Contact1Name'),
  7979. 'Name',
  7980. '/label',
  7981. 'input' => array(
  7982. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7983. 'id' => 'Contact1Name', 'maxlength' => '255'
  7984. ),
  7985. '/div'
  7986. );
  7987. $this->assertTags($result, $expected);
  7988. $result = $this->Form->input('Contact.1.id');
  7989. $this->assertTags($result, array(
  7990. 'input' => array(
  7991. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7992. 'id' => 'Contact1Id'
  7993. )
  7994. ));
  7995. $result = $this->Form->input("Model.1.name");
  7996. $expected = array(
  7997. 'div' => array('class' => 'input text'),
  7998. 'label' => array('for' => 'Model1Name'),
  7999. 'Name',
  8000. '/label',
  8001. 'input' => array(
  8002. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8003. 'id' => 'Model1Name'
  8004. ),
  8005. '/div'
  8006. );
  8007. $this->assertTags($result, $expected);
  8008. }
  8009. /**
  8010. * testFormEnd method
  8011. *
  8012. * @return void
  8013. */
  8014. public function testFormEnd() {
  8015. $this->assertEquals('</form>', $this->Form->end());
  8016. $result = $this->Form->end('');
  8017. $expected = array(
  8018. 'div' => array('class' => 'submit'),
  8019. 'input' => array('type' => 'submit', 'value' => ''),
  8020. '/div',
  8021. '/form'
  8022. );
  8023. $this->assertTags($result, $expected);
  8024. $result = $this->Form->end(array('label' => ''));
  8025. $expected = array(
  8026. 'div' => array('class' => 'submit'),
  8027. 'input' => array('type' => 'submit', 'value' => ''),
  8028. '/div',
  8029. '/form'
  8030. );
  8031. $this->assertTags($result, $expected);
  8032. $result = $this->Form->end('save');
  8033. $expected = array(
  8034. 'div' => array('class' => 'submit'),
  8035. 'input' => array('type' => 'submit', 'value' => 'save'),
  8036. '/div',
  8037. '/form'
  8038. );
  8039. $this->assertTags($result, $expected);
  8040. $result = $this->Form->end(array('label' => 'save'));
  8041. $expected = array(
  8042. 'div' => array('class' => 'submit'),
  8043. 'input' => array('type' => 'submit', 'value' => 'save'),
  8044. '/div',
  8045. '/form'
  8046. );
  8047. $this->assertTags($result, $expected);
  8048. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8049. $expected = array(
  8050. 'div' => array('class' => 'submit'),
  8051. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8052. '/div',
  8053. '/form'
  8054. );
  8055. $this->assertTags($result, $expected);
  8056. $result = $this->Form->end(array('name' => 'Whatever'));
  8057. $expected = array(
  8058. 'div' => array('class' => 'submit'),
  8059. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8060. '/div',
  8061. '/form'
  8062. );
  8063. $this->assertTags($result, $expected);
  8064. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8065. $expected = array(
  8066. 'div' => array('class' => 'good'),
  8067. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8068. '/div',
  8069. '/form'
  8070. );
  8071. $this->assertTags($result, $expected);
  8072. $result = $this->Form->end(array(
  8073. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8074. ));
  8075. $expected = array(
  8076. 'div' => array('class' => 'good'),
  8077. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8078. '/div',
  8079. '/form'
  8080. );
  8081. $this->assertTags($result, $expected);
  8082. }
  8083. /**
  8084. * testMultipleFormWithIdFields method
  8085. *
  8086. * @return void
  8087. */
  8088. public function testMultipleFormWithIdFields() {
  8089. $this->Form->create('UserForm');
  8090. $result = $this->Form->input('id');
  8091. $this->assertTags($result, array('input' => array(
  8092. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8093. )));
  8094. $result = $this->Form->input('ValidateItem.id');
  8095. $this->assertTags($result, array('input' => array(
  8096. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8097. 'id' => 'ValidateItemId'
  8098. )));
  8099. $result = $this->Form->input('ValidateUser.id');
  8100. $this->assertTags($result, array('input' => array(
  8101. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8102. 'id' => 'ValidateUserId'
  8103. )));
  8104. }
  8105. /**
  8106. * testDbLessModel method
  8107. *
  8108. * @return void
  8109. */
  8110. public function testDbLessModel() {
  8111. $this->Form->create('TestMail');
  8112. $result = $this->Form->input('name');
  8113. $expected = array(
  8114. 'div' => array('class' => 'input text'),
  8115. 'label' => array('for' => 'TestMailName'),
  8116. 'Name',
  8117. '/label',
  8118. 'input' => array(
  8119. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8120. 'id' => 'TestMailName'
  8121. ),
  8122. '/div'
  8123. );
  8124. $this->assertTags($result, $expected);
  8125. ClassRegistry::init('TestMail');
  8126. $this->Form->create('TestMail');
  8127. $result = $this->Form->input('name');
  8128. $expected = array(
  8129. 'div' => array('class' => 'input text'),
  8130. 'label' => array('for' => 'TestMailName'),
  8131. 'Name',
  8132. '/label',
  8133. 'input' => array(
  8134. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8135. 'id' => 'TestMailName'
  8136. ),
  8137. '/div'
  8138. );
  8139. $this->assertTags($result, $expected);
  8140. }
  8141. /**
  8142. * testBrokenness method
  8143. *
  8144. * @return void
  8145. */
  8146. public function testBrokenness() {
  8147. /*
  8148. * #4 This test has two parents and four children. By default (as of r7117) both
  8149. * parents are show but the first parent is missing a child. This is the inconsistency
  8150. * in the default behaviour - one parent has all children, the other does not - dependent
  8151. * on the data values.
  8152. */
  8153. $result = $this->Form->select('Model.field', array(
  8154. 'Fred' => array(
  8155. 'freds_son_1' => 'Fred',
  8156. 'freds_son_2' => 'Freddie'
  8157. ),
  8158. 'Bert' => array(
  8159. 'berts_son_1' => 'Albert',
  8160. 'berts_son_2' => 'Bertie')
  8161. ),
  8162. array('showParents' => true, 'empty' => false)
  8163. );
  8164. $expected = array(
  8165. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8166. array('optgroup' => array('label' => 'Fred')),
  8167. array('option' => array('value' => 'freds_son_1')),
  8168. 'Fred',
  8169. '/option',
  8170. array('option' => array('value' => 'freds_son_2')),
  8171. 'Freddie',
  8172. '/option',
  8173. '/optgroup',
  8174. array('optgroup' => array('label' => 'Bert')),
  8175. array('option' => array('value' => 'berts_son_1')),
  8176. 'Albert',
  8177. '/option',
  8178. array('option' => array('value' => 'berts_son_2')),
  8179. 'Bertie',
  8180. '/option',
  8181. '/optgroup',
  8182. '/select'
  8183. );
  8184. $this->assertTags($result, $expected);
  8185. /*
  8186. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8187. * changed, so we should expect the same select list data, just with a different name
  8188. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8189. * This is where data corruption can occur, because when a select value is missing from
  8190. * a list a form will substitute the first value in the list - without the user knowing.
  8191. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8192. * affect the availability of 3 => 'Three' as a valid option.
  8193. */
  8194. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8195. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8196. ));
  8197. $result = $this->Form->select(
  8198. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8199. );
  8200. $expected = array(
  8201. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8202. array('option' => array('value' => 1)),
  8203. 'One',
  8204. '/option',
  8205. array('option' => array('value' => 2)),
  8206. 'Two',
  8207. '/option',
  8208. array('optgroup' => array('label' => 'Three')),
  8209. array('option' => array('value' => 3)),
  8210. 'Three',
  8211. '/option',
  8212. array('option' => array('value' => 4)),
  8213. 'Four',
  8214. '/option',
  8215. array('option' => array('value' => 5)),
  8216. 'Five',
  8217. '/option',
  8218. '/optgroup',
  8219. '/select'
  8220. );
  8221. $this->assertTags($result, $expected);
  8222. }
  8223. /**
  8224. * Test the generation of fields for a multi record form.
  8225. *
  8226. * @return void
  8227. */
  8228. public function testMultiRecordForm() {
  8229. $this->Form->create('ValidateProfile');
  8230. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8231. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8232. $expected = array(
  8233. 'div' => array('class' => 'input textarea'),
  8234. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8235. 'Name',
  8236. '/label',
  8237. 'textarea' => array(
  8238. 'id' => 'ValidateProfile1ValidateItem2Name',
  8239. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8240. 'cols' => 30,
  8241. 'rows' => 6
  8242. ),
  8243. 'Value',
  8244. '/textarea',
  8245. '/div'
  8246. );
  8247. $this->assertTags($result, $expected);
  8248. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8249. $expected = array(
  8250. 'div' => array('class' => 'input date'),
  8251. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8252. 'Created',
  8253. '/label',
  8254. array('select' => array(
  8255. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8256. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8257. )
  8258. ),
  8259. array('option' => array('value' => '')), '/option',
  8260. $this->dateRegex['monthsRegex'],
  8261. '/select', '-',
  8262. array('select' => array(
  8263. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8264. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8265. )
  8266. ),
  8267. array('option' => array('value' => '')), '/option',
  8268. $this->dateRegex['daysRegex'],
  8269. '/select', '-',
  8270. array('select' => array(
  8271. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8272. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8273. )
  8274. ),
  8275. array('option' => array('value' => '')), '/option',
  8276. $this->dateRegex['yearsRegex'],
  8277. '/select',
  8278. '/div'
  8279. );
  8280. $this->assertTags($result, $expected);
  8281. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8282. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8283. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8284. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8285. $expected = array(
  8286. 'div' => array('class' => 'input select error'),
  8287. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8288. 'Profile',
  8289. '/label',
  8290. 'select' => array(
  8291. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8292. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8293. 'class' => 'form-error'
  8294. ),
  8295. '/select',
  8296. array('div' => array('class' => 'error-message')),
  8297. 'Error',
  8298. '/div',
  8299. '/div'
  8300. );
  8301. $this->assertTags($result, $expected);
  8302. }
  8303. /**
  8304. * test the correct display of multi-record form validation errors.
  8305. *
  8306. * @return void
  8307. */
  8308. public function testMultiRecordFormValidationErrors() {
  8309. $this->Form->create('ValidateProfile');
  8310. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8311. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8312. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8313. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8314. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8315. $result = $this->Form->error('ValidateProfile.2.city');
  8316. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8317. $result = $this->Form->error('2.city');
  8318. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8319. }
  8320. /**
  8321. * test the correct display of multi-record form validation errors.
  8322. *
  8323. * @return void
  8324. */
  8325. public function testSaveManyRecordFormValidationErrors() {
  8326. $this->Form->create('ValidateUser');
  8327. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8328. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8329. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8330. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8331. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8332. $result = $this->Form->error('ValidateUser.0.city');
  8333. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8334. }
  8335. /**
  8336. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8337. *
  8338. * @return void
  8339. */
  8340. public function testInputTemplate() {
  8341. $result = $this->Form->input('Contact.email', array(
  8342. 'type' => 'text', 'format' => array('input')
  8343. ));
  8344. $expected = array(
  8345. 'div' => array('class' => 'input text'),
  8346. 'input' => array(
  8347. 'type' => 'text', 'name' => 'data[Contact][email]',
  8348. 'id' => 'ContactEmail'
  8349. ),
  8350. '/div'
  8351. );
  8352. $this->assertTags($result, $expected);
  8353. $result = $this->Form->input('Contact.email', array(
  8354. 'type' => 'text', 'format' => array('input', 'label'),
  8355. 'label' => '<em>Email (required)</em>'
  8356. ));
  8357. $expected = array(
  8358. 'div' => array('class' => 'input text'),
  8359. array('input' => array(
  8360. 'type' => 'text', 'name' => 'data[Contact][email]',
  8361. 'id' => 'ContactEmail'
  8362. )),
  8363. 'label' => array('for' => 'ContactEmail'),
  8364. 'em' => array(),
  8365. 'Email (required)',
  8366. '/em',
  8367. '/label',
  8368. '/div'
  8369. );
  8370. $this->assertTags($result, $expected);
  8371. $result = $this->Form->input('Contact.email', array(
  8372. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8373. 'between' => '<div>Something in the middle</div>',
  8374. 'after' => '<span>Some text at the end</span>'
  8375. ));
  8376. $expected = array(
  8377. 'div' => array('class' => 'input text'),
  8378. array('input' => array(
  8379. 'type' => 'text', 'name' => 'data[Contact][email]',
  8380. 'id' => 'ContactEmail'
  8381. )),
  8382. array('div' => array()),
  8383. 'Something in the middle',
  8384. '/div',
  8385. 'label' => array('for' => 'ContactEmail'),
  8386. 'Email',
  8387. '/label',
  8388. 'span' => array(),
  8389. 'Some text at the end',
  8390. '/span',
  8391. '/div'
  8392. );
  8393. $this->assertTags($result, $expected);
  8394. $result = $this->Form->input('Contact.method', array(
  8395. 'type' => 'radio',
  8396. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8397. 'between' => 'I am between',
  8398. ));
  8399. $expected = array(
  8400. 'div' => array('class' => 'input radio'),
  8401. 'fieldset' => array(),
  8402. 'legend' => array(),
  8403. 'Method',
  8404. '/legend',
  8405. 'I am between',
  8406. 'input' => array(
  8407. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8408. 'value' => '', 'id' => 'ContactMethod_'
  8409. ),
  8410. array('input' => array(
  8411. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8412. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8413. )),
  8414. array('label' => array('for' => 'ContactMethodEmail')),
  8415. 'Email',
  8416. '/label',
  8417. array('input' => array(
  8418. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8419. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8420. )),
  8421. array('label' => array('for' => 'ContactMethodPigeon')),
  8422. 'Pigeon',
  8423. '/label',
  8424. '/fieldset',
  8425. '/div',
  8426. );
  8427. $this->assertTags($result, $expected);
  8428. }
  8429. /**
  8430. * test that some html5 inputs + FormHelper::__call() work
  8431. *
  8432. * @return void
  8433. */
  8434. public function testHtml5Inputs() {
  8435. $result = $this->Form->email('User.email');
  8436. $expected = array(
  8437. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8438. );
  8439. $this->assertTags($result, $expected);
  8440. $result = $this->Form->search('User.query');
  8441. $expected = array(
  8442. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8443. );
  8444. $this->assertTags($result, $expected);
  8445. $result = $this->Form->search('User.query', array('value' => 'test'));
  8446. $expected = array(
  8447. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8448. );
  8449. $this->assertTags($result, $expected);
  8450. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8451. $expected = array(
  8452. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8453. );
  8454. $this->assertTags($result, $expected);
  8455. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8456. $expected = array(
  8457. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8458. );
  8459. $this->assertTags($result, $expected);
  8460. }
  8461. /**
  8462. *
  8463. * @expectedException CakeException
  8464. * @return void
  8465. */
  8466. public function testHtml5InputException() {
  8467. $this->Form->email();
  8468. }
  8469. /**
  8470. * Tests that a model can be loaded from the model names passed in the request object
  8471. *
  8472. * @return void
  8473. */
  8474. public function testIntrospectModelFromRequest() {
  8475. $this->loadFixtures('Post');
  8476. App::build(array(
  8477. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8478. ));
  8479. CakePlugin::load('TestPlugin');
  8480. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8481. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8482. $this->Form->create('TestPluginPost');
  8483. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8484. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8485. CakePlugin::unload();
  8486. App::build();
  8487. }
  8488. /**
  8489. * Tests that it is possible to set the validation errors directly in the helper for a field
  8490. *
  8491. * @return void
  8492. */
  8493. public function testCustomValidationErrors() {
  8494. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8495. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8496. $this->assertEquals('Badness!', $result);
  8497. }
  8498. /**
  8499. * Tests that the 'on' key validates as expected on create
  8500. *
  8501. * @return void
  8502. */
  8503. public function testRequiredOnCreate() {
  8504. $this->Form->create('Contact');
  8505. $result = $this->Form->input('Contact.imrequiredonupdate');
  8506. $expected = array(
  8507. 'div' => array('class' => 'input text'),
  8508. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8509. 'Imrequiredonupdate',
  8510. '/label',
  8511. 'input' => array(
  8512. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8513. 'id' => 'ContactImrequiredonupdate'
  8514. ),
  8515. '/div'
  8516. );
  8517. $this->assertTags($result, $expected);
  8518. $result = $this->Form->input('Contact.imrequiredoncreate');
  8519. $expected = array(
  8520. 'div' => array('class' => 'input text required'),
  8521. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8522. 'Imrequiredoncreate',
  8523. '/label',
  8524. 'input' => array(
  8525. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8526. 'id' => 'ContactImrequiredoncreate',
  8527. 'required' => 'required'
  8528. ),
  8529. '/div'
  8530. );
  8531. $this->assertTags($result, $expected);
  8532. $result = $this->Form->input('Contact.imrequiredonboth');
  8533. $expected = array(
  8534. 'div' => array('class' => 'input text required'),
  8535. 'label' => array('for' => 'ContactImrequiredonboth'),
  8536. 'Imrequiredonboth',
  8537. '/label',
  8538. 'input' => array(
  8539. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8540. 'id' => 'ContactImrequiredonboth',
  8541. 'required' => 'required'
  8542. ),
  8543. '/div'
  8544. );
  8545. $this->assertTags($result, $expected);
  8546. $this->Form->inputDefaults(array('required' => false));
  8547. $result = $this->Form->input('Contact.imrequired');
  8548. $expected = array(
  8549. 'div' => array('class' => 'input text'),
  8550. 'label' => array('for' => 'ContactImrequired'),
  8551. 'Imrequired',
  8552. '/label',
  8553. 'input' => array(
  8554. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8555. 'id' => 'ContactImrequired'
  8556. ),
  8557. '/div'
  8558. );
  8559. $this->assertTags($result, $expected);
  8560. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8561. $this->assertTags($result, $expected);
  8562. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8563. $expected = array(
  8564. 'div' => array('class' => 'input text required'),
  8565. 'label' => array('for' => 'ContactImrequired'),
  8566. 'Imrequired',
  8567. '/label',
  8568. 'input' => array(
  8569. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8570. 'id' => 'ContactImrequired'
  8571. ),
  8572. '/div'
  8573. );
  8574. $this->assertTags($result, $expected);
  8575. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8576. $this->assertTags($result, $expected);
  8577. }
  8578. /**
  8579. * Tests that the 'on' key validates as expected on update
  8580. *
  8581. * @return void
  8582. */
  8583. public function testRequiredOnUpdate() {
  8584. $this->Form->request->data['Contact']['id'] = 1;
  8585. $this->Form->create('Contact');
  8586. $result = $this->Form->input('Contact.imrequiredonupdate');
  8587. $expected = array(
  8588. 'div' => array('class' => 'input text required'),
  8589. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8590. 'Imrequiredonupdate',
  8591. '/label',
  8592. 'input' => array(
  8593. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8594. 'id' => 'ContactImrequiredonupdate',
  8595. 'required' => 'required'
  8596. ),
  8597. '/div'
  8598. );
  8599. $this->assertTags($result, $expected);
  8600. $result = $this->Form->input('Contact.imrequiredoncreate');
  8601. $expected = array(
  8602. 'div' => array('class' => 'input text'),
  8603. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8604. 'Imrequiredoncreate',
  8605. '/label',
  8606. 'input' => array(
  8607. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8608. 'id' => 'ContactImrequiredoncreate'
  8609. ),
  8610. '/div'
  8611. );
  8612. $this->assertTags($result, $expected);
  8613. $result = $this->Form->input('Contact.imrequiredonboth');
  8614. $expected = array(
  8615. 'div' => array('class' => 'input text required'),
  8616. 'label' => array('for' => 'ContactImrequiredonboth'),
  8617. 'Imrequiredonboth',
  8618. '/label',
  8619. 'input' => array(
  8620. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8621. 'id' => 'ContactImrequiredonboth',
  8622. 'required' => 'required'
  8623. ),
  8624. '/div'
  8625. );
  8626. $this->assertTags($result, $expected);
  8627. $result = $this->Form->input('Contact.imrequired');
  8628. $expected = array(
  8629. 'div' => array('class' => 'input text required'),
  8630. 'label' => array('for' => 'ContactImrequired'),
  8631. 'Imrequired',
  8632. '/label',
  8633. 'input' => array(
  8634. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8635. 'id' => 'ContactImrequired',
  8636. 'required' => 'required'
  8637. ),
  8638. '/div'
  8639. );
  8640. $this->assertTags($result, $expected);
  8641. }
  8642. /**
  8643. * Test inputDefaults setter and getter
  8644. *
  8645. * @return void
  8646. */
  8647. public function testInputDefaults() {
  8648. $this->Form->create('Contact');
  8649. $this->Form->inputDefaults(array(
  8650. 'label' => false,
  8651. 'div' => array(
  8652. 'style' => 'color: #000;'
  8653. )
  8654. ));
  8655. $result = $this->Form->input('Contact.field1');
  8656. $expected = array(
  8657. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8658. 'input' => array(
  8659. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8660. 'id' => 'ContactField1'
  8661. ),
  8662. '/div'
  8663. );
  8664. $this->assertTags($result, $expected);
  8665. $this->Form->inputDefaults(array(
  8666. 'div' => false,
  8667. 'label' => 'Label',
  8668. ));
  8669. $result = $this->Form->input('Contact.field1');
  8670. $expected = array(
  8671. 'label' => array('for' => 'ContactField1'),
  8672. 'Label',
  8673. '/label',
  8674. 'input' => array(
  8675. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8676. 'id' => 'ContactField1'
  8677. ),
  8678. );
  8679. $this->assertTags($result, $expected);
  8680. $this->Form->inputDefaults(array(
  8681. 'label' => false,
  8682. ), true);
  8683. $result = $this->Form->input('Contact.field1');
  8684. $expected = array(
  8685. 'input' => array(
  8686. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8687. 'id' => 'ContactField1'
  8688. ),
  8689. );
  8690. $this->assertTags($result, $expected);
  8691. $result = $this->Form->inputDefaults();
  8692. $expected = array(
  8693. 'div' => false,
  8694. 'label' => false,
  8695. );
  8696. $this->assertEquals($expected, $result);
  8697. }
  8698. }