FormHelperTest.php 256 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151
  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 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $expected .= ':' . 'Model.valid';
  710. $expected = array(
  711. 'div' => array('style' => 'display:none;'),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  714. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  715. )),
  716. array('input' => array(
  717. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  718. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  719. )),
  720. '/div'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Tests correct generation of number fields for double and float fields
  726. *
  727. * @return void
  728. */
  729. public function testTextFieldGenerationForFloats() {
  730. $model = ClassRegistry::getObject('Contact');
  731. $model->setSchema(array('foo' => array(
  732. 'type' => 'float',
  733. 'null' => false,
  734. 'default' => null,
  735. 'length' => null
  736. )));
  737. $this->Form->create('Contact');
  738. $result = $this->Form->input('foo');
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => 'any'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. $result = $this->Form->input('foo', array('step' => 0.5));
  754. $expected = array(
  755. 'div' => array('class' => 'input number'),
  756. 'label' => array('for' => 'ContactFoo'),
  757. 'Foo',
  758. '/label',
  759. array('input' => array(
  760. 'type' => 'number',
  761. 'name' => 'data[Contact][foo]',
  762. 'id' => 'ContactFoo',
  763. 'step' => '0.5'
  764. )),
  765. '/div'
  766. );
  767. $this->assertTags($result, $expected);
  768. }
  769. /**
  770. * Tests correct generation of number fields for integer fields
  771. *
  772. * @access public
  773. * @return void
  774. */
  775. public function testTextFieldTypeNumberGenerationForIntegers() {
  776. $model = ClassRegistry::getObject('Contact');
  777. $model->setSchema(array('foo' => array(
  778. 'type' => 'integer',
  779. 'null' => false,
  780. 'default' => null,
  781. 'length' => null
  782. )));
  783. $this->Form->create('Contact');
  784. $result = $this->Form->input('foo');
  785. $expected = array(
  786. 'div' => array('class' => 'input number'),
  787. 'label' => array('for' => 'ContactFoo'),
  788. 'Foo',
  789. '/label',
  790. array('input' => array(
  791. 'type' => 'number', 'name' => 'data[Contact][foo]',
  792. 'id' => 'ContactFoo'
  793. )),
  794. '/div'
  795. );
  796. $this->assertTags($result, $expected);
  797. }
  798. /**
  799. * testFormSecurityMultipleFields method
  800. *
  801. * Test secure() with multiple row form. Ensure hash is correct.
  802. *
  803. * @return void
  804. */
  805. public function testFormSecurityMultipleFields() {
  806. $key = 'testKey';
  807. $fields = array(
  808. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  809. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  810. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  811. );
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $result = $this->Form->secure($fields);
  814. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  815. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  816. $expected = array(
  817. 'div' => array('style' => 'display:none;'),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  820. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  821. )),
  822. array('input' => array(
  823. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  824. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  825. )),
  826. '/div'
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * testFormSecurityMultipleSubmitButtons
  832. *
  833. * test form submit generation and ensure that _Token is only created on end()
  834. *
  835. * @return void
  836. */
  837. public function testFormSecurityMultipleSubmitButtons() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('Addresses');
  841. $this->Form->input('Address.title');
  842. $this->Form->input('Address.first_name');
  843. $result = $this->Form->submit('Save', array('name' => 'save'));
  844. $expected = array(
  845. 'div' => array('class' => 'submit'),
  846. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  847. '/div',
  848. );
  849. $this->assertTags($result, $expected);
  850. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  851. $expected = array(
  852. 'div' => array('class' => 'submit'),
  853. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  854. '/div',
  855. );
  856. $this->assertTags($result, $expected);
  857. $result = $this->Form->end(null);
  858. $expected = array(
  859. 'div' => array('style' => 'display:none;'),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  862. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  863. )),
  864. array('input' => array(
  865. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  866. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  867. )),
  868. '/div'
  869. );
  870. $this->assertTags($result, $expected);
  871. }
  872. /**
  873. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  874. *
  875. * @return void
  876. */
  877. public function testSecurityButtonNestedNamed() {
  878. $key = 'testKey';
  879. $this->Form->request['_Token'] = array('key' => $key);
  880. $this->Form->create('Addresses');
  881. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  882. $result = $this->Form->unlockField();
  883. $this->assertEquals(array('Address.button'), $result);
  884. }
  885. /**
  886. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecuritySubmitNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that the correct fields are unlocked for image submits with no names.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitImageNoName() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('User');
  907. $result = $this->Form->submit('save.png');
  908. $expected = array(
  909. 'div' => array('class' => 'submit'),
  910. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  911. '/div'
  912. );
  913. $this->assertTags($result, $expected);
  914. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  915. }
  916. /**
  917. * Test that the correct fields are unlocked for image submits with names.
  918. *
  919. * @return void
  920. */
  921. public function testSecuritySubmitImageName() {
  922. $key = 'testKey';
  923. $this->Form->request['_Token'] = array('key' => $key);
  924. $this->Form->create('User');
  925. $result = $this->Form->submit('save.png', array('name' => 'test'));
  926. $expected = array(
  927. 'div' => array('class' => 'submit'),
  928. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  929. '/div'
  930. );
  931. $this->assertTags($result, $expected);
  932. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  933. }
  934. /**
  935. * testFormSecurityMultipleInputFields method
  936. *
  937. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  938. *
  939. * @return void
  940. */
  941. public function testFormSecurityMultipleInputFields() {
  942. $key = 'testKey';
  943. $this->Form->request['_Token'] = array('key' => $key);
  944. $this->Form->create('Addresses');
  945. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  946. $this->Form->input('Addresses.0.title');
  947. $this->Form->input('Addresses.0.first_name');
  948. $this->Form->input('Addresses.0.last_name');
  949. $this->Form->input('Addresses.0.address');
  950. $this->Form->input('Addresses.0.city');
  951. $this->Form->input('Addresses.0.phone');
  952. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  953. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  954. $this->Form->input('Addresses.1.title');
  955. $this->Form->input('Addresses.1.first_name');
  956. $this->Form->input('Addresses.1.last_name');
  957. $this->Form->input('Addresses.1.address');
  958. $this->Form->input('Addresses.1.city');
  959. $this->Form->input('Addresses.1.phone');
  960. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  961. $result = $this->Form->secure($this->Form->fields);
  962. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  963. $expected = array(
  964. 'div' => array('style' => 'display:none;'),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  967. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  968. )),
  969. array('input' => array(
  970. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  971. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  972. )),
  973. '/div'
  974. );
  975. $this->assertTags($result, $expected);
  976. }
  977. /**
  978. * Test form security with Model.field.0 style inputs
  979. *
  980. * @return void
  981. */
  982. public function testFormSecurityArrayFields() {
  983. $key = 'testKey';
  984. $this->Form->request->params['_Token']['key'] = $key;
  985. $this->Form->create('Address');
  986. $this->Form->input('Address.primary.1');
  987. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  988. $this->Form->input('Address.secondary.1.0');
  989. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  990. }
  991. /**
  992. * testFormSecurityMultipleInputDisabledFields method
  993. *
  994. * test secure form generation with multiple records and disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityMultipleInputDisabledFields() {
  999. $key = 'testKey';
  1000. $this->Form->request->params['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.0.title');
  1007. $this->Form->input('Addresses.0.first_name');
  1008. $this->Form->input('Addresses.0.last_name');
  1009. $this->Form->input('Addresses.0.address');
  1010. $this->Form->input('Addresses.0.city');
  1011. $this->Form->input('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->input('Addresses.1.title');
  1014. $this->Form->input('Addresses.1.first_name');
  1015. $this->Form->input('Addresses.1.last_name');
  1016. $this->Form->input('Addresses.1.address');
  1017. $this->Form->input('Addresses.1.city');
  1018. $this->Form->input('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1025. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1029. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. }
  1035. /**
  1036. * testFormSecurityInputDisabledFields method
  1037. *
  1038. * Test single record form with disabled fields.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityInputUnlockedFields() {
  1043. $key = 'testKey';
  1044. $this->Form->request['_Token'] = array(
  1045. 'key' => $key,
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->input('Addresses.title');
  1052. $this->Form->input('Addresses.first_name');
  1053. $this->Form->input('Addresses.last_name');
  1054. $this->Form->input('Addresses.address');
  1055. $this->Form->input('Addresses.city');
  1056. $this->Form->input('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1069. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1070. )),
  1071. array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1073. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1074. )),
  1075. '/div'
  1076. );
  1077. $this->assertTags($result, $expected);
  1078. }
  1079. /**
  1080. * test securing inputs with custom name attributes.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecureWithCustomNameAttribute() {
  1085. $this->Form->request->params['_Token']['key'] = 'testKey';
  1086. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1087. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1088. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1089. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1090. }
  1091. /**
  1092. * testFormSecuredInput method
  1093. *
  1094. * Test generation of entire secure form, assertions made on input() output.
  1095. *
  1096. * @return void
  1097. */
  1098. public function testFormSecuredInput() {
  1099. $this->Form->request['_Token'] = array('key' => 'testKey');
  1100. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1101. $encoding = strtolower(Configure::read('App.encoding'));
  1102. $expected = array(
  1103. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1104. 'div' => array('style' => 'display:none;'),
  1105. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1108. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1114. $expected = array(
  1115. 'div' => array('class' => 'input text'),
  1116. 'label' => array('for' => 'UserFormPublished'),
  1117. 'Published',
  1118. '/label',
  1119. array('input' => array(
  1120. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1121. 'id' => 'UserFormPublished'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormOther'),
  1130. 'Other',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1134. 'id' => 'UserFormOther'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->hidden('UserForm.stuff');
  1140. $expected = array('input' => array(
  1141. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1142. 'id' => 'UserFormStuff'
  1143. ));
  1144. $this->assertTags($result, $expected);
  1145. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1146. $expected = array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1148. 'value' => '0', 'id' => 'UserFormHidden'
  1149. ));
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1152. $expected = array(
  1153. 'div' => array('class' => 'input checkbox'),
  1154. array('input' => array(
  1155. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1156. 'value' => '0', 'id' => 'UserFormSomething_'
  1157. )),
  1158. array('input' => array(
  1159. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1160. 'value' => '1', 'id' => 'UserFormSomething'
  1161. )),
  1162. 'label' => array('for' => 'UserFormSomething'),
  1163. 'Something',
  1164. '/label',
  1165. '/div'
  1166. );
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->fields;
  1169. $expected = array(
  1170. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1171. 'UserForm.hidden' => '0', 'UserForm.something'
  1172. );
  1173. $this->assertEquals($expected, $result);
  1174. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1175. $result = $this->Form->secure($this->Form->fields);
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1180. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1184. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * Tests that the correct keys are added to the field hash index
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredFileInput() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $result = $this->Form->file('Attachment.file');
  1199. $expected = array(
  1200. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1201. 'Attachment.file.error', 'Attachment.file.size'
  1202. );
  1203. $this->assertEquals($expected, $this->Form->fields);
  1204. }
  1205. /**
  1206. * test that multiple selects keys are added to field hash
  1207. *
  1208. * @return void
  1209. */
  1210. public function testFormSecuredMultipleSelect() {
  1211. $this->Form->request['_Token'] = array('key' => 'testKey');
  1212. $this->assertEquals(array(), $this->Form->fields);
  1213. $options = array('1' => 'one', '2' => 'two');
  1214. $this->Form->select('Model.select', $options);
  1215. $expected = array('Model.select');
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. $this->Form->fields = array();
  1218. $this->Form->select('Model.select', $options, array('multiple' => true));
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testFormSecuredRadio method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testFormSecuredRadio() {
  1227. $this->Form->request['_Token'] = array('key' => 'testKey');
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. $options = array('1' => 'option1', '2' => 'option2');
  1230. $this->Form->radio('Test.test', $options);
  1231. $expected = array('Test.test');
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1236. * hashing.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testFormSecuredAndDisabled() {
  1241. $this->Form->request['_Token'] = array('key' => 'testKey');
  1242. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1243. $this->Form->text('Model.text', array('disabled' => true));
  1244. $this->Form->password('Model.text', array('disabled' => true));
  1245. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1246. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1247. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1248. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1249. $this->Form->month('Model.month', array('disabled' => true));
  1250. $this->Form->day('Model.day', array('disabled' => true));
  1251. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1252. $this->Form->minute('Model.minute', array('disabled' => true));
  1253. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1254. $expected = array(
  1255. 'Model.radio' => ''
  1256. );
  1257. $this->assertEquals($expected, $this->Form->fields);
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = array('Please provide an email');
  1329. $this->assertEquals($expected, $result);
  1330. $this->Form->setEntity('Contact.1.email');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. $this->Form->setEntity('Contact.0.name');
  1335. $result = $this->Form->tagIsInvalid();
  1336. $expected = false;
  1337. $this->assertSame($expected, $result);
  1338. }
  1339. /**
  1340. * testPasswordValidation method
  1341. *
  1342. * test validation errors on password input.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testPasswordValidation() {
  1347. $Contact = ClassRegistry::getObject('Contact');
  1348. $Contact->validationErrors['password'] = array('Please provide a password');
  1349. $result = $this->Form->input('Contact.password');
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. array('div' => array('class' => 'error-message')),
  1360. 'Please provide a password',
  1361. '/div',
  1362. '/div'
  1363. );
  1364. $this->assertTags($result, $expected);
  1365. }
  1366. /**
  1367. * testEmptyErrorValidation method
  1368. *
  1369. * test validation error div when validation message is an empty string
  1370. *
  1371. * @access public
  1372. * @return void
  1373. */
  1374. public function testEmptyErrorValidation() {
  1375. $this->Form->validationErrors['Contact']['password'] = '';
  1376. $result = $this->Form->input('Contact.password');
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. array('div' => array('class' => 'error-message')),
  1387. array(),
  1388. '/div',
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. }
  1393. /**
  1394. * testEmptyInputErrorValidation method
  1395. *
  1396. * test validation error div when validation message is overridden by an empty string when calling input()
  1397. *
  1398. * @access public
  1399. * @return void
  1400. */
  1401. public function testEmptyInputErrorValidation() {
  1402. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1403. $result = $this->Form->input('Contact.password', array('error' => ''));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. array(),
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. }
  1420. /**
  1421. * testFormValidationAssociated method
  1422. *
  1423. * test display of form errors in conjunction with model::validates.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociated() {
  1428. $this->UserForm = ClassRegistry::getObject('UserForm');
  1429. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1430. $data = array(
  1431. 'UserForm' => array('name' => 'user'),
  1432. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1433. );
  1434. $result = $this->UserForm->OpenidUrl->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1437. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array(
  1441. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1442. 'accept-charset' => $encoding
  1443. ),
  1444. 'div' => array('style' => 'display:none;'),
  1445. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1446. '/div'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $result = $this->Form->error(
  1450. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1451. );
  1452. $this->assertEquals('Error, not registered', $result);
  1453. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1454. }
  1455. /**
  1456. * testFormValidationAssociatedFirstLevel method
  1457. *
  1458. * test form error display with associated model.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormValidationAssociatedFirstLevel() {
  1463. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1464. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1465. $data = array(
  1466. 'ValidateUser' => array('name' => 'mariano'),
  1467. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1468. );
  1469. $result = $this->ValidateUser->create($data);
  1470. $this->assertFalse(empty($result));
  1471. $this->assertFalse($this->ValidateUser->validates());
  1472. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1473. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1474. $encoding = strtolower(Configure::read('App.encoding'));
  1475. $expected = array(
  1476. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1477. 'div' => array('style' => 'display:none;'),
  1478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1479. '/div'
  1480. );
  1481. $this->assertTags($result, $expected);
  1482. $result = $this->Form->error(
  1483. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid email', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid name', $result);
  1490. $result = $this->Form->error(
  1491. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid city', $result);
  1494. unset($this->ValidateUser->ValidateProfile);
  1495. unset($this->ValidateUser);
  1496. }
  1497. /**
  1498. * testFormValidationAssociatedSecondLevel method
  1499. *
  1500. * test form error display with associated model.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormValidationAssociatedSecondLevel() {
  1505. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1506. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1507. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1508. $data = array(
  1509. 'ValidateUser' => array('name' => 'mariano'),
  1510. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1511. 'ValidateItem' => array('name' => 'Item')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1518. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1519. $encoding = strtolower(Configure::read('App.encoding'));
  1520. $expected = array(
  1521. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1522. 'div' => array('style' => 'display:none;'),
  1523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1524. '/div'
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->error(
  1528. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid email', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1533. );
  1534. $this->assertEquals('Invalid name', $result);
  1535. $result = $this->Form->error(
  1536. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1537. );
  1538. $result = $this->Form->error(
  1539. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid description', $result);
  1542. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1543. unset($this->ValidateUser->ValidateProfile);
  1544. unset($this->ValidateUser);
  1545. }
  1546. /**
  1547. * testFormValidationMultiRecord method
  1548. *
  1549. * test form error display with multiple records.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormValidationMultiRecord() {
  1554. $Contact = ClassRegistry::getObject('Contact');
  1555. $Contact->validationErrors[2] = array(
  1556. 'name' => array('This field cannot be left blank')
  1557. );
  1558. $result = $this->Form->input('Contact.2.name');
  1559. $expected = array(
  1560. 'div' => array('class' => 'input text error'),
  1561. 'label' => array('for' => 'Contact2Name'),
  1562. 'Name',
  1563. '/label',
  1564. 'input' => array(
  1565. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1566. 'class' => 'form-error', 'maxlength' => 255
  1567. ),
  1568. array('div' => array('class' => 'error-message')),
  1569. 'This field cannot be left blank',
  1570. '/div',
  1571. '/div'
  1572. );
  1573. $this->assertTags($result, $expected);
  1574. }
  1575. /**
  1576. * testMultipleInputValidation method
  1577. *
  1578. * test multiple record form validation error display.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testMultipleInputValidation() {
  1583. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1584. $Address->validationErrors[0] = array(
  1585. 'title' => array('This field cannot be empty'),
  1586. 'first_name' => array('This field cannot be empty')
  1587. );
  1588. $Address->validationErrors[1] = array(
  1589. 'last_name' => array('You must have a last name')
  1590. );
  1591. $this->Form->create();
  1592. $result = $this->Form->input('Address.0.title');
  1593. $expected = array(
  1594. 'div' => array('class'),
  1595. 'label' => array('for'),
  1596. 'preg:/[^<]+/',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1600. ),
  1601. array('div' => array('class' => 'error-message')),
  1602. 'This field cannot be empty',
  1603. '/div',
  1604. '/div'
  1605. );
  1606. $this->assertTags($result, $expected);
  1607. $result = $this->Form->input('Address.0.first_name');
  1608. $expected = array(
  1609. 'div' => array('class'),
  1610. 'label' => array('for'),
  1611. 'preg:/[^<]+/',
  1612. '/label',
  1613. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1614. array('div' => array('class' => 'error-message')),
  1615. 'This field cannot be empty',
  1616. '/div',
  1617. '/div'
  1618. );
  1619. $this->assertTags($result, $expected);
  1620. $result = $this->Form->input('Address.0.last_name');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'preg:/[^<]+/',
  1625. '/label',
  1626. 'input' => array('type' => 'text', 'name', 'id'),
  1627. '/div'
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->input('Address.1.last_name');
  1631. $expected = array(
  1632. 'div' => array('class'),
  1633. 'label' => array('for'),
  1634. 'preg:/[^<]+/',
  1635. '/label',
  1636. 'input' => array(
  1637. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1638. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1639. ),
  1640. array('div' => array('class' => 'error-message')),
  1641. 'You must have a last name',
  1642. '/div',
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. }
  1647. /**
  1648. * testInput method
  1649. *
  1650. * Test various incarnations of input().
  1651. *
  1652. * @return void
  1653. */
  1654. public function testInput() {
  1655. $result = $this->Form->input('ValidateUser.balance');
  1656. $expected = array(
  1657. 'div' => array('class'),
  1658. 'label' => array('for'),
  1659. 'Balance',
  1660. '/label',
  1661. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1662. '/div',
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1666. $expected = array(
  1667. 'div' => array('class' => 'input text'),
  1668. 'label' => array('for' => 'custom'),
  1669. 'Email',
  1670. '/label',
  1671. array('input' => array(
  1672. 'type' => 'text', 'name' => 'data[Contact][email]',
  1673. 'id' => 'custom', 'maxlength' => 255
  1674. )),
  1675. '/div'
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1679. $expected = array(
  1680. '<div',
  1681. 'label' => array('for' => 'ContactEmail'),
  1682. 'Email',
  1683. '/label',
  1684. array('input' => array(
  1685. 'type' => 'text', 'name' => 'data[Contact][email]',
  1686. 'id' => 'ContactEmail', 'maxlength' => 255
  1687. )),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. $result = $this->Form->hidden('Contact.idontexist');
  1692. $expected = array('input' => array(
  1693. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1694. 'id' => 'ContactIdontexist'
  1695. ));
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1698. $expected = array(
  1699. 'div' => array('class' => 'input text'),
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'text', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail'
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1711. $expected = array(
  1712. 'div' => array('class' => 'input text'),
  1713. 'label' => array('for' => 'Contact5Email'),
  1714. 'Email',
  1715. '/label',
  1716. array('input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1718. 'id' => 'Contact5Email'
  1719. )),
  1720. '/div'
  1721. );
  1722. $this->assertTags($result, $expected);
  1723. $result = $this->Form->input('Contact.password');
  1724. $expected = array(
  1725. 'div' => array('class' => 'input password'),
  1726. 'label' => array('for' => 'ContactPassword'),
  1727. 'Password',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'password', 'name' => 'data[Contact][password]',
  1731. 'id' => 'ContactPassword'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $result = $this->Form->input('Contact.email', array(
  1737. 'type' => 'file', 'class' => 'textbox'
  1738. ));
  1739. $expected = array(
  1740. 'div' => array('class' => 'input file'),
  1741. 'label' => array('for' => 'ContactEmail'),
  1742. 'Email',
  1743. '/label',
  1744. array('input' => array(
  1745. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1746. 'id' => 'ContactEmail'
  1747. )),
  1748. '/div'
  1749. );
  1750. $this->assertTags($result, $expected);
  1751. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1752. $result = $this->Form->input('Contact.phone');
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'ContactPhone'),
  1756. 'Phone',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1760. 'value' => 'Hello &amp; World &gt; weird chars',
  1761. 'id' => 'ContactPhone', 'maxlength' => 255
  1762. )),
  1763. '/div'
  1764. );
  1765. $this->assertTags($result, $expected);
  1766. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1767. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1768. $expected = array(
  1769. 'div' => array('class' => 'input text'),
  1770. 'label' => array('for' => 'myId'),
  1771. 'Field',
  1772. '/label',
  1773. 'input' => array(
  1774. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1775. 'value' => 'My value', 'id' => 'myId'
  1776. ),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. unset($this->Form->request->data);
  1781. $Contact = ClassRegistry::getObject('Contact');
  1782. $Contact->validationErrors['field'] = array('Badness!');
  1783. $result = $this->Form->input('Contact.field');
  1784. $expected = array(
  1785. 'div' => array('class' => 'input text error'),
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. array('div' => array('class' => 'error-message')),
  1794. 'Badness!',
  1795. '/div',
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. $result = $this->Form->input('Contact.field', array(
  1800. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1801. ));
  1802. $expected = array(
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('span' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/span'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1817. ));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text error'),
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('div' => array('class' => 'error')),
  1828. 'Badness!',
  1829. '/div'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1834. ));
  1835. $expected = array(
  1836. 'span' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. 'Badness!',
  1845. '/span'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input text error'),
  1851. 'label' => array('for' => 'ContactField'),
  1852. 'Field',
  1853. '/label',
  1854. 'input' => array(
  1855. 'type' => 'text', 'name' => 'data[Contact][field]',
  1856. 'id' => 'ContactField', 'class' => 'form-error'
  1857. ),
  1858. 'A message to you, Rudy',
  1859. array('div' => array('class' => 'error-message')),
  1860. 'Badness!',
  1861. '/div',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $this->Form->setEntity(null);
  1866. $this->Form->setEntity('Contact.field');
  1867. $result = $this->Form->input('Contact.field', array(
  1868. 'after' => 'A message to you, Rudy', 'error' => false
  1869. ));
  1870. $expected = array(
  1871. 'div' => array('class' => 'input text'),
  1872. 'label' => array('for' => 'ContactField'),
  1873. 'Field',
  1874. '/label',
  1875. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1876. 'A message to you, Rudy',
  1877. '/div'
  1878. );
  1879. $this->assertTags($result, $expected);
  1880. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1881. $expected = array(
  1882. 'div' => array('class' => 'input text'),
  1883. 'label' => array('for' => 'ObjectField'),
  1884. 'Field',
  1885. '/label',
  1886. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1887. 'A message to you, Rudy',
  1888. '/div'
  1889. );
  1890. $this->assertTags($result, $expected);
  1891. $Contact->validationErrors['field'] = array('minLength');
  1892. $result = $this->Form->input('Contact.field', array(
  1893. 'error' => array(
  1894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1895. 'maxLength' => 'login too large'
  1896. )
  1897. ));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text error'),
  1900. 'label' => array('for' => 'ContactField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1904. array('div' => array('class' => 'error-message')),
  1905. 'Le login doit contenir au moins 2 caractères',
  1906. '/div',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('maxLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1914. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1915. 'maxLength' => 'login too large',
  1916. )
  1917. ));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input text error'),
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1924. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1925. 'login too large',
  1926. '/span',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. }
  1931. /**
  1932. * Test that inputs with 0 can be created.
  1933. *
  1934. * @return void
  1935. */
  1936. public function testInputZero() {
  1937. $this->Form->create('User');
  1938. $result = $this->Form->input('0');
  1939. $expected = array(
  1940. 'div' => array('class' => 'input text'),
  1941. 'label' => array('for' => 'User0'), '/label',
  1942. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1943. '/div'
  1944. );
  1945. $this->assertTags($result, $expected);
  1946. }
  1947. /**
  1948. * test input() with checkbox creation
  1949. *
  1950. * @return void
  1951. */
  1952. public function testInputCheckbox() {
  1953. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1954. $expected = array(
  1955. 'div' => array('class' => 'input checkbox'),
  1956. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1957. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1958. '/div'
  1959. );
  1960. $this->assertTags($result, $expected);
  1961. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input checkbox'),
  1964. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1965. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1966. '/div'
  1967. );
  1968. $this->assertTags($result, $expected);
  1969. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1970. $expected = array(
  1971. 'div' => array('class' => 'input checkbox'),
  1972. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1973. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1974. '/div'
  1975. );
  1976. $this->assertTags($result, $expected);
  1977. }
  1978. /**
  1979. * test form->input() with time types.
  1980. *
  1981. */
  1982. public function testInputTime() {
  1983. extract($this->dateRegex);
  1984. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1985. $result = explode(':', $result);
  1986. $this->assertRegExp('/option value="23"/', $result[0]);
  1987. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1988. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1989. $result = explode(':', $result);
  1990. $this->assertRegExp('/option value="23"/', $result[0]);
  1991. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1992. $result = $this->Form->input('Model.field', array(
  1993. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1994. ));
  1995. $result = explode(':', $result);
  1996. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1997. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1998. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1999. $result = $this->Form->input('Model.field', array(
  2000. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2001. ));
  2002. $result = explode(':', $result);
  2003. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2004. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2005. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. $result = $this->Form->input('Random.start_time', array(
  2016. 'type' => 'time',
  2017. 'selected' => '18:15'
  2018. ));
  2019. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2020. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2021. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2022. }
  2023. /**
  2024. * Test interval + selected near the hour roll over.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testTimeSelectedWithInterval() {
  2029. $result = $this->Form->input('Model.start_time', array(
  2030. 'type' => 'time',
  2031. 'interval' => 15,
  2032. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2033. ));
  2034. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2037. $result = $this->Form->input('Model.start_time', array(
  2038. 'type' => 'time',
  2039. 'interval' => 15,
  2040. 'selected' => '2012-10-23 15:57:00'
  2041. ));
  2042. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2043. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2044. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2045. $result = $this->Form->input('Model.start_time', array(
  2046. 'timeFormat' => 24,
  2047. 'type' => 'time',
  2048. 'interval' => 15,
  2049. 'selected' => '15:57'
  2050. ));
  2051. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2052. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2053. $result = $this->Form->input('Model.start_time', array(
  2054. 'timeFormat' => 24,
  2055. 'type' => 'time',
  2056. 'interval' => 15,
  2057. 'selected' => '23:57'
  2058. ));
  2059. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2061. $result = $this->Form->input('Model.created', array(
  2062. 'timeFormat' => 24,
  2063. 'type' => 'datetime',
  2064. 'interval' => 15,
  2065. 'selected' => '2012-09-30 23:56'
  2066. ));
  2067. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2068. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2069. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2070. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2071. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2072. }
  2073. /**
  2074. * test form->input() with datetime, date and time types
  2075. *
  2076. * @return void
  2077. */
  2078. public function testInputDatetime() {
  2079. extract($this->dateRegex);
  2080. $result = $this->Form->input('prueba', array(
  2081. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2082. 'maxYear' => date('Y') + 1, 'interval' => 15
  2083. ));
  2084. $result = explode(':', $result);
  2085. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2086. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2087. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2088. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2089. //related to ticket #5013
  2090. $result = $this->Form->input('Contact.date', array(
  2091. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2092. ));
  2093. $this->assertRegExp('/class="customClass"/', $result);
  2094. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2095. $result = $this->Form->input('Contact.date', array(
  2096. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2097. ));
  2098. $this->assertRegExp('/id="customIdDay"/', $result);
  2099. $this->assertRegExp('/id="customIdMonth"/', $result);
  2100. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2101. $result = $this->Form->input('Model.field', array(
  2102. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2103. ));
  2104. $this->assertRegExp('/id="customIDDay"/', $result);
  2105. $this->assertRegExp('/id="customIDHour"/', $result);
  2106. $result = explode('</select><select', $result);
  2107. $result = explode(':', $result[1]);
  2108. $this->assertRegExp('/option value="23"/', $result[0]);
  2109. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2110. $result = $this->Form->input('Model.field', array(
  2111. 'type' => 'datetime', 'timeFormat' => 12
  2112. ));
  2113. $result = explode('</select><select', $result);
  2114. $result = explode(':', $result[1]);
  2115. $this->assertRegExp('/option value="12"/', $result[0]);
  2116. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2117. $this->Form->request->data = array('Contact' => array('created' => null));
  2118. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2119. $expected = array(
  2120. 'div' => array('class' => 'input date'),
  2121. 'label' => array('for' => 'ContactCreatedMonth'),
  2122. 'Created',
  2123. '/label',
  2124. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2125. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2126. $monthsRegex,
  2127. '/select', '-',
  2128. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2129. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2130. $daysRegex,
  2131. '/select', '-',
  2132. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2133. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2134. $yearsRegex,
  2135. '/select',
  2136. '/div'
  2137. );
  2138. $this->assertTags($result, $expected);
  2139. $this->Form->request->data = array('Contact' => array('created' => null));
  2140. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2141. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2142. $this->Form->request->data = array('Contact' => array('created' => null));
  2143. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2144. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2145. $result = $this->Form->input('Contact.created', array(
  2146. 'type' => 'date',
  2147. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2148. 'timeFormat' => 'NONE'
  2149. ));
  2150. $this->assertRegExp('/for\="created-month"/', $result);
  2151. }
  2152. /**
  2153. * Test generating checkboxes in a loop.
  2154. *
  2155. * @return void
  2156. */
  2157. public function testInputCheckboxesInLoop() {
  2158. for ($i = 1; $i < 5; $i++) {
  2159. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2160. $expected = array(
  2161. 'div' => array('class' => 'input checkbox'),
  2162. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2163. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2164. 'label' => array('for' => "Contact{$i}Email"),
  2165. 'Email',
  2166. '/label',
  2167. '/div'
  2168. );
  2169. $this->assertTags($result, $expected);
  2170. }
  2171. }
  2172. /**
  2173. * test input name with leading integer, ensure attributes are generated correctly.
  2174. *
  2175. * @return void
  2176. */
  2177. public function testInputWithLeadingInteger() {
  2178. $result = $this->Form->text('0.Node.title');
  2179. $expected = array(
  2180. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2181. );
  2182. $this->assertTags($result, $expected);
  2183. }
  2184. /**
  2185. * test form->input() with select type inputs.
  2186. *
  2187. * @return void
  2188. */
  2189. public function testInputSelectType() {
  2190. $result = $this->Form->input('email', array(
  2191. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2192. );
  2193. $expected = array(
  2194. 'div' => array('class' => 'input select'),
  2195. 'label' => array('for' => 'email'),
  2196. 'Email',
  2197. '/label',
  2198. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2199. array('option' => array('value' => '')),
  2200. '/option',
  2201. array('option' => array('value' => 'è')),
  2202. 'Firést',
  2203. '/option',
  2204. array('option' => array('value' => 'é')),
  2205. 'Secoènd',
  2206. '/option',
  2207. '/select',
  2208. '/div'
  2209. );
  2210. $this->assertTags($result, $expected);
  2211. $result = $this->Form->input('email', array(
  2212. 'options' => array('First', 'Second'), 'empty' => true)
  2213. );
  2214. $expected = array(
  2215. 'div' => array('class' => 'input select'),
  2216. 'label' => array('for' => 'email'),
  2217. 'Email',
  2218. '/label',
  2219. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2220. array('option' => array('value' => '')),
  2221. '/option',
  2222. array('option' => array('value' => '0')),
  2223. 'First',
  2224. '/option',
  2225. array('option' => array('value' => '1')),
  2226. 'Second',
  2227. '/option',
  2228. '/select',
  2229. '/div'
  2230. );
  2231. $this->assertTags($result, $expected);
  2232. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2233. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2234. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2235. $expected = array(
  2236. 'div' => array('class' => 'input select'),
  2237. 'label' => array('for' => 'ModelUserId'),
  2238. 'User',
  2239. '/label',
  2240. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2241. array('option' => array('value' => '')),
  2242. '/option',
  2243. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2244. 'good',
  2245. '/option',
  2246. array('option' => array('value' => 'other')),
  2247. 'bad',
  2248. '/option',
  2249. '/select',
  2250. '/div'
  2251. );
  2252. $this->assertTags($result, $expected);
  2253. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2254. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2255. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2256. $expected = array(
  2257. 'div' => array('class' => 'input select'),
  2258. 'label' => array('for' => 'ThingUserId'),
  2259. 'User',
  2260. '/label',
  2261. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2262. array('option' => array('value' => '')),
  2263. 'Some Empty',
  2264. '/option',
  2265. array('option' => array('value' => 'value')),
  2266. 'good',
  2267. '/option',
  2268. array('option' => array('value' => 'other')),
  2269. 'bad',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2276. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2277. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'ThingUserId'),
  2281. 'User',
  2282. '/label',
  2283. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2284. array('option' => array('value' => '')),
  2285. 'Some Empty',
  2286. '/option',
  2287. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2288. 'good',
  2289. '/option',
  2290. array('option' => array('value' => 'other')),
  2291. 'bad',
  2292. '/option',
  2293. '/select',
  2294. '/div'
  2295. );
  2296. $this->assertTags($result, $expected);
  2297. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2298. $this->Form->request->data = array('User' => array('User' => array('value')));
  2299. $result = $this->Form->input('User.User', array('empty' => true));
  2300. $expected = array(
  2301. 'div' => array('class' => 'input select'),
  2302. 'label' => array('for' => 'UserUser'),
  2303. 'User',
  2304. '/label',
  2305. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2306. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2307. array('option' => array('value' => '')),
  2308. '/option',
  2309. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2310. 'good',
  2311. '/option',
  2312. array('option' => array('value' => 'other')),
  2313. 'bad',
  2314. '/option',
  2315. '/select',
  2316. '/div'
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $this->Form->data = array();
  2320. $result = $this->Form->input('Publisher.id', array(
  2321. 'label' => 'Publisher',
  2322. 'type' => 'select',
  2323. 'multiple' => 'checkbox',
  2324. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2325. ));
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => 'PublisherId')),
  2329. 'Publisher',
  2330. '/label',
  2331. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2334. array('label' => array('for' => 'PublisherIdValue1')),
  2335. 'Label 1',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2340. array('label' => array('for' => 'PublisherIdValue2')),
  2341. 'Label 2',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $this->assertTags($result, $expected);
  2347. }
  2348. /**
  2349. * test that input() and a non standard primary key makes a hidden input by default.
  2350. *
  2351. * @return void
  2352. */
  2353. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2354. $this->Form->create('User');
  2355. $this->Form->fieldset = array(
  2356. 'User' => array(
  2357. 'fields' => array(
  2358. 'model_id' => array('type' => 'integer')
  2359. ),
  2360. 'validates' => array(),
  2361. 'key' => 'model_id'
  2362. )
  2363. );
  2364. $result = $this->Form->input('model_id');
  2365. $expected = array(
  2366. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. }
  2370. /**
  2371. * test that overriding the magic select type widget is possible
  2372. *
  2373. * @return void
  2374. */
  2375. public function testInputOverridingMagicSelectType() {
  2376. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2377. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2378. $expected = array(
  2379. 'div' => array('class' => 'input text'),
  2380. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2381. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2382. '/div'
  2383. );
  2384. $this->assertTags($result, $expected);
  2385. //Check that magic types still work for plural/singular vars
  2386. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2387. $result = $this->Form->input('Model.type');
  2388. $expected = array(
  2389. 'div' => array('class' => 'input select'),
  2390. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2391. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2392. array('option' => array('value' => 'value')), 'good', '/option',
  2393. array('option' => array('value' => 'other')), 'bad', '/option',
  2394. '/select',
  2395. '/div'
  2396. );
  2397. $this->assertTags($result, $expected);
  2398. }
  2399. /**
  2400. * Test that magic input() selects are created for type=number
  2401. *
  2402. * @return void
  2403. */
  2404. public function testInputMagicSelectForTypeNumber() {
  2405. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2406. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2407. $result = $this->Form->input('ValidateUser.balance');
  2408. $expected = array(
  2409. 'div' => array('class' => 'input select'),
  2410. 'label' => array('for' => 'ValidateUserBalance'),
  2411. 'Balance',
  2412. '/label',
  2413. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2414. array('option' => array('value' => '0')),
  2415. 'nothing',
  2416. '/option',
  2417. array('option' => array('value' => '1', 'selected' => 'selected')),
  2418. 'some',
  2419. '/option',
  2420. array('option' => array('value' => '100')),
  2421. 'a lot',
  2422. '/option',
  2423. '/select',
  2424. '/div'
  2425. );
  2426. $this->assertTags($result, $expected);
  2427. }
  2428. /**
  2429. * Test that magic input() selects can easily be converted into radio types without error.
  2430. *
  2431. * @return void
  2432. */
  2433. public function testInputMagicSelectChangeToRadio() {
  2434. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2435. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2436. $this->assertRegExp('/input type="radio"/', $result);
  2437. }
  2438. /**
  2439. * fields with the same name as the model should work.
  2440. *
  2441. * @return void
  2442. */
  2443. public function testInputWithMatchingFieldAndModelName() {
  2444. $this->Form->create('User');
  2445. $this->Form->fieldset = array(
  2446. 'User' => array(
  2447. 'fields' => array(
  2448. 'User' => array('type' => 'text')
  2449. ),
  2450. 'validates' => array(),
  2451. 'key' => 'id'
  2452. )
  2453. );
  2454. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2455. $result = $this->Form->input('User', array('type' => 'text'));
  2456. $expected = array(
  2457. 'div' => array('class' => 'input text'),
  2458. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2459. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2460. '/div'
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. }
  2464. /**
  2465. * testFormInputs method
  2466. *
  2467. * test correct results from form::inputs().
  2468. *
  2469. * @return void
  2470. */
  2471. public function testFormInputs() {
  2472. $this->Form->create('Contact');
  2473. $result = $this->Form->inputs('The Legend');
  2474. $expected = array(
  2475. '<fieldset',
  2476. '<legend',
  2477. 'The Legend',
  2478. '/legend',
  2479. '*/fieldset',
  2480. );
  2481. $this->assertTags($result, $expected);
  2482. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2483. $expected = array(
  2484. 'fieldset' => array('class' => 'classy-stuff'),
  2485. '<legend',
  2486. 'Field of Dreams',
  2487. '/legend',
  2488. '*/fieldset'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->Form->create('Contact');
  2492. $this->Form->request['prefix'] = 'admin';
  2493. $this->Form->request['action'] = 'admin_edit';
  2494. $result = $this->Form->inputs();
  2495. $expected = array(
  2496. '<fieldset',
  2497. '<legend',
  2498. 'Edit Contact',
  2499. '/legend',
  2500. '*/fieldset',
  2501. );
  2502. $this->assertTags($result, $expected);
  2503. $this->Form->create('Contact');
  2504. $result = $this->Form->inputs(false);
  2505. $expected = array(
  2506. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2507. array('div' => array('class' => 'input text')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input text')),
  2510. '*/div',
  2511. array('div' => array('class' => 'input text')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input password')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input date')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input date')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input datetime')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input number')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input select')),
  2524. '*/div',
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2529. $expected = array(
  2530. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2531. array('div' => array('class' => 'input text')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input text')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input password')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input date')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input date')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input datetime')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input number')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input select')),
  2548. '*/div',
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. $this->Form->create('Contact');
  2552. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2553. $expected = array(
  2554. 'fieldset' => array(),
  2555. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2556. array('div' => array('class' => 'input text')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input text')),
  2559. '*/div',
  2560. array('div' => array('class' => 'input text')),
  2561. '*/div',
  2562. array('div' => array('class' => 'input password')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input date')),
  2565. '*/div',
  2566. array('div' => array('class' => 'input date')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input datetime')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input number')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input select')),
  2573. '*/div',
  2574. '/fieldset'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. $this->Form->create('Contact');
  2578. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2579. $expected = array(
  2580. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2581. array('div' => array('class' => 'input text')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input text')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input text')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input password')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input date')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input date')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input datetime')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input number')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input select')),
  2598. '*/div',
  2599. );
  2600. $this->assertTags($result, $expected);
  2601. $this->Form->create('Contact');
  2602. $result = $this->Form->inputs('Hello');
  2603. $expected = array(
  2604. 'fieldset' => array(),
  2605. 'legend' => array(),
  2606. 'Hello',
  2607. '/legend',
  2608. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2609. array('div' => array('class' => 'input text')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input text')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input text')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input password')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input date')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input date')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input datetime')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input number')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input select')),
  2626. '*/div',
  2627. '/fieldset'
  2628. );
  2629. $this->assertTags($result, $expected);
  2630. $this->Form->create('Contact');
  2631. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2632. $expected = array(
  2633. 'fieldset' => array(),
  2634. 'legend' => array(),
  2635. 'Hello',
  2636. '/legend',
  2637. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2638. array('div' => array('class' => 'input text')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input text')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input text')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input password')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input date')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input date')),
  2649. '*/div',
  2650. array('div' => array('class' => 'input datetime')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input number')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input select')),
  2655. '*/div',
  2656. '/fieldset'
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. }
  2660. /**
  2661. * testSelectAsCheckbox method
  2662. *
  2663. * test multi-select widget with checkbox formatting.
  2664. *
  2665. * @return void
  2666. */
  2667. public function testSelectAsCheckbox() {
  2668. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2669. $expected = array(
  2670. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2671. array('div' => array('class' => 'checkbox')),
  2672. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2673. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2674. 'first',
  2675. '/label',
  2676. '/div',
  2677. array('div' => array('class' => 'checkbox')),
  2678. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2679. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2680. 'second',
  2681. '/label',
  2682. '/div',
  2683. array('div' => array('class' => 'checkbox')),
  2684. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2685. array('label' => array('for' => 'ModelMultiField2')),
  2686. 'third',
  2687. '/label',
  2688. '/div',
  2689. );
  2690. $this->assertTags($result, $expected);
  2691. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2692. $expected = array(
  2693. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2694. array('div' => array('class' => 'checkbox')),
  2695. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2696. array('label' => array('for' => 'ModelMultiField12')),
  2697. 'half',
  2698. '/label',
  2699. '/div',
  2700. );
  2701. $this->assertTags($result, $expected);
  2702. }
  2703. /**
  2704. * testLabel method
  2705. *
  2706. * test label generation.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testLabel() {
  2711. $this->Form->text('Person.name');
  2712. $result = $this->Form->label();
  2713. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2714. $this->Form->text('Person.name');
  2715. $result = $this->Form->label();
  2716. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2717. $result = $this->Form->label('Person.first_name');
  2718. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2719. $result = $this->Form->label('Person.first_name', 'Your first name');
  2720. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2721. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2722. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2723. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2724. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2725. $result = $this->Form->label('Person.first_name', '');
  2726. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2727. $result = $this->Form->label('Person.2.name', '');
  2728. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2729. }
  2730. /**
  2731. * testTextbox method
  2732. *
  2733. * test textbox element generation
  2734. *
  2735. * @return void
  2736. */
  2737. public function testTextbox() {
  2738. $result = $this->Form->text('Model.field');
  2739. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2740. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2741. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2742. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2743. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2744. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2745. $result = $this->Form->text('Model.text');
  2746. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2747. $Contact = ClassRegistry::getObject('Contact');
  2748. $Contact->validationErrors['text'] = array(true);
  2749. $this->Form->request->data['Contact']['text'] = 'test';
  2750. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2751. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2752. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2753. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2754. $expected = array(
  2755. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. }
  2759. /**
  2760. * testDefaultValue method
  2761. *
  2762. * Test default value setting
  2763. *
  2764. * @return void
  2765. */
  2766. public function testDefaultValue() {
  2767. $this->Form->request->data['Model']['field'] = 'test';
  2768. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2769. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2770. unset($this->Form->request->data['Model']['field']);
  2771. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2772. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2773. }
  2774. /**
  2775. * testCheckboxDefaultValue method
  2776. *
  2777. * Test default value setting on checkbox() method
  2778. *
  2779. * @return void
  2780. */
  2781. public function testCheckboxDefaultValue() {
  2782. $this->Form->request->data['Model']['field'] = false;
  2783. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2784. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2785. unset($this->Form->request->data['Model']['field']);
  2786. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2787. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2788. $this->Form->request->data['Model']['field'] = true;
  2789. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2790. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2791. unset($this->Form->request->data['Model']['field']);
  2792. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2793. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2794. }
  2795. /**
  2796. * testError method
  2797. *
  2798. * Test field error generation
  2799. *
  2800. * @return void
  2801. */
  2802. public function testError() {
  2803. $Contact = ClassRegistry::getObject('Contact');
  2804. $Contact->validationErrors['field'] = array(1);
  2805. $result = $this->Form->error('Contact.field');
  2806. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2807. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2808. $this->assertEquals('Error in field Field', $result);
  2809. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2810. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2811. $this->assertEquals('This field contains invalid input', $result);
  2812. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2813. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2814. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2815. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2816. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2817. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2818. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2819. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2820. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2821. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2822. $this->assertEquals('<strong>Badness!</strong>', $result);
  2823. $Contact->validationErrors['field'] = array("email");
  2824. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2825. $expected = array(
  2826. 'div' => array('class' => 'field-error'),
  2827. 'No good!',
  2828. '/div'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2832. $result = $this->Form->error('Contact.field', array(
  2833. 'notEmpty' => 'Cannot be empty',
  2834. 'email' => 'No good!'
  2835. ));
  2836. $expected = array(
  2837. 'div' => array('class' => 'error-message'),
  2838. 'ul' => array(),
  2839. '<li', 'Cannot be empty', '/li',
  2840. '<li', 'No good!', '/li',
  2841. '<li', 'Something else', '/li',
  2842. '/ul',
  2843. '/div'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. /** Testing error messages list options **/
  2847. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2848. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2849. $expected = array(
  2850. 'div' => array('class' => 'error-message'),
  2851. 'ol' => array(),
  2852. '<li', 'notEmpty', '/li',
  2853. '<li', 'email', '/li',
  2854. '/ol',
  2855. '/div'
  2856. );
  2857. $this->assertTags($result, $expected);
  2858. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2859. $expected = array(
  2860. 'div' => array('class' => 'error-message'),
  2861. 'ol' => array(),
  2862. '<li', 'notEmpty', '/li',
  2863. '<li', 'email', '/li',
  2864. '/ol',
  2865. '/div'
  2866. );
  2867. $this->assertTags($result, $expected);
  2868. $result = $this->Form->error('Contact.field', null, array(
  2869. 'listOptions' => array(
  2870. 'class' => 'ul-class',
  2871. 'itemOptions' => array(
  2872. 'class' => 'li-class'
  2873. )
  2874. )
  2875. ));
  2876. $expected = array(
  2877. 'div' => array('class' => 'error-message'),
  2878. 'ul' => array('class' => 'ul-class'),
  2879. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2880. array('li' => array('class' => 'li-class')), 'email', '/li',
  2881. '/ul',
  2882. '/div'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. }
  2886. /**
  2887. * test error options when using form->input();
  2888. *
  2889. * @return void
  2890. */
  2891. public function testInputErrorEscape() {
  2892. $this->Form->create('ValidateProfile');
  2893. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2894. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2895. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2896. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2897. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2898. $this->assertRegExp('/required<br>/', $result);
  2899. }
  2900. /**
  2901. * testPassword method
  2902. *
  2903. * Test password element generation
  2904. *
  2905. * @return void
  2906. */
  2907. public function testPassword() {
  2908. $Contact = ClassRegistry::getObject('Contact');
  2909. $result = $this->Form->password('Contact.field');
  2910. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2911. $Contact->validationErrors['passwd'] = 1;
  2912. $this->Form->request->data['Contact']['passwd'] = 'test';
  2913. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2914. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2915. }
  2916. /**
  2917. * testRadio method
  2918. *
  2919. * Test radio element set generation
  2920. *
  2921. * @return void
  2922. */
  2923. public function testRadio() {
  2924. $result = $this->Form->radio('Model.field', array('option A'));
  2925. $expected = array(
  2926. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2927. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2928. 'label' => array('for' => 'ModelField0'),
  2929. 'option A',
  2930. '/label'
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2934. $expected = array(
  2935. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2936. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2937. 'label' => array('for' => 'ModelField12'),
  2938. 'half',
  2939. '/label'
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2943. $expected = array(
  2944. 'fieldset' => array(),
  2945. 'legend' => array(),
  2946. 'Field',
  2947. '/legend',
  2948. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2949. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2950. array('label' => array('for' => 'ModelField0')),
  2951. 'option A',
  2952. '/label',
  2953. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2954. array('label' => array('for' => 'ModelField1')),
  2955. 'option B',
  2956. '/label',
  2957. '/fieldset'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2961. $expected = array(
  2962. 'fieldset' => array(),
  2963. 'legend' => array(),
  2964. 'Field',
  2965. '/legend',
  2966. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2968. array('label' => array('for' => 'ModelField0')),
  2969. 'option A',
  2970. '/label',
  2971. 'br' => array(),
  2972. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2973. array('label' => array('for' => 'ModelField1')),
  2974. 'option B',
  2975. '/label',
  2976. '/fieldset'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2980. $expected = array(
  2981. 'fieldset' => array(),
  2982. 'legend' => array(),
  2983. 'Gender',
  2984. '/legend',
  2985. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2986. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2987. array('label' => array('for' => 'EmployeeGenderMale')),
  2988. 'Male',
  2989. '/label',
  2990. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  2991. array('label' => array('for' => 'EmployeeGenderFemale')),
  2992. 'Female',
  2993. '/label',
  2994. '/fieldset',
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  2998. $expected = array(
  2999. 'fieldset' => array(),
  3000. 'legend' => array(),
  3001. 'Gender',
  3002. '/legend',
  3003. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3004. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3005. array('label' => array('for' => 'OfficerGenderMale')),
  3006. 'Male',
  3007. '/label',
  3008. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3009. array('label' => array('for' => 'OfficerGenderFemale')),
  3010. 'Female',
  3011. '/label',
  3012. '/fieldset',
  3013. );
  3014. $this->assertTags($result, $expected);
  3015. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3016. $expected = array(
  3017. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3018. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3019. 'label' => array('for' => 'Contact1Imrequired0'),
  3020. 'option A',
  3021. '/label'
  3022. );
  3023. $this->assertTags($result, $expected);
  3024. $result = $this->Form->radio('Model.1.field', array('option A'));
  3025. $expected = array(
  3026. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3027. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3028. 'label' => array('for' => 'Model1Field0'),
  3029. 'option A',
  3030. '/label'
  3031. );
  3032. $this->assertTags($result, $expected);
  3033. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3034. $expected = array(
  3035. 'fieldset' => array(),
  3036. 'legend' => array(),
  3037. 'Field',
  3038. '/legend',
  3039. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3040. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3041. array('label' => array('for' => 'ModelField0')),
  3042. 'option A',
  3043. '/label',
  3044. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3045. array('label' => array('for' => 'ModelField1')),
  3046. 'option B',
  3047. '/label',
  3048. '/fieldset'
  3049. );
  3050. $this->assertTags($result, $expected);
  3051. $result = $this->Form->radio(
  3052. 'Model.field',
  3053. array('option A', 'option B'),
  3054. array('between' => 'I am between')
  3055. );
  3056. $expected = array(
  3057. 'fieldset' => array(),
  3058. 'legend' => array(),
  3059. 'Field',
  3060. '/legend',
  3061. 'I am between',
  3062. 'input' => array(
  3063. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3064. 'value' => '', 'id' => 'ModelField_'
  3065. ),
  3066. array('input' => array(
  3067. 'type' => 'radio', 'name' => 'data[Model][field]',
  3068. 'value' => '0', 'id' => 'ModelField0'
  3069. )),
  3070. array('label' => array('for' => 'ModelField0')),
  3071. 'option A',
  3072. '/label',
  3073. array('input' => array(
  3074. 'type' => 'radio', 'name' => 'data[Model][field]',
  3075. 'value' => '1', 'id' => 'ModelField1'
  3076. )),
  3077. array('label' => array('for' => 'ModelField1')),
  3078. 'option B',
  3079. '/label',
  3080. '/fieldset'
  3081. );
  3082. $this->assertTags($result, $expected);
  3083. }
  3084. /**
  3085. * Test that radios with a 0 value are selected under the correct conditions.
  3086. * Also ensure that values that are booleanish are handled correctly.
  3087. *
  3088. * @return void
  3089. */
  3090. public function testRadioOptionWithBooleanishValues() {
  3091. $expected = array(
  3092. 'fieldset' => array(),
  3093. 'legend' => array(),
  3094. 'Field',
  3095. '/legend',
  3096. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3097. array('label' => array('for' => 'ModelField1')),
  3098. 'Yes',
  3099. '/label',
  3100. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3101. array('label' => array('for' => 'ModelField0')),
  3102. 'No',
  3103. '/label',
  3104. '/fieldset'
  3105. );
  3106. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3107. $this->assertTags($result, $expected);
  3108. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3109. $this->assertTags($result, $expected);
  3110. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3111. $this->assertTags($result, $expected);
  3112. $expected = array(
  3113. 'fieldset' => array(),
  3114. 'legend' => array(),
  3115. 'Field',
  3116. '/legend',
  3117. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3118. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3119. array('label' => array('for' => 'ModelField1')),
  3120. 'Yes',
  3121. '/label',
  3122. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3123. array('label' => array('for' => 'ModelField0')),
  3124. 'No',
  3125. '/label',
  3126. '/fieldset'
  3127. );
  3128. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3129. $this->assertTags($result, $expected);
  3130. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3131. $this->assertTags($result, $expected);
  3132. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3133. $this->assertTags($result, $expected);
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Field',
  3138. '/legend',
  3139. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3140. array('label' => array('for' => 'ModelField1')),
  3141. 'Yes',
  3142. '/label',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3144. array('label' => array('for' => 'ModelField0')),
  3145. 'No',
  3146. '/label',
  3147. '/fieldset'
  3148. );
  3149. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3150. $this->assertTags($result, $expected);
  3151. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3152. $this->assertTags($result, $expected);
  3153. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3154. $this->assertTags($result, $expected);
  3155. }
  3156. /**
  3157. * test disabled radio options
  3158. *
  3159. * @return void
  3160. */
  3161. public function testRadioDisabled() {
  3162. $result = $this->Form->radio(
  3163. 'Model.field',
  3164. array('option A', 'option B'),
  3165. array('disabled' => array('option A'), 'value' => '0')
  3166. );
  3167. $expected = array(
  3168. 'fieldset' => array(),
  3169. 'legend' => array(),
  3170. 'Field',
  3171. '/legend',
  3172. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3173. array('label' => array('for' => 'ModelField0')),
  3174. 'option A',
  3175. '/label',
  3176. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3177. array('label' => array('for' => 'ModelField1')),
  3178. 'option B',
  3179. '/label',
  3180. '/fieldset'
  3181. );
  3182. $this->assertTags($result, $expected);
  3183. $result = $this->Form->radio(
  3184. 'Model.field',
  3185. array('option A', 'option B'),
  3186. array('disabled' => true, 'value' => '0')
  3187. );
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3194. array('label' => array('for' => 'ModelField0')),
  3195. 'option A',
  3196. '/label',
  3197. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3198. array('label' => array('for' => 'ModelField1')),
  3199. 'option B',
  3200. '/label',
  3201. '/fieldset'
  3202. );
  3203. $this->assertTags($result, $expected);
  3204. $result = $this->Form->radio(
  3205. 'Model.field',
  3206. array('option A', 'option B'),
  3207. array('disabled' => 'disabled', 'value' => '0')
  3208. );
  3209. $expected = array(
  3210. 'fieldset' => array(),
  3211. 'legend' => array(),
  3212. 'Field',
  3213. '/legend',
  3214. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3215. array('label' => array('for' => 'ModelField0')),
  3216. 'option A',
  3217. '/label',
  3218. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3219. array('label' => array('for' => 'ModelField1')),
  3220. 'option B',
  3221. '/label',
  3222. '/fieldset'
  3223. );
  3224. $this->assertTags($result, $expected);
  3225. }
  3226. /**
  3227. * test disabling the hidden input for radio buttons
  3228. *
  3229. * @return void
  3230. */
  3231. public function testRadioHiddenInputDisabling() {
  3232. $result = $this->Form->input('Model.1.field', array(
  3233. 'type' => 'radio',
  3234. 'options' => array('option A'),
  3235. 'hiddenField' => false
  3236. )
  3237. );
  3238. $expected = array(
  3239. 'div' => array('class' => 'input radio'),
  3240. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3241. 'label' => array('for' => 'Model1Field0'),
  3242. 'option A',
  3243. '/label',
  3244. '/div'
  3245. );
  3246. $this->assertTags($result, $expected);
  3247. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3248. $expected = array(
  3249. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3250. 'label' => array('for' => 'Model1Field0'),
  3251. 'option A',
  3252. '/label'
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. }
  3256. /**
  3257. * test adding an empty option for radio buttons
  3258. *
  3259. * @return void
  3260. */
  3261. public function testRadioAddEmptyOption() {
  3262. $result = $this->Form->input('Model.1.field', array(
  3263. 'type' => 'radio',
  3264. 'options' => array('option A'),
  3265. 'empty' => true,
  3266. 'hiddenField' => false
  3267. ));
  3268. $expected = array(
  3269. 'div' => array('class' => 'input radio'),
  3270. 'fieldset' => array(),
  3271. 'legend' => array(),
  3272. 'Field',
  3273. '/legend',
  3274. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3275. array('label' => array('for' => 'Model1Field')),
  3276. __('empty'),
  3277. '/label',
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3279. array('label' => array('for' => 'Model1Field0')),
  3280. 'option A',
  3281. '/label',
  3282. '/fieldset',
  3283. '/div'
  3284. );
  3285. $this->assertTags($result, $expected);
  3286. $result = $this->Form->input('Model.1.field', array(
  3287. 'type' => 'radio',
  3288. 'options' => array('option A'),
  3289. 'empty' => 'CustomEmptyLabel',
  3290. 'hiddenField' => false
  3291. ));
  3292. $expected = array(
  3293. 'div' => array('class' => 'input radio'),
  3294. 'fieldset' => array(),
  3295. 'legend' => array(),
  3296. 'Field',
  3297. '/legend',
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3299. array('label' => array('for' => 'Model1Field')),
  3300. 'CustomEmptyLabel',
  3301. '/label',
  3302. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3303. array('label' => array('for' => 'Model1Field0')),
  3304. 'option A',
  3305. '/label',
  3306. '/fieldset',
  3307. '/div'
  3308. );
  3309. $this->assertTags($result, $expected);
  3310. $result = $this->Form->input('Model.1.field', array(
  3311. 'type' => 'radio',
  3312. 'options' => array('option A'),
  3313. 'empty' => false,
  3314. 'hiddenField' => false
  3315. ));
  3316. $this->assertTextNotContains('"Model1Field"', $result);
  3317. }
  3318. /**
  3319. * testSelect method
  3320. *
  3321. * Test select element generation.
  3322. *
  3323. * @return void
  3324. */
  3325. public function testSelect() {
  3326. $result = $this->Form->select('Model.field', array());
  3327. $expected = array(
  3328. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3329. array('option' => array('value' => '')),
  3330. '/option',
  3331. '/select'
  3332. );
  3333. $this->assertTags($result, $expected);
  3334. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3335. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3336. $expected = array(
  3337. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3338. array('option' => array('value' => '')),
  3339. '/option',
  3340. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3341. 'good',
  3342. '/option',
  3343. array('option' => array('value' => 'other')),
  3344. 'bad',
  3345. '/option',
  3346. '/select'
  3347. );
  3348. $this->assertTags($result, $expected);
  3349. $this->Form->request->data = array();
  3350. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3351. $expected = array(
  3352. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3353. array('option' => array('value' => '')),
  3354. '/option',
  3355. array('option' => array('value' => 'value')),
  3356. 'good',
  3357. '/option',
  3358. array('option' => array('value' => 'other')),
  3359. 'bad',
  3360. '/option',
  3361. '/select'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. $result = $this->Form->select(
  3365. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3366. array('empty' => false)
  3367. );
  3368. $expected = array(
  3369. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3370. array('option' => array('value' => 'first')),
  3371. 'first &quot;html&quot; &lt;chars&gt;',
  3372. '/option',
  3373. array('option' => array('value' => 'second')),
  3374. 'value',
  3375. '/option',
  3376. '/select'
  3377. );
  3378. $this->assertTags($result, $expected);
  3379. $result = $this->Form->select(
  3380. 'Model.field',
  3381. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3382. array('escape' => false, 'empty' => false)
  3383. );
  3384. $expected = array(
  3385. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3386. array('option' => array('value' => 'first')),
  3387. 'first "html" <chars>',
  3388. '/option',
  3389. array('option' => array('value' => 'second')),
  3390. 'value',
  3391. '/option',
  3392. '/select'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. $options = array(
  3396. array('value' => 'first', 'name' => 'First'),
  3397. array('value' => 'first', 'name' => 'Another First'),
  3398. );
  3399. $result = $this->Form->select(
  3400. 'Model.field',
  3401. $options,
  3402. array('escape' => false, 'empty' => false)
  3403. );
  3404. $expected = array(
  3405. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3406. array('option' => array('value' => 'first')),
  3407. 'First',
  3408. '/option',
  3409. array('option' => array('value' => 'first')),
  3410. 'Another First',
  3411. '/option',
  3412. '/select'
  3413. );
  3414. $this->assertTags($result, $expected);
  3415. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3416. $result = $this->Form->select(
  3417. 'Model.contact_id',
  3418. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3419. array('escape' => false, 'empty' => 'pick something')
  3420. );
  3421. $expected = array(
  3422. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3423. array('option' => array('value' => '')), 'pick something', '/option',
  3424. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3425. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3426. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $this->Form->request->data['Model']['field'] = 0;
  3431. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3432. $expected = array(
  3433. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3434. array('option' => array('value' => '')), '/option',
  3435. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3436. array('option' => array('value' => '1')), 'Yes', '/option',
  3437. '/select'
  3438. );
  3439. $this->assertTags($result, $expected);
  3440. $this->Form->request->data['Model']['field'] = 50;
  3441. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3442. $expected = array(
  3443. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3444. array('option' => array('value' => '')), '/option',
  3445. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3446. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3447. '/select'
  3448. );
  3449. $this->assertTags($result, $expected);
  3450. }
  3451. /**
  3452. * test that select() with optiongroups listens to the escape param.
  3453. *
  3454. * @return void
  3455. */
  3456. public function testSelectOptionGroupEscaping() {
  3457. $options = array(
  3458. '>< Key' => array(
  3459. 1 => 'One',
  3460. 2 => 'Two'
  3461. )
  3462. );
  3463. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3464. $expected = array(
  3465. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3466. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3467. array('option' => array('value' => '1')), 'One', '/option',
  3468. array('option' => array('value' => '2')), 'Two', '/option',
  3469. '/optgroup',
  3470. '/select'
  3471. );
  3472. $this->assertTags($result, $expected);
  3473. $options = array(
  3474. '>< Key' => array(
  3475. 1 => 'One',
  3476. 2 => 'Two'
  3477. )
  3478. );
  3479. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3480. $expected = array(
  3481. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3482. 'optgroup' => array('label' => '>< Key'),
  3483. array('option' => array('value' => '1')), 'One', '/option',
  3484. array('option' => array('value' => '2')), 'Two', '/option',
  3485. '/optgroup',
  3486. '/select'
  3487. );
  3488. $this->assertTags($result, $expected);
  3489. }
  3490. /**
  3491. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3492. *
  3493. * @return void
  3494. */
  3495. public function testSelectWithNullAttributes() {
  3496. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3497. $expected = array(
  3498. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3499. array('option' => array('value' => '0')),
  3500. 'first',
  3501. '/option',
  3502. array('option' => array('value' => '1')),
  3503. 'second',
  3504. '/option',
  3505. '/select'
  3506. );
  3507. $this->assertTags($result, $expected);
  3508. }
  3509. /**
  3510. * testNestedSelect method
  3511. *
  3512. * test select element generation with optgroups
  3513. *
  3514. * @return void
  3515. */
  3516. public function testNestedSelect() {
  3517. $result = $this->Form->select(
  3518. 'Model.field',
  3519. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3520. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3521. )), array('empty' => false)
  3522. );
  3523. $expected = array(
  3524. 'select' => array('name' => 'data[Model][field]',
  3525. 'id' => 'ModelField'),
  3526. array('option' => array('value' => 1)),
  3527. 'One',
  3528. '/option',
  3529. array('option' => array('value' => 2)),
  3530. 'Two',
  3531. '/option',
  3532. array('optgroup' => array('label' => 'Three')),
  3533. array('option' => array('value' => 4)),
  3534. 'Four',
  3535. '/option',
  3536. array('option' => array('value' => 5)),
  3537. 'Five',
  3538. '/option',
  3539. '/optgroup',
  3540. '/select'
  3541. );
  3542. $this->assertTags($result, $expected);
  3543. $result = $this->Form->select(
  3544. 'Model.field',
  3545. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3546. array('showParents' => true, 'empty' => false)
  3547. );
  3548. $expected = array(
  3549. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3550. array('option' => array('value' => 1)),
  3551. 'One',
  3552. '/option',
  3553. array('option' => array('value' => 2)),
  3554. 'Two',
  3555. '/option',
  3556. array('optgroup' => array('label' => 'Three')),
  3557. array('option' => array('value' => 3)),
  3558. 'Three',
  3559. '/option',
  3560. array('option' => array('value' => 4)),
  3561. 'Four',
  3562. '/option',
  3563. '/optgroup',
  3564. '/select'
  3565. );
  3566. $this->assertTags($result, $expected);
  3567. }
  3568. /**
  3569. * testSelectMultiple method
  3570. *
  3571. * test generation of multiple select elements
  3572. *
  3573. * @return void
  3574. */
  3575. public function testSelectMultiple() {
  3576. $options = array('first', 'second', 'third');
  3577. $result = $this->Form->select(
  3578. 'Model.multi_field', $options, array('multiple' => true)
  3579. );
  3580. $expected = array(
  3581. 'input' => array(
  3582. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3583. ),
  3584. 'select' => array(
  3585. 'name' => 'data[Model][multi_field][]',
  3586. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3587. ),
  3588. array('option' => array('value' => '0')),
  3589. 'first',
  3590. '/option',
  3591. array('option' => array('value' => '1')),
  3592. 'second',
  3593. '/option',
  3594. array('option' => array('value' => '2')),
  3595. 'third',
  3596. '/option',
  3597. '/select'
  3598. );
  3599. $this->assertTags($result, $expected);
  3600. $result = $this->Form->select(
  3601. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3602. );
  3603. $expected = array(
  3604. 'input' => array(
  3605. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3606. ),
  3607. 'select' => array(
  3608. 'name' => 'data[Model][multi_field][]',
  3609. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3610. ),
  3611. array('option' => array('value' => '0')),
  3612. 'first',
  3613. '/option',
  3614. array('option' => array('value' => '1')),
  3615. 'second',
  3616. '/option',
  3617. array('option' => array('value' => '2')),
  3618. 'third',
  3619. '/option',
  3620. '/select'
  3621. );
  3622. $this->assertTags($result, $expected);
  3623. $result = $this->Form->select(
  3624. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3625. );
  3626. $expected = array(
  3627. 'input' => array(
  3628. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3629. ),
  3630. 'select' => array(
  3631. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3632. 'multiple' => 'multiple'
  3633. ),
  3634. array('option' => array('value' => '0', 'selected' => 'selected')),
  3635. 'first',
  3636. '/option',
  3637. array('option' => array('value' => '1', 'selected' => 'selected')),
  3638. 'second',
  3639. '/option',
  3640. array('option' => array('value' => '2')),
  3641. 'third',
  3642. '/option',
  3643. '/select'
  3644. );
  3645. $this->assertTags($result, $expected);
  3646. $result = $this->Form->select(
  3647. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3648. );
  3649. $expected = array(
  3650. 'select' => array(
  3651. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3652. ),
  3653. array('option' => array('value' => '0', 'selected' => 'selected')),
  3654. 'first',
  3655. '/option',
  3656. array('option' => array('value' => '1', 'selected' => 'selected')),
  3657. 'second',
  3658. '/option',
  3659. array('option' => array('value' => '2')),
  3660. 'third',
  3661. '/option',
  3662. '/select'
  3663. );
  3664. $this->assertTags($result, $expected);
  3665. }
  3666. /**
  3667. * test generation of habtm select boxes.
  3668. *
  3669. * @return void
  3670. */
  3671. public function testHabtmSelectBox() {
  3672. $this->View->viewVars['contactTags'] = array(
  3673. 1 => 'blue',
  3674. 2 => 'red',
  3675. 3 => 'green'
  3676. );
  3677. $this->Form->request->data = array(
  3678. 'Contact' => array(),
  3679. 'ContactTag' => array(
  3680. array(
  3681. 'id' => 1,
  3682. 'name' => 'blue'
  3683. ),
  3684. array(
  3685. 'id' => 3,
  3686. 'name' => 'green'
  3687. )
  3688. )
  3689. );
  3690. $this->Form->create('Contact');
  3691. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3692. $expected = array(
  3693. 'input' => array(
  3694. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3695. ),
  3696. 'select' => array(
  3697. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3698. 'multiple' => 'multiple'
  3699. ),
  3700. array('option' => array('value' => '1', 'selected' => 'selected')),
  3701. 'blue',
  3702. '/option',
  3703. array('option' => array('value' => '2')),
  3704. 'red',
  3705. '/option',
  3706. array('option' => array('value' => '3', 'selected' => 'selected')),
  3707. 'green',
  3708. '/option',
  3709. '/select'
  3710. );
  3711. $this->assertTags($result, $expected);
  3712. }
  3713. /**
  3714. * test generation of multi select elements in checkbox format
  3715. *
  3716. * @return void
  3717. */
  3718. public function testSelectMultipleCheckboxes() {
  3719. $result = $this->Form->select(
  3720. 'Model.multi_field',
  3721. array('first', 'second', 'third'),
  3722. array('multiple' => 'checkbox')
  3723. );
  3724. $expected = array(
  3725. 'input' => array(
  3726. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3727. ),
  3728. array('div' => array('class' => 'checkbox')),
  3729. array('input' => array(
  3730. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3731. 'value' => '0', 'id' => 'ModelMultiField0'
  3732. )),
  3733. array('label' => array('for' => 'ModelMultiField0')),
  3734. 'first',
  3735. '/label',
  3736. '/div',
  3737. array('div' => array('class' => 'checkbox')),
  3738. array('input' => array(
  3739. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3740. 'value' => '1', 'id' => 'ModelMultiField1'
  3741. )),
  3742. array('label' => array('for' => 'ModelMultiField1')),
  3743. 'second',
  3744. '/label',
  3745. '/div',
  3746. array('div' => array('class' => 'checkbox')),
  3747. array('input' => array(
  3748. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3749. 'value' => '2', 'id' => 'ModelMultiField2'
  3750. )),
  3751. array('label' => array('for' => 'ModelMultiField2')),
  3752. 'third',
  3753. '/label',
  3754. '/div'
  3755. );
  3756. $this->assertTags($result, $expected);
  3757. $result = $this->Form->select(
  3758. 'Model.multi_field',
  3759. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3760. array('multiple' => 'checkbox')
  3761. );
  3762. $expected = array(
  3763. 'input' => array(
  3764. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3765. ),
  3766. array('div' => array('class' => 'checkbox')),
  3767. array('input' => array(
  3768. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3769. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3770. )),
  3771. array('label' => array('for' => 'ModelMultiFieldA')),
  3772. 'first',
  3773. '/label',
  3774. '/div',
  3775. array('div' => array('class' => 'checkbox')),
  3776. array('input' => array(
  3777. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3778. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3779. )),
  3780. array('label' => array('for' => 'ModelMultiFieldB')),
  3781. 'second',
  3782. '/label',
  3783. '/div',
  3784. array('div' => array('class' => 'checkbox')),
  3785. array('input' => array(
  3786. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3787. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3788. )),
  3789. array('label' => array('for' => 'ModelMultiFieldC')),
  3790. 'third',
  3791. '/label',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->select(
  3796. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3797. );
  3798. $expected = array(
  3799. 'input' => array(
  3800. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3801. ),
  3802. array('div' => array('class' => 'checkbox')),
  3803. array('input' => array(
  3804. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3805. 'value' => '1', 'id' => 'ModelMultiField1'
  3806. )),
  3807. array('label' => array('for' => 'ModelMultiField1')),
  3808. 'first',
  3809. '/label',
  3810. '/div'
  3811. );
  3812. $this->assertTags($result, $expected);
  3813. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3814. $result = $this->Form->select(
  3815. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3816. );
  3817. $expected = array(
  3818. 'input' => array(
  3819. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3820. ),
  3821. array('div' => array('class' => 'checkbox')),
  3822. array('input' => array(
  3823. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3824. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3825. )),
  3826. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3827. 'first',
  3828. '/label',
  3829. '/div',
  3830. array('div' => array('class' => 'checkbox')),
  3831. array('input' => array(
  3832. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3833. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3834. )),
  3835. array('label' => array('for' => 'ModelTagsArray')),
  3836. 'Array',
  3837. '/label',
  3838. '/div'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. }
  3842. /**
  3843. * test multiple checkboxes with div styles.
  3844. *
  3845. * @return void
  3846. */
  3847. public function testSelectMultipleCheckboxDiv() {
  3848. $result = $this->Form->select(
  3849. 'Model.tags',
  3850. array('first', 'second'),
  3851. array('multiple' => 'checkbox', 'class' => 'my-class')
  3852. );
  3853. $expected = array(
  3854. 'input' => array(
  3855. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3856. ),
  3857. array('div' => array('class' => 'my-class')),
  3858. array('input' => array(
  3859. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3860. 'value' => '0', 'id' => 'ModelTags0'
  3861. )),
  3862. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3863. '/div',
  3864. array('div' => array('class' => 'my-class')),
  3865. array('input' => array(
  3866. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3867. 'value' => '1', 'id' => 'ModelTags1'
  3868. )),
  3869. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3870. '/div'
  3871. );
  3872. $this->assertTags($result, $expected);
  3873. $result = $this->Form->input('Model.tags', array(
  3874. 'options' => array('first', 'second'),
  3875. 'multiple' => 'checkbox',
  3876. 'class' => 'my-class',
  3877. 'div' => false,
  3878. 'label' => false
  3879. ));
  3880. $this->assertTags($result, $expected);
  3881. $Contact = ClassRegistry::getObject('Contact');
  3882. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3883. $result = $this->Form->input('Contact.tags', array(
  3884. 'options' => array('one'),
  3885. 'multiple' => 'checkbox',
  3886. 'label' => false,
  3887. 'div' => false
  3888. ));
  3889. $expected = array(
  3890. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3891. array('div' => array('class' => 'checkbox form-error')),
  3892. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3893. array('label' => array('for' => 'ContactTags0')),
  3894. 'one',
  3895. '/label',
  3896. '/div'
  3897. );
  3898. $this->assertTags($result, $expected);
  3899. $result = $this->Form->input('Contact.tags', array(
  3900. 'options' => array('one'),
  3901. 'multiple' => 'checkbox',
  3902. 'class' => 'mycheckbox',
  3903. 'label' => false,
  3904. 'div' => false
  3905. ));
  3906. $expected = array(
  3907. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3908. array('div' => array('class' => 'mycheckbox form-error')),
  3909. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3910. array('label' => array('for' => 'ContactTags0')),
  3911. 'one',
  3912. '/label',
  3913. '/div'
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. }
  3917. /**
  3918. * Checks the security hash array generated for multiple-input checkbox elements
  3919. *
  3920. * @return void
  3921. */
  3922. public function testSelectMultipleCheckboxSecurity() {
  3923. $this->Form->request['_Token'] = array('key' => 'testKey');
  3924. $this->assertEquals(array(), $this->Form->fields);
  3925. $result = $this->Form->select(
  3926. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3927. array('multiple' => 'checkbox')
  3928. );
  3929. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3930. $result = $this->Form->secure($this->Form->fields);
  3931. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3932. $this->assertRegExp('/"' . $key . '"/', $result);
  3933. }
  3934. /**
  3935. * Multiple select elements should always be secured as they always participate
  3936. * in the POST data.
  3937. *
  3938. * @return void
  3939. */
  3940. public function testSelectMultipleSecureWithNoOptions() {
  3941. $this->Form->request['_Token'] = array('key' => 'testkey');
  3942. $this->assertEquals(array(), $this->Form->fields);
  3943. $result = $this->Form->select(
  3944. 'Model.select',
  3945. array(),
  3946. array('multiple' => true)
  3947. );
  3948. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3949. }
  3950. /**
  3951. * When a select box has no options it should not be added to the fields list
  3952. * as it always fail post validation.
  3953. *
  3954. * @return void
  3955. */
  3956. public function testSelectNoSecureWithNoOptions() {
  3957. $this->Form->request['_Token'] = array('key' => 'testkey');
  3958. $this->assertEquals(array(), $this->Form->fields);
  3959. $this->Form->select(
  3960. 'Model.select',
  3961. array()
  3962. );
  3963. $this->assertEquals(array(), $this->Form->fields);
  3964. $this->Form->select(
  3965. 'Model.select',
  3966. array(),
  3967. array('empty' => true)
  3968. );
  3969. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3970. }
  3971. /**
  3972. * testInputMultipleCheckboxes method
  3973. *
  3974. * test input() resulting in multi select elements being generated.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testInputMultipleCheckboxes() {
  3979. $result = $this->Form->input('Model.multi_field', array(
  3980. 'options' => array('first', 'second', 'third'),
  3981. 'multiple' => 'checkbox'
  3982. ));
  3983. $expected = array(
  3984. array('div' => array('class' => 'input select')),
  3985. array('label' => array('for' => 'ModelMultiField')),
  3986. 'Multi Field',
  3987. '/label',
  3988. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3989. array('div' => array('class' => 'checkbox')),
  3990. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3991. array('label' => array('for' => 'ModelMultiField0')),
  3992. 'first',
  3993. '/label',
  3994. '/div',
  3995. array('div' => array('class' => 'checkbox')),
  3996. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3997. array('label' => array('for' => 'ModelMultiField1')),
  3998. 'second',
  3999. '/label',
  4000. '/div',
  4001. array('div' => array('class' => 'checkbox')),
  4002. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4003. array('label' => array('for' => 'ModelMultiField2')),
  4004. 'third',
  4005. '/label',
  4006. '/div',
  4007. '/div'
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. $result = $this->Form->input('Model.multi_field', array(
  4011. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4012. 'multiple' => 'checkbox'
  4013. ));
  4014. $expected = array(
  4015. array('div' => array('class' => 'input select')),
  4016. array('label' => array('for' => 'ModelMultiField')),
  4017. 'Multi Field',
  4018. '/label',
  4019. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4020. array('div' => array('class' => 'checkbox')),
  4021. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4022. array('label' => array('for' => 'ModelMultiFieldA')),
  4023. 'first',
  4024. '/label',
  4025. '/div',
  4026. array('div' => array('class' => 'checkbox')),
  4027. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4028. array('label' => array('for' => 'ModelMultiFieldB')),
  4029. 'second',
  4030. '/label',
  4031. '/div',
  4032. array('div' => array('class' => 'checkbox')),
  4033. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4034. array('label' => array('for' => 'ModelMultiFieldC')),
  4035. 'third',
  4036. '/label',
  4037. '/div',
  4038. '/div'
  4039. );
  4040. $this->assertTags($result, $expected);
  4041. $result = $this->Form->input('Model.multi_field', array(
  4042. 'options' => array('1' => 'first'),
  4043. 'multiple' => 'checkbox',
  4044. 'label' => false,
  4045. 'div' => false
  4046. ));
  4047. $expected = array(
  4048. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4049. array('div' => array('class' => 'checkbox')),
  4050. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4051. array('label' => array('for' => 'ModelMultiField1')),
  4052. 'first',
  4053. '/label',
  4054. '/div'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->input('Model.multi_field', array(
  4058. 'options' => array('2' => 'second'),
  4059. 'multiple' => 'checkbox',
  4060. 'label' => false,
  4061. 'div' => false
  4062. ));
  4063. $expected = array(
  4064. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4065. array('div' => array('class' => 'checkbox')),
  4066. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4067. array('label' => array('for' => 'ModelMultiField2')),
  4068. 'second',
  4069. '/label',
  4070. '/div'
  4071. );
  4072. $this->assertTags($result, $expected);
  4073. }
  4074. /**
  4075. * testSelectHiddenFieldOmission method
  4076. *
  4077. * test that select() with 'hiddenField' => false omits the hidden field
  4078. *
  4079. * @return void
  4080. */
  4081. public function testSelectHiddenFieldOmission() {
  4082. $result = $this->Form->select('Model.multi_field',
  4083. array('first', 'second'),
  4084. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4085. );
  4086. $expected = array(
  4087. array('div' => array('class' => 'checkbox')),
  4088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4089. array('label' => array('for' => 'ModelMultiField0')),
  4090. 'first',
  4091. '/label',
  4092. '/div',
  4093. array('div' => array('class' => 'checkbox')),
  4094. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4095. array('label' => array('for' => 'ModelMultiField1')),
  4096. 'second',
  4097. '/label',
  4098. '/div'
  4099. );
  4100. $this->assertTags($result, $expected);
  4101. $result = $this->Form->input('Model.multi_field', array(
  4102. 'options' => array('first', 'second'),
  4103. 'multiple' => 'checkbox',
  4104. 'hiddenField' => false
  4105. ));
  4106. $expected = array(
  4107. array('div' => array('class' => 'input select')),
  4108. array('label' => array('for' => 'ModelMultiField')),
  4109. 'Multi Field',
  4110. '/label',
  4111. array('div' => array('class' => 'checkbox')),
  4112. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4113. array('label' => array('for' => 'ModelMultiField0')),
  4114. 'first',
  4115. '/label',
  4116. '/div',
  4117. array('div' => array('class' => 'checkbox')),
  4118. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4119. array('label' => array('for' => 'ModelMultiField1')),
  4120. 'second',
  4121. '/label',
  4122. '/div',
  4123. '/div'
  4124. );
  4125. $this->assertTags($result, $expected);
  4126. }
  4127. /**
  4128. * test that select() with multiple = checkbox works with overriding name attribute.
  4129. *
  4130. * @return void
  4131. */
  4132. public function testSelectCheckboxMultipleOverrideName() {
  4133. $result = $this->Form->input('category', array(
  4134. 'type' => 'select',
  4135. 'multiple' => 'checkbox',
  4136. 'name' => 'data[fish]',
  4137. 'options' => array('1', '2'),
  4138. 'div' => false,
  4139. 'label' => false,
  4140. ));
  4141. $expected = array(
  4142. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4143. array('div' => array('class' => 'checkbox')),
  4144. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4145. array('label' => array('for' => 'Category0')), '1', '/label',
  4146. '/div',
  4147. array('div' => array('class' => 'checkbox')),
  4148. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4149. array('label' => array('for' => 'Category1')), '2', '/label',
  4150. '/div'
  4151. );
  4152. $this->assertTags($result, $expected);
  4153. }
  4154. /**
  4155. * Test that 'id' overrides all the checkbox id's as well.
  4156. *
  4157. * @return void
  4158. */
  4159. public function testSelectCheckboxMultipleId() {
  4160. $result = $this->Form->select(
  4161. 'Model.multi_field',
  4162. array('first', 'second', 'third'),
  4163. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4164. );
  4165. $expected = array(
  4166. 'input' => array(
  4167. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4168. ),
  4169. array('div' => array('class' => 'checkbox')),
  4170. array('input' => array(
  4171. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4172. 'value' => '0', 'id' => 'CustomId0'
  4173. )),
  4174. array('label' => array('for' => 'CustomId0')),
  4175. 'first',
  4176. '/label',
  4177. '/div',
  4178. array('div' => array('class' => 'checkbox')),
  4179. array('input' => array(
  4180. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4181. 'value' => '1', 'id' => 'CustomId1'
  4182. )),
  4183. array('label' => array('for' => 'CustomId1')),
  4184. 'second',
  4185. '/label',
  4186. '/div',
  4187. array('div' => array('class' => 'checkbox')),
  4188. array('input' => array(
  4189. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4190. 'value' => '2', 'id' => 'CustomId2'
  4191. )),
  4192. array('label' => array('for' => 'CustomId2')),
  4193. 'third',
  4194. '/label',
  4195. '/div'
  4196. );
  4197. $this->assertTags($result, $expected);
  4198. }
  4199. /**
  4200. * testCheckbox method
  4201. *
  4202. * Test generation of checkboxes
  4203. *
  4204. * @return void
  4205. */
  4206. public function testCheckbox() {
  4207. $result = $this->Form->checkbox('Model.field');
  4208. $expected = array(
  4209. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4210. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4211. );
  4212. $this->assertTags($result, $expected);
  4213. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4214. $expected = array(
  4215. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4216. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4217. );
  4218. $this->assertTags($result, $expected);
  4219. $Contact = ClassRegistry::getObject('Contact');
  4220. $Contact->validationErrors['field'] = 1;
  4221. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4222. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4223. $expected = array(
  4224. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4225. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4226. );
  4227. $this->assertTags($result, $expected);
  4228. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4229. $expected = array(
  4230. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4231. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4232. );
  4233. $this->assertTags($result, $expected);
  4234. $this->Form->request->data['Contact']['field'] = '';
  4235. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4236. $expected = array(
  4237. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4238. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $Contact->validationErrors = array();
  4242. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4243. $expected = array(
  4244. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4245. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4246. );
  4247. $this->assertTags($result, $expected);
  4248. $this->Form->request->data['Contact']['published'] = 1;
  4249. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4250. $expected = array(
  4251. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4252. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4253. );
  4254. $this->assertTags($result, $expected);
  4255. $this->Form->request->data['Contact']['published'] = 0;
  4256. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4257. $expected = array(
  4258. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4259. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4260. );
  4261. $this->assertTags($result, $expected);
  4262. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4263. $expected = array(
  4264. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4265. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $result = $this->Form->checkbox('CustomField.1.value');
  4269. $expected = array(
  4270. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4271. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. }
  4275. /**
  4276. * test checkbox() with a custom name attribute
  4277. *
  4278. * @return void
  4279. */
  4280. public function testCheckboxCustomNameAttribute() {
  4281. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4282. $expected = array(
  4283. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4284. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4285. );
  4286. $this->assertTags($result, $expected);
  4287. }
  4288. /**
  4289. * test the checked option for checkboxes.
  4290. *
  4291. * @return void
  4292. */
  4293. public function testCheckboxCheckedOption() {
  4294. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4295. $expected = array(
  4296. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4297. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4298. );
  4299. $this->assertTags($result, $expected);
  4300. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4301. $expected = array(
  4302. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4303. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4304. );
  4305. $this->assertTags($result, $expected);
  4306. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4307. $expected = array(
  4308. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4309. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4313. $expected = array(
  4314. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4315. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4316. );
  4317. $this->assertTags($result, $expected);
  4318. $this->Form->request->data['Model']['field'] = 1;
  4319. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4320. $expected = array(
  4321. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4322. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. }
  4326. /**
  4327. * Test that disabled attribute works on both the checkbox and hidden input.
  4328. *
  4329. * @return void
  4330. */
  4331. public function testCheckboxDisabling() {
  4332. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4333. $expected = array(
  4334. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4335. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4336. );
  4337. $this->assertTags($result, $expected);
  4338. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4339. $expected = array(
  4340. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4341. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. }
  4345. /**
  4346. * Test that the hidden input for checkboxes can be omitted or set to a
  4347. * specific value.
  4348. *
  4349. * @return void
  4350. */
  4351. public function testCheckboxHiddenField() {
  4352. $result = $this->Form->input('UserForm.something', array(
  4353. 'type' => 'checkbox',
  4354. 'hiddenField' => false
  4355. ));
  4356. $expected = array(
  4357. 'div' => array('class' => 'input checkbox'),
  4358. array('input' => array(
  4359. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4360. 'value' => '1', 'id' => 'UserFormSomething'
  4361. )),
  4362. 'label' => array('for' => 'UserFormSomething'),
  4363. 'Something',
  4364. '/label',
  4365. '/div'
  4366. );
  4367. $this->assertTags($result, $expected);
  4368. $result = $this->Form->input('UserForm.something', array(
  4369. 'type' => 'checkbox',
  4370. 'value' => 'Y',
  4371. 'hiddenField' => 'N',
  4372. ));
  4373. $expected = array(
  4374. 'div' => array('class' => 'input checkbox'),
  4375. array('input' => array(
  4376. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4377. 'value' => 'N', 'id' => 'UserFormSomething_'
  4378. )),
  4379. array('input' => array(
  4380. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4381. 'value' => 'Y', 'id' => 'UserFormSomething'
  4382. )),
  4383. 'label' => array('for' => 'UserFormSomething'),
  4384. 'Something',
  4385. '/label',
  4386. '/div'
  4387. );
  4388. $this->assertTags($result, $expected);
  4389. }
  4390. /**
  4391. * testDateTime method
  4392. *
  4393. * Test generation of date/time select elements
  4394. *
  4395. * @return void
  4396. */
  4397. public function testDateTime() {
  4398. extract($this->dateRegex);
  4399. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4400. $now = strtotime('now');
  4401. $expected = array(
  4402. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4403. $daysRegex,
  4404. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4405. date('j', $now),
  4406. '/option',
  4407. '*/select',
  4408. '-',
  4409. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4410. $monthsRegex,
  4411. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4412. date('F', $now),
  4413. '/option',
  4414. '*/select',
  4415. '-',
  4416. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4417. $yearsRegex,
  4418. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4419. date('Y', $now),
  4420. '/option',
  4421. '*/select',
  4422. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4423. $hoursRegex,
  4424. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4425. date('g', $now),
  4426. '/option',
  4427. '*/select',
  4428. ':',
  4429. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4430. $minutesRegex,
  4431. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4432. date('i', $now),
  4433. '/option',
  4434. '*/select',
  4435. ' ',
  4436. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4437. $meridianRegex,
  4438. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4439. date('a', $now),
  4440. '/option',
  4441. '*/select'
  4442. );
  4443. $this->assertTags($result, $expected);
  4444. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4445. $expected = array(
  4446. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4447. $daysRegex,
  4448. array('option' => array('value' => '')),
  4449. '/option',
  4450. '*/select',
  4451. '-',
  4452. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4453. $monthsRegex,
  4454. array('option' => array('value' => '')),
  4455. '/option',
  4456. '*/select',
  4457. '-',
  4458. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4459. $yearsRegex,
  4460. array('option' => array('value' => '')),
  4461. '/option',
  4462. '*/select',
  4463. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4464. $hoursRegex,
  4465. array('option' => array('value' => '')),
  4466. '/option',
  4467. '*/select',
  4468. ':',
  4469. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4470. $minutesRegex,
  4471. array('option' => array('value' => '')),
  4472. '/option',
  4473. '*/select',
  4474. ' ',
  4475. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4476. $meridianRegex,
  4477. array('option' => array('value' => '')),
  4478. '/option',
  4479. '*/select'
  4480. );
  4481. $this->assertTags($result, $expected);
  4482. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4483. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4484. $this->assertTags($result, $expected);
  4485. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4486. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4487. $this->assertTags($result, $expected);
  4488. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4489. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4490. $expected = array(
  4491. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4492. $daysRegex,
  4493. array('option' => array('value' => '')),
  4494. '/option',
  4495. '*/select',
  4496. '-',
  4497. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4498. $monthsRegex,
  4499. array('option' => array('value' => '')),
  4500. '/option',
  4501. '*/select',
  4502. '-',
  4503. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4504. $yearsRegex,
  4505. array('option' => array('value' => '')),
  4506. '/option',
  4507. '*/select',
  4508. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4509. $hoursRegex,
  4510. array('option' => array('value' => '')),
  4511. '/option',
  4512. '*/select',
  4513. ':',
  4514. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4515. $minutesRegex,
  4516. array('option' => array('value' => '')),
  4517. '/option',
  4518. array('option' => array('value' => '00')),
  4519. '00',
  4520. '/option',
  4521. array('option' => array('value' => '05')),
  4522. '05',
  4523. '/option',
  4524. array('option' => array('value' => '10')),
  4525. '10',
  4526. '/option',
  4527. '*/select',
  4528. ' ',
  4529. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4530. $meridianRegex,
  4531. array('option' => array('value' => '')),
  4532. '/option',
  4533. '*/select'
  4534. );
  4535. $this->assertTags($result, $expected);
  4536. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4537. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4538. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4539. $this->Form->request->data['Contact']['data'] = null;
  4540. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4541. $expected = array(
  4542. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4543. $daysRegex,
  4544. array('option' => array('value' => '')),
  4545. '/option',
  4546. '*/select',
  4547. '-',
  4548. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4549. $monthsRegex,
  4550. array('option' => array('value' => '')),
  4551. '/option',
  4552. '*/select',
  4553. '-',
  4554. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4555. $yearsRegex,
  4556. array('option' => array('value' => '')),
  4557. '/option',
  4558. '*/select',
  4559. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4560. $hoursRegex,
  4561. array('option' => array('value' => '')),
  4562. '/option',
  4563. '*/select',
  4564. ':',
  4565. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4566. $minutesRegex,
  4567. array('option' => array('value' => '')),
  4568. '/option',
  4569. '*/select',
  4570. ' ',
  4571. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4572. $meridianRegex,
  4573. array('option' => array('value' => '')),
  4574. '/option',
  4575. '*/select'
  4576. );
  4577. $this->assertTags($result, $expected);
  4578. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4579. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4580. $now = strtotime($this->Form->data['Model']['field']);
  4581. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4582. $expected = array(
  4583. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4584. $daysRegex,
  4585. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4586. date('j', $now),
  4587. '/option',
  4588. '*/select',
  4589. '-',
  4590. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4591. $monthsRegex,
  4592. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4593. date('F', $now),
  4594. '/option',
  4595. '*/select',
  4596. '-',
  4597. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4598. $yearsRegex,
  4599. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4600. date('Y', $now),
  4601. '/option',
  4602. '*/select',
  4603. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4604. $hoursRegex,
  4605. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4606. date('g', $now),
  4607. '/option',
  4608. '*/select',
  4609. ':',
  4610. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4611. $minutesRegex,
  4612. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4613. date('i', $now),
  4614. '/option',
  4615. '*/select',
  4616. ' ',
  4617. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4618. $meridianRegex,
  4619. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4620. date('a', $now),
  4621. '/option',
  4622. '*/select'
  4623. );
  4624. $this->assertTags($result, $expected);
  4625. $selected = strtotime('2008-10-26 12:33:00');
  4626. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4627. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4628. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4629. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4630. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4631. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4632. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4633. $this->Form->create('Contact');
  4634. $result = $this->Form->input('published');
  4635. $now = strtotime('now');
  4636. $expected = array(
  4637. 'div' => array('class' => 'input date'),
  4638. 'label' => array('for' => 'ContactPublishedMonth'),
  4639. 'Published',
  4640. '/label',
  4641. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4642. $monthsRegex,
  4643. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4644. date('F', $now),
  4645. '/option',
  4646. '*/select',
  4647. '-',
  4648. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4649. $daysRegex,
  4650. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4651. date('j', $now),
  4652. '/option',
  4653. '*/select',
  4654. '-',
  4655. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4656. $yearsRegex,
  4657. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4658. date('Y', $now),
  4659. '/option',
  4660. '*/select',
  4661. '/div'
  4662. );
  4663. $this->assertTags($result, $expected);
  4664. $result = $this->Form->input('published2', array('type' => 'date'));
  4665. $now = strtotime('now');
  4666. $expected = array(
  4667. 'div' => array('class' => 'input date'),
  4668. 'label' => array('for' => 'ContactPublished2Month'),
  4669. 'Published2',
  4670. '/label',
  4671. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4672. $monthsRegex,
  4673. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4674. date('F', $now),
  4675. '/option',
  4676. '*/select',
  4677. '-',
  4678. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4679. $daysRegex,
  4680. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4681. date('j', $now),
  4682. '/option',
  4683. '*/select',
  4684. '-',
  4685. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4686. $yearsRegex,
  4687. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4688. date('Y', $now),
  4689. '/option',
  4690. '*/select',
  4691. '/div'
  4692. );
  4693. $this->assertTags($result, $expected);
  4694. $this->Form->create('Contact');
  4695. $result = $this->Form->input('published', array('monthNames' => false));
  4696. $now = strtotime('now');
  4697. $expected = array(
  4698. 'div' => array('class' => 'input date'),
  4699. 'label' => array('for' => 'ContactPublishedMonth'),
  4700. 'Published',
  4701. '/label',
  4702. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4703. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4704. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4705. date('m', $now),
  4706. '/option',
  4707. '*/select',
  4708. '-',
  4709. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4710. $daysRegex,
  4711. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4712. date('j', $now),
  4713. '/option',
  4714. '*/select',
  4715. '-',
  4716. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4717. $yearsRegex,
  4718. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4719. date('Y', $now),
  4720. '/option',
  4721. '*/select',
  4722. '/div'
  4723. );
  4724. $this->assertTags($result, $expected);
  4725. $result = $this->Form->input('published', array('type' => 'time'));
  4726. $now = strtotime('now');
  4727. $expected = array(
  4728. 'div' => array('class' => 'input time'),
  4729. 'label' => array('for' => 'ContactPublishedHour'),
  4730. 'Published',
  4731. '/label',
  4732. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4733. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4734. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4735. date('g', $now),
  4736. '/option',
  4737. '*/select',
  4738. ':',
  4739. );
  4740. $this->assertTags($result, $expected);
  4741. $result = $this->Form->input('published', array(
  4742. 'timeFormat' => 24,
  4743. 'interval' => 5,
  4744. 'selected' => strtotime('2009-09-03 13:37:00'),
  4745. 'type' => 'datetime'
  4746. ));
  4747. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4748. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4749. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4750. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4751. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4752. $this->assertNoErrors();
  4753. $this->Form->request->data['Contact'] = array(
  4754. 'date' => array(
  4755. 'day' => '',
  4756. 'month' => '',
  4757. 'year' => '',
  4758. 'hour' => '',
  4759. 'min' => '',
  4760. 'meridian' => ''
  4761. )
  4762. );
  4763. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4764. }
  4765. /**
  4766. * test that datetime() and default values work.
  4767. *
  4768. * @return void
  4769. */
  4770. public function testDatetimeWithDefault() {
  4771. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4772. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4773. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4774. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4775. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4776. 'default' => '2009-06-01 11:15:30'
  4777. ));
  4778. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4779. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4780. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4781. }
  4782. /**
  4783. * test that bogus non-date time data doesn't cause errors.
  4784. *
  4785. * @return void
  4786. */
  4787. public function testDateTimeWithBogusData() {
  4788. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4789. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4790. }
  4791. /**
  4792. * testDateTimeEmptyAsArray
  4793. *
  4794. * @return void
  4795. */
  4796. public function testDateTimeEmptyAsArray() {
  4797. $result = $this->Form->dateTime('Contact.date',
  4798. 'DMY',
  4799. '12',
  4800. array(
  4801. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4802. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4803. )
  4804. )
  4805. );
  4806. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4807. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4808. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4809. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4810. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4811. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4812. $result = $this->Form->dateTime('Contact.date',
  4813. 'DMY',
  4814. '12',
  4815. array(
  4816. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4817. )
  4818. );
  4819. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4820. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4821. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4822. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4823. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4824. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4825. }
  4826. /**
  4827. * testFormDateTimeMulti method
  4828. *
  4829. * test multiple datetime element generation
  4830. *
  4831. * @return void
  4832. */
  4833. public function testFormDateTimeMulti() {
  4834. extract($this->dateRegex);
  4835. $result = $this->Form->dateTime('Contact.1.updated');
  4836. $expected = array(
  4837. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4838. $daysRegex,
  4839. array('option' => array('value' => '')),
  4840. '/option',
  4841. '*/select',
  4842. '-',
  4843. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4844. $monthsRegex,
  4845. array('option' => array('value' => '')),
  4846. '/option',
  4847. '*/select',
  4848. '-',
  4849. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4850. $yearsRegex,
  4851. array('option' => array('value' => '')),
  4852. '/option',
  4853. '*/select',
  4854. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4855. $hoursRegex,
  4856. array('option' => array('value' => '')),
  4857. '/option',
  4858. '*/select',
  4859. ':',
  4860. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4861. $minutesRegex,
  4862. array('option' => array('value' => '')),
  4863. '/option',
  4864. '*/select',
  4865. ' ',
  4866. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4867. $meridianRegex,
  4868. array('option' => array('value' => '')),
  4869. '/option',
  4870. '*/select'
  4871. );
  4872. $this->assertTags($result, $expected);
  4873. $result = $this->Form->dateTime('Contact.2.updated');
  4874. $expected = array(
  4875. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4876. $daysRegex,
  4877. array('option' => array('value' => '')),
  4878. '/option',
  4879. '*/select',
  4880. '-',
  4881. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4882. $monthsRegex,
  4883. array('option' => array('value' => '')),
  4884. '/option',
  4885. '*/select',
  4886. '-',
  4887. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4888. $yearsRegex,
  4889. array('option' => array('value' => '')),
  4890. '/option',
  4891. '*/select',
  4892. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4893. $hoursRegex,
  4894. array('option' => array('value' => '')),
  4895. '/option',
  4896. '*/select',
  4897. ':',
  4898. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4899. $minutesRegex,
  4900. array('option' => array('value' => '')),
  4901. '/option',
  4902. '*/select',
  4903. ' ',
  4904. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4905. $meridianRegex,
  4906. array('option' => array('value' => '')),
  4907. '/option',
  4908. '*/select'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. }
  4912. /**
  4913. * When changing the date format, the label should always focus the first select box when
  4914. * clicked.
  4915. *
  4916. * @return void
  4917. */
  4918. public function testDateTimeLabelIdMatchesFirstInput() {
  4919. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4920. $this->assertContains('label for="ModelDateMonth"', $result);
  4921. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4922. $this->assertContains('label for="ModelDateDay"', $result);
  4923. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4924. $this->assertContains('label for="ModelDateYear"', $result);
  4925. }
  4926. /**
  4927. * testMonth method
  4928. *
  4929. * @return void
  4930. */
  4931. public function testMonth() {
  4932. $result = $this->Form->month('Model.field');
  4933. $expected = array(
  4934. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4935. array('option' => array('value' => '')),
  4936. '/option',
  4937. array('option' => array('value' => '01')),
  4938. date('F', strtotime('2008-01-01 00:00:00')),
  4939. '/option',
  4940. array('option' => array('value' => '02')),
  4941. date('F', strtotime('2008-02-01 00:00:00')),
  4942. '/option',
  4943. '*/select',
  4944. );
  4945. $this->assertTags($result, $expected);
  4946. $result = $this->Form->month('Model.field', array('empty' => true));
  4947. $expected = array(
  4948. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4949. array('option' => array('value' => '')),
  4950. '/option',
  4951. array('option' => array('value' => '01')),
  4952. date('F', strtotime('2008-01-01 00:00:00')),
  4953. '/option',
  4954. array('option' => array('value' => '02')),
  4955. date('F', strtotime('2008-02-01 00:00:00')),
  4956. '/option',
  4957. '*/select',
  4958. );
  4959. $this->assertTags($result, $expected);
  4960. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4961. $expected = array(
  4962. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4963. array('option' => array('value' => '')),
  4964. '/option',
  4965. array('option' => array('value' => '01')),
  4966. '01',
  4967. '/option',
  4968. array('option' => array('value' => '02')),
  4969. '02',
  4970. '/option',
  4971. '*/select',
  4972. );
  4973. $this->assertTags($result, $expected);
  4974. $monthNames = array(
  4975. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4976. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4977. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4978. $expected = array(
  4979. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4980. array('option' => array('value' => '')),
  4981. '/option',
  4982. array('option' => array('value' => '01')),
  4983. 'Jan',
  4984. '/option',
  4985. array('option' => array('value' => '02')),
  4986. 'Feb',
  4987. '/option',
  4988. '*/select',
  4989. );
  4990. $this->assertTags($result, $expected);
  4991. }
  4992. /**
  4993. * testDay method
  4994. *
  4995. * @return void
  4996. */
  4997. public function testDay() {
  4998. extract($this->dateRegex);
  4999. $result = $this->Form->day('Model.field', array('value' => false));
  5000. $expected = array(
  5001. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5002. array('option' => array('value' => '')),
  5003. '/option',
  5004. array('option' => array('value' => '01')),
  5005. '1',
  5006. '/option',
  5007. array('option' => array('value' => '02')),
  5008. '2',
  5009. '/option',
  5010. $daysRegex,
  5011. '/select',
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5015. $result = $this->Form->day('Model.field');
  5016. $expected = array(
  5017. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. array('option' => array('value' => '01')),
  5021. '1',
  5022. '/option',
  5023. array('option' => array('value' => '02')),
  5024. '2',
  5025. '/option',
  5026. $daysRegex,
  5027. array('option' => array('value' => '10', 'selected' => 'selected')),
  5028. '10',
  5029. '/option',
  5030. $daysRegex,
  5031. '/select',
  5032. );
  5033. $this->assertTags($result, $expected);
  5034. $this->Form->request->data['Model']['field'] = '';
  5035. $result = $this->Form->day('Model.field', array('value' => '10'));
  5036. $expected = array(
  5037. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5038. array('option' => array('value' => '')),
  5039. '/option',
  5040. array('option' => array('value' => '01')),
  5041. '1',
  5042. '/option',
  5043. array('option' => array('value' => '02')),
  5044. '2',
  5045. '/option',
  5046. $daysRegex,
  5047. array('option' => array('value' => '10', 'selected' => 'selected')),
  5048. '10',
  5049. '/option',
  5050. $daysRegex,
  5051. '/select',
  5052. );
  5053. $this->assertTags($result, $expected);
  5054. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5055. $result = $this->Form->day('Model.field', array('value' => true));
  5056. $expected = array(
  5057. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5058. array('option' => array('value' => '')),
  5059. '/option',
  5060. array('option' => array('value' => '01')),
  5061. '1',
  5062. '/option',
  5063. array('option' => array('value' => '02')),
  5064. '2',
  5065. '/option',
  5066. $daysRegex,
  5067. array('option' => array('value' => '10', 'selected' => 'selected')),
  5068. '10',
  5069. '/option',
  5070. $daysRegex,
  5071. '/select',
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. }
  5075. /**
  5076. * testMinute method
  5077. *
  5078. * @return void
  5079. */
  5080. public function testMinute() {
  5081. extract($this->dateRegex);
  5082. $result = $this->Form->minute('Model.field');
  5083. $expected = array(
  5084. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5085. array('option' => array('value' => '')),
  5086. '/option',
  5087. array('option' => array('value' => '00')),
  5088. '00',
  5089. '/option',
  5090. array('option' => array('value' => '01')),
  5091. '01',
  5092. '/option',
  5093. array('option' => array('value' => '02')),
  5094. '02',
  5095. '/option',
  5096. $minutesRegex,
  5097. '/select',
  5098. );
  5099. $this->assertTags($result, $expected);
  5100. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5101. $result = $this->Form->minute('Model.field');
  5102. $expected = array(
  5103. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5104. array('option' => array('value' => '')),
  5105. '/option',
  5106. array('option' => array('value' => '00')),
  5107. '00',
  5108. '/option',
  5109. array('option' => array('value' => '01')),
  5110. '01',
  5111. '/option',
  5112. array('option' => array('value' => '02')),
  5113. '02',
  5114. '/option',
  5115. $minutesRegex,
  5116. array('option' => array('value' => '12', 'selected' => 'selected')),
  5117. '12',
  5118. '/option',
  5119. $minutesRegex,
  5120. '/select',
  5121. );
  5122. $this->assertTags($result, $expected);
  5123. $this->Form->request->data['Model']['field'] = '';
  5124. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5125. $expected = array(
  5126. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5127. array('option' => array('value' => '')),
  5128. '/option',
  5129. array('option' => array('value' => '00')),
  5130. '00',
  5131. '/option',
  5132. array('option' => array('value' => '05')),
  5133. '05',
  5134. '/option',
  5135. array('option' => array('value' => '10')),
  5136. '10',
  5137. '/option',
  5138. $minutesRegex,
  5139. '/select',
  5140. );
  5141. $this->assertTags($result, $expected);
  5142. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5143. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5144. $expected = array(
  5145. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5146. array('option' => array('value' => '')),
  5147. '/option',
  5148. array('option' => array('value' => '00')),
  5149. '00',
  5150. '/option',
  5151. array('option' => array('value' => '05')),
  5152. '05',
  5153. '/option',
  5154. array('option' => array('value' => '10', 'selected' => 'selected')),
  5155. '10',
  5156. '/option',
  5157. $minutesRegex,
  5158. '/select',
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. }
  5162. /**
  5163. * testHour method
  5164. *
  5165. * @return void
  5166. */
  5167. public function testHour() {
  5168. extract($this->dateRegex);
  5169. $result = $this->Form->hour('Model.field', false);
  5170. $expected = array(
  5171. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5172. array('option' => array('value' => '')),
  5173. '/option',
  5174. array('option' => array('value' => '01')),
  5175. '1',
  5176. '/option',
  5177. array('option' => array('value' => '02')),
  5178. '2',
  5179. '/option',
  5180. $hoursRegex,
  5181. '/select',
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5185. $result = $this->Form->hour('Model.field', false);
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5188. array('option' => array('value' => '')),
  5189. '/option',
  5190. array('option' => array('value' => '01')),
  5191. '1',
  5192. '/option',
  5193. array('option' => array('value' => '02')),
  5194. '2',
  5195. '/option',
  5196. $hoursRegex,
  5197. array('option' => array('value' => '12', 'selected' => 'selected')),
  5198. '12',
  5199. '/option',
  5200. '/select',
  5201. );
  5202. $this->assertTags($result, $expected);
  5203. $this->Form->request->data['Model']['field'] = '';
  5204. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5205. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5206. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5207. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5208. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5209. $result = $this->Form->hour('Model.field', true);
  5210. $expected = array(
  5211. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5212. array('option' => array('value' => '')),
  5213. '/option',
  5214. array('option' => array('value' => '00', 'selected' => 'selected')),
  5215. '0',
  5216. '/option',
  5217. array('option' => array('value' => '01')),
  5218. '1',
  5219. '/option',
  5220. array('option' => array('value' => '02')),
  5221. '2',
  5222. '/option',
  5223. $hoursRegex,
  5224. '/select',
  5225. );
  5226. $this->assertTags($result, $expected);
  5227. unset($this->Form->request->data['Model']['field']);
  5228. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5229. $thisHour = date('H');
  5230. $optValue = date('G');
  5231. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5232. }
  5233. /**
  5234. * testYear method
  5235. *
  5236. * @return void
  5237. */
  5238. public function testYear() {
  5239. $result = $this->Form->year('Model.field', 2006, 2007);
  5240. $expected = array(
  5241. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5242. array('option' => array('value' => '')),
  5243. '/option',
  5244. array('option' => array('value' => '2007')),
  5245. '2007',
  5246. '/option',
  5247. array('option' => array('value' => '2006')),
  5248. '2006',
  5249. '/option',
  5250. '/select',
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5254. $expected = array(
  5255. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. array('option' => array('value' => '2006')),
  5259. '2006',
  5260. '/option',
  5261. array('option' => array('value' => '2007')),
  5262. '2007',
  5263. '/option',
  5264. '/select',
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->request->data['Contact']['published'] = '';
  5268. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5269. $expected = array(
  5270. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5271. array('option' => array('value' => '')),
  5272. '/option',
  5273. array('option' => array('value' => '2007')),
  5274. '2007',
  5275. '/option',
  5276. array('option' => array('value' => '2006')),
  5277. '2006',
  5278. '/option',
  5279. '/select',
  5280. );
  5281. $this->assertTags($result, $expected);
  5282. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5283. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5284. $expected = array(
  5285. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5286. array('option' => array('value' => '2007')),
  5287. '2007',
  5288. '/option',
  5289. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5290. '2006',
  5291. '/option',
  5292. '/select',
  5293. );
  5294. $this->assertTags($result, $expected);
  5295. $this->Form->request->data['Contact']['published'] = '';
  5296. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5297. $expected = array(
  5298. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5299. array('option' => array('value' => '')),
  5300. '/option',
  5301. array('option' => array('value' => '2007')),
  5302. '2007',
  5303. '/option',
  5304. array('option' => array('value' => '2006')),
  5305. '2006',
  5306. '/option',
  5307. '/select',
  5308. );
  5309. $this->assertTags($result, $expected);
  5310. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5311. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5312. $expected = array(
  5313. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5314. array('option' => array('value' => '2007')),
  5315. '2007',
  5316. '/option',
  5317. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5318. '2006',
  5319. '/option',
  5320. '/select',
  5321. );
  5322. $this->assertTags($result, $expected);
  5323. $this->Form->request->data['Contact']['published'] = '';
  5324. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5325. $expected = array(
  5326. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5327. array('option' => array('value' => '')),
  5328. '/option',
  5329. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5330. '2007',
  5331. '/option',
  5332. array('option' => array('value' => '2006')),
  5333. '2006',
  5334. '/option',
  5335. '/select',
  5336. );
  5337. $this->assertTags($result, $expected);
  5338. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5339. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5340. $expected = array(
  5341. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5342. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5343. '2007',
  5344. '/option',
  5345. array('option' => array('value' => '2006')),
  5346. '2006',
  5347. '/option',
  5348. '/select',
  5349. );
  5350. $this->assertTags($result, $expected);
  5351. $this->Form->request->data['Contact']['published'] = '';
  5352. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5353. $expected = array(
  5354. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5355. array('option' => array('value' => '2008')),
  5356. '2008',
  5357. '/option',
  5358. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5359. '2007',
  5360. '/option',
  5361. array('option' => array('value' => '2006')),
  5362. '2006',
  5363. '/option',
  5364. '/select',
  5365. );
  5366. $this->assertTags($result, $expected);
  5367. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5368. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5369. $expected = array(
  5370. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5371. array('option' => array('value' => '2008')),
  5372. '2008',
  5373. '/option',
  5374. array('option' => array('value' => '2007')),
  5375. '2007',
  5376. '/option',
  5377. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5378. '2006',
  5379. '/option',
  5380. '/select',
  5381. );
  5382. $this->assertTags($result, $expected);
  5383. $this->Form->request->data = array();
  5384. $this->Form->create('Contact');
  5385. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5386. $expected = array(
  5387. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5388. array('option' => array('value' => '2008')),
  5389. '2008',
  5390. '/option',
  5391. array('option' => array('value' => '2007')),
  5392. '2007',
  5393. '/option',
  5394. array('option' => array('value' => '2006')),
  5395. '2006',
  5396. '/option',
  5397. '/select',
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5401. $this->assertContains('data[Contact][published][year]', $result);
  5402. }
  5403. /**
  5404. * testTextArea method
  5405. *
  5406. * @return void
  5407. */
  5408. public function testTextArea() {
  5409. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5410. $result = $this->Form->textarea('Model.field');
  5411. $expected = array(
  5412. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5413. 'some test data',
  5414. '/textarea',
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $result = $this->Form->textarea('Model.tmp');
  5418. $expected = array(
  5419. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5420. '/textarea',
  5421. );
  5422. $this->assertTags($result, $expected);
  5423. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5424. $result = $this->Form->textarea('Model.field');
  5425. $expected = array(
  5426. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5427. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5428. '/textarea',
  5429. );
  5430. $this->assertTags($result, $expected);
  5431. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5432. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5433. $expected = array(
  5434. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5435. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5436. '/textarea',
  5437. );
  5438. $this->assertTags($result, $expected);
  5439. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5440. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5441. $expected = array(
  5442. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5443. '/textarea'
  5444. );
  5445. $this->assertTags($result, $expected);
  5446. }
  5447. /**
  5448. * testTextAreaWithStupidCharacters method
  5449. *
  5450. * test text area with non-ascii characters
  5451. *
  5452. * @return void
  5453. */
  5454. public function testTextAreaWithStupidCharacters() {
  5455. $this->loadFixtures('Post');
  5456. $result = $this->Form->input('Post.content', array(
  5457. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5458. ));
  5459. $expected = array(
  5460. 'div' => array('class' => 'input text'),
  5461. 'label' => array('for' => 'PostContent'),
  5462. 'Current Text',
  5463. '/label',
  5464. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5465. 'GREAT®',
  5466. '/textarea',
  5467. '/div'
  5468. );
  5469. $this->assertTags($result, $expected);
  5470. }
  5471. /**
  5472. * testHiddenField method
  5473. *
  5474. * @return void
  5475. */
  5476. public function testHiddenField() {
  5477. $Contact = ClassRegistry::getObject('Contact');
  5478. $Contact->validationErrors['field'] = 1;
  5479. $this->Form->request->data['Contact']['field'] = 'test';
  5480. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5481. $this->assertTags($result, array(
  5482. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5483. );
  5484. }
  5485. /**
  5486. * testFileUploadField method
  5487. *
  5488. * @return void
  5489. */
  5490. public function testFileUploadField() {
  5491. $result = $this->Form->file('Model.upload');
  5492. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5493. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5494. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5495. $expected = array(
  5496. 'div' => array('class' => 'input file'),
  5497. 'label' => array('for' => 'ModelUpload'),
  5498. 'Upload',
  5499. '/label',
  5500. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5501. '/div'
  5502. );
  5503. $this->assertTags($result, $expected);
  5504. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5505. $result = $this->Form->file('Model.upload');
  5506. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5507. }
  5508. /**
  5509. * test File upload input on a model not used in create();
  5510. *
  5511. * @return void
  5512. */
  5513. public function testFileUploadOnOtherModel() {
  5514. $this->Form->create('ValidateUser', array('type' => 'file'));
  5515. $result = $this->Form->file('ValidateProfile.city');
  5516. $expected = array(
  5517. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5518. );
  5519. $this->assertTags($result, $expected);
  5520. }
  5521. /**
  5522. * testButton method
  5523. *
  5524. * @return void
  5525. */
  5526. public function testButton() {
  5527. $result = $this->Form->button('Hi');
  5528. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5529. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5530. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5531. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5532. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5533. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5534. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5535. $result = $this->Form->button('No type', array('type' => false));
  5536. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5537. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5538. $this->assertNotRegExp('/\&039/', $result);
  5539. }
  5540. /**
  5541. * Test that button() makes unlocked fields by default.
  5542. *
  5543. * @return void
  5544. */
  5545. public function testButtonUnlockedByDefault() {
  5546. $this->Form->request->params['_Token']['key'] = 'secured';
  5547. $this->Form->button('Save', array('name' => 'save'));
  5548. $this->Form->button('Clear');
  5549. $result = $this->Form->unlockField();
  5550. $this->assertEquals(array('save'), $result);
  5551. }
  5552. /**
  5553. * testPostButton method
  5554. *
  5555. * @return void
  5556. */
  5557. public function testPostButton() {
  5558. $result = $this->Form->postButton('Hi', '/controller/action');
  5559. $this->assertTags($result, array(
  5560. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5561. 'div' => array('style' => 'display:none;'),
  5562. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5563. '/div',
  5564. 'button' => array('type' => 'submit'),
  5565. 'Hi',
  5566. '/button',
  5567. '/form'
  5568. ));
  5569. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5570. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5571. }
  5572. /**
  5573. * Test that postButton adds _Token fields.
  5574. *
  5575. * @return void
  5576. */
  5577. public function testSecurePostButton() {
  5578. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5579. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5580. $expected = array(
  5581. 'form' => array(
  5582. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5583. ),
  5584. array('div' => array('style' => 'display:none;')),
  5585. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5586. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5587. '/div',
  5588. 'button' => array('type' => 'submit'),
  5589. 'Delete',
  5590. '/button',
  5591. array('div' => array('style' => 'display:none;')),
  5592. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5593. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5594. '/div',
  5595. '/form',
  5596. );
  5597. $this->assertTags($result, $expected);
  5598. }
  5599. /**
  5600. * testPostLink method
  5601. *
  5602. * @return void
  5603. */
  5604. public function testPostLink() {
  5605. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5606. $this->assertTags($result, array(
  5607. 'form' => array(
  5608. 'method' => 'post', 'action' => '/posts/delete/1',
  5609. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5610. ),
  5611. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5612. '/form',
  5613. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5614. 'Delete',
  5615. '/a'
  5616. ));
  5617. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5618. $this->assertTags($result, array(
  5619. 'form' => array(
  5620. 'method' => 'post', 'action' => '/posts/delete/1',
  5621. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5622. ),
  5623. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5624. '/form',
  5625. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5626. 'Delete',
  5627. '/a'
  5628. ));
  5629. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5630. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5631. }
  5632. /**
  5633. * Test that postLink adds _Token fields.
  5634. *
  5635. * @return void
  5636. */
  5637. public function testSecurePostLink() {
  5638. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5639. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5640. $expected = array(
  5641. 'form' => array(
  5642. 'method' => 'post', 'action' => '/posts/delete/1',
  5643. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5644. ),
  5645. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5646. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5647. 'div' => array('style' => 'display:none;'),
  5648. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5649. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5650. '/div',
  5651. '/form',
  5652. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5653. 'Delete',
  5654. '/a'
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. }
  5658. /**
  5659. * testSubmitButton method
  5660. *
  5661. * @return void
  5662. */
  5663. public function testSubmitButton() {
  5664. $result = $this->Form->submit('');
  5665. $expected = array(
  5666. 'div' => array('class' => 'submit'),
  5667. 'input' => array('type' => 'submit', 'value' => ''),
  5668. '/div'
  5669. );
  5670. $this->assertTags($result, $expected);
  5671. $result = $this->Form->submit('Test Submit');
  5672. $expected = array(
  5673. 'div' => array('class' => 'submit'),
  5674. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5675. '/div'
  5676. );
  5677. $this->assertTags($result, $expected);
  5678. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5679. $expected = array(
  5680. 'span' => array('class' => 'submit'),
  5681. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5682. '/span'
  5683. );
  5684. $this->assertTags($result, $expected);
  5685. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5686. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5687. $this->assertTags($result, $expected);
  5688. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5689. $expected = array(
  5690. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5691. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5692. '/div'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $result = $this->Form->submit('Next >');
  5696. $expected = array(
  5697. 'div' => array('class' => 'submit'),
  5698. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5699. '/div'
  5700. );
  5701. $this->assertTags($result, $expected);
  5702. $result = $this->Form->submit('Next >', array('escape' => false));
  5703. $expected = array(
  5704. 'div' => array('class' => 'submit'),
  5705. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5706. '/div'
  5707. );
  5708. $this->assertTags($result, $expected);
  5709. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5710. $expected = array(
  5711. 'div' => array('class' => 'submit'),
  5712. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5713. '/div'
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. $before = '--before--';
  5717. $after = '--after--';
  5718. $result = $this->Form->submit('Test', array('before' => $before));
  5719. $expected = array(
  5720. 'div' => array('class' => 'submit'),
  5721. '--before--',
  5722. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5723. '/div'
  5724. );
  5725. $this->assertTags($result, $expected);
  5726. $result = $this->Form->submit('Test', array('after' => $after));
  5727. $expected = array(
  5728. 'div' => array('class' => 'submit'),
  5729. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5730. '--after--',
  5731. '/div'
  5732. );
  5733. $this->assertTags($result, $expected);
  5734. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5735. $expected = array(
  5736. 'div' => array('class' => 'submit'),
  5737. '--before--',
  5738. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5739. '--after--',
  5740. '/div'
  5741. );
  5742. $this->assertTags($result, $expected);
  5743. }
  5744. /**
  5745. * test image submit types.
  5746. *
  5747. * @return void
  5748. */
  5749. public function testSubmitImage() {
  5750. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5751. $expected = array(
  5752. 'div' => array('class' => 'submit'),
  5753. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5754. '/div'
  5755. );
  5756. $this->assertTags($result, $expected);
  5757. $result = $this->Form->submit('/relative/cake.power.gif');
  5758. $expected = array(
  5759. 'div' => array('class' => 'submit'),
  5760. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5761. '/div'
  5762. );
  5763. $this->assertTags($result, $expected);
  5764. $result = $this->Form->submit('cake.power.gif');
  5765. $expected = array(
  5766. 'div' => array('class' => 'submit'),
  5767. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5768. '/div'
  5769. );
  5770. $this->assertTags($result, $expected);
  5771. $result = $this->Form->submit('Not.an.image');
  5772. $expected = array(
  5773. 'div' => array('class' => 'submit'),
  5774. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5775. '/div'
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $after = '--after--';
  5779. $before = '--before--';
  5780. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5781. $expected = array(
  5782. 'div' => array('class' => 'submit'),
  5783. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5784. '--after--',
  5785. '/div'
  5786. );
  5787. $this->assertTags($result, $expected);
  5788. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5789. $expected = array(
  5790. 'div' => array('class' => 'submit'),
  5791. '--before--',
  5792. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5793. '/div'
  5794. );
  5795. $this->assertTags($result, $expected);
  5796. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5797. $expected = array(
  5798. 'div' => array('class' => 'submit'),
  5799. '--before--',
  5800. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5801. '--after--',
  5802. '/div'
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5806. $expected = array(
  5807. 'div' => array('class' => 'submit'),
  5808. '--before--',
  5809. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5810. '--after--',
  5811. '/div'
  5812. );
  5813. $this->assertTags($result, $expected);
  5814. }
  5815. /**
  5816. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5817. * be submitted at a time.
  5818. *
  5819. * @return void
  5820. */
  5821. public function testSubmitUnlockedByDefault() {
  5822. $this->Form->request->params['_Token']['key'] = 'secured';
  5823. $this->Form->submit('Go go');
  5824. $this->Form->submit('Save', array('name' => 'save'));
  5825. $result = $this->Form->unlockField();
  5826. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5827. }
  5828. /**
  5829. * Test submit image with timestamps.
  5830. *
  5831. * @return void
  5832. */
  5833. public function testSubmitImageTimestamp() {
  5834. Configure::write('Asset.timestamp', 'force');
  5835. $result = $this->Form->submit('cake.power.gif');
  5836. $expected = array(
  5837. 'div' => array('class' => 'submit'),
  5838. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5839. '/div'
  5840. );
  5841. $this->assertTags($result, $expected);
  5842. }
  5843. /**
  5844. * test the create() method
  5845. *
  5846. * @return void
  5847. */
  5848. public function testCreate() {
  5849. $result = $this->Form->create('Contact');
  5850. $encoding = strtolower(Configure::read('App.encoding'));
  5851. $expected = array(
  5852. 'form' => array(
  5853. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5854. 'accept-charset' => $encoding
  5855. ),
  5856. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5857. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5858. '/div'
  5859. );
  5860. $this->assertTags($result, $expected);
  5861. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5862. $expected = array('form' => array(
  5863. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5864. 'accept-charset' => $encoding
  5865. ));
  5866. $this->assertTags($result, $expected);
  5867. $result = $this->Form->create('Contact', array('type' => 'get'));
  5868. $expected = array('form' => array(
  5869. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5870. 'accept-charset' => $encoding
  5871. ));
  5872. $this->assertTags($result, $expected);
  5873. $result = $this->Form->create('Contact', array('type' => 'put'));
  5874. $expected = array(
  5875. 'form' => array(
  5876. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5877. 'accept-charset' => $encoding
  5878. ),
  5879. 'div' => array('style' => 'display:none;'),
  5880. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5881. '/div'
  5882. );
  5883. $this->assertTags($result, $expected);
  5884. $result = $this->Form->create('Contact', array('type' => 'file'));
  5885. $expected = array(
  5886. 'form' => array(
  5887. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5888. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5889. ),
  5890. 'div' => array('style' => 'display:none;'),
  5891. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5892. '/div'
  5893. );
  5894. $this->assertTags($result, $expected);
  5895. $this->Form->request->data['Contact']['id'] = 1;
  5896. $this->Form->request->here = '/contacts/edit/1';
  5897. $this->Form->request['action'] = 'edit';
  5898. $result = $this->Form->create('Contact');
  5899. $expected = array(
  5900. 'form' => array(
  5901. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5902. 'accept-charset' => $encoding
  5903. ),
  5904. 'div' => array('style' => 'display:none;'),
  5905. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5906. '/div'
  5907. );
  5908. $this->assertTags($result, $expected);
  5909. $this->Form->request->data['Contact']['id'] = 1;
  5910. $this->Form->request->here = '/contacts/edit/1';
  5911. $this->Form->request['action'] = 'edit';
  5912. $result = $this->Form->create('Contact', array('type' => 'file'));
  5913. $expected = array(
  5914. 'form' => array(
  5915. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5916. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5917. ),
  5918. 'div' => array('style' => 'display:none;'),
  5919. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5920. '/div'
  5921. );
  5922. $this->assertTags($result, $expected);
  5923. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5924. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5925. $expected = array(
  5926. 'form' => array(
  5927. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5928. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5929. ),
  5930. 'div' => array('style' => 'display:none;'),
  5931. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5932. '/div'
  5933. );
  5934. $this->assertTags($result, $expected);
  5935. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5936. $expected = array(
  5937. 'form' => array(
  5938. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5939. 'accept-charset' => $encoding
  5940. ),
  5941. 'div' => array('style' => 'display:none;'),
  5942. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5943. '/div'
  5944. );
  5945. $this->assertTags($result, $expected);
  5946. $this->Form->request['action'] = 'add';
  5947. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5948. $expected = array(
  5949. 'form' => array(
  5950. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5951. 'accept-charset' => $encoding
  5952. ),
  5953. 'div' => array('style' => 'display:none;'),
  5954. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5955. '/div'
  5956. );
  5957. $this->assertTags($result, $expected);
  5958. $result = $this->Form->create('User', array('action' => 'login'));
  5959. $expected = array(
  5960. 'form' => array(
  5961. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5962. 'accept-charset' => $encoding
  5963. ),
  5964. 'div' => array('style' => 'display:none;'),
  5965. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5966. '/div'
  5967. );
  5968. $this->assertTags($result, $expected);
  5969. $result = $this->Form->create('User', array('url' => '/users/login'));
  5970. $expected = array(
  5971. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5972. 'div' => array('style' => 'display:none;'),
  5973. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $this->Form->request['controller'] = 'pages';
  5978. $result = $this->Form->create('User', array('action' => 'signup'));
  5979. $expected = array(
  5980. 'form' => array(
  5981. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5982. 'accept-charset' => $encoding
  5983. ),
  5984. 'div' => array('style' => 'display:none;'),
  5985. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5986. '/div'
  5987. );
  5988. $this->assertTags($result, $expected);
  5989. $this->Form->request->data = array();
  5990. $this->Form->request['controller'] = 'contacts';
  5991. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5992. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5993. $expected = array(
  5994. 'form' => array(
  5995. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5996. 'accept-charset' => 'utf-8'
  5997. ),
  5998. 'div' => array('style' => 'display:none;'),
  5999. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6000. '/div'
  6001. );
  6002. $this->assertTags($result, $expected);
  6003. }
  6004. /**
  6005. * Test the onsubmit option for create()
  6006. *
  6007. * @return void
  6008. */
  6009. public function testCreateOnSubmit() {
  6010. $this->Form->request->data = array();
  6011. $this->Form->request['controller'] = 'contacts';
  6012. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6013. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6014. $expected = array(
  6015. 'form' => array(
  6016. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6017. 'accept-charset' => 'utf-8'
  6018. ),
  6019. 'div' => array('style' => 'display:none;'),
  6020. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6021. '/div'
  6022. );
  6023. $this->assertTags($result, $expected);
  6024. $this->Form->request->data = array();
  6025. $this->Form->request['controller'] = 'contacts';
  6026. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6027. $result = $this->Form->create(array(
  6028. 'url' => array('action' => 'index', 'param'),
  6029. 'default' => false,
  6030. 'onsubmit' => 'someFunction();'
  6031. ));
  6032. $expected = array(
  6033. 'form' => array(
  6034. 'id' => 'ContactAddForm', 'method' => 'post',
  6035. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6036. 'action' => '/contacts/index/param',
  6037. 'accept-charset' => 'utf-8'
  6038. ),
  6039. 'div' => array('style' => 'display:none;'),
  6040. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6041. '/div'
  6042. );
  6043. $this->assertTags($result, $expected);
  6044. }
  6045. /**
  6046. * test create() with automatic url generation
  6047. *
  6048. * @return void
  6049. */
  6050. public function testCreateAutoUrl() {
  6051. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6052. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6053. $this->Form->request->base = '/base_url';
  6054. $result = $this->Form->create('Contact');
  6055. $expected = array(
  6056. 'form' => array(
  6057. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6058. 'accept-charset' => 'utf-8'
  6059. ),
  6060. 'div' => array('style' => 'display:none;'),
  6061. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6062. '/div'
  6063. );
  6064. $this->assertTags($result, $expected);
  6065. $this->Form->request['action'] = 'delete';
  6066. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6067. $this->Form->request->base = '/base_url';
  6068. $result = $this->Form->create('Contact');
  6069. $expected = array(
  6070. 'form' => array(
  6071. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6072. 'accept-charset' => 'utf-8'
  6073. ),
  6074. 'div' => array('style' => 'display:none;'),
  6075. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6076. '/div'
  6077. );
  6078. $this->assertTags($result, $expected);
  6079. }
  6080. /**
  6081. * test create() with a custom route
  6082. *
  6083. * @return void
  6084. */
  6085. public function testCreateCustomRoute() {
  6086. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6087. $encoding = strtolower(Configure::read('App.encoding'));
  6088. $result = $this->Form->create('User', array('action' => 'login'));
  6089. $expected = array(
  6090. 'form' => array(
  6091. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6092. 'accept-charset' => $encoding
  6093. ),
  6094. 'div' => array('style' => 'display:none;'),
  6095. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6096. '/div'
  6097. );
  6098. $this->assertTags($result, $expected);
  6099. }
  6100. /**
  6101. * test that inputDefaults are stored and used.
  6102. *
  6103. * @return void
  6104. */
  6105. public function testCreateWithInputDefaults() {
  6106. $this->Form->create('User', array(
  6107. 'inputDefaults' => array(
  6108. 'div' => false,
  6109. 'label' => false,
  6110. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6111. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6112. )
  6113. ));
  6114. $result = $this->Form->input('username');
  6115. $expected = array(
  6116. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6117. );
  6118. $this->assertTags($result, $expected);
  6119. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6120. $expected = array(
  6121. 'div' => array('class' => 'input text'),
  6122. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6123. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6124. '/div'
  6125. );
  6126. $this->assertTags($result, $expected);
  6127. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6128. $expected = array(
  6129. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6130. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6131. );
  6132. $this->assertTags($result, $expected);
  6133. $this->Form->create('User', array(
  6134. 'inputDefaults' => array(
  6135. 'div' => false,
  6136. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6137. )
  6138. ));
  6139. $result = $this->Form->input('username', array('div' => true));
  6140. $expected = array(
  6141. 'div' => array('class' => 'input text'),
  6142. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6143. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6144. '/div'
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. }
  6148. /**
  6149. * test automatic accept-charset overriding
  6150. *
  6151. * @return void
  6152. */
  6153. public function testCreateWithAcceptCharset() {
  6154. $result = $this->Form->create('UserForm', array(
  6155. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6156. )
  6157. );
  6158. $expected = array(
  6159. 'form' => array(
  6160. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6161. 'accept-charset' => 'iso-8859-1'
  6162. ),
  6163. 'div' => array('style' => 'display:none;'),
  6164. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6165. '/div'
  6166. );
  6167. $this->assertTags($result, $expected);
  6168. }
  6169. /**
  6170. * Test base form url when url param is passed with multiple parameters (&)
  6171. *
  6172. */
  6173. public function testCreateQuerystringrequest() {
  6174. $encoding = strtolower(Configure::read('App.encoding'));
  6175. $result = $this->Form->create('Contact', array(
  6176. 'type' => 'post',
  6177. 'escape' => false,
  6178. 'url' => array(
  6179. 'controller' => 'controller',
  6180. 'action' => 'action',
  6181. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6182. )
  6183. ));
  6184. $expected = array(
  6185. 'form' => array(
  6186. 'id' => 'ContactAddForm',
  6187. 'method' => 'post',
  6188. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6189. 'accept-charset' => $encoding
  6190. ),
  6191. 'div' => array('style' => 'display:none;'),
  6192. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6193. '/div'
  6194. );
  6195. $this->assertTags($result, $expected);
  6196. $result = $this->Form->create('Contact', array(
  6197. 'type' => 'post',
  6198. 'url' => array(
  6199. 'controller' => 'controller',
  6200. 'action' => 'action',
  6201. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6202. )
  6203. ));
  6204. $expected = array(
  6205. 'form' => array(
  6206. 'id' => 'ContactAddForm',
  6207. 'method' => 'post',
  6208. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6209. 'accept-charset' => $encoding
  6210. ),
  6211. 'div' => array('style' => 'display:none;'),
  6212. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6213. '/div'
  6214. );
  6215. $this->assertTags($result, $expected);
  6216. }
  6217. /**
  6218. * test that create() doesn't cause errors by multiple id's being in the primary key
  6219. * as could happen with multiple select or checkboxes.
  6220. *
  6221. * @return void
  6222. */
  6223. public function testCreateWithMultipleIdInData() {
  6224. $encoding = strtolower(Configure::read('App.encoding'));
  6225. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6226. $result = $this->Form->create('Contact');
  6227. $expected = array(
  6228. 'form' => array(
  6229. 'id' => 'ContactAddForm',
  6230. 'method' => 'post',
  6231. 'action' => '/contacts/add',
  6232. 'accept-charset' => $encoding
  6233. ),
  6234. 'div' => array('style' => 'display:none;'),
  6235. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6236. '/div'
  6237. );
  6238. $this->assertTags($result, $expected);
  6239. }
  6240. /**
  6241. * test that create() doesn't add in extra passed params.
  6242. *
  6243. * @return void
  6244. */
  6245. public function testCreatePassedArgs() {
  6246. $encoding = strtolower(Configure::read('App.encoding'));
  6247. $this->Form->request->data['Contact']['id'] = 1;
  6248. $result = $this->Form->create('Contact', array(
  6249. 'type' => 'post',
  6250. 'escape' => false,
  6251. 'url' => array(
  6252. 'action' => 'edit',
  6253. 'myparam'
  6254. )
  6255. ));
  6256. $expected = array(
  6257. 'form' => array(
  6258. 'id' => 'ContactAddForm',
  6259. 'method' => 'post',
  6260. 'action' => '/contacts/edit/myparam',
  6261. 'accept-charset' => $encoding
  6262. ),
  6263. 'div' => array('style' => 'display:none;'),
  6264. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6265. '/div'
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. }
  6269. /**
  6270. * test creating a get form, and get form inputs.
  6271. *
  6272. * @return void
  6273. */
  6274. public function testGetFormCreate() {
  6275. $encoding = strtolower(Configure::read('App.encoding'));
  6276. $result = $this->Form->create('Contact', array('type' => 'get'));
  6277. $this->assertTags($result, array('form' => array(
  6278. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6279. 'accept-charset' => $encoding
  6280. )));
  6281. $result = $this->Form->text('Contact.name');
  6282. $this->assertTags($result, array('input' => array(
  6283. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6284. )));
  6285. $result = $this->Form->password('password');
  6286. $this->assertTags($result, array('input' => array(
  6287. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6288. )));
  6289. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6290. $result = $this->Form->text('user_form');
  6291. $this->assertTags($result, array('input' => array(
  6292. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6293. )));
  6294. }
  6295. /**
  6296. * test get form, and inputs when the model param is false
  6297. *
  6298. * @return void
  6299. */
  6300. public function testGetFormWithFalseModel() {
  6301. $encoding = strtolower(Configure::read('App.encoding'));
  6302. $this->Form->request['controller'] = 'contact_test';
  6303. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6304. $expected = array('form' => array(
  6305. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6306. 'accept-charset' => $encoding
  6307. ));
  6308. $this->assertTags($result, $expected);
  6309. $result = $this->Form->text('reason');
  6310. $expected = array(
  6311. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6312. );
  6313. $this->assertTags($result, $expected);
  6314. }
  6315. /**
  6316. * test that datetime() works with GET style forms.
  6317. *
  6318. * @return void
  6319. */
  6320. public function testDateTimeWithGetForms() {
  6321. extract($this->dateRegex);
  6322. $this->Form->create('Contact', array('type' => 'get'));
  6323. $result = $this->Form->datetime('created');
  6324. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6325. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6326. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6327. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6328. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6329. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6330. }
  6331. /**
  6332. * testEditFormWithData method
  6333. *
  6334. * test auto populating form elements from submitted data.
  6335. *
  6336. * @return void
  6337. */
  6338. public function testEditFormWithData() {
  6339. $this->Form->request->data = array('Person' => array(
  6340. 'id' => 1,
  6341. 'first_name' => 'Nate',
  6342. 'last_name' => 'Abele',
  6343. 'email' => 'nate@example.com'
  6344. ));
  6345. $this->Form->request->addParams(array(
  6346. 'models' => array('Person'),
  6347. 'controller' => 'people',
  6348. 'action' => 'add'
  6349. ));
  6350. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6351. $this->Form->create();
  6352. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6353. $expected = array(
  6354. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6355. 'select' => array(
  6356. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6357. ),
  6358. array('option' => array('value' => 1)), 'Nate', '/option',
  6359. array('option' => array('value' => 2)), 'Garrett', '/option',
  6360. array('option' => array('value' => 3)), 'Larry', '/option',
  6361. '/select'
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. }
  6365. /**
  6366. * Test that required fields are created for various types of validation.
  6367. *
  6368. * @return void
  6369. */
  6370. public function testFormInputRequiredDetection() {
  6371. $this->Form->create('Contact');
  6372. $result = $this->Form->input('Contact.non_existing');
  6373. $expected = array(
  6374. 'div' => array('class' => 'input text'),
  6375. 'label' => array('for' => 'ContactNonExisting'),
  6376. 'Non Existing',
  6377. '/label',
  6378. 'input' => array(
  6379. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6380. 'id' => 'ContactNonExisting'
  6381. ),
  6382. '/div'
  6383. );
  6384. $this->assertTags($result, $expected);
  6385. $result = $this->Form->input('Contact.imrequired');
  6386. $expected = array(
  6387. 'div' => array('class' => 'input text required'),
  6388. 'label' => array('for' => 'ContactImrequired'),
  6389. 'Imrequired',
  6390. '/label',
  6391. 'input' => array(
  6392. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6393. 'id' => 'ContactImrequired'
  6394. ),
  6395. '/div'
  6396. );
  6397. $this->assertTags($result, $expected);
  6398. $result = $this->Form->input('Contact.imalsorequired');
  6399. $expected = array(
  6400. 'div' => array('class' => 'input text required'),
  6401. 'label' => array('for' => 'ContactImalsorequired'),
  6402. 'Imalsorequired',
  6403. '/label',
  6404. 'input' => array(
  6405. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6406. 'id' => 'ContactImalsorequired'
  6407. ),
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. $result = $this->Form->input('Contact.imrequiredtoo');
  6412. $expected = array(
  6413. 'div' => array('class' => 'input text required'),
  6414. 'label' => array('for' => 'ContactImrequiredtoo'),
  6415. 'Imrequiredtoo',
  6416. '/label',
  6417. 'input' => array(
  6418. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6419. 'id' => 'ContactImrequiredtoo'
  6420. ),
  6421. '/div'
  6422. );
  6423. $this->assertTags($result, $expected);
  6424. $result = $this->Form->input('Contact.required_one');
  6425. $expected = array(
  6426. 'div' => array('class' => 'input text required'),
  6427. 'label' => array('for' => 'ContactRequiredOne'),
  6428. 'Required One',
  6429. '/label',
  6430. 'input' => array(
  6431. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6432. 'id' => 'ContactRequiredOne'
  6433. ),
  6434. '/div'
  6435. );
  6436. $this->assertTags($result, $expected);
  6437. $result = $this->Form->input('Contact.string_required');
  6438. $expected = array(
  6439. 'div' => array('class' => 'input text required'),
  6440. 'label' => array('for' => 'ContactStringRequired'),
  6441. 'String Required',
  6442. '/label',
  6443. 'input' => array(
  6444. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6445. 'id' => 'ContactStringRequired'
  6446. ),
  6447. '/div'
  6448. );
  6449. $this->assertTags($result, $expected);
  6450. $result = $this->Form->input('Contact.imnotrequired');
  6451. $expected = array(
  6452. 'div' => array('class' => 'input text'),
  6453. 'label' => array('for' => 'ContactImnotrequired'),
  6454. 'Imnotrequired',
  6455. '/label',
  6456. 'input' => array(
  6457. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6458. 'id' => 'ContactImnotrequired'
  6459. ),
  6460. '/div'
  6461. );
  6462. $this->assertTags($result, $expected);
  6463. $result = $this->Form->input('Contact.imalsonotrequired');
  6464. $expected = array(
  6465. 'div' => array('class' => 'input text'),
  6466. 'label' => array('for' => 'ContactImalsonotrequired'),
  6467. 'Imalsonotrequired',
  6468. '/label',
  6469. 'input' => array(
  6470. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6471. 'id' => 'ContactImalsonotrequired'
  6472. ),
  6473. '/div'
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. $result = $this->Form->input('Contact.imalsonotrequired2');
  6477. $expected = array(
  6478. 'div' => array('class' => 'input text'),
  6479. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6480. 'Imalsonotrequired2',
  6481. '/label',
  6482. 'input' => array(
  6483. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6484. 'id' => 'ContactImalsonotrequired2'
  6485. ),
  6486. '/div'
  6487. );
  6488. $this->assertTags($result, $expected);
  6489. $result = $this->Form->input('Contact.imnotrequiredeither');
  6490. $expected = array(
  6491. 'div' => array('class' => 'input text'),
  6492. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6493. 'Imnotrequiredeither',
  6494. '/label',
  6495. 'input' => array(
  6496. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6497. 'id' => 'ContactImnotrequiredeither'
  6498. ),
  6499. '/div'
  6500. );
  6501. $this->assertTags($result, $expected);
  6502. $result = $this->Form->input('Contact.iamrequiredalways');
  6503. $expected = array(
  6504. 'div' => array('class' => 'input text required'),
  6505. 'label' => array('for' => 'ContactIamrequiredalways'),
  6506. 'Iamrequiredalways',
  6507. '/label',
  6508. 'input' => array(
  6509. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6510. 'id' => 'ContactIamrequiredalways'
  6511. ),
  6512. '/div'
  6513. );
  6514. $this->assertTags($result, $expected);
  6515. }
  6516. /**
  6517. * testFormMagicInput method
  6518. *
  6519. * @return void
  6520. */
  6521. public function testFormMagicInput() {
  6522. $encoding = strtolower(Configure::read('App.encoding'));
  6523. $result = $this->Form->create('Contact');
  6524. $expected = array(
  6525. 'form' => array(
  6526. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6527. 'accept-charset' => $encoding
  6528. ),
  6529. 'div' => array('style' => 'display:none;'),
  6530. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6531. '/div'
  6532. );
  6533. $this->assertTags($result, $expected);
  6534. $result = $this->Form->input('name');
  6535. $expected = array(
  6536. 'div' => array('class' => 'input text'),
  6537. 'label' => array('for' => 'ContactName'),
  6538. 'Name',
  6539. '/label',
  6540. 'input' => array(
  6541. 'type' => 'text', 'name' => 'data[Contact][name]',
  6542. 'id' => 'ContactName', 'maxlength' => '255'
  6543. ),
  6544. '/div'
  6545. );
  6546. $this->assertTags($result, $expected);
  6547. $result = $this->Form->input('non_existing_field_in_contact_model');
  6548. $expected = array(
  6549. 'div' => array('class' => 'input text'),
  6550. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6551. 'Non Existing Field In Contact Model',
  6552. '/label',
  6553. 'input' => array(
  6554. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6555. 'id' => 'ContactNonExistingFieldInContactModel'
  6556. ),
  6557. '/div'
  6558. );
  6559. $this->assertTags($result, $expected);
  6560. $result = $this->Form->input('Address.street');
  6561. $expected = array(
  6562. 'div' => array('class' => 'input text'),
  6563. 'label' => array('for' => 'AddressStreet'),
  6564. 'Street',
  6565. '/label',
  6566. 'input' => array(
  6567. 'type' => 'text', 'name' => 'data[Address][street]',
  6568. 'id' => 'AddressStreet'
  6569. ),
  6570. '/div'
  6571. );
  6572. $this->assertTags($result, $expected);
  6573. $result = $this->Form->input('Address.non_existing_field_in_model');
  6574. $expected = array(
  6575. 'div' => array('class' => 'input text'),
  6576. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6577. 'Non Existing Field In Model',
  6578. '/label',
  6579. 'input' => array(
  6580. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6581. 'id' => 'AddressNonExistingFieldInModel'
  6582. ),
  6583. '/div'
  6584. );
  6585. $this->assertTags($result, $expected);
  6586. $result = $this->Form->input('name', array('div' => false));
  6587. $expected = array(
  6588. 'label' => array('for' => 'ContactName'),
  6589. 'Name',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'data[Contact][name]',
  6593. 'id' => 'ContactName', 'maxlength' => '255'
  6594. )
  6595. );
  6596. $this->assertTags($result, $expected);
  6597. extract($this->dateRegex);
  6598. $now = strtotime('now');
  6599. $result = $this->Form->input('Contact.published', array('div' => false));
  6600. $expected = array(
  6601. 'label' => array('for' => 'ContactPublishedMonth'),
  6602. 'Published',
  6603. '/label',
  6604. array('select' => array(
  6605. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6606. )),
  6607. $monthsRegex,
  6608. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6609. date('F', $now),
  6610. '/option',
  6611. '*/select',
  6612. '-',
  6613. array('select' => array(
  6614. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6615. )),
  6616. $daysRegex,
  6617. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6618. date('j', $now),
  6619. '/option',
  6620. '*/select',
  6621. '-',
  6622. array('select' => array(
  6623. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6624. )),
  6625. $yearsRegex,
  6626. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6627. date('Y', $now),
  6628. '*/select'
  6629. );
  6630. $this->assertTags($result, $expected);
  6631. $result = $this->Form->input('Contact.updated', array('div' => false));
  6632. $expected = array(
  6633. 'label' => array('for' => 'ContactUpdatedMonth'),
  6634. 'Updated',
  6635. '/label',
  6636. array('select' => array(
  6637. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6638. )),
  6639. $monthsRegex,
  6640. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6641. date('F', $now),
  6642. '/option',
  6643. '*/select',
  6644. '-',
  6645. array('select' => array(
  6646. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6647. )),
  6648. $daysRegex,
  6649. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6650. date('j', $now),
  6651. '/option',
  6652. '*/select',
  6653. '-',
  6654. array('select' => array(
  6655. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6656. )),
  6657. $yearsRegex,
  6658. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6659. date('Y', $now),
  6660. '*/select'
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. $result = $this->Form->input('UserForm.stuff');
  6664. $expected = array(
  6665. 'div' => array('class' => 'input text'),
  6666. 'label' => array('for' => 'UserFormStuff'),
  6667. 'Stuff',
  6668. '/label',
  6669. 'input' => array(
  6670. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6671. 'id' => 'UserFormStuff', 'maxlength' => 10
  6672. ),
  6673. '/div'
  6674. );
  6675. $this->assertTags($result, $expected);
  6676. }
  6677. /**
  6678. * testForMagicInputNonExistingNorValidated method
  6679. *
  6680. * @return void
  6681. */
  6682. public function testForMagicInputNonExistingNorValidated() {
  6683. $encoding = strtolower(Configure::read('App.encoding'));
  6684. $result = $this->Form->create('Contact');
  6685. $expected = array(
  6686. 'form' => array(
  6687. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6688. 'accept-charset' => $encoding
  6689. ),
  6690. 'div' => array('style' => 'display:none;'),
  6691. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6692. '/div'
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6696. $expected = array(
  6697. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6698. 'Non Existing Nor Validated',
  6699. '/label',
  6700. 'input' => array(
  6701. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6702. 'id' => 'ContactNonExistingNorValidated'
  6703. )
  6704. );
  6705. $this->assertTags($result, $expected);
  6706. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6707. 'div' => false, 'value' => 'my value'
  6708. ));
  6709. $expected = array(
  6710. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6711. 'Non Existing Nor Validated',
  6712. '/label',
  6713. 'input' => array(
  6714. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6715. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6716. )
  6717. );
  6718. $this->assertTags($result, $expected);
  6719. $this->Form->request->data = array(
  6720. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6721. ));
  6722. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6723. $expected = array(
  6724. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6725. 'Non Existing Nor Validated',
  6726. '/label',
  6727. 'input' => array(
  6728. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6729. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6730. )
  6731. );
  6732. $this->assertTags($result, $expected);
  6733. }
  6734. /**
  6735. * testFormMagicInputLabel method
  6736. *
  6737. * @return void
  6738. */
  6739. public function testFormMagicInputLabel() {
  6740. $encoding = strtolower(Configure::read('App.encoding'));
  6741. $result = $this->Form->create('Contact');
  6742. $expected = array(
  6743. 'form' => array(
  6744. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6745. 'accept-charset' => $encoding
  6746. ),
  6747. 'div' => array('style' => 'display:none;'),
  6748. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6749. '/div'
  6750. );
  6751. $this->assertTags($result, $expected);
  6752. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6753. $this->assertTags($result, array('input' => array(
  6754. 'name' => 'data[Contact][name]', 'type' => 'text',
  6755. 'id' => 'ContactName', 'maxlength' => '255')
  6756. ));
  6757. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6758. $expected = array(
  6759. 'label' => array('for' => 'ContactName'),
  6760. 'My label',
  6761. '/label',
  6762. 'input' => array(
  6763. 'type' => 'text', 'name' => 'data[Contact][name]',
  6764. 'id' => 'ContactName', 'maxlength' => '255'
  6765. )
  6766. );
  6767. $this->assertTags($result, $expected);
  6768. $result = $this->Form->input('Contact.name', array(
  6769. 'div' => false, 'label' => array('class' => 'mandatory')
  6770. ));
  6771. $expected = array(
  6772. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6773. 'Name',
  6774. '/label',
  6775. 'input' => array(
  6776. 'type' => 'text', 'name' => 'data[Contact][name]',
  6777. 'id' => 'ContactName', 'maxlength' => '255'
  6778. )
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->input('Contact.name', array(
  6782. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6783. ));
  6784. $expected = array(
  6785. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6786. 'My label',
  6787. '/label',
  6788. 'input' => array(
  6789. 'type' => 'text', 'name' => 'data[Contact][name]',
  6790. 'id' => 'ContactName', 'maxlength' => '255'
  6791. )
  6792. );
  6793. $this->assertTags($result, $expected);
  6794. $result = $this->Form->input('Contact.name', array(
  6795. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6796. ));
  6797. $expected = array(
  6798. 'label' => array('for' => 'my_id'),
  6799. 'Name',
  6800. '/label',
  6801. 'input' => array(
  6802. 'type' => 'text', 'name' => 'data[Contact][name]',
  6803. 'id' => 'my_id', 'maxlength' => '255'
  6804. )
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. $result = $this->Form->input('1.id');
  6808. $this->assertTags($result, array('input' => array(
  6809. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6810. 'id' => 'Contact1Id'
  6811. )));
  6812. $result = $this->Form->input("1.name");
  6813. $expected = array(
  6814. 'div' => array('class' => 'input text'),
  6815. 'label' => array('for' => 'Contact1Name'),
  6816. 'Name',
  6817. '/label',
  6818. 'input' => array(
  6819. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6820. 'id' => 'Contact1Name', 'maxlength' => '255'
  6821. ),
  6822. '/div'
  6823. );
  6824. $this->assertTags($result, $expected);
  6825. $result = $this->Form->input('Contact.1.id');
  6826. $this->assertTags($result, array(
  6827. 'input' => array(
  6828. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6829. 'id' => 'Contact1Id'
  6830. )
  6831. ));
  6832. $result = $this->Form->input("Model.1.name");
  6833. $expected = array(
  6834. 'div' => array('class' => 'input text'),
  6835. 'label' => array('for' => 'Model1Name'),
  6836. 'Name',
  6837. '/label',
  6838. 'input' => array(
  6839. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6840. 'id' => 'Model1Name'
  6841. ),
  6842. '/div'
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. }
  6846. /**
  6847. * testFormEnd method
  6848. *
  6849. * @return void
  6850. */
  6851. public function testFormEnd() {
  6852. $this->assertEquals('</form>', $this->Form->end());
  6853. $result = $this->Form->end('');
  6854. $expected = array(
  6855. 'div' => array('class' => 'submit'),
  6856. 'input' => array('type' => 'submit', 'value' => ''),
  6857. '/div',
  6858. '/form'
  6859. );
  6860. $this->assertTags($result, $expected);
  6861. $result = $this->Form->end(array('label' => ''));
  6862. $expected = array(
  6863. 'div' => array('class' => 'submit'),
  6864. 'input' => array('type' => 'submit', 'value' => ''),
  6865. '/div',
  6866. '/form'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $result = $this->Form->end('save');
  6870. $expected = array(
  6871. 'div' => array('class' => 'submit'),
  6872. 'input' => array('type' => 'submit', 'value' => 'save'),
  6873. '/div',
  6874. '/form'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. $result = $this->Form->end(array('label' => 'save'));
  6878. $expected = array(
  6879. 'div' => array('class' => 'submit'),
  6880. 'input' => array('type' => 'submit', 'value' => 'save'),
  6881. '/div',
  6882. '/form'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6886. $expected = array(
  6887. 'div' => array('class' => 'submit'),
  6888. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6889. '/div',
  6890. '/form'
  6891. );
  6892. $this->assertTags($result, $expected);
  6893. $result = $this->Form->end(array('name' => 'Whatever'));
  6894. $expected = array(
  6895. 'div' => array('class' => 'submit'),
  6896. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6897. '/div',
  6898. '/form'
  6899. );
  6900. $this->assertTags($result, $expected);
  6901. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6902. $expected = array(
  6903. 'div' => array('class' => 'good'),
  6904. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6905. '/div',
  6906. '/form'
  6907. );
  6908. $this->assertTags($result, $expected);
  6909. $result = $this->Form->end(array(
  6910. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6911. ));
  6912. $expected = array(
  6913. 'div' => array('class' => 'good'),
  6914. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6915. '/div',
  6916. '/form'
  6917. );
  6918. $this->assertTags($result, $expected);
  6919. }
  6920. /**
  6921. * testMultipleFormWithIdFields method
  6922. *
  6923. * @return void
  6924. */
  6925. public function testMultipleFormWithIdFields() {
  6926. $this->Form->create('UserForm');
  6927. $result = $this->Form->input('id');
  6928. $this->assertTags($result, array('input' => array(
  6929. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6930. )));
  6931. $result = $this->Form->input('ValidateItem.id');
  6932. $this->assertTags($result, array('input' => array(
  6933. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6934. 'id' => 'ValidateItemId'
  6935. )));
  6936. $result = $this->Form->input('ValidateUser.id');
  6937. $this->assertTags($result, array('input' => array(
  6938. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6939. 'id' => 'ValidateUserId'
  6940. )));
  6941. }
  6942. /**
  6943. * testDbLessModel method
  6944. *
  6945. * @return void
  6946. */
  6947. public function testDbLessModel() {
  6948. $this->Form->create('TestMail');
  6949. $result = $this->Form->input('name');
  6950. $expected = array(
  6951. 'div' => array('class' => 'input text'),
  6952. 'label' => array('for' => 'TestMailName'),
  6953. 'Name',
  6954. '/label',
  6955. 'input' => array(
  6956. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6957. 'id' => 'TestMailName'
  6958. ),
  6959. '/div'
  6960. );
  6961. $this->assertTags($result, $expected);
  6962. ClassRegistry::init('TestMail');
  6963. $this->Form->create('TestMail');
  6964. $result = $this->Form->input('name');
  6965. $expected = array(
  6966. 'div' => array('class' => 'input text'),
  6967. 'label' => array('for' => 'TestMailName'),
  6968. 'Name',
  6969. '/label',
  6970. 'input' => array(
  6971. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6972. 'id' => 'TestMailName'
  6973. ),
  6974. '/div'
  6975. );
  6976. $this->assertTags($result, $expected);
  6977. }
  6978. /**
  6979. * testBrokenness method
  6980. *
  6981. * @return void
  6982. */
  6983. public function testBrokenness() {
  6984. /*
  6985. * #4 This test has two parents and four children. By default (as of r7117) both
  6986. * parents are show but the first parent is missing a child. This is the inconsistency
  6987. * in the default behaviour - one parent has all children, the other does not - dependent
  6988. * on the data values.
  6989. */
  6990. $result = $this->Form->select('Model.field', array(
  6991. 'Fred' => array(
  6992. 'freds_son_1' => 'Fred',
  6993. 'freds_son_2' => 'Freddie'
  6994. ),
  6995. 'Bert' => array(
  6996. 'berts_son_1' => 'Albert',
  6997. 'berts_son_2' => 'Bertie')
  6998. ),
  6999. array('showParents' => true, 'empty' => false)
  7000. );
  7001. $expected = array(
  7002. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7003. array('optgroup' => array('label' => 'Fred')),
  7004. array('option' => array('value' => 'freds_son_1')),
  7005. 'Fred',
  7006. '/option',
  7007. array('option' => array('value' => 'freds_son_2')),
  7008. 'Freddie',
  7009. '/option',
  7010. '/optgroup',
  7011. array('optgroup' => array('label' => 'Bert')),
  7012. array('option' => array('value' => 'berts_son_1')),
  7013. 'Albert',
  7014. '/option',
  7015. array('option' => array('value' => 'berts_son_2')),
  7016. 'Bertie',
  7017. '/option',
  7018. '/optgroup',
  7019. '/select'
  7020. );
  7021. $this->assertTags($result, $expected);
  7022. /*
  7023. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7024. * changed, so we should expect the same select list data, just with a different name
  7025. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7026. * This is where data corruption can occur, because when a select value is missing from
  7027. * a list a form will substitute the first value in the list - without the user knowing.
  7028. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7029. * affect the availability of 3 => 'Three' as a valid option.
  7030. */
  7031. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7032. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7033. ));
  7034. $result = $this->Form->select(
  7035. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7036. );
  7037. $expected = array(
  7038. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7039. array('option' => array('value' => 1)),
  7040. 'One',
  7041. '/option',
  7042. array('option' => array('value' => 2)),
  7043. 'Two',
  7044. '/option',
  7045. array('optgroup' => array('label' => 'Three')),
  7046. array('option' => array('value' => 3)),
  7047. 'Three',
  7048. '/option',
  7049. array('option' => array('value' => 4)),
  7050. 'Four',
  7051. '/option',
  7052. array('option' => array('value' => 5)),
  7053. 'Five',
  7054. '/option',
  7055. '/optgroup',
  7056. '/select'
  7057. );
  7058. $this->assertTags($result, $expected);
  7059. }
  7060. /**
  7061. * Test the generation of fields for a multi record form.
  7062. *
  7063. * @return void
  7064. */
  7065. public function testMultiRecordForm() {
  7066. $this->Form->create('ValidateProfile');
  7067. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7068. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7069. $expected = array(
  7070. 'div' => array('class' => 'input textarea'),
  7071. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7072. 'Name',
  7073. '/label',
  7074. 'textarea' => array(
  7075. 'id' => 'ValidateProfile1ValidateItem2Name',
  7076. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7077. 'cols' => 30,
  7078. 'rows' => 6
  7079. ),
  7080. 'Value',
  7081. '/textarea',
  7082. '/div'
  7083. );
  7084. $this->assertTags($result, $expected);
  7085. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7086. $expected = array(
  7087. 'div' => array('class' => 'input date'),
  7088. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7089. 'Created',
  7090. '/label',
  7091. array('select' => array(
  7092. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7093. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7094. )
  7095. ),
  7096. array('option' => array('value' => '')), '/option',
  7097. $this->dateRegex['monthsRegex'],
  7098. '/select', '-',
  7099. array('select' => array(
  7100. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7101. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7102. )
  7103. ),
  7104. array('option' => array('value' => '')), '/option',
  7105. $this->dateRegex['daysRegex'],
  7106. '/select', '-',
  7107. array('select' => array(
  7108. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7109. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7110. )
  7111. ),
  7112. array('option' => array('value' => '')), '/option',
  7113. $this->dateRegex['yearsRegex'],
  7114. '/select',
  7115. '/div'
  7116. );
  7117. $this->assertTags($result, $expected);
  7118. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7119. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7120. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7121. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7122. $expected = array(
  7123. 'div' => array('class' => 'input select error'),
  7124. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7125. 'Profile',
  7126. '/label',
  7127. 'select' => array(
  7128. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7129. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7130. 'class' => 'form-error'
  7131. ),
  7132. '/select',
  7133. array('div' => array('class' => 'error-message')),
  7134. 'Error',
  7135. '/div',
  7136. '/div'
  7137. );
  7138. $this->assertTags($result, $expected);
  7139. }
  7140. /**
  7141. * test the correct display of multi-record form validation errors.
  7142. *
  7143. * @return void
  7144. */
  7145. public function testMultiRecordFormValidationErrors() {
  7146. $this->Form->create('ValidateProfile');
  7147. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7148. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7149. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7150. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7151. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7152. $result = $this->Form->error('ValidateProfile.2.city');
  7153. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7154. $result = $this->Form->error('2.city');
  7155. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7156. }
  7157. /**
  7158. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7159. *
  7160. * @return void
  7161. */
  7162. public function testInputTemplate() {
  7163. $result = $this->Form->input('Contact.email', array(
  7164. 'type' => 'text', 'format' => array('input')
  7165. ));
  7166. $expected = array(
  7167. 'div' => array('class' => 'input text'),
  7168. 'input' => array(
  7169. 'type' => 'text', 'name' => 'data[Contact][email]',
  7170. 'id' => 'ContactEmail'
  7171. ),
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. $result = $this->Form->input('Contact.email', array(
  7176. 'type' => 'text', 'format' => array('input', 'label'),
  7177. 'label' => '<em>Email (required)</em>'
  7178. ));
  7179. $expected = array(
  7180. 'div' => array('class' => 'input text'),
  7181. array('input' => array(
  7182. 'type' => 'text', 'name' => 'data[Contact][email]',
  7183. 'id' => 'ContactEmail'
  7184. )),
  7185. 'label' => array('for' => 'ContactEmail'),
  7186. 'em' => array(),
  7187. 'Email (required)',
  7188. '/em',
  7189. '/label',
  7190. '/div'
  7191. );
  7192. $this->assertTags($result, $expected);
  7193. $result = $this->Form->input('Contact.email', array(
  7194. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7195. 'between' => '<div>Something in the middle</div>',
  7196. 'after' => '<span>Some text at the end</span>'
  7197. ));
  7198. $expected = array(
  7199. 'div' => array('class' => 'input text'),
  7200. array('input' => array(
  7201. 'type' => 'text', 'name' => 'data[Contact][email]',
  7202. 'id' => 'ContactEmail'
  7203. )),
  7204. array('div' => array()),
  7205. 'Something in the middle',
  7206. '/div',
  7207. 'label' => array('for' => 'ContactEmail'),
  7208. 'Email',
  7209. '/label',
  7210. 'span' => array(),
  7211. 'Some text at the end',
  7212. '/span',
  7213. '/div'
  7214. );
  7215. $this->assertTags($result, $expected);
  7216. $result = $this->Form->input('Contact.method', array(
  7217. 'type' => 'radio',
  7218. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7219. 'between' => 'I am between',
  7220. ));
  7221. $expected = array(
  7222. 'div' => array('class' => 'input radio'),
  7223. 'fieldset' => array(),
  7224. 'legend' => array(),
  7225. 'Method',
  7226. '/legend',
  7227. 'I am between',
  7228. 'input' => array(
  7229. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7230. 'value' => '', 'id' => 'ContactMethod_'
  7231. ),
  7232. array('input' => array(
  7233. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7234. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7235. )),
  7236. array('label' => array('for' => 'ContactMethodEmail')),
  7237. 'Email',
  7238. '/label',
  7239. array('input' => array(
  7240. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7241. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7242. )),
  7243. array('label' => array('for' => 'ContactMethodPigeon')),
  7244. 'Pigeon',
  7245. '/label',
  7246. '/fieldset',
  7247. '/div',
  7248. );
  7249. $this->assertTags($result, $expected);
  7250. }
  7251. /**
  7252. * test that some html5 inputs + FormHelper::__call() work
  7253. *
  7254. * @return void
  7255. */
  7256. public function testHtml5Inputs() {
  7257. $result = $this->Form->email('User.email');
  7258. $expected = array(
  7259. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7260. );
  7261. $this->assertTags($result, $expected);
  7262. $result = $this->Form->search('User.query');
  7263. $expected = array(
  7264. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7265. );
  7266. $this->assertTags($result, $expected);
  7267. $result = $this->Form->search('User.query', array('value' => 'test'));
  7268. $expected = array(
  7269. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7270. );
  7271. $this->assertTags($result, $expected);
  7272. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7273. $expected = array(
  7274. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7275. );
  7276. $this->assertTags($result, $expected);
  7277. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7278. $expected = array(
  7279. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7280. );
  7281. $this->assertTags($result, $expected);
  7282. }
  7283. /**
  7284. *
  7285. * @expectedException CakeException
  7286. * @return void
  7287. */
  7288. public function testHtml5InputException() {
  7289. $this->Form->email();
  7290. }
  7291. /**
  7292. * Tests that a model can be loaded from the model names passed in the request object
  7293. *
  7294. * @return void
  7295. */
  7296. public function testIntrospectModelFromRequest() {
  7297. $this->loadFixtures('Post');
  7298. App::build(array(
  7299. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7300. ));
  7301. CakePlugin::load('TestPlugin');
  7302. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7303. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7304. $this->Form->create('TestPluginPost');
  7305. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7306. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7307. CakePlugin::unload();
  7308. App::build();
  7309. }
  7310. /**
  7311. * Tests that it is possible to set the validation errors directly in the helper for a field
  7312. *
  7313. * @return void
  7314. */
  7315. public function testCustomValidationErrors() {
  7316. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7317. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7318. $this->assertEquals('Badness!', $result);
  7319. }
  7320. /**
  7321. * Tests that the 'on' key validates as expected on create
  7322. *
  7323. * @return void
  7324. */
  7325. public function testRequiredOnCreate() {
  7326. $this->Form->create('Contact');
  7327. $result = $this->Form->input('Contact.imrequiredonupdate');
  7328. $expected = array(
  7329. 'div' => array('class' => 'input text'),
  7330. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7331. 'Imrequiredonupdate',
  7332. '/label',
  7333. 'input' => array(
  7334. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7335. 'id' => 'ContactImrequiredonupdate'
  7336. ),
  7337. '/div'
  7338. );
  7339. $this->assertTags($result, $expected);
  7340. $result = $this->Form->input('Contact.imrequiredoncreate');
  7341. $expected = array(
  7342. 'div' => array('class' => 'input text required'),
  7343. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7344. 'Imrequiredoncreate',
  7345. '/label',
  7346. 'input' => array(
  7347. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7348. 'id' => 'ContactImrequiredoncreate'
  7349. ),
  7350. '/div'
  7351. );
  7352. $this->assertTags($result, $expected);
  7353. $result = $this->Form->input('Contact.imrequiredonboth');
  7354. $expected = array(
  7355. 'div' => array('class' => 'input text required'),
  7356. 'label' => array('for' => 'ContactImrequiredonboth'),
  7357. 'Imrequiredonboth',
  7358. '/label',
  7359. 'input' => array(
  7360. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7361. 'id' => 'ContactImrequiredonboth'
  7362. ),
  7363. '/div'
  7364. );
  7365. $this->assertTags($result, $expected);
  7366. $result = $this->Form->input('Contact.imrequired');
  7367. $expected = array(
  7368. 'div' => array('class' => 'input text required'),
  7369. 'label' => array('for' => 'ContactImrequired'),
  7370. 'Imrequired',
  7371. '/label',
  7372. 'input' => array(
  7373. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7374. 'id' => 'ContactImrequired'
  7375. ),
  7376. '/div'
  7377. );
  7378. $this->assertTags($result, $expected);
  7379. }
  7380. /**
  7381. * Tests that the 'on' key validates as expected on update
  7382. *
  7383. * @return void
  7384. */
  7385. public function testRequiredOnUpdate() {
  7386. $this->Form->request->data['Contact']['id'] = 1;
  7387. $this->Form->create('Contact');
  7388. $result = $this->Form->input('Contact.imrequiredonupdate');
  7389. $expected = array(
  7390. 'div' => array('class' => 'input text required'),
  7391. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7392. 'Imrequiredonupdate',
  7393. '/label',
  7394. 'input' => array(
  7395. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7396. 'id' => 'ContactImrequiredonupdate'
  7397. ),
  7398. '/div'
  7399. );
  7400. $this->assertTags($result, $expected);
  7401. $result = $this->Form->input('Contact.imrequiredoncreate');
  7402. $expected = array(
  7403. 'div' => array('class' => 'input text'),
  7404. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7405. 'Imrequiredoncreate',
  7406. '/label',
  7407. 'input' => array(
  7408. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7409. 'id' => 'ContactImrequiredoncreate'
  7410. ),
  7411. '/div'
  7412. );
  7413. $this->assertTags($result, $expected);
  7414. $result = $this->Form->input('Contact.imrequiredonboth');
  7415. $expected = array(
  7416. 'div' => array('class' => 'input text required'),
  7417. 'label' => array('for' => 'ContactImrequiredonboth'),
  7418. 'Imrequiredonboth',
  7419. '/label',
  7420. 'input' => array(
  7421. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7422. 'id' => 'ContactImrequiredonboth'
  7423. ),
  7424. '/div'
  7425. );
  7426. $this->assertTags($result, $expected);
  7427. $result = $this->Form->input('Contact.imrequired');
  7428. $expected = array(
  7429. 'div' => array('class' => 'input text required'),
  7430. 'label' => array('for' => 'ContactImrequired'),
  7431. 'Imrequired',
  7432. '/label',
  7433. 'input' => array(
  7434. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7435. 'id' => 'ContactImrequired'
  7436. ),
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. }
  7441. /**
  7442. * Test inputDefaults setter and getter
  7443. *
  7444. * @return void
  7445. */
  7446. public function testInputDefaults() {
  7447. $this->Form->create('Contact');
  7448. $this->Form->inputDefaults(array(
  7449. 'label' => false,
  7450. 'div' => array(
  7451. 'style' => 'color: #000;'
  7452. )
  7453. ));
  7454. $result = $this->Form->input('Contact.field1');
  7455. $expected = array(
  7456. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7457. 'input' => array(
  7458. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7459. 'id' => 'ContactField1'
  7460. ),
  7461. '/div'
  7462. );
  7463. $this->assertTags($result, $expected);
  7464. $this->Form->inputDefaults(array(
  7465. 'div' => false,
  7466. 'label' => 'Label',
  7467. ));
  7468. $result = $this->Form->input('Contact.field1');
  7469. $expected = array(
  7470. 'label' => array('for' => 'ContactField1'),
  7471. 'Label',
  7472. '/label',
  7473. 'input' => array(
  7474. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7475. 'id' => 'ContactField1'
  7476. ),
  7477. );
  7478. $this->assertTags($result, $expected);
  7479. $this->Form->inputDefaults(array(
  7480. 'label' => false,
  7481. ), true);
  7482. $result = $this->Form->input('Contact.field1');
  7483. $expected = array(
  7484. 'input' => array(
  7485. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7486. 'id' => 'ContactField1'
  7487. ),
  7488. );
  7489. $this->assertTags($result, $expected);
  7490. $result = $this->Form->inputDefaults();
  7491. $expected = array(
  7492. 'div' => false,
  7493. 'label' => false,
  7494. );
  7495. $this->assertEqual($result, $expected);
  7496. }
  7497. }