FormHelperTest.php 261 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253
  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), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. 'iamrequiredalways' => array(
  114. 'email' => array('rule' => 'email'),
  115. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  116. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  117. ),
  118. );
  119. /**
  120. * schema method
  121. *
  122. * @return void
  123. */
  124. public function setSchema($schema) {
  125. $this->_schema = $schema;
  126. }
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  133. /**
  134. * hasAndBelongsToMany property
  135. *
  136. * @var array
  137. */
  138. public $belongsTo = array('User' => array('className' => 'UserForm'));
  139. }
  140. /**
  141. * ContactTagsContact class
  142. *
  143. * @package cake
  144. * @package Cake.Test.Case.View.Helper
  145. */
  146. class ContactTagsContact extends CakeTestModel {
  147. /**
  148. * useTable property
  149. *
  150. * @var bool false
  151. */
  152. public $useTable = false;
  153. /**
  154. * name property
  155. *
  156. * @var string 'Contact'
  157. */
  158. public $name = 'ContactTagsContact';
  159. /**
  160. * Default schema
  161. *
  162. * @var array
  163. */
  164. protected $_schema = array(
  165. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  166. 'contact_tag_id' => array(
  167. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  168. )
  169. );
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function setSchema($schema) {
  176. $this->_schema = $schema;
  177. }
  178. }
  179. /**
  180. * ContactNonStandardPk class
  181. *
  182. * @package cake
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactNonStandardPk extends Contact {
  186. /**
  187. * primaryKey property
  188. *
  189. * @var string 'pk'
  190. */
  191. public $primaryKey = 'pk';
  192. /**
  193. * name property
  194. *
  195. * @var string 'ContactNonStandardPk'
  196. */
  197. public $name = 'ContactNonStandardPk';
  198. /**
  199. * schema method
  200. *
  201. * @return void
  202. */
  203. public function schema($field = false) {
  204. $this->_schema = parent::schema();
  205. $this->_schema['pk'] = $this->_schema['id'];
  206. unset($this->_schema['id']);
  207. return $this->_schema;
  208. }
  209. }
  210. /**
  211. * ContactTag class
  212. *
  213. * @package cake
  214. * @package Cake.Test.Case.View.Helper
  215. */
  216. class ContactTag extends Model {
  217. /**
  218. * useTable property
  219. *
  220. * @var bool false
  221. */
  222. public $useTable = false;
  223. /**
  224. * schema definition
  225. *
  226. * @var array
  227. */
  228. protected $_schema = array(
  229. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  230. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  231. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  232. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  233. );
  234. }
  235. /**
  236. * UserForm class
  237. *
  238. * @package cake
  239. * @package Cake.Test.Case.View.Helper
  240. */
  241. class UserForm extends CakeTestModel {
  242. /**
  243. * useTable property
  244. *
  245. * @var bool false
  246. */
  247. public $useTable = false;
  248. /**
  249. * primaryKey property
  250. *
  251. * @var string 'id'
  252. */
  253. public $primaryKey = 'id';
  254. /**
  255. * name property
  256. *
  257. * @var string 'UserForm'
  258. */
  259. public $name = 'UserForm';
  260. /**
  261. * hasMany property
  262. *
  263. * @var array
  264. */
  265. public $hasMany = array(
  266. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  267. ));
  268. /**
  269. * schema definition
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  276. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  277. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  278. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  279. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  280. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  281. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  282. );
  283. }
  284. /**
  285. * OpenidUrl class
  286. *
  287. * @package cake
  288. * @package Cake.Test.Case.View.Helper
  289. */
  290. class OpenidUrl extends CakeTestModel {
  291. /**
  292. * useTable property
  293. *
  294. * @var bool false
  295. */
  296. public $useTable = false;
  297. /**
  298. * primaryKey property
  299. *
  300. * @var string 'id'
  301. */
  302. public $primaryKey = 'id';
  303. /**
  304. * name property
  305. *
  306. * @var string 'OpenidUrl'
  307. */
  308. public $name = 'OpenidUrl';
  309. /**
  310. * belongsTo property
  311. *
  312. * @var array
  313. */
  314. public $belongsTo = array('UserForm' => array(
  315. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  316. ));
  317. /**
  318. * validate property
  319. *
  320. * @var array
  321. */
  322. public $validate = array('openid_not_registered' => array());
  323. /**
  324. * schema method
  325. *
  326. * @var array
  327. */
  328. protected $_schema = array(
  329. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'user_form_id' => array(
  331. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  332. ),
  333. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  334. );
  335. /**
  336. * beforeValidate method
  337. *
  338. * @return void
  339. */
  340. public function beforeValidate($options = array()) {
  341. $this->invalidate('openid_not_registered');
  342. return true;
  343. }
  344. }
  345. /**
  346. * ValidateUser class
  347. *
  348. * @package cake
  349. * @package Cake.Test.Case.View.Helper
  350. */
  351. class ValidateUser extends CakeTestModel {
  352. /**
  353. * primaryKey property
  354. *
  355. * @var string 'id'
  356. */
  357. public $primaryKey = 'id';
  358. /**
  359. * useTable property
  360. *
  361. * @var bool false
  362. */
  363. public $useTable = false;
  364. /**
  365. * name property
  366. *
  367. * @var string 'ValidateUser'
  368. */
  369. public $name = 'ValidateUser';
  370. /**
  371. * hasOne property
  372. *
  373. * @var array
  374. */
  375. public $hasOne = array('ValidateProfile' => array(
  376. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  377. ));
  378. /**
  379. * schema method
  380. *
  381. * @var array
  382. */
  383. protected $_schema = array(
  384. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  385. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  386. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  387. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  388. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  389. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  390. );
  391. /**
  392. * beforeValidate method
  393. *
  394. * @return void
  395. */
  396. public function beforeValidate($options = array()) {
  397. $this->invalidate('email');
  398. return false;
  399. }
  400. }
  401. /**
  402. * ValidateProfile class
  403. *
  404. * @package cake
  405. * @package Cake.Test.Case.View.Helper
  406. */
  407. class ValidateProfile extends CakeTestModel {
  408. /**
  409. * primaryKey property
  410. *
  411. * @var string 'id'
  412. */
  413. public $primaryKey = 'id';
  414. /**
  415. * useTable property
  416. *
  417. * @var bool false
  418. */
  419. public $useTable = false;
  420. /**
  421. * schema property
  422. *
  423. * @var array
  424. */
  425. protected $_schema = array(
  426. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  427. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  428. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  429. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  430. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  431. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  432. );
  433. /**
  434. * name property
  435. *
  436. * @var string 'ValidateProfile'
  437. */
  438. public $name = 'ValidateProfile';
  439. /**
  440. * hasOne property
  441. *
  442. * @var array
  443. */
  444. public $hasOne = array('ValidateItem' => array(
  445. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  446. ));
  447. /**
  448. * belongsTo property
  449. *
  450. * @var array
  451. */
  452. public $belongsTo = array('ValidateUser' => array(
  453. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  454. ));
  455. /**
  456. * beforeValidate method
  457. *
  458. * @return void
  459. */
  460. public function beforeValidate($options = array()) {
  461. $this->invalidate('full_name');
  462. $this->invalidate('city');
  463. return false;
  464. }
  465. }
  466. /**
  467. * ValidateItem class
  468. *
  469. * @package cake
  470. * @package Cake.Test.Case.View.Helper
  471. */
  472. class ValidateItem extends CakeTestModel {
  473. /**
  474. * primaryKey property
  475. *
  476. * @var string 'id'
  477. */
  478. public $primaryKey = 'id';
  479. /**
  480. * useTable property
  481. *
  482. * @var bool false
  483. */
  484. public $useTable = false;
  485. /**
  486. * name property
  487. *
  488. * @var string 'ValidateItem'
  489. */
  490. public $name = 'ValidateItem';
  491. /**
  492. * schema property
  493. *
  494. * @var array
  495. */
  496. protected $_schema = array(
  497. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  498. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  499. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  500. 'description' => array(
  501. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  502. ),
  503. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  504. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  505. );
  506. /**
  507. * belongsTo property
  508. *
  509. * @var array
  510. */
  511. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  512. /**
  513. * beforeValidate method
  514. *
  515. * @return void
  516. */
  517. public function beforeValidate($options = array()) {
  518. $this->invalidate('description');
  519. return false;
  520. }
  521. }
  522. /**
  523. * TestMail class
  524. *
  525. * @package cake
  526. * @package Cake.Test.Case.View.Helper
  527. */
  528. class TestMail extends CakeTestModel {
  529. /**
  530. * primaryKey property
  531. *
  532. * @var string 'id'
  533. */
  534. public $primaryKey = 'id';
  535. /**
  536. * useTable property
  537. *
  538. * @var bool false
  539. */
  540. public $useTable = false;
  541. /**
  542. * name property
  543. *
  544. * @var string 'TestMail'
  545. */
  546. public $name = 'TestMail';
  547. }
  548. /**
  549. * FormHelperTest class
  550. *
  551. * @package cake
  552. * @subpackage Cake.Test.Case.View.Helper
  553. * @property FormHelper $Form
  554. */
  555. class FormHelperTest extends CakeTestCase {
  556. /**
  557. * Fixtures to be used
  558. *
  559. * @var array
  560. */
  561. public $fixtures = array('core.post');
  562. /**
  563. * Do not load the fixtures by default
  564. *
  565. * @var boolean
  566. */
  567. public $autoFixtures = false;
  568. /**
  569. * setUp method
  570. *
  571. * @return void
  572. */
  573. public function setUp() {
  574. parent::setUp();
  575. Configure::write('App.base', '');
  576. $this->Controller = new ContactTestController();
  577. $this->View = new View($this->Controller);
  578. $this->Form = new FormHelper($this->View);
  579. $this->Form->Html = new HtmlHelper($this->View);
  580. $this->Form->request = new CakeRequest('contacts/add', false);
  581. $this->Form->request->here = '/contacts/add';
  582. $this->Form->request['action'] = 'add';
  583. $this->Form->request->webroot = '';
  584. $this->Form->request->base = '';
  585. ClassRegistry::addObject('Contact', new Contact());
  586. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  587. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  588. ClassRegistry::addObject('User', new UserForm());
  589. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  590. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  591. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  592. $this->oldSalt = Configure::read('Security.salt');
  593. $this->dateRegex = array(
  594. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  595. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  596. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  597. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  598. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  599. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  600. );
  601. Configure::write('Security.salt', 'foo!');
  602. }
  603. /**
  604. * tearDown method
  605. *
  606. * @return void
  607. */
  608. public function tearDown() {
  609. parent::tearDown();
  610. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  611. Configure::write('Security.salt', $this->oldSalt);
  612. }
  613. /**
  614. * testFormCreateWithSecurity method
  615. *
  616. * Test form->create() with security key.
  617. *
  618. * @return void
  619. */
  620. public function testCreateWithSecurity() {
  621. $this->Form->request['_Token'] = array('key' => 'testKey');
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  624. $expected = array(
  625. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  626. 'div' => array('style' => 'display:none;'),
  627. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  630. )),
  631. '/div'
  632. );
  633. $this->assertTags($result, $expected);
  634. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  635. $expected['form']['id'] = 'MyForm';
  636. $this->assertTags($result, $expected);
  637. }
  638. /**
  639. * test that create() clears the fields property so it starts fresh
  640. *
  641. * @return void
  642. */
  643. public function testCreateClearingFields() {
  644. $this->Form->fields = array('model_id');
  645. $this->Form->create('Contact');
  646. $this->assertEquals(array(), $this->Form->fields);
  647. }
  648. /**
  649. * Tests form hash generation with model-less data
  650. *
  651. * @return void
  652. */
  653. public function testValidateHashNoModel() {
  654. $this->Form->request['_Token'] = array('key' => 'foo');
  655. $result = $this->Form->secure(array('anything'));
  656. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  657. }
  658. /**
  659. * Tests that models with identical field names get resolved properly
  660. *
  661. * @return void
  662. */
  663. public function testDuplicateFieldNameResolution() {
  664. $result = $this->Form->create('ValidateUser');
  665. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  666. $result = $this->Form->input('ValidateItem.name');
  667. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  668. $result = $this->Form->input('ValidateUser.name');
  669. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  670. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  671. $this->assertRegExp('/type="text"/', $result);
  672. $result = $this->Form->input('ValidateItem.name');
  673. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  675. $this->assertRegExp('/<textarea/', $result);
  676. $result = $this->Form->input('name');
  677. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  679. $this->assertRegExp('/type="text"/', $result);
  680. }
  681. /**
  682. * Tests that hidden fields generated for checkboxes don't get locked
  683. *
  684. * @return void
  685. */
  686. public function testNoCheckboxLocking() {
  687. $this->Form->request['_Token'] = array('key' => 'foo');
  688. $this->assertSame(array(), $this->Form->fields);
  689. $this->Form->checkbox('check', array('value' => '1'));
  690. $this->assertSame($this->Form->fields, array('check'));
  691. }
  692. /**
  693. * testFormSecurityFields method
  694. *
  695. * Test generation of secure form hash generation.
  696. *
  697. * @return void
  698. */
  699. public function testFormSecurityFields() {
  700. $key = 'testKey';
  701. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  702. $this->Form->request['_Token'] = array('key' => $key);
  703. $result = $this->Form->secure($fields);
  704. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  705. $hash .= ':' . 'Model.valid';
  706. $hash = urlencode($hash);
  707. $expected = array(
  708. 'div' => array('style' => 'display:none;'),
  709. array('input' => array(
  710. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  711. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  712. )),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  715. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  716. )),
  717. '/div'
  718. );
  719. $this->assertTags($result, $expected);
  720. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  721. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  722. $result = $this->Form->secure($fields);
  723. $expected = array(
  724. 'div' => array('class' => 'hidden'),
  725. array('input' => array(
  726. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  727. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  728. )),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  731. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  732. )),
  733. '/div'
  734. );
  735. $this->assertTags($result, $expected);
  736. }
  737. /**
  738. * Tests correct generation of number fields for double and float fields
  739. *
  740. * @return void
  741. */
  742. public function testTextFieldGenerationForFloats() {
  743. $model = ClassRegistry::getObject('Contact');
  744. $model->setSchema(array('foo' => array(
  745. 'type' => 'float',
  746. 'null' => false,
  747. 'default' => null,
  748. 'length' => null
  749. )));
  750. $this->Form->create('Contact');
  751. $result = $this->Form->input('foo');
  752. $expected = array(
  753. 'div' => array('class' => 'input number'),
  754. 'label' => array('for' => 'ContactFoo'),
  755. 'Foo',
  756. '/label',
  757. array('input' => array(
  758. 'type' => 'number',
  759. 'name' => 'data[Contact][foo]',
  760. 'id' => 'ContactFoo',
  761. 'step' => 'any'
  762. )),
  763. '/div'
  764. );
  765. $this->assertTags($result, $expected);
  766. $result = $this->Form->input('foo', array('step' => 0.5));
  767. $expected = array(
  768. 'div' => array('class' => 'input number'),
  769. 'label' => array('for' => 'ContactFoo'),
  770. 'Foo',
  771. '/label',
  772. array('input' => array(
  773. 'type' => 'number',
  774. 'name' => 'data[Contact][foo]',
  775. 'id' => 'ContactFoo',
  776. 'step' => '0.5'
  777. )),
  778. '/div'
  779. );
  780. $this->assertTags($result, $expected);
  781. }
  782. /**
  783. * Tests correct generation of number fields for integer fields
  784. *
  785. * @access public
  786. * @return void
  787. */
  788. public function testTextFieldTypeNumberGenerationForIntegers() {
  789. $model = ClassRegistry::getObject('Contact');
  790. $model->setSchema(array('foo' => array(
  791. 'type' => 'integer',
  792. 'null' => false,
  793. 'default' => null,
  794. 'length' => null
  795. )));
  796. $this->Form->create('Contact');
  797. $result = $this->Form->input('foo');
  798. $expected = array(
  799. 'div' => array('class' => 'input number'),
  800. 'label' => array('for' => 'ContactFoo'),
  801. 'Foo',
  802. '/label',
  803. array('input' => array(
  804. 'type' => 'number', 'name' => 'data[Contact][foo]',
  805. 'id' => 'ContactFoo'
  806. )),
  807. '/div'
  808. );
  809. $this->assertTags($result, $expected);
  810. }
  811. /**
  812. * testFormSecurityMultipleFields method
  813. *
  814. * Test secure() with multiple row form. Ensure hash is correct.
  815. *
  816. * @return void
  817. */
  818. public function testFormSecurityMultipleFields() {
  819. $key = 'testKey';
  820. $fields = array(
  821. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  822. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  823. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  824. );
  825. $this->Form->request['_Token'] = array('key' => $key);
  826. $result = $this->Form->secure($fields);
  827. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  828. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  829. $expected = array(
  830. 'div' => array('style' => 'display:none;'),
  831. array('input' => array(
  832. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  833. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  834. )),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  837. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  838. )),
  839. '/div'
  840. );
  841. $this->assertTags($result, $expected);
  842. }
  843. /**
  844. * testFormSecurityMultipleSubmitButtons
  845. *
  846. * test form submit generation and ensure that _Token is only created on end()
  847. *
  848. * @return void
  849. */
  850. public function testFormSecurityMultipleSubmitButtons() {
  851. $key = 'testKey';
  852. $this->Form->request['_Token'] = array('key' => $key);
  853. $this->Form->create('Addresses');
  854. $this->Form->input('Address.title');
  855. $this->Form->input('Address.first_name');
  856. $result = $this->Form->submit('Save', array('name' => 'save'));
  857. $expected = array(
  858. 'div' => array('class' => 'submit'),
  859. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  860. '/div',
  861. );
  862. $this->assertTags($result, $expected);
  863. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  864. $expected = array(
  865. 'div' => array('class' => 'submit'),
  866. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  867. '/div',
  868. );
  869. $this->assertTags($result, $expected);
  870. $result = $this->Form->end(null);
  871. $expected = array(
  872. 'div' => array('style' => 'display:none;'),
  873. array('input' => array(
  874. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  875. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  876. )),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  879. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  880. )),
  881. '/div'
  882. );
  883. $this->assertTags($result, $expected);
  884. }
  885. /**
  886. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecurityButtonNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitNestedNamed() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('Addresses');
  907. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  908. $result = $this->Form->unlockField();
  909. $this->assertEquals(array('Address.button'), $result);
  910. }
  911. /**
  912. * Test that the correct fields are unlocked for image submits with no names.
  913. *
  914. * @return void
  915. */
  916. public function testSecuritySubmitImageNoName() {
  917. $key = 'testKey';
  918. $this->Form->request['_Token'] = array('key' => $key);
  919. $this->Form->create('User');
  920. $result = $this->Form->submit('save.png');
  921. $expected = array(
  922. 'div' => array('class' => 'submit'),
  923. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  924. '/div'
  925. );
  926. $this->assertTags($result, $expected);
  927. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  928. }
  929. /**
  930. * Test that the correct fields are unlocked for image submits with names.
  931. *
  932. * @return void
  933. */
  934. public function testSecuritySubmitImageName() {
  935. $key = 'testKey';
  936. $this->Form->request['_Token'] = array('key' => $key);
  937. $this->Form->create('User');
  938. $result = $this->Form->submit('save.png', array('name' => 'test'));
  939. $expected = array(
  940. 'div' => array('class' => 'submit'),
  941. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  942. '/div'
  943. );
  944. $this->assertTags($result, $expected);
  945. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  946. }
  947. /**
  948. * testFormSecurityMultipleInputFields method
  949. *
  950. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  951. *
  952. * @return void
  953. */
  954. public function testFormSecurityMultipleInputFields() {
  955. $key = 'testKey';
  956. $this->Form->request['_Token'] = array('key' => $key);
  957. $this->Form->create('Addresses');
  958. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  959. $this->Form->input('Addresses.0.title');
  960. $this->Form->input('Addresses.0.first_name');
  961. $this->Form->input('Addresses.0.last_name');
  962. $this->Form->input('Addresses.0.address');
  963. $this->Form->input('Addresses.0.city');
  964. $this->Form->input('Addresses.0.phone');
  965. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  966. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  967. $this->Form->input('Addresses.1.title');
  968. $this->Form->input('Addresses.1.first_name');
  969. $this->Form->input('Addresses.1.last_name');
  970. $this->Form->input('Addresses.1.address');
  971. $this->Form->input('Addresses.1.city');
  972. $this->Form->input('Addresses.1.phone');
  973. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  974. $result = $this->Form->secure($this->Form->fields);
  975. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  976. $expected = array(
  977. 'div' => array('style' => 'display:none;'),
  978. array('input' => array(
  979. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  980. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  981. )),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  984. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  985. )),
  986. '/div'
  987. );
  988. $this->assertTags($result, $expected);
  989. }
  990. /**
  991. * Test form security with Model.field.0 style inputs
  992. *
  993. * @return void
  994. */
  995. public function testFormSecurityArrayFields() {
  996. $key = 'testKey';
  997. $this->Form->request->params['_Token']['key'] = $key;
  998. $this->Form->create('Address');
  999. $this->Form->input('Address.primary.1');
  1000. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1001. $this->Form->input('Address.secondary.1.0');
  1002. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1003. }
  1004. /**
  1005. * testFormSecurityMultipleInputDisabledFields method
  1006. *
  1007. * test secure form generation with multiple records and disabled fields.
  1008. *
  1009. * @return void
  1010. */
  1011. public function testFormSecurityMultipleInputDisabledFields() {
  1012. $key = 'testKey';
  1013. $this->Form->request->params['_Token'] = array(
  1014. 'key' => $key,
  1015. 'unlockedFields' => array('first_name', 'address')
  1016. );
  1017. $this->Form->create();
  1018. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1019. $this->Form->input('Addresses.0.title');
  1020. $this->Form->input('Addresses.0.first_name');
  1021. $this->Form->input('Addresses.0.last_name');
  1022. $this->Form->input('Addresses.0.address');
  1023. $this->Form->input('Addresses.0.city');
  1024. $this->Form->input('Addresses.0.phone');
  1025. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1026. $this->Form->input('Addresses.1.title');
  1027. $this->Form->input('Addresses.1.first_name');
  1028. $this->Form->input('Addresses.1.last_name');
  1029. $this->Form->input('Addresses.1.address');
  1030. $this->Form->input('Addresses.1.city');
  1031. $this->Form->input('Addresses.1.phone');
  1032. $result = $this->Form->secure($this->Form->fields);
  1033. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1034. $expected = array(
  1035. 'div' => array('style' => 'display:none;'),
  1036. array('input' => array(
  1037. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1038. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1039. )),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1042. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1043. )),
  1044. '/div'
  1045. );
  1046. $this->assertTags($result, $expected);
  1047. }
  1048. /**
  1049. * testFormSecurityInputDisabledFields method
  1050. *
  1051. * Test single record form with disabled fields.
  1052. *
  1053. * @return void
  1054. */
  1055. public function testFormSecurityInputUnlockedFields() {
  1056. $key = 'testKey';
  1057. $this->Form->request['_Token'] = array(
  1058. 'key' => $key,
  1059. 'unlockedFields' => array('first_name', 'address')
  1060. );
  1061. $this->Form->create();
  1062. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1063. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1064. $this->Form->input('Addresses.title');
  1065. $this->Form->input('Addresses.first_name');
  1066. $this->Form->input('Addresses.last_name');
  1067. $this->Form->input('Addresses.address');
  1068. $this->Form->input('Addresses.city');
  1069. $this->Form->input('Addresses.phone');
  1070. $result = $this->Form->fields;
  1071. $expected = array(
  1072. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1073. 'Addresses.city', 'Addresses.phone'
  1074. );
  1075. $this->assertEquals($expected, $result);
  1076. $result = $this->Form->secure($expected);
  1077. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1078. $expected = array(
  1079. 'div' => array('style' => 'display:none;'),
  1080. array('input' => array(
  1081. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1082. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1083. )),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1086. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1087. )),
  1088. '/div'
  1089. );
  1090. $this->assertTags($result, $expected);
  1091. }
  1092. /**
  1093. * test securing inputs with custom name attributes.
  1094. *
  1095. * @return void
  1096. */
  1097. public function testFormSecureWithCustomNameAttribute() {
  1098. $this->Form->request->params['_Token']['key'] = 'testKey';
  1099. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1100. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1101. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1102. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1103. }
  1104. /**
  1105. * testFormSecuredInput method
  1106. *
  1107. * Test generation of entire secure form, assertions made on input() output.
  1108. *
  1109. * @return void
  1110. */
  1111. public function testFormSecuredInput() {
  1112. $this->Form->request['_Token'] = array('key' => 'testKey');
  1113. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1114. $encoding = strtolower(Configure::read('App.encoding'));
  1115. $expected = array(
  1116. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1117. 'div' => array('style' => 'display:none;'),
  1118. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1119. array('input' => array(
  1120. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1121. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormPublished'),
  1130. 'Published',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1134. 'id' => 'UserFormPublished'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1140. $expected = array(
  1141. 'div' => array('class' => 'input text'),
  1142. 'label' => array('for' => 'UserFormOther'),
  1143. 'Other',
  1144. '/label',
  1145. array('input' => array(
  1146. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1147. 'id' => 'UserFormOther'
  1148. )),
  1149. '/div'
  1150. );
  1151. $this->assertTags($result, $expected);
  1152. $result = $this->Form->hidden('UserForm.stuff');
  1153. $expected = array('input' => array(
  1154. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1155. 'id' => 'UserFormStuff'
  1156. ));
  1157. $this->assertTags($result, $expected);
  1158. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1159. $expected = array('input' => array(
  1160. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1161. 'value' => '0', 'id' => 'UserFormHidden'
  1162. ));
  1163. $this->assertTags($result, $expected);
  1164. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1165. $expected = array(
  1166. 'div' => array('class' => 'input checkbox'),
  1167. array('input' => array(
  1168. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1169. 'value' => '0', 'id' => 'UserFormSomething_'
  1170. )),
  1171. array('input' => array(
  1172. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1173. 'value' => '1', 'id' => 'UserFormSomething'
  1174. )),
  1175. 'label' => array('for' => 'UserFormSomething'),
  1176. 'Something',
  1177. '/label',
  1178. '/div'
  1179. );
  1180. $this->assertTags($result, $expected);
  1181. $result = $this->Form->fields;
  1182. $expected = array(
  1183. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1184. 'UserForm.hidden' => '0', 'UserForm.something'
  1185. );
  1186. $this->assertEquals($expected, $result);
  1187. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1188. $result = $this->Form->secure($this->Form->fields);
  1189. $expected = array(
  1190. 'div' => array('style' => 'display:none;'),
  1191. array('input' => array(
  1192. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1193. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1194. )),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1197. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1198. )),
  1199. '/div'
  1200. );
  1201. $this->assertTags($result, $expected);
  1202. }
  1203. /**
  1204. * Tests that the correct keys are added to the field hash index
  1205. *
  1206. * @return void
  1207. */
  1208. public function testFormSecuredFileInput() {
  1209. $this->Form->request['_Token'] = array('key' => 'testKey');
  1210. $this->assertEquals(array(), $this->Form->fields);
  1211. $result = $this->Form->file('Attachment.file');
  1212. $expected = array(
  1213. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1214. 'Attachment.file.error', 'Attachment.file.size'
  1215. );
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. }
  1218. /**
  1219. * test that multiple selects keys are added to field hash
  1220. *
  1221. * @return void
  1222. */
  1223. public function testFormSecuredMultipleSelect() {
  1224. $this->Form->request['_Token'] = array('key' => 'testKey');
  1225. $this->assertEquals(array(), $this->Form->fields);
  1226. $options = array('1' => 'one', '2' => 'two');
  1227. $this->Form->select('Model.select', $options);
  1228. $expected = array('Model.select');
  1229. $this->assertEquals($expected, $this->Form->fields);
  1230. $this->Form->fields = array();
  1231. $this->Form->select('Model.select', $options, array('multiple' => true));
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * testFormSecuredRadio method
  1236. *
  1237. * @return void
  1238. */
  1239. public function testFormSecuredRadio() {
  1240. $this->Form->request['_Token'] = array('key' => 'testKey');
  1241. $this->assertEquals(array(), $this->Form->fields);
  1242. $options = array('1' => 'option1', '2' => 'option2');
  1243. $this->Form->radio('Test.test', $options);
  1244. $expected = array('Test.test');
  1245. $this->assertEquals($expected, $this->Form->fields);
  1246. }
  1247. /**
  1248. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1249. * hashing.
  1250. *
  1251. * @return void
  1252. */
  1253. public function testFormSecuredAndDisabled() {
  1254. $this->Form->request['_Token'] = array('key' => 'testKey');
  1255. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1256. $this->Form->text('Model.text', array('disabled' => true));
  1257. $this->Form->password('Model.text', array('disabled' => true));
  1258. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1259. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1260. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1261. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1262. $this->Form->month('Model.month', array('disabled' => true));
  1263. $this->Form->day('Model.day', array('disabled' => true));
  1264. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1265. $this->Form->minute('Model.minute', array('disabled' => true));
  1266. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1267. $expected = array(
  1268. 'Model.radio' => ''
  1269. );
  1270. $this->assertEquals($expected, $this->Form->fields);
  1271. }
  1272. /**
  1273. * testDisableSecurityUsingForm method
  1274. *
  1275. * @return void
  1276. */
  1277. public function testDisableSecurityUsingForm() {
  1278. $this->Form->request['_Token'] = array(
  1279. 'key' => 'testKey',
  1280. 'disabledFields' => array()
  1281. );
  1282. $this->Form->create();
  1283. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1284. $this->Form->input('Addresses.title');
  1285. $this->Form->input('Addresses.first_name', array('secure' => false));
  1286. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1287. $this->Form->input('Addresses.zip', array(
  1288. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1289. ));
  1290. $result = $this->Form->fields;
  1291. $expected = array(
  1292. 'Addresses.id' => '123456', 'Addresses.title',
  1293. );
  1294. $this->assertEquals($expected, $result);
  1295. }
  1296. /**
  1297. * test disableField
  1298. *
  1299. * @return void
  1300. */
  1301. public function testUnlockFieldAddsToList() {
  1302. $this->Form->request['_Token'] = array(
  1303. 'key' => 'testKey',
  1304. 'unlockedFields' => array()
  1305. );
  1306. $this->Form->create('Contact');
  1307. $this->Form->unlockField('Contact.name');
  1308. $this->Form->text('Contact.name');
  1309. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1310. $this->assertEquals(array(), $this->Form->fields);
  1311. }
  1312. /**
  1313. * test unlockField removing from fields array.
  1314. *
  1315. * @return void
  1316. */
  1317. public function testUnlockFieldRemovingFromFields() {
  1318. $this->Form->request['_Token'] = array(
  1319. 'key' => 'testKey',
  1320. 'unlockedFields' => array()
  1321. );
  1322. $this->Form->create('Contact');
  1323. $this->Form->hidden('Contact.id', array('value' => 1));
  1324. $this->Form->text('Contact.name');
  1325. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1326. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1327. $this->Form->unlockField('Contact.name');
  1328. $this->Form->unlockField('Contact.id');
  1329. $this->assertEquals(array(), $this->Form->fields);
  1330. }
  1331. /**
  1332. * testTagIsInvalid method
  1333. *
  1334. * @return void
  1335. */
  1336. public function testTagIsInvalid() {
  1337. $Contact = ClassRegistry::getObject('Contact');
  1338. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1339. $this->Form->setEntity('Contact.0.email');
  1340. $result = $this->Form->tagIsInvalid();
  1341. $expected = array('Please provide an email');
  1342. $this->assertEquals($expected, $result);
  1343. $this->Form->setEntity('Contact.1.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = false;
  1346. $this->assertSame($expected, $result);
  1347. $this->Form->setEntity('Contact.0.name');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. }
  1352. /**
  1353. * testPasswordValidation method
  1354. *
  1355. * test validation errors on password input.
  1356. *
  1357. * @return void
  1358. */
  1359. public function testPasswordValidation() {
  1360. $Contact = ClassRegistry::getObject('Contact');
  1361. $Contact->validationErrors['password'] = array('Please provide a password');
  1362. $result = $this->Form->input('Contact.password');
  1363. $expected = array(
  1364. 'div' => array('class' => 'input password error'),
  1365. 'label' => array('for' => 'ContactPassword'),
  1366. 'Password',
  1367. '/label',
  1368. 'input' => array(
  1369. 'type' => 'password', 'name' => 'data[Contact][password]',
  1370. 'id' => 'ContactPassword', 'class' => 'form-error'
  1371. ),
  1372. array('div' => array('class' => 'error-message')),
  1373. 'Please provide a password',
  1374. '/div',
  1375. '/div'
  1376. );
  1377. $this->assertTags($result, $expected);
  1378. }
  1379. /**
  1380. * testEmptyErrorValidation method
  1381. *
  1382. * test validation error div when validation message is an empty string
  1383. *
  1384. * @access public
  1385. * @return void
  1386. */
  1387. public function testEmptyErrorValidation() {
  1388. $this->Form->validationErrors['Contact']['password'] = '';
  1389. $result = $this->Form->input('Contact.password');
  1390. $expected = array(
  1391. 'div' => array('class' => 'input password error'),
  1392. 'label' => array('for' => 'ContactPassword'),
  1393. 'Password',
  1394. '/label',
  1395. 'input' => array(
  1396. 'type' => 'password', 'name' => 'data[Contact][password]',
  1397. 'id' => 'ContactPassword', 'class' => 'form-error'
  1398. ),
  1399. array('div' => array('class' => 'error-message')),
  1400. array(),
  1401. '/div',
  1402. '/div'
  1403. );
  1404. $this->assertTags($result, $expected);
  1405. }
  1406. /**
  1407. * testEmptyInputErrorValidation method
  1408. *
  1409. * test validation error div when validation message is overridden by an empty string when calling input()
  1410. *
  1411. * @access public
  1412. * @return void
  1413. */
  1414. public function testEmptyInputErrorValidation() {
  1415. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1416. $result = $this->Form->input('Contact.password', array('error' => ''));
  1417. $expected = array(
  1418. 'div' => array('class' => 'input password error'),
  1419. 'label' => array('for' => 'ContactPassword'),
  1420. 'Password',
  1421. '/label',
  1422. 'input' => array(
  1423. 'type' => 'password', 'name' => 'data[Contact][password]',
  1424. 'id' => 'ContactPassword', 'class' => 'form-error'
  1425. ),
  1426. array('div' => array('class' => 'error-message')),
  1427. array(),
  1428. '/div',
  1429. '/div'
  1430. );
  1431. $this->assertTags($result, $expected);
  1432. }
  1433. /**
  1434. * testFormValidationAssociated method
  1435. *
  1436. * test display of form errors in conjunction with model::validates.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testFormValidationAssociated() {
  1441. $this->UserForm = ClassRegistry::getObject('UserForm');
  1442. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1443. $data = array(
  1444. 'UserForm' => array('name' => 'user'),
  1445. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1446. );
  1447. $result = $this->UserForm->OpenidUrl->create($data);
  1448. $this->assertFalse(empty($result));
  1449. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1450. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1451. $encoding = strtolower(Configure::read('App.encoding'));
  1452. $expected = array(
  1453. 'form' => array(
  1454. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1455. 'accept-charset' => $encoding
  1456. ),
  1457. 'div' => array('style' => 'display:none;'),
  1458. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1459. '/div'
  1460. );
  1461. $this->assertTags($result, $expected);
  1462. $result = $this->Form->error(
  1463. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1464. );
  1465. $this->assertEquals('Error, not registered', $result);
  1466. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1467. }
  1468. /**
  1469. * testFormValidationAssociatedFirstLevel method
  1470. *
  1471. * test form error display with associated model.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testFormValidationAssociatedFirstLevel() {
  1476. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1477. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1478. $data = array(
  1479. 'ValidateUser' => array('name' => 'mariano'),
  1480. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1481. );
  1482. $result = $this->ValidateUser->create($data);
  1483. $this->assertFalse(empty($result));
  1484. $this->assertFalse($this->ValidateUser->validates());
  1485. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1486. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1487. $encoding = strtolower(Configure::read('App.encoding'));
  1488. $expected = array(
  1489. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1490. 'div' => array('style' => 'display:none;'),
  1491. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1492. '/div'
  1493. );
  1494. $this->assertTags($result, $expected);
  1495. $result = $this->Form->error(
  1496. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid email', $result);
  1499. $result = $this->Form->error(
  1500. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Invalid name', $result);
  1503. $result = $this->Form->error(
  1504. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid city', $result);
  1507. unset($this->ValidateUser->ValidateProfile);
  1508. unset($this->ValidateUser);
  1509. }
  1510. /**
  1511. * testFormValidationAssociatedSecondLevel method
  1512. *
  1513. * test form error display with associated model.
  1514. *
  1515. * @return void
  1516. */
  1517. public function testFormValidationAssociatedSecondLevel() {
  1518. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1519. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1520. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1521. $data = array(
  1522. 'ValidateUser' => array('name' => 'mariano'),
  1523. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1524. 'ValidateItem' => array('name' => 'Item')
  1525. );
  1526. $result = $this->ValidateUser->create($data);
  1527. $this->assertFalse(empty($result));
  1528. $this->assertFalse($this->ValidateUser->validates());
  1529. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1530. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1531. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1532. $encoding = strtolower(Configure::read('App.encoding'));
  1533. $expected = array(
  1534. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1535. 'div' => array('style' => 'display:none;'),
  1536. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1537. '/div'
  1538. );
  1539. $this->assertTags($result, $expected);
  1540. $result = $this->Form->error(
  1541. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1542. );
  1543. $this->assertEquals('Invalid email', $result);
  1544. $result = $this->Form->error(
  1545. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid name', $result);
  1548. $result = $this->Form->error(
  1549. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1550. );
  1551. $result = $this->Form->error(
  1552. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1553. );
  1554. $this->assertEquals('Invalid description', $result);
  1555. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1556. unset($this->ValidateUser->ValidateProfile);
  1557. unset($this->ValidateUser);
  1558. }
  1559. /**
  1560. * testFormValidationMultiRecord method
  1561. *
  1562. * test form error display with multiple records.
  1563. *
  1564. * @return void
  1565. */
  1566. public function testFormValidationMultiRecord() {
  1567. $Contact = ClassRegistry::getObject('Contact');
  1568. $Contact->validationErrors[2] = array(
  1569. 'name' => array('This field cannot be left blank')
  1570. );
  1571. $result = $this->Form->input('Contact.2.name');
  1572. $expected = array(
  1573. 'div' => array('class' => 'input text error'),
  1574. 'label' => array('for' => 'Contact2Name'),
  1575. 'Name',
  1576. '/label',
  1577. 'input' => array(
  1578. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1579. 'class' => 'form-error', 'maxlength' => 255
  1580. ),
  1581. array('div' => array('class' => 'error-message')),
  1582. 'This field cannot be left blank',
  1583. '/div',
  1584. '/div'
  1585. );
  1586. $this->assertTags($result, $expected);
  1587. }
  1588. /**
  1589. * testMultipleInputValidation method
  1590. *
  1591. * test multiple record form validation error display.
  1592. *
  1593. * @return void
  1594. */
  1595. public function testMultipleInputValidation() {
  1596. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1597. $Address->validationErrors[0] = array(
  1598. 'title' => array('This field cannot be empty'),
  1599. 'first_name' => array('This field cannot be empty')
  1600. );
  1601. $Address->validationErrors[1] = array(
  1602. 'last_name' => array('You must have a last name')
  1603. );
  1604. $this->Form->create();
  1605. $result = $this->Form->input('Address.0.title');
  1606. $expected = array(
  1607. 'div' => array('class'),
  1608. 'label' => array('for'),
  1609. 'preg:/[^<]+/',
  1610. '/label',
  1611. 'input' => array(
  1612. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1613. ),
  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.first_name');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'preg:/[^<]+/',
  1625. '/label',
  1626. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1627. array('div' => array('class' => 'error-message')),
  1628. 'This field cannot be empty',
  1629. '/div',
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Address.0.last_name');
  1634. $expected = array(
  1635. 'div' => array('class'),
  1636. 'label' => array('for'),
  1637. 'preg:/[^<]+/',
  1638. '/label',
  1639. 'input' => array('type' => 'text', 'name', 'id'),
  1640. '/div'
  1641. );
  1642. $this->assertTags($result, $expected);
  1643. $result = $this->Form->input('Address.1.last_name');
  1644. $expected = array(
  1645. 'div' => array('class'),
  1646. 'label' => array('for'),
  1647. 'preg:/[^<]+/',
  1648. '/label',
  1649. 'input' => array(
  1650. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1651. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1652. ),
  1653. array('div' => array('class' => 'error-message')),
  1654. 'You must have a last name',
  1655. '/div',
  1656. '/div'
  1657. );
  1658. $this->assertTags($result, $expected);
  1659. }
  1660. /**
  1661. * testInput method
  1662. *
  1663. * Test various incarnations of input().
  1664. *
  1665. * @return void
  1666. */
  1667. public function testInput() {
  1668. $result = $this->Form->input('ValidateUser.balance');
  1669. $expected = array(
  1670. 'div' => array('class'),
  1671. 'label' => array('for'),
  1672. 'Balance',
  1673. '/label',
  1674. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1675. '/div',
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1679. $expected = array(
  1680. 'div' => array('class' => 'input text'),
  1681. 'label' => array('for' => 'custom'),
  1682. 'Email',
  1683. '/label',
  1684. array('input' => array(
  1685. 'type' => 'text', 'name' => 'data[Contact][email]',
  1686. 'id' => 'custom', 'maxlength' => 255
  1687. )),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1692. $expected = array(
  1693. '<div',
  1694. 'label' => array('for' => 'ContactEmail'),
  1695. 'Email',
  1696. '/label',
  1697. array('input' => array(
  1698. 'type' => 'text', 'name' => 'data[Contact][email]',
  1699. 'id' => 'ContactEmail', 'maxlength' => 255
  1700. )),
  1701. '/div'
  1702. );
  1703. $this->assertTags($result, $expected);
  1704. $result = $this->Form->hidden('Contact.idontexist');
  1705. $expected = array('input' => array(
  1706. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1707. 'id' => 'ContactIdontexist'
  1708. ));
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1711. $expected = array(
  1712. 'div' => array('class' => 'input text'),
  1713. 'label' => array('for' => 'ContactEmail'),
  1714. 'Email',
  1715. '/label',
  1716. array('input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][email]',
  1718. 'id' => 'ContactEmail'
  1719. )),
  1720. '/div'
  1721. );
  1722. $this->assertTags($result, $expected);
  1723. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1724. $expected = array(
  1725. 'div' => array('class' => 'input text'),
  1726. 'label' => array('for' => 'Contact5Email'),
  1727. 'Email',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1731. 'id' => 'Contact5Email'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $result = $this->Form->input('Contact.password');
  1737. $expected = array(
  1738. 'div' => array('class' => 'input password'),
  1739. 'label' => array('for' => 'ContactPassword'),
  1740. 'Password',
  1741. '/label',
  1742. array('input' => array(
  1743. 'type' => 'password', 'name' => 'data[Contact][password]',
  1744. 'id' => 'ContactPassword'
  1745. )),
  1746. '/div'
  1747. );
  1748. $this->assertTags($result, $expected);
  1749. $result = $this->Form->input('Contact.email', array(
  1750. 'type' => 'file', 'class' => 'textbox'
  1751. ));
  1752. $expected = array(
  1753. 'div' => array('class' => 'input file'),
  1754. 'label' => array('for' => 'ContactEmail'),
  1755. 'Email',
  1756. '/label',
  1757. array('input' => array(
  1758. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1759. 'id' => 'ContactEmail'
  1760. )),
  1761. '/div'
  1762. );
  1763. $this->assertTags($result, $expected);
  1764. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1765. $result = $this->Form->input('Contact.phone');
  1766. $expected = array(
  1767. 'div' => array('class' => 'input text'),
  1768. 'label' => array('for' => 'ContactPhone'),
  1769. 'Phone',
  1770. '/label',
  1771. array('input' => array(
  1772. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1773. 'value' => 'Hello &amp; World &gt; weird chars',
  1774. 'id' => 'ContactPhone', 'maxlength' => 255
  1775. )),
  1776. '/div'
  1777. );
  1778. $this->assertTags($result, $expected);
  1779. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1780. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1781. $expected = array(
  1782. 'div' => array('class' => 'input text'),
  1783. 'label' => array('for' => 'myId'),
  1784. 'Field',
  1785. '/label',
  1786. 'input' => array(
  1787. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1788. 'value' => 'My value', 'id' => 'myId'
  1789. ),
  1790. '/div'
  1791. );
  1792. $this->assertTags($result, $expected);
  1793. unset($this->Form->request->data);
  1794. $Contact = ClassRegistry::getObject('Contact');
  1795. $Contact->validationErrors['field'] = array('Badness!');
  1796. $result = $this->Form->input('Contact.field');
  1797. $expected = array(
  1798. 'div' => array('class' => 'input text error'),
  1799. 'label' => array('for' => 'ContactField'),
  1800. 'Field',
  1801. '/label',
  1802. 'input' => array(
  1803. 'type' => 'text', 'name' => 'data[Contact][field]',
  1804. 'id' => 'ContactField', 'class' => 'form-error'
  1805. ),
  1806. array('div' => array('class' => 'error-message')),
  1807. 'Badness!',
  1808. '/div',
  1809. '/div'
  1810. );
  1811. $this->assertTags($result, $expected);
  1812. $result = $this->Form->input('Contact.field', array(
  1813. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1814. ));
  1815. $expected = array(
  1816. 'label' => array('for' => 'ContactField'),
  1817. 'Field',
  1818. '/label',
  1819. 'input' => array(
  1820. 'type' => 'text', 'name' => 'data[Contact][field]',
  1821. 'id' => 'ContactField', 'class' => 'form-error'
  1822. ),
  1823. array('span' => array('class' => 'error-message')),
  1824. 'Badness!',
  1825. '/span'
  1826. );
  1827. $this->assertTags($result, $expected);
  1828. $result = $this->Form->input('Contact.field', array(
  1829. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1830. ));
  1831. $expected = array(
  1832. 'div' => array('class' => 'input text error'),
  1833. 'label' => array('for' => 'ContactField'),
  1834. 'Field',
  1835. '/label',
  1836. 'input' => array(
  1837. 'type' => 'text', 'name' => 'data[Contact][field]',
  1838. 'id' => 'ContactField', 'class' => 'form-error'
  1839. ),
  1840. array('div' => array('class' => 'error')),
  1841. 'Badness!',
  1842. '/div'
  1843. );
  1844. $this->assertTags($result, $expected);
  1845. $result = $this->Form->input('Contact.field', array(
  1846. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1847. ));
  1848. $expected = array(
  1849. 'span' => array('class' => 'input text error'),
  1850. 'label' => array('for' => 'ContactField'),
  1851. 'Field',
  1852. '/label',
  1853. 'input' => array(
  1854. 'type' => 'text', 'name' => 'data[Contact][field]',
  1855. 'id' => 'ContactField', 'class' => 'form-error'
  1856. ),
  1857. 'Badness!',
  1858. '/span'
  1859. );
  1860. $this->assertTags($result, $expected);
  1861. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text error'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array(
  1868. 'type' => 'text', 'name' => 'data[Contact][field]',
  1869. 'id' => 'ContactField', 'class' => 'form-error'
  1870. ),
  1871. 'A message to you, Rudy',
  1872. array('div' => array('class' => 'error-message')),
  1873. 'Badness!',
  1874. '/div',
  1875. '/div'
  1876. );
  1877. $this->assertTags($result, $expected);
  1878. $this->Form->setEntity(null);
  1879. $this->Form->setEntity('Contact.field');
  1880. $result = $this->Form->input('Contact.field', array(
  1881. 'after' => 'A message to you, Rudy', 'error' => false
  1882. ));
  1883. $expected = array(
  1884. 'div' => array('class' => 'input text'),
  1885. 'label' => array('for' => 'ContactField'),
  1886. 'Field',
  1887. '/label',
  1888. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1889. 'A message to you, Rudy',
  1890. '/div'
  1891. );
  1892. $this->assertTags($result, $expected);
  1893. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input text'),
  1896. 'label' => array('for' => 'ObjectField'),
  1897. 'Field',
  1898. '/label',
  1899. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1900. 'A message to you, Rudy',
  1901. '/div'
  1902. );
  1903. $this->assertTags($result, $expected);
  1904. $Contact->validationErrors['field'] = array('minLength');
  1905. $result = $this->Form->input('Contact.field', array(
  1906. 'error' => array(
  1907. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1908. 'maxLength' => 'login too large'
  1909. )
  1910. ));
  1911. $expected = array(
  1912. 'div' => array('class' => 'input text error'),
  1913. 'label' => array('for' => 'ContactField'),
  1914. 'Field',
  1915. '/label',
  1916. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1917. array('div' => array('class' => 'error-message')),
  1918. 'Le login doit contenir au moins 2 caractères',
  1919. '/div',
  1920. '/div'
  1921. );
  1922. $this->assertTags($result, $expected);
  1923. $Contact->validationErrors['field'] = array('maxLength');
  1924. $result = $this->Form->input('Contact.field', array(
  1925. 'error' => array(
  1926. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1927. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1928. 'maxLength' => 'login too large',
  1929. )
  1930. ));
  1931. $expected = array(
  1932. 'div' => array('class' => 'input text error'),
  1933. 'label' => array('for' => 'ContactField'),
  1934. 'Field',
  1935. '/label',
  1936. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1937. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1938. 'login too large',
  1939. '/span',
  1940. '/div'
  1941. );
  1942. $this->assertTags($result, $expected);
  1943. }
  1944. /**
  1945. * test input() with checkbox creation
  1946. *
  1947. * @return void
  1948. */
  1949. public function testInputCheckbox() {
  1950. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1951. $expected = array(
  1952. 'div' => array('class' => 'input checkbox'),
  1953. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1954. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1955. '/div'
  1956. );
  1957. $this->assertTags($result, $expected);
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. }
  1975. /**
  1976. * test form->input() with time types.
  1977. *
  1978. */
  1979. public function testInputTime() {
  1980. extract($this->dateRegex);
  1981. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1982. $result = explode(':', $result);
  1983. $this->assertRegExp('/option value="23"/', $result[0]);
  1984. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1985. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1986. $result = explode(':', $result);
  1987. $this->assertRegExp('/option value="23"/', $result[0]);
  1988. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1989. $result = $this->Form->input('Model.field', array(
  1990. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1991. ));
  1992. $result = explode(':', $result);
  1993. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1994. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1995. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1996. $result = $this->Form->input('Model.field', array(
  1997. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1998. ));
  1999. $result = explode(':', $result);
  2000. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2001. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2002. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2003. $result = $this->Form->input('prueba', array(
  2004. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2005. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2006. ));
  2007. $result = explode(':', $result);
  2008. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2009. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2010. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2011. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2012. $result = $this->Form->input('Random.start_time', array(
  2013. 'type' => 'time',
  2014. 'selected' => '18:15'
  2015. ));
  2016. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2017. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2018. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2019. }
  2020. /**
  2021. * Test interval + selected near the hour roll over.
  2022. *
  2023. * @return void
  2024. */
  2025. public function testTimeSelectedWithInterval() {
  2026. $result = $this->Form->input('Model.start_time', array(
  2027. 'type' => 'time',
  2028. 'interval' => 15,
  2029. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2030. ));
  2031. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2032. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2033. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2034. $result = $this->Form->input('Model.start_time', array(
  2035. 'type' => 'time',
  2036. 'interval' => 15,
  2037. 'selected' => '2012-10-23 15:57:00'
  2038. ));
  2039. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2040. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2041. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2042. $result = $this->Form->input('Model.start_time', array(
  2043. 'timeFormat' => 24,
  2044. 'type' => 'time',
  2045. 'interval' => 15,
  2046. 'selected' => '15:57'
  2047. ));
  2048. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2049. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2050. $result = $this->Form->input('Model.start_time', array(
  2051. 'timeFormat' => 24,
  2052. 'type' => 'time',
  2053. 'interval' => 15,
  2054. 'selected' => '23:57'
  2055. ));
  2056. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2057. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2058. $result = $this->Form->input('Model.created', array(
  2059. 'timeFormat' => 24,
  2060. 'type' => 'datetime',
  2061. 'interval' => 15,
  2062. 'selected' => '2012-09-30 23:56'
  2063. ));
  2064. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2065. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2066. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2067. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. }
  2070. /**
  2071. * test form->input() with datetime, date and time types
  2072. *
  2073. * @return void
  2074. */
  2075. public function testInputDatetime() {
  2076. extract($this->dateRegex);
  2077. $result = $this->Form->input('prueba', array(
  2078. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2079. 'maxYear' => date('Y') + 1, 'interval' => 15
  2080. ));
  2081. $result = explode(':', $result);
  2082. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2083. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2084. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2085. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2086. //related to ticket #5013
  2087. $result = $this->Form->input('Contact.date', array(
  2088. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2089. ));
  2090. $this->assertRegExp('/class="customClass"/', $result);
  2091. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2092. $result = $this->Form->input('Contact.date', array(
  2093. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2094. ));
  2095. $this->assertRegExp('/id="customIdDay"/', $result);
  2096. $this->assertRegExp('/id="customIdMonth"/', $result);
  2097. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2098. $result = $this->Form->input('Model.field', array(
  2099. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2100. ));
  2101. $this->assertRegExp('/id="customIDDay"/', $result);
  2102. $this->assertRegExp('/id="customIDHour"/', $result);
  2103. $result = explode('</select><select', $result);
  2104. $result = explode(':', $result[1]);
  2105. $this->assertRegExp('/option value="23"/', $result[0]);
  2106. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2107. $result = $this->Form->input('Model.field', array(
  2108. 'type' => 'datetime', 'timeFormat' => 12
  2109. ));
  2110. $result = explode('</select><select', $result);
  2111. $result = explode(':', $result[1]);
  2112. $this->assertRegExp('/option value="12"/', $result[0]);
  2113. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2114. $this->Form->request->data = array('Contact' => array('created' => null));
  2115. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2116. $expected = array(
  2117. 'div' => array('class' => 'input date'),
  2118. 'label' => array('for' => 'ContactCreatedMonth'),
  2119. 'Created',
  2120. '/label',
  2121. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2122. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2123. $monthsRegex,
  2124. '/select', '-',
  2125. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2126. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2127. $daysRegex,
  2128. '/select', '-',
  2129. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2130. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2131. $yearsRegex,
  2132. '/select',
  2133. '/div'
  2134. );
  2135. $this->assertTags($result, $expected);
  2136. $this->Form->request->data = array('Contact' => array('created' => null));
  2137. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2138. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2139. $this->Form->request->data = array('Contact' => array('created' => null));
  2140. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2141. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2142. $result = $this->Form->input('Contact.created', array(
  2143. 'type' => 'date',
  2144. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2145. 'timeFormat' => 'NONE'
  2146. ));
  2147. $this->assertRegExp('/for\="created-month"/', $result);
  2148. }
  2149. /**
  2150. * Test generating checkboxes in a loop.
  2151. *
  2152. * @return void
  2153. */
  2154. public function testInputCheckboxesInLoop() {
  2155. for ($i = 1; $i < 5; $i++) {
  2156. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2157. $expected = array(
  2158. 'div' => array('class' => 'input checkbox'),
  2159. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2160. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2161. 'label' => array('for' => "Contact{$i}Email"),
  2162. 'Email',
  2163. '/label',
  2164. '/div'
  2165. );
  2166. $this->assertTags($result, $expected);
  2167. }
  2168. }
  2169. /**
  2170. * Test generating checkboxes with disabled elements.
  2171. *
  2172. * @return void
  2173. */
  2174. public function testInputCheckboxWithDisabledElements() {
  2175. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2176. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2177. $expected = array(
  2178. array('div' => array('class' => 'input select')),
  2179. array('label' => array('for' => "ContactMultiple")),
  2180. 'Multiple',
  2181. '/label',
  2182. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2183. array('div' => array('class' => 'checkbox')),
  2184. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2185. array('label' => array('for' => "ContactMultiple1")),
  2186. 'One',
  2187. '/label',
  2188. '/div',
  2189. array('div' => array('class' => 'checkbox')),
  2190. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2191. array('label' => array('for' => "ContactMultiple2")),
  2192. 'Two',
  2193. '/label',
  2194. '/div',
  2195. array('div' => array('class' => 'checkbox')),
  2196. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2197. array('label' => array('for' => "ContactMultiple3")),
  2198. 'Three',
  2199. '/label',
  2200. '/div',
  2201. '/div'
  2202. );
  2203. $this->assertTags($result, $expected);
  2204. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2205. $this->assertTags($result, $expected);
  2206. $disabled = array('2', 3);
  2207. $expected = array(
  2208. array('div' => array('class' => 'input select')),
  2209. array('label' => array('for' => "ContactMultiple")),
  2210. 'Multiple',
  2211. '/label',
  2212. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2213. array('div' => array('class' => 'checkbox')),
  2214. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2215. array('label' => array('for' => "ContactMultiple1")),
  2216. 'One',
  2217. '/label',
  2218. '/div',
  2219. array('div' => array('class' => 'checkbox')),
  2220. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2221. array('label' => array('for' => "ContactMultiple2")),
  2222. 'Two',
  2223. '/label',
  2224. '/div',
  2225. array('div' => array('class' => 'checkbox')),
  2226. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2227. array('label' => array('for' => "ContactMultiple3")),
  2228. 'Three',
  2229. '/label',
  2230. '/div',
  2231. '/div'
  2232. );
  2233. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2234. $this->assertTags($result, $expected);
  2235. }
  2236. /**
  2237. * test input name with leading integer, ensure attributes are generated correctly.
  2238. *
  2239. * @return void
  2240. */
  2241. public function testInputWithLeadingInteger() {
  2242. $result = $this->Form->text('0.Node.title');
  2243. $expected = array(
  2244. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2245. );
  2246. $this->assertTags($result, $expected);
  2247. }
  2248. /**
  2249. * test form->input() with select type inputs.
  2250. *
  2251. * @return void
  2252. */
  2253. public function testInputSelectType() {
  2254. $result = $this->Form->input('email', array(
  2255. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2256. );
  2257. $expected = array(
  2258. 'div' => array('class' => 'input select'),
  2259. 'label' => array('for' => 'email'),
  2260. 'Email',
  2261. '/label',
  2262. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2263. array('option' => array('value' => '')),
  2264. '/option',
  2265. array('option' => array('value' => 'è')),
  2266. 'Firést',
  2267. '/option',
  2268. array('option' => array('value' => 'é')),
  2269. 'Secoènd',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $result = $this->Form->input('email', array(
  2276. 'options' => array('First', 'Second'), 'empty' => true)
  2277. );
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'email'),
  2281. 'Email',
  2282. '/label',
  2283. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2284. array('option' => array('value' => '')),
  2285. '/option',
  2286. array('option' => array('value' => '0')),
  2287. 'First',
  2288. '/option',
  2289. array('option' => array('value' => '1')),
  2290. 'Second',
  2291. '/option',
  2292. '/select',
  2293. '/div'
  2294. );
  2295. $this->assertTags($result, $expected);
  2296. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2297. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2298. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2299. $expected = array(
  2300. 'div' => array('class' => 'input select'),
  2301. 'label' => array('for' => 'ModelUserId'),
  2302. 'User',
  2303. '/label',
  2304. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2305. array('option' => array('value' => '')),
  2306. '/option',
  2307. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2308. 'good',
  2309. '/option',
  2310. array('option' => array('value' => 'other')),
  2311. 'bad',
  2312. '/option',
  2313. '/select',
  2314. '/div'
  2315. );
  2316. $this->assertTags($result, $expected);
  2317. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2318. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2319. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2320. $expected = array(
  2321. 'div' => array('class' => 'input select'),
  2322. 'label' => array('for' => 'ThingUserId'),
  2323. 'User',
  2324. '/label',
  2325. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2326. array('option' => array('value' => '')),
  2327. 'Some Empty',
  2328. '/option',
  2329. array('option' => array('value' => 'value')),
  2330. 'good',
  2331. '/option',
  2332. array('option' => array('value' => 'other')),
  2333. 'bad',
  2334. '/option',
  2335. '/select',
  2336. '/div'
  2337. );
  2338. $this->assertTags($result, $expected);
  2339. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2340. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2341. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2342. $expected = array(
  2343. 'div' => array('class' => 'input select'),
  2344. 'label' => array('for' => 'ThingUserId'),
  2345. 'User',
  2346. '/label',
  2347. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2348. array('option' => array('value' => '')),
  2349. 'Some Empty',
  2350. '/option',
  2351. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2352. 'good',
  2353. '/option',
  2354. array('option' => array('value' => 'other')),
  2355. 'bad',
  2356. '/option',
  2357. '/select',
  2358. '/div'
  2359. );
  2360. $this->assertTags($result, $expected);
  2361. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2362. $this->Form->request->data = array('User' => array('User' => array('value')));
  2363. $result = $this->Form->input('User.User', array('empty' => true));
  2364. $expected = array(
  2365. 'div' => array('class' => 'input select'),
  2366. 'label' => array('for' => 'UserUser'),
  2367. 'User',
  2368. '/label',
  2369. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2370. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2371. array('option' => array('value' => '')),
  2372. '/option',
  2373. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2374. 'good',
  2375. '/option',
  2376. array('option' => array('value' => 'other')),
  2377. 'bad',
  2378. '/option',
  2379. '/select',
  2380. '/div'
  2381. );
  2382. $this->assertTags($result, $expected);
  2383. $this->Form->data = array();
  2384. $result = $this->Form->input('Publisher.id', array(
  2385. 'label' => 'Publisher',
  2386. 'type' => 'select',
  2387. 'multiple' => 'checkbox',
  2388. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2389. ));
  2390. $expected = array(
  2391. array('div' => array('class' => 'input select')),
  2392. array('label' => array('for' => 'PublisherId')),
  2393. 'Publisher',
  2394. '/label',
  2395. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2396. array('div' => array('class' => 'checkbox')),
  2397. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2398. array('label' => array('for' => 'PublisherIdValue1')),
  2399. 'Label 1',
  2400. '/label',
  2401. '/div',
  2402. array('div' => array('class' => 'checkbox')),
  2403. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2404. array('label' => array('for' => 'PublisherIdValue2')),
  2405. 'Label 2',
  2406. '/label',
  2407. '/div',
  2408. '/div'
  2409. );
  2410. $this->assertTags($result, $expected);
  2411. }
  2412. /**
  2413. * test that input() and a non standard primary key makes a hidden input by default.
  2414. *
  2415. * @return void
  2416. */
  2417. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2418. $this->Form->create('User');
  2419. $this->Form->fieldset = array(
  2420. 'User' => array(
  2421. 'fields' => array(
  2422. 'model_id' => array('type' => 'integer')
  2423. ),
  2424. 'validates' => array(),
  2425. 'key' => 'model_id'
  2426. )
  2427. );
  2428. $result = $this->Form->input('model_id');
  2429. $expected = array(
  2430. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2431. );
  2432. $this->assertTags($result, $expected);
  2433. }
  2434. /**
  2435. * test that overriding the magic select type widget is possible
  2436. *
  2437. * @return void
  2438. */
  2439. public function testInputOverridingMagicSelectType() {
  2440. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2441. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2442. $expected = array(
  2443. 'div' => array('class' => 'input text'),
  2444. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2445. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2446. '/div'
  2447. );
  2448. $this->assertTags($result, $expected);
  2449. //Check that magic types still work for plural/singular vars
  2450. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2451. $result = $this->Form->input('Model.type');
  2452. $expected = array(
  2453. 'div' => array('class' => 'input select'),
  2454. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2455. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2456. array('option' => array('value' => 'value')), 'good', '/option',
  2457. array('option' => array('value' => 'other')), 'bad', '/option',
  2458. '/select',
  2459. '/div'
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. }
  2463. /**
  2464. * Test that magic input() selects are created for type=number
  2465. *
  2466. * @return void
  2467. */
  2468. public function testInputMagicSelectForTypeNumber() {
  2469. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2470. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2471. $result = $this->Form->input('ValidateUser.balance');
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ValidateUserBalance'),
  2475. 'Balance',
  2476. '/label',
  2477. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2478. array('option' => array('value' => '0')),
  2479. 'nothing',
  2480. '/option',
  2481. array('option' => array('value' => '1', 'selected' => 'selected')),
  2482. 'some',
  2483. '/option',
  2484. array('option' => array('value' => '100')),
  2485. 'a lot',
  2486. '/option',
  2487. '/select',
  2488. '/div'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. }
  2492. /**
  2493. * Test that magic input() selects can easily be converted into radio types without error.
  2494. *
  2495. * @return void
  2496. */
  2497. public function testInputMagicSelectChangeToRadio() {
  2498. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2499. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2500. $this->assertRegExp('/input type="radio"/', $result);
  2501. }
  2502. /**
  2503. * fields with the same name as the model should work.
  2504. *
  2505. * @return void
  2506. */
  2507. public function testInputWithMatchingFieldAndModelName() {
  2508. $this->Form->create('User');
  2509. $this->Form->fieldset = array(
  2510. 'User' => array(
  2511. 'fields' => array(
  2512. 'User' => array('type' => 'text')
  2513. ),
  2514. 'validates' => array(),
  2515. 'key' => 'id'
  2516. )
  2517. );
  2518. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2519. $result = $this->Form->input('User', array('type' => 'text'));
  2520. $expected = array(
  2521. 'div' => array('class' => 'input text'),
  2522. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2523. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2524. '/div'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. }
  2528. /**
  2529. * testFormInputs method
  2530. *
  2531. * test correct results from form::inputs().
  2532. *
  2533. * @return void
  2534. */
  2535. public function testFormInputs() {
  2536. $this->Form->create('Contact');
  2537. $result = $this->Form->inputs('The Legend');
  2538. $expected = array(
  2539. '<fieldset',
  2540. '<legend',
  2541. 'The Legend',
  2542. '/legend',
  2543. '*/fieldset',
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2547. $expected = array(
  2548. 'fieldset' => array('class' => 'classy-stuff'),
  2549. '<legend',
  2550. 'Field of Dreams',
  2551. '/legend',
  2552. '*/fieldset'
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. $this->Form->create('Contact');
  2556. $this->Form->request['prefix'] = 'admin';
  2557. $this->Form->request['action'] = 'admin_edit';
  2558. $result = $this->Form->inputs();
  2559. $expected = array(
  2560. '<fieldset',
  2561. '<legend',
  2562. 'Edit Contact',
  2563. '/legend',
  2564. '*/fieldset',
  2565. );
  2566. $this->assertTags($result, $expected);
  2567. $this->Form->create('Contact');
  2568. $result = $this->Form->inputs(false);
  2569. $expected = array(
  2570. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2571. array('div' => array('class' => 'input text')),
  2572. '*/div',
  2573. array('div' => array('class' => 'input text')),
  2574. '*/div',
  2575. array('div' => array('class' => 'input text')),
  2576. '*/div',
  2577. array('div' => array('class' => 'input password')),
  2578. '*/div',
  2579. array('div' => array('class' => 'input date')),
  2580. '*/div',
  2581. array('div' => array('class' => 'input date')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input datetime')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input number')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input select')),
  2588. '*/div',
  2589. );
  2590. $this->assertTags($result, $expected);
  2591. $this->Form->create('Contact');
  2592. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2593. $expected = array(
  2594. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2595. array('div' => array('class' => 'input text')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input text')),
  2598. '*/div',
  2599. array('div' => array('class' => 'input text')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input password')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input date')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input date')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input datetime')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input number')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input select')),
  2612. '*/div',
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->create('Contact');
  2616. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2617. $expected = array(
  2618. 'fieldset' => array(),
  2619. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2620. array('div' => array('class' => 'input text')),
  2621. '*/div',
  2622. array('div' => array('class' => 'input text')),
  2623. '*/div',
  2624. array('div' => array('class' => 'input text')),
  2625. '*/div',
  2626. array('div' => array('class' => 'input password')),
  2627. '*/div',
  2628. array('div' => array('class' => 'input date')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input date')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input datetime')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input number')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input select')),
  2637. '*/div',
  2638. '/fieldset'
  2639. );
  2640. $this->assertTags($result, $expected);
  2641. $this->Form->create('Contact');
  2642. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2643. $expected = array(
  2644. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2645. array('div' => array('class' => 'input text')),
  2646. '*/div',
  2647. array('div' => array('class' => 'input text')),
  2648. '*/div',
  2649. array('div' => array('class' => 'input text')),
  2650. '*/div',
  2651. array('div' => array('class' => 'input password')),
  2652. '*/div',
  2653. array('div' => array('class' => 'input date')),
  2654. '*/div',
  2655. array('div' => array('class' => 'input date')),
  2656. '*/div',
  2657. array('div' => array('class' => 'input datetime')),
  2658. '*/div',
  2659. array('div' => array('class' => 'input number')),
  2660. '*/div',
  2661. array('div' => array('class' => 'input select')),
  2662. '*/div',
  2663. );
  2664. $this->assertTags($result, $expected);
  2665. $this->Form->create('Contact');
  2666. $result = $this->Form->inputs('Hello');
  2667. $expected = array(
  2668. 'fieldset' => array(),
  2669. 'legend' => array(),
  2670. 'Hello',
  2671. '/legend',
  2672. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2673. array('div' => array('class' => 'input text')),
  2674. '*/div',
  2675. array('div' => array('class' => 'input text')),
  2676. '*/div',
  2677. array('div' => array('class' => 'input text')),
  2678. '*/div',
  2679. array('div' => array('class' => 'input password')),
  2680. '*/div',
  2681. array('div' => array('class' => 'input date')),
  2682. '*/div',
  2683. array('div' => array('class' => 'input date')),
  2684. '*/div',
  2685. array('div' => array('class' => 'input datetime')),
  2686. '*/div',
  2687. array('div' => array('class' => 'input number')),
  2688. '*/div',
  2689. array('div' => array('class' => 'input select')),
  2690. '*/div',
  2691. '/fieldset'
  2692. );
  2693. $this->assertTags($result, $expected);
  2694. $this->Form->create('Contact');
  2695. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2696. $expected = array(
  2697. 'fieldset' => array(),
  2698. 'legend' => array(),
  2699. 'Hello',
  2700. '/legend',
  2701. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2702. array('div' => array('class' => 'input text')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input text')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input text')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input password')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input date')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input date')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input datetime')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input number')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input select')),
  2719. '*/div',
  2720. '/fieldset'
  2721. );
  2722. $this->assertTags($result, $expected);
  2723. }
  2724. /**
  2725. * testSelectAsCheckbox method
  2726. *
  2727. * test multi-select widget with checkbox formatting.
  2728. *
  2729. * @return void
  2730. */
  2731. public function testSelectAsCheckbox() {
  2732. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2733. $expected = array(
  2734. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2735. array('div' => array('class' => 'checkbox')),
  2736. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2737. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2738. 'first',
  2739. '/label',
  2740. '/div',
  2741. array('div' => array('class' => 'checkbox')),
  2742. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2743. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2744. 'second',
  2745. '/label',
  2746. '/div',
  2747. array('div' => array('class' => 'checkbox')),
  2748. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2749. array('label' => array('for' => 'ModelMultiField2')),
  2750. 'third',
  2751. '/label',
  2752. '/div',
  2753. );
  2754. $this->assertTags($result, $expected);
  2755. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2756. $expected = array(
  2757. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2758. array('div' => array('class' => 'checkbox')),
  2759. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2760. array('label' => array('for' => 'ModelMultiField12')),
  2761. 'half',
  2762. '/label',
  2763. '/div',
  2764. );
  2765. $this->assertTags($result, $expected);
  2766. }
  2767. /**
  2768. * testLabel method
  2769. *
  2770. * test label generation.
  2771. *
  2772. * @return void
  2773. */
  2774. public function testLabel() {
  2775. $this->Form->text('Person.name');
  2776. $result = $this->Form->label();
  2777. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2778. $this->Form->text('Person.name');
  2779. $result = $this->Form->label();
  2780. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2781. $result = $this->Form->label('Person.first_name');
  2782. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2783. $result = $this->Form->label('Person.first_name', 'Your first name');
  2784. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2785. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2786. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2787. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2788. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2789. $result = $this->Form->label('Person.first_name', '');
  2790. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2791. $result = $this->Form->label('Person.2.name', '');
  2792. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2793. }
  2794. /**
  2795. * testTextbox method
  2796. *
  2797. * test textbox element generation
  2798. *
  2799. * @return void
  2800. */
  2801. public function testTextbox() {
  2802. $result = $this->Form->text('Model.field');
  2803. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2804. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2805. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2806. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2807. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2808. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2809. $result = $this->Form->text('Model.text');
  2810. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2811. $Contact = ClassRegistry::getObject('Contact');
  2812. $Contact->validationErrors['text'] = array(true);
  2813. $this->Form->request->data['Contact']['text'] = 'test';
  2814. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2815. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2816. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2817. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2818. $expected = array(
  2819. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2820. );
  2821. $this->assertTags($result, $expected);
  2822. }
  2823. /**
  2824. * testDefaultValue method
  2825. *
  2826. * Test default value setting
  2827. *
  2828. * @return void
  2829. */
  2830. public function testDefaultValue() {
  2831. $this->Form->request->data['Model']['field'] = 'test';
  2832. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2833. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2834. unset($this->Form->request->data['Model']['field']);
  2835. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2836. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2837. }
  2838. /**
  2839. * testCheckboxDefaultValue method
  2840. *
  2841. * Test default value setting on checkbox() method
  2842. *
  2843. * @return void
  2844. */
  2845. public function testCheckboxDefaultValue() {
  2846. $this->Form->request->data['Model']['field'] = false;
  2847. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2848. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2849. unset($this->Form->request->data['Model']['field']);
  2850. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2851. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2852. $this->Form->request->data['Model']['field'] = true;
  2853. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2854. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2855. unset($this->Form->request->data['Model']['field']);
  2856. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2857. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2858. }
  2859. /**
  2860. * testError method
  2861. *
  2862. * Test field error generation
  2863. *
  2864. * @return void
  2865. */
  2866. public function testError() {
  2867. $Contact = ClassRegistry::getObject('Contact');
  2868. $Contact->validationErrors['field'] = array(1);
  2869. $result = $this->Form->error('Contact.field');
  2870. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2871. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2872. $this->assertEquals('Error in field Field', $result);
  2873. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2874. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2875. $this->assertEquals('This field contains invalid input', $result);
  2876. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2877. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2878. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2879. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2880. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2881. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2882. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2883. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2884. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2885. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2886. $this->assertEquals('<strong>Badness!</strong>', $result);
  2887. $Contact->validationErrors['field'] = array("email");
  2888. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2889. $expected = array(
  2890. 'div' => array('class' => 'field-error'),
  2891. 'No good!',
  2892. '/div'
  2893. );
  2894. $this->assertTags($result, $expected);
  2895. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2896. $result = $this->Form->error('Contact.field', array(
  2897. 'notEmpty' => 'Cannot be empty',
  2898. 'email' => 'No good!'
  2899. ));
  2900. $expected = array(
  2901. 'div' => array('class' => 'error-message'),
  2902. 'ul' => array(),
  2903. '<li', 'Cannot be empty', '/li',
  2904. '<li', 'No good!', '/li',
  2905. '<li', 'Something else', '/li',
  2906. '/ul',
  2907. '/div'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. /** Testing error messages list options **/
  2911. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2912. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2913. $expected = array(
  2914. 'div' => array('class' => 'error-message'),
  2915. 'ol' => array(),
  2916. '<li', 'notEmpty', '/li',
  2917. '<li', 'email', '/li',
  2918. '/ol',
  2919. '/div'
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2923. $expected = array(
  2924. 'div' => array('class' => 'error-message'),
  2925. 'ol' => array(),
  2926. '<li', 'notEmpty', '/li',
  2927. '<li', 'email', '/li',
  2928. '/ol',
  2929. '/div'
  2930. );
  2931. $this->assertTags($result, $expected);
  2932. $result = $this->Form->error('Contact.field', null, array(
  2933. 'listOptions' => array(
  2934. 'class' => 'ul-class',
  2935. 'itemOptions' => array(
  2936. 'class' => 'li-class'
  2937. )
  2938. )
  2939. ));
  2940. $expected = array(
  2941. 'div' => array('class' => 'error-message'),
  2942. 'ul' => array('class' => 'ul-class'),
  2943. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2944. array('li' => array('class' => 'li-class')), 'email', '/li',
  2945. '/ul',
  2946. '/div'
  2947. );
  2948. $this->assertTags($result, $expected);
  2949. }
  2950. /**
  2951. * test error options when using form->input();
  2952. *
  2953. * @return void
  2954. */
  2955. public function testInputErrorEscape() {
  2956. $this->Form->create('ValidateProfile');
  2957. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2958. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2959. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2960. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2961. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2962. $this->assertRegExp('/required<br>/', $result);
  2963. }
  2964. /**
  2965. * testPassword method
  2966. *
  2967. * Test password element generation
  2968. *
  2969. * @return void
  2970. */
  2971. public function testPassword() {
  2972. $Contact = ClassRegistry::getObject('Contact');
  2973. $result = $this->Form->password('Contact.field');
  2974. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2975. $Contact->validationErrors['passwd'] = 1;
  2976. $this->Form->request->data['Contact']['passwd'] = 'test';
  2977. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2978. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2979. }
  2980. /**
  2981. * testRadio method
  2982. *
  2983. * Test radio element set generation
  2984. *
  2985. * @return void
  2986. */
  2987. public function testRadio() {
  2988. $result = $this->Form->radio('Model.field', array('option A'));
  2989. $expected = array(
  2990. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2992. 'label' => array('for' => 'ModelField0'),
  2993. 'option A',
  2994. '/label'
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2998. $expected = array(
  2999. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3000. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3001. 'label' => array('for' => 'ModelField12'),
  3002. 'half',
  3003. '/label'
  3004. );
  3005. $this->assertTags($result, $expected);
  3006. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3007. $expected = array(
  3008. 'fieldset' => array(),
  3009. 'legend' => array(),
  3010. 'Field',
  3011. '/legend',
  3012. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3013. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3014. array('label' => array('for' => 'ModelField0')),
  3015. 'option A',
  3016. '/label',
  3017. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3018. array('label' => array('for' => 'ModelField1')),
  3019. 'option B',
  3020. '/label',
  3021. '/fieldset'
  3022. );
  3023. $this->assertTags($result, $expected);
  3024. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3025. $expected = array(
  3026. 'fieldset' => array(),
  3027. 'legend' => array(),
  3028. 'Field',
  3029. '/legend',
  3030. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3031. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3032. array('label' => array('for' => 'ModelField0')),
  3033. 'option A',
  3034. '/label',
  3035. 'br' => array(),
  3036. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3037. array('label' => array('for' => 'ModelField1')),
  3038. 'option B',
  3039. '/label',
  3040. '/fieldset'
  3041. );
  3042. $this->assertTags($result, $expected);
  3043. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3044. $expected = array(
  3045. 'div' => array('class' => 'input radio'),
  3046. 'fieldset' => array(),
  3047. 'legend' => array(),
  3048. 'Legend title',
  3049. '/legend',
  3050. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3051. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3052. array('label' => array('for' => 'NewsletterSubscribe0')),
  3053. 'Unsubscribe',
  3054. '/label',
  3055. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3056. array('label' => array('for' => 'NewsletterSubscribe1')),
  3057. 'Subscribe',
  3058. '/label',
  3059. '/fieldset',
  3060. '/div'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3064. $expected = array(
  3065. 'div' => array('class' => 'input radio'),
  3066. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3067. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3068. array('label' => array('for' => 'NewsletterSubscribe0')),
  3069. 'Unsubscribe',
  3070. '/label',
  3071. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3072. array('label' => array('for' => 'NewsletterSubscribe1')),
  3073. 'Subscribe',
  3074. '/label',
  3075. '/div'
  3076. );
  3077. $this->assertTags($result, $expected);
  3078. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3079. $expected = array(
  3080. 'div' => array('class' => 'input radio'),
  3081. 'fieldset' => array(),
  3082. 'legend' => array(),
  3083. 'Legend title',
  3084. '/legend',
  3085. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3086. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3087. 'Unsubscribe',
  3088. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3089. 'Subscribe',
  3090. '/fieldset',
  3091. '/div'
  3092. );
  3093. $this->assertTags($result, $expected);
  3094. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3095. $expected = array(
  3096. 'div' => array('class' => 'input radio'),
  3097. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3098. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3099. 'Unsubscribe',
  3100. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3101. 'Subscribe',
  3102. '/div'
  3103. );
  3104. $this->assertTags($result, $expected);
  3105. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3106. $expected = array(
  3107. 'div' => array('class' => 'input radio'),
  3108. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3109. 'Unsubscribe',
  3110. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3111. 'Subscribe',
  3112. '/div'
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3116. $expected = array(
  3117. 'fieldset' => array(),
  3118. 'legend' => array(),
  3119. 'Gender',
  3120. '/legend',
  3121. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3122. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3123. array('label' => array('for' => 'EmployeeGenderMale')),
  3124. 'Male',
  3125. '/label',
  3126. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3127. array('label' => array('for' => 'EmployeeGenderFemale')),
  3128. 'Female',
  3129. '/label',
  3130. '/fieldset',
  3131. );
  3132. $this->assertTags($result, $expected);
  3133. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Gender',
  3138. '/legend',
  3139. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3140. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3141. array('label' => array('for' => 'OfficerGenderMale')),
  3142. 'Male',
  3143. '/label',
  3144. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3145. array('label' => array('for' => 'OfficerGenderFemale')),
  3146. 'Female',
  3147. '/label',
  3148. '/fieldset',
  3149. );
  3150. $this->assertTags($result, $expected);
  3151. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3152. $expected = array(
  3153. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3154. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3155. 'label' => array('for' => 'Contact1Imrequired0'),
  3156. 'option A',
  3157. '/label'
  3158. );
  3159. $this->assertTags($result, $expected);
  3160. $result = $this->Form->radio('Model.1.field', array('option A'));
  3161. $expected = array(
  3162. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3163. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3164. 'label' => array('for' => 'Model1Field0'),
  3165. 'option A',
  3166. '/label'
  3167. );
  3168. $this->assertTags($result, $expected);
  3169. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3170. $expected = array(
  3171. 'fieldset' => array(),
  3172. 'legend' => array(),
  3173. 'Field',
  3174. '/legend',
  3175. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3176. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3177. array('label' => array('for' => 'ModelField0')),
  3178. 'option A',
  3179. '/label',
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3181. array('label' => array('for' => 'ModelField1')),
  3182. 'option B',
  3183. '/label',
  3184. '/fieldset'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $result = $this->Form->radio(
  3188. 'Model.field',
  3189. array('option A', 'option B'),
  3190. array('between' => 'I am between')
  3191. );
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'I am between',
  3198. 'input' => array(
  3199. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3200. 'value' => '', 'id' => 'ModelField_'
  3201. ),
  3202. array('input' => array(
  3203. 'type' => 'radio', 'name' => 'data[Model][field]',
  3204. 'value' => '0', 'id' => 'ModelField0'
  3205. )),
  3206. array('label' => array('for' => 'ModelField0')),
  3207. 'option A',
  3208. '/label',
  3209. array('input' => array(
  3210. 'type' => 'radio', 'name' => 'data[Model][field]',
  3211. 'value' => '1', 'id' => 'ModelField1'
  3212. )),
  3213. array('label' => array('for' => 'ModelField1')),
  3214. 'option B',
  3215. '/label',
  3216. '/fieldset'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. }
  3220. /**
  3221. * Test that radios with a 0 value are selected under the correct conditions.
  3222. *
  3223. * @return void
  3224. */
  3225. public function testRadioOptionWithZeroValue() {
  3226. $expected = array(
  3227. 'fieldset' => array(),
  3228. 'legend' => array(),
  3229. 'Field',
  3230. '/legend',
  3231. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3232. array('label' => array('for' => 'ModelField1')),
  3233. 'Yes',
  3234. '/label',
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3236. array('label' => array('for' => 'ModelField0')),
  3237. 'No',
  3238. '/label',
  3239. '/fieldset'
  3240. );
  3241. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3242. $this->assertTags($result, $expected);
  3243. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3244. $this->assertTags($result, $expected);
  3245. $expected = array(
  3246. 'fieldset' => array(),
  3247. 'legend' => array(),
  3248. 'Field',
  3249. '/legend',
  3250. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3251. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3252. array('label' => array('for' => 'ModelField1')),
  3253. 'Yes',
  3254. '/label',
  3255. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3256. array('label' => array('for' => 'ModelField0')),
  3257. 'No',
  3258. '/label',
  3259. '/fieldset'
  3260. );
  3261. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3262. $this->assertTags($result, $expected);
  3263. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3264. $this->assertTags($result, $expected);
  3265. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3266. $this->assertTags($result, $expected);
  3267. }
  3268. /**
  3269. * test disabled radio options
  3270. *
  3271. * @return void
  3272. */
  3273. public function testRadioDisabled() {
  3274. $result = $this->Form->radio(
  3275. 'Model.field',
  3276. array('option A', 'option B'),
  3277. array('disabled' => array('option A'), 'value' => '0')
  3278. );
  3279. $expected = array(
  3280. 'fieldset' => array(),
  3281. 'legend' => array(),
  3282. 'Field',
  3283. '/legend',
  3284. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3285. array('label' => array('for' => 'ModelField0')),
  3286. 'option A',
  3287. '/label',
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3289. array('label' => array('for' => 'ModelField1')),
  3290. 'option B',
  3291. '/label',
  3292. '/fieldset'
  3293. );
  3294. $this->assertTags($result, $expected);
  3295. $result = $this->Form->radio(
  3296. 'Model.field',
  3297. array('option A', 'option B'),
  3298. array('disabled' => true, 'value' => '0')
  3299. );
  3300. $expected = array(
  3301. 'fieldset' => array(),
  3302. 'legend' => array(),
  3303. 'Field',
  3304. '/legend',
  3305. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3306. array('label' => array('for' => 'ModelField0')),
  3307. 'option A',
  3308. '/label',
  3309. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3310. array('label' => array('for' => 'ModelField1')),
  3311. 'option B',
  3312. '/label',
  3313. '/fieldset'
  3314. );
  3315. $this->assertTags($result, $expected);
  3316. $result = $this->Form->radio(
  3317. 'Model.field',
  3318. array('option A', 'option B'),
  3319. array('disabled' => 'disabled', 'value' => '0')
  3320. );
  3321. $expected = array(
  3322. 'fieldset' => array(),
  3323. 'legend' => array(),
  3324. 'Field',
  3325. '/legend',
  3326. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3327. array('label' => array('for' => 'ModelField0')),
  3328. 'option A',
  3329. '/label',
  3330. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3331. array('label' => array('for' => 'ModelField1')),
  3332. 'option B',
  3333. '/label',
  3334. '/fieldset'
  3335. );
  3336. $this->assertTags($result, $expected);
  3337. }
  3338. /**
  3339. * test disabling the hidden input for radio buttons
  3340. *
  3341. * @return void
  3342. */
  3343. public function testRadioHiddenInputDisabling() {
  3344. $result = $this->Form->input('Model.1.field', array(
  3345. 'type' => 'radio',
  3346. 'options' => array('option A'),
  3347. 'hiddenField' => false
  3348. )
  3349. );
  3350. $expected = array(
  3351. 'div' => array('class' => 'input radio'),
  3352. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3353. 'label' => array('for' => 'Model1Field0'),
  3354. 'option A',
  3355. '/label',
  3356. '/div'
  3357. );
  3358. $this->assertTags($result, $expected);
  3359. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3360. $expected = array(
  3361. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3362. 'label' => array('for' => 'Model1Field0'),
  3363. 'option A',
  3364. '/label'
  3365. );
  3366. $this->assertTags($result, $expected);
  3367. }
  3368. /**
  3369. * test adding an empty option for radio buttons
  3370. *
  3371. * @return void
  3372. */
  3373. public function testRadioAddEmptyOption() {
  3374. $result = $this->Form->input('Model.1.field', array(
  3375. 'type' => 'radio',
  3376. 'options' => array('option A'),
  3377. 'empty' => true,
  3378. 'hiddenField' => false
  3379. ));
  3380. $expected = array(
  3381. 'div' => array('class' => 'input radio'),
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3387. array('label' => array('for' => 'Model1Field')),
  3388. __('empty'),
  3389. '/label',
  3390. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3391. array('label' => array('for' => 'Model1Field0')),
  3392. 'option A',
  3393. '/label',
  3394. '/fieldset',
  3395. '/div'
  3396. );
  3397. $this->assertTags($result, $expected);
  3398. $result = $this->Form->input('Model.1.field', array(
  3399. 'type' => 'radio',
  3400. 'options' => array('option A'),
  3401. 'empty' => 'CustomEmptyLabel',
  3402. 'hiddenField' => false
  3403. ));
  3404. $expected = array(
  3405. 'div' => array('class' => 'input radio'),
  3406. 'fieldset' => array(),
  3407. 'legend' => array(),
  3408. 'Field',
  3409. '/legend',
  3410. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3411. array('label' => array('for' => 'Model1Field')),
  3412. 'CustomEmptyLabel',
  3413. '/label',
  3414. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3415. array('label' => array('for' => 'Model1Field0')),
  3416. 'option A',
  3417. '/label',
  3418. '/fieldset',
  3419. '/div'
  3420. );
  3421. $this->assertTags($result, $expected);
  3422. $result = $this->Form->input('Model.1.field', array(
  3423. 'type' => 'radio',
  3424. 'options' => array('option A'),
  3425. 'empty' => false,
  3426. 'hiddenField' => false
  3427. ));
  3428. $this->assertTextNotContains('"Model1Field"', $result);
  3429. }
  3430. /**
  3431. * testSelect method
  3432. *
  3433. * Test select element generation.
  3434. *
  3435. * @return void
  3436. */
  3437. public function testSelect() {
  3438. $result = $this->Form->select('Model.field', array());
  3439. $expected = array(
  3440. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3441. array('option' => array('value' => '')),
  3442. '/option',
  3443. '/select'
  3444. );
  3445. $this->assertTags($result, $expected);
  3446. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3447. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3448. $expected = array(
  3449. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3450. array('option' => array('value' => '')),
  3451. '/option',
  3452. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3453. 'good',
  3454. '/option',
  3455. array('option' => array('value' => 'other')),
  3456. 'bad',
  3457. '/option',
  3458. '/select'
  3459. );
  3460. $this->assertTags($result, $expected);
  3461. $this->Form->request->data = array();
  3462. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3463. $expected = array(
  3464. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3465. array('option' => array('value' => '')),
  3466. '/option',
  3467. array('option' => array('value' => 'value')),
  3468. 'good',
  3469. '/option',
  3470. array('option' => array('value' => 'other')),
  3471. 'bad',
  3472. '/option',
  3473. '/select'
  3474. );
  3475. $this->assertTags($result, $expected);
  3476. $result = $this->Form->select(
  3477. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3478. array('empty' => false)
  3479. );
  3480. $expected = array(
  3481. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3482. array('option' => array('value' => 'first')),
  3483. 'first &quot;html&quot; &lt;chars&gt;',
  3484. '/option',
  3485. array('option' => array('value' => 'second')),
  3486. 'value',
  3487. '/option',
  3488. '/select'
  3489. );
  3490. $this->assertTags($result, $expected);
  3491. $result = $this->Form->select(
  3492. 'Model.field',
  3493. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3494. array('escape' => false, 'empty' => false)
  3495. );
  3496. $expected = array(
  3497. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3498. array('option' => array('value' => 'first')),
  3499. 'first "html" <chars>',
  3500. '/option',
  3501. array('option' => array('value' => 'second')),
  3502. 'value',
  3503. '/option',
  3504. '/select'
  3505. );
  3506. $this->assertTags($result, $expected);
  3507. $options = array(
  3508. array('value' => 'first', 'name' => 'First'),
  3509. array('value' => 'first', 'name' => 'Another First'),
  3510. );
  3511. $result = $this->Form->select(
  3512. 'Model.field',
  3513. $options,
  3514. array('escape' => false, 'empty' => false)
  3515. );
  3516. $expected = array(
  3517. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3518. array('option' => array('value' => 'first')),
  3519. 'First',
  3520. '/option',
  3521. array('option' => array('value' => 'first')),
  3522. 'Another First',
  3523. '/option',
  3524. '/select'
  3525. );
  3526. $this->assertTags($result, $expected);
  3527. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3528. $result = $this->Form->select(
  3529. 'Model.contact_id',
  3530. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3531. array('escape' => false, 'empty' => 'pick something')
  3532. );
  3533. $expected = array(
  3534. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3535. array('option' => array('value' => '')), 'pick something', '/option',
  3536. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3537. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3538. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3539. '/select'
  3540. );
  3541. $this->assertTags($result, $expected);
  3542. $this->Form->request->data['Model']['field'] = 0;
  3543. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3544. $expected = array(
  3545. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3546. array('option' => array('value' => '')), '/option',
  3547. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3548. array('option' => array('value' => '1')), 'Yes', '/option',
  3549. '/select'
  3550. );
  3551. $this->assertTags($result, $expected);
  3552. }
  3553. /**
  3554. * test that select() with optiongroups listens to the escape param.
  3555. *
  3556. * @return void
  3557. */
  3558. public function testSelectOptionGroupEscaping() {
  3559. $options = array(
  3560. '>< Key' => array(
  3561. 1 => 'One',
  3562. 2 => 'Two'
  3563. )
  3564. );
  3565. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3566. $expected = array(
  3567. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3568. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3569. array('option' => array('value' => '1')), 'One', '/option',
  3570. array('option' => array('value' => '2')), 'Two', '/option',
  3571. '/optgroup',
  3572. '/select'
  3573. );
  3574. $this->assertTags($result, $expected);
  3575. $options = array(
  3576. '>< Key' => array(
  3577. 1 => 'One',
  3578. 2 => 'Two'
  3579. )
  3580. );
  3581. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3582. $expected = array(
  3583. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3584. 'optgroup' => array('label' => '>< Key'),
  3585. array('option' => array('value' => '1')), 'One', '/option',
  3586. array('option' => array('value' => '2')), 'Two', '/option',
  3587. '/optgroup',
  3588. '/select'
  3589. );
  3590. $this->assertTags($result, $expected);
  3591. }
  3592. /**
  3593. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3594. *
  3595. * @return void
  3596. */
  3597. public function testSelectWithNullAttributes() {
  3598. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3599. $expected = array(
  3600. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3601. array('option' => array('value' => '0')),
  3602. 'first',
  3603. '/option',
  3604. array('option' => array('value' => '1')),
  3605. 'second',
  3606. '/option',
  3607. '/select'
  3608. );
  3609. $this->assertTags($result, $expected);
  3610. }
  3611. /**
  3612. * testNestedSelect method
  3613. *
  3614. * test select element generation with optgroups
  3615. *
  3616. * @return void
  3617. */
  3618. public function testNestedSelect() {
  3619. $result = $this->Form->select(
  3620. 'Model.field',
  3621. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3622. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3623. )), array('empty' => false)
  3624. );
  3625. $expected = array(
  3626. 'select' => array('name' => 'data[Model][field]',
  3627. 'id' => 'ModelField'),
  3628. array('option' => array('value' => 1)),
  3629. 'One',
  3630. '/option',
  3631. array('option' => array('value' => 2)),
  3632. 'Two',
  3633. '/option',
  3634. array('optgroup' => array('label' => 'Three')),
  3635. array('option' => array('value' => 4)),
  3636. 'Four',
  3637. '/option',
  3638. array('option' => array('value' => 5)),
  3639. 'Five',
  3640. '/option',
  3641. '/optgroup',
  3642. '/select'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->select(
  3646. 'Model.field',
  3647. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3648. array('showParents' => true, 'empty' => false)
  3649. );
  3650. $expected = array(
  3651. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3652. array('option' => array('value' => 1)),
  3653. 'One',
  3654. '/option',
  3655. array('option' => array('value' => 2)),
  3656. 'Two',
  3657. '/option',
  3658. array('optgroup' => array('label' => 'Three')),
  3659. array('option' => array('value' => 3)),
  3660. 'Three',
  3661. '/option',
  3662. array('option' => array('value' => 4)),
  3663. 'Four',
  3664. '/option',
  3665. '/optgroup',
  3666. '/select'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. }
  3670. /**
  3671. * testSelectMultiple method
  3672. *
  3673. * test generation of multiple select elements
  3674. *
  3675. * @return void
  3676. */
  3677. public function testSelectMultiple() {
  3678. $options = array('first', 'second', 'third');
  3679. $result = $this->Form->select(
  3680. 'Model.multi_field', $options, array('multiple' => true)
  3681. );
  3682. $expected = array(
  3683. 'input' => array(
  3684. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3685. ),
  3686. 'select' => array(
  3687. 'name' => 'data[Model][multi_field][]',
  3688. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3689. ),
  3690. array('option' => array('value' => '0')),
  3691. 'first',
  3692. '/option',
  3693. array('option' => array('value' => '1')),
  3694. 'second',
  3695. '/option',
  3696. array('option' => array('value' => '2')),
  3697. 'third',
  3698. '/option',
  3699. '/select'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. $result = $this->Form->select(
  3703. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3704. );
  3705. $expected = array(
  3706. 'input' => array(
  3707. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3708. ),
  3709. 'select' => array(
  3710. 'name' => 'data[Model][multi_field][]',
  3711. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3712. ),
  3713. array('option' => array('value' => '0')),
  3714. 'first',
  3715. '/option',
  3716. array('option' => array('value' => '1')),
  3717. 'second',
  3718. '/option',
  3719. array('option' => array('value' => '2')),
  3720. 'third',
  3721. '/option',
  3722. '/select'
  3723. );
  3724. $this->assertTags($result, $expected);
  3725. $result = $this->Form->select(
  3726. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3727. );
  3728. $expected = array(
  3729. 'input' => array(
  3730. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3731. ),
  3732. 'select' => array(
  3733. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3734. 'multiple' => 'multiple'
  3735. ),
  3736. array('option' => array('value' => '0', 'selected' => 'selected')),
  3737. 'first',
  3738. '/option',
  3739. array('option' => array('value' => '1', 'selected' => 'selected')),
  3740. 'second',
  3741. '/option',
  3742. array('option' => array('value' => '2')),
  3743. 'third',
  3744. '/option',
  3745. '/select'
  3746. );
  3747. $this->assertTags($result, $expected);
  3748. $result = $this->Form->select(
  3749. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3750. );
  3751. $expected = array(
  3752. 'select' => array(
  3753. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3754. ),
  3755. array('option' => array('value' => '0', 'selected' => 'selected')),
  3756. 'first',
  3757. '/option',
  3758. array('option' => array('value' => '1', 'selected' => 'selected')),
  3759. 'second',
  3760. '/option',
  3761. array('option' => array('value' => '2')),
  3762. 'third',
  3763. '/option',
  3764. '/select'
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. }
  3768. /**
  3769. * test generation of habtm select boxes.
  3770. *
  3771. * @return void
  3772. */
  3773. public function testHabtmSelectBox() {
  3774. $this->View->viewVars['contactTags'] = array(
  3775. 1 => 'blue',
  3776. 2 => 'red',
  3777. 3 => 'green'
  3778. );
  3779. $this->Form->request->data = array(
  3780. 'Contact' => array(),
  3781. 'ContactTag' => array(
  3782. array(
  3783. 'id' => 1,
  3784. 'name' => 'blue'
  3785. ),
  3786. array(
  3787. 'id' => 3,
  3788. 'name' => 'green'
  3789. )
  3790. )
  3791. );
  3792. $this->Form->create('Contact');
  3793. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3794. $expected = array(
  3795. 'input' => array(
  3796. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3797. ),
  3798. 'select' => array(
  3799. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3800. 'multiple' => 'multiple'
  3801. ),
  3802. array('option' => array('value' => '1', 'selected' => 'selected')),
  3803. 'blue',
  3804. '/option',
  3805. array('option' => array('value' => '2')),
  3806. 'red',
  3807. '/option',
  3808. array('option' => array('value' => '3', 'selected' => 'selected')),
  3809. 'green',
  3810. '/option',
  3811. '/select'
  3812. );
  3813. $this->assertTags($result, $expected);
  3814. }
  3815. /**
  3816. * test generation of multi select elements in checkbox format
  3817. *
  3818. * @return void
  3819. */
  3820. public function testSelectMultipleCheckboxes() {
  3821. $result = $this->Form->select(
  3822. 'Model.multi_field',
  3823. array('first', 'second', 'third'),
  3824. array('multiple' => 'checkbox')
  3825. );
  3826. $expected = array(
  3827. 'input' => array(
  3828. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3829. ),
  3830. array('div' => array('class' => 'checkbox')),
  3831. array('input' => array(
  3832. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3833. 'value' => '0', 'id' => 'ModelMultiField0'
  3834. )),
  3835. array('label' => array('for' => 'ModelMultiField0')),
  3836. 'first',
  3837. '/label',
  3838. '/div',
  3839. array('div' => array('class' => 'checkbox')),
  3840. array('input' => array(
  3841. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3842. 'value' => '1', 'id' => 'ModelMultiField1'
  3843. )),
  3844. array('label' => array('for' => 'ModelMultiField1')),
  3845. 'second',
  3846. '/label',
  3847. '/div',
  3848. array('div' => array('class' => 'checkbox')),
  3849. array('input' => array(
  3850. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3851. 'value' => '2', 'id' => 'ModelMultiField2'
  3852. )),
  3853. array('label' => array('for' => 'ModelMultiField2')),
  3854. 'third',
  3855. '/label',
  3856. '/div'
  3857. );
  3858. $this->assertTags($result, $expected);
  3859. $result = $this->Form->select(
  3860. 'Model.multi_field',
  3861. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3862. array('multiple' => 'checkbox')
  3863. );
  3864. $expected = array(
  3865. 'input' => array(
  3866. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3867. ),
  3868. array('div' => array('class' => 'checkbox')),
  3869. array('input' => array(
  3870. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3871. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3872. )),
  3873. array('label' => array('for' => 'ModelMultiFieldA')),
  3874. 'first',
  3875. '/label',
  3876. '/div',
  3877. array('div' => array('class' => 'checkbox')),
  3878. array('input' => array(
  3879. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3880. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3881. )),
  3882. array('label' => array('for' => 'ModelMultiFieldB')),
  3883. 'second',
  3884. '/label',
  3885. '/div',
  3886. array('div' => array('class' => 'checkbox')),
  3887. array('input' => array(
  3888. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3889. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3890. )),
  3891. array('label' => array('for' => 'ModelMultiFieldC')),
  3892. 'third',
  3893. '/label',
  3894. '/div'
  3895. );
  3896. $this->assertTags($result, $expected);
  3897. $result = $this->Form->select(
  3898. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3899. );
  3900. $expected = array(
  3901. 'input' => array(
  3902. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3903. ),
  3904. array('div' => array('class' => 'checkbox')),
  3905. array('input' => array(
  3906. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3907. 'value' => '1', 'id' => 'ModelMultiField1'
  3908. )),
  3909. array('label' => array('for' => 'ModelMultiField1')),
  3910. 'first',
  3911. '/label',
  3912. '/div'
  3913. );
  3914. $this->assertTags($result, $expected);
  3915. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3916. $result = $this->Form->select(
  3917. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3918. );
  3919. $expected = array(
  3920. 'input' => array(
  3921. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3922. ),
  3923. array('div' => array('class' => 'checkbox')),
  3924. array('input' => array(
  3925. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3926. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3927. )),
  3928. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3929. 'first',
  3930. '/label',
  3931. '/div',
  3932. array('div' => array('class' => 'checkbox')),
  3933. array('input' => array(
  3934. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3935. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3936. )),
  3937. array('label' => array('for' => 'ModelTagsArray')),
  3938. 'Array',
  3939. '/label',
  3940. '/div'
  3941. );
  3942. $this->assertTags($result, $expected);
  3943. }
  3944. /**
  3945. * test multiple checkboxes with div styles.
  3946. *
  3947. * @return void
  3948. */
  3949. public function testSelectMultipleCheckboxDiv() {
  3950. $result = $this->Form->select(
  3951. 'Model.tags',
  3952. array('first', 'second'),
  3953. array('multiple' => 'checkbox', 'class' => 'my-class')
  3954. );
  3955. $expected = array(
  3956. 'input' => array(
  3957. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3958. ),
  3959. array('div' => array('class' => 'my-class')),
  3960. array('input' => array(
  3961. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3962. 'value' => '0', 'id' => 'ModelTags0'
  3963. )),
  3964. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3965. '/div',
  3966. array('div' => array('class' => 'my-class')),
  3967. array('input' => array(
  3968. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3969. 'value' => '1', 'id' => 'ModelTags1'
  3970. )),
  3971. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3972. '/div'
  3973. );
  3974. $this->assertTags($result, $expected);
  3975. $result = $this->Form->input('Model.tags', array(
  3976. 'options' => array('first', 'second'),
  3977. 'multiple' => 'checkbox',
  3978. 'class' => 'my-class',
  3979. 'div' => false,
  3980. 'label' => false
  3981. ));
  3982. $this->assertTags($result, $expected);
  3983. $Contact = ClassRegistry::getObject('Contact');
  3984. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3985. $result = $this->Form->input('Contact.tags', array(
  3986. 'options' => array('one'),
  3987. 'multiple' => 'checkbox',
  3988. 'label' => false,
  3989. 'div' => false
  3990. ));
  3991. $expected = array(
  3992. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3993. array('div' => array('class' => 'checkbox form-error')),
  3994. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3995. array('label' => array('for' => 'ContactTags0')),
  3996. 'one',
  3997. '/label',
  3998. '/div'
  3999. );
  4000. $this->assertTags($result, $expected);
  4001. $result = $this->Form->input('Contact.tags', array(
  4002. 'options' => array('one'),
  4003. 'multiple' => 'checkbox',
  4004. 'class' => 'mycheckbox',
  4005. 'label' => false,
  4006. 'div' => false
  4007. ));
  4008. $expected = array(
  4009. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4010. array('div' => array('class' => 'mycheckbox form-error')),
  4011. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4012. array('label' => array('for' => 'ContactTags0')),
  4013. 'one',
  4014. '/label',
  4015. '/div'
  4016. );
  4017. $this->assertTags($result, $expected);
  4018. }
  4019. /**
  4020. * Checks the security hash array generated for multiple-input checkbox elements
  4021. *
  4022. * @return void
  4023. */
  4024. public function testSelectMultipleCheckboxSecurity() {
  4025. $this->Form->request['_Token'] = array('key' => 'testKey');
  4026. $this->assertEquals(array(), $this->Form->fields);
  4027. $result = $this->Form->select(
  4028. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4029. array('multiple' => 'checkbox')
  4030. );
  4031. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4032. $result = $this->Form->secure($this->Form->fields);
  4033. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4034. $this->assertRegExp('/"' . $key . '"/', $result);
  4035. }
  4036. /**
  4037. * Multiple select elements should always be secured as they always participate
  4038. * in the POST data.
  4039. *
  4040. * @return void
  4041. */
  4042. public function testSelectMultipleSecureWithNoOptions() {
  4043. $this->Form->request['_Token'] = array('key' => 'testkey');
  4044. $this->assertEquals(array(), $this->Form->fields);
  4045. $result = $this->Form->select(
  4046. 'Model.select',
  4047. array(),
  4048. array('multiple' => true)
  4049. );
  4050. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4051. }
  4052. /**
  4053. * When a select box has no options it should not be added to the fields list
  4054. * as it always fail post validation.
  4055. *
  4056. * @return void
  4057. */
  4058. public function testSelectNoSecureWithNoOptions() {
  4059. $this->Form->request['_Token'] = array('key' => 'testkey');
  4060. $this->assertEquals(array(), $this->Form->fields);
  4061. $this->Form->select(
  4062. 'Model.select',
  4063. array()
  4064. );
  4065. $this->assertEquals(array(), $this->Form->fields);
  4066. $this->Form->select(
  4067. 'Model.select',
  4068. array(),
  4069. array('empty' => true)
  4070. );
  4071. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4072. }
  4073. /**
  4074. * testInputMultipleCheckboxes method
  4075. *
  4076. * test input() resulting in multi select elements being generated.
  4077. *
  4078. * @return void
  4079. */
  4080. public function testInputMultipleCheckboxes() {
  4081. $result = $this->Form->input('Model.multi_field', array(
  4082. 'options' => array('first', 'second', 'third'),
  4083. 'multiple' => 'checkbox'
  4084. ));
  4085. $expected = array(
  4086. array('div' => array('class' => 'input select')),
  4087. array('label' => array('for' => 'ModelMultiField')),
  4088. 'Multi Field',
  4089. '/label',
  4090. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4091. array('div' => array('class' => 'checkbox')),
  4092. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4093. array('label' => array('for' => 'ModelMultiField0')),
  4094. 'first',
  4095. '/label',
  4096. '/div',
  4097. array('div' => array('class' => 'checkbox')),
  4098. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4099. array('label' => array('for' => 'ModelMultiField1')),
  4100. 'second',
  4101. '/label',
  4102. '/div',
  4103. array('div' => array('class' => 'checkbox')),
  4104. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4105. array('label' => array('for' => 'ModelMultiField2')),
  4106. 'third',
  4107. '/label',
  4108. '/div',
  4109. '/div'
  4110. );
  4111. $this->assertTags($result, $expected);
  4112. $result = $this->Form->input('Model.multi_field', array(
  4113. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4114. 'multiple' => 'checkbox'
  4115. ));
  4116. $expected = array(
  4117. array('div' => array('class' => 'input select')),
  4118. array('label' => array('for' => 'ModelMultiField')),
  4119. 'Multi Field',
  4120. '/label',
  4121. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4122. array('div' => array('class' => 'checkbox')),
  4123. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4124. array('label' => array('for' => 'ModelMultiFieldA')),
  4125. 'first',
  4126. '/label',
  4127. '/div',
  4128. array('div' => array('class' => 'checkbox')),
  4129. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4130. array('label' => array('for' => 'ModelMultiFieldB')),
  4131. 'second',
  4132. '/label',
  4133. '/div',
  4134. array('div' => array('class' => 'checkbox')),
  4135. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4136. array('label' => array('for' => 'ModelMultiFieldC')),
  4137. 'third',
  4138. '/label',
  4139. '/div',
  4140. '/div'
  4141. );
  4142. $this->assertTags($result, $expected);
  4143. $result = $this->Form->input('Model.multi_field', array(
  4144. 'options' => array('1' => 'first'),
  4145. 'multiple' => 'checkbox',
  4146. 'label' => false,
  4147. 'div' => false
  4148. ));
  4149. $expected = array(
  4150. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4151. array('div' => array('class' => 'checkbox')),
  4152. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4153. array('label' => array('for' => 'ModelMultiField1')),
  4154. 'first',
  4155. '/label',
  4156. '/div'
  4157. );
  4158. $this->assertTags($result, $expected);
  4159. $result = $this->Form->input('Model.multi_field', array(
  4160. 'options' => array('2' => 'second'),
  4161. 'multiple' => 'checkbox',
  4162. 'label' => false,
  4163. 'div' => false
  4164. ));
  4165. $expected = array(
  4166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4167. array('div' => array('class' => 'checkbox')),
  4168. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4169. array('label' => array('for' => 'ModelMultiField2')),
  4170. 'second',
  4171. '/label',
  4172. '/div'
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. }
  4176. /**
  4177. * testSelectHiddenFieldOmission method
  4178. *
  4179. * test that select() with 'hiddenField' => false omits the hidden field
  4180. *
  4181. * @return void
  4182. */
  4183. public function testSelectHiddenFieldOmission() {
  4184. $result = $this->Form->select('Model.multi_field',
  4185. array('first', 'second'),
  4186. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4187. );
  4188. $expected = array(
  4189. array('div' => array('class' => 'checkbox')),
  4190. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4191. array('label' => array('for' => 'ModelMultiField0')),
  4192. 'first',
  4193. '/label',
  4194. '/div',
  4195. array('div' => array('class' => 'checkbox')),
  4196. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4197. array('label' => array('for' => 'ModelMultiField1')),
  4198. 'second',
  4199. '/label',
  4200. '/div'
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. $result = $this->Form->input('Model.multi_field', array(
  4204. 'options' => array('first', 'second'),
  4205. 'multiple' => 'checkbox',
  4206. 'hiddenField' => false
  4207. ));
  4208. $expected = array(
  4209. array('div' => array('class' => 'input select')),
  4210. array('label' => array('for' => 'ModelMultiField')),
  4211. 'Multi Field',
  4212. '/label',
  4213. array('div' => array('class' => 'checkbox')),
  4214. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4215. array('label' => array('for' => 'ModelMultiField0')),
  4216. 'first',
  4217. '/label',
  4218. '/div',
  4219. array('div' => array('class' => 'checkbox')),
  4220. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4221. array('label' => array('for' => 'ModelMultiField1')),
  4222. 'second',
  4223. '/label',
  4224. '/div',
  4225. '/div'
  4226. );
  4227. $this->assertTags($result, $expected);
  4228. }
  4229. /**
  4230. * test that select() with multiple = checkbox works with overriding name attribute.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testSelectCheckboxMultipleOverrideName() {
  4235. $result = $this->Form->input('category', array(
  4236. 'type' => 'select',
  4237. 'multiple' => 'checkbox',
  4238. 'name' => 'data[fish]',
  4239. 'options' => array('1', '2'),
  4240. 'div' => false,
  4241. 'label' => false,
  4242. ));
  4243. $expected = array(
  4244. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4245. array('div' => array('class' => 'checkbox')),
  4246. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4247. array('label' => array('for' => 'Category0')), '1', '/label',
  4248. '/div',
  4249. array('div' => array('class' => 'checkbox')),
  4250. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4251. array('label' => array('for' => 'Category1')), '2', '/label',
  4252. '/div'
  4253. );
  4254. $this->assertTags($result, $expected);
  4255. }
  4256. /**
  4257. * Test that 'id' overrides all the checkbox id's as well.
  4258. *
  4259. * @return void
  4260. */
  4261. public function testSelectCheckboxMultipleId() {
  4262. $result = $this->Form->select(
  4263. 'Model.multi_field',
  4264. array('first', 'second', 'third'),
  4265. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4266. );
  4267. $expected = array(
  4268. 'input' => array(
  4269. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4270. ),
  4271. array('div' => array('class' => 'checkbox')),
  4272. array('input' => array(
  4273. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4274. 'value' => '0', 'id' => 'CustomId0'
  4275. )),
  4276. array('label' => array('for' => 'CustomId0')),
  4277. 'first',
  4278. '/label',
  4279. '/div',
  4280. array('div' => array('class' => 'checkbox')),
  4281. array('input' => array(
  4282. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4283. 'value' => '1', 'id' => 'CustomId1'
  4284. )),
  4285. array('label' => array('for' => 'CustomId1')),
  4286. 'second',
  4287. '/label',
  4288. '/div',
  4289. array('div' => array('class' => 'checkbox')),
  4290. array('input' => array(
  4291. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4292. 'value' => '2', 'id' => 'CustomId2'
  4293. )),
  4294. array('label' => array('for' => 'CustomId2')),
  4295. 'third',
  4296. '/label',
  4297. '/div'
  4298. );
  4299. $this->assertTags($result, $expected);
  4300. }
  4301. /**
  4302. * testCheckbox method
  4303. *
  4304. * Test generation of checkboxes
  4305. *
  4306. * @return void
  4307. */
  4308. public function testCheckbox() {
  4309. $result = $this->Form->checkbox('Model.field');
  4310. $expected = array(
  4311. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4312. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4313. );
  4314. $this->assertTags($result, $expected);
  4315. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4316. $expected = array(
  4317. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4318. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4319. );
  4320. $this->assertTags($result, $expected);
  4321. $Contact = ClassRegistry::getObject('Contact');
  4322. $Contact->validationErrors['field'] = 1;
  4323. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4324. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4325. $expected = array(
  4326. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4327. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4331. $expected = array(
  4332. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4333. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4334. );
  4335. $this->assertTags($result, $expected);
  4336. $this->Form->request->data['Contact']['field'] = '';
  4337. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4338. $expected = array(
  4339. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4340. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4341. );
  4342. $this->assertTags($result, $expected);
  4343. $Contact->validationErrors = array();
  4344. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4345. $expected = array(
  4346. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4347. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. $this->Form->request->data['Contact']['published'] = 1;
  4351. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4352. $expected = array(
  4353. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4354. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4355. );
  4356. $this->assertTags($result, $expected);
  4357. $this->Form->request->data['Contact']['published'] = 0;
  4358. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4359. $expected = array(
  4360. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4361. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4362. );
  4363. $this->assertTags($result, $expected);
  4364. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4365. $expected = array(
  4366. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4367. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4368. );
  4369. $this->assertTags($result, $expected);
  4370. $result = $this->Form->checkbox('CustomField.1.value');
  4371. $expected = array(
  4372. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4373. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. }
  4377. /**
  4378. * test checkbox() with a custom name attribute
  4379. *
  4380. * @return void
  4381. */
  4382. public function testCheckboxCustomNameAttribute() {
  4383. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4384. $expected = array(
  4385. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4386. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4387. );
  4388. $this->assertTags($result, $expected);
  4389. }
  4390. /**
  4391. * test the checked option for checkboxes.
  4392. *
  4393. * @return void
  4394. */
  4395. public function testCheckboxCheckedOption() {
  4396. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4397. $expected = array(
  4398. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4399. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4403. $expected = array(
  4404. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4405. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4406. );
  4407. $this->assertTags($result, $expected);
  4408. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4409. $expected = array(
  4410. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4411. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4412. );
  4413. $this->assertTags($result, $expected);
  4414. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4415. $expected = array(
  4416. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4417. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4418. );
  4419. $this->assertTags($result, $expected);
  4420. $this->Form->request->data['Model']['field'] = 1;
  4421. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4422. $expected = array(
  4423. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4424. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4425. );
  4426. $this->assertTags($result, $expected);
  4427. }
  4428. /**
  4429. * Test that disabled attribute works on both the checkbox and hidden input.
  4430. *
  4431. * @return void
  4432. */
  4433. public function testCheckboxDisabling() {
  4434. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4435. $expected = array(
  4436. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4437. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4438. );
  4439. $this->assertTags($result, $expected);
  4440. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4441. $expected = array(
  4442. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4443. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4444. );
  4445. $this->assertTags($result, $expected);
  4446. }
  4447. /**
  4448. * Test that the hidden input for checkboxes can be omitted or set to a
  4449. * specific value.
  4450. *
  4451. * @return void
  4452. */
  4453. public function testCheckboxHiddenField() {
  4454. $result = $this->Form->input('UserForm.something', array(
  4455. 'type' => 'checkbox',
  4456. 'hiddenField' => false
  4457. ));
  4458. $expected = array(
  4459. 'div' => array('class' => 'input checkbox'),
  4460. array('input' => array(
  4461. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4462. 'value' => '1', 'id' => 'UserFormSomething'
  4463. )),
  4464. 'label' => array('for' => 'UserFormSomething'),
  4465. 'Something',
  4466. '/label',
  4467. '/div'
  4468. );
  4469. $this->assertTags($result, $expected);
  4470. $result = $this->Form->input('UserForm.something', array(
  4471. 'type' => 'checkbox',
  4472. 'value' => 'Y',
  4473. 'hiddenField' => 'N',
  4474. ));
  4475. $expected = array(
  4476. 'div' => array('class' => 'input checkbox'),
  4477. array('input' => array(
  4478. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4479. 'value' => 'N', 'id' => 'UserFormSomething_'
  4480. )),
  4481. array('input' => array(
  4482. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4483. 'value' => 'Y', 'id' => 'UserFormSomething'
  4484. )),
  4485. 'label' => array('for' => 'UserFormSomething'),
  4486. 'Something',
  4487. '/label',
  4488. '/div'
  4489. );
  4490. $this->assertTags($result, $expected);
  4491. }
  4492. /**
  4493. * testDateTime method
  4494. *
  4495. * Test generation of date/time select elements
  4496. *
  4497. * @return void
  4498. */
  4499. public function testDateTime() {
  4500. extract($this->dateRegex);
  4501. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4502. $now = strtotime('now');
  4503. $expected = array(
  4504. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4505. $daysRegex,
  4506. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4507. date('j', $now),
  4508. '/option',
  4509. '*/select',
  4510. '-',
  4511. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4512. $monthsRegex,
  4513. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4514. date('F', $now),
  4515. '/option',
  4516. '*/select',
  4517. '-',
  4518. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4519. $yearsRegex,
  4520. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4521. date('Y', $now),
  4522. '/option',
  4523. '*/select',
  4524. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4525. $hoursRegex,
  4526. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4527. date('g', $now),
  4528. '/option',
  4529. '*/select',
  4530. ':',
  4531. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4532. $minutesRegex,
  4533. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4534. date('i', $now),
  4535. '/option',
  4536. '*/select',
  4537. ' ',
  4538. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4539. $meridianRegex,
  4540. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4541. date('a', $now),
  4542. '/option',
  4543. '*/select'
  4544. );
  4545. $this->assertTags($result, $expected);
  4546. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4547. $expected = array(
  4548. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4549. $daysRegex,
  4550. array('option' => array('value' => '')),
  4551. '/option',
  4552. '*/select',
  4553. '-',
  4554. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4555. $monthsRegex,
  4556. array('option' => array('value' => '')),
  4557. '/option',
  4558. '*/select',
  4559. '-',
  4560. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4561. $yearsRegex,
  4562. array('option' => array('value' => '')),
  4563. '/option',
  4564. '*/select',
  4565. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4566. $hoursRegex,
  4567. array('option' => array('value' => '')),
  4568. '/option',
  4569. '*/select',
  4570. ':',
  4571. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4572. $minutesRegex,
  4573. array('option' => array('value' => '')),
  4574. '/option',
  4575. '*/select',
  4576. ' ',
  4577. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4578. $meridianRegex,
  4579. array('option' => array('value' => '')),
  4580. '/option',
  4581. '*/select'
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4585. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4586. $this->assertTags($result, $expected);
  4587. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4588. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4589. $this->assertTags($result, $expected);
  4590. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4591. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4592. $expected = array(
  4593. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4594. $daysRegex,
  4595. array('option' => array('value' => '')),
  4596. '/option',
  4597. '*/select',
  4598. '-',
  4599. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4600. $monthsRegex,
  4601. array('option' => array('value' => '')),
  4602. '/option',
  4603. '*/select',
  4604. '-',
  4605. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4606. $yearsRegex,
  4607. array('option' => array('value' => '')),
  4608. '/option',
  4609. '*/select',
  4610. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4611. $hoursRegex,
  4612. array('option' => array('value' => '')),
  4613. '/option',
  4614. '*/select',
  4615. ':',
  4616. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4617. $minutesRegex,
  4618. array('option' => array('value' => '')),
  4619. '/option',
  4620. array('option' => array('value' => '00')),
  4621. '00',
  4622. '/option',
  4623. array('option' => array('value' => '05')),
  4624. '05',
  4625. '/option',
  4626. array('option' => array('value' => '10')),
  4627. '10',
  4628. '/option',
  4629. '*/select',
  4630. ' ',
  4631. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4632. $meridianRegex,
  4633. array('option' => array('value' => '')),
  4634. '/option',
  4635. '*/select'
  4636. );
  4637. $this->assertTags($result, $expected);
  4638. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4639. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4640. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4641. $this->Form->request->data['Contact']['data'] = null;
  4642. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4643. $expected = array(
  4644. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4645. $daysRegex,
  4646. array('option' => array('value' => '')),
  4647. '/option',
  4648. '*/select',
  4649. '-',
  4650. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4651. $monthsRegex,
  4652. array('option' => array('value' => '')),
  4653. '/option',
  4654. '*/select',
  4655. '-',
  4656. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4657. $yearsRegex,
  4658. array('option' => array('value' => '')),
  4659. '/option',
  4660. '*/select',
  4661. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4662. $hoursRegex,
  4663. array('option' => array('value' => '')),
  4664. '/option',
  4665. '*/select',
  4666. ':',
  4667. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4668. $minutesRegex,
  4669. array('option' => array('value' => '')),
  4670. '/option',
  4671. '*/select',
  4672. ' ',
  4673. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4674. $meridianRegex,
  4675. array('option' => array('value' => '')),
  4676. '/option',
  4677. '*/select'
  4678. );
  4679. $this->assertTags($result, $expected);
  4680. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4681. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4682. $now = strtotime($this->Form->data['Model']['field']);
  4683. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4684. $expected = array(
  4685. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4686. $daysRegex,
  4687. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4688. date('j', $now),
  4689. '/option',
  4690. '*/select',
  4691. '-',
  4692. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4693. $monthsRegex,
  4694. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4695. date('F', $now),
  4696. '/option',
  4697. '*/select',
  4698. '-',
  4699. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4700. $yearsRegex,
  4701. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4702. date('Y', $now),
  4703. '/option',
  4704. '*/select',
  4705. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4706. $hoursRegex,
  4707. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4708. date('g', $now),
  4709. '/option',
  4710. '*/select',
  4711. ':',
  4712. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4713. $minutesRegex,
  4714. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4715. date('i', $now),
  4716. '/option',
  4717. '*/select',
  4718. ' ',
  4719. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4720. $meridianRegex,
  4721. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4722. date('a', $now),
  4723. '/option',
  4724. '*/select'
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. $selected = strtotime('2008-10-26 12:33:00');
  4728. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4729. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4730. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4731. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4732. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4733. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4734. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4735. $this->Form->create('Contact');
  4736. $result = $this->Form->input('published');
  4737. $now = strtotime('now');
  4738. $expected = array(
  4739. 'div' => array('class' => 'input date'),
  4740. 'label' => array('for' => 'ContactPublishedMonth'),
  4741. 'Published',
  4742. '/label',
  4743. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4744. $monthsRegex,
  4745. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4746. date('F', $now),
  4747. '/option',
  4748. '*/select',
  4749. '-',
  4750. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4751. $daysRegex,
  4752. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4753. date('j', $now),
  4754. '/option',
  4755. '*/select',
  4756. '-',
  4757. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4758. $yearsRegex,
  4759. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4760. date('Y', $now),
  4761. '/option',
  4762. '*/select',
  4763. '/div'
  4764. );
  4765. $this->assertTags($result, $expected);
  4766. $result = $this->Form->input('published2', array('type' => 'date'));
  4767. $now = strtotime('now');
  4768. $expected = array(
  4769. 'div' => array('class' => 'input date'),
  4770. 'label' => array('for' => 'ContactPublished2Month'),
  4771. 'Published2',
  4772. '/label',
  4773. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4774. $monthsRegex,
  4775. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4776. date('F', $now),
  4777. '/option',
  4778. '*/select',
  4779. '-',
  4780. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4781. $daysRegex,
  4782. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4783. date('j', $now),
  4784. '/option',
  4785. '*/select',
  4786. '-',
  4787. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4788. $yearsRegex,
  4789. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4790. date('Y', $now),
  4791. '/option',
  4792. '*/select',
  4793. '/div'
  4794. );
  4795. $this->assertTags($result, $expected);
  4796. $this->Form->create('Contact');
  4797. $result = $this->Form->input('published', array('monthNames' => false));
  4798. $now = strtotime('now');
  4799. $expected = array(
  4800. 'div' => array('class' => 'input date'),
  4801. 'label' => array('for' => 'ContactPublishedMonth'),
  4802. 'Published',
  4803. '/label',
  4804. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4805. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4806. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4807. date('m', $now),
  4808. '/option',
  4809. '*/select',
  4810. '-',
  4811. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4812. $daysRegex,
  4813. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4814. date('j', $now),
  4815. '/option',
  4816. '*/select',
  4817. '-',
  4818. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4819. $yearsRegex,
  4820. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4821. date('Y', $now),
  4822. '/option',
  4823. '*/select',
  4824. '/div'
  4825. );
  4826. $this->assertTags($result, $expected);
  4827. $result = $this->Form->input('published', array('type' => 'time'));
  4828. $now = strtotime('now');
  4829. $expected = array(
  4830. 'div' => array('class' => 'input time'),
  4831. 'label' => array('for' => 'ContactPublishedHour'),
  4832. 'Published',
  4833. '/label',
  4834. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4835. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4836. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4837. date('g', $now),
  4838. '/option',
  4839. '*/select',
  4840. ':',
  4841. );
  4842. $this->assertTags($result, $expected);
  4843. $result = $this->Form->input('published', array(
  4844. 'timeFormat' => 24,
  4845. 'interval' => 5,
  4846. 'selected' => strtotime('2009-09-03 13:37:00'),
  4847. 'type' => 'datetime'
  4848. ));
  4849. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4850. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4851. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4852. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4853. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4854. $this->assertNoErrors();
  4855. $this->Form->request->data['Contact'] = array(
  4856. 'date' => array(
  4857. 'day' => '',
  4858. 'month' => '',
  4859. 'year' => '',
  4860. 'hour' => '',
  4861. 'min' => '',
  4862. 'meridian' => ''
  4863. )
  4864. );
  4865. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4866. }
  4867. /**
  4868. * test that datetime() and default values work.
  4869. *
  4870. * @return void
  4871. */
  4872. public function testDatetimeWithDefault() {
  4873. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4874. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4875. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4876. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4877. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4878. 'default' => '2009-06-01 11:15:30'
  4879. ));
  4880. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4881. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4882. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4883. }
  4884. /**
  4885. * test that bogus non-date time data doesn't cause errors.
  4886. *
  4887. * @return void
  4888. */
  4889. public function testDateTimeWithBogusData() {
  4890. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4891. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4892. }
  4893. /**
  4894. * testDateTimeEmptyAsArray
  4895. *
  4896. * @return void
  4897. */
  4898. public function testDateTimeEmptyAsArray() {
  4899. $result = $this->Form->dateTime('Contact.date',
  4900. 'DMY',
  4901. '12',
  4902. array(
  4903. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4904. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4905. )
  4906. )
  4907. );
  4908. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4909. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4910. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4911. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4912. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4913. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4914. $result = $this->Form->dateTime('Contact.date',
  4915. 'DMY',
  4916. '12',
  4917. array(
  4918. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4919. )
  4920. );
  4921. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4922. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4923. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4924. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4925. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4926. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4927. }
  4928. /**
  4929. * testFormDateTimeMulti method
  4930. *
  4931. * test multiple datetime element generation
  4932. *
  4933. * @return void
  4934. */
  4935. public function testFormDateTimeMulti() {
  4936. extract($this->dateRegex);
  4937. $result = $this->Form->dateTime('Contact.1.updated');
  4938. $expected = array(
  4939. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4940. $daysRegex,
  4941. array('option' => array('value' => '')),
  4942. '/option',
  4943. '*/select',
  4944. '-',
  4945. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4946. $monthsRegex,
  4947. array('option' => array('value' => '')),
  4948. '/option',
  4949. '*/select',
  4950. '-',
  4951. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4952. $yearsRegex,
  4953. array('option' => array('value' => '')),
  4954. '/option',
  4955. '*/select',
  4956. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4957. $hoursRegex,
  4958. array('option' => array('value' => '')),
  4959. '/option',
  4960. '*/select',
  4961. ':',
  4962. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4963. $minutesRegex,
  4964. array('option' => array('value' => '')),
  4965. '/option',
  4966. '*/select',
  4967. ' ',
  4968. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4969. $meridianRegex,
  4970. array('option' => array('value' => '')),
  4971. '/option',
  4972. '*/select'
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $result = $this->Form->dateTime('Contact.2.updated');
  4976. $expected = array(
  4977. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4978. $daysRegex,
  4979. array('option' => array('value' => '')),
  4980. '/option',
  4981. '*/select',
  4982. '-',
  4983. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4984. $monthsRegex,
  4985. array('option' => array('value' => '')),
  4986. '/option',
  4987. '*/select',
  4988. '-',
  4989. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4990. $yearsRegex,
  4991. array('option' => array('value' => '')),
  4992. '/option',
  4993. '*/select',
  4994. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4995. $hoursRegex,
  4996. array('option' => array('value' => '')),
  4997. '/option',
  4998. '*/select',
  4999. ':',
  5000. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5001. $minutesRegex,
  5002. array('option' => array('value' => '')),
  5003. '/option',
  5004. '*/select',
  5005. ' ',
  5006. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5007. $meridianRegex,
  5008. array('option' => array('value' => '')),
  5009. '/option',
  5010. '*/select'
  5011. );
  5012. $this->assertTags($result, $expected);
  5013. }
  5014. /**
  5015. * When changing the date format, the label should always focus the first select box when
  5016. * clicked.
  5017. *
  5018. * @return void
  5019. */
  5020. public function testDateTimeLabelIdMatchesFirstInput() {
  5021. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5022. $this->assertContains('label for="ModelDateMonth"', $result);
  5023. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5024. $this->assertContains('label for="ModelDateDay"', $result);
  5025. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5026. $this->assertContains('label for="ModelDateYear"', $result);
  5027. }
  5028. /**
  5029. * testMonth method
  5030. *
  5031. * @return void
  5032. */
  5033. public function testMonth() {
  5034. $result = $this->Form->month('Model.field');
  5035. $expected = array(
  5036. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5037. array('option' => array('value' => '')),
  5038. '/option',
  5039. array('option' => array('value' => '01')),
  5040. date('F', strtotime('2008-01-01 00:00:00')),
  5041. '/option',
  5042. array('option' => array('value' => '02')),
  5043. date('F', strtotime('2008-02-01 00:00:00')),
  5044. '/option',
  5045. '*/select',
  5046. );
  5047. $this->assertTags($result, $expected);
  5048. $result = $this->Form->month('Model.field', array('empty' => true));
  5049. $expected = array(
  5050. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5051. array('option' => array('value' => '')),
  5052. '/option',
  5053. array('option' => array('value' => '01')),
  5054. date('F', strtotime('2008-01-01 00:00:00')),
  5055. '/option',
  5056. array('option' => array('value' => '02')),
  5057. date('F', strtotime('2008-02-01 00:00:00')),
  5058. '/option',
  5059. '*/select',
  5060. );
  5061. $this->assertTags($result, $expected);
  5062. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5063. $expected = array(
  5064. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5065. array('option' => array('value' => '')),
  5066. '/option',
  5067. array('option' => array('value' => '01')),
  5068. '01',
  5069. '/option',
  5070. array('option' => array('value' => '02')),
  5071. '02',
  5072. '/option',
  5073. '*/select',
  5074. );
  5075. $this->assertTags($result, $expected);
  5076. $monthNames = array(
  5077. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5078. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5079. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5080. $expected = array(
  5081. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5082. array('option' => array('value' => '')),
  5083. '/option',
  5084. array('option' => array('value' => '01')),
  5085. 'Jan',
  5086. '/option',
  5087. array('option' => array('value' => '02')),
  5088. 'Feb',
  5089. '/option',
  5090. '*/select',
  5091. );
  5092. $this->assertTags($result, $expected);
  5093. }
  5094. /**
  5095. * testDay method
  5096. *
  5097. * @return void
  5098. */
  5099. public function testDay() {
  5100. extract($this->dateRegex);
  5101. $result = $this->Form->day('Model.field', array('value' => false));
  5102. $expected = array(
  5103. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5104. array('option' => array('value' => '')),
  5105. '/option',
  5106. array('option' => array('value' => '01')),
  5107. '1',
  5108. '/option',
  5109. array('option' => array('value' => '02')),
  5110. '2',
  5111. '/option',
  5112. $daysRegex,
  5113. '/select',
  5114. );
  5115. $this->assertTags($result, $expected);
  5116. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5117. $result = $this->Form->day('Model.field');
  5118. $expected = array(
  5119. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5120. array('option' => array('value' => '')),
  5121. '/option',
  5122. array('option' => array('value' => '01')),
  5123. '1',
  5124. '/option',
  5125. array('option' => array('value' => '02')),
  5126. '2',
  5127. '/option',
  5128. $daysRegex,
  5129. array('option' => array('value' => '10', 'selected' => 'selected')),
  5130. '10',
  5131. '/option',
  5132. $daysRegex,
  5133. '/select',
  5134. );
  5135. $this->assertTags($result, $expected);
  5136. $this->Form->request->data['Model']['field'] = '';
  5137. $result = $this->Form->day('Model.field', array('value' => '10'));
  5138. $expected = array(
  5139. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. array('option' => array('value' => '01')),
  5143. '1',
  5144. '/option',
  5145. array('option' => array('value' => '02')),
  5146. '2',
  5147. '/option',
  5148. $daysRegex,
  5149. array('option' => array('value' => '10', 'selected' => 'selected')),
  5150. '10',
  5151. '/option',
  5152. $daysRegex,
  5153. '/select',
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5157. $result = $this->Form->day('Model.field', array('value' => true));
  5158. $expected = array(
  5159. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5160. array('option' => array('value' => '')),
  5161. '/option',
  5162. array('option' => array('value' => '01')),
  5163. '1',
  5164. '/option',
  5165. array('option' => array('value' => '02')),
  5166. '2',
  5167. '/option',
  5168. $daysRegex,
  5169. array('option' => array('value' => '10', 'selected' => 'selected')),
  5170. '10',
  5171. '/option',
  5172. $daysRegex,
  5173. '/select',
  5174. );
  5175. $this->assertTags($result, $expected);
  5176. }
  5177. /**
  5178. * testMinute method
  5179. *
  5180. * @return void
  5181. */
  5182. public function testMinute() {
  5183. extract($this->dateRegex);
  5184. $result = $this->Form->minute('Model.field');
  5185. $expected = array(
  5186. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5187. array('option' => array('value' => '')),
  5188. '/option',
  5189. array('option' => array('value' => '00')),
  5190. '00',
  5191. '/option',
  5192. array('option' => array('value' => '01')),
  5193. '01',
  5194. '/option',
  5195. array('option' => array('value' => '02')),
  5196. '02',
  5197. '/option',
  5198. $minutesRegex,
  5199. '/select',
  5200. );
  5201. $this->assertTags($result, $expected);
  5202. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5203. $result = $this->Form->minute('Model.field');
  5204. $expected = array(
  5205. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. array('option' => array('value' => '00')),
  5209. '00',
  5210. '/option',
  5211. array('option' => array('value' => '01')),
  5212. '01',
  5213. '/option',
  5214. array('option' => array('value' => '02')),
  5215. '02',
  5216. '/option',
  5217. $minutesRegex,
  5218. array('option' => array('value' => '12', 'selected' => 'selected')),
  5219. '12',
  5220. '/option',
  5221. $minutesRegex,
  5222. '/select',
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->Form->request->data['Model']['field'] = '';
  5226. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '00')),
  5232. '00',
  5233. '/option',
  5234. array('option' => array('value' => '05')),
  5235. '05',
  5236. '/option',
  5237. array('option' => array('value' => '10')),
  5238. '10',
  5239. '/option',
  5240. $minutesRegex,
  5241. '/select',
  5242. );
  5243. $this->assertTags($result, $expected);
  5244. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5245. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5246. $expected = array(
  5247. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5248. array('option' => array('value' => '')),
  5249. '/option',
  5250. array('option' => array('value' => '00')),
  5251. '00',
  5252. '/option',
  5253. array('option' => array('value' => '05')),
  5254. '05',
  5255. '/option',
  5256. array('option' => array('value' => '10', 'selected' => 'selected')),
  5257. '10',
  5258. '/option',
  5259. $minutesRegex,
  5260. '/select',
  5261. );
  5262. $this->assertTags($result, $expected);
  5263. }
  5264. /**
  5265. * testHour method
  5266. *
  5267. * @return void
  5268. */
  5269. public function testHour() {
  5270. extract($this->dateRegex);
  5271. $result = $this->Form->hour('Model.field', false);
  5272. $expected = array(
  5273. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5274. array('option' => array('value' => '')),
  5275. '/option',
  5276. array('option' => array('value' => '01')),
  5277. '1',
  5278. '/option',
  5279. array('option' => array('value' => '02')),
  5280. '2',
  5281. '/option',
  5282. $hoursRegex,
  5283. '/select',
  5284. );
  5285. $this->assertTags($result, $expected);
  5286. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5287. $result = $this->Form->hour('Model.field', false);
  5288. $expected = array(
  5289. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5290. array('option' => array('value' => '')),
  5291. '/option',
  5292. array('option' => array('value' => '01')),
  5293. '1',
  5294. '/option',
  5295. array('option' => array('value' => '02')),
  5296. '2',
  5297. '/option',
  5298. $hoursRegex,
  5299. array('option' => array('value' => '12', 'selected' => 'selected')),
  5300. '12',
  5301. '/option',
  5302. '/select',
  5303. );
  5304. $this->assertTags($result, $expected);
  5305. $this->Form->request->data['Model']['field'] = '';
  5306. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5307. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5308. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5309. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5310. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5311. $result = $this->Form->hour('Model.field', true);
  5312. $expected = array(
  5313. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5314. array('option' => array('value' => '')),
  5315. '/option',
  5316. array('option' => array('value' => '00', 'selected' => 'selected')),
  5317. '0',
  5318. '/option',
  5319. array('option' => array('value' => '01')),
  5320. '1',
  5321. '/option',
  5322. array('option' => array('value' => '02')),
  5323. '2',
  5324. '/option',
  5325. $hoursRegex,
  5326. '/select',
  5327. );
  5328. $this->assertTags($result, $expected);
  5329. unset($this->Form->request->data['Model']['field']);
  5330. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5331. $thisHour = date('H');
  5332. $optValue = date('G');
  5333. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5334. }
  5335. /**
  5336. * testYear method
  5337. *
  5338. * @return void
  5339. */
  5340. public function testYear() {
  5341. $result = $this->Form->year('Model.field', 2006, 2007);
  5342. $expected = array(
  5343. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5344. array('option' => array('value' => '')),
  5345. '/option',
  5346. array('option' => array('value' => '2007')),
  5347. '2007',
  5348. '/option',
  5349. array('option' => array('value' => '2006')),
  5350. '2006',
  5351. '/option',
  5352. '/select',
  5353. );
  5354. $this->assertTags($result, $expected);
  5355. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5356. $expected = array(
  5357. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5358. array('option' => array('value' => '')),
  5359. '/option',
  5360. array('option' => array('value' => '2006')),
  5361. '2006',
  5362. '/option',
  5363. array('option' => array('value' => '2007')),
  5364. '2007',
  5365. '/option',
  5366. '/select',
  5367. );
  5368. $this->assertTags($result, $expected);
  5369. $this->request->data['Contact']['published'] = '';
  5370. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5371. $expected = array(
  5372. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5373. array('option' => array('value' => '')),
  5374. '/option',
  5375. array('option' => array('value' => '2007')),
  5376. '2007',
  5377. '/option',
  5378. array('option' => array('value' => '2006')),
  5379. '2006',
  5380. '/option',
  5381. '/select',
  5382. );
  5383. $this->assertTags($result, $expected);
  5384. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5385. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5386. $expected = array(
  5387. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5388. array('option' => array('value' => '2007')),
  5389. '2007',
  5390. '/option',
  5391. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5392. '2006',
  5393. '/option',
  5394. '/select',
  5395. );
  5396. $this->assertTags($result, $expected);
  5397. $this->Form->request->data['Contact']['published'] = '';
  5398. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5399. $expected = array(
  5400. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5401. array('option' => array('value' => '')),
  5402. '/option',
  5403. array('option' => array('value' => '2007')),
  5404. '2007',
  5405. '/option',
  5406. array('option' => array('value' => '2006')),
  5407. '2006',
  5408. '/option',
  5409. '/select',
  5410. );
  5411. $this->assertTags($result, $expected);
  5412. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5413. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5414. $expected = array(
  5415. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5416. array('option' => array('value' => '2007')),
  5417. '2007',
  5418. '/option',
  5419. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5420. '2006',
  5421. '/option',
  5422. '/select',
  5423. );
  5424. $this->assertTags($result, $expected);
  5425. $this->Form->request->data['Contact']['published'] = '';
  5426. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5427. $expected = array(
  5428. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5429. array('option' => array('value' => '')),
  5430. '/option',
  5431. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5432. '2007',
  5433. '/option',
  5434. array('option' => array('value' => '2006')),
  5435. '2006',
  5436. '/option',
  5437. '/select',
  5438. );
  5439. $this->assertTags($result, $expected);
  5440. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5441. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5442. $expected = array(
  5443. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5444. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5445. '2007',
  5446. '/option',
  5447. array('option' => array('value' => '2006')),
  5448. '2006',
  5449. '/option',
  5450. '/select',
  5451. );
  5452. $this->assertTags($result, $expected);
  5453. $this->Form->request->data['Contact']['published'] = '';
  5454. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5455. $expected = array(
  5456. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5457. array('option' => array('value' => '2008')),
  5458. '2008',
  5459. '/option',
  5460. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5461. '2007',
  5462. '/option',
  5463. array('option' => array('value' => '2006')),
  5464. '2006',
  5465. '/option',
  5466. '/select',
  5467. );
  5468. $this->assertTags($result, $expected);
  5469. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5470. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5471. $expected = array(
  5472. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5473. array('option' => array('value' => '2008')),
  5474. '2008',
  5475. '/option',
  5476. array('option' => array('value' => '2007')),
  5477. '2007',
  5478. '/option',
  5479. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5480. '2006',
  5481. '/option',
  5482. '/select',
  5483. );
  5484. $this->assertTags($result, $expected);
  5485. $this->Form->request->data = array();
  5486. $this->Form->create('Contact');
  5487. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5488. $expected = array(
  5489. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5490. array('option' => array('value' => '2008')),
  5491. '2008',
  5492. '/option',
  5493. array('option' => array('value' => '2007')),
  5494. '2007',
  5495. '/option',
  5496. array('option' => array('value' => '2006')),
  5497. '2006',
  5498. '/option',
  5499. '/select',
  5500. );
  5501. $this->assertTags($result, $expected);
  5502. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5503. $this->assertContains('data[Contact][published][year]', $result);
  5504. }
  5505. /**
  5506. * testTextArea method
  5507. *
  5508. * @return void
  5509. */
  5510. public function testTextArea() {
  5511. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5512. $result = $this->Form->textarea('Model.field');
  5513. $expected = array(
  5514. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5515. 'some test data',
  5516. '/textarea',
  5517. );
  5518. $this->assertTags($result, $expected);
  5519. $result = $this->Form->textarea('Model.tmp');
  5520. $expected = array(
  5521. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5522. '/textarea',
  5523. );
  5524. $this->assertTags($result, $expected);
  5525. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5526. $result = $this->Form->textarea('Model.field');
  5527. $expected = array(
  5528. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5529. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5530. '/textarea',
  5531. );
  5532. $this->assertTags($result, $expected);
  5533. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5534. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5535. $expected = array(
  5536. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5537. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5538. '/textarea',
  5539. );
  5540. $this->assertTags($result, $expected);
  5541. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5542. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5543. $expected = array(
  5544. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5545. '/textarea'
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. }
  5549. /**
  5550. * testTextAreaWithStupidCharacters method
  5551. *
  5552. * test text area with non-ascii characters
  5553. *
  5554. * @return void
  5555. */
  5556. public function testTextAreaWithStupidCharacters() {
  5557. $this->loadFixtures('Post');
  5558. $result = $this->Form->input('Post.content', array(
  5559. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5560. ));
  5561. $expected = array(
  5562. 'div' => array('class' => 'input text'),
  5563. 'label' => array('for' => 'PostContent'),
  5564. 'Current Text',
  5565. '/label',
  5566. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5567. 'GREAT®',
  5568. '/textarea',
  5569. '/div'
  5570. );
  5571. $this->assertTags($result, $expected);
  5572. }
  5573. /**
  5574. * testHiddenField method
  5575. *
  5576. * @return void
  5577. */
  5578. public function testHiddenField() {
  5579. $Contact = ClassRegistry::getObject('Contact');
  5580. $Contact->validationErrors['field'] = 1;
  5581. $this->Form->request->data['Contact']['field'] = 'test';
  5582. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5583. $this->assertTags($result, array(
  5584. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5585. );
  5586. }
  5587. /**
  5588. * testFileUploadField method
  5589. *
  5590. * @return void
  5591. */
  5592. public function testFileUploadField() {
  5593. $result = $this->Form->file('Model.upload');
  5594. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5595. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5596. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5597. $expected = array(
  5598. 'div' => array('class' => 'input file'),
  5599. 'label' => array('for' => 'ModelUpload'),
  5600. 'Upload',
  5601. '/label',
  5602. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5603. '/div'
  5604. );
  5605. $this->assertTags($result, $expected);
  5606. }
  5607. /**
  5608. * test File upload input on a model not used in create();
  5609. *
  5610. * @return void
  5611. */
  5612. public function testFileUploadOnOtherModel() {
  5613. $this->Form->create('ValidateUser', array('type' => 'file'));
  5614. $result = $this->Form->file('ValidateProfile.city');
  5615. $expected = array(
  5616. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5617. );
  5618. $this->assertTags($result, $expected);
  5619. }
  5620. /**
  5621. * testButton method
  5622. *
  5623. * @return void
  5624. */
  5625. public function testButton() {
  5626. $result = $this->Form->button('Hi');
  5627. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5628. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5629. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5630. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5631. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5632. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5633. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5634. $result = $this->Form->button('No type', array('type' => false));
  5635. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5636. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5637. $this->assertNotRegExp('/\&039/', $result);
  5638. }
  5639. /**
  5640. * Test that button() makes unlocked fields by default.
  5641. *
  5642. * @return void
  5643. */
  5644. public function testButtonUnlockedByDefault() {
  5645. $this->Form->request->params['_Token']['key'] = 'secured';
  5646. $this->Form->button('Save', array('name' => 'save'));
  5647. $this->Form->button('Clear');
  5648. $result = $this->Form->unlockField();
  5649. $this->assertEquals(array('save'), $result);
  5650. }
  5651. /**
  5652. * testPostButton method
  5653. *
  5654. * @return void
  5655. */
  5656. public function testPostButton() {
  5657. $result = $this->Form->postButton('Hi', '/controller/action');
  5658. $this->assertTags($result, array(
  5659. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5660. 'div' => array('style' => 'display:none;'),
  5661. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5662. '/div',
  5663. 'button' => array('type' => 'submit'),
  5664. 'Hi',
  5665. '/button',
  5666. '/form'
  5667. ));
  5668. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5669. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5670. }
  5671. /**
  5672. * Test that postButton adds _Token fields.
  5673. *
  5674. * @return void
  5675. */
  5676. public function testSecurePostButton() {
  5677. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5678. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5679. $expected = array(
  5680. 'form' => array(
  5681. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5682. ),
  5683. array('div' => array('style' => 'display:none;')),
  5684. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5685. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5686. '/div',
  5687. 'button' => array('type' => 'submit'),
  5688. 'Delete',
  5689. '/button',
  5690. array('div' => array('style' => 'display:none;')),
  5691. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5692. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5693. '/div',
  5694. '/form',
  5695. );
  5696. $this->assertTags($result, $expected);
  5697. }
  5698. /**
  5699. * testPostLink method
  5700. *
  5701. * @return void
  5702. */
  5703. public function testPostLink() {
  5704. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5705. $this->assertTags($result, array(
  5706. 'form' => array(
  5707. 'method' => 'post', 'action' => '/posts/delete/1',
  5708. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5709. ),
  5710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5711. '/form',
  5712. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5713. 'Delete',
  5714. '/a'
  5715. ));
  5716. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5717. $this->assertTags($result, array(
  5718. 'form' => array(
  5719. 'method' => 'post', 'action' => '/posts/delete/1',
  5720. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5721. ),
  5722. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5723. '/form',
  5724. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5725. 'Delete',
  5726. '/a'
  5727. ));
  5728. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5729. $this->assertTags($result, array(
  5730. 'form' => array(
  5731. 'method' => 'post', 'action' => '/posts/delete/1',
  5732. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5733. ),
  5734. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5735. '/form',
  5736. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5737. 'Delete',
  5738. '/a'
  5739. ));
  5740. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5741. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5742. }
  5743. /**
  5744. * Test that postLink adds _Token fields.
  5745. *
  5746. * @return void
  5747. */
  5748. public function testSecurePostLink() {
  5749. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5750. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5751. $expected = array(
  5752. 'form' => array(
  5753. 'method' => 'post', 'action' => '/posts/delete/1',
  5754. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5755. ),
  5756. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5757. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5758. 'div' => array('style' => 'display:none;'),
  5759. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5760. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5761. '/div',
  5762. '/form',
  5763. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5764. 'Delete',
  5765. '/a'
  5766. );
  5767. $this->assertTags($result, $expected);
  5768. }
  5769. /**
  5770. * testSubmitButton method
  5771. *
  5772. * @return void
  5773. */
  5774. public function testSubmitButton() {
  5775. $result = $this->Form->submit('');
  5776. $expected = array(
  5777. 'div' => array('class' => 'submit'),
  5778. 'input' => array('type' => 'submit', 'value' => ''),
  5779. '/div'
  5780. );
  5781. $this->assertTags($result, $expected);
  5782. $result = $this->Form->submit('Test Submit');
  5783. $expected = array(
  5784. 'div' => array('class' => 'submit'),
  5785. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5786. '/div'
  5787. );
  5788. $this->assertTags($result, $expected);
  5789. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5790. $expected = array(
  5791. 'span' => array('class' => 'submit'),
  5792. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5793. '/span'
  5794. );
  5795. $this->assertTags($result, $expected);
  5796. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5797. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5798. $this->assertTags($result, $expected);
  5799. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5800. $expected = array(
  5801. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5802. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5803. '/div'
  5804. );
  5805. $this->assertTags($result, $expected);
  5806. $result = $this->Form->submit('Next >');
  5807. $expected = array(
  5808. 'div' => array('class' => 'submit'),
  5809. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5810. '/div'
  5811. );
  5812. $this->assertTags($result, $expected);
  5813. $result = $this->Form->submit('Next >', array('escape' => false));
  5814. $expected = array(
  5815. 'div' => array('class' => 'submit'),
  5816. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5817. '/div'
  5818. );
  5819. $this->assertTags($result, $expected);
  5820. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5821. $expected = array(
  5822. 'div' => array('class' => 'submit'),
  5823. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5824. '/div'
  5825. );
  5826. $this->assertTags($result, $expected);
  5827. $before = '--before--';
  5828. $after = '--after--';
  5829. $result = $this->Form->submit('Test', array('before' => $before));
  5830. $expected = array(
  5831. 'div' => array('class' => 'submit'),
  5832. '--before--',
  5833. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5834. '/div'
  5835. );
  5836. $this->assertTags($result, $expected);
  5837. $result = $this->Form->submit('Test', array('after' => $after));
  5838. $expected = array(
  5839. 'div' => array('class' => 'submit'),
  5840. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5841. '--after--',
  5842. '/div'
  5843. );
  5844. $this->assertTags($result, $expected);
  5845. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5846. $expected = array(
  5847. 'div' => array('class' => 'submit'),
  5848. '--before--',
  5849. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5850. '--after--',
  5851. '/div'
  5852. );
  5853. $this->assertTags($result, $expected);
  5854. }
  5855. /**
  5856. * test image submit types.
  5857. *
  5858. * @return void
  5859. */
  5860. public function testSubmitImage() {
  5861. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5862. $expected = array(
  5863. 'div' => array('class' => 'submit'),
  5864. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5865. '/div'
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $result = $this->Form->submit('/relative/cake.power.gif');
  5869. $expected = array(
  5870. 'div' => array('class' => 'submit'),
  5871. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5872. '/div'
  5873. );
  5874. $this->assertTags($result, $expected);
  5875. $result = $this->Form->submit('cake.power.gif');
  5876. $expected = array(
  5877. 'div' => array('class' => 'submit'),
  5878. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5879. '/div'
  5880. );
  5881. $this->assertTags($result, $expected);
  5882. $result = $this->Form->submit('Not.an.image');
  5883. $expected = array(
  5884. 'div' => array('class' => 'submit'),
  5885. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5886. '/div'
  5887. );
  5888. $this->assertTags($result, $expected);
  5889. $after = '--after--';
  5890. $before = '--before--';
  5891. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5892. $expected = array(
  5893. 'div' => array('class' => 'submit'),
  5894. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5895. '--after--',
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5900. $expected = array(
  5901. 'div' => array('class' => 'submit'),
  5902. '--before--',
  5903. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5904. '/div'
  5905. );
  5906. $this->assertTags($result, $expected);
  5907. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5908. $expected = array(
  5909. 'div' => array('class' => 'submit'),
  5910. '--before--',
  5911. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5912. '--after--',
  5913. '/div'
  5914. );
  5915. $this->assertTags($result, $expected);
  5916. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5917. $expected = array(
  5918. 'div' => array('class' => 'submit'),
  5919. '--before--',
  5920. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5921. '--after--',
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. }
  5926. /**
  5927. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5928. * be submitted at a time.
  5929. *
  5930. * @return void
  5931. */
  5932. public function testSubmitUnlockedByDefault() {
  5933. $this->Form->request->params['_Token']['key'] = 'secured';
  5934. $this->Form->submit('Go go');
  5935. $this->Form->submit('Save', array('name' => 'save'));
  5936. $result = $this->Form->unlockField();
  5937. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5938. }
  5939. /**
  5940. * Test submit image with timestamps.
  5941. *
  5942. * @return void
  5943. */
  5944. public function testSubmitImageTimestamp() {
  5945. Configure::write('Asset.timestamp', 'force');
  5946. $result = $this->Form->submit('cake.power.gif');
  5947. $expected = array(
  5948. 'div' => array('class' => 'submit'),
  5949. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5950. '/div'
  5951. );
  5952. $this->assertTags($result, $expected);
  5953. }
  5954. /**
  5955. * test the create() method
  5956. *
  5957. * @return void
  5958. */
  5959. public function testCreate() {
  5960. $result = $this->Form->create('Contact');
  5961. $encoding = strtolower(Configure::read('App.encoding'));
  5962. $expected = array(
  5963. 'form' => array(
  5964. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5965. 'accept-charset' => $encoding
  5966. ),
  5967. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5968. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5969. '/div'
  5970. );
  5971. $this->assertTags($result, $expected);
  5972. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5973. $expected = array('form' => array(
  5974. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5975. 'accept-charset' => $encoding
  5976. ));
  5977. $this->assertTags($result, $expected);
  5978. $result = $this->Form->create('Contact', array('type' => 'get'));
  5979. $expected = array('form' => array(
  5980. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5981. 'accept-charset' => $encoding
  5982. ));
  5983. $this->assertTags($result, $expected);
  5984. $result = $this->Form->create('Contact', array('type' => 'put'));
  5985. $expected = array(
  5986. 'form' => array(
  5987. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5988. 'accept-charset' => $encoding
  5989. ),
  5990. 'div' => array('style' => 'display:none;'),
  5991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5992. '/div'
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. $result = $this->Form->create('Contact', array('type' => 'file'));
  5996. $expected = array(
  5997. 'form' => array(
  5998. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5999. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6000. ),
  6001. 'div' => array('style' => 'display:none;'),
  6002. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6003. '/div'
  6004. );
  6005. $this->assertTags($result, $expected);
  6006. $this->Form->request->data['Contact']['id'] = 1;
  6007. $this->Form->request->here = '/contacts/edit/1';
  6008. $this->Form->request['action'] = 'edit';
  6009. $result = $this->Form->create('Contact');
  6010. $expected = array(
  6011. 'form' => array(
  6012. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6013. 'accept-charset' => $encoding
  6014. ),
  6015. 'div' => array('style' => 'display:none;'),
  6016. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6017. '/div'
  6018. );
  6019. $this->assertTags($result, $expected);
  6020. $this->Form->request->data['Contact']['id'] = 1;
  6021. $this->Form->request->here = '/contacts/edit/1';
  6022. $this->Form->request['action'] = 'edit';
  6023. $result = $this->Form->create('Contact', array('type' => 'file'));
  6024. $expected = array(
  6025. 'form' => array(
  6026. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6027. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6028. ),
  6029. 'div' => array('style' => 'display:none;'),
  6030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6031. '/div'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6035. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6036. $expected = array(
  6037. 'form' => array(
  6038. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6039. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6040. ),
  6041. 'div' => array('style' => 'display:none;'),
  6042. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6043. '/div'
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6047. $expected = array(
  6048. 'form' => array(
  6049. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6050. 'accept-charset' => $encoding
  6051. ),
  6052. 'div' => array('style' => 'display:none;'),
  6053. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6054. '/div'
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. $this->Form->request['action'] = 'add';
  6058. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6059. $expected = array(
  6060. 'form' => array(
  6061. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6062. 'accept-charset' => $encoding
  6063. ),
  6064. 'div' => array('style' => 'display:none;'),
  6065. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6066. '/div'
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. $result = $this->Form->create('User', array('action' => 'login'));
  6070. $expected = array(
  6071. 'form' => array(
  6072. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6073. 'accept-charset' => $encoding
  6074. ),
  6075. 'div' => array('style' => 'display:none;'),
  6076. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6077. '/div'
  6078. );
  6079. $this->assertTags($result, $expected);
  6080. $result = $this->Form->create('User', array('url' => '/users/login'));
  6081. $expected = array(
  6082. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6083. 'div' => array('style' => 'display:none;'),
  6084. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6085. '/div'
  6086. );
  6087. $this->assertTags($result, $expected);
  6088. $this->Form->request['controller'] = 'pages';
  6089. $result = $this->Form->create('User', array('action' => 'signup'));
  6090. $expected = array(
  6091. 'form' => array(
  6092. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6093. 'accept-charset' => $encoding
  6094. ),
  6095. 'div' => array('style' => 'display:none;'),
  6096. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6097. '/div'
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. $this->Form->request->data = array();
  6101. $this->Form->request['controller'] = 'contacts';
  6102. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6103. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6104. $expected = array(
  6105. 'form' => array(
  6106. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6107. 'accept-charset' => 'utf-8'
  6108. ),
  6109. 'div' => array('style' => 'display:none;'),
  6110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6111. '/div'
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. }
  6115. /**
  6116. * Test the onsubmit option for create()
  6117. *
  6118. * @return void
  6119. */
  6120. public function testCreateOnSubmit() {
  6121. $this->Form->request->data = array();
  6122. $this->Form->request['controller'] = 'contacts';
  6123. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6124. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6125. $expected = array(
  6126. 'form' => array(
  6127. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6128. 'accept-charset' => 'utf-8'
  6129. ),
  6130. 'div' => array('style' => 'display:none;'),
  6131. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6132. '/div'
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->request->data = array();
  6136. $this->Form->request['controller'] = 'contacts';
  6137. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6138. $result = $this->Form->create(array(
  6139. 'url' => array('action' => 'index', 'param'),
  6140. 'default' => false,
  6141. 'onsubmit' => 'someFunction();'
  6142. ));
  6143. $expected = array(
  6144. 'form' => array(
  6145. 'id' => 'ContactAddForm', 'method' => 'post',
  6146. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6147. 'action' => '/contacts/index/param',
  6148. 'accept-charset' => 'utf-8'
  6149. ),
  6150. 'div' => array('style' => 'display:none;'),
  6151. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6152. '/div'
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. }
  6156. /**
  6157. * test create() with automatic url generation
  6158. *
  6159. * @return void
  6160. */
  6161. public function testCreateAutoUrl() {
  6162. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6163. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6164. $this->Form->request->base = '/base_url';
  6165. $result = $this->Form->create('Contact');
  6166. $expected = array(
  6167. 'form' => array(
  6168. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6169. 'accept-charset' => 'utf-8'
  6170. ),
  6171. 'div' => array('style' => 'display:none;'),
  6172. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6173. '/div'
  6174. );
  6175. $this->assertTags($result, $expected);
  6176. $this->Form->request['action'] = 'delete';
  6177. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6178. $this->Form->request->base = '/base_url';
  6179. $result = $this->Form->create('Contact');
  6180. $expected = array(
  6181. 'form' => array(
  6182. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6183. 'accept-charset' => 'utf-8'
  6184. ),
  6185. 'div' => array('style' => 'display:none;'),
  6186. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6187. '/div'
  6188. );
  6189. $this->assertTags($result, $expected);
  6190. }
  6191. /**
  6192. * test create() with a custom route
  6193. *
  6194. * @return void
  6195. */
  6196. public function testCreateCustomRoute() {
  6197. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6198. $encoding = strtolower(Configure::read('App.encoding'));
  6199. $result = $this->Form->create('User', array('action' => 'login'));
  6200. $expected = array(
  6201. 'form' => array(
  6202. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6203. 'accept-charset' => $encoding
  6204. ),
  6205. 'div' => array('style' => 'display:none;'),
  6206. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6207. '/div'
  6208. );
  6209. $this->assertTags($result, $expected);
  6210. }
  6211. /**
  6212. * test that inputDefaults are stored and used.
  6213. *
  6214. * @return void
  6215. */
  6216. public function testCreateWithInputDefaults() {
  6217. $this->Form->create('User', array(
  6218. 'inputDefaults' => array(
  6219. 'div' => false,
  6220. 'label' => false,
  6221. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6222. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6223. )
  6224. ));
  6225. $result = $this->Form->input('username');
  6226. $expected = array(
  6227. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6228. );
  6229. $this->assertTags($result, $expected);
  6230. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6231. $expected = array(
  6232. 'div' => array('class' => 'input text'),
  6233. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6234. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6235. '/div'
  6236. );
  6237. $this->assertTags($result, $expected);
  6238. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6239. $expected = array(
  6240. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6241. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6242. );
  6243. $this->assertTags($result, $expected);
  6244. $this->Form->create('User', array(
  6245. 'inputDefaults' => array(
  6246. 'div' => false,
  6247. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6248. )
  6249. ));
  6250. $result = $this->Form->input('username', array('div' => true));
  6251. $expected = array(
  6252. 'div' => array('class' => 'input text'),
  6253. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6254. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6255. '/div'
  6256. );
  6257. $this->assertTags($result, $expected);
  6258. }
  6259. /**
  6260. * test automatic accept-charset overriding
  6261. *
  6262. * @return void
  6263. */
  6264. public function testCreateWithAcceptCharset() {
  6265. $result = $this->Form->create('UserForm', array(
  6266. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6267. )
  6268. );
  6269. $expected = array(
  6270. 'form' => array(
  6271. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6272. 'accept-charset' => 'iso-8859-1'
  6273. ),
  6274. 'div' => array('style' => 'display:none;'),
  6275. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6276. '/div'
  6277. );
  6278. $this->assertTags($result, $expected);
  6279. }
  6280. /**
  6281. * Test base form url when url param is passed with multiple parameters (&)
  6282. *
  6283. */
  6284. public function testCreateQuerystringrequest() {
  6285. $encoding = strtolower(Configure::read('App.encoding'));
  6286. $result = $this->Form->create('Contact', array(
  6287. 'type' => 'post',
  6288. 'escape' => false,
  6289. 'url' => array(
  6290. 'controller' => 'controller',
  6291. 'action' => 'action',
  6292. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6293. )
  6294. ));
  6295. $expected = array(
  6296. 'form' => array(
  6297. 'id' => 'ContactAddForm',
  6298. 'method' => 'post',
  6299. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6300. 'accept-charset' => $encoding
  6301. ),
  6302. 'div' => array('style' => 'display:none;'),
  6303. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6304. '/div'
  6305. );
  6306. $this->assertTags($result, $expected);
  6307. $result = $this->Form->create('Contact', array(
  6308. 'type' => 'post',
  6309. 'url' => array(
  6310. 'controller' => 'controller',
  6311. 'action' => 'action',
  6312. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6313. )
  6314. ));
  6315. $expected = array(
  6316. 'form' => array(
  6317. 'id' => 'ContactAddForm',
  6318. 'method' => 'post',
  6319. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6320. 'accept-charset' => $encoding
  6321. ),
  6322. 'div' => array('style' => 'display:none;'),
  6323. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6324. '/div'
  6325. );
  6326. $this->assertTags($result, $expected);
  6327. }
  6328. /**
  6329. * test that create() doesn't cause errors by multiple id's being in the primary key
  6330. * as could happen with multiple select or checkboxes.
  6331. *
  6332. * @return void
  6333. */
  6334. public function testCreateWithMultipleIdInData() {
  6335. $encoding = strtolower(Configure::read('App.encoding'));
  6336. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6337. $result = $this->Form->create('Contact');
  6338. $expected = array(
  6339. 'form' => array(
  6340. 'id' => 'ContactAddForm',
  6341. 'method' => 'post',
  6342. 'action' => '/contacts/add',
  6343. 'accept-charset' => $encoding
  6344. ),
  6345. 'div' => array('style' => 'display:none;'),
  6346. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6347. '/div'
  6348. );
  6349. $this->assertTags($result, $expected);
  6350. }
  6351. /**
  6352. * test that create() doesn't add in extra passed params.
  6353. *
  6354. * @return void
  6355. */
  6356. public function testCreatePassedArgs() {
  6357. $encoding = strtolower(Configure::read('App.encoding'));
  6358. $this->Form->request->data['Contact']['id'] = 1;
  6359. $result = $this->Form->create('Contact', array(
  6360. 'type' => 'post',
  6361. 'escape' => false,
  6362. 'url' => array(
  6363. 'action' => 'edit',
  6364. 'myparam'
  6365. )
  6366. ));
  6367. $expected = array(
  6368. 'form' => array(
  6369. 'id' => 'ContactAddForm',
  6370. 'method' => 'post',
  6371. 'action' => '/contacts/edit/myparam',
  6372. 'accept-charset' => $encoding
  6373. ),
  6374. 'div' => array('style' => 'display:none;'),
  6375. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6376. '/div'
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. }
  6380. /**
  6381. * test creating a get form, and get form inputs.
  6382. *
  6383. * @return void
  6384. */
  6385. public function testGetFormCreate() {
  6386. $encoding = strtolower(Configure::read('App.encoding'));
  6387. $result = $this->Form->create('Contact', array('type' => 'get'));
  6388. $this->assertTags($result, array('form' => array(
  6389. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6390. 'accept-charset' => $encoding
  6391. )));
  6392. $result = $this->Form->text('Contact.name');
  6393. $this->assertTags($result, array('input' => array(
  6394. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6395. )));
  6396. $result = $this->Form->password('password');
  6397. $this->assertTags($result, array('input' => array(
  6398. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6399. )));
  6400. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6401. $result = $this->Form->text('user_form');
  6402. $this->assertTags($result, array('input' => array(
  6403. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6404. )));
  6405. }
  6406. /**
  6407. * test get form, and inputs when the model param is false
  6408. *
  6409. * @return void
  6410. */
  6411. public function testGetFormWithFalseModel() {
  6412. $encoding = strtolower(Configure::read('App.encoding'));
  6413. $this->Form->request['controller'] = 'contact_test';
  6414. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6415. $expected = array('form' => array(
  6416. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6417. 'accept-charset' => $encoding
  6418. ));
  6419. $this->assertTags($result, $expected);
  6420. $result = $this->Form->text('reason');
  6421. $expected = array(
  6422. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. }
  6426. /**
  6427. * test that datetime() works with GET style forms.
  6428. *
  6429. * @return void
  6430. */
  6431. public function testDateTimeWithGetForms() {
  6432. extract($this->dateRegex);
  6433. $this->Form->create('Contact', array('type' => 'get'));
  6434. $result = $this->Form->datetime('created');
  6435. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6436. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6437. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6438. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6439. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6440. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6441. }
  6442. /**
  6443. * testEditFormWithData method
  6444. *
  6445. * test auto populating form elements from submitted data.
  6446. *
  6447. * @return void
  6448. */
  6449. public function testEditFormWithData() {
  6450. $this->Form->request->data = array('Person' => array(
  6451. 'id' => 1,
  6452. 'first_name' => 'Nate',
  6453. 'last_name' => 'Abele',
  6454. 'email' => 'nate@example.com'
  6455. ));
  6456. $this->Form->request->addParams(array(
  6457. 'models' => array('Person'),
  6458. 'controller' => 'people',
  6459. 'action' => 'add'
  6460. ));
  6461. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6462. $this->Form->create();
  6463. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6464. $expected = array(
  6465. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6466. 'select' => array(
  6467. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6468. ),
  6469. array('option' => array('value' => 1)), 'Nate', '/option',
  6470. array('option' => array('value' => 2)), 'Garrett', '/option',
  6471. array('option' => array('value' => 3)), 'Larry', '/option',
  6472. '/select'
  6473. );
  6474. $this->assertTags($result, $expected);
  6475. }
  6476. /**
  6477. * Test that required fields are created for various types of validation.
  6478. *
  6479. * @return void
  6480. */
  6481. public function testFormInputRequiredDetection() {
  6482. $this->Form->create('Contact');
  6483. $result = $this->Form->input('Contact.non_existing');
  6484. $expected = array(
  6485. 'div' => array('class' => 'input text'),
  6486. 'label' => array('for' => 'ContactNonExisting'),
  6487. 'Non Existing',
  6488. '/label',
  6489. 'input' => array(
  6490. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6491. 'id' => 'ContactNonExisting'
  6492. ),
  6493. '/div'
  6494. );
  6495. $this->assertTags($result, $expected);
  6496. $result = $this->Form->input('Contact.imrequired');
  6497. $expected = array(
  6498. 'div' => array('class' => 'input text required'),
  6499. 'label' => array('for' => 'ContactImrequired'),
  6500. 'Imrequired',
  6501. '/label',
  6502. 'input' => array(
  6503. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6504. 'id' => 'ContactImrequired'
  6505. ),
  6506. '/div'
  6507. );
  6508. $this->assertTags($result, $expected);
  6509. $result = $this->Form->input('Contact.imalsorequired');
  6510. $expected = array(
  6511. 'div' => array('class' => 'input text required'),
  6512. 'label' => array('for' => 'ContactImalsorequired'),
  6513. 'Imalsorequired',
  6514. '/label',
  6515. 'input' => array(
  6516. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6517. 'id' => 'ContactImalsorequired'
  6518. ),
  6519. '/div'
  6520. );
  6521. $this->assertTags($result, $expected);
  6522. $result = $this->Form->input('Contact.imrequiredtoo');
  6523. $expected = array(
  6524. 'div' => array('class' => 'input text required'),
  6525. 'label' => array('for' => 'ContactImrequiredtoo'),
  6526. 'Imrequiredtoo',
  6527. '/label',
  6528. 'input' => array(
  6529. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6530. 'id' => 'ContactImrequiredtoo'
  6531. ),
  6532. '/div'
  6533. );
  6534. $this->assertTags($result, $expected);
  6535. $result = $this->Form->input('Contact.required_one');
  6536. $expected = array(
  6537. 'div' => array('class' => 'input text required'),
  6538. 'label' => array('for' => 'ContactRequiredOne'),
  6539. 'Required One',
  6540. '/label',
  6541. 'input' => array(
  6542. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6543. 'id' => 'ContactRequiredOne'
  6544. ),
  6545. '/div'
  6546. );
  6547. $this->assertTags($result, $expected);
  6548. $result = $this->Form->input('Contact.string_required');
  6549. $expected = array(
  6550. 'div' => array('class' => 'input text required'),
  6551. 'label' => array('for' => 'ContactStringRequired'),
  6552. 'String Required',
  6553. '/label',
  6554. 'input' => array(
  6555. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6556. 'id' => 'ContactStringRequired'
  6557. ),
  6558. '/div'
  6559. );
  6560. $this->assertTags($result, $expected);
  6561. $result = $this->Form->input('Contact.imnotrequired');
  6562. $expected = array(
  6563. 'div' => array('class' => 'input text'),
  6564. 'label' => array('for' => 'ContactImnotrequired'),
  6565. 'Imnotrequired',
  6566. '/label',
  6567. 'input' => array(
  6568. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6569. 'id' => 'ContactImnotrequired'
  6570. ),
  6571. '/div'
  6572. );
  6573. $this->assertTags($result, $expected);
  6574. $result = $this->Form->input('Contact.imalsonotrequired');
  6575. $expected = array(
  6576. 'div' => array('class' => 'input text'),
  6577. 'label' => array('for' => 'ContactImalsonotrequired'),
  6578. 'Imalsonotrequired',
  6579. '/label',
  6580. 'input' => array(
  6581. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6582. 'id' => 'ContactImalsonotrequired'
  6583. ),
  6584. '/div'
  6585. );
  6586. $this->assertTags($result, $expected);
  6587. $result = $this->Form->input('Contact.imnotrequiredeither');
  6588. $expected = array(
  6589. 'div' => array('class' => 'input text'),
  6590. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6591. 'Imnotrequiredeither',
  6592. '/label',
  6593. 'input' => array(
  6594. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6595. 'id' => 'ContactImnotrequiredeither'
  6596. ),
  6597. '/div'
  6598. );
  6599. $this->assertTags($result, $expected);
  6600. $result = $this->Form->input('Contact.iamrequiredalways');
  6601. $expected = array(
  6602. 'div' => array('class' => 'input text required'),
  6603. 'label' => array('for' => 'ContactIamrequiredalways'),
  6604. 'Iamrequiredalways',
  6605. '/label',
  6606. 'input' => array(
  6607. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6608. 'id' => 'ContactIamrequiredalways'
  6609. ),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. }
  6614. /**
  6615. * testFormMagicInput method
  6616. *
  6617. * @return void
  6618. */
  6619. public function testFormMagicInput() {
  6620. $encoding = strtolower(Configure::read('App.encoding'));
  6621. $result = $this->Form->create('Contact');
  6622. $expected = array(
  6623. 'form' => array(
  6624. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6625. 'accept-charset' => $encoding
  6626. ),
  6627. 'div' => array('style' => 'display:none;'),
  6628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6629. '/div'
  6630. );
  6631. $this->assertTags($result, $expected);
  6632. $result = $this->Form->input('name');
  6633. $expected = array(
  6634. 'div' => array('class' => 'input text'),
  6635. 'label' => array('for' => 'ContactName'),
  6636. 'Name',
  6637. '/label',
  6638. 'input' => array(
  6639. 'type' => 'text', 'name' => 'data[Contact][name]',
  6640. 'id' => 'ContactName', 'maxlength' => '255'
  6641. ),
  6642. '/div'
  6643. );
  6644. $this->assertTags($result, $expected);
  6645. $result = $this->Form->input('non_existing_field_in_contact_model');
  6646. $expected = array(
  6647. 'div' => array('class' => 'input text'),
  6648. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6649. 'Non Existing Field In Contact Model',
  6650. '/label',
  6651. 'input' => array(
  6652. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6653. 'id' => 'ContactNonExistingFieldInContactModel'
  6654. ),
  6655. '/div'
  6656. );
  6657. $this->assertTags($result, $expected);
  6658. $result = $this->Form->input('Address.street');
  6659. $expected = array(
  6660. 'div' => array('class' => 'input text'),
  6661. 'label' => array('for' => 'AddressStreet'),
  6662. 'Street',
  6663. '/label',
  6664. 'input' => array(
  6665. 'type' => 'text', 'name' => 'data[Address][street]',
  6666. 'id' => 'AddressStreet'
  6667. ),
  6668. '/div'
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. $result = $this->Form->input('Address.non_existing_field_in_model');
  6672. $expected = array(
  6673. 'div' => array('class' => 'input text'),
  6674. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6675. 'Non Existing Field In Model',
  6676. '/label',
  6677. 'input' => array(
  6678. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6679. 'id' => 'AddressNonExistingFieldInModel'
  6680. ),
  6681. '/div'
  6682. );
  6683. $this->assertTags($result, $expected);
  6684. $result = $this->Form->input('name', array('div' => false));
  6685. $expected = array(
  6686. 'label' => array('for' => 'ContactName'),
  6687. 'Name',
  6688. '/label',
  6689. 'input' => array(
  6690. 'type' => 'text', 'name' => 'data[Contact][name]',
  6691. 'id' => 'ContactName', 'maxlength' => '255'
  6692. )
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. extract($this->dateRegex);
  6696. $now = strtotime('now');
  6697. $result = $this->Form->input('Contact.published', array('div' => false));
  6698. $expected = array(
  6699. 'label' => array('for' => 'ContactPublishedMonth'),
  6700. 'Published',
  6701. '/label',
  6702. array('select' => array(
  6703. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6704. )),
  6705. $monthsRegex,
  6706. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6707. date('F', $now),
  6708. '/option',
  6709. '*/select',
  6710. '-',
  6711. array('select' => array(
  6712. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6713. )),
  6714. $daysRegex,
  6715. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6716. date('j', $now),
  6717. '/option',
  6718. '*/select',
  6719. '-',
  6720. array('select' => array(
  6721. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6722. )),
  6723. $yearsRegex,
  6724. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6725. date('Y', $now),
  6726. '*/select'
  6727. );
  6728. $this->assertTags($result, $expected);
  6729. $result = $this->Form->input('Contact.updated', array('div' => false));
  6730. $expected = array(
  6731. 'label' => array('for' => 'ContactUpdatedMonth'),
  6732. 'Updated',
  6733. '/label',
  6734. array('select' => array(
  6735. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6736. )),
  6737. $monthsRegex,
  6738. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6739. date('F', $now),
  6740. '/option',
  6741. '*/select',
  6742. '-',
  6743. array('select' => array(
  6744. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6745. )),
  6746. $daysRegex,
  6747. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6748. date('j', $now),
  6749. '/option',
  6750. '*/select',
  6751. '-',
  6752. array('select' => array(
  6753. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6754. )),
  6755. $yearsRegex,
  6756. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6757. date('Y', $now),
  6758. '*/select'
  6759. );
  6760. $this->assertTags($result, $expected);
  6761. $result = $this->Form->input('UserForm.stuff');
  6762. $expected = array(
  6763. 'div' => array('class' => 'input text'),
  6764. 'label' => array('for' => 'UserFormStuff'),
  6765. 'Stuff',
  6766. '/label',
  6767. 'input' => array(
  6768. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6769. 'id' => 'UserFormStuff', 'maxlength' => 10
  6770. ),
  6771. '/div'
  6772. );
  6773. $this->assertTags($result, $expected);
  6774. }
  6775. /**
  6776. * testForMagicInputNonExistingNorValidated method
  6777. *
  6778. * @return void
  6779. */
  6780. public function testForMagicInputNonExistingNorValidated() {
  6781. $encoding = strtolower(Configure::read('App.encoding'));
  6782. $result = $this->Form->create('Contact');
  6783. $expected = array(
  6784. 'form' => array(
  6785. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6786. 'accept-charset' => $encoding
  6787. ),
  6788. 'div' => array('style' => 'display:none;'),
  6789. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6790. '/div'
  6791. );
  6792. $this->assertTags($result, $expected);
  6793. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6794. $expected = array(
  6795. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6796. 'Non Existing Nor Validated',
  6797. '/label',
  6798. 'input' => array(
  6799. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6800. 'id' => 'ContactNonExistingNorValidated'
  6801. )
  6802. );
  6803. $this->assertTags($result, $expected);
  6804. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6805. 'div' => false, 'value' => 'my value'
  6806. ));
  6807. $expected = array(
  6808. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6809. 'Non Existing Nor Validated',
  6810. '/label',
  6811. 'input' => array(
  6812. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6813. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6814. )
  6815. );
  6816. $this->assertTags($result, $expected);
  6817. $this->Form->request->data = array(
  6818. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6819. ));
  6820. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6821. $expected = array(
  6822. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6823. 'Non Existing Nor Validated',
  6824. '/label',
  6825. 'input' => array(
  6826. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6827. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6828. )
  6829. );
  6830. $this->assertTags($result, $expected);
  6831. }
  6832. /**
  6833. * testFormMagicInputLabel method
  6834. *
  6835. * @return void
  6836. */
  6837. public function testFormMagicInputLabel() {
  6838. $encoding = strtolower(Configure::read('App.encoding'));
  6839. $result = $this->Form->create('Contact');
  6840. $expected = array(
  6841. 'form' => array(
  6842. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6843. 'accept-charset' => $encoding
  6844. ),
  6845. 'div' => array('style' => 'display:none;'),
  6846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6851. $this->assertTags($result, array('input' => array(
  6852. 'name' => 'data[Contact][name]', 'type' => 'text',
  6853. 'id' => 'ContactName', 'maxlength' => '255')
  6854. ));
  6855. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6856. $expected = array(
  6857. 'label' => array('for' => 'ContactName'),
  6858. 'My label',
  6859. '/label',
  6860. 'input' => array(
  6861. 'type' => 'text', 'name' => 'data[Contact][name]',
  6862. 'id' => 'ContactName', 'maxlength' => '255'
  6863. )
  6864. );
  6865. $this->assertTags($result, $expected);
  6866. $result = $this->Form->input('Contact.name', array(
  6867. 'div' => false, 'label' => array('class' => 'mandatory')
  6868. ));
  6869. $expected = array(
  6870. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6871. 'Name',
  6872. '/label',
  6873. 'input' => array(
  6874. 'type' => 'text', 'name' => 'data[Contact][name]',
  6875. 'id' => 'ContactName', 'maxlength' => '255'
  6876. )
  6877. );
  6878. $this->assertTags($result, $expected);
  6879. $result = $this->Form->input('Contact.name', array(
  6880. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6881. ));
  6882. $expected = array(
  6883. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6884. 'My label',
  6885. '/label',
  6886. 'input' => array(
  6887. 'type' => 'text', 'name' => 'data[Contact][name]',
  6888. 'id' => 'ContactName', 'maxlength' => '255'
  6889. )
  6890. );
  6891. $this->assertTags($result, $expected);
  6892. $result = $this->Form->input('Contact.name', array(
  6893. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6894. ));
  6895. $expected = array(
  6896. 'label' => array('for' => 'my_id'),
  6897. 'Name',
  6898. '/label',
  6899. 'input' => array(
  6900. 'type' => 'text', 'name' => 'data[Contact][name]',
  6901. 'id' => 'my_id', 'maxlength' => '255'
  6902. )
  6903. );
  6904. $this->assertTags($result, $expected);
  6905. $result = $this->Form->input('1.id');
  6906. $this->assertTags($result, array('input' => array(
  6907. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6908. 'id' => 'Contact1Id'
  6909. )));
  6910. $result = $this->Form->input("1.name");
  6911. $expected = array(
  6912. 'div' => array('class' => 'input text'),
  6913. 'label' => array('for' => 'Contact1Name'),
  6914. 'Name',
  6915. '/label',
  6916. 'input' => array(
  6917. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6918. 'id' => 'Contact1Name', 'maxlength' => '255'
  6919. ),
  6920. '/div'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. $result = $this->Form->input('Contact.1.id');
  6924. $this->assertTags($result, array(
  6925. 'input' => array(
  6926. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6927. 'id' => 'Contact1Id'
  6928. )
  6929. ));
  6930. $result = $this->Form->input("Model.1.name");
  6931. $expected = array(
  6932. 'div' => array('class' => 'input text'),
  6933. 'label' => array('for' => 'Model1Name'),
  6934. 'Name',
  6935. '/label',
  6936. 'input' => array(
  6937. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6938. 'id' => 'Model1Name'
  6939. ),
  6940. '/div'
  6941. );
  6942. $this->assertTags($result, $expected);
  6943. }
  6944. /**
  6945. * testFormEnd method
  6946. *
  6947. * @return void
  6948. */
  6949. public function testFormEnd() {
  6950. $this->assertEquals('</form>', $this->Form->end());
  6951. $result = $this->Form->end('');
  6952. $expected = array(
  6953. 'div' => array('class' => 'submit'),
  6954. 'input' => array('type' => 'submit', 'value' => ''),
  6955. '/div',
  6956. '/form'
  6957. );
  6958. $this->assertTags($result, $expected);
  6959. $result = $this->Form->end(array('label' => ''));
  6960. $expected = array(
  6961. 'div' => array('class' => 'submit'),
  6962. 'input' => array('type' => 'submit', 'value' => ''),
  6963. '/div',
  6964. '/form'
  6965. );
  6966. $this->assertTags($result, $expected);
  6967. $result = $this->Form->end('save');
  6968. $expected = array(
  6969. 'div' => array('class' => 'submit'),
  6970. 'input' => array('type' => 'submit', 'value' => 'save'),
  6971. '/div',
  6972. '/form'
  6973. );
  6974. $this->assertTags($result, $expected);
  6975. $result = $this->Form->end(array('label' => 'save'));
  6976. $expected = array(
  6977. 'div' => array('class' => 'submit'),
  6978. 'input' => array('type' => 'submit', 'value' => 'save'),
  6979. '/div',
  6980. '/form'
  6981. );
  6982. $this->assertTags($result, $expected);
  6983. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6984. $expected = array(
  6985. 'div' => array('class' => 'submit'),
  6986. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6987. '/div',
  6988. '/form'
  6989. );
  6990. $this->assertTags($result, $expected);
  6991. $result = $this->Form->end(array('name' => 'Whatever'));
  6992. $expected = array(
  6993. 'div' => array('class' => 'submit'),
  6994. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6995. '/div',
  6996. '/form'
  6997. );
  6998. $this->assertTags($result, $expected);
  6999. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7000. $expected = array(
  7001. 'div' => array('class' => 'good'),
  7002. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7003. '/div',
  7004. '/form'
  7005. );
  7006. $this->assertTags($result, $expected);
  7007. $result = $this->Form->end(array(
  7008. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7009. ));
  7010. $expected = array(
  7011. 'div' => array('class' => 'good'),
  7012. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7013. '/div',
  7014. '/form'
  7015. );
  7016. $this->assertTags($result, $expected);
  7017. }
  7018. /**
  7019. * testMultipleFormWithIdFields method
  7020. *
  7021. * @return void
  7022. */
  7023. public function testMultipleFormWithIdFields() {
  7024. $this->Form->create('UserForm');
  7025. $result = $this->Form->input('id');
  7026. $this->assertTags($result, array('input' => array(
  7027. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7028. )));
  7029. $result = $this->Form->input('ValidateItem.id');
  7030. $this->assertTags($result, array('input' => array(
  7031. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7032. 'id' => 'ValidateItemId'
  7033. )));
  7034. $result = $this->Form->input('ValidateUser.id');
  7035. $this->assertTags($result, array('input' => array(
  7036. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7037. 'id' => 'ValidateUserId'
  7038. )));
  7039. }
  7040. /**
  7041. * testDbLessModel method
  7042. *
  7043. * @return void
  7044. */
  7045. public function testDbLessModel() {
  7046. $this->Form->create('TestMail');
  7047. $result = $this->Form->input('name');
  7048. $expected = array(
  7049. 'div' => array('class' => 'input text'),
  7050. 'label' => array('for' => 'TestMailName'),
  7051. 'Name',
  7052. '/label',
  7053. 'input' => array(
  7054. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7055. 'id' => 'TestMailName'
  7056. ),
  7057. '/div'
  7058. );
  7059. $this->assertTags($result, $expected);
  7060. ClassRegistry::init('TestMail');
  7061. $this->Form->create('TestMail');
  7062. $result = $this->Form->input('name');
  7063. $expected = array(
  7064. 'div' => array('class' => 'input text'),
  7065. 'label' => array('for' => 'TestMailName'),
  7066. 'Name',
  7067. '/label',
  7068. 'input' => array(
  7069. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7070. 'id' => 'TestMailName'
  7071. ),
  7072. '/div'
  7073. );
  7074. $this->assertTags($result, $expected);
  7075. }
  7076. /**
  7077. * testBrokenness method
  7078. *
  7079. * @return void
  7080. */
  7081. public function testBrokenness() {
  7082. /*
  7083. * #4 This test has two parents and four children. By default (as of r7117) both
  7084. * parents are show but the first parent is missing a child. This is the inconsistency
  7085. * in the default behaviour - one parent has all children, the other does not - dependent
  7086. * on the data values.
  7087. */
  7088. $result = $this->Form->select('Model.field', array(
  7089. 'Fred' => array(
  7090. 'freds_son_1' => 'Fred',
  7091. 'freds_son_2' => 'Freddie'
  7092. ),
  7093. 'Bert' => array(
  7094. 'berts_son_1' => 'Albert',
  7095. 'berts_son_2' => 'Bertie')
  7096. ),
  7097. array('showParents' => true, 'empty' => false)
  7098. );
  7099. $expected = array(
  7100. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7101. array('optgroup' => array('label' => 'Fred')),
  7102. array('option' => array('value' => 'freds_son_1')),
  7103. 'Fred',
  7104. '/option',
  7105. array('option' => array('value' => 'freds_son_2')),
  7106. 'Freddie',
  7107. '/option',
  7108. '/optgroup',
  7109. array('optgroup' => array('label' => 'Bert')),
  7110. array('option' => array('value' => 'berts_son_1')),
  7111. 'Albert',
  7112. '/option',
  7113. array('option' => array('value' => 'berts_son_2')),
  7114. 'Bertie',
  7115. '/option',
  7116. '/optgroup',
  7117. '/select'
  7118. );
  7119. $this->assertTags($result, $expected);
  7120. /*
  7121. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7122. * changed, so we should expect the same select list data, just with a different name
  7123. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7124. * This is where data corruption can occur, because when a select value is missing from
  7125. * a list a form will substitute the first value in the list - without the user knowing.
  7126. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7127. * affect the availability of 3 => 'Three' as a valid option.
  7128. */
  7129. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7130. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7131. ));
  7132. $result = $this->Form->select(
  7133. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7134. );
  7135. $expected = array(
  7136. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7137. array('option' => array('value' => 1)),
  7138. 'One',
  7139. '/option',
  7140. array('option' => array('value' => 2)),
  7141. 'Two',
  7142. '/option',
  7143. array('optgroup' => array('label' => 'Three')),
  7144. array('option' => array('value' => 3)),
  7145. 'Three',
  7146. '/option',
  7147. array('option' => array('value' => 4)),
  7148. 'Four',
  7149. '/option',
  7150. array('option' => array('value' => 5)),
  7151. 'Five',
  7152. '/option',
  7153. '/optgroup',
  7154. '/select'
  7155. );
  7156. $this->assertTags($result, $expected);
  7157. }
  7158. /**
  7159. * Test the generation of fields for a multi record form.
  7160. *
  7161. * @return void
  7162. */
  7163. public function testMultiRecordForm() {
  7164. $this->Form->create('ValidateProfile');
  7165. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7166. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7167. $expected = array(
  7168. 'div' => array('class' => 'input textarea'),
  7169. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7170. 'Name',
  7171. '/label',
  7172. 'textarea' => array(
  7173. 'id' => 'ValidateProfile1ValidateItem2Name',
  7174. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7175. 'cols' => 30,
  7176. 'rows' => 6
  7177. ),
  7178. 'Value',
  7179. '/textarea',
  7180. '/div'
  7181. );
  7182. $this->assertTags($result, $expected);
  7183. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7184. $expected = array(
  7185. 'div' => array('class' => 'input date'),
  7186. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7187. 'Created',
  7188. '/label',
  7189. array('select' => array(
  7190. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7191. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7192. )
  7193. ),
  7194. array('option' => array('value' => '')), '/option',
  7195. $this->dateRegex['monthsRegex'],
  7196. '/select', '-',
  7197. array('select' => array(
  7198. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7199. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7200. )
  7201. ),
  7202. array('option' => array('value' => '')), '/option',
  7203. $this->dateRegex['daysRegex'],
  7204. '/select', '-',
  7205. array('select' => array(
  7206. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7207. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7208. )
  7209. ),
  7210. array('option' => array('value' => '')), '/option',
  7211. $this->dateRegex['yearsRegex'],
  7212. '/select',
  7213. '/div'
  7214. );
  7215. $this->assertTags($result, $expected);
  7216. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7217. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7218. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7219. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7220. $expected = array(
  7221. 'div' => array('class' => 'input select error'),
  7222. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7223. 'Profile',
  7224. '/label',
  7225. 'select' => array(
  7226. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7227. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7228. 'class' => 'form-error'
  7229. ),
  7230. '/select',
  7231. array('div' => array('class' => 'error-message')),
  7232. 'Error',
  7233. '/div',
  7234. '/div'
  7235. );
  7236. $this->assertTags($result, $expected);
  7237. }
  7238. /**
  7239. * test the correct display of multi-record form validation errors.
  7240. *
  7241. * @return void
  7242. */
  7243. public function testMultiRecordFormValidationErrors() {
  7244. $this->Form->create('ValidateProfile');
  7245. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7246. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7247. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7248. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7249. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7250. $result = $this->Form->error('ValidateProfile.2.city');
  7251. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7252. $result = $this->Form->error('2.city');
  7253. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7254. }
  7255. /**
  7256. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7257. *
  7258. * @return void
  7259. */
  7260. public function testInputTemplate() {
  7261. $result = $this->Form->input('Contact.email', array(
  7262. 'type' => 'text', 'format' => array('input')
  7263. ));
  7264. $expected = array(
  7265. 'div' => array('class' => 'input text'),
  7266. 'input' => array(
  7267. 'type' => 'text', 'name' => 'data[Contact][email]',
  7268. 'id' => 'ContactEmail'
  7269. ),
  7270. '/div'
  7271. );
  7272. $this->assertTags($result, $expected);
  7273. $result = $this->Form->input('Contact.email', array(
  7274. 'type' => 'text', 'format' => array('input', 'label'),
  7275. 'label' => '<em>Email (required)</em>'
  7276. ));
  7277. $expected = array(
  7278. 'div' => array('class' => 'input text'),
  7279. array('input' => array(
  7280. 'type' => 'text', 'name' => 'data[Contact][email]',
  7281. 'id' => 'ContactEmail'
  7282. )),
  7283. 'label' => array('for' => 'ContactEmail'),
  7284. 'em' => array(),
  7285. 'Email (required)',
  7286. '/em',
  7287. '/label',
  7288. '/div'
  7289. );
  7290. $this->assertTags($result, $expected);
  7291. $result = $this->Form->input('Contact.email', array(
  7292. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7293. 'between' => '<div>Something in the middle</div>',
  7294. 'after' => '<span>Some text at the end</span>'
  7295. ));
  7296. $expected = array(
  7297. 'div' => array('class' => 'input text'),
  7298. array('input' => array(
  7299. 'type' => 'text', 'name' => 'data[Contact][email]',
  7300. 'id' => 'ContactEmail'
  7301. )),
  7302. array('div' => array()),
  7303. 'Something in the middle',
  7304. '/div',
  7305. 'label' => array('for' => 'ContactEmail'),
  7306. 'Email',
  7307. '/label',
  7308. 'span' => array(),
  7309. 'Some text at the end',
  7310. '/span',
  7311. '/div'
  7312. );
  7313. $this->assertTags($result, $expected);
  7314. $result = $this->Form->input('Contact.method', array(
  7315. 'type' => 'radio',
  7316. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7317. 'between' => 'I am between',
  7318. ));
  7319. $expected = array(
  7320. 'div' => array('class' => 'input radio'),
  7321. 'fieldset' => array(),
  7322. 'legend' => array(),
  7323. 'Method',
  7324. '/legend',
  7325. 'I am between',
  7326. 'input' => array(
  7327. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7328. 'value' => '', 'id' => 'ContactMethod_'
  7329. ),
  7330. array('input' => array(
  7331. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7332. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7333. )),
  7334. array('label' => array('for' => 'ContactMethodEmail')),
  7335. 'Email',
  7336. '/label',
  7337. array('input' => array(
  7338. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7339. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7340. )),
  7341. array('label' => array('for' => 'ContactMethodPigeon')),
  7342. 'Pigeon',
  7343. '/label',
  7344. '/fieldset',
  7345. '/div',
  7346. );
  7347. $this->assertTags($result, $expected);
  7348. }
  7349. /**
  7350. * test that some html5 inputs + FormHelper::__call() work
  7351. *
  7352. * @return void
  7353. */
  7354. public function testHtml5Inputs() {
  7355. $result = $this->Form->email('User.email');
  7356. $expected = array(
  7357. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7358. );
  7359. $this->assertTags($result, $expected);
  7360. $result = $this->Form->search('User.query');
  7361. $expected = array(
  7362. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7363. );
  7364. $this->assertTags($result, $expected);
  7365. $result = $this->Form->search('User.query', array('value' => 'test'));
  7366. $expected = array(
  7367. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7371. $expected = array(
  7372. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7373. );
  7374. $this->assertTags($result, $expected);
  7375. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7376. $expected = array(
  7377. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7378. );
  7379. $this->assertTags($result, $expected);
  7380. }
  7381. /**
  7382. *
  7383. * @expectedException CakeException
  7384. * @return void
  7385. */
  7386. public function testHtml5InputException() {
  7387. $this->Form->email();
  7388. }
  7389. /**
  7390. * Tests that a model can be loaded from the model names passed in the request object
  7391. *
  7392. * @return void
  7393. */
  7394. public function testIntrospectModelFromRequest() {
  7395. $this->loadFixtures('Post');
  7396. App::build(array(
  7397. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7398. ));
  7399. CakePlugin::load('TestPlugin');
  7400. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7401. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7402. $this->Form->create('TestPluginPost');
  7403. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7404. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7405. CakePlugin::unload();
  7406. App::build();
  7407. }
  7408. /**
  7409. * Tests that it is possible to set the validation errors directly in the helper for a field
  7410. *
  7411. * @return void
  7412. */
  7413. public function testCustomValidationErrors() {
  7414. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7415. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7416. $this->assertEquals('Badness!', $result);
  7417. }
  7418. /**
  7419. * Tests that the 'on' key validates as expected on create
  7420. *
  7421. * @return void
  7422. */
  7423. public function testRequiredOnCreate() {
  7424. $this->Form->create('Contact');
  7425. $result = $this->Form->input('Contact.imrequiredonupdate');
  7426. $expected = array(
  7427. 'div' => array('class' => 'input text'),
  7428. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7429. 'Imrequiredonupdate',
  7430. '/label',
  7431. 'input' => array(
  7432. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7433. 'id' => 'ContactImrequiredonupdate'
  7434. ),
  7435. '/div'
  7436. );
  7437. $this->assertTags($result, $expected);
  7438. $result = $this->Form->input('Contact.imrequiredoncreate');
  7439. $expected = array(
  7440. 'div' => array('class' => 'input text required'),
  7441. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7442. 'Imrequiredoncreate',
  7443. '/label',
  7444. 'input' => array(
  7445. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7446. 'id' => 'ContactImrequiredoncreate'
  7447. ),
  7448. '/div'
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $result = $this->Form->input('Contact.imrequiredonboth');
  7452. $expected = array(
  7453. 'div' => array('class' => 'input text required'),
  7454. 'label' => array('for' => 'ContactImrequiredonboth'),
  7455. 'Imrequiredonboth',
  7456. '/label',
  7457. 'input' => array(
  7458. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7459. 'id' => 'ContactImrequiredonboth'
  7460. ),
  7461. '/div'
  7462. );
  7463. $this->assertTags($result, $expected);
  7464. $result = $this->Form->input('Contact.imrequired');
  7465. $expected = array(
  7466. 'div' => array('class' => 'input text required'),
  7467. 'label' => array('for' => 'ContactImrequired'),
  7468. 'Imrequired',
  7469. '/label',
  7470. 'input' => array(
  7471. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7472. 'id' => 'ContactImrequired'
  7473. ),
  7474. '/div'
  7475. );
  7476. $this->assertTags($result, $expected);
  7477. }
  7478. /**
  7479. * Tests that the 'on' key validates as expected on update
  7480. *
  7481. * @return void
  7482. */
  7483. public function testRequiredOnUpdate() {
  7484. $this->Form->request->data['Contact']['id'] = 1;
  7485. $this->Form->create('Contact');
  7486. $result = $this->Form->input('Contact.imrequiredonupdate');
  7487. $expected = array(
  7488. 'div' => array('class' => 'input text required'),
  7489. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7490. 'Imrequiredonupdate',
  7491. '/label',
  7492. 'input' => array(
  7493. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7494. 'id' => 'ContactImrequiredonupdate'
  7495. ),
  7496. '/div'
  7497. );
  7498. $this->assertTags($result, $expected);
  7499. $result = $this->Form->input('Contact.imrequiredoncreate');
  7500. $expected = array(
  7501. 'div' => array('class' => 'input text'),
  7502. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7503. 'Imrequiredoncreate',
  7504. '/label',
  7505. 'input' => array(
  7506. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7507. 'id' => 'ContactImrequiredoncreate'
  7508. ),
  7509. '/div'
  7510. );
  7511. $this->assertTags($result, $expected);
  7512. $result = $this->Form->input('Contact.imrequiredonboth');
  7513. $expected = array(
  7514. 'div' => array('class' => 'input text required'),
  7515. 'label' => array('for' => 'ContactImrequiredonboth'),
  7516. 'Imrequiredonboth',
  7517. '/label',
  7518. 'input' => array(
  7519. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7520. 'id' => 'ContactImrequiredonboth'
  7521. ),
  7522. '/div'
  7523. );
  7524. $this->assertTags($result, $expected);
  7525. $result = $this->Form->input('Contact.imrequired');
  7526. $expected = array(
  7527. 'div' => array('class' => 'input text required'),
  7528. 'label' => array('for' => 'ContactImrequired'),
  7529. 'Imrequired',
  7530. '/label',
  7531. 'input' => array(
  7532. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7533. 'id' => 'ContactImrequired'
  7534. ),
  7535. '/div'
  7536. );
  7537. $this->assertTags($result, $expected);
  7538. }
  7539. /**
  7540. * Test inputDefaults setter and getter
  7541. *
  7542. * @return void
  7543. */
  7544. public function testInputDefaults() {
  7545. $this->Form->create('Contact');
  7546. $this->Form->inputDefaults(array(
  7547. 'label' => false,
  7548. 'div' => array(
  7549. 'style' => 'color: #000;'
  7550. )
  7551. ));
  7552. $result = $this->Form->input('Contact.field1');
  7553. $expected = array(
  7554. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7555. 'input' => array(
  7556. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7557. 'id' => 'ContactField1'
  7558. ),
  7559. '/div'
  7560. );
  7561. $this->assertTags($result, $expected);
  7562. $this->Form->inputDefaults(array(
  7563. 'div' => false,
  7564. 'label' => 'Label',
  7565. ));
  7566. $result = $this->Form->input('Contact.field1');
  7567. $expected = array(
  7568. 'label' => array('for' => 'ContactField1'),
  7569. 'Label',
  7570. '/label',
  7571. 'input' => array(
  7572. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7573. 'id' => 'ContactField1'
  7574. ),
  7575. );
  7576. $this->assertTags($result, $expected);
  7577. $this->Form->inputDefaults(array(
  7578. 'label' => false,
  7579. ), true);
  7580. $result = $this->Form->input('Contact.field1');
  7581. $expected = array(
  7582. 'input' => array(
  7583. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7584. 'id' => 'ContactField1'
  7585. ),
  7586. );
  7587. $this->assertTags($result, $expected);
  7588. $result = $this->Form->inputDefaults();
  7589. $expected = array(
  7590. 'div' => false,
  7591. 'label' => false,
  7592. );
  7593. $this->assertEqual($result, $expected);
  7594. }
  7595. }