FormHelperTest.php 263 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352
  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. * Test validation errors.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testPasswordValidation() {
  1362. $Contact = ClassRegistry::getObject('Contact');
  1363. $Contact->validationErrors['password'] = array('Please provide a password');
  1364. $result = $this->Form->input('Contact.password');
  1365. $expected = array(
  1366. 'div' => array('class' => 'input password error'),
  1367. 'label' => array('for' => 'ContactPassword'),
  1368. 'Password',
  1369. '/label',
  1370. 'input' => array(
  1371. 'type' => 'password', 'name' => 'data[Contact][password]',
  1372. 'id' => 'ContactPassword', 'class' => 'form-error'
  1373. ),
  1374. array('div' => array('class' => 'error-message')),
  1375. 'Please provide a password',
  1376. '/div',
  1377. '/div'
  1378. );
  1379. $this->assertTags($result, $expected);
  1380. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1381. $expected = array(
  1382. 'div' => array('class' => 'input password error'),
  1383. 'label' => array('for' => 'ContactPassword'),
  1384. 'Password',
  1385. '/label',
  1386. 'input' => array(
  1387. 'type' => 'password', 'name' => 'data[Contact][password]',
  1388. 'id' => 'ContactPassword', 'class' => 'form-error'
  1389. ),
  1390. '/div'
  1391. );
  1392. $this->assertTags($result, $expected);
  1393. }
  1394. /**
  1395. * Test validation errors, when validation message is an empty string.
  1396. *
  1397. * @access public
  1398. * @return void
  1399. */
  1400. public function testEmptyErrorValidation() {
  1401. $this->Form->validationErrors['Contact']['password'] = '';
  1402. $result = $this->Form->input('Contact.password');
  1403. $expected = array(
  1404. 'div' => array('class' => 'input password error'),
  1405. 'label' => array('for' => 'ContactPassword'),
  1406. 'Password',
  1407. '/label',
  1408. 'input' => array(
  1409. 'type' => 'password', 'name' => 'data[Contact][password]',
  1410. 'id' => 'ContactPassword', 'class' => 'form-error'
  1411. ),
  1412. array('div' => array('class' => 'error-message')),
  1413. array(),
  1414. '/div',
  1415. '/div'
  1416. );
  1417. $this->assertTags($result, $expected);
  1418. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1419. $expected = array(
  1420. 'div' => array('class' => 'input password error'),
  1421. 'label' => array('for' => 'ContactPassword'),
  1422. 'Password',
  1423. '/label',
  1424. 'input' => array(
  1425. 'type' => 'password', 'name' => 'data[Contact][password]',
  1426. 'id' => 'ContactPassword', 'class' => 'form-error'
  1427. ),
  1428. '/div'
  1429. );
  1430. $this->assertTags($result, $expected);
  1431. }
  1432. /**
  1433. * Test validation errors, when calling input() overriding validation message by an empty string.
  1434. *
  1435. * @access public
  1436. * @return void
  1437. */
  1438. public function testEmptyInputErrorValidation() {
  1439. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1440. $result = $this->Form->input('Contact.password', array('error' => ''));
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. array('div' => array('class' => 'error-message')),
  1451. array(),
  1452. '/div',
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1457. $expected = array(
  1458. 'div' => array('class' => 'input password error'),
  1459. 'label' => array('for' => 'ContactPassword'),
  1460. 'Password',
  1461. '/label',
  1462. 'input' => array(
  1463. 'type' => 'password', 'name' => 'data[Contact][password]',
  1464. 'id' => 'ContactPassword', 'class' => 'form-error'
  1465. ),
  1466. '/div'
  1467. );
  1468. $this->assertTags($result, $expected);
  1469. }
  1470. /**
  1471. * testFormValidationAssociated method
  1472. *
  1473. * test display of form errors in conjunction with model::validates.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testFormValidationAssociated() {
  1478. $this->UserForm = ClassRegistry::getObject('UserForm');
  1479. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1480. $data = array(
  1481. 'UserForm' => array('name' => 'user'),
  1482. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1483. );
  1484. $result = $this->UserForm->OpenidUrl->create($data);
  1485. $this->assertFalse(empty($result));
  1486. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1487. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1488. $encoding = strtolower(Configure::read('App.encoding'));
  1489. $expected = array(
  1490. 'form' => array(
  1491. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1492. 'accept-charset' => $encoding
  1493. ),
  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. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Error, not registered', $result);
  1503. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1504. }
  1505. /**
  1506. * testFormValidationAssociatedFirstLevel method
  1507. *
  1508. * test form error display with associated model.
  1509. *
  1510. * @return void
  1511. */
  1512. public function testFormValidationAssociatedFirstLevel() {
  1513. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1514. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1515. $data = array(
  1516. 'ValidateUser' => array('name' => 'mariano'),
  1517. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1518. );
  1519. $result = $this->ValidateUser->create($data);
  1520. $this->assertFalse(empty($result));
  1521. $this->assertFalse($this->ValidateUser->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $this->assertEquals('Invalid city', $result);
  1544. unset($this->ValidateUser->ValidateProfile);
  1545. unset($this->ValidateUser);
  1546. }
  1547. /**
  1548. * testFormValidationAssociatedSecondLevel method
  1549. *
  1550. * test form error display with associated model.
  1551. *
  1552. * @return void
  1553. */
  1554. public function testFormValidationAssociatedSecondLevel() {
  1555. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1556. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1557. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1558. $data = array(
  1559. 'ValidateUser' => array('name' => 'mariano'),
  1560. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1561. 'ValidateItem' => array('name' => 'Item')
  1562. );
  1563. $result = $this->ValidateUser->create($data);
  1564. $this->assertFalse(empty($result));
  1565. $this->assertFalse($this->ValidateUser->validates());
  1566. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1567. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1568. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1569. $encoding = strtolower(Configure::read('App.encoding'));
  1570. $expected = array(
  1571. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1572. 'div' => array('style' => 'display:none;'),
  1573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. $result = $this->Form->error(
  1578. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1579. );
  1580. $this->assertEquals('Invalid email', $result);
  1581. $result = $this->Form->error(
  1582. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1583. );
  1584. $this->assertEquals('Invalid name', $result);
  1585. $result = $this->Form->error(
  1586. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1587. );
  1588. $result = $this->Form->error(
  1589. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1590. );
  1591. $this->assertEquals('Invalid description', $result);
  1592. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1593. unset($this->ValidateUser->ValidateProfile);
  1594. unset($this->ValidateUser);
  1595. }
  1596. /**
  1597. * testFormValidationMultiRecord method
  1598. *
  1599. * test form error display with multiple records.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testFormValidationMultiRecord() {
  1604. $Contact = ClassRegistry::getObject('Contact');
  1605. $Contact->validationErrors[2] = array(
  1606. 'name' => array('This field cannot be left blank')
  1607. );
  1608. $result = $this->Form->input('Contact.2.name');
  1609. $expected = array(
  1610. 'div' => array('class' => 'input text error'),
  1611. 'label' => array('for' => 'Contact2Name'),
  1612. 'Name',
  1613. '/label',
  1614. 'input' => array(
  1615. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1616. 'class' => 'form-error', 'maxlength' => 255
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be left blank',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. }
  1625. /**
  1626. * testMultipleInputValidation method
  1627. *
  1628. * test multiple record form validation error display.
  1629. *
  1630. * @return void
  1631. */
  1632. public function testMultipleInputValidation() {
  1633. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1634. $Address->validationErrors[0] = array(
  1635. 'title' => array('This field cannot be empty'),
  1636. 'first_name' => array('This field cannot be empty')
  1637. );
  1638. $Address->validationErrors[1] = array(
  1639. 'last_name' => array('You must have a last name')
  1640. );
  1641. $this->Form->create();
  1642. $result = $this->Form->input('Address.0.title');
  1643. $expected = array(
  1644. 'div' => array('class'),
  1645. 'label' => array('for'),
  1646. 'preg:/[^<]+/',
  1647. '/label',
  1648. 'input' => array(
  1649. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1650. ),
  1651. array('div' => array('class' => 'error-message')),
  1652. 'This field cannot be empty',
  1653. '/div',
  1654. '/div'
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Address.0.first_name');
  1658. $expected = array(
  1659. 'div' => array('class'),
  1660. 'label' => array('for'),
  1661. 'preg:/[^<]+/',
  1662. '/label',
  1663. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1664. array('div' => array('class' => 'error-message')),
  1665. 'This field cannot be empty',
  1666. '/div',
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Address.0.last_name');
  1671. $expected = array(
  1672. 'div' => array('class'),
  1673. 'label' => array('for'),
  1674. 'preg:/[^<]+/',
  1675. '/label',
  1676. 'input' => array('type' => 'text', 'name', 'id'),
  1677. '/div'
  1678. );
  1679. $this->assertTags($result, $expected);
  1680. $result = $this->Form->input('Address.1.last_name');
  1681. $expected = array(
  1682. 'div' => array('class'),
  1683. 'label' => array('for'),
  1684. 'preg:/[^<]+/',
  1685. '/label',
  1686. 'input' => array(
  1687. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1688. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1689. ),
  1690. array('div' => array('class' => 'error-message')),
  1691. 'You must have a last name',
  1692. '/div',
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. }
  1697. /**
  1698. * testInput method
  1699. *
  1700. * Test various incarnations of input().
  1701. *
  1702. * @return void
  1703. */
  1704. public function testInput() {
  1705. $result = $this->Form->input('ValidateUser.balance');
  1706. $expected = array(
  1707. 'div' => array('class'),
  1708. 'label' => array('for'),
  1709. 'Balance',
  1710. '/label',
  1711. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1712. '/div',
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'custom'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][email]',
  1723. 'id' => 'custom', 'maxlength' => 255
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1729. $expected = array(
  1730. '<div',
  1731. 'label' => array('for' => 'ContactEmail'),
  1732. 'Email',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'text', 'name' => 'data[Contact][email]',
  1736. 'id' => 'ContactEmail', 'maxlength' => 255
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->hidden('Contact.idontexist');
  1742. $expected = array('input' => array(
  1743. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1744. 'id' => 'ContactIdontexist'
  1745. ));
  1746. $this->assertTags($result, $expected);
  1747. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1748. $expected = array(
  1749. 'div' => array('class' => 'input text'),
  1750. 'label' => array('for' => 'ContactEmail'),
  1751. 'Email',
  1752. '/label',
  1753. array('input' => array(
  1754. 'type' => 'text', 'name' => 'data[Contact][email]',
  1755. 'id' => 'ContactEmail'
  1756. )),
  1757. '/div'
  1758. );
  1759. $this->assertTags($result, $expected);
  1760. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1761. $expected = array(
  1762. 'div' => array('class' => 'input text'),
  1763. 'label' => array('for' => 'Contact5Email'),
  1764. 'Email',
  1765. '/label',
  1766. array('input' => array(
  1767. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1768. 'id' => 'Contact5Email'
  1769. )),
  1770. '/div'
  1771. );
  1772. $this->assertTags($result, $expected);
  1773. $result = $this->Form->input('Contact.password');
  1774. $expected = array(
  1775. 'div' => array('class' => 'input password'),
  1776. 'label' => array('for' => 'ContactPassword'),
  1777. 'Password',
  1778. '/label',
  1779. array('input' => array(
  1780. 'type' => 'password', 'name' => 'data[Contact][password]',
  1781. 'id' => 'ContactPassword'
  1782. )),
  1783. '/div'
  1784. );
  1785. $this->assertTags($result, $expected);
  1786. $result = $this->Form->input('Contact.email', array(
  1787. 'type' => 'file', 'class' => 'textbox'
  1788. ));
  1789. $expected = array(
  1790. 'div' => array('class' => 'input file'),
  1791. 'label' => array('for' => 'ContactEmail'),
  1792. 'Email',
  1793. '/label',
  1794. array('input' => array(
  1795. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1796. 'id' => 'ContactEmail'
  1797. )),
  1798. '/div'
  1799. );
  1800. $this->assertTags($result, $expected);
  1801. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1802. $result = $this->Form->input('Contact.phone');
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text'),
  1805. 'label' => array('for' => 'ContactPhone'),
  1806. 'Phone',
  1807. '/label',
  1808. array('input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1810. 'value' => 'Hello &amp; World &gt; weird chars',
  1811. 'id' => 'ContactPhone', 'maxlength' => 255
  1812. )),
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1817. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text'),
  1820. 'label' => array('for' => 'myId'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1825. 'value' => 'My value', 'id' => 'myId'
  1826. ),
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. unset($this->Form->request->data);
  1831. $Contact = ClassRegistry::getObject('Contact');
  1832. $Contact->validationErrors['field'] = array('Badness!');
  1833. $result = $this->Form->input('Contact.field');
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text error'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array(
  1840. 'type' => 'text', 'name' => 'data[Contact][field]',
  1841. 'id' => 'ContactField', 'class' => 'form-error'
  1842. ),
  1843. array('div' => array('class' => 'error-message')),
  1844. 'Badness!',
  1845. '/div',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1851. ));
  1852. $expected = array(
  1853. 'label' => array('for' => 'ContactField'),
  1854. 'Field',
  1855. '/label',
  1856. 'input' => array(
  1857. 'type' => 'text', 'name' => 'data[Contact][field]',
  1858. 'id' => 'ContactField', 'class' => 'form-error'
  1859. ),
  1860. array('span' => array('class' => 'error-message')),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array(
  1866. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1867. ));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text error'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'text', 'name' => 'data[Contact][field]',
  1875. 'id' => 'ContactField', 'class' => 'form-error'
  1876. ),
  1877. array('div' => array('class' => 'error')),
  1878. 'Badness!',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $result = $this->Form->input('Contact.field', array(
  1883. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1884. ));
  1885. $expected = array(
  1886. 'span' => array('class' => 'input text error'),
  1887. 'label' => array('for' => 'ContactField'),
  1888. 'Field',
  1889. '/label',
  1890. 'input' => array(
  1891. 'type' => 'text', 'name' => 'data[Contact][field]',
  1892. 'id' => 'ContactField', 'class' => 'form-error'
  1893. ),
  1894. 'Badness!',
  1895. '/span'
  1896. );
  1897. $this->assertTags($result, $expected);
  1898. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1899. $expected = array(
  1900. 'div' => array('class' => 'input text error'),
  1901. 'label' => array('for' => 'ContactField'),
  1902. 'Field',
  1903. '/label',
  1904. 'input' => array(
  1905. 'type' => 'text', 'name' => 'data[Contact][field]',
  1906. 'id' => 'ContactField', 'class' => 'form-error'
  1907. ),
  1908. 'A message to you, Rudy',
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Badness!',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $this->Form->setEntity(null);
  1916. $this->Form->setEntity('Contact.field');
  1917. $result = $this->Form->input('Contact.field', array(
  1918. 'after' => 'A message to you, Rudy', 'error' => false
  1919. ));
  1920. $expected = array(
  1921. 'div' => array('class' => 'input text'),
  1922. 'label' => array('for' => 'ContactField'),
  1923. 'Field',
  1924. '/label',
  1925. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1926. 'A message to you, Rudy',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1931. $expected = array(
  1932. 'div' => array('class' => 'input text'),
  1933. 'label' => array('for' => 'ObjectField'),
  1934. 'Field',
  1935. '/label',
  1936. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1937. 'A message to you, Rudy',
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. $Contact->validationErrors['field'] = array('minLength');
  1942. $result = $this->Form->input('Contact.field', array(
  1943. 'error' => array(
  1944. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1945. 'maxLength' => 'login too large'
  1946. )
  1947. ));
  1948. $expected = array(
  1949. 'div' => array('class' => 'input text error'),
  1950. 'label' => array('for' => 'ContactField'),
  1951. 'Field',
  1952. '/label',
  1953. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1954. array('div' => array('class' => 'error-message')),
  1955. 'Le login doit contenir au moins 2 caractères',
  1956. '/div',
  1957. '/div'
  1958. );
  1959. $this->assertTags($result, $expected);
  1960. $Contact->validationErrors['field'] = array('maxLength');
  1961. $result = $this->Form->input('Contact.field', array(
  1962. 'error' => array(
  1963. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1964. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1965. 'maxLength' => 'login too large',
  1966. )
  1967. ));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input text error'),
  1970. 'label' => array('for' => 'ContactField'),
  1971. 'Field',
  1972. '/label',
  1973. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1974. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1975. 'login too large',
  1976. '/span',
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. }
  1981. /**
  1982. * Test that inputs with 0 can be created.
  1983. *
  1984. * @return void
  1985. */
  1986. public function testInputZero() {
  1987. $this->Form->create('User');
  1988. $result = $this->Form->input('0');
  1989. $expected = array(
  1990. 'div' => array('class' => 'input text'),
  1991. 'label' => array('for' => 'User0'), '/label',
  1992. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. }
  1997. /**
  1998. * test input() with checkbox creation
  1999. *
  2000. * @return void
  2001. */
  2002. public function testInputCheckbox() {
  2003. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2004. $expected = array(
  2005. 'div' => array('class' => 'input checkbox'),
  2006. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2007. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2008. '/div'
  2009. );
  2010. $this->assertTags($result, $expected);
  2011. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2012. $expected = array(
  2013. 'div' => array('class' => 'input checkbox'),
  2014. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2015. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2016. '/div'
  2017. );
  2018. $this->assertTags($result, $expected);
  2019. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2020. $expected = array(
  2021. 'div' => array('class' => 'input checkbox'),
  2022. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2023. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2024. '/div'
  2025. );
  2026. $this->assertTags($result, $expected);
  2027. }
  2028. /**
  2029. * test form->input() with time types.
  2030. *
  2031. */
  2032. public function testInputTime() {
  2033. extract($this->dateRegex);
  2034. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2035. $result = explode(':', $result);
  2036. $this->assertRegExp('/option value="23"/', $result[0]);
  2037. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2038. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2039. $result = explode(':', $result);
  2040. $this->assertRegExp('/option value="23"/', $result[0]);
  2041. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2042. $result = $this->Form->input('Model.field', array(
  2043. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2044. ));
  2045. $result = explode(':', $result);
  2046. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2047. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2048. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2049. $result = $this->Form->input('Model.field', array(
  2050. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2051. ));
  2052. $result = explode(':', $result);
  2053. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2054. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2055. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2056. $result = $this->Form->input('prueba', array(
  2057. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2058. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2059. ));
  2060. $result = explode(':', $result);
  2061. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2062. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2063. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2064. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2065. $result = $this->Form->input('Random.start_time', array(
  2066. 'type' => 'time',
  2067. 'selected' => '18:15'
  2068. ));
  2069. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2070. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2071. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2072. }
  2073. /**
  2074. * Test interval + selected near the hour roll over.
  2075. *
  2076. * @return void
  2077. */
  2078. public function testTimeSelectedWithInterval() {
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2083. ));
  2084. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2087. $result = $this->Form->input('Model.start_time', array(
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '2012-10-23 15:57:00'
  2091. ));
  2092. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2095. $result = $this->Form->input('Model.start_time', array(
  2096. 'timeFormat' => 24,
  2097. 'type' => 'time',
  2098. 'interval' => 15,
  2099. 'selected' => '15:57'
  2100. ));
  2101. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2103. $result = $this->Form->input('Model.start_time', array(
  2104. 'timeFormat' => 24,
  2105. 'type' => 'time',
  2106. 'interval' => 15,
  2107. 'selected' => '23:57'
  2108. ));
  2109. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2110. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2111. $result = $this->Form->input('Model.created', array(
  2112. 'timeFormat' => 24,
  2113. 'type' => 'datetime',
  2114. 'interval' => 15,
  2115. 'selected' => '2012-09-30 23:56'
  2116. ));
  2117. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2118. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2119. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2120. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2121. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2122. }
  2123. /**
  2124. * test form->input() with datetime, date and time types
  2125. *
  2126. * @return void
  2127. */
  2128. public function testInputDatetime() {
  2129. extract($this->dateRegex);
  2130. $result = $this->Form->input('prueba', array(
  2131. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2132. 'maxYear' => date('Y') + 1, 'interval' => 15
  2133. ));
  2134. $result = explode(':', $result);
  2135. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2136. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2137. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2138. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2139. //related to ticket #5013
  2140. $result = $this->Form->input('Contact.date', array(
  2141. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2142. ));
  2143. $this->assertRegExp('/class="customClass"/', $result);
  2144. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2145. $result = $this->Form->input('Contact.date', array(
  2146. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2147. ));
  2148. $this->assertRegExp('/id="customIdDay"/', $result);
  2149. $this->assertRegExp('/id="customIdMonth"/', $result);
  2150. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2151. $result = $this->Form->input('Model.field', array(
  2152. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2153. ));
  2154. $this->assertRegExp('/id="customIDDay"/', $result);
  2155. $this->assertRegExp('/id="customIDHour"/', $result);
  2156. $result = explode('</select><select', $result);
  2157. $result = explode(':', $result[1]);
  2158. $this->assertRegExp('/option value="23"/', $result[0]);
  2159. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2160. $result = $this->Form->input('Model.field', array(
  2161. 'type' => 'datetime', 'timeFormat' => 12
  2162. ));
  2163. $result = explode('</select><select', $result);
  2164. $result = explode(':', $result[1]);
  2165. $this->assertRegExp('/option value="12"/', $result[0]);
  2166. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2167. $this->Form->request->data = array('Contact' => array('created' => null));
  2168. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2169. $expected = array(
  2170. 'div' => array('class' => 'input date'),
  2171. 'label' => array('for' => 'ContactCreatedMonth'),
  2172. 'Created',
  2173. '/label',
  2174. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2175. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2176. $monthsRegex,
  2177. '/select', '-',
  2178. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2179. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2180. $daysRegex,
  2181. '/select', '-',
  2182. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2183. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2184. $yearsRegex,
  2185. '/select',
  2186. '/div'
  2187. );
  2188. $this->assertTags($result, $expected);
  2189. $this->Form->request->data = array('Contact' => array('created' => null));
  2190. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2191. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2192. $this->Form->request->data = array('Contact' => array('created' => null));
  2193. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2194. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2195. $result = $this->Form->input('Contact.created', array(
  2196. 'type' => 'date',
  2197. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2198. 'timeFormat' => 'NONE'
  2199. ));
  2200. $this->assertRegExp('/for\="created-month"/', $result);
  2201. }
  2202. /**
  2203. * Test generating checkboxes in a loop.
  2204. *
  2205. * @return void
  2206. */
  2207. public function testInputCheckboxesInLoop() {
  2208. for ($i = 1; $i < 5; $i++) {
  2209. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2210. $expected = array(
  2211. 'div' => array('class' => 'input checkbox'),
  2212. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2213. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2214. 'label' => array('for' => "Contact{$i}Email"),
  2215. 'Email',
  2216. '/label',
  2217. '/div'
  2218. );
  2219. $this->assertTags($result, $expected);
  2220. }
  2221. }
  2222. /**
  2223. * Test generating checkboxes with disabled elements.
  2224. *
  2225. * @return void
  2226. */
  2227. public function testInputCheckboxWithDisabledElements() {
  2228. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2229. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2230. $expected = array(
  2231. array('div' => array('class' => 'input select')),
  2232. array('label' => array('for' => "ContactMultiple")),
  2233. 'Multiple',
  2234. '/label',
  2235. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2236. array('div' => array('class' => 'checkbox')),
  2237. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2238. array('label' => array('for' => "ContactMultiple1")),
  2239. 'One',
  2240. '/label',
  2241. '/div',
  2242. array('div' => array('class' => 'checkbox')),
  2243. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2244. array('label' => array('for' => "ContactMultiple2")),
  2245. 'Two',
  2246. '/label',
  2247. '/div',
  2248. array('div' => array('class' => 'checkbox')),
  2249. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2250. array('label' => array('for' => "ContactMultiple3")),
  2251. 'Three',
  2252. '/label',
  2253. '/div',
  2254. '/div'
  2255. );
  2256. $this->assertTags($result, $expected);
  2257. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2258. $this->assertTags($result, $expected);
  2259. $disabled = array('2', 3);
  2260. $expected = array(
  2261. array('div' => array('class' => 'input select')),
  2262. array('label' => array('for' => "ContactMultiple")),
  2263. 'Multiple',
  2264. '/label',
  2265. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2266. array('div' => array('class' => 'checkbox')),
  2267. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2268. array('label' => array('for' => "ContactMultiple1")),
  2269. 'One',
  2270. '/label',
  2271. '/div',
  2272. array('div' => array('class' => 'checkbox')),
  2273. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2274. array('label' => array('for' => "ContactMultiple2")),
  2275. 'Two',
  2276. '/label',
  2277. '/div',
  2278. array('div' => array('class' => 'checkbox')),
  2279. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2280. array('label' => array('for' => "ContactMultiple3")),
  2281. 'Three',
  2282. '/label',
  2283. '/div',
  2284. '/div'
  2285. );
  2286. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2287. $this->assertTags($result, $expected);
  2288. }
  2289. /**
  2290. * test input name with leading integer, ensure attributes are generated correctly.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputWithLeadingInteger() {
  2295. $result = $this->Form->text('0.Node.title');
  2296. $expected = array(
  2297. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2298. );
  2299. $this->assertTags($result, $expected);
  2300. }
  2301. /**
  2302. * test form->input() with select type inputs.
  2303. *
  2304. * @return void
  2305. */
  2306. public function testInputSelectType() {
  2307. $result = $this->Form->input('email', array(
  2308. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2309. );
  2310. $expected = array(
  2311. 'div' => array('class' => 'input select'),
  2312. 'label' => array('for' => 'email'),
  2313. 'Email',
  2314. '/label',
  2315. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2316. array('option' => array('value' => '')),
  2317. '/option',
  2318. array('option' => array('value' => 'è')),
  2319. 'Firést',
  2320. '/option',
  2321. array('option' => array('value' => 'é')),
  2322. 'Secoènd',
  2323. '/option',
  2324. '/select',
  2325. '/div'
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. $result = $this->Form->input('email', array(
  2329. 'options' => array('First', 'Second'), 'empty' => true)
  2330. );
  2331. $expected = array(
  2332. 'div' => array('class' => 'input select'),
  2333. 'label' => array('for' => 'email'),
  2334. 'Email',
  2335. '/label',
  2336. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2337. array('option' => array('value' => '')),
  2338. '/option',
  2339. array('option' => array('value' => '0')),
  2340. 'First',
  2341. '/option',
  2342. array('option' => array('value' => '1')),
  2343. 'Second',
  2344. '/option',
  2345. '/select',
  2346. '/div'
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2350. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2351. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2352. $expected = array(
  2353. 'div' => array('class' => 'input select'),
  2354. 'label' => array('for' => 'ModelUserId'),
  2355. 'User',
  2356. '/label',
  2357. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2358. array('option' => array('value' => '')),
  2359. '/option',
  2360. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2361. 'good',
  2362. '/option',
  2363. array('option' => array('value' => 'other')),
  2364. 'bad',
  2365. '/option',
  2366. '/select',
  2367. '/div'
  2368. );
  2369. $this->assertTags($result, $expected);
  2370. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2371. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2372. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2373. $expected = array(
  2374. 'div' => array('class' => 'input select'),
  2375. 'label' => array('for' => 'ThingUserId'),
  2376. 'User',
  2377. '/label',
  2378. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2379. array('option' => array('value' => '')),
  2380. 'Some Empty',
  2381. '/option',
  2382. array('option' => array('value' => 'value')),
  2383. 'good',
  2384. '/option',
  2385. array('option' => array('value' => 'other')),
  2386. 'bad',
  2387. '/option',
  2388. '/select',
  2389. '/div'
  2390. );
  2391. $this->assertTags($result, $expected);
  2392. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2393. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2394. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2395. $expected = array(
  2396. 'div' => array('class' => 'input select'),
  2397. 'label' => array('for' => 'ThingUserId'),
  2398. 'User',
  2399. '/label',
  2400. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2401. array('option' => array('value' => '')),
  2402. 'Some Empty',
  2403. '/option',
  2404. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2405. 'good',
  2406. '/option',
  2407. array('option' => array('value' => 'other')),
  2408. 'bad',
  2409. '/option',
  2410. '/select',
  2411. '/div'
  2412. );
  2413. $this->assertTags($result, $expected);
  2414. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2415. $this->Form->request->data = array('User' => array('User' => array('value')));
  2416. $result = $this->Form->input('User.User', array('empty' => true));
  2417. $expected = array(
  2418. 'div' => array('class' => 'input select'),
  2419. 'label' => array('for' => 'UserUser'),
  2420. 'User',
  2421. '/label',
  2422. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2423. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2424. array('option' => array('value' => '')),
  2425. '/option',
  2426. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2427. 'good',
  2428. '/option',
  2429. array('option' => array('value' => 'other')),
  2430. 'bad',
  2431. '/option',
  2432. '/select',
  2433. '/div'
  2434. );
  2435. $this->assertTags($result, $expected);
  2436. $this->Form->data = array();
  2437. $result = $this->Form->input('Publisher.id', array(
  2438. 'label' => 'Publisher',
  2439. 'type' => 'select',
  2440. 'multiple' => 'checkbox',
  2441. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2442. ));
  2443. $expected = array(
  2444. array('div' => array('class' => 'input select')),
  2445. array('label' => array('for' => 'PublisherId')),
  2446. 'Publisher',
  2447. '/label',
  2448. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2449. array('div' => array('class' => 'checkbox')),
  2450. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2451. array('label' => array('for' => 'PublisherIdValue1')),
  2452. 'Label 1',
  2453. '/label',
  2454. '/div',
  2455. array('div' => array('class' => 'checkbox')),
  2456. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2457. array('label' => array('for' => 'PublisherIdValue2')),
  2458. 'Label 2',
  2459. '/label',
  2460. '/div',
  2461. '/div'
  2462. );
  2463. $this->assertTags($result, $expected);
  2464. }
  2465. /**
  2466. * test that input() and a non standard primary key makes a hidden input by default.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2471. $this->Form->create('User');
  2472. $this->Form->fieldset = array(
  2473. 'User' => array(
  2474. 'fields' => array(
  2475. 'model_id' => array('type' => 'integer')
  2476. ),
  2477. 'validates' => array(),
  2478. 'key' => 'model_id'
  2479. )
  2480. );
  2481. $result = $this->Form->input('model_id');
  2482. $expected = array(
  2483. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2484. );
  2485. $this->assertTags($result, $expected);
  2486. }
  2487. /**
  2488. * test that overriding the magic select type widget is possible
  2489. *
  2490. * @return void
  2491. */
  2492. public function testInputOverridingMagicSelectType() {
  2493. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2494. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2495. $expected = array(
  2496. 'div' => array('class' => 'input text'),
  2497. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2498. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2499. '/div'
  2500. );
  2501. $this->assertTags($result, $expected);
  2502. //Check that magic types still work for plural/singular vars
  2503. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2504. $result = $this->Form->input('Model.type');
  2505. $expected = array(
  2506. 'div' => array('class' => 'input select'),
  2507. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2508. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2509. array('option' => array('value' => 'value')), 'good', '/option',
  2510. array('option' => array('value' => 'other')), 'bad', '/option',
  2511. '/select',
  2512. '/div'
  2513. );
  2514. $this->assertTags($result, $expected);
  2515. }
  2516. /**
  2517. * Test that magic input() selects are created for type=number
  2518. *
  2519. * @return void
  2520. */
  2521. public function testInputMagicSelectForTypeNumber() {
  2522. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2523. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2524. $result = $this->Form->input('ValidateUser.balance');
  2525. $expected = array(
  2526. 'div' => array('class' => 'input select'),
  2527. 'label' => array('for' => 'ValidateUserBalance'),
  2528. 'Balance',
  2529. '/label',
  2530. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2531. array('option' => array('value' => '0')),
  2532. 'nothing',
  2533. '/option',
  2534. array('option' => array('value' => '1', 'selected' => 'selected')),
  2535. 'some',
  2536. '/option',
  2537. array('option' => array('value' => '100')),
  2538. 'a lot',
  2539. '/option',
  2540. '/select',
  2541. '/div'
  2542. );
  2543. $this->assertTags($result, $expected);
  2544. }
  2545. /**
  2546. * Test that magic input() selects can easily be converted into radio types without error.
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputMagicSelectChangeToRadio() {
  2551. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2552. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2553. $this->assertRegExp('/input type="radio"/', $result);
  2554. }
  2555. /**
  2556. * fields with the same name as the model should work.
  2557. *
  2558. * @return void
  2559. */
  2560. public function testInputWithMatchingFieldAndModelName() {
  2561. $this->Form->create('User');
  2562. $this->Form->fieldset = array(
  2563. 'User' => array(
  2564. 'fields' => array(
  2565. 'User' => array('type' => 'text')
  2566. ),
  2567. 'validates' => array(),
  2568. 'key' => 'id'
  2569. )
  2570. );
  2571. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2572. $result = $this->Form->input('User', array('type' => 'text'));
  2573. $expected = array(
  2574. 'div' => array('class' => 'input text'),
  2575. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2576. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2577. '/div'
  2578. );
  2579. $this->assertTags($result, $expected);
  2580. }
  2581. /**
  2582. * testFormInputs method
  2583. *
  2584. * test correct results from form::inputs().
  2585. *
  2586. * @return void
  2587. */
  2588. public function testFormInputs() {
  2589. $this->Form->create('Contact');
  2590. $result = $this->Form->inputs('The Legend');
  2591. $expected = array(
  2592. '<fieldset',
  2593. '<legend',
  2594. 'The Legend',
  2595. '/legend',
  2596. '*/fieldset',
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2600. $expected = array(
  2601. 'fieldset' => array('class' => 'classy-stuff'),
  2602. '<legend',
  2603. 'Field of Dreams',
  2604. '/legend',
  2605. '*/fieldset'
  2606. );
  2607. $this->assertTags($result, $expected);
  2608. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2609. $this->assertTags($result, $expected);
  2610. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2611. $this->assertTags($result, $expected);
  2612. $this->Form->create('Contact');
  2613. $this->Form->request['prefix'] = 'admin';
  2614. $this->Form->request['action'] = 'admin_edit';
  2615. $result = $this->Form->inputs();
  2616. $expected = array(
  2617. '<fieldset',
  2618. '<legend',
  2619. 'Edit Contact',
  2620. '/legend',
  2621. '*/fieldset',
  2622. );
  2623. $this->assertTags($result, $expected);
  2624. $this->Form->create('Contact');
  2625. $result = $this->Form->inputs(false);
  2626. $expected = array(
  2627. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2628. array('div' => array('class' => 'input text')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input text')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input text')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input password')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input date')),
  2637. '*/div',
  2638. array('div' => array('class' => 'input date')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input datetime')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input number')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input select')),
  2645. '*/div',
  2646. );
  2647. $this->assertTags($result, $expected);
  2648. $this->Form->create('Contact');
  2649. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2650. $expected = array(
  2651. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2652. array('div' => array('class' => 'input text')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input text')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input text')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input password')),
  2659. '*/div',
  2660. array('div' => array('class' => 'input date')),
  2661. '*/div',
  2662. array('div' => array('class' => 'input date')),
  2663. '*/div',
  2664. array('div' => array('class' => 'input datetime')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input number')),
  2667. '*/div',
  2668. array('div' => array('class' => 'input select')),
  2669. '*/div',
  2670. );
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2674. $this->assertTags($result, $expected);
  2675. $this->Form->create('Contact');
  2676. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2677. $expected = array(
  2678. 'fieldset' => array(),
  2679. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2680. array('div' => array('class' => 'input text')),
  2681. '*/div',
  2682. array('div' => array('class' => 'input text')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input text')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input password')),
  2687. '*/div',
  2688. array('div' => array('class' => 'input date')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input date')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input datetime')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input number')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input select')),
  2697. '*/div',
  2698. '/fieldset'
  2699. );
  2700. $this->assertTags($result, $expected);
  2701. $this->Form->create('Contact');
  2702. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2703. $expected = array(
  2704. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2705. array('div' => array('class' => 'input text')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input text')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input text')),
  2710. '*/div',
  2711. array('div' => array('class' => 'input password')),
  2712. '*/div',
  2713. array('div' => array('class' => 'input date')),
  2714. '*/div',
  2715. array('div' => array('class' => 'input date')),
  2716. '*/div',
  2717. array('div' => array('class' => 'input datetime')),
  2718. '*/div',
  2719. array('div' => array('class' => 'input number')),
  2720. '*/div',
  2721. array('div' => array('class' => 'input select')),
  2722. '*/div',
  2723. );
  2724. $this->assertTags($result, $expected);
  2725. $this->Form->create('Contact');
  2726. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2727. $this->assertTags($result, $expected);
  2728. $this->Form->create('Contact');
  2729. $result = $this->Form->inputs('Hello');
  2730. $expected = array(
  2731. 'fieldset' => array(),
  2732. 'legend' => array(),
  2733. 'Hello',
  2734. '/legend',
  2735. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2736. array('div' => array('class' => 'input text')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input text')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input password')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input date')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input date')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input datetime')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input number')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input select')),
  2753. '*/div',
  2754. '/fieldset'
  2755. );
  2756. $this->assertTags($result, $expected);
  2757. $this->Form->create('Contact');
  2758. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2759. $expected = array(
  2760. 'fieldset' => array(),
  2761. 'legend' => array(),
  2762. 'Hello',
  2763. '/legend',
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input text')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input text')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. '/fieldset'
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2788. $this->assertTags($result, $expected);
  2789. $this->Form->end();
  2790. $this->Form->create(false);
  2791. $expected = array(
  2792. 'fieldset' => array(),
  2793. array('div' => array('class' => 'input text')),
  2794. 'label' => array('for' => 'foo'),
  2795. 'Foo',
  2796. '/label',
  2797. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2798. '*/div',
  2799. '/fieldset'
  2800. );
  2801. $result = $this->Form->inputs(
  2802. array('foo' => array('type' => 'text')),
  2803. array(),
  2804. array('legend' => false)
  2805. );
  2806. $this->assertTags($result, $expected);
  2807. }
  2808. /**
  2809. * testSelectAsCheckbox method
  2810. *
  2811. * test multi-select widget with checkbox formatting.
  2812. *
  2813. * @return void
  2814. */
  2815. public function testSelectAsCheckbox() {
  2816. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2817. $expected = array(
  2818. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2819. array('div' => array('class' => 'checkbox')),
  2820. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2821. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2822. 'first',
  2823. '/label',
  2824. '/div',
  2825. array('div' => array('class' => 'checkbox')),
  2826. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2827. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2828. 'second',
  2829. '/label',
  2830. '/div',
  2831. array('div' => array('class' => 'checkbox')),
  2832. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2833. array('label' => array('for' => 'ModelMultiField2')),
  2834. 'third',
  2835. '/label',
  2836. '/div',
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2840. $expected = array(
  2841. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2842. array('div' => array('class' => 'checkbox')),
  2843. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2844. array('label' => array('for' => 'ModelMultiField12')),
  2845. 'half',
  2846. '/label',
  2847. '/div',
  2848. );
  2849. $this->assertTags($result, $expected);
  2850. }
  2851. /**
  2852. * testLabel method
  2853. *
  2854. * test label generation.
  2855. *
  2856. * @return void
  2857. */
  2858. public function testLabel() {
  2859. $this->Form->text('Person.name');
  2860. $result = $this->Form->label();
  2861. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2862. $this->Form->text('Person.name');
  2863. $result = $this->Form->label();
  2864. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2865. $result = $this->Form->label('Person.first_name');
  2866. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2867. $result = $this->Form->label('Person.first_name', 'Your first name');
  2868. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2869. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2870. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2871. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2872. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2873. $result = $this->Form->label('Person.first_name', '');
  2874. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2875. $result = $this->Form->label('Person.2.name', '');
  2876. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2877. }
  2878. /**
  2879. * testTextbox method
  2880. *
  2881. * test textbox element generation
  2882. *
  2883. * @return void
  2884. */
  2885. public function testTextbox() {
  2886. $result = $this->Form->text('Model.field');
  2887. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2888. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2889. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2890. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2891. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2892. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2893. $result = $this->Form->text('Model.text');
  2894. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2895. $Contact = ClassRegistry::getObject('Contact');
  2896. $Contact->validationErrors['text'] = array(true);
  2897. $this->Form->request->data['Contact']['text'] = 'test';
  2898. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2899. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2900. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2901. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2902. $expected = array(
  2903. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2904. );
  2905. $this->assertTags($result, $expected);
  2906. }
  2907. /**
  2908. * testDefaultValue method
  2909. *
  2910. * Test default value setting
  2911. *
  2912. * @return void
  2913. */
  2914. public function testDefaultValue() {
  2915. $this->Form->request->data['Model']['field'] = 'test';
  2916. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2917. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2918. unset($this->Form->request->data['Model']['field']);
  2919. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2920. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2921. }
  2922. /**
  2923. * testCheckboxDefaultValue method
  2924. *
  2925. * Test default value setting on checkbox() method
  2926. *
  2927. * @return void
  2928. */
  2929. public function testCheckboxDefaultValue() {
  2930. $this->Form->request->data['Model']['field'] = false;
  2931. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2932. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2933. unset($this->Form->request->data['Model']['field']);
  2934. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2935. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2936. $this->Form->request->data['Model']['field'] = true;
  2937. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2938. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2939. unset($this->Form->request->data['Model']['field']);
  2940. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2941. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2942. }
  2943. /**
  2944. * testError method
  2945. *
  2946. * Test field error generation
  2947. *
  2948. * @return void
  2949. */
  2950. public function testError() {
  2951. $Contact = ClassRegistry::getObject('Contact');
  2952. $Contact->validationErrors['field'] = array(1);
  2953. $result = $this->Form->error('Contact.field');
  2954. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2955. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2956. $this->assertEquals('Error in field Field', $result);
  2957. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2958. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2959. $this->assertEquals('This field contains invalid input', $result);
  2960. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2961. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2962. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2963. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2964. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2965. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2966. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2967. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2968. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2969. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2970. $this->assertEquals('<strong>Badness!</strong>', $result);
  2971. $Contact->validationErrors['field'] = array("email");
  2972. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2973. $expected = array(
  2974. 'div' => array('class' => 'field-error'),
  2975. 'No good!',
  2976. '/div'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2980. $result = $this->Form->error('Contact.field', array(
  2981. 'notEmpty' => 'Cannot be empty',
  2982. 'email' => 'No good!'
  2983. ));
  2984. $expected = array(
  2985. 'div' => array('class' => 'error-message'),
  2986. 'ul' => array(),
  2987. '<li', 'Cannot be empty', '/li',
  2988. '<li', 'No good!', '/li',
  2989. '<li', 'Something else', '/li',
  2990. '/ul',
  2991. '/div'
  2992. );
  2993. $this->assertTags($result, $expected);
  2994. /** Testing error messages list options **/
  2995. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2996. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2997. $expected = array(
  2998. 'div' => array('class' => 'error-message'),
  2999. 'ol' => array(),
  3000. '<li', 'notEmpty', '/li',
  3001. '<li', 'email', '/li',
  3002. '/ol',
  3003. '/div'
  3004. );
  3005. $this->assertTags($result, $expected);
  3006. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3007. $expected = array(
  3008. 'div' => array('class' => 'error-message'),
  3009. 'ol' => array(),
  3010. '<li', 'notEmpty', '/li',
  3011. '<li', 'email', '/li',
  3012. '/ol',
  3013. '/div'
  3014. );
  3015. $this->assertTags($result, $expected);
  3016. $result = $this->Form->error('Contact.field', null, array(
  3017. 'listOptions' => array(
  3018. 'class' => 'ul-class',
  3019. 'itemOptions' => array(
  3020. 'class' => 'li-class'
  3021. )
  3022. )
  3023. ));
  3024. $expected = array(
  3025. 'div' => array('class' => 'error-message'),
  3026. 'ul' => array('class' => 'ul-class'),
  3027. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3028. array('li' => array('class' => 'li-class')), 'email', '/li',
  3029. '/ul',
  3030. '/div'
  3031. );
  3032. $this->assertTags($result, $expected);
  3033. }
  3034. /**
  3035. * test error options when using form->input();
  3036. *
  3037. * @return void
  3038. */
  3039. public function testInputErrorEscape() {
  3040. $this->Form->create('ValidateProfile');
  3041. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3042. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3043. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3044. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3045. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3046. $this->assertRegExp('/required<br>/', $result);
  3047. }
  3048. /**
  3049. * testPassword method
  3050. *
  3051. * Test password element generation
  3052. *
  3053. * @return void
  3054. */
  3055. public function testPassword() {
  3056. $Contact = ClassRegistry::getObject('Contact');
  3057. $result = $this->Form->password('Contact.field');
  3058. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3059. $Contact->validationErrors['passwd'] = 1;
  3060. $this->Form->request->data['Contact']['passwd'] = 'test';
  3061. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3062. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3063. }
  3064. /**
  3065. * testRadio method
  3066. *
  3067. * Test radio element set generation
  3068. *
  3069. * @return void
  3070. */
  3071. public function testRadio() {
  3072. $result = $this->Form->radio('Model.field', array('option A'));
  3073. $expected = array(
  3074. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3075. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3076. 'label' => array('for' => 'ModelField0'),
  3077. 'option A',
  3078. '/label'
  3079. );
  3080. $this->assertTags($result, $expected);
  3081. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3082. $expected = array(
  3083. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3084. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3085. 'label' => array('for' => 'ModelField12'),
  3086. 'half',
  3087. '/label'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3091. $expected = array(
  3092. 'fieldset' => array(),
  3093. 'legend' => array(),
  3094. 'Field',
  3095. '/legend',
  3096. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3098. array('label' => array('for' => 'ModelField0')),
  3099. 'option A',
  3100. '/label',
  3101. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3102. array('label' => array('for' => 'ModelField1')),
  3103. 'option B',
  3104. '/label',
  3105. '/fieldset'
  3106. );
  3107. $this->assertTags($result, $expected);
  3108. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3109. $expected = array(
  3110. 'fieldset' => array(),
  3111. 'legend' => array(),
  3112. 'Field',
  3113. '/legend',
  3114. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3115. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3116. array('label' => array('for' => 'ModelField0')),
  3117. 'option A',
  3118. '/label',
  3119. 'br' => array(),
  3120. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3121. array('label' => array('for' => 'ModelField1')),
  3122. 'option B',
  3123. '/label',
  3124. '/fieldset'
  3125. );
  3126. $this->assertTags($result, $expected);
  3127. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3128. $expected = array(
  3129. 'fieldset' => array(),
  3130. 'legend' => array(),
  3131. 'Gender',
  3132. '/legend',
  3133. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3134. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3135. array('label' => array('for' => 'EmployeeGenderMale')),
  3136. 'Male',
  3137. '/label',
  3138. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3139. array('label' => array('for' => 'EmployeeGenderFemale')),
  3140. 'Female',
  3141. '/label',
  3142. '/fieldset',
  3143. );
  3144. $this->assertTags($result, $expected);
  3145. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3146. $expected = array(
  3147. 'fieldset' => array(),
  3148. 'legend' => array(),
  3149. 'Gender',
  3150. '/legend',
  3151. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3152. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3153. array('label' => array('for' => 'OfficerGenderMale')),
  3154. 'Male',
  3155. '/label',
  3156. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3157. array('label' => array('for' => 'OfficerGenderFemale')),
  3158. 'Female',
  3159. '/label',
  3160. '/fieldset',
  3161. );
  3162. $this->assertTags($result, $expected);
  3163. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3164. $expected = array(
  3165. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3166. array('input' => array(
  3167. 'type' => 'radio',
  3168. 'name' => 'data[Contact][1][imrequired]',
  3169. 'value' => '0',
  3170. 'id' => 'Contact1Imrequired0',
  3171. 'required' => 'required'
  3172. )),
  3173. 'label' => array('for' => 'Contact1Imrequired0'),
  3174. 'option A',
  3175. '/label'
  3176. );
  3177. $this->assertTags($result, $expected);
  3178. $result = $this->Form->radio('Model.1.field', array('option A'));
  3179. $expected = array(
  3180. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3182. 'label' => array('for' => 'Model1Field0'),
  3183. 'option A',
  3184. '/label'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3194. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3195. array('label' => array('for' => 'ModelField0')),
  3196. 'option A',
  3197. '/label',
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3199. array('label' => array('for' => 'ModelField1')),
  3200. 'option B',
  3201. '/label',
  3202. '/fieldset'
  3203. );
  3204. $this->assertTags($result, $expected);
  3205. $result = $this->Form->radio(
  3206. 'Model.field',
  3207. array('option A', 'option B'),
  3208. array('between' => 'I am between')
  3209. );
  3210. $expected = array(
  3211. 'fieldset' => array(),
  3212. 'legend' => array(),
  3213. 'Field',
  3214. '/legend',
  3215. 'I am between',
  3216. 'input' => array(
  3217. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3218. 'value' => '', 'id' => 'ModelField_'
  3219. ),
  3220. array('input' => array(
  3221. 'type' => 'radio', 'name' => 'data[Model][field]',
  3222. 'value' => '0', 'id' => 'ModelField0'
  3223. )),
  3224. array('label' => array('for' => 'ModelField0')),
  3225. 'option A',
  3226. '/label',
  3227. array('input' => array(
  3228. 'type' => 'radio', 'name' => 'data[Model][field]',
  3229. 'value' => '1', 'id' => 'ModelField1'
  3230. )),
  3231. array('label' => array('for' => 'ModelField1')),
  3232. 'option B',
  3233. '/label',
  3234. '/fieldset'
  3235. );
  3236. $this->assertTags($result, $expected);
  3237. }
  3238. /**
  3239. * Test that radios with a 0 value are selected under the correct conditions.
  3240. * Also ensure that values that are booleanish are handled correctly.
  3241. *
  3242. * @return void
  3243. */
  3244. public function testRadioOptionWithBooleanishValues() {
  3245. $expected = array(
  3246. 'fieldset' => array(),
  3247. 'legend' => array(),
  3248. 'Field',
  3249. '/legend',
  3250. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3251. array('label' => array('for' => 'ModelField1')),
  3252. 'Yes',
  3253. '/label',
  3254. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3255. array('label' => array('for' => 'ModelField0')),
  3256. 'No',
  3257. '/label',
  3258. '/fieldset'
  3259. );
  3260. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3265. $this->assertTags($result, $expected);
  3266. $expected = array(
  3267. 'fieldset' => array(),
  3268. 'legend' => array(),
  3269. 'Field',
  3270. '/legend',
  3271. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3272. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3273. array('label' => array('for' => 'ModelField1')),
  3274. 'Yes',
  3275. '/label',
  3276. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3277. array('label' => array('for' => 'ModelField0')),
  3278. 'No',
  3279. '/label',
  3280. '/fieldset'
  3281. );
  3282. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3283. $this->assertTags($result, $expected);
  3284. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3285. $this->assertTags($result, $expected);
  3286. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3287. $this->assertTags($result, $expected);
  3288. $expected = array(
  3289. 'fieldset' => array(),
  3290. 'legend' => array(),
  3291. 'Field',
  3292. '/legend',
  3293. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3294. array('label' => array('for' => 'ModelField1')),
  3295. 'Yes',
  3296. '/label',
  3297. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3298. array('label' => array('for' => 'ModelField0')),
  3299. 'No',
  3300. '/label',
  3301. '/fieldset'
  3302. );
  3303. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3304. $this->assertTags($result, $expected);
  3305. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3306. $this->assertTags($result, $expected);
  3307. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3308. $this->assertTags($result, $expected);
  3309. }
  3310. /**
  3311. * test disabled radio options
  3312. *
  3313. * @return void
  3314. */
  3315. public function testRadioDisabled() {
  3316. $result = $this->Form->radio(
  3317. 'Model.field',
  3318. array('option A', 'option B'),
  3319. array('disabled' => array('option A'), 'value' => '0')
  3320. );
  3321. $expected = array(
  3322. 'fieldset' => array(),
  3323. 'legend' => array(),
  3324. 'Field',
  3325. '/legend',
  3326. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3327. array('label' => array('for' => 'ModelField0')),
  3328. 'option A',
  3329. '/label',
  3330. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3331. array('label' => array('for' => 'ModelField1')),
  3332. 'option B',
  3333. '/label',
  3334. '/fieldset'
  3335. );
  3336. $this->assertTags($result, $expected);
  3337. $result = $this->Form->radio(
  3338. 'Model.field',
  3339. array('option A', 'option B'),
  3340. array('disabled' => true, 'value' => '0')
  3341. );
  3342. $expected = array(
  3343. 'fieldset' => array(),
  3344. 'legend' => array(),
  3345. 'Field',
  3346. '/legend',
  3347. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3348. array('label' => array('for' => 'ModelField0')),
  3349. 'option A',
  3350. '/label',
  3351. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3352. array('label' => array('for' => 'ModelField1')),
  3353. 'option B',
  3354. '/label',
  3355. '/fieldset'
  3356. );
  3357. $this->assertTags($result, $expected);
  3358. $result = $this->Form->radio(
  3359. 'Model.field',
  3360. array('option A', 'option B'),
  3361. array('disabled' => 'disabled', 'value' => '0')
  3362. );
  3363. $expected = array(
  3364. 'fieldset' => array(),
  3365. 'legend' => array(),
  3366. 'Field',
  3367. '/legend',
  3368. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3369. array('label' => array('for' => 'ModelField0')),
  3370. 'option A',
  3371. '/label',
  3372. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3373. array('label' => array('for' => 'ModelField1')),
  3374. 'option B',
  3375. '/label',
  3376. '/fieldset'
  3377. );
  3378. $this->assertTags($result, $expected);
  3379. }
  3380. /**
  3381. * test disabling the hidden input for radio buttons
  3382. *
  3383. * @return void
  3384. */
  3385. public function testRadioHiddenInputDisabling() {
  3386. $result = $this->Form->input('Model.1.field', array(
  3387. 'type' => 'radio',
  3388. 'options' => array('option A'),
  3389. 'hiddenField' => false
  3390. )
  3391. );
  3392. $expected = array(
  3393. 'div' => array('class' => 'input radio'),
  3394. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3395. 'label' => array('for' => 'Model1Field0'),
  3396. 'option A',
  3397. '/label',
  3398. '/div'
  3399. );
  3400. $this->assertTags($result, $expected);
  3401. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3402. $expected = array(
  3403. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3404. 'label' => array('for' => 'Model1Field0'),
  3405. 'option A',
  3406. '/label'
  3407. );
  3408. $this->assertTags($result, $expected);
  3409. }
  3410. /**
  3411. * test adding an empty option for radio buttons
  3412. *
  3413. * @return void
  3414. */
  3415. public function testRadioAddEmptyOption() {
  3416. $result = $this->Form->input('Model.1.field', array(
  3417. 'type' => 'radio',
  3418. 'options' => array('option A'),
  3419. 'empty' => true,
  3420. 'hiddenField' => false
  3421. ));
  3422. $expected = array(
  3423. 'div' => array('class' => 'input radio'),
  3424. 'fieldset' => array(),
  3425. 'legend' => array(),
  3426. 'Field',
  3427. '/legend',
  3428. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3429. array('label' => array('for' => 'Model1Field')),
  3430. __('empty'),
  3431. '/label',
  3432. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3433. array('label' => array('for' => 'Model1Field0')),
  3434. 'option A',
  3435. '/label',
  3436. '/fieldset',
  3437. '/div'
  3438. );
  3439. $this->assertTags($result, $expected);
  3440. $result = $this->Form->input('Model.1.field', array(
  3441. 'type' => 'radio',
  3442. 'options' => array('option A'),
  3443. 'empty' => 'CustomEmptyLabel',
  3444. 'hiddenField' => false
  3445. ));
  3446. $expected = array(
  3447. 'div' => array('class' => 'input radio'),
  3448. 'fieldset' => array(),
  3449. 'legend' => array(),
  3450. 'Field',
  3451. '/legend',
  3452. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3453. array('label' => array('for' => 'Model1Field')),
  3454. 'CustomEmptyLabel',
  3455. '/label',
  3456. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3457. array('label' => array('for' => 'Model1Field0')),
  3458. 'option A',
  3459. '/label',
  3460. '/fieldset',
  3461. '/div'
  3462. );
  3463. $this->assertTags($result, $expected);
  3464. $result = $this->Form->input('Model.1.field', array(
  3465. 'type' => 'radio',
  3466. 'options' => array('option A'),
  3467. 'empty' => false,
  3468. 'hiddenField' => false
  3469. ));
  3470. $this->assertTextNotContains('"Model1Field"', $result);
  3471. }
  3472. /**
  3473. * testSelect method
  3474. *
  3475. * Test select element generation.
  3476. *
  3477. * @return void
  3478. */
  3479. public function testSelect() {
  3480. $result = $this->Form->select('Model.field', array());
  3481. $expected = array(
  3482. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3483. array('option' => array('value' => '')),
  3484. '/option',
  3485. '/select'
  3486. );
  3487. $this->assertTags($result, $expected);
  3488. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3489. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3490. $expected = array(
  3491. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3492. array('option' => array('value' => '')),
  3493. '/option',
  3494. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3495. 'good',
  3496. '/option',
  3497. array('option' => array('value' => 'other')),
  3498. 'bad',
  3499. '/option',
  3500. '/select'
  3501. );
  3502. $this->assertTags($result, $expected);
  3503. $this->Form->request->data = array();
  3504. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3505. $expected = array(
  3506. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3507. array('option' => array('value' => '')),
  3508. '/option',
  3509. array('option' => array('value' => 'value')),
  3510. 'good',
  3511. '/option',
  3512. array('option' => array('value' => 'other')),
  3513. 'bad',
  3514. '/option',
  3515. '/select'
  3516. );
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->select(
  3519. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3520. array('empty' => false)
  3521. );
  3522. $expected = array(
  3523. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3524. array('option' => array('value' => 'first')),
  3525. 'first &quot;html&quot; &lt;chars&gt;',
  3526. '/option',
  3527. array('option' => array('value' => 'second')),
  3528. 'value',
  3529. '/option',
  3530. '/select'
  3531. );
  3532. $this->assertTags($result, $expected);
  3533. $result = $this->Form->select(
  3534. 'Model.field',
  3535. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3536. array('escape' => false, 'empty' => false)
  3537. );
  3538. $expected = array(
  3539. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3540. array('option' => array('value' => 'first')),
  3541. 'first "html" <chars>',
  3542. '/option',
  3543. array('option' => array('value' => 'second')),
  3544. 'value',
  3545. '/option',
  3546. '/select'
  3547. );
  3548. $this->assertTags($result, $expected);
  3549. $options = array(
  3550. array('value' => 'first', 'name' => 'First'),
  3551. array('value' => 'first', 'name' => 'Another First'),
  3552. );
  3553. $result = $this->Form->select(
  3554. 'Model.field',
  3555. $options,
  3556. array('escape' => false, 'empty' => false)
  3557. );
  3558. $expected = array(
  3559. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3560. array('option' => array('value' => 'first')),
  3561. 'First',
  3562. '/option',
  3563. array('option' => array('value' => 'first')),
  3564. 'Another First',
  3565. '/option',
  3566. '/select'
  3567. );
  3568. $this->assertTags($result, $expected);
  3569. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3570. $result = $this->Form->select(
  3571. 'Model.contact_id',
  3572. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3573. array('escape' => false, 'empty' => 'pick something')
  3574. );
  3575. $expected = array(
  3576. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3577. array('option' => array('value' => '')), 'pick something', '/option',
  3578. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3579. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3580. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3581. '/select'
  3582. );
  3583. $this->assertTags($result, $expected);
  3584. $this->Form->request->data['Model']['field'] = 0;
  3585. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3586. $expected = array(
  3587. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3588. array('option' => array('value' => '')), '/option',
  3589. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3590. array('option' => array('value' => '1')), 'Yes', '/option',
  3591. '/select'
  3592. );
  3593. $this->assertTags($result, $expected);
  3594. }
  3595. /**
  3596. * test that select() with optiongroups listens to the escape param.
  3597. *
  3598. * @return void
  3599. */
  3600. public function testSelectOptionGroupEscaping() {
  3601. $options = array(
  3602. '>< Key' => array(
  3603. 1 => 'One',
  3604. 2 => 'Two'
  3605. )
  3606. );
  3607. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3608. $expected = array(
  3609. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3610. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3611. array('option' => array('value' => '1')), 'One', '/option',
  3612. array('option' => array('value' => '2')), 'Two', '/option',
  3613. '/optgroup',
  3614. '/select'
  3615. );
  3616. $this->assertTags($result, $expected);
  3617. $options = array(
  3618. '>< Key' => array(
  3619. 1 => 'One',
  3620. 2 => 'Two'
  3621. )
  3622. );
  3623. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3624. $expected = array(
  3625. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3626. 'optgroup' => array('label' => '>< Key'),
  3627. array('option' => array('value' => '1')), 'One', '/option',
  3628. array('option' => array('value' => '2')), 'Two', '/option',
  3629. '/optgroup',
  3630. '/select'
  3631. );
  3632. $this->assertTags($result, $expected);
  3633. }
  3634. /**
  3635. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3636. *
  3637. * @return void
  3638. */
  3639. public function testSelectWithNullAttributes() {
  3640. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3641. $expected = array(
  3642. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3643. array('option' => array('value' => '0')),
  3644. 'first',
  3645. '/option',
  3646. array('option' => array('value' => '1')),
  3647. 'second',
  3648. '/option',
  3649. '/select'
  3650. );
  3651. $this->assertTags($result, $expected);
  3652. }
  3653. /**
  3654. * testNestedSelect method
  3655. *
  3656. * test select element generation with optgroups
  3657. *
  3658. * @return void
  3659. */
  3660. public function testNestedSelect() {
  3661. $result = $this->Form->select(
  3662. 'Model.field',
  3663. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3664. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3665. )), array('empty' => false)
  3666. );
  3667. $expected = array(
  3668. 'select' => array('name' => 'data[Model][field]',
  3669. 'id' => 'ModelField'),
  3670. array('option' => array('value' => 1)),
  3671. 'One',
  3672. '/option',
  3673. array('option' => array('value' => 2)),
  3674. 'Two',
  3675. '/option',
  3676. array('optgroup' => array('label' => 'Three')),
  3677. array('option' => array('value' => 4)),
  3678. 'Four',
  3679. '/option',
  3680. array('option' => array('value' => 5)),
  3681. 'Five',
  3682. '/option',
  3683. '/optgroup',
  3684. '/select'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. $result = $this->Form->select(
  3688. 'Model.field',
  3689. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3690. array('showParents' => true, 'empty' => false)
  3691. );
  3692. $expected = array(
  3693. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3694. array('option' => array('value' => 1)),
  3695. 'One',
  3696. '/option',
  3697. array('option' => array('value' => 2)),
  3698. 'Two',
  3699. '/option',
  3700. array('optgroup' => array('label' => 'Three')),
  3701. array('option' => array('value' => 3)),
  3702. 'Three',
  3703. '/option',
  3704. array('option' => array('value' => 4)),
  3705. 'Four',
  3706. '/option',
  3707. '/optgroup',
  3708. '/select'
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. }
  3712. /**
  3713. * testSelectMultiple method
  3714. *
  3715. * test generation of multiple select elements
  3716. *
  3717. * @return void
  3718. */
  3719. public function testSelectMultiple() {
  3720. $options = array('first', 'second', 'third');
  3721. $result = $this->Form->select(
  3722. 'Model.multi_field', $options, array('multiple' => true)
  3723. );
  3724. $expected = array(
  3725. 'input' => array(
  3726. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3727. ),
  3728. 'select' => array(
  3729. 'name' => 'data[Model][multi_field][]',
  3730. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3731. ),
  3732. array('option' => array('value' => '0')),
  3733. 'first',
  3734. '/option',
  3735. array('option' => array('value' => '1')),
  3736. 'second',
  3737. '/option',
  3738. array('option' => array('value' => '2')),
  3739. 'third',
  3740. '/option',
  3741. '/select'
  3742. );
  3743. $this->assertTags($result, $expected);
  3744. $result = $this->Form->select(
  3745. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3746. );
  3747. $expected = array(
  3748. 'input' => array(
  3749. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3750. ),
  3751. 'select' => array(
  3752. 'name' => 'data[Model][multi_field][]',
  3753. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3754. ),
  3755. array('option' => array('value' => '0')),
  3756. 'first',
  3757. '/option',
  3758. array('option' => array('value' => '1')),
  3759. 'second',
  3760. '/option',
  3761. array('option' => array('value' => '2')),
  3762. 'third',
  3763. '/option',
  3764. '/select'
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. $result = $this->Form->select(
  3768. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3769. );
  3770. $expected = array(
  3771. 'input' => array(
  3772. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3773. ),
  3774. 'select' => array(
  3775. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3776. 'multiple' => 'multiple'
  3777. ),
  3778. array('option' => array('value' => '0', 'selected' => 'selected')),
  3779. 'first',
  3780. '/option',
  3781. array('option' => array('value' => '1', 'selected' => 'selected')),
  3782. 'second',
  3783. '/option',
  3784. array('option' => array('value' => '2')),
  3785. 'third',
  3786. '/option',
  3787. '/select'
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. $result = $this->Form->select(
  3791. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3792. );
  3793. $expected = array(
  3794. 'select' => array(
  3795. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3796. ),
  3797. array('option' => array('value' => '0', 'selected' => 'selected')),
  3798. 'first',
  3799. '/option',
  3800. array('option' => array('value' => '1', 'selected' => 'selected')),
  3801. 'second',
  3802. '/option',
  3803. array('option' => array('value' => '2')),
  3804. 'third',
  3805. '/option',
  3806. '/select'
  3807. );
  3808. $this->assertTags($result, $expected);
  3809. }
  3810. /**
  3811. * test generation of habtm select boxes.
  3812. *
  3813. * @return void
  3814. */
  3815. public function testHabtmSelectBox() {
  3816. $this->View->viewVars['contactTags'] = array(
  3817. 1 => 'blue',
  3818. 2 => 'red',
  3819. 3 => 'green'
  3820. );
  3821. $this->Form->request->data = array(
  3822. 'Contact' => array(),
  3823. 'ContactTag' => array(
  3824. array(
  3825. 'id' => 1,
  3826. 'name' => 'blue'
  3827. ),
  3828. array(
  3829. 'id' => 3,
  3830. 'name' => 'green'
  3831. )
  3832. )
  3833. );
  3834. $this->Form->create('Contact');
  3835. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3836. $expected = array(
  3837. 'input' => array(
  3838. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3839. ),
  3840. 'select' => array(
  3841. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3842. 'multiple' => 'multiple'
  3843. ),
  3844. array('option' => array('value' => '1', 'selected' => 'selected')),
  3845. 'blue',
  3846. '/option',
  3847. array('option' => array('value' => '2')),
  3848. 'red',
  3849. '/option',
  3850. array('option' => array('value' => '3', 'selected' => 'selected')),
  3851. 'green',
  3852. '/option',
  3853. '/select'
  3854. );
  3855. $this->assertTags($result, $expected);
  3856. }
  3857. /**
  3858. * test generation of multi select elements in checkbox format
  3859. *
  3860. * @return void
  3861. */
  3862. public function testSelectMultipleCheckboxes() {
  3863. $result = $this->Form->select(
  3864. 'Model.multi_field',
  3865. array('first', 'second', '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' => '0', 'id' => 'ModelMultiField0'
  3876. )),
  3877. array('label' => array('for' => 'ModelMultiField0')),
  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' => '1', 'id' => 'ModelMultiField1'
  3885. )),
  3886. array('label' => array('for' => 'ModelMultiField1')),
  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' => '2', 'id' => 'ModelMultiField2'
  3894. )),
  3895. array('label' => array('for' => 'ModelMultiField2')),
  3896. 'third',
  3897. '/label',
  3898. '/div'
  3899. );
  3900. $this->assertTags($result, $expected);
  3901. $result = $this->Form->select(
  3902. 'Model.multi_field',
  3903. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3904. array('multiple' => 'checkbox')
  3905. );
  3906. $expected = array(
  3907. 'input' => array(
  3908. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3909. ),
  3910. array('div' => array('class' => 'checkbox')),
  3911. array('input' => array(
  3912. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3913. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3914. )),
  3915. array('label' => array('for' => 'ModelMultiFieldA')),
  3916. 'first',
  3917. '/label',
  3918. '/div',
  3919. array('div' => array('class' => 'checkbox')),
  3920. array('input' => array(
  3921. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3922. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3923. )),
  3924. array('label' => array('for' => 'ModelMultiFieldB')),
  3925. 'second',
  3926. '/label',
  3927. '/div',
  3928. array('div' => array('class' => 'checkbox')),
  3929. array('input' => array(
  3930. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3931. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3932. )),
  3933. array('label' => array('for' => 'ModelMultiFieldC')),
  3934. 'third',
  3935. '/label',
  3936. '/div'
  3937. );
  3938. $this->assertTags($result, $expected);
  3939. $result = $this->Form->select(
  3940. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3941. );
  3942. $expected = array(
  3943. 'input' => array(
  3944. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3945. ),
  3946. array('div' => array('class' => 'checkbox')),
  3947. array('input' => array(
  3948. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3949. 'value' => '1', 'id' => 'ModelMultiField1'
  3950. )),
  3951. array('label' => array('for' => 'ModelMultiField1')),
  3952. 'first',
  3953. '/label',
  3954. '/div'
  3955. );
  3956. $this->assertTags($result, $expected);
  3957. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3958. $result = $this->Form->select(
  3959. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3960. );
  3961. $expected = array(
  3962. 'input' => array(
  3963. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3964. ),
  3965. array('div' => array('class' => 'checkbox')),
  3966. array('input' => array(
  3967. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3968. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3969. )),
  3970. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3971. 'first',
  3972. '/label',
  3973. '/div',
  3974. array('div' => array('class' => 'checkbox')),
  3975. array('input' => array(
  3976. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3977. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3978. )),
  3979. array('label' => array('for' => 'ModelTagsArray')),
  3980. 'Array',
  3981. '/label',
  3982. '/div'
  3983. );
  3984. $this->assertTags($result, $expected);
  3985. }
  3986. /**
  3987. * test multiple checkboxes with div styles.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testSelectMultipleCheckboxDiv() {
  3992. $result = $this->Form->select(
  3993. 'Model.tags',
  3994. array('first', 'second'),
  3995. array('multiple' => 'checkbox', 'class' => 'my-class')
  3996. );
  3997. $expected = array(
  3998. 'input' => array(
  3999. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4000. ),
  4001. array('div' => array('class' => 'my-class')),
  4002. array('input' => array(
  4003. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4004. 'value' => '0', 'id' => 'ModelTags0'
  4005. )),
  4006. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4007. '/div',
  4008. array('div' => array('class' => 'my-class')),
  4009. array('input' => array(
  4010. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4011. 'value' => '1', 'id' => 'ModelTags1'
  4012. )),
  4013. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4014. '/div'
  4015. );
  4016. $this->assertTags($result, $expected);
  4017. $result = $this->Form->input('Model.tags', array(
  4018. 'options' => array('first', 'second'),
  4019. 'multiple' => 'checkbox',
  4020. 'class' => 'my-class',
  4021. 'div' => false,
  4022. 'label' => false
  4023. ));
  4024. $this->assertTags($result, $expected);
  4025. $Contact = ClassRegistry::getObject('Contact');
  4026. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4027. $result = $this->Form->input('Contact.tags', array(
  4028. 'options' => array('one'),
  4029. 'multiple' => 'checkbox',
  4030. 'label' => false,
  4031. 'div' => false
  4032. ));
  4033. $expected = array(
  4034. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4035. array('div' => array('class' => 'checkbox form-error')),
  4036. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4037. array('label' => array('for' => 'ContactTags0')),
  4038. 'one',
  4039. '/label',
  4040. '/div'
  4041. );
  4042. $this->assertTags($result, $expected);
  4043. $result = $this->Form->input('Contact.tags', array(
  4044. 'options' => array('one'),
  4045. 'multiple' => 'checkbox',
  4046. 'class' => 'mycheckbox',
  4047. 'label' => false,
  4048. 'div' => false
  4049. ));
  4050. $expected = array(
  4051. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4052. array('div' => array('class' => 'mycheckbox form-error')),
  4053. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4054. array('label' => array('for' => 'ContactTags0')),
  4055. 'one',
  4056. '/label',
  4057. '/div'
  4058. );
  4059. $this->assertTags($result, $expected);
  4060. }
  4061. /**
  4062. * Checks the security hash array generated for multiple-input checkbox elements
  4063. *
  4064. * @return void
  4065. */
  4066. public function testSelectMultipleCheckboxSecurity() {
  4067. $this->Form->request['_Token'] = array('key' => 'testKey');
  4068. $this->assertEquals(array(), $this->Form->fields);
  4069. $result = $this->Form->select(
  4070. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4071. array('multiple' => 'checkbox')
  4072. );
  4073. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4074. $result = $this->Form->secure($this->Form->fields);
  4075. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4076. $this->assertRegExp('/"' . $key . '"/', $result);
  4077. }
  4078. /**
  4079. * Multiple select elements should always be secured as they always participate
  4080. * in the POST data.
  4081. *
  4082. * @return void
  4083. */
  4084. public function testSelectMultipleSecureWithNoOptions() {
  4085. $this->Form->request['_Token'] = array('key' => 'testkey');
  4086. $this->assertEquals(array(), $this->Form->fields);
  4087. $result = $this->Form->select(
  4088. 'Model.select',
  4089. array(),
  4090. array('multiple' => true)
  4091. );
  4092. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4093. }
  4094. /**
  4095. * When a select box has no options it should not be added to the fields list
  4096. * as it always fail post validation.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testSelectNoSecureWithNoOptions() {
  4101. $this->Form->request['_Token'] = array('key' => 'testkey');
  4102. $this->assertEquals(array(), $this->Form->fields);
  4103. $this->Form->select(
  4104. 'Model.select',
  4105. array()
  4106. );
  4107. $this->assertEquals(array(), $this->Form->fields);
  4108. $this->Form->select(
  4109. 'Model.select',
  4110. array(),
  4111. array('empty' => true)
  4112. );
  4113. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4114. }
  4115. /**
  4116. * testInputMultipleCheckboxes method
  4117. *
  4118. * test input() resulting in multi select elements being generated.
  4119. *
  4120. * @return void
  4121. */
  4122. public function testInputMultipleCheckboxes() {
  4123. $result = $this->Form->input('Model.multi_field', array(
  4124. 'options' => array('first', 'second', 'third'),
  4125. 'multiple' => 'checkbox'
  4126. ));
  4127. $expected = array(
  4128. array('div' => array('class' => 'input select')),
  4129. array('label' => array('for' => 'ModelMultiField')),
  4130. 'Multi Field',
  4131. '/label',
  4132. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4133. array('div' => array('class' => 'checkbox')),
  4134. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4135. array('label' => array('for' => 'ModelMultiField0')),
  4136. 'first',
  4137. '/label',
  4138. '/div',
  4139. array('div' => array('class' => 'checkbox')),
  4140. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4141. array('label' => array('for' => 'ModelMultiField1')),
  4142. 'second',
  4143. '/label',
  4144. '/div',
  4145. array('div' => array('class' => 'checkbox')),
  4146. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4147. array('label' => array('for' => 'ModelMultiField2')),
  4148. 'third',
  4149. '/label',
  4150. '/div',
  4151. '/div'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $result = $this->Form->input('Model.multi_field', array(
  4155. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4156. 'multiple' => 'checkbox'
  4157. ));
  4158. $expected = array(
  4159. array('div' => array('class' => 'input select')),
  4160. array('label' => array('for' => 'ModelMultiField')),
  4161. 'Multi Field',
  4162. '/label',
  4163. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4164. array('div' => array('class' => 'checkbox')),
  4165. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4166. array('label' => array('for' => 'ModelMultiFieldA')),
  4167. 'first',
  4168. '/label',
  4169. '/div',
  4170. array('div' => array('class' => 'checkbox')),
  4171. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4172. array('label' => array('for' => 'ModelMultiFieldB')),
  4173. 'second',
  4174. '/label',
  4175. '/div',
  4176. array('div' => array('class' => 'checkbox')),
  4177. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4178. array('label' => array('for' => 'ModelMultiFieldC')),
  4179. 'third',
  4180. '/label',
  4181. '/div',
  4182. '/div'
  4183. );
  4184. $this->assertTags($result, $expected);
  4185. $result = $this->Form->input('Model.multi_field', array(
  4186. 'options' => array('1' => 'first'),
  4187. 'multiple' => 'checkbox',
  4188. 'label' => false,
  4189. 'div' => false
  4190. ));
  4191. $expected = array(
  4192. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4193. array('div' => array('class' => 'checkbox')),
  4194. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4195. array('label' => array('for' => 'ModelMultiField1')),
  4196. 'first',
  4197. '/label',
  4198. '/div'
  4199. );
  4200. $this->assertTags($result, $expected);
  4201. $result = $this->Form->input('Model.multi_field', array(
  4202. 'options' => array('2' => 'second'),
  4203. 'multiple' => 'checkbox',
  4204. 'label' => false,
  4205. 'div' => false
  4206. ));
  4207. $expected = array(
  4208. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4209. array('div' => array('class' => 'checkbox')),
  4210. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4211. array('label' => array('for' => 'ModelMultiField2')),
  4212. 'second',
  4213. '/label',
  4214. '/div'
  4215. );
  4216. $this->assertTags($result, $expected);
  4217. }
  4218. /**
  4219. * testSelectHiddenFieldOmission method
  4220. *
  4221. * test that select() with 'hiddenField' => false omits the hidden field
  4222. *
  4223. * @return void
  4224. */
  4225. public function testSelectHiddenFieldOmission() {
  4226. $result = $this->Form->select('Model.multi_field',
  4227. array('first', 'second'),
  4228. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4229. );
  4230. $expected = array(
  4231. array('div' => array('class' => 'checkbox')),
  4232. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4233. array('label' => array('for' => 'ModelMultiField0')),
  4234. 'first',
  4235. '/label',
  4236. '/div',
  4237. array('div' => array('class' => 'checkbox')),
  4238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4239. array('label' => array('for' => 'ModelMultiField1')),
  4240. 'second',
  4241. '/label',
  4242. '/div'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. $result = $this->Form->input('Model.multi_field', array(
  4246. 'options' => array('first', 'second'),
  4247. 'multiple' => 'checkbox',
  4248. 'hiddenField' => false
  4249. ));
  4250. $expected = array(
  4251. array('div' => array('class' => 'input select')),
  4252. array('label' => array('for' => 'ModelMultiField')),
  4253. 'Multi Field',
  4254. '/label',
  4255. array('div' => array('class' => 'checkbox')),
  4256. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4257. array('label' => array('for' => 'ModelMultiField0')),
  4258. 'first',
  4259. '/label',
  4260. '/div',
  4261. array('div' => array('class' => 'checkbox')),
  4262. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4263. array('label' => array('for' => 'ModelMultiField1')),
  4264. 'second',
  4265. '/label',
  4266. '/div',
  4267. '/div'
  4268. );
  4269. $this->assertTags($result, $expected);
  4270. }
  4271. /**
  4272. * test that select() with multiple = checkbox works with overriding name attribute.
  4273. *
  4274. * @return void
  4275. */
  4276. public function testSelectCheckboxMultipleOverrideName() {
  4277. $result = $this->Form->input('category', array(
  4278. 'type' => 'select',
  4279. 'multiple' => 'checkbox',
  4280. 'name' => 'data[fish]',
  4281. 'options' => array('1', '2'),
  4282. 'div' => false,
  4283. 'label' => false,
  4284. ));
  4285. $expected = array(
  4286. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4287. array('div' => array('class' => 'checkbox')),
  4288. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4289. array('label' => array('for' => 'Category0')), '1', '/label',
  4290. '/div',
  4291. array('div' => array('class' => 'checkbox')),
  4292. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4293. array('label' => array('for' => 'Category1')), '2', '/label',
  4294. '/div'
  4295. );
  4296. $this->assertTags($result, $expected);
  4297. }
  4298. /**
  4299. * Test that 'id' overrides all the checkbox id's as well.
  4300. *
  4301. * @return void
  4302. */
  4303. public function testSelectCheckboxMultipleId() {
  4304. $result = $this->Form->select(
  4305. 'Model.multi_field',
  4306. array('first', 'second', 'third'),
  4307. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4308. );
  4309. $expected = array(
  4310. 'input' => array(
  4311. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4312. ),
  4313. array('div' => array('class' => 'checkbox')),
  4314. array('input' => array(
  4315. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4316. 'value' => '0', 'id' => 'CustomId0'
  4317. )),
  4318. array('label' => array('for' => 'CustomId0')),
  4319. 'first',
  4320. '/label',
  4321. '/div',
  4322. array('div' => array('class' => 'checkbox')),
  4323. array('input' => array(
  4324. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4325. 'value' => '1', 'id' => 'CustomId1'
  4326. )),
  4327. array('label' => array('for' => 'CustomId1')),
  4328. 'second',
  4329. '/label',
  4330. '/div',
  4331. array('div' => array('class' => 'checkbox')),
  4332. array('input' => array(
  4333. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4334. 'value' => '2', 'id' => 'CustomId2'
  4335. )),
  4336. array('label' => array('for' => 'CustomId2')),
  4337. 'third',
  4338. '/label',
  4339. '/div'
  4340. );
  4341. $this->assertTags($result, $expected);
  4342. }
  4343. /**
  4344. * testCheckbox method
  4345. *
  4346. * Test generation of checkboxes
  4347. *
  4348. * @return void
  4349. */
  4350. public function testCheckbox() {
  4351. $result = $this->Form->checkbox('Model.field');
  4352. $expected = array(
  4353. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4354. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4355. );
  4356. $this->assertTags($result, $expected);
  4357. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4358. $expected = array(
  4359. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4360. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. $Contact = ClassRegistry::getObject('Contact');
  4364. $Contact->validationErrors['field'] = 1;
  4365. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4366. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4367. $expected = array(
  4368. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4369. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4370. );
  4371. $this->assertTags($result, $expected);
  4372. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4373. $expected = array(
  4374. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4375. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4376. );
  4377. $this->assertTags($result, $expected);
  4378. $this->Form->request->data['Contact']['field'] = '';
  4379. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4380. $expected = array(
  4381. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4382. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4383. );
  4384. $this->assertTags($result, $expected);
  4385. $Contact->validationErrors = array();
  4386. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4387. $expected = array(
  4388. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4389. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4390. );
  4391. $this->assertTags($result, $expected);
  4392. $this->Form->request->data['Contact']['published'] = 1;
  4393. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4394. $expected = array(
  4395. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4396. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4397. );
  4398. $this->assertTags($result, $expected);
  4399. $this->Form->request->data['Contact']['published'] = 0;
  4400. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4401. $expected = array(
  4402. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4403. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4407. $expected = array(
  4408. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4409. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4410. );
  4411. $this->assertTags($result, $expected);
  4412. $result = $this->Form->checkbox('CustomField.1.value');
  4413. $expected = array(
  4414. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4415. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4416. );
  4417. $this->assertTags($result, $expected);
  4418. }
  4419. /**
  4420. * test checkbox() with a custom name attribute
  4421. *
  4422. * @return void
  4423. */
  4424. public function testCheckboxCustomNameAttribute() {
  4425. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4426. $expected = array(
  4427. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4428. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4429. );
  4430. $this->assertTags($result, $expected);
  4431. }
  4432. /**
  4433. * test the checked option for checkboxes.
  4434. *
  4435. * @return void
  4436. */
  4437. public function testCheckboxCheckedOption() {
  4438. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4439. $expected = array(
  4440. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4441. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4442. );
  4443. $this->assertTags($result, $expected);
  4444. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4445. $expected = array(
  4446. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4447. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4448. );
  4449. $this->assertTags($result, $expected);
  4450. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4451. $expected = array(
  4452. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4453. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4454. );
  4455. $this->assertTags($result, $expected);
  4456. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4457. $expected = array(
  4458. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4459. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4460. );
  4461. $this->assertTags($result, $expected);
  4462. $this->Form->request->data['Model']['field'] = 1;
  4463. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4464. $expected = array(
  4465. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4466. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4467. );
  4468. $this->assertTags($result, $expected);
  4469. }
  4470. /**
  4471. * Test that disabled attribute works on both the checkbox and hidden input.
  4472. *
  4473. * @return void
  4474. */
  4475. public function testCheckboxDisabling() {
  4476. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4477. $expected = array(
  4478. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4479. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4480. );
  4481. $this->assertTags($result, $expected);
  4482. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4483. $expected = array(
  4484. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4485. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4486. );
  4487. $this->assertTags($result, $expected);
  4488. }
  4489. /**
  4490. * Test that the hidden input for checkboxes can be omitted or set to a
  4491. * specific value.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testCheckboxHiddenField() {
  4496. $result = $this->Form->input('UserForm.something', array(
  4497. 'type' => 'checkbox',
  4498. 'hiddenField' => false
  4499. ));
  4500. $expected = array(
  4501. 'div' => array('class' => 'input checkbox'),
  4502. array('input' => array(
  4503. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4504. 'value' => '1', 'id' => 'UserFormSomething'
  4505. )),
  4506. 'label' => array('for' => 'UserFormSomething'),
  4507. 'Something',
  4508. '/label',
  4509. '/div'
  4510. );
  4511. $this->assertTags($result, $expected);
  4512. $result = $this->Form->input('UserForm.something', array(
  4513. 'type' => 'checkbox',
  4514. 'value' => 'Y',
  4515. 'hiddenField' => 'N',
  4516. ));
  4517. $expected = array(
  4518. 'div' => array('class' => 'input checkbox'),
  4519. array('input' => array(
  4520. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4521. 'value' => 'N', 'id' => 'UserFormSomething_'
  4522. )),
  4523. array('input' => array(
  4524. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4525. 'value' => 'Y', 'id' => 'UserFormSomething'
  4526. )),
  4527. 'label' => array('for' => 'UserFormSomething'),
  4528. 'Something',
  4529. '/label',
  4530. '/div'
  4531. );
  4532. $this->assertTags($result, $expected);
  4533. }
  4534. /**
  4535. * testDateTime method
  4536. *
  4537. * Test generation of date/time select elements
  4538. *
  4539. * @return void
  4540. */
  4541. public function testDateTime() {
  4542. extract($this->dateRegex);
  4543. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4544. $now = strtotime('now');
  4545. $expected = array(
  4546. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4547. $daysRegex,
  4548. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4549. date('j', $now),
  4550. '/option',
  4551. '*/select',
  4552. '-',
  4553. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4554. $monthsRegex,
  4555. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4556. date('F', $now),
  4557. '/option',
  4558. '*/select',
  4559. '-',
  4560. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4561. $yearsRegex,
  4562. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4563. date('Y', $now),
  4564. '/option',
  4565. '*/select',
  4566. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4567. $hoursRegex,
  4568. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4569. date('g', $now),
  4570. '/option',
  4571. '*/select',
  4572. ':',
  4573. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4574. $minutesRegex,
  4575. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4576. date('i', $now),
  4577. '/option',
  4578. '*/select',
  4579. ' ',
  4580. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4581. $meridianRegex,
  4582. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4583. date('a', $now),
  4584. '/option',
  4585. '*/select'
  4586. );
  4587. $this->assertTags($result, $expected);
  4588. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4589. $expected = array(
  4590. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4591. $daysRegex,
  4592. array('option' => array('value' => '')),
  4593. '/option',
  4594. '*/select',
  4595. '-',
  4596. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4597. $monthsRegex,
  4598. array('option' => array('value' => '')),
  4599. '/option',
  4600. '*/select',
  4601. '-',
  4602. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4603. $yearsRegex,
  4604. array('option' => array('value' => '')),
  4605. '/option',
  4606. '*/select',
  4607. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4608. $hoursRegex,
  4609. array('option' => array('value' => '')),
  4610. '/option',
  4611. '*/select',
  4612. ':',
  4613. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4614. $minutesRegex,
  4615. array('option' => array('value' => '')),
  4616. '/option',
  4617. '*/select',
  4618. ' ',
  4619. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4620. $meridianRegex,
  4621. array('option' => array('value' => '')),
  4622. '/option',
  4623. '*/select'
  4624. );
  4625. $this->assertTags($result, $expected);
  4626. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4627. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4628. $this->assertTags($result, $expected);
  4629. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4630. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4631. $this->assertTags($result, $expected);
  4632. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4633. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4634. $expected = array(
  4635. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4636. $daysRegex,
  4637. array('option' => array('value' => '')),
  4638. '/option',
  4639. '*/select',
  4640. '-',
  4641. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4642. $monthsRegex,
  4643. array('option' => array('value' => '')),
  4644. '/option',
  4645. '*/select',
  4646. '-',
  4647. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4648. $yearsRegex,
  4649. array('option' => array('value' => '')),
  4650. '/option',
  4651. '*/select',
  4652. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4653. $hoursRegex,
  4654. array('option' => array('value' => '')),
  4655. '/option',
  4656. '*/select',
  4657. ':',
  4658. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4659. $minutesRegex,
  4660. array('option' => array('value' => '')),
  4661. '/option',
  4662. array('option' => array('value' => '00')),
  4663. '00',
  4664. '/option',
  4665. array('option' => array('value' => '05')),
  4666. '05',
  4667. '/option',
  4668. array('option' => array('value' => '10')),
  4669. '10',
  4670. '/option',
  4671. '*/select',
  4672. ' ',
  4673. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4674. $meridianRegex,
  4675. array('option' => array('value' => '')),
  4676. '/option',
  4677. '*/select'
  4678. );
  4679. $this->assertTags($result, $expected);
  4680. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4681. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4682. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4683. $this->Form->request->data['Contact']['data'] = null;
  4684. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4685. $expected = array(
  4686. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4687. $daysRegex,
  4688. array('option' => array('value' => '')),
  4689. '/option',
  4690. '*/select',
  4691. '-',
  4692. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4693. $monthsRegex,
  4694. array('option' => array('value' => '')),
  4695. '/option',
  4696. '*/select',
  4697. '-',
  4698. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4699. $yearsRegex,
  4700. array('option' => array('value' => '')),
  4701. '/option',
  4702. '*/select',
  4703. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4704. $hoursRegex,
  4705. array('option' => array('value' => '')),
  4706. '/option',
  4707. '*/select',
  4708. ':',
  4709. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4710. $minutesRegex,
  4711. array('option' => array('value' => '')),
  4712. '/option',
  4713. '*/select',
  4714. ' ',
  4715. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4716. $meridianRegex,
  4717. array('option' => array('value' => '')),
  4718. '/option',
  4719. '*/select'
  4720. );
  4721. $this->assertTags($result, $expected);
  4722. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4723. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4724. $now = strtotime($this->Form->data['Model']['field']);
  4725. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4726. $expected = array(
  4727. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4728. $daysRegex,
  4729. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4730. date('j', $now),
  4731. '/option',
  4732. '*/select',
  4733. '-',
  4734. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4735. $monthsRegex,
  4736. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4737. date('F', $now),
  4738. '/option',
  4739. '*/select',
  4740. '-',
  4741. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4742. $yearsRegex,
  4743. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4744. date('Y', $now),
  4745. '/option',
  4746. '*/select',
  4747. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4748. $hoursRegex,
  4749. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4750. date('g', $now),
  4751. '/option',
  4752. '*/select',
  4753. ':',
  4754. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4755. $minutesRegex,
  4756. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4757. date('i', $now),
  4758. '/option',
  4759. '*/select',
  4760. ' ',
  4761. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4762. $meridianRegex,
  4763. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4764. date('a', $now),
  4765. '/option',
  4766. '*/select'
  4767. );
  4768. $this->assertTags($result, $expected);
  4769. $selected = strtotime('2008-10-26 12:33:00');
  4770. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4771. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4772. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4773. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4774. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4775. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4776. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4777. $this->Form->create('Contact');
  4778. $result = $this->Form->input('published');
  4779. $now = strtotime('now');
  4780. $expected = array(
  4781. 'div' => array('class' => 'input date'),
  4782. 'label' => array('for' => 'ContactPublishedMonth'),
  4783. 'Published',
  4784. '/label',
  4785. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4786. $monthsRegex,
  4787. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4788. date('F', $now),
  4789. '/option',
  4790. '*/select',
  4791. '-',
  4792. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4793. $daysRegex,
  4794. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4795. date('j', $now),
  4796. '/option',
  4797. '*/select',
  4798. '-',
  4799. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4800. $yearsRegex,
  4801. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4802. date('Y', $now),
  4803. '/option',
  4804. '*/select',
  4805. '/div'
  4806. );
  4807. $this->assertTags($result, $expected);
  4808. $result = $this->Form->input('published2', array('type' => 'date'));
  4809. $now = strtotime('now');
  4810. $expected = array(
  4811. 'div' => array('class' => 'input date'),
  4812. 'label' => array('for' => 'ContactPublished2Month'),
  4813. 'Published2',
  4814. '/label',
  4815. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4816. $monthsRegex,
  4817. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4818. date('F', $now),
  4819. '/option',
  4820. '*/select',
  4821. '-',
  4822. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4823. $daysRegex,
  4824. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4825. date('j', $now),
  4826. '/option',
  4827. '*/select',
  4828. '-',
  4829. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4830. $yearsRegex,
  4831. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4832. date('Y', $now),
  4833. '/option',
  4834. '*/select',
  4835. '/div'
  4836. );
  4837. $this->assertTags($result, $expected);
  4838. $this->Form->create('Contact');
  4839. $result = $this->Form->input('published', array('monthNames' => false));
  4840. $now = strtotime('now');
  4841. $expected = array(
  4842. 'div' => array('class' => 'input date'),
  4843. 'label' => array('for' => 'ContactPublishedMonth'),
  4844. 'Published',
  4845. '/label',
  4846. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4847. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4848. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4849. date('m', $now),
  4850. '/option',
  4851. '*/select',
  4852. '-',
  4853. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4854. $daysRegex,
  4855. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4856. date('j', $now),
  4857. '/option',
  4858. '*/select',
  4859. '-',
  4860. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4861. $yearsRegex,
  4862. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4863. date('Y', $now),
  4864. '/option',
  4865. '*/select',
  4866. '/div'
  4867. );
  4868. $this->assertTags($result, $expected);
  4869. $result = $this->Form->input('published', array('type' => 'time'));
  4870. $now = strtotime('now');
  4871. $expected = array(
  4872. 'div' => array('class' => 'input time'),
  4873. 'label' => array('for' => 'ContactPublishedHour'),
  4874. 'Published',
  4875. '/label',
  4876. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4877. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4878. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4879. date('g', $now),
  4880. '/option',
  4881. '*/select',
  4882. ':',
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. $result = $this->Form->input('published', array(
  4886. 'timeFormat' => 24,
  4887. 'interval' => 5,
  4888. 'selected' => strtotime('2009-09-03 13:37:00'),
  4889. 'type' => 'datetime'
  4890. ));
  4891. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4892. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4893. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4894. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4895. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4896. $this->assertNoErrors();
  4897. $this->Form->request->data['Contact'] = array(
  4898. 'date' => array(
  4899. 'day' => '',
  4900. 'month' => '',
  4901. 'year' => '',
  4902. 'hour' => '',
  4903. 'min' => '',
  4904. 'meridian' => ''
  4905. )
  4906. );
  4907. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4908. }
  4909. /**
  4910. * test that datetime() and default values work.
  4911. *
  4912. * @return void
  4913. */
  4914. public function testDatetimeWithDefault() {
  4915. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4916. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4917. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4918. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4919. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4920. 'default' => '2009-06-01 11:15:30'
  4921. ));
  4922. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4923. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4924. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4925. }
  4926. /**
  4927. * test that bogus non-date time data doesn't cause errors.
  4928. *
  4929. * @return void
  4930. */
  4931. public function testDateTimeWithBogusData() {
  4932. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4933. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4934. }
  4935. /**
  4936. * testDateTimeEmptyAsArray
  4937. *
  4938. * @return void
  4939. */
  4940. public function testDateTimeEmptyAsArray() {
  4941. $result = $this->Form->dateTime('Contact.date',
  4942. 'DMY',
  4943. '12',
  4944. array(
  4945. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4946. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4947. )
  4948. )
  4949. );
  4950. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4951. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4952. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4953. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4954. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4955. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4956. $result = $this->Form->dateTime('Contact.date',
  4957. 'DMY',
  4958. '12',
  4959. array(
  4960. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4961. )
  4962. );
  4963. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4964. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4965. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4966. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4967. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4968. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4969. }
  4970. /**
  4971. * testFormDateTimeMulti method
  4972. *
  4973. * test multiple datetime element generation
  4974. *
  4975. * @return void
  4976. */
  4977. public function testFormDateTimeMulti() {
  4978. extract($this->dateRegex);
  4979. $result = $this->Form->dateTime('Contact.1.updated');
  4980. $expected = array(
  4981. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4982. $daysRegex,
  4983. array('option' => array('value' => '')),
  4984. '/option',
  4985. '*/select',
  4986. '-',
  4987. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4988. $monthsRegex,
  4989. array('option' => array('value' => '')),
  4990. '/option',
  4991. '*/select',
  4992. '-',
  4993. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4994. $yearsRegex,
  4995. array('option' => array('value' => '')),
  4996. '/option',
  4997. '*/select',
  4998. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4999. $hoursRegex,
  5000. array('option' => array('value' => '')),
  5001. '/option',
  5002. '*/select',
  5003. ':',
  5004. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5005. $minutesRegex,
  5006. array('option' => array('value' => '')),
  5007. '/option',
  5008. '*/select',
  5009. ' ',
  5010. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5011. $meridianRegex,
  5012. array('option' => array('value' => '')),
  5013. '/option',
  5014. '*/select'
  5015. );
  5016. $this->assertTags($result, $expected);
  5017. $result = $this->Form->dateTime('Contact.2.updated');
  5018. $expected = array(
  5019. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5020. $daysRegex,
  5021. array('option' => array('value' => '')),
  5022. '/option',
  5023. '*/select',
  5024. '-',
  5025. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5026. $monthsRegex,
  5027. array('option' => array('value' => '')),
  5028. '/option',
  5029. '*/select',
  5030. '-',
  5031. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5032. $yearsRegex,
  5033. array('option' => array('value' => '')),
  5034. '/option',
  5035. '*/select',
  5036. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5037. $hoursRegex,
  5038. array('option' => array('value' => '')),
  5039. '/option',
  5040. '*/select',
  5041. ':',
  5042. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5043. $minutesRegex,
  5044. array('option' => array('value' => '')),
  5045. '/option',
  5046. '*/select',
  5047. ' ',
  5048. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5049. $meridianRegex,
  5050. array('option' => array('value' => '')),
  5051. '/option',
  5052. '*/select'
  5053. );
  5054. $this->assertTags($result, $expected);
  5055. }
  5056. /**
  5057. * When changing the date format, the label should always focus the first select box when
  5058. * clicked.
  5059. *
  5060. * @return void
  5061. */
  5062. public function testDateTimeLabelIdMatchesFirstInput() {
  5063. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5064. $this->assertContains('label for="ModelDateMonth"', $result);
  5065. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5066. $this->assertContains('label for="ModelDateDay"', $result);
  5067. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5068. $this->assertContains('label for="ModelDateYear"', $result);
  5069. }
  5070. /**
  5071. * testMonth method
  5072. *
  5073. * @return void
  5074. */
  5075. public function testMonth() {
  5076. $result = $this->Form->month('Model.field');
  5077. $expected = array(
  5078. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5079. array('option' => array('value' => '')),
  5080. '/option',
  5081. array('option' => array('value' => '01')),
  5082. date('F', strtotime('2008-01-01 00:00:00')),
  5083. '/option',
  5084. array('option' => array('value' => '02')),
  5085. date('F', strtotime('2008-02-01 00:00:00')),
  5086. '/option',
  5087. '*/select',
  5088. );
  5089. $this->assertTags($result, $expected);
  5090. $result = $this->Form->month('Model.field', array('empty' => true));
  5091. $expected = array(
  5092. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5093. array('option' => array('value' => '')),
  5094. '/option',
  5095. array('option' => array('value' => '01')),
  5096. date('F', strtotime('2008-01-01 00:00:00')),
  5097. '/option',
  5098. array('option' => array('value' => '02')),
  5099. date('F', strtotime('2008-02-01 00:00:00')),
  5100. '/option',
  5101. '*/select',
  5102. );
  5103. $this->assertTags($result, $expected);
  5104. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5105. $expected = array(
  5106. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5107. array('option' => array('value' => '')),
  5108. '/option',
  5109. array('option' => array('value' => '01')),
  5110. '01',
  5111. '/option',
  5112. array('option' => array('value' => '02')),
  5113. '02',
  5114. '/option',
  5115. '*/select',
  5116. );
  5117. $this->assertTags($result, $expected);
  5118. $monthNames = array(
  5119. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5120. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5121. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5122. $expected = array(
  5123. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5124. array('option' => array('value' => '')),
  5125. '/option',
  5126. array('option' => array('value' => '01')),
  5127. 'Jan',
  5128. '/option',
  5129. array('option' => array('value' => '02')),
  5130. 'Feb',
  5131. '/option',
  5132. '*/select',
  5133. );
  5134. $this->assertTags($result, $expected);
  5135. }
  5136. /**
  5137. * testDay method
  5138. *
  5139. * @return void
  5140. */
  5141. public function testDay() {
  5142. extract($this->dateRegex);
  5143. $result = $this->Form->day('Model.field', array('value' => false));
  5144. $expected = array(
  5145. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5146. array('option' => array('value' => '')),
  5147. '/option',
  5148. array('option' => array('value' => '01')),
  5149. '1',
  5150. '/option',
  5151. array('option' => array('value' => '02')),
  5152. '2',
  5153. '/option',
  5154. $daysRegex,
  5155. '/select',
  5156. );
  5157. $this->assertTags($result, $expected);
  5158. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5159. $result = $this->Form->day('Model.field');
  5160. $expected = array(
  5161. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5162. array('option' => array('value' => '')),
  5163. '/option',
  5164. array('option' => array('value' => '01')),
  5165. '1',
  5166. '/option',
  5167. array('option' => array('value' => '02')),
  5168. '2',
  5169. '/option',
  5170. $daysRegex,
  5171. array('option' => array('value' => '10', 'selected' => 'selected')),
  5172. '10',
  5173. '/option',
  5174. $daysRegex,
  5175. '/select',
  5176. );
  5177. $this->assertTags($result, $expected);
  5178. $this->Form->request->data['Model']['field'] = '';
  5179. $result = $this->Form->day('Model.field', array('value' => '10'));
  5180. $expected = array(
  5181. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5182. array('option' => array('value' => '')),
  5183. '/option',
  5184. array('option' => array('value' => '01')),
  5185. '1',
  5186. '/option',
  5187. array('option' => array('value' => '02')),
  5188. '2',
  5189. '/option',
  5190. $daysRegex,
  5191. array('option' => array('value' => '10', 'selected' => 'selected')),
  5192. '10',
  5193. '/option',
  5194. $daysRegex,
  5195. '/select',
  5196. );
  5197. $this->assertTags($result, $expected);
  5198. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5199. $result = $this->Form->day('Model.field', array('value' => true));
  5200. $expected = array(
  5201. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5202. array('option' => array('value' => '')),
  5203. '/option',
  5204. array('option' => array('value' => '01')),
  5205. '1',
  5206. '/option',
  5207. array('option' => array('value' => '02')),
  5208. '2',
  5209. '/option',
  5210. $daysRegex,
  5211. array('option' => array('value' => '10', 'selected' => 'selected')),
  5212. '10',
  5213. '/option',
  5214. $daysRegex,
  5215. '/select',
  5216. );
  5217. $this->assertTags($result, $expected);
  5218. }
  5219. /**
  5220. * testMinute method
  5221. *
  5222. * @return void
  5223. */
  5224. public function testMinute() {
  5225. extract($this->dateRegex);
  5226. $result = $this->Form->minute('Model.field');
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '00')),
  5232. '00',
  5233. '/option',
  5234. array('option' => array('value' => '01')),
  5235. '01',
  5236. '/option',
  5237. array('option' => array('value' => '02')),
  5238. '02',
  5239. '/option',
  5240. $minutesRegex,
  5241. '/select',
  5242. );
  5243. $this->assertTags($result, $expected);
  5244. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5245. $result = $this->Form->minute('Model.field');
  5246. $expected = array(
  5247. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5248. array('option' => array('value' => '')),
  5249. '/option',
  5250. array('option' => array('value' => '00')),
  5251. '00',
  5252. '/option',
  5253. array('option' => array('value' => '01')),
  5254. '01',
  5255. '/option',
  5256. array('option' => array('value' => '02')),
  5257. '02',
  5258. '/option',
  5259. $minutesRegex,
  5260. array('option' => array('value' => '12', 'selected' => 'selected')),
  5261. '12',
  5262. '/option',
  5263. $minutesRegex,
  5264. '/select',
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->Form->request->data['Model']['field'] = '';
  5268. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5269. $expected = array(
  5270. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5271. array('option' => array('value' => '')),
  5272. '/option',
  5273. array('option' => array('value' => '00')),
  5274. '00',
  5275. '/option',
  5276. array('option' => array('value' => '05')),
  5277. '05',
  5278. '/option',
  5279. array('option' => array('value' => '10')),
  5280. '10',
  5281. '/option',
  5282. $minutesRegex,
  5283. '/select',
  5284. );
  5285. $this->assertTags($result, $expected);
  5286. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5287. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5288. $expected = array(
  5289. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5290. array('option' => array('value' => '')),
  5291. '/option',
  5292. array('option' => array('value' => '00')),
  5293. '00',
  5294. '/option',
  5295. array('option' => array('value' => '05')),
  5296. '05',
  5297. '/option',
  5298. array('option' => array('value' => '10', 'selected' => 'selected')),
  5299. '10',
  5300. '/option',
  5301. $minutesRegex,
  5302. '/select',
  5303. );
  5304. $this->assertTags($result, $expected);
  5305. }
  5306. /**
  5307. * testHour method
  5308. *
  5309. * @return void
  5310. */
  5311. public function testHour() {
  5312. extract($this->dateRegex);
  5313. $result = $this->Form->hour('Model.field', false);
  5314. $expected = array(
  5315. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5316. array('option' => array('value' => '')),
  5317. '/option',
  5318. array('option' => array('value' => '01')),
  5319. '1',
  5320. '/option',
  5321. array('option' => array('value' => '02')),
  5322. '2',
  5323. '/option',
  5324. $hoursRegex,
  5325. '/select',
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5329. $result = $this->Form->hour('Model.field', false);
  5330. $expected = array(
  5331. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5332. array('option' => array('value' => '')),
  5333. '/option',
  5334. array('option' => array('value' => '01')),
  5335. '1',
  5336. '/option',
  5337. array('option' => array('value' => '02')),
  5338. '2',
  5339. '/option',
  5340. $hoursRegex,
  5341. array('option' => array('value' => '12', 'selected' => 'selected')),
  5342. '12',
  5343. '/option',
  5344. '/select',
  5345. );
  5346. $this->assertTags($result, $expected);
  5347. $this->Form->request->data['Model']['field'] = '';
  5348. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5349. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5350. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5351. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5352. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5353. $result = $this->Form->hour('Model.field', true);
  5354. $expected = array(
  5355. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5356. array('option' => array('value' => '')),
  5357. '/option',
  5358. array('option' => array('value' => '00', 'selected' => 'selected')),
  5359. '0',
  5360. '/option',
  5361. array('option' => array('value' => '01')),
  5362. '1',
  5363. '/option',
  5364. array('option' => array('value' => '02')),
  5365. '2',
  5366. '/option',
  5367. $hoursRegex,
  5368. '/select',
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. unset($this->Form->request->data['Model']['field']);
  5372. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5373. $thisHour = date('H');
  5374. $optValue = date('G');
  5375. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5376. }
  5377. /**
  5378. * testYear method
  5379. *
  5380. * @return void
  5381. */
  5382. public function testYear() {
  5383. $result = $this->Form->year('Model.field', 2006, 2007);
  5384. $expected = array(
  5385. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5386. array('option' => array('value' => '')),
  5387. '/option',
  5388. array('option' => array('value' => '2007')),
  5389. '2007',
  5390. '/option',
  5391. array('option' => array('value' => '2006')),
  5392. '2006',
  5393. '/option',
  5394. '/select',
  5395. );
  5396. $this->assertTags($result, $expected);
  5397. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5398. $expected = array(
  5399. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5400. array('option' => array('value' => '')),
  5401. '/option',
  5402. array('option' => array('value' => '2006')),
  5403. '2006',
  5404. '/option',
  5405. array('option' => array('value' => '2007')),
  5406. '2007',
  5407. '/option',
  5408. '/select',
  5409. );
  5410. $this->assertTags($result, $expected);
  5411. $this->request->data['Contact']['published'] = '';
  5412. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5413. $expected = array(
  5414. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5415. array('option' => array('value' => '')),
  5416. '/option',
  5417. array('option' => array('value' => '2007')),
  5418. '2007',
  5419. '/option',
  5420. array('option' => array('value' => '2006')),
  5421. '2006',
  5422. '/option',
  5423. '/select',
  5424. );
  5425. $this->assertTags($result, $expected);
  5426. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5427. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5428. $expected = array(
  5429. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5430. array('option' => array('value' => '2007')),
  5431. '2007',
  5432. '/option',
  5433. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5434. '2006',
  5435. '/option',
  5436. '/select',
  5437. );
  5438. $this->assertTags($result, $expected);
  5439. $this->Form->request->data['Contact']['published'] = '';
  5440. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5441. $expected = array(
  5442. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5443. array('option' => array('value' => '')),
  5444. '/option',
  5445. array('option' => array('value' => '2007')),
  5446. '2007',
  5447. '/option',
  5448. array('option' => array('value' => '2006')),
  5449. '2006',
  5450. '/option',
  5451. '/select',
  5452. );
  5453. $this->assertTags($result, $expected);
  5454. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5455. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5456. $expected = array(
  5457. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5458. array('option' => array('value' => '2007')),
  5459. '2007',
  5460. '/option',
  5461. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5462. '2006',
  5463. '/option',
  5464. '/select',
  5465. );
  5466. $this->assertTags($result, $expected);
  5467. $this->Form->request->data['Contact']['published'] = '';
  5468. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5469. $expected = array(
  5470. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5471. array('option' => array('value' => '')),
  5472. '/option',
  5473. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5474. '2007',
  5475. '/option',
  5476. array('option' => array('value' => '2006')),
  5477. '2006',
  5478. '/option',
  5479. '/select',
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5483. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5484. $expected = array(
  5485. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5486. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5487. '2007',
  5488. '/option',
  5489. array('option' => array('value' => '2006')),
  5490. '2006',
  5491. '/option',
  5492. '/select',
  5493. );
  5494. $this->assertTags($result, $expected);
  5495. $this->Form->request->data['Contact']['published'] = '';
  5496. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5497. $expected = array(
  5498. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5499. array('option' => array('value' => '2008')),
  5500. '2008',
  5501. '/option',
  5502. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5503. '2007',
  5504. '/option',
  5505. array('option' => array('value' => '2006')),
  5506. '2006',
  5507. '/option',
  5508. '/select',
  5509. );
  5510. $this->assertTags($result, $expected);
  5511. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5512. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5513. $expected = array(
  5514. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5515. array('option' => array('value' => '2008')),
  5516. '2008',
  5517. '/option',
  5518. array('option' => array('value' => '2007')),
  5519. '2007',
  5520. '/option',
  5521. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5522. '2006',
  5523. '/option',
  5524. '/select',
  5525. );
  5526. $this->assertTags($result, $expected);
  5527. $this->Form->request->data = array();
  5528. $this->Form->create('Contact');
  5529. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5530. $expected = array(
  5531. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5532. array('option' => array('value' => '2008')),
  5533. '2008',
  5534. '/option',
  5535. array('option' => array('value' => '2007')),
  5536. '2007',
  5537. '/option',
  5538. array('option' => array('value' => '2006')),
  5539. '2006',
  5540. '/option',
  5541. '/select',
  5542. );
  5543. $this->assertTags($result, $expected);
  5544. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5545. $this->assertContains('data[Contact][published][year]', $result);
  5546. }
  5547. /**
  5548. * testTextArea method
  5549. *
  5550. * @return void
  5551. */
  5552. public function testTextArea() {
  5553. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5554. $result = $this->Form->textarea('Model.field');
  5555. $expected = array(
  5556. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5557. 'some test data',
  5558. '/textarea',
  5559. );
  5560. $this->assertTags($result, $expected);
  5561. $result = $this->Form->textarea('Model.tmp');
  5562. $expected = array(
  5563. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5564. '/textarea',
  5565. );
  5566. $this->assertTags($result, $expected);
  5567. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5568. $result = $this->Form->textarea('Model.field');
  5569. $expected = array(
  5570. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5571. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5572. '/textarea',
  5573. );
  5574. $this->assertTags($result, $expected);
  5575. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5576. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5577. $expected = array(
  5578. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5579. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5580. '/textarea',
  5581. );
  5582. $this->assertTags($result, $expected);
  5583. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5584. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5585. $expected = array(
  5586. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5587. '/textarea'
  5588. );
  5589. $this->assertTags($result, $expected);
  5590. }
  5591. /**
  5592. * testTextAreaWithStupidCharacters method
  5593. *
  5594. * test text area with non-ascii characters
  5595. *
  5596. * @return void
  5597. */
  5598. public function testTextAreaWithStupidCharacters() {
  5599. $this->loadFixtures('Post');
  5600. $result = $this->Form->input('Post.content', array(
  5601. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5602. ));
  5603. $expected = array(
  5604. 'div' => array('class' => 'input textarea'),
  5605. 'label' => array('for' => 'PostContent'),
  5606. 'Current Text',
  5607. '/label',
  5608. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5609. 'GREAT®',
  5610. '/textarea',
  5611. '/div'
  5612. );
  5613. $this->assertTags($result, $expected);
  5614. }
  5615. /**
  5616. * testHiddenField method
  5617. *
  5618. * @return void
  5619. */
  5620. public function testHiddenField() {
  5621. $Contact = ClassRegistry::getObject('Contact');
  5622. $Contact->validationErrors['field'] = 1;
  5623. $this->Form->request->data['Contact']['field'] = 'test';
  5624. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5625. $this->assertTags($result, array(
  5626. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5627. );
  5628. }
  5629. /**
  5630. * testFileUploadField method
  5631. *
  5632. * @return void
  5633. */
  5634. public function testFileUploadField() {
  5635. $result = $this->Form->file('Model.upload');
  5636. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5637. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5638. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5639. $expected = array(
  5640. 'div' => array('class' => 'input file'),
  5641. 'label' => array('for' => 'ModelUpload'),
  5642. 'Upload',
  5643. '/label',
  5644. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5645. '/div'
  5646. );
  5647. $this->assertTags($result, $expected);
  5648. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5649. $result = $this->Form->file('Model.upload');
  5650. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5651. }
  5652. /**
  5653. * test File upload input on a model not used in create();
  5654. *
  5655. * @return void
  5656. */
  5657. public function testFileUploadOnOtherModel() {
  5658. $this->Form->create('ValidateUser', array('type' => 'file'));
  5659. $result = $this->Form->file('ValidateProfile.city');
  5660. $expected = array(
  5661. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5662. );
  5663. $this->assertTags($result, $expected);
  5664. }
  5665. /**
  5666. * testButton method
  5667. *
  5668. * @return void
  5669. */
  5670. public function testButton() {
  5671. $result = $this->Form->button('Hi');
  5672. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5673. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5674. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5675. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5676. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5677. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5678. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5679. $result = $this->Form->button('No type', array('type' => false));
  5680. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5681. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5682. $this->assertNotRegExp('/\&039/', $result);
  5683. }
  5684. /**
  5685. * Test that button() makes unlocked fields by default.
  5686. *
  5687. * @return void
  5688. */
  5689. public function testButtonUnlockedByDefault() {
  5690. $this->Form->request->params['_Token']['key'] = 'secured';
  5691. $this->Form->button('Save', array('name' => 'save'));
  5692. $this->Form->button('Clear');
  5693. $result = $this->Form->unlockField();
  5694. $this->assertEquals(array('save'), $result);
  5695. }
  5696. /**
  5697. * testPostButton method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testPostButton() {
  5702. $result = $this->Form->postButton('Hi', '/controller/action');
  5703. $this->assertTags($result, array(
  5704. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5705. 'div' => array('style' => 'display:none;'),
  5706. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5707. '/div',
  5708. 'button' => array('type' => 'submit'),
  5709. 'Hi',
  5710. '/button',
  5711. '/form'
  5712. ));
  5713. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5714. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5715. }
  5716. /**
  5717. * Test that postButton adds _Token fields.
  5718. *
  5719. * @return void
  5720. */
  5721. public function testSecurePostButton() {
  5722. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5723. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5724. $expected = array(
  5725. 'form' => array(
  5726. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5727. ),
  5728. array('div' => array('style' => 'display:none;')),
  5729. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5730. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5731. '/div',
  5732. 'button' => array('type' => 'submit'),
  5733. 'Delete',
  5734. '/button',
  5735. array('div' => array('style' => 'display:none;')),
  5736. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5737. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5738. '/div',
  5739. '/form',
  5740. );
  5741. $this->assertTags($result, $expected);
  5742. }
  5743. /**
  5744. * testPostLink method
  5745. *
  5746. * @return void
  5747. */
  5748. public function testPostLink() {
  5749. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5750. $this->assertTags($result, array(
  5751. 'form' => array(
  5752. 'method' => 'post', 'action' => '/posts/delete/1',
  5753. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5754. ),
  5755. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5756. '/form',
  5757. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5758. 'Delete',
  5759. '/a'
  5760. ));
  5761. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5762. $this->assertTags($result, array(
  5763. 'form' => array(
  5764. 'method' => 'post', 'action' => '/posts/delete/1',
  5765. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5766. ),
  5767. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5768. '/form',
  5769. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5770. 'Delete',
  5771. '/a'
  5772. ));
  5773. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5774. $this->assertTags($result, array(
  5775. 'form' => array(
  5776. 'method' => 'post', 'action' => '/posts/delete/1',
  5777. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5778. ),
  5779. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5780. '/form',
  5781. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5782. 'Delete',
  5783. '/a'
  5784. ));
  5785. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5786. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5787. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5788. $this->assertTags($result, array(
  5789. 'form' => array(
  5790. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5791. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5792. ),
  5793. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5794. '/form',
  5795. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5796. 'Delete',
  5797. '/a'
  5798. ));
  5799. }
  5800. /**
  5801. * Test that postLink adds _Token fields.
  5802. *
  5803. * @return void
  5804. */
  5805. public function testSecurePostLink() {
  5806. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5807. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5808. $expected = array(
  5809. 'form' => array(
  5810. 'method' => 'post', 'action' => '/posts/delete/1',
  5811. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5812. ),
  5813. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5814. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5815. 'div' => array('style' => 'display:none;'),
  5816. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5817. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5818. '/div',
  5819. '/form',
  5820. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5821. 'Delete',
  5822. '/a'
  5823. );
  5824. $this->assertTags($result, $expected);
  5825. }
  5826. /**
  5827. * testSubmitButton method
  5828. *
  5829. * @return void
  5830. */
  5831. public function testSubmitButton() {
  5832. $result = $this->Form->submit('');
  5833. $expected = array(
  5834. 'div' => array('class' => 'submit'),
  5835. 'input' => array('type' => 'submit', 'value' => ''),
  5836. '/div'
  5837. );
  5838. $this->assertTags($result, $expected);
  5839. $result = $this->Form->submit('Test Submit');
  5840. $expected = array(
  5841. 'div' => array('class' => 'submit'),
  5842. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5843. '/div'
  5844. );
  5845. $this->assertTags($result, $expected);
  5846. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5847. $expected = array(
  5848. 'span' => array('class' => 'submit'),
  5849. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5850. '/span'
  5851. );
  5852. $this->assertTags($result, $expected);
  5853. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5854. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5855. $this->assertTags($result, $expected);
  5856. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5857. $expected = array(
  5858. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5859. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5860. '/div'
  5861. );
  5862. $this->assertTags($result, $expected);
  5863. $result = $this->Form->submit('Next >');
  5864. $expected = array(
  5865. 'div' => array('class' => 'submit'),
  5866. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5867. '/div'
  5868. );
  5869. $this->assertTags($result, $expected);
  5870. $result = $this->Form->submit('Next >', array('escape' => false));
  5871. $expected = array(
  5872. 'div' => array('class' => 'submit'),
  5873. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5874. '/div'
  5875. );
  5876. $this->assertTags($result, $expected);
  5877. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5878. $expected = array(
  5879. 'div' => array('class' => 'submit'),
  5880. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5881. '/div'
  5882. );
  5883. $this->assertTags($result, $expected);
  5884. $before = '--before--';
  5885. $after = '--after--';
  5886. $result = $this->Form->submit('Test', array('before' => $before));
  5887. $expected = array(
  5888. 'div' => array('class' => 'submit'),
  5889. '--before--',
  5890. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5891. '/div'
  5892. );
  5893. $this->assertTags($result, $expected);
  5894. $result = $this->Form->submit('Test', array('after' => $after));
  5895. $expected = array(
  5896. 'div' => array('class' => 'submit'),
  5897. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5898. '--after--',
  5899. '/div'
  5900. );
  5901. $this->assertTags($result, $expected);
  5902. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5903. $expected = array(
  5904. 'div' => array('class' => 'submit'),
  5905. '--before--',
  5906. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5907. '--after--',
  5908. '/div'
  5909. );
  5910. $this->assertTags($result, $expected);
  5911. }
  5912. /**
  5913. * test image submit types.
  5914. *
  5915. * @return void
  5916. */
  5917. public function testSubmitImage() {
  5918. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5919. $expected = array(
  5920. 'div' => array('class' => 'submit'),
  5921. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. $result = $this->Form->submit('/relative/cake.power.gif');
  5926. $expected = array(
  5927. 'div' => array('class' => 'submit'),
  5928. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5929. '/div'
  5930. );
  5931. $this->assertTags($result, $expected);
  5932. $result = $this->Form->submit('cake.power.gif');
  5933. $expected = array(
  5934. 'div' => array('class' => 'submit'),
  5935. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5936. '/div'
  5937. );
  5938. $this->assertTags($result, $expected);
  5939. $result = $this->Form->submit('Not.an.image');
  5940. $expected = array(
  5941. 'div' => array('class' => 'submit'),
  5942. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5943. '/div'
  5944. );
  5945. $this->assertTags($result, $expected);
  5946. $after = '--after--';
  5947. $before = '--before--';
  5948. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5949. $expected = array(
  5950. 'div' => array('class' => 'submit'),
  5951. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5952. '--after--',
  5953. '/div'
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5957. $expected = array(
  5958. 'div' => array('class' => 'submit'),
  5959. '--before--',
  5960. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5961. '/div'
  5962. );
  5963. $this->assertTags($result, $expected);
  5964. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5965. $expected = array(
  5966. 'div' => array('class' => 'submit'),
  5967. '--before--',
  5968. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5969. '--after--',
  5970. '/div'
  5971. );
  5972. $this->assertTags($result, $expected);
  5973. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5974. $expected = array(
  5975. 'div' => array('class' => 'submit'),
  5976. '--before--',
  5977. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5978. '--after--',
  5979. '/div'
  5980. );
  5981. $this->assertTags($result, $expected);
  5982. }
  5983. /**
  5984. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5985. * be submitted at a time.
  5986. *
  5987. * @return void
  5988. */
  5989. public function testSubmitUnlockedByDefault() {
  5990. $this->Form->request->params['_Token']['key'] = 'secured';
  5991. $this->Form->submit('Go go');
  5992. $this->Form->submit('Save', array('name' => 'save'));
  5993. $result = $this->Form->unlockField();
  5994. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5995. }
  5996. /**
  5997. * Test submit image with timestamps.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testSubmitImageTimestamp() {
  6002. Configure::write('Asset.timestamp', 'force');
  6003. $result = $this->Form->submit('cake.power.gif');
  6004. $expected = array(
  6005. 'div' => array('class' => 'submit'),
  6006. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6007. '/div'
  6008. );
  6009. $this->assertTags($result, $expected);
  6010. }
  6011. /**
  6012. * test the create() method
  6013. *
  6014. * @return void
  6015. */
  6016. public function testCreate() {
  6017. $result = $this->Form->create('Contact');
  6018. $encoding = strtolower(Configure::read('App.encoding'));
  6019. $expected = array(
  6020. 'form' => array(
  6021. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6022. 'accept-charset' => $encoding
  6023. ),
  6024. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6025. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6026. '/div'
  6027. );
  6028. $this->assertTags($result, $expected);
  6029. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6030. $expected = array('form' => array(
  6031. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6032. 'accept-charset' => $encoding
  6033. ));
  6034. $this->assertTags($result, $expected);
  6035. $result = $this->Form->create('Contact', array('type' => 'get'));
  6036. $expected = array('form' => array(
  6037. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6038. 'accept-charset' => $encoding
  6039. ));
  6040. $this->assertTags($result, $expected);
  6041. $result = $this->Form->create('Contact', array('type' => 'put'));
  6042. $expected = array(
  6043. 'form' => array(
  6044. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6045. 'accept-charset' => $encoding
  6046. ),
  6047. 'div' => array('style' => 'display:none;'),
  6048. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6049. '/div'
  6050. );
  6051. $this->assertTags($result, $expected);
  6052. $result = $this->Form->create('Contact', array('type' => 'file'));
  6053. $expected = array(
  6054. 'form' => array(
  6055. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6056. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6057. ),
  6058. 'div' => array('style' => 'display:none;'),
  6059. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6060. '/div'
  6061. );
  6062. $this->assertTags($result, $expected);
  6063. $this->Form->request->data['Contact']['id'] = 1;
  6064. $this->Form->request->here = '/contacts/edit/1';
  6065. $this->Form->request['action'] = 'edit';
  6066. $result = $this->Form->create('Contact');
  6067. $expected = array(
  6068. 'form' => array(
  6069. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6070. 'accept-charset' => $encoding
  6071. ),
  6072. 'div' => array('style' => 'display:none;'),
  6073. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6074. '/div'
  6075. );
  6076. $this->assertTags($result, $expected);
  6077. $this->Form->request->data['Contact']['id'] = 1;
  6078. $this->Form->request->here = '/contacts/edit/1';
  6079. $this->Form->request['action'] = 'edit';
  6080. $result = $this->Form->create('Contact', array('type' => 'file'));
  6081. $expected = array(
  6082. 'form' => array(
  6083. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6084. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6085. ),
  6086. 'div' => array('style' => 'display:none;'),
  6087. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6088. '/div'
  6089. );
  6090. $this->assertTags($result, $expected);
  6091. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6092. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6093. $expected = array(
  6094. 'form' => array(
  6095. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6096. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6097. ),
  6098. 'div' => array('style' => 'display:none;'),
  6099. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6100. '/div'
  6101. );
  6102. $this->assertTags($result, $expected);
  6103. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6104. $expected = array(
  6105. 'form' => array(
  6106. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6107. 'accept-charset' => $encoding
  6108. ),
  6109. 'div' => array('style' => 'display:none;'),
  6110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6111. '/div'
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. $this->Form->request['action'] = 'add';
  6115. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6116. $expected = array(
  6117. 'form' => array(
  6118. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6119. 'accept-charset' => $encoding
  6120. ),
  6121. 'div' => array('style' => 'display:none;'),
  6122. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6123. '/div'
  6124. );
  6125. $this->assertTags($result, $expected);
  6126. $result = $this->Form->create('User', array('action' => 'login'));
  6127. $expected = array(
  6128. 'form' => array(
  6129. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6130. 'accept-charset' => $encoding
  6131. ),
  6132. 'div' => array('style' => 'display:none;'),
  6133. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6134. '/div'
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. $result = $this->Form->create('User', array('url' => '/users/login'));
  6138. $expected = array(
  6139. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6140. 'div' => array('style' => 'display:none;'),
  6141. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6142. '/div'
  6143. );
  6144. $this->assertTags($result, $expected);
  6145. $this->Form->request['controller'] = 'pages';
  6146. $result = $this->Form->create('User', array('action' => 'signup'));
  6147. $expected = array(
  6148. 'form' => array(
  6149. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6150. 'accept-charset' => $encoding
  6151. ),
  6152. 'div' => array('style' => 'display:none;'),
  6153. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6154. '/div'
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. $this->Form->request->data = array();
  6158. $this->Form->request['controller'] = 'contacts';
  6159. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6160. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6161. $expected = array(
  6162. 'form' => array(
  6163. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6164. 'accept-charset' => 'utf-8'
  6165. ),
  6166. 'div' => array('style' => 'display:none;'),
  6167. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6168. '/div'
  6169. );
  6170. $this->assertTags($result, $expected);
  6171. }
  6172. /**
  6173. * Test the onsubmit option for create()
  6174. *
  6175. * @return void
  6176. */
  6177. public function testCreateOnSubmit() {
  6178. $this->Form->request->data = array();
  6179. $this->Form->request['controller'] = 'contacts';
  6180. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6181. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6182. $expected = array(
  6183. 'form' => array(
  6184. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6185. 'accept-charset' => 'utf-8'
  6186. ),
  6187. 'div' => array('style' => 'display:none;'),
  6188. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6189. '/div'
  6190. );
  6191. $this->assertTags($result, $expected);
  6192. $this->Form->request->data = array();
  6193. $this->Form->request['controller'] = 'contacts';
  6194. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6195. $result = $this->Form->create(array(
  6196. 'url' => array('action' => 'index', 'param'),
  6197. 'default' => false,
  6198. 'onsubmit' => 'someFunction();'
  6199. ));
  6200. $expected = array(
  6201. 'form' => array(
  6202. 'id' => 'ContactAddForm', 'method' => 'post',
  6203. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6204. 'action' => '/contacts/index/param',
  6205. 'accept-charset' => 'utf-8'
  6206. ),
  6207. 'div' => array('style' => 'display:none;'),
  6208. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6209. '/div'
  6210. );
  6211. $this->assertTags($result, $expected);
  6212. }
  6213. /**
  6214. * test create() with automatic url generation
  6215. *
  6216. * @return void
  6217. */
  6218. public function testCreateAutoUrl() {
  6219. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6220. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6221. $this->Form->request->base = '/base_url';
  6222. $result = $this->Form->create('Contact');
  6223. $expected = array(
  6224. 'form' => array(
  6225. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6226. 'accept-charset' => 'utf-8'
  6227. ),
  6228. 'div' => array('style' => 'display:none;'),
  6229. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6230. '/div'
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. $this->Form->request['action'] = 'delete';
  6234. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6235. $this->Form->request->base = '/base_url';
  6236. $result = $this->Form->create('Contact');
  6237. $expected = array(
  6238. 'form' => array(
  6239. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6240. 'accept-charset' => 'utf-8'
  6241. ),
  6242. 'div' => array('style' => 'display:none;'),
  6243. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6244. '/div'
  6245. );
  6246. $this->assertTags($result, $expected);
  6247. }
  6248. /**
  6249. * test create() with a custom route
  6250. *
  6251. * @return void
  6252. */
  6253. public function testCreateCustomRoute() {
  6254. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6255. $encoding = strtolower(Configure::read('App.encoding'));
  6256. $result = $this->Form->create('User', array('action' => 'login'));
  6257. $expected = array(
  6258. 'form' => array(
  6259. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6260. 'accept-charset' => $encoding
  6261. ),
  6262. 'div' => array('style' => 'display:none;'),
  6263. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6264. '/div'
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. }
  6268. /**
  6269. * test that inputDefaults are stored and used.
  6270. *
  6271. * @return void
  6272. */
  6273. public function testCreateWithInputDefaults() {
  6274. $this->Form->create('User', array(
  6275. 'inputDefaults' => array(
  6276. 'div' => false,
  6277. 'label' => false,
  6278. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6279. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6280. )
  6281. ));
  6282. $result = $this->Form->input('username');
  6283. $expected = array(
  6284. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6285. );
  6286. $this->assertTags($result, $expected);
  6287. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6288. $expected = array(
  6289. 'div' => array('class' => 'input text'),
  6290. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6291. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6292. '/div'
  6293. );
  6294. $this->assertTags($result, $expected);
  6295. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6296. $expected = array(
  6297. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6298. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6299. );
  6300. $this->assertTags($result, $expected);
  6301. $this->Form->create('User', array(
  6302. 'inputDefaults' => array(
  6303. 'div' => false,
  6304. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6305. )
  6306. ));
  6307. $result = $this->Form->input('username', array('div' => true));
  6308. $expected = array(
  6309. 'div' => array('class' => 'input text'),
  6310. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6311. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6312. '/div'
  6313. );
  6314. $this->assertTags($result, $expected);
  6315. }
  6316. /**
  6317. * test automatic accept-charset overriding
  6318. *
  6319. * @return void
  6320. */
  6321. public function testCreateWithAcceptCharset() {
  6322. $result = $this->Form->create('UserForm', array(
  6323. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6324. )
  6325. );
  6326. $expected = array(
  6327. 'form' => array(
  6328. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6329. 'accept-charset' => 'iso-8859-1'
  6330. ),
  6331. 'div' => array('style' => 'display:none;'),
  6332. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6333. '/div'
  6334. );
  6335. $this->assertTags($result, $expected);
  6336. }
  6337. /**
  6338. * Test base form url when url param is passed with multiple parameters (&)
  6339. *
  6340. */
  6341. public function testCreateQuerystringrequest() {
  6342. $encoding = strtolower(Configure::read('App.encoding'));
  6343. $result = $this->Form->create('Contact', array(
  6344. 'type' => 'post',
  6345. 'escape' => false,
  6346. 'url' => array(
  6347. 'controller' => 'controller',
  6348. 'action' => 'action',
  6349. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6350. )
  6351. ));
  6352. $expected = array(
  6353. 'form' => array(
  6354. 'id' => 'ContactAddForm',
  6355. 'method' => 'post',
  6356. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6357. 'accept-charset' => $encoding
  6358. ),
  6359. 'div' => array('style' => 'display:none;'),
  6360. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6361. '/div'
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $result = $this->Form->create('Contact', array(
  6365. 'type' => 'post',
  6366. 'url' => array(
  6367. 'controller' => 'controller',
  6368. 'action' => 'action',
  6369. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6370. )
  6371. ));
  6372. $expected = array(
  6373. 'form' => array(
  6374. 'id' => 'ContactAddForm',
  6375. 'method' => 'post',
  6376. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6377. 'accept-charset' => $encoding
  6378. ),
  6379. 'div' => array('style' => 'display:none;'),
  6380. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6381. '/div'
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. }
  6385. /**
  6386. * test that create() doesn't cause errors by multiple id's being in the primary key
  6387. * as could happen with multiple select or checkboxes.
  6388. *
  6389. * @return void
  6390. */
  6391. public function testCreateWithMultipleIdInData() {
  6392. $encoding = strtolower(Configure::read('App.encoding'));
  6393. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6394. $result = $this->Form->create('Contact');
  6395. $expected = array(
  6396. 'form' => array(
  6397. 'id' => 'ContactAddForm',
  6398. 'method' => 'post',
  6399. 'action' => '/contacts/add',
  6400. 'accept-charset' => $encoding
  6401. ),
  6402. 'div' => array('style' => 'display:none;'),
  6403. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6404. '/div'
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. }
  6408. /**
  6409. * test that create() doesn't add in extra passed params.
  6410. *
  6411. * @return void
  6412. */
  6413. public function testCreatePassedArgs() {
  6414. $encoding = strtolower(Configure::read('App.encoding'));
  6415. $this->Form->request->data['Contact']['id'] = 1;
  6416. $result = $this->Form->create('Contact', array(
  6417. 'type' => 'post',
  6418. 'escape' => false,
  6419. 'url' => array(
  6420. 'action' => 'edit',
  6421. 'myparam'
  6422. )
  6423. ));
  6424. $expected = array(
  6425. 'form' => array(
  6426. 'id' => 'ContactAddForm',
  6427. 'method' => 'post',
  6428. 'action' => '/contacts/edit/myparam',
  6429. 'accept-charset' => $encoding
  6430. ),
  6431. 'div' => array('style' => 'display:none;'),
  6432. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. }
  6437. /**
  6438. * test creating a get form, and get form inputs.
  6439. *
  6440. * @return void
  6441. */
  6442. public function testGetFormCreate() {
  6443. $encoding = strtolower(Configure::read('App.encoding'));
  6444. $result = $this->Form->create('Contact', array('type' => 'get'));
  6445. $this->assertTags($result, array('form' => array(
  6446. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6447. 'accept-charset' => $encoding
  6448. )));
  6449. $result = $this->Form->text('Contact.name');
  6450. $this->assertTags($result, array('input' => array(
  6451. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6452. )));
  6453. $result = $this->Form->password('password');
  6454. $this->assertTags($result, array('input' => array(
  6455. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6456. )));
  6457. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6458. $result = $this->Form->text('user_form');
  6459. $this->assertTags($result, array('input' => array(
  6460. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6461. )));
  6462. }
  6463. /**
  6464. * test get form, and inputs when the model param is false
  6465. *
  6466. * @return void
  6467. */
  6468. public function testGetFormWithFalseModel() {
  6469. $encoding = strtolower(Configure::read('App.encoding'));
  6470. $this->Form->request['controller'] = 'contact_test';
  6471. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6472. $expected = array('form' => array(
  6473. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6474. 'accept-charset' => $encoding
  6475. ));
  6476. $this->assertTags($result, $expected);
  6477. $result = $this->Form->text('reason');
  6478. $expected = array(
  6479. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6480. );
  6481. $this->assertTags($result, $expected);
  6482. }
  6483. /**
  6484. * test that datetime() works with GET style forms.
  6485. *
  6486. * @return void
  6487. */
  6488. public function testDateTimeWithGetForms() {
  6489. extract($this->dateRegex);
  6490. $this->Form->create('Contact', array('type' => 'get'));
  6491. $result = $this->Form->datetime('created');
  6492. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6493. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6494. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6495. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6496. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6497. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6498. }
  6499. /**
  6500. * testEditFormWithData method
  6501. *
  6502. * test auto populating form elements from submitted data.
  6503. *
  6504. * @return void
  6505. */
  6506. public function testEditFormWithData() {
  6507. $this->Form->request->data = array('Person' => array(
  6508. 'id' => 1,
  6509. 'first_name' => 'Nate',
  6510. 'last_name' => 'Abele',
  6511. 'email' => 'nate@example.com'
  6512. ));
  6513. $this->Form->request->addParams(array(
  6514. 'models' => array('Person'),
  6515. 'controller' => 'people',
  6516. 'action' => 'add'
  6517. ));
  6518. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6519. $this->Form->create();
  6520. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6521. $expected = array(
  6522. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6523. 'select' => array(
  6524. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6525. ),
  6526. array('option' => array('value' => 1)), 'Nate', '/option',
  6527. array('option' => array('value' => 2)), 'Garrett', '/option',
  6528. array('option' => array('value' => 3)), 'Larry', '/option',
  6529. '/select'
  6530. );
  6531. $this->assertTags($result, $expected);
  6532. }
  6533. /**
  6534. * Test that required fields are created for various types of validation.
  6535. *
  6536. * @return void
  6537. */
  6538. public function testFormInputRequiredDetection() {
  6539. $this->Form->create('Contact');
  6540. $result = $this->Form->input('Contact.non_existing');
  6541. $expected = array(
  6542. 'div' => array('class' => 'input text'),
  6543. 'label' => array('for' => 'ContactNonExisting'),
  6544. 'Non Existing',
  6545. '/label',
  6546. 'input' => array(
  6547. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6548. 'id' => 'ContactNonExisting'
  6549. ),
  6550. '/div'
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. $result = $this->Form->input('Contact.imrequired');
  6554. $expected = array(
  6555. 'div' => array('class' => 'input text required'),
  6556. 'label' => array('for' => 'ContactImrequired'),
  6557. 'Imrequired',
  6558. '/label',
  6559. 'input' => array(
  6560. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6561. 'id' => 'ContactImrequired',
  6562. 'required' => 'required'
  6563. ),
  6564. '/div'
  6565. );
  6566. $this->assertTags($result, $expected);
  6567. $result = $this->Form->input('Contact.imalsorequired');
  6568. $expected = array(
  6569. 'div' => array('class' => 'input text required'),
  6570. 'label' => array('for' => 'ContactImalsorequired'),
  6571. 'Imalsorequired',
  6572. '/label',
  6573. 'input' => array(
  6574. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6575. 'id' => 'ContactImalsorequired',
  6576. 'required' => 'required'
  6577. ),
  6578. '/div'
  6579. );
  6580. $this->assertTags($result, $expected);
  6581. $result = $this->Form->input('Contact.imrequiredtoo');
  6582. $expected = array(
  6583. 'div' => array('class' => 'input text required'),
  6584. 'label' => array('for' => 'ContactImrequiredtoo'),
  6585. 'Imrequiredtoo',
  6586. '/label',
  6587. 'input' => array(
  6588. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6589. 'id' => 'ContactImrequiredtoo',
  6590. 'required' => 'required'
  6591. ),
  6592. '/div'
  6593. );
  6594. $this->assertTags($result, $expected);
  6595. $result = $this->Form->input('Contact.required_one');
  6596. $expected = array(
  6597. 'div' => array('class' => 'input text required'),
  6598. 'label' => array('for' => 'ContactRequiredOne'),
  6599. 'Required One',
  6600. '/label',
  6601. 'input' => array(
  6602. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6603. 'id' => 'ContactRequiredOne',
  6604. 'required' => 'required'
  6605. ),
  6606. '/div'
  6607. );
  6608. $this->assertTags($result, $expected);
  6609. $result = $this->Form->input('Contact.string_required');
  6610. $expected = array(
  6611. 'div' => array('class' => 'input text required'),
  6612. 'label' => array('for' => 'ContactStringRequired'),
  6613. 'String Required',
  6614. '/label',
  6615. 'input' => array(
  6616. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6617. 'id' => 'ContactStringRequired',
  6618. 'required' => 'required'
  6619. ),
  6620. '/div'
  6621. );
  6622. $this->assertTags($result, $expected);
  6623. $result = $this->Form->input('Contact.imnotrequired');
  6624. $expected = array(
  6625. 'div' => array('class' => 'input text'),
  6626. 'label' => array('for' => 'ContactImnotrequired'),
  6627. 'Imnotrequired',
  6628. '/label',
  6629. 'input' => array(
  6630. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6631. 'id' => 'ContactImnotrequired'
  6632. ),
  6633. '/div'
  6634. );
  6635. $this->assertTags($result, $expected);
  6636. $result = $this->Form->input('Contact.imalsonotrequired');
  6637. $expected = array(
  6638. 'div' => array('class' => 'input text'),
  6639. 'label' => array('for' => 'ContactImalsonotrequired'),
  6640. 'Imalsonotrequired',
  6641. '/label',
  6642. 'input' => array(
  6643. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6644. 'id' => 'ContactImalsonotrequired'
  6645. ),
  6646. '/div'
  6647. );
  6648. $this->assertTags($result, $expected);
  6649. $result = $this->Form->input('Contact.imalsonotrequired2');
  6650. $expected = array(
  6651. 'div' => array('class' => 'input text'),
  6652. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6653. 'Imalsonotrequired2',
  6654. '/label',
  6655. 'input' => array(
  6656. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6657. 'id' => 'ContactImalsonotrequired2'
  6658. ),
  6659. '/div'
  6660. );
  6661. $this->assertTags($result, $expected);
  6662. $result = $this->Form->input('Contact.imnotrequiredeither');
  6663. $expected = array(
  6664. 'div' => array('class' => 'input text'),
  6665. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6666. 'Imnotrequiredeither',
  6667. '/label',
  6668. 'input' => array(
  6669. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6670. 'id' => 'ContactImnotrequiredeither'
  6671. ),
  6672. '/div'
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $result = $this->Form->input('Contact.iamrequiredalways');
  6676. $expected = array(
  6677. 'div' => array('class' => 'input text required'),
  6678. 'label' => array('for' => 'ContactIamrequiredalways'),
  6679. 'Iamrequiredalways',
  6680. '/label',
  6681. 'input' => array(
  6682. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6683. 'id' => 'ContactIamrequiredalways',
  6684. 'required' => 'required'
  6685. ),
  6686. '/div'
  6687. );
  6688. $this->assertTags($result, $expected);
  6689. }
  6690. /**
  6691. * testFormMagicInput method
  6692. *
  6693. * @return void
  6694. */
  6695. public function testFormMagicInput() {
  6696. $encoding = strtolower(Configure::read('App.encoding'));
  6697. $result = $this->Form->create('Contact');
  6698. $expected = array(
  6699. 'form' => array(
  6700. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6701. 'accept-charset' => $encoding
  6702. ),
  6703. 'div' => array('style' => 'display:none;'),
  6704. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6705. '/div'
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->input('name');
  6709. $expected = array(
  6710. 'div' => array('class' => 'input text'),
  6711. 'label' => array('for' => 'ContactName'),
  6712. 'Name',
  6713. '/label',
  6714. 'input' => array(
  6715. 'type' => 'text', 'name' => 'data[Contact][name]',
  6716. 'id' => 'ContactName', 'maxlength' => '255'
  6717. ),
  6718. '/div'
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. $result = $this->Form->input('non_existing_field_in_contact_model');
  6722. $expected = array(
  6723. 'div' => array('class' => 'input text'),
  6724. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6725. 'Non Existing Field In Contact Model',
  6726. '/label',
  6727. 'input' => array(
  6728. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6729. 'id' => 'ContactNonExistingFieldInContactModel'
  6730. ),
  6731. '/div'
  6732. );
  6733. $this->assertTags($result, $expected);
  6734. $result = $this->Form->input('Address.street');
  6735. $expected = array(
  6736. 'div' => array('class' => 'input text'),
  6737. 'label' => array('for' => 'AddressStreet'),
  6738. 'Street',
  6739. '/label',
  6740. 'input' => array(
  6741. 'type' => 'text', 'name' => 'data[Address][street]',
  6742. 'id' => 'AddressStreet'
  6743. ),
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. $result = $this->Form->input('Address.non_existing_field_in_model');
  6748. $expected = array(
  6749. 'div' => array('class' => 'input text'),
  6750. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6751. 'Non Existing Field In Model',
  6752. '/label',
  6753. 'input' => array(
  6754. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6755. 'id' => 'AddressNonExistingFieldInModel'
  6756. ),
  6757. '/div'
  6758. );
  6759. $this->assertTags($result, $expected);
  6760. $result = $this->Form->input('name', array('div' => false));
  6761. $expected = array(
  6762. 'label' => array('for' => 'ContactName'),
  6763. 'Name',
  6764. '/label',
  6765. 'input' => array(
  6766. 'type' => 'text', 'name' => 'data[Contact][name]',
  6767. 'id' => 'ContactName', 'maxlength' => '255'
  6768. )
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. extract($this->dateRegex);
  6772. $now = strtotime('now');
  6773. $result = $this->Form->input('Contact.published', array('div' => false));
  6774. $expected = array(
  6775. 'label' => array('for' => 'ContactPublishedMonth'),
  6776. 'Published',
  6777. '/label',
  6778. array('select' => array(
  6779. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6780. )),
  6781. $monthsRegex,
  6782. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6783. date('F', $now),
  6784. '/option',
  6785. '*/select',
  6786. '-',
  6787. array('select' => array(
  6788. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6789. )),
  6790. $daysRegex,
  6791. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6792. date('j', $now),
  6793. '/option',
  6794. '*/select',
  6795. '-',
  6796. array('select' => array(
  6797. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6798. )),
  6799. $yearsRegex,
  6800. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6801. date('Y', $now),
  6802. '*/select'
  6803. );
  6804. $this->assertTags($result, $expected);
  6805. $result = $this->Form->input('Contact.updated', array('div' => false));
  6806. $expected = array(
  6807. 'label' => array('for' => 'ContactUpdatedMonth'),
  6808. 'Updated',
  6809. '/label',
  6810. array('select' => array(
  6811. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6812. )),
  6813. $monthsRegex,
  6814. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6815. date('F', $now),
  6816. '/option',
  6817. '*/select',
  6818. '-',
  6819. array('select' => array(
  6820. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6821. )),
  6822. $daysRegex,
  6823. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6824. date('j', $now),
  6825. '/option',
  6826. '*/select',
  6827. '-',
  6828. array('select' => array(
  6829. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6830. )),
  6831. $yearsRegex,
  6832. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6833. date('Y', $now),
  6834. '*/select'
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. $result = $this->Form->input('UserForm.stuff');
  6838. $expected = array(
  6839. 'div' => array('class' => 'input text'),
  6840. 'label' => array('for' => 'UserFormStuff'),
  6841. 'Stuff',
  6842. '/label',
  6843. 'input' => array(
  6844. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6845. 'id' => 'UserFormStuff', 'maxlength' => 10
  6846. ),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. }
  6851. /**
  6852. * testForMagicInputNonExistingNorValidated method
  6853. *
  6854. * @return void
  6855. */
  6856. public function testForMagicInputNonExistingNorValidated() {
  6857. $encoding = strtolower(Configure::read('App.encoding'));
  6858. $result = $this->Form->create('Contact');
  6859. $expected = array(
  6860. 'form' => array(
  6861. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6862. 'accept-charset' => $encoding
  6863. ),
  6864. 'div' => array('style' => 'display:none;'),
  6865. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6866. '/div'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6870. $expected = array(
  6871. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6872. 'Non Existing Nor Validated',
  6873. '/label',
  6874. 'input' => array(
  6875. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6876. 'id' => 'ContactNonExistingNorValidated'
  6877. )
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6881. 'div' => false, 'value' => 'my value'
  6882. ));
  6883. $expected = array(
  6884. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6885. 'Non Existing Nor Validated',
  6886. '/label',
  6887. 'input' => array(
  6888. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6889. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6890. )
  6891. );
  6892. $this->assertTags($result, $expected);
  6893. $this->Form->request->data = array(
  6894. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6895. ));
  6896. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6897. $expected = array(
  6898. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6899. 'Non Existing Nor Validated',
  6900. '/label',
  6901. 'input' => array(
  6902. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6903. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6904. )
  6905. );
  6906. $this->assertTags($result, $expected);
  6907. }
  6908. /**
  6909. * testFormMagicInputLabel method
  6910. *
  6911. * @return void
  6912. */
  6913. public function testFormMagicInputLabel() {
  6914. $encoding = strtolower(Configure::read('App.encoding'));
  6915. $result = $this->Form->create('Contact');
  6916. $expected = array(
  6917. 'form' => array(
  6918. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6919. 'accept-charset' => $encoding
  6920. ),
  6921. 'div' => array('style' => 'display:none;'),
  6922. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6923. '/div'
  6924. );
  6925. $this->assertTags($result, $expected);
  6926. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6927. $this->assertTags($result, array('input' => array(
  6928. 'name' => 'data[Contact][name]', 'type' => 'text',
  6929. 'id' => 'ContactName', 'maxlength' => '255')
  6930. ));
  6931. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6932. $expected = array(
  6933. 'label' => array('for' => 'ContactName'),
  6934. 'My label',
  6935. '/label',
  6936. 'input' => array(
  6937. 'type' => 'text', 'name' => 'data[Contact][name]',
  6938. 'id' => 'ContactName', 'maxlength' => '255'
  6939. )
  6940. );
  6941. $this->assertTags($result, $expected);
  6942. $result = $this->Form->input('Contact.name', array(
  6943. 'div' => false, 'label' => array('class' => 'mandatory')
  6944. ));
  6945. $expected = array(
  6946. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6947. 'Name',
  6948. '/label',
  6949. 'input' => array(
  6950. 'type' => 'text', 'name' => 'data[Contact][name]',
  6951. 'id' => 'ContactName', 'maxlength' => '255'
  6952. )
  6953. );
  6954. $this->assertTags($result, $expected);
  6955. $result = $this->Form->input('Contact.name', array(
  6956. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6957. ));
  6958. $expected = array(
  6959. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6960. 'My label',
  6961. '/label',
  6962. 'input' => array(
  6963. 'type' => 'text', 'name' => 'data[Contact][name]',
  6964. 'id' => 'ContactName', 'maxlength' => '255'
  6965. )
  6966. );
  6967. $this->assertTags($result, $expected);
  6968. $result = $this->Form->input('Contact.name', array(
  6969. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6970. ));
  6971. $expected = array(
  6972. 'label' => array('for' => 'my_id'),
  6973. 'Name',
  6974. '/label',
  6975. 'input' => array(
  6976. 'type' => 'text', 'name' => 'data[Contact][name]',
  6977. 'id' => 'my_id', 'maxlength' => '255'
  6978. )
  6979. );
  6980. $this->assertTags($result, $expected);
  6981. $result = $this->Form->input('1.id');
  6982. $this->assertTags($result, array('input' => array(
  6983. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6984. 'id' => 'Contact1Id'
  6985. )));
  6986. $result = $this->Form->input("1.name");
  6987. $expected = array(
  6988. 'div' => array('class' => 'input text'),
  6989. 'label' => array('for' => 'Contact1Name'),
  6990. 'Name',
  6991. '/label',
  6992. 'input' => array(
  6993. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6994. 'id' => 'Contact1Name', 'maxlength' => '255'
  6995. ),
  6996. '/div'
  6997. );
  6998. $this->assertTags($result, $expected);
  6999. $result = $this->Form->input('Contact.1.id');
  7000. $this->assertTags($result, array(
  7001. 'input' => array(
  7002. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7003. 'id' => 'Contact1Id'
  7004. )
  7005. ));
  7006. $result = $this->Form->input("Model.1.name");
  7007. $expected = array(
  7008. 'div' => array('class' => 'input text'),
  7009. 'label' => array('for' => 'Model1Name'),
  7010. 'Name',
  7011. '/label',
  7012. 'input' => array(
  7013. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7014. 'id' => 'Model1Name'
  7015. ),
  7016. '/div'
  7017. );
  7018. $this->assertTags($result, $expected);
  7019. }
  7020. /**
  7021. * testFormEnd method
  7022. *
  7023. * @return void
  7024. */
  7025. public function testFormEnd() {
  7026. $this->assertEquals('</form>', $this->Form->end());
  7027. $result = $this->Form->end('');
  7028. $expected = array(
  7029. 'div' => array('class' => 'submit'),
  7030. 'input' => array('type' => 'submit', 'value' => ''),
  7031. '/div',
  7032. '/form'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. $result = $this->Form->end(array('label' => ''));
  7036. $expected = array(
  7037. 'div' => array('class' => 'submit'),
  7038. 'input' => array('type' => 'submit', 'value' => ''),
  7039. '/div',
  7040. '/form'
  7041. );
  7042. $this->assertTags($result, $expected);
  7043. $result = $this->Form->end('save');
  7044. $expected = array(
  7045. 'div' => array('class' => 'submit'),
  7046. 'input' => array('type' => 'submit', 'value' => 'save'),
  7047. '/div',
  7048. '/form'
  7049. );
  7050. $this->assertTags($result, $expected);
  7051. $result = $this->Form->end(array('label' => 'save'));
  7052. $expected = array(
  7053. 'div' => array('class' => 'submit'),
  7054. 'input' => array('type' => 'submit', 'value' => 'save'),
  7055. '/div',
  7056. '/form'
  7057. );
  7058. $this->assertTags($result, $expected);
  7059. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7060. $expected = array(
  7061. 'div' => array('class' => 'submit'),
  7062. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7063. '/div',
  7064. '/form'
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. $result = $this->Form->end(array('name' => 'Whatever'));
  7068. $expected = array(
  7069. 'div' => array('class' => 'submit'),
  7070. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7071. '/div',
  7072. '/form'
  7073. );
  7074. $this->assertTags($result, $expected);
  7075. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7076. $expected = array(
  7077. 'div' => array('class' => 'good'),
  7078. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7079. '/div',
  7080. '/form'
  7081. );
  7082. $this->assertTags($result, $expected);
  7083. $result = $this->Form->end(array(
  7084. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7085. ));
  7086. $expected = array(
  7087. 'div' => array('class' => 'good'),
  7088. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7089. '/div',
  7090. '/form'
  7091. );
  7092. $this->assertTags($result, $expected);
  7093. }
  7094. /**
  7095. * testMultipleFormWithIdFields method
  7096. *
  7097. * @return void
  7098. */
  7099. public function testMultipleFormWithIdFields() {
  7100. $this->Form->create('UserForm');
  7101. $result = $this->Form->input('id');
  7102. $this->assertTags($result, array('input' => array(
  7103. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7104. )));
  7105. $result = $this->Form->input('ValidateItem.id');
  7106. $this->assertTags($result, array('input' => array(
  7107. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7108. 'id' => 'ValidateItemId'
  7109. )));
  7110. $result = $this->Form->input('ValidateUser.id');
  7111. $this->assertTags($result, array('input' => array(
  7112. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7113. 'id' => 'ValidateUserId'
  7114. )));
  7115. }
  7116. /**
  7117. * testDbLessModel method
  7118. *
  7119. * @return void
  7120. */
  7121. public function testDbLessModel() {
  7122. $this->Form->create('TestMail');
  7123. $result = $this->Form->input('name');
  7124. $expected = array(
  7125. 'div' => array('class' => 'input text'),
  7126. 'label' => array('for' => 'TestMailName'),
  7127. 'Name',
  7128. '/label',
  7129. 'input' => array(
  7130. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7131. 'id' => 'TestMailName'
  7132. ),
  7133. '/div'
  7134. );
  7135. $this->assertTags($result, $expected);
  7136. ClassRegistry::init('TestMail');
  7137. $this->Form->create('TestMail');
  7138. $result = $this->Form->input('name');
  7139. $expected = array(
  7140. 'div' => array('class' => 'input text'),
  7141. 'label' => array('for' => 'TestMailName'),
  7142. 'Name',
  7143. '/label',
  7144. 'input' => array(
  7145. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7146. 'id' => 'TestMailName'
  7147. ),
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. }
  7152. /**
  7153. * testBrokenness method
  7154. *
  7155. * @return void
  7156. */
  7157. public function testBrokenness() {
  7158. /*
  7159. * #4 This test has two parents and four children. By default (as of r7117) both
  7160. * parents are show but the first parent is missing a child. This is the inconsistency
  7161. * in the default behaviour - one parent has all children, the other does not - dependent
  7162. * on the data values.
  7163. */
  7164. $result = $this->Form->select('Model.field', array(
  7165. 'Fred' => array(
  7166. 'freds_son_1' => 'Fred',
  7167. 'freds_son_2' => 'Freddie'
  7168. ),
  7169. 'Bert' => array(
  7170. 'berts_son_1' => 'Albert',
  7171. 'berts_son_2' => 'Bertie')
  7172. ),
  7173. array('showParents' => true, 'empty' => false)
  7174. );
  7175. $expected = array(
  7176. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7177. array('optgroup' => array('label' => 'Fred')),
  7178. array('option' => array('value' => 'freds_son_1')),
  7179. 'Fred',
  7180. '/option',
  7181. array('option' => array('value' => 'freds_son_2')),
  7182. 'Freddie',
  7183. '/option',
  7184. '/optgroup',
  7185. array('optgroup' => array('label' => 'Bert')),
  7186. array('option' => array('value' => 'berts_son_1')),
  7187. 'Albert',
  7188. '/option',
  7189. array('option' => array('value' => 'berts_son_2')),
  7190. 'Bertie',
  7191. '/option',
  7192. '/optgroup',
  7193. '/select'
  7194. );
  7195. $this->assertTags($result, $expected);
  7196. /*
  7197. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7198. * changed, so we should expect the same select list data, just with a different name
  7199. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7200. * This is where data corruption can occur, because when a select value is missing from
  7201. * a list a form will substitute the first value in the list - without the user knowing.
  7202. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7203. * affect the availability of 3 => 'Three' as a valid option.
  7204. */
  7205. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7206. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7207. ));
  7208. $result = $this->Form->select(
  7209. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7210. );
  7211. $expected = array(
  7212. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7213. array('option' => array('value' => 1)),
  7214. 'One',
  7215. '/option',
  7216. array('option' => array('value' => 2)),
  7217. 'Two',
  7218. '/option',
  7219. array('optgroup' => array('label' => 'Three')),
  7220. array('option' => array('value' => 3)),
  7221. 'Three',
  7222. '/option',
  7223. array('option' => array('value' => 4)),
  7224. 'Four',
  7225. '/option',
  7226. array('option' => array('value' => 5)),
  7227. 'Five',
  7228. '/option',
  7229. '/optgroup',
  7230. '/select'
  7231. );
  7232. $this->assertTags($result, $expected);
  7233. }
  7234. /**
  7235. * Test the generation of fields for a multi record form.
  7236. *
  7237. * @return void
  7238. */
  7239. public function testMultiRecordForm() {
  7240. $this->Form->create('ValidateProfile');
  7241. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7242. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7243. $expected = array(
  7244. 'div' => array('class' => 'input textarea'),
  7245. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7246. 'Name',
  7247. '/label',
  7248. 'textarea' => array(
  7249. 'id' => 'ValidateProfile1ValidateItem2Name',
  7250. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7251. 'cols' => 30,
  7252. 'rows' => 6
  7253. ),
  7254. 'Value',
  7255. '/textarea',
  7256. '/div'
  7257. );
  7258. $this->assertTags($result, $expected);
  7259. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7260. $expected = array(
  7261. 'div' => array('class' => 'input date'),
  7262. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7263. 'Created',
  7264. '/label',
  7265. array('select' => array(
  7266. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7267. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7268. )
  7269. ),
  7270. array('option' => array('value' => '')), '/option',
  7271. $this->dateRegex['monthsRegex'],
  7272. '/select', '-',
  7273. array('select' => array(
  7274. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7275. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7276. )
  7277. ),
  7278. array('option' => array('value' => '')), '/option',
  7279. $this->dateRegex['daysRegex'],
  7280. '/select', '-',
  7281. array('select' => array(
  7282. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7283. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7284. )
  7285. ),
  7286. array('option' => array('value' => '')), '/option',
  7287. $this->dateRegex['yearsRegex'],
  7288. '/select',
  7289. '/div'
  7290. );
  7291. $this->assertTags($result, $expected);
  7292. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7293. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7294. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7295. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7296. $expected = array(
  7297. 'div' => array('class' => 'input select error'),
  7298. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7299. 'Profile',
  7300. '/label',
  7301. 'select' => array(
  7302. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7303. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7304. 'class' => 'form-error'
  7305. ),
  7306. '/select',
  7307. array('div' => array('class' => 'error-message')),
  7308. 'Error',
  7309. '/div',
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. }
  7314. /**
  7315. * test the correct display of multi-record form validation errors.
  7316. *
  7317. * @return void
  7318. */
  7319. public function testMultiRecordFormValidationErrors() {
  7320. $this->Form->create('ValidateProfile');
  7321. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7322. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7323. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7324. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7325. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7326. $result = $this->Form->error('ValidateProfile.2.city');
  7327. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7328. $result = $this->Form->error('2.city');
  7329. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7330. }
  7331. /**
  7332. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7333. *
  7334. * @return void
  7335. */
  7336. public function testInputTemplate() {
  7337. $result = $this->Form->input('Contact.email', array(
  7338. 'type' => 'text', 'format' => array('input')
  7339. ));
  7340. $expected = array(
  7341. 'div' => array('class' => 'input text'),
  7342. 'input' => array(
  7343. 'type' => 'text', 'name' => 'data[Contact][email]',
  7344. 'id' => 'ContactEmail'
  7345. ),
  7346. '/div'
  7347. );
  7348. $this->assertTags($result, $expected);
  7349. $result = $this->Form->input('Contact.email', array(
  7350. 'type' => 'text', 'format' => array('input', 'label'),
  7351. 'label' => '<em>Email (required)</em>'
  7352. ));
  7353. $expected = array(
  7354. 'div' => array('class' => 'input text'),
  7355. array('input' => array(
  7356. 'type' => 'text', 'name' => 'data[Contact][email]',
  7357. 'id' => 'ContactEmail'
  7358. )),
  7359. 'label' => array('for' => 'ContactEmail'),
  7360. 'em' => array(),
  7361. 'Email (required)',
  7362. '/em',
  7363. '/label',
  7364. '/div'
  7365. );
  7366. $this->assertTags($result, $expected);
  7367. $result = $this->Form->input('Contact.email', array(
  7368. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7369. 'between' => '<div>Something in the middle</div>',
  7370. 'after' => '<span>Some text at the end</span>'
  7371. ));
  7372. $expected = array(
  7373. 'div' => array('class' => 'input text'),
  7374. array('input' => array(
  7375. 'type' => 'text', 'name' => 'data[Contact][email]',
  7376. 'id' => 'ContactEmail'
  7377. )),
  7378. array('div' => array()),
  7379. 'Something in the middle',
  7380. '/div',
  7381. 'label' => array('for' => 'ContactEmail'),
  7382. 'Email',
  7383. '/label',
  7384. 'span' => array(),
  7385. 'Some text at the end',
  7386. '/span',
  7387. '/div'
  7388. );
  7389. $this->assertTags($result, $expected);
  7390. $result = $this->Form->input('Contact.method', array(
  7391. 'type' => 'radio',
  7392. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7393. 'between' => 'I am between',
  7394. ));
  7395. $expected = array(
  7396. 'div' => array('class' => 'input radio'),
  7397. 'fieldset' => array(),
  7398. 'legend' => array(),
  7399. 'Method',
  7400. '/legend',
  7401. 'I am between',
  7402. 'input' => array(
  7403. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7404. 'value' => '', 'id' => 'ContactMethod_'
  7405. ),
  7406. array('input' => array(
  7407. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7408. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7409. )),
  7410. array('label' => array('for' => 'ContactMethodEmail')),
  7411. 'Email',
  7412. '/label',
  7413. array('input' => array(
  7414. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7415. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7416. )),
  7417. array('label' => array('for' => 'ContactMethodPigeon')),
  7418. 'Pigeon',
  7419. '/label',
  7420. '/fieldset',
  7421. '/div',
  7422. );
  7423. $this->assertTags($result, $expected);
  7424. }
  7425. /**
  7426. * test that some html5 inputs + FormHelper::__call() work
  7427. *
  7428. * @return void
  7429. */
  7430. public function testHtml5Inputs() {
  7431. $result = $this->Form->email('User.email');
  7432. $expected = array(
  7433. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7434. );
  7435. $this->assertTags($result, $expected);
  7436. $result = $this->Form->search('User.query');
  7437. $expected = array(
  7438. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7439. );
  7440. $this->assertTags($result, $expected);
  7441. $result = $this->Form->search('User.query', array('value' => 'test'));
  7442. $expected = array(
  7443. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7444. );
  7445. $this->assertTags($result, $expected);
  7446. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7447. $expected = array(
  7448. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7452. $expected = array(
  7453. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7454. );
  7455. $this->assertTags($result, $expected);
  7456. }
  7457. /**
  7458. *
  7459. * @expectedException CakeException
  7460. * @return void
  7461. */
  7462. public function testHtml5InputException() {
  7463. $this->Form->email();
  7464. }
  7465. /**
  7466. * Tests that a model can be loaded from the model names passed in the request object
  7467. *
  7468. * @return void
  7469. */
  7470. public function testIntrospectModelFromRequest() {
  7471. $this->loadFixtures('Post');
  7472. App::build(array(
  7473. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7474. ));
  7475. CakePlugin::load('TestPlugin');
  7476. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7477. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7478. $this->Form->create('TestPluginPost');
  7479. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7480. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7481. CakePlugin::unload();
  7482. App::build();
  7483. }
  7484. /**
  7485. * Tests that it is possible to set the validation errors directly in the helper for a field
  7486. *
  7487. * @return void
  7488. */
  7489. public function testCustomValidationErrors() {
  7490. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7491. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7492. $this->assertEquals('Badness!', $result);
  7493. }
  7494. /**
  7495. * Tests that the 'on' key validates as expected on create
  7496. *
  7497. * @return void
  7498. */
  7499. public function testRequiredOnCreate() {
  7500. $this->Form->create('Contact');
  7501. $result = $this->Form->input('Contact.imrequiredonupdate');
  7502. $expected = array(
  7503. 'div' => array('class' => 'input text'),
  7504. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7505. 'Imrequiredonupdate',
  7506. '/label',
  7507. 'input' => array(
  7508. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7509. 'id' => 'ContactImrequiredonupdate'
  7510. ),
  7511. '/div'
  7512. );
  7513. $this->assertTags($result, $expected);
  7514. $result = $this->Form->input('Contact.imrequiredoncreate');
  7515. $expected = array(
  7516. 'div' => array('class' => 'input text required'),
  7517. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7518. 'Imrequiredoncreate',
  7519. '/label',
  7520. 'input' => array(
  7521. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7522. 'id' => 'ContactImrequiredoncreate',
  7523. 'required' => 'required'
  7524. ),
  7525. '/div'
  7526. );
  7527. $this->assertTags($result, $expected);
  7528. $result = $this->Form->input('Contact.imrequiredonboth');
  7529. $expected = array(
  7530. 'div' => array('class' => 'input text required'),
  7531. 'label' => array('for' => 'ContactImrequiredonboth'),
  7532. 'Imrequiredonboth',
  7533. '/label',
  7534. 'input' => array(
  7535. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7536. 'id' => 'ContactImrequiredonboth',
  7537. 'required' => 'required'
  7538. ),
  7539. '/div'
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. $this->Form->inputDefaults(array('required' => false));
  7543. $result = $this->Form->input('Contact.imrequired');
  7544. $expected = array(
  7545. 'div' => array('class' => 'input text required'),
  7546. 'label' => array('for' => 'ContactImrequired'),
  7547. 'Imrequired',
  7548. '/label',
  7549. 'input' => array(
  7550. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7551. 'id' => 'ContactImrequired'
  7552. ),
  7553. '/div'
  7554. );
  7555. $this->assertTags($result, $expected);
  7556. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7557. $this->assertTags($result, $expected);
  7558. }
  7559. /**
  7560. * Tests that the 'on' key validates as expected on update
  7561. *
  7562. * @return void
  7563. */
  7564. public function testRequiredOnUpdate() {
  7565. $this->Form->request->data['Contact']['id'] = 1;
  7566. $this->Form->create('Contact');
  7567. $result = $this->Form->input('Contact.imrequiredonupdate');
  7568. $expected = array(
  7569. 'div' => array('class' => 'input text required'),
  7570. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7571. 'Imrequiredonupdate',
  7572. '/label',
  7573. 'input' => array(
  7574. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7575. 'id' => 'ContactImrequiredonupdate',
  7576. 'required' => 'required'
  7577. ),
  7578. '/div'
  7579. );
  7580. $this->assertTags($result, $expected);
  7581. $result = $this->Form->input('Contact.imrequiredoncreate');
  7582. $expected = array(
  7583. 'div' => array('class' => 'input text'),
  7584. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7585. 'Imrequiredoncreate',
  7586. '/label',
  7587. 'input' => array(
  7588. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7589. 'id' => 'ContactImrequiredoncreate'
  7590. ),
  7591. '/div'
  7592. );
  7593. $this->assertTags($result, $expected);
  7594. $result = $this->Form->input('Contact.imrequiredonboth');
  7595. $expected = array(
  7596. 'div' => array('class' => 'input text required'),
  7597. 'label' => array('for' => 'ContactImrequiredonboth'),
  7598. 'Imrequiredonboth',
  7599. '/label',
  7600. 'input' => array(
  7601. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7602. 'id' => 'ContactImrequiredonboth',
  7603. 'required' => 'required'
  7604. ),
  7605. '/div'
  7606. );
  7607. $this->assertTags($result, $expected);
  7608. $result = $this->Form->input('Contact.imrequired');
  7609. $expected = array(
  7610. 'div' => array('class' => 'input text required'),
  7611. 'label' => array('for' => 'ContactImrequired'),
  7612. 'Imrequired',
  7613. '/label',
  7614. 'input' => array(
  7615. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7616. 'id' => 'ContactImrequired',
  7617. 'required' => 'required'
  7618. ),
  7619. '/div'
  7620. );
  7621. $this->assertTags($result, $expected);
  7622. }
  7623. /**
  7624. * Test inputDefaults setter and getter
  7625. *
  7626. * @return void
  7627. */
  7628. public function testInputDefaults() {
  7629. $this->Form->create('Contact');
  7630. $this->Form->inputDefaults(array(
  7631. 'label' => false,
  7632. 'div' => array(
  7633. 'style' => 'color: #000;'
  7634. )
  7635. ));
  7636. $result = $this->Form->input('Contact.field1');
  7637. $expected = array(
  7638. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7639. 'input' => array(
  7640. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7641. 'id' => 'ContactField1'
  7642. ),
  7643. '/div'
  7644. );
  7645. $this->assertTags($result, $expected);
  7646. $this->Form->inputDefaults(array(
  7647. 'div' => false,
  7648. 'label' => 'Label',
  7649. ));
  7650. $result = $this->Form->input('Contact.field1');
  7651. $expected = array(
  7652. 'label' => array('for' => 'ContactField1'),
  7653. 'Label',
  7654. '/label',
  7655. 'input' => array(
  7656. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7657. 'id' => 'ContactField1'
  7658. ),
  7659. );
  7660. $this->assertTags($result, $expected);
  7661. $this->Form->inputDefaults(array(
  7662. 'label' => false,
  7663. ), true);
  7664. $result = $this->Form->input('Contact.field1');
  7665. $expected = array(
  7666. 'input' => array(
  7667. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7668. 'id' => 'ContactField1'
  7669. ),
  7670. );
  7671. $this->assertTags($result, $expected);
  7672. $result = $this->Form->inputDefaults();
  7673. $expected = array(
  7674. 'div' => false,
  7675. 'label' => false,
  7676. );
  7677. $this->assertEqual($result, $expected);
  7678. }
  7679. }