FormHelperTest.php 279 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package cake
  33. * @package Cake.Test.Case.View.Helper
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. * @package cake
  47. * @package Cake.Test.Case.View.Helper
  48. */
  49. class Contact extends CakeTestModel {
  50. /**
  51. * useTable property
  52. *
  53. * @var bool false
  54. */
  55. public $useTable = false;
  56. /**
  57. * Default schema
  58. *
  59. * @var array
  60. */
  61. protected $_schema = array(
  62. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  63. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  67. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  68. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  69. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  70. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  71. );
  72. /**
  73. * validate property
  74. *
  75. * @var array
  76. */
  77. public $validate = array(
  78. 'non_existing' => array(),
  79. 'idontexist' => array(),
  80. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  81. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  82. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  83. 'imrequiredonboth' => array(
  84. 'required' => array('rule' => 'alphaNumeric'),
  85. ),
  86. 'string_required' => 'notEmpty',
  87. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  88. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  89. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  90. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'imalsonotrequired' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30)),
  94. ),
  95. 'imalsonotrequired2' => array(
  96. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  97. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. ),
  99. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array('User' => array('className' => 'UserForm'));
  127. }
  128. /**
  129. * ContactTagsContact class
  130. *
  131. * @package cake
  132. * @package Cake.Test.Case.View.Helper
  133. */
  134. class ContactTagsContact extends CakeTestModel {
  135. /**
  136. * useTable property
  137. *
  138. * @var bool false
  139. */
  140. public $useTable = false;
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. * @package cake
  165. * @package Cake.Test.Case.View.Helper
  166. */
  167. class ContactNonStandardPk extends Contact {
  168. /**
  169. * primaryKey property
  170. *
  171. * @var string 'pk'
  172. */
  173. public $primaryKey = 'pk';
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function schema($field = false) {
  180. $this->_schema = parent::schema();
  181. $this->_schema['pk'] = $this->_schema['id'];
  182. unset($this->_schema['id']);
  183. return $this->_schema;
  184. }
  185. }
  186. /**
  187. * ContactTag class
  188. *
  189. * @package cake
  190. * @package Cake.Test.Case.View.Helper
  191. */
  192. class ContactTag extends Model {
  193. /**
  194. * useTable property
  195. *
  196. * @var bool false
  197. */
  198. public $useTable = false;
  199. /**
  200. * schema definition
  201. *
  202. * @var array
  203. */
  204. protected $_schema = array(
  205. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  206. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  207. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  208. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  209. );
  210. }
  211. /**
  212. * UserForm class
  213. *
  214. * @package cake
  215. * @package Cake.Test.Case.View.Helper
  216. */
  217. class UserForm extends CakeTestModel {
  218. /**
  219. * useTable property
  220. *
  221. * @var bool false
  222. */
  223. public $useTable = false;
  224. /**
  225. * hasMany property
  226. *
  227. * @var array
  228. */
  229. public $hasMany = array(
  230. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  231. ));
  232. /**
  233. * schema definition
  234. *
  235. * @var array
  236. */
  237. protected $_schema = array(
  238. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  239. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  240. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  241. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  242. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  243. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  244. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  245. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  246. );
  247. }
  248. /**
  249. * OpenidUrl class
  250. *
  251. * @package cake
  252. * @package Cake.Test.Case.View.Helper
  253. */
  254. class OpenidUrl extends CakeTestModel {
  255. /**
  256. * useTable property
  257. *
  258. * @var bool false
  259. */
  260. public $useTable = false;
  261. /**
  262. * belongsTo property
  263. *
  264. * @var array
  265. */
  266. public $belongsTo = array('UserForm' => array(
  267. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  268. ));
  269. /**
  270. * validate property
  271. *
  272. * @var array
  273. */
  274. public $validate = array('openid_not_registered' => array());
  275. /**
  276. * schema method
  277. *
  278. * @var array
  279. */
  280. protected $_schema = array(
  281. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  282. 'user_form_id' => array(
  283. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  284. ),
  285. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. );
  287. /**
  288. * beforeValidate method
  289. *
  290. * @return void
  291. */
  292. public function beforeValidate($options = array()) {
  293. $this->invalidate('openid_not_registered');
  294. return true;
  295. }
  296. }
  297. /**
  298. * ValidateUser class
  299. *
  300. * @package cake
  301. * @package Cake.Test.Case.View.Helper
  302. */
  303. class ValidateUser extends CakeTestModel {
  304. /**
  305. * useTable property
  306. *
  307. * @var bool false
  308. */
  309. public $useTable = false;
  310. /**
  311. * hasOne property
  312. *
  313. * @var array
  314. */
  315. public $hasOne = array('ValidateProfile' => array(
  316. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  317. ));
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  327. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  328. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  329. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('email');
  338. return false;
  339. }
  340. }
  341. /**
  342. * ValidateProfile class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateProfile extends CakeTestModel {
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * schema property
  356. *
  357. * @var array
  358. */
  359. protected $_schema = array(
  360. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  362. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  364. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  365. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  366. );
  367. /**
  368. * hasOne property
  369. *
  370. * @var array
  371. */
  372. public $hasOne = array('ValidateItem' => array(
  373. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  374. ));
  375. /**
  376. * belongsTo property
  377. *
  378. * @var array
  379. */
  380. public $belongsTo = array('ValidateUser' => array(
  381. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  382. ));
  383. /**
  384. * beforeValidate method
  385. *
  386. * @return void
  387. */
  388. public function beforeValidate($options = array()) {
  389. $this->invalidate('full_name');
  390. $this->invalidate('city');
  391. return false;
  392. }
  393. }
  394. /**
  395. * ValidateItem class
  396. *
  397. * @package cake
  398. * @package Cake.Test.Case.View.Helper
  399. */
  400. class ValidateItem extends CakeTestModel {
  401. /**
  402. * useTable property
  403. *
  404. * @var bool false
  405. */
  406. public $useTable = false;
  407. /**
  408. * schema property
  409. *
  410. * @var array
  411. */
  412. protected $_schema = array(
  413. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  415. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  416. 'description' => array(
  417. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  418. ),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * belongsTo property
  424. *
  425. * @var array
  426. */
  427. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  428. /**
  429. * beforeValidate method
  430. *
  431. * @return void
  432. */
  433. public function beforeValidate($options = array()) {
  434. $this->invalidate('description');
  435. return false;
  436. }
  437. }
  438. /**
  439. * TestMail class
  440. *
  441. * @package cake
  442. * @package Cake.Test.Case.View.Helper
  443. */
  444. class TestMail extends CakeTestModel {
  445. /**
  446. * useTable property
  447. *
  448. * @var bool false
  449. */
  450. public $useTable = false;
  451. }
  452. /**
  453. * FormHelperTest class
  454. *
  455. * @package cake
  456. * @subpackage Cake.Test.Case.View.Helper
  457. * @property FormHelper $Form
  458. */
  459. class FormHelperTest extends CakeTestCase {
  460. /**
  461. * Fixtures to be used
  462. *
  463. * @var array
  464. */
  465. public $fixtures = array('core.post');
  466. /**
  467. * Do not load the fixtures by default
  468. *
  469. * @var boolean
  470. */
  471. public $autoFixtures = false;
  472. /**
  473. * setUp method
  474. *
  475. * @return void
  476. */
  477. public function setUp() {
  478. parent::setUp();
  479. Configure::write('App.base', '');
  480. $this->Controller = new ContactTestController();
  481. $this->View = new View($this->Controller);
  482. $this->Form = new FormHelper($this->View);
  483. $this->Form->Html = new HtmlHelper($this->View);
  484. $this->Form->request = new CakeRequest('contacts/add', false);
  485. $this->Form->request->here = '/contacts/add';
  486. $this->Form->request['action'] = 'add';
  487. $this->Form->request->webroot = '';
  488. $this->Form->request->base = '';
  489. ClassRegistry::addObject('Contact', new Contact());
  490. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  491. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  492. ClassRegistry::addObject('User', new UserForm());
  493. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  494. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  495. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  496. $this->oldSalt = Configure::read('Security.salt');
  497. $this->dateRegex = array(
  498. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  499. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  500. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  503. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  504. );
  505. Configure::write('Security.salt', 'foo!');
  506. }
  507. /**
  508. * tearDown method
  509. *
  510. * @return void
  511. */
  512. public function tearDown() {
  513. parent::tearDown();
  514. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  515. Configure::write('Security.salt', $this->oldSalt);
  516. }
  517. /**
  518. * testFormCreateWithSecurity method
  519. *
  520. * Test form->create() with security key.
  521. *
  522. * @return void
  523. */
  524. public function testCreateWithSecurity() {
  525. $this->Form->request['_Token'] = array('key' => 'testKey');
  526. $encoding = strtolower(Configure::read('App.encoding'));
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  528. $expected = array(
  529. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  530. 'div' => array('style' => 'display:none;'),
  531. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  532. array('input' => array(
  533. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  534. )),
  535. '/div'
  536. );
  537. $this->assertTags($result, $expected);
  538. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  539. $expected['form']['id'] = 'MyForm';
  540. $this->assertTags($result, $expected);
  541. }
  542. /**
  543. * testFormCreateGetNoSecurity method
  544. *
  545. * Test form->create() with no security key as its a get form
  546. *
  547. * @return void
  548. */
  549. public function testCreateEndGetNoSecurity() {
  550. $this->Form->request['_Token'] = array('key' => 'testKey');
  551. $encoding = strtolower(Configure::read('App.encoding'));
  552. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  553. $this->assertNotContains('Token', $result);
  554. $result = $this->Form->end('Save');
  555. $this->assertNotContains('Token', $result);
  556. }
  557. /**
  558. * test that create() clears the fields property so it starts fresh
  559. *
  560. * @return void
  561. */
  562. public function testCreateClearingFields() {
  563. $this->Form->fields = array('model_id');
  564. $this->Form->create('Contact');
  565. $this->assertEquals(array(), $this->Form->fields);
  566. }
  567. /**
  568. * Tests form hash generation with model-less data
  569. *
  570. * @return void
  571. */
  572. public function testValidateHashNoModel() {
  573. $this->Form->request['_Token'] = array('key' => 'foo');
  574. $result = $this->Form->secure(array('anything'));
  575. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  576. }
  577. /**
  578. * Tests that models with identical field names get resolved properly
  579. *
  580. * @return void
  581. */
  582. public function testDuplicateFieldNameResolution() {
  583. $result = $this->Form->create('ValidateUser');
  584. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  585. $result = $this->Form->input('ValidateItem.name');
  586. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  587. $result = $this->Form->input('ValidateUser.name');
  588. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  589. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  590. $this->assertRegExp('/type="text"/', $result);
  591. $result = $this->Form->input('ValidateItem.name');
  592. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  593. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  594. $this->assertRegExp('/<textarea/', $result);
  595. $result = $this->Form->input('name');
  596. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  597. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  598. $this->assertRegExp('/type="text"/', $result);
  599. }
  600. /**
  601. * Tests that hidden fields generated for checkboxes don't get locked
  602. *
  603. * @return void
  604. */
  605. public function testNoCheckboxLocking() {
  606. $this->Form->request['_Token'] = array('key' => 'foo');
  607. $this->assertSame(array(), $this->Form->fields);
  608. $this->Form->checkbox('check', array('value' => '1'));
  609. $this->assertSame($this->Form->fields, array('check'));
  610. }
  611. /**
  612. * testFormSecurityFields method
  613. *
  614. * Test generation of secure form hash generation.
  615. *
  616. * @return void
  617. */
  618. public function testFormSecurityFields() {
  619. $key = 'testKey';
  620. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  621. $this->Form->request['_Token'] = array('key' => $key);
  622. $result = $this->Form->secure($fields);
  623. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  624. $hash .= ':' . 'Model.valid';
  625. $hash = urlencode($hash);
  626. $expected = array(
  627. 'div' => array('style' => 'display:none;'),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  630. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  631. )),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  634. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  635. )),
  636. '/div'
  637. );
  638. $this->assertTags($result, $expected);
  639. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  640. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  641. $result = $this->Form->secure($fields);
  642. $expected = array(
  643. 'div' => array('class' => 'hidden'),
  644. array('input' => array(
  645. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  646. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  647. )),
  648. array('input' => array(
  649. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  650. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  651. )),
  652. '/div'
  653. );
  654. $this->assertTags($result, $expected);
  655. }
  656. /**
  657. * Tests correct generation of number fields for double and float fields
  658. *
  659. * @return void
  660. */
  661. public function testTextFieldGenerationForFloats() {
  662. $model = ClassRegistry::getObject('Contact');
  663. $model->setSchema(array('foo' => array(
  664. 'type' => 'float',
  665. 'null' => false,
  666. 'default' => null,
  667. 'length' => 10
  668. )));
  669. $this->Form->create('Contact');
  670. $result = $this->Form->input('foo');
  671. $expected = array(
  672. 'div' => array('class' => 'input number'),
  673. 'label' => array('for' => 'ContactFoo'),
  674. 'Foo',
  675. '/label',
  676. array('input' => array(
  677. 'type' => 'number',
  678. 'name' => 'data[Contact][foo]',
  679. 'id' => 'ContactFoo',
  680. 'step' => 'any'
  681. )),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Form->input('foo', array('step' => 0.5));
  686. $expected = array(
  687. 'div' => array('class' => 'input number'),
  688. 'label' => array('for' => 'ContactFoo'),
  689. 'Foo',
  690. '/label',
  691. array('input' => array(
  692. 'type' => 'number',
  693. 'name' => 'data[Contact][foo]',
  694. 'id' => 'ContactFoo',
  695. 'step' => '0.5'
  696. )),
  697. '/div'
  698. );
  699. $this->assertTags($result, $expected);
  700. }
  701. /**
  702. * Tests correct generation of number fields for integer fields
  703. *
  704. * @access public
  705. * @return void
  706. */
  707. public function testTextFieldTypeNumberGenerationForIntegers() {
  708. $model = ClassRegistry::getObject('Contact');
  709. $model->setSchema(array('foo' => array(
  710. 'type' => 'integer',
  711. 'null' => false,
  712. 'default' => null,
  713. 'length' => null
  714. )));
  715. $this->Form->create('Contact');
  716. $result = $this->Form->input('foo');
  717. $expected = array(
  718. 'div' => array('class' => 'input number'),
  719. 'label' => array('for' => 'ContactFoo'),
  720. 'Foo',
  721. '/label',
  722. array('input' => array(
  723. 'type' => 'number', 'name' => 'data[Contact][foo]',
  724. 'id' => 'ContactFoo'
  725. )),
  726. '/div'
  727. );
  728. $this->assertTags($result, $expected);
  729. }
  730. /**
  731. * testFormSecurityMultipleFields method
  732. *
  733. * Test secure() with multiple row form. Ensure hash is correct.
  734. *
  735. * @return void
  736. */
  737. public function testFormSecurityMultipleFields() {
  738. $key = 'testKey';
  739. $fields = array(
  740. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  741. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  742. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  743. );
  744. $this->Form->request['_Token'] = array('key' => $key);
  745. $result = $this->Form->secure($fields);
  746. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  747. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  748. $expected = array(
  749. 'div' => array('style' => 'display:none;'),
  750. array('input' => array(
  751. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  752. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  753. )),
  754. array('input' => array(
  755. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  756. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  757. )),
  758. '/div'
  759. );
  760. $this->assertTags($result, $expected);
  761. }
  762. /**
  763. * testFormSecurityMultipleSubmitButtons
  764. *
  765. * test form submit generation and ensure that _Token is only created on end()
  766. *
  767. * @return void
  768. */
  769. public function testFormSecurityMultipleSubmitButtons() {
  770. $key = 'testKey';
  771. $this->Form->request['_Token'] = array('key' => $key);
  772. $this->Form->create('Addresses');
  773. $this->Form->input('Address.title');
  774. $this->Form->input('Address.first_name');
  775. $result = $this->Form->submit('Save', array('name' => 'save'));
  776. $expected = array(
  777. 'div' => array('class' => 'submit'),
  778. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  779. '/div',
  780. );
  781. $this->assertTags($result, $expected);
  782. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  783. $expected = array(
  784. 'div' => array('class' => 'submit'),
  785. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  786. '/div',
  787. );
  788. $this->assertTags($result, $expected);
  789. $result = $this->Form->end(null);
  790. $expected = array(
  791. 'div' => array('style' => 'display:none;'),
  792. array('input' => array(
  793. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  794. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  795. )),
  796. array('input' => array(
  797. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  798. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  799. )),
  800. '/div'
  801. );
  802. $this->assertTags($result, $expected);
  803. }
  804. /**
  805. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecurityButtonNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitNestedNamed() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  827. $result = $this->Form->unlockField();
  828. $this->assertEquals(array('Address.button'), $result);
  829. }
  830. /**
  831. * Test that the correct fields are unlocked for image submits with no names.
  832. *
  833. * @return void
  834. */
  835. public function testSecuritySubmitImageNoName() {
  836. $key = 'testKey';
  837. $this->Form->request['_Token'] = array('key' => $key);
  838. $this->Form->create('User');
  839. $result = $this->Form->submit('save.png');
  840. $expected = array(
  841. 'div' => array('class' => 'submit'),
  842. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png', array('name' => 'test'));
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * testFormSecurityMultipleInputFields method
  868. *
  869. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  870. *
  871. * @return void
  872. */
  873. public function testFormSecurityMultipleInputFields() {
  874. $key = 'testKey';
  875. $this->Form->request['_Token'] = array('key' => $key);
  876. $this->Form->create('Addresses');
  877. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  878. $this->Form->input('Addresses.0.title');
  879. $this->Form->input('Addresses.0.first_name');
  880. $this->Form->input('Addresses.0.last_name');
  881. $this->Form->input('Addresses.0.address');
  882. $this->Form->input('Addresses.0.city');
  883. $this->Form->input('Addresses.0.phone');
  884. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  885. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  886. $this->Form->input('Addresses.1.title');
  887. $this->Form->input('Addresses.1.first_name');
  888. $this->Form->input('Addresses.1.last_name');
  889. $this->Form->input('Addresses.1.address');
  890. $this->Form->input('Addresses.1.city');
  891. $this->Form->input('Addresses.1.phone');
  892. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  893. $result = $this->Form->secure($this->Form->fields);
  894. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  895. $expected = array(
  896. 'div' => array('style' => 'display:none;'),
  897. array('input' => array(
  898. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  899. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  900. )),
  901. array('input' => array(
  902. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  903. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  904. )),
  905. '/div'
  906. );
  907. $this->assertTags($result, $expected);
  908. }
  909. /**
  910. * Test form security with Model.field.0 style inputs
  911. *
  912. * @return void
  913. */
  914. public function testFormSecurityArrayFields() {
  915. $key = 'testKey';
  916. $this->Form->request->params['_Token']['key'] = $key;
  917. $this->Form->create('Address');
  918. $this->Form->input('Address.primary.1');
  919. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  920. $this->Form->input('Address.secondary.1.0');
  921. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  922. }
  923. /**
  924. * testFormSecurityMultipleInputDisabledFields method
  925. *
  926. * test secure form generation with multiple records and disabled fields.
  927. *
  928. * @return void
  929. */
  930. public function testFormSecurityMultipleInputDisabledFields() {
  931. $key = 'testKey';
  932. $this->Form->request->params['_Token'] = array(
  933. 'key' => $key,
  934. 'unlockedFields' => array('first_name', 'address')
  935. );
  936. $this->Form->create();
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $result = $this->Form->secure($this->Form->fields);
  952. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  953. $expected = array(
  954. 'div' => array('style' => 'display:none;'),
  955. array('input' => array(
  956. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  957. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  958. )),
  959. array('input' => array(
  960. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  961. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  962. )),
  963. '/div'
  964. );
  965. $this->assertTags($result, $expected);
  966. }
  967. /**
  968. * testFormSecurityInputDisabledFields method
  969. *
  970. * Test single record form with disabled fields.
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityInputUnlockedFields() {
  975. $key = 'testKey';
  976. $this->Form->request['_Token'] = array(
  977. 'key' => $key,
  978. 'unlockedFields' => array('first_name', 'address')
  979. );
  980. $this->Form->create();
  981. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  982. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  983. $this->Form->input('Addresses.title');
  984. $this->Form->input('Addresses.first_name');
  985. $this->Form->input('Addresses.last_name');
  986. $this->Form->input('Addresses.address');
  987. $this->Form->input('Addresses.city');
  988. $this->Form->input('Addresses.phone');
  989. $result = $this->Form->fields;
  990. $expected = array(
  991. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  992. 'Addresses.city', 'Addresses.phone'
  993. );
  994. $this->assertEquals($expected, $result);
  995. $result = $this->Form->secure($expected);
  996. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  997. $expected = array(
  998. 'div' => array('style' => 'display:none;'),
  999. array('input' => array(
  1000. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1001. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1002. )),
  1003. array('input' => array(
  1004. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1005. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1006. )),
  1007. '/div'
  1008. );
  1009. $this->assertTags($result, $expected);
  1010. }
  1011. /**
  1012. * test securing inputs with custom name attributes.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testFormSecureWithCustomNameAttribute() {
  1017. $this->Form->request->params['_Token']['key'] = 'testKey';
  1018. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1019. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1020. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1021. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1022. }
  1023. /**
  1024. * testFormSecuredInput method
  1025. *
  1026. * Test generation of entire secure form, assertions made on input() output.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testFormSecuredInput() {
  1031. $this->Form->request['_Token'] = array('key' => 'testKey');
  1032. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1033. $encoding = strtolower(Configure::read('App.encoding'));
  1034. $expected = array(
  1035. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1036. 'div' => array('style' => 'display:none;'),
  1037. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1038. array('input' => array(
  1039. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1040. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormPublished'),
  1049. 'Published',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1053. 'id' => 'UserFormPublished'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1059. $expected = array(
  1060. 'div' => array('class' => 'input text'),
  1061. 'label' => array('for' => 'UserFormOther'),
  1062. 'Other',
  1063. '/label',
  1064. array('input' => array(
  1065. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1066. 'id' => 'UserFormOther'
  1067. )),
  1068. '/div'
  1069. );
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->hidden('UserForm.stuff');
  1072. $expected = array('input' => array(
  1073. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1074. 'id' => 'UserFormStuff'
  1075. ));
  1076. $this->assertTags($result, $expected);
  1077. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1078. $expected = array('input' => array(
  1079. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1080. 'value' => '0', 'id' => 'UserFormHidden'
  1081. ));
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1084. $expected = array(
  1085. 'div' => array('class' => 'input checkbox'),
  1086. array('input' => array(
  1087. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1088. 'value' => '0', 'id' => 'UserFormSomething_'
  1089. )),
  1090. array('input' => array(
  1091. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1092. 'value' => '1', 'id' => 'UserFormSomething'
  1093. )),
  1094. 'label' => array('for' => 'UserFormSomething'),
  1095. 'Something',
  1096. '/label',
  1097. '/div'
  1098. );
  1099. $this->assertTags($result, $expected);
  1100. $result = $this->Form->fields;
  1101. $expected = array(
  1102. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1103. 'UserForm.hidden' => '0', 'UserForm.something'
  1104. );
  1105. $this->assertEquals($expected, $result);
  1106. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1107. $result = $this->Form->secure($this->Form->fields);
  1108. $expected = array(
  1109. 'div' => array('style' => 'display:none;'),
  1110. array('input' => array(
  1111. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1112. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1113. )),
  1114. array('input' => array(
  1115. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1116. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1117. )),
  1118. '/div'
  1119. );
  1120. $this->assertTags($result, $expected);
  1121. }
  1122. /**
  1123. * Tests that the correct keys are added to the field hash index
  1124. *
  1125. * @return void
  1126. */
  1127. public function testFormSecuredFileInput() {
  1128. $this->Form->request['_Token'] = array('key' => 'testKey');
  1129. $this->assertEquals(array(), $this->Form->fields);
  1130. $this->Form->file('Attachment.file');
  1131. $expected = array(
  1132. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1133. 'Attachment.file.error', 'Attachment.file.size'
  1134. );
  1135. $this->assertEquals($expected, $this->Form->fields);
  1136. }
  1137. /**
  1138. * test that multiple selects keys are added to field hash
  1139. *
  1140. * @return void
  1141. */
  1142. public function testFormSecuredMultipleSelect() {
  1143. $this->Form->request['_Token'] = array('key' => 'testKey');
  1144. $this->assertEquals(array(), $this->Form->fields);
  1145. $options = array('1' => 'one', '2' => 'two');
  1146. $this->Form->select('Model.select', $options);
  1147. $expected = array('Model.select');
  1148. $this->assertEquals($expected, $this->Form->fields);
  1149. $this->Form->fields = array();
  1150. $this->Form->select('Model.select', $options, array('multiple' => true));
  1151. $this->assertEquals($expected, $this->Form->fields);
  1152. }
  1153. /**
  1154. * testFormSecuredRadio method
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFormSecuredRadio() {
  1159. $this->Form->request['_Token'] = array('key' => 'testKey');
  1160. $this->assertEquals(array(), $this->Form->fields);
  1161. $options = array('1' => 'option1', '2' => 'option2');
  1162. $this->Form->radio('Test.test', $options);
  1163. $expected = array('Test.test');
  1164. $this->assertEquals($expected, $this->Form->fields);
  1165. }
  1166. /**
  1167. * Test that when disabled is in a list based attribute array it works.
  1168. *
  1169. * @return void
  1170. */
  1171. public function testFormSecuredAndDisabledNotAssoc() {
  1172. $this->Form->request['_Token'] = array('key' => 'testKey');
  1173. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1174. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1175. $this->Form->text('Model.text', array('disabled'));
  1176. $this->Form->textarea('Model.textarea', array('disabled'));
  1177. $this->Form->password('Model.password', array('disabled'));
  1178. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1179. $expected = array(
  1180. 'Model.radio' => ''
  1181. );
  1182. $this->assertEquals($expected, $this->Form->fields);
  1183. }
  1184. /**
  1185. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1186. * hashing.
  1187. *
  1188. * @return void
  1189. */
  1190. public function testFormSecuredAndDisabled() {
  1191. $this->Form->request['_Token'] = array('key' => 'testKey');
  1192. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1193. $this->Form->text('Model.text', array('disabled' => true));
  1194. $this->Form->password('Model.text', array('disabled' => true));
  1195. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1196. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1197. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1198. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1199. $this->Form->month('Model.month', array('disabled' => true));
  1200. $this->Form->day('Model.day', array('disabled' => true));
  1201. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1202. $this->Form->minute('Model.minute', array('disabled' => true));
  1203. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1204. $expected = array(
  1205. 'Model.radio' => ''
  1206. );
  1207. $this->assertEquals($expected, $this->Form->fields);
  1208. }
  1209. /**
  1210. * testDisableSecurityUsingForm method
  1211. *
  1212. * @return void
  1213. */
  1214. public function testDisableSecurityUsingForm() {
  1215. $this->Form->request['_Token'] = array(
  1216. 'key' => 'testKey',
  1217. 'disabledFields' => array()
  1218. );
  1219. $this->Form->create();
  1220. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1221. $this->Form->input('Addresses.title');
  1222. $this->Form->input('Addresses.first_name', array('secure' => false));
  1223. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1224. $this->Form->input('Addresses.zip', array(
  1225. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1226. ));
  1227. $result = $this->Form->fields;
  1228. $expected = array(
  1229. 'Addresses.id' => '123456', 'Addresses.title',
  1230. );
  1231. $this->assertEquals($expected, $result);
  1232. }
  1233. /**
  1234. * test disableField
  1235. *
  1236. * @return void
  1237. */
  1238. public function testUnlockFieldAddsToList() {
  1239. $this->Form->request['_Token'] = array(
  1240. 'key' => 'testKey',
  1241. 'unlockedFields' => array()
  1242. );
  1243. $this->Form->create('Contact');
  1244. $this->Form->unlockField('Contact.name');
  1245. $this->Form->text('Contact.name');
  1246. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1247. $this->assertEquals(array(), $this->Form->fields);
  1248. }
  1249. /**
  1250. * test unlockField removing from fields array.
  1251. *
  1252. * @return void
  1253. */
  1254. public function testUnlockFieldRemovingFromFields() {
  1255. $this->Form->request['_Token'] = array(
  1256. 'key' => 'testKey',
  1257. 'unlockedFields' => array()
  1258. );
  1259. $this->Form->create('Contact');
  1260. $this->Form->hidden('Contact.id', array('value' => 1));
  1261. $this->Form->text('Contact.name');
  1262. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1263. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1264. $this->Form->unlockField('Contact.name');
  1265. $this->Form->unlockField('Contact.id');
  1266. $this->assertEquals(array(), $this->Form->fields);
  1267. }
  1268. /**
  1269. * testTagIsInvalid method
  1270. *
  1271. * @return void
  1272. */
  1273. public function testTagIsInvalid() {
  1274. $Contact = ClassRegistry::getObject('Contact');
  1275. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1276. $this->Form->setEntity('Contact.0.email');
  1277. $result = $this->Form->tagIsInvalid();
  1278. $expected = array('Please provide an email');
  1279. $this->assertEquals($expected, $result);
  1280. $this->Form->setEntity('Contact.1.email');
  1281. $result = $this->Form->tagIsInvalid();
  1282. $expected = false;
  1283. $this->assertSame($expected, $result);
  1284. $this->Form->setEntity('Contact.0.name');
  1285. $result = $this->Form->tagIsInvalid();
  1286. $expected = false;
  1287. $this->assertSame($expected, $result);
  1288. }
  1289. /**
  1290. * Test validation errors.
  1291. *
  1292. * @return void
  1293. */
  1294. public function testPasswordValidation() {
  1295. $Contact = ClassRegistry::getObject('Contact');
  1296. $Contact->validationErrors['password'] = array('Please provide a password');
  1297. $result = $this->Form->input('Contact.password');
  1298. $expected = array(
  1299. 'div' => array('class' => 'input password error'),
  1300. 'label' => array('for' => 'ContactPassword'),
  1301. 'Password',
  1302. '/label',
  1303. 'input' => array(
  1304. 'type' => 'password', 'name' => 'data[Contact][password]',
  1305. 'id' => 'ContactPassword', 'class' => 'form-error'
  1306. ),
  1307. array('div' => array('class' => 'error-message')),
  1308. 'Please provide a password',
  1309. '/div',
  1310. '/div'
  1311. );
  1312. $this->assertTags($result, $expected);
  1313. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1314. $expected = array(
  1315. 'div' => array('class' => 'input password error'),
  1316. 'label' => array('for' => 'ContactPassword'),
  1317. 'Password',
  1318. '/label',
  1319. 'input' => array(
  1320. 'type' => 'password', 'name' => 'data[Contact][password]',
  1321. 'id' => 'ContactPassword', 'class' => 'form-error'
  1322. ),
  1323. '/div'
  1324. );
  1325. $this->assertTags($result, $expected);
  1326. }
  1327. /**
  1328. * Test validation errors, when validation message is an empty string.
  1329. *
  1330. * @access public
  1331. * @return void
  1332. */
  1333. public function testEmptyErrorValidation() {
  1334. $this->Form->validationErrors['Contact']['password'] = '';
  1335. $result = $this->Form->input('Contact.password');
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. array('div' => array('class' => 'error-message')),
  1346. array(),
  1347. '/div',
  1348. '/div'
  1349. );
  1350. $this->assertTags($result, $expected);
  1351. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1352. $expected = array(
  1353. 'div' => array('class' => 'input password error'),
  1354. 'label' => array('for' => 'ContactPassword'),
  1355. 'Password',
  1356. '/label',
  1357. 'input' => array(
  1358. 'type' => 'password', 'name' => 'data[Contact][password]',
  1359. 'id' => 'ContactPassword', 'class' => 'form-error'
  1360. ),
  1361. '/div'
  1362. );
  1363. $this->assertTags($result, $expected);
  1364. }
  1365. /**
  1366. * Test validation errors, when calling input() overriding validation message by an empty string.
  1367. *
  1368. * @access public
  1369. * @return void
  1370. */
  1371. public function testEmptyInputErrorValidation() {
  1372. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1373. $result = $this->Form->input('Contact.password', array('error' => ''));
  1374. $expected = array(
  1375. 'div' => array('class' => 'input password error'),
  1376. 'label' => array('for' => 'ContactPassword'),
  1377. 'Password',
  1378. '/label',
  1379. 'input' => array(
  1380. 'type' => 'password', 'name' => 'data[Contact][password]',
  1381. 'id' => 'ContactPassword', 'class' => 'form-error'
  1382. ),
  1383. array('div' => array('class' => 'error-message')),
  1384. array(),
  1385. '/div',
  1386. '/div'
  1387. );
  1388. $this->assertTags($result, $expected);
  1389. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1390. $expected = array(
  1391. 'div' => array('class' => 'input password error'),
  1392. 'label' => array('for' => 'ContactPassword'),
  1393. 'Password',
  1394. '/label',
  1395. 'input' => array(
  1396. 'type' => 'password', 'name' => 'data[Contact][password]',
  1397. 'id' => 'ContactPassword', 'class' => 'form-error'
  1398. ),
  1399. '/div'
  1400. );
  1401. $this->assertTags($result, $expected);
  1402. }
  1403. /**
  1404. * testFormValidationAssociated method
  1405. *
  1406. * test display of form errors in conjunction with model::validates.
  1407. *
  1408. * @return void
  1409. */
  1410. public function testFormValidationAssociated() {
  1411. $this->UserForm = ClassRegistry::getObject('UserForm');
  1412. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1413. $data = array(
  1414. 'UserForm' => array('name' => 'user'),
  1415. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1416. );
  1417. $result = $this->UserForm->OpenidUrl->create($data);
  1418. $this->assertFalse(empty($result));
  1419. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1420. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1421. $encoding = strtolower(Configure::read('App.encoding'));
  1422. $expected = array(
  1423. 'form' => array(
  1424. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1425. 'accept-charset' => $encoding
  1426. ),
  1427. 'div' => array('style' => 'display:none;'),
  1428. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1429. '/div'
  1430. );
  1431. $this->assertTags($result, $expected);
  1432. $result = $this->Form->error(
  1433. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1434. );
  1435. $this->assertEquals('Error, not registered', $result);
  1436. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1437. }
  1438. /**
  1439. * testFormValidationAssociatedFirstLevel method
  1440. *
  1441. * test form error display with associated model.
  1442. *
  1443. * @return void
  1444. */
  1445. public function testFormValidationAssociatedFirstLevel() {
  1446. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1447. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1448. $data = array(
  1449. 'ValidateUser' => array('name' => 'mariano'),
  1450. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1451. );
  1452. $result = $this->ValidateUser->create($data);
  1453. $this->assertFalse(empty($result));
  1454. $this->assertFalse($this->ValidateUser->validates());
  1455. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1456. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1457. $encoding = strtolower(Configure::read('App.encoding'));
  1458. $expected = array(
  1459. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1460. 'div' => array('style' => 'display:none;'),
  1461. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1462. '/div'
  1463. );
  1464. $this->assertTags($result, $expected);
  1465. $result = $this->Form->error(
  1466. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1467. );
  1468. $this->assertEquals('Invalid email', $result);
  1469. $result = $this->Form->error(
  1470. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1471. );
  1472. $this->assertEquals('Invalid name', $result);
  1473. $result = $this->Form->error(
  1474. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1475. );
  1476. $this->assertEquals('Invalid city', $result);
  1477. unset($this->ValidateUser->ValidateProfile);
  1478. unset($this->ValidateUser);
  1479. }
  1480. /**
  1481. * testFormValidationAssociatedSecondLevel method
  1482. *
  1483. * test form error display with associated model.
  1484. *
  1485. * @return void
  1486. */
  1487. public function testFormValidationAssociatedSecondLevel() {
  1488. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1489. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1490. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1491. $data = array(
  1492. 'ValidateUser' => array('name' => 'mariano'),
  1493. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1494. 'ValidateItem' => array('name' => 'Item')
  1495. );
  1496. $result = $this->ValidateUser->create($data);
  1497. $this->assertFalse(empty($result));
  1498. $this->assertFalse($this->ValidateUser->validates());
  1499. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1500. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1501. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1502. $encoding = strtolower(Configure::read('App.encoding'));
  1503. $expected = array(
  1504. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1505. 'div' => array('style' => 'display:none;'),
  1506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1507. '/div'
  1508. );
  1509. $this->assertTags($result, $expected);
  1510. $result = $this->Form->error(
  1511. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1512. );
  1513. $this->assertEquals('Invalid email', $result);
  1514. $result = $this->Form->error(
  1515. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1516. );
  1517. $this->assertEquals('Invalid name', $result);
  1518. $result = $this->Form->error(
  1519. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1520. );
  1521. $result = $this->Form->error(
  1522. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1523. );
  1524. $this->assertEquals('Invalid description', $result);
  1525. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1526. unset($this->ValidateUser->ValidateProfile);
  1527. unset($this->ValidateUser);
  1528. }
  1529. /**
  1530. * testFormValidationMultiRecord method
  1531. *
  1532. * test form error display with multiple records.
  1533. *
  1534. * @return void
  1535. */
  1536. public function testFormValidationMultiRecord() {
  1537. $Contact = ClassRegistry::getObject('Contact');
  1538. $Contact->validationErrors[2] = array(
  1539. 'name' => array('This field cannot be left blank')
  1540. );
  1541. $result = $this->Form->input('Contact.2.name');
  1542. $expected = array(
  1543. 'div' => array('class' => 'input text error'),
  1544. 'label' => array('for' => 'Contact2Name'),
  1545. 'Name',
  1546. '/label',
  1547. 'input' => array(
  1548. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1549. 'class' => 'form-error', 'maxlength' => 255
  1550. ),
  1551. array('div' => array('class' => 'error-message')),
  1552. 'This field cannot be left blank',
  1553. '/div',
  1554. '/div'
  1555. );
  1556. $this->assertTags($result, $expected);
  1557. }
  1558. /**
  1559. * testMultipleInputValidation method
  1560. *
  1561. * test multiple record form validation error display.
  1562. *
  1563. * @return void
  1564. */
  1565. public function testMultipleInputValidation() {
  1566. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1567. $Address->validationErrors[0] = array(
  1568. 'title' => array('This field cannot be empty'),
  1569. 'first_name' => array('This field cannot be empty')
  1570. );
  1571. $Address->validationErrors[1] = array(
  1572. 'last_name' => array('You must have a last name')
  1573. );
  1574. $this->Form->create();
  1575. $result = $this->Form->input('Address.0.title');
  1576. $expected = array(
  1577. 'div' => array('class'),
  1578. 'label' => array('for'),
  1579. 'preg:/[^<]+/',
  1580. '/label',
  1581. 'input' => array(
  1582. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1583. ),
  1584. array('div' => array('class' => 'error-message')),
  1585. 'This field cannot be empty',
  1586. '/div',
  1587. '/div'
  1588. );
  1589. $this->assertTags($result, $expected);
  1590. $result = $this->Form->input('Address.0.first_name');
  1591. $expected = array(
  1592. 'div' => array('class'),
  1593. 'label' => array('for'),
  1594. 'preg:/[^<]+/',
  1595. '/label',
  1596. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1597. array('div' => array('class' => 'error-message')),
  1598. 'This field cannot be empty',
  1599. '/div',
  1600. '/div'
  1601. );
  1602. $this->assertTags($result, $expected);
  1603. $result = $this->Form->input('Address.0.last_name');
  1604. $expected = array(
  1605. 'div' => array('class'),
  1606. 'label' => array('for'),
  1607. 'preg:/[^<]+/',
  1608. '/label',
  1609. 'input' => array('type' => 'text', 'name', 'id'),
  1610. '/div'
  1611. );
  1612. $this->assertTags($result, $expected);
  1613. $result = $this->Form->input('Address.1.last_name');
  1614. $expected = array(
  1615. 'div' => array('class'),
  1616. 'label' => array('for'),
  1617. 'preg:/[^<]+/',
  1618. '/label',
  1619. 'input' => array(
  1620. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1621. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1622. ),
  1623. array('div' => array('class' => 'error-message')),
  1624. 'You must have a last name',
  1625. '/div',
  1626. '/div'
  1627. );
  1628. $this->assertTags($result, $expected);
  1629. }
  1630. /**
  1631. * testInput method
  1632. *
  1633. * Test various incarnations of input().
  1634. *
  1635. * @return void
  1636. */
  1637. public function testInput() {
  1638. $result = $this->Form->input('ValidateUser.balance');
  1639. $expected = array(
  1640. 'div' => array('class'),
  1641. 'label' => array('for'),
  1642. 'Balance',
  1643. '/label',
  1644. 'input' => array('name', 'type' => 'number', 'id'),
  1645. '/div',
  1646. );
  1647. $this->assertTags($result, $expected);
  1648. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1649. $expected = array(
  1650. 'div' => array('class' => 'input email'),
  1651. 'label' => array('for' => 'custom'),
  1652. 'Email',
  1653. '/label',
  1654. array('input' => array(
  1655. 'type' => 'email', 'name' => 'data[Contact][email]',
  1656. 'id' => 'custom', 'maxlength' => 255
  1657. )),
  1658. '/div'
  1659. );
  1660. $this->assertTags($result, $expected);
  1661. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1662. $expected = array(
  1663. '<div',
  1664. 'label' => array('for' => 'ContactEmail'),
  1665. 'Email',
  1666. '/label',
  1667. array('input' => array(
  1668. 'type' => 'email', 'name' => 'data[Contact][email]',
  1669. 'id' => 'ContactEmail', 'maxlength' => 255
  1670. )),
  1671. '/div'
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->hidden('Contact.idontexist');
  1675. $expected = array('input' => array(
  1676. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1677. 'id' => 'ContactIdontexist'
  1678. ));
  1679. $this->assertTags($result, $expected);
  1680. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1681. $expected = array(
  1682. 'div' => array('class' => 'input text'),
  1683. 'label' => array('for' => 'ContactEmail'),
  1684. 'Email',
  1685. '/label',
  1686. array('input' => array(
  1687. 'type' => 'text', 'name' => 'data[Contact][email]',
  1688. 'id' => 'ContactEmail'
  1689. )),
  1690. '/div'
  1691. );
  1692. $this->assertTags($result, $expected);
  1693. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1694. $expected = array(
  1695. 'div' => array('class' => 'input text'),
  1696. 'label' => array('for' => 'Contact5Email'),
  1697. 'Email',
  1698. '/label',
  1699. array('input' => array(
  1700. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1701. 'id' => 'Contact5Email'
  1702. )),
  1703. '/div'
  1704. );
  1705. $this->assertTags($result, $expected);
  1706. $result = $this->Form->input('Contact.password');
  1707. $expected = array(
  1708. 'div' => array('class' => 'input password'),
  1709. 'label' => array('for' => 'ContactPassword'),
  1710. 'Password',
  1711. '/label',
  1712. array('input' => array(
  1713. 'type' => 'password', 'name' => 'data[Contact][password]',
  1714. 'id' => 'ContactPassword'
  1715. )),
  1716. '/div'
  1717. );
  1718. $this->assertTags($result, $expected);
  1719. $result = $this->Form->input('Contact.email', array(
  1720. 'type' => 'file', 'class' => 'textbox'
  1721. ));
  1722. $expected = array(
  1723. 'div' => array('class' => 'input file'),
  1724. 'label' => array('for' => 'ContactEmail'),
  1725. 'Email',
  1726. '/label',
  1727. array('input' => array(
  1728. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1729. 'id' => 'ContactEmail'
  1730. )),
  1731. '/div'
  1732. );
  1733. $this->assertTags($result, $expected);
  1734. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1735. $result = $this->Form->input('Contact.phone');
  1736. $expected = array(
  1737. 'div' => array('class' => 'input tel'),
  1738. 'label' => array('for' => 'ContactPhone'),
  1739. 'Phone',
  1740. '/label',
  1741. array('input' => array(
  1742. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1743. 'value' => 'Hello &amp; World &gt; weird chars',
  1744. 'id' => 'ContactPhone', 'maxlength' => 255
  1745. )),
  1746. '/div'
  1747. );
  1748. $this->assertTags($result, $expected);
  1749. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1750. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1751. $expected = array(
  1752. 'div' => array('class' => 'input text'),
  1753. 'label' => array('for' => 'myId'),
  1754. 'Field',
  1755. '/label',
  1756. 'input' => array(
  1757. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1758. 'value' => 'My value', 'id' => 'myId'
  1759. ),
  1760. '/div'
  1761. );
  1762. $this->assertTags($result, $expected);
  1763. unset($this->Form->request->data);
  1764. $Contact = ClassRegistry::getObject('Contact');
  1765. $Contact->validationErrors['field'] = array('Badness!');
  1766. $result = $this->Form->input('Contact.field');
  1767. $expected = array(
  1768. 'div' => array('class' => 'input text error'),
  1769. 'label' => array('for' => 'ContactField'),
  1770. 'Field',
  1771. '/label',
  1772. 'input' => array(
  1773. 'type' => 'text', 'name' => 'data[Contact][field]',
  1774. 'id' => 'ContactField', 'class' => 'form-error'
  1775. ),
  1776. array('div' => array('class' => 'error-message')),
  1777. 'Badness!',
  1778. '/div',
  1779. '/div'
  1780. );
  1781. $this->assertTags($result, $expected);
  1782. $result = $this->Form->input('Contact.field', array(
  1783. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1784. ));
  1785. $expected = array(
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. array('span' => array('class' => 'error-message')),
  1794. 'Badness!',
  1795. '/span'
  1796. );
  1797. $this->assertTags($result, $expected);
  1798. $result = $this->Form->input('Contact.field', array(
  1799. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1800. ));
  1801. $expected = array(
  1802. 'div' => array('class' => 'input text error'),
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('div' => array('class' => 'error')),
  1811. 'Badness!',
  1812. '/div'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1817. ));
  1818. $expected = array(
  1819. 'span' => array('class' => 'input text error'),
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. 'Badness!',
  1828. '/span'
  1829. );
  1830. $this->assertTags($result, $expected);
  1831. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1832. $expected = array(
  1833. 'div' => array('class' => 'input text error'),
  1834. 'label' => array('for' => 'ContactField'),
  1835. 'Field',
  1836. '/label',
  1837. 'input' => array(
  1838. 'type' => 'text', 'name' => 'data[Contact][field]',
  1839. 'id' => 'ContactField', 'class' => 'form-error'
  1840. ),
  1841. 'A message to you, Rudy',
  1842. array('div' => array('class' => 'error-message')),
  1843. 'Badness!',
  1844. '/div',
  1845. '/div'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $this->Form->setEntity(null);
  1849. $this->Form->setEntity('Contact.field');
  1850. $result = $this->Form->input('Contact.field', array(
  1851. 'after' => 'A message to you, Rudy', 'error' => false
  1852. ));
  1853. $expected = array(
  1854. 'div' => array('class' => 'input text'),
  1855. 'label' => array('for' => 'ContactField'),
  1856. 'Field',
  1857. '/label',
  1858. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1859. 'A message to you, Rudy',
  1860. '/div'
  1861. );
  1862. $this->assertTags($result, $expected);
  1863. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1864. $expected = array(
  1865. 'div' => array('class' => 'input text'),
  1866. 'label' => array('for' => 'ObjectField'),
  1867. 'Field',
  1868. '/label',
  1869. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1870. 'A message to you, Rudy',
  1871. '/div'
  1872. );
  1873. $this->assertTags($result, $expected);
  1874. $Contact->validationErrors['field'] = array('minLength');
  1875. $result = $this->Form->input('Contact.field', array(
  1876. 'error' => array(
  1877. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1878. 'maxLength' => 'login too large'
  1879. )
  1880. ));
  1881. $expected = array(
  1882. 'div' => array('class' => 'input text error'),
  1883. 'label' => array('for' => 'ContactField'),
  1884. 'Field',
  1885. '/label',
  1886. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1887. array('div' => array('class' => 'error-message')),
  1888. 'Le login doit contenir au moins 2 caractères',
  1889. '/div',
  1890. '/div'
  1891. );
  1892. $this->assertTags($result, $expected);
  1893. $Contact->validationErrors['field'] = array('maxLength');
  1894. $result = $this->Form->input('Contact.field', array(
  1895. 'error' => array(
  1896. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1897. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1898. 'maxLength' => 'login too large',
  1899. )
  1900. ));
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1907. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1908. 'login too large',
  1909. '/span',
  1910. '/div'
  1911. );
  1912. $this->assertTags($result, $expected);
  1913. }
  1914. /**
  1915. * Test that inputs with 0 can be created.
  1916. *
  1917. * @return void
  1918. */
  1919. public function testInputZero() {
  1920. $this->Form->create('User');
  1921. $result = $this->Form->input('0');
  1922. $expected = array(
  1923. 'div' => array('class' => 'input text'),
  1924. 'label' => array('for' => 'User0'), '/label',
  1925. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1926. '/div'
  1927. );
  1928. $this->assertTags($result, $expected);
  1929. }
  1930. /**
  1931. * test input() with checkbox creation
  1932. *
  1933. * @return void
  1934. */
  1935. public function testInputCheckbox() {
  1936. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input checkbox'),
  1939. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1940. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1945. $expected = array(
  1946. 'div' => array('class' => 'input checkbox'),
  1947. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1948. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1953. $expected = array(
  1954. 'div' => array('class' => 'input checkbox'),
  1955. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1956. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1957. '/div'
  1958. );
  1959. $this->assertTags($result, $expected);
  1960. }
  1961. /**
  1962. * test form->input() with time types.
  1963. *
  1964. */
  1965. public function testInputTime() {
  1966. extract($this->dateRegex);
  1967. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1968. $result = explode(':', $result);
  1969. $this->assertRegExp('/option value="23"/', $result[0]);
  1970. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1971. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1972. $result = explode(':', $result);
  1973. $this->assertRegExp('/option value="23"/', $result[0]);
  1974. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1975. $result = $this->Form->input('Model.field', array(
  1976. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1977. ));
  1978. $result = explode(':', $result);
  1979. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1980. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1981. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1982. $result = $this->Form->input('Model.field', array(
  1983. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1984. ));
  1985. $result = explode(':', $result);
  1986. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1987. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1988. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1989. $result = $this->Form->input('prueba', array(
  1990. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1991. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1992. ));
  1993. $result = explode(':', $result);
  1994. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1995. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1996. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1997. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1998. $result = $this->Form->input('Random.start_time', array(
  1999. 'type' => 'time',
  2000. 'selected' => '18:15'
  2001. ));
  2002. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2003. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2004. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2005. $result = $this->Form->input('published', array('type' => 'time'));
  2006. $now = strtotime('now');
  2007. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2008. $now = strtotime('2013-03-09 00:42:21');
  2009. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2010. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2011. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2012. }
  2013. /**
  2014. * Test interval + selected near the hour roll over.
  2015. *
  2016. * @return void
  2017. */
  2018. public function testTimeSelectedWithInterval() {
  2019. $result = $this->Form->input('Model.start_time', array(
  2020. 'type' => 'time',
  2021. 'interval' => 15,
  2022. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2023. ));
  2024. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2025. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2026. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2027. $result = $this->Form->input('Model.start_time', array(
  2028. 'type' => 'time',
  2029. 'interval' => 15,
  2030. 'selected' => '2012-10-23 15:57:00'
  2031. ));
  2032. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2033. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2034. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2035. $result = $this->Form->input('Model.start_time', array(
  2036. 'timeFormat' => 24,
  2037. 'type' => 'time',
  2038. 'interval' => 15,
  2039. 'selected' => '15:57'
  2040. ));
  2041. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2042. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2043. $result = $this->Form->input('Model.start_time', array(
  2044. 'timeFormat' => 24,
  2045. 'type' => 'time',
  2046. 'interval' => 15,
  2047. 'selected' => '23:57'
  2048. ));
  2049. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2050. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2051. $result = $this->Form->input('Model.created', array(
  2052. 'timeFormat' => 24,
  2053. 'type' => 'datetime',
  2054. 'interval' => 15,
  2055. 'selected' => '2012-09-30 23:56'
  2056. ));
  2057. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2058. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2059. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2061. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2062. }
  2063. /**
  2064. * Test interval & timeFormat = 12
  2065. *
  2066. * @return void
  2067. */
  2068. public function testInputTimeWithIntervalAnd12HourFormat() {
  2069. $result = $this->Form->input('Model.start_time', array(
  2070. 'type' => 'time',
  2071. 'timeFormat' => 12,
  2072. 'interval' => 5,
  2073. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'type' => 'time',
  2080. 'timeFormat' => '12',
  2081. 'interval' => 5,
  2082. 'selected' => '2013-04-19 16:30:00'
  2083. ));
  2084. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2085. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2087. }
  2088. /**
  2089. * test form->input() with datetime, date and time types
  2090. *
  2091. * @return void
  2092. */
  2093. public function testInputDatetime() {
  2094. extract($this->dateRegex);
  2095. $result = $this->Form->input('prueba', array(
  2096. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2097. 'maxYear' => date('Y') + 1, 'interval' => 15
  2098. ));
  2099. $result = explode(':', $result);
  2100. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2101. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2102. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2103. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2104. //related to ticket #5013
  2105. $result = $this->Form->input('Contact.date', array(
  2106. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2107. ));
  2108. $this->assertRegExp('/class="customClass"/', $result);
  2109. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2110. $result = $this->Form->input('Contact.date', array(
  2111. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2112. ));
  2113. $this->assertRegExp('/id="customIdDay"/', $result);
  2114. $this->assertRegExp('/id="customIdMonth"/', $result);
  2115. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2116. $result = $this->Form->input('Model.field', array(
  2117. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2118. ));
  2119. $this->assertRegExp('/id="customIDDay"/', $result);
  2120. $this->assertRegExp('/id="customIDHour"/', $result);
  2121. $result = explode('</select><select', $result);
  2122. $result = explode(':', $result[1]);
  2123. $this->assertRegExp('/option value="23"/', $result[0]);
  2124. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2125. $result = $this->Form->input('Model.field', array(
  2126. 'type' => 'datetime', 'timeFormat' => 12
  2127. ));
  2128. $result = explode('</select><select', $result);
  2129. $result = explode(':', $result[1]);
  2130. $this->assertRegExp('/option value="12"/', $result[0]);
  2131. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2132. $this->Form->request->data = array('Contact' => array('created' => null));
  2133. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2134. $expected = array(
  2135. 'div' => array('class' => 'input date'),
  2136. 'label' => array('for' => 'ContactCreatedMonth'),
  2137. 'Created',
  2138. '/label',
  2139. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2140. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2141. $monthsRegex,
  2142. '/select', '-',
  2143. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2144. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2145. $daysRegex,
  2146. '/select', '-',
  2147. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2148. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2149. $yearsRegex,
  2150. '/select',
  2151. '/div'
  2152. );
  2153. $this->assertTags($result, $expected);
  2154. $this->Form->request->data = array('Contact' => array('created' => null));
  2155. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2156. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2157. $this->Form->request->data = array('Contact' => array('created' => null));
  2158. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2159. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2160. $result = $this->Form->input('Contact.created', array(
  2161. 'type' => 'date',
  2162. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2163. 'timeFormat' => 'NONE'
  2164. ));
  2165. $this->assertRegExp('/for\="created-month"/', $result);
  2166. }
  2167. /**
  2168. * Test generating checkboxes in a loop.
  2169. *
  2170. * @return void
  2171. */
  2172. public function testInputCheckboxesInLoop() {
  2173. for ($i = 1; $i < 5; $i++) {
  2174. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2175. $expected = array(
  2176. 'div' => array('class' => 'input checkbox'),
  2177. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2178. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2179. 'label' => array('for' => "Contact{$i}Email"),
  2180. 'Email',
  2181. '/label',
  2182. '/div'
  2183. );
  2184. $this->assertTags($result, $expected);
  2185. }
  2186. }
  2187. /**
  2188. * Test generating checkboxes with disabled elements.
  2189. *
  2190. * @return void
  2191. */
  2192. public function testInputCheckboxWithDisabledElements() {
  2193. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2194. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2195. $expected = array(
  2196. array('div' => array('class' => 'input select')),
  2197. array('label' => array('for' => "ContactMultiple")),
  2198. 'Multiple',
  2199. '/label',
  2200. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2201. array('div' => array('class' => 'checkbox')),
  2202. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2203. array('label' => array('for' => "ContactMultiple1")),
  2204. 'One',
  2205. '/label',
  2206. '/div',
  2207. array('div' => array('class' => 'checkbox')),
  2208. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2209. array('label' => array('for' => "ContactMultiple2")),
  2210. 'Two',
  2211. '/label',
  2212. '/div',
  2213. array('div' => array('class' => 'checkbox')),
  2214. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2215. array('label' => array('for' => "ContactMultiple3")),
  2216. 'Three',
  2217. '/label',
  2218. '/div',
  2219. '/div'
  2220. );
  2221. $this->assertTags($result, $expected);
  2222. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2223. $this->assertTags($result, $expected);
  2224. $disabled = array('2', 3);
  2225. $expected = array(
  2226. array('div' => array('class' => 'input select')),
  2227. array('label' => array('for' => "ContactMultiple")),
  2228. 'Multiple',
  2229. '/label',
  2230. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2231. array('div' => array('class' => 'checkbox')),
  2232. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2233. array('label' => array('for' => "ContactMultiple1")),
  2234. 'One',
  2235. '/label',
  2236. '/div',
  2237. array('div' => array('class' => 'checkbox')),
  2238. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2239. array('label' => array('for' => "ContactMultiple2")),
  2240. 'Two',
  2241. '/label',
  2242. '/div',
  2243. array('div' => array('class' => 'checkbox')),
  2244. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2245. array('label' => array('for' => "ContactMultiple3")),
  2246. 'Three',
  2247. '/label',
  2248. '/div',
  2249. '/div'
  2250. );
  2251. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2252. $this->assertTags($result, $expected);
  2253. // make sure 50 does only disable 50, and not 50f5c0cf
  2254. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2255. $disabled = array(50);
  2256. $expected = array(
  2257. array('div' => array('class' => 'input select')),
  2258. array('label' => array('for' => "ContactMultiple")),
  2259. 'Multiple',
  2260. '/label',
  2261. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2262. array('div' => array('class' => 'checkbox')),
  2263. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2264. array('label' => array('for' => "ContactMultiple50")),
  2265. 'Fifty',
  2266. '/label',
  2267. '/div',
  2268. array('div' => array('class' => 'checkbox')),
  2269. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2270. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2271. 'Stringy',
  2272. '/label',
  2273. '/div',
  2274. '/div'
  2275. );
  2276. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2277. $this->assertTags($result, $expected);
  2278. }
  2279. /**
  2280. * test input name with leading integer, ensure attributes are generated correctly.
  2281. *
  2282. * @return void
  2283. */
  2284. public function testInputWithLeadingInteger() {
  2285. $result = $this->Form->text('0.Node.title');
  2286. $expected = array(
  2287. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2288. );
  2289. $this->assertTags($result, $expected);
  2290. }
  2291. /**
  2292. * test form->input() with select type inputs.
  2293. *
  2294. * @return void
  2295. */
  2296. public function testInputSelectType() {
  2297. $result = $this->Form->input('email', array(
  2298. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2299. );
  2300. $expected = array(
  2301. 'div' => array('class' => 'input select'),
  2302. 'label' => array('for' => 'email'),
  2303. 'Email',
  2304. '/label',
  2305. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2306. array('option' => array('value' => '')),
  2307. '/option',
  2308. array('option' => array('value' => 'è')),
  2309. 'Firést',
  2310. '/option',
  2311. array('option' => array('value' => 'é')),
  2312. 'Secoènd',
  2313. '/option',
  2314. '/select',
  2315. '/div'
  2316. );
  2317. $this->assertTags($result, $expected);
  2318. $result = $this->Form->input('email', array(
  2319. 'options' => array('First', 'Second'), 'empty' => true)
  2320. );
  2321. $expected = array(
  2322. 'div' => array('class' => 'input select'),
  2323. 'label' => array('for' => 'email'),
  2324. 'Email',
  2325. '/label',
  2326. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2327. array('option' => array('value' => '')),
  2328. '/option',
  2329. array('option' => array('value' => '0')),
  2330. 'First',
  2331. '/option',
  2332. array('option' => array('value' => '1')),
  2333. 'Second',
  2334. '/option',
  2335. '/select',
  2336. '/div'
  2337. );
  2338. $this->assertTags($result, $expected);
  2339. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2340. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2341. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2342. $expected = array(
  2343. 'div' => array('class' => 'input select'),
  2344. 'label' => array('for' => 'ModelUserId'),
  2345. 'User',
  2346. '/label',
  2347. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2348. array('option' => array('value' => '')),
  2349. '/option',
  2350. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2351. 'good',
  2352. '/option',
  2353. array('option' => array('value' => 'other')),
  2354. 'bad',
  2355. '/option',
  2356. '/select',
  2357. '/div'
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2361. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2362. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2363. $expected = array(
  2364. 'div' => array('class' => 'input select'),
  2365. 'label' => array('for' => 'ThingUserId'),
  2366. 'User',
  2367. '/label',
  2368. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2369. array('option' => array('value' => '')),
  2370. 'Some Empty',
  2371. '/option',
  2372. array('option' => array('value' => 'value')),
  2373. 'good',
  2374. '/option',
  2375. array('option' => array('value' => 'other')),
  2376. 'bad',
  2377. '/option',
  2378. '/select',
  2379. '/div'
  2380. );
  2381. $this->assertTags($result, $expected);
  2382. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2383. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2384. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2385. $expected = array(
  2386. 'div' => array('class' => 'input select'),
  2387. 'label' => array('for' => 'ThingUserId'),
  2388. 'User',
  2389. '/label',
  2390. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2391. array('option' => array('value' => '')),
  2392. 'Some Empty',
  2393. '/option',
  2394. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2395. 'good',
  2396. '/option',
  2397. array('option' => array('value' => 'other')),
  2398. 'bad',
  2399. '/option',
  2400. '/select',
  2401. '/div'
  2402. );
  2403. $this->assertTags($result, $expected);
  2404. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2405. $this->Form->request->data = array('User' => array('User' => array('value')));
  2406. $result = $this->Form->input('User.User', array('empty' => true));
  2407. $expected = array(
  2408. 'div' => array('class' => 'input select'),
  2409. 'label' => array('for' => 'UserUser'),
  2410. 'User',
  2411. '/label',
  2412. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2413. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2414. array('option' => array('value' => '')),
  2415. '/option',
  2416. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2417. 'good',
  2418. '/option',
  2419. array('option' => array('value' => 'other')),
  2420. 'bad',
  2421. '/option',
  2422. '/select',
  2423. '/div'
  2424. );
  2425. $this->assertTags($result, $expected);
  2426. $this->Form->data = array();
  2427. $result = $this->Form->input('Publisher.id', array(
  2428. 'label' => 'Publisher',
  2429. 'type' => 'select',
  2430. 'multiple' => 'checkbox',
  2431. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2432. ));
  2433. $expected = array(
  2434. array('div' => array('class' => 'input select')),
  2435. array('label' => array('for' => 'PublisherId')),
  2436. 'Publisher',
  2437. '/label',
  2438. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2439. array('div' => array('class' => 'checkbox')),
  2440. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2441. array('label' => array('for' => 'PublisherIdValue1')),
  2442. 'Label 1',
  2443. '/label',
  2444. '/div',
  2445. array('div' => array('class' => 'checkbox')),
  2446. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2447. array('label' => array('for' => 'PublisherIdValue2')),
  2448. 'Label 2',
  2449. '/label',
  2450. '/div',
  2451. '/div'
  2452. );
  2453. $this->assertTags($result, $expected);
  2454. }
  2455. /**
  2456. * test that input() and a non standard primary key makes a hidden input by default.
  2457. *
  2458. * @return void
  2459. */
  2460. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2461. $this->Form->create('User');
  2462. $this->Form->fieldset = array(
  2463. 'User' => array(
  2464. 'fields' => array(
  2465. 'model_id' => array('type' => 'integer')
  2466. ),
  2467. 'validates' => array(),
  2468. 'key' => 'model_id'
  2469. )
  2470. );
  2471. $result = $this->Form->input('model_id');
  2472. $expected = array(
  2473. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2474. );
  2475. $this->assertTags($result, $expected);
  2476. }
  2477. /**
  2478. * test that overriding the magic select type widget is possible
  2479. *
  2480. * @return void
  2481. */
  2482. public function testInputOverridingMagicSelectType() {
  2483. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2484. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2485. $expected = array(
  2486. 'div' => array('class' => 'input text'),
  2487. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2488. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2489. '/div'
  2490. );
  2491. $this->assertTags($result, $expected);
  2492. //Check that magic types still work for plural/singular vars
  2493. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2494. $result = $this->Form->input('Model.type');
  2495. $expected = array(
  2496. 'div' => array('class' => 'input select'),
  2497. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2498. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2499. array('option' => array('value' => 'value')), 'good', '/option',
  2500. array('option' => array('value' => 'other')), 'bad', '/option',
  2501. '/select',
  2502. '/div'
  2503. );
  2504. $this->assertTags($result, $expected);
  2505. }
  2506. /**
  2507. * Test that magic input() selects are created for type=number
  2508. *
  2509. * @return void
  2510. */
  2511. public function testInputMagicSelectForTypeNumber() {
  2512. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2513. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2514. $result = $this->Form->input('ValidateUser.balance');
  2515. $expected = array(
  2516. 'div' => array('class' => 'input select'),
  2517. 'label' => array('for' => 'ValidateUserBalance'),
  2518. 'Balance',
  2519. '/label',
  2520. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2521. array('option' => array('value' => '0')),
  2522. 'nothing',
  2523. '/option',
  2524. array('option' => array('value' => '1', 'selected' => 'selected')),
  2525. 'some',
  2526. '/option',
  2527. array('option' => array('value' => '100')),
  2528. 'a lot',
  2529. '/option',
  2530. '/select',
  2531. '/div'
  2532. );
  2533. $this->assertTags($result, $expected);
  2534. }
  2535. /**
  2536. * Test that magic input() selects can easily be converted into radio types without error.
  2537. *
  2538. * @return void
  2539. */
  2540. public function testInputMagicSelectChangeToRadio() {
  2541. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2542. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2543. $this->assertRegExp('/input type="radio"/', $result);
  2544. }
  2545. /**
  2546. * fields with the same name as the model should work.
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputWithMatchingFieldAndModelName() {
  2551. $this->Form->create('User');
  2552. $this->Form->fieldset = array(
  2553. 'User' => array(
  2554. 'fields' => array(
  2555. 'User' => array('type' => 'text')
  2556. ),
  2557. 'validates' => array(),
  2558. 'key' => 'id'
  2559. )
  2560. );
  2561. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2562. $result = $this->Form->input('User', array('type' => 'text'));
  2563. $expected = array(
  2564. 'div' => array('class' => 'input text'),
  2565. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2566. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2567. '/div'
  2568. );
  2569. $this->assertTags($result, $expected);
  2570. }
  2571. /**
  2572. * testFormInputs method
  2573. *
  2574. * test correct results from form::inputs().
  2575. *
  2576. * @return void
  2577. */
  2578. public function testFormInputs() {
  2579. $this->Form->create('Contact');
  2580. $result = $this->Form->inputs('The Legend');
  2581. $expected = array(
  2582. '<fieldset',
  2583. '<legend',
  2584. 'The Legend',
  2585. '/legend',
  2586. '*/fieldset',
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2590. $expected = array(
  2591. 'fieldset' => array('class' => 'classy-stuff'),
  2592. '<legend',
  2593. 'Field of Dreams',
  2594. '/legend',
  2595. '*/fieldset'
  2596. );
  2597. $this->assertTags($result, $expected);
  2598. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2599. $this->assertTags($result, $expected);
  2600. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2601. $this->assertTags($result, $expected);
  2602. $this->Form->create('Contact');
  2603. $this->Form->request['prefix'] = 'admin';
  2604. $this->Form->request['action'] = 'admin_edit';
  2605. $result = $this->Form->inputs();
  2606. $expected = array(
  2607. '<fieldset',
  2608. '<legend',
  2609. 'Edit Contact',
  2610. '/legend',
  2611. '*/fieldset',
  2612. );
  2613. $this->assertTags($result, $expected);
  2614. $this->Form->create('Contact');
  2615. $result = $this->Form->inputs(false);
  2616. $expected = array(
  2617. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2618. array('div' => array('class' => 'input text')),
  2619. '*/div',
  2620. array('div' => array('class' => 'input email')),
  2621. '*/div',
  2622. array('div' => array('class' => 'input tel')),
  2623. '*/div',
  2624. array('div' => array('class' => 'input password')),
  2625. '*/div',
  2626. array('div' => array('class' => 'input date')),
  2627. '*/div',
  2628. array('div' => array('class' => 'input date')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input datetime')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input number')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input select')),
  2635. '*/div',
  2636. );
  2637. $this->assertTags($result, $expected);
  2638. $this->Form->create('Contact');
  2639. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2640. $expected = array(
  2641. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2642. array('div' => array('class' => 'input text')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input email')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input tel')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input password')),
  2649. '*/div',
  2650. array('div' => array('class' => 'input date')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input date')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input datetime')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input number')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input select')),
  2659. '*/div',
  2660. );
  2661. $this->assertTags($result, $expected);
  2662. $this->Form->create('Contact');
  2663. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2664. $this->assertTags($result, $expected);
  2665. $this->Form->create('Contact');
  2666. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2667. $expected = array(
  2668. 'fieldset' => array(),
  2669. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2670. array('div' => array('class' => 'input text')),
  2671. '*/div',
  2672. array('div' => array('class' => 'input email')),
  2673. '*/div',
  2674. array('div' => array('class' => 'input tel')),
  2675. '*/div',
  2676. array('div' => array('class' => 'input password')),
  2677. '*/div',
  2678. array('div' => array('class' => 'input date')),
  2679. '*/div',
  2680. array('div' => array('class' => 'input date')),
  2681. '*/div',
  2682. array('div' => array('class' => 'input datetime')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input number')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input select')),
  2687. '*/div',
  2688. '/fieldset'
  2689. );
  2690. $this->assertTags($result, $expected);
  2691. $this->Form->create('Contact');
  2692. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2693. $expected = array(
  2694. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2695. array('div' => array('class' => 'input text')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input email')),
  2698. '*/div',
  2699. array('div' => array('class' => 'input tel')),
  2700. '*/div',
  2701. array('div' => array('class' => 'input password')),
  2702. '*/div',
  2703. array('div' => array('class' => 'input date')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input date')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input datetime')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input number')),
  2710. '*/div',
  2711. array('div' => array('class' => 'input select')),
  2712. '*/div',
  2713. );
  2714. $this->assertTags($result, $expected);
  2715. $this->Form->create('Contact');
  2716. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2717. $this->assertTags($result, $expected);
  2718. $this->Form->create('Contact');
  2719. $result = $this->Form->inputs('Hello');
  2720. $expected = array(
  2721. 'fieldset' => array(),
  2722. 'legend' => array(),
  2723. 'Hello',
  2724. '/legend',
  2725. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2726. array('div' => array('class' => 'input text')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input email')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input tel')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input password')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input date')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input date')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input datetime')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input number')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input select')),
  2743. '*/div',
  2744. '/fieldset'
  2745. );
  2746. $this->assertTags($result, $expected);
  2747. $this->Form->create('Contact');
  2748. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2749. $expected = array(
  2750. 'fieldset' => array(),
  2751. 'legend' => array(),
  2752. 'Hello',
  2753. '/legend',
  2754. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2755. array('div' => array('class' => 'input text')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input email')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input tel')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input password')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input date')),
  2764. '*/div',
  2765. array('div' => array('class' => 'input date')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input datetime')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input number')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input select')),
  2772. '*/div',
  2773. '/fieldset'
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. $this->Form->create('Contact');
  2777. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2778. $this->assertTags($result, $expected);
  2779. $this->Form->end();
  2780. $this->Form->create(false);
  2781. $expected = array(
  2782. 'fieldset' => array(),
  2783. array('div' => array('class' => 'input text')),
  2784. 'label' => array('for' => 'foo'),
  2785. 'Foo',
  2786. '/label',
  2787. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2788. '*/div',
  2789. '/fieldset'
  2790. );
  2791. $result = $this->Form->inputs(
  2792. array('foo' => array('type' => 'text')),
  2793. array(),
  2794. array('legend' => false)
  2795. );
  2796. $this->assertTags($result, $expected);
  2797. }
  2798. /**
  2799. * Tests inputs() works with plugin models
  2800. *
  2801. * @return void
  2802. */
  2803. public function testInputsPluginModel() {
  2804. $this->loadFixtures('Post');
  2805. App::build(array(
  2806. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2807. ));
  2808. CakePlugin::load('TestPlugin');
  2809. $this->Form->request['models'] = array(
  2810. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2811. );
  2812. $this->Form->create('TestPlugin.TestPluginPost');
  2813. $result = $this->Form->inputs();
  2814. $this->assertContains('data[TestPluginPost][id]', $result);
  2815. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2816. $this->assertContains('data[TestPluginPost][title]', $result);
  2817. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2818. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2819. $this->assertEquals('TestPluginPost', $this->Form->model());
  2820. }
  2821. /**
  2822. * testSelectAsCheckbox method
  2823. *
  2824. * test multi-select widget with checkbox formatting.
  2825. *
  2826. * @return void
  2827. */
  2828. public function testSelectAsCheckbox() {
  2829. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2830. $expected = array(
  2831. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2832. array('div' => array('class' => 'checkbox')),
  2833. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2834. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2835. 'first',
  2836. '/label',
  2837. '/div',
  2838. array('div' => array('class' => 'checkbox')),
  2839. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2840. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2841. 'second',
  2842. '/label',
  2843. '/div',
  2844. array('div' => array('class' => 'checkbox')),
  2845. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2846. array('label' => array('for' => 'ModelMultiField2')),
  2847. 'third',
  2848. '/label',
  2849. '/div',
  2850. );
  2851. $this->assertTags($result, $expected);
  2852. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2853. $expected = array(
  2854. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2855. array('div' => array('class' => 'checkbox')),
  2856. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2857. array('label' => array('for' => 'ModelMultiField12')),
  2858. 'half',
  2859. '/label',
  2860. '/div',
  2861. );
  2862. $this->assertTags($result, $expected);
  2863. }
  2864. /**
  2865. * testLabel method
  2866. *
  2867. * test label generation.
  2868. *
  2869. * @return void
  2870. */
  2871. public function testLabel() {
  2872. $this->Form->text('Person.name');
  2873. $result = $this->Form->label();
  2874. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2875. $this->Form->text('Person.name');
  2876. $result = $this->Form->label();
  2877. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2878. $result = $this->Form->label('Person.first_name');
  2879. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2880. $result = $this->Form->label('Person.first_name', 'Your first name');
  2881. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2882. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2883. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2884. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2885. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2886. $result = $this->Form->label('Person.first_name', '');
  2887. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2888. $result = $this->Form->label('Person.2.name', '');
  2889. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2890. }
  2891. /**
  2892. * testTextbox method
  2893. *
  2894. * test textbox element generation
  2895. *
  2896. * @return void
  2897. */
  2898. public function testTextbox() {
  2899. $result = $this->Form->text('Model.field');
  2900. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2901. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2902. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2903. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2904. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2905. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2906. $result = $this->Form->text('Model.text');
  2907. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2908. $Contact = ClassRegistry::getObject('Contact');
  2909. $Contact->validationErrors['text'] = array(true);
  2910. $this->Form->request->data['Contact']['text'] = 'test';
  2911. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2912. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2913. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2914. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2915. $expected = array(
  2916. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2917. );
  2918. $this->assertTags($result, $expected);
  2919. }
  2920. /**
  2921. * testDefaultValue method
  2922. *
  2923. * Test default value setting
  2924. *
  2925. * @return void
  2926. */
  2927. public function testDefaultValue() {
  2928. $this->Form->request->data['Model']['field'] = 'test';
  2929. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2930. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2931. unset($this->Form->request->data['Model']['field']);
  2932. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2933. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2934. }
  2935. /**
  2936. * testCheckboxDefaultValue method
  2937. *
  2938. * Test default value setting on checkbox() method
  2939. *
  2940. * @return void
  2941. */
  2942. public function testCheckboxDefaultValue() {
  2943. $this->Form->request->data['Model']['field'] = false;
  2944. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2945. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2946. unset($this->Form->request->data['Model']['field']);
  2947. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2948. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2949. $this->Form->request->data['Model']['field'] = true;
  2950. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2951. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2952. unset($this->Form->request->data['Model']['field']);
  2953. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2954. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2955. }
  2956. /**
  2957. * testError method
  2958. *
  2959. * Test field error generation
  2960. *
  2961. * @return void
  2962. */
  2963. public function testError() {
  2964. $Contact = ClassRegistry::getObject('Contact');
  2965. $Contact->validationErrors['field'] = array(1);
  2966. $result = $this->Form->error('Contact.field');
  2967. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2968. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2969. $this->assertEquals('Error in field Field', $result);
  2970. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2971. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2972. $this->assertEquals('This field contains invalid input', $result);
  2973. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2974. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2975. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2976. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2977. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2978. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2979. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2980. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2981. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2982. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2983. $this->assertEquals('<strong>Badness!</strong>', $result);
  2984. $Contact->validationErrors['field'] = array("email");
  2985. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2986. $expected = array(
  2987. 'div' => array('class' => 'field-error'),
  2988. 'No good!',
  2989. '/div'
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2993. $result = $this->Form->error('Contact.field', array(
  2994. 'notEmpty' => 'Cannot be empty',
  2995. 'email' => 'No good!'
  2996. ));
  2997. $expected = array(
  2998. 'div' => array('class' => 'error-message'),
  2999. 'ul' => array(),
  3000. '<li', 'Cannot be empty', '/li',
  3001. '<li', 'No good!', '/li',
  3002. '<li', 'Something else', '/li',
  3003. '/ul',
  3004. '/div'
  3005. );
  3006. $this->assertTags($result, $expected);
  3007. // Testing error messages list options
  3008. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3009. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3010. $expected = array(
  3011. 'div' => array('class' => 'error-message'),
  3012. 'ol' => array(),
  3013. '<li', 'notEmpty', '/li',
  3014. '<li', 'email', '/li',
  3015. '/ol',
  3016. '/div'
  3017. );
  3018. $this->assertTags($result, $expected);
  3019. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3020. $expected = array(
  3021. 'div' => array('class' => 'error-message'),
  3022. 'ol' => array(),
  3023. '<li', 'notEmpty', '/li',
  3024. '<li', 'email', '/li',
  3025. '/ol',
  3026. '/div'
  3027. );
  3028. $this->assertTags($result, $expected);
  3029. $result = $this->Form->error('Contact.field', null, array(
  3030. 'listOptions' => array(
  3031. 'class' => 'ul-class',
  3032. 'itemOptions' => array(
  3033. 'class' => 'li-class'
  3034. )
  3035. )
  3036. ));
  3037. $expected = array(
  3038. 'div' => array('class' => 'error-message'),
  3039. 'ul' => array('class' => 'ul-class'),
  3040. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3041. array('li' => array('class' => 'li-class')), 'email', '/li',
  3042. '/ul',
  3043. '/div'
  3044. );
  3045. $this->assertTags($result, $expected);
  3046. }
  3047. /**
  3048. * test error options when using form->input();
  3049. *
  3050. * @return void
  3051. */
  3052. public function testInputErrorEscape() {
  3053. $this->Form->create('ValidateProfile');
  3054. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3055. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3056. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3057. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3058. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3059. $this->assertRegExp('/required<br>/', $result);
  3060. }
  3061. /**
  3062. * testPassword method
  3063. *
  3064. * Test password element generation
  3065. *
  3066. * @return void
  3067. */
  3068. public function testPassword() {
  3069. $Contact = ClassRegistry::getObject('Contact');
  3070. $result = $this->Form->password('Contact.field');
  3071. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3072. $Contact->validationErrors['passwd'] = 1;
  3073. $this->Form->request->data['Contact']['passwd'] = 'test';
  3074. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3075. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3076. }
  3077. /**
  3078. * testRadio method
  3079. *
  3080. * Test radio element set generation
  3081. *
  3082. * @return void
  3083. */
  3084. public function testRadio() {
  3085. $result = $this->Form->radio('Model.field', array('option A'));
  3086. $expected = array(
  3087. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3088. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3089. 'label' => array('for' => 'ModelField0'),
  3090. 'option A',
  3091. '/label'
  3092. );
  3093. $this->assertTags($result, $expected);
  3094. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3095. $expected = array(
  3096. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3098. 'label' => array('for' => 'ModelField12'),
  3099. 'half',
  3100. '/label'
  3101. );
  3102. $this->assertTags($result, $expected);
  3103. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3104. $expected = array(
  3105. 'fieldset' => array(),
  3106. 'legend' => array(),
  3107. 'Field',
  3108. '/legend',
  3109. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3110. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3111. array('label' => array('for' => 'ModelField0')),
  3112. 'option A',
  3113. '/label',
  3114. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3115. array('label' => array('for' => 'ModelField1')),
  3116. 'option B',
  3117. '/label',
  3118. '/fieldset'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3122. $expected = array(
  3123. 'fieldset' => array(),
  3124. 'legend' => array(),
  3125. 'Field',
  3126. '/legend',
  3127. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3128. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3129. array('label' => array('for' => 'ModelField0')),
  3130. 'option A',
  3131. '/label',
  3132. 'br' => array(),
  3133. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3134. array('label' => array('for' => 'ModelField1')),
  3135. 'option B',
  3136. '/label',
  3137. '/fieldset'
  3138. );
  3139. $this->assertTags($result, $expected);
  3140. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3141. $expected = array(
  3142. 'fieldset' => array(),
  3143. 'legend' => array(),
  3144. 'Gender',
  3145. '/legend',
  3146. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3147. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3148. array('label' => array('for' => 'EmployeeGenderMale')),
  3149. 'Male',
  3150. '/label',
  3151. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3152. array('label' => array('for' => 'EmployeeGenderFemale')),
  3153. 'Female',
  3154. '/label',
  3155. '/fieldset',
  3156. );
  3157. $this->assertTags($result, $expected);
  3158. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3159. $expected = array(
  3160. 'fieldset' => array(),
  3161. 'legend' => array(),
  3162. 'Gender',
  3163. '/legend',
  3164. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3165. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3166. array('label' => array('for' => 'OfficerGenderMale')),
  3167. 'Male',
  3168. '/label',
  3169. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3170. array('label' => array('for' => 'OfficerGenderFemale')),
  3171. 'Female',
  3172. '/label',
  3173. '/fieldset',
  3174. );
  3175. $this->assertTags($result, $expected);
  3176. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3177. $expected = array(
  3178. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3179. array('input' => array(
  3180. 'type' => 'radio',
  3181. 'name' => 'data[Contact][1][imrequired]',
  3182. 'value' => '0',
  3183. 'id' => 'Contact1Imrequired0',
  3184. 'required' => 'required'
  3185. )),
  3186. 'label' => array('for' => 'Contact1Imrequired0'),
  3187. 'option A',
  3188. '/label'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.1.field', array('option A'));
  3192. $expected = array(
  3193. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3194. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3195. 'label' => array('for' => 'Model1Field0'),
  3196. 'option A',
  3197. '/label'
  3198. );
  3199. $this->assertTags($result, $expected);
  3200. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3201. $expected = array(
  3202. 'fieldset' => array(),
  3203. 'legend' => array(),
  3204. 'Field',
  3205. '/legend',
  3206. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3207. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3208. array('label' => array('for' => 'ModelField0')),
  3209. 'option A',
  3210. '/label',
  3211. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3212. array('label' => array('for' => 'ModelField1')),
  3213. 'option B',
  3214. '/label',
  3215. '/fieldset'
  3216. );
  3217. $this->assertTags($result, $expected);
  3218. }
  3219. /**
  3220. * Test radio inputs with between as string or array. Also ensure
  3221. * that an array with less between elements works.
  3222. *
  3223. * @return void
  3224. */
  3225. public function testRadioBetween() {
  3226. $result = $this->Form->radio(
  3227. 'Model.field',
  3228. array('option A', 'option B'),
  3229. array('between' => 'I am between')
  3230. );
  3231. $expected = array(
  3232. 'fieldset' => array(),
  3233. 'legend' => array(),
  3234. 'Field',
  3235. '/legend',
  3236. 'I am between',
  3237. 'input' => array(
  3238. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3239. 'value' => '', 'id' => 'ModelField_'
  3240. ),
  3241. array('input' => array(
  3242. 'type' => 'radio', 'name' => 'data[Model][field]',
  3243. 'value' => '0', 'id' => 'ModelField0'
  3244. )),
  3245. array('label' => array('for' => 'ModelField0')),
  3246. 'option A',
  3247. '/label',
  3248. array('input' => array(
  3249. 'type' => 'radio', 'name' => 'data[Model][field]',
  3250. 'value' => '1', 'id' => 'ModelField1'
  3251. )),
  3252. array('label' => array('for' => 'ModelField1')),
  3253. 'option B',
  3254. '/label',
  3255. '/fieldset'
  3256. );
  3257. $this->assertTags($result, $expected);
  3258. $result = $this->Form->radio(
  3259. 'Model.field',
  3260. array('option A', 'option B', 'option C'),
  3261. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3262. );
  3263. $expected = array(
  3264. 'fieldset' => array(),
  3265. 'legend' => array(),
  3266. 'Field',
  3267. '/legend',
  3268. 'input' => array(
  3269. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3270. 'value' => '', 'id' => 'ModelField_'
  3271. ),
  3272. array('input' => array(
  3273. 'type' => 'radio', 'name' => 'data[Model][field]',
  3274. 'value' => '0', 'id' => 'ModelField0'
  3275. )),
  3276. array('label' => array('for' => 'ModelField0')),
  3277. 'option A',
  3278. '/label',
  3279. 'between A',
  3280. '--separator--',
  3281. array('input' => array(
  3282. 'type' => 'radio', 'name' => 'data[Model][field]',
  3283. 'value' => '1', 'id' => 'ModelField1'
  3284. )),
  3285. array('label' => array('for' => 'ModelField1')),
  3286. 'option B',
  3287. '/label',
  3288. 'between B',
  3289. '--separator--',
  3290. array('input' => array(
  3291. 'type' => 'radio', 'name' => 'data[Model][field]',
  3292. 'value' => '2', 'id' => 'ModelField2'
  3293. )),
  3294. array('label' => array('for' => 'ModelField2')),
  3295. 'option C',
  3296. '/label',
  3297. 'between C',
  3298. '/fieldset'
  3299. );
  3300. $this->assertTags($result, $expected);
  3301. $result = $this->Form->input('Model.field', array(
  3302. 'options' => array('1' => 'first', '2' => 'second'),
  3303. 'type' => 'radio',
  3304. 'before' => '--before--',
  3305. 'after' => '--after--',
  3306. 'separator' => '--separator--',
  3307. 'between' => array('--between first--', '--between second--')
  3308. ));
  3309. $expected = array(
  3310. 'div' => array('class' => 'input radio'),
  3311. '--before--',
  3312. 'fieldset' => array(),
  3313. 'legend' => array(),
  3314. 'Field',
  3315. '/legend',
  3316. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3317. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3318. array('label' => array('for' => 'ModelField1')),
  3319. 'first',
  3320. '/label',
  3321. '--between first--',
  3322. '--separator--',
  3323. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3324. array('label' => array('for' => 'ModelField2')),
  3325. 'second',
  3326. '/label',
  3327. '--between second--',
  3328. '/fieldset',
  3329. '--after--',
  3330. '/div'
  3331. );
  3332. $this->assertTags($result, $expected);
  3333. $result = $this->Form->input('Model.field', array(
  3334. 'options' => array('1' => 'first', '2' => 'second'),
  3335. 'type' => 'radio',
  3336. 'before' => '--before--',
  3337. 'after' => '--after--',
  3338. 'separator' => '--separator--',
  3339. 'between' => array('--between first--')
  3340. ));
  3341. $expected = array(
  3342. 'div' => array('class' => 'input radio'),
  3343. '--before--',
  3344. 'fieldset' => array(),
  3345. 'legend' => array(),
  3346. 'Field',
  3347. '/legend',
  3348. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3349. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3350. array('label' => array('for' => 'ModelField1')),
  3351. 'first',
  3352. '/label',
  3353. '--between first--',
  3354. '--separator--',
  3355. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3356. array('label' => array('for' => 'ModelField2')),
  3357. 'second',
  3358. '/label',
  3359. '/fieldset',
  3360. '--after--',
  3361. '/div'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. }
  3365. /**
  3366. * Test that radios with a 0 value are selected under the correct conditions.
  3367. * Also ensure that values that are booleanish are handled correctly.
  3368. *
  3369. * @return void
  3370. */
  3371. public function testRadioOptionWithBooleanishValues() {
  3372. $expected = array(
  3373. 'fieldset' => array(),
  3374. 'legend' => array(),
  3375. 'Field',
  3376. '/legend',
  3377. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3378. array('label' => array('for' => 'ModelField1')),
  3379. 'Yes',
  3380. '/label',
  3381. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3382. array('label' => array('for' => 'ModelField0')),
  3383. 'No',
  3384. '/label',
  3385. '/fieldset'
  3386. );
  3387. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3388. $this->assertTags($result, $expected);
  3389. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3390. $this->assertTags($result, $expected);
  3391. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3392. $this->assertTags($result, $expected);
  3393. $expected = array(
  3394. 'fieldset' => array(),
  3395. 'legend' => array(),
  3396. 'Field',
  3397. '/legend',
  3398. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3399. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3400. array('label' => array('for' => 'ModelField1')),
  3401. 'Yes',
  3402. '/label',
  3403. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3404. array('label' => array('for' => 'ModelField0')),
  3405. 'No',
  3406. '/label',
  3407. '/fieldset'
  3408. );
  3409. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3410. $this->assertTags($result, $expected);
  3411. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3412. $this->assertTags($result, $expected);
  3413. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3414. $this->assertTags($result, $expected);
  3415. $expected = array(
  3416. 'fieldset' => array(),
  3417. 'legend' => array(),
  3418. 'Field',
  3419. '/legend',
  3420. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3421. array('label' => array('for' => 'ModelField1')),
  3422. 'Yes',
  3423. '/label',
  3424. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3425. array('label' => array('for' => 'ModelField0')),
  3426. 'No',
  3427. '/label',
  3428. '/fieldset'
  3429. );
  3430. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3431. $this->assertTags($result, $expected);
  3432. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3433. $this->assertTags($result, $expected);
  3434. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3435. $this->assertTags($result, $expected);
  3436. }
  3437. /**
  3438. * test disabled radio options
  3439. *
  3440. * @return void
  3441. */
  3442. public function testRadioDisabled() {
  3443. $result = $this->Form->radio(
  3444. 'Model.field',
  3445. array('option A', 'option B'),
  3446. array('disabled' => array(0), 'value' => '0')
  3447. );
  3448. $expected = array(
  3449. 'fieldset' => array(),
  3450. 'legend' => array(),
  3451. 'Field',
  3452. '/legend',
  3453. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3454. array('label' => array('for' => 'ModelField0')),
  3455. 'option A',
  3456. '/label',
  3457. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3458. array('label' => array('for' => 'ModelField1')),
  3459. 'option B',
  3460. '/label',
  3461. '/fieldset'
  3462. );
  3463. $this->assertTags($result, $expected);
  3464. $result = $this->Form->radio(
  3465. 'Model.field',
  3466. array('option A', 'option B'),
  3467. array('disabled' => true, 'value' => '0')
  3468. );
  3469. $expected = array(
  3470. 'fieldset' => array(),
  3471. 'legend' => array(),
  3472. 'Field',
  3473. '/legend',
  3474. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3475. array('label' => array('for' => 'ModelField0')),
  3476. 'option A',
  3477. '/label',
  3478. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3479. array('label' => array('for' => 'ModelField1')),
  3480. 'option B',
  3481. '/label',
  3482. '/fieldset'
  3483. );
  3484. $this->assertTags($result, $expected);
  3485. $result = $this->Form->radio(
  3486. 'Model.field',
  3487. array('option A', 'option B'),
  3488. array('disabled' => 'disabled', 'value' => '0')
  3489. );
  3490. $expected = array(
  3491. 'fieldset' => array(),
  3492. 'legend' => array(),
  3493. 'Field',
  3494. '/legend',
  3495. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3496. array('label' => array('for' => 'ModelField0')),
  3497. 'option A',
  3498. '/label',
  3499. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3500. array('label' => array('for' => 'ModelField1')),
  3501. 'option B',
  3502. '/label',
  3503. '/fieldset'
  3504. );
  3505. $this->assertTags($result, $expected);
  3506. $result = $this->Form->input('Model.field', array(
  3507. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3508. 'type' => 'radio',
  3509. 'disabled' => array(2, '3x'),
  3510. ));
  3511. $expected = array(
  3512. 'div' => array('class' => 'input radio'),
  3513. 'fieldset' => array(),
  3514. 'legend' => array(),
  3515. 'Field',
  3516. '/legend',
  3517. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3518. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3519. array('label' => array('for' => 'ModelField1')),
  3520. 'first',
  3521. '/label',
  3522. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3523. array('label' => array('for' => 'ModelField2')),
  3524. 'second',
  3525. '/label',
  3526. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3527. array('label' => array('for' => 'ModelField2x')),
  3528. '2x',
  3529. '/label',
  3530. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3531. array('label' => array('for' => 'ModelField3')),
  3532. 'third',
  3533. '/label',
  3534. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3535. array('label' => array('for' => 'ModelField3x')),
  3536. '3x',
  3537. '/label',
  3538. '/fieldset',
  3539. '/div'
  3540. );
  3541. $this->assertTags($result, $expected);
  3542. }
  3543. /**
  3544. * test disabling the hidden input for radio buttons
  3545. *
  3546. * @return void
  3547. */
  3548. public function testRadioHiddenInputDisabling() {
  3549. $result = $this->Form->input('Model.1.field', array(
  3550. 'type' => 'radio',
  3551. 'options' => array('option A'),
  3552. 'hiddenField' => false
  3553. )
  3554. );
  3555. $expected = array(
  3556. 'div' => array('class' => 'input radio'),
  3557. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3558. 'label' => array('for' => 'Model1Field0'),
  3559. 'option A',
  3560. '/label',
  3561. '/div'
  3562. );
  3563. $this->assertTags($result, $expected);
  3564. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3565. $expected = array(
  3566. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3567. 'label' => array('for' => 'Model1Field0'),
  3568. 'option A',
  3569. '/label'
  3570. );
  3571. $this->assertTags($result, $expected);
  3572. }
  3573. /**
  3574. * test adding an empty option for radio buttons
  3575. *
  3576. * @return void
  3577. */
  3578. public function testRadioAddEmptyOption() {
  3579. $result = $this->Form->input('Model.1.field', array(
  3580. 'type' => 'radio',
  3581. 'options' => array('option A'),
  3582. 'empty' => true,
  3583. 'hiddenField' => false
  3584. ));
  3585. $expected = array(
  3586. 'div' => array('class' => 'input radio'),
  3587. 'fieldset' => array(),
  3588. 'legend' => array(),
  3589. 'Field',
  3590. '/legend',
  3591. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3592. array('label' => array('for' => 'Model1Field')),
  3593. __('empty'),
  3594. '/label',
  3595. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3596. array('label' => array('for' => 'Model1Field0')),
  3597. 'option A',
  3598. '/label',
  3599. '/fieldset',
  3600. '/div'
  3601. );
  3602. $this->assertTags($result, $expected);
  3603. $result = $this->Form->input('Model.1.field', array(
  3604. 'type' => 'radio',
  3605. 'options' => array('option A'),
  3606. 'empty' => 'CustomEmptyLabel',
  3607. 'hiddenField' => false
  3608. ));
  3609. $expected = array(
  3610. 'div' => array('class' => 'input radio'),
  3611. 'fieldset' => array(),
  3612. 'legend' => array(),
  3613. 'Field',
  3614. '/legend',
  3615. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3616. array('label' => array('for' => 'Model1Field')),
  3617. 'CustomEmptyLabel',
  3618. '/label',
  3619. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3620. array('label' => array('for' => 'Model1Field0')),
  3621. 'option A',
  3622. '/label',
  3623. '/fieldset',
  3624. '/div'
  3625. );
  3626. $this->assertTags($result, $expected);
  3627. $result = $this->Form->input('Model.1.field', array(
  3628. 'type' => 'radio',
  3629. 'options' => array('option A'),
  3630. 'empty' => false,
  3631. 'hiddenField' => false
  3632. ));
  3633. $this->assertTextNotContains('"Model1Field"', $result);
  3634. }
  3635. /**
  3636. * testSelect method
  3637. *
  3638. * Test select element generation.
  3639. *
  3640. * @return void
  3641. */
  3642. public function testSelect() {
  3643. $result = $this->Form->select('Model.field', array());
  3644. $expected = array(
  3645. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3646. array('option' => array('value' => '')),
  3647. '/option',
  3648. '/select'
  3649. );
  3650. $this->assertTags($result, $expected);
  3651. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3652. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3653. $expected = array(
  3654. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3655. array('option' => array('value' => '')),
  3656. '/option',
  3657. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3658. 'good',
  3659. '/option',
  3660. array('option' => array('value' => 'other')),
  3661. 'bad',
  3662. '/option',
  3663. '/select'
  3664. );
  3665. $this->assertTags($result, $expected);
  3666. $this->Form->request->data = array();
  3667. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3668. $expected = array(
  3669. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3670. array('option' => array('value' => '')),
  3671. '/option',
  3672. array('option' => array('value' => 'value')),
  3673. 'good',
  3674. '/option',
  3675. array('option' => array('value' => 'other')),
  3676. 'bad',
  3677. '/option',
  3678. '/select'
  3679. );
  3680. $this->assertTags($result, $expected);
  3681. $result = $this->Form->select(
  3682. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3683. array('empty' => false)
  3684. );
  3685. $expected = array(
  3686. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3687. array('option' => array('value' => 'first')),
  3688. 'first &quot;html&quot; &lt;chars&gt;',
  3689. '/option',
  3690. array('option' => array('value' => 'second')),
  3691. 'value',
  3692. '/option',
  3693. '/select'
  3694. );
  3695. $this->assertTags($result, $expected);
  3696. $result = $this->Form->select(
  3697. 'Model.field',
  3698. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3699. array('escape' => false, 'empty' => false)
  3700. );
  3701. $expected = array(
  3702. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3703. array('option' => array('value' => 'first')),
  3704. 'first "html" <chars>',
  3705. '/option',
  3706. array('option' => array('value' => 'second')),
  3707. 'value',
  3708. '/option',
  3709. '/select'
  3710. );
  3711. $this->assertTags($result, $expected);
  3712. $options = array(
  3713. array('value' => 'first', 'name' => 'First'),
  3714. array('value' => 'first', 'name' => 'Another First'),
  3715. );
  3716. $result = $this->Form->select(
  3717. 'Model.field',
  3718. $options,
  3719. array('escape' => false, 'empty' => false)
  3720. );
  3721. $expected = array(
  3722. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3723. array('option' => array('value' => 'first')),
  3724. 'First',
  3725. '/option',
  3726. array('option' => array('value' => 'first')),
  3727. 'Another First',
  3728. '/option',
  3729. '/select'
  3730. );
  3731. $this->assertTags($result, $expected);
  3732. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3733. $result = $this->Form->select(
  3734. 'Model.contact_id',
  3735. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3736. array('escape' => false, 'empty' => 'pick something')
  3737. );
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3740. array('option' => array('value' => '')), 'pick something', '/option',
  3741. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3742. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3743. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3744. '/select'
  3745. );
  3746. $this->assertTags($result, $expected);
  3747. $this->Form->request->data['Model']['field'] = 0;
  3748. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3749. $expected = array(
  3750. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3751. array('option' => array('value' => '')), '/option',
  3752. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3753. array('option' => array('value' => '1')), 'Yes', '/option',
  3754. '/select'
  3755. );
  3756. $this->assertTags($result, $expected);
  3757. $this->Form->request->data['Model']['field'] = 50;
  3758. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3759. $expected = array(
  3760. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3761. array('option' => array('value' => '')), '/option',
  3762. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3763. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3764. '/select'
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. }
  3768. /**
  3769. * test that select() with optiongroups listens to the escape param.
  3770. *
  3771. * @return void
  3772. */
  3773. public function testSelectOptionGroupEscaping() {
  3774. $options = array(
  3775. '>< Key' => array(
  3776. 1 => 'One',
  3777. 2 => 'Two'
  3778. )
  3779. );
  3780. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3781. $expected = array(
  3782. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3783. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3784. array('option' => array('value' => '1')), 'One', '/option',
  3785. array('option' => array('value' => '2')), 'Two', '/option',
  3786. '/optgroup',
  3787. '/select'
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. $options = array(
  3791. '>< Key' => array(
  3792. 1 => 'One',
  3793. 2 => 'Two'
  3794. )
  3795. );
  3796. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3797. $expected = array(
  3798. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3799. 'optgroup' => array('label' => '>< Key'),
  3800. array('option' => array('value' => '1')), 'One', '/option',
  3801. array('option' => array('value' => '2')), 'Two', '/option',
  3802. '/optgroup',
  3803. '/select'
  3804. );
  3805. $this->assertTags($result, $expected);
  3806. }
  3807. /**
  3808. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3809. *
  3810. * @return void
  3811. */
  3812. public function testSelectWithNullAttributes() {
  3813. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3814. $expected = array(
  3815. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3816. array('option' => array('value' => '0')),
  3817. 'first',
  3818. '/option',
  3819. array('option' => array('value' => '1')),
  3820. 'second',
  3821. '/option',
  3822. '/select'
  3823. );
  3824. $this->assertTags($result, $expected);
  3825. }
  3826. /**
  3827. * testNestedSelect method
  3828. *
  3829. * test select element generation with optgroups
  3830. *
  3831. * @return void
  3832. */
  3833. public function testNestedSelect() {
  3834. $result = $this->Form->select(
  3835. 'Model.field',
  3836. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3837. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3838. )), array('empty' => false)
  3839. );
  3840. $expected = array(
  3841. 'select' => array('name' => 'data[Model][field]',
  3842. 'id' => 'ModelField'),
  3843. array('option' => array('value' => 1)),
  3844. 'One',
  3845. '/option',
  3846. array('option' => array('value' => 2)),
  3847. 'Two',
  3848. '/option',
  3849. array('optgroup' => array('label' => 'Three')),
  3850. array('option' => array('value' => 4)),
  3851. 'Four',
  3852. '/option',
  3853. array('option' => array('value' => 5)),
  3854. 'Five',
  3855. '/option',
  3856. '/optgroup',
  3857. '/select'
  3858. );
  3859. $this->assertTags($result, $expected);
  3860. $result = $this->Form->select(
  3861. 'Model.field',
  3862. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3863. array('showParents' => true, 'empty' => false)
  3864. );
  3865. $expected = array(
  3866. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3867. array('option' => array('value' => 1)),
  3868. 'One',
  3869. '/option',
  3870. array('option' => array('value' => 2)),
  3871. 'Two',
  3872. '/option',
  3873. array('optgroup' => array('label' => 'Three')),
  3874. array('option' => array('value' => 3)),
  3875. 'Three',
  3876. '/option',
  3877. array('option' => array('value' => 4)),
  3878. 'Four',
  3879. '/option',
  3880. '/optgroup',
  3881. '/select'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. }
  3885. /**
  3886. * testSelectMultiple method
  3887. *
  3888. * test generation of multiple select elements
  3889. *
  3890. * @return void
  3891. */
  3892. public function testSelectMultiple() {
  3893. $options = array('first', 'second', 'third');
  3894. $result = $this->Form->select(
  3895. 'Model.multi_field', $options, array('multiple' => true)
  3896. );
  3897. $expected = array(
  3898. 'input' => array(
  3899. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3900. ),
  3901. 'select' => array(
  3902. 'name' => 'data[Model][multi_field][]',
  3903. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3904. ),
  3905. array('option' => array('value' => '0')),
  3906. 'first',
  3907. '/option',
  3908. array('option' => array('value' => '1')),
  3909. 'second',
  3910. '/option',
  3911. array('option' => array('value' => '2')),
  3912. 'third',
  3913. '/option',
  3914. '/select'
  3915. );
  3916. $this->assertTags($result, $expected);
  3917. $result = $this->Form->select(
  3918. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3919. );
  3920. $expected = array(
  3921. 'input' => array(
  3922. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3923. ),
  3924. 'select' => array(
  3925. 'name' => 'data[Model][multi_field][]',
  3926. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3927. ),
  3928. array('option' => array('value' => '0')),
  3929. 'first',
  3930. '/option',
  3931. array('option' => array('value' => '1')),
  3932. 'second',
  3933. '/option',
  3934. array('option' => array('value' => '2')),
  3935. 'third',
  3936. '/option',
  3937. '/select'
  3938. );
  3939. $this->assertTags($result, $expected);
  3940. $result = $this->Form->select(
  3941. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3942. );
  3943. $expected = array(
  3944. 'input' => array(
  3945. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3946. ),
  3947. 'select' => array(
  3948. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3949. 'multiple' => 'multiple'
  3950. ),
  3951. array('option' => array('value' => '0', 'selected' => 'selected')),
  3952. 'first',
  3953. '/option',
  3954. array('option' => array('value' => '1', 'selected' => 'selected')),
  3955. 'second',
  3956. '/option',
  3957. array('option' => array('value' => '2')),
  3958. 'third',
  3959. '/option',
  3960. '/select'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. $result = $this->Form->select(
  3964. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3965. );
  3966. $expected = array(
  3967. 'select' => array(
  3968. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3969. ),
  3970. array('option' => array('value' => '0', 'selected' => 'selected')),
  3971. 'first',
  3972. '/option',
  3973. array('option' => array('value' => '1', 'selected' => 'selected')),
  3974. 'second',
  3975. '/option',
  3976. array('option' => array('value' => '2')),
  3977. 'third',
  3978. '/option',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  3983. $selected = array('2', '3x');
  3984. $result = $this->Form->select(
  3985. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  3986. );
  3987. $expected = array(
  3988. 'input' => array(
  3989. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3990. ),
  3991. 'select' => array(
  3992. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  3993. ),
  3994. array('option' => array('value' => '1')),
  3995. 'One',
  3996. '/option',
  3997. array('option' => array('value' => '2', 'selected' => 'selected')),
  3998. 'Two',
  3999. '/option',
  4000. array('option' => array('value' => '3')),
  4001. 'Three',
  4002. '/option',
  4003. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4004. 'Stringy',
  4005. '/option',
  4006. '/select'
  4007. );
  4008. $this->assertTags($result, $expected);
  4009. }
  4010. /**
  4011. * Test generating multiple select with disabled elements.
  4012. *
  4013. * @return void
  4014. */
  4015. public function testSelectMultipleWithDisabledElements() {
  4016. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4017. $disabled = array(2, 3);
  4018. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4019. $expected = array(
  4020. 'input' => array(
  4021. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4022. ),
  4023. 'select' => array(
  4024. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4025. ),
  4026. array('option' => array('value' => '1')),
  4027. 'One',
  4028. '/option',
  4029. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4030. 'Two',
  4031. '/option',
  4032. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4033. 'Three',
  4034. '/option',
  4035. array('option' => array('value' => '3x')),
  4036. 'Stringy',
  4037. '/option',
  4038. '/select'
  4039. );
  4040. $this->assertTags($result, $expected);
  4041. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4042. $disabled = array('2', '3x');
  4043. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4044. $expected = array(
  4045. array('div' => array('class' => 'input select')),
  4046. array('label' => array('for' => 'ContactMultiple')),
  4047. 'Multiple',
  4048. '/label',
  4049. 'input' => array(
  4050. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4051. ),
  4052. 'select' => array(
  4053. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4054. ),
  4055. array('option' => array('value' => '1')),
  4056. 'One',
  4057. '/option',
  4058. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4059. 'Two',
  4060. '/option',
  4061. array('option' => array('value' => '3')),
  4062. 'Three',
  4063. '/option',
  4064. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4065. 'Stringy',
  4066. '/option',
  4067. '/select',
  4068. '/div'
  4069. );
  4070. $this->assertTags($result, $expected);
  4071. }
  4072. /**
  4073. * test generation of habtm select boxes.
  4074. *
  4075. * @return void
  4076. */
  4077. public function testHabtmSelectBox() {
  4078. $this->View->viewVars['contactTags'] = array(
  4079. 1 => 'blue',
  4080. 2 => 'red',
  4081. 3 => 'green'
  4082. );
  4083. $this->Form->request->data = array(
  4084. 'Contact' => array(),
  4085. 'ContactTag' => array(
  4086. array(
  4087. 'id' => '1',
  4088. 'name' => 'blue'
  4089. ),
  4090. array(
  4091. 'id' => 3,
  4092. 'name' => 'green'
  4093. )
  4094. )
  4095. );
  4096. $this->Form->create('Contact');
  4097. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4098. $expected = array(
  4099. 'input' => array(
  4100. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4101. ),
  4102. 'select' => array(
  4103. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4104. 'multiple' => 'multiple'
  4105. ),
  4106. array('option' => array('value' => '1', 'selected' => 'selected')),
  4107. 'blue',
  4108. '/option',
  4109. array('option' => array('value' => '2')),
  4110. 'red',
  4111. '/option',
  4112. array('option' => array('value' => '3', 'selected' => 'selected')),
  4113. 'green',
  4114. '/option',
  4115. '/select'
  4116. );
  4117. $this->assertTags($result, $expected);
  4118. // make sure only 50 is selected, and not 50f5c0cf
  4119. $this->View->viewVars['contactTags'] = array(
  4120. '1' => 'blue',
  4121. '50f5c0cf' => 'red',
  4122. '50' => 'green'
  4123. );
  4124. $this->Form->request->data = array(
  4125. 'Contact' => array(),
  4126. 'ContactTag' => array(
  4127. array(
  4128. 'id' => 1,
  4129. 'name' => 'blue'
  4130. ),
  4131. array(
  4132. 'id' => 50,
  4133. 'name' => 'green'
  4134. )
  4135. )
  4136. );
  4137. $this->Form->create('Contact');
  4138. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4139. $expected = array(
  4140. 'input' => array(
  4141. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4142. ),
  4143. 'select' => array(
  4144. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4145. 'multiple' => 'multiple'
  4146. ),
  4147. array('option' => array('value' => '1', 'selected' => 'selected')),
  4148. 'blue',
  4149. '/option',
  4150. array('option' => array('value' => '50f5c0cf')),
  4151. 'red',
  4152. '/option',
  4153. array('option' => array('value' => '50', 'selected' => 'selected')),
  4154. 'green',
  4155. '/option',
  4156. '/select'
  4157. );
  4158. $this->assertTags($result, $expected);
  4159. }
  4160. /**
  4161. * test generation of multi select elements in checkbox format
  4162. *
  4163. * @return void
  4164. */
  4165. public function testSelectMultipleCheckboxes() {
  4166. $result = $this->Form->select(
  4167. 'Model.multi_field',
  4168. array('first', 'second', 'third'),
  4169. array('multiple' => 'checkbox')
  4170. );
  4171. $expected = array(
  4172. 'input' => array(
  4173. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4174. ),
  4175. array('div' => array('class' => 'checkbox')),
  4176. array('input' => array(
  4177. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4178. 'value' => '0', 'id' => 'ModelMultiField0'
  4179. )),
  4180. array('label' => array('for' => 'ModelMultiField0')),
  4181. 'first',
  4182. '/label',
  4183. '/div',
  4184. array('div' => array('class' => 'checkbox')),
  4185. array('input' => array(
  4186. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4187. 'value' => '1', 'id' => 'ModelMultiField1'
  4188. )),
  4189. array('label' => array('for' => 'ModelMultiField1')),
  4190. 'second',
  4191. '/label',
  4192. '/div',
  4193. array('div' => array('class' => 'checkbox')),
  4194. array('input' => array(
  4195. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4196. 'value' => '2', 'id' => 'ModelMultiField2'
  4197. )),
  4198. array('label' => array('for' => 'ModelMultiField2')),
  4199. 'third',
  4200. '/label',
  4201. '/div'
  4202. );
  4203. $this->assertTags($result, $expected);
  4204. $result = $this->Form->select(
  4205. 'Model.multi_field',
  4206. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4207. array('multiple' => 'checkbox')
  4208. );
  4209. $expected = array(
  4210. 'input' => array(
  4211. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4212. ),
  4213. array('div' => array('class' => 'checkbox')),
  4214. array('input' => array(
  4215. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4216. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4217. )),
  4218. array('label' => array('for' => 'ModelMultiFieldA')),
  4219. 'first',
  4220. '/label',
  4221. '/div',
  4222. array('div' => array('class' => 'checkbox')),
  4223. array('input' => array(
  4224. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4225. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4226. )),
  4227. array('label' => array('for' => 'ModelMultiFieldB')),
  4228. 'second',
  4229. '/label',
  4230. '/div',
  4231. array('div' => array('class' => 'checkbox')),
  4232. array('input' => array(
  4233. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4234. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4235. )),
  4236. array('label' => array('for' => 'ModelMultiFieldC')),
  4237. 'third',
  4238. '/label',
  4239. '/div'
  4240. );
  4241. $this->assertTags($result, $expected);
  4242. $result = $this->Form->select(
  4243. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4244. );
  4245. $expected = array(
  4246. 'input' => array(
  4247. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4248. ),
  4249. array('div' => array('class' => 'checkbox')),
  4250. array('input' => array(
  4251. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4252. 'value' => '1', 'id' => 'ModelMultiField1'
  4253. )),
  4254. array('label' => array('for' => 'ModelMultiField1')),
  4255. 'first',
  4256. '/label',
  4257. '/div'
  4258. );
  4259. $this->assertTags($result, $expected);
  4260. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4261. $result = $this->Form->select(
  4262. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4263. );
  4264. $expected = array(
  4265. 'input' => array(
  4266. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4267. ),
  4268. array('div' => array('class' => 'checkbox')),
  4269. array('input' => array(
  4270. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4271. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4272. )),
  4273. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4274. 'first',
  4275. '/label',
  4276. '/div',
  4277. array('div' => array('class' => 'checkbox')),
  4278. array('input' => array(
  4279. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4280. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4281. )),
  4282. array('label' => array('for' => 'ModelTagsArray')),
  4283. 'Array',
  4284. '/label',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. }
  4289. /**
  4290. * test multiple checkboxes with div styles.
  4291. *
  4292. * @return void
  4293. */
  4294. public function testSelectMultipleCheckboxDiv() {
  4295. $result = $this->Form->select(
  4296. 'Model.tags',
  4297. array('first', 'second'),
  4298. array('multiple' => 'checkbox', 'class' => 'my-class')
  4299. );
  4300. $expected = array(
  4301. 'input' => array(
  4302. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4303. ),
  4304. array('div' => array('class' => 'my-class')),
  4305. array('input' => array(
  4306. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4307. 'value' => '0', 'id' => 'ModelTags0'
  4308. )),
  4309. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4310. '/div',
  4311. array('div' => array('class' => 'my-class')),
  4312. array('input' => array(
  4313. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4314. 'value' => '1', 'id' => 'ModelTags1'
  4315. )),
  4316. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4317. '/div'
  4318. );
  4319. $this->assertTags($result, $expected);
  4320. $result = $this->Form->input('Model.tags', array(
  4321. 'options' => array('first', 'second'),
  4322. 'multiple' => 'checkbox',
  4323. 'class' => 'my-class',
  4324. 'div' => false,
  4325. 'label' => false
  4326. ));
  4327. $this->assertTags($result, $expected);
  4328. $Contact = ClassRegistry::getObject('Contact');
  4329. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4330. $result = $this->Form->input('Contact.tags', array(
  4331. 'options' => array('one'),
  4332. 'multiple' => 'checkbox',
  4333. 'label' => false,
  4334. 'div' => false
  4335. ));
  4336. $expected = array(
  4337. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4338. array('div' => array('class' => 'checkbox form-error')),
  4339. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4340. array('label' => array('for' => 'ContactTags0')),
  4341. 'one',
  4342. '/label',
  4343. '/div'
  4344. );
  4345. $this->assertTags($result, $expected);
  4346. $result = $this->Form->input('Contact.tags', array(
  4347. 'options' => array('one'),
  4348. 'multiple' => 'checkbox',
  4349. 'class' => 'mycheckbox',
  4350. 'label' => false,
  4351. 'div' => false
  4352. ));
  4353. $expected = array(
  4354. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4355. array('div' => array('class' => 'mycheckbox form-error')),
  4356. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4357. array('label' => array('for' => 'ContactTags0')),
  4358. 'one',
  4359. '/label',
  4360. '/div'
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. }
  4364. /**
  4365. * Checks the security hash array generated for multiple-input checkbox elements
  4366. *
  4367. * @return void
  4368. */
  4369. public function testSelectMultipleCheckboxSecurity() {
  4370. $this->Form->request['_Token'] = array('key' => 'testKey');
  4371. $this->assertEquals(array(), $this->Form->fields);
  4372. $result = $this->Form->select(
  4373. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4374. array('multiple' => 'checkbox')
  4375. );
  4376. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4377. $result = $this->Form->secure($this->Form->fields);
  4378. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4379. $this->assertRegExp('/"' . $key . '"/', $result);
  4380. }
  4381. /**
  4382. * Multiple select elements should always be secured as they always participate
  4383. * in the POST data.
  4384. *
  4385. * @return void
  4386. */
  4387. public function testSelectMultipleSecureWithNoOptions() {
  4388. $this->Form->request['_Token'] = array('key' => 'testkey');
  4389. $this->assertEquals(array(), $this->Form->fields);
  4390. $this->Form->select(
  4391. 'Model.select',
  4392. array(),
  4393. array('multiple' => true)
  4394. );
  4395. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4396. }
  4397. /**
  4398. * When a select box has no options it should not be added to the fields list
  4399. * as it always fail post validation.
  4400. *
  4401. * @return void
  4402. */
  4403. public function testSelectNoSecureWithNoOptions() {
  4404. $this->Form->request['_Token'] = array('key' => 'testkey');
  4405. $this->assertEquals(array(), $this->Form->fields);
  4406. $this->Form->select(
  4407. 'Model.select',
  4408. array()
  4409. );
  4410. $this->assertEquals(array(), $this->Form->fields);
  4411. $this->Form->select(
  4412. 'Model.select',
  4413. array(),
  4414. array('empty' => true)
  4415. );
  4416. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4417. }
  4418. /**
  4419. * testInputMultipleCheckboxes method
  4420. *
  4421. * test input() resulting in multi select elements being generated.
  4422. *
  4423. * @return void
  4424. */
  4425. public function testInputMultipleCheckboxes() {
  4426. $result = $this->Form->input('Model.multi_field', array(
  4427. 'options' => array('first', 'second', 'third'),
  4428. 'multiple' => 'checkbox'
  4429. ));
  4430. $expected = array(
  4431. array('div' => array('class' => 'input select')),
  4432. array('label' => array('for' => 'ModelMultiField')),
  4433. 'Multi Field',
  4434. '/label',
  4435. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4436. array('div' => array('class' => 'checkbox')),
  4437. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4438. array('label' => array('for' => 'ModelMultiField0')),
  4439. 'first',
  4440. '/label',
  4441. '/div',
  4442. array('div' => array('class' => 'checkbox')),
  4443. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4444. array('label' => array('for' => 'ModelMultiField1')),
  4445. 'second',
  4446. '/label',
  4447. '/div',
  4448. array('div' => array('class' => 'checkbox')),
  4449. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4450. array('label' => array('for' => 'ModelMultiField2')),
  4451. 'third',
  4452. '/label',
  4453. '/div',
  4454. '/div'
  4455. );
  4456. $this->assertTags($result, $expected);
  4457. $result = $this->Form->input('Model.multi_field', array(
  4458. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4459. 'multiple' => 'checkbox'
  4460. ));
  4461. $expected = array(
  4462. array('div' => array('class' => 'input select')),
  4463. array('label' => array('for' => 'ModelMultiField')),
  4464. 'Multi Field',
  4465. '/label',
  4466. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4467. array('div' => array('class' => 'checkbox')),
  4468. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4469. array('label' => array('for' => 'ModelMultiFieldA')),
  4470. 'first',
  4471. '/label',
  4472. '/div',
  4473. array('div' => array('class' => 'checkbox')),
  4474. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4475. array('label' => array('for' => 'ModelMultiFieldB')),
  4476. 'second',
  4477. '/label',
  4478. '/div',
  4479. array('div' => array('class' => 'checkbox')),
  4480. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4481. array('label' => array('for' => 'ModelMultiFieldC')),
  4482. 'third',
  4483. '/label',
  4484. '/div',
  4485. '/div'
  4486. );
  4487. $this->assertTags($result, $expected);
  4488. $result = $this->Form->input('Model.multi_field', array(
  4489. 'options' => array('1' => 'first'),
  4490. 'multiple' => 'checkbox',
  4491. 'label' => false,
  4492. 'div' => false
  4493. ));
  4494. $expected = array(
  4495. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4496. array('div' => array('class' => 'checkbox')),
  4497. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4498. array('label' => array('for' => 'ModelMultiField1')),
  4499. 'first',
  4500. '/label',
  4501. '/div'
  4502. );
  4503. $this->assertTags($result, $expected);
  4504. $result = $this->Form->input('Model.multi_field', array(
  4505. 'options' => array('2' => 'second'),
  4506. 'multiple' => 'checkbox',
  4507. 'label' => false,
  4508. 'div' => false
  4509. ));
  4510. $expected = array(
  4511. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4512. array('div' => array('class' => 'checkbox')),
  4513. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4514. array('label' => array('for' => 'ModelMultiField2')),
  4515. 'second',
  4516. '/label',
  4517. '/div'
  4518. );
  4519. $this->assertTags($result, $expected);
  4520. }
  4521. /**
  4522. * testSelectHiddenFieldOmission method
  4523. *
  4524. * test that select() with 'hiddenField' => false omits the hidden field
  4525. *
  4526. * @return void
  4527. */
  4528. public function testSelectHiddenFieldOmission() {
  4529. $result = $this->Form->select('Model.multi_field',
  4530. array('first', 'second'),
  4531. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4532. );
  4533. $expected = array(
  4534. array('div' => array('class' => 'checkbox')),
  4535. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4536. array('label' => array('for' => 'ModelMultiField0')),
  4537. 'first',
  4538. '/label',
  4539. '/div',
  4540. array('div' => array('class' => 'checkbox')),
  4541. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4542. array('label' => array('for' => 'ModelMultiField1')),
  4543. 'second',
  4544. '/label',
  4545. '/div'
  4546. );
  4547. $this->assertTags($result, $expected);
  4548. $result = $this->Form->input('Model.multi_field', array(
  4549. 'options' => array('first', 'second'),
  4550. 'multiple' => 'checkbox',
  4551. 'hiddenField' => false
  4552. ));
  4553. $expected = array(
  4554. array('div' => array('class' => 'input select')),
  4555. array('label' => array('for' => 'ModelMultiField')),
  4556. 'Multi Field',
  4557. '/label',
  4558. array('div' => array('class' => 'checkbox')),
  4559. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4560. array('label' => array('for' => 'ModelMultiField0')),
  4561. 'first',
  4562. '/label',
  4563. '/div',
  4564. array('div' => array('class' => 'checkbox')),
  4565. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4566. array('label' => array('for' => 'ModelMultiField1')),
  4567. 'second',
  4568. '/label',
  4569. '/div',
  4570. '/div'
  4571. );
  4572. $this->assertTags($result, $expected);
  4573. }
  4574. /**
  4575. * test that select() with multiple = checkbox works with overriding name attribute.
  4576. *
  4577. * @return void
  4578. */
  4579. public function testSelectCheckboxMultipleOverrideName() {
  4580. $result = $this->Form->input('category', array(
  4581. 'type' => 'select',
  4582. 'multiple' => 'checkbox',
  4583. 'name' => 'data[fish]',
  4584. 'options' => array('1', '2'),
  4585. 'div' => false,
  4586. 'label' => false,
  4587. ));
  4588. $expected = array(
  4589. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4590. array('div' => array('class' => 'checkbox')),
  4591. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4592. array('label' => array('for' => 'Category0')), '1', '/label',
  4593. '/div',
  4594. array('div' => array('class' => 'checkbox')),
  4595. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4596. array('label' => array('for' => 'Category1')), '2', '/label',
  4597. '/div'
  4598. );
  4599. $this->assertTags($result, $expected);
  4600. }
  4601. /**
  4602. * Test that 'id' overrides all the checkbox id's as well.
  4603. *
  4604. * @return void
  4605. */
  4606. public function testSelectCheckboxMultipleId() {
  4607. $result = $this->Form->select(
  4608. 'Model.multi_field',
  4609. array('first', 'second', 'third'),
  4610. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4611. );
  4612. $expected = array(
  4613. 'input' => array(
  4614. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4615. ),
  4616. array('div' => array('class' => 'checkbox')),
  4617. array('input' => array(
  4618. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4619. 'value' => '0', 'id' => 'CustomId0'
  4620. )),
  4621. array('label' => array('for' => 'CustomId0')),
  4622. 'first',
  4623. '/label',
  4624. '/div',
  4625. array('div' => array('class' => 'checkbox')),
  4626. array('input' => array(
  4627. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4628. 'value' => '1', 'id' => 'CustomId1'
  4629. )),
  4630. array('label' => array('for' => 'CustomId1')),
  4631. 'second',
  4632. '/label',
  4633. '/div',
  4634. array('div' => array('class' => 'checkbox')),
  4635. array('input' => array(
  4636. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4637. 'value' => '2', 'id' => 'CustomId2'
  4638. )),
  4639. array('label' => array('for' => 'CustomId2')),
  4640. 'third',
  4641. '/label',
  4642. '/div'
  4643. );
  4644. $this->assertTags($result, $expected);
  4645. }
  4646. /**
  4647. * testCheckbox method
  4648. *
  4649. * Test generation of checkboxes
  4650. *
  4651. * @return void
  4652. */
  4653. public function testCheckbox() {
  4654. $result = $this->Form->checkbox('Model.field');
  4655. $expected = array(
  4656. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4657. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4658. );
  4659. $this->assertTags($result, $expected);
  4660. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4661. $expected = array(
  4662. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4663. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4664. );
  4665. $this->assertTags($result, $expected);
  4666. $Contact = ClassRegistry::getObject('Contact');
  4667. $Contact->validationErrors['field'] = 1;
  4668. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4669. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4670. $expected = array(
  4671. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4672. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4673. );
  4674. $this->assertTags($result, $expected);
  4675. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4676. $expected = array(
  4677. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4678. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4679. );
  4680. $this->assertTags($result, $expected);
  4681. $this->Form->request->data['Contact']['field'] = '';
  4682. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4683. $expected = array(
  4684. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4685. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4686. );
  4687. $this->assertTags($result, $expected);
  4688. $Contact->validationErrors = array();
  4689. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4690. $expected = array(
  4691. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4692. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4693. );
  4694. $this->assertTags($result, $expected);
  4695. $this->Form->request->data['Contact']['published'] = 1;
  4696. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4697. $expected = array(
  4698. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4699. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4700. );
  4701. $this->assertTags($result, $expected);
  4702. $this->Form->request->data['Contact']['published'] = 0;
  4703. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4704. $expected = array(
  4705. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4706. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4707. );
  4708. $this->assertTags($result, $expected);
  4709. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4710. $expected = array(
  4711. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4712. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. $result = $this->Form->checkbox('CustomField.1.value');
  4716. $expected = array(
  4717. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4718. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4719. );
  4720. $this->assertTags($result, $expected);
  4721. }
  4722. /**
  4723. * test checkbox() with a custom name attribute
  4724. *
  4725. * @return void
  4726. */
  4727. public function testCheckboxCustomNameAttribute() {
  4728. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4729. $expected = array(
  4730. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4731. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4732. );
  4733. $this->assertTags($result, $expected);
  4734. }
  4735. /**
  4736. * test the checked option for checkboxes.
  4737. *
  4738. * @return void
  4739. */
  4740. public function testCheckboxCheckedOption() {
  4741. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4742. $expected = array(
  4743. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4744. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4745. );
  4746. $this->assertTags($result, $expected);
  4747. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4748. $expected = array(
  4749. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4750. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4751. );
  4752. $this->assertTags($result, $expected);
  4753. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4754. $expected = array(
  4755. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4756. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4757. );
  4758. $this->assertTags($result, $expected);
  4759. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4760. $expected = array(
  4761. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4762. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4763. );
  4764. $this->assertTags($result, $expected);
  4765. $this->Form->request->data['Model']['field'] = 1;
  4766. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4767. $expected = array(
  4768. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4770. );
  4771. $this->assertTags($result, $expected);
  4772. }
  4773. /**
  4774. * Test that disabled attribute works on both the checkbox and hidden input.
  4775. *
  4776. * @return void
  4777. */
  4778. public function testCheckboxDisabling() {
  4779. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4780. $expected = array(
  4781. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4782. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4783. );
  4784. $this->assertTags($result, $expected);
  4785. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4786. $expected = array(
  4787. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4788. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4789. );
  4790. $this->assertTags($result, $expected);
  4791. }
  4792. /**
  4793. * Test that the hidden input for checkboxes can be omitted or set to a
  4794. * specific value.
  4795. *
  4796. * @return void
  4797. */
  4798. public function testCheckboxHiddenField() {
  4799. $result = $this->Form->input('UserForm.something', array(
  4800. 'type' => 'checkbox',
  4801. 'hiddenField' => false
  4802. ));
  4803. $expected = array(
  4804. 'div' => array('class' => 'input checkbox'),
  4805. array('input' => array(
  4806. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4807. 'value' => '1', 'id' => 'UserFormSomething'
  4808. )),
  4809. 'label' => array('for' => 'UserFormSomething'),
  4810. 'Something',
  4811. '/label',
  4812. '/div'
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $result = $this->Form->input('UserForm.something', array(
  4816. 'type' => 'checkbox',
  4817. 'value' => 'Y',
  4818. 'hiddenField' => 'N',
  4819. ));
  4820. $expected = array(
  4821. 'div' => array('class' => 'input checkbox'),
  4822. array('input' => array(
  4823. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4824. 'value' => 'N', 'id' => 'UserFormSomething_'
  4825. )),
  4826. array('input' => array(
  4827. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4828. 'value' => 'Y', 'id' => 'UserFormSomething'
  4829. )),
  4830. 'label' => array('for' => 'UserFormSomething'),
  4831. 'Something',
  4832. '/label',
  4833. '/div'
  4834. );
  4835. $this->assertTags($result, $expected);
  4836. }
  4837. /**
  4838. * testDateTime method
  4839. *
  4840. * Test generation of date/time select elements
  4841. *
  4842. * @return void
  4843. */
  4844. public function testDateTime() {
  4845. extract($this->dateRegex);
  4846. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4847. $now = strtotime('now');
  4848. $expected = array(
  4849. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4850. $daysRegex,
  4851. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4852. date('j', $now),
  4853. '/option',
  4854. '*/select',
  4855. '-',
  4856. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4857. $monthsRegex,
  4858. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4859. date('F', $now),
  4860. '/option',
  4861. '*/select',
  4862. '-',
  4863. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4864. $yearsRegex,
  4865. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4866. date('Y', $now),
  4867. '/option',
  4868. '*/select',
  4869. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4870. $hoursRegex,
  4871. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4872. date('g', $now),
  4873. '/option',
  4874. '*/select',
  4875. ':',
  4876. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4877. $minutesRegex,
  4878. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4879. date('i', $now),
  4880. '/option',
  4881. '*/select',
  4882. ' ',
  4883. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4884. $meridianRegex,
  4885. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4886. date('a', $now),
  4887. '/option',
  4888. '*/select'
  4889. );
  4890. $this->assertTags($result, $expected);
  4891. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4892. $expected = array(
  4893. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4894. $daysRegex,
  4895. array('option' => array('value' => '')),
  4896. '/option',
  4897. '*/select',
  4898. '-',
  4899. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4900. $monthsRegex,
  4901. array('option' => array('value' => '')),
  4902. '/option',
  4903. '*/select',
  4904. '-',
  4905. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4906. $yearsRegex,
  4907. array('option' => array('value' => '')),
  4908. '/option',
  4909. '*/select',
  4910. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4911. $hoursRegex,
  4912. array('option' => array('value' => '')),
  4913. '/option',
  4914. '*/select',
  4915. ':',
  4916. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4917. $minutesRegex,
  4918. array('option' => array('value' => '')),
  4919. '/option',
  4920. '*/select',
  4921. ' ',
  4922. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4923. $meridianRegex,
  4924. array('option' => array('value' => '')),
  4925. '/option',
  4926. '*/select'
  4927. );
  4928. $this->assertTags($result, $expected);
  4929. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4930. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4931. $this->assertTags($result, $expected);
  4932. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4933. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4934. $this->assertTags($result, $expected);
  4935. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4936. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4937. $expected = array(
  4938. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4939. $daysRegex,
  4940. array('option' => array('value' => '')),
  4941. '/option',
  4942. '*/select',
  4943. '-',
  4944. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4945. $monthsRegex,
  4946. array('option' => array('value' => '')),
  4947. '/option',
  4948. '*/select',
  4949. '-',
  4950. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4951. $yearsRegex,
  4952. array('option' => array('value' => '')),
  4953. '/option',
  4954. '*/select',
  4955. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4956. $hoursRegex,
  4957. array('option' => array('value' => '')),
  4958. '/option',
  4959. '*/select',
  4960. ':',
  4961. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4962. $minutesRegex,
  4963. array('option' => array('value' => '')),
  4964. '/option',
  4965. array('option' => array('value' => '00')),
  4966. '00',
  4967. '/option',
  4968. array('option' => array('value' => '05')),
  4969. '05',
  4970. '/option',
  4971. array('option' => array('value' => '10')),
  4972. '10',
  4973. '/option',
  4974. '*/select',
  4975. ' ',
  4976. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4977. $meridianRegex,
  4978. array('option' => array('value' => '')),
  4979. '/option',
  4980. '*/select'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4984. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4985. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4986. $this->Form->request->data['Contact']['data'] = null;
  4987. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4988. $expected = array(
  4989. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4990. $daysRegex,
  4991. array('option' => array('value' => '')),
  4992. '/option',
  4993. '*/select',
  4994. '-',
  4995. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4996. $monthsRegex,
  4997. array('option' => array('value' => '')),
  4998. '/option',
  4999. '*/select',
  5000. '-',
  5001. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5002. $yearsRegex,
  5003. array('option' => array('value' => '')),
  5004. '/option',
  5005. '*/select',
  5006. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5007. $hoursRegex,
  5008. array('option' => array('value' => '')),
  5009. '/option',
  5010. '*/select',
  5011. ':',
  5012. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5013. $minutesRegex,
  5014. array('option' => array('value' => '')),
  5015. '/option',
  5016. '*/select',
  5017. ' ',
  5018. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5019. $meridianRegex,
  5020. array('option' => array('value' => '')),
  5021. '/option',
  5022. '*/select'
  5023. );
  5024. $this->assertTags($result, $expected);
  5025. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5026. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5027. $now = strtotime($this->Form->data['Model']['field']);
  5028. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5029. $expected = array(
  5030. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5031. $daysRegex,
  5032. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5033. date('j', $now),
  5034. '/option',
  5035. '*/select',
  5036. '-',
  5037. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5038. $monthsRegex,
  5039. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5040. date('F', $now),
  5041. '/option',
  5042. '*/select',
  5043. '-',
  5044. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5045. $yearsRegex,
  5046. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5047. date('Y', $now),
  5048. '/option',
  5049. '*/select',
  5050. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5051. $hoursRegex,
  5052. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5053. date('g', $now),
  5054. '/option',
  5055. '*/select',
  5056. ':',
  5057. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5058. $minutesRegex,
  5059. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5060. date('i', $now),
  5061. '/option',
  5062. '*/select',
  5063. ' ',
  5064. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5065. $meridianRegex,
  5066. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5067. date('a', $now),
  5068. '/option',
  5069. '*/select'
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $selected = strtotime('2008-10-26 12:33:00');
  5073. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5074. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5075. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5076. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5077. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5078. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5079. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5080. $this->Form->create('Contact');
  5081. $result = $this->Form->input('published');
  5082. $now = strtotime('now');
  5083. $expected = array(
  5084. 'div' => array('class' => 'input date'),
  5085. 'label' => array('for' => 'ContactPublishedMonth'),
  5086. 'Published',
  5087. '/label',
  5088. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5089. $monthsRegex,
  5090. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5091. date('F', $now),
  5092. '/option',
  5093. '*/select',
  5094. '-',
  5095. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5096. $daysRegex,
  5097. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5098. date('j', $now),
  5099. '/option',
  5100. '*/select',
  5101. '-',
  5102. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5103. $yearsRegex,
  5104. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5105. date('Y', $now),
  5106. '/option',
  5107. '*/select',
  5108. '/div'
  5109. );
  5110. $this->assertTags($result, $expected);
  5111. $result = $this->Form->input('published2', array('type' => 'date'));
  5112. $now = strtotime('now');
  5113. $expected = array(
  5114. 'div' => array('class' => 'input date'),
  5115. 'label' => array('for' => 'ContactPublished2Month'),
  5116. 'Published2',
  5117. '/label',
  5118. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5119. $monthsRegex,
  5120. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5121. date('F', $now),
  5122. '/option',
  5123. '*/select',
  5124. '-',
  5125. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5126. $daysRegex,
  5127. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5128. date('j', $now),
  5129. '/option',
  5130. '*/select',
  5131. '-',
  5132. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5133. $yearsRegex,
  5134. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5135. date('Y', $now),
  5136. '/option',
  5137. '*/select',
  5138. '/div'
  5139. );
  5140. $this->assertTags($result, $expected);
  5141. $this->Form->create('Contact');
  5142. $result = $this->Form->input('published', array('monthNames' => false));
  5143. $now = strtotime('now');
  5144. $expected = array(
  5145. 'div' => array('class' => 'input date'),
  5146. 'label' => array('for' => 'ContactPublishedMonth'),
  5147. 'Published',
  5148. '/label',
  5149. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5150. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5151. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5152. date('m', $now),
  5153. '/option',
  5154. '*/select',
  5155. '-',
  5156. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5157. $daysRegex,
  5158. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5159. date('j', $now),
  5160. '/option',
  5161. '*/select',
  5162. '-',
  5163. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5164. $yearsRegex,
  5165. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5166. date('Y', $now),
  5167. '/option',
  5168. '*/select',
  5169. '/div'
  5170. );
  5171. $this->assertTags($result, $expected);
  5172. $result = $this->Form->input('published', array(
  5173. 'timeFormat' => 24,
  5174. 'interval' => 5,
  5175. 'selected' => strtotime('2009-09-03 13:37:00'),
  5176. 'type' => 'datetime'
  5177. ));
  5178. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5179. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5180. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5181. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5182. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5183. }
  5184. /**
  5185. * Test that empty values don't trigger errors.
  5186. *
  5187. * @return void
  5188. */
  5189. public function testDateTimeNoErrorsOnEmptyData() {
  5190. $this->Form->request->data['Contact'] = array(
  5191. 'date' => array(
  5192. 'day' => '',
  5193. 'month' => '',
  5194. 'year' => '',
  5195. 'hour' => '',
  5196. 'min' => '',
  5197. 'meridian' => ''
  5198. )
  5199. );
  5200. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5201. $this->assertNotEmpty($result);
  5202. }
  5203. /**
  5204. * test that datetime() and default values work.
  5205. *
  5206. * @return void
  5207. */
  5208. public function testDatetimeWithDefault() {
  5209. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5210. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5211. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5212. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5213. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5214. 'default' => '2009-06-01 11:15:30'
  5215. ));
  5216. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5217. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5218. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5219. }
  5220. /**
  5221. * test that bogus non-date time data doesn't cause errors.
  5222. *
  5223. * @return void
  5224. */
  5225. public function testDateTimeWithBogusData() {
  5226. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5227. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5228. }
  5229. /**
  5230. * testDateTimeEmptyAsArray
  5231. *
  5232. * @return void
  5233. */
  5234. public function testDateTimeEmptyAsArray() {
  5235. $result = $this->Form->dateTime('Contact.date',
  5236. 'DMY',
  5237. '12',
  5238. array(
  5239. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5240. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5241. )
  5242. )
  5243. );
  5244. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5245. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5246. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5247. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5248. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5249. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5250. $result = $this->Form->dateTime('Contact.date',
  5251. 'DMY',
  5252. '12',
  5253. array(
  5254. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5255. )
  5256. );
  5257. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5258. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5259. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5260. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5261. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5262. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5263. }
  5264. /**
  5265. * testFormDateTimeMulti method
  5266. *
  5267. * test multiple datetime element generation
  5268. *
  5269. * @return void
  5270. */
  5271. public function testFormDateTimeMulti() {
  5272. extract($this->dateRegex);
  5273. $result = $this->Form->dateTime('Contact.1.updated');
  5274. $expected = array(
  5275. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5276. $daysRegex,
  5277. array('option' => array('value' => '')),
  5278. '/option',
  5279. '*/select',
  5280. '-',
  5281. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5282. $monthsRegex,
  5283. array('option' => array('value' => '')),
  5284. '/option',
  5285. '*/select',
  5286. '-',
  5287. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5288. $yearsRegex,
  5289. array('option' => array('value' => '')),
  5290. '/option',
  5291. '*/select',
  5292. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5293. $hoursRegex,
  5294. array('option' => array('value' => '')),
  5295. '/option',
  5296. '*/select',
  5297. ':',
  5298. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5299. $minutesRegex,
  5300. array('option' => array('value' => '')),
  5301. '/option',
  5302. '*/select',
  5303. ' ',
  5304. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5305. $meridianRegex,
  5306. array('option' => array('value' => '')),
  5307. '/option',
  5308. '*/select'
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $result = $this->Form->dateTime('Contact.2.updated');
  5312. $expected = array(
  5313. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5314. $daysRegex,
  5315. array('option' => array('value' => '')),
  5316. '/option',
  5317. '*/select',
  5318. '-',
  5319. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5320. $monthsRegex,
  5321. array('option' => array('value' => '')),
  5322. '/option',
  5323. '*/select',
  5324. '-',
  5325. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5326. $yearsRegex,
  5327. array('option' => array('value' => '')),
  5328. '/option',
  5329. '*/select',
  5330. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5331. $hoursRegex,
  5332. array('option' => array('value' => '')),
  5333. '/option',
  5334. '*/select',
  5335. ':',
  5336. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5337. $minutesRegex,
  5338. array('option' => array('value' => '')),
  5339. '/option',
  5340. '*/select',
  5341. ' ',
  5342. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5343. $meridianRegex,
  5344. array('option' => array('value' => '')),
  5345. '/option',
  5346. '*/select'
  5347. );
  5348. $this->assertTags($result, $expected);
  5349. }
  5350. /**
  5351. * When changing the date format, the label should always focus the first select box when
  5352. * clicked.
  5353. *
  5354. * @return void
  5355. */
  5356. public function testDateTimeLabelIdMatchesFirstInput() {
  5357. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5358. $this->assertContains('label for="ModelDateMonth"', $result);
  5359. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5360. $this->assertContains('label for="ModelDateDay"', $result);
  5361. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5362. $this->assertContains('label for="ModelDateYear"', $result);
  5363. }
  5364. /**
  5365. * testMonth method
  5366. *
  5367. * @return void
  5368. */
  5369. public function testMonth() {
  5370. $result = $this->Form->month('Model.field');
  5371. $expected = array(
  5372. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5373. array('option' => array('value' => '')),
  5374. '/option',
  5375. array('option' => array('value' => '01')),
  5376. date('F', strtotime('2008-01-01 00:00:00')),
  5377. '/option',
  5378. array('option' => array('value' => '02')),
  5379. date('F', strtotime('2008-02-01 00:00:00')),
  5380. '/option',
  5381. '*/select',
  5382. );
  5383. $this->assertTags($result, $expected);
  5384. $result = $this->Form->month('Model.field', array('empty' => true));
  5385. $expected = array(
  5386. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5387. array('option' => array('value' => '')),
  5388. '/option',
  5389. array('option' => array('value' => '01')),
  5390. date('F', strtotime('2008-01-01 00:00:00')),
  5391. '/option',
  5392. array('option' => array('value' => '02')),
  5393. date('F', strtotime('2008-02-01 00:00:00')),
  5394. '/option',
  5395. '*/select',
  5396. );
  5397. $this->assertTags($result, $expected);
  5398. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5399. $expected = array(
  5400. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5401. array('option' => array('value' => '')),
  5402. '/option',
  5403. array('option' => array('value' => '01')),
  5404. '01',
  5405. '/option',
  5406. array('option' => array('value' => '02')),
  5407. '02',
  5408. '/option',
  5409. '*/select',
  5410. );
  5411. $this->assertTags($result, $expected);
  5412. $monthNames = array(
  5413. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5414. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5415. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5416. $expected = array(
  5417. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5418. array('option' => array('value' => '')),
  5419. '/option',
  5420. array('option' => array('value' => '01')),
  5421. 'Jan',
  5422. '/option',
  5423. array('option' => array('value' => '02')),
  5424. 'Feb',
  5425. '/option',
  5426. '*/select',
  5427. );
  5428. $this->assertTags($result, $expected);
  5429. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5430. $result = $this->Form->month('Project.release');
  5431. $expected = array(
  5432. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5433. array('option' => array('value' => '')),
  5434. '/option',
  5435. array('option' => array('value' => '01')),
  5436. 'January',
  5437. '/option',
  5438. array('option' => array('value' => '02', 'selected' => 'selected')),
  5439. 'February',
  5440. '/option',
  5441. '*/select',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. }
  5445. /**
  5446. * testDay method
  5447. *
  5448. * @return void
  5449. */
  5450. public function testDay() {
  5451. extract($this->dateRegex);
  5452. $result = $this->Form->day('Model.field', array('value' => false));
  5453. $expected = array(
  5454. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5455. array('option' => array('value' => '')),
  5456. '/option',
  5457. array('option' => array('value' => '01')),
  5458. '1',
  5459. '/option',
  5460. array('option' => array('value' => '02')),
  5461. '2',
  5462. '/option',
  5463. $daysRegex,
  5464. '/select',
  5465. );
  5466. $this->assertTags($result, $expected);
  5467. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5468. $result = $this->Form->day('Model.field');
  5469. $expected = array(
  5470. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5471. array('option' => array('value' => '')),
  5472. '/option',
  5473. array('option' => array('value' => '01')),
  5474. '1',
  5475. '/option',
  5476. array('option' => array('value' => '02')),
  5477. '2',
  5478. '/option',
  5479. $daysRegex,
  5480. array('option' => array('value' => '10', 'selected' => 'selected')),
  5481. '10',
  5482. '/option',
  5483. $daysRegex,
  5484. '/select',
  5485. );
  5486. $this->assertTags($result, $expected);
  5487. $this->Form->request->data['Model']['field'] = '';
  5488. $result = $this->Form->day('Model.field', array('value' => '10'));
  5489. $expected = array(
  5490. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5491. array('option' => array('value' => '')),
  5492. '/option',
  5493. array('option' => array('value' => '01')),
  5494. '1',
  5495. '/option',
  5496. array('option' => array('value' => '02')),
  5497. '2',
  5498. '/option',
  5499. $daysRegex,
  5500. array('option' => array('value' => '10', 'selected' => 'selected')),
  5501. '10',
  5502. '/option',
  5503. $daysRegex,
  5504. '/select',
  5505. );
  5506. $this->assertTags($result, $expected);
  5507. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5508. $result = $this->Form->day('Model.field', array('value' => true));
  5509. $expected = array(
  5510. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5511. array('option' => array('value' => '')),
  5512. '/option',
  5513. array('option' => array('value' => '01')),
  5514. '1',
  5515. '/option',
  5516. array('option' => array('value' => '02')),
  5517. '2',
  5518. '/option',
  5519. $daysRegex,
  5520. array('option' => array('value' => '10', 'selected' => 'selected')),
  5521. '10',
  5522. '/option',
  5523. $daysRegex,
  5524. '/select',
  5525. );
  5526. $this->assertTags($result, $expected);
  5527. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5528. $result = $this->Form->day('Project.release');
  5529. $expected = array(
  5530. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5531. array('option' => array('value' => '')),
  5532. '/option',
  5533. array('option' => array('value' => '01')),
  5534. '1',
  5535. '/option',
  5536. array('option' => array('value' => '02')),
  5537. '2',
  5538. '/option',
  5539. $daysRegex,
  5540. array('option' => array('value' => '10', 'selected' => 'selected')),
  5541. '10',
  5542. '/option',
  5543. $daysRegex,
  5544. '/select',
  5545. );
  5546. $this->assertTags($result, $expected);
  5547. }
  5548. /**
  5549. * testMinute method
  5550. *
  5551. * @return void
  5552. */
  5553. public function testMinute() {
  5554. extract($this->dateRegex);
  5555. $result = $this->Form->minute('Model.field');
  5556. $expected = array(
  5557. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5558. array('option' => array('value' => '')),
  5559. '/option',
  5560. array('option' => array('value' => '00')),
  5561. '00',
  5562. '/option',
  5563. array('option' => array('value' => '01')),
  5564. '01',
  5565. '/option',
  5566. array('option' => array('value' => '02')),
  5567. '02',
  5568. '/option',
  5569. $minutesRegex,
  5570. '/select',
  5571. );
  5572. $this->assertTags($result, $expected);
  5573. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5574. $result = $this->Form->minute('Model.field');
  5575. $expected = array(
  5576. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5577. array('option' => array('value' => '')),
  5578. '/option',
  5579. array('option' => array('value' => '00')),
  5580. '00',
  5581. '/option',
  5582. array('option' => array('value' => '01')),
  5583. '01',
  5584. '/option',
  5585. array('option' => array('value' => '02')),
  5586. '02',
  5587. '/option',
  5588. $minutesRegex,
  5589. array('option' => array('value' => '12', 'selected' => 'selected')),
  5590. '12',
  5591. '/option',
  5592. $minutesRegex,
  5593. '/select',
  5594. );
  5595. $this->assertTags($result, $expected);
  5596. $this->Form->request->data['Model']['field'] = '';
  5597. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5598. $expected = array(
  5599. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5600. array('option' => array('value' => '')),
  5601. '/option',
  5602. array('option' => array('value' => '00')),
  5603. '00',
  5604. '/option',
  5605. array('option' => array('value' => '05')),
  5606. '05',
  5607. '/option',
  5608. array('option' => array('value' => '10')),
  5609. '10',
  5610. '/option',
  5611. $minutesRegex,
  5612. '/select',
  5613. );
  5614. $this->assertTags($result, $expected);
  5615. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5616. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5617. $expected = array(
  5618. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5619. array('option' => array('value' => '')),
  5620. '/option',
  5621. array('option' => array('value' => '00')),
  5622. '00',
  5623. '/option',
  5624. array('option' => array('value' => '05')),
  5625. '05',
  5626. '/option',
  5627. array('option' => array('value' => '10', 'selected' => 'selected')),
  5628. '10',
  5629. '/option',
  5630. $minutesRegex,
  5631. '/select',
  5632. );
  5633. $this->assertTags($result, $expected);
  5634. }
  5635. /**
  5636. * testHour method
  5637. *
  5638. * @return void
  5639. */
  5640. public function testHour() {
  5641. extract($this->dateRegex);
  5642. $result = $this->Form->hour('Model.field', false);
  5643. $expected = array(
  5644. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5645. array('option' => array('value' => '')),
  5646. '/option',
  5647. array('option' => array('value' => '01')),
  5648. '1',
  5649. '/option',
  5650. array('option' => array('value' => '02')),
  5651. '2',
  5652. '/option',
  5653. $hoursRegex,
  5654. '/select',
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5658. $result = $this->Form->hour('Model.field', false);
  5659. $expected = array(
  5660. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5661. array('option' => array('value' => '')),
  5662. '/option',
  5663. array('option' => array('value' => '01')),
  5664. '1',
  5665. '/option',
  5666. array('option' => array('value' => '02')),
  5667. '2',
  5668. '/option',
  5669. $hoursRegex,
  5670. array('option' => array('value' => '12', 'selected' => 'selected')),
  5671. '12',
  5672. '/option',
  5673. '/select',
  5674. );
  5675. $this->assertTags($result, $expected);
  5676. $this->Form->request->data['Model']['field'] = '';
  5677. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5678. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5679. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5680. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5681. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5682. $result = $this->Form->hour('Model.field', true);
  5683. $expected = array(
  5684. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5685. array('option' => array('value' => '')),
  5686. '/option',
  5687. array('option' => array('value' => '00', 'selected' => 'selected')),
  5688. '0',
  5689. '/option',
  5690. array('option' => array('value' => '01')),
  5691. '1',
  5692. '/option',
  5693. array('option' => array('value' => '02')),
  5694. '2',
  5695. '/option',
  5696. $hoursRegex,
  5697. '/select',
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. unset($this->Form->request->data['Model']['field']);
  5701. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5702. $thisHour = date('H');
  5703. $optValue = date('G');
  5704. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5705. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5706. $result = $this->Form->hour('Model.field', true);
  5707. $expected = array(
  5708. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5709. array('option' => array('value' => '')),
  5710. '/option',
  5711. array('option' => array('value' => '00')),
  5712. '0',
  5713. '/option',
  5714. array('option' => array('value' => '01', 'selected' => 'selected')),
  5715. '1',
  5716. '/option',
  5717. array('option' => array('value' => '02')),
  5718. '2',
  5719. '/option',
  5720. $hoursRegex,
  5721. '/select',
  5722. );
  5723. $this->assertTags($result, $expected);
  5724. }
  5725. /**
  5726. * testYear method
  5727. *
  5728. * @return void
  5729. */
  5730. public function testYear() {
  5731. $result = $this->Form->year('Model.field', 2006, 2007);
  5732. $expected = array(
  5733. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5734. array('option' => array('value' => '')),
  5735. '/option',
  5736. array('option' => array('value' => '2007')),
  5737. '2007',
  5738. '/option',
  5739. array('option' => array('value' => '2006')),
  5740. '2006',
  5741. '/option',
  5742. '/select',
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5746. $expected = array(
  5747. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5748. array('option' => array('value' => '')),
  5749. '/option',
  5750. array('option' => array('value' => '2006')),
  5751. '2006',
  5752. '/option',
  5753. array('option' => array('value' => '2007')),
  5754. '2007',
  5755. '/option',
  5756. '/select',
  5757. );
  5758. $this->assertTags($result, $expected);
  5759. $this->request->data['Contact']['published'] = '';
  5760. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5761. $expected = array(
  5762. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5763. array('option' => array('value' => '')),
  5764. '/option',
  5765. array('option' => array('value' => '2007')),
  5766. '2007',
  5767. '/option',
  5768. array('option' => array('value' => '2006')),
  5769. '2006',
  5770. '/option',
  5771. '/select',
  5772. );
  5773. $this->assertTags($result, $expected);
  5774. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5775. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5776. $expected = array(
  5777. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5778. array('option' => array('value' => '2007')),
  5779. '2007',
  5780. '/option',
  5781. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5782. '2006',
  5783. '/option',
  5784. '/select',
  5785. );
  5786. $this->assertTags($result, $expected);
  5787. $this->Form->request->data['Contact']['published'] = '';
  5788. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5789. $expected = array(
  5790. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5791. array('option' => array('value' => '')),
  5792. '/option',
  5793. array('option' => array('value' => '2007')),
  5794. '2007',
  5795. '/option',
  5796. array('option' => array('value' => '2006')),
  5797. '2006',
  5798. '/option',
  5799. '/select',
  5800. );
  5801. $this->assertTags($result, $expected);
  5802. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5803. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5804. $expected = array(
  5805. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5806. array('option' => array('value' => '2007')),
  5807. '2007',
  5808. '/option',
  5809. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5810. '2006',
  5811. '/option',
  5812. '/select',
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $this->Form->request->data['Contact']['published'] = '';
  5816. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5817. $expected = array(
  5818. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5819. array('option' => array('value' => '')),
  5820. '/option',
  5821. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5822. '2007',
  5823. '/option',
  5824. array('option' => array('value' => '2006')),
  5825. '2006',
  5826. '/option',
  5827. '/select',
  5828. );
  5829. $this->assertTags($result, $expected);
  5830. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5831. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5832. $expected = array(
  5833. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5834. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5835. '2007',
  5836. '/option',
  5837. array('option' => array('value' => '2006')),
  5838. '2006',
  5839. '/option',
  5840. '/select',
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $this->Form->request->data['Contact']['published'] = '';
  5844. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5845. $expected = array(
  5846. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5847. array('option' => array('value' => '2008')),
  5848. '2008',
  5849. '/option',
  5850. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5851. '2007',
  5852. '/option',
  5853. array('option' => array('value' => '2006')),
  5854. '2006',
  5855. '/option',
  5856. '/select',
  5857. );
  5858. $this->assertTags($result, $expected);
  5859. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5860. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5861. $expected = array(
  5862. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5863. array('option' => array('value' => '2008')),
  5864. '2008',
  5865. '/option',
  5866. array('option' => array('value' => '2007')),
  5867. '2007',
  5868. '/option',
  5869. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5870. '2006',
  5871. '/option',
  5872. '/select',
  5873. );
  5874. $this->assertTags($result, $expected);
  5875. $this->Form->request->data = array();
  5876. $this->Form->create('Contact');
  5877. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5878. $expected = array(
  5879. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5880. array('option' => array('value' => '2008')),
  5881. '2008',
  5882. '/option',
  5883. array('option' => array('value' => '2007')),
  5884. '2007',
  5885. '/option',
  5886. array('option' => array('value' => '2006')),
  5887. '2006',
  5888. '/option',
  5889. '/select',
  5890. );
  5891. $this->assertTags($result, $expected);
  5892. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5893. $this->assertContains('data[Contact][published][year]', $result);
  5894. }
  5895. /**
  5896. * testYearAutoExpandRange method
  5897. *
  5898. * @return void
  5899. */
  5900. public function testYearAutoExpandRange() {
  5901. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5902. $result = $this->Form->year('User.birthday');
  5903. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5904. $result = $matches[1];
  5905. $expected = range(date('Y') + 20, 1930);
  5906. $this->assertEquals($result, $expected);
  5907. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5908. $result = $this->Form->year('Project.release');
  5909. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5910. $result = $matches[1];
  5911. $expected = range(2050, date('Y') - 20);
  5912. $this->assertEquals($result, $expected);
  5913. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5914. $result = $this->Form->year('Project.release', 1890, 1900);
  5915. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5916. $result = $matches[1];
  5917. $expected = range(1900, 1881);
  5918. $this->assertEquals($result, $expected);
  5919. }
  5920. /**
  5921. * testTextArea method
  5922. *
  5923. * @return void
  5924. */
  5925. public function testTextArea() {
  5926. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5927. $result = $this->Form->textarea('Model.field');
  5928. $expected = array(
  5929. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5930. 'some test data',
  5931. '/textarea',
  5932. );
  5933. $this->assertTags($result, $expected);
  5934. $result = $this->Form->textarea('Model.tmp');
  5935. $expected = array(
  5936. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5937. '/textarea',
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5941. $result = $this->Form->textarea('Model.field');
  5942. $expected = array(
  5943. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5944. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5945. '/textarea',
  5946. );
  5947. $this->assertTags($result, $expected);
  5948. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5949. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5950. $expected = array(
  5951. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5952. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5953. '/textarea',
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5957. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5958. $expected = array(
  5959. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5960. '/textarea'
  5961. );
  5962. $this->assertTags($result, $expected);
  5963. }
  5964. /**
  5965. * testTextAreaWithStupidCharacters method
  5966. *
  5967. * test text area with non-ascii characters
  5968. *
  5969. * @return void
  5970. */
  5971. public function testTextAreaWithStupidCharacters() {
  5972. $this->loadFixtures('Post');
  5973. $result = $this->Form->input('Post.content', array(
  5974. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5975. ));
  5976. $expected = array(
  5977. 'div' => array('class' => 'input textarea'),
  5978. 'label' => array('for' => 'PostContent'),
  5979. 'Current Text',
  5980. '/label',
  5981. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5982. 'GREAT®',
  5983. '/textarea',
  5984. '/div'
  5985. );
  5986. $this->assertTags($result, $expected);
  5987. }
  5988. /**
  5989. * testHiddenField method
  5990. *
  5991. * @return void
  5992. */
  5993. public function testHiddenField() {
  5994. $Contact = ClassRegistry::getObject('Contact');
  5995. $Contact->validationErrors['field'] = 1;
  5996. $this->Form->request->data['Contact']['field'] = 'test';
  5997. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5998. $this->assertTags($result, array(
  5999. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6000. );
  6001. }
  6002. /**
  6003. * testFileUploadField method
  6004. *
  6005. * @return void
  6006. */
  6007. public function testFileUploadField() {
  6008. $result = $this->Form->file('Model.upload');
  6009. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6010. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6011. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6012. $expected = array(
  6013. 'div' => array('class' => 'input file'),
  6014. 'label' => array('for' => 'ModelUpload'),
  6015. 'Upload',
  6016. '/label',
  6017. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6018. '/div'
  6019. );
  6020. $this->assertTags($result, $expected);
  6021. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6022. $result = $this->Form->file('Model.upload');
  6023. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6024. }
  6025. /**
  6026. * test File upload input on a model not used in create();
  6027. *
  6028. * @return void
  6029. */
  6030. public function testFileUploadOnOtherModel() {
  6031. $this->Form->create('ValidateUser', array('type' => 'file'));
  6032. $result = $this->Form->file('ValidateProfile.city');
  6033. $expected = array(
  6034. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6035. );
  6036. $this->assertTags($result, $expected);
  6037. }
  6038. /**
  6039. * testButton method
  6040. *
  6041. * @return void
  6042. */
  6043. public function testButton() {
  6044. $result = $this->Form->button('Hi');
  6045. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6046. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6047. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6048. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6049. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6050. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6051. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6052. $result = $this->Form->button('No type', array('type' => false));
  6053. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6054. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6055. $this->assertNotRegExp('/\&039/', $result);
  6056. }
  6057. /**
  6058. * Test that button() makes unlocked fields by default.
  6059. *
  6060. * @return void
  6061. */
  6062. public function testButtonUnlockedByDefault() {
  6063. $this->Form->request->params['_Token']['key'] = 'secured';
  6064. $this->Form->button('Save', array('name' => 'save'));
  6065. $this->Form->button('Clear');
  6066. $result = $this->Form->unlockField();
  6067. $this->assertEquals(array('save'), $result);
  6068. }
  6069. /**
  6070. * testPostButton method
  6071. *
  6072. * @return void
  6073. */
  6074. public function testPostButton() {
  6075. $result = $this->Form->postButton('Hi', '/controller/action');
  6076. $this->assertTags($result, array(
  6077. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6078. 'div' => array('style' => 'display:none;'),
  6079. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6080. '/div',
  6081. 'button' => array('type' => 'submit'),
  6082. 'Hi',
  6083. '/button',
  6084. '/form'
  6085. ));
  6086. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6087. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6088. }
  6089. /**
  6090. * Test that postButton adds _Token fields.
  6091. *
  6092. * @return void
  6093. */
  6094. public function testSecurePostButton() {
  6095. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6096. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6097. $expected = array(
  6098. 'form' => array(
  6099. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6100. ),
  6101. array('div' => array('style' => 'display:none;')),
  6102. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6103. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6104. '/div',
  6105. 'button' => array('type' => 'submit'),
  6106. 'Delete',
  6107. '/button',
  6108. array('div' => array('style' => 'display:none;')),
  6109. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6110. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6111. '/div',
  6112. '/form',
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. }
  6116. /**
  6117. * testPostLink method
  6118. *
  6119. * @return void
  6120. */
  6121. public function testPostLink() {
  6122. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6123. $this->assertTags($result, array(
  6124. 'form' => array(
  6125. 'method' => 'post', 'action' => '/posts/delete/1',
  6126. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6127. ),
  6128. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6129. '/form',
  6130. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6131. 'Delete',
  6132. '/a'
  6133. ));
  6134. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6135. $this->assertTags($result, array(
  6136. 'form' => array(
  6137. 'method' => 'post', 'action' => '/posts/delete/1',
  6138. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6139. ),
  6140. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6141. '/form',
  6142. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6143. 'Delete',
  6144. '/a'
  6145. ));
  6146. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6147. $this->assertTags($result, array(
  6148. 'form' => array(
  6149. 'method' => 'post', 'action' => '/posts/delete/1',
  6150. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6151. ),
  6152. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6153. '/form',
  6154. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6155. 'Delete',
  6156. '/a'
  6157. ));
  6158. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6159. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6160. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6161. $this->assertTags($result, array(
  6162. 'form' => array(
  6163. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6164. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6165. ),
  6166. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6167. '/form',
  6168. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6169. 'Delete',
  6170. '/a'
  6171. ));
  6172. }
  6173. /**
  6174. * Test that postLink adds _Token fields.
  6175. *
  6176. * @return void
  6177. */
  6178. public function testSecurePostLink() {
  6179. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6180. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6181. $expected = array(
  6182. 'form' => array(
  6183. 'method' => 'post', 'action' => '/posts/delete/1',
  6184. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6185. ),
  6186. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6187. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6188. 'div' => array('style' => 'display:none;'),
  6189. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6190. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6191. '/div',
  6192. '/form',
  6193. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6194. 'Delete',
  6195. '/a'
  6196. );
  6197. $this->assertTags($result, $expected);
  6198. }
  6199. /**
  6200. * testSubmitButton method
  6201. *
  6202. * @return void
  6203. */
  6204. public function testSubmitButton() {
  6205. $result = $this->Form->submit('');
  6206. $expected = array(
  6207. 'div' => array('class' => 'submit'),
  6208. 'input' => array('type' => 'submit', 'value' => ''),
  6209. '/div'
  6210. );
  6211. $this->assertTags($result, $expected);
  6212. $result = $this->Form->submit('Test Submit');
  6213. $expected = array(
  6214. 'div' => array('class' => 'submit'),
  6215. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6216. '/div'
  6217. );
  6218. $this->assertTags($result, $expected);
  6219. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6220. $expected = array(
  6221. 'span' => array('class' => 'submit'),
  6222. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6223. '/span'
  6224. );
  6225. $this->assertTags($result, $expected);
  6226. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6227. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6228. $this->assertTags($result, $expected);
  6229. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6230. $expected = array(
  6231. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6232. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6233. '/div'
  6234. );
  6235. $this->assertTags($result, $expected);
  6236. $result = $this->Form->submit('Next >');
  6237. $expected = array(
  6238. 'div' => array('class' => 'submit'),
  6239. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6240. '/div'
  6241. );
  6242. $this->assertTags($result, $expected);
  6243. $result = $this->Form->submit('Next >', array('escape' => false));
  6244. $expected = array(
  6245. 'div' => array('class' => 'submit'),
  6246. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6247. '/div'
  6248. );
  6249. $this->assertTags($result, $expected);
  6250. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6251. $expected = array(
  6252. 'div' => array('class' => 'submit'),
  6253. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6254. '/div'
  6255. );
  6256. $this->assertTags($result, $expected);
  6257. $before = '--before--';
  6258. $after = '--after--';
  6259. $result = $this->Form->submit('Test', array('before' => $before));
  6260. $expected = array(
  6261. 'div' => array('class' => 'submit'),
  6262. '--before--',
  6263. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6264. '/div'
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. $result = $this->Form->submit('Test', array('after' => $after));
  6268. $expected = array(
  6269. 'div' => array('class' => 'submit'),
  6270. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6271. '--after--',
  6272. '/div'
  6273. );
  6274. $this->assertTags($result, $expected);
  6275. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6276. $expected = array(
  6277. 'div' => array('class' => 'submit'),
  6278. '--before--',
  6279. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6280. '--after--',
  6281. '/div'
  6282. );
  6283. $this->assertTags($result, $expected);
  6284. }
  6285. /**
  6286. * test image submit types.
  6287. *
  6288. * @return void
  6289. */
  6290. public function testSubmitImage() {
  6291. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6292. $expected = array(
  6293. 'div' => array('class' => 'submit'),
  6294. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6295. '/div'
  6296. );
  6297. $this->assertTags($result, $expected);
  6298. $result = $this->Form->submit('/relative/cake.power.gif');
  6299. $expected = array(
  6300. 'div' => array('class' => 'submit'),
  6301. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6302. '/div'
  6303. );
  6304. $this->assertTags($result, $expected);
  6305. $result = $this->Form->submit('cake.power.gif');
  6306. $expected = array(
  6307. 'div' => array('class' => 'submit'),
  6308. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6309. '/div'
  6310. );
  6311. $this->assertTags($result, $expected);
  6312. $result = $this->Form->submit('Not.an.image');
  6313. $expected = array(
  6314. 'div' => array('class' => 'submit'),
  6315. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6316. '/div'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. $after = '--after--';
  6320. $before = '--before--';
  6321. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6322. $expected = array(
  6323. 'div' => array('class' => 'submit'),
  6324. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6325. '--after--',
  6326. '/div'
  6327. );
  6328. $this->assertTags($result, $expected);
  6329. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6330. $expected = array(
  6331. 'div' => array('class' => 'submit'),
  6332. '--before--',
  6333. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6334. '/div'
  6335. );
  6336. $this->assertTags($result, $expected);
  6337. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6338. $expected = array(
  6339. 'div' => array('class' => 'submit'),
  6340. '--before--',
  6341. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6342. '--after--',
  6343. '/div'
  6344. );
  6345. $this->assertTags($result, $expected);
  6346. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6347. $expected = array(
  6348. 'div' => array('class' => 'submit'),
  6349. '--before--',
  6350. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6351. '--after--',
  6352. '/div'
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. }
  6356. /**
  6357. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6358. * be submitted at a time.
  6359. *
  6360. * @return void
  6361. */
  6362. public function testSubmitUnlockedByDefault() {
  6363. $this->Form->request->params['_Token']['key'] = 'secured';
  6364. $this->Form->submit('Go go');
  6365. $this->Form->submit('Save', array('name' => 'save'));
  6366. $result = $this->Form->unlockField();
  6367. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6368. }
  6369. /**
  6370. * Test submit image with timestamps.
  6371. *
  6372. * @return void
  6373. */
  6374. public function testSubmitImageTimestamp() {
  6375. Configure::write('Asset.timestamp', 'force');
  6376. $result = $this->Form->submit('cake.power.gif');
  6377. $expected = array(
  6378. 'div' => array('class' => 'submit'),
  6379. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6380. '/div'
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. }
  6384. /**
  6385. * test the create() method
  6386. *
  6387. * @return void
  6388. */
  6389. public function testCreate() {
  6390. $result = $this->Form->create('Contact');
  6391. $encoding = strtolower(Configure::read('App.encoding'));
  6392. $expected = array(
  6393. 'form' => array(
  6394. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6395. 'accept-charset' => $encoding
  6396. ),
  6397. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6398. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6399. '/div'
  6400. );
  6401. $this->assertTags($result, $expected);
  6402. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6403. $expected = array('form' => array(
  6404. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6405. 'accept-charset' => $encoding
  6406. ));
  6407. $this->assertTags($result, $expected);
  6408. $result = $this->Form->create('Contact', array('type' => 'get'));
  6409. $expected = array('form' => array(
  6410. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6411. 'accept-charset' => $encoding
  6412. ));
  6413. $this->assertTags($result, $expected);
  6414. $result = $this->Form->create('Contact', array('type' => 'put'));
  6415. $expected = array(
  6416. 'form' => array(
  6417. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6418. 'accept-charset' => $encoding
  6419. ),
  6420. 'div' => array('style' => 'display:none;'),
  6421. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6422. '/div'
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. $result = $this->Form->create('Contact', array('type' => 'file'));
  6426. $expected = array(
  6427. 'form' => array(
  6428. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6429. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  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. $this->Form->request->data['Contact']['id'] = 1;
  6437. $this->Form->request->here = '/contacts/edit/1';
  6438. $this->Form->request['action'] = 'edit';
  6439. $result = $this->Form->create('Contact');
  6440. $expected = array(
  6441. 'form' => array(
  6442. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6443. 'accept-charset' => $encoding
  6444. ),
  6445. 'div' => array('style' => 'display:none;'),
  6446. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6447. '/div'
  6448. );
  6449. $this->assertTags($result, $expected);
  6450. $this->Form->request->data['Contact']['id'] = 1;
  6451. $this->Form->request->here = '/contacts/edit/1';
  6452. $this->Form->request['action'] = 'edit';
  6453. $result = $this->Form->create('Contact', array('type' => 'file'));
  6454. $expected = array(
  6455. 'form' => array(
  6456. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6457. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6458. ),
  6459. 'div' => array('style' => 'display:none;'),
  6460. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6461. '/div'
  6462. );
  6463. $this->assertTags($result, $expected);
  6464. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6465. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6466. $expected = array(
  6467. 'form' => array(
  6468. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6469. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6470. ),
  6471. 'div' => array('style' => 'display:none;'),
  6472. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6473. '/div'
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6477. $expected = array(
  6478. 'form' => array(
  6479. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6480. 'accept-charset' => $encoding
  6481. ),
  6482. 'div' => array('style' => 'display:none;'),
  6483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6484. '/div'
  6485. );
  6486. $this->assertTags($result, $expected);
  6487. $this->Form->request['action'] = 'add';
  6488. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6489. $expected = array(
  6490. 'form' => array(
  6491. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6492. 'accept-charset' => $encoding
  6493. ),
  6494. 'div' => array('style' => 'display:none;'),
  6495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6496. '/div'
  6497. );
  6498. $this->assertTags($result, $expected);
  6499. $result = $this->Form->create('User', array('action' => 'login'));
  6500. $expected = array(
  6501. 'form' => array(
  6502. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6503. 'accept-charset' => $encoding
  6504. ),
  6505. 'div' => array('style' => 'display:none;'),
  6506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6507. '/div'
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. $result = $this->Form->create('User', array('url' => '/users/login'));
  6511. $expected = array(
  6512. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6513. 'div' => array('style' => 'display:none;'),
  6514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6515. '/div'
  6516. );
  6517. $this->assertTags($result, $expected);
  6518. $this->Form->request['controller'] = 'pages';
  6519. $result = $this->Form->create('User', array('action' => 'signup'));
  6520. $expected = array(
  6521. 'form' => array(
  6522. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6523. 'accept-charset' => $encoding
  6524. ),
  6525. 'div' => array('style' => 'display:none;'),
  6526. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6527. '/div'
  6528. );
  6529. $this->assertTags($result, $expected);
  6530. $this->Form->request->data = array();
  6531. $this->Form->request['controller'] = 'contacts';
  6532. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6533. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6534. $expected = array(
  6535. 'form' => array(
  6536. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6537. 'accept-charset' => 'utf-8'
  6538. ),
  6539. 'div' => array('style' => 'display:none;'),
  6540. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6541. '/div'
  6542. );
  6543. $this->assertTags($result, $expected);
  6544. }
  6545. /**
  6546. * Test the onsubmit option for create()
  6547. *
  6548. * @return void
  6549. */
  6550. public function testCreateOnSubmit() {
  6551. $this->Form->request->data = array();
  6552. $this->Form->request['controller'] = 'contacts';
  6553. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6554. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6555. $expected = array(
  6556. 'form' => array(
  6557. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6558. 'accept-charset' => 'utf-8'
  6559. ),
  6560. 'div' => array('style' => 'display:none;'),
  6561. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6562. '/div'
  6563. );
  6564. $this->assertTags($result, $expected);
  6565. $this->Form->request->data = array();
  6566. $this->Form->request['controller'] = 'contacts';
  6567. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6568. $result = $this->Form->create(array(
  6569. 'url' => array('action' => 'index', 'param'),
  6570. 'default' => false,
  6571. 'onsubmit' => 'someFunction();'
  6572. ));
  6573. $expected = array(
  6574. 'form' => array(
  6575. 'id' => 'ContactAddForm', 'method' => 'post',
  6576. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6577. 'action' => '/contacts/index/param',
  6578. 'accept-charset' => 'utf-8'
  6579. ),
  6580. 'div' => array('style' => 'display:none;'),
  6581. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. }
  6586. /**
  6587. * test create() with automatic url generation
  6588. *
  6589. * @return void
  6590. */
  6591. public function testCreateAutoUrl() {
  6592. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6593. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6594. $this->Form->request->base = '/base_url';
  6595. $result = $this->Form->create('Contact');
  6596. $expected = array(
  6597. 'form' => array(
  6598. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6599. 'accept-charset' => 'utf-8'
  6600. ),
  6601. 'div' => array('style' => 'display:none;'),
  6602. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6603. '/div'
  6604. );
  6605. $this->assertTags($result, $expected);
  6606. $this->Form->request['action'] = 'delete';
  6607. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6608. $this->Form->request->base = '/base_url';
  6609. $result = $this->Form->create('Contact');
  6610. $expected = array(
  6611. 'form' => array(
  6612. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6613. 'accept-charset' => 'utf-8'
  6614. ),
  6615. 'div' => array('style' => 'display:none;'),
  6616. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6617. '/div'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. }
  6621. /**
  6622. * test create() with a custom route
  6623. *
  6624. * @return void
  6625. */
  6626. public function testCreateCustomRoute() {
  6627. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6628. $encoding = strtolower(Configure::read('App.encoding'));
  6629. $result = $this->Form->create('User', array('action' => 'login'));
  6630. $expected = array(
  6631. 'form' => array(
  6632. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6633. 'accept-charset' => $encoding
  6634. ),
  6635. 'div' => array('style' => 'display:none;'),
  6636. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6637. '/div'
  6638. );
  6639. $this->assertTags($result, $expected);
  6640. }
  6641. /**
  6642. * test that inputDefaults are stored and used.
  6643. *
  6644. * @return void
  6645. */
  6646. public function testCreateWithInputDefaults() {
  6647. $this->Form->create('User', array(
  6648. 'inputDefaults' => array(
  6649. 'div' => false,
  6650. 'label' => false,
  6651. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6652. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6653. )
  6654. ));
  6655. $result = $this->Form->input('username');
  6656. $expected = array(
  6657. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6658. );
  6659. $this->assertTags($result, $expected);
  6660. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6661. $expected = array(
  6662. 'div' => array('class' => 'input text'),
  6663. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6664. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6665. '/div'
  6666. );
  6667. $this->assertTags($result, $expected);
  6668. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6669. $expected = array(
  6670. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6671. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6672. );
  6673. $this->assertTags($result, $expected);
  6674. $this->Form->create('User', array(
  6675. 'inputDefaults' => array(
  6676. 'div' => false,
  6677. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6678. )
  6679. ));
  6680. $result = $this->Form->input('username', array('div' => true));
  6681. $expected = array(
  6682. 'div' => array('class' => 'input text'),
  6683. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6684. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6685. '/div'
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. }
  6689. /**
  6690. * test automatic accept-charset overriding
  6691. *
  6692. * @return void
  6693. */
  6694. public function testCreateWithAcceptCharset() {
  6695. $result = $this->Form->create('UserForm', array(
  6696. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6697. )
  6698. );
  6699. $expected = array(
  6700. 'form' => array(
  6701. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6702. 'accept-charset' => 'iso-8859-1'
  6703. ),
  6704. 'div' => array('style' => 'display:none;'),
  6705. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6706. '/div'
  6707. );
  6708. $this->assertTags($result, $expected);
  6709. }
  6710. /**
  6711. * Test base form url when url param is passed with multiple parameters (&)
  6712. *
  6713. */
  6714. public function testCreateQuerystringrequest() {
  6715. $encoding = strtolower(Configure::read('App.encoding'));
  6716. $result = $this->Form->create('Contact', array(
  6717. 'type' => 'post',
  6718. 'escape' => false,
  6719. 'url' => array(
  6720. 'controller' => 'controller',
  6721. 'action' => 'action',
  6722. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6723. )
  6724. ));
  6725. $expected = array(
  6726. 'form' => array(
  6727. 'id' => 'ContactAddForm',
  6728. 'method' => 'post',
  6729. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6730. 'accept-charset' => $encoding
  6731. ),
  6732. 'div' => array('style' => 'display:none;'),
  6733. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6734. '/div'
  6735. );
  6736. $this->assertTags($result, $expected);
  6737. $result = $this->Form->create('Contact', array(
  6738. 'type' => 'post',
  6739. 'url' => array(
  6740. 'controller' => 'controller',
  6741. 'action' => 'action',
  6742. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6743. )
  6744. ));
  6745. $expected = array(
  6746. 'form' => array(
  6747. 'id' => 'ContactAddForm',
  6748. 'method' => 'post',
  6749. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6750. 'accept-charset' => $encoding
  6751. ),
  6752. 'div' => array('style' => 'display:none;'),
  6753. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6754. '/div'
  6755. );
  6756. $this->assertTags($result, $expected);
  6757. }
  6758. /**
  6759. * test that create() doesn't cause errors by multiple id's being in the primary key
  6760. * as could happen with multiple select or checkboxes.
  6761. *
  6762. * @return void
  6763. */
  6764. public function testCreateWithMultipleIdInData() {
  6765. $encoding = strtolower(Configure::read('App.encoding'));
  6766. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6767. $result = $this->Form->create('Contact');
  6768. $expected = array(
  6769. 'form' => array(
  6770. 'id' => 'ContactAddForm',
  6771. 'method' => 'post',
  6772. 'action' => '/contacts/add',
  6773. 'accept-charset' => $encoding
  6774. ),
  6775. 'div' => array('style' => 'display:none;'),
  6776. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6777. '/div'
  6778. );
  6779. $this->assertTags($result, $expected);
  6780. }
  6781. /**
  6782. * test that create() doesn't add in extra passed params.
  6783. *
  6784. * @return void
  6785. */
  6786. public function testCreatePassedArgs() {
  6787. $encoding = strtolower(Configure::read('App.encoding'));
  6788. $this->Form->request->data['Contact']['id'] = 1;
  6789. $result = $this->Form->create('Contact', array(
  6790. 'type' => 'post',
  6791. 'escape' => false,
  6792. 'url' => array(
  6793. 'action' => 'edit',
  6794. 'myparam'
  6795. )
  6796. ));
  6797. $expected = array(
  6798. 'form' => array(
  6799. 'id' => 'ContactAddForm',
  6800. 'method' => 'post',
  6801. 'action' => '/contacts/edit/myparam',
  6802. 'accept-charset' => $encoding
  6803. ),
  6804. 'div' => array('style' => 'display:none;'),
  6805. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6806. '/div'
  6807. );
  6808. $this->assertTags($result, $expected);
  6809. }
  6810. /**
  6811. * test creating a get form, and get form inputs.
  6812. *
  6813. * @return void
  6814. */
  6815. public function testGetFormCreate() {
  6816. $encoding = strtolower(Configure::read('App.encoding'));
  6817. $result = $this->Form->create('Contact', array('type' => 'get'));
  6818. $this->assertTags($result, array('form' => array(
  6819. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6820. 'accept-charset' => $encoding
  6821. )));
  6822. $result = $this->Form->text('Contact.name');
  6823. $this->assertTags($result, array('input' => array(
  6824. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6825. )));
  6826. $result = $this->Form->password('password');
  6827. $this->assertTags($result, array('input' => array(
  6828. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6829. )));
  6830. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6831. $result = $this->Form->text('user_form');
  6832. $this->assertTags($result, array('input' => array(
  6833. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6834. )));
  6835. }
  6836. /**
  6837. * test get form, and inputs when the model param is false
  6838. *
  6839. * @return void
  6840. */
  6841. public function testGetFormWithFalseModel() {
  6842. $encoding = strtolower(Configure::read('App.encoding'));
  6843. $this->Form->request['controller'] = 'contact_test';
  6844. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6845. $expected = array('form' => array(
  6846. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6847. 'accept-charset' => $encoding
  6848. ));
  6849. $this->assertTags($result, $expected);
  6850. $result = $this->Form->text('reason');
  6851. $expected = array(
  6852. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6853. );
  6854. $this->assertTags($result, $expected);
  6855. }
  6856. /**
  6857. * test that datetime() works with GET style forms.
  6858. *
  6859. * @return void
  6860. */
  6861. public function testDateTimeWithGetForms() {
  6862. extract($this->dateRegex);
  6863. $this->Form->create('Contact', array('type' => 'get'));
  6864. $result = $this->Form->datetime('created');
  6865. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6866. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6867. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6868. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6869. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6870. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6871. }
  6872. /**
  6873. * testEditFormWithData method
  6874. *
  6875. * test auto populating form elements from submitted data.
  6876. *
  6877. * @return void
  6878. */
  6879. public function testEditFormWithData() {
  6880. $this->Form->request->data = array('Person' => array(
  6881. 'id' => 1,
  6882. 'first_name' => 'Nate',
  6883. 'last_name' => 'Abele',
  6884. 'email' => 'nate@example.com'
  6885. ));
  6886. $this->Form->request->addParams(array(
  6887. 'models' => array('Person'),
  6888. 'controller' => 'people',
  6889. 'action' => 'add'
  6890. ));
  6891. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6892. $this->Form->create();
  6893. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6894. $expected = array(
  6895. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6896. 'select' => array(
  6897. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6898. ),
  6899. array('option' => array('value' => 1)), 'Nate', '/option',
  6900. array('option' => array('value' => 2)), 'Garrett', '/option',
  6901. array('option' => array('value' => 3)), 'Larry', '/option',
  6902. '/select'
  6903. );
  6904. $this->assertTags($result, $expected);
  6905. }
  6906. /**
  6907. * Test that required fields are created for various types of validation.
  6908. *
  6909. * @return void
  6910. */
  6911. public function testFormInputRequiredDetection() {
  6912. $this->Form->create('Contact');
  6913. $result = $this->Form->input('Contact.non_existing');
  6914. $expected = array(
  6915. 'div' => array('class' => 'input text'),
  6916. 'label' => array('for' => 'ContactNonExisting'),
  6917. 'Non Existing',
  6918. '/label',
  6919. 'input' => array(
  6920. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6921. 'id' => 'ContactNonExisting'
  6922. ),
  6923. '/div'
  6924. );
  6925. $this->assertTags($result, $expected);
  6926. $result = $this->Form->input('Contact.imrequired');
  6927. $expected = array(
  6928. 'div' => array('class' => 'input text required'),
  6929. 'label' => array('for' => 'ContactImrequired'),
  6930. 'Imrequired',
  6931. '/label',
  6932. 'input' => array(
  6933. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6934. 'id' => 'ContactImrequired',
  6935. 'required' => 'required'
  6936. ),
  6937. '/div'
  6938. );
  6939. $this->assertTags($result, $expected);
  6940. $result = $this->Form->input('Contact.imalsorequired');
  6941. $expected = array(
  6942. 'div' => array('class' => 'input text required'),
  6943. 'label' => array('for' => 'ContactImalsorequired'),
  6944. 'Imalsorequired',
  6945. '/label',
  6946. 'input' => array(
  6947. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6948. 'id' => 'ContactImalsorequired',
  6949. 'required' => 'required'
  6950. ),
  6951. '/div'
  6952. );
  6953. $this->assertTags($result, $expected);
  6954. $result = $this->Form->input('Contact.imrequiredtoo');
  6955. $expected = array(
  6956. 'div' => array('class' => 'input text required'),
  6957. 'label' => array('for' => 'ContactImrequiredtoo'),
  6958. 'Imrequiredtoo',
  6959. '/label',
  6960. 'input' => array(
  6961. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6962. 'id' => 'ContactImrequiredtoo',
  6963. 'required' => 'required'
  6964. ),
  6965. '/div'
  6966. );
  6967. $this->assertTags($result, $expected);
  6968. $result = $this->Form->input('Contact.required_one');
  6969. $expected = array(
  6970. 'div' => array('class' => 'input text required'),
  6971. 'label' => array('for' => 'ContactRequiredOne'),
  6972. 'Required One',
  6973. '/label',
  6974. 'input' => array(
  6975. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6976. 'id' => 'ContactRequiredOne',
  6977. 'required' => 'required'
  6978. ),
  6979. '/div'
  6980. );
  6981. $this->assertTags($result, $expected);
  6982. $result = $this->Form->input('Contact.string_required');
  6983. $expected = array(
  6984. 'div' => array('class' => 'input text required'),
  6985. 'label' => array('for' => 'ContactStringRequired'),
  6986. 'String Required',
  6987. '/label',
  6988. 'input' => array(
  6989. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6990. 'id' => 'ContactStringRequired',
  6991. 'required' => 'required'
  6992. ),
  6993. '/div'
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. $result = $this->Form->input('Contact.imnotrequired');
  6997. $expected = array(
  6998. 'div' => array('class' => 'input text'),
  6999. 'label' => array('for' => 'ContactImnotrequired'),
  7000. 'Imnotrequired',
  7001. '/label',
  7002. 'input' => array(
  7003. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7004. 'id' => 'ContactImnotrequired'
  7005. ),
  7006. '/div'
  7007. );
  7008. $this->assertTags($result, $expected);
  7009. $result = $this->Form->input('Contact.imalsonotrequired');
  7010. $expected = array(
  7011. 'div' => array('class' => 'input text'),
  7012. 'label' => array('for' => 'ContactImalsonotrequired'),
  7013. 'Imalsonotrequired',
  7014. '/label',
  7015. 'input' => array(
  7016. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7017. 'id' => 'ContactImalsonotrequired'
  7018. ),
  7019. '/div'
  7020. );
  7021. $this->assertTags($result, $expected);
  7022. $result = $this->Form->input('Contact.imalsonotrequired2');
  7023. $expected = array(
  7024. 'div' => array('class' => 'input text'),
  7025. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7026. 'Imalsonotrequired2',
  7027. '/label',
  7028. 'input' => array(
  7029. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7030. 'id' => 'ContactImalsonotrequired2'
  7031. ),
  7032. '/div'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. $result = $this->Form->input('Contact.imnotrequiredeither');
  7036. $expected = array(
  7037. 'div' => array('class' => 'input text'),
  7038. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7039. 'Imnotrequiredeither',
  7040. '/label',
  7041. 'input' => array(
  7042. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7043. 'id' => 'ContactImnotrequiredeither'
  7044. ),
  7045. '/div'
  7046. );
  7047. $this->assertTags($result, $expected);
  7048. $result = $this->Form->input('Contact.iamrequiredalways');
  7049. $expected = array(
  7050. 'div' => array('class' => 'input text required'),
  7051. 'label' => array('for' => 'ContactIamrequiredalways'),
  7052. 'Iamrequiredalways',
  7053. '/label',
  7054. 'input' => array(
  7055. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7056. 'id' => 'ContactIamrequiredalways',
  7057. 'required' => 'required'
  7058. ),
  7059. '/div'
  7060. );
  7061. $this->assertTags($result, $expected);
  7062. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7063. $expected = array(
  7064. 'div' => array('class' => 'input checkbox required'),
  7065. array('input' => array(
  7066. 'type' => 'hidden',
  7067. 'name' => 'data[Contact][boolean_field]',
  7068. 'id' => 'ContactBooleanField_',
  7069. 'value' => '0'
  7070. )),
  7071. array('input' => array(
  7072. 'type' => 'checkbox',
  7073. 'name' => 'data[Contact][boolean_field]',
  7074. 'value' => '1',
  7075. 'id' => 'ContactBooleanField'
  7076. )),
  7077. 'label' => array('for' => 'ContactBooleanField'),
  7078. 'Boolean Field',
  7079. '/label',
  7080. '/div'
  7081. );
  7082. $this->assertTags($result, $expected);
  7083. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7084. $expected = array(
  7085. 'div' => array('class' => 'input checkbox required'),
  7086. array('input' => array(
  7087. 'type' => 'hidden',
  7088. 'name' => 'data[Contact][boolean_field]',
  7089. 'id' => 'ContactBooleanField_',
  7090. 'value' => '0'
  7091. )),
  7092. array('input' => array(
  7093. 'type' => 'checkbox',
  7094. 'name' => 'data[Contact][boolean_field]',
  7095. 'value' => '1',
  7096. 'id' => 'ContactBooleanField',
  7097. 'required' => 'required'
  7098. )),
  7099. 'label' => array('for' => 'ContactBooleanField'),
  7100. 'Boolean Field',
  7101. '/label',
  7102. '/div'
  7103. );
  7104. $this->assertTags($result, $expected);
  7105. }
  7106. /**
  7107. * testFormMagicInput method
  7108. *
  7109. * @return void
  7110. */
  7111. public function testFormMagicInput() {
  7112. $encoding = strtolower(Configure::read('App.encoding'));
  7113. $result = $this->Form->create('Contact');
  7114. $expected = array(
  7115. 'form' => array(
  7116. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7117. 'accept-charset' => $encoding
  7118. ),
  7119. 'div' => array('style' => 'display:none;'),
  7120. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7121. '/div'
  7122. );
  7123. $this->assertTags($result, $expected);
  7124. $result = $this->Form->input('name');
  7125. $expected = array(
  7126. 'div' => array('class' => 'input text'),
  7127. 'label' => array('for' => 'ContactName'),
  7128. 'Name',
  7129. '/label',
  7130. 'input' => array(
  7131. 'type' => 'text', 'name' => 'data[Contact][name]',
  7132. 'id' => 'ContactName', 'maxlength' => '255'
  7133. ),
  7134. '/div'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. $result = $this->Form->input('non_existing_field_in_contact_model');
  7138. $expected = array(
  7139. 'div' => array('class' => 'input text'),
  7140. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7141. 'Non Existing Field In Contact Model',
  7142. '/label',
  7143. 'input' => array(
  7144. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7145. 'id' => 'ContactNonExistingFieldInContactModel'
  7146. ),
  7147. '/div'
  7148. );
  7149. $this->assertTags($result, $expected);
  7150. $result = $this->Form->input('Address.street');
  7151. $expected = array(
  7152. 'div' => array('class' => 'input text'),
  7153. 'label' => array('for' => 'AddressStreet'),
  7154. 'Street',
  7155. '/label',
  7156. 'input' => array(
  7157. 'type' => 'text', 'name' => 'data[Address][street]',
  7158. 'id' => 'AddressStreet'
  7159. ),
  7160. '/div'
  7161. );
  7162. $this->assertTags($result, $expected);
  7163. $result = $this->Form->input('Address.non_existing_field_in_model');
  7164. $expected = array(
  7165. 'div' => array('class' => 'input text'),
  7166. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7167. 'Non Existing Field In Model',
  7168. '/label',
  7169. 'input' => array(
  7170. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7171. 'id' => 'AddressNonExistingFieldInModel'
  7172. ),
  7173. '/div'
  7174. );
  7175. $this->assertTags($result, $expected);
  7176. $result = $this->Form->input('name', array('div' => false));
  7177. $expected = array(
  7178. 'label' => array('for' => 'ContactName'),
  7179. 'Name',
  7180. '/label',
  7181. 'input' => array(
  7182. 'type' => 'text', 'name' => 'data[Contact][name]',
  7183. 'id' => 'ContactName', 'maxlength' => '255'
  7184. )
  7185. );
  7186. $this->assertTags($result, $expected);
  7187. extract($this->dateRegex);
  7188. $now = strtotime('now');
  7189. $result = $this->Form->input('Contact.published', array('div' => false));
  7190. $expected = array(
  7191. 'label' => array('for' => 'ContactPublishedMonth'),
  7192. 'Published',
  7193. '/label',
  7194. array('select' => array(
  7195. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7196. )),
  7197. $monthsRegex,
  7198. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7199. date('F', $now),
  7200. '/option',
  7201. '*/select',
  7202. '-',
  7203. array('select' => array(
  7204. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7205. )),
  7206. $daysRegex,
  7207. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7208. date('j', $now),
  7209. '/option',
  7210. '*/select',
  7211. '-',
  7212. array('select' => array(
  7213. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7214. )),
  7215. $yearsRegex,
  7216. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7217. date('Y', $now),
  7218. '*/select'
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. $result = $this->Form->input('Contact.updated', array('div' => false));
  7222. $expected = array(
  7223. 'label' => array('for' => 'ContactUpdatedMonth'),
  7224. 'Updated',
  7225. '/label',
  7226. array('select' => array(
  7227. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7228. )),
  7229. $monthsRegex,
  7230. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7231. date('F', $now),
  7232. '/option',
  7233. '*/select',
  7234. '-',
  7235. array('select' => array(
  7236. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7237. )),
  7238. $daysRegex,
  7239. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7240. date('j', $now),
  7241. '/option',
  7242. '*/select',
  7243. '-',
  7244. array('select' => array(
  7245. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7246. )),
  7247. $yearsRegex,
  7248. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7249. date('Y', $now),
  7250. '*/select'
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. $result = $this->Form->input('UserForm.stuff');
  7254. $expected = array(
  7255. 'div' => array('class' => 'input text'),
  7256. 'label' => array('for' => 'UserFormStuff'),
  7257. 'Stuff',
  7258. '/label',
  7259. 'input' => array(
  7260. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7261. 'id' => 'UserFormStuff', 'maxlength' => 10
  7262. ),
  7263. '/div'
  7264. );
  7265. $this->assertTags($result, $expected);
  7266. }
  7267. /**
  7268. * testForMagicInputNonExistingNorValidated method
  7269. *
  7270. * @return void
  7271. */
  7272. public function testForMagicInputNonExistingNorValidated() {
  7273. $encoding = strtolower(Configure::read('App.encoding'));
  7274. $result = $this->Form->create('Contact');
  7275. $expected = array(
  7276. 'form' => array(
  7277. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7278. 'accept-charset' => $encoding
  7279. ),
  7280. 'div' => array('style' => 'display:none;'),
  7281. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7282. '/div'
  7283. );
  7284. $this->assertTags($result, $expected);
  7285. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7286. $expected = array(
  7287. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7288. 'Non Existing Nor Validated',
  7289. '/label',
  7290. 'input' => array(
  7291. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7292. 'id' => 'ContactNonExistingNorValidated'
  7293. )
  7294. );
  7295. $this->assertTags($result, $expected);
  7296. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7297. 'div' => false, 'value' => 'my value'
  7298. ));
  7299. $expected = array(
  7300. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7301. 'Non Existing Nor Validated',
  7302. '/label',
  7303. 'input' => array(
  7304. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7305. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7306. )
  7307. );
  7308. $this->assertTags($result, $expected);
  7309. $this->Form->request->data = array(
  7310. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7311. ));
  7312. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7313. $expected = array(
  7314. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7315. 'Non Existing Nor Validated',
  7316. '/label',
  7317. 'input' => array(
  7318. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7319. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7320. )
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. }
  7324. /**
  7325. * testFormMagicInputLabel method
  7326. *
  7327. * @return void
  7328. */
  7329. public function testFormMagicInputLabel() {
  7330. $encoding = strtolower(Configure::read('App.encoding'));
  7331. $result = $this->Form->create('Contact');
  7332. $expected = array(
  7333. 'form' => array(
  7334. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7335. 'accept-charset' => $encoding
  7336. ),
  7337. 'div' => array('style' => 'display:none;'),
  7338. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7343. $this->assertTags($result, array('input' => array(
  7344. 'name' => 'data[Contact][name]', 'type' => 'text',
  7345. 'id' => 'ContactName', 'maxlength' => '255')
  7346. ));
  7347. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7348. $expected = array(
  7349. 'label' => array('for' => 'ContactName'),
  7350. 'My label',
  7351. '/label',
  7352. 'input' => array(
  7353. 'type' => 'text', 'name' => 'data[Contact][name]',
  7354. 'id' => 'ContactName', 'maxlength' => '255'
  7355. )
  7356. );
  7357. $this->assertTags($result, $expected);
  7358. $result = $this->Form->input('Contact.name', array(
  7359. 'div' => false, 'label' => array('class' => 'mandatory')
  7360. ));
  7361. $expected = array(
  7362. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7363. 'Name',
  7364. '/label',
  7365. 'input' => array(
  7366. 'type' => 'text', 'name' => 'data[Contact][name]',
  7367. 'id' => 'ContactName', 'maxlength' => '255'
  7368. )
  7369. );
  7370. $this->assertTags($result, $expected);
  7371. $result = $this->Form->input('Contact.name', array(
  7372. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7373. ));
  7374. $expected = array(
  7375. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7376. 'My label',
  7377. '/label',
  7378. 'input' => array(
  7379. 'type' => 'text', 'name' => 'data[Contact][name]',
  7380. 'id' => 'ContactName', 'maxlength' => '255'
  7381. )
  7382. );
  7383. $this->assertTags($result, $expected);
  7384. $result = $this->Form->input('Contact.name', array(
  7385. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7386. ));
  7387. $expected = array(
  7388. 'label' => array('for' => 'my_id'),
  7389. 'Name',
  7390. '/label',
  7391. 'input' => array(
  7392. 'type' => 'text', 'name' => 'data[Contact][name]',
  7393. 'id' => 'my_id', 'maxlength' => '255'
  7394. )
  7395. );
  7396. $this->assertTags($result, $expected);
  7397. $result = $this->Form->input('1.id');
  7398. $this->assertTags($result, array('input' => array(
  7399. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7400. 'id' => 'Contact1Id'
  7401. )));
  7402. $result = $this->Form->input("1.name");
  7403. $expected = array(
  7404. 'div' => array('class' => 'input text'),
  7405. 'label' => array('for' => 'Contact1Name'),
  7406. 'Name',
  7407. '/label',
  7408. 'input' => array(
  7409. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7410. 'id' => 'Contact1Name', 'maxlength' => '255'
  7411. ),
  7412. '/div'
  7413. );
  7414. $this->assertTags($result, $expected);
  7415. $result = $this->Form->input('Contact.1.id');
  7416. $this->assertTags($result, array(
  7417. 'input' => array(
  7418. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7419. 'id' => 'Contact1Id'
  7420. )
  7421. ));
  7422. $result = $this->Form->input("Model.1.name");
  7423. $expected = array(
  7424. 'div' => array('class' => 'input text'),
  7425. 'label' => array('for' => 'Model1Name'),
  7426. 'Name',
  7427. '/label',
  7428. 'input' => array(
  7429. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7430. 'id' => 'Model1Name'
  7431. ),
  7432. '/div'
  7433. );
  7434. $this->assertTags($result, $expected);
  7435. }
  7436. /**
  7437. * testFormEnd method
  7438. *
  7439. * @return void
  7440. */
  7441. public function testFormEnd() {
  7442. $this->assertEquals('</form>', $this->Form->end());
  7443. $result = $this->Form->end('');
  7444. $expected = array(
  7445. 'div' => array('class' => 'submit'),
  7446. 'input' => array('type' => 'submit', 'value' => ''),
  7447. '/div',
  7448. '/form'
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $result = $this->Form->end(array('label' => ''));
  7452. $expected = array(
  7453. 'div' => array('class' => 'submit'),
  7454. 'input' => array('type' => 'submit', 'value' => ''),
  7455. '/div',
  7456. '/form'
  7457. );
  7458. $this->assertTags($result, $expected);
  7459. $result = $this->Form->end('save');
  7460. $expected = array(
  7461. 'div' => array('class' => 'submit'),
  7462. 'input' => array('type' => 'submit', 'value' => 'save'),
  7463. '/div',
  7464. '/form'
  7465. );
  7466. $this->assertTags($result, $expected);
  7467. $result = $this->Form->end(array('label' => 'save'));
  7468. $expected = array(
  7469. 'div' => array('class' => 'submit'),
  7470. 'input' => array('type' => 'submit', 'value' => 'save'),
  7471. '/div',
  7472. '/form'
  7473. );
  7474. $this->assertTags($result, $expected);
  7475. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7476. $expected = array(
  7477. 'div' => array('class' => 'submit'),
  7478. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7479. '/div',
  7480. '/form'
  7481. );
  7482. $this->assertTags($result, $expected);
  7483. $result = $this->Form->end(array('name' => 'Whatever'));
  7484. $expected = array(
  7485. 'div' => array('class' => 'submit'),
  7486. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7487. '/div',
  7488. '/form'
  7489. );
  7490. $this->assertTags($result, $expected);
  7491. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7492. $expected = array(
  7493. 'div' => array('class' => 'good'),
  7494. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7495. '/div',
  7496. '/form'
  7497. );
  7498. $this->assertTags($result, $expected);
  7499. $result = $this->Form->end(array(
  7500. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7501. ));
  7502. $expected = array(
  7503. 'div' => array('class' => 'good'),
  7504. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7505. '/div',
  7506. '/form'
  7507. );
  7508. $this->assertTags($result, $expected);
  7509. }
  7510. /**
  7511. * testMultipleFormWithIdFields method
  7512. *
  7513. * @return void
  7514. */
  7515. public function testMultipleFormWithIdFields() {
  7516. $this->Form->create('UserForm');
  7517. $result = $this->Form->input('id');
  7518. $this->assertTags($result, array('input' => array(
  7519. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7520. )));
  7521. $result = $this->Form->input('ValidateItem.id');
  7522. $this->assertTags($result, array('input' => array(
  7523. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7524. 'id' => 'ValidateItemId'
  7525. )));
  7526. $result = $this->Form->input('ValidateUser.id');
  7527. $this->assertTags($result, array('input' => array(
  7528. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7529. 'id' => 'ValidateUserId'
  7530. )));
  7531. }
  7532. /**
  7533. * testDbLessModel method
  7534. *
  7535. * @return void
  7536. */
  7537. public function testDbLessModel() {
  7538. $this->Form->create('TestMail');
  7539. $result = $this->Form->input('name');
  7540. $expected = array(
  7541. 'div' => array('class' => 'input text'),
  7542. 'label' => array('for' => 'TestMailName'),
  7543. 'Name',
  7544. '/label',
  7545. 'input' => array(
  7546. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7547. 'id' => 'TestMailName'
  7548. ),
  7549. '/div'
  7550. );
  7551. $this->assertTags($result, $expected);
  7552. ClassRegistry::init('TestMail');
  7553. $this->Form->create('TestMail');
  7554. $result = $this->Form->input('name');
  7555. $expected = array(
  7556. 'div' => array('class' => 'input text'),
  7557. 'label' => array('for' => 'TestMailName'),
  7558. 'Name',
  7559. '/label',
  7560. 'input' => array(
  7561. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7562. 'id' => 'TestMailName'
  7563. ),
  7564. '/div'
  7565. );
  7566. $this->assertTags($result, $expected);
  7567. }
  7568. /**
  7569. * testBrokenness method
  7570. *
  7571. * @return void
  7572. */
  7573. public function testBrokenness() {
  7574. /*
  7575. * #4 This test has two parents and four children. By default (as of r7117) both
  7576. * parents are show but the first parent is missing a child. This is the inconsistency
  7577. * in the default behaviour - one parent has all children, the other does not - dependent
  7578. * on the data values.
  7579. */
  7580. $result = $this->Form->select('Model.field', array(
  7581. 'Fred' => array(
  7582. 'freds_son_1' => 'Fred',
  7583. 'freds_son_2' => 'Freddie'
  7584. ),
  7585. 'Bert' => array(
  7586. 'berts_son_1' => 'Albert',
  7587. 'berts_son_2' => 'Bertie')
  7588. ),
  7589. array('showParents' => true, 'empty' => false)
  7590. );
  7591. $expected = array(
  7592. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7593. array('optgroup' => array('label' => 'Fred')),
  7594. array('option' => array('value' => 'freds_son_1')),
  7595. 'Fred',
  7596. '/option',
  7597. array('option' => array('value' => 'freds_son_2')),
  7598. 'Freddie',
  7599. '/option',
  7600. '/optgroup',
  7601. array('optgroup' => array('label' => 'Bert')),
  7602. array('option' => array('value' => 'berts_son_1')),
  7603. 'Albert',
  7604. '/option',
  7605. array('option' => array('value' => 'berts_son_2')),
  7606. 'Bertie',
  7607. '/option',
  7608. '/optgroup',
  7609. '/select'
  7610. );
  7611. $this->assertTags($result, $expected);
  7612. /*
  7613. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7614. * changed, so we should expect the same select list data, just with a different name
  7615. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7616. * This is where data corruption can occur, because when a select value is missing from
  7617. * a list a form will substitute the first value in the list - without the user knowing.
  7618. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7619. * affect the availability of 3 => 'Three' as a valid option.
  7620. */
  7621. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7622. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7623. ));
  7624. $result = $this->Form->select(
  7625. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7626. );
  7627. $expected = array(
  7628. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7629. array('option' => array('value' => 1)),
  7630. 'One',
  7631. '/option',
  7632. array('option' => array('value' => 2)),
  7633. 'Two',
  7634. '/option',
  7635. array('optgroup' => array('label' => 'Three')),
  7636. array('option' => array('value' => 3)),
  7637. 'Three',
  7638. '/option',
  7639. array('option' => array('value' => 4)),
  7640. 'Four',
  7641. '/option',
  7642. array('option' => array('value' => 5)),
  7643. 'Five',
  7644. '/option',
  7645. '/optgroup',
  7646. '/select'
  7647. );
  7648. $this->assertTags($result, $expected);
  7649. }
  7650. /**
  7651. * Test the generation of fields for a multi record form.
  7652. *
  7653. * @return void
  7654. */
  7655. public function testMultiRecordForm() {
  7656. $this->Form->create('ValidateProfile');
  7657. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7658. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7659. $expected = array(
  7660. 'div' => array('class' => 'input textarea'),
  7661. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7662. 'Name',
  7663. '/label',
  7664. 'textarea' => array(
  7665. 'id' => 'ValidateProfile1ValidateItem2Name',
  7666. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7667. 'cols' => 30,
  7668. 'rows' => 6
  7669. ),
  7670. 'Value',
  7671. '/textarea',
  7672. '/div'
  7673. );
  7674. $this->assertTags($result, $expected);
  7675. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7676. $expected = array(
  7677. 'div' => array('class' => 'input date'),
  7678. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7679. 'Created',
  7680. '/label',
  7681. array('select' => array(
  7682. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7683. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7684. )
  7685. ),
  7686. array('option' => array('value' => '')), '/option',
  7687. $this->dateRegex['monthsRegex'],
  7688. '/select', '-',
  7689. array('select' => array(
  7690. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7691. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7692. )
  7693. ),
  7694. array('option' => array('value' => '')), '/option',
  7695. $this->dateRegex['daysRegex'],
  7696. '/select', '-',
  7697. array('select' => array(
  7698. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7699. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7700. )
  7701. ),
  7702. array('option' => array('value' => '')), '/option',
  7703. $this->dateRegex['yearsRegex'],
  7704. '/select',
  7705. '/div'
  7706. );
  7707. $this->assertTags($result, $expected);
  7708. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7709. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7710. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7711. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7712. $expected = array(
  7713. 'div' => array('class' => 'input select error'),
  7714. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7715. 'Profile',
  7716. '/label',
  7717. 'select' => array(
  7718. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7719. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7720. 'class' => 'form-error'
  7721. ),
  7722. '/select',
  7723. array('div' => array('class' => 'error-message')),
  7724. 'Error',
  7725. '/div',
  7726. '/div'
  7727. );
  7728. $this->assertTags($result, $expected);
  7729. }
  7730. /**
  7731. * test the correct display of multi-record form validation errors.
  7732. *
  7733. * @return void
  7734. */
  7735. public function testMultiRecordFormValidationErrors() {
  7736. $this->Form->create('ValidateProfile');
  7737. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7738. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7739. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7740. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7741. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7742. $result = $this->Form->error('ValidateProfile.2.city');
  7743. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7744. $result = $this->Form->error('2.city');
  7745. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7746. }
  7747. /**
  7748. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7749. *
  7750. * @return void
  7751. */
  7752. public function testInputTemplate() {
  7753. $result = $this->Form->input('Contact.email', array(
  7754. 'type' => 'text', 'format' => array('input')
  7755. ));
  7756. $expected = array(
  7757. 'div' => array('class' => 'input text'),
  7758. 'input' => array(
  7759. 'type' => 'text', 'name' => 'data[Contact][email]',
  7760. 'id' => 'ContactEmail'
  7761. ),
  7762. '/div'
  7763. );
  7764. $this->assertTags($result, $expected);
  7765. $result = $this->Form->input('Contact.email', array(
  7766. 'type' => 'text', 'format' => array('input', 'label'),
  7767. 'label' => '<em>Email (required)</em>'
  7768. ));
  7769. $expected = array(
  7770. 'div' => array('class' => 'input text'),
  7771. array('input' => array(
  7772. 'type' => 'text', 'name' => 'data[Contact][email]',
  7773. 'id' => 'ContactEmail'
  7774. )),
  7775. 'label' => array('for' => 'ContactEmail'),
  7776. 'em' => array(),
  7777. 'Email (required)',
  7778. '/em',
  7779. '/label',
  7780. '/div'
  7781. );
  7782. $this->assertTags($result, $expected);
  7783. $result = $this->Form->input('Contact.email', array(
  7784. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7785. 'between' => '<div>Something in the middle</div>',
  7786. 'after' => '<span>Some text at the end</span>'
  7787. ));
  7788. $expected = array(
  7789. 'div' => array('class' => 'input text'),
  7790. array('input' => array(
  7791. 'type' => 'text', 'name' => 'data[Contact][email]',
  7792. 'id' => 'ContactEmail'
  7793. )),
  7794. array('div' => array()),
  7795. 'Something in the middle',
  7796. '/div',
  7797. 'label' => array('for' => 'ContactEmail'),
  7798. 'Email',
  7799. '/label',
  7800. 'span' => array(),
  7801. 'Some text at the end',
  7802. '/span',
  7803. '/div'
  7804. );
  7805. $this->assertTags($result, $expected);
  7806. $result = $this->Form->input('Contact.method', array(
  7807. 'type' => 'radio',
  7808. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7809. 'between' => 'I am between',
  7810. ));
  7811. $expected = array(
  7812. 'div' => array('class' => 'input radio'),
  7813. 'fieldset' => array(),
  7814. 'legend' => array(),
  7815. 'Method',
  7816. '/legend',
  7817. 'I am between',
  7818. 'input' => array(
  7819. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7820. 'value' => '', 'id' => 'ContactMethod_'
  7821. ),
  7822. array('input' => array(
  7823. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7824. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7825. )),
  7826. array('label' => array('for' => 'ContactMethodEmail')),
  7827. 'Email',
  7828. '/label',
  7829. array('input' => array(
  7830. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7831. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7832. )),
  7833. array('label' => array('for' => 'ContactMethodPigeon')),
  7834. 'Pigeon',
  7835. '/label',
  7836. '/fieldset',
  7837. '/div',
  7838. );
  7839. $this->assertTags($result, $expected);
  7840. }
  7841. /**
  7842. * test that some html5 inputs + FormHelper::__call() work
  7843. *
  7844. * @return void
  7845. */
  7846. public function testHtml5Inputs() {
  7847. $result = $this->Form->email('User.email');
  7848. $expected = array(
  7849. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7850. );
  7851. $this->assertTags($result, $expected);
  7852. $result = $this->Form->search('User.query');
  7853. $expected = array(
  7854. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. $result = $this->Form->search('User.query', array('value' => 'test'));
  7858. $expected = array(
  7859. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7860. );
  7861. $this->assertTags($result, $expected);
  7862. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7863. $expected = array(
  7864. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7865. );
  7866. $this->assertTags($result, $expected);
  7867. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7868. $expected = array(
  7869. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7870. );
  7871. $this->assertTags($result, $expected);
  7872. }
  7873. /**
  7874. *
  7875. * @expectedException CakeException
  7876. * @return void
  7877. */
  7878. public function testHtml5InputException() {
  7879. $this->Form->email();
  7880. }
  7881. /**
  7882. * Tests that a model can be loaded from the model names passed in the request object
  7883. *
  7884. * @return void
  7885. */
  7886. public function testIntrospectModelFromRequest() {
  7887. $this->loadFixtures('Post');
  7888. App::build(array(
  7889. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7890. ));
  7891. CakePlugin::load('TestPlugin');
  7892. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7893. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7894. $this->Form->create('TestPluginPost');
  7895. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7896. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7897. CakePlugin::unload();
  7898. App::build();
  7899. }
  7900. /**
  7901. * Tests that it is possible to set the validation errors directly in the helper for a field
  7902. *
  7903. * @return void
  7904. */
  7905. public function testCustomValidationErrors() {
  7906. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7907. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7908. $this->assertEquals('Badness!', $result);
  7909. }
  7910. /**
  7911. * Tests that the 'on' key validates as expected on create
  7912. *
  7913. * @return void
  7914. */
  7915. public function testRequiredOnCreate() {
  7916. $this->Form->create('Contact');
  7917. $result = $this->Form->input('Contact.imrequiredonupdate');
  7918. $expected = array(
  7919. 'div' => array('class' => 'input text'),
  7920. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7921. 'Imrequiredonupdate',
  7922. '/label',
  7923. 'input' => array(
  7924. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7925. 'id' => 'ContactImrequiredonupdate'
  7926. ),
  7927. '/div'
  7928. );
  7929. $this->assertTags($result, $expected);
  7930. $result = $this->Form->input('Contact.imrequiredoncreate');
  7931. $expected = array(
  7932. 'div' => array('class' => 'input text required'),
  7933. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7934. 'Imrequiredoncreate',
  7935. '/label',
  7936. 'input' => array(
  7937. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7938. 'id' => 'ContactImrequiredoncreate',
  7939. 'required' => 'required'
  7940. ),
  7941. '/div'
  7942. );
  7943. $this->assertTags($result, $expected);
  7944. $result = $this->Form->input('Contact.imrequiredonboth');
  7945. $expected = array(
  7946. 'div' => array('class' => 'input text required'),
  7947. 'label' => array('for' => 'ContactImrequiredonboth'),
  7948. 'Imrequiredonboth',
  7949. '/label',
  7950. 'input' => array(
  7951. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7952. 'id' => 'ContactImrequiredonboth',
  7953. 'required' => 'required'
  7954. ),
  7955. '/div'
  7956. );
  7957. $this->assertTags($result, $expected);
  7958. $this->Form->inputDefaults(array('required' => false));
  7959. $result = $this->Form->input('Contact.imrequired');
  7960. $expected = array(
  7961. 'div' => array('class' => 'input text required'),
  7962. 'label' => array('for' => 'ContactImrequired'),
  7963. 'Imrequired',
  7964. '/label',
  7965. 'input' => array(
  7966. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7967. 'id' => 'ContactImrequired'
  7968. ),
  7969. '/div'
  7970. );
  7971. $this->assertTags($result, $expected);
  7972. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7973. $this->assertTags($result, $expected);
  7974. }
  7975. /**
  7976. * Tests that the 'on' key validates as expected on update
  7977. *
  7978. * @return void
  7979. */
  7980. public function testRequiredOnUpdate() {
  7981. $this->Form->request->data['Contact']['id'] = 1;
  7982. $this->Form->create('Contact');
  7983. $result = $this->Form->input('Contact.imrequiredonupdate');
  7984. $expected = array(
  7985. 'div' => array('class' => 'input text required'),
  7986. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7987. 'Imrequiredonupdate',
  7988. '/label',
  7989. 'input' => array(
  7990. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7991. 'id' => 'ContactImrequiredonupdate',
  7992. 'required' => 'required'
  7993. ),
  7994. '/div'
  7995. );
  7996. $this->assertTags($result, $expected);
  7997. $result = $this->Form->input('Contact.imrequiredoncreate');
  7998. $expected = array(
  7999. 'div' => array('class' => 'input text'),
  8000. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8001. 'Imrequiredoncreate',
  8002. '/label',
  8003. 'input' => array(
  8004. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8005. 'id' => 'ContactImrequiredoncreate'
  8006. ),
  8007. '/div'
  8008. );
  8009. $this->assertTags($result, $expected);
  8010. $result = $this->Form->input('Contact.imrequiredonboth');
  8011. $expected = array(
  8012. 'div' => array('class' => 'input text required'),
  8013. 'label' => array('for' => 'ContactImrequiredonboth'),
  8014. 'Imrequiredonboth',
  8015. '/label',
  8016. 'input' => array(
  8017. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8018. 'id' => 'ContactImrequiredonboth',
  8019. 'required' => 'required'
  8020. ),
  8021. '/div'
  8022. );
  8023. $this->assertTags($result, $expected);
  8024. $result = $this->Form->input('Contact.imrequired');
  8025. $expected = array(
  8026. 'div' => array('class' => 'input text required'),
  8027. 'label' => array('for' => 'ContactImrequired'),
  8028. 'Imrequired',
  8029. '/label',
  8030. 'input' => array(
  8031. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8032. 'id' => 'ContactImrequired',
  8033. 'required' => 'required'
  8034. ),
  8035. '/div'
  8036. );
  8037. $this->assertTags($result, $expected);
  8038. }
  8039. /**
  8040. * Test inputDefaults setter and getter
  8041. *
  8042. * @return void
  8043. */
  8044. public function testInputDefaults() {
  8045. $this->Form->create('Contact');
  8046. $this->Form->inputDefaults(array(
  8047. 'label' => false,
  8048. 'div' => array(
  8049. 'style' => 'color: #000;'
  8050. )
  8051. ));
  8052. $result = $this->Form->input('Contact.field1');
  8053. $expected = array(
  8054. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8055. 'input' => array(
  8056. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8057. 'id' => 'ContactField1'
  8058. ),
  8059. '/div'
  8060. );
  8061. $this->assertTags($result, $expected);
  8062. $this->Form->inputDefaults(array(
  8063. 'div' => false,
  8064. 'label' => 'Label',
  8065. ));
  8066. $result = $this->Form->input('Contact.field1');
  8067. $expected = array(
  8068. 'label' => array('for' => 'ContactField1'),
  8069. 'Label',
  8070. '/label',
  8071. 'input' => array(
  8072. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8073. 'id' => 'ContactField1'
  8074. ),
  8075. );
  8076. $this->assertTags($result, $expected);
  8077. $this->Form->inputDefaults(array(
  8078. 'label' => false,
  8079. ), true);
  8080. $result = $this->Form->input('Contact.field1');
  8081. $expected = array(
  8082. 'input' => array(
  8083. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8084. 'id' => 'ContactField1'
  8085. ),
  8086. );
  8087. $this->assertTags($result, $expected);
  8088. $result = $this->Form->inputDefaults();
  8089. $expected = array(
  8090. 'div' => false,
  8091. 'label' => false,
  8092. );
  8093. $this->assertEqual($result, $expected);
  8094. }
  8095. }