FormHelperTest.php 278 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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
  33. * @package Cake.Test.Case.View.Helper
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. * @package cake
  47. * @package Cake.Test.Case.View.Helper
  48. */
  49. class Contact extends CakeTestModel {
  50. /**
  51. * useTable property
  52. *
  53. * @var bool false
  54. */
  55. public $useTable = false;
  56. /**
  57. * Default schema
  58. *
  59. * @var array
  60. */
  61. protected $_schema = array(
  62. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  63. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  67. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  68. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  69. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  70. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  71. );
  72. /**
  73. * validate property
  74. *
  75. * @var array
  76. */
  77. public $validate = array(
  78. 'non_existing' => array(),
  79. 'idontexist' => array(),
  80. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  81. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  82. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  83. 'imrequiredonboth' => array(
  84. 'required' => array('rule' => 'alphaNumeric'),
  85. ),
  86. 'string_required' => 'notEmpty',
  87. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  88. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  89. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  90. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'imalsonotrequired' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30)),
  94. ),
  95. 'imalsonotrequired2' => array(
  96. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  97. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. ),
  99. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array('User' => array('className' => 'UserForm'));
  127. }
  128. /**
  129. * ContactTagsContact class
  130. *
  131. * @package cake
  132. * @package Cake.Test.Case.View.Helper
  133. */
  134. class ContactTagsContact extends CakeTestModel {
  135. /**
  136. * useTable property
  137. *
  138. * @var bool false
  139. */
  140. public $useTable = false;
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. * @package cake
  165. * @package Cake.Test.Case.View.Helper
  166. */
  167. class ContactNonStandardPk extends Contact {
  168. /**
  169. * primaryKey property
  170. *
  171. * @var string 'pk'
  172. */
  173. public $primaryKey = 'pk';
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function schema($field = false) {
  180. $this->_schema = parent::schema();
  181. $this->_schema['pk'] = $this->_schema['id'];
  182. unset($this->_schema['id']);
  183. return $this->_schema;
  184. }
  185. }
  186. /**
  187. * ContactTag class
  188. *
  189. * @package cake
  190. * @package Cake.Test.Case.View.Helper
  191. */
  192. class ContactTag extends Model {
  193. /**
  194. * useTable property
  195. *
  196. * @var bool false
  197. */
  198. public $useTable = false;
  199. /**
  200. * schema definition
  201. *
  202. * @var array
  203. */
  204. protected $_schema = array(
  205. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  206. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  207. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  208. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  209. );
  210. }
  211. /**
  212. * UserForm class
  213. *
  214. * @package cake
  215. * @package Cake.Test.Case.View.Helper
  216. */
  217. class UserForm extends CakeTestModel {
  218. /**
  219. * useTable property
  220. *
  221. * @var bool false
  222. */
  223. public $useTable = false;
  224. /**
  225. * hasMany property
  226. *
  227. * @var array
  228. */
  229. public $hasMany = array(
  230. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  231. ));
  232. /**
  233. * schema definition
  234. *
  235. * @var array
  236. */
  237. protected $_schema = array(
  238. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  239. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  240. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  241. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  242. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  243. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  244. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  245. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  246. );
  247. }
  248. /**
  249. * OpenidUrl class
  250. *
  251. * @package cake
  252. * @package Cake.Test.Case.View.Helper
  253. */
  254. class OpenidUrl extends CakeTestModel {
  255. /**
  256. * useTable property
  257. *
  258. * @var bool false
  259. */
  260. public $useTable = false;
  261. /**
  262. * belongsTo property
  263. *
  264. * @var array
  265. */
  266. public $belongsTo = array('UserForm' => array(
  267. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  268. ));
  269. /**
  270. * validate property
  271. *
  272. * @var array
  273. */
  274. public $validate = array('openid_not_registered' => array());
  275. /**
  276. * schema method
  277. *
  278. * @var array
  279. */
  280. protected $_schema = array(
  281. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  282. 'user_form_id' => array(
  283. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  284. ),
  285. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. );
  287. /**
  288. * beforeValidate method
  289. *
  290. * @return void
  291. */
  292. public function beforeValidate($options = array()) {
  293. $this->invalidate('openid_not_registered');
  294. return true;
  295. }
  296. }
  297. /**
  298. * ValidateUser class
  299. *
  300. * @package cake
  301. * @package Cake.Test.Case.View.Helper
  302. */
  303. class ValidateUser extends CakeTestModel {
  304. /**
  305. * useTable property
  306. *
  307. * @var bool false
  308. */
  309. public $useTable = false;
  310. /**
  311. * hasOne property
  312. *
  313. * @var array
  314. */
  315. public $hasOne = array('ValidateProfile' => array(
  316. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  317. ));
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  327. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  328. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  329. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('email');
  338. return false;
  339. }
  340. }
  341. /**
  342. * ValidateProfile class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateProfile extends CakeTestModel {
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * schema property
  356. *
  357. * @var array
  358. */
  359. protected $_schema = array(
  360. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  362. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  364. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  365. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  366. );
  367. /**
  368. * hasOne property
  369. *
  370. * @var array
  371. */
  372. public $hasOne = array('ValidateItem' => array(
  373. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  374. ));
  375. /**
  376. * belongsTo property
  377. *
  378. * @var array
  379. */
  380. public $belongsTo = array('ValidateUser' => array(
  381. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  382. ));
  383. /**
  384. * beforeValidate method
  385. *
  386. * @return void
  387. */
  388. public function beforeValidate($options = array()) {
  389. $this->invalidate('full_name');
  390. $this->invalidate('city');
  391. return false;
  392. }
  393. }
  394. /**
  395. * ValidateItem class
  396. *
  397. * @package cake
  398. * @package Cake.Test.Case.View.Helper
  399. */
  400. class ValidateItem extends CakeTestModel {
  401. /**
  402. * useTable property
  403. *
  404. * @var bool false
  405. */
  406. public $useTable = false;
  407. /**
  408. * schema property
  409. *
  410. * @var array
  411. */
  412. protected $_schema = array(
  413. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  415. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  416. 'description' => array(
  417. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  418. ),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * belongsTo property
  424. *
  425. * @var array
  426. */
  427. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  428. /**
  429. * beforeValidate method
  430. *
  431. * @return void
  432. */
  433. public function beforeValidate($options = array()) {
  434. $this->invalidate('description');
  435. return false;
  436. }
  437. }
  438. /**
  439. * TestMail class
  440. *
  441. * @package cake
  442. * @package Cake.Test.Case.View.Helper
  443. */
  444. class TestMail extends CakeTestModel {
  445. /**
  446. * useTable property
  447. *
  448. * @var bool false
  449. */
  450. public $useTable = false;
  451. }
  452. /**
  453. * FormHelperTest class
  454. *
  455. * @package cake
  456. * @subpackage Cake.Test.Case.View.Helper
  457. * @property FormHelper $Form
  458. */
  459. class FormHelperTest extends CakeTestCase {
  460. /**
  461. * Fixtures to be used
  462. *
  463. * @var array
  464. */
  465. public $fixtures = array('core.post');
  466. /**
  467. * Do not load the fixtures by default
  468. *
  469. * @var boolean
  470. */
  471. public $autoFixtures = false;
  472. /**
  473. * setUp method
  474. *
  475. * @return void
  476. */
  477. public function setUp() {
  478. parent::setUp();
  479. Configure::write('App.base', '');
  480. $this->Controller = new ContactTestController();
  481. $this->View = new View($this->Controller);
  482. $this->Form = new FormHelper($this->View);
  483. $this->Form->Html = new HtmlHelper($this->View);
  484. $this->Form->request = new CakeRequest('contacts/add', false);
  485. $this->Form->request->here = '/contacts/add';
  486. $this->Form->request['action'] = 'add';
  487. $this->Form->request->webroot = '';
  488. $this->Form->request->base = '';
  489. ClassRegistry::addObject('Contact', new Contact());
  490. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  491. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  492. ClassRegistry::addObject('User', new UserForm());
  493. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  494. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  495. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  496. $this->oldSalt = Configure::read('Security.salt');
  497. $this->dateRegex = array(
  498. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  499. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  500. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  503. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  504. );
  505. Configure::write('Security.salt', 'foo!');
  506. }
  507. /**
  508. * tearDown method
  509. *
  510. * @return void
  511. */
  512. public function tearDown() {
  513. parent::tearDown();
  514. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  515. Configure::write('Security.salt', $this->oldSalt);
  516. }
  517. /**
  518. * testFormCreateWithSecurity method
  519. *
  520. * Test form->create() with security key.
  521. *
  522. * @return void
  523. */
  524. public function testCreateWithSecurity() {
  525. $this->Form->request['_Token'] = array('key' => 'testKey');
  526. $encoding = strtolower(Configure::read('App.encoding'));
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  528. $expected = array(
  529. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  530. 'div' => array('style' => 'display:none;'),
  531. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  532. array('input' => array(
  533. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  534. )),
  535. '/div'
  536. );
  537. $this->assertTags($result, $expected);
  538. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  539. $expected['form']['id'] = 'MyForm';
  540. $this->assertTags($result, $expected);
  541. }
  542. /**
  543. * testFormCreateGetNoSecurity method
  544. *
  545. * Test form->create() with no security key as its a get form
  546. *
  547. * @return void
  548. */
  549. public function testCreateEndGetNoSecurity() {
  550. $this->Form->request['_Token'] = array('key' => 'testKey');
  551. $encoding = strtolower(Configure::read('App.encoding'));
  552. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  553. $this->assertNotContains('Token', $result);
  554. $result = $this->Form->end('Save');
  555. $this->assertNotContains('Token', $result);
  556. }
  557. /**
  558. * test that create() clears the fields property so it starts fresh
  559. *
  560. * @return void
  561. */
  562. public function testCreateClearingFields() {
  563. $this->Form->fields = array('model_id');
  564. $this->Form->create('Contact');
  565. $this->assertEquals(array(), $this->Form->fields);
  566. }
  567. /**
  568. * Tests form hash generation with model-less data
  569. *
  570. * @return void
  571. */
  572. public function testValidateHashNoModel() {
  573. $this->Form->request['_Token'] = array('key' => 'foo');
  574. $result = $this->Form->secure(array('anything'));
  575. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  576. }
  577. /**
  578. * Tests that models with identical field names get resolved properly
  579. *
  580. * @return void
  581. */
  582. public function testDuplicateFieldNameResolution() {
  583. $result = $this->Form->create('ValidateUser');
  584. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  585. $result = $this->Form->input('ValidateItem.name');
  586. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  587. $result = $this->Form->input('ValidateUser.name');
  588. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  589. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  590. $this->assertRegExp('/type="text"/', $result);
  591. $result = $this->Form->input('ValidateItem.name');
  592. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  593. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  594. $this->assertRegExp('/<textarea/', $result);
  595. $result = $this->Form->input('name');
  596. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  597. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  598. $this->assertRegExp('/type="text"/', $result);
  599. }
  600. /**
  601. * Tests that hidden fields generated for checkboxes don't get locked
  602. *
  603. * @return void
  604. */
  605. public function testNoCheckboxLocking() {
  606. $this->Form->request['_Token'] = array('key' => 'foo');
  607. $this->assertSame(array(), $this->Form->fields);
  608. $this->Form->checkbox('check', array('value' => '1'));
  609. $this->assertSame($this->Form->fields, array('check'));
  610. }
  611. /**
  612. * testFormSecurityFields method
  613. *
  614. * Test generation of secure form hash generation.
  615. *
  616. * @return void
  617. */
  618. public function testFormSecurityFields() {
  619. $key = 'testKey';
  620. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  621. $this->Form->request['_Token'] = array('key' => $key);
  622. $result = $this->Form->secure($fields);
  623. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  624. $hash .= ':' . 'Model.valid';
  625. $hash = urlencode($hash);
  626. $expected = array(
  627. 'div' => array('style' => 'display:none;'),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  630. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  631. )),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  634. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  635. )),
  636. '/div'
  637. );
  638. $this->assertTags($result, $expected);
  639. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  640. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  641. $result = $this->Form->secure($fields);
  642. $expected = array(
  643. 'div' => array('class' => 'hidden'),
  644. array('input' => array(
  645. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  646. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  647. )),
  648. array('input' => array(
  649. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  650. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  651. )),
  652. '/div'
  653. );
  654. $this->assertTags($result, $expected);
  655. }
  656. /**
  657. * Tests correct generation of number fields for double and float fields
  658. *
  659. * @return void
  660. */
  661. public function testTextFieldGenerationForFloats() {
  662. $model = ClassRegistry::getObject('Contact');
  663. $model->setSchema(array('foo' => array(
  664. 'type' => 'float',
  665. 'null' => false,
  666. 'default' => null,
  667. 'length' => null
  668. )));
  669. $this->Form->create('Contact');
  670. $result = $this->Form->input('foo');
  671. $expected = array(
  672. 'div' => array('class' => 'input number'),
  673. 'label' => array('for' => 'ContactFoo'),
  674. 'Foo',
  675. '/label',
  676. array('input' => array(
  677. 'type' => 'number',
  678. 'name' => 'data[Contact][foo]',
  679. 'id' => 'ContactFoo',
  680. 'step' => 'any'
  681. )),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Form->input('foo', array('step' => 0.5));
  686. $expected = array(
  687. 'div' => array('class' => 'input number'),
  688. 'label' => array('for' => 'ContactFoo'),
  689. 'Foo',
  690. '/label',
  691. array('input' => array(
  692. 'type' => 'number',
  693. 'name' => 'data[Contact][foo]',
  694. 'id' => 'ContactFoo',
  695. 'step' => '0.5'
  696. )),
  697. '/div'
  698. );
  699. $this->assertTags($result, $expected);
  700. }
  701. /**
  702. * Tests correct generation of number fields for integer fields
  703. *
  704. * @access public
  705. * @return void
  706. */
  707. public function testTextFieldTypeNumberGenerationForIntegers() {
  708. $model = ClassRegistry::getObject('Contact');
  709. $model->setSchema(array('foo' => array(
  710. 'type' => 'integer',
  711. 'null' => false,
  712. 'default' => null,
  713. 'length' => null
  714. )));
  715. $this->Form->create('Contact');
  716. $result = $this->Form->input('foo');
  717. $expected = array(
  718. 'div' => array('class' => 'input number'),
  719. 'label' => array('for' => 'ContactFoo'),
  720. 'Foo',
  721. '/label',
  722. array('input' => array(
  723. 'type' => 'number', 'name' => 'data[Contact][foo]',
  724. 'id' => 'ContactFoo'
  725. )),
  726. '/div'
  727. );
  728. $this->assertTags($result, $expected);
  729. }
  730. /**
  731. * testFormSecurityMultipleFields method
  732. *
  733. * Test secure() with multiple row form. Ensure hash is correct.
  734. *
  735. * @return void
  736. */
  737. public function testFormSecurityMultipleFields() {
  738. $key = 'testKey';
  739. $fields = array(
  740. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  741. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  742. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  743. );
  744. $this->Form->request['_Token'] = array('key' => $key);
  745. $result = $this->Form->secure($fields);
  746. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  747. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  748. $expected = array(
  749. 'div' => array('style' => 'display:none;'),
  750. array('input' => array(
  751. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  752. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  753. )),
  754. array('input' => array(
  755. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  756. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  757. )),
  758. '/div'
  759. );
  760. $this->assertTags($result, $expected);
  761. }
  762. /**
  763. * testFormSecurityMultipleSubmitButtons
  764. *
  765. * test form submit generation and ensure that _Token is only created on end()
  766. *
  767. * @return void
  768. */
  769. public function testFormSecurityMultipleSubmitButtons() {
  770. $key = 'testKey';
  771. $this->Form->request['_Token'] = array('key' => $key);
  772. $this->Form->create('Addresses');
  773. $this->Form->input('Address.title');
  774. $this->Form->input('Address.first_name');
  775. $result = $this->Form->submit('Save', array('name' => 'save'));
  776. $expected = array(
  777. 'div' => array('class' => 'submit'),
  778. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  779. '/div',
  780. );
  781. $this->assertTags($result, $expected);
  782. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  783. $expected = array(
  784. 'div' => array('class' => 'submit'),
  785. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  786. '/div',
  787. );
  788. $this->assertTags($result, $expected);
  789. $result = $this->Form->end(null);
  790. $expected = array(
  791. 'div' => array('style' => 'display:none;'),
  792. array('input' => array(
  793. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  794. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  795. )),
  796. array('input' => array(
  797. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  798. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  799. )),
  800. '/div'
  801. );
  802. $this->assertTags($result, $expected);
  803. }
  804. /**
  805. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecurityButtonNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitNestedNamed() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  827. $result = $this->Form->unlockField();
  828. $this->assertEquals(array('Address.button'), $result);
  829. }
  830. /**
  831. * Test that the correct fields are unlocked for image submits with no names.
  832. *
  833. * @return void
  834. */
  835. public function testSecuritySubmitImageNoName() {
  836. $key = 'testKey';
  837. $this->Form->request['_Token'] = array('key' => $key);
  838. $this->Form->create('User');
  839. $result = $this->Form->submit('save.png');
  840. $expected = array(
  841. 'div' => array('class' => 'submit'),
  842. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png', array('name' => 'test'));
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * testFormSecurityMultipleInputFields method
  868. *
  869. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  870. *
  871. * @return void
  872. */
  873. public function testFormSecurityMultipleInputFields() {
  874. $key = 'testKey';
  875. $this->Form->request['_Token'] = array('key' => $key);
  876. $this->Form->create('Addresses');
  877. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  878. $this->Form->input('Addresses.0.title');
  879. $this->Form->input('Addresses.0.first_name');
  880. $this->Form->input('Addresses.0.last_name');
  881. $this->Form->input('Addresses.0.address');
  882. $this->Form->input('Addresses.0.city');
  883. $this->Form->input('Addresses.0.phone');
  884. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  885. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  886. $this->Form->input('Addresses.1.title');
  887. $this->Form->input('Addresses.1.first_name');
  888. $this->Form->input('Addresses.1.last_name');
  889. $this->Form->input('Addresses.1.address');
  890. $this->Form->input('Addresses.1.city');
  891. $this->Form->input('Addresses.1.phone');
  892. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  893. $result = $this->Form->secure($this->Form->fields);
  894. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  895. $expected = array(
  896. 'div' => array('style' => 'display:none;'),
  897. array('input' => array(
  898. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  899. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  900. )),
  901. array('input' => array(
  902. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  903. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  904. )),
  905. '/div'
  906. );
  907. $this->assertTags($result, $expected);
  908. }
  909. /**
  910. * Test form security with Model.field.0 style inputs
  911. *
  912. * @return void
  913. */
  914. public function testFormSecurityArrayFields() {
  915. $key = 'testKey';
  916. $this->Form->request->params['_Token']['key'] = $key;
  917. $this->Form->create('Address');
  918. $this->Form->input('Address.primary.1');
  919. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  920. $this->Form->input('Address.secondary.1.0');
  921. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  922. }
  923. /**
  924. * testFormSecurityMultipleInputDisabledFields method
  925. *
  926. * test secure form generation with multiple records and disabled fields.
  927. *
  928. * @return void
  929. */
  930. public function testFormSecurityMultipleInputDisabledFields() {
  931. $key = 'testKey';
  932. $this->Form->request->params['_Token'] = array(
  933. 'key' => $key,
  934. 'unlockedFields' => array('first_name', 'address')
  935. );
  936. $this->Form->create();
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $result = $this->Form->secure($this->Form->fields);
  952. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  953. $expected = array(
  954. 'div' => array('style' => 'display:none;'),
  955. array('input' => array(
  956. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  957. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  958. )),
  959. array('input' => array(
  960. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  961. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  962. )),
  963. '/div'
  964. );
  965. $this->assertTags($result, $expected);
  966. }
  967. /**
  968. * testFormSecurityInputDisabledFields method
  969. *
  970. * Test single record form with disabled fields.
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityInputUnlockedFields() {
  975. $key = 'testKey';
  976. $this->Form->request['_Token'] = array(
  977. 'key' => $key,
  978. 'unlockedFields' => array('first_name', 'address')
  979. );
  980. $this->Form->create();
  981. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  982. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  983. $this->Form->input('Addresses.title');
  984. $this->Form->input('Addresses.first_name');
  985. $this->Form->input('Addresses.last_name');
  986. $this->Form->input('Addresses.address');
  987. $this->Form->input('Addresses.city');
  988. $this->Form->input('Addresses.phone');
  989. $result = $this->Form->fields;
  990. $expected = array(
  991. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  992. 'Addresses.city', 'Addresses.phone'
  993. );
  994. $this->assertEquals($expected, $result);
  995. $result = $this->Form->secure($expected);
  996. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  997. $expected = array(
  998. 'div' => array('style' => 'display:none;'),
  999. array('input' => array(
  1000. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1001. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1002. )),
  1003. array('input' => array(
  1004. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1005. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1006. )),
  1007. '/div'
  1008. );
  1009. $this->assertTags($result, $expected);
  1010. }
  1011. /**
  1012. * test securing inputs with custom name attributes.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testFormSecureWithCustomNameAttribute() {
  1017. $this->Form->request->params['_Token']['key'] = 'testKey';
  1018. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1019. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1020. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1021. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1022. }
  1023. /**
  1024. * testFormSecuredInput method
  1025. *
  1026. * Test generation of entire secure form, assertions made on input() output.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testFormSecuredInput() {
  1031. $this->Form->request['_Token'] = array('key' => 'testKey');
  1032. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1033. $encoding = strtolower(Configure::read('App.encoding'));
  1034. $expected = array(
  1035. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1036. 'div' => array('style' => 'display:none;'),
  1037. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1038. array('input' => array(
  1039. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1040. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormPublished'),
  1049. 'Published',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1053. 'id' => 'UserFormPublished'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1059. $expected = array(
  1060. 'div' => array('class' => 'input text'),
  1061. 'label' => array('for' => 'UserFormOther'),
  1062. 'Other',
  1063. '/label',
  1064. array('input' => array(
  1065. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1066. 'id' => 'UserFormOther'
  1067. )),
  1068. '/div'
  1069. );
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->hidden('UserForm.stuff');
  1072. $expected = array('input' => array(
  1073. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1074. 'id' => 'UserFormStuff'
  1075. ));
  1076. $this->assertTags($result, $expected);
  1077. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1078. $expected = array('input' => array(
  1079. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1080. 'value' => '0', 'id' => 'UserFormHidden'
  1081. ));
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1084. $expected = array(
  1085. 'div' => array('class' => 'input checkbox'),
  1086. array('input' => array(
  1087. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1088. 'value' => '0', 'id' => 'UserFormSomething_'
  1089. )),
  1090. array('input' => array(
  1091. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1092. 'value' => '1', 'id' => 'UserFormSomething'
  1093. )),
  1094. 'label' => array('for' => 'UserFormSomething'),
  1095. 'Something',
  1096. '/label',
  1097. '/div'
  1098. );
  1099. $this->assertTags($result, $expected);
  1100. $result = $this->Form->fields;
  1101. $expected = array(
  1102. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1103. 'UserForm.hidden' => '0', 'UserForm.something'
  1104. );
  1105. $this->assertEquals($expected, $result);
  1106. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1107. $result = $this->Form->secure($this->Form->fields);
  1108. $expected = array(
  1109. 'div' => array('style' => 'display:none;'),
  1110. array('input' => array(
  1111. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1112. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1113. )),
  1114. array('input' => array(
  1115. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1116. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1117. )),
  1118. '/div'
  1119. );
  1120. $this->assertTags($result, $expected);
  1121. }
  1122. /**
  1123. * Tests that the correct keys are added to the field hash index
  1124. *
  1125. * @return void
  1126. */
  1127. public function testFormSecuredFileInput() {
  1128. $this->Form->request['_Token'] = array('key' => 'testKey');
  1129. $this->assertEquals(array(), $this->Form->fields);
  1130. $this->Form->file('Attachment.file');
  1131. $expected = array(
  1132. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1133. 'Attachment.file.error', 'Attachment.file.size'
  1134. );
  1135. $this->assertEquals($expected, $this->Form->fields);
  1136. }
  1137. /**
  1138. * test that multiple selects keys are added to field hash
  1139. *
  1140. * @return void
  1141. */
  1142. public function testFormSecuredMultipleSelect() {
  1143. $this->Form->request['_Token'] = array('key' => 'testKey');
  1144. $this->assertEquals(array(), $this->Form->fields);
  1145. $options = array('1' => 'one', '2' => 'two');
  1146. $this->Form->select('Model.select', $options);
  1147. $expected = array('Model.select');
  1148. $this->assertEquals($expected, $this->Form->fields);
  1149. $this->Form->fields = array();
  1150. $this->Form->select('Model.select', $options, array('multiple' => true));
  1151. $this->assertEquals($expected, $this->Form->fields);
  1152. }
  1153. /**
  1154. * testFormSecuredRadio method
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFormSecuredRadio() {
  1159. $this->Form->request['_Token'] = array('key' => 'testKey');
  1160. $this->assertEquals(array(), $this->Form->fields);
  1161. $options = array('1' => 'option1', '2' => 'option2');
  1162. $this->Form->radio('Test.test', $options);
  1163. $expected = array('Test.test');
  1164. $this->assertEquals($expected, $this->Form->fields);
  1165. }
  1166. /**
  1167. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1168. * hashing.
  1169. *
  1170. * @return void
  1171. */
  1172. public function testFormSecuredAndDisabled() {
  1173. $this->Form->request['_Token'] = array('key' => 'testKey');
  1174. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1175. $this->Form->text('Model.text', array('disabled' => true));
  1176. $this->Form->password('Model.text', array('disabled' => true));
  1177. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1178. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1179. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1180. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1181. $this->Form->month('Model.month', array('disabled' => true));
  1182. $this->Form->day('Model.day', array('disabled' => true));
  1183. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1184. $this->Form->minute('Model.minute', array('disabled' => true));
  1185. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1186. $expected = array(
  1187. 'Model.radio' => ''
  1188. );
  1189. $this->assertEquals($expected, $this->Form->fields);
  1190. }
  1191. /**
  1192. * testDisableSecurityUsingForm method
  1193. *
  1194. * @return void
  1195. */
  1196. public function testDisableSecurityUsingForm() {
  1197. $this->Form->request['_Token'] = array(
  1198. 'key' => 'testKey',
  1199. 'disabledFields' => array()
  1200. );
  1201. $this->Form->create();
  1202. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1203. $this->Form->input('Addresses.title');
  1204. $this->Form->input('Addresses.first_name', array('secure' => false));
  1205. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1206. $this->Form->input('Addresses.zip', array(
  1207. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1208. ));
  1209. $result = $this->Form->fields;
  1210. $expected = array(
  1211. 'Addresses.id' => '123456', 'Addresses.title',
  1212. );
  1213. $this->assertEquals($expected, $result);
  1214. }
  1215. /**
  1216. * test disableField
  1217. *
  1218. * @return void
  1219. */
  1220. public function testUnlockFieldAddsToList() {
  1221. $this->Form->request['_Token'] = array(
  1222. 'key' => 'testKey',
  1223. 'unlockedFields' => array()
  1224. );
  1225. $this->Form->create('Contact');
  1226. $this->Form->unlockField('Contact.name');
  1227. $this->Form->text('Contact.name');
  1228. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. }
  1231. /**
  1232. * test unlockField removing from fields array.
  1233. *
  1234. * @return void
  1235. */
  1236. public function testUnlockFieldRemovingFromFields() {
  1237. $this->Form->request['_Token'] = array(
  1238. 'key' => 'testKey',
  1239. 'unlockedFields' => array()
  1240. );
  1241. $this->Form->create('Contact');
  1242. $this->Form->hidden('Contact.id', array('value' => 1));
  1243. $this->Form->text('Contact.name');
  1244. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1245. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1246. $this->Form->unlockField('Contact.name');
  1247. $this->Form->unlockField('Contact.id');
  1248. $this->assertEquals(array(), $this->Form->fields);
  1249. }
  1250. /**
  1251. * testTagIsInvalid method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testTagIsInvalid() {
  1256. $Contact = ClassRegistry::getObject('Contact');
  1257. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1258. $this->Form->setEntity('Contact.0.email');
  1259. $result = $this->Form->tagIsInvalid();
  1260. $expected = array('Please provide an email');
  1261. $this->assertEquals($expected, $result);
  1262. $this->Form->setEntity('Contact.1.email');
  1263. $result = $this->Form->tagIsInvalid();
  1264. $expected = false;
  1265. $this->assertSame($expected, $result);
  1266. $this->Form->setEntity('Contact.0.name');
  1267. $result = $this->Form->tagIsInvalid();
  1268. $expected = false;
  1269. $this->assertSame($expected, $result);
  1270. }
  1271. /**
  1272. * Test validation errors.
  1273. *
  1274. * @return void
  1275. */
  1276. public function testPasswordValidation() {
  1277. $Contact = ClassRegistry::getObject('Contact');
  1278. $Contact->validationErrors['password'] = array('Please provide a password');
  1279. $result = $this->Form->input('Contact.password');
  1280. $expected = array(
  1281. 'div' => array('class' => 'input password error'),
  1282. 'label' => array('for' => 'ContactPassword'),
  1283. 'Password',
  1284. '/label',
  1285. 'input' => array(
  1286. 'type' => 'password', 'name' => 'data[Contact][password]',
  1287. 'id' => 'ContactPassword', 'class' => 'form-error'
  1288. ),
  1289. array('div' => array('class' => 'error-message')),
  1290. 'Please provide a password',
  1291. '/div',
  1292. '/div'
  1293. );
  1294. $this->assertTags($result, $expected);
  1295. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1296. $expected = array(
  1297. 'div' => array('class' => 'input password error'),
  1298. 'label' => array('for' => 'ContactPassword'),
  1299. 'Password',
  1300. '/label',
  1301. 'input' => array(
  1302. 'type' => 'password', 'name' => 'data[Contact][password]',
  1303. 'id' => 'ContactPassword', 'class' => 'form-error'
  1304. ),
  1305. '/div'
  1306. );
  1307. $this->assertTags($result, $expected);
  1308. }
  1309. /**
  1310. * Test validation errors, when validation message is an empty string.
  1311. *
  1312. * @access public
  1313. * @return void
  1314. */
  1315. public function testEmptyErrorValidation() {
  1316. $this->Form->validationErrors['Contact']['password'] = '';
  1317. $result = $this->Form->input('Contact.password');
  1318. $expected = array(
  1319. 'div' => array('class' => 'input password error'),
  1320. 'label' => array('for' => 'ContactPassword'),
  1321. 'Password',
  1322. '/label',
  1323. 'input' => array(
  1324. 'type' => 'password', 'name' => 'data[Contact][password]',
  1325. 'id' => 'ContactPassword', 'class' => 'form-error'
  1326. ),
  1327. array('div' => array('class' => 'error-message')),
  1328. array(),
  1329. '/div',
  1330. '/div'
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1334. $expected = array(
  1335. 'div' => array('class' => 'input password error'),
  1336. 'label' => array('for' => 'ContactPassword'),
  1337. 'Password',
  1338. '/label',
  1339. 'input' => array(
  1340. 'type' => 'password', 'name' => 'data[Contact][password]',
  1341. 'id' => 'ContactPassword', 'class' => 'form-error'
  1342. ),
  1343. '/div'
  1344. );
  1345. $this->assertTags($result, $expected);
  1346. }
  1347. /**
  1348. * Test validation errors, when calling input() overriding validation message by an empty string.
  1349. *
  1350. * @access public
  1351. * @return void
  1352. */
  1353. public function testEmptyInputErrorValidation() {
  1354. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1355. $result = $this->Form->input('Contact.password', array('error' => ''));
  1356. $expected = array(
  1357. 'div' => array('class' => 'input password error'),
  1358. 'label' => array('for' => 'ContactPassword'),
  1359. 'Password',
  1360. '/label',
  1361. 'input' => array(
  1362. 'type' => 'password', 'name' => 'data[Contact][password]',
  1363. 'id' => 'ContactPassword', 'class' => 'form-error'
  1364. ),
  1365. array('div' => array('class' => 'error-message')),
  1366. array(),
  1367. '/div',
  1368. '/div'
  1369. );
  1370. $this->assertTags($result, $expected);
  1371. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1372. $expected = array(
  1373. 'div' => array('class' => 'input password error'),
  1374. 'label' => array('for' => 'ContactPassword'),
  1375. 'Password',
  1376. '/label',
  1377. 'input' => array(
  1378. 'type' => 'password', 'name' => 'data[Contact][password]',
  1379. 'id' => 'ContactPassword', 'class' => 'form-error'
  1380. ),
  1381. '/div'
  1382. );
  1383. $this->assertTags($result, $expected);
  1384. }
  1385. /**
  1386. * testFormValidationAssociated method
  1387. *
  1388. * test display of form errors in conjunction with model::validates.
  1389. *
  1390. * @return void
  1391. */
  1392. public function testFormValidationAssociated() {
  1393. $this->UserForm = ClassRegistry::getObject('UserForm');
  1394. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1395. $data = array(
  1396. 'UserForm' => array('name' => 'user'),
  1397. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1398. );
  1399. $result = $this->UserForm->OpenidUrl->create($data);
  1400. $this->assertFalse(empty($result));
  1401. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1402. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1403. $encoding = strtolower(Configure::read('App.encoding'));
  1404. $expected = array(
  1405. 'form' => array(
  1406. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1407. 'accept-charset' => $encoding
  1408. ),
  1409. 'div' => array('style' => 'display:none;'),
  1410. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1411. '/div'
  1412. );
  1413. $this->assertTags($result, $expected);
  1414. $result = $this->Form->error(
  1415. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1416. );
  1417. $this->assertEquals('Error, not registered', $result);
  1418. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1419. }
  1420. /**
  1421. * testFormValidationAssociatedFirstLevel method
  1422. *
  1423. * test form error display with associated model.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociatedFirstLevel() {
  1428. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1429. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1430. $data = array(
  1431. 'ValidateUser' => array('name' => 'mariano'),
  1432. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1433. );
  1434. $result = $this->ValidateUser->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->ValidateUser->validates());
  1437. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1438. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1439. $encoding = strtolower(Configure::read('App.encoding'));
  1440. $expected = array(
  1441. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1442. 'div' => array('style' => 'display:none;'),
  1443. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1444. '/div'
  1445. );
  1446. $this->assertTags($result, $expected);
  1447. $result = $this->Form->error(
  1448. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1449. );
  1450. $this->assertEquals('Invalid email', $result);
  1451. $result = $this->Form->error(
  1452. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1453. );
  1454. $this->assertEquals('Invalid name', $result);
  1455. $result = $this->Form->error(
  1456. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1457. );
  1458. $this->assertEquals('Invalid city', $result);
  1459. unset($this->ValidateUser->ValidateProfile);
  1460. unset($this->ValidateUser);
  1461. }
  1462. /**
  1463. * testFormValidationAssociatedSecondLevel method
  1464. *
  1465. * test form error display with associated model.
  1466. *
  1467. * @return void
  1468. */
  1469. public function testFormValidationAssociatedSecondLevel() {
  1470. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1471. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1472. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1473. $data = array(
  1474. 'ValidateUser' => array('name' => 'mariano'),
  1475. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1476. 'ValidateItem' => array('name' => 'Item')
  1477. );
  1478. $result = $this->ValidateUser->create($data);
  1479. $this->assertFalse(empty($result));
  1480. $this->assertFalse($this->ValidateUser->validates());
  1481. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1482. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1483. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1484. $encoding = strtolower(Configure::read('App.encoding'));
  1485. $expected = array(
  1486. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1487. 'div' => array('style' => 'display:none;'),
  1488. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1489. '/div'
  1490. );
  1491. $this->assertTags($result, $expected);
  1492. $result = $this->Form->error(
  1493. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1494. );
  1495. $this->assertEquals('Invalid email', $result);
  1496. $result = $this->Form->error(
  1497. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1498. );
  1499. $this->assertEquals('Invalid name', $result);
  1500. $result = $this->Form->error(
  1501. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1502. );
  1503. $result = $this->Form->error(
  1504. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid description', $result);
  1507. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1508. unset($this->ValidateUser->ValidateProfile);
  1509. unset($this->ValidateUser);
  1510. }
  1511. /**
  1512. * testFormValidationMultiRecord method
  1513. *
  1514. * test form error display with multiple records.
  1515. *
  1516. * @return void
  1517. */
  1518. public function testFormValidationMultiRecord() {
  1519. $Contact = ClassRegistry::getObject('Contact');
  1520. $Contact->validationErrors[2] = array(
  1521. 'name' => array('This field cannot be left blank')
  1522. );
  1523. $result = $this->Form->input('Contact.2.name');
  1524. $expected = array(
  1525. 'div' => array('class' => 'input text error'),
  1526. 'label' => array('for' => 'Contact2Name'),
  1527. 'Name',
  1528. '/label',
  1529. 'input' => array(
  1530. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1531. 'class' => 'form-error', 'maxlength' => 255
  1532. ),
  1533. array('div' => array('class' => 'error-message')),
  1534. 'This field cannot be left blank',
  1535. '/div',
  1536. '/div'
  1537. );
  1538. $this->assertTags($result, $expected);
  1539. }
  1540. /**
  1541. * testMultipleInputValidation method
  1542. *
  1543. * test multiple record form validation error display.
  1544. *
  1545. * @return void
  1546. */
  1547. public function testMultipleInputValidation() {
  1548. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1549. $Address->validationErrors[0] = array(
  1550. 'title' => array('This field cannot be empty'),
  1551. 'first_name' => array('This field cannot be empty')
  1552. );
  1553. $Address->validationErrors[1] = array(
  1554. 'last_name' => array('You must have a last name')
  1555. );
  1556. $this->Form->create();
  1557. $result = $this->Form->input('Address.0.title');
  1558. $expected = array(
  1559. 'div' => array('class'),
  1560. 'label' => array('for'),
  1561. 'preg:/[^<]+/',
  1562. '/label',
  1563. 'input' => array(
  1564. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1565. ),
  1566. array('div' => array('class' => 'error-message')),
  1567. 'This field cannot be empty',
  1568. '/div',
  1569. '/div'
  1570. );
  1571. $this->assertTags($result, $expected);
  1572. $result = $this->Form->input('Address.0.first_name');
  1573. $expected = array(
  1574. 'div' => array('class'),
  1575. 'label' => array('for'),
  1576. 'preg:/[^<]+/',
  1577. '/label',
  1578. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1579. array('div' => array('class' => 'error-message')),
  1580. 'This field cannot be empty',
  1581. '/div',
  1582. '/div'
  1583. );
  1584. $this->assertTags($result, $expected);
  1585. $result = $this->Form->input('Address.0.last_name');
  1586. $expected = array(
  1587. 'div' => array('class'),
  1588. 'label' => array('for'),
  1589. 'preg:/[^<]+/',
  1590. '/label',
  1591. 'input' => array('type' => 'text', 'name', 'id'),
  1592. '/div'
  1593. );
  1594. $this->assertTags($result, $expected);
  1595. $result = $this->Form->input('Address.1.last_name');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1603. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1604. ),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'You must have a last name',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. }
  1612. /**
  1613. * testInput method
  1614. *
  1615. * Test various incarnations of input().
  1616. *
  1617. * @return void
  1618. */
  1619. public function testInput() {
  1620. $result = $this->Form->input('ValidateUser.balance');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'Balance',
  1625. '/label',
  1626. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1627. '/div',
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1631. $expected = array(
  1632. 'div' => array('class' => 'input email'),
  1633. 'label' => array('for' => 'custom'),
  1634. 'Email',
  1635. '/label',
  1636. array('input' => array(
  1637. 'type' => 'email', 'name' => 'data[Contact][email]',
  1638. 'id' => 'custom', 'maxlength' => 255
  1639. )),
  1640. '/div'
  1641. );
  1642. $this->assertTags($result, $expected);
  1643. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1644. $expected = array(
  1645. '<div',
  1646. 'label' => array('for' => 'ContactEmail'),
  1647. 'Email',
  1648. '/label',
  1649. array('input' => array(
  1650. 'type' => 'email', 'name' => 'data[Contact][email]',
  1651. 'id' => 'ContactEmail', 'maxlength' => 255
  1652. )),
  1653. '/div'
  1654. );
  1655. $this->assertTags($result, $expected);
  1656. $result = $this->Form->hidden('Contact.idontexist');
  1657. $expected = array('input' => array(
  1658. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1659. 'id' => 'ContactIdontexist'
  1660. ));
  1661. $this->assertTags($result, $expected);
  1662. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1663. $expected = array(
  1664. 'div' => array('class' => 'input text'),
  1665. 'label' => array('for' => 'ContactEmail'),
  1666. 'Email',
  1667. '/label',
  1668. array('input' => array(
  1669. 'type' => 'text', 'name' => 'data[Contact][email]',
  1670. 'id' => 'ContactEmail'
  1671. )),
  1672. '/div'
  1673. );
  1674. $this->assertTags($result, $expected);
  1675. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1676. $expected = array(
  1677. 'div' => array('class' => 'input text'),
  1678. 'label' => array('for' => 'Contact5Email'),
  1679. 'Email',
  1680. '/label',
  1681. array('input' => array(
  1682. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1683. 'id' => 'Contact5Email'
  1684. )),
  1685. '/div'
  1686. );
  1687. $this->assertTags($result, $expected);
  1688. $result = $this->Form->input('Contact.password');
  1689. $expected = array(
  1690. 'div' => array('class' => 'input password'),
  1691. 'label' => array('for' => 'ContactPassword'),
  1692. 'Password',
  1693. '/label',
  1694. array('input' => array(
  1695. 'type' => 'password', 'name' => 'data[Contact][password]',
  1696. 'id' => 'ContactPassword'
  1697. )),
  1698. '/div'
  1699. );
  1700. $this->assertTags($result, $expected);
  1701. $result = $this->Form->input('Contact.email', array(
  1702. 'type' => 'file', 'class' => 'textbox'
  1703. ));
  1704. $expected = array(
  1705. 'div' => array('class' => 'input file'),
  1706. 'label' => array('for' => 'ContactEmail'),
  1707. 'Email',
  1708. '/label',
  1709. array('input' => array(
  1710. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1711. 'id' => 'ContactEmail'
  1712. )),
  1713. '/div'
  1714. );
  1715. $this->assertTags($result, $expected);
  1716. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1717. $result = $this->Form->input('Contact.phone');
  1718. $expected = array(
  1719. 'div' => array('class' => 'input tel'),
  1720. 'label' => array('for' => 'ContactPhone'),
  1721. 'Phone',
  1722. '/label',
  1723. array('input' => array(
  1724. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1725. 'value' => 'Hello &amp; World &gt; weird chars',
  1726. 'id' => 'ContactPhone', 'maxlength' => 255
  1727. )),
  1728. '/div'
  1729. );
  1730. $this->assertTags($result, $expected);
  1731. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1732. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1733. $expected = array(
  1734. 'div' => array('class' => 'input text'),
  1735. 'label' => array('for' => 'myId'),
  1736. 'Field',
  1737. '/label',
  1738. 'input' => array(
  1739. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1740. 'value' => 'My value', 'id' => 'myId'
  1741. ),
  1742. '/div'
  1743. );
  1744. $this->assertTags($result, $expected);
  1745. unset($this->Form->request->data);
  1746. $Contact = ClassRegistry::getObject('Contact');
  1747. $Contact->validationErrors['field'] = array('Badness!');
  1748. $result = $this->Form->input('Contact.field');
  1749. $expected = array(
  1750. 'div' => array('class' => 'input text error'),
  1751. 'label' => array('for' => 'ContactField'),
  1752. 'Field',
  1753. '/label',
  1754. 'input' => array(
  1755. 'type' => 'text', 'name' => 'data[Contact][field]',
  1756. 'id' => 'ContactField', 'class' => 'form-error'
  1757. ),
  1758. array('div' => array('class' => 'error-message')),
  1759. 'Badness!',
  1760. '/div',
  1761. '/div'
  1762. );
  1763. $this->assertTags($result, $expected);
  1764. $result = $this->Form->input('Contact.field', array(
  1765. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1766. ));
  1767. $expected = array(
  1768. 'label' => array('for' => 'ContactField'),
  1769. 'Field',
  1770. '/label',
  1771. 'input' => array(
  1772. 'type' => 'text', 'name' => 'data[Contact][field]',
  1773. 'id' => 'ContactField', 'class' => 'form-error'
  1774. ),
  1775. array('span' => array('class' => 'error-message')),
  1776. 'Badness!',
  1777. '/span'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. $result = $this->Form->input('Contact.field', array(
  1781. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1782. ));
  1783. $expected = array(
  1784. 'div' => array('class' => 'input text error'),
  1785. 'label' => array('for' => 'ContactField'),
  1786. 'Field',
  1787. '/label',
  1788. 'input' => array(
  1789. 'type' => 'text', 'name' => 'data[Contact][field]',
  1790. 'id' => 'ContactField', 'class' => 'form-error'
  1791. ),
  1792. array('div' => array('class' => 'error')),
  1793. 'Badness!',
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. $result = $this->Form->input('Contact.field', array(
  1798. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1799. ));
  1800. $expected = array(
  1801. 'span' => array('class' => 'input text error'),
  1802. 'label' => array('for' => 'ContactField'),
  1803. 'Field',
  1804. '/label',
  1805. 'input' => array(
  1806. 'type' => 'text', 'name' => 'data[Contact][field]',
  1807. 'id' => 'ContactField', 'class' => 'form-error'
  1808. ),
  1809. 'Badness!',
  1810. '/span'
  1811. );
  1812. $this->assertTags($result, $expected);
  1813. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1814. $expected = array(
  1815. 'div' => array('class' => 'input text error'),
  1816. 'label' => array('for' => 'ContactField'),
  1817. 'Field',
  1818. '/label',
  1819. 'input' => array(
  1820. 'type' => 'text', 'name' => 'data[Contact][field]',
  1821. 'id' => 'ContactField', 'class' => 'form-error'
  1822. ),
  1823. 'A message to you, Rudy',
  1824. array('div' => array('class' => 'error-message')),
  1825. 'Badness!',
  1826. '/div',
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. $this->Form->setEntity(null);
  1831. $this->Form->setEntity('Contact.field');
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'after' => 'A message to you, Rudy', 'error' => false
  1834. ));
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1841. 'A message to you, Rudy',
  1842. '/div'
  1843. );
  1844. $this->assertTags($result, $expected);
  1845. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1846. $expected = array(
  1847. 'div' => array('class' => 'input text'),
  1848. 'label' => array('for' => 'ObjectField'),
  1849. 'Field',
  1850. '/label',
  1851. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1852. 'A message to you, Rudy',
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $Contact->validationErrors['field'] = array('minLength');
  1857. $result = $this->Form->input('Contact.field', array(
  1858. 'error' => array(
  1859. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1860. 'maxLength' => 'login too large'
  1861. )
  1862. ));
  1863. $expected = array(
  1864. 'div' => array('class' => 'input text error'),
  1865. 'label' => array('for' => 'ContactField'),
  1866. 'Field',
  1867. '/label',
  1868. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1869. array('div' => array('class' => 'error-message')),
  1870. 'Le login doit contenir au moins 2 caractères',
  1871. '/div',
  1872. '/div'
  1873. );
  1874. $this->assertTags($result, $expected);
  1875. $Contact->validationErrors['field'] = array('maxLength');
  1876. $result = $this->Form->input('Contact.field', array(
  1877. 'error' => array(
  1878. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1879. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1880. 'maxLength' => 'login too large',
  1881. )
  1882. ));
  1883. $expected = array(
  1884. 'div' => array('class' => 'input text error'),
  1885. 'label' => array('for' => 'ContactField'),
  1886. 'Field',
  1887. '/label',
  1888. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1889. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1890. 'login too large',
  1891. '/span',
  1892. '/div'
  1893. );
  1894. $this->assertTags($result, $expected);
  1895. }
  1896. /**
  1897. * Test that inputs with 0 can be created.
  1898. *
  1899. * @return void
  1900. */
  1901. public function testInputZero() {
  1902. $this->Form->create('User');
  1903. $result = $this->Form->input('0');
  1904. $expected = array(
  1905. 'div' => array('class' => 'input text'),
  1906. 'label' => array('for' => 'User0'), '/label',
  1907. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1908. '/div'
  1909. );
  1910. $this->assertTags($result, $expected);
  1911. }
  1912. /**
  1913. * test input() with checkbox creation
  1914. *
  1915. * @return void
  1916. */
  1917. public function testInputCheckbox() {
  1918. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1919. $expected = array(
  1920. 'div' => array('class' => 'input checkbox'),
  1921. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1922. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1923. '/div'
  1924. );
  1925. $this->assertTags($result, $expected);
  1926. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1927. $expected = array(
  1928. 'div' => array('class' => 'input checkbox'),
  1929. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1930. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1931. '/div'
  1932. );
  1933. $this->assertTags($result, $expected);
  1934. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input checkbox'),
  1937. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1938. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1939. '/div'
  1940. );
  1941. $this->assertTags($result, $expected);
  1942. }
  1943. /**
  1944. * test form->input() with time types.
  1945. *
  1946. */
  1947. public function testInputTime() {
  1948. extract($this->dateRegex);
  1949. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1950. $result = explode(':', $result);
  1951. $this->assertRegExp('/option value="23"/', $result[0]);
  1952. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1953. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1954. $result = explode(':', $result);
  1955. $this->assertRegExp('/option value="23"/', $result[0]);
  1956. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1957. $result = $this->Form->input('Model.field', array(
  1958. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1959. ));
  1960. $result = explode(':', $result);
  1961. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1962. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1963. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1964. $result = $this->Form->input('Model.field', array(
  1965. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1966. ));
  1967. $result = explode(':', $result);
  1968. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1969. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1970. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1971. $result = $this->Form->input('prueba', array(
  1972. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1973. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1974. ));
  1975. $result = explode(':', $result);
  1976. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1977. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1978. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1979. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1980. $result = $this->Form->input('Random.start_time', array(
  1981. 'type' => 'time',
  1982. 'selected' => '18:15'
  1983. ));
  1984. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1985. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1986. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1987. $result = $this->Form->input('published', array('type' => 'time'));
  1988. $now = strtotime('now');
  1989. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  1990. $now = strtotime('2013-03-09 00:42:21');
  1991. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  1992. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  1993. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  1994. }
  1995. /**
  1996. * Test interval + selected near the hour roll over.
  1997. *
  1998. * @return void
  1999. */
  2000. public function testTimeSelectedWithInterval() {
  2001. $result = $this->Form->input('Model.start_time', array(
  2002. 'type' => 'time',
  2003. 'interval' => 15,
  2004. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2005. ));
  2006. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2007. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2008. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2009. $result = $this->Form->input('Model.start_time', array(
  2010. 'type' => 'time',
  2011. 'interval' => 15,
  2012. 'selected' => '2012-10-23 15:57:00'
  2013. ));
  2014. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2015. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2016. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2017. $result = $this->Form->input('Model.start_time', array(
  2018. 'timeFormat' => 24,
  2019. 'type' => 'time',
  2020. 'interval' => 15,
  2021. 'selected' => '15:57'
  2022. ));
  2023. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2024. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2025. $result = $this->Form->input('Model.start_time', array(
  2026. 'timeFormat' => 24,
  2027. 'type' => 'time',
  2028. 'interval' => 15,
  2029. 'selected' => '23:57'
  2030. ));
  2031. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2032. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2033. $result = $this->Form->input('Model.created', array(
  2034. 'timeFormat' => 24,
  2035. 'type' => 'datetime',
  2036. 'interval' => 15,
  2037. 'selected' => '2012-09-30 23:56'
  2038. ));
  2039. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2040. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2041. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2042. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2043. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2044. }
  2045. /**
  2046. * test form->input() with datetime, date and time types
  2047. *
  2048. * @return void
  2049. */
  2050. public function testInputDatetime() {
  2051. extract($this->dateRegex);
  2052. $result = $this->Form->input('prueba', array(
  2053. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2054. 'maxYear' => date('Y') + 1, 'interval' => 15
  2055. ));
  2056. $result = explode(':', $result);
  2057. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2058. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2059. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2060. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2061. //related to ticket #5013
  2062. $result = $this->Form->input('Contact.date', array(
  2063. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2064. ));
  2065. $this->assertRegExp('/class="customClass"/', $result);
  2066. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2067. $result = $this->Form->input('Contact.date', array(
  2068. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2069. ));
  2070. $this->assertRegExp('/id="customIdDay"/', $result);
  2071. $this->assertRegExp('/id="customIdMonth"/', $result);
  2072. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2073. $result = $this->Form->input('Model.field', array(
  2074. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2075. ));
  2076. $this->assertRegExp('/id="customIDDay"/', $result);
  2077. $this->assertRegExp('/id="customIDHour"/', $result);
  2078. $result = explode('</select><select', $result);
  2079. $result = explode(':', $result[1]);
  2080. $this->assertRegExp('/option value="23"/', $result[0]);
  2081. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2082. $result = $this->Form->input('Model.field', array(
  2083. 'type' => 'datetime', 'timeFormat' => 12
  2084. ));
  2085. $result = explode('</select><select', $result);
  2086. $result = explode(':', $result[1]);
  2087. $this->assertRegExp('/option value="12"/', $result[0]);
  2088. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2089. $this->Form->request->data = array('Contact' => array('created' => null));
  2090. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2091. $expected = array(
  2092. 'div' => array('class' => 'input date'),
  2093. 'label' => array('for' => 'ContactCreatedMonth'),
  2094. 'Created',
  2095. '/label',
  2096. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2097. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2098. $monthsRegex,
  2099. '/select', '-',
  2100. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2101. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2102. $daysRegex,
  2103. '/select', '-',
  2104. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2105. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2106. $yearsRegex,
  2107. '/select',
  2108. '/div'
  2109. );
  2110. $this->assertTags($result, $expected);
  2111. $this->Form->request->data = array('Contact' => array('created' => null));
  2112. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2113. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2114. $this->Form->request->data = array('Contact' => array('created' => null));
  2115. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2116. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2117. $result = $this->Form->input('Contact.created', array(
  2118. 'type' => 'date',
  2119. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2120. 'timeFormat' => 'NONE'
  2121. ));
  2122. $this->assertRegExp('/for\="created-month"/', $result);
  2123. }
  2124. /**
  2125. * Test generating checkboxes in a loop.
  2126. *
  2127. * @return void
  2128. */
  2129. public function testInputCheckboxesInLoop() {
  2130. for ($i = 1; $i < 5; $i++) {
  2131. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2132. $expected = array(
  2133. 'div' => array('class' => 'input checkbox'),
  2134. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2135. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2136. 'label' => array('for' => "Contact{$i}Email"),
  2137. 'Email',
  2138. '/label',
  2139. '/div'
  2140. );
  2141. $this->assertTags($result, $expected);
  2142. }
  2143. }
  2144. /**
  2145. * Test generating checkboxes with disabled elements.
  2146. *
  2147. * @return void
  2148. */
  2149. public function testInputCheckboxWithDisabledElements() {
  2150. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2151. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2152. $expected = array(
  2153. array('div' => array('class' => 'input select')),
  2154. array('label' => array('for' => "ContactMultiple")),
  2155. 'Multiple',
  2156. '/label',
  2157. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2158. array('div' => array('class' => 'checkbox')),
  2159. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2160. array('label' => array('for' => "ContactMultiple1")),
  2161. 'One',
  2162. '/label',
  2163. '/div',
  2164. array('div' => array('class' => 'checkbox')),
  2165. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2166. array('label' => array('for' => "ContactMultiple2")),
  2167. 'Two',
  2168. '/label',
  2169. '/div',
  2170. array('div' => array('class' => 'checkbox')),
  2171. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2172. array('label' => array('for' => "ContactMultiple3")),
  2173. 'Three',
  2174. '/label',
  2175. '/div',
  2176. '/div'
  2177. );
  2178. $this->assertTags($result, $expected);
  2179. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2180. $this->assertTags($result, $expected);
  2181. $disabled = array('2', 3);
  2182. $expected = array(
  2183. array('div' => array('class' => 'input select')),
  2184. array('label' => array('for' => "ContactMultiple")),
  2185. 'Multiple',
  2186. '/label',
  2187. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2188. array('div' => array('class' => 'checkbox')),
  2189. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2190. array('label' => array('for' => "ContactMultiple1")),
  2191. 'One',
  2192. '/label',
  2193. '/div',
  2194. array('div' => array('class' => 'checkbox')),
  2195. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2196. array('label' => array('for' => "ContactMultiple2")),
  2197. 'Two',
  2198. '/label',
  2199. '/div',
  2200. array('div' => array('class' => 'checkbox')),
  2201. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2202. array('label' => array('for' => "ContactMultiple3")),
  2203. 'Three',
  2204. '/label',
  2205. '/div',
  2206. '/div'
  2207. );
  2208. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2209. $this->assertTags($result, $expected);
  2210. // make sure 50 does only disable 50, and not 50f5c0cf
  2211. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2212. $disabled = array(50);
  2213. $expected = array(
  2214. array('div' => array('class' => 'input select')),
  2215. array('label' => array('for' => "ContactMultiple")),
  2216. 'Multiple',
  2217. '/label',
  2218. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2219. array('div' => array('class' => 'checkbox')),
  2220. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2221. array('label' => array('for' => "ContactMultiple50")),
  2222. 'Fifty',
  2223. '/label',
  2224. '/div',
  2225. array('div' => array('class' => 'checkbox')),
  2226. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2227. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2228. 'Stringy',
  2229. '/label',
  2230. '/div',
  2231. '/div'
  2232. );
  2233. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2234. $this->assertTags($result, $expected);
  2235. }
  2236. /**
  2237. * test input name with leading integer, ensure attributes are generated correctly.
  2238. *
  2239. * @return void
  2240. */
  2241. public function testInputWithLeadingInteger() {
  2242. $result = $this->Form->text('0.Node.title');
  2243. $expected = array(
  2244. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2245. );
  2246. $this->assertTags($result, $expected);
  2247. }
  2248. /**
  2249. * test form->input() with select type inputs.
  2250. *
  2251. * @return void
  2252. */
  2253. public function testInputSelectType() {
  2254. $result = $this->Form->input('email', array(
  2255. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2256. );
  2257. $expected = array(
  2258. 'div' => array('class' => 'input select'),
  2259. 'label' => array('for' => 'email'),
  2260. 'Email',
  2261. '/label',
  2262. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2263. array('option' => array('value' => '')),
  2264. '/option',
  2265. array('option' => array('value' => 'è')),
  2266. 'Firést',
  2267. '/option',
  2268. array('option' => array('value' => 'é')),
  2269. 'Secoènd',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $result = $this->Form->input('email', array(
  2276. 'options' => array('First', 'Second'), 'empty' => true)
  2277. );
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'email'),
  2281. 'Email',
  2282. '/label',
  2283. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2284. array('option' => array('value' => '')),
  2285. '/option',
  2286. array('option' => array('value' => '0')),
  2287. 'First',
  2288. '/option',
  2289. array('option' => array('value' => '1')),
  2290. 'Second',
  2291. '/option',
  2292. '/select',
  2293. '/div'
  2294. );
  2295. $this->assertTags($result, $expected);
  2296. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2297. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2298. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2299. $expected = array(
  2300. 'div' => array('class' => 'input select'),
  2301. 'label' => array('for' => 'ModelUserId'),
  2302. 'User',
  2303. '/label',
  2304. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2305. array('option' => array('value' => '')),
  2306. '/option',
  2307. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2308. 'good',
  2309. '/option',
  2310. array('option' => array('value' => 'other')),
  2311. 'bad',
  2312. '/option',
  2313. '/select',
  2314. '/div'
  2315. );
  2316. $this->assertTags($result, $expected);
  2317. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2318. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2319. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2320. $expected = array(
  2321. 'div' => array('class' => 'input select'),
  2322. 'label' => array('for' => 'ThingUserId'),
  2323. 'User',
  2324. '/label',
  2325. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2326. array('option' => array('value' => '')),
  2327. 'Some Empty',
  2328. '/option',
  2329. array('option' => array('value' => 'value')),
  2330. 'good',
  2331. '/option',
  2332. array('option' => array('value' => 'other')),
  2333. 'bad',
  2334. '/option',
  2335. '/select',
  2336. '/div'
  2337. );
  2338. $this->assertTags($result, $expected);
  2339. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2340. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2341. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2342. $expected = array(
  2343. 'div' => array('class' => 'input select'),
  2344. 'label' => array('for' => 'ThingUserId'),
  2345. 'User',
  2346. '/label',
  2347. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2348. array('option' => array('value' => '')),
  2349. 'Some Empty',
  2350. '/option',
  2351. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2352. 'good',
  2353. '/option',
  2354. array('option' => array('value' => 'other')),
  2355. 'bad',
  2356. '/option',
  2357. '/select',
  2358. '/div'
  2359. );
  2360. $this->assertTags($result, $expected);
  2361. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2362. $this->Form->request->data = array('User' => array('User' => array('value')));
  2363. $result = $this->Form->input('User.User', array('empty' => true));
  2364. $expected = array(
  2365. 'div' => array('class' => 'input select'),
  2366. 'label' => array('for' => 'UserUser'),
  2367. 'User',
  2368. '/label',
  2369. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2370. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2371. array('option' => array('value' => '')),
  2372. '/option',
  2373. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2374. 'good',
  2375. '/option',
  2376. array('option' => array('value' => 'other')),
  2377. 'bad',
  2378. '/option',
  2379. '/select',
  2380. '/div'
  2381. );
  2382. $this->assertTags($result, $expected);
  2383. $this->Form->data = array();
  2384. $result = $this->Form->input('Publisher.id', array(
  2385. 'label' => 'Publisher',
  2386. 'type' => 'select',
  2387. 'multiple' => 'checkbox',
  2388. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2389. ));
  2390. $expected = array(
  2391. array('div' => array('class' => 'input select')),
  2392. array('label' => array('for' => 'PublisherId')),
  2393. 'Publisher',
  2394. '/label',
  2395. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2396. array('div' => array('class' => 'checkbox')),
  2397. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2398. array('label' => array('for' => 'PublisherIdValue1')),
  2399. 'Label 1',
  2400. '/label',
  2401. '/div',
  2402. array('div' => array('class' => 'checkbox')),
  2403. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2404. array('label' => array('for' => 'PublisherIdValue2')),
  2405. 'Label 2',
  2406. '/label',
  2407. '/div',
  2408. '/div'
  2409. );
  2410. $this->assertTags($result, $expected);
  2411. }
  2412. /**
  2413. * test that input() and a non standard primary key makes a hidden input by default.
  2414. *
  2415. * @return void
  2416. */
  2417. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2418. $this->Form->create('User');
  2419. $this->Form->fieldset = array(
  2420. 'User' => array(
  2421. 'fields' => array(
  2422. 'model_id' => array('type' => 'integer')
  2423. ),
  2424. 'validates' => array(),
  2425. 'key' => 'model_id'
  2426. )
  2427. );
  2428. $result = $this->Form->input('model_id');
  2429. $expected = array(
  2430. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2431. );
  2432. $this->assertTags($result, $expected);
  2433. }
  2434. /**
  2435. * test that overriding the magic select type widget is possible
  2436. *
  2437. * @return void
  2438. */
  2439. public function testInputOverridingMagicSelectType() {
  2440. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2441. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2442. $expected = array(
  2443. 'div' => array('class' => 'input text'),
  2444. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2445. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2446. '/div'
  2447. );
  2448. $this->assertTags($result, $expected);
  2449. //Check that magic types still work for plural/singular vars
  2450. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2451. $result = $this->Form->input('Model.type');
  2452. $expected = array(
  2453. 'div' => array('class' => 'input select'),
  2454. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2455. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2456. array('option' => array('value' => 'value')), 'good', '/option',
  2457. array('option' => array('value' => 'other')), 'bad', '/option',
  2458. '/select',
  2459. '/div'
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. }
  2463. /**
  2464. * Test that magic input() selects are created for type=number
  2465. *
  2466. * @return void
  2467. */
  2468. public function testInputMagicSelectForTypeNumber() {
  2469. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2470. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2471. $result = $this->Form->input('ValidateUser.balance');
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ValidateUserBalance'),
  2475. 'Balance',
  2476. '/label',
  2477. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2478. array('option' => array('value' => '0')),
  2479. 'nothing',
  2480. '/option',
  2481. array('option' => array('value' => '1', 'selected' => 'selected')),
  2482. 'some',
  2483. '/option',
  2484. array('option' => array('value' => '100')),
  2485. 'a lot',
  2486. '/option',
  2487. '/select',
  2488. '/div'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. }
  2492. /**
  2493. * Test that magic input() selects can easily be converted into radio types without error.
  2494. *
  2495. * @return void
  2496. */
  2497. public function testInputMagicSelectChangeToRadio() {
  2498. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2499. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2500. $this->assertRegExp('/input type="radio"/', $result);
  2501. }
  2502. /**
  2503. * fields with the same name as the model should work.
  2504. *
  2505. * @return void
  2506. */
  2507. public function testInputWithMatchingFieldAndModelName() {
  2508. $this->Form->create('User');
  2509. $this->Form->fieldset = array(
  2510. 'User' => array(
  2511. 'fields' => array(
  2512. 'User' => array('type' => 'text')
  2513. ),
  2514. 'validates' => array(),
  2515. 'key' => 'id'
  2516. )
  2517. );
  2518. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2519. $result = $this->Form->input('User', array('type' => 'text'));
  2520. $expected = array(
  2521. 'div' => array('class' => 'input text'),
  2522. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2523. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2524. '/div'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. }
  2528. /**
  2529. * testFormInputs method
  2530. *
  2531. * test correct results from form::inputs().
  2532. *
  2533. * @return void
  2534. */
  2535. public function testFormInputs() {
  2536. $this->Form->create('Contact');
  2537. $result = $this->Form->inputs('The Legend');
  2538. $expected = array(
  2539. '<fieldset',
  2540. '<legend',
  2541. 'The Legend',
  2542. '/legend',
  2543. '*/fieldset',
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2547. $expected = array(
  2548. 'fieldset' => array('class' => 'classy-stuff'),
  2549. '<legend',
  2550. 'Field of Dreams',
  2551. '/legend',
  2552. '*/fieldset'
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2556. $this->assertTags($result, $expected);
  2557. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2558. $this->assertTags($result, $expected);
  2559. $this->Form->create('Contact');
  2560. $this->Form->request['prefix'] = 'admin';
  2561. $this->Form->request['action'] = 'admin_edit';
  2562. $result = $this->Form->inputs();
  2563. $expected = array(
  2564. '<fieldset',
  2565. '<legend',
  2566. 'Edit Contact',
  2567. '/legend',
  2568. '*/fieldset',
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $this->Form->create('Contact');
  2572. $result = $this->Form->inputs(false);
  2573. $expected = array(
  2574. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2575. array('div' => array('class' => 'input text')),
  2576. '*/div',
  2577. array('div' => array('class' => 'input email')),
  2578. '*/div',
  2579. array('div' => array('class' => 'input tel')),
  2580. '*/div',
  2581. array('div' => array('class' => 'input password')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input date')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input date')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input datetime')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input number')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input select')),
  2592. '*/div',
  2593. );
  2594. $this->assertTags($result, $expected);
  2595. $this->Form->create('Contact');
  2596. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2597. $expected = array(
  2598. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2599. array('div' => array('class' => 'input text')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input email')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input tel')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input password')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input date')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input date')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input datetime')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input number')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input select')),
  2616. '*/div',
  2617. );
  2618. $this->assertTags($result, $expected);
  2619. $this->Form->create('Contact');
  2620. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2621. $this->assertTags($result, $expected);
  2622. $this->Form->create('Contact');
  2623. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2624. $expected = array(
  2625. 'fieldset' => array(),
  2626. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2627. array('div' => array('class' => 'input text')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input email')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input tel')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input password')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input date')),
  2636. '*/div',
  2637. array('div' => array('class' => 'input date')),
  2638. '*/div',
  2639. array('div' => array('class' => 'input datetime')),
  2640. '*/div',
  2641. array('div' => array('class' => 'input number')),
  2642. '*/div',
  2643. array('div' => array('class' => 'input select')),
  2644. '*/div',
  2645. '/fieldset'
  2646. );
  2647. $this->assertTags($result, $expected);
  2648. $this->Form->create('Contact');
  2649. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2650. $expected = array(
  2651. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2652. array('div' => array('class' => 'input text')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input email')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input tel')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input password')),
  2659. '*/div',
  2660. array('div' => array('class' => 'input date')),
  2661. '*/div',
  2662. array('div' => array('class' => 'input date')),
  2663. '*/div',
  2664. array('div' => array('class' => 'input datetime')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input number')),
  2667. '*/div',
  2668. array('div' => array('class' => 'input select')),
  2669. '*/div',
  2670. );
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2674. $this->assertTags($result, $expected);
  2675. $this->Form->create('Contact');
  2676. $result = $this->Form->inputs('Hello');
  2677. $expected = array(
  2678. 'fieldset' => array(),
  2679. 'legend' => array(),
  2680. 'Hello',
  2681. '/legend',
  2682. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2683. array('div' => array('class' => 'input text')),
  2684. '*/div',
  2685. array('div' => array('class' => 'input email')),
  2686. '*/div',
  2687. array('div' => array('class' => 'input tel')),
  2688. '*/div',
  2689. array('div' => array('class' => 'input password')),
  2690. '*/div',
  2691. array('div' => array('class' => 'input date')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input date')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input datetime')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input number')),
  2698. '*/div',
  2699. array('div' => array('class' => 'input select')),
  2700. '*/div',
  2701. '/fieldset'
  2702. );
  2703. $this->assertTags($result, $expected);
  2704. $this->Form->create('Contact');
  2705. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2706. $expected = array(
  2707. 'fieldset' => array(),
  2708. 'legend' => array(),
  2709. 'Hello',
  2710. '/legend',
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. '/fieldset'
  2731. );
  2732. $this->assertTags($result, $expected);
  2733. $this->Form->create('Contact');
  2734. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2735. $this->assertTags($result, $expected);
  2736. $this->Form->end();
  2737. $this->Form->create(false);
  2738. $expected = array(
  2739. 'fieldset' => array(),
  2740. array('div' => array('class' => 'input text')),
  2741. 'label' => array('for' => 'foo'),
  2742. 'Foo',
  2743. '/label',
  2744. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2745. '*/div',
  2746. '/fieldset'
  2747. );
  2748. $result = $this->Form->inputs(
  2749. array('foo' => array('type' => 'text')),
  2750. array(),
  2751. array('legend' => false)
  2752. );
  2753. $this->assertTags($result, $expected);
  2754. }
  2755. /**
  2756. * Tests inputs() works with plugin models
  2757. *
  2758. * @return void
  2759. */
  2760. public function testInputsPluginModel() {
  2761. $this->loadFixtures('Post');
  2762. App::build(array(
  2763. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2764. ));
  2765. CakePlugin::load('TestPlugin');
  2766. $this->Form->request['models'] = array(
  2767. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2768. );
  2769. $this->Form->create('TestPlugin.TestPluginPost');
  2770. $result = $this->Form->inputs();
  2771. $this->assertContains('data[TestPluginPost][id]', $result);
  2772. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2773. $this->assertContains('data[TestPluginPost][title]', $result);
  2774. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2775. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2776. $this->assertEquals('TestPluginPost', $this->Form->model());
  2777. }
  2778. /**
  2779. * testSelectAsCheckbox method
  2780. *
  2781. * test multi-select widget with checkbox formatting.
  2782. *
  2783. * @return void
  2784. */
  2785. public function testSelectAsCheckbox() {
  2786. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2787. $expected = array(
  2788. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2789. array('div' => array('class' => 'checkbox')),
  2790. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2791. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2792. 'first',
  2793. '/label',
  2794. '/div',
  2795. array('div' => array('class' => 'checkbox')),
  2796. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2797. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2798. 'second',
  2799. '/label',
  2800. '/div',
  2801. array('div' => array('class' => 'checkbox')),
  2802. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2803. array('label' => array('for' => 'ModelMultiField2')),
  2804. 'third',
  2805. '/label',
  2806. '/div',
  2807. );
  2808. $this->assertTags($result, $expected);
  2809. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2810. $expected = array(
  2811. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2812. array('div' => array('class' => 'checkbox')),
  2813. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2814. array('label' => array('for' => 'ModelMultiField12')),
  2815. 'half',
  2816. '/label',
  2817. '/div',
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. }
  2821. /**
  2822. * testLabel method
  2823. *
  2824. * test label generation.
  2825. *
  2826. * @return void
  2827. */
  2828. public function testLabel() {
  2829. $this->Form->text('Person.name');
  2830. $result = $this->Form->label();
  2831. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2832. $this->Form->text('Person.name');
  2833. $result = $this->Form->label();
  2834. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2835. $result = $this->Form->label('Person.first_name');
  2836. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2837. $result = $this->Form->label('Person.first_name', 'Your first name');
  2838. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2839. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2840. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2841. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2842. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2843. $result = $this->Form->label('Person.first_name', '');
  2844. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2845. $result = $this->Form->label('Person.2.name', '');
  2846. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2847. }
  2848. /**
  2849. * testTextbox method
  2850. *
  2851. * test textbox element generation
  2852. *
  2853. * @return void
  2854. */
  2855. public function testTextbox() {
  2856. $result = $this->Form->text('Model.field');
  2857. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2858. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2859. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2860. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2861. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2862. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2863. $result = $this->Form->text('Model.text');
  2864. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2865. $Contact = ClassRegistry::getObject('Contact');
  2866. $Contact->validationErrors['text'] = array(true);
  2867. $this->Form->request->data['Contact']['text'] = 'test';
  2868. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2869. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2870. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2871. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2872. $expected = array(
  2873. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. }
  2877. /**
  2878. * testDefaultValue method
  2879. *
  2880. * Test default value setting
  2881. *
  2882. * @return void
  2883. */
  2884. public function testDefaultValue() {
  2885. $this->Form->request->data['Model']['field'] = 'test';
  2886. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2887. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2888. unset($this->Form->request->data['Model']['field']);
  2889. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2890. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2891. }
  2892. /**
  2893. * testCheckboxDefaultValue method
  2894. *
  2895. * Test default value setting on checkbox() method
  2896. *
  2897. * @return void
  2898. */
  2899. public function testCheckboxDefaultValue() {
  2900. $this->Form->request->data['Model']['field'] = false;
  2901. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2902. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2903. unset($this->Form->request->data['Model']['field']);
  2904. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2905. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2906. $this->Form->request->data['Model']['field'] = true;
  2907. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2908. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2909. unset($this->Form->request->data['Model']['field']);
  2910. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2911. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2912. }
  2913. /**
  2914. * testError method
  2915. *
  2916. * Test field error generation
  2917. *
  2918. * @return void
  2919. */
  2920. public function testError() {
  2921. $Contact = ClassRegistry::getObject('Contact');
  2922. $Contact->validationErrors['field'] = array(1);
  2923. $result = $this->Form->error('Contact.field');
  2924. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2925. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2926. $this->assertEquals('Error in field Field', $result);
  2927. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2928. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2929. $this->assertEquals('This field contains invalid input', $result);
  2930. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2931. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2932. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2933. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2934. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2935. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2936. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2937. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2938. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2939. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2940. $this->assertEquals('<strong>Badness!</strong>', $result);
  2941. $Contact->validationErrors['field'] = array("email");
  2942. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2943. $expected = array(
  2944. 'div' => array('class' => 'field-error'),
  2945. 'No good!',
  2946. '/div'
  2947. );
  2948. $this->assertTags($result, $expected);
  2949. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2950. $result = $this->Form->error('Contact.field', array(
  2951. 'notEmpty' => 'Cannot be empty',
  2952. 'email' => 'No good!'
  2953. ));
  2954. $expected = array(
  2955. 'div' => array('class' => 'error-message'),
  2956. 'ul' => array(),
  2957. '<li', 'Cannot be empty', '/li',
  2958. '<li', 'No good!', '/li',
  2959. '<li', 'Something else', '/li',
  2960. '/ul',
  2961. '/div'
  2962. );
  2963. $this->assertTags($result, $expected);
  2964. // Testing error messages list options
  2965. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2966. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2967. $expected = array(
  2968. 'div' => array('class' => 'error-message'),
  2969. 'ol' => array(),
  2970. '<li', 'notEmpty', '/li',
  2971. '<li', 'email', '/li',
  2972. '/ol',
  2973. '/div'
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2977. $expected = array(
  2978. 'div' => array('class' => 'error-message'),
  2979. 'ol' => array(),
  2980. '<li', 'notEmpty', '/li',
  2981. '<li', 'email', '/li',
  2982. '/ol',
  2983. '/div'
  2984. );
  2985. $this->assertTags($result, $expected);
  2986. $result = $this->Form->error('Contact.field', null, array(
  2987. 'listOptions' => array(
  2988. 'class' => 'ul-class',
  2989. 'itemOptions' => array(
  2990. 'class' => 'li-class'
  2991. )
  2992. )
  2993. ));
  2994. $expected = array(
  2995. 'div' => array('class' => 'error-message'),
  2996. 'ul' => array('class' => 'ul-class'),
  2997. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2998. array('li' => array('class' => 'li-class')), 'email', '/li',
  2999. '/ul',
  3000. '/div'
  3001. );
  3002. $this->assertTags($result, $expected);
  3003. }
  3004. /**
  3005. * test error options when using form->input();
  3006. *
  3007. * @return void
  3008. */
  3009. public function testInputErrorEscape() {
  3010. $this->Form->create('ValidateProfile');
  3011. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3012. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3013. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3014. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3015. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3016. $this->assertRegExp('/required<br>/', $result);
  3017. }
  3018. /**
  3019. * testPassword method
  3020. *
  3021. * Test password element generation
  3022. *
  3023. * @return void
  3024. */
  3025. public function testPassword() {
  3026. $Contact = ClassRegistry::getObject('Contact');
  3027. $result = $this->Form->password('Contact.field');
  3028. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3029. $Contact->validationErrors['passwd'] = 1;
  3030. $this->Form->request->data['Contact']['passwd'] = 'test';
  3031. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3032. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3033. }
  3034. /**
  3035. * testRadio method
  3036. *
  3037. * Test radio element set generation
  3038. *
  3039. * @return void
  3040. */
  3041. public function testRadio() {
  3042. $result = $this->Form->radio('Model.field', array('option A'));
  3043. $expected = array(
  3044. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3045. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3046. 'label' => array('for' => 'ModelField0'),
  3047. 'option A',
  3048. '/label'
  3049. );
  3050. $this->assertTags($result, $expected);
  3051. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3052. $expected = array(
  3053. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3054. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3055. 'label' => array('for' => 'ModelField12'),
  3056. 'half',
  3057. '/label'
  3058. );
  3059. $this->assertTags($result, $expected);
  3060. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3061. $expected = array(
  3062. 'fieldset' => array(),
  3063. 'legend' => array(),
  3064. 'Field',
  3065. '/legend',
  3066. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3067. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3068. array('label' => array('for' => 'ModelField0')),
  3069. 'option A',
  3070. '/label',
  3071. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3072. array('label' => array('for' => 'ModelField1')),
  3073. 'option B',
  3074. '/label',
  3075. '/fieldset'
  3076. );
  3077. $this->assertTags($result, $expected);
  3078. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3079. $expected = array(
  3080. 'fieldset' => array(),
  3081. 'legend' => array(),
  3082. 'Field',
  3083. '/legend',
  3084. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3085. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3086. array('label' => array('for' => 'ModelField0')),
  3087. 'option A',
  3088. '/label',
  3089. 'br' => array(),
  3090. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3091. array('label' => array('for' => 'ModelField1')),
  3092. 'option B',
  3093. '/label',
  3094. '/fieldset'
  3095. );
  3096. $this->assertTags($result, $expected);
  3097. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3098. $expected = array(
  3099. 'fieldset' => array(),
  3100. 'legend' => array(),
  3101. 'Gender',
  3102. '/legend',
  3103. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3104. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3105. array('label' => array('for' => 'EmployeeGenderMale')),
  3106. 'Male',
  3107. '/label',
  3108. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3109. array('label' => array('for' => 'EmployeeGenderFemale')),
  3110. 'Female',
  3111. '/label',
  3112. '/fieldset',
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3116. $expected = array(
  3117. 'fieldset' => array(),
  3118. 'legend' => array(),
  3119. 'Gender',
  3120. '/legend',
  3121. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3122. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3123. array('label' => array('for' => 'OfficerGenderMale')),
  3124. 'Male',
  3125. '/label',
  3126. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3127. array('label' => array('for' => 'OfficerGenderFemale')),
  3128. 'Female',
  3129. '/label',
  3130. '/fieldset',
  3131. );
  3132. $this->assertTags($result, $expected);
  3133. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3134. $expected = array(
  3135. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3136. array('input' => array(
  3137. 'type' => 'radio',
  3138. 'name' => 'data[Contact][1][imrequired]',
  3139. 'value' => '0',
  3140. 'id' => 'Contact1Imrequired0',
  3141. 'required' => 'required'
  3142. )),
  3143. 'label' => array('for' => 'Contact1Imrequired0'),
  3144. 'option A',
  3145. '/label'
  3146. );
  3147. $this->assertTags($result, $expected);
  3148. $result = $this->Form->radio('Model.1.field', array('option A'));
  3149. $expected = array(
  3150. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3151. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3152. 'label' => array('for' => 'Model1Field0'),
  3153. 'option A',
  3154. '/label'
  3155. );
  3156. $this->assertTags($result, $expected);
  3157. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3158. $expected = array(
  3159. 'fieldset' => array(),
  3160. 'legend' => array(),
  3161. 'Field',
  3162. '/legend',
  3163. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3165. array('label' => array('for' => 'ModelField0')),
  3166. 'option A',
  3167. '/label',
  3168. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3169. array('label' => array('for' => 'ModelField1')),
  3170. 'option B',
  3171. '/label',
  3172. '/fieldset'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. }
  3176. /**
  3177. * Test radio inputs with between as string or array. Also ensure
  3178. * that an array with less between elements works.
  3179. *
  3180. * @return void
  3181. */
  3182. public function testRadioBetween() {
  3183. $result = $this->Form->radio(
  3184. 'Model.field',
  3185. array('option A', 'option B'),
  3186. array('between' => 'I am between')
  3187. );
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. 'I am between',
  3194. 'input' => array(
  3195. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3196. 'value' => '', 'id' => 'ModelField_'
  3197. ),
  3198. array('input' => array(
  3199. 'type' => 'radio', 'name' => 'data[Model][field]',
  3200. 'value' => '0', 'id' => 'ModelField0'
  3201. )),
  3202. array('label' => array('for' => 'ModelField0')),
  3203. 'option A',
  3204. '/label',
  3205. array('input' => array(
  3206. 'type' => 'radio', 'name' => 'data[Model][field]',
  3207. 'value' => '1', 'id' => 'ModelField1'
  3208. )),
  3209. array('label' => array('for' => 'ModelField1')),
  3210. 'option B',
  3211. '/label',
  3212. '/fieldset'
  3213. );
  3214. $this->assertTags($result, $expected);
  3215. $result = $this->Form->radio(
  3216. 'Model.field',
  3217. array('option A', 'option B', 'option C'),
  3218. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3219. );
  3220. $expected = array(
  3221. 'fieldset' => array(),
  3222. 'legend' => array(),
  3223. 'Field',
  3224. '/legend',
  3225. 'input' => array(
  3226. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3227. 'value' => '', 'id' => 'ModelField_'
  3228. ),
  3229. array('input' => array(
  3230. 'type' => 'radio', 'name' => 'data[Model][field]',
  3231. 'value' => '0', 'id' => 'ModelField0'
  3232. )),
  3233. array('label' => array('for' => 'ModelField0')),
  3234. 'option A',
  3235. '/label',
  3236. 'between A',
  3237. '--separator--',
  3238. array('input' => array(
  3239. 'type' => 'radio', 'name' => 'data[Model][field]',
  3240. 'value' => '1', 'id' => 'ModelField1'
  3241. )),
  3242. array('label' => array('for' => 'ModelField1')),
  3243. 'option B',
  3244. '/label',
  3245. 'between B',
  3246. '--separator--',
  3247. array('input' => array(
  3248. 'type' => 'radio', 'name' => 'data[Model][field]',
  3249. 'value' => '2', 'id' => 'ModelField2'
  3250. )),
  3251. array('label' => array('for' => 'ModelField2')),
  3252. 'option C',
  3253. '/label',
  3254. 'between C',
  3255. '/fieldset'
  3256. );
  3257. $this->assertTags($result, $expected);
  3258. $result = $this->Form->input('Model.field', array(
  3259. 'options' => array('1' => 'first', '2' => 'second'),
  3260. 'type' => 'radio',
  3261. 'before' => '--before--',
  3262. 'after' => '--after--',
  3263. 'separator' => '--separator--',
  3264. 'between' => array('--between first--', '--between second--')
  3265. ));
  3266. $expected = array(
  3267. 'div' => array('class' => 'input radio'),
  3268. '--before--',
  3269. 'fieldset' => array(),
  3270. 'legend' => array(),
  3271. 'Field',
  3272. '/legend',
  3273. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3274. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3275. array('label' => array('for' => 'ModelField1')),
  3276. 'first',
  3277. '/label',
  3278. '--between first--',
  3279. '--separator--',
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3281. array('label' => array('for' => 'ModelField2')),
  3282. 'second',
  3283. '/label',
  3284. '--between second--',
  3285. '/fieldset',
  3286. '--after--',
  3287. '/div'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. $result = $this->Form->input('Model.field', array(
  3291. 'options' => array('1' => 'first', '2' => 'second'),
  3292. 'type' => 'radio',
  3293. 'before' => '--before--',
  3294. 'after' => '--after--',
  3295. 'separator' => '--separator--',
  3296. 'between' => array('--between first--')
  3297. ));
  3298. $expected = array(
  3299. 'div' => array('class' => 'input radio'),
  3300. '--before--',
  3301. 'fieldset' => array(),
  3302. 'legend' => array(),
  3303. 'Field',
  3304. '/legend',
  3305. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3306. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3307. array('label' => array('for' => 'ModelField1')),
  3308. 'first',
  3309. '/label',
  3310. '--between first--',
  3311. '--separator--',
  3312. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3313. array('label' => array('for' => 'ModelField2')),
  3314. 'second',
  3315. '/label',
  3316. '/fieldset',
  3317. '--after--',
  3318. '/div'
  3319. );
  3320. $this->assertTags($result, $expected);
  3321. }
  3322. /**
  3323. * Test that radios with a 0 value are selected under the correct conditions.
  3324. * Also ensure that values that are booleanish are handled correctly.
  3325. *
  3326. * @return void
  3327. */
  3328. public function testRadioOptionWithBooleanishValues() {
  3329. $expected = array(
  3330. 'fieldset' => array(),
  3331. 'legend' => array(),
  3332. 'Field',
  3333. '/legend',
  3334. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3335. array('label' => array('for' => 'ModelField1')),
  3336. 'Yes',
  3337. '/label',
  3338. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3339. array('label' => array('for' => 'ModelField0')),
  3340. 'No',
  3341. '/label',
  3342. '/fieldset'
  3343. );
  3344. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3345. $this->assertTags($result, $expected);
  3346. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3347. $this->assertTags($result, $expected);
  3348. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3349. $this->assertTags($result, $expected);
  3350. $expected = array(
  3351. 'fieldset' => array(),
  3352. 'legend' => array(),
  3353. 'Field',
  3354. '/legend',
  3355. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3356. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3357. array('label' => array('for' => 'ModelField1')),
  3358. 'Yes',
  3359. '/label',
  3360. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3361. array('label' => array('for' => 'ModelField0')),
  3362. 'No',
  3363. '/label',
  3364. '/fieldset'
  3365. );
  3366. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3367. $this->assertTags($result, $expected);
  3368. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3369. $this->assertTags($result, $expected);
  3370. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3371. $this->assertTags($result, $expected);
  3372. $expected = array(
  3373. 'fieldset' => array(),
  3374. 'legend' => array(),
  3375. 'Field',
  3376. '/legend',
  3377. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3378. array('label' => array('for' => 'ModelField1')),
  3379. 'Yes',
  3380. '/label',
  3381. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3382. array('label' => array('for' => 'ModelField0')),
  3383. 'No',
  3384. '/label',
  3385. '/fieldset'
  3386. );
  3387. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3388. $this->assertTags($result, $expected);
  3389. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3390. $this->assertTags($result, $expected);
  3391. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3392. $this->assertTags($result, $expected);
  3393. }
  3394. /**
  3395. * test disabled radio options
  3396. *
  3397. * @return void
  3398. */
  3399. public function testRadioDisabled() {
  3400. $result = $this->Form->radio(
  3401. 'Model.field',
  3402. array('option A', 'option B'),
  3403. array('disabled' => array(0), 'value' => '0')
  3404. );
  3405. $expected = array(
  3406. 'fieldset' => array(),
  3407. 'legend' => array(),
  3408. 'Field',
  3409. '/legend',
  3410. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3411. array('label' => array('for' => 'ModelField0')),
  3412. 'option A',
  3413. '/label',
  3414. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3415. array('label' => array('for' => 'ModelField1')),
  3416. 'option B',
  3417. '/label',
  3418. '/fieldset'
  3419. );
  3420. $this->assertTags($result, $expected);
  3421. $result = $this->Form->radio(
  3422. 'Model.field',
  3423. array('option A', 'option B'),
  3424. array('disabled' => true, 'value' => '0')
  3425. );
  3426. $expected = array(
  3427. 'fieldset' => array(),
  3428. 'legend' => array(),
  3429. 'Field',
  3430. '/legend',
  3431. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3432. array('label' => array('for' => 'ModelField0')),
  3433. 'option A',
  3434. '/label',
  3435. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3436. array('label' => array('for' => 'ModelField1')),
  3437. 'option B',
  3438. '/label',
  3439. '/fieldset'
  3440. );
  3441. $this->assertTags($result, $expected);
  3442. $result = $this->Form->radio(
  3443. 'Model.field',
  3444. array('option A', 'option B'),
  3445. array('disabled' => 'disabled', 'value' => '0')
  3446. );
  3447. $expected = array(
  3448. 'fieldset' => array(),
  3449. 'legend' => array(),
  3450. 'Field',
  3451. '/legend',
  3452. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3453. array('label' => array('for' => 'ModelField0')),
  3454. 'option A',
  3455. '/label',
  3456. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3457. array('label' => array('for' => 'ModelField1')),
  3458. 'option B',
  3459. '/label',
  3460. '/fieldset'
  3461. );
  3462. $this->assertTags($result, $expected);
  3463. $result = $this->Form->input('Model.field', array(
  3464. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3465. 'type' => 'radio',
  3466. 'disabled' => array(2, '3x'),
  3467. ));
  3468. $expected = array(
  3469. 'div' => array('class' => 'input radio'),
  3470. 'fieldset' => array(),
  3471. 'legend' => array(),
  3472. 'Field',
  3473. '/legend',
  3474. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3475. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3476. array('label' => array('for' => 'ModelField1')),
  3477. 'first',
  3478. '/label',
  3479. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3480. array('label' => array('for' => 'ModelField2')),
  3481. 'second',
  3482. '/label',
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3484. array('label' => array('for' => 'ModelField2x')),
  3485. '2x',
  3486. '/label',
  3487. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3488. array('label' => array('for' => 'ModelField3')),
  3489. 'third',
  3490. '/label',
  3491. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3492. array('label' => array('for' => 'ModelField3x')),
  3493. '3x',
  3494. '/label',
  3495. '/fieldset',
  3496. '/div'
  3497. );
  3498. $this->assertTags($result, $expected);
  3499. }
  3500. /**
  3501. * test disabling the hidden input for radio buttons
  3502. *
  3503. * @return void
  3504. */
  3505. public function testRadioHiddenInputDisabling() {
  3506. $result = $this->Form->input('Model.1.field', array(
  3507. 'type' => 'radio',
  3508. 'options' => array('option A'),
  3509. 'hiddenField' => false
  3510. )
  3511. );
  3512. $expected = array(
  3513. 'div' => array('class' => 'input radio'),
  3514. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3515. 'label' => array('for' => 'Model1Field0'),
  3516. 'option A',
  3517. '/label',
  3518. '/div'
  3519. );
  3520. $this->assertTags($result, $expected);
  3521. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3522. $expected = array(
  3523. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3524. 'label' => array('for' => 'Model1Field0'),
  3525. 'option A',
  3526. '/label'
  3527. );
  3528. $this->assertTags($result, $expected);
  3529. }
  3530. /**
  3531. * test adding an empty option for radio buttons
  3532. *
  3533. * @return void
  3534. */
  3535. public function testRadioAddEmptyOption() {
  3536. $result = $this->Form->input('Model.1.field', array(
  3537. 'type' => 'radio',
  3538. 'options' => array('option A'),
  3539. 'empty' => true,
  3540. 'hiddenField' => false
  3541. ));
  3542. $expected = array(
  3543. 'div' => array('class' => 'input radio'),
  3544. 'fieldset' => array(),
  3545. 'legend' => array(),
  3546. 'Field',
  3547. '/legend',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3549. array('label' => array('for' => 'Model1Field')),
  3550. __('empty'),
  3551. '/label',
  3552. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3553. array('label' => array('for' => 'Model1Field0')),
  3554. 'option A',
  3555. '/label',
  3556. '/fieldset',
  3557. '/div'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. $result = $this->Form->input('Model.1.field', array(
  3561. 'type' => 'radio',
  3562. 'options' => array('option A'),
  3563. 'empty' => 'CustomEmptyLabel',
  3564. 'hiddenField' => false
  3565. ));
  3566. $expected = array(
  3567. 'div' => array('class' => 'input radio'),
  3568. 'fieldset' => array(),
  3569. 'legend' => array(),
  3570. 'Field',
  3571. '/legend',
  3572. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3573. array('label' => array('for' => 'Model1Field')),
  3574. 'CustomEmptyLabel',
  3575. '/label',
  3576. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3577. array('label' => array('for' => 'Model1Field0')),
  3578. 'option A',
  3579. '/label',
  3580. '/fieldset',
  3581. '/div'
  3582. );
  3583. $this->assertTags($result, $expected);
  3584. $result = $this->Form->input('Model.1.field', array(
  3585. 'type' => 'radio',
  3586. 'options' => array('option A'),
  3587. 'empty' => false,
  3588. 'hiddenField' => false
  3589. ));
  3590. $this->assertTextNotContains('"Model1Field"', $result);
  3591. }
  3592. /**
  3593. * testSelect method
  3594. *
  3595. * Test select element generation.
  3596. *
  3597. * @return void
  3598. */
  3599. public function testSelect() {
  3600. $result = $this->Form->select('Model.field', array());
  3601. $expected = array(
  3602. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3603. array('option' => array('value' => '')),
  3604. '/option',
  3605. '/select'
  3606. );
  3607. $this->assertTags($result, $expected);
  3608. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3609. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3610. $expected = array(
  3611. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3612. array('option' => array('value' => '')),
  3613. '/option',
  3614. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3615. 'good',
  3616. '/option',
  3617. array('option' => array('value' => 'other')),
  3618. 'bad',
  3619. '/option',
  3620. '/select'
  3621. );
  3622. $this->assertTags($result, $expected);
  3623. $this->Form->request->data = array();
  3624. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3625. $expected = array(
  3626. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3627. array('option' => array('value' => '')),
  3628. '/option',
  3629. array('option' => array('value' => 'value')),
  3630. 'good',
  3631. '/option',
  3632. array('option' => array('value' => 'other')),
  3633. 'bad',
  3634. '/option',
  3635. '/select'
  3636. );
  3637. $this->assertTags($result, $expected);
  3638. $result = $this->Form->select(
  3639. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3640. array('empty' => false)
  3641. );
  3642. $expected = array(
  3643. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3644. array('option' => array('value' => 'first')),
  3645. 'first &quot;html&quot; &lt;chars&gt;',
  3646. '/option',
  3647. array('option' => array('value' => 'second')),
  3648. 'value',
  3649. '/option',
  3650. '/select'
  3651. );
  3652. $this->assertTags($result, $expected);
  3653. $result = $this->Form->select(
  3654. 'Model.field',
  3655. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3656. array('escape' => false, 'empty' => false)
  3657. );
  3658. $expected = array(
  3659. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3660. array('option' => array('value' => 'first')),
  3661. 'first "html" <chars>',
  3662. '/option',
  3663. array('option' => array('value' => 'second')),
  3664. 'value',
  3665. '/option',
  3666. '/select'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. $options = array(
  3670. array('value' => 'first', 'name' => 'First'),
  3671. array('value' => 'first', 'name' => 'Another First'),
  3672. );
  3673. $result = $this->Form->select(
  3674. 'Model.field',
  3675. $options,
  3676. array('escape' => false, 'empty' => false)
  3677. );
  3678. $expected = array(
  3679. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3680. array('option' => array('value' => 'first')),
  3681. 'First',
  3682. '/option',
  3683. array('option' => array('value' => 'first')),
  3684. 'Another First',
  3685. '/option',
  3686. '/select'
  3687. );
  3688. $this->assertTags($result, $expected);
  3689. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3690. $result = $this->Form->select(
  3691. 'Model.contact_id',
  3692. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3693. array('escape' => false, 'empty' => 'pick something')
  3694. );
  3695. $expected = array(
  3696. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3697. array('option' => array('value' => '')), 'pick something', '/option',
  3698. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3699. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3700. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3701. '/select'
  3702. );
  3703. $this->assertTags($result, $expected);
  3704. $this->Form->request->data['Model']['field'] = 0;
  3705. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3706. $expected = array(
  3707. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3708. array('option' => array('value' => '')), '/option',
  3709. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3710. array('option' => array('value' => '1')), 'Yes', '/option',
  3711. '/select'
  3712. );
  3713. $this->assertTags($result, $expected);
  3714. $this->Form->request->data['Model']['field'] = 50;
  3715. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3716. $expected = array(
  3717. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3718. array('option' => array('value' => '')), '/option',
  3719. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3720. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3721. '/select'
  3722. );
  3723. $this->assertTags($result, $expected);
  3724. }
  3725. /**
  3726. * test that select() with optiongroups listens to the escape param.
  3727. *
  3728. * @return void
  3729. */
  3730. public function testSelectOptionGroupEscaping() {
  3731. $options = array(
  3732. '>< Key' => array(
  3733. 1 => 'One',
  3734. 2 => 'Two'
  3735. )
  3736. );
  3737. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3740. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3741. array('option' => array('value' => '1')), 'One', '/option',
  3742. array('option' => array('value' => '2')), 'Two', '/option',
  3743. '/optgroup',
  3744. '/select'
  3745. );
  3746. $this->assertTags($result, $expected);
  3747. $options = array(
  3748. '>< Key' => array(
  3749. 1 => 'One',
  3750. 2 => 'Two'
  3751. )
  3752. );
  3753. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3754. $expected = array(
  3755. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3756. 'optgroup' => array('label' => '>< Key'),
  3757. array('option' => array('value' => '1')), 'One', '/option',
  3758. array('option' => array('value' => '2')), 'Two', '/option',
  3759. '/optgroup',
  3760. '/select'
  3761. );
  3762. $this->assertTags($result, $expected);
  3763. }
  3764. /**
  3765. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3766. *
  3767. * @return void
  3768. */
  3769. public function testSelectWithNullAttributes() {
  3770. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3771. $expected = array(
  3772. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3773. array('option' => array('value' => '0')),
  3774. 'first',
  3775. '/option',
  3776. array('option' => array('value' => '1')),
  3777. 'second',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. }
  3783. /**
  3784. * testNestedSelect method
  3785. *
  3786. * test select element generation with optgroups
  3787. *
  3788. * @return void
  3789. */
  3790. public function testNestedSelect() {
  3791. $result = $this->Form->select(
  3792. 'Model.field',
  3793. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3794. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3795. )), array('empty' => false)
  3796. );
  3797. $expected = array(
  3798. 'select' => array('name' => 'data[Model][field]',
  3799. 'id' => 'ModelField'),
  3800. array('option' => array('value' => 1)),
  3801. 'One',
  3802. '/option',
  3803. array('option' => array('value' => 2)),
  3804. 'Two',
  3805. '/option',
  3806. array('optgroup' => array('label' => 'Three')),
  3807. array('option' => array('value' => 4)),
  3808. 'Four',
  3809. '/option',
  3810. array('option' => array('value' => 5)),
  3811. 'Five',
  3812. '/option',
  3813. '/optgroup',
  3814. '/select'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $result = $this->Form->select(
  3818. 'Model.field',
  3819. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3820. array('showParents' => true, 'empty' => false)
  3821. );
  3822. $expected = array(
  3823. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3824. array('option' => array('value' => 1)),
  3825. 'One',
  3826. '/option',
  3827. array('option' => array('value' => 2)),
  3828. 'Two',
  3829. '/option',
  3830. array('optgroup' => array('label' => 'Three')),
  3831. array('option' => array('value' => 3)),
  3832. 'Three',
  3833. '/option',
  3834. array('option' => array('value' => 4)),
  3835. 'Four',
  3836. '/option',
  3837. '/optgroup',
  3838. '/select'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. }
  3842. /**
  3843. * testSelectMultiple method
  3844. *
  3845. * test generation of multiple select elements
  3846. *
  3847. * @return void
  3848. */
  3849. public function testSelectMultiple() {
  3850. $options = array('first', 'second', 'third');
  3851. $result = $this->Form->select(
  3852. 'Model.multi_field', $options, array('multiple' => true)
  3853. );
  3854. $expected = array(
  3855. 'input' => array(
  3856. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3857. ),
  3858. 'select' => array(
  3859. 'name' => 'data[Model][multi_field][]',
  3860. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3861. ),
  3862. array('option' => array('value' => '0')),
  3863. 'first',
  3864. '/option',
  3865. array('option' => array('value' => '1')),
  3866. 'second',
  3867. '/option',
  3868. array('option' => array('value' => '2')),
  3869. 'third',
  3870. '/option',
  3871. '/select'
  3872. );
  3873. $this->assertTags($result, $expected);
  3874. $result = $this->Form->select(
  3875. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3876. );
  3877. $expected = array(
  3878. 'input' => array(
  3879. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3880. ),
  3881. 'select' => array(
  3882. 'name' => 'data[Model][multi_field][]',
  3883. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3884. ),
  3885. array('option' => array('value' => '0')),
  3886. 'first',
  3887. '/option',
  3888. array('option' => array('value' => '1')),
  3889. 'second',
  3890. '/option',
  3891. array('option' => array('value' => '2')),
  3892. 'third',
  3893. '/option',
  3894. '/select'
  3895. );
  3896. $this->assertTags($result, $expected);
  3897. $result = $this->Form->select(
  3898. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3899. );
  3900. $expected = array(
  3901. 'input' => array(
  3902. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3903. ),
  3904. 'select' => array(
  3905. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3906. 'multiple' => 'multiple'
  3907. ),
  3908. array('option' => array('value' => '0', 'selected' => 'selected')),
  3909. 'first',
  3910. '/option',
  3911. array('option' => array('value' => '1', 'selected' => 'selected')),
  3912. 'second',
  3913. '/option',
  3914. array('option' => array('value' => '2')),
  3915. 'third',
  3916. '/option',
  3917. '/select'
  3918. );
  3919. $this->assertTags($result, $expected);
  3920. $result = $this->Form->select(
  3921. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3922. );
  3923. $expected = array(
  3924. 'select' => array(
  3925. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3926. ),
  3927. array('option' => array('value' => '0', 'selected' => 'selected')),
  3928. 'first',
  3929. '/option',
  3930. array('option' => array('value' => '1', 'selected' => 'selected')),
  3931. 'second',
  3932. '/option',
  3933. array('option' => array('value' => '2')),
  3934. 'third',
  3935. '/option',
  3936. '/select'
  3937. );
  3938. $this->assertTags($result, $expected);
  3939. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  3940. $selected = array('2', '3x');
  3941. $result = $this->Form->select(
  3942. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  3943. );
  3944. $expected = array(
  3945. 'input' => array(
  3946. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3947. ),
  3948. 'select' => array(
  3949. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  3950. ),
  3951. array('option' => array('value' => '1')),
  3952. 'One',
  3953. '/option',
  3954. array('option' => array('value' => '2', 'selected' => 'selected')),
  3955. 'Two',
  3956. '/option',
  3957. array('option' => array('value' => '3')),
  3958. 'Three',
  3959. '/option',
  3960. array('option' => array('value' => '3x', 'selected' => 'selected')),
  3961. 'Stringy',
  3962. '/option',
  3963. '/select'
  3964. );
  3965. $this->assertTags($result, $expected);
  3966. }
  3967. /**
  3968. * Test generating multiple select with disabled elements.
  3969. *
  3970. * @return void
  3971. */
  3972. public function testSelectMultipleWithDisabledElements() {
  3973. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  3974. $disabled = array(2, 3);
  3975. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  3976. $expected = array(
  3977. 'input' => array(
  3978. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  3979. ),
  3980. 'select' => array(
  3981. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  3982. ),
  3983. array('option' => array('value' => '1')),
  3984. 'One',
  3985. '/option',
  3986. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  3987. 'Two',
  3988. '/option',
  3989. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  3990. 'Three',
  3991. '/option',
  3992. array('option' => array('value' => '3x')),
  3993. 'Stringy',
  3994. '/option',
  3995. '/select'
  3996. );
  3997. $this->assertTags($result, $expected);
  3998. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  3999. $disabled = array('2', '3x');
  4000. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4001. $expected = array(
  4002. array('div' => array('class' => 'input select')),
  4003. array('label' => array('for' => 'ContactMultiple')),
  4004. 'Multiple',
  4005. '/label',
  4006. 'input' => array(
  4007. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4008. ),
  4009. 'select' => array(
  4010. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4011. ),
  4012. array('option' => array('value' => '1')),
  4013. 'One',
  4014. '/option',
  4015. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4016. 'Two',
  4017. '/option',
  4018. array('option' => array('value' => '3')),
  4019. 'Three',
  4020. '/option',
  4021. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4022. 'Stringy',
  4023. '/option',
  4024. '/select',
  4025. '/div'
  4026. );
  4027. $this->assertTags($result, $expected);
  4028. }
  4029. /**
  4030. * test generation of habtm select boxes.
  4031. *
  4032. * @return void
  4033. */
  4034. public function testHabtmSelectBox() {
  4035. $this->View->viewVars['contactTags'] = array(
  4036. 1 => 'blue',
  4037. 2 => 'red',
  4038. 3 => 'green'
  4039. );
  4040. $this->Form->request->data = array(
  4041. 'Contact' => array(),
  4042. 'ContactTag' => array(
  4043. array(
  4044. 'id' => '1',
  4045. 'name' => 'blue'
  4046. ),
  4047. array(
  4048. 'id' => 3,
  4049. 'name' => 'green'
  4050. )
  4051. )
  4052. );
  4053. $this->Form->create('Contact');
  4054. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4055. $expected = array(
  4056. 'input' => array(
  4057. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4058. ),
  4059. 'select' => array(
  4060. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4061. 'multiple' => 'multiple'
  4062. ),
  4063. array('option' => array('value' => '1', 'selected' => 'selected')),
  4064. 'blue',
  4065. '/option',
  4066. array('option' => array('value' => '2')),
  4067. 'red',
  4068. '/option',
  4069. array('option' => array('value' => '3', 'selected' => 'selected')),
  4070. 'green',
  4071. '/option',
  4072. '/select'
  4073. );
  4074. $this->assertTags($result, $expected);
  4075. // make sure only 50 is selected, and not 50f5c0cf
  4076. $this->View->viewVars['contactTags'] = array(
  4077. '1' => 'blue',
  4078. '50f5c0cf' => 'red',
  4079. '50' => 'green'
  4080. );
  4081. $this->Form->request->data = array(
  4082. 'Contact' => array(),
  4083. 'ContactTag' => array(
  4084. array(
  4085. 'id' => 1,
  4086. 'name' => 'blue'
  4087. ),
  4088. array(
  4089. 'id' => 50,
  4090. 'name' => 'green'
  4091. )
  4092. )
  4093. );
  4094. $this->Form->create('Contact');
  4095. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4096. $expected = array(
  4097. 'input' => array(
  4098. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4099. ),
  4100. 'select' => array(
  4101. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4102. 'multiple' => 'multiple'
  4103. ),
  4104. array('option' => array('value' => '1', 'selected' => 'selected')),
  4105. 'blue',
  4106. '/option',
  4107. array('option' => array('value' => '50f5c0cf')),
  4108. 'red',
  4109. '/option',
  4110. array('option' => array('value' => '50', 'selected' => 'selected')),
  4111. 'green',
  4112. '/option',
  4113. '/select'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. }
  4117. /**
  4118. * test generation of multi select elements in checkbox format
  4119. *
  4120. * @return void
  4121. */
  4122. public function testSelectMultipleCheckboxes() {
  4123. $result = $this->Form->select(
  4124. 'Model.multi_field',
  4125. array('first', 'second', 'third'),
  4126. array('multiple' => 'checkbox')
  4127. );
  4128. $expected = array(
  4129. 'input' => array(
  4130. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4131. ),
  4132. array('div' => array('class' => 'checkbox')),
  4133. array('input' => array(
  4134. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4135. 'value' => '0', 'id' => 'ModelMultiField0'
  4136. )),
  4137. array('label' => array('for' => 'ModelMultiField0')),
  4138. 'first',
  4139. '/label',
  4140. '/div',
  4141. array('div' => array('class' => 'checkbox')),
  4142. array('input' => array(
  4143. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4144. 'value' => '1', 'id' => 'ModelMultiField1'
  4145. )),
  4146. array('label' => array('for' => 'ModelMultiField1')),
  4147. 'second',
  4148. '/label',
  4149. '/div',
  4150. array('div' => array('class' => 'checkbox')),
  4151. array('input' => array(
  4152. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4153. 'value' => '2', 'id' => 'ModelMultiField2'
  4154. )),
  4155. array('label' => array('for' => 'ModelMultiField2')),
  4156. 'third',
  4157. '/label',
  4158. '/div'
  4159. );
  4160. $this->assertTags($result, $expected);
  4161. $result = $this->Form->select(
  4162. 'Model.multi_field',
  4163. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4164. array('multiple' => 'checkbox')
  4165. );
  4166. $expected = array(
  4167. 'input' => array(
  4168. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4169. ),
  4170. array('div' => array('class' => 'checkbox')),
  4171. array('input' => array(
  4172. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4173. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4174. )),
  4175. array('label' => array('for' => 'ModelMultiFieldA')),
  4176. 'first',
  4177. '/label',
  4178. '/div',
  4179. array('div' => array('class' => 'checkbox')),
  4180. array('input' => array(
  4181. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4182. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4183. )),
  4184. array('label' => array('for' => 'ModelMultiFieldB')),
  4185. 'second',
  4186. '/label',
  4187. '/div',
  4188. array('div' => array('class' => 'checkbox')),
  4189. array('input' => array(
  4190. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4191. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4192. )),
  4193. array('label' => array('for' => 'ModelMultiFieldC')),
  4194. 'third',
  4195. '/label',
  4196. '/div'
  4197. );
  4198. $this->assertTags($result, $expected);
  4199. $result = $this->Form->select(
  4200. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4201. );
  4202. $expected = array(
  4203. 'input' => array(
  4204. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4205. ),
  4206. array('div' => array('class' => 'checkbox')),
  4207. array('input' => array(
  4208. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4209. 'value' => '1', 'id' => 'ModelMultiField1'
  4210. )),
  4211. array('label' => array('for' => 'ModelMultiField1')),
  4212. 'first',
  4213. '/label',
  4214. '/div'
  4215. );
  4216. $this->assertTags($result, $expected);
  4217. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4218. $result = $this->Form->select(
  4219. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4220. );
  4221. $expected = array(
  4222. 'input' => array(
  4223. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4224. ),
  4225. array('div' => array('class' => 'checkbox')),
  4226. array('input' => array(
  4227. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4228. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4229. )),
  4230. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4231. 'first',
  4232. '/label',
  4233. '/div',
  4234. array('div' => array('class' => 'checkbox')),
  4235. array('input' => array(
  4236. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4237. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4238. )),
  4239. array('label' => array('for' => 'ModelTagsArray')),
  4240. 'Array',
  4241. '/label',
  4242. '/div'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. }
  4246. /**
  4247. * test multiple checkboxes with div styles.
  4248. *
  4249. * @return void
  4250. */
  4251. public function testSelectMultipleCheckboxDiv() {
  4252. $result = $this->Form->select(
  4253. 'Model.tags',
  4254. array('first', 'second'),
  4255. array('multiple' => 'checkbox', 'class' => 'my-class')
  4256. );
  4257. $expected = array(
  4258. 'input' => array(
  4259. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4260. ),
  4261. array('div' => array('class' => 'my-class')),
  4262. array('input' => array(
  4263. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4264. 'value' => '0', 'id' => 'ModelTags0'
  4265. )),
  4266. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4267. '/div',
  4268. array('div' => array('class' => 'my-class')),
  4269. array('input' => array(
  4270. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4271. 'value' => '1', 'id' => 'ModelTags1'
  4272. )),
  4273. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4274. '/div'
  4275. );
  4276. $this->assertTags($result, $expected);
  4277. $result = $this->Form->input('Model.tags', array(
  4278. 'options' => array('first', 'second'),
  4279. 'multiple' => 'checkbox',
  4280. 'class' => 'my-class',
  4281. 'div' => false,
  4282. 'label' => false
  4283. ));
  4284. $this->assertTags($result, $expected);
  4285. $Contact = ClassRegistry::getObject('Contact');
  4286. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4287. $result = $this->Form->input('Contact.tags', array(
  4288. 'options' => array('one'),
  4289. 'multiple' => 'checkbox',
  4290. 'label' => false,
  4291. 'div' => false
  4292. ));
  4293. $expected = array(
  4294. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4295. array('div' => array('class' => 'checkbox form-error')),
  4296. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4297. array('label' => array('for' => 'ContactTags0')),
  4298. 'one',
  4299. '/label',
  4300. '/div'
  4301. );
  4302. $this->assertTags($result, $expected);
  4303. $result = $this->Form->input('Contact.tags', array(
  4304. 'options' => array('one'),
  4305. 'multiple' => 'checkbox',
  4306. 'class' => 'mycheckbox',
  4307. 'label' => false,
  4308. 'div' => false
  4309. ));
  4310. $expected = array(
  4311. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4312. array('div' => array('class' => 'mycheckbox form-error')),
  4313. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4314. array('label' => array('for' => 'ContactTags0')),
  4315. 'one',
  4316. '/label',
  4317. '/div'
  4318. );
  4319. $this->assertTags($result, $expected);
  4320. }
  4321. /**
  4322. * Checks the security hash array generated for multiple-input checkbox elements
  4323. *
  4324. * @return void
  4325. */
  4326. public function testSelectMultipleCheckboxSecurity() {
  4327. $this->Form->request['_Token'] = array('key' => 'testKey');
  4328. $this->assertEquals(array(), $this->Form->fields);
  4329. $result = $this->Form->select(
  4330. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4331. array('multiple' => 'checkbox')
  4332. );
  4333. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4334. $result = $this->Form->secure($this->Form->fields);
  4335. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4336. $this->assertRegExp('/"' . $key . '"/', $result);
  4337. }
  4338. /**
  4339. * Multiple select elements should always be secured as they always participate
  4340. * in the POST data.
  4341. *
  4342. * @return void
  4343. */
  4344. public function testSelectMultipleSecureWithNoOptions() {
  4345. $this->Form->request['_Token'] = array('key' => 'testkey');
  4346. $this->assertEquals(array(), $this->Form->fields);
  4347. $this->Form->select(
  4348. 'Model.select',
  4349. array(),
  4350. array('multiple' => true)
  4351. );
  4352. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4353. }
  4354. /**
  4355. * When a select box has no options it should not be added to the fields list
  4356. * as it always fail post validation.
  4357. *
  4358. * @return void
  4359. */
  4360. public function testSelectNoSecureWithNoOptions() {
  4361. $this->Form->request['_Token'] = array('key' => 'testkey');
  4362. $this->assertEquals(array(), $this->Form->fields);
  4363. $this->Form->select(
  4364. 'Model.select',
  4365. array()
  4366. );
  4367. $this->assertEquals(array(), $this->Form->fields);
  4368. $this->Form->select(
  4369. 'Model.select',
  4370. array(),
  4371. array('empty' => true)
  4372. );
  4373. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4374. }
  4375. /**
  4376. * testInputMultipleCheckboxes method
  4377. *
  4378. * test input() resulting in multi select elements being generated.
  4379. *
  4380. * @return void
  4381. */
  4382. public function testInputMultipleCheckboxes() {
  4383. $result = $this->Form->input('Model.multi_field', array(
  4384. 'options' => array('first', 'second', 'third'),
  4385. 'multiple' => 'checkbox'
  4386. ));
  4387. $expected = array(
  4388. array('div' => array('class' => 'input select')),
  4389. array('label' => array('for' => 'ModelMultiField')),
  4390. 'Multi Field',
  4391. '/label',
  4392. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4393. array('div' => array('class' => 'checkbox')),
  4394. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4395. array('label' => array('for' => 'ModelMultiField0')),
  4396. 'first',
  4397. '/label',
  4398. '/div',
  4399. array('div' => array('class' => 'checkbox')),
  4400. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4401. array('label' => array('for' => 'ModelMultiField1')),
  4402. 'second',
  4403. '/label',
  4404. '/div',
  4405. array('div' => array('class' => 'checkbox')),
  4406. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4407. array('label' => array('for' => 'ModelMultiField2')),
  4408. 'third',
  4409. '/label',
  4410. '/div',
  4411. '/div'
  4412. );
  4413. $this->assertTags($result, $expected);
  4414. $result = $this->Form->input('Model.multi_field', array(
  4415. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4416. 'multiple' => 'checkbox'
  4417. ));
  4418. $expected = array(
  4419. array('div' => array('class' => 'input select')),
  4420. array('label' => array('for' => 'ModelMultiField')),
  4421. 'Multi Field',
  4422. '/label',
  4423. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4424. array('div' => array('class' => 'checkbox')),
  4425. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4426. array('label' => array('for' => 'ModelMultiFieldA')),
  4427. 'first',
  4428. '/label',
  4429. '/div',
  4430. array('div' => array('class' => 'checkbox')),
  4431. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4432. array('label' => array('for' => 'ModelMultiFieldB')),
  4433. 'second',
  4434. '/label',
  4435. '/div',
  4436. array('div' => array('class' => 'checkbox')),
  4437. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4438. array('label' => array('for' => 'ModelMultiFieldC')),
  4439. 'third',
  4440. '/label',
  4441. '/div',
  4442. '/div'
  4443. );
  4444. $this->assertTags($result, $expected);
  4445. $result = $this->Form->input('Model.multi_field', array(
  4446. 'options' => array('1' => 'first'),
  4447. 'multiple' => 'checkbox',
  4448. 'label' => false,
  4449. 'div' => false
  4450. ));
  4451. $expected = array(
  4452. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4453. array('div' => array('class' => 'checkbox')),
  4454. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4455. array('label' => array('for' => 'ModelMultiField1')),
  4456. 'first',
  4457. '/label',
  4458. '/div'
  4459. );
  4460. $this->assertTags($result, $expected);
  4461. $result = $this->Form->input('Model.multi_field', array(
  4462. 'options' => array('2' => 'second'),
  4463. 'multiple' => 'checkbox',
  4464. 'label' => false,
  4465. 'div' => false
  4466. ));
  4467. $expected = array(
  4468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4469. array('div' => array('class' => 'checkbox')),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4471. array('label' => array('for' => 'ModelMultiField2')),
  4472. 'second',
  4473. '/label',
  4474. '/div'
  4475. );
  4476. $this->assertTags($result, $expected);
  4477. }
  4478. /**
  4479. * testSelectHiddenFieldOmission method
  4480. *
  4481. * test that select() with 'hiddenField' => false omits the hidden field
  4482. *
  4483. * @return void
  4484. */
  4485. public function testSelectHiddenFieldOmission() {
  4486. $result = $this->Form->select('Model.multi_field',
  4487. array('first', 'second'),
  4488. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4489. );
  4490. $expected = array(
  4491. array('div' => array('class' => 'checkbox')),
  4492. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4493. array('label' => array('for' => 'ModelMultiField0')),
  4494. 'first',
  4495. '/label',
  4496. '/div',
  4497. array('div' => array('class' => 'checkbox')),
  4498. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4499. array('label' => array('for' => 'ModelMultiField1')),
  4500. 'second',
  4501. '/label',
  4502. '/div'
  4503. );
  4504. $this->assertTags($result, $expected);
  4505. $result = $this->Form->input('Model.multi_field', array(
  4506. 'options' => array('first', 'second'),
  4507. 'multiple' => 'checkbox',
  4508. 'hiddenField' => false
  4509. ));
  4510. $expected = array(
  4511. array('div' => array('class' => 'input select')),
  4512. array('label' => array('for' => 'ModelMultiField')),
  4513. 'Multi Field',
  4514. '/label',
  4515. array('div' => array('class' => 'checkbox')),
  4516. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4517. array('label' => array('for' => 'ModelMultiField0')),
  4518. 'first',
  4519. '/label',
  4520. '/div',
  4521. array('div' => array('class' => 'checkbox')),
  4522. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4523. array('label' => array('for' => 'ModelMultiField1')),
  4524. 'second',
  4525. '/label',
  4526. '/div',
  4527. '/div'
  4528. );
  4529. $this->assertTags($result, $expected);
  4530. }
  4531. /**
  4532. * test that select() with multiple = checkbox works with overriding name attribute.
  4533. *
  4534. * @return void
  4535. */
  4536. public function testSelectCheckboxMultipleOverrideName() {
  4537. $result = $this->Form->input('category', array(
  4538. 'type' => 'select',
  4539. 'multiple' => 'checkbox',
  4540. 'name' => 'data[fish]',
  4541. 'options' => array('1', '2'),
  4542. 'div' => false,
  4543. 'label' => false,
  4544. ));
  4545. $expected = array(
  4546. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4547. array('div' => array('class' => 'checkbox')),
  4548. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4549. array('label' => array('for' => 'Category0')), '1', '/label',
  4550. '/div',
  4551. array('div' => array('class' => 'checkbox')),
  4552. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4553. array('label' => array('for' => 'Category1')), '2', '/label',
  4554. '/div'
  4555. );
  4556. $this->assertTags($result, $expected);
  4557. }
  4558. /**
  4559. * Test that 'id' overrides all the checkbox id's as well.
  4560. *
  4561. * @return void
  4562. */
  4563. public function testSelectCheckboxMultipleId() {
  4564. $result = $this->Form->select(
  4565. 'Model.multi_field',
  4566. array('first', 'second', 'third'),
  4567. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4568. );
  4569. $expected = array(
  4570. 'input' => array(
  4571. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4572. ),
  4573. array('div' => array('class' => 'checkbox')),
  4574. array('input' => array(
  4575. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4576. 'value' => '0', 'id' => 'CustomId0'
  4577. )),
  4578. array('label' => array('for' => 'CustomId0')),
  4579. 'first',
  4580. '/label',
  4581. '/div',
  4582. array('div' => array('class' => 'checkbox')),
  4583. array('input' => array(
  4584. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4585. 'value' => '1', 'id' => 'CustomId1'
  4586. )),
  4587. array('label' => array('for' => 'CustomId1')),
  4588. 'second',
  4589. '/label',
  4590. '/div',
  4591. array('div' => array('class' => 'checkbox')),
  4592. array('input' => array(
  4593. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4594. 'value' => '2', 'id' => 'CustomId2'
  4595. )),
  4596. array('label' => array('for' => 'CustomId2')),
  4597. 'third',
  4598. '/label',
  4599. '/div'
  4600. );
  4601. $this->assertTags($result, $expected);
  4602. }
  4603. /**
  4604. * testCheckbox method
  4605. *
  4606. * Test generation of checkboxes
  4607. *
  4608. * @return void
  4609. */
  4610. public function testCheckbox() {
  4611. $result = $this->Form->checkbox('Model.field');
  4612. $expected = array(
  4613. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4614. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4615. );
  4616. $this->assertTags($result, $expected);
  4617. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4618. $expected = array(
  4619. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4620. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4621. );
  4622. $this->assertTags($result, $expected);
  4623. $Contact = ClassRegistry::getObject('Contact');
  4624. $Contact->validationErrors['field'] = 1;
  4625. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4626. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4627. $expected = array(
  4628. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4629. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4630. );
  4631. $this->assertTags($result, $expected);
  4632. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4633. $expected = array(
  4634. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4635. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4636. );
  4637. $this->assertTags($result, $expected);
  4638. $this->Form->request->data['Contact']['field'] = '';
  4639. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4640. $expected = array(
  4641. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4642. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4643. );
  4644. $this->assertTags($result, $expected);
  4645. $Contact->validationErrors = array();
  4646. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4647. $expected = array(
  4648. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4649. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4650. );
  4651. $this->assertTags($result, $expected);
  4652. $this->Form->request->data['Contact']['published'] = 1;
  4653. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4654. $expected = array(
  4655. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4656. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4657. );
  4658. $this->assertTags($result, $expected);
  4659. $this->Form->request->data['Contact']['published'] = 0;
  4660. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4661. $expected = array(
  4662. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4663. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4664. );
  4665. $this->assertTags($result, $expected);
  4666. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4667. $expected = array(
  4668. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4669. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4670. );
  4671. $this->assertTags($result, $expected);
  4672. $result = $this->Form->checkbox('CustomField.1.value');
  4673. $expected = array(
  4674. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4675. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. }
  4679. /**
  4680. * test checkbox() with a custom name attribute
  4681. *
  4682. * @return void
  4683. */
  4684. public function testCheckboxCustomNameAttribute() {
  4685. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4686. $expected = array(
  4687. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4688. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4689. );
  4690. $this->assertTags($result, $expected);
  4691. }
  4692. /**
  4693. * test the checked option for checkboxes.
  4694. *
  4695. * @return void
  4696. */
  4697. public function testCheckboxCheckedOption() {
  4698. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4699. $expected = array(
  4700. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4701. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4702. );
  4703. $this->assertTags($result, $expected);
  4704. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4705. $expected = array(
  4706. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4707. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4708. );
  4709. $this->assertTags($result, $expected);
  4710. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4711. $expected = array(
  4712. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4713. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4714. );
  4715. $this->assertTags($result, $expected);
  4716. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4717. $expected = array(
  4718. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4719. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4720. );
  4721. $this->assertTags($result, $expected);
  4722. $this->Form->request->data['Model']['field'] = 1;
  4723. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4724. $expected = array(
  4725. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4726. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4727. );
  4728. $this->assertTags($result, $expected);
  4729. }
  4730. /**
  4731. * Test that disabled attribute works on both the checkbox and hidden input.
  4732. *
  4733. * @return void
  4734. */
  4735. public function testCheckboxDisabling() {
  4736. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4737. $expected = array(
  4738. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4739. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4740. );
  4741. $this->assertTags($result, $expected);
  4742. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4743. $expected = array(
  4744. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4745. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4746. );
  4747. $this->assertTags($result, $expected);
  4748. }
  4749. /**
  4750. * Test that the hidden input for checkboxes can be omitted or set to a
  4751. * specific value.
  4752. *
  4753. * @return void
  4754. */
  4755. public function testCheckboxHiddenField() {
  4756. $result = $this->Form->input('UserForm.something', array(
  4757. 'type' => 'checkbox',
  4758. 'hiddenField' => false
  4759. ));
  4760. $expected = array(
  4761. 'div' => array('class' => 'input checkbox'),
  4762. array('input' => array(
  4763. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4764. 'value' => '1', 'id' => 'UserFormSomething'
  4765. )),
  4766. 'label' => array('for' => 'UserFormSomething'),
  4767. 'Something',
  4768. '/label',
  4769. '/div'
  4770. );
  4771. $this->assertTags($result, $expected);
  4772. $result = $this->Form->input('UserForm.something', array(
  4773. 'type' => 'checkbox',
  4774. 'value' => 'Y',
  4775. 'hiddenField' => 'N',
  4776. ));
  4777. $expected = array(
  4778. 'div' => array('class' => 'input checkbox'),
  4779. array('input' => array(
  4780. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4781. 'value' => 'N', 'id' => 'UserFormSomething_'
  4782. )),
  4783. array('input' => array(
  4784. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4785. 'value' => 'Y', 'id' => 'UserFormSomething'
  4786. )),
  4787. 'label' => array('for' => 'UserFormSomething'),
  4788. 'Something',
  4789. '/label',
  4790. '/div'
  4791. );
  4792. $this->assertTags($result, $expected);
  4793. }
  4794. /**
  4795. * testDateTime method
  4796. *
  4797. * Test generation of date/time select elements
  4798. *
  4799. * @return void
  4800. */
  4801. public function testDateTime() {
  4802. extract($this->dateRegex);
  4803. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4804. $now = strtotime('now');
  4805. $expected = array(
  4806. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4807. $daysRegex,
  4808. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4809. date('j', $now),
  4810. '/option',
  4811. '*/select',
  4812. '-',
  4813. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4814. $monthsRegex,
  4815. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4816. date('F', $now),
  4817. '/option',
  4818. '*/select',
  4819. '-',
  4820. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4821. $yearsRegex,
  4822. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4823. date('Y', $now),
  4824. '/option',
  4825. '*/select',
  4826. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4827. $hoursRegex,
  4828. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4829. date('g', $now),
  4830. '/option',
  4831. '*/select',
  4832. ':',
  4833. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4834. $minutesRegex,
  4835. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4836. date('i', $now),
  4837. '/option',
  4838. '*/select',
  4839. ' ',
  4840. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4841. $meridianRegex,
  4842. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4843. date('a', $now),
  4844. '/option',
  4845. '*/select'
  4846. );
  4847. $this->assertTags($result, $expected);
  4848. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4849. $expected = array(
  4850. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4851. $daysRegex,
  4852. array('option' => array('value' => '')),
  4853. '/option',
  4854. '*/select',
  4855. '-',
  4856. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4857. $monthsRegex,
  4858. array('option' => array('value' => '')),
  4859. '/option',
  4860. '*/select',
  4861. '-',
  4862. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4863. $yearsRegex,
  4864. array('option' => array('value' => '')),
  4865. '/option',
  4866. '*/select',
  4867. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4868. $hoursRegex,
  4869. array('option' => array('value' => '')),
  4870. '/option',
  4871. '*/select',
  4872. ':',
  4873. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4874. $minutesRegex,
  4875. array('option' => array('value' => '')),
  4876. '/option',
  4877. '*/select',
  4878. ' ',
  4879. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4880. $meridianRegex,
  4881. array('option' => array('value' => '')),
  4882. '/option',
  4883. '*/select'
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4887. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4888. $this->assertTags($result, $expected);
  4889. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4890. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4891. $this->assertTags($result, $expected);
  4892. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4893. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4894. $expected = array(
  4895. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4896. $daysRegex,
  4897. array('option' => array('value' => '')),
  4898. '/option',
  4899. '*/select',
  4900. '-',
  4901. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4902. $monthsRegex,
  4903. array('option' => array('value' => '')),
  4904. '/option',
  4905. '*/select',
  4906. '-',
  4907. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4908. $yearsRegex,
  4909. array('option' => array('value' => '')),
  4910. '/option',
  4911. '*/select',
  4912. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4913. $hoursRegex,
  4914. array('option' => array('value' => '')),
  4915. '/option',
  4916. '*/select',
  4917. ':',
  4918. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4919. $minutesRegex,
  4920. array('option' => array('value' => '')),
  4921. '/option',
  4922. array('option' => array('value' => '00')),
  4923. '00',
  4924. '/option',
  4925. array('option' => array('value' => '05')),
  4926. '05',
  4927. '/option',
  4928. array('option' => array('value' => '10')),
  4929. '10',
  4930. '/option',
  4931. '*/select',
  4932. ' ',
  4933. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4934. $meridianRegex,
  4935. array('option' => array('value' => '')),
  4936. '/option',
  4937. '*/select'
  4938. );
  4939. $this->assertTags($result, $expected);
  4940. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4941. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4942. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4943. $this->Form->request->data['Contact']['data'] = null;
  4944. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4945. $expected = array(
  4946. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4947. $daysRegex,
  4948. array('option' => array('value' => '')),
  4949. '/option',
  4950. '*/select',
  4951. '-',
  4952. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4953. $monthsRegex,
  4954. array('option' => array('value' => '')),
  4955. '/option',
  4956. '*/select',
  4957. '-',
  4958. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4959. $yearsRegex,
  4960. array('option' => array('value' => '')),
  4961. '/option',
  4962. '*/select',
  4963. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4964. $hoursRegex,
  4965. array('option' => array('value' => '')),
  4966. '/option',
  4967. '*/select',
  4968. ':',
  4969. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4970. $minutesRegex,
  4971. array('option' => array('value' => '')),
  4972. '/option',
  4973. '*/select',
  4974. ' ',
  4975. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4976. $meridianRegex,
  4977. array('option' => array('value' => '')),
  4978. '/option',
  4979. '*/select'
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4983. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4984. $now = strtotime($this->Form->data['Model']['field']);
  4985. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4986. $expected = array(
  4987. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4988. $daysRegex,
  4989. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4990. date('j', $now),
  4991. '/option',
  4992. '*/select',
  4993. '-',
  4994. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4995. $monthsRegex,
  4996. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4997. date('F', $now),
  4998. '/option',
  4999. '*/select',
  5000. '-',
  5001. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5002. $yearsRegex,
  5003. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5004. date('Y', $now),
  5005. '/option',
  5006. '*/select',
  5007. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5008. $hoursRegex,
  5009. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5010. date('g', $now),
  5011. '/option',
  5012. '*/select',
  5013. ':',
  5014. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5015. $minutesRegex,
  5016. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5017. date('i', $now),
  5018. '/option',
  5019. '*/select',
  5020. ' ',
  5021. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5022. $meridianRegex,
  5023. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5024. date('a', $now),
  5025. '/option',
  5026. '*/select'
  5027. );
  5028. $this->assertTags($result, $expected);
  5029. $selected = strtotime('2008-10-26 12:33:00');
  5030. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5031. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5032. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5033. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5034. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5035. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5036. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5037. $this->Form->create('Contact');
  5038. $result = $this->Form->input('published');
  5039. $now = strtotime('now');
  5040. $expected = array(
  5041. 'div' => array('class' => 'input date'),
  5042. 'label' => array('for' => 'ContactPublishedMonth'),
  5043. 'Published',
  5044. '/label',
  5045. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5046. $monthsRegex,
  5047. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5048. date('F', $now),
  5049. '/option',
  5050. '*/select',
  5051. '-',
  5052. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5053. $daysRegex,
  5054. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5055. date('j', $now),
  5056. '/option',
  5057. '*/select',
  5058. '-',
  5059. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5060. $yearsRegex,
  5061. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5062. date('Y', $now),
  5063. '/option',
  5064. '*/select',
  5065. '/div'
  5066. );
  5067. $this->assertTags($result, $expected);
  5068. $result = $this->Form->input('published2', array('type' => 'date'));
  5069. $now = strtotime('now');
  5070. $expected = array(
  5071. 'div' => array('class' => 'input date'),
  5072. 'label' => array('for' => 'ContactPublished2Month'),
  5073. 'Published2',
  5074. '/label',
  5075. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5076. $monthsRegex,
  5077. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5078. date('F', $now),
  5079. '/option',
  5080. '*/select',
  5081. '-',
  5082. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5083. $daysRegex,
  5084. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5085. date('j', $now),
  5086. '/option',
  5087. '*/select',
  5088. '-',
  5089. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5090. $yearsRegex,
  5091. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5092. date('Y', $now),
  5093. '/option',
  5094. '*/select',
  5095. '/div'
  5096. );
  5097. $this->assertTags($result, $expected);
  5098. $this->Form->create('Contact');
  5099. $result = $this->Form->input('published', array('monthNames' => false));
  5100. $now = strtotime('now');
  5101. $expected = array(
  5102. 'div' => array('class' => 'input date'),
  5103. 'label' => array('for' => 'ContactPublishedMonth'),
  5104. 'Published',
  5105. '/label',
  5106. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5107. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5108. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5109. date('m', $now),
  5110. '/option',
  5111. '*/select',
  5112. '-',
  5113. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5114. $daysRegex,
  5115. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5116. date('j', $now),
  5117. '/option',
  5118. '*/select',
  5119. '-',
  5120. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5121. $yearsRegex,
  5122. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5123. date('Y', $now),
  5124. '/option',
  5125. '*/select',
  5126. '/div'
  5127. );
  5128. $this->assertTags($result, $expected);
  5129. $result = $this->Form->input('published', array(
  5130. 'timeFormat' => 24,
  5131. 'interval' => 5,
  5132. 'selected' => strtotime('2009-09-03 13:37:00'),
  5133. 'type' => 'datetime'
  5134. ));
  5135. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5136. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5137. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5138. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5139. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5140. }
  5141. /**
  5142. * Test that empty values don't trigger errors.
  5143. *
  5144. * @return void
  5145. */
  5146. public function testDateTimeNoErrorsOnEmptyData() {
  5147. $this->Form->request->data['Contact'] = array(
  5148. 'date' => array(
  5149. 'day' => '',
  5150. 'month' => '',
  5151. 'year' => '',
  5152. 'hour' => '',
  5153. 'min' => '',
  5154. 'meridian' => ''
  5155. )
  5156. );
  5157. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5158. $this->assertNotEmpty($result);
  5159. }
  5160. /**
  5161. * test that datetime() and default values work.
  5162. *
  5163. * @return void
  5164. */
  5165. public function testDatetimeWithDefault() {
  5166. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5167. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5168. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5169. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5170. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5171. 'default' => '2009-06-01 11:15:30'
  5172. ));
  5173. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5174. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5175. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5176. }
  5177. /**
  5178. * test that bogus non-date time data doesn't cause errors.
  5179. *
  5180. * @return void
  5181. */
  5182. public function testDateTimeWithBogusData() {
  5183. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5184. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5185. }
  5186. /**
  5187. * testDateTimeEmptyAsArray
  5188. *
  5189. * @return void
  5190. */
  5191. public function testDateTimeEmptyAsArray() {
  5192. $result = $this->Form->dateTime('Contact.date',
  5193. 'DMY',
  5194. '12',
  5195. array(
  5196. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5197. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5198. )
  5199. )
  5200. );
  5201. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5202. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5203. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5204. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5205. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5206. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5207. $result = $this->Form->dateTime('Contact.date',
  5208. 'DMY',
  5209. '12',
  5210. array(
  5211. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5212. )
  5213. );
  5214. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5215. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5216. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5217. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5218. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5219. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5220. }
  5221. /**
  5222. * testFormDateTimeMulti method
  5223. *
  5224. * test multiple datetime element generation
  5225. *
  5226. * @return void
  5227. */
  5228. public function testFormDateTimeMulti() {
  5229. extract($this->dateRegex);
  5230. $result = $this->Form->dateTime('Contact.1.updated');
  5231. $expected = array(
  5232. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5233. $daysRegex,
  5234. array('option' => array('value' => '')),
  5235. '/option',
  5236. '*/select',
  5237. '-',
  5238. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5239. $monthsRegex,
  5240. array('option' => array('value' => '')),
  5241. '/option',
  5242. '*/select',
  5243. '-',
  5244. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5245. $yearsRegex,
  5246. array('option' => array('value' => '')),
  5247. '/option',
  5248. '*/select',
  5249. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5250. $hoursRegex,
  5251. array('option' => array('value' => '')),
  5252. '/option',
  5253. '*/select',
  5254. ':',
  5255. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5256. $minutesRegex,
  5257. array('option' => array('value' => '')),
  5258. '/option',
  5259. '*/select',
  5260. ' ',
  5261. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5262. $meridianRegex,
  5263. array('option' => array('value' => '')),
  5264. '/option',
  5265. '*/select'
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $result = $this->Form->dateTime('Contact.2.updated');
  5269. $expected = array(
  5270. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5271. $daysRegex,
  5272. array('option' => array('value' => '')),
  5273. '/option',
  5274. '*/select',
  5275. '-',
  5276. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5277. $monthsRegex,
  5278. array('option' => array('value' => '')),
  5279. '/option',
  5280. '*/select',
  5281. '-',
  5282. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5283. $yearsRegex,
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. '*/select',
  5287. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5288. $hoursRegex,
  5289. array('option' => array('value' => '')),
  5290. '/option',
  5291. '*/select',
  5292. ':',
  5293. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5294. $minutesRegex,
  5295. array('option' => array('value' => '')),
  5296. '/option',
  5297. '*/select',
  5298. ' ',
  5299. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5300. $meridianRegex,
  5301. array('option' => array('value' => '')),
  5302. '/option',
  5303. '*/select'
  5304. );
  5305. $this->assertTags($result, $expected);
  5306. }
  5307. /**
  5308. * When changing the date format, the label should always focus the first select box when
  5309. * clicked.
  5310. *
  5311. * @return void
  5312. */
  5313. public function testDateTimeLabelIdMatchesFirstInput() {
  5314. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5315. $this->assertContains('label for="ModelDateMonth"', $result);
  5316. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5317. $this->assertContains('label for="ModelDateDay"', $result);
  5318. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5319. $this->assertContains('label for="ModelDateYear"', $result);
  5320. }
  5321. /**
  5322. * testMonth method
  5323. *
  5324. * @return void
  5325. */
  5326. public function testMonth() {
  5327. $result = $this->Form->month('Model.field');
  5328. $expected = array(
  5329. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5330. array('option' => array('value' => '')),
  5331. '/option',
  5332. array('option' => array('value' => '01')),
  5333. date('F', strtotime('2008-01-01 00:00:00')),
  5334. '/option',
  5335. array('option' => array('value' => '02')),
  5336. date('F', strtotime('2008-02-01 00:00:00')),
  5337. '/option',
  5338. '*/select',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $result = $this->Form->month('Model.field', array('empty' => true));
  5342. $expected = array(
  5343. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5344. array('option' => array('value' => '')),
  5345. '/option',
  5346. array('option' => array('value' => '01')),
  5347. date('F', strtotime('2008-01-01 00:00:00')),
  5348. '/option',
  5349. array('option' => array('value' => '02')),
  5350. date('F', strtotime('2008-02-01 00:00:00')),
  5351. '/option',
  5352. '*/select',
  5353. );
  5354. $this->assertTags($result, $expected);
  5355. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5356. $expected = array(
  5357. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5358. array('option' => array('value' => '')),
  5359. '/option',
  5360. array('option' => array('value' => '01')),
  5361. '01',
  5362. '/option',
  5363. array('option' => array('value' => '02')),
  5364. '02',
  5365. '/option',
  5366. '*/select',
  5367. );
  5368. $this->assertTags($result, $expected);
  5369. $monthNames = array(
  5370. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5371. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5372. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5373. $expected = array(
  5374. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5375. array('option' => array('value' => '')),
  5376. '/option',
  5377. array('option' => array('value' => '01')),
  5378. 'Jan',
  5379. '/option',
  5380. array('option' => array('value' => '02')),
  5381. 'Feb',
  5382. '/option',
  5383. '*/select',
  5384. );
  5385. $this->assertTags($result, $expected);
  5386. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5387. $result = $this->Form->month('Project.release');
  5388. $expected = array(
  5389. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5390. array('option' => array('value' => '')),
  5391. '/option',
  5392. array('option' => array('value' => '01')),
  5393. 'January',
  5394. '/option',
  5395. array('option' => array('value' => '02', 'selected' => 'selected')),
  5396. 'February',
  5397. '/option',
  5398. '*/select',
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. }
  5402. /**
  5403. * testDay method
  5404. *
  5405. * @return void
  5406. */
  5407. public function testDay() {
  5408. extract($this->dateRegex);
  5409. $result = $this->Form->day('Model.field', array('value' => false));
  5410. $expected = array(
  5411. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5412. array('option' => array('value' => '')),
  5413. '/option',
  5414. array('option' => array('value' => '01')),
  5415. '1',
  5416. '/option',
  5417. array('option' => array('value' => '02')),
  5418. '2',
  5419. '/option',
  5420. $daysRegex,
  5421. '/select',
  5422. );
  5423. $this->assertTags($result, $expected);
  5424. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5425. $result = $this->Form->day('Model.field');
  5426. $expected = array(
  5427. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5428. array('option' => array('value' => '')),
  5429. '/option',
  5430. array('option' => array('value' => '01')),
  5431. '1',
  5432. '/option',
  5433. array('option' => array('value' => '02')),
  5434. '2',
  5435. '/option',
  5436. $daysRegex,
  5437. array('option' => array('value' => '10', 'selected' => 'selected')),
  5438. '10',
  5439. '/option',
  5440. $daysRegex,
  5441. '/select',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. $this->Form->request->data['Model']['field'] = '';
  5445. $result = $this->Form->day('Model.field', array('value' => '10'));
  5446. $expected = array(
  5447. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5448. array('option' => array('value' => '')),
  5449. '/option',
  5450. array('option' => array('value' => '01')),
  5451. '1',
  5452. '/option',
  5453. array('option' => array('value' => '02')),
  5454. '2',
  5455. '/option',
  5456. $daysRegex,
  5457. array('option' => array('value' => '10', 'selected' => 'selected')),
  5458. '10',
  5459. '/option',
  5460. $daysRegex,
  5461. '/select',
  5462. );
  5463. $this->assertTags($result, $expected);
  5464. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5465. $result = $this->Form->day('Model.field', array('value' => true));
  5466. $expected = array(
  5467. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5468. array('option' => array('value' => '')),
  5469. '/option',
  5470. array('option' => array('value' => '01')),
  5471. '1',
  5472. '/option',
  5473. array('option' => array('value' => '02')),
  5474. '2',
  5475. '/option',
  5476. $daysRegex,
  5477. array('option' => array('value' => '10', 'selected' => 'selected')),
  5478. '10',
  5479. '/option',
  5480. $daysRegex,
  5481. '/select',
  5482. );
  5483. $this->assertTags($result, $expected);
  5484. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5485. $result = $this->Form->day('Project.release');
  5486. $expected = array(
  5487. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5488. array('option' => array('value' => '')),
  5489. '/option',
  5490. array('option' => array('value' => '01')),
  5491. '1',
  5492. '/option',
  5493. array('option' => array('value' => '02')),
  5494. '2',
  5495. '/option',
  5496. $daysRegex,
  5497. array('option' => array('value' => '10', 'selected' => 'selected')),
  5498. '10',
  5499. '/option',
  5500. $daysRegex,
  5501. '/select',
  5502. );
  5503. $this->assertTags($result, $expected);
  5504. }
  5505. /**
  5506. * testMinute method
  5507. *
  5508. * @return void
  5509. */
  5510. public function testMinute() {
  5511. extract($this->dateRegex);
  5512. $result = $this->Form->minute('Model.field');
  5513. $expected = array(
  5514. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. array('option' => array('value' => '00')),
  5518. '00',
  5519. '/option',
  5520. array('option' => array('value' => '01')),
  5521. '01',
  5522. '/option',
  5523. array('option' => array('value' => '02')),
  5524. '02',
  5525. '/option',
  5526. $minutesRegex,
  5527. '/select',
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5531. $result = $this->Form->minute('Model.field');
  5532. $expected = array(
  5533. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5534. array('option' => array('value' => '')),
  5535. '/option',
  5536. array('option' => array('value' => '00')),
  5537. '00',
  5538. '/option',
  5539. array('option' => array('value' => '01')),
  5540. '01',
  5541. '/option',
  5542. array('option' => array('value' => '02')),
  5543. '02',
  5544. '/option',
  5545. $minutesRegex,
  5546. array('option' => array('value' => '12', 'selected' => 'selected')),
  5547. '12',
  5548. '/option',
  5549. $minutesRegex,
  5550. '/select',
  5551. );
  5552. $this->assertTags($result, $expected);
  5553. $this->Form->request->data['Model']['field'] = '';
  5554. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5555. $expected = array(
  5556. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. array('option' => array('value' => '00')),
  5560. '00',
  5561. '/option',
  5562. array('option' => array('value' => '05')),
  5563. '05',
  5564. '/option',
  5565. array('option' => array('value' => '10')),
  5566. '10',
  5567. '/option',
  5568. $minutesRegex,
  5569. '/select',
  5570. );
  5571. $this->assertTags($result, $expected);
  5572. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5573. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5574. $expected = array(
  5575. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5576. array('option' => array('value' => '')),
  5577. '/option',
  5578. array('option' => array('value' => '00')),
  5579. '00',
  5580. '/option',
  5581. array('option' => array('value' => '05')),
  5582. '05',
  5583. '/option',
  5584. array('option' => array('value' => '10', 'selected' => 'selected')),
  5585. '10',
  5586. '/option',
  5587. $minutesRegex,
  5588. '/select',
  5589. );
  5590. $this->assertTags($result, $expected);
  5591. }
  5592. /**
  5593. * testHour method
  5594. *
  5595. * @return void
  5596. */
  5597. public function testHour() {
  5598. extract($this->dateRegex);
  5599. $result = $this->Form->hour('Model.field', false);
  5600. $expected = array(
  5601. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5602. array('option' => array('value' => '')),
  5603. '/option',
  5604. array('option' => array('value' => '01')),
  5605. '1',
  5606. '/option',
  5607. array('option' => array('value' => '02')),
  5608. '2',
  5609. '/option',
  5610. $hoursRegex,
  5611. '/select',
  5612. );
  5613. $this->assertTags($result, $expected);
  5614. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5615. $result = $this->Form->hour('Model.field', false);
  5616. $expected = array(
  5617. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5618. array('option' => array('value' => '')),
  5619. '/option',
  5620. array('option' => array('value' => '01')),
  5621. '1',
  5622. '/option',
  5623. array('option' => array('value' => '02')),
  5624. '2',
  5625. '/option',
  5626. $hoursRegex,
  5627. array('option' => array('value' => '12', 'selected' => 'selected')),
  5628. '12',
  5629. '/option',
  5630. '/select',
  5631. );
  5632. $this->assertTags($result, $expected);
  5633. $this->Form->request->data['Model']['field'] = '';
  5634. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5635. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5636. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5637. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5638. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5639. $result = $this->Form->hour('Model.field', true);
  5640. $expected = array(
  5641. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5642. array('option' => array('value' => '')),
  5643. '/option',
  5644. array('option' => array('value' => '00', 'selected' => 'selected')),
  5645. '0',
  5646. '/option',
  5647. array('option' => array('value' => '01')),
  5648. '1',
  5649. '/option',
  5650. array('option' => array('value' => '02')),
  5651. '2',
  5652. '/option',
  5653. $hoursRegex,
  5654. '/select',
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. unset($this->Form->request->data['Model']['field']);
  5658. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5659. $thisHour = date('H');
  5660. $optValue = date('G');
  5661. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5662. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5663. $result = $this->Form->hour('Model.field', true);
  5664. $expected = array(
  5665. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5666. array('option' => array('value' => '')),
  5667. '/option',
  5668. array('option' => array('value' => '00')),
  5669. '0',
  5670. '/option',
  5671. array('option' => array('value' => '01', 'selected' => 'selected')),
  5672. '1',
  5673. '/option',
  5674. array('option' => array('value' => '02')),
  5675. '2',
  5676. '/option',
  5677. $hoursRegex,
  5678. '/select',
  5679. );
  5680. $this->assertTags($result, $expected);
  5681. }
  5682. /**
  5683. * testYear method
  5684. *
  5685. * @return void
  5686. */
  5687. public function testYear() {
  5688. $result = $this->Form->year('Model.field', 2006, 2007);
  5689. $expected = array(
  5690. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5691. array('option' => array('value' => '')),
  5692. '/option',
  5693. array('option' => array('value' => '2007')),
  5694. '2007',
  5695. '/option',
  5696. array('option' => array('value' => '2006')),
  5697. '2006',
  5698. '/option',
  5699. '/select',
  5700. );
  5701. $this->assertTags($result, $expected);
  5702. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5703. $expected = array(
  5704. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5705. array('option' => array('value' => '')),
  5706. '/option',
  5707. array('option' => array('value' => '2006')),
  5708. '2006',
  5709. '/option',
  5710. array('option' => array('value' => '2007')),
  5711. '2007',
  5712. '/option',
  5713. '/select',
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. $this->request->data['Contact']['published'] = '';
  5717. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5718. $expected = array(
  5719. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5720. array('option' => array('value' => '')),
  5721. '/option',
  5722. array('option' => array('value' => '2007')),
  5723. '2007',
  5724. '/option',
  5725. array('option' => array('value' => '2006')),
  5726. '2006',
  5727. '/option',
  5728. '/select',
  5729. );
  5730. $this->assertTags($result, $expected);
  5731. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5732. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5733. $expected = array(
  5734. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5735. array('option' => array('value' => '2007')),
  5736. '2007',
  5737. '/option',
  5738. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5739. '2006',
  5740. '/option',
  5741. '/select',
  5742. );
  5743. $this->assertTags($result, $expected);
  5744. $this->Form->request->data['Contact']['published'] = '';
  5745. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5746. $expected = array(
  5747. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5748. array('option' => array('value' => '')),
  5749. '/option',
  5750. array('option' => array('value' => '2007')),
  5751. '2007',
  5752. '/option',
  5753. array('option' => array('value' => '2006')),
  5754. '2006',
  5755. '/option',
  5756. '/select',
  5757. );
  5758. $this->assertTags($result, $expected);
  5759. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5760. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5761. $expected = array(
  5762. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5763. array('option' => array('value' => '2007')),
  5764. '2007',
  5765. '/option',
  5766. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5767. '2006',
  5768. '/option',
  5769. '/select',
  5770. );
  5771. $this->assertTags($result, $expected);
  5772. $this->Form->request->data['Contact']['published'] = '';
  5773. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5774. $expected = array(
  5775. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5776. array('option' => array('value' => '')),
  5777. '/option',
  5778. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5779. '2007',
  5780. '/option',
  5781. array('option' => array('value' => '2006')),
  5782. '2006',
  5783. '/option',
  5784. '/select',
  5785. );
  5786. $this->assertTags($result, $expected);
  5787. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5788. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5789. $expected = array(
  5790. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5791. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5792. '2007',
  5793. '/option',
  5794. array('option' => array('value' => '2006')),
  5795. '2006',
  5796. '/option',
  5797. '/select',
  5798. );
  5799. $this->assertTags($result, $expected);
  5800. $this->Form->request->data['Contact']['published'] = '';
  5801. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5802. $expected = array(
  5803. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5804. array('option' => array('value' => '2008')),
  5805. '2008',
  5806. '/option',
  5807. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5808. '2007',
  5809. '/option',
  5810. array('option' => array('value' => '2006')),
  5811. '2006',
  5812. '/option',
  5813. '/select',
  5814. );
  5815. $this->assertTags($result, $expected);
  5816. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5817. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5818. $expected = array(
  5819. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5820. array('option' => array('value' => '2008')),
  5821. '2008',
  5822. '/option',
  5823. array('option' => array('value' => '2007')),
  5824. '2007',
  5825. '/option',
  5826. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5827. '2006',
  5828. '/option',
  5829. '/select',
  5830. );
  5831. $this->assertTags($result, $expected);
  5832. $this->Form->request->data = array();
  5833. $this->Form->create('Contact');
  5834. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5835. $expected = array(
  5836. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5837. array('option' => array('value' => '2008')),
  5838. '2008',
  5839. '/option',
  5840. array('option' => array('value' => '2007')),
  5841. '2007',
  5842. '/option',
  5843. array('option' => array('value' => '2006')),
  5844. '2006',
  5845. '/option',
  5846. '/select',
  5847. );
  5848. $this->assertTags($result, $expected);
  5849. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5850. $this->assertContains('data[Contact][published][year]', $result);
  5851. }
  5852. /**
  5853. * testYearAutoExpandRange method
  5854. *
  5855. * @return void
  5856. */
  5857. public function testYearAutoExpandRange() {
  5858. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5859. $result = $this->Form->year('User.birthday');
  5860. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5861. $result = $matches[1];
  5862. $expected = range(date('Y') + 20, 1930);
  5863. $this->assertEquals($result, $expected);
  5864. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5865. $result = $this->Form->year('Project.release');
  5866. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5867. $result = $matches[1];
  5868. $expected = range(2050, date('Y') - 20);
  5869. $this->assertEquals($result, $expected);
  5870. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5871. $result = $this->Form->year('Project.release', 1890, 1900);
  5872. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5873. $result = $matches[1];
  5874. $expected = range(1900, 1881);
  5875. $this->assertEquals($result, $expected);
  5876. }
  5877. /**
  5878. * testTextArea method
  5879. *
  5880. * @return void
  5881. */
  5882. public function testTextArea() {
  5883. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5884. $result = $this->Form->textarea('Model.field');
  5885. $expected = array(
  5886. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5887. 'some test data',
  5888. '/textarea',
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $result = $this->Form->textarea('Model.tmp');
  5892. $expected = array(
  5893. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5894. '/textarea',
  5895. );
  5896. $this->assertTags($result, $expected);
  5897. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5898. $result = $this->Form->textarea('Model.field');
  5899. $expected = array(
  5900. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5901. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5902. '/textarea',
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5906. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5907. $expected = array(
  5908. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5909. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5910. '/textarea',
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5914. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5915. $expected = array(
  5916. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5917. '/textarea'
  5918. );
  5919. $this->assertTags($result, $expected);
  5920. }
  5921. /**
  5922. * testTextAreaWithStupidCharacters method
  5923. *
  5924. * test text area with non-ascii characters
  5925. *
  5926. * @return void
  5927. */
  5928. public function testTextAreaWithStupidCharacters() {
  5929. $this->loadFixtures('Post');
  5930. $result = $this->Form->input('Post.content', array(
  5931. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5932. ));
  5933. $expected = array(
  5934. 'div' => array('class' => 'input textarea'),
  5935. 'label' => array('for' => 'PostContent'),
  5936. 'Current Text',
  5937. '/label',
  5938. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5939. 'GREAT®',
  5940. '/textarea',
  5941. '/div'
  5942. );
  5943. $this->assertTags($result, $expected);
  5944. }
  5945. /**
  5946. * testHiddenField method
  5947. *
  5948. * @return void
  5949. */
  5950. public function testHiddenField() {
  5951. $Contact = ClassRegistry::getObject('Contact');
  5952. $Contact->validationErrors['field'] = 1;
  5953. $this->Form->request->data['Contact']['field'] = 'test';
  5954. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5955. $this->assertTags($result, array(
  5956. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5957. );
  5958. }
  5959. /**
  5960. * testFileUploadField method
  5961. *
  5962. * @return void
  5963. */
  5964. public function testFileUploadField() {
  5965. $result = $this->Form->file('Model.upload');
  5966. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5967. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5968. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5969. $expected = array(
  5970. 'div' => array('class' => 'input file'),
  5971. 'label' => array('for' => 'ModelUpload'),
  5972. 'Upload',
  5973. '/label',
  5974. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5975. '/div'
  5976. );
  5977. $this->assertTags($result, $expected);
  5978. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5979. $result = $this->Form->file('Model.upload');
  5980. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5981. }
  5982. /**
  5983. * test File upload input on a model not used in create();
  5984. *
  5985. * @return void
  5986. */
  5987. public function testFileUploadOnOtherModel() {
  5988. $this->Form->create('ValidateUser', array('type' => 'file'));
  5989. $result = $this->Form->file('ValidateProfile.city');
  5990. $expected = array(
  5991. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. }
  5995. /**
  5996. * testButton method
  5997. *
  5998. * @return void
  5999. */
  6000. public function testButton() {
  6001. $result = $this->Form->button('Hi');
  6002. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6003. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6004. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6005. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6006. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6007. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6008. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6009. $result = $this->Form->button('No type', array('type' => false));
  6010. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6011. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6012. $this->assertNotRegExp('/\&039/', $result);
  6013. }
  6014. /**
  6015. * Test that button() makes unlocked fields by default.
  6016. *
  6017. * @return void
  6018. */
  6019. public function testButtonUnlockedByDefault() {
  6020. $this->Form->request->params['_Token']['key'] = 'secured';
  6021. $this->Form->button('Save', array('name' => 'save'));
  6022. $this->Form->button('Clear');
  6023. $result = $this->Form->unlockField();
  6024. $this->assertEquals(array('save'), $result);
  6025. }
  6026. /**
  6027. * testPostButton method
  6028. *
  6029. * @return void
  6030. */
  6031. public function testPostButton() {
  6032. $result = $this->Form->postButton('Hi', '/controller/action');
  6033. $this->assertTags($result, array(
  6034. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6035. 'div' => array('style' => 'display:none;'),
  6036. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6037. '/div',
  6038. 'button' => array('type' => 'submit'),
  6039. 'Hi',
  6040. '/button',
  6041. '/form'
  6042. ));
  6043. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6044. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6045. }
  6046. /**
  6047. * Test that postButton adds _Token fields.
  6048. *
  6049. * @return void
  6050. */
  6051. public function testSecurePostButton() {
  6052. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6053. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6054. $expected = array(
  6055. 'form' => array(
  6056. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6057. ),
  6058. array('div' => array('style' => 'display:none;')),
  6059. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6060. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6061. '/div',
  6062. 'button' => array('type' => 'submit'),
  6063. 'Delete',
  6064. '/button',
  6065. array('div' => array('style' => 'display:none;')),
  6066. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6067. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6068. '/div',
  6069. '/form',
  6070. );
  6071. $this->assertTags($result, $expected);
  6072. }
  6073. /**
  6074. * testPostLink method
  6075. *
  6076. * @return void
  6077. */
  6078. public function testPostLink() {
  6079. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6080. $this->assertTags($result, array(
  6081. 'form' => array(
  6082. 'method' => 'post', 'action' => '/posts/delete/1',
  6083. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6084. ),
  6085. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6086. '/form',
  6087. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6088. 'Delete',
  6089. '/a'
  6090. ));
  6091. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6092. $this->assertTags($result, array(
  6093. 'form' => array(
  6094. 'method' => 'post', 'action' => '/posts/delete/1',
  6095. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6096. ),
  6097. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6098. '/form',
  6099. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6100. 'Delete',
  6101. '/a'
  6102. ));
  6103. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6104. $this->assertTags($result, array(
  6105. 'form' => array(
  6106. 'method' => 'post', 'action' => '/posts/delete/1',
  6107. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6108. ),
  6109. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6110. '/form',
  6111. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6112. 'Delete',
  6113. '/a'
  6114. ));
  6115. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6116. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6117. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6118. $this->assertTags($result, array(
  6119. 'form' => array(
  6120. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6121. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6122. ),
  6123. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6124. '/form',
  6125. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6126. 'Delete',
  6127. '/a'
  6128. ));
  6129. }
  6130. /**
  6131. * Test that postLink adds _Token fields.
  6132. *
  6133. * @return void
  6134. */
  6135. public function testSecurePostLink() {
  6136. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6137. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6138. $expected = array(
  6139. 'form' => array(
  6140. 'method' => 'post', 'action' => '/posts/delete/1',
  6141. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6142. ),
  6143. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6144. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6145. 'div' => array('style' => 'display:none;'),
  6146. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6147. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6148. '/div',
  6149. '/form',
  6150. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6151. 'Delete',
  6152. '/a'
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. }
  6156. /**
  6157. * testSubmitButton method
  6158. *
  6159. * @return void
  6160. */
  6161. public function testSubmitButton() {
  6162. $result = $this->Form->submit('');
  6163. $expected = array(
  6164. 'div' => array('class' => 'submit'),
  6165. 'input' => array('type' => 'submit', 'value' => ''),
  6166. '/div'
  6167. );
  6168. $this->assertTags($result, $expected);
  6169. $result = $this->Form->submit('Test Submit');
  6170. $expected = array(
  6171. 'div' => array('class' => 'submit'),
  6172. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6173. '/div'
  6174. );
  6175. $this->assertTags($result, $expected);
  6176. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6177. $expected = array(
  6178. 'span' => array('class' => 'submit'),
  6179. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6180. '/span'
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6184. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6185. $this->assertTags($result, $expected);
  6186. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6187. $expected = array(
  6188. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6189. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6190. '/div'
  6191. );
  6192. $this->assertTags($result, $expected);
  6193. $result = $this->Form->submit('Next >');
  6194. $expected = array(
  6195. 'div' => array('class' => 'submit'),
  6196. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6197. '/div'
  6198. );
  6199. $this->assertTags($result, $expected);
  6200. $result = $this->Form->submit('Next >', array('escape' => false));
  6201. $expected = array(
  6202. 'div' => array('class' => 'submit'),
  6203. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6204. '/div'
  6205. );
  6206. $this->assertTags($result, $expected);
  6207. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6208. $expected = array(
  6209. 'div' => array('class' => 'submit'),
  6210. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6211. '/div'
  6212. );
  6213. $this->assertTags($result, $expected);
  6214. $before = '--before--';
  6215. $after = '--after--';
  6216. $result = $this->Form->submit('Test', array('before' => $before));
  6217. $expected = array(
  6218. 'div' => array('class' => 'submit'),
  6219. '--before--',
  6220. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6221. '/div'
  6222. );
  6223. $this->assertTags($result, $expected);
  6224. $result = $this->Form->submit('Test', array('after' => $after));
  6225. $expected = array(
  6226. 'div' => array('class' => 'submit'),
  6227. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6228. '--after--',
  6229. '/div'
  6230. );
  6231. $this->assertTags($result, $expected);
  6232. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6233. $expected = array(
  6234. 'div' => array('class' => 'submit'),
  6235. '--before--',
  6236. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6237. '--after--',
  6238. '/div'
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. }
  6242. /**
  6243. * test image submit types.
  6244. *
  6245. * @return void
  6246. */
  6247. public function testSubmitImage() {
  6248. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6249. $expected = array(
  6250. 'div' => array('class' => 'submit'),
  6251. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6252. '/div'
  6253. );
  6254. $this->assertTags($result, $expected);
  6255. $result = $this->Form->submit('/relative/cake.power.gif');
  6256. $expected = array(
  6257. 'div' => array('class' => 'submit'),
  6258. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6259. '/div'
  6260. );
  6261. $this->assertTags($result, $expected);
  6262. $result = $this->Form->submit('cake.power.gif');
  6263. $expected = array(
  6264. 'div' => array('class' => 'submit'),
  6265. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6266. '/div'
  6267. );
  6268. $this->assertTags($result, $expected);
  6269. $result = $this->Form->submit('Not.an.image');
  6270. $expected = array(
  6271. 'div' => array('class' => 'submit'),
  6272. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6273. '/div'
  6274. );
  6275. $this->assertTags($result, $expected);
  6276. $after = '--after--';
  6277. $before = '--before--';
  6278. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6279. $expected = array(
  6280. 'div' => array('class' => 'submit'),
  6281. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6282. '--after--',
  6283. '/div'
  6284. );
  6285. $this->assertTags($result, $expected);
  6286. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6287. $expected = array(
  6288. 'div' => array('class' => 'submit'),
  6289. '--before--',
  6290. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6291. '/div'
  6292. );
  6293. $this->assertTags($result, $expected);
  6294. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6295. $expected = array(
  6296. 'div' => array('class' => 'submit'),
  6297. '--before--',
  6298. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6299. '--after--',
  6300. '/div'
  6301. );
  6302. $this->assertTags($result, $expected);
  6303. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6304. $expected = array(
  6305. 'div' => array('class' => 'submit'),
  6306. '--before--',
  6307. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6308. '--after--',
  6309. '/div'
  6310. );
  6311. $this->assertTags($result, $expected);
  6312. }
  6313. /**
  6314. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6315. * be submitted at a time.
  6316. *
  6317. * @return void
  6318. */
  6319. public function testSubmitUnlockedByDefault() {
  6320. $this->Form->request->params['_Token']['key'] = 'secured';
  6321. $this->Form->submit('Go go');
  6322. $this->Form->submit('Save', array('name' => 'save'));
  6323. $result = $this->Form->unlockField();
  6324. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6325. }
  6326. /**
  6327. * Test submit image with timestamps.
  6328. *
  6329. * @return void
  6330. */
  6331. public function testSubmitImageTimestamp() {
  6332. Configure::write('Asset.timestamp', 'force');
  6333. $result = $this->Form->submit('cake.power.gif');
  6334. $expected = array(
  6335. 'div' => array('class' => 'submit'),
  6336. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6337. '/div'
  6338. );
  6339. $this->assertTags($result, $expected);
  6340. }
  6341. /**
  6342. * test the create() method
  6343. *
  6344. * @return void
  6345. */
  6346. public function testCreate() {
  6347. $result = $this->Form->create('Contact');
  6348. $encoding = strtolower(Configure::read('App.encoding'));
  6349. $expected = array(
  6350. 'form' => array(
  6351. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6352. 'accept-charset' => $encoding
  6353. ),
  6354. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6356. '/div'
  6357. );
  6358. $this->assertTags($result, $expected);
  6359. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6360. $expected = array('form' => array(
  6361. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6362. 'accept-charset' => $encoding
  6363. ));
  6364. $this->assertTags($result, $expected);
  6365. $result = $this->Form->create('Contact', array('type' => 'get'));
  6366. $expected = array('form' => array(
  6367. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6368. 'accept-charset' => $encoding
  6369. ));
  6370. $this->assertTags($result, $expected);
  6371. $result = $this->Form->create('Contact', array('type' => 'put'));
  6372. $expected = array(
  6373. 'form' => array(
  6374. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6375. 'accept-charset' => $encoding
  6376. ),
  6377. 'div' => array('style' => 'display:none;'),
  6378. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6379. '/div'
  6380. );
  6381. $this->assertTags($result, $expected);
  6382. $result = $this->Form->create('Contact', array('type' => 'file'));
  6383. $expected = array(
  6384. 'form' => array(
  6385. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6386. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6387. ),
  6388. 'div' => array('style' => 'display:none;'),
  6389. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6390. '/div'
  6391. );
  6392. $this->assertTags($result, $expected);
  6393. $this->Form->request->data['Contact']['id'] = 1;
  6394. $this->Form->request->here = '/contacts/edit/1';
  6395. $this->Form->request['action'] = 'edit';
  6396. $result = $this->Form->create('Contact');
  6397. $expected = array(
  6398. 'form' => array(
  6399. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6400. 'accept-charset' => $encoding
  6401. ),
  6402. 'div' => array('style' => 'display:none;'),
  6403. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6404. '/div'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $this->Form->request->data['Contact']['id'] = 1;
  6408. $this->Form->request->here = '/contacts/edit/1';
  6409. $this->Form->request['action'] = 'edit';
  6410. $result = $this->Form->create('Contact', array('type' => 'file'));
  6411. $expected = array(
  6412. 'form' => array(
  6413. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6414. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6415. ),
  6416. 'div' => array('style' => 'display:none;'),
  6417. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6418. '/div'
  6419. );
  6420. $this->assertTags($result, $expected);
  6421. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6422. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6423. $expected = array(
  6424. 'form' => array(
  6425. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6426. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6427. ),
  6428. 'div' => array('style' => 'display:none;'),
  6429. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6430. '/div'
  6431. );
  6432. $this->assertTags($result, $expected);
  6433. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6434. $expected = array(
  6435. 'form' => array(
  6436. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6437. 'accept-charset' => $encoding
  6438. ),
  6439. 'div' => array('style' => 'display:none;'),
  6440. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6441. '/div'
  6442. );
  6443. $this->assertTags($result, $expected);
  6444. $this->Form->request['action'] = 'add';
  6445. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6446. $expected = array(
  6447. 'form' => array(
  6448. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6449. 'accept-charset' => $encoding
  6450. ),
  6451. 'div' => array('style' => 'display:none;'),
  6452. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6453. '/div'
  6454. );
  6455. $this->assertTags($result, $expected);
  6456. $result = $this->Form->create('User', array('action' => 'login'));
  6457. $expected = array(
  6458. 'form' => array(
  6459. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6460. 'accept-charset' => $encoding
  6461. ),
  6462. 'div' => array('style' => 'display:none;'),
  6463. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6464. '/div'
  6465. );
  6466. $this->assertTags($result, $expected);
  6467. $result = $this->Form->create('User', array('url' => '/users/login'));
  6468. $expected = array(
  6469. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6470. 'div' => array('style' => 'display:none;'),
  6471. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6472. '/div'
  6473. );
  6474. $this->assertTags($result, $expected);
  6475. $this->Form->request['controller'] = 'pages';
  6476. $result = $this->Form->create('User', array('action' => 'signup'));
  6477. $expected = array(
  6478. 'form' => array(
  6479. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6480. 'accept-charset' => $encoding
  6481. ),
  6482. 'div' => array('style' => 'display:none;'),
  6483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6484. '/div'
  6485. );
  6486. $this->assertTags($result, $expected);
  6487. $this->Form->request->data = array();
  6488. $this->Form->request['controller'] = 'contacts';
  6489. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6490. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6491. $expected = array(
  6492. 'form' => array(
  6493. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6494. 'accept-charset' => 'utf-8'
  6495. ),
  6496. 'div' => array('style' => 'display:none;'),
  6497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6498. '/div'
  6499. );
  6500. $this->assertTags($result, $expected);
  6501. }
  6502. /**
  6503. * Test the onsubmit option for create()
  6504. *
  6505. * @return void
  6506. */
  6507. public function testCreateOnSubmit() {
  6508. $this->Form->request->data = array();
  6509. $this->Form->request['controller'] = 'contacts';
  6510. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6511. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6512. $expected = array(
  6513. 'form' => array(
  6514. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6515. 'accept-charset' => 'utf-8'
  6516. ),
  6517. 'div' => array('style' => 'display:none;'),
  6518. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6519. '/div'
  6520. );
  6521. $this->assertTags($result, $expected);
  6522. $this->Form->request->data = array();
  6523. $this->Form->request['controller'] = 'contacts';
  6524. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6525. $result = $this->Form->create(array(
  6526. 'url' => array('action' => 'index', 'param'),
  6527. 'default' => false,
  6528. 'onsubmit' => 'someFunction();'
  6529. ));
  6530. $expected = array(
  6531. 'form' => array(
  6532. 'id' => 'ContactAddForm', 'method' => 'post',
  6533. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6534. 'action' => '/contacts/index/param',
  6535. 'accept-charset' => 'utf-8'
  6536. ),
  6537. 'div' => array('style' => 'display:none;'),
  6538. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6539. '/div'
  6540. );
  6541. $this->assertTags($result, $expected);
  6542. }
  6543. /**
  6544. * test create() with automatic url generation
  6545. *
  6546. * @return void
  6547. */
  6548. public function testCreateAutoUrl() {
  6549. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6550. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6551. $this->Form->request->base = '/base_url';
  6552. $result = $this->Form->create('Contact');
  6553. $expected = array(
  6554. 'form' => array(
  6555. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6556. 'accept-charset' => 'utf-8'
  6557. ),
  6558. 'div' => array('style' => 'display:none;'),
  6559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6560. '/div'
  6561. );
  6562. $this->assertTags($result, $expected);
  6563. $this->Form->request['action'] = 'delete';
  6564. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6565. $this->Form->request->base = '/base_url';
  6566. $result = $this->Form->create('Contact');
  6567. $expected = array(
  6568. 'form' => array(
  6569. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6570. 'accept-charset' => 'utf-8'
  6571. ),
  6572. 'div' => array('style' => 'display:none;'),
  6573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6574. '/div'
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. }
  6578. /**
  6579. * test create() with a custom route
  6580. *
  6581. * @return void
  6582. */
  6583. public function testCreateCustomRoute() {
  6584. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6585. $encoding = strtolower(Configure::read('App.encoding'));
  6586. $result = $this->Form->create('User', array('action' => 'login'));
  6587. $expected = array(
  6588. 'form' => array(
  6589. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6590. 'accept-charset' => $encoding
  6591. ),
  6592. 'div' => array('style' => 'display:none;'),
  6593. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6594. '/div'
  6595. );
  6596. $this->assertTags($result, $expected);
  6597. }
  6598. /**
  6599. * test that inputDefaults are stored and used.
  6600. *
  6601. * @return void
  6602. */
  6603. public function testCreateWithInputDefaults() {
  6604. $this->Form->create('User', array(
  6605. 'inputDefaults' => array(
  6606. 'div' => false,
  6607. 'label' => false,
  6608. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6609. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6610. )
  6611. ));
  6612. $result = $this->Form->input('username');
  6613. $expected = array(
  6614. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6615. );
  6616. $this->assertTags($result, $expected);
  6617. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6618. $expected = array(
  6619. 'div' => array('class' => 'input text'),
  6620. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6621. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6622. '/div'
  6623. );
  6624. $this->assertTags($result, $expected);
  6625. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6626. $expected = array(
  6627. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6628. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6629. );
  6630. $this->assertTags($result, $expected);
  6631. $this->Form->create('User', array(
  6632. 'inputDefaults' => array(
  6633. 'div' => false,
  6634. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6635. )
  6636. ));
  6637. $result = $this->Form->input('username', array('div' => true));
  6638. $expected = array(
  6639. 'div' => array('class' => 'input text'),
  6640. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6641. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6642. '/div'
  6643. );
  6644. $this->assertTags($result, $expected);
  6645. }
  6646. /**
  6647. * test automatic accept-charset overriding
  6648. *
  6649. * @return void
  6650. */
  6651. public function testCreateWithAcceptCharset() {
  6652. $result = $this->Form->create('UserForm', array(
  6653. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6654. )
  6655. );
  6656. $expected = array(
  6657. 'form' => array(
  6658. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6659. 'accept-charset' => 'iso-8859-1'
  6660. ),
  6661. 'div' => array('style' => 'display:none;'),
  6662. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6663. '/div'
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. }
  6667. /**
  6668. * Test base form url when url param is passed with multiple parameters (&)
  6669. *
  6670. */
  6671. public function testCreateQuerystringrequest() {
  6672. $encoding = strtolower(Configure::read('App.encoding'));
  6673. $result = $this->Form->create('Contact', array(
  6674. 'type' => 'post',
  6675. 'escape' => false,
  6676. 'url' => array(
  6677. 'controller' => 'controller',
  6678. 'action' => 'action',
  6679. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6680. )
  6681. ));
  6682. $expected = array(
  6683. 'form' => array(
  6684. 'id' => 'ContactAddForm',
  6685. 'method' => 'post',
  6686. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6687. 'accept-charset' => $encoding
  6688. ),
  6689. 'div' => array('style' => 'display:none;'),
  6690. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6691. '/div'
  6692. );
  6693. $this->assertTags($result, $expected);
  6694. $result = $this->Form->create('Contact', array(
  6695. 'type' => 'post',
  6696. 'url' => array(
  6697. 'controller' => 'controller',
  6698. 'action' => 'action',
  6699. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6700. )
  6701. ));
  6702. $expected = array(
  6703. 'form' => array(
  6704. 'id' => 'ContactAddForm',
  6705. 'method' => 'post',
  6706. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6707. 'accept-charset' => $encoding
  6708. ),
  6709. 'div' => array('style' => 'display:none;'),
  6710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. }
  6715. /**
  6716. * test that create() doesn't cause errors by multiple id's being in the primary key
  6717. * as could happen with multiple select or checkboxes.
  6718. *
  6719. * @return void
  6720. */
  6721. public function testCreateWithMultipleIdInData() {
  6722. $encoding = strtolower(Configure::read('App.encoding'));
  6723. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6724. $result = $this->Form->create('Contact');
  6725. $expected = array(
  6726. 'form' => array(
  6727. 'id' => 'ContactAddForm',
  6728. 'method' => 'post',
  6729. 'action' => '/contacts/add',
  6730. 'accept-charset' => $encoding
  6731. ),
  6732. 'div' => array('style' => 'display:none;'),
  6733. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6734. '/div'
  6735. );
  6736. $this->assertTags($result, $expected);
  6737. }
  6738. /**
  6739. * test that create() doesn't add in extra passed params.
  6740. *
  6741. * @return void
  6742. */
  6743. public function testCreatePassedArgs() {
  6744. $encoding = strtolower(Configure::read('App.encoding'));
  6745. $this->Form->request->data['Contact']['id'] = 1;
  6746. $result = $this->Form->create('Contact', array(
  6747. 'type' => 'post',
  6748. 'escape' => false,
  6749. 'url' => array(
  6750. 'action' => 'edit',
  6751. 'myparam'
  6752. )
  6753. ));
  6754. $expected = array(
  6755. 'form' => array(
  6756. 'id' => 'ContactAddForm',
  6757. 'method' => 'post',
  6758. 'action' => '/contacts/edit/myparam',
  6759. 'accept-charset' => $encoding
  6760. ),
  6761. 'div' => array('style' => 'display:none;'),
  6762. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6763. '/div'
  6764. );
  6765. $this->assertTags($result, $expected);
  6766. }
  6767. /**
  6768. * test creating a get form, and get form inputs.
  6769. *
  6770. * @return void
  6771. */
  6772. public function testGetFormCreate() {
  6773. $encoding = strtolower(Configure::read('App.encoding'));
  6774. $result = $this->Form->create('Contact', array('type' => 'get'));
  6775. $this->assertTags($result, array('form' => array(
  6776. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6777. 'accept-charset' => $encoding
  6778. )));
  6779. $result = $this->Form->text('Contact.name');
  6780. $this->assertTags($result, array('input' => array(
  6781. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6782. )));
  6783. $result = $this->Form->password('password');
  6784. $this->assertTags($result, array('input' => array(
  6785. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6786. )));
  6787. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6788. $result = $this->Form->text('user_form');
  6789. $this->assertTags($result, array('input' => array(
  6790. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6791. )));
  6792. }
  6793. /**
  6794. * test get form, and inputs when the model param is false
  6795. *
  6796. * @return void
  6797. */
  6798. public function testGetFormWithFalseModel() {
  6799. $encoding = strtolower(Configure::read('App.encoding'));
  6800. $this->Form->request['controller'] = 'contact_test';
  6801. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6802. $expected = array('form' => array(
  6803. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6804. 'accept-charset' => $encoding
  6805. ));
  6806. $this->assertTags($result, $expected);
  6807. $result = $this->Form->text('reason');
  6808. $expected = array(
  6809. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6810. );
  6811. $this->assertTags($result, $expected);
  6812. }
  6813. /**
  6814. * test that datetime() works with GET style forms.
  6815. *
  6816. * @return void
  6817. */
  6818. public function testDateTimeWithGetForms() {
  6819. extract($this->dateRegex);
  6820. $this->Form->create('Contact', array('type' => 'get'));
  6821. $result = $this->Form->datetime('created');
  6822. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6823. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6824. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6825. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6826. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6827. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6828. }
  6829. /**
  6830. * testEditFormWithData method
  6831. *
  6832. * test auto populating form elements from submitted data.
  6833. *
  6834. * @return void
  6835. */
  6836. public function testEditFormWithData() {
  6837. $this->Form->request->data = array('Person' => array(
  6838. 'id' => 1,
  6839. 'first_name' => 'Nate',
  6840. 'last_name' => 'Abele',
  6841. 'email' => 'nate@example.com'
  6842. ));
  6843. $this->Form->request->addParams(array(
  6844. 'models' => array('Person'),
  6845. 'controller' => 'people',
  6846. 'action' => 'add'
  6847. ));
  6848. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6849. $this->Form->create();
  6850. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6851. $expected = array(
  6852. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6853. 'select' => array(
  6854. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6855. ),
  6856. array('option' => array('value' => 1)), 'Nate', '/option',
  6857. array('option' => array('value' => 2)), 'Garrett', '/option',
  6858. array('option' => array('value' => 3)), 'Larry', '/option',
  6859. '/select'
  6860. );
  6861. $this->assertTags($result, $expected);
  6862. }
  6863. /**
  6864. * Test that required fields are created for various types of validation.
  6865. *
  6866. * @return void
  6867. */
  6868. public function testFormInputRequiredDetection() {
  6869. $this->Form->create('Contact');
  6870. $result = $this->Form->input('Contact.non_existing');
  6871. $expected = array(
  6872. 'div' => array('class' => 'input text'),
  6873. 'label' => array('for' => 'ContactNonExisting'),
  6874. 'Non Existing',
  6875. '/label',
  6876. 'input' => array(
  6877. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6878. 'id' => 'ContactNonExisting'
  6879. ),
  6880. '/div'
  6881. );
  6882. $this->assertTags($result, $expected);
  6883. $result = $this->Form->input('Contact.imrequired');
  6884. $expected = array(
  6885. 'div' => array('class' => 'input text required'),
  6886. 'label' => array('for' => 'ContactImrequired'),
  6887. 'Imrequired',
  6888. '/label',
  6889. 'input' => array(
  6890. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6891. 'id' => 'ContactImrequired',
  6892. 'required' => 'required'
  6893. ),
  6894. '/div'
  6895. );
  6896. $this->assertTags($result, $expected);
  6897. $result = $this->Form->input('Contact.imalsorequired');
  6898. $expected = array(
  6899. 'div' => array('class' => 'input text required'),
  6900. 'label' => array('for' => 'ContactImalsorequired'),
  6901. 'Imalsorequired',
  6902. '/label',
  6903. 'input' => array(
  6904. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6905. 'id' => 'ContactImalsorequired',
  6906. 'required' => 'required'
  6907. ),
  6908. '/div'
  6909. );
  6910. $this->assertTags($result, $expected);
  6911. $result = $this->Form->input('Contact.imrequiredtoo');
  6912. $expected = array(
  6913. 'div' => array('class' => 'input text required'),
  6914. 'label' => array('for' => 'ContactImrequiredtoo'),
  6915. 'Imrequiredtoo',
  6916. '/label',
  6917. 'input' => array(
  6918. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6919. 'id' => 'ContactImrequiredtoo',
  6920. 'required' => 'required'
  6921. ),
  6922. '/div'
  6923. );
  6924. $this->assertTags($result, $expected);
  6925. $result = $this->Form->input('Contact.required_one');
  6926. $expected = array(
  6927. 'div' => array('class' => 'input text required'),
  6928. 'label' => array('for' => 'ContactRequiredOne'),
  6929. 'Required One',
  6930. '/label',
  6931. 'input' => array(
  6932. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6933. 'id' => 'ContactRequiredOne',
  6934. 'required' => 'required'
  6935. ),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $result = $this->Form->input('Contact.string_required');
  6940. $expected = array(
  6941. 'div' => array('class' => 'input text required'),
  6942. 'label' => array('for' => 'ContactStringRequired'),
  6943. 'String Required',
  6944. '/label',
  6945. 'input' => array(
  6946. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6947. 'id' => 'ContactStringRequired',
  6948. 'required' => 'required'
  6949. ),
  6950. '/div'
  6951. );
  6952. $this->assertTags($result, $expected);
  6953. $result = $this->Form->input('Contact.imnotrequired');
  6954. $expected = array(
  6955. 'div' => array('class' => 'input text'),
  6956. 'label' => array('for' => 'ContactImnotrequired'),
  6957. 'Imnotrequired',
  6958. '/label',
  6959. 'input' => array(
  6960. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6961. 'id' => 'ContactImnotrequired'
  6962. ),
  6963. '/div'
  6964. );
  6965. $this->assertTags($result, $expected);
  6966. $result = $this->Form->input('Contact.imalsonotrequired');
  6967. $expected = array(
  6968. 'div' => array('class' => 'input text'),
  6969. 'label' => array('for' => 'ContactImalsonotrequired'),
  6970. 'Imalsonotrequired',
  6971. '/label',
  6972. 'input' => array(
  6973. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6974. 'id' => 'ContactImalsonotrequired'
  6975. ),
  6976. '/div'
  6977. );
  6978. $this->assertTags($result, $expected);
  6979. $result = $this->Form->input('Contact.imalsonotrequired2');
  6980. $expected = array(
  6981. 'div' => array('class' => 'input text'),
  6982. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6983. 'Imalsonotrequired2',
  6984. '/label',
  6985. 'input' => array(
  6986. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6987. 'id' => 'ContactImalsonotrequired2'
  6988. ),
  6989. '/div'
  6990. );
  6991. $this->assertTags($result, $expected);
  6992. $result = $this->Form->input('Contact.imnotrequiredeither');
  6993. $expected = array(
  6994. 'div' => array('class' => 'input text'),
  6995. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6996. 'Imnotrequiredeither',
  6997. '/label',
  6998. 'input' => array(
  6999. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7000. 'id' => 'ContactImnotrequiredeither'
  7001. ),
  7002. '/div'
  7003. );
  7004. $this->assertTags($result, $expected);
  7005. $result = $this->Form->input('Contact.iamrequiredalways');
  7006. $expected = array(
  7007. 'div' => array('class' => 'input text required'),
  7008. 'label' => array('for' => 'ContactIamrequiredalways'),
  7009. 'Iamrequiredalways',
  7010. '/label',
  7011. 'input' => array(
  7012. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7013. 'id' => 'ContactIamrequiredalways',
  7014. 'required' => 'required'
  7015. ),
  7016. '/div'
  7017. );
  7018. $this->assertTags($result, $expected);
  7019. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7020. $expected = array(
  7021. 'div' => array('class' => 'input checkbox required'),
  7022. array('input' => array(
  7023. 'type' => 'hidden',
  7024. 'name' => 'data[Contact][boolean_field]',
  7025. 'id' => 'ContactBooleanField_',
  7026. 'value' => '0'
  7027. )),
  7028. array('input' => array(
  7029. 'type' => 'checkbox',
  7030. 'name' => 'data[Contact][boolean_field]',
  7031. 'value' => '1',
  7032. 'id' => 'ContactBooleanField'
  7033. )),
  7034. 'label' => array('for' => 'ContactBooleanField'),
  7035. 'Boolean Field',
  7036. '/label',
  7037. '/div'
  7038. );
  7039. $this->assertTags($result, $expected);
  7040. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7041. $expected = array(
  7042. 'div' => array('class' => 'input checkbox required'),
  7043. array('input' => array(
  7044. 'type' => 'hidden',
  7045. 'name' => 'data[Contact][boolean_field]',
  7046. 'id' => 'ContactBooleanField_',
  7047. 'value' => '0'
  7048. )),
  7049. array('input' => array(
  7050. 'type' => 'checkbox',
  7051. 'name' => 'data[Contact][boolean_field]',
  7052. 'value' => '1',
  7053. 'id' => 'ContactBooleanField',
  7054. 'required' => 'required'
  7055. )),
  7056. 'label' => array('for' => 'ContactBooleanField'),
  7057. 'Boolean Field',
  7058. '/label',
  7059. '/div'
  7060. );
  7061. $this->assertTags($result, $expected);
  7062. }
  7063. /**
  7064. * testFormMagicInput method
  7065. *
  7066. * @return void
  7067. */
  7068. public function testFormMagicInput() {
  7069. $encoding = strtolower(Configure::read('App.encoding'));
  7070. $result = $this->Form->create('Contact');
  7071. $expected = array(
  7072. 'form' => array(
  7073. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7074. 'accept-charset' => $encoding
  7075. ),
  7076. 'div' => array('style' => 'display:none;'),
  7077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7078. '/div'
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. $result = $this->Form->input('name');
  7082. $expected = array(
  7083. 'div' => array('class' => 'input text'),
  7084. 'label' => array('for' => 'ContactName'),
  7085. 'Name',
  7086. '/label',
  7087. 'input' => array(
  7088. 'type' => 'text', 'name' => 'data[Contact][name]',
  7089. 'id' => 'ContactName', 'maxlength' => '255'
  7090. ),
  7091. '/div'
  7092. );
  7093. $this->assertTags($result, $expected);
  7094. $result = $this->Form->input('non_existing_field_in_contact_model');
  7095. $expected = array(
  7096. 'div' => array('class' => 'input text'),
  7097. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7098. 'Non Existing Field In Contact Model',
  7099. '/label',
  7100. 'input' => array(
  7101. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7102. 'id' => 'ContactNonExistingFieldInContactModel'
  7103. ),
  7104. '/div'
  7105. );
  7106. $this->assertTags($result, $expected);
  7107. $result = $this->Form->input('Address.street');
  7108. $expected = array(
  7109. 'div' => array('class' => 'input text'),
  7110. 'label' => array('for' => 'AddressStreet'),
  7111. 'Street',
  7112. '/label',
  7113. 'input' => array(
  7114. 'type' => 'text', 'name' => 'data[Address][street]',
  7115. 'id' => 'AddressStreet'
  7116. ),
  7117. '/div'
  7118. );
  7119. $this->assertTags($result, $expected);
  7120. $result = $this->Form->input('Address.non_existing_field_in_model');
  7121. $expected = array(
  7122. 'div' => array('class' => 'input text'),
  7123. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7124. 'Non Existing Field In Model',
  7125. '/label',
  7126. 'input' => array(
  7127. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7128. 'id' => 'AddressNonExistingFieldInModel'
  7129. ),
  7130. '/div'
  7131. );
  7132. $this->assertTags($result, $expected);
  7133. $result = $this->Form->input('name', array('div' => false));
  7134. $expected = array(
  7135. 'label' => array('for' => 'ContactName'),
  7136. 'Name',
  7137. '/label',
  7138. 'input' => array(
  7139. 'type' => 'text', 'name' => 'data[Contact][name]',
  7140. 'id' => 'ContactName', 'maxlength' => '255'
  7141. )
  7142. );
  7143. $this->assertTags($result, $expected);
  7144. extract($this->dateRegex);
  7145. $now = strtotime('now');
  7146. $result = $this->Form->input('Contact.published', array('div' => false));
  7147. $expected = array(
  7148. 'label' => array('for' => 'ContactPublishedMonth'),
  7149. 'Published',
  7150. '/label',
  7151. array('select' => array(
  7152. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7153. )),
  7154. $monthsRegex,
  7155. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7156. date('F', $now),
  7157. '/option',
  7158. '*/select',
  7159. '-',
  7160. array('select' => array(
  7161. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7162. )),
  7163. $daysRegex,
  7164. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7165. date('j', $now),
  7166. '/option',
  7167. '*/select',
  7168. '-',
  7169. array('select' => array(
  7170. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7171. )),
  7172. $yearsRegex,
  7173. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7174. date('Y', $now),
  7175. '*/select'
  7176. );
  7177. $this->assertTags($result, $expected);
  7178. $result = $this->Form->input('Contact.updated', array('div' => false));
  7179. $expected = array(
  7180. 'label' => array('for' => 'ContactUpdatedMonth'),
  7181. 'Updated',
  7182. '/label',
  7183. array('select' => array(
  7184. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7185. )),
  7186. $monthsRegex,
  7187. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7188. date('F', $now),
  7189. '/option',
  7190. '*/select',
  7191. '-',
  7192. array('select' => array(
  7193. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7194. )),
  7195. $daysRegex,
  7196. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7197. date('j', $now),
  7198. '/option',
  7199. '*/select',
  7200. '-',
  7201. array('select' => array(
  7202. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7203. )),
  7204. $yearsRegex,
  7205. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7206. date('Y', $now),
  7207. '*/select'
  7208. );
  7209. $this->assertTags($result, $expected);
  7210. $result = $this->Form->input('UserForm.stuff');
  7211. $expected = array(
  7212. 'div' => array('class' => 'input text'),
  7213. 'label' => array('for' => 'UserFormStuff'),
  7214. 'Stuff',
  7215. '/label',
  7216. 'input' => array(
  7217. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7218. 'id' => 'UserFormStuff', 'maxlength' => 10
  7219. ),
  7220. '/div'
  7221. );
  7222. $this->assertTags($result, $expected);
  7223. }
  7224. /**
  7225. * testForMagicInputNonExistingNorValidated method
  7226. *
  7227. * @return void
  7228. */
  7229. public function testForMagicInputNonExistingNorValidated() {
  7230. $encoding = strtolower(Configure::read('App.encoding'));
  7231. $result = $this->Form->create('Contact');
  7232. $expected = array(
  7233. 'form' => array(
  7234. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7235. 'accept-charset' => $encoding
  7236. ),
  7237. 'div' => array('style' => 'display:none;'),
  7238. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7239. '/div'
  7240. );
  7241. $this->assertTags($result, $expected);
  7242. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7243. $expected = array(
  7244. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7245. 'Non Existing Nor Validated',
  7246. '/label',
  7247. 'input' => array(
  7248. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7249. 'id' => 'ContactNonExistingNorValidated'
  7250. )
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7254. 'div' => false, 'value' => 'my value'
  7255. ));
  7256. $expected = array(
  7257. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7258. 'Non Existing Nor Validated',
  7259. '/label',
  7260. 'input' => array(
  7261. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7262. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7263. )
  7264. );
  7265. $this->assertTags($result, $expected);
  7266. $this->Form->request->data = array(
  7267. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7268. ));
  7269. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7270. $expected = array(
  7271. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7272. 'Non Existing Nor Validated',
  7273. '/label',
  7274. 'input' => array(
  7275. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7276. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7277. )
  7278. );
  7279. $this->assertTags($result, $expected);
  7280. }
  7281. /**
  7282. * testFormMagicInputLabel method
  7283. *
  7284. * @return void
  7285. */
  7286. public function testFormMagicInputLabel() {
  7287. $encoding = strtolower(Configure::read('App.encoding'));
  7288. $result = $this->Form->create('Contact');
  7289. $expected = array(
  7290. 'form' => array(
  7291. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7292. 'accept-charset' => $encoding
  7293. ),
  7294. 'div' => array('style' => 'display:none;'),
  7295. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7296. '/div'
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7300. $this->assertTags($result, array('input' => array(
  7301. 'name' => 'data[Contact][name]', 'type' => 'text',
  7302. 'id' => 'ContactName', 'maxlength' => '255')
  7303. ));
  7304. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7305. $expected = array(
  7306. 'label' => array('for' => 'ContactName'),
  7307. 'My label',
  7308. '/label',
  7309. 'input' => array(
  7310. 'type' => 'text', 'name' => 'data[Contact][name]',
  7311. 'id' => 'ContactName', 'maxlength' => '255'
  7312. )
  7313. );
  7314. $this->assertTags($result, $expected);
  7315. $result = $this->Form->input('Contact.name', array(
  7316. 'div' => false, 'label' => array('class' => 'mandatory')
  7317. ));
  7318. $expected = array(
  7319. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7320. 'Name',
  7321. '/label',
  7322. 'input' => array(
  7323. 'type' => 'text', 'name' => 'data[Contact][name]',
  7324. 'id' => 'ContactName', 'maxlength' => '255'
  7325. )
  7326. );
  7327. $this->assertTags($result, $expected);
  7328. $result = $this->Form->input('Contact.name', array(
  7329. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7330. ));
  7331. $expected = array(
  7332. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7333. 'My label',
  7334. '/label',
  7335. 'input' => array(
  7336. 'type' => 'text', 'name' => 'data[Contact][name]',
  7337. 'id' => 'ContactName', 'maxlength' => '255'
  7338. )
  7339. );
  7340. $this->assertTags($result, $expected);
  7341. $result = $this->Form->input('Contact.name', array(
  7342. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7343. ));
  7344. $expected = array(
  7345. 'label' => array('for' => 'my_id'),
  7346. 'Name',
  7347. '/label',
  7348. 'input' => array(
  7349. 'type' => 'text', 'name' => 'data[Contact][name]',
  7350. 'id' => 'my_id', 'maxlength' => '255'
  7351. )
  7352. );
  7353. $this->assertTags($result, $expected);
  7354. $result = $this->Form->input('1.id');
  7355. $this->assertTags($result, array('input' => array(
  7356. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7357. 'id' => 'Contact1Id'
  7358. )));
  7359. $result = $this->Form->input("1.name");
  7360. $expected = array(
  7361. 'div' => array('class' => 'input text'),
  7362. 'label' => array('for' => 'Contact1Name'),
  7363. 'Name',
  7364. '/label',
  7365. 'input' => array(
  7366. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7367. 'id' => 'Contact1Name', 'maxlength' => '255'
  7368. ),
  7369. '/div'
  7370. );
  7371. $this->assertTags($result, $expected);
  7372. $result = $this->Form->input('Contact.1.id');
  7373. $this->assertTags($result, array(
  7374. 'input' => array(
  7375. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7376. 'id' => 'Contact1Id'
  7377. )
  7378. ));
  7379. $result = $this->Form->input("Model.1.name");
  7380. $expected = array(
  7381. 'div' => array('class' => 'input text'),
  7382. 'label' => array('for' => 'Model1Name'),
  7383. 'Name',
  7384. '/label',
  7385. 'input' => array(
  7386. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7387. 'id' => 'Model1Name'
  7388. ),
  7389. '/div'
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. }
  7393. /**
  7394. * testFormEnd method
  7395. *
  7396. * @return void
  7397. */
  7398. public function testFormEnd() {
  7399. $this->assertEquals('</form>', $this->Form->end());
  7400. $result = $this->Form->end('');
  7401. $expected = array(
  7402. 'div' => array('class' => 'submit'),
  7403. 'input' => array('type' => 'submit', 'value' => ''),
  7404. '/div',
  7405. '/form'
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $result = $this->Form->end(array('label' => ''));
  7409. $expected = array(
  7410. 'div' => array('class' => 'submit'),
  7411. 'input' => array('type' => 'submit', 'value' => ''),
  7412. '/div',
  7413. '/form'
  7414. );
  7415. $this->assertTags($result, $expected);
  7416. $result = $this->Form->end('save');
  7417. $expected = array(
  7418. 'div' => array('class' => 'submit'),
  7419. 'input' => array('type' => 'submit', 'value' => 'save'),
  7420. '/div',
  7421. '/form'
  7422. );
  7423. $this->assertTags($result, $expected);
  7424. $result = $this->Form->end(array('label' => 'save'));
  7425. $expected = array(
  7426. 'div' => array('class' => 'submit'),
  7427. 'input' => array('type' => 'submit', 'value' => 'save'),
  7428. '/div',
  7429. '/form'
  7430. );
  7431. $this->assertTags($result, $expected);
  7432. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7433. $expected = array(
  7434. 'div' => array('class' => 'submit'),
  7435. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7436. '/div',
  7437. '/form'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $result = $this->Form->end(array('name' => 'Whatever'));
  7441. $expected = array(
  7442. 'div' => array('class' => 'submit'),
  7443. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7444. '/div',
  7445. '/form'
  7446. );
  7447. $this->assertTags($result, $expected);
  7448. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7449. $expected = array(
  7450. 'div' => array('class' => 'good'),
  7451. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7452. '/div',
  7453. '/form'
  7454. );
  7455. $this->assertTags($result, $expected);
  7456. $result = $this->Form->end(array(
  7457. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7458. ));
  7459. $expected = array(
  7460. 'div' => array('class' => 'good'),
  7461. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7462. '/div',
  7463. '/form'
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. }
  7467. /**
  7468. * testMultipleFormWithIdFields method
  7469. *
  7470. * @return void
  7471. */
  7472. public function testMultipleFormWithIdFields() {
  7473. $this->Form->create('UserForm');
  7474. $result = $this->Form->input('id');
  7475. $this->assertTags($result, array('input' => array(
  7476. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7477. )));
  7478. $result = $this->Form->input('ValidateItem.id');
  7479. $this->assertTags($result, array('input' => array(
  7480. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7481. 'id' => 'ValidateItemId'
  7482. )));
  7483. $result = $this->Form->input('ValidateUser.id');
  7484. $this->assertTags($result, array('input' => array(
  7485. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7486. 'id' => 'ValidateUserId'
  7487. )));
  7488. }
  7489. /**
  7490. * testDbLessModel method
  7491. *
  7492. * @return void
  7493. */
  7494. public function testDbLessModel() {
  7495. $this->Form->create('TestMail');
  7496. $result = $this->Form->input('name');
  7497. $expected = array(
  7498. 'div' => array('class' => 'input text'),
  7499. 'label' => array('for' => 'TestMailName'),
  7500. 'Name',
  7501. '/label',
  7502. 'input' => array(
  7503. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7504. 'id' => 'TestMailName'
  7505. ),
  7506. '/div'
  7507. );
  7508. $this->assertTags($result, $expected);
  7509. ClassRegistry::init('TestMail');
  7510. $this->Form->create('TestMail');
  7511. $result = $this->Form->input('name');
  7512. $expected = array(
  7513. 'div' => array('class' => 'input text'),
  7514. 'label' => array('for' => 'TestMailName'),
  7515. 'Name',
  7516. '/label',
  7517. 'input' => array(
  7518. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7519. 'id' => 'TestMailName'
  7520. ),
  7521. '/div'
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. }
  7525. /**
  7526. * testBrokenness method
  7527. *
  7528. * @return void
  7529. */
  7530. public function testBrokenness() {
  7531. /*
  7532. * #4 This test has two parents and four children. By default (as of r7117) both
  7533. * parents are show but the first parent is missing a child. This is the inconsistency
  7534. * in the default behaviour - one parent has all children, the other does not - dependent
  7535. * on the data values.
  7536. */
  7537. $result = $this->Form->select('Model.field', array(
  7538. 'Fred' => array(
  7539. 'freds_son_1' => 'Fred',
  7540. 'freds_son_2' => 'Freddie'
  7541. ),
  7542. 'Bert' => array(
  7543. 'berts_son_1' => 'Albert',
  7544. 'berts_son_2' => 'Bertie')
  7545. ),
  7546. array('showParents' => true, 'empty' => false)
  7547. );
  7548. $expected = array(
  7549. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7550. array('optgroup' => array('label' => 'Fred')),
  7551. array('option' => array('value' => 'freds_son_1')),
  7552. 'Fred',
  7553. '/option',
  7554. array('option' => array('value' => 'freds_son_2')),
  7555. 'Freddie',
  7556. '/option',
  7557. '/optgroup',
  7558. array('optgroup' => array('label' => 'Bert')),
  7559. array('option' => array('value' => 'berts_son_1')),
  7560. 'Albert',
  7561. '/option',
  7562. array('option' => array('value' => 'berts_son_2')),
  7563. 'Bertie',
  7564. '/option',
  7565. '/optgroup',
  7566. '/select'
  7567. );
  7568. $this->assertTags($result, $expected);
  7569. /*
  7570. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7571. * changed, so we should expect the same select list data, just with a different name
  7572. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7573. * This is where data corruption can occur, because when a select value is missing from
  7574. * a list a form will substitute the first value in the list - without the user knowing.
  7575. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7576. * affect the availability of 3 => 'Three' as a valid option.
  7577. */
  7578. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7579. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7580. ));
  7581. $result = $this->Form->select(
  7582. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7583. );
  7584. $expected = array(
  7585. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7586. array('option' => array('value' => 1)),
  7587. 'One',
  7588. '/option',
  7589. array('option' => array('value' => 2)),
  7590. 'Two',
  7591. '/option',
  7592. array('optgroup' => array('label' => 'Three')),
  7593. array('option' => array('value' => 3)),
  7594. 'Three',
  7595. '/option',
  7596. array('option' => array('value' => 4)),
  7597. 'Four',
  7598. '/option',
  7599. array('option' => array('value' => 5)),
  7600. 'Five',
  7601. '/option',
  7602. '/optgroup',
  7603. '/select'
  7604. );
  7605. $this->assertTags($result, $expected);
  7606. }
  7607. /**
  7608. * Test the generation of fields for a multi record form.
  7609. *
  7610. * @return void
  7611. */
  7612. public function testMultiRecordForm() {
  7613. $this->Form->create('ValidateProfile');
  7614. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7615. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7616. $expected = array(
  7617. 'div' => array('class' => 'input textarea'),
  7618. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7619. 'Name',
  7620. '/label',
  7621. 'textarea' => array(
  7622. 'id' => 'ValidateProfile1ValidateItem2Name',
  7623. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7624. 'cols' => 30,
  7625. 'rows' => 6
  7626. ),
  7627. 'Value',
  7628. '/textarea',
  7629. '/div'
  7630. );
  7631. $this->assertTags($result, $expected);
  7632. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7633. $expected = array(
  7634. 'div' => array('class' => 'input date'),
  7635. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7636. 'Created',
  7637. '/label',
  7638. array('select' => array(
  7639. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7640. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7641. )
  7642. ),
  7643. array('option' => array('value' => '')), '/option',
  7644. $this->dateRegex['monthsRegex'],
  7645. '/select', '-',
  7646. array('select' => array(
  7647. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7648. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7649. )
  7650. ),
  7651. array('option' => array('value' => '')), '/option',
  7652. $this->dateRegex['daysRegex'],
  7653. '/select', '-',
  7654. array('select' => array(
  7655. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7656. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7657. )
  7658. ),
  7659. array('option' => array('value' => '')), '/option',
  7660. $this->dateRegex['yearsRegex'],
  7661. '/select',
  7662. '/div'
  7663. );
  7664. $this->assertTags($result, $expected);
  7665. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7666. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7667. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7668. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7669. $expected = array(
  7670. 'div' => array('class' => 'input select error'),
  7671. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7672. 'Profile',
  7673. '/label',
  7674. 'select' => array(
  7675. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7676. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7677. 'class' => 'form-error'
  7678. ),
  7679. '/select',
  7680. array('div' => array('class' => 'error-message')),
  7681. 'Error',
  7682. '/div',
  7683. '/div'
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. }
  7687. /**
  7688. * test the correct display of multi-record form validation errors.
  7689. *
  7690. * @return void
  7691. */
  7692. public function testMultiRecordFormValidationErrors() {
  7693. $this->Form->create('ValidateProfile');
  7694. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7695. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7696. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7697. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7698. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7699. $result = $this->Form->error('ValidateProfile.2.city');
  7700. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7701. $result = $this->Form->error('2.city');
  7702. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7703. }
  7704. /**
  7705. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7706. *
  7707. * @return void
  7708. */
  7709. public function testInputTemplate() {
  7710. $result = $this->Form->input('Contact.email', array(
  7711. 'type' => 'text', 'format' => array('input')
  7712. ));
  7713. $expected = array(
  7714. 'div' => array('class' => 'input text'),
  7715. 'input' => array(
  7716. 'type' => 'text', 'name' => 'data[Contact][email]',
  7717. 'id' => 'ContactEmail'
  7718. ),
  7719. '/div'
  7720. );
  7721. $this->assertTags($result, $expected);
  7722. $result = $this->Form->input('Contact.email', array(
  7723. 'type' => 'text', 'format' => array('input', 'label'),
  7724. 'label' => '<em>Email (required)</em>'
  7725. ));
  7726. $expected = array(
  7727. 'div' => array('class' => 'input text'),
  7728. array('input' => array(
  7729. 'type' => 'text', 'name' => 'data[Contact][email]',
  7730. 'id' => 'ContactEmail'
  7731. )),
  7732. 'label' => array('for' => 'ContactEmail'),
  7733. 'em' => array(),
  7734. 'Email (required)',
  7735. '/em',
  7736. '/label',
  7737. '/div'
  7738. );
  7739. $this->assertTags($result, $expected);
  7740. $result = $this->Form->input('Contact.email', array(
  7741. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7742. 'between' => '<div>Something in the middle</div>',
  7743. 'after' => '<span>Some text at the end</span>'
  7744. ));
  7745. $expected = array(
  7746. 'div' => array('class' => 'input text'),
  7747. array('input' => array(
  7748. 'type' => 'text', 'name' => 'data[Contact][email]',
  7749. 'id' => 'ContactEmail'
  7750. )),
  7751. array('div' => array()),
  7752. 'Something in the middle',
  7753. '/div',
  7754. 'label' => array('for' => 'ContactEmail'),
  7755. 'Email',
  7756. '/label',
  7757. 'span' => array(),
  7758. 'Some text at the end',
  7759. '/span',
  7760. '/div'
  7761. );
  7762. $this->assertTags($result, $expected);
  7763. $result = $this->Form->input('Contact.method', array(
  7764. 'type' => 'radio',
  7765. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7766. 'between' => 'I am between',
  7767. ));
  7768. $expected = array(
  7769. 'div' => array('class' => 'input radio'),
  7770. 'fieldset' => array(),
  7771. 'legend' => array(),
  7772. 'Method',
  7773. '/legend',
  7774. 'I am between',
  7775. 'input' => array(
  7776. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7777. 'value' => '', 'id' => 'ContactMethod_'
  7778. ),
  7779. array('input' => array(
  7780. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7781. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7782. )),
  7783. array('label' => array('for' => 'ContactMethodEmail')),
  7784. 'Email',
  7785. '/label',
  7786. array('input' => array(
  7787. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7788. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7789. )),
  7790. array('label' => array('for' => 'ContactMethodPigeon')),
  7791. 'Pigeon',
  7792. '/label',
  7793. '/fieldset',
  7794. '/div',
  7795. );
  7796. $this->assertTags($result, $expected);
  7797. }
  7798. /**
  7799. * test that some html5 inputs + FormHelper::__call() work
  7800. *
  7801. * @return void
  7802. */
  7803. public function testHtml5Inputs() {
  7804. $result = $this->Form->email('User.email');
  7805. $expected = array(
  7806. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7807. );
  7808. $this->assertTags($result, $expected);
  7809. $result = $this->Form->search('User.query');
  7810. $expected = array(
  7811. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7812. );
  7813. $this->assertTags($result, $expected);
  7814. $result = $this->Form->search('User.query', array('value' => 'test'));
  7815. $expected = array(
  7816. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7817. );
  7818. $this->assertTags($result, $expected);
  7819. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7820. $expected = array(
  7821. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7822. );
  7823. $this->assertTags($result, $expected);
  7824. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7825. $expected = array(
  7826. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7827. );
  7828. $this->assertTags($result, $expected);
  7829. }
  7830. /**
  7831. *
  7832. * @expectedException CakeException
  7833. * @return void
  7834. */
  7835. public function testHtml5InputException() {
  7836. $this->Form->email();
  7837. }
  7838. /**
  7839. * Tests that a model can be loaded from the model names passed in the request object
  7840. *
  7841. * @return void
  7842. */
  7843. public function testIntrospectModelFromRequest() {
  7844. $this->loadFixtures('Post');
  7845. App::build(array(
  7846. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7847. ));
  7848. CakePlugin::load('TestPlugin');
  7849. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7850. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7851. $this->Form->create('TestPluginPost');
  7852. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7853. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7854. CakePlugin::unload();
  7855. App::build();
  7856. }
  7857. /**
  7858. * Tests that it is possible to set the validation errors directly in the helper for a field
  7859. *
  7860. * @return void
  7861. */
  7862. public function testCustomValidationErrors() {
  7863. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7864. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7865. $this->assertEquals('Badness!', $result);
  7866. }
  7867. /**
  7868. * Tests that the 'on' key validates as expected on create
  7869. *
  7870. * @return void
  7871. */
  7872. public function testRequiredOnCreate() {
  7873. $this->Form->create('Contact');
  7874. $result = $this->Form->input('Contact.imrequiredonupdate');
  7875. $expected = array(
  7876. 'div' => array('class' => 'input text'),
  7877. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7878. 'Imrequiredonupdate',
  7879. '/label',
  7880. 'input' => array(
  7881. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7882. 'id' => 'ContactImrequiredonupdate'
  7883. ),
  7884. '/div'
  7885. );
  7886. $this->assertTags($result, $expected);
  7887. $result = $this->Form->input('Contact.imrequiredoncreate');
  7888. $expected = array(
  7889. 'div' => array('class' => 'input text required'),
  7890. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7891. 'Imrequiredoncreate',
  7892. '/label',
  7893. 'input' => array(
  7894. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7895. 'id' => 'ContactImrequiredoncreate',
  7896. 'required' => 'required'
  7897. ),
  7898. '/div'
  7899. );
  7900. $this->assertTags($result, $expected);
  7901. $result = $this->Form->input('Contact.imrequiredonboth');
  7902. $expected = array(
  7903. 'div' => array('class' => 'input text required'),
  7904. 'label' => array('for' => 'ContactImrequiredonboth'),
  7905. 'Imrequiredonboth',
  7906. '/label',
  7907. 'input' => array(
  7908. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7909. 'id' => 'ContactImrequiredonboth',
  7910. 'required' => 'required'
  7911. ),
  7912. '/div'
  7913. );
  7914. $this->assertTags($result, $expected);
  7915. $this->Form->inputDefaults(array('required' => false));
  7916. $result = $this->Form->input('Contact.imrequired');
  7917. $expected = array(
  7918. 'div' => array('class' => 'input text required'),
  7919. 'label' => array('for' => 'ContactImrequired'),
  7920. 'Imrequired',
  7921. '/label',
  7922. 'input' => array(
  7923. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7924. 'id' => 'ContactImrequired'
  7925. ),
  7926. '/div'
  7927. );
  7928. $this->assertTags($result, $expected);
  7929. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7930. $this->assertTags($result, $expected);
  7931. }
  7932. /**
  7933. * Tests that the 'on' key validates as expected on update
  7934. *
  7935. * @return void
  7936. */
  7937. public function testRequiredOnUpdate() {
  7938. $this->Form->request->data['Contact']['id'] = 1;
  7939. $this->Form->create('Contact');
  7940. $result = $this->Form->input('Contact.imrequiredonupdate');
  7941. $expected = array(
  7942. 'div' => array('class' => 'input text required'),
  7943. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7944. 'Imrequiredonupdate',
  7945. '/label',
  7946. 'input' => array(
  7947. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7948. 'id' => 'ContactImrequiredonupdate',
  7949. 'required' => 'required'
  7950. ),
  7951. '/div'
  7952. );
  7953. $this->assertTags($result, $expected);
  7954. $result = $this->Form->input('Contact.imrequiredoncreate');
  7955. $expected = array(
  7956. 'div' => array('class' => 'input text'),
  7957. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7958. 'Imrequiredoncreate',
  7959. '/label',
  7960. 'input' => array(
  7961. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7962. 'id' => 'ContactImrequiredoncreate'
  7963. ),
  7964. '/div'
  7965. );
  7966. $this->assertTags($result, $expected);
  7967. $result = $this->Form->input('Contact.imrequiredonboth');
  7968. $expected = array(
  7969. 'div' => array('class' => 'input text required'),
  7970. 'label' => array('for' => 'ContactImrequiredonboth'),
  7971. 'Imrequiredonboth',
  7972. '/label',
  7973. 'input' => array(
  7974. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7975. 'id' => 'ContactImrequiredonboth',
  7976. 'required' => 'required'
  7977. ),
  7978. '/div'
  7979. );
  7980. $this->assertTags($result, $expected);
  7981. $result = $this->Form->input('Contact.imrequired');
  7982. $expected = array(
  7983. 'div' => array('class' => 'input text required'),
  7984. 'label' => array('for' => 'ContactImrequired'),
  7985. 'Imrequired',
  7986. '/label',
  7987. 'input' => array(
  7988. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7989. 'id' => 'ContactImrequired',
  7990. 'required' => 'required'
  7991. ),
  7992. '/div'
  7993. );
  7994. $this->assertTags($result, $expected);
  7995. }
  7996. /**
  7997. * Test inputDefaults setter and getter
  7998. *
  7999. * @return void
  8000. */
  8001. public function testInputDefaults() {
  8002. $this->Form->create('Contact');
  8003. $this->Form->inputDefaults(array(
  8004. 'label' => false,
  8005. 'div' => array(
  8006. 'style' => 'color: #000;'
  8007. )
  8008. ));
  8009. $result = $this->Form->input('Contact.field1');
  8010. $expected = array(
  8011. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8012. 'input' => array(
  8013. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8014. 'id' => 'ContactField1'
  8015. ),
  8016. '/div'
  8017. );
  8018. $this->assertTags($result, $expected);
  8019. $this->Form->inputDefaults(array(
  8020. 'div' => false,
  8021. 'label' => 'Label',
  8022. ));
  8023. $result = $this->Form->input('Contact.field1');
  8024. $expected = array(
  8025. 'label' => array('for' => 'ContactField1'),
  8026. 'Label',
  8027. '/label',
  8028. 'input' => array(
  8029. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8030. 'id' => 'ContactField1'
  8031. ),
  8032. );
  8033. $this->assertTags($result, $expected);
  8034. $this->Form->inputDefaults(array(
  8035. 'label' => false,
  8036. ), true);
  8037. $result = $this->Form->input('Contact.field1');
  8038. $expected = array(
  8039. 'input' => array(
  8040. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8041. 'id' => 'ContactField1'
  8042. ),
  8043. );
  8044. $this->assertTags($result, $expected);
  8045. $result = $this->Form->inputDefaults();
  8046. $expected = array(
  8047. 'div' => false,
  8048. 'label' => false,
  8049. );
  8050. $this->assertEqual($result, $expected);
  8051. }
  8052. }