FormHelperTest.php 262 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $hash .= ':' . 'Model.valid';
  710. $hash = urlencode($hash);
  711. $expected = array(
  712. 'div' => array('style' => 'display:none;'),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  715. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  716. )),
  717. array('input' => array(
  718. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  719. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  725. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  726. $result = $this->Form->secure($fields);
  727. $expected = array(
  728. 'div' => array('class' => 'hidden'),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  731. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  732. )),
  733. array('input' => array(
  734. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  735. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  736. )),
  737. '/div'
  738. );
  739. $this->assertTags($result, $expected);
  740. }
  741. /**
  742. * Tests correct generation of number fields for double and float fields
  743. *
  744. * @return void
  745. */
  746. public function testTextFieldGenerationForFloats() {
  747. $model = ClassRegistry::getObject('Contact');
  748. $model->setSchema(array('foo' => array(
  749. 'type' => 'float',
  750. 'null' => false,
  751. 'default' => null,
  752. 'length' => null
  753. )));
  754. $this->Form->create('Contact');
  755. $result = $this->Form->input('foo');
  756. $expected = array(
  757. 'div' => array('class' => 'input number'),
  758. 'label' => array('for' => 'ContactFoo'),
  759. 'Foo',
  760. '/label',
  761. array('input' => array(
  762. 'type' => 'number',
  763. 'name' => 'data[Contact][foo]',
  764. 'id' => 'ContactFoo',
  765. 'step' => 'any'
  766. )),
  767. '/div'
  768. );
  769. $this->assertTags($result, $expected);
  770. $result = $this->Form->input('foo', array('step' => 0.5));
  771. $expected = array(
  772. 'div' => array('class' => 'input number'),
  773. 'label' => array('for' => 'ContactFoo'),
  774. 'Foo',
  775. '/label',
  776. array('input' => array(
  777. 'type' => 'number',
  778. 'name' => 'data[Contact][foo]',
  779. 'id' => 'ContactFoo',
  780. 'step' => '0.5'
  781. )),
  782. '/div'
  783. );
  784. $this->assertTags($result, $expected);
  785. }
  786. /**
  787. * Tests correct generation of number fields for integer fields
  788. *
  789. * @access public
  790. * @return void
  791. */
  792. public function testTextFieldTypeNumberGenerationForIntegers() {
  793. $model = ClassRegistry::getObject('Contact');
  794. $model->setSchema(array('foo' => array(
  795. 'type' => 'integer',
  796. 'null' => false,
  797. 'default' => null,
  798. 'length' => null
  799. )));
  800. $this->Form->create('Contact');
  801. $result = $this->Form->input('foo');
  802. $expected = array(
  803. 'div' => array('class' => 'input number'),
  804. 'label' => array('for' => 'ContactFoo'),
  805. 'Foo',
  806. '/label',
  807. array('input' => array(
  808. 'type' => 'number', 'name' => 'data[Contact][foo]',
  809. 'id' => 'ContactFoo'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleFields method
  817. *
  818. * Test secure() with multiple row form. Ensure hash is correct.
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleFields() {
  823. $key = 'testKey';
  824. $fields = array(
  825. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  826. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  827. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  828. );
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $result = $this->Form->secure($fields);
  831. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  832. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  833. $expected = array(
  834. 'div' => array('style' => 'display:none;'),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  837. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  838. )),
  839. array('input' => array(
  840. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  841. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  842. )),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. }
  847. /**
  848. * testFormSecurityMultipleSubmitButtons
  849. *
  850. * test form submit generation and ensure that _Token is only created on end()
  851. *
  852. * @return void
  853. */
  854. public function testFormSecurityMultipleSubmitButtons() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('Addresses');
  858. $this->Form->input('Address.title');
  859. $this->Form->input('Address.first_name');
  860. $result = $this->Form->submit('Save', array('name' => 'save'));
  861. $expected = array(
  862. 'div' => array('class' => 'submit'),
  863. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  864. '/div',
  865. );
  866. $this->assertTags($result, $expected);
  867. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  868. $expected = array(
  869. 'div' => array('class' => 'submit'),
  870. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  871. '/div',
  872. );
  873. $this->assertTags($result, $expected);
  874. $result = $this->Form->end(null);
  875. $expected = array(
  876. 'div' => array('style' => 'display:none;'),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  879. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  880. )),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  883. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  884. )),
  885. '/div'
  886. );
  887. $this->assertTags($result, $expected);
  888. }
  889. /**
  890. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  891. *
  892. * @return void
  893. */
  894. public function testSecurityButtonNestedNamed() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('Addresses');
  898. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  899. $result = $this->Form->unlockField();
  900. $this->assertEquals(array('Address.button'), $result);
  901. }
  902. /**
  903. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  904. *
  905. * @return void
  906. */
  907. public function testSecuritySubmitNestedNamed() {
  908. $key = 'testKey';
  909. $this->Form->request['_Token'] = array('key' => $key);
  910. $this->Form->create('Addresses');
  911. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  912. $result = $this->Form->unlockField();
  913. $this->assertEquals(array('Address.button'), $result);
  914. }
  915. /**
  916. * Test that the correct fields are unlocked for image submits with no names.
  917. *
  918. * @return void
  919. */
  920. public function testSecuritySubmitImageNoName() {
  921. $key = 'testKey';
  922. $this->Form->request['_Token'] = array('key' => $key);
  923. $this->Form->create('User');
  924. $result = $this->Form->submit('save.png');
  925. $expected = array(
  926. 'div' => array('class' => 'submit'),
  927. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  932. }
  933. /**
  934. * Test that the correct fields are unlocked for image submits with names.
  935. *
  936. * @return void
  937. */
  938. public function testSecuritySubmitImageName() {
  939. $key = 'testKey';
  940. $this->Form->request['_Token'] = array('key' => $key);
  941. $this->Form->create('User');
  942. $result = $this->Form->submit('save.png', array('name' => 'test'));
  943. $expected = array(
  944. 'div' => array('class' => 'submit'),
  945. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  946. '/div'
  947. );
  948. $this->assertTags($result, $expected);
  949. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  950. }
  951. /**
  952. * testFormSecurityMultipleInputFields method
  953. *
  954. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityMultipleInputFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array('key' => $key);
  961. $this->Form->create('Addresses');
  962. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  963. $this->Form->input('Addresses.0.title');
  964. $this->Form->input('Addresses.0.first_name');
  965. $this->Form->input('Addresses.0.last_name');
  966. $this->Form->input('Addresses.0.address');
  967. $this->Form->input('Addresses.0.city');
  968. $this->Form->input('Addresses.0.phone');
  969. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  970. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  971. $this->Form->input('Addresses.1.title');
  972. $this->Form->input('Addresses.1.first_name');
  973. $this->Form->input('Addresses.1.last_name');
  974. $this->Form->input('Addresses.1.address');
  975. $this->Form->input('Addresses.1.city');
  976. $this->Form->input('Addresses.1.phone');
  977. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  978. $result = $this->Form->secure($this->Form->fields);
  979. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  980. $expected = array(
  981. 'div' => array('style' => 'display:none;'),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  984. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  985. )),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  988. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  989. )),
  990. '/div'
  991. );
  992. $this->assertTags($result, $expected);
  993. }
  994. /**
  995. * Test form security with Model.field.0 style inputs
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityArrayFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request->params['_Token']['key'] = $key;
  1002. $this->Form->create('Address');
  1003. $this->Form->input('Address.primary.1');
  1004. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1005. $this->Form->input('Address.secondary.1.0');
  1006. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1007. }
  1008. /**
  1009. * testFormSecurityMultipleInputDisabledFields method
  1010. *
  1011. * test secure form generation with multiple records and disabled fields.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityMultipleInputDisabledFields() {
  1016. $key = 'testKey';
  1017. $this->Form->request->params['_Token'] = array(
  1018. 'key' => $key,
  1019. 'unlockedFields' => array('first_name', 'address')
  1020. );
  1021. $this->Form->create();
  1022. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1023. $this->Form->input('Addresses.0.title');
  1024. $this->Form->input('Addresses.0.first_name');
  1025. $this->Form->input('Addresses.0.last_name');
  1026. $this->Form->input('Addresses.0.address');
  1027. $this->Form->input('Addresses.0.city');
  1028. $this->Form->input('Addresses.0.phone');
  1029. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1030. $this->Form->input('Addresses.1.title');
  1031. $this->Form->input('Addresses.1.first_name');
  1032. $this->Form->input('Addresses.1.last_name');
  1033. $this->Form->input('Addresses.1.address');
  1034. $this->Form->input('Addresses.1.city');
  1035. $this->Form->input('Addresses.1.phone');
  1036. $result = $this->Form->secure($this->Form->fields);
  1037. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1038. $expected = array(
  1039. 'div' => array('style' => 'display:none;'),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1042. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1043. )),
  1044. array('input' => array(
  1045. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1046. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1047. )),
  1048. '/div'
  1049. );
  1050. $this->assertTags($result, $expected);
  1051. }
  1052. /**
  1053. * testFormSecurityInputDisabledFields method
  1054. *
  1055. * Test single record form with disabled fields.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testFormSecurityInputUnlockedFields() {
  1060. $key = 'testKey';
  1061. $this->Form->request['_Token'] = array(
  1062. 'key' => $key,
  1063. 'unlockedFields' => array('first_name', 'address')
  1064. );
  1065. $this->Form->create();
  1066. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1067. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1068. $this->Form->input('Addresses.title');
  1069. $this->Form->input('Addresses.first_name');
  1070. $this->Form->input('Addresses.last_name');
  1071. $this->Form->input('Addresses.address');
  1072. $this->Form->input('Addresses.city');
  1073. $this->Form->input('Addresses.phone');
  1074. $result = $this->Form->fields;
  1075. $expected = array(
  1076. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1077. 'Addresses.city', 'Addresses.phone'
  1078. );
  1079. $this->assertEquals($expected, $result);
  1080. $result = $this->Form->secure($expected);
  1081. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1082. $expected = array(
  1083. 'div' => array('style' => 'display:none;'),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1086. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1087. )),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1090. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * test securing inputs with custom name attributes.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testFormSecureWithCustomNameAttribute() {
  1102. $this->Form->request->params['_Token']['key'] = 'testKey';
  1103. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1104. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1105. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1106. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1107. }
  1108. /**
  1109. * testFormSecuredInput method
  1110. *
  1111. * Test generation of entire secure form, assertions made on input() output.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecuredInput() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1118. $encoding = strtolower(Configure::read('App.encoding'));
  1119. $expected = array(
  1120. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1121. 'div' => array('style' => 'display:none;'),
  1122. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1123. array('input' => array(
  1124. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1125. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1131. $expected = array(
  1132. 'div' => array('class' => 'input text'),
  1133. 'label' => array('for' => 'UserFormPublished'),
  1134. 'Published',
  1135. '/label',
  1136. array('input' => array(
  1137. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1138. 'id' => 'UserFormPublished'
  1139. )),
  1140. '/div'
  1141. );
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input text'),
  1146. 'label' => array('for' => 'UserFormOther'),
  1147. 'Other',
  1148. '/label',
  1149. array('input' => array(
  1150. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1151. 'id' => 'UserFormOther'
  1152. )),
  1153. '/div'
  1154. );
  1155. $this->assertTags($result, $expected);
  1156. $result = $this->Form->hidden('UserForm.stuff');
  1157. $expected = array('input' => array(
  1158. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1159. 'id' => 'UserFormStuff'
  1160. ));
  1161. $this->assertTags($result, $expected);
  1162. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1163. $expected = array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1165. 'value' => '0', 'id' => 'UserFormHidden'
  1166. ));
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1169. $expected = array(
  1170. 'div' => array('class' => 'input checkbox'),
  1171. array('input' => array(
  1172. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1173. 'value' => '0', 'id' => 'UserFormSomething_'
  1174. )),
  1175. array('input' => array(
  1176. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1177. 'value' => '1', 'id' => 'UserFormSomething'
  1178. )),
  1179. 'label' => array('for' => 'UserFormSomething'),
  1180. 'Something',
  1181. '/label',
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->fields;
  1186. $expected = array(
  1187. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1188. 'UserForm.hidden' => '0', 'UserForm.something'
  1189. );
  1190. $this->assertEquals($expected, $result);
  1191. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1192. $result = $this->Form->secure($this->Form->fields);
  1193. $expected = array(
  1194. 'div' => array('style' => 'display:none;'),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1197. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1198. )),
  1199. array('input' => array(
  1200. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1201. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1202. )),
  1203. '/div'
  1204. );
  1205. $this->assertTags($result, $expected);
  1206. }
  1207. /**
  1208. * Tests that the correct keys are added to the field hash index
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredFileInput() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->assertEquals(array(), $this->Form->fields);
  1215. $result = $this->Form->file('Attachment.file');
  1216. $expected = array(
  1217. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1218. 'Attachment.file.error', 'Attachment.file.size'
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that multiple selects keys are added to field hash
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFormSecuredMultipleSelect() {
  1228. $this->Form->request['_Token'] = array('key' => 'testKey');
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. $options = array('1' => 'one', '2' => 'two');
  1231. $this->Form->select('Model.select', $options);
  1232. $expected = array('Model.select');
  1233. $this->assertEquals($expected, $this->Form->fields);
  1234. $this->Form->fields = array();
  1235. $this->Form->select('Model.select', $options, array('multiple' => true));
  1236. $this->assertEquals($expected, $this->Form->fields);
  1237. }
  1238. /**
  1239. * testFormSecuredRadio method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testFormSecuredRadio() {
  1244. $this->Form->request['_Token'] = array('key' => 'testKey');
  1245. $this->assertEquals(array(), $this->Form->fields);
  1246. $options = array('1' => 'option1', '2' => 'option2');
  1247. $this->Form->radio('Test.test', $options);
  1248. $expected = array('Test.test');
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1253. * hashing.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFormSecuredAndDisabled() {
  1258. $this->Form->request['_Token'] = array('key' => 'testKey');
  1259. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1260. $this->Form->text('Model.text', array('disabled' => true));
  1261. $this->Form->password('Model.text', array('disabled' => true));
  1262. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1263. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1264. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1265. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1266. $this->Form->month('Model.month', array('disabled' => true));
  1267. $this->Form->day('Model.day', array('disabled' => true));
  1268. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1269. $this->Form->minute('Model.minute', array('disabled' => true));
  1270. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1271. $expected = array(
  1272. 'Model.radio' => ''
  1273. );
  1274. $this->assertEquals($expected, $this->Form->fields);
  1275. }
  1276. /**
  1277. * testDisableSecurityUsingForm method
  1278. *
  1279. * @return void
  1280. */
  1281. public function testDisableSecurityUsingForm() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'disabledFields' => array()
  1285. );
  1286. $this->Form->create();
  1287. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1288. $this->Form->input('Addresses.title');
  1289. $this->Form->input('Addresses.first_name', array('secure' => false));
  1290. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1291. $this->Form->input('Addresses.zip', array(
  1292. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1293. ));
  1294. $result = $this->Form->fields;
  1295. $expected = array(
  1296. 'Addresses.id' => '123456', 'Addresses.title',
  1297. );
  1298. $this->assertEquals($expected, $result);
  1299. }
  1300. /**
  1301. * test disableField
  1302. *
  1303. * @return void
  1304. */
  1305. public function testUnlockFieldAddsToList() {
  1306. $this->Form->request['_Token'] = array(
  1307. 'key' => 'testKey',
  1308. 'unlockedFields' => array()
  1309. );
  1310. $this->Form->create('Contact');
  1311. $this->Form->unlockField('Contact.name');
  1312. $this->Form->text('Contact.name');
  1313. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1314. $this->assertEquals(array(), $this->Form->fields);
  1315. }
  1316. /**
  1317. * test unlockField removing from fields array.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUnlockFieldRemovingFromFields() {
  1322. $this->Form->request['_Token'] = array(
  1323. 'key' => 'testKey',
  1324. 'unlockedFields' => array()
  1325. );
  1326. $this->Form->create('Contact');
  1327. $this->Form->hidden('Contact.id', array('value' => 1));
  1328. $this->Form->text('Contact.name');
  1329. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1330. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1331. $this->Form->unlockField('Contact.name');
  1332. $this->Form->unlockField('Contact.id');
  1333. $this->assertEquals(array(), $this->Form->fields);
  1334. }
  1335. /**
  1336. * testTagIsInvalid method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testTagIsInvalid() {
  1341. $Contact = ClassRegistry::getObject('Contact');
  1342. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1343. $this->Form->setEntity('Contact.0.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = array('Please provide an email');
  1346. $this->assertEquals($expected, $result);
  1347. $this->Form->setEntity('Contact.1.email');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. $this->Form->setEntity('Contact.0.name');
  1352. $result = $this->Form->tagIsInvalid();
  1353. $expected = false;
  1354. $this->assertSame($expected, $result);
  1355. }
  1356. /**
  1357. * testPasswordValidation method
  1358. *
  1359. * test validation errors on password input.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testPasswordValidation() {
  1364. $Contact = ClassRegistry::getObject('Contact');
  1365. $Contact->validationErrors['password'] = array('Please provide a password');
  1366. $result = $this->Form->input('Contact.password');
  1367. $expected = array(
  1368. 'div' => array('class' => 'input password error'),
  1369. 'label' => array('for' => 'ContactPassword'),
  1370. 'Password',
  1371. '/label',
  1372. 'input' => array(
  1373. 'type' => 'password', 'name' => 'data[Contact][password]',
  1374. 'id' => 'ContactPassword', 'class' => 'form-error'
  1375. ),
  1376. array('div' => array('class' => 'error-message')),
  1377. 'Please provide a password',
  1378. '/div',
  1379. '/div'
  1380. );
  1381. $this->assertTags($result, $expected);
  1382. }
  1383. /**
  1384. * testEmptyErrorValidation method
  1385. *
  1386. * test validation error div when validation message is an empty string
  1387. *
  1388. * @access public
  1389. * @return void
  1390. */
  1391. public function testEmptyErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = '';
  1393. $result = $this->Form->input('Contact.password');
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. }
  1410. /**
  1411. * testEmptyInputErrorValidation method
  1412. *
  1413. * test validation error div when validation message is overridden by an empty string when calling input()
  1414. *
  1415. * @access public
  1416. * @return void
  1417. */
  1418. public function testEmptyInputErrorValidation() {
  1419. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1420. $result = $this->Form->input('Contact.password', array('error' => ''));
  1421. $expected = array(
  1422. 'div' => array('class' => 'input password error'),
  1423. 'label' => array('for' => 'ContactPassword'),
  1424. 'Password',
  1425. '/label',
  1426. 'input' => array(
  1427. 'type' => 'password', 'name' => 'data[Contact][password]',
  1428. 'id' => 'ContactPassword', 'class' => 'form-error'
  1429. ),
  1430. array('div' => array('class' => 'error-message')),
  1431. array(),
  1432. '/div',
  1433. '/div'
  1434. );
  1435. $this->assertTags($result, $expected);
  1436. }
  1437. /**
  1438. * testFormValidationAssociated method
  1439. *
  1440. * test display of form errors in conjunction with model::validates.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testFormValidationAssociated() {
  1445. $this->UserForm = ClassRegistry::getObject('UserForm');
  1446. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1447. $data = array(
  1448. 'UserForm' => array('name' => 'user'),
  1449. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1450. );
  1451. $result = $this->UserForm->OpenidUrl->create($data);
  1452. $this->assertFalse(empty($result));
  1453. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1454. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1455. $encoding = strtolower(Configure::read('App.encoding'));
  1456. $expected = array(
  1457. 'form' => array(
  1458. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1459. 'accept-charset' => $encoding
  1460. ),
  1461. 'div' => array('style' => 'display:none;'),
  1462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1463. '/div'
  1464. );
  1465. $this->assertTags($result, $expected);
  1466. $result = $this->Form->error(
  1467. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1468. );
  1469. $this->assertEquals('Error, not registered', $result);
  1470. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1471. }
  1472. /**
  1473. * testFormValidationAssociatedFirstLevel method
  1474. *
  1475. * test form error display with associated model.
  1476. *
  1477. * @return void
  1478. */
  1479. public function testFormValidationAssociatedFirstLevel() {
  1480. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1481. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1482. $data = array(
  1483. 'ValidateUser' => array('name' => 'mariano'),
  1484. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1485. );
  1486. $result = $this->ValidateUser->create($data);
  1487. $this->assertFalse(empty($result));
  1488. $this->assertFalse($this->ValidateUser->validates());
  1489. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1490. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1491. $encoding = strtolower(Configure::read('App.encoding'));
  1492. $expected = array(
  1493. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1494. 'div' => array('style' => 'display:none;'),
  1495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1496. '/div'
  1497. );
  1498. $this->assertTags($result, $expected);
  1499. $result = $this->Form->error(
  1500. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Invalid email', $result);
  1503. $result = $this->Form->error(
  1504. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid name', $result);
  1507. $result = $this->Form->error(
  1508. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1509. );
  1510. $this->assertEquals('Invalid city', $result);
  1511. unset($this->ValidateUser->ValidateProfile);
  1512. unset($this->ValidateUser);
  1513. }
  1514. /**
  1515. * testFormValidationAssociatedSecondLevel method
  1516. *
  1517. * test form error display with associated model.
  1518. *
  1519. * @return void
  1520. */
  1521. public function testFormValidationAssociatedSecondLevel() {
  1522. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1523. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1524. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1525. $data = array(
  1526. 'ValidateUser' => array('name' => 'mariano'),
  1527. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1528. 'ValidateItem' => array('name' => 'Item')
  1529. );
  1530. $result = $this->ValidateUser->create($data);
  1531. $this->assertFalse(empty($result));
  1532. $this->assertFalse($this->ValidateUser->validates());
  1533. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1534. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1535. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1536. $encoding = strtolower(Configure::read('App.encoding'));
  1537. $expected = array(
  1538. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1539. 'div' => array('style' => 'display:none;'),
  1540. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1541. '/div'
  1542. );
  1543. $this->assertTags($result, $expected);
  1544. $result = $this->Form->error(
  1545. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid email', $result);
  1548. $result = $this->Form->error(
  1549. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1550. );
  1551. $this->assertEquals('Invalid name', $result);
  1552. $result = $this->Form->error(
  1553. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1554. );
  1555. $result = $this->Form->error(
  1556. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1557. );
  1558. $this->assertEquals('Invalid description', $result);
  1559. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1560. unset($this->ValidateUser->ValidateProfile);
  1561. unset($this->ValidateUser);
  1562. }
  1563. /**
  1564. * testFormValidationMultiRecord method
  1565. *
  1566. * test form error display with multiple records.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testFormValidationMultiRecord() {
  1571. $Contact = ClassRegistry::getObject('Contact');
  1572. $Contact->validationErrors[2] = array(
  1573. 'name' => array('This field cannot be left blank')
  1574. );
  1575. $result = $this->Form->input('Contact.2.name');
  1576. $expected = array(
  1577. 'div' => array('class' => 'input text error'),
  1578. 'label' => array('for' => 'Contact2Name'),
  1579. 'Name',
  1580. '/label',
  1581. 'input' => array(
  1582. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1583. 'class' => 'form-error', 'maxlength' => 255
  1584. ),
  1585. array('div' => array('class' => 'error-message')),
  1586. 'This field cannot be left blank',
  1587. '/div',
  1588. '/div'
  1589. );
  1590. $this->assertTags($result, $expected);
  1591. }
  1592. /**
  1593. * testMultipleInputValidation method
  1594. *
  1595. * test multiple record form validation error display.
  1596. *
  1597. * @return void
  1598. */
  1599. public function testMultipleInputValidation() {
  1600. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1601. $Address->validationErrors[0] = array(
  1602. 'title' => array('This field cannot be empty'),
  1603. 'first_name' => array('This field cannot be empty')
  1604. );
  1605. $Address->validationErrors[1] = array(
  1606. 'last_name' => array('You must have a last name')
  1607. );
  1608. $this->Form->create();
  1609. $result = $this->Form->input('Address.0.title');
  1610. $expected = array(
  1611. 'div' => array('class'),
  1612. 'label' => array('for'),
  1613. 'preg:/[^<]+/',
  1614. '/label',
  1615. 'input' => array(
  1616. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be empty',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. $result = $this->Form->input('Address.0.first_name');
  1625. $expected = array(
  1626. 'div' => array('class'),
  1627. 'label' => array('for'),
  1628. 'preg:/[^<]+/',
  1629. '/label',
  1630. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'This field cannot be empty',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->input('Address.0.last_name');
  1638. $expected = array(
  1639. 'div' => array('class'),
  1640. 'label' => array('for'),
  1641. 'preg:/[^<]+/',
  1642. '/label',
  1643. 'input' => array('type' => 'text', 'name', 'id'),
  1644. '/div'
  1645. );
  1646. $this->assertTags($result, $expected);
  1647. $result = $this->Form->input('Address.1.last_name');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'preg:/[^<]+/',
  1652. '/label',
  1653. 'input' => array(
  1654. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1655. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1656. ),
  1657. array('div' => array('class' => 'error-message')),
  1658. 'You must have a last name',
  1659. '/div',
  1660. '/div'
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. }
  1664. /**
  1665. * testInput method
  1666. *
  1667. * Test various incarnations of input().
  1668. *
  1669. * @return void
  1670. */
  1671. public function testInput() {
  1672. $result = $this->Form->input('ValidateUser.balance');
  1673. $expected = array(
  1674. 'div' => array('class'),
  1675. 'label' => array('for'),
  1676. 'Balance',
  1677. '/label',
  1678. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1679. '/div',
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1683. $expected = array(
  1684. 'div' => array('class' => 'input text'),
  1685. 'label' => array('for' => 'custom'),
  1686. 'Email',
  1687. '/label',
  1688. array('input' => array(
  1689. 'type' => 'text', 'name' => 'data[Contact][email]',
  1690. 'id' => 'custom', 'maxlength' => 255
  1691. )),
  1692. '/div'
  1693. );
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1696. $expected = array(
  1697. '<div',
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'data[Contact][email]',
  1703. 'id' => 'ContactEmail', 'maxlength' => 255
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->hidden('Contact.idontexist');
  1709. $expected = array('input' => array(
  1710. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1711. 'id' => 'ContactIdontexist'
  1712. ));
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1715. $expected = array(
  1716. 'div' => array('class' => 'input text'),
  1717. 'label' => array('for' => 'ContactEmail'),
  1718. 'Email',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'text', 'name' => 'data[Contact][email]',
  1722. 'id' => 'ContactEmail'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1728. $expected = array(
  1729. 'div' => array('class' => 'input text'),
  1730. 'label' => array('for' => 'Contact5Email'),
  1731. 'Email',
  1732. '/label',
  1733. array('input' => array(
  1734. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1735. 'id' => 'Contact5Email'
  1736. )),
  1737. '/div'
  1738. );
  1739. $this->assertTags($result, $expected);
  1740. $result = $this->Form->input('Contact.password');
  1741. $expected = array(
  1742. 'div' => array('class' => 'input password'),
  1743. 'label' => array('for' => 'ContactPassword'),
  1744. 'Password',
  1745. '/label',
  1746. array('input' => array(
  1747. 'type' => 'password', 'name' => 'data[Contact][password]',
  1748. 'id' => 'ContactPassword'
  1749. )),
  1750. '/div'
  1751. );
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Contact.email', array(
  1754. 'type' => 'file', 'class' => 'textbox'
  1755. ));
  1756. $expected = array(
  1757. 'div' => array('class' => 'input file'),
  1758. 'label' => array('for' => 'ContactEmail'),
  1759. 'Email',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1763. 'id' => 'ContactEmail'
  1764. )),
  1765. '/div'
  1766. );
  1767. $this->assertTags($result, $expected);
  1768. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1769. $result = $this->Form->input('Contact.phone');
  1770. $expected = array(
  1771. 'div' => array('class' => 'input text'),
  1772. 'label' => array('for' => 'ContactPhone'),
  1773. 'Phone',
  1774. '/label',
  1775. array('input' => array(
  1776. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1777. 'value' => 'Hello &amp; World &gt; weird chars',
  1778. 'id' => 'ContactPhone', 'maxlength' => 255
  1779. )),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1784. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1785. $expected = array(
  1786. 'div' => array('class' => 'input text'),
  1787. 'label' => array('for' => 'myId'),
  1788. 'Field',
  1789. '/label',
  1790. 'input' => array(
  1791. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1792. 'value' => 'My value', 'id' => 'myId'
  1793. ),
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. unset($this->Form->request->data);
  1798. $Contact = ClassRegistry::getObject('Contact');
  1799. $Contact->validationErrors['field'] = array('Badness!');
  1800. $result = $this->Form->input('Contact.field');
  1801. $expected = array(
  1802. 'div' => array('class' => 'input text error'),
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('div' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/div',
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $result = $this->Form->input('Contact.field', array(
  1817. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1818. ));
  1819. $expected = array(
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('span' => array('class' => 'error-message')),
  1828. 'Badness!',
  1829. '/span'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1834. ));
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. array('div' => array('class' => 'error')),
  1845. 'Badness!',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1851. ));
  1852. $expected = array(
  1853. 'span' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text error'),
  1868. 'label' => array('for' => 'ContactField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array(
  1872. 'type' => 'text', 'name' => 'data[Contact][field]',
  1873. 'id' => 'ContactField', 'class' => 'form-error'
  1874. ),
  1875. 'A message to you, Rudy',
  1876. array('div' => array('class' => 'error-message')),
  1877. 'Badness!',
  1878. '/div',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $this->Form->setEntity(null);
  1883. $this->Form->setEntity('Contact.field');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'after' => 'A message to you, Rudy', 'error' => false
  1886. ));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text'),
  1889. 'label' => array('for' => 'ContactField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1893. 'A message to you, Rudy',
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text'),
  1900. 'label' => array('for' => 'ObjectField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1904. 'A message to you, Rudy',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('minLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1912. 'maxLength' => 'login too large'
  1913. )
  1914. ));
  1915. $expected = array(
  1916. 'div' => array('class' => 'input text error'),
  1917. 'label' => array('for' => 'ContactField'),
  1918. 'Field',
  1919. '/label',
  1920. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1921. array('div' => array('class' => 'error-message')),
  1922. 'Le login doit contenir au moins 2 caractères',
  1923. '/div',
  1924. '/div'
  1925. );
  1926. $this->assertTags($result, $expected);
  1927. $Contact->validationErrors['field'] = array('maxLength');
  1928. $result = $this->Form->input('Contact.field', array(
  1929. 'error' => array(
  1930. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1931. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1932. 'maxLength' => 'login too large',
  1933. )
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input text error'),
  1937. 'label' => array('for' => 'ContactField'),
  1938. 'Field',
  1939. '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1941. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1942. 'login too large',
  1943. '/span',
  1944. '/div'
  1945. );
  1946. $this->assertTags($result, $expected);
  1947. }
  1948. /**
  1949. * test input() with checkbox creation
  1950. *
  1951. * @return void
  1952. */
  1953. public function testInputCheckbox() {
  1954. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1955. $expected = array(
  1956. 'div' => array('class' => 'input checkbox'),
  1957. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1958. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1959. '/div'
  1960. );
  1961. $this->assertTags($result, $expected);
  1962. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1963. $expected = array(
  1964. 'div' => array('class' => 'input checkbox'),
  1965. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1966. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1967. '/div'
  1968. );
  1969. $this->assertTags($result, $expected);
  1970. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1971. $expected = array(
  1972. 'div' => array('class' => 'input checkbox'),
  1973. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1974. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1975. '/div'
  1976. );
  1977. $this->assertTags($result, $expected);
  1978. }
  1979. /**
  1980. * test form->input() with time types.
  1981. *
  1982. */
  1983. public function testInputTime() {
  1984. extract($this->dateRegex);
  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('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1990. $result = explode(':', $result);
  1991. $this->assertRegExp('/option value="23"/', $result[0]);
  1992. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1993. $result = $this->Form->input('Model.field', array(
  1994. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1995. ));
  1996. $result = explode(':', $result);
  1997. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1998. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1999. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2000. $result = $this->Form->input('Model.field', array(
  2001. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2002. ));
  2003. $result = explode(':', $result);
  2004. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2005. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2006. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2007. $result = $this->Form->input('prueba', array(
  2008. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2009. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2010. ));
  2011. $result = explode(':', $result);
  2012. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2013. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2015. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2016. $result = $this->Form->input('Random.start_time', array(
  2017. 'type' => 'time',
  2018. 'selected' => '18:15'
  2019. ));
  2020. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2021. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2022. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2023. }
  2024. /**
  2025. * Test interval + selected near the hour roll over.
  2026. *
  2027. * @return void
  2028. */
  2029. public function testTimeSelectedWithInterval() {
  2030. $result = $this->Form->input('Model.start_time', array(
  2031. 'type' => 'time',
  2032. 'interval' => 15,
  2033. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2034. ));
  2035. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2036. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2037. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2038. $result = $this->Form->input('Model.start_time', array(
  2039. 'type' => 'time',
  2040. 'interval' => 15,
  2041. 'selected' => '2012-10-23 15:57:00'
  2042. ));
  2043. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2044. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2045. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2046. $result = $this->Form->input('Model.start_time', array(
  2047. 'timeFormat' => 24,
  2048. 'type' => 'time',
  2049. 'interval' => 15,
  2050. 'selected' => '15:57'
  2051. ));
  2052. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2053. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2054. $result = $this->Form->input('Model.start_time', array(
  2055. 'timeFormat' => 24,
  2056. 'type' => 'time',
  2057. 'interval' => 15,
  2058. 'selected' => '23:57'
  2059. ));
  2060. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2061. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2062. $result = $this->Form->input('Model.created', array(
  2063. 'timeFormat' => 24,
  2064. 'type' => 'datetime',
  2065. 'interval' => 15,
  2066. 'selected' => '2012-09-30 23:56'
  2067. ));
  2068. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2069. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2070. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2071. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2072. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2073. }
  2074. /**
  2075. * test form->input() with datetime, date and time types
  2076. *
  2077. * @return void
  2078. */
  2079. public function testInputDatetime() {
  2080. extract($this->dateRegex);
  2081. $result = $this->Form->input('prueba', array(
  2082. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2083. 'maxYear' => date('Y') + 1, 'interval' => 15
  2084. ));
  2085. $result = explode(':', $result);
  2086. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2087. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2088. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2089. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2090. //related to ticket #5013
  2091. $result = $this->Form->input('Contact.date', array(
  2092. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2093. ));
  2094. $this->assertRegExp('/class="customClass"/', $result);
  2095. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2096. $result = $this->Form->input('Contact.date', array(
  2097. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2098. ));
  2099. $this->assertRegExp('/id="customIdDay"/', $result);
  2100. $this->assertRegExp('/id="customIdMonth"/', $result);
  2101. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2102. $result = $this->Form->input('Model.field', array(
  2103. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2104. ));
  2105. $this->assertRegExp('/id="customIDDay"/', $result);
  2106. $this->assertRegExp('/id="customIDHour"/', $result);
  2107. $result = explode('</select><select', $result);
  2108. $result = explode(':', $result[1]);
  2109. $this->assertRegExp('/option value="23"/', $result[0]);
  2110. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2111. $result = $this->Form->input('Model.field', array(
  2112. 'type' => 'datetime', 'timeFormat' => 12
  2113. ));
  2114. $result = explode('</select><select', $result);
  2115. $result = explode(':', $result[1]);
  2116. $this->assertRegExp('/option value="12"/', $result[0]);
  2117. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2118. $this->Form->request->data = array('Contact' => array('created' => null));
  2119. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2120. $expected = array(
  2121. 'div' => array('class' => 'input date'),
  2122. 'label' => array('for' => 'ContactCreatedMonth'),
  2123. 'Created',
  2124. '/label',
  2125. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2126. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2127. $monthsRegex,
  2128. '/select', '-',
  2129. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2130. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2131. $daysRegex,
  2132. '/select', '-',
  2133. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2134. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2135. $yearsRegex,
  2136. '/select',
  2137. '/div'
  2138. );
  2139. $this->assertTags($result, $expected);
  2140. $this->Form->request->data = array('Contact' => array('created' => null));
  2141. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2142. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2143. $this->Form->request->data = array('Contact' => array('created' => null));
  2144. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2145. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2146. $result = $this->Form->input('Contact.created', array(
  2147. 'type' => 'date',
  2148. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2149. 'timeFormat' => 'NONE'
  2150. ));
  2151. $this->assertRegExp('/for\="created-month"/', $result);
  2152. }
  2153. /**
  2154. * Test generating checkboxes in a loop.
  2155. *
  2156. * @return void
  2157. */
  2158. public function testInputCheckboxesInLoop() {
  2159. for ($i = 1; $i < 5; $i++) {
  2160. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input checkbox'),
  2163. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2164. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2165. 'label' => array('for' => "Contact{$i}Email"),
  2166. 'Email',
  2167. '/label',
  2168. '/div'
  2169. );
  2170. $this->assertTags($result, $expected);
  2171. }
  2172. }
  2173. /**
  2174. * Test generating checkboxes with disabled elements.
  2175. *
  2176. * @return void
  2177. */
  2178. public function testInputCheckboxWithDisabledElements() {
  2179. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2180. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2181. $expected = array(
  2182. array('div' => array('class' => 'input select')),
  2183. array('label' => array('for' => "ContactMultiple")),
  2184. 'Multiple',
  2185. '/label',
  2186. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2187. array('div' => array('class' => 'checkbox')),
  2188. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2189. array('label' => array('for' => "ContactMultiple1")),
  2190. 'One',
  2191. '/label',
  2192. '/div',
  2193. array('div' => array('class' => 'checkbox')),
  2194. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2195. array('label' => array('for' => "ContactMultiple2")),
  2196. 'Two',
  2197. '/label',
  2198. '/div',
  2199. array('div' => array('class' => 'checkbox')),
  2200. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2201. array('label' => array('for' => "ContactMultiple3")),
  2202. 'Three',
  2203. '/label',
  2204. '/div',
  2205. '/div'
  2206. );
  2207. $this->assertTags($result, $expected);
  2208. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2209. $this->assertTags($result, $expected);
  2210. $disabled = array('2', 3);
  2211. $expected = array(
  2212. array('div' => array('class' => 'input select')),
  2213. array('label' => array('for' => "ContactMultiple")),
  2214. 'Multiple',
  2215. '/label',
  2216. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2217. array('div' => array('class' => 'checkbox')),
  2218. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2219. array('label' => array('for' => "ContactMultiple1")),
  2220. 'One',
  2221. '/label',
  2222. '/div',
  2223. array('div' => array('class' => 'checkbox')),
  2224. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2225. array('label' => array('for' => "ContactMultiple2")),
  2226. 'Two',
  2227. '/label',
  2228. '/div',
  2229. array('div' => array('class' => 'checkbox')),
  2230. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2231. array('label' => array('for' => "ContactMultiple3")),
  2232. 'Three',
  2233. '/label',
  2234. '/div',
  2235. '/div'
  2236. );
  2237. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2238. $this->assertTags($result, $expected);
  2239. }
  2240. /**
  2241. * test input name with leading integer, ensure attributes are generated correctly.
  2242. *
  2243. * @return void
  2244. */
  2245. public function testInputWithLeadingInteger() {
  2246. $result = $this->Form->text('0.Node.title');
  2247. $expected = array(
  2248. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2249. );
  2250. $this->assertTags($result, $expected);
  2251. }
  2252. /**
  2253. * test form->input() with select type inputs.
  2254. *
  2255. * @return void
  2256. */
  2257. public function testInputSelectType() {
  2258. $result = $this->Form->input('email', array(
  2259. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2260. );
  2261. $expected = array(
  2262. 'div' => array('class' => 'input select'),
  2263. 'label' => array('for' => 'email'),
  2264. 'Email',
  2265. '/label',
  2266. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2267. array('option' => array('value' => '')),
  2268. '/option',
  2269. array('option' => array('value' => 'è')),
  2270. 'Firést',
  2271. '/option',
  2272. array('option' => array('value' => 'é')),
  2273. 'Secoènd',
  2274. '/option',
  2275. '/select',
  2276. '/div'
  2277. );
  2278. $this->assertTags($result, $expected);
  2279. $result = $this->Form->input('email', array(
  2280. 'options' => array('First', 'Second'), 'empty' => true)
  2281. );
  2282. $expected = array(
  2283. 'div' => array('class' => 'input select'),
  2284. 'label' => array('for' => 'email'),
  2285. 'Email',
  2286. '/label',
  2287. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2288. array('option' => array('value' => '')),
  2289. '/option',
  2290. array('option' => array('value' => '0')),
  2291. 'First',
  2292. '/option',
  2293. array('option' => array('value' => '1')),
  2294. 'Second',
  2295. '/option',
  2296. '/select',
  2297. '/div'
  2298. );
  2299. $this->assertTags($result, $expected);
  2300. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2301. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2302. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2303. $expected = array(
  2304. 'div' => array('class' => 'input select'),
  2305. 'label' => array('for' => 'ModelUserId'),
  2306. 'User',
  2307. '/label',
  2308. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2309. array('option' => array('value' => '')),
  2310. '/option',
  2311. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2312. 'good',
  2313. '/option',
  2314. array('option' => array('value' => 'other')),
  2315. 'bad',
  2316. '/option',
  2317. '/select',
  2318. '/div'
  2319. );
  2320. $this->assertTags($result, $expected);
  2321. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2322. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2323. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2324. $expected = array(
  2325. 'div' => array('class' => 'input select'),
  2326. 'label' => array('for' => 'ThingUserId'),
  2327. 'User',
  2328. '/label',
  2329. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2330. array('option' => array('value' => '')),
  2331. 'Some Empty',
  2332. '/option',
  2333. array('option' => array('value' => 'value')),
  2334. 'good',
  2335. '/option',
  2336. array('option' => array('value' => 'other')),
  2337. 'bad',
  2338. '/option',
  2339. '/select',
  2340. '/div'
  2341. );
  2342. $this->assertTags($result, $expected);
  2343. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2344. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2345. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2346. $expected = array(
  2347. 'div' => array('class' => 'input select'),
  2348. 'label' => array('for' => 'ThingUserId'),
  2349. 'User',
  2350. '/label',
  2351. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2352. array('option' => array('value' => '')),
  2353. 'Some Empty',
  2354. '/option',
  2355. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2356. 'good',
  2357. '/option',
  2358. array('option' => array('value' => 'other')),
  2359. 'bad',
  2360. '/option',
  2361. '/select',
  2362. '/div'
  2363. );
  2364. $this->assertTags($result, $expected);
  2365. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2366. $this->Form->request->data = array('User' => array('User' => array('value')));
  2367. $result = $this->Form->input('User.User', array('empty' => true));
  2368. $expected = array(
  2369. 'div' => array('class' => 'input select'),
  2370. 'label' => array('for' => 'UserUser'),
  2371. 'User',
  2372. '/label',
  2373. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2374. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2375. array('option' => array('value' => '')),
  2376. '/option',
  2377. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2378. 'good',
  2379. '/option',
  2380. array('option' => array('value' => 'other')),
  2381. 'bad',
  2382. '/option',
  2383. '/select',
  2384. '/div'
  2385. );
  2386. $this->assertTags($result, $expected);
  2387. $this->Form->data = array();
  2388. $result = $this->Form->input('Publisher.id', array(
  2389. 'label' => 'Publisher',
  2390. 'type' => 'select',
  2391. 'multiple' => 'checkbox',
  2392. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2393. ));
  2394. $expected = array(
  2395. array('div' => array('class' => 'input select')),
  2396. array('label' => array('for' => 'PublisherId')),
  2397. 'Publisher',
  2398. '/label',
  2399. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2400. array('div' => array('class' => 'checkbox')),
  2401. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2402. array('label' => array('for' => 'PublisherIdValue1')),
  2403. 'Label 1',
  2404. '/label',
  2405. '/div',
  2406. array('div' => array('class' => 'checkbox')),
  2407. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2408. array('label' => array('for' => 'PublisherIdValue2')),
  2409. 'Label 2',
  2410. '/label',
  2411. '/div',
  2412. '/div'
  2413. );
  2414. $this->assertTags($result, $expected);
  2415. }
  2416. /**
  2417. * test that input() and a non standard primary key makes a hidden input by default.
  2418. *
  2419. * @return void
  2420. */
  2421. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2422. $this->Form->create('User');
  2423. $this->Form->fieldset = array(
  2424. 'User' => array(
  2425. 'fields' => array(
  2426. 'model_id' => array('type' => 'integer')
  2427. ),
  2428. 'validates' => array(),
  2429. 'key' => 'model_id'
  2430. )
  2431. );
  2432. $result = $this->Form->input('model_id');
  2433. $expected = array(
  2434. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2435. );
  2436. $this->assertTags($result, $expected);
  2437. }
  2438. /**
  2439. * test that overriding the magic select type widget is possible
  2440. *
  2441. * @return void
  2442. */
  2443. public function testInputOverridingMagicSelectType() {
  2444. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2445. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2446. $expected = array(
  2447. 'div' => array('class' => 'input text'),
  2448. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2449. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2450. '/div'
  2451. );
  2452. $this->assertTags($result, $expected);
  2453. //Check that magic types still work for plural/singular vars
  2454. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2455. $result = $this->Form->input('Model.type');
  2456. $expected = array(
  2457. 'div' => array('class' => 'input select'),
  2458. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2459. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2460. array('option' => array('value' => 'value')), 'good', '/option',
  2461. array('option' => array('value' => 'other')), 'bad', '/option',
  2462. '/select',
  2463. '/div'
  2464. );
  2465. $this->assertTags($result, $expected);
  2466. }
  2467. /**
  2468. * Test that magic input() selects are created for type=number
  2469. *
  2470. * @return void
  2471. */
  2472. public function testInputMagicSelectForTypeNumber() {
  2473. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2474. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2475. $result = $this->Form->input('ValidateUser.balance');
  2476. $expected = array(
  2477. 'div' => array('class' => 'input select'),
  2478. 'label' => array('for' => 'ValidateUserBalance'),
  2479. 'Balance',
  2480. '/label',
  2481. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2482. array('option' => array('value' => '0')),
  2483. 'nothing',
  2484. '/option',
  2485. array('option' => array('value' => '1', 'selected' => 'selected')),
  2486. 'some',
  2487. '/option',
  2488. array('option' => array('value' => '100')),
  2489. 'a lot',
  2490. '/option',
  2491. '/select',
  2492. '/div'
  2493. );
  2494. $this->assertTags($result, $expected);
  2495. }
  2496. /**
  2497. * Test that magic input() selects can easily be converted into radio types without error.
  2498. *
  2499. * @return void
  2500. */
  2501. public function testInputMagicSelectChangeToRadio() {
  2502. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2503. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2504. $this->assertRegExp('/input type="radio"/', $result);
  2505. }
  2506. /**
  2507. * fields with the same name as the model should work.
  2508. *
  2509. * @return void
  2510. */
  2511. public function testInputWithMatchingFieldAndModelName() {
  2512. $this->Form->create('User');
  2513. $this->Form->fieldset = array(
  2514. 'User' => array(
  2515. 'fields' => array(
  2516. 'User' => array('type' => 'text')
  2517. ),
  2518. 'validates' => array(),
  2519. 'key' => 'id'
  2520. )
  2521. );
  2522. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2523. $result = $this->Form->input('User', array('type' => 'text'));
  2524. $expected = array(
  2525. 'div' => array('class' => 'input text'),
  2526. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2527. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2528. '/div'
  2529. );
  2530. $this->assertTags($result, $expected);
  2531. }
  2532. /**
  2533. * testFormInputs method
  2534. *
  2535. * test correct results from form::inputs().
  2536. *
  2537. * @return void
  2538. */
  2539. public function testFormInputs() {
  2540. $this->Form->create('Contact');
  2541. $result = $this->Form->inputs('The Legend');
  2542. $expected = array(
  2543. '<fieldset',
  2544. '<legend',
  2545. 'The Legend',
  2546. '/legend',
  2547. '*/fieldset',
  2548. );
  2549. $this->assertTags($result, $expected);
  2550. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2551. $expected = array(
  2552. 'fieldset' => array('class' => 'classy-stuff'),
  2553. '<legend',
  2554. 'Field of Dreams',
  2555. '/legend',
  2556. '*/fieldset'
  2557. );
  2558. $this->assertTags($result, $expected);
  2559. $this->Form->create('Contact');
  2560. $this->Form->request['prefix'] = 'admin';
  2561. $this->Form->request['action'] = 'admin_edit';
  2562. $result = $this->Form->inputs();
  2563. $expected = array(
  2564. '<fieldset',
  2565. '<legend',
  2566. 'Edit Contact',
  2567. '/legend',
  2568. '*/fieldset',
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $this->Form->create('Contact');
  2572. $result = $this->Form->inputs(false);
  2573. $expected = array(
  2574. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2575. array('div' => array('class' => 'input text')),
  2576. '*/div',
  2577. array('div' => array('class' => 'input text')),
  2578. '*/div',
  2579. array('div' => array('class' => 'input text')),
  2580. '*/div',
  2581. array('div' => array('class' => 'input password')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input date')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input date')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input datetime')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input number')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input select')),
  2592. '*/div',
  2593. );
  2594. $this->assertTags($result, $expected);
  2595. $this->Form->create('Contact');
  2596. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2597. $expected = array(
  2598. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2599. array('div' => array('class' => 'input text')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input text')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input text')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input password')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input date')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input date')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input datetime')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input number')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input select')),
  2616. '*/div',
  2617. );
  2618. $this->assertTags($result, $expected);
  2619. $this->Form->create('Contact');
  2620. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2621. $expected = array(
  2622. 'fieldset' => array(),
  2623. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2624. array('div' => array('class' => 'input text')),
  2625. '*/div',
  2626. array('div' => array('class' => 'input text')),
  2627. '*/div',
  2628. array('div' => array('class' => 'input text')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input password')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input date')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input date')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input datetime')),
  2637. '*/div',
  2638. array('div' => array('class' => 'input number')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input select')),
  2641. '*/div',
  2642. '/fieldset'
  2643. );
  2644. $this->assertTags($result, $expected);
  2645. $this->Form->create('Contact');
  2646. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2647. $expected = array(
  2648. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2649. array('div' => array('class' => 'input text')),
  2650. '*/div',
  2651. array('div' => array('class' => 'input text')),
  2652. '*/div',
  2653. array('div' => array('class' => 'input text')),
  2654. '*/div',
  2655. array('div' => array('class' => 'input password')),
  2656. '*/div',
  2657. array('div' => array('class' => 'input date')),
  2658. '*/div',
  2659. array('div' => array('class' => 'input date')),
  2660. '*/div',
  2661. array('div' => array('class' => 'input datetime')),
  2662. '*/div',
  2663. array('div' => array('class' => 'input number')),
  2664. '*/div',
  2665. array('div' => array('class' => 'input select')),
  2666. '*/div',
  2667. );
  2668. $this->assertTags($result, $expected);
  2669. $this->Form->create('Contact');
  2670. $result = $this->Form->inputs('Hello');
  2671. $expected = array(
  2672. 'fieldset' => array(),
  2673. 'legend' => array(),
  2674. 'Hello',
  2675. '/legend',
  2676. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2677. array('div' => array('class' => 'input text')),
  2678. '*/div',
  2679. array('div' => array('class' => 'input text')),
  2680. '*/div',
  2681. array('div' => array('class' => 'input text')),
  2682. '*/div',
  2683. array('div' => array('class' => 'input password')),
  2684. '*/div',
  2685. array('div' => array('class' => 'input date')),
  2686. '*/div',
  2687. array('div' => array('class' => 'input date')),
  2688. '*/div',
  2689. array('div' => array('class' => 'input datetime')),
  2690. '*/div',
  2691. array('div' => array('class' => 'input number')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input select')),
  2694. '*/div',
  2695. '/fieldset'
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. $this->Form->create('Contact');
  2699. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2700. $expected = array(
  2701. 'fieldset' => array(),
  2702. 'legend' => array(),
  2703. 'Hello',
  2704. '/legend',
  2705. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2706. array('div' => array('class' => 'input text')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input text')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input text')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input password')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input date')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input date')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input datetime')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input number')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input select')),
  2723. '*/div',
  2724. '/fieldset'
  2725. );
  2726. $this->assertTags($result, $expected);
  2727. }
  2728. /**
  2729. * testSelectAsCheckbox method
  2730. *
  2731. * test multi-select widget with checkbox formatting.
  2732. *
  2733. * @return void
  2734. */
  2735. public function testSelectAsCheckbox() {
  2736. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2737. $expected = array(
  2738. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2739. array('div' => array('class' => 'checkbox')),
  2740. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2741. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2742. 'first',
  2743. '/label',
  2744. '/div',
  2745. array('div' => array('class' => 'checkbox')),
  2746. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2747. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2748. 'second',
  2749. '/label',
  2750. '/div',
  2751. array('div' => array('class' => 'checkbox')),
  2752. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2753. array('label' => array('for' => 'ModelMultiField2')),
  2754. 'third',
  2755. '/label',
  2756. '/div',
  2757. );
  2758. $this->assertTags($result, $expected);
  2759. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2760. $expected = array(
  2761. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2762. array('div' => array('class' => 'checkbox')),
  2763. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2764. array('label' => array('for' => 'ModelMultiField12')),
  2765. 'half',
  2766. '/label',
  2767. '/div',
  2768. );
  2769. $this->assertTags($result, $expected);
  2770. }
  2771. /**
  2772. * testLabel method
  2773. *
  2774. * test label generation.
  2775. *
  2776. * @return void
  2777. */
  2778. public function testLabel() {
  2779. $this->Form->text('Person.name');
  2780. $result = $this->Form->label();
  2781. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2782. $this->Form->text('Person.name');
  2783. $result = $this->Form->label();
  2784. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2785. $result = $this->Form->label('Person.first_name');
  2786. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2787. $result = $this->Form->label('Person.first_name', 'Your first name');
  2788. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2789. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2790. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2791. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2792. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2793. $result = $this->Form->label('Person.first_name', '');
  2794. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2795. $result = $this->Form->label('Person.2.name', '');
  2796. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2797. }
  2798. /**
  2799. * testTextbox method
  2800. *
  2801. * test textbox element generation
  2802. *
  2803. * @return void
  2804. */
  2805. public function testTextbox() {
  2806. $result = $this->Form->text('Model.field');
  2807. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2808. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2809. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2810. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2811. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2812. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2813. $result = $this->Form->text('Model.text');
  2814. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2815. $Contact = ClassRegistry::getObject('Contact');
  2816. $Contact->validationErrors['text'] = array(true);
  2817. $this->Form->request->data['Contact']['text'] = 'test';
  2818. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2819. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2820. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2821. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2824. );
  2825. $this->assertTags($result, $expected);
  2826. }
  2827. /**
  2828. * testDefaultValue method
  2829. *
  2830. * Test default value setting
  2831. *
  2832. * @return void
  2833. */
  2834. public function testDefaultValue() {
  2835. $this->Form->request->data['Model']['field'] = 'test';
  2836. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2837. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2838. unset($this->Form->request->data['Model']['field']);
  2839. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2840. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2841. }
  2842. /**
  2843. * testCheckboxDefaultValue method
  2844. *
  2845. * Test default value setting on checkbox() method
  2846. *
  2847. * @return void
  2848. */
  2849. public function testCheckboxDefaultValue() {
  2850. $this->Form->request->data['Model']['field'] = false;
  2851. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2852. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2853. unset($this->Form->request->data['Model']['field']);
  2854. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2855. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2856. $this->Form->request->data['Model']['field'] = true;
  2857. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2858. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2859. unset($this->Form->request->data['Model']['field']);
  2860. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2861. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2862. }
  2863. /**
  2864. * testError method
  2865. *
  2866. * Test field error generation
  2867. *
  2868. * @return void
  2869. */
  2870. public function testError() {
  2871. $Contact = ClassRegistry::getObject('Contact');
  2872. $Contact->validationErrors['field'] = array(1);
  2873. $result = $this->Form->error('Contact.field');
  2874. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2875. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2876. $this->assertEquals('Error in field Field', $result);
  2877. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2878. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2879. $this->assertEquals('This field contains invalid input', $result);
  2880. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2881. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2882. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2883. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2884. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2885. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2886. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2887. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2888. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2889. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2890. $this->assertEquals('<strong>Badness!</strong>', $result);
  2891. $Contact->validationErrors['field'] = array("email");
  2892. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2893. $expected = array(
  2894. 'div' => array('class' => 'field-error'),
  2895. 'No good!',
  2896. '/div'
  2897. );
  2898. $this->assertTags($result, $expected);
  2899. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2900. $result = $this->Form->error('Contact.field', array(
  2901. 'notEmpty' => 'Cannot be empty',
  2902. 'email' => 'No good!'
  2903. ));
  2904. $expected = array(
  2905. 'div' => array('class' => 'error-message'),
  2906. 'ul' => array(),
  2907. '<li', 'Cannot be empty', '/li',
  2908. '<li', 'No good!', '/li',
  2909. '<li', 'Something else', '/li',
  2910. '/ul',
  2911. '/div'
  2912. );
  2913. $this->assertTags($result, $expected);
  2914. /** Testing error messages list options **/
  2915. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2916. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2917. $expected = array(
  2918. 'div' => array('class' => 'error-message'),
  2919. 'ol' => array(),
  2920. '<li', 'notEmpty', '/li',
  2921. '<li', 'email', '/li',
  2922. '/ol',
  2923. '/div'
  2924. );
  2925. $this->assertTags($result, $expected);
  2926. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2927. $expected = array(
  2928. 'div' => array('class' => 'error-message'),
  2929. 'ol' => array(),
  2930. '<li', 'notEmpty', '/li',
  2931. '<li', 'email', '/li',
  2932. '/ol',
  2933. '/div'
  2934. );
  2935. $this->assertTags($result, $expected);
  2936. $result = $this->Form->error('Contact.field', null, array(
  2937. 'listOptions' => array(
  2938. 'class' => 'ul-class',
  2939. 'itemOptions' => array(
  2940. 'class' => 'li-class'
  2941. )
  2942. )
  2943. ));
  2944. $expected = array(
  2945. 'div' => array('class' => 'error-message'),
  2946. 'ul' => array('class' => 'ul-class'),
  2947. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2948. array('li' => array('class' => 'li-class')), 'email', '/li',
  2949. '/ul',
  2950. '/div'
  2951. );
  2952. $this->assertTags($result, $expected);
  2953. }
  2954. /**
  2955. * test error options when using form->input();
  2956. *
  2957. * @return void
  2958. */
  2959. public function testInputErrorEscape() {
  2960. $this->Form->create('ValidateProfile');
  2961. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2962. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2963. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2964. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2965. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2966. $this->assertRegExp('/required<br>/', $result);
  2967. }
  2968. /**
  2969. * testPassword method
  2970. *
  2971. * Test password element generation
  2972. *
  2973. * @return void
  2974. */
  2975. public function testPassword() {
  2976. $Contact = ClassRegistry::getObject('Contact');
  2977. $result = $this->Form->password('Contact.field');
  2978. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2979. $Contact->validationErrors['passwd'] = 1;
  2980. $this->Form->request->data['Contact']['passwd'] = 'test';
  2981. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. }
  2984. /**
  2985. * testRadio method
  2986. *
  2987. * Test radio element set generation
  2988. *
  2989. * @return void
  2990. */
  2991. public function testRadio() {
  2992. $result = $this->Form->radio('Model.field', array('option A'));
  2993. $expected = array(
  2994. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2995. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2996. 'label' => array('for' => 'ModelField0'),
  2997. 'option A',
  2998. '/label'
  2999. );
  3000. $this->assertTags($result, $expected);
  3001. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3002. $expected = array(
  3003. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3004. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3005. 'label' => array('for' => 'ModelField12'),
  3006. 'half',
  3007. '/label'
  3008. );
  3009. $this->assertTags($result, $expected);
  3010. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3011. $expected = array(
  3012. 'fieldset' => array(),
  3013. 'legend' => array(),
  3014. 'Field',
  3015. '/legend',
  3016. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3017. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3018. array('label' => array('for' => 'ModelField0')),
  3019. 'option A',
  3020. '/label',
  3021. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3022. array('label' => array('for' => 'ModelField1')),
  3023. 'option B',
  3024. '/label',
  3025. '/fieldset'
  3026. );
  3027. $this->assertTags($result, $expected);
  3028. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3029. $expected = array(
  3030. 'fieldset' => array(),
  3031. 'legend' => array(),
  3032. 'Field',
  3033. '/legend',
  3034. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3035. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3036. array('label' => array('for' => 'ModelField0')),
  3037. 'option A',
  3038. '/label',
  3039. 'br' => array(),
  3040. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3041. array('label' => array('for' => 'ModelField1')),
  3042. 'option B',
  3043. '/label',
  3044. '/fieldset'
  3045. );
  3046. $this->assertTags($result, $expected);
  3047. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3048. $expected = array(
  3049. 'div' => array('class' => 'input radio'),
  3050. 'fieldset' => array(),
  3051. 'legend' => array(),
  3052. 'Legend title',
  3053. '/legend',
  3054. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3055. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3056. array('label' => array('for' => 'NewsletterSubscribe0')),
  3057. 'Unsubscribe',
  3058. '/label',
  3059. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3060. array('label' => array('for' => 'NewsletterSubscribe1')),
  3061. 'Subscribe',
  3062. '/label',
  3063. '/fieldset',
  3064. '/div'
  3065. );
  3066. $this->assertTags($result, $expected);
  3067. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3068. $expected = array(
  3069. 'div' => array('class' => 'input radio'),
  3070. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3071. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3072. array('label' => array('for' => 'NewsletterSubscribe0')),
  3073. 'Unsubscribe',
  3074. '/label',
  3075. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3076. array('label' => array('for' => 'NewsletterSubscribe1')),
  3077. 'Subscribe',
  3078. '/label',
  3079. '/div'
  3080. );
  3081. $this->assertTags($result, $expected);
  3082. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3083. $expected = array(
  3084. 'div' => array('class' => 'input radio'),
  3085. 'fieldset' => array(),
  3086. 'legend' => array(),
  3087. 'Legend title',
  3088. '/legend',
  3089. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3090. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3091. 'Unsubscribe',
  3092. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3093. 'Subscribe',
  3094. '/fieldset',
  3095. '/div'
  3096. );
  3097. $this->assertTags($result, $expected);
  3098. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3099. $expected = array(
  3100. 'div' => array('class' => 'input radio'),
  3101. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3102. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3103. 'Unsubscribe',
  3104. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3105. 'Subscribe',
  3106. '/div'
  3107. );
  3108. $this->assertTags($result, $expected);
  3109. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3110. $expected = array(
  3111. 'div' => array('class' => 'input radio'),
  3112. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3113. 'Unsubscribe',
  3114. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3115. 'Subscribe',
  3116. '/div'
  3117. );
  3118. $this->assertTags($result, $expected);
  3119. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3120. $expected = array(
  3121. 'fieldset' => array(),
  3122. 'legend' => array(),
  3123. 'Gender',
  3124. '/legend',
  3125. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3126. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3127. array('label' => array('for' => 'EmployeeGenderMale')),
  3128. 'Male',
  3129. '/label',
  3130. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3131. array('label' => array('for' => 'EmployeeGenderFemale')),
  3132. 'Female',
  3133. '/label',
  3134. '/fieldset',
  3135. );
  3136. $this->assertTags($result, $expected);
  3137. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3138. $expected = array(
  3139. 'fieldset' => array(),
  3140. 'legend' => array(),
  3141. 'Gender',
  3142. '/legend',
  3143. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3144. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3145. array('label' => array('for' => 'OfficerGenderMale')),
  3146. 'Male',
  3147. '/label',
  3148. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3149. array('label' => array('for' => 'OfficerGenderFemale')),
  3150. 'Female',
  3151. '/label',
  3152. '/fieldset',
  3153. );
  3154. $this->assertTags($result, $expected);
  3155. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3159. 'label' => array('for' => 'Contact1Imrequired0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.1.field', array('option A'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3168. 'label' => array('for' => 'Model1Field0'),
  3169. 'option A',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio(
  3192. 'Model.field',
  3193. array('option A', 'option B'),
  3194. array('between' => 'I am between')
  3195. );
  3196. $expected = array(
  3197. 'fieldset' => array(),
  3198. 'legend' => array(),
  3199. 'Field',
  3200. '/legend',
  3201. 'I am between',
  3202. 'input' => array(
  3203. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3204. 'value' => '', 'id' => 'ModelField_'
  3205. ),
  3206. array('input' => array(
  3207. 'type' => 'radio', 'name' => 'data[Model][field]',
  3208. 'value' => '0', 'id' => 'ModelField0'
  3209. )),
  3210. array('label' => array('for' => 'ModelField0')),
  3211. 'option A',
  3212. '/label',
  3213. array('input' => array(
  3214. 'type' => 'radio', 'name' => 'data[Model][field]',
  3215. 'value' => '1', 'id' => 'ModelField1'
  3216. )),
  3217. array('label' => array('for' => 'ModelField1')),
  3218. 'option B',
  3219. '/label',
  3220. '/fieldset'
  3221. );
  3222. $this->assertTags($result, $expected);
  3223. }
  3224. /**
  3225. * Test that radios with a 0 value are selected under the correct conditions.
  3226. *
  3227. * @return void
  3228. */
  3229. public function testRadioOptionWithZeroValue() {
  3230. $expected = array(
  3231. 'fieldset' => array(),
  3232. 'legend' => array(),
  3233. 'Field',
  3234. '/legend',
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3236. array('label' => array('for' => 'ModelField1')),
  3237. 'Yes',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3240. array('label' => array('for' => 'ModelField0')),
  3241. 'No',
  3242. '/label',
  3243. '/fieldset'
  3244. );
  3245. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3246. $this->assertTags($result, $expected);
  3247. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3248. $this->assertTags($result, $expected);
  3249. $expected = array(
  3250. 'fieldset' => array(),
  3251. 'legend' => array(),
  3252. 'Field',
  3253. '/legend',
  3254. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3255. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3256. array('label' => array('for' => 'ModelField1')),
  3257. 'Yes',
  3258. '/label',
  3259. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3260. array('label' => array('for' => 'ModelField0')),
  3261. 'No',
  3262. '/label',
  3263. '/fieldset'
  3264. );
  3265. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3266. $this->assertTags($result, $expected);
  3267. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3268. $this->assertTags($result, $expected);
  3269. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3270. $this->assertTags($result, $expected);
  3271. }
  3272. /**
  3273. * test disabled radio options
  3274. *
  3275. * @return void
  3276. */
  3277. public function testRadioDisabled() {
  3278. $result = $this->Form->radio(
  3279. 'Model.field',
  3280. array('option A', 'option B'),
  3281. array('disabled' => array('option A'), 'value' => '0')
  3282. );
  3283. $expected = array(
  3284. 'fieldset' => array(),
  3285. 'legend' => array(),
  3286. 'Field',
  3287. '/legend',
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3289. array('label' => array('for' => 'ModelField0')),
  3290. 'option A',
  3291. '/label',
  3292. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3293. array('label' => array('for' => 'ModelField1')),
  3294. 'option B',
  3295. '/label',
  3296. '/fieldset'
  3297. );
  3298. $this->assertTags($result, $expected);
  3299. $result = $this->Form->radio(
  3300. 'Model.field',
  3301. array('option A', 'option B'),
  3302. array('disabled' => true, 'value' => '0')
  3303. );
  3304. $expected = array(
  3305. 'fieldset' => array(),
  3306. 'legend' => array(),
  3307. 'Field',
  3308. '/legend',
  3309. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3310. array('label' => array('for' => 'ModelField0')),
  3311. 'option A',
  3312. '/label',
  3313. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3314. array('label' => array('for' => 'ModelField1')),
  3315. 'option B',
  3316. '/label',
  3317. '/fieldset'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $result = $this->Form->radio(
  3321. 'Model.field',
  3322. array('option A', 'option B'),
  3323. array('disabled' => 'disabled', 'value' => '0')
  3324. );
  3325. $expected = array(
  3326. 'fieldset' => array(),
  3327. 'legend' => array(),
  3328. 'Field',
  3329. '/legend',
  3330. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3331. array('label' => array('for' => 'ModelField0')),
  3332. 'option A',
  3333. '/label',
  3334. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3335. array('label' => array('for' => 'ModelField1')),
  3336. 'option B',
  3337. '/label',
  3338. '/fieldset'
  3339. );
  3340. $this->assertTags($result, $expected);
  3341. }
  3342. /**
  3343. * test disabling the hidden input for radio buttons
  3344. *
  3345. * @return void
  3346. */
  3347. public function testRadioHiddenInputDisabling() {
  3348. $result = $this->Form->input('Model.1.field', array(
  3349. 'type' => 'radio',
  3350. 'options' => array('option A'),
  3351. 'hiddenField' => false
  3352. )
  3353. );
  3354. $expected = array(
  3355. 'div' => array('class' => 'input radio'),
  3356. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3357. 'label' => array('for' => 'Model1Field0'),
  3358. 'option A',
  3359. '/label',
  3360. '/div'
  3361. );
  3362. $this->assertTags($result, $expected);
  3363. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3364. $expected = array(
  3365. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3366. 'label' => array('for' => 'Model1Field0'),
  3367. 'option A',
  3368. '/label'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. }
  3372. /**
  3373. * test adding an empty option for radio buttons
  3374. *
  3375. * @return void
  3376. */
  3377. public function testRadioAddEmptyOption() {
  3378. $result = $this->Form->input('Model.1.field', array(
  3379. 'type' => 'radio',
  3380. 'options' => array('option A'),
  3381. 'empty' => true,
  3382. 'hiddenField' => false
  3383. ));
  3384. $expected = array(
  3385. 'div' => array('class' => 'input radio'),
  3386. 'fieldset' => array(),
  3387. 'legend' => array(),
  3388. 'Field',
  3389. '/legend',
  3390. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3391. array('label' => array('for' => 'Model1Field')),
  3392. __('empty'),
  3393. '/label',
  3394. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3395. array('label' => array('for' => 'Model1Field0')),
  3396. 'option A',
  3397. '/label',
  3398. '/fieldset',
  3399. '/div'
  3400. );
  3401. $this->assertTags($result, $expected);
  3402. $result = $this->Form->input('Model.1.field', array(
  3403. 'type' => 'radio',
  3404. 'options' => array('option A'),
  3405. 'empty' => 'CustomEmptyLabel',
  3406. 'hiddenField' => false
  3407. ));
  3408. $expected = array(
  3409. 'div' => array('class' => 'input radio'),
  3410. 'fieldset' => array(),
  3411. 'legend' => array(),
  3412. 'Field',
  3413. '/legend',
  3414. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3415. array('label' => array('for' => 'Model1Field')),
  3416. 'CustomEmptyLabel',
  3417. '/label',
  3418. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3419. array('label' => array('for' => 'Model1Field0')),
  3420. 'option A',
  3421. '/label',
  3422. '/fieldset',
  3423. '/div'
  3424. );
  3425. $this->assertTags($result, $expected);
  3426. $result = $this->Form->input('Model.1.field', array(
  3427. 'type' => 'radio',
  3428. 'options' => array('option A'),
  3429. 'empty' => false,
  3430. 'hiddenField' => false
  3431. ));
  3432. $this->assertTextNotContains('"Model1Field"', $result);
  3433. }
  3434. /**
  3435. * testSelect method
  3436. *
  3437. * Test select element generation.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testSelect() {
  3442. $result = $this->Form->select('Model.field', array());
  3443. $expected = array(
  3444. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3445. array('option' => array('value' => '')),
  3446. '/option',
  3447. '/select'
  3448. );
  3449. $this->assertTags($result, $expected);
  3450. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3451. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3452. $expected = array(
  3453. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3454. array('option' => array('value' => '')),
  3455. '/option',
  3456. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3457. 'good',
  3458. '/option',
  3459. array('option' => array('value' => 'other')),
  3460. 'bad',
  3461. '/option',
  3462. '/select'
  3463. );
  3464. $this->assertTags($result, $expected);
  3465. $this->Form->request->data = array();
  3466. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3467. $expected = array(
  3468. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3469. array('option' => array('value' => '')),
  3470. '/option',
  3471. array('option' => array('value' => 'value')),
  3472. 'good',
  3473. '/option',
  3474. array('option' => array('value' => 'other')),
  3475. 'bad',
  3476. '/option',
  3477. '/select'
  3478. );
  3479. $this->assertTags($result, $expected);
  3480. $result = $this->Form->select(
  3481. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3482. array('empty' => false)
  3483. );
  3484. $expected = array(
  3485. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3486. array('option' => array('value' => 'first')),
  3487. 'first &quot;html&quot; &lt;chars&gt;',
  3488. '/option',
  3489. array('option' => array('value' => 'second')),
  3490. 'value',
  3491. '/option',
  3492. '/select'
  3493. );
  3494. $this->assertTags($result, $expected);
  3495. $result = $this->Form->select(
  3496. 'Model.field',
  3497. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3498. array('escape' => false, 'empty' => false)
  3499. );
  3500. $expected = array(
  3501. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3502. array('option' => array('value' => 'first')),
  3503. 'first "html" <chars>',
  3504. '/option',
  3505. array('option' => array('value' => 'second')),
  3506. 'value',
  3507. '/option',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. $options = array(
  3512. array('value' => 'first', 'name' => 'First'),
  3513. array('value' => 'first', 'name' => 'Another First'),
  3514. );
  3515. $result = $this->Form->select(
  3516. 'Model.field',
  3517. $options,
  3518. array('escape' => false, 'empty' => false)
  3519. );
  3520. $expected = array(
  3521. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3522. array('option' => array('value' => 'first')),
  3523. 'First',
  3524. '/option',
  3525. array('option' => array('value' => 'first')),
  3526. 'Another First',
  3527. '/option',
  3528. '/select'
  3529. );
  3530. $this->assertTags($result, $expected);
  3531. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3532. $result = $this->Form->select(
  3533. 'Model.contact_id',
  3534. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3535. array('escape' => false, 'empty' => 'pick something')
  3536. );
  3537. $expected = array(
  3538. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3539. array('option' => array('value' => '')), 'pick something', '/option',
  3540. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3541. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3542. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3543. '/select'
  3544. );
  3545. $this->assertTags($result, $expected);
  3546. $this->Form->request->data['Model']['field'] = 0;
  3547. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3548. $expected = array(
  3549. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3550. array('option' => array('value' => '')), '/option',
  3551. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3552. array('option' => array('value' => '1')), 'Yes', '/option',
  3553. '/select'
  3554. );
  3555. $this->assertTags($result, $expected);
  3556. }
  3557. /**
  3558. * test that select() with optiongroups listens to the escape param.
  3559. *
  3560. * @return void
  3561. */
  3562. public function testSelectOptionGroupEscaping() {
  3563. $options = array(
  3564. '>< Key' => array(
  3565. 1 => 'One',
  3566. 2 => 'Two'
  3567. )
  3568. );
  3569. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3570. $expected = array(
  3571. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3572. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3573. array('option' => array('value' => '1')), 'One', '/option',
  3574. array('option' => array('value' => '2')), 'Two', '/option',
  3575. '/optgroup',
  3576. '/select'
  3577. );
  3578. $this->assertTags($result, $expected);
  3579. $options = array(
  3580. '>< Key' => array(
  3581. 1 => 'One',
  3582. 2 => 'Two'
  3583. )
  3584. );
  3585. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3586. $expected = array(
  3587. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3588. 'optgroup' => array('label' => '>< Key'),
  3589. array('option' => array('value' => '1')), 'One', '/option',
  3590. array('option' => array('value' => '2')), 'Two', '/option',
  3591. '/optgroup',
  3592. '/select'
  3593. );
  3594. $this->assertTags($result, $expected);
  3595. }
  3596. /**
  3597. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3598. *
  3599. * @return void
  3600. */
  3601. public function testSelectWithNullAttributes() {
  3602. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3603. $expected = array(
  3604. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3605. array('option' => array('value' => '0')),
  3606. 'first',
  3607. '/option',
  3608. array('option' => array('value' => '1')),
  3609. 'second',
  3610. '/option',
  3611. '/select'
  3612. );
  3613. $this->assertTags($result, $expected);
  3614. }
  3615. /**
  3616. * testNestedSelect method
  3617. *
  3618. * test select element generation with optgroups
  3619. *
  3620. * @return void
  3621. */
  3622. public function testNestedSelect() {
  3623. $result = $this->Form->select(
  3624. 'Model.field',
  3625. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3626. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3627. )), array('empty' => false)
  3628. );
  3629. $expected = array(
  3630. 'select' => array('name' => 'data[Model][field]',
  3631. 'id' => 'ModelField'),
  3632. array('option' => array('value' => 1)),
  3633. 'One',
  3634. '/option',
  3635. array('option' => array('value' => 2)),
  3636. 'Two',
  3637. '/option',
  3638. array('optgroup' => array('label' => 'Three')),
  3639. array('option' => array('value' => 4)),
  3640. 'Four',
  3641. '/option',
  3642. array('option' => array('value' => 5)),
  3643. 'Five',
  3644. '/option',
  3645. '/optgroup',
  3646. '/select'
  3647. );
  3648. $this->assertTags($result, $expected);
  3649. $result = $this->Form->select(
  3650. 'Model.field',
  3651. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3652. array('showParents' => true, 'empty' => false)
  3653. );
  3654. $expected = array(
  3655. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3656. array('option' => array('value' => 1)),
  3657. 'One',
  3658. '/option',
  3659. array('option' => array('value' => 2)),
  3660. 'Two',
  3661. '/option',
  3662. array('optgroup' => array('label' => 'Three')),
  3663. array('option' => array('value' => 3)),
  3664. 'Three',
  3665. '/option',
  3666. array('option' => array('value' => 4)),
  3667. 'Four',
  3668. '/option',
  3669. '/optgroup',
  3670. '/select'
  3671. );
  3672. $this->assertTags($result, $expected);
  3673. }
  3674. /**
  3675. * testSelectMultiple method
  3676. *
  3677. * test generation of multiple select elements
  3678. *
  3679. * @return void
  3680. */
  3681. public function testSelectMultiple() {
  3682. $options = array('first', 'second', 'third');
  3683. $result = $this->Form->select(
  3684. 'Model.multi_field', $options, array('multiple' => true)
  3685. );
  3686. $expected = array(
  3687. 'input' => array(
  3688. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3689. ),
  3690. 'select' => array(
  3691. 'name' => 'data[Model][multi_field][]',
  3692. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3693. ),
  3694. array('option' => array('value' => '0')),
  3695. 'first',
  3696. '/option',
  3697. array('option' => array('value' => '1')),
  3698. 'second',
  3699. '/option',
  3700. array('option' => array('value' => '2')),
  3701. 'third',
  3702. '/option',
  3703. '/select'
  3704. );
  3705. $this->assertTags($result, $expected);
  3706. $result = $this->Form->select(
  3707. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3708. );
  3709. $expected = array(
  3710. 'input' => array(
  3711. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3712. ),
  3713. 'select' => array(
  3714. 'name' => 'data[Model][multi_field][]',
  3715. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3716. ),
  3717. array('option' => array('value' => '0')),
  3718. 'first',
  3719. '/option',
  3720. array('option' => array('value' => '1')),
  3721. 'second',
  3722. '/option',
  3723. array('option' => array('value' => '2')),
  3724. 'third',
  3725. '/option',
  3726. '/select'
  3727. );
  3728. $this->assertTags($result, $expected);
  3729. $result = $this->Form->select(
  3730. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3731. );
  3732. $expected = array(
  3733. 'input' => array(
  3734. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3735. ),
  3736. 'select' => array(
  3737. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3738. 'multiple' => 'multiple'
  3739. ),
  3740. array('option' => array('value' => '0', 'selected' => 'selected')),
  3741. 'first',
  3742. '/option',
  3743. array('option' => array('value' => '1', 'selected' => 'selected')),
  3744. 'second',
  3745. '/option',
  3746. array('option' => array('value' => '2')),
  3747. 'third',
  3748. '/option',
  3749. '/select'
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $result = $this->Form->select(
  3753. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3754. );
  3755. $expected = array(
  3756. 'select' => array(
  3757. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3758. ),
  3759. array('option' => array('value' => '0', 'selected' => 'selected')),
  3760. 'first',
  3761. '/option',
  3762. array('option' => array('value' => '1', 'selected' => 'selected')),
  3763. 'second',
  3764. '/option',
  3765. array('option' => array('value' => '2')),
  3766. 'third',
  3767. '/option',
  3768. '/select'
  3769. );
  3770. $this->assertTags($result, $expected);
  3771. }
  3772. /**
  3773. * test generation of habtm select boxes.
  3774. *
  3775. * @return void
  3776. */
  3777. public function testHabtmSelectBox() {
  3778. $this->View->viewVars['contactTags'] = array(
  3779. 1 => 'blue',
  3780. 2 => 'red',
  3781. 3 => 'green'
  3782. );
  3783. $this->Form->request->data = array(
  3784. 'Contact' => array(),
  3785. 'ContactTag' => array(
  3786. array(
  3787. 'id' => 1,
  3788. 'name' => 'blue'
  3789. ),
  3790. array(
  3791. 'id' => 3,
  3792. 'name' => 'green'
  3793. )
  3794. )
  3795. );
  3796. $this->Form->create('Contact');
  3797. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3798. $expected = array(
  3799. 'input' => array(
  3800. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3801. ),
  3802. 'select' => array(
  3803. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3804. 'multiple' => 'multiple'
  3805. ),
  3806. array('option' => array('value' => '1', 'selected' => 'selected')),
  3807. 'blue',
  3808. '/option',
  3809. array('option' => array('value' => '2')),
  3810. 'red',
  3811. '/option',
  3812. array('option' => array('value' => '3', 'selected' => 'selected')),
  3813. 'green',
  3814. '/option',
  3815. '/select'
  3816. );
  3817. $this->assertTags($result, $expected);
  3818. }
  3819. /**
  3820. * test generation of multi select elements in checkbox format
  3821. *
  3822. * @return void
  3823. */
  3824. public function testSelectMultipleCheckboxes() {
  3825. $result = $this->Form->select(
  3826. 'Model.multi_field',
  3827. array('first', 'second', 'third'),
  3828. array('multiple' => 'checkbox')
  3829. );
  3830. $expected = array(
  3831. 'input' => array(
  3832. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3833. ),
  3834. array('div' => array('class' => 'checkbox')),
  3835. array('input' => array(
  3836. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3837. 'value' => '0', 'id' => 'ModelMultiField0'
  3838. )),
  3839. array('label' => array('for' => 'ModelMultiField0')),
  3840. 'first',
  3841. '/label',
  3842. '/div',
  3843. array('div' => array('class' => 'checkbox')),
  3844. array('input' => array(
  3845. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3846. 'value' => '1', 'id' => 'ModelMultiField1'
  3847. )),
  3848. array('label' => array('for' => 'ModelMultiField1')),
  3849. 'second',
  3850. '/label',
  3851. '/div',
  3852. array('div' => array('class' => 'checkbox')),
  3853. array('input' => array(
  3854. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3855. 'value' => '2', 'id' => 'ModelMultiField2'
  3856. )),
  3857. array('label' => array('for' => 'ModelMultiField2')),
  3858. 'third',
  3859. '/label',
  3860. '/div'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. $result = $this->Form->select(
  3864. 'Model.multi_field',
  3865. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3866. array('multiple' => 'checkbox')
  3867. );
  3868. $expected = array(
  3869. 'input' => array(
  3870. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3871. ),
  3872. array('div' => array('class' => 'checkbox')),
  3873. array('input' => array(
  3874. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3875. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3876. )),
  3877. array('label' => array('for' => 'ModelMultiFieldA')),
  3878. 'first',
  3879. '/label',
  3880. '/div',
  3881. array('div' => array('class' => 'checkbox')),
  3882. array('input' => array(
  3883. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3884. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3885. )),
  3886. array('label' => array('for' => 'ModelMultiFieldB')),
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. array('div' => array('class' => 'checkbox')),
  3891. array('input' => array(
  3892. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3893. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3894. )),
  3895. array('label' => array('for' => 'ModelMultiFieldC')),
  3896. 'third',
  3897. '/label',
  3898. '/div'
  3899. );
  3900. $this->assertTags($result, $expected);
  3901. $result = $this->Form->select(
  3902. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3903. );
  3904. $expected = array(
  3905. 'input' => array(
  3906. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3907. ),
  3908. array('div' => array('class' => 'checkbox')),
  3909. array('input' => array(
  3910. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3911. 'value' => '1', 'id' => 'ModelMultiField1'
  3912. )),
  3913. array('label' => array('for' => 'ModelMultiField1')),
  3914. 'first',
  3915. '/label',
  3916. '/div'
  3917. );
  3918. $this->assertTags($result, $expected);
  3919. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3920. $result = $this->Form->select(
  3921. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3922. );
  3923. $expected = array(
  3924. 'input' => array(
  3925. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3926. ),
  3927. array('div' => array('class' => 'checkbox')),
  3928. array('input' => array(
  3929. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3930. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3931. )),
  3932. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3933. 'first',
  3934. '/label',
  3935. '/div',
  3936. array('div' => array('class' => 'checkbox')),
  3937. array('input' => array(
  3938. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3939. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3940. )),
  3941. array('label' => array('for' => 'ModelTagsArray')),
  3942. 'Array',
  3943. '/label',
  3944. '/div'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. }
  3948. /**
  3949. * test multiple checkboxes with div styles.
  3950. *
  3951. * @return void
  3952. */
  3953. public function testSelectMultipleCheckboxDiv() {
  3954. $result = $this->Form->select(
  3955. 'Model.tags',
  3956. array('first', 'second'),
  3957. array('multiple' => 'checkbox', 'class' => 'my-class')
  3958. );
  3959. $expected = array(
  3960. 'input' => array(
  3961. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3962. ),
  3963. array('div' => array('class' => 'my-class')),
  3964. array('input' => array(
  3965. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3966. 'value' => '0', 'id' => 'ModelTags0'
  3967. )),
  3968. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3969. '/div',
  3970. array('div' => array('class' => 'my-class')),
  3971. array('input' => array(
  3972. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3973. 'value' => '1', 'id' => 'ModelTags1'
  3974. )),
  3975. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3976. '/div'
  3977. );
  3978. $this->assertTags($result, $expected);
  3979. $result = $this->Form->input('Model.tags', array(
  3980. 'options' => array('first', 'second'),
  3981. 'multiple' => 'checkbox',
  3982. 'class' => 'my-class',
  3983. 'div' => false,
  3984. 'label' => false
  3985. ));
  3986. $this->assertTags($result, $expected);
  3987. $Contact = ClassRegistry::getObject('Contact');
  3988. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3989. $result = $this->Form->input('Contact.tags', array(
  3990. 'options' => array('one'),
  3991. 'multiple' => 'checkbox',
  3992. 'label' => false,
  3993. 'div' => false
  3994. ));
  3995. $expected = array(
  3996. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3997. array('div' => array('class' => 'checkbox form-error')),
  3998. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3999. array('label' => array('for' => 'ContactTags0')),
  4000. 'one',
  4001. '/label',
  4002. '/div'
  4003. );
  4004. $this->assertTags($result, $expected);
  4005. $result = $this->Form->input('Contact.tags', array(
  4006. 'options' => array('one'),
  4007. 'multiple' => 'checkbox',
  4008. 'class' => 'mycheckbox',
  4009. 'label' => false,
  4010. 'div' => false
  4011. ));
  4012. $expected = array(
  4013. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4014. array('div' => array('class' => 'mycheckbox form-error')),
  4015. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4016. array('label' => array('for' => 'ContactTags0')),
  4017. 'one',
  4018. '/label',
  4019. '/div'
  4020. );
  4021. $this->assertTags($result, $expected);
  4022. }
  4023. /**
  4024. * Checks the security hash array generated for multiple-input checkbox elements
  4025. *
  4026. * @return void
  4027. */
  4028. public function testSelectMultipleCheckboxSecurity() {
  4029. $this->Form->request['_Token'] = array('key' => 'testKey');
  4030. $this->assertEquals(array(), $this->Form->fields);
  4031. $result = $this->Form->select(
  4032. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4033. array('multiple' => 'checkbox')
  4034. );
  4035. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4036. $result = $this->Form->secure($this->Form->fields);
  4037. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4038. $this->assertRegExp('/"' . $key . '"/', $result);
  4039. }
  4040. /**
  4041. * Multiple select elements should always be secured as they always participate
  4042. * in the POST data.
  4043. *
  4044. * @return void
  4045. */
  4046. public function testSelectMultipleSecureWithNoOptions() {
  4047. $this->Form->request['_Token'] = array('key' => 'testkey');
  4048. $this->assertEquals(array(), $this->Form->fields);
  4049. $result = $this->Form->select(
  4050. 'Model.select',
  4051. array(),
  4052. array('multiple' => true)
  4053. );
  4054. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4055. }
  4056. /**
  4057. * When a select box has no options it should not be added to the fields list
  4058. * as it always fail post validation.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testSelectNoSecureWithNoOptions() {
  4063. $this->Form->request['_Token'] = array('key' => 'testkey');
  4064. $this->assertEquals(array(), $this->Form->fields);
  4065. $this->Form->select(
  4066. 'Model.select',
  4067. array()
  4068. );
  4069. $this->assertEquals(array(), $this->Form->fields);
  4070. $this->Form->select(
  4071. 'Model.select',
  4072. array(),
  4073. array('empty' => true)
  4074. );
  4075. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4076. }
  4077. /**
  4078. * testInputMultipleCheckboxes method
  4079. *
  4080. * test input() resulting in multi select elements being generated.
  4081. *
  4082. * @return void
  4083. */
  4084. public function testInputMultipleCheckboxes() {
  4085. $result = $this->Form->input('Model.multi_field', array(
  4086. 'options' => array('first', 'second', 'third'),
  4087. 'multiple' => 'checkbox'
  4088. ));
  4089. $expected = array(
  4090. array('div' => array('class' => 'input select')),
  4091. array('label' => array('for' => 'ModelMultiField')),
  4092. 'Multi Field',
  4093. '/label',
  4094. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4095. array('div' => array('class' => 'checkbox')),
  4096. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4097. array('label' => array('for' => 'ModelMultiField0')),
  4098. 'first',
  4099. '/label',
  4100. '/div',
  4101. array('div' => array('class' => 'checkbox')),
  4102. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4103. array('label' => array('for' => 'ModelMultiField1')),
  4104. 'second',
  4105. '/label',
  4106. '/div',
  4107. array('div' => array('class' => 'checkbox')),
  4108. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4109. array('label' => array('for' => 'ModelMultiField2')),
  4110. 'third',
  4111. '/label',
  4112. '/div',
  4113. '/div'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. $result = $this->Form->input('Model.multi_field', array(
  4117. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4118. 'multiple' => 'checkbox'
  4119. ));
  4120. $expected = array(
  4121. array('div' => array('class' => 'input select')),
  4122. array('label' => array('for' => 'ModelMultiField')),
  4123. 'Multi Field',
  4124. '/label',
  4125. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4126. array('div' => array('class' => 'checkbox')),
  4127. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4128. array('label' => array('for' => 'ModelMultiFieldA')),
  4129. 'first',
  4130. '/label',
  4131. '/div',
  4132. array('div' => array('class' => 'checkbox')),
  4133. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4134. array('label' => array('for' => 'ModelMultiFieldB')),
  4135. 'second',
  4136. '/label',
  4137. '/div',
  4138. array('div' => array('class' => 'checkbox')),
  4139. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4140. array('label' => array('for' => 'ModelMultiFieldC')),
  4141. 'third',
  4142. '/label',
  4143. '/div',
  4144. '/div'
  4145. );
  4146. $this->assertTags($result, $expected);
  4147. $result = $this->Form->input('Model.multi_field', array(
  4148. 'options' => array('1' => 'first'),
  4149. 'multiple' => 'checkbox',
  4150. 'label' => false,
  4151. 'div' => false
  4152. ));
  4153. $expected = array(
  4154. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4155. array('div' => array('class' => 'checkbox')),
  4156. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4157. array('label' => array('for' => 'ModelMultiField1')),
  4158. 'first',
  4159. '/label',
  4160. '/div'
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. $result = $this->Form->input('Model.multi_field', array(
  4164. 'options' => array('2' => 'second'),
  4165. 'multiple' => 'checkbox',
  4166. 'label' => false,
  4167. 'div' => false
  4168. ));
  4169. $expected = array(
  4170. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4171. array('div' => array('class' => 'checkbox')),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4173. array('label' => array('for' => 'ModelMultiField2')),
  4174. 'second',
  4175. '/label',
  4176. '/div'
  4177. );
  4178. $this->assertTags($result, $expected);
  4179. }
  4180. /**
  4181. * testSelectHiddenFieldOmission method
  4182. *
  4183. * test that select() with 'hiddenField' => false omits the hidden field
  4184. *
  4185. * @return void
  4186. */
  4187. public function testSelectHiddenFieldOmission() {
  4188. $result = $this->Form->select('Model.multi_field',
  4189. array('first', 'second'),
  4190. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4191. );
  4192. $expected = array(
  4193. array('div' => array('class' => 'checkbox')),
  4194. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4195. array('label' => array('for' => 'ModelMultiField0')),
  4196. 'first',
  4197. '/label',
  4198. '/div',
  4199. array('div' => array('class' => 'checkbox')),
  4200. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4201. array('label' => array('for' => 'ModelMultiField1')),
  4202. 'second',
  4203. '/label',
  4204. '/div'
  4205. );
  4206. $this->assertTags($result, $expected);
  4207. $result = $this->Form->input('Model.multi_field', array(
  4208. 'options' => array('first', 'second'),
  4209. 'multiple' => 'checkbox',
  4210. 'hiddenField' => false
  4211. ));
  4212. $expected = array(
  4213. array('div' => array('class' => 'input select')),
  4214. array('label' => array('for' => 'ModelMultiField')),
  4215. 'Multi Field',
  4216. '/label',
  4217. array('div' => array('class' => 'checkbox')),
  4218. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4219. array('label' => array('for' => 'ModelMultiField0')),
  4220. 'first',
  4221. '/label',
  4222. '/div',
  4223. array('div' => array('class' => 'checkbox')),
  4224. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4225. array('label' => array('for' => 'ModelMultiField1')),
  4226. 'second',
  4227. '/label',
  4228. '/div',
  4229. '/div'
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. }
  4233. /**
  4234. * test that select() with multiple = checkbox works with overriding name attribute.
  4235. *
  4236. * @return void
  4237. */
  4238. public function testSelectCheckboxMultipleOverrideName() {
  4239. $result = $this->Form->input('category', array(
  4240. 'type' => 'select',
  4241. 'multiple' => 'checkbox',
  4242. 'name' => 'data[fish]',
  4243. 'options' => array('1', '2'),
  4244. 'div' => false,
  4245. 'label' => false,
  4246. ));
  4247. $expected = array(
  4248. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4249. array('div' => array('class' => 'checkbox')),
  4250. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4251. array('label' => array('for' => 'Category0')), '1', '/label',
  4252. '/div',
  4253. array('div' => array('class' => 'checkbox')),
  4254. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4255. array('label' => array('for' => 'Category1')), '2', '/label',
  4256. '/div'
  4257. );
  4258. $this->assertTags($result, $expected);
  4259. }
  4260. /**
  4261. * Test that 'id' overrides all the checkbox id's as well.
  4262. *
  4263. * @return void
  4264. */
  4265. public function testSelectCheckboxMultipleId() {
  4266. $result = $this->Form->select(
  4267. 'Model.multi_field',
  4268. array('first', 'second', 'third'),
  4269. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4270. );
  4271. $expected = array(
  4272. 'input' => array(
  4273. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4274. ),
  4275. array('div' => array('class' => 'checkbox')),
  4276. array('input' => array(
  4277. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4278. 'value' => '0', 'id' => 'CustomId0'
  4279. )),
  4280. array('label' => array('for' => 'CustomId0')),
  4281. 'first',
  4282. '/label',
  4283. '/div',
  4284. array('div' => array('class' => 'checkbox')),
  4285. array('input' => array(
  4286. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4287. 'value' => '1', 'id' => 'CustomId1'
  4288. )),
  4289. array('label' => array('for' => 'CustomId1')),
  4290. 'second',
  4291. '/label',
  4292. '/div',
  4293. array('div' => array('class' => 'checkbox')),
  4294. array('input' => array(
  4295. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4296. 'value' => '2', 'id' => 'CustomId2'
  4297. )),
  4298. array('label' => array('for' => 'CustomId2')),
  4299. 'third',
  4300. '/label',
  4301. '/div'
  4302. );
  4303. $this->assertTags($result, $expected);
  4304. }
  4305. /**
  4306. * testCheckbox method
  4307. *
  4308. * Test generation of checkboxes
  4309. *
  4310. * @return void
  4311. */
  4312. public function testCheckbox() {
  4313. $result = $this->Form->checkbox('Model.field');
  4314. $expected = array(
  4315. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4316. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4317. );
  4318. $this->assertTags($result, $expected);
  4319. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4320. $expected = array(
  4321. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4322. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. $Contact = ClassRegistry::getObject('Contact');
  4326. $Contact->validationErrors['field'] = 1;
  4327. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4328. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4329. $expected = array(
  4330. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4331. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4332. );
  4333. $this->assertTags($result, $expected);
  4334. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4335. $expected = array(
  4336. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4337. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4338. );
  4339. $this->assertTags($result, $expected);
  4340. $this->Form->request->data['Contact']['field'] = '';
  4341. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4342. $expected = array(
  4343. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4344. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4345. );
  4346. $this->assertTags($result, $expected);
  4347. $Contact->validationErrors = array();
  4348. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4349. $expected = array(
  4350. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4351. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4352. );
  4353. $this->assertTags($result, $expected);
  4354. $this->Form->request->data['Contact']['published'] = 1;
  4355. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4356. $expected = array(
  4357. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4358. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4359. );
  4360. $this->assertTags($result, $expected);
  4361. $this->Form->request->data['Contact']['published'] = 0;
  4362. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4363. $expected = array(
  4364. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4365. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4366. );
  4367. $this->assertTags($result, $expected);
  4368. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4369. $expected = array(
  4370. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4371. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4372. );
  4373. $this->assertTags($result, $expected);
  4374. $result = $this->Form->checkbox('CustomField.1.value');
  4375. $expected = array(
  4376. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4377. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4378. );
  4379. $this->assertTags($result, $expected);
  4380. }
  4381. /**
  4382. * test checkbox() with a custom name attribute
  4383. *
  4384. * @return void
  4385. */
  4386. public function testCheckboxCustomNameAttribute() {
  4387. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4388. $expected = array(
  4389. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4390. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4391. );
  4392. $this->assertTags($result, $expected);
  4393. }
  4394. /**
  4395. * test the checked option for checkboxes.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testCheckboxCheckedOption() {
  4400. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4401. $expected = array(
  4402. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4403. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4407. $expected = array(
  4408. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4409. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4410. );
  4411. $this->assertTags($result, $expected);
  4412. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4413. $expected = array(
  4414. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4415. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4416. );
  4417. $this->assertTags($result, $expected);
  4418. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4419. $expected = array(
  4420. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4421. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4422. );
  4423. $this->assertTags($result, $expected);
  4424. $this->Form->request->data['Model']['field'] = 1;
  4425. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4426. $expected = array(
  4427. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4428. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4429. );
  4430. $this->assertTags($result, $expected);
  4431. }
  4432. /**
  4433. * Test that disabled attribute works on both the checkbox and hidden input.
  4434. *
  4435. * @return void
  4436. */
  4437. public function testCheckboxDisabling() {
  4438. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4439. $expected = array(
  4440. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4441. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4442. );
  4443. $this->assertTags($result, $expected);
  4444. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4445. $expected = array(
  4446. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4447. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4448. );
  4449. $this->assertTags($result, $expected);
  4450. }
  4451. /**
  4452. * Test that the hidden input for checkboxes can be omitted or set to a
  4453. * specific value.
  4454. *
  4455. * @return void
  4456. */
  4457. public function testCheckboxHiddenField() {
  4458. $result = $this->Form->input('UserForm.something', array(
  4459. 'type' => 'checkbox',
  4460. 'hiddenField' => false
  4461. ));
  4462. $expected = array(
  4463. 'div' => array('class' => 'input checkbox'),
  4464. array('input' => array(
  4465. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4466. 'value' => '1', 'id' => 'UserFormSomething'
  4467. )),
  4468. 'label' => array('for' => 'UserFormSomething'),
  4469. 'Something',
  4470. '/label',
  4471. '/div'
  4472. );
  4473. $this->assertTags($result, $expected);
  4474. $result = $this->Form->input('UserForm.something', array(
  4475. 'type' => 'checkbox',
  4476. 'value' => 'Y',
  4477. 'hiddenField' => 'N',
  4478. ));
  4479. $expected = array(
  4480. 'div' => array('class' => 'input checkbox'),
  4481. array('input' => array(
  4482. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4483. 'value' => 'N', 'id' => 'UserFormSomething_'
  4484. )),
  4485. array('input' => array(
  4486. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4487. 'value' => 'Y', 'id' => 'UserFormSomething'
  4488. )),
  4489. 'label' => array('for' => 'UserFormSomething'),
  4490. 'Something',
  4491. '/label',
  4492. '/div'
  4493. );
  4494. $this->assertTags($result, $expected);
  4495. }
  4496. /**
  4497. * testDateTime method
  4498. *
  4499. * Test generation of date/time select elements
  4500. *
  4501. * @return void
  4502. */
  4503. public function testDateTime() {
  4504. extract($this->dateRegex);
  4505. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4506. $now = strtotime('now');
  4507. $expected = array(
  4508. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4509. $daysRegex,
  4510. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4511. date('j', $now),
  4512. '/option',
  4513. '*/select',
  4514. '-',
  4515. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4516. $monthsRegex,
  4517. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4518. date('F', $now),
  4519. '/option',
  4520. '*/select',
  4521. '-',
  4522. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4523. $yearsRegex,
  4524. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4525. date('Y', $now),
  4526. '/option',
  4527. '*/select',
  4528. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4529. $hoursRegex,
  4530. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4531. date('g', $now),
  4532. '/option',
  4533. '*/select',
  4534. ':',
  4535. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4536. $minutesRegex,
  4537. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4538. date('i', $now),
  4539. '/option',
  4540. '*/select',
  4541. ' ',
  4542. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4543. $meridianRegex,
  4544. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4545. date('a', $now),
  4546. '/option',
  4547. '*/select'
  4548. );
  4549. $this->assertTags($result, $expected);
  4550. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4551. $expected = array(
  4552. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4553. $daysRegex,
  4554. array('option' => array('value' => '')),
  4555. '/option',
  4556. '*/select',
  4557. '-',
  4558. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4559. $monthsRegex,
  4560. array('option' => array('value' => '')),
  4561. '/option',
  4562. '*/select',
  4563. '-',
  4564. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4565. $yearsRegex,
  4566. array('option' => array('value' => '')),
  4567. '/option',
  4568. '*/select',
  4569. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4570. $hoursRegex,
  4571. array('option' => array('value' => '')),
  4572. '/option',
  4573. '*/select',
  4574. ':',
  4575. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4576. $minutesRegex,
  4577. array('option' => array('value' => '')),
  4578. '/option',
  4579. '*/select',
  4580. ' ',
  4581. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4582. $meridianRegex,
  4583. array('option' => array('value' => '')),
  4584. '/option',
  4585. '*/select'
  4586. );
  4587. $this->assertTags($result, $expected);
  4588. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4589. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4590. $this->assertTags($result, $expected);
  4591. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4592. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4593. $this->assertTags($result, $expected);
  4594. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4595. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4596. $expected = array(
  4597. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4598. $daysRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select',
  4602. '-',
  4603. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4604. $monthsRegex,
  4605. array('option' => array('value' => '')),
  4606. '/option',
  4607. '*/select',
  4608. '-',
  4609. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4610. $yearsRegex,
  4611. array('option' => array('value' => '')),
  4612. '/option',
  4613. '*/select',
  4614. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4615. $hoursRegex,
  4616. array('option' => array('value' => '')),
  4617. '/option',
  4618. '*/select',
  4619. ':',
  4620. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4621. $minutesRegex,
  4622. array('option' => array('value' => '')),
  4623. '/option',
  4624. array('option' => array('value' => '00')),
  4625. '00',
  4626. '/option',
  4627. array('option' => array('value' => '05')),
  4628. '05',
  4629. '/option',
  4630. array('option' => array('value' => '10')),
  4631. '10',
  4632. '/option',
  4633. '*/select',
  4634. ' ',
  4635. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4636. $meridianRegex,
  4637. array('option' => array('value' => '')),
  4638. '/option',
  4639. '*/select'
  4640. );
  4641. $this->assertTags($result, $expected);
  4642. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4643. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4644. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4645. $this->Form->request->data['Contact']['data'] = null;
  4646. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4647. $expected = array(
  4648. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4649. $daysRegex,
  4650. array('option' => array('value' => '')),
  4651. '/option',
  4652. '*/select',
  4653. '-',
  4654. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4655. $monthsRegex,
  4656. array('option' => array('value' => '')),
  4657. '/option',
  4658. '*/select',
  4659. '-',
  4660. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4661. $yearsRegex,
  4662. array('option' => array('value' => '')),
  4663. '/option',
  4664. '*/select',
  4665. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4666. $hoursRegex,
  4667. array('option' => array('value' => '')),
  4668. '/option',
  4669. '*/select',
  4670. ':',
  4671. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4672. $minutesRegex,
  4673. array('option' => array('value' => '')),
  4674. '/option',
  4675. '*/select',
  4676. ' ',
  4677. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4678. $meridianRegex,
  4679. array('option' => array('value' => '')),
  4680. '/option',
  4681. '*/select'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4685. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4686. $now = strtotime($this->Form->data['Model']['field']);
  4687. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4688. $expected = array(
  4689. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4690. $daysRegex,
  4691. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4692. date('j', $now),
  4693. '/option',
  4694. '*/select',
  4695. '-',
  4696. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4697. $monthsRegex,
  4698. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4699. date('F', $now),
  4700. '/option',
  4701. '*/select',
  4702. '-',
  4703. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4704. $yearsRegex,
  4705. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4706. date('Y', $now),
  4707. '/option',
  4708. '*/select',
  4709. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4710. $hoursRegex,
  4711. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4712. date('g', $now),
  4713. '/option',
  4714. '*/select',
  4715. ':',
  4716. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4717. $minutesRegex,
  4718. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4719. date('i', $now),
  4720. '/option',
  4721. '*/select',
  4722. ' ',
  4723. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4724. $meridianRegex,
  4725. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4726. date('a', $now),
  4727. '/option',
  4728. '*/select'
  4729. );
  4730. $this->assertTags($result, $expected);
  4731. $selected = strtotime('2008-10-26 12:33:00');
  4732. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4733. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4734. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4735. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4736. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4737. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4738. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4739. $this->Form->create('Contact');
  4740. $result = $this->Form->input('published');
  4741. $now = strtotime('now');
  4742. $expected = array(
  4743. 'div' => array('class' => 'input date'),
  4744. 'label' => array('for' => 'ContactPublishedMonth'),
  4745. 'Published',
  4746. '/label',
  4747. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4748. $monthsRegex,
  4749. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4750. date('F', $now),
  4751. '/option',
  4752. '*/select',
  4753. '-',
  4754. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4755. $daysRegex,
  4756. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4757. date('j', $now),
  4758. '/option',
  4759. '*/select',
  4760. '-',
  4761. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4762. $yearsRegex,
  4763. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4764. date('Y', $now),
  4765. '/option',
  4766. '*/select',
  4767. '/div'
  4768. );
  4769. $this->assertTags($result, $expected);
  4770. $result = $this->Form->input('published2', array('type' => 'date'));
  4771. $now = strtotime('now');
  4772. $expected = array(
  4773. 'div' => array('class' => 'input date'),
  4774. 'label' => array('for' => 'ContactPublished2Month'),
  4775. 'Published2',
  4776. '/label',
  4777. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4778. $monthsRegex,
  4779. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4780. date('F', $now),
  4781. '/option',
  4782. '*/select',
  4783. '-',
  4784. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4785. $daysRegex,
  4786. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4787. date('j', $now),
  4788. '/option',
  4789. '*/select',
  4790. '-',
  4791. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4792. $yearsRegex,
  4793. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4794. date('Y', $now),
  4795. '/option',
  4796. '*/select',
  4797. '/div'
  4798. );
  4799. $this->assertTags($result, $expected);
  4800. $this->Form->create('Contact');
  4801. $result = $this->Form->input('published', array('monthNames' => false));
  4802. $now = strtotime('now');
  4803. $expected = array(
  4804. 'div' => array('class' => 'input date'),
  4805. 'label' => array('for' => 'ContactPublishedMonth'),
  4806. 'Published',
  4807. '/label',
  4808. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4809. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4810. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4811. date('m', $now),
  4812. '/option',
  4813. '*/select',
  4814. '-',
  4815. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4816. $daysRegex,
  4817. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4818. date('j', $now),
  4819. '/option',
  4820. '*/select',
  4821. '-',
  4822. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4823. $yearsRegex,
  4824. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4825. date('Y', $now),
  4826. '/option',
  4827. '*/select',
  4828. '/div'
  4829. );
  4830. $this->assertTags($result, $expected);
  4831. $result = $this->Form->input('published', array('type' => 'time'));
  4832. $now = strtotime('now');
  4833. $expected = array(
  4834. 'div' => array('class' => 'input time'),
  4835. 'label' => array('for' => 'ContactPublishedHour'),
  4836. 'Published',
  4837. '/label',
  4838. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4839. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4840. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4841. date('g', $now),
  4842. '/option',
  4843. '*/select',
  4844. ':',
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. $result = $this->Form->input('published', array(
  4848. 'timeFormat' => 24,
  4849. 'interval' => 5,
  4850. 'selected' => strtotime('2009-09-03 13:37:00'),
  4851. 'type' => 'datetime'
  4852. ));
  4853. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4854. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4855. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4856. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4857. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4858. $this->assertNoErrors();
  4859. $this->Form->request->data['Contact'] = array(
  4860. 'date' => array(
  4861. 'day' => '',
  4862. 'month' => '',
  4863. 'year' => '',
  4864. 'hour' => '',
  4865. 'min' => '',
  4866. 'meridian' => ''
  4867. )
  4868. );
  4869. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4870. }
  4871. /**
  4872. * test that datetime() and default values work.
  4873. *
  4874. * @return void
  4875. */
  4876. public function testDatetimeWithDefault() {
  4877. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4878. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4879. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4880. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4881. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4882. 'default' => '2009-06-01 11:15:30'
  4883. ));
  4884. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4885. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4886. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4887. }
  4888. /**
  4889. * test that bogus non-date time data doesn't cause errors.
  4890. *
  4891. * @return void
  4892. */
  4893. public function testDateTimeWithBogusData() {
  4894. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4895. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4896. }
  4897. /**
  4898. * testDateTimeEmptyAsArray
  4899. *
  4900. * @return void
  4901. */
  4902. public function testDateTimeEmptyAsArray() {
  4903. $result = $this->Form->dateTime('Contact.date',
  4904. 'DMY',
  4905. '12',
  4906. array(
  4907. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4908. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4909. )
  4910. )
  4911. );
  4912. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4913. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4914. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4915. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4916. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4917. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4918. $result = $this->Form->dateTime('Contact.date',
  4919. 'DMY',
  4920. '12',
  4921. array(
  4922. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4923. )
  4924. );
  4925. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4926. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4927. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4928. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4929. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4930. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4931. }
  4932. /**
  4933. * testFormDateTimeMulti method
  4934. *
  4935. * test multiple datetime element generation
  4936. *
  4937. * @return void
  4938. */
  4939. public function testFormDateTimeMulti() {
  4940. extract($this->dateRegex);
  4941. $result = $this->Form->dateTime('Contact.1.updated');
  4942. $expected = array(
  4943. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4944. $daysRegex,
  4945. array('option' => array('value' => '')),
  4946. '/option',
  4947. '*/select',
  4948. '-',
  4949. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4950. $monthsRegex,
  4951. array('option' => array('value' => '')),
  4952. '/option',
  4953. '*/select',
  4954. '-',
  4955. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4956. $yearsRegex,
  4957. array('option' => array('value' => '')),
  4958. '/option',
  4959. '*/select',
  4960. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4961. $hoursRegex,
  4962. array('option' => array('value' => '')),
  4963. '/option',
  4964. '*/select',
  4965. ':',
  4966. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4967. $minutesRegex,
  4968. array('option' => array('value' => '')),
  4969. '/option',
  4970. '*/select',
  4971. ' ',
  4972. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4973. $meridianRegex,
  4974. array('option' => array('value' => '')),
  4975. '/option',
  4976. '*/select'
  4977. );
  4978. $this->assertTags($result, $expected);
  4979. $result = $this->Form->dateTime('Contact.2.updated');
  4980. $expected = array(
  4981. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4982. $daysRegex,
  4983. array('option' => array('value' => '')),
  4984. '/option',
  4985. '*/select',
  4986. '-',
  4987. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4988. $monthsRegex,
  4989. array('option' => array('value' => '')),
  4990. '/option',
  4991. '*/select',
  4992. '-',
  4993. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4994. $yearsRegex,
  4995. array('option' => array('value' => '')),
  4996. '/option',
  4997. '*/select',
  4998. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4999. $hoursRegex,
  5000. array('option' => array('value' => '')),
  5001. '/option',
  5002. '*/select',
  5003. ':',
  5004. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5005. $minutesRegex,
  5006. array('option' => array('value' => '')),
  5007. '/option',
  5008. '*/select',
  5009. ' ',
  5010. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5011. $meridianRegex,
  5012. array('option' => array('value' => '')),
  5013. '/option',
  5014. '*/select'
  5015. );
  5016. $this->assertTags($result, $expected);
  5017. }
  5018. /**
  5019. * When changing the date format, the label should always focus the first select box when
  5020. * clicked.
  5021. *
  5022. * @return void
  5023. */
  5024. public function testDateTimeLabelIdMatchesFirstInput() {
  5025. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5026. $this->assertContains('label for="ModelDateMonth"', $result);
  5027. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5028. $this->assertContains('label for="ModelDateDay"', $result);
  5029. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5030. $this->assertContains('label for="ModelDateYear"', $result);
  5031. }
  5032. /**
  5033. * testMonth method
  5034. *
  5035. * @return void
  5036. */
  5037. public function testMonth() {
  5038. $result = $this->Form->month('Model.field');
  5039. $expected = array(
  5040. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. array('option' => array('value' => '01')),
  5044. date('F', strtotime('2008-01-01 00:00:00')),
  5045. '/option',
  5046. array('option' => array('value' => '02')),
  5047. date('F', strtotime('2008-02-01 00:00:00')),
  5048. '/option',
  5049. '*/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $result = $this->Form->month('Model.field', array('empty' => true));
  5053. $expected = array(
  5054. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5055. array('option' => array('value' => '')),
  5056. '/option',
  5057. array('option' => array('value' => '01')),
  5058. date('F', strtotime('2008-01-01 00:00:00')),
  5059. '/option',
  5060. array('option' => array('value' => '02')),
  5061. date('F', strtotime('2008-02-01 00:00:00')),
  5062. '/option',
  5063. '*/select',
  5064. );
  5065. $this->assertTags($result, $expected);
  5066. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5067. $expected = array(
  5068. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5069. array('option' => array('value' => '')),
  5070. '/option',
  5071. array('option' => array('value' => '01')),
  5072. '01',
  5073. '/option',
  5074. array('option' => array('value' => '02')),
  5075. '02',
  5076. '/option',
  5077. '*/select',
  5078. );
  5079. $this->assertTags($result, $expected);
  5080. $monthNames = array(
  5081. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5082. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5083. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5084. $expected = array(
  5085. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5086. array('option' => array('value' => '')),
  5087. '/option',
  5088. array('option' => array('value' => '01')),
  5089. 'Jan',
  5090. '/option',
  5091. array('option' => array('value' => '02')),
  5092. 'Feb',
  5093. '/option',
  5094. '*/select',
  5095. );
  5096. $this->assertTags($result, $expected);
  5097. }
  5098. /**
  5099. * testDay method
  5100. *
  5101. * @return void
  5102. */
  5103. public function testDay() {
  5104. extract($this->dateRegex);
  5105. $result = $this->Form->day('Model.field', array('value' => false));
  5106. $expected = array(
  5107. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5108. array('option' => array('value' => '')),
  5109. '/option',
  5110. array('option' => array('value' => '01')),
  5111. '1',
  5112. '/option',
  5113. array('option' => array('value' => '02')),
  5114. '2',
  5115. '/option',
  5116. $daysRegex,
  5117. '/select',
  5118. );
  5119. $this->assertTags($result, $expected);
  5120. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5121. $result = $this->Form->day('Model.field');
  5122. $expected = array(
  5123. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5124. array('option' => array('value' => '')),
  5125. '/option',
  5126. array('option' => array('value' => '01')),
  5127. '1',
  5128. '/option',
  5129. array('option' => array('value' => '02')),
  5130. '2',
  5131. '/option',
  5132. $daysRegex,
  5133. array('option' => array('value' => '10', 'selected' => 'selected')),
  5134. '10',
  5135. '/option',
  5136. $daysRegex,
  5137. '/select',
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. $this->Form->request->data['Model']['field'] = '';
  5141. $result = $this->Form->day('Model.field', array('value' => '10'));
  5142. $expected = array(
  5143. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5144. array('option' => array('value' => '')),
  5145. '/option',
  5146. array('option' => array('value' => '01')),
  5147. '1',
  5148. '/option',
  5149. array('option' => array('value' => '02')),
  5150. '2',
  5151. '/option',
  5152. $daysRegex,
  5153. array('option' => array('value' => '10', 'selected' => 'selected')),
  5154. '10',
  5155. '/option',
  5156. $daysRegex,
  5157. '/select',
  5158. );
  5159. $this->assertTags($result, $expected);
  5160. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5161. $result = $this->Form->day('Model.field', array('value' => true));
  5162. $expected = array(
  5163. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5164. array('option' => array('value' => '')),
  5165. '/option',
  5166. array('option' => array('value' => '01')),
  5167. '1',
  5168. '/option',
  5169. array('option' => array('value' => '02')),
  5170. '2',
  5171. '/option',
  5172. $daysRegex,
  5173. array('option' => array('value' => '10', 'selected' => 'selected')),
  5174. '10',
  5175. '/option',
  5176. $daysRegex,
  5177. '/select',
  5178. );
  5179. $this->assertTags($result, $expected);
  5180. }
  5181. /**
  5182. * testMinute method
  5183. *
  5184. * @return void
  5185. */
  5186. public function testMinute() {
  5187. extract($this->dateRegex);
  5188. $result = $this->Form->minute('Model.field');
  5189. $expected = array(
  5190. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '00')),
  5194. '00',
  5195. '/option',
  5196. array('option' => array('value' => '01')),
  5197. '01',
  5198. '/option',
  5199. array('option' => array('value' => '02')),
  5200. '02',
  5201. '/option',
  5202. $minutesRegex,
  5203. '/select',
  5204. );
  5205. $this->assertTags($result, $expected);
  5206. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5207. $result = $this->Form->minute('Model.field');
  5208. $expected = array(
  5209. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5210. array('option' => array('value' => '')),
  5211. '/option',
  5212. array('option' => array('value' => '00')),
  5213. '00',
  5214. '/option',
  5215. array('option' => array('value' => '01')),
  5216. '01',
  5217. '/option',
  5218. array('option' => array('value' => '02')),
  5219. '02',
  5220. '/option',
  5221. $minutesRegex,
  5222. array('option' => array('value' => '12', 'selected' => 'selected')),
  5223. '12',
  5224. '/option',
  5225. $minutesRegex,
  5226. '/select',
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. $this->Form->request->data['Model']['field'] = '';
  5230. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5231. $expected = array(
  5232. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5233. array('option' => array('value' => '')),
  5234. '/option',
  5235. array('option' => array('value' => '00')),
  5236. '00',
  5237. '/option',
  5238. array('option' => array('value' => '05')),
  5239. '05',
  5240. '/option',
  5241. array('option' => array('value' => '10')),
  5242. '10',
  5243. '/option',
  5244. $minutesRegex,
  5245. '/select',
  5246. );
  5247. $this->assertTags($result, $expected);
  5248. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5249. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5250. $expected = array(
  5251. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5252. array('option' => array('value' => '')),
  5253. '/option',
  5254. array('option' => array('value' => '00')),
  5255. '00',
  5256. '/option',
  5257. array('option' => array('value' => '05')),
  5258. '05',
  5259. '/option',
  5260. array('option' => array('value' => '10', 'selected' => 'selected')),
  5261. '10',
  5262. '/option',
  5263. $minutesRegex,
  5264. '/select',
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. }
  5268. /**
  5269. * testHour method
  5270. *
  5271. * @return void
  5272. */
  5273. public function testHour() {
  5274. extract($this->dateRegex);
  5275. $result = $this->Form->hour('Model.field', false);
  5276. $expected = array(
  5277. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5278. array('option' => array('value' => '')),
  5279. '/option',
  5280. array('option' => array('value' => '01')),
  5281. '1',
  5282. '/option',
  5283. array('option' => array('value' => '02')),
  5284. '2',
  5285. '/option',
  5286. $hoursRegex,
  5287. '/select',
  5288. );
  5289. $this->assertTags($result, $expected);
  5290. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5291. $result = $this->Form->hour('Model.field', false);
  5292. $expected = array(
  5293. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5294. array('option' => array('value' => '')),
  5295. '/option',
  5296. array('option' => array('value' => '01')),
  5297. '1',
  5298. '/option',
  5299. array('option' => array('value' => '02')),
  5300. '2',
  5301. '/option',
  5302. $hoursRegex,
  5303. array('option' => array('value' => '12', 'selected' => 'selected')),
  5304. '12',
  5305. '/option',
  5306. '/select',
  5307. );
  5308. $this->assertTags($result, $expected);
  5309. $this->Form->request->data['Model']['field'] = '';
  5310. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5311. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5312. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5313. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5314. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5315. $result = $this->Form->hour('Model.field', true);
  5316. $expected = array(
  5317. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5318. array('option' => array('value' => '')),
  5319. '/option',
  5320. array('option' => array('value' => '00', 'selected' => 'selected')),
  5321. '0',
  5322. '/option',
  5323. array('option' => array('value' => '01')),
  5324. '1',
  5325. '/option',
  5326. array('option' => array('value' => '02')),
  5327. '2',
  5328. '/option',
  5329. $hoursRegex,
  5330. '/select',
  5331. );
  5332. $this->assertTags($result, $expected);
  5333. unset($this->Form->request->data['Model']['field']);
  5334. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5335. $thisHour = date('H');
  5336. $optValue = date('G');
  5337. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5338. }
  5339. /**
  5340. * testYear method
  5341. *
  5342. * @return void
  5343. */
  5344. public function testYear() {
  5345. $result = $this->Form->year('Model.field', 2006, 2007);
  5346. $expected = array(
  5347. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5348. array('option' => array('value' => '')),
  5349. '/option',
  5350. array('option' => array('value' => '2007')),
  5351. '2007',
  5352. '/option',
  5353. array('option' => array('value' => '2006')),
  5354. '2006',
  5355. '/option',
  5356. '/select',
  5357. );
  5358. $this->assertTags($result, $expected);
  5359. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5360. $expected = array(
  5361. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5362. array('option' => array('value' => '')),
  5363. '/option',
  5364. array('option' => array('value' => '2006')),
  5365. '2006',
  5366. '/option',
  5367. array('option' => array('value' => '2007')),
  5368. '2007',
  5369. '/option',
  5370. '/select',
  5371. );
  5372. $this->assertTags($result, $expected);
  5373. $this->request->data['Contact']['published'] = '';
  5374. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5375. $expected = array(
  5376. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5377. array('option' => array('value' => '')),
  5378. '/option',
  5379. array('option' => array('value' => '2007')),
  5380. '2007',
  5381. '/option',
  5382. array('option' => array('value' => '2006')),
  5383. '2006',
  5384. '/option',
  5385. '/select',
  5386. );
  5387. $this->assertTags($result, $expected);
  5388. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5389. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5390. $expected = array(
  5391. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5392. array('option' => array('value' => '2007')),
  5393. '2007',
  5394. '/option',
  5395. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5396. '2006',
  5397. '/option',
  5398. '/select',
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. $this->Form->request->data['Contact']['published'] = '';
  5402. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5403. $expected = array(
  5404. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5405. array('option' => array('value' => '')),
  5406. '/option',
  5407. array('option' => array('value' => '2007')),
  5408. '2007',
  5409. '/option',
  5410. array('option' => array('value' => '2006')),
  5411. '2006',
  5412. '/option',
  5413. '/select',
  5414. );
  5415. $this->assertTags($result, $expected);
  5416. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5417. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5418. $expected = array(
  5419. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5420. array('option' => array('value' => '2007')),
  5421. '2007',
  5422. '/option',
  5423. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5424. '2006',
  5425. '/option',
  5426. '/select',
  5427. );
  5428. $this->assertTags($result, $expected);
  5429. $this->Form->request->data['Contact']['published'] = '';
  5430. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5431. $expected = array(
  5432. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5433. array('option' => array('value' => '')),
  5434. '/option',
  5435. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5436. '2007',
  5437. '/option',
  5438. array('option' => array('value' => '2006')),
  5439. '2006',
  5440. '/option',
  5441. '/select',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5445. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5446. $expected = array(
  5447. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5448. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5449. '2007',
  5450. '/option',
  5451. array('option' => array('value' => '2006')),
  5452. '2006',
  5453. '/option',
  5454. '/select',
  5455. );
  5456. $this->assertTags($result, $expected);
  5457. $this->Form->request->data['Contact']['published'] = '';
  5458. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5459. $expected = array(
  5460. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5461. array('option' => array('value' => '2008')),
  5462. '2008',
  5463. '/option',
  5464. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5465. '2007',
  5466. '/option',
  5467. array('option' => array('value' => '2006')),
  5468. '2006',
  5469. '/option',
  5470. '/select',
  5471. );
  5472. $this->assertTags($result, $expected);
  5473. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5474. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5475. $expected = array(
  5476. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5477. array('option' => array('value' => '2008')),
  5478. '2008',
  5479. '/option',
  5480. array('option' => array('value' => '2007')),
  5481. '2007',
  5482. '/option',
  5483. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5484. '2006',
  5485. '/option',
  5486. '/select',
  5487. );
  5488. $this->assertTags($result, $expected);
  5489. $this->Form->request->data = array();
  5490. $this->Form->create('Contact');
  5491. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5492. $expected = array(
  5493. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5494. array('option' => array('value' => '2008')),
  5495. '2008',
  5496. '/option',
  5497. array('option' => array('value' => '2007')),
  5498. '2007',
  5499. '/option',
  5500. array('option' => array('value' => '2006')),
  5501. '2006',
  5502. '/option',
  5503. '/select',
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5507. $this->assertContains('data[Contact][published][year]', $result);
  5508. }
  5509. /**
  5510. * testTextArea method
  5511. *
  5512. * @return void
  5513. */
  5514. public function testTextArea() {
  5515. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5516. $result = $this->Form->textarea('Model.field');
  5517. $expected = array(
  5518. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5519. 'some test data',
  5520. '/textarea',
  5521. );
  5522. $this->assertTags($result, $expected);
  5523. $result = $this->Form->textarea('Model.tmp');
  5524. $expected = array(
  5525. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5526. '/textarea',
  5527. );
  5528. $this->assertTags($result, $expected);
  5529. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5530. $result = $this->Form->textarea('Model.field');
  5531. $expected = array(
  5532. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5533. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5534. '/textarea',
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5538. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5539. $expected = array(
  5540. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5541. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5542. '/textarea',
  5543. );
  5544. $this->assertTags($result, $expected);
  5545. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5546. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5547. $expected = array(
  5548. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5549. '/textarea'
  5550. );
  5551. $this->assertTags($result, $expected);
  5552. }
  5553. /**
  5554. * testTextAreaWithStupidCharacters method
  5555. *
  5556. * test text area with non-ascii characters
  5557. *
  5558. * @return void
  5559. */
  5560. public function testTextAreaWithStupidCharacters() {
  5561. $this->loadFixtures('Post');
  5562. $result = $this->Form->input('Post.content', array(
  5563. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5564. ));
  5565. $expected = array(
  5566. 'div' => array('class' => 'input text'),
  5567. 'label' => array('for' => 'PostContent'),
  5568. 'Current Text',
  5569. '/label',
  5570. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5571. 'GREAT®',
  5572. '/textarea',
  5573. '/div'
  5574. );
  5575. $this->assertTags($result, $expected);
  5576. }
  5577. /**
  5578. * testHiddenField method
  5579. *
  5580. * @return void
  5581. */
  5582. public function testHiddenField() {
  5583. $Contact = ClassRegistry::getObject('Contact');
  5584. $Contact->validationErrors['field'] = 1;
  5585. $this->Form->request->data['Contact']['field'] = 'test';
  5586. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5587. $this->assertTags($result, array(
  5588. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5589. );
  5590. }
  5591. /**
  5592. * testFileUploadField method
  5593. *
  5594. * @return void
  5595. */
  5596. public function testFileUploadField() {
  5597. $result = $this->Form->file('Model.upload');
  5598. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5599. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5600. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5601. $expected = array(
  5602. 'div' => array('class' => 'input file'),
  5603. 'label' => array('for' => 'ModelUpload'),
  5604. 'Upload',
  5605. '/label',
  5606. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5607. '/div'
  5608. );
  5609. $this->assertTags($result, $expected);
  5610. }
  5611. /**
  5612. * test File upload input on a model not used in create();
  5613. *
  5614. * @return void
  5615. */
  5616. public function testFileUploadOnOtherModel() {
  5617. $this->Form->create('ValidateUser', array('type' => 'file'));
  5618. $result = $this->Form->file('ValidateProfile.city');
  5619. $expected = array(
  5620. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5621. );
  5622. $this->assertTags($result, $expected);
  5623. }
  5624. /**
  5625. * testButton method
  5626. *
  5627. * @return void
  5628. */
  5629. public function testButton() {
  5630. $result = $this->Form->button('Hi');
  5631. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5632. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5633. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5634. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5635. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5636. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5637. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5638. $result = $this->Form->button('No type', array('type' => false));
  5639. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5640. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5641. $this->assertNotRegExp('/\&039/', $result);
  5642. }
  5643. /**
  5644. * Test that button() makes unlocked fields by default.
  5645. *
  5646. * @return void
  5647. */
  5648. public function testButtonUnlockedByDefault() {
  5649. $this->Form->request->params['_Token']['key'] = 'secured';
  5650. $this->Form->button('Save', array('name' => 'save'));
  5651. $this->Form->button('Clear');
  5652. $result = $this->Form->unlockField();
  5653. $this->assertEquals(array('save'), $result);
  5654. }
  5655. /**
  5656. * testPostButton method
  5657. *
  5658. * @return void
  5659. */
  5660. public function testPostButton() {
  5661. $result = $this->Form->postButton('Hi', '/controller/action');
  5662. $this->assertTags($result, array(
  5663. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5664. 'div' => array('style' => 'display:none;'),
  5665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5666. '/div',
  5667. 'button' => array('type' => 'submit'),
  5668. 'Hi',
  5669. '/button',
  5670. '/form'
  5671. ));
  5672. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5673. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5674. }
  5675. /**
  5676. * Test that postButton adds _Token fields.
  5677. *
  5678. * @return void
  5679. */
  5680. public function testSecurePostButton() {
  5681. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5682. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5683. $expected = array(
  5684. 'form' => array(
  5685. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5686. ),
  5687. array('div' => array('style' => 'display:none;')),
  5688. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5689. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5690. '/div',
  5691. 'button' => array('type' => 'submit'),
  5692. 'Delete',
  5693. '/button',
  5694. array('div' => array('style' => 'display:none;')),
  5695. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5696. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5697. '/div',
  5698. '/form',
  5699. );
  5700. $this->assertTags($result, $expected);
  5701. }
  5702. /**
  5703. * testPostLink method
  5704. *
  5705. * @return void
  5706. */
  5707. public function testPostLink() {
  5708. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5709. $this->assertTags($result, array(
  5710. 'form' => array(
  5711. 'method' => 'post', 'action' => '/posts/delete/1',
  5712. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5713. ),
  5714. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5715. '/form',
  5716. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5717. 'Delete',
  5718. '/a'
  5719. ));
  5720. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5721. $this->assertTags($result, array(
  5722. 'form' => array(
  5723. 'method' => 'post', 'action' => '/posts/delete/1',
  5724. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5725. ),
  5726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5727. '/form',
  5728. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5729. 'Delete',
  5730. '/a'
  5731. ));
  5732. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5733. $this->assertTags($result, array(
  5734. 'form' => array(
  5735. 'method' => 'post', 'action' => '/posts/delete/1',
  5736. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5737. ),
  5738. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5739. '/form',
  5740. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5741. 'Delete',
  5742. '/a'
  5743. ));
  5744. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5745. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5746. }
  5747. /**
  5748. * Test that postLink adds _Token fields.
  5749. *
  5750. * @return void
  5751. */
  5752. public function testSecurePostLink() {
  5753. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5754. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5755. $expected = array(
  5756. 'form' => array(
  5757. 'method' => 'post', 'action' => '/posts/delete/1',
  5758. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5759. ),
  5760. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5761. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5762. 'div' => array('style' => 'display:none;'),
  5763. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5764. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5765. '/div',
  5766. '/form',
  5767. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5768. 'Delete',
  5769. '/a'
  5770. );
  5771. $this->assertTags($result, $expected);
  5772. }
  5773. /**
  5774. * testSubmitButton method
  5775. *
  5776. * @return void
  5777. */
  5778. public function testSubmitButton() {
  5779. $result = $this->Form->submit('');
  5780. $expected = array(
  5781. 'div' => array('class' => 'submit'),
  5782. 'input' => array('type' => 'submit', 'value' => ''),
  5783. '/div'
  5784. );
  5785. $this->assertTags($result, $expected);
  5786. $result = $this->Form->submit('Test Submit');
  5787. $expected = array(
  5788. 'div' => array('class' => 'submit'),
  5789. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5790. '/div'
  5791. );
  5792. $this->assertTags($result, $expected);
  5793. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5794. $expected = array(
  5795. 'span' => array('class' => 'submit'),
  5796. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5797. '/span'
  5798. );
  5799. $this->assertTags($result, $expected);
  5800. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5801. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5802. $this->assertTags($result, $expected);
  5803. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5804. $expected = array(
  5805. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5806. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5807. '/div'
  5808. );
  5809. $this->assertTags($result, $expected);
  5810. $result = $this->Form->submit('Next >');
  5811. $expected = array(
  5812. 'div' => array('class' => 'submit'),
  5813. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5814. '/div'
  5815. );
  5816. $this->assertTags($result, $expected);
  5817. $result = $this->Form->submit('Next >', array('escape' => false));
  5818. $expected = array(
  5819. 'div' => array('class' => 'submit'),
  5820. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5821. '/div'
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5825. $expected = array(
  5826. 'div' => array('class' => 'submit'),
  5827. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5828. '/div'
  5829. );
  5830. $this->assertTags($result, $expected);
  5831. $before = '--before--';
  5832. $after = '--after--';
  5833. $result = $this->Form->submit('Test', array('before' => $before));
  5834. $expected = array(
  5835. 'div' => array('class' => 'submit'),
  5836. '--before--',
  5837. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5838. '/div'
  5839. );
  5840. $this->assertTags($result, $expected);
  5841. $result = $this->Form->submit('Test', array('after' => $after));
  5842. $expected = array(
  5843. 'div' => array('class' => 'submit'),
  5844. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5845. '--after--',
  5846. '/div'
  5847. );
  5848. $this->assertTags($result, $expected);
  5849. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5850. $expected = array(
  5851. 'div' => array('class' => 'submit'),
  5852. '--before--',
  5853. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5854. '--after--',
  5855. '/div'
  5856. );
  5857. $this->assertTags($result, $expected);
  5858. }
  5859. /**
  5860. * test image submit types.
  5861. *
  5862. * @return void
  5863. */
  5864. public function testSubmitImage() {
  5865. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5866. $expected = array(
  5867. 'div' => array('class' => 'submit'),
  5868. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5869. '/div'
  5870. );
  5871. $this->assertTags($result, $expected);
  5872. $result = $this->Form->submit('/relative/cake.power.gif');
  5873. $expected = array(
  5874. 'div' => array('class' => 'submit'),
  5875. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5876. '/div'
  5877. );
  5878. $this->assertTags($result, $expected);
  5879. $result = $this->Form->submit('cake.power.gif');
  5880. $expected = array(
  5881. 'div' => array('class' => 'submit'),
  5882. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5883. '/div'
  5884. );
  5885. $this->assertTags($result, $expected);
  5886. $result = $this->Form->submit('Not.an.image');
  5887. $expected = array(
  5888. 'div' => array('class' => 'submit'),
  5889. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5890. '/div'
  5891. );
  5892. $this->assertTags($result, $expected);
  5893. $after = '--after--';
  5894. $before = '--before--';
  5895. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5896. $expected = array(
  5897. 'div' => array('class' => 'submit'),
  5898. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5899. '--after--',
  5900. '/div'
  5901. );
  5902. $this->assertTags($result, $expected);
  5903. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5904. $expected = array(
  5905. 'div' => array('class' => 'submit'),
  5906. '--before--',
  5907. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5908. '/div'
  5909. );
  5910. $this->assertTags($result, $expected);
  5911. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5912. $expected = array(
  5913. 'div' => array('class' => 'submit'),
  5914. '--before--',
  5915. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5916. '--after--',
  5917. '/div'
  5918. );
  5919. $this->assertTags($result, $expected);
  5920. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5921. $expected = array(
  5922. 'div' => array('class' => 'submit'),
  5923. '--before--',
  5924. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5925. '--after--',
  5926. '/div'
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. }
  5930. /**
  5931. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5932. * be submitted at a time.
  5933. *
  5934. * @return void
  5935. */
  5936. public function testSubmitUnlockedByDefault() {
  5937. $this->Form->request->params['_Token']['key'] = 'secured';
  5938. $this->Form->submit('Go go');
  5939. $this->Form->submit('Save', array('name' => 'save'));
  5940. $result = $this->Form->unlockField();
  5941. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5942. }
  5943. /**
  5944. * Test submit image with timestamps.
  5945. *
  5946. * @return void
  5947. */
  5948. public function testSubmitImageTimestamp() {
  5949. Configure::write('Asset.timestamp', 'force');
  5950. $result = $this->Form->submit('cake.power.gif');
  5951. $expected = array(
  5952. 'div' => array('class' => 'submit'),
  5953. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5954. '/div'
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. }
  5958. /**
  5959. * test the create() method
  5960. *
  5961. * @return void
  5962. */
  5963. public function testCreate() {
  5964. $result = $this->Form->create('Contact');
  5965. $encoding = strtolower(Configure::read('App.encoding'));
  5966. $expected = array(
  5967. 'form' => array(
  5968. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5969. 'accept-charset' => $encoding
  5970. ),
  5971. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5972. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5973. '/div'
  5974. );
  5975. $this->assertTags($result, $expected);
  5976. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5977. $expected = array('form' => array(
  5978. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5979. 'accept-charset' => $encoding
  5980. ));
  5981. $this->assertTags($result, $expected);
  5982. $result = $this->Form->create('Contact', array('type' => 'get'));
  5983. $expected = array('form' => array(
  5984. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5985. 'accept-charset' => $encoding
  5986. ));
  5987. $this->assertTags($result, $expected);
  5988. $result = $this->Form->create('Contact', array('type' => 'put'));
  5989. $expected = array(
  5990. 'form' => array(
  5991. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5992. 'accept-charset' => $encoding
  5993. ),
  5994. 'div' => array('style' => 'display:none;'),
  5995. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5996. '/div'
  5997. );
  5998. $this->assertTags($result, $expected);
  5999. $result = $this->Form->create('Contact', array('type' => 'file'));
  6000. $expected = array(
  6001. 'form' => array(
  6002. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6003. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6004. ),
  6005. 'div' => array('style' => 'display:none;'),
  6006. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6007. '/div'
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. $this->Form->request->data['Contact']['id'] = 1;
  6011. $this->Form->request->here = '/contacts/edit/1';
  6012. $this->Form->request['action'] = 'edit';
  6013. $result = $this->Form->create('Contact');
  6014. $expected = array(
  6015. 'form' => array(
  6016. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6017. 'accept-charset' => $encoding
  6018. ),
  6019. 'div' => array('style' => 'display:none;'),
  6020. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6021. '/div'
  6022. );
  6023. $this->assertTags($result, $expected);
  6024. $this->Form->request->data['Contact']['id'] = 1;
  6025. $this->Form->request->here = '/contacts/edit/1';
  6026. $this->Form->request['action'] = 'edit';
  6027. $result = $this->Form->create('Contact', array('type' => 'file'));
  6028. $expected = array(
  6029. 'form' => array(
  6030. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6031. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6032. ),
  6033. 'div' => array('style' => 'display:none;'),
  6034. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6035. '/div'
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6039. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6040. $expected = array(
  6041. 'form' => array(
  6042. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6043. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6044. ),
  6045. 'div' => array('style' => 'display:none;'),
  6046. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6047. '/div'
  6048. );
  6049. $this->assertTags($result, $expected);
  6050. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6051. $expected = array(
  6052. 'form' => array(
  6053. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6054. 'accept-charset' => $encoding
  6055. ),
  6056. 'div' => array('style' => 'display:none;'),
  6057. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6058. '/div'
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. $this->Form->request['action'] = 'add';
  6062. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6063. $expected = array(
  6064. 'form' => array(
  6065. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6066. 'accept-charset' => $encoding
  6067. ),
  6068. 'div' => array('style' => 'display:none;'),
  6069. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6070. '/div'
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. $result = $this->Form->create('User', array('action' => 'login'));
  6074. $expected = array(
  6075. 'form' => array(
  6076. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6077. 'accept-charset' => $encoding
  6078. ),
  6079. 'div' => array('style' => 'display:none;'),
  6080. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6081. '/div'
  6082. );
  6083. $this->assertTags($result, $expected);
  6084. $result = $this->Form->create('User', array('url' => '/users/login'));
  6085. $expected = array(
  6086. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6087. 'div' => array('style' => 'display:none;'),
  6088. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6089. '/div'
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $this->Form->request['controller'] = 'pages';
  6093. $result = $this->Form->create('User', array('action' => 'signup'));
  6094. $expected = array(
  6095. 'form' => array(
  6096. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6097. 'accept-charset' => $encoding
  6098. ),
  6099. 'div' => array('style' => 'display:none;'),
  6100. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6101. '/div'
  6102. );
  6103. $this->assertTags($result, $expected);
  6104. $this->Form->request->data = array();
  6105. $this->Form->request['controller'] = 'contacts';
  6106. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6107. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6108. $expected = array(
  6109. 'form' => array(
  6110. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6111. 'accept-charset' => 'utf-8'
  6112. ),
  6113. 'div' => array('style' => 'display:none;'),
  6114. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6115. '/div'
  6116. );
  6117. $this->assertTags($result, $expected);
  6118. }
  6119. /**
  6120. * Test the onsubmit option for create()
  6121. *
  6122. * @return void
  6123. */
  6124. public function testCreateOnSubmit() {
  6125. $this->Form->request->data = array();
  6126. $this->Form->request['controller'] = 'contacts';
  6127. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6128. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6129. $expected = array(
  6130. 'form' => array(
  6131. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6132. 'accept-charset' => 'utf-8'
  6133. ),
  6134. 'div' => array('style' => 'display:none;'),
  6135. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6136. '/div'
  6137. );
  6138. $this->assertTags($result, $expected);
  6139. $this->Form->request->data = array();
  6140. $this->Form->request['controller'] = 'contacts';
  6141. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6142. $result = $this->Form->create(array(
  6143. 'url' => array('action' => 'index', 'param'),
  6144. 'default' => false,
  6145. 'onsubmit' => 'someFunction();'
  6146. ));
  6147. $expected = array(
  6148. 'form' => array(
  6149. 'id' => 'ContactAddForm', 'method' => 'post',
  6150. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6151. 'action' => '/contacts/index/param',
  6152. 'accept-charset' => 'utf-8'
  6153. ),
  6154. 'div' => array('style' => 'display:none;'),
  6155. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6156. '/div'
  6157. );
  6158. $this->assertTags($result, $expected);
  6159. }
  6160. /**
  6161. * test create() with automatic url generation
  6162. *
  6163. * @return void
  6164. */
  6165. public function testCreateAutoUrl() {
  6166. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6167. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6168. $this->Form->request->base = '/base_url';
  6169. $result = $this->Form->create('Contact');
  6170. $expected = array(
  6171. 'form' => array(
  6172. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6173. 'accept-charset' => 'utf-8'
  6174. ),
  6175. 'div' => array('style' => 'display:none;'),
  6176. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6177. '/div'
  6178. );
  6179. $this->assertTags($result, $expected);
  6180. $this->Form->request['action'] = 'delete';
  6181. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6182. $this->Form->request->base = '/base_url';
  6183. $result = $this->Form->create('Contact');
  6184. $expected = array(
  6185. 'form' => array(
  6186. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6187. 'accept-charset' => 'utf-8'
  6188. ),
  6189. 'div' => array('style' => 'display:none;'),
  6190. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6191. '/div'
  6192. );
  6193. $this->assertTags($result, $expected);
  6194. }
  6195. /**
  6196. * test create() with a custom route
  6197. *
  6198. * @return void
  6199. */
  6200. public function testCreateCustomRoute() {
  6201. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6202. $encoding = strtolower(Configure::read('App.encoding'));
  6203. $result = $this->Form->create('User', array('action' => 'login'));
  6204. $expected = array(
  6205. 'form' => array(
  6206. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6207. 'accept-charset' => $encoding
  6208. ),
  6209. 'div' => array('style' => 'display:none;'),
  6210. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6211. '/div'
  6212. );
  6213. $this->assertTags($result, $expected);
  6214. }
  6215. /**
  6216. * test that inputDefaults are stored and used.
  6217. *
  6218. * @return void
  6219. */
  6220. public function testCreateWithInputDefaults() {
  6221. $this->Form->create('User', array(
  6222. 'inputDefaults' => array(
  6223. 'div' => false,
  6224. 'label' => false,
  6225. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6226. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6227. )
  6228. ));
  6229. $result = $this->Form->input('username');
  6230. $expected = array(
  6231. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6232. );
  6233. $this->assertTags($result, $expected);
  6234. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6235. $expected = array(
  6236. 'div' => array('class' => 'input text'),
  6237. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6238. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6239. '/div'
  6240. );
  6241. $this->assertTags($result, $expected);
  6242. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6243. $expected = array(
  6244. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6245. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6246. );
  6247. $this->assertTags($result, $expected);
  6248. $this->Form->create('User', array(
  6249. 'inputDefaults' => array(
  6250. 'div' => false,
  6251. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6252. )
  6253. ));
  6254. $result = $this->Form->input('username', array('div' => true));
  6255. $expected = array(
  6256. 'div' => array('class' => 'input text'),
  6257. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6258. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6259. '/div'
  6260. );
  6261. $this->assertTags($result, $expected);
  6262. }
  6263. /**
  6264. * test automatic accept-charset overriding
  6265. *
  6266. * @return void
  6267. */
  6268. public function testCreateWithAcceptCharset() {
  6269. $result = $this->Form->create('UserForm', array(
  6270. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6271. )
  6272. );
  6273. $expected = array(
  6274. 'form' => array(
  6275. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6276. 'accept-charset' => 'iso-8859-1'
  6277. ),
  6278. 'div' => array('style' => 'display:none;'),
  6279. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6280. '/div'
  6281. );
  6282. $this->assertTags($result, $expected);
  6283. }
  6284. /**
  6285. * Test base form url when url param is passed with multiple parameters (&)
  6286. *
  6287. */
  6288. public function testCreateQuerystringrequest() {
  6289. $encoding = strtolower(Configure::read('App.encoding'));
  6290. $result = $this->Form->create('Contact', array(
  6291. 'type' => 'post',
  6292. 'escape' => false,
  6293. 'url' => array(
  6294. 'controller' => 'controller',
  6295. 'action' => 'action',
  6296. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6297. )
  6298. ));
  6299. $expected = array(
  6300. 'form' => array(
  6301. 'id' => 'ContactAddForm',
  6302. 'method' => 'post',
  6303. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6304. 'accept-charset' => $encoding
  6305. ),
  6306. 'div' => array('style' => 'display:none;'),
  6307. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6308. '/div'
  6309. );
  6310. $this->assertTags($result, $expected);
  6311. $result = $this->Form->create('Contact', array(
  6312. 'type' => 'post',
  6313. 'url' => array(
  6314. 'controller' => 'controller',
  6315. 'action' => 'action',
  6316. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6317. )
  6318. ));
  6319. $expected = array(
  6320. 'form' => array(
  6321. 'id' => 'ContactAddForm',
  6322. 'method' => 'post',
  6323. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6324. 'accept-charset' => $encoding
  6325. ),
  6326. 'div' => array('style' => 'display:none;'),
  6327. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6328. '/div'
  6329. );
  6330. $this->assertTags($result, $expected);
  6331. }
  6332. /**
  6333. * test that create() doesn't cause errors by multiple id's being in the primary key
  6334. * as could happen with multiple select or checkboxes.
  6335. *
  6336. * @return void
  6337. */
  6338. public function testCreateWithMultipleIdInData() {
  6339. $encoding = strtolower(Configure::read('App.encoding'));
  6340. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6341. $result = $this->Form->create('Contact');
  6342. $expected = array(
  6343. 'form' => array(
  6344. 'id' => 'ContactAddForm',
  6345. 'method' => 'post',
  6346. 'action' => '/contacts/add',
  6347. 'accept-charset' => $encoding
  6348. ),
  6349. 'div' => array('style' => 'display:none;'),
  6350. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6351. '/div'
  6352. );
  6353. $this->assertTags($result, $expected);
  6354. }
  6355. /**
  6356. * test that create() doesn't add in extra passed params.
  6357. *
  6358. * @return void
  6359. */
  6360. public function testCreatePassedArgs() {
  6361. $encoding = strtolower(Configure::read('App.encoding'));
  6362. $this->Form->request->data['Contact']['id'] = 1;
  6363. $result = $this->Form->create('Contact', array(
  6364. 'type' => 'post',
  6365. 'escape' => false,
  6366. 'url' => array(
  6367. 'action' => 'edit',
  6368. 'myparam'
  6369. )
  6370. ));
  6371. $expected = array(
  6372. 'form' => array(
  6373. 'id' => 'ContactAddForm',
  6374. 'method' => 'post',
  6375. 'action' => '/contacts/edit/myparam',
  6376. 'accept-charset' => $encoding
  6377. ),
  6378. 'div' => array('style' => 'display:none;'),
  6379. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6380. '/div'
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. }
  6384. /**
  6385. * test creating a get form, and get form inputs.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testGetFormCreate() {
  6390. $encoding = strtolower(Configure::read('App.encoding'));
  6391. $result = $this->Form->create('Contact', array('type' => 'get'));
  6392. $this->assertTags($result, array('form' => array(
  6393. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6394. 'accept-charset' => $encoding
  6395. )));
  6396. $result = $this->Form->text('Contact.name');
  6397. $this->assertTags($result, array('input' => array(
  6398. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6399. )));
  6400. $result = $this->Form->password('password');
  6401. $this->assertTags($result, array('input' => array(
  6402. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6403. )));
  6404. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6405. $result = $this->Form->text('user_form');
  6406. $this->assertTags($result, array('input' => array(
  6407. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6408. )));
  6409. }
  6410. /**
  6411. * test get form, and inputs when the model param is false
  6412. *
  6413. * @return void
  6414. */
  6415. public function testGetFormWithFalseModel() {
  6416. $encoding = strtolower(Configure::read('App.encoding'));
  6417. $this->Form->request['controller'] = 'contact_test';
  6418. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6419. $expected = array('form' => array(
  6420. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6421. 'accept-charset' => $encoding
  6422. ));
  6423. $this->assertTags($result, $expected);
  6424. $result = $this->Form->text('reason');
  6425. $expected = array(
  6426. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6427. );
  6428. $this->assertTags($result, $expected);
  6429. }
  6430. /**
  6431. * test that datetime() works with GET style forms.
  6432. *
  6433. * @return void
  6434. */
  6435. public function testDateTimeWithGetForms() {
  6436. extract($this->dateRegex);
  6437. $this->Form->create('Contact', array('type' => 'get'));
  6438. $result = $this->Form->datetime('created');
  6439. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6440. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6441. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6442. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6443. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6444. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6445. }
  6446. /**
  6447. * testEditFormWithData method
  6448. *
  6449. * test auto populating form elements from submitted data.
  6450. *
  6451. * @return void
  6452. */
  6453. public function testEditFormWithData() {
  6454. $this->Form->request->data = array('Person' => array(
  6455. 'id' => 1,
  6456. 'first_name' => 'Nate',
  6457. 'last_name' => 'Abele',
  6458. 'email' => 'nate@example.com'
  6459. ));
  6460. $this->Form->request->addParams(array(
  6461. 'models' => array('Person'),
  6462. 'controller' => 'people',
  6463. 'action' => 'add'
  6464. ));
  6465. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6466. $this->Form->create();
  6467. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6468. $expected = array(
  6469. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6470. 'select' => array(
  6471. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6472. ),
  6473. array('option' => array('value' => 1)), 'Nate', '/option',
  6474. array('option' => array('value' => 2)), 'Garrett', '/option',
  6475. array('option' => array('value' => 3)), 'Larry', '/option',
  6476. '/select'
  6477. );
  6478. $this->assertTags($result, $expected);
  6479. }
  6480. /**
  6481. * Test that required fields are created for various types of validation.
  6482. *
  6483. * @return void
  6484. */
  6485. public function testFormInputRequiredDetection() {
  6486. $this->Form->create('Contact');
  6487. $result = $this->Form->input('Contact.non_existing');
  6488. $expected = array(
  6489. 'div' => array('class' => 'input text'),
  6490. 'label' => array('for' => 'ContactNonExisting'),
  6491. 'Non Existing',
  6492. '/label',
  6493. 'input' => array(
  6494. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6495. 'id' => 'ContactNonExisting'
  6496. ),
  6497. '/div'
  6498. );
  6499. $this->assertTags($result, $expected);
  6500. $result = $this->Form->input('Contact.imrequired');
  6501. $expected = array(
  6502. 'div' => array('class' => 'input text required'),
  6503. 'label' => array('for' => 'ContactImrequired'),
  6504. 'Imrequired',
  6505. '/label',
  6506. 'input' => array(
  6507. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6508. 'id' => 'ContactImrequired'
  6509. ),
  6510. '/div'
  6511. );
  6512. $this->assertTags($result, $expected);
  6513. $result = $this->Form->input('Contact.imalsorequired');
  6514. $expected = array(
  6515. 'div' => array('class' => 'input text required'),
  6516. 'label' => array('for' => 'ContactImalsorequired'),
  6517. 'Imalsorequired',
  6518. '/label',
  6519. 'input' => array(
  6520. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6521. 'id' => 'ContactImalsorequired'
  6522. ),
  6523. '/div'
  6524. );
  6525. $this->assertTags($result, $expected);
  6526. $result = $this->Form->input('Contact.imrequiredtoo');
  6527. $expected = array(
  6528. 'div' => array('class' => 'input text required'),
  6529. 'label' => array('for' => 'ContactImrequiredtoo'),
  6530. 'Imrequiredtoo',
  6531. '/label',
  6532. 'input' => array(
  6533. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6534. 'id' => 'ContactImrequiredtoo'
  6535. ),
  6536. '/div'
  6537. );
  6538. $this->assertTags($result, $expected);
  6539. $result = $this->Form->input('Contact.required_one');
  6540. $expected = array(
  6541. 'div' => array('class' => 'input text required'),
  6542. 'label' => array('for' => 'ContactRequiredOne'),
  6543. 'Required One',
  6544. '/label',
  6545. 'input' => array(
  6546. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6547. 'id' => 'ContactRequiredOne'
  6548. ),
  6549. '/div'
  6550. );
  6551. $this->assertTags($result, $expected);
  6552. $result = $this->Form->input('Contact.string_required');
  6553. $expected = array(
  6554. 'div' => array('class' => 'input text required'),
  6555. 'label' => array('for' => 'ContactStringRequired'),
  6556. 'String Required',
  6557. '/label',
  6558. 'input' => array(
  6559. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6560. 'id' => 'ContactStringRequired'
  6561. ),
  6562. '/div'
  6563. );
  6564. $this->assertTags($result, $expected);
  6565. $result = $this->Form->input('Contact.imnotrequired');
  6566. $expected = array(
  6567. 'div' => array('class' => 'input text'),
  6568. 'label' => array('for' => 'ContactImnotrequired'),
  6569. 'Imnotrequired',
  6570. '/label',
  6571. 'input' => array(
  6572. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6573. 'id' => 'ContactImnotrequired'
  6574. ),
  6575. '/div'
  6576. );
  6577. $this->assertTags($result, $expected);
  6578. $result = $this->Form->input('Contact.imalsonotrequired');
  6579. $expected = array(
  6580. 'div' => array('class' => 'input text'),
  6581. 'label' => array('for' => 'ContactImalsonotrequired'),
  6582. 'Imalsonotrequired',
  6583. '/label',
  6584. 'input' => array(
  6585. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6586. 'id' => 'ContactImalsonotrequired'
  6587. ),
  6588. '/div'
  6589. );
  6590. $this->assertTags($result, $expected);
  6591. $result = $this->Form->input('Contact.imalsonotrequired2');
  6592. $expected = array(
  6593. 'div' => array('class' => 'input text'),
  6594. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6595. 'Imalsonotrequired2',
  6596. '/label',
  6597. 'input' => array(
  6598. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6599. 'id' => 'ContactImalsonotrequired2'
  6600. ),
  6601. '/div'
  6602. );
  6603. $this->assertTags($result, $expected);
  6604. $result = $this->Form->input('Contact.imnotrequiredeither');
  6605. $expected = array(
  6606. 'div' => array('class' => 'input text'),
  6607. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6608. 'Imnotrequiredeither',
  6609. '/label',
  6610. 'input' => array(
  6611. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6612. 'id' => 'ContactImnotrequiredeither'
  6613. ),
  6614. '/div'
  6615. );
  6616. $this->assertTags($result, $expected);
  6617. $result = $this->Form->input('Contact.iamrequiredalways');
  6618. $expected = array(
  6619. 'div' => array('class' => 'input text required'),
  6620. 'label' => array('for' => 'ContactIamrequiredalways'),
  6621. 'Iamrequiredalways',
  6622. '/label',
  6623. 'input' => array(
  6624. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6625. 'id' => 'ContactIamrequiredalways'
  6626. ),
  6627. '/div'
  6628. );
  6629. $this->assertTags($result, $expected);
  6630. }
  6631. /**
  6632. * testFormMagicInput method
  6633. *
  6634. * @return void
  6635. */
  6636. public function testFormMagicInput() {
  6637. $encoding = strtolower(Configure::read('App.encoding'));
  6638. $result = $this->Form->create('Contact');
  6639. $expected = array(
  6640. 'form' => array(
  6641. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6642. 'accept-charset' => $encoding
  6643. ),
  6644. 'div' => array('style' => 'display:none;'),
  6645. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6646. '/div'
  6647. );
  6648. $this->assertTags($result, $expected);
  6649. $result = $this->Form->input('name');
  6650. $expected = array(
  6651. 'div' => array('class' => 'input text'),
  6652. 'label' => array('for' => 'ContactName'),
  6653. 'Name',
  6654. '/label',
  6655. 'input' => array(
  6656. 'type' => 'text', 'name' => 'data[Contact][name]',
  6657. 'id' => 'ContactName', 'maxlength' => '255'
  6658. ),
  6659. '/div'
  6660. );
  6661. $this->assertTags($result, $expected);
  6662. $result = $this->Form->input('non_existing_field_in_contact_model');
  6663. $expected = array(
  6664. 'div' => array('class' => 'input text'),
  6665. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6666. 'Non Existing Field In Contact Model',
  6667. '/label',
  6668. 'input' => array(
  6669. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6670. 'id' => 'ContactNonExistingFieldInContactModel'
  6671. ),
  6672. '/div'
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $result = $this->Form->input('Address.street');
  6676. $expected = array(
  6677. 'div' => array('class' => 'input text'),
  6678. 'label' => array('for' => 'AddressStreet'),
  6679. 'Street',
  6680. '/label',
  6681. 'input' => array(
  6682. 'type' => 'text', 'name' => 'data[Address][street]',
  6683. 'id' => 'AddressStreet'
  6684. ),
  6685. '/div'
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. $result = $this->Form->input('Address.non_existing_field_in_model');
  6689. $expected = array(
  6690. 'div' => array('class' => 'input text'),
  6691. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6692. 'Non Existing Field In Model',
  6693. '/label',
  6694. 'input' => array(
  6695. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6696. 'id' => 'AddressNonExistingFieldInModel'
  6697. ),
  6698. '/div'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. $result = $this->Form->input('name', array('div' => false));
  6702. $expected = array(
  6703. 'label' => array('for' => 'ContactName'),
  6704. 'Name',
  6705. '/label',
  6706. 'input' => array(
  6707. 'type' => 'text', 'name' => 'data[Contact][name]',
  6708. 'id' => 'ContactName', 'maxlength' => '255'
  6709. )
  6710. );
  6711. $this->assertTags($result, $expected);
  6712. extract($this->dateRegex);
  6713. $now = strtotime('now');
  6714. $result = $this->Form->input('Contact.published', array('div' => false));
  6715. $expected = array(
  6716. 'label' => array('for' => 'ContactPublishedMonth'),
  6717. 'Published',
  6718. '/label',
  6719. array('select' => array(
  6720. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6721. )),
  6722. $monthsRegex,
  6723. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6724. date('F', $now),
  6725. '/option',
  6726. '*/select',
  6727. '-',
  6728. array('select' => array(
  6729. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6730. )),
  6731. $daysRegex,
  6732. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6733. date('j', $now),
  6734. '/option',
  6735. '*/select',
  6736. '-',
  6737. array('select' => array(
  6738. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6739. )),
  6740. $yearsRegex,
  6741. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6742. date('Y', $now),
  6743. '*/select'
  6744. );
  6745. $this->assertTags($result, $expected);
  6746. $result = $this->Form->input('Contact.updated', array('div' => false));
  6747. $expected = array(
  6748. 'label' => array('for' => 'ContactUpdatedMonth'),
  6749. 'Updated',
  6750. '/label',
  6751. array('select' => array(
  6752. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6753. )),
  6754. $monthsRegex,
  6755. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6756. date('F', $now),
  6757. '/option',
  6758. '*/select',
  6759. '-',
  6760. array('select' => array(
  6761. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6762. )),
  6763. $daysRegex,
  6764. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6765. date('j', $now),
  6766. '/option',
  6767. '*/select',
  6768. '-',
  6769. array('select' => array(
  6770. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6771. )),
  6772. $yearsRegex,
  6773. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6774. date('Y', $now),
  6775. '*/select'
  6776. );
  6777. $this->assertTags($result, $expected);
  6778. $result = $this->Form->input('UserForm.stuff');
  6779. $expected = array(
  6780. 'div' => array('class' => 'input text'),
  6781. 'label' => array('for' => 'UserFormStuff'),
  6782. 'Stuff',
  6783. '/label',
  6784. 'input' => array(
  6785. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6786. 'id' => 'UserFormStuff', 'maxlength' => 10
  6787. ),
  6788. '/div'
  6789. );
  6790. $this->assertTags($result, $expected);
  6791. }
  6792. /**
  6793. * testForMagicInputNonExistingNorValidated method
  6794. *
  6795. * @return void
  6796. */
  6797. public function testForMagicInputNonExistingNorValidated() {
  6798. $encoding = strtolower(Configure::read('App.encoding'));
  6799. $result = $this->Form->create('Contact');
  6800. $expected = array(
  6801. 'form' => array(
  6802. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6803. 'accept-charset' => $encoding
  6804. ),
  6805. 'div' => array('style' => 'display:none;'),
  6806. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6807. '/div'
  6808. );
  6809. $this->assertTags($result, $expected);
  6810. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6811. $expected = array(
  6812. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6813. 'Non Existing Nor Validated',
  6814. '/label',
  6815. 'input' => array(
  6816. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6817. 'id' => 'ContactNonExistingNorValidated'
  6818. )
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6822. 'div' => false, 'value' => 'my value'
  6823. ));
  6824. $expected = array(
  6825. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6826. 'Non Existing Nor Validated',
  6827. '/label',
  6828. 'input' => array(
  6829. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6830. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6831. )
  6832. );
  6833. $this->assertTags($result, $expected);
  6834. $this->Form->request->data = array(
  6835. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6836. ));
  6837. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6838. $expected = array(
  6839. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6840. 'Non Existing Nor Validated',
  6841. '/label',
  6842. 'input' => array(
  6843. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6844. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6845. )
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. }
  6849. /**
  6850. * testFormMagicInputLabel method
  6851. *
  6852. * @return void
  6853. */
  6854. public function testFormMagicInputLabel() {
  6855. $encoding = strtolower(Configure::read('App.encoding'));
  6856. $result = $this->Form->create('Contact');
  6857. $expected = array(
  6858. 'form' => array(
  6859. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6860. 'accept-charset' => $encoding
  6861. ),
  6862. 'div' => array('style' => 'display:none;'),
  6863. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6864. '/div'
  6865. );
  6866. $this->assertTags($result, $expected);
  6867. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6868. $this->assertTags($result, array('input' => array(
  6869. 'name' => 'data[Contact][name]', 'type' => 'text',
  6870. 'id' => 'ContactName', 'maxlength' => '255')
  6871. ));
  6872. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6873. $expected = array(
  6874. 'label' => array('for' => 'ContactName'),
  6875. 'My label',
  6876. '/label',
  6877. 'input' => array(
  6878. 'type' => 'text', 'name' => 'data[Contact][name]',
  6879. 'id' => 'ContactName', 'maxlength' => '255'
  6880. )
  6881. );
  6882. $this->assertTags($result, $expected);
  6883. $result = $this->Form->input('Contact.name', array(
  6884. 'div' => false, 'label' => array('class' => 'mandatory')
  6885. ));
  6886. $expected = array(
  6887. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6888. 'Name',
  6889. '/label',
  6890. 'input' => array(
  6891. 'type' => 'text', 'name' => 'data[Contact][name]',
  6892. 'id' => 'ContactName', 'maxlength' => '255'
  6893. )
  6894. );
  6895. $this->assertTags($result, $expected);
  6896. $result = $this->Form->input('Contact.name', array(
  6897. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6898. ));
  6899. $expected = array(
  6900. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6901. 'My label',
  6902. '/label',
  6903. 'input' => array(
  6904. 'type' => 'text', 'name' => 'data[Contact][name]',
  6905. 'id' => 'ContactName', 'maxlength' => '255'
  6906. )
  6907. );
  6908. $this->assertTags($result, $expected);
  6909. $result = $this->Form->input('Contact.name', array(
  6910. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6911. ));
  6912. $expected = array(
  6913. 'label' => array('for' => 'my_id'),
  6914. 'Name',
  6915. '/label',
  6916. 'input' => array(
  6917. 'type' => 'text', 'name' => 'data[Contact][name]',
  6918. 'id' => 'my_id', 'maxlength' => '255'
  6919. )
  6920. );
  6921. $this->assertTags($result, $expected);
  6922. $result = $this->Form->input('1.id');
  6923. $this->assertTags($result, array('input' => array(
  6924. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6925. 'id' => 'Contact1Id'
  6926. )));
  6927. $result = $this->Form->input("1.name");
  6928. $expected = array(
  6929. 'div' => array('class' => 'input text'),
  6930. 'label' => array('for' => 'Contact1Name'),
  6931. 'Name',
  6932. '/label',
  6933. 'input' => array(
  6934. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6935. 'id' => 'Contact1Name', 'maxlength' => '255'
  6936. ),
  6937. '/div'
  6938. );
  6939. $this->assertTags($result, $expected);
  6940. $result = $this->Form->input('Contact.1.id');
  6941. $this->assertTags($result, array(
  6942. 'input' => array(
  6943. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6944. 'id' => 'Contact1Id'
  6945. )
  6946. ));
  6947. $result = $this->Form->input("Model.1.name");
  6948. $expected = array(
  6949. 'div' => array('class' => 'input text'),
  6950. 'label' => array('for' => 'Model1Name'),
  6951. 'Name',
  6952. '/label',
  6953. 'input' => array(
  6954. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6955. 'id' => 'Model1Name'
  6956. ),
  6957. '/div'
  6958. );
  6959. $this->assertTags($result, $expected);
  6960. }
  6961. /**
  6962. * testFormEnd method
  6963. *
  6964. * @return void
  6965. */
  6966. public function testFormEnd() {
  6967. $this->assertEquals('</form>', $this->Form->end());
  6968. $result = $this->Form->end('');
  6969. $expected = array(
  6970. 'div' => array('class' => 'submit'),
  6971. 'input' => array('type' => 'submit', 'value' => ''),
  6972. '/div',
  6973. '/form'
  6974. );
  6975. $this->assertTags($result, $expected);
  6976. $result = $this->Form->end(array('label' => ''));
  6977. $expected = array(
  6978. 'div' => array('class' => 'submit'),
  6979. 'input' => array('type' => 'submit', 'value' => ''),
  6980. '/div',
  6981. '/form'
  6982. );
  6983. $this->assertTags($result, $expected);
  6984. $result = $this->Form->end('save');
  6985. $expected = array(
  6986. 'div' => array('class' => 'submit'),
  6987. 'input' => array('type' => 'submit', 'value' => 'save'),
  6988. '/div',
  6989. '/form'
  6990. );
  6991. $this->assertTags($result, $expected);
  6992. $result = $this->Form->end(array('label' => 'save'));
  6993. $expected = array(
  6994. 'div' => array('class' => 'submit'),
  6995. 'input' => array('type' => 'submit', 'value' => 'save'),
  6996. '/div',
  6997. '/form'
  6998. );
  6999. $this->assertTags($result, $expected);
  7000. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7001. $expected = array(
  7002. 'div' => array('class' => 'submit'),
  7003. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7004. '/div',
  7005. '/form'
  7006. );
  7007. $this->assertTags($result, $expected);
  7008. $result = $this->Form->end(array('name' => 'Whatever'));
  7009. $expected = array(
  7010. 'div' => array('class' => 'submit'),
  7011. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7012. '/div',
  7013. '/form'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7017. $expected = array(
  7018. 'div' => array('class' => 'good'),
  7019. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7020. '/div',
  7021. '/form'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->end(array(
  7025. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7026. ));
  7027. $expected = array(
  7028. 'div' => array('class' => 'good'),
  7029. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7030. '/div',
  7031. '/form'
  7032. );
  7033. $this->assertTags($result, $expected);
  7034. }
  7035. /**
  7036. * testMultipleFormWithIdFields method
  7037. *
  7038. * @return void
  7039. */
  7040. public function testMultipleFormWithIdFields() {
  7041. $this->Form->create('UserForm');
  7042. $result = $this->Form->input('id');
  7043. $this->assertTags($result, array('input' => array(
  7044. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7045. )));
  7046. $result = $this->Form->input('ValidateItem.id');
  7047. $this->assertTags($result, array('input' => array(
  7048. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7049. 'id' => 'ValidateItemId'
  7050. )));
  7051. $result = $this->Form->input('ValidateUser.id');
  7052. $this->assertTags($result, array('input' => array(
  7053. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7054. 'id' => 'ValidateUserId'
  7055. )));
  7056. }
  7057. /**
  7058. * testDbLessModel method
  7059. *
  7060. * @return void
  7061. */
  7062. public function testDbLessModel() {
  7063. $this->Form->create('TestMail');
  7064. $result = $this->Form->input('name');
  7065. $expected = array(
  7066. 'div' => array('class' => 'input text'),
  7067. 'label' => array('for' => 'TestMailName'),
  7068. 'Name',
  7069. '/label',
  7070. 'input' => array(
  7071. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7072. 'id' => 'TestMailName'
  7073. ),
  7074. '/div'
  7075. );
  7076. $this->assertTags($result, $expected);
  7077. ClassRegistry::init('TestMail');
  7078. $this->Form->create('TestMail');
  7079. $result = $this->Form->input('name');
  7080. $expected = array(
  7081. 'div' => array('class' => 'input text'),
  7082. 'label' => array('for' => 'TestMailName'),
  7083. 'Name',
  7084. '/label',
  7085. 'input' => array(
  7086. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7087. 'id' => 'TestMailName'
  7088. ),
  7089. '/div'
  7090. );
  7091. $this->assertTags($result, $expected);
  7092. }
  7093. /**
  7094. * testBrokenness method
  7095. *
  7096. * @return void
  7097. */
  7098. public function testBrokenness() {
  7099. /*
  7100. * #4 This test has two parents and four children. By default (as of r7117) both
  7101. * parents are show but the first parent is missing a child. This is the inconsistency
  7102. * in the default behaviour - one parent has all children, the other does not - dependent
  7103. * on the data values.
  7104. */
  7105. $result = $this->Form->select('Model.field', array(
  7106. 'Fred' => array(
  7107. 'freds_son_1' => 'Fred',
  7108. 'freds_son_2' => 'Freddie'
  7109. ),
  7110. 'Bert' => array(
  7111. 'berts_son_1' => 'Albert',
  7112. 'berts_son_2' => 'Bertie')
  7113. ),
  7114. array('showParents' => true, 'empty' => false)
  7115. );
  7116. $expected = array(
  7117. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7118. array('optgroup' => array('label' => 'Fred')),
  7119. array('option' => array('value' => 'freds_son_1')),
  7120. 'Fred',
  7121. '/option',
  7122. array('option' => array('value' => 'freds_son_2')),
  7123. 'Freddie',
  7124. '/option',
  7125. '/optgroup',
  7126. array('optgroup' => array('label' => 'Bert')),
  7127. array('option' => array('value' => 'berts_son_1')),
  7128. 'Albert',
  7129. '/option',
  7130. array('option' => array('value' => 'berts_son_2')),
  7131. 'Bertie',
  7132. '/option',
  7133. '/optgroup',
  7134. '/select'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. /*
  7138. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7139. * changed, so we should expect the same select list data, just with a different name
  7140. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7141. * This is where data corruption can occur, because when a select value is missing from
  7142. * a list a form will substitute the first value in the list - without the user knowing.
  7143. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7144. * affect the availability of 3 => 'Three' as a valid option.
  7145. */
  7146. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7147. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7148. ));
  7149. $result = $this->Form->select(
  7150. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7151. );
  7152. $expected = array(
  7153. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7154. array('option' => array('value' => 1)),
  7155. 'One',
  7156. '/option',
  7157. array('option' => array('value' => 2)),
  7158. 'Two',
  7159. '/option',
  7160. array('optgroup' => array('label' => 'Three')),
  7161. array('option' => array('value' => 3)),
  7162. 'Three',
  7163. '/option',
  7164. array('option' => array('value' => 4)),
  7165. 'Four',
  7166. '/option',
  7167. array('option' => array('value' => 5)),
  7168. 'Five',
  7169. '/option',
  7170. '/optgroup',
  7171. '/select'
  7172. );
  7173. $this->assertTags($result, $expected);
  7174. }
  7175. /**
  7176. * Test the generation of fields for a multi record form.
  7177. *
  7178. * @return void
  7179. */
  7180. public function testMultiRecordForm() {
  7181. $this->Form->create('ValidateProfile');
  7182. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7183. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7184. $expected = array(
  7185. 'div' => array('class' => 'input textarea'),
  7186. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7187. 'Name',
  7188. '/label',
  7189. 'textarea' => array(
  7190. 'id' => 'ValidateProfile1ValidateItem2Name',
  7191. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7192. 'cols' => 30,
  7193. 'rows' => 6
  7194. ),
  7195. 'Value',
  7196. '/textarea',
  7197. '/div'
  7198. );
  7199. $this->assertTags($result, $expected);
  7200. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7201. $expected = array(
  7202. 'div' => array('class' => 'input date'),
  7203. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7204. 'Created',
  7205. '/label',
  7206. array('select' => array(
  7207. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7208. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7209. )
  7210. ),
  7211. array('option' => array('value' => '')), '/option',
  7212. $this->dateRegex['monthsRegex'],
  7213. '/select', '-',
  7214. array('select' => array(
  7215. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7216. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7217. )
  7218. ),
  7219. array('option' => array('value' => '')), '/option',
  7220. $this->dateRegex['daysRegex'],
  7221. '/select', '-',
  7222. array('select' => array(
  7223. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7224. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7225. )
  7226. ),
  7227. array('option' => array('value' => '')), '/option',
  7228. $this->dateRegex['yearsRegex'],
  7229. '/select',
  7230. '/div'
  7231. );
  7232. $this->assertTags($result, $expected);
  7233. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7234. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7235. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7236. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7237. $expected = array(
  7238. 'div' => array('class' => 'input select error'),
  7239. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7240. 'Profile',
  7241. '/label',
  7242. 'select' => array(
  7243. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7244. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7245. 'class' => 'form-error'
  7246. ),
  7247. '/select',
  7248. array('div' => array('class' => 'error-message')),
  7249. 'Error',
  7250. '/div',
  7251. '/div'
  7252. );
  7253. $this->assertTags($result, $expected);
  7254. }
  7255. /**
  7256. * test the correct display of multi-record form validation errors.
  7257. *
  7258. * @return void
  7259. */
  7260. public function testMultiRecordFormValidationErrors() {
  7261. $this->Form->create('ValidateProfile');
  7262. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7263. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7264. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7265. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7266. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7267. $result = $this->Form->error('ValidateProfile.2.city');
  7268. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7269. $result = $this->Form->error('2.city');
  7270. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7271. }
  7272. /**
  7273. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7274. *
  7275. * @return void
  7276. */
  7277. public function testInputTemplate() {
  7278. $result = $this->Form->input('Contact.email', array(
  7279. 'type' => 'text', 'format' => array('input')
  7280. ));
  7281. $expected = array(
  7282. 'div' => array('class' => 'input text'),
  7283. 'input' => array(
  7284. 'type' => 'text', 'name' => 'data[Contact][email]',
  7285. 'id' => 'ContactEmail'
  7286. ),
  7287. '/div'
  7288. );
  7289. $this->assertTags($result, $expected);
  7290. $result = $this->Form->input('Contact.email', array(
  7291. 'type' => 'text', 'format' => array('input', 'label'),
  7292. 'label' => '<em>Email (required)</em>'
  7293. ));
  7294. $expected = array(
  7295. 'div' => array('class' => 'input text'),
  7296. array('input' => array(
  7297. 'type' => 'text', 'name' => 'data[Contact][email]',
  7298. 'id' => 'ContactEmail'
  7299. )),
  7300. 'label' => array('for' => 'ContactEmail'),
  7301. 'em' => array(),
  7302. 'Email (required)',
  7303. '/em',
  7304. '/label',
  7305. '/div'
  7306. );
  7307. $this->assertTags($result, $expected);
  7308. $result = $this->Form->input('Contact.email', array(
  7309. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7310. 'between' => '<div>Something in the middle</div>',
  7311. 'after' => '<span>Some text at the end</span>'
  7312. ));
  7313. $expected = array(
  7314. 'div' => array('class' => 'input text'),
  7315. array('input' => array(
  7316. 'type' => 'text', 'name' => 'data[Contact][email]',
  7317. 'id' => 'ContactEmail'
  7318. )),
  7319. array('div' => array()),
  7320. 'Something in the middle',
  7321. '/div',
  7322. 'label' => array('for' => 'ContactEmail'),
  7323. 'Email',
  7324. '/label',
  7325. 'span' => array(),
  7326. 'Some text at the end',
  7327. '/span',
  7328. '/div'
  7329. );
  7330. $this->assertTags($result, $expected);
  7331. $result = $this->Form->input('Contact.method', array(
  7332. 'type' => 'radio',
  7333. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7334. 'between' => 'I am between',
  7335. ));
  7336. $expected = array(
  7337. 'div' => array('class' => 'input radio'),
  7338. 'fieldset' => array(),
  7339. 'legend' => array(),
  7340. 'Method',
  7341. '/legend',
  7342. 'I am between',
  7343. 'input' => array(
  7344. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7345. 'value' => '', 'id' => 'ContactMethod_'
  7346. ),
  7347. array('input' => array(
  7348. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7349. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7350. )),
  7351. array('label' => array('for' => 'ContactMethodEmail')),
  7352. 'Email',
  7353. '/label',
  7354. array('input' => array(
  7355. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7356. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7357. )),
  7358. array('label' => array('for' => 'ContactMethodPigeon')),
  7359. 'Pigeon',
  7360. '/label',
  7361. '/fieldset',
  7362. '/div',
  7363. );
  7364. $this->assertTags($result, $expected);
  7365. }
  7366. /**
  7367. * test that some html5 inputs + FormHelper::__call() work
  7368. *
  7369. * @return void
  7370. */
  7371. public function testHtml5Inputs() {
  7372. $result = $this->Form->email('User.email');
  7373. $expected = array(
  7374. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. $result = $this->Form->search('User.query');
  7378. $expected = array(
  7379. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7380. );
  7381. $this->assertTags($result, $expected);
  7382. $result = $this->Form->search('User.query', array('value' => 'test'));
  7383. $expected = array(
  7384. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7385. );
  7386. $this->assertTags($result, $expected);
  7387. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7388. $expected = array(
  7389. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7393. $expected = array(
  7394. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7395. );
  7396. $this->assertTags($result, $expected);
  7397. }
  7398. /**
  7399. *
  7400. * @expectedException CakeException
  7401. * @return void
  7402. */
  7403. public function testHtml5InputException() {
  7404. $this->Form->email();
  7405. }
  7406. /**
  7407. * Tests that a model can be loaded from the model names passed in the request object
  7408. *
  7409. * @return void
  7410. */
  7411. public function testIntrospectModelFromRequest() {
  7412. $this->loadFixtures('Post');
  7413. App::build(array(
  7414. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7415. ));
  7416. CakePlugin::load('TestPlugin');
  7417. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7418. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7419. $this->Form->create('TestPluginPost');
  7420. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7421. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7422. CakePlugin::unload();
  7423. App::build();
  7424. }
  7425. /**
  7426. * Tests that it is possible to set the validation errors directly in the helper for a field
  7427. *
  7428. * @return void
  7429. */
  7430. public function testCustomValidationErrors() {
  7431. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7432. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7433. $this->assertEquals('Badness!', $result);
  7434. }
  7435. /**
  7436. * Tests that the 'on' key validates as expected on create
  7437. *
  7438. * @return void
  7439. */
  7440. public function testRequiredOnCreate() {
  7441. $this->Form->create('Contact');
  7442. $result = $this->Form->input('Contact.imrequiredonupdate');
  7443. $expected = array(
  7444. 'div' => array('class' => 'input text'),
  7445. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7446. 'Imrequiredonupdate',
  7447. '/label',
  7448. 'input' => array(
  7449. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7450. 'id' => 'ContactImrequiredonupdate'
  7451. ),
  7452. '/div'
  7453. );
  7454. $this->assertTags($result, $expected);
  7455. $result = $this->Form->input('Contact.imrequiredoncreate');
  7456. $expected = array(
  7457. 'div' => array('class' => 'input text required'),
  7458. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7459. 'Imrequiredoncreate',
  7460. '/label',
  7461. 'input' => array(
  7462. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7463. 'id' => 'ContactImrequiredoncreate'
  7464. ),
  7465. '/div'
  7466. );
  7467. $this->assertTags($result, $expected);
  7468. $result = $this->Form->input('Contact.imrequiredonboth');
  7469. $expected = array(
  7470. 'div' => array('class' => 'input text required'),
  7471. 'label' => array('for' => 'ContactImrequiredonboth'),
  7472. 'Imrequiredonboth',
  7473. '/label',
  7474. 'input' => array(
  7475. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7476. 'id' => 'ContactImrequiredonboth'
  7477. ),
  7478. '/div'
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. $result = $this->Form->input('Contact.imrequired');
  7482. $expected = array(
  7483. 'div' => array('class' => 'input text required'),
  7484. 'label' => array('for' => 'ContactImrequired'),
  7485. 'Imrequired',
  7486. '/label',
  7487. 'input' => array(
  7488. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7489. 'id' => 'ContactImrequired'
  7490. ),
  7491. '/div'
  7492. );
  7493. $this->assertTags($result, $expected);
  7494. }
  7495. /**
  7496. * Tests that the 'on' key validates as expected on update
  7497. *
  7498. * @return void
  7499. */
  7500. public function testRequiredOnUpdate() {
  7501. $this->Form->request->data['Contact']['id'] = 1;
  7502. $this->Form->create('Contact');
  7503. $result = $this->Form->input('Contact.imrequiredonupdate');
  7504. $expected = array(
  7505. 'div' => array('class' => 'input text required'),
  7506. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7507. 'Imrequiredonupdate',
  7508. '/label',
  7509. 'input' => array(
  7510. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7511. 'id' => 'ContactImrequiredonupdate'
  7512. ),
  7513. '/div'
  7514. );
  7515. $this->assertTags($result, $expected);
  7516. $result = $this->Form->input('Contact.imrequiredoncreate');
  7517. $expected = array(
  7518. 'div' => array('class' => 'input text'),
  7519. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7520. 'Imrequiredoncreate',
  7521. '/label',
  7522. 'input' => array(
  7523. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7524. 'id' => 'ContactImrequiredoncreate'
  7525. ),
  7526. '/div'
  7527. );
  7528. $this->assertTags($result, $expected);
  7529. $result = $this->Form->input('Contact.imrequiredonboth');
  7530. $expected = array(
  7531. 'div' => array('class' => 'input text required'),
  7532. 'label' => array('for' => 'ContactImrequiredonboth'),
  7533. 'Imrequiredonboth',
  7534. '/label',
  7535. 'input' => array(
  7536. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7537. 'id' => 'ContactImrequiredonboth'
  7538. ),
  7539. '/div'
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. $result = $this->Form->input('Contact.imrequired');
  7543. $expected = array(
  7544. 'div' => array('class' => 'input text required'),
  7545. 'label' => array('for' => 'ContactImrequired'),
  7546. 'Imrequired',
  7547. '/label',
  7548. 'input' => array(
  7549. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7550. 'id' => 'ContactImrequired'
  7551. ),
  7552. '/div'
  7553. );
  7554. $this->assertTags($result, $expected);
  7555. }
  7556. /**
  7557. * Test inputDefaults setter and getter
  7558. *
  7559. * @return void
  7560. */
  7561. public function testInputDefaults() {
  7562. $this->Form->create('Contact');
  7563. $this->Form->inputDefaults(array(
  7564. 'label' => false,
  7565. 'div' => array(
  7566. 'style' => 'color: #000;'
  7567. )
  7568. ));
  7569. $result = $this->Form->input('Contact.field1');
  7570. $expected = array(
  7571. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7572. 'input' => array(
  7573. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7574. 'id' => 'ContactField1'
  7575. ),
  7576. '/div'
  7577. );
  7578. $this->assertTags($result, $expected);
  7579. $this->Form->inputDefaults(array(
  7580. 'div' => false,
  7581. 'label' => 'Label',
  7582. ));
  7583. $result = $this->Form->input('Contact.field1');
  7584. $expected = array(
  7585. 'label' => array('for' => 'ContactField1'),
  7586. 'Label',
  7587. '/label',
  7588. 'input' => array(
  7589. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7590. 'id' => 'ContactField1'
  7591. ),
  7592. );
  7593. $this->assertTags($result, $expected);
  7594. $this->Form->inputDefaults(array(
  7595. 'label' => false,
  7596. ), true);
  7597. $result = $this->Form->input('Contact.field1');
  7598. $expected = array(
  7599. 'input' => array(
  7600. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7601. 'id' => 'ContactField1'
  7602. ),
  7603. );
  7604. $this->assertTags($result, $expected);
  7605. $result = $this->Form->inputDefaults();
  7606. $expected = array(
  7607. 'div' => false,
  7608. 'label' => false,
  7609. );
  7610. $this->assertEqual($result, $expected);
  7611. }
  7612. }