FormHelperTest.php 318 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  319. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  320. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $secureAttributes = array('form' => 'MyTestForm');
  613. $this->Form->request['_Token'] = array('key' => $key);
  614. $result = $this->Form->secure($fields, $secureAttributes);
  615. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  616. $hash .= ':' . 'Model.valid';
  617. $hash = urlencode($hash);
  618. $expected = array(
  619. 'div' => array('style' => 'display:none;'),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  622. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
  623. 'form' => 'MyTestForm',
  624. )),
  625. array('input' => array(
  626. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  627. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
  628. 'form' => 'MyTestForm',
  629. )),
  630. '/div'
  631. );
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * Tests correct generation of number fields for double and float fields
  636. *
  637. * @return void
  638. */
  639. public function testTextFieldGenerationForFloats() {
  640. $model = ClassRegistry::getObject('Contact');
  641. $model->setSchema(array('foo' => array(
  642. 'type' => 'float',
  643. 'null' => false,
  644. 'default' => null,
  645. 'length' => 10
  646. )));
  647. $this->Form->create('Contact');
  648. $result = $this->Form->input('foo');
  649. $expected = array(
  650. 'div' => array('class' => 'input number'),
  651. 'label' => array('for' => 'ContactFoo'),
  652. 'Foo',
  653. '/label',
  654. array('input' => array(
  655. 'type' => 'number',
  656. 'name' => 'data[Contact][foo]',
  657. 'id' => 'ContactFoo',
  658. 'step' => 'any'
  659. )),
  660. '/div'
  661. );
  662. $this->assertTags($result, $expected);
  663. $result = $this->Form->input('foo', array('step' => 0.5));
  664. $expected = array(
  665. 'div' => array('class' => 'input number'),
  666. 'label' => array('for' => 'ContactFoo'),
  667. 'Foo',
  668. '/label',
  669. array('input' => array(
  670. 'type' => 'number',
  671. 'name' => 'data[Contact][foo]',
  672. 'id' => 'ContactFoo',
  673. 'step' => '0.5'
  674. )),
  675. '/div'
  676. );
  677. $this->assertTags($result, $expected);
  678. }
  679. /**
  680. * Tests correct generation of number fields for integer fields
  681. *
  682. * @return void
  683. */
  684. public function testTextFieldTypeNumberGenerationForIntegers() {
  685. $model = ClassRegistry::getObject('Contact');
  686. $model->setSchema(array('foo' => array(
  687. 'type' => 'integer',
  688. 'null' => false,
  689. 'default' => null,
  690. 'length' => null
  691. )));
  692. $this->Form->create('Contact');
  693. $result = $this->Form->input('foo');
  694. $expected = array(
  695. 'div' => array('class' => 'input number'),
  696. 'label' => array('for' => 'ContactFoo'),
  697. 'Foo',
  698. '/label',
  699. array('input' => array(
  700. 'type' => 'number', 'name' => 'data[Contact][foo]',
  701. 'id' => 'ContactFoo'
  702. )),
  703. '/div'
  704. );
  705. $this->assertTags($result, $expected);
  706. }
  707. /**
  708. * Tests correct generation of file upload fields for binary fields
  709. *
  710. * @return void
  711. */
  712. public function testFileUploadFieldTypeGenerationForBinaries() {
  713. $model = ClassRegistry::getObject('Contact');
  714. $model->setSchema(array('foo' => array(
  715. 'type' => 'binary',
  716. 'null' => false,
  717. 'default' => null,
  718. 'length' => 1024
  719. )));
  720. $this->Form->create('Contact');
  721. $result = $this->Form->input('foo');
  722. $expected = array(
  723. 'div' => array('class' => 'input file'),
  724. 'label' => array('for' => 'ContactFoo'),
  725. 'Foo',
  726. '/label',
  727. array('input' => array(
  728. 'type' => 'file', 'name' => 'data[Contact][foo]',
  729. 'id' => 'ContactFoo'
  730. )),
  731. '/div'
  732. );
  733. $this->assertTags($result, $expected);
  734. }
  735. /**
  736. * testFormSecurityMultipleFields method
  737. *
  738. * Test secure() with multiple row form. Ensure hash is correct.
  739. *
  740. * @return void
  741. */
  742. public function testFormSecurityMultipleFields() {
  743. $key = 'testKey';
  744. $fields = array(
  745. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  746. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  747. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  748. );
  749. $this->Form->request['_Token'] = array('key' => $key);
  750. $result = $this->Form->secure($fields);
  751. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  752. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  753. $expected = array(
  754. 'div' => array('style' => 'display:none;'),
  755. array('input' => array(
  756. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  757. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  758. )),
  759. array('input' => array(
  760. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  761. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  762. )),
  763. '/div'
  764. );
  765. $this->assertTags($result, $expected);
  766. }
  767. /**
  768. * testFormSecurityMultipleSubmitButtons
  769. *
  770. * test form submit generation and ensure that _Token is only created on end()
  771. *
  772. * @return void
  773. */
  774. public function testFormSecurityMultipleSubmitButtons() {
  775. $key = 'testKey';
  776. $this->Form->request['_Token'] = array('key' => $key);
  777. $this->Form->create('Addresses');
  778. $this->Form->input('Address.title');
  779. $this->Form->input('Address.first_name');
  780. $result = $this->Form->submit('Save', array('name' => 'save'));
  781. $expected = array(
  782. 'div' => array('class' => 'submit'),
  783. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  784. '/div',
  785. );
  786. $this->assertTags($result, $expected);
  787. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  788. $expected = array(
  789. 'div' => array('class' => 'submit'),
  790. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  791. '/div',
  792. );
  793. $this->assertTags($result, $expected);
  794. $result = $this->Form->end(null);
  795. $expected = array(
  796. 'div' => array('style' => 'display:none;'),
  797. array('input' => array(
  798. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  799. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  800. )),
  801. array('input' => array(
  802. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  803. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  804. )),
  805. '/div'
  806. );
  807. $this->assertTags($result, $expected);
  808. }
  809. /**
  810. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  811. *
  812. * @return void
  813. */
  814. public function testSecurityButtonNestedNamed() {
  815. $key = 'testKey';
  816. $this->Form->request['_Token'] = array('key' => $key);
  817. $this->Form->create('Addresses');
  818. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  819. $result = $this->Form->unlockField();
  820. $this->assertEquals(array('Address.button'), $result);
  821. }
  822. /**
  823. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  824. *
  825. * @return void
  826. */
  827. public function testSecuritySubmitNestedNamed() {
  828. $key = 'testKey';
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $this->Form->create('Addresses');
  831. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  832. $result = $this->Form->unlockField();
  833. $this->assertEquals(array('Address.button'), $result);
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with no names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageNoName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png');
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * Test that the correct fields are unlocked for image submits with names.
  855. *
  856. * @return void
  857. */
  858. public function testSecuritySubmitImageName() {
  859. $key = 'testKey';
  860. $this->Form->request['_Token'] = array('key' => $key);
  861. $this->Form->create('User');
  862. $result = $this->Form->submit('save.png', array('name' => 'test'));
  863. $expected = array(
  864. 'div' => array('class' => 'submit'),
  865. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  866. '/div'
  867. );
  868. $this->assertTags($result, $expected);
  869. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  870. }
  871. /**
  872. * testFormSecurityMultipleInputFields method
  873. *
  874. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  875. *
  876. * @return void
  877. */
  878. public function testFormSecurityMultipleInputFields() {
  879. $key = 'testKey';
  880. $this->Form->request['_Token'] = array('key' => $key);
  881. $this->Form->create('Addresses');
  882. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  883. $this->Form->input('Addresses.0.title');
  884. $this->Form->input('Addresses.0.first_name');
  885. $this->Form->input('Addresses.0.last_name');
  886. $this->Form->input('Addresses.0.address');
  887. $this->Form->input('Addresses.0.city');
  888. $this->Form->input('Addresses.0.phone');
  889. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  890. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  891. $this->Form->input('Addresses.1.title');
  892. $this->Form->input('Addresses.1.first_name');
  893. $this->Form->input('Addresses.1.last_name');
  894. $this->Form->input('Addresses.1.address');
  895. $this->Form->input('Addresses.1.city');
  896. $this->Form->input('Addresses.1.phone');
  897. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  898. $result = $this->Form->secure($this->Form->fields);
  899. $hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
  900. $expected = array(
  901. 'div' => array('style' => 'display:none;'),
  902. array('input' => array(
  903. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  904. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  905. )),
  906. array('input' => array(
  907. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  908. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  909. )),
  910. '/div'
  911. );
  912. $this->assertTags($result, $expected);
  913. }
  914. /**
  915. * Test form security with Model.field.0 style inputs
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityArrayFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token']['key'] = $key;
  922. $this->Form->create('Address');
  923. $this->Form->input('Address.primary.1');
  924. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  925. $this->Form->input('Address.secondary.1.0');
  926. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  927. }
  928. /**
  929. * testFormSecurityMultipleInputDisabledFields method
  930. *
  931. * test secure form generation with multiple records and disabled fields.
  932. *
  933. * @return void
  934. */
  935. public function testFormSecurityMultipleInputDisabledFields() {
  936. $key = 'testKey';
  937. $this->Form->request->params['_Token'] = array(
  938. 'key' => $key,
  939. 'unlockedFields' => array('first_name', 'address')
  940. );
  941. $this->Form->create();
  942. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  943. $this->Form->input('Addresses.0.title');
  944. $this->Form->input('Addresses.0.first_name');
  945. $this->Form->input('Addresses.0.last_name');
  946. $this->Form->input('Addresses.0.address');
  947. $this->Form->input('Addresses.0.city');
  948. $this->Form->input('Addresses.0.phone');
  949. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  950. $this->Form->input('Addresses.1.title');
  951. $this->Form->input('Addresses.1.first_name');
  952. $this->Form->input('Addresses.1.last_name');
  953. $this->Form->input('Addresses.1.address');
  954. $this->Form->input('Addresses.1.city');
  955. $this->Form->input('Addresses.1.phone');
  956. $result = $this->Form->secure($this->Form->fields);
  957. $hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
  958. $expected = array(
  959. 'div' => array('style' => 'display:none;'),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  962. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  963. )),
  964. array('input' => array(
  965. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  966. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  967. )),
  968. '/div'
  969. );
  970. $this->assertTags($result, $expected);
  971. }
  972. /**
  973. * testFormSecurityInputDisabledFields method
  974. *
  975. * Test single record form with disabled fields.
  976. *
  977. * @return void
  978. */
  979. public function testFormSecurityInputUnlockedFields() {
  980. $key = 'testKey';
  981. $this->Form->request['_Token'] = array(
  982. 'key' => $key,
  983. 'unlockedFields' => array('first_name', 'address')
  984. );
  985. $this->Form->create();
  986. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  987. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  988. $this->Form->input('Addresses.title');
  989. $this->Form->input('Addresses.first_name');
  990. $this->Form->input('Addresses.last_name');
  991. $this->Form->input('Addresses.address');
  992. $this->Form->input('Addresses.city');
  993. $this->Form->input('Addresses.phone');
  994. $result = $this->Form->fields;
  995. $expected = array(
  996. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  997. 'Addresses.city', 'Addresses.phone'
  998. );
  999. $this->assertEquals($expected, $result);
  1000. $result = $this->Form->secure($expected);
  1001. $hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
  1002. $expected = array(
  1003. 'div' => array('style' => 'display:none;'),
  1004. array('input' => array(
  1005. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1006. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1007. )),
  1008. array('input' => array(
  1009. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1010. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1011. )),
  1012. '/div'
  1013. );
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * test securing inputs with custom name attributes.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testFormSecureWithCustomNameAttribute() {
  1022. $this->Form->request->params['_Token']['key'] = 'testKey';
  1023. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1024. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1025. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1026. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1027. }
  1028. /**
  1029. * testFormSecuredInput method
  1030. *
  1031. * Test generation of entire secure form, assertions made on input() output.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testFormSecuredInput() {
  1036. $this->Form->request['_Token'] = array('key' => 'testKey');
  1037. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1038. $encoding = strtolower(Configure::read('App.encoding'));
  1039. $expected = array(
  1040. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1041. 'div' => array('style' => 'display:none;'),
  1042. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1043. array('input' => array(
  1044. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1045. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1046. )),
  1047. '/div'
  1048. );
  1049. $this->assertTags($result, $expected);
  1050. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1051. $expected = array(
  1052. 'div' => array('class' => 'input text'),
  1053. 'label' => array('for' => 'UserFormPublished'),
  1054. 'Published',
  1055. '/label',
  1056. array('input' => array(
  1057. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1058. 'id' => 'UserFormPublished'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1064. $expected = array(
  1065. 'div' => array('class' => 'input text'),
  1066. 'label' => array('for' => 'UserFormOther'),
  1067. 'Other',
  1068. '/label',
  1069. array('input' => array(
  1070. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1071. 'id' => 'UserFormOther'
  1072. )),
  1073. '/div'
  1074. );
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->hidden('UserForm.stuff');
  1077. $expected = array(
  1078. 'input' => array(
  1079. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1080. 'id' => 'UserFormStuff'
  1081. ));
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1084. $expected = array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1086. 'value' => '0', 'id' => 'UserFormHidden'
  1087. ));
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1090. $expected = array(
  1091. 'div' => array('class' => 'input checkbox'),
  1092. array('input' => array(
  1093. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1094. 'value' => '0', 'id' => 'UserFormSomething_'
  1095. )),
  1096. array('input' => array(
  1097. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1098. 'value' => '1', 'id' => 'UserFormSomething'
  1099. )),
  1100. 'label' => array('for' => 'UserFormSomething'),
  1101. 'Something',
  1102. '/label',
  1103. '/div'
  1104. );
  1105. $this->assertTags($result, $expected);
  1106. $result = $this->Form->fields;
  1107. $expected = array(
  1108. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1109. 'UserForm.hidden' => '0', 'UserForm.something'
  1110. );
  1111. $this->assertEquals($expected, $result);
  1112. $hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
  1113. $result = $this->Form->secure($this->Form->fields);
  1114. $expected = array(
  1115. 'div' => array('style' => 'display:none;'),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1118. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1119. )),
  1120. array('input' => array(
  1121. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1122. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. }
  1128. /**
  1129. * Test secured inputs with custom names.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testSecuredInputCustomName() {
  1134. $this->Form->request['_Token'] = array('key' => 'testKey');
  1135. $this->assertEquals(array(), $this->Form->fields);
  1136. $this->Form->input('text_input', array(
  1137. 'name' => 'data[Option][General.default_role]',
  1138. ));
  1139. $expected = array('Option.General.default_role');
  1140. $this->assertEquals($expected, $this->Form->fields);
  1141. $this->Form->input('select_box', array(
  1142. 'name' => 'data[Option][General.select_role]',
  1143. 'type' => 'select',
  1144. 'options' => array(1, 2),
  1145. ));
  1146. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * Tests that the correct keys are added to the field hash index
  1151. *
  1152. * @return void
  1153. */
  1154. public function testSecuredFileInput() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $this->Form->file('Attachment.file');
  1158. $expected = array(
  1159. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1160. 'Attachment.file.error', 'Attachment.file.size'
  1161. );
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * test that multiple selects keys are added to field hash
  1166. *
  1167. * @return void
  1168. */
  1169. public function testSecuredMultipleSelect() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'one', '2' => 'two');
  1173. $this->Form->select('Model.select', $options);
  1174. $expected = array('Model.select');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. $this->Form->fields = array();
  1177. $this->Form->select('Model.select', $options, array('multiple' => true));
  1178. $this->assertEquals($expected, $this->Form->fields);
  1179. }
  1180. /**
  1181. * testFormSecuredRadio method
  1182. *
  1183. * @return void
  1184. */
  1185. public function testSecuredRadio() {
  1186. $this->Form->request['_Token'] = array('key' => 'testKey');
  1187. $this->assertEquals(array(), $this->Form->fields);
  1188. $options = array('1' => 'option1', '2' => 'option2');
  1189. $this->Form->radio('Test.test', $options);
  1190. $expected = array('Test.test');
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * Test that when disabled is in a list based attribute array it works.
  1195. *
  1196. * @return void
  1197. */
  1198. public function testSecuredAndDisabledNotAssoc() {
  1199. $this->Form->request['_Token'] = array('key' => 'testKey');
  1200. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1201. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1202. $this->Form->text('Model.text', array('disabled'));
  1203. $this->Form->textarea('Model.textarea', array('disabled'));
  1204. $this->Form->password('Model.password', array('disabled'));
  1205. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1206. $expected = array(
  1207. 'Model.radio' => ''
  1208. );
  1209. $this->assertEquals($expected, $this->Form->fields);
  1210. }
  1211. /**
  1212. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1213. * hashing.
  1214. *
  1215. * @return void
  1216. */
  1217. public function testSecuredAndDisabled() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1220. $this->Form->text('Model.text', array('disabled' => true));
  1221. $this->Form->password('Model.text', array('disabled' => true));
  1222. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1223. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1224. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1225. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1226. $this->Form->month('Model.month', array('disabled' => true));
  1227. $this->Form->day('Model.day', array('disabled' => true));
  1228. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1229. $this->Form->minute('Model.minute', array('disabled' => true));
  1230. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1231. $expected = array(
  1232. 'Model.radio' => ''
  1233. );
  1234. $this->assertEquals($expected, $this->Form->fields);
  1235. }
  1236. /**
  1237. * Test that only the path + query elements of a form's URL show up in their hash.
  1238. *
  1239. * @return void
  1240. */
  1241. public function testSecuredFormUrlIgnoresHost() {
  1242. $this->Form->request['_Token'] = array('key' => 'testKey');
  1243. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1244. $this->Form->create('Address', array(
  1245. 'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
  1246. ));
  1247. $result = $this->Form->secure();
  1248. $this->assertContains($expected, $result);
  1249. $this->Form->create('Address', array('url' => 'http://localhost/articles/view/1?page=1'));
  1250. $result = $this->Form->secure();
  1251. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1252. $this->Form->create('Address', array('url' => '/articles/view/1?page=1'));
  1253. $result = $this->Form->secure();
  1254. $this->assertContains($expected, $result, 'URL path + query should work.');
  1255. $this->Form->create('Address', array('url' => '/articles/view/1'));
  1256. $result = $this->Form->secure();
  1257. $this->assertNotContains($expected, $result, 'URL is different');
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $this->assertEquals($expected, $result);
  1329. $this->Form->setEntity('Contact.1.email');
  1330. $result = $this->Form->tagIsInvalid();
  1331. $this->assertFalse($result);
  1332. $this->Form->setEntity('Contact.0.name');
  1333. $result = $this->Form->tagIsInvalid();
  1334. $this->assertFalse($result);
  1335. }
  1336. /**
  1337. * Test tagIsInvalid with validation errors from a saveMany
  1338. *
  1339. * @return void
  1340. */
  1341. public function testTagIsInvalidSaveMany() {
  1342. $Contact = ClassRegistry::getObject('Contact');
  1343. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1344. $this->Form->create('Contact');
  1345. $this->Form->setEntity('0.email');
  1346. $result = $this->Form->tagIsInvalid();
  1347. $this->assertEquals($expected, $result);
  1348. $this->Form->setEntity('0.Contact.email');
  1349. $result = $this->Form->tagIsInvalid();
  1350. $this->assertEquals($expected, $result);
  1351. }
  1352. /**
  1353. * Test validation errors.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testPasswordValidation() {
  1358. $Contact = ClassRegistry::getObject('Contact');
  1359. $Contact->validationErrors['password'] = array('Please provide a password');
  1360. $result = $this->Form->input('Contact.password');
  1361. $expected = array(
  1362. 'div' => array('class' => 'input password error'),
  1363. 'label' => array('for' => 'ContactPassword'),
  1364. 'Password',
  1365. '/label',
  1366. 'input' => array(
  1367. 'type' => 'password', 'name' => 'data[Contact][password]',
  1368. 'id' => 'ContactPassword', 'class' => 'form-error'
  1369. ),
  1370. array('div' => array('class' => 'error-message')),
  1371. 'Please provide a password',
  1372. '/div',
  1373. '/div'
  1374. );
  1375. $this->assertTags($result, $expected);
  1376. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. '/div'
  1387. );
  1388. $this->assertTags($result, $expected);
  1389. }
  1390. /**
  1391. * Test validation errors, when validation message is an empty string.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testEmptyErrorValidation() {
  1396. $this->Form->validationErrors['Contact']['password'] = '';
  1397. $result = $this->Form->input('Contact.password');
  1398. $expected = array(
  1399. 'div' => array('class' => 'input password error'),
  1400. 'label' => array('for' => 'ContactPassword'),
  1401. 'Password',
  1402. '/label',
  1403. 'input' => array(
  1404. 'type' => 'password', 'name' => 'data[Contact][password]',
  1405. 'id' => 'ContactPassword', 'class' => 'form-error'
  1406. ),
  1407. array('div' => array('class' => 'error-message')),
  1408. array(),
  1409. '/div',
  1410. '/div'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1414. $expected = array(
  1415. 'div' => array('class' => 'input password error'),
  1416. 'label' => array('for' => 'ContactPassword'),
  1417. 'Password',
  1418. '/label',
  1419. 'input' => array(
  1420. 'type' => 'password', 'name' => 'data[Contact][password]',
  1421. 'id' => 'ContactPassword', 'class' => 'form-error'
  1422. ),
  1423. '/div'
  1424. );
  1425. $this->assertTags($result, $expected);
  1426. }
  1427. /**
  1428. * Test validation errors, when calling input() overriding validation message by an empty string.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testEmptyInputErrorValidation() {
  1433. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1434. $result = $this->Form->input('Contact.password', array('error' => ''));
  1435. $expected = array(
  1436. 'div' => array('class' => 'input password error'),
  1437. 'label' => array('for' => 'ContactPassword'),
  1438. 'Password',
  1439. '/label',
  1440. 'input' => array(
  1441. 'type' => 'password', 'name' => 'data[Contact][password]',
  1442. 'id' => 'ContactPassword', 'class' => 'form-error'
  1443. ),
  1444. array('div' => array('class' => 'error-message')),
  1445. array(),
  1446. '/div',
  1447. '/div'
  1448. );
  1449. $this->assertTags($result, $expected);
  1450. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1451. $expected = array(
  1452. 'div' => array('class' => 'input password error'),
  1453. 'label' => array('for' => 'ContactPassword'),
  1454. 'Password',
  1455. '/label',
  1456. 'input' => array(
  1457. 'type' => 'password', 'name' => 'data[Contact][password]',
  1458. 'id' => 'ContactPassword', 'class' => 'form-error'
  1459. ),
  1460. '/div'
  1461. );
  1462. $this->assertTags($result, $expected);
  1463. }
  1464. /**
  1465. * testFormValidationAssociated method
  1466. *
  1467. * test display of form errors in conjunction with model::validates.
  1468. *
  1469. * @return void
  1470. */
  1471. public function testFormValidationAssociated() {
  1472. $this->UserForm = ClassRegistry::getObject('UserForm');
  1473. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1474. $data = array(
  1475. 'UserForm' => array('name' => 'user'),
  1476. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1477. );
  1478. $result = $this->UserForm->OpenidUrl->create($data);
  1479. $this->assertFalse(empty($result));
  1480. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1481. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1482. $encoding = strtolower(Configure::read('App.encoding'));
  1483. $expected = array(
  1484. 'form' => array(
  1485. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1486. 'accept-charset' => $encoding
  1487. ),
  1488. 'div' => array('style' => 'display:none;'),
  1489. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1490. '/div'
  1491. );
  1492. $this->assertTags($result, $expected);
  1493. $result = $this->Form->error(
  1494. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Error, not registered', $result);
  1497. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1498. }
  1499. /**
  1500. * testFormValidationAssociatedFirstLevel method
  1501. *
  1502. * test form error display with associated model.
  1503. *
  1504. * @return void
  1505. */
  1506. public function testFormValidationAssociatedFirstLevel() {
  1507. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1508. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1509. $data = array(
  1510. 'ValidateUser' => array('name' => 'mariano'),
  1511. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1518. $encoding = strtolower(Configure::read('App.encoding'));
  1519. $expected = array(
  1520. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1521. 'div' => array('style' => 'display:none;'),
  1522. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1523. '/div'
  1524. );
  1525. $this->assertTags($result, $expected);
  1526. $result = $this->Form->error(
  1527. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1528. );
  1529. $this->assertEquals('Invalid email', $result);
  1530. $result = $this->Form->error(
  1531. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid name', $result);
  1534. $result = $this->Form->error(
  1535. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid city', $result);
  1538. unset($this->ValidateUser->ValidateProfile);
  1539. unset($this->ValidateUser);
  1540. }
  1541. /**
  1542. * testFormValidationAssociatedSecondLevel method
  1543. *
  1544. * test form error display with associated model.
  1545. *
  1546. * @return void
  1547. */
  1548. public function testFormValidationAssociatedSecondLevel() {
  1549. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1550. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1551. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1552. $data = array(
  1553. 'ValidateUser' => array('name' => 'mariano'),
  1554. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1555. 'ValidateItem' => array('name' => 'Item')
  1556. );
  1557. $result = $this->ValidateUser->create($data);
  1558. $this->assertFalse(empty($result));
  1559. $this->assertFalse($this->ValidateUser->validates());
  1560. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1561. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1562. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1563. $encoding = strtolower(Configure::read('App.encoding'));
  1564. $expected = array(
  1565. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1566. 'div' => array('style' => 'display:none;'),
  1567. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1568. '/div'
  1569. );
  1570. $this->assertTags($result, $expected);
  1571. $result = $this->Form->error(
  1572. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1573. );
  1574. $this->assertEquals('Invalid email', $result);
  1575. $result = $this->Form->error(
  1576. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1577. );
  1578. $this->assertEquals('Invalid name', $result);
  1579. $result = $this->Form->error(
  1580. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1581. );
  1582. $result = $this->Form->error(
  1583. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1584. );
  1585. $this->assertEquals('Invalid description', $result);
  1586. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1587. unset($this->ValidateUser->ValidateProfile);
  1588. unset($this->ValidateUser);
  1589. }
  1590. /**
  1591. * testFormValidationMultiRecord method
  1592. *
  1593. * test form error display with multiple records.
  1594. *
  1595. * @return void
  1596. */
  1597. public function testFormValidationMultiRecord() {
  1598. $Contact = ClassRegistry::getObject('Contact');
  1599. $Contact->validationErrors[2] = array(
  1600. 'name' => array('This field cannot be left blank')
  1601. );
  1602. $result = $this->Form->input('Contact.2.name');
  1603. $expected = array(
  1604. 'div' => array('class' => 'input text error'),
  1605. 'label' => array('for' => 'Contact2Name'),
  1606. 'Name',
  1607. '/label',
  1608. 'input' => array(
  1609. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1610. 'class' => 'form-error', 'maxlength' => 255
  1611. ),
  1612. array('div' => array('class' => 'error-message')),
  1613. 'This field cannot be left blank',
  1614. '/div',
  1615. '/div'
  1616. );
  1617. $this->assertTags($result, $expected);
  1618. }
  1619. /**
  1620. * testMultipleInputValidation method
  1621. *
  1622. * test multiple record form validation error display.
  1623. *
  1624. * @return void
  1625. */
  1626. public function testMultipleInputValidation() {
  1627. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1628. $Address->validationErrors[0] = array(
  1629. 'title' => array('This field cannot be empty'),
  1630. 'first_name' => array('This field cannot be empty')
  1631. );
  1632. $Address->validationErrors[1] = array(
  1633. 'last_name' => array('You must have a last name')
  1634. );
  1635. $this->Form->create();
  1636. $result = $this->Form->input('Address.0.title');
  1637. $expected = array(
  1638. 'div' => array('class'),
  1639. 'label' => array('for'),
  1640. 'preg:/[^<]+/',
  1641. '/label',
  1642. 'input' => array(
  1643. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'This field cannot be empty',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. $result = $this->Form->input('Address.0.first_name');
  1652. $expected = array(
  1653. 'div' => array('class'),
  1654. 'label' => array('for'),
  1655. 'preg:/[^<]+/',
  1656. '/label',
  1657. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1658. array('div' => array('class' => 'error-message')),
  1659. 'This field cannot be empty',
  1660. '/div',
  1661. '/div'
  1662. );
  1663. $this->assertTags($result, $expected);
  1664. $result = $this->Form->input('Address.0.last_name');
  1665. $expected = array(
  1666. 'div' => array('class'),
  1667. 'label' => array('for'),
  1668. 'preg:/[^<]+/',
  1669. '/label',
  1670. 'input' => array('type' => 'text', 'name', 'id'),
  1671. '/div'
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->input('Address.1.last_name');
  1675. $expected = array(
  1676. 'div' => array('class'),
  1677. 'label' => array('for'),
  1678. 'preg:/[^<]+/',
  1679. '/label',
  1680. 'input' => array(
  1681. 'type' => 'text', 'name', 'id',
  1682. 'class' => 'form-error'
  1683. ),
  1684. array('div' => array('class' => 'error-message')),
  1685. 'You must have a last name',
  1686. '/div',
  1687. '/div'
  1688. );
  1689. $this->assertTags($result, $expected);
  1690. }
  1691. /**
  1692. * testInput method
  1693. *
  1694. * Test various incarnations of input().
  1695. *
  1696. * @return void
  1697. */
  1698. public function testInput() {
  1699. $result = $this->Form->input('ValidateUser.balance');
  1700. $expected = array(
  1701. 'div' => array('class'),
  1702. 'label' => array('for'),
  1703. 'Balance',
  1704. '/label',
  1705. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1706. '/div',
  1707. );
  1708. $this->assertTags($result, $expected);
  1709. $result = $this->Form->input('ValidateUser.cost_decimal');
  1710. $expected = array(
  1711. 'div' => array('class'),
  1712. 'label' => array('for'),
  1713. 'Cost Decimal',
  1714. '/label',
  1715. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1716. '/div',
  1717. );
  1718. $this->assertTags($result, $expected);
  1719. $result = $this->Form->input('ValidateUser.ratio');
  1720. $expected = array(
  1721. 'div' => array('class'),
  1722. 'label' => array('for'),
  1723. 'Ratio',
  1724. '/label',
  1725. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1726. '/div',
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. $result = $this->Form->input('ValidateUser.population');
  1730. $expected = array(
  1731. 'div' => array('class'),
  1732. 'label' => array('for'),
  1733. 'Population',
  1734. '/label',
  1735. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1736. '/div',
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1740. $expected = array(
  1741. 'div' => array('class' => 'input email'),
  1742. 'label' => array('for' => 'custom'),
  1743. 'Email',
  1744. '/label',
  1745. array('input' => array(
  1746. 'type' => 'email', 'name' => 'data[Contact][email]',
  1747. 'id' => 'custom', 'maxlength' => 255
  1748. )),
  1749. '/div'
  1750. );
  1751. $this->assertTags($result, $expected);
  1752. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1753. $expected = array(
  1754. '<div',
  1755. 'label' => array('for' => 'ContactEmail'),
  1756. 'Email',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'email', 'name' => 'data[Contact][email]',
  1760. 'id' => 'ContactEmail', 'maxlength' => 255
  1761. )),
  1762. '/div'
  1763. );
  1764. $this->assertTags($result, $expected);
  1765. $result = $this->Form->hidden('Contact.idontexist');
  1766. $expected = array('input' => array(
  1767. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1768. 'id' => 'ContactIdontexist'
  1769. ));
  1770. $this->assertTags($result, $expected);
  1771. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1772. $expected = array(
  1773. 'div' => array('class' => 'input text'),
  1774. 'label' => array('for' => 'ContactEmail'),
  1775. 'Email',
  1776. '/label',
  1777. array('input' => array(
  1778. 'type' => 'text', 'name' => 'data[Contact][email]',
  1779. 'id' => 'ContactEmail'
  1780. )),
  1781. '/div'
  1782. );
  1783. $this->assertTags($result, $expected);
  1784. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1785. $expected = array(
  1786. 'div' => array('class' => 'input text'),
  1787. 'label' => array('for' => 'Contact5Email'),
  1788. 'Email',
  1789. '/label',
  1790. array('input' => array(
  1791. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1792. 'id' => 'Contact5Email'
  1793. )),
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. $result = $this->Form->input('Contact.password');
  1798. $expected = array(
  1799. 'div' => array('class' => 'input password'),
  1800. 'label' => array('for' => 'ContactPassword'),
  1801. 'Password',
  1802. '/label',
  1803. array('input' => array(
  1804. 'type' => 'password', 'name' => 'data[Contact][password]',
  1805. 'id' => 'ContactPassword'
  1806. )),
  1807. '/div'
  1808. );
  1809. $this->assertTags($result, $expected);
  1810. $result = $this->Form->input('Contact.email', array(
  1811. 'type' => 'file', 'class' => 'textbox'
  1812. ));
  1813. $expected = array(
  1814. 'div' => array('class' => 'input file'),
  1815. 'label' => array('for' => 'ContactEmail'),
  1816. 'Email',
  1817. '/label',
  1818. array('input' => array(
  1819. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1820. 'id' => 'ContactEmail'
  1821. )),
  1822. '/div'
  1823. );
  1824. $this->assertTags($result, $expected);
  1825. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1826. $result = $this->Form->input('Contact.phone');
  1827. $expected = array(
  1828. 'div' => array('class' => 'input tel'),
  1829. 'label' => array('for' => 'ContactPhone'),
  1830. 'Phone',
  1831. '/label',
  1832. array('input' => array(
  1833. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1834. 'value' => 'Hello &amp; World &gt; weird chars',
  1835. 'id' => 'ContactPhone', 'maxlength' => 255
  1836. )),
  1837. '/div'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1841. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1842. $expected = array(
  1843. 'div' => array('class' => 'input text'),
  1844. 'label' => array('for' => 'myId'),
  1845. 'Field',
  1846. '/label',
  1847. 'input' => array(
  1848. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1849. 'value' => 'My value', 'id' => 'myId'
  1850. ),
  1851. '/div'
  1852. );
  1853. $this->assertTags($result, $expected);
  1854. unset($this->Form->request->data);
  1855. $Contact = ClassRegistry::getObject('Contact');
  1856. $Contact->validationErrors['field'] = array('Badness!');
  1857. $result = $this->Form->input('Contact.field');
  1858. $expected = array(
  1859. 'div' => array('class' => 'input text error'),
  1860. 'label' => array('for' => 'ContactField'),
  1861. 'Field',
  1862. '/label',
  1863. 'input' => array(
  1864. 'type' => 'text', 'name' => 'data[Contact][field]',
  1865. 'id' => 'ContactField', 'class' => 'form-error'
  1866. ),
  1867. array('div' => array('class' => 'error-message')),
  1868. 'Badness!',
  1869. '/div',
  1870. '/div'
  1871. );
  1872. $this->assertTags($result, $expected);
  1873. $result = $this->Form->input('Contact.field', array(
  1874. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1875. ));
  1876. $expected = array(
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array(
  1881. 'type' => 'text', 'name' => 'data[Contact][field]',
  1882. 'id' => 'ContactField', 'class' => 'form-error'
  1883. ),
  1884. array('span' => array('class' => 'error-message')),
  1885. 'Badness!',
  1886. '/span'
  1887. );
  1888. $this->assertTags($result, $expected);
  1889. $result = $this->Form->input('Contact.field', array(
  1890. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1891. ));
  1892. $expected = array(
  1893. 'div' => array('class' => 'input text error'),
  1894. 'label' => array('for' => 'ContactField'),
  1895. 'Field',
  1896. '/label',
  1897. 'input' => array(
  1898. 'type' => 'text', 'name' => 'data[Contact][field]',
  1899. 'id' => 'ContactField', 'class' => 'form-error'
  1900. ),
  1901. array('div' => array('class' => 'error')),
  1902. 'Badness!',
  1903. '/div'
  1904. );
  1905. $this->assertTags($result, $expected);
  1906. $result = $this->Form->input('Contact.field', array(
  1907. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1908. ));
  1909. $expected = array(
  1910. 'span' => array('class' => 'input text error'),
  1911. 'label' => array('for' => 'ContactField'),
  1912. 'Field',
  1913. '/label',
  1914. 'input' => array(
  1915. 'type' => 'text', 'name' => 'data[Contact][field]',
  1916. 'id' => 'ContactField', 'class' => 'form-error'
  1917. ),
  1918. 'Badness!',
  1919. '/span'
  1920. );
  1921. $this->assertTags($result, $expected);
  1922. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array(
  1929. 'type' => 'text', 'name' => 'data[Contact][field]',
  1930. 'id' => 'ContactField', 'class' => 'form-error'
  1931. ),
  1932. 'A message to you, Rudy',
  1933. array('div' => array('class' => 'error-message')),
  1934. 'Badness!',
  1935. '/div',
  1936. '/div'
  1937. );
  1938. $this->assertTags($result, $expected);
  1939. $this->Form->setEntity(null);
  1940. $this->Form->setEntity('Contact.field');
  1941. $result = $this->Form->input('Contact.field', array(
  1942. 'after' => 'A message to you, Rudy', 'error' => false
  1943. ));
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'ContactField'),
  1947. 'Field',
  1948. '/label',
  1949. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1950. 'A message to you, Rudy',
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1955. $expected = array(
  1956. 'div' => array('class' => 'input text'),
  1957. 'label' => array('for' => 'ObjectField'),
  1958. 'Field',
  1959. '/label',
  1960. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1961. 'A message to you, Rudy',
  1962. '/div'
  1963. );
  1964. $this->assertTags($result, $expected);
  1965. $Contact->validationErrors['field'] = array('minLength');
  1966. $result = $this->Form->input('Contact.field', array(
  1967. 'error' => array(
  1968. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1969. 'maxLength' => 'login too large'
  1970. )
  1971. ));
  1972. $expected = array(
  1973. 'div' => array('class' => 'input text error'),
  1974. 'label' => array('for' => 'ContactField'),
  1975. 'Field',
  1976. '/label',
  1977. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1978. array('div' => array('class' => 'error-message')),
  1979. 'Le login doit contenir au moins 2 caractères',
  1980. '/div',
  1981. '/div'
  1982. );
  1983. $this->assertTags($result, $expected);
  1984. $Contact->validationErrors['field'] = array('maxLength');
  1985. $result = $this->Form->input('Contact.field', array(
  1986. 'error' => array(
  1987. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1988. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1989. 'maxLength' => 'login too large',
  1990. )
  1991. ));
  1992. $expected = array(
  1993. 'div' => array('class' => 'input text error'),
  1994. 'label' => array('for' => 'ContactField'),
  1995. 'Field',
  1996. '/label',
  1997. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1998. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1999. 'login too large',
  2000. '/span',
  2001. '/div'
  2002. );
  2003. $this->assertTags($result, $expected);
  2004. }
  2005. /**
  2006. * Test that inputs with 0 can be created.
  2007. *
  2008. * @return void
  2009. */
  2010. public function testInputZero() {
  2011. $this->Form->create('User');
  2012. $result = $this->Form->input('0');
  2013. $expected = array(
  2014. 'div' => array('class' => 'input text'),
  2015. 'label' => array('for' => 'User0'), '/label',
  2016. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  2017. '/div'
  2018. );
  2019. $this->assertTags($result, $expected);
  2020. }
  2021. /**
  2022. * test input() with checkbox creation
  2023. *
  2024. * @return void
  2025. */
  2026. public function testInputCheckbox() {
  2027. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2028. $expected = array(
  2029. 'div' => array('class' => 'input checkbox'),
  2030. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2031. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2032. '/div'
  2033. );
  2034. $this->assertTags($result, $expected);
  2035. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2036. $expected = array(
  2037. 'div' => array('class' => 'input checkbox'),
  2038. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2039. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2040. '/div'
  2041. );
  2042. $this->assertTags($result, $expected);
  2043. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2044. $expected = array(
  2045. 'div' => array('class' => 'input checkbox'),
  2046. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2047. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2048. '/div'
  2049. );
  2050. $this->assertTags($result, $expected);
  2051. $result = $this->Form->input('User.disabled', array(
  2052. 'label' => 'Disabled',
  2053. 'type' => 'checkbox',
  2054. 'data-foo' => 'disabled'
  2055. ));
  2056. $expected = array(
  2057. 'div' => array('class' => 'input checkbox'),
  2058. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2059. array('input' => array(
  2060. 'type' => 'checkbox',
  2061. 'name' => 'data[User][disabled]',
  2062. 'value' => '1',
  2063. 'id' => 'UserDisabled',
  2064. 'data-foo' => 'disabled'
  2065. )),
  2066. 'label' => array('for' => 'UserDisabled'),
  2067. 'Disabled',
  2068. '/label',
  2069. '/div'
  2070. );
  2071. $this->assertTags($result, $expected);
  2072. }
  2073. /**
  2074. * test form->input() with time types.
  2075. *
  2076. * @return void
  2077. */
  2078. public function testInputTime() {
  2079. extract($this->dateRegex);
  2080. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2081. $result = explode(':', $result);
  2082. $this->assertRegExp('/option value="23"/', $result[0]);
  2083. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2084. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2085. $result = explode(':', $result);
  2086. $this->assertRegExp('/option value="23"/', $result[0]);
  2087. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2088. $result = $this->Form->input('Model.field', array(
  2089. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2090. ));
  2091. $result = explode(':', $result);
  2092. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2093. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2094. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2095. $result = $this->Form->input('Model.field', array(
  2096. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2097. ));
  2098. $result = explode(':', $result);
  2099. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2100. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2101. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2102. $result = $this->Form->input('prueba', array(
  2103. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2104. 'maxYear' => date('Y') + 1, 'interval' => 15
  2105. ));
  2106. $result = explode(':', $result);
  2107. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2108. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2109. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2110. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2111. $result = $this->Form->input('Random.start_time', array(
  2112. 'type' => 'time',
  2113. 'selected' => '18:15'
  2114. ));
  2115. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2116. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2117. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2118. $result = $this->Form->input('published', array('type' => 'time'));
  2119. $now = strtotime('now');
  2120. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2121. $now = strtotime('2013-03-09 00:42:21');
  2122. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2123. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2124. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2125. }
  2126. /**
  2127. * Test interval + selected near the hour roll over.
  2128. *
  2129. * @return void
  2130. */
  2131. public function testTimeSelectedWithInterval() {
  2132. $result = $this->Form->input('Model.start_time', array(
  2133. 'type' => 'time',
  2134. 'interval' => 15,
  2135. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2136. ));
  2137. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2138. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2139. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2140. $result = $this->Form->input('Model.start_time', array(
  2141. 'type' => 'time',
  2142. 'interval' => 15,
  2143. 'selected' => '2012-10-23 15:57:00'
  2144. ));
  2145. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2146. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'timeFormat' => 24,
  2150. 'type' => 'time',
  2151. 'interval' => 15,
  2152. 'selected' => '15:57'
  2153. ));
  2154. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2155. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2156. $result = $this->Form->input('Model.start_time', array(
  2157. 'timeFormat' => 24,
  2158. 'type' => 'time',
  2159. 'interval' => 15,
  2160. 'selected' => '23:57'
  2161. ));
  2162. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2163. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2164. $result = $this->Form->input('Model.created', array(
  2165. 'timeFormat' => 24,
  2166. 'type' => 'datetime',
  2167. 'interval' => 15,
  2168. 'selected' => '2012-09-30 23:56'
  2169. ));
  2170. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2171. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2172. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2173. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2174. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2175. }
  2176. /**
  2177. * Test time with selected values around 12:xx:xx
  2178. *
  2179. * @return void
  2180. */
  2181. public function testTimeSelectedWithIntervalTwelve() {
  2182. $result = $this->Form->input('Model.start_time', array(
  2183. 'type' => 'time',
  2184. 'timeFormat' => 12,
  2185. 'interval' => 15,
  2186. 'selected' => '00:00:00'
  2187. ));
  2188. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2189. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2190. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2191. $result = $this->Form->input('Model.start_time', array(
  2192. 'type' => 'time',
  2193. 'timeFormat' => 12,
  2194. 'interval' => 15,
  2195. 'selected' => '12:00:00'
  2196. ));
  2197. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2198. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2199. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2200. $result = $this->Form->input('Model.start_time', array(
  2201. 'type' => 'time',
  2202. 'timeFormat' => 12,
  2203. 'interval' => 15,
  2204. 'selected' => '12:15:00'
  2205. ));
  2206. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2207. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2208. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2209. }
  2210. /**
  2211. * Test interval & timeFormat = 12
  2212. *
  2213. * @return void
  2214. */
  2215. public function testInputTimeWithIntervalAnd12HourFormat() {
  2216. $result = $this->Form->input('Model.start_time', array(
  2217. 'type' => 'time',
  2218. 'timeFormat' => 12,
  2219. 'interval' => 5,
  2220. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2221. ));
  2222. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2223. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2224. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2225. $result = $this->Form->input('Model.start_time', array(
  2226. 'type' => 'time',
  2227. 'timeFormat' => '12',
  2228. 'interval' => 5,
  2229. 'selected' => '2013-04-19 16:30:00'
  2230. ));
  2231. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2232. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2233. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2234. $result = $this->Form->input('Model.start_time', array(
  2235. 'type' => 'time',
  2236. 'timeFormat' => '12',
  2237. 'interval' => 10,
  2238. 'selected' => '2013-05-19 00:33:00'
  2239. ));
  2240. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2241. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2242. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2243. $result = $this->Form->input('Model.start_time', array(
  2244. 'type' => 'time',
  2245. 'timeFormat' => '12',
  2246. 'interval' => 10,
  2247. 'selected' => '2013-05-19 13:33:00'
  2248. ));
  2249. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2250. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2251. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2252. $result = $this->Form->input('Model.start_time', array(
  2253. 'type' => 'time',
  2254. 'timeFormat' => '12',
  2255. 'interval' => 10,
  2256. 'selected' => '2013-05-19 01:33:00'
  2257. ));
  2258. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2259. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2260. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2261. }
  2262. /**
  2263. * test form->input() with datetime, date and time types
  2264. *
  2265. * @return void
  2266. */
  2267. public function testInputDatetime() {
  2268. extract($this->dateRegex);
  2269. $result = $this->Form->input('prueba', array(
  2270. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2271. 'maxYear' => date('Y') + 1, 'interval' => 15
  2272. ));
  2273. $result = explode(':', $result);
  2274. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2275. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2276. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2277. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2278. //related to ticket #5013
  2279. $result = $this->Form->input('Contact.date', array(
  2280. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2281. ));
  2282. $this->assertRegExp('/class="customClass"/', $result);
  2283. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2284. $result = $this->Form->input('Contact.date', array(
  2285. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2286. ));
  2287. $this->assertRegExp('/id="customIdDay"/', $result);
  2288. $this->assertRegExp('/id="customIdMonth"/', $result);
  2289. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2290. $result = $this->Form->input('Model.field', array(
  2291. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2292. ));
  2293. $this->assertRegExp('/id="customIDDay"/', $result);
  2294. $this->assertRegExp('/id="customIDHour"/', $result);
  2295. $result = explode('</select><select', $result);
  2296. $result = explode(':', $result[1]);
  2297. $this->assertRegExp('/option value="23"/', $result[0]);
  2298. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2299. $result = $this->Form->input('Model.field', array(
  2300. 'type' => 'datetime', 'timeFormat' => 12
  2301. ));
  2302. $result = explode('</select><select', $result);
  2303. $result = explode(':', $result[1]);
  2304. $this->assertRegExp('/option value="12"/', $result[0]);
  2305. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2306. $this->Form->request->data = array('Contact' => array('created' => null));
  2307. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2308. $expected = array(
  2309. 'div' => array('class' => 'input date'),
  2310. 'label' => array('for' => 'ContactCreatedMonth'),
  2311. 'Created',
  2312. '/label',
  2313. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2314. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2315. $monthsRegex,
  2316. '/select', '-',
  2317. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2318. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2319. $daysRegex,
  2320. '/select', '-',
  2321. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2322. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2323. $yearsRegex,
  2324. '/select',
  2325. '/div'
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. $this->Form->request->data = array('Contact' => array('created' => null));
  2329. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2330. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2331. $this->Form->request->data = array('Contact' => array('created' => null));
  2332. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2333. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2334. $result = $this->Form->input('Contact.created', array(
  2335. 'type' => 'date',
  2336. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2337. 'timeFormat' => 'NONE'
  2338. ));
  2339. $this->assertRegExp('/for\="created-month"/', $result);
  2340. }
  2341. /**
  2342. * Test generating checkboxes in a loop.
  2343. *
  2344. * @return void
  2345. */
  2346. public function testInputCheckboxesInLoop() {
  2347. for ($i = 1; $i < 5; $i++) {
  2348. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2349. $expected = array(
  2350. 'div' => array('class' => 'input checkbox'),
  2351. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2352. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2353. 'label' => array('for' => "Contact{$i}Email"),
  2354. 'Email',
  2355. '/label',
  2356. '/div'
  2357. );
  2358. $this->assertTags($result, $expected);
  2359. }
  2360. }
  2361. /**
  2362. * Test generating checkboxes with disabled elements.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputCheckboxWithDisabledElements() {
  2367. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2368. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2369. $expected = array(
  2370. array('div' => array('class' => 'input select')),
  2371. array('label' => array('for' => "ContactMultiple")),
  2372. 'Multiple',
  2373. '/label',
  2374. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2375. array('div' => array('class' => 'checkbox')),
  2376. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2377. array('label' => array('for' => "ContactMultiple1")),
  2378. 'One',
  2379. '/label',
  2380. '/div',
  2381. array('div' => array('class' => 'checkbox')),
  2382. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2383. array('label' => array('for' => "ContactMultiple2")),
  2384. 'Two',
  2385. '/label',
  2386. '/div',
  2387. array('div' => array('class' => 'checkbox')),
  2388. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2389. array('label' => array('for' => "ContactMultiple3")),
  2390. 'Three',
  2391. '/label',
  2392. '/div',
  2393. '/div'
  2394. );
  2395. $this->assertTags($result, $expected);
  2396. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2397. $this->assertTags($result, $expected);
  2398. $disabled = array('2', 3);
  2399. $expected = array(
  2400. array('div' => array('class' => 'input select')),
  2401. array('label' => array('for' => "ContactMultiple")),
  2402. 'Multiple',
  2403. '/label',
  2404. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2405. array('div' => array('class' => 'checkbox')),
  2406. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2407. array('label' => array('for' => "ContactMultiple1")),
  2408. 'One',
  2409. '/label',
  2410. '/div',
  2411. array('div' => array('class' => 'checkbox')),
  2412. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2413. array('label' => array('for' => "ContactMultiple2")),
  2414. 'Two',
  2415. '/label',
  2416. '/div',
  2417. array('div' => array('class' => 'checkbox')),
  2418. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2419. array('label' => array('for' => "ContactMultiple3")),
  2420. 'Three',
  2421. '/label',
  2422. '/div',
  2423. '/div'
  2424. );
  2425. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2426. $this->assertTags($result, $expected);
  2427. // make sure 50 does only disable 50, and not 50f5c0cf
  2428. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2429. $disabled = array(50);
  2430. $expected = array(
  2431. array('div' => array('class' => 'input select')),
  2432. array('label' => array('for' => "ContactMultiple")),
  2433. 'Multiple',
  2434. '/label',
  2435. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2436. array('div' => array('class' => 'checkbox')),
  2437. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2438. array('label' => array('for' => "ContactMultiple50")),
  2439. 'Fifty',
  2440. '/label',
  2441. '/div',
  2442. array('div' => array('class' => 'checkbox')),
  2443. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2444. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2445. 'Stringy',
  2446. '/label',
  2447. '/div',
  2448. '/div'
  2449. );
  2450. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2451. $this->assertTags($result, $expected);
  2452. }
  2453. /**
  2454. * test input name with leading integer, ensure attributes are generated correctly.
  2455. *
  2456. * @return void
  2457. */
  2458. public function testInputWithLeadingInteger() {
  2459. $result = $this->Form->text('0.Node.title');
  2460. $expected = array(
  2461. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2462. );
  2463. $this->assertTags($result, $expected);
  2464. }
  2465. /**
  2466. * test form->input() with select type inputs.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testInputSelectType() {
  2471. $result = $this->Form->input('email', array(
  2472. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2473. );
  2474. $expected = array(
  2475. 'div' => array('class' => 'input select'),
  2476. 'label' => array('for' => 'email'),
  2477. 'Email',
  2478. '/label',
  2479. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2480. array('option' => array('value' => '')),
  2481. '/option',
  2482. array('option' => array('value' => 'è')),
  2483. 'Firést',
  2484. '/option',
  2485. array('option' => array('value' => 'é')),
  2486. 'Secoènd',
  2487. '/option',
  2488. '/select',
  2489. '/div'
  2490. );
  2491. $this->assertTags($result, $expected);
  2492. $result = $this->Form->input('email', array(
  2493. 'options' => array('First', 'Second'), 'empty' => true)
  2494. );
  2495. $expected = array(
  2496. 'div' => array('class' => 'input select'),
  2497. 'label' => array('for' => 'email'),
  2498. 'Email',
  2499. '/label',
  2500. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2501. array('option' => array('value' => '')),
  2502. '/option',
  2503. array('option' => array('value' => '0')),
  2504. 'First',
  2505. '/option',
  2506. array('option' => array('value' => '1')),
  2507. 'Second',
  2508. '/option',
  2509. '/select',
  2510. '/div'
  2511. );
  2512. $this->assertTags($result, $expected);
  2513. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2514. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2515. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2516. $expected = array(
  2517. 'div' => array('class' => 'input select'),
  2518. 'label' => array('for' => 'ModelUserId'),
  2519. 'User',
  2520. '/label',
  2521. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2522. array('option' => array('value' => '')),
  2523. '/option',
  2524. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2525. 'good',
  2526. '/option',
  2527. array('option' => array('value' => 'other')),
  2528. 'bad',
  2529. '/option',
  2530. '/select',
  2531. '/div'
  2532. );
  2533. $this->assertTags($result, $expected);
  2534. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2535. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2536. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2537. $expected = array(
  2538. 'div' => array('class' => 'input select'),
  2539. 'label' => array('for' => 'ThingUserId'),
  2540. 'User',
  2541. '/label',
  2542. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2543. array('option' => array('value' => '')),
  2544. 'Some Empty',
  2545. '/option',
  2546. array('option' => array('value' => 'value')),
  2547. 'good',
  2548. '/option',
  2549. array('option' => array('value' => 'other')),
  2550. 'bad',
  2551. '/option',
  2552. '/select',
  2553. '/div'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2557. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2558. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2559. $expected = array(
  2560. 'div' => array('class' => 'input select'),
  2561. 'label' => array('for' => 'ThingUserId'),
  2562. 'User',
  2563. '/label',
  2564. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2565. array('option' => array('value' => '')),
  2566. 'Some Empty',
  2567. '/option',
  2568. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2569. 'good',
  2570. '/option',
  2571. array('option' => array('value' => 'other')),
  2572. 'bad',
  2573. '/option',
  2574. '/select',
  2575. '/div'
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2579. $this->Form->request->data = array('User' => array('User' => array('value')));
  2580. $result = $this->Form->input('User.User', array('empty' => true));
  2581. $expected = array(
  2582. 'div' => array('class' => 'input select'),
  2583. 'label' => array('for' => 'UserUser'),
  2584. 'User',
  2585. '/label',
  2586. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2587. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2588. array('option' => array('value' => '')),
  2589. '/option',
  2590. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2591. 'good',
  2592. '/option',
  2593. array('option' => array('value' => 'other')),
  2594. 'bad',
  2595. '/option',
  2596. '/select',
  2597. '/div'
  2598. );
  2599. $this->assertTags($result, $expected);
  2600. $this->Form->data = array();
  2601. $result = $this->Form->input('Publisher.id', array(
  2602. 'label' => 'Publisher',
  2603. 'type' => 'select',
  2604. 'multiple' => 'checkbox',
  2605. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2606. ));
  2607. $expected = array(
  2608. array('div' => array('class' => 'input select')),
  2609. array('label' => array('for' => 'PublisherId')),
  2610. 'Publisher',
  2611. '/label',
  2612. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2613. array('div' => array('class' => 'checkbox')),
  2614. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2615. array('label' => array('for' => 'PublisherIdValue1')),
  2616. 'Label 1',
  2617. '/label',
  2618. '/div',
  2619. array('div' => array('class' => 'checkbox')),
  2620. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2621. array('label' => array('for' => 'PublisherIdValue2')),
  2622. 'Label 2',
  2623. '/label',
  2624. '/div',
  2625. '/div'
  2626. );
  2627. $this->assertTags($result, $expected);
  2628. }
  2629. /**
  2630. * test that input() and a non standard primary key makes a hidden input by default.
  2631. *
  2632. * @return void
  2633. */
  2634. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2635. $this->Form->create('User');
  2636. $this->Form->fieldset = array(
  2637. 'User' => array(
  2638. 'fields' => array(
  2639. 'model_id' => array('type' => 'integer')
  2640. ),
  2641. 'validates' => array(),
  2642. 'key' => 'model_id'
  2643. )
  2644. );
  2645. $result = $this->Form->input('model_id');
  2646. $expected = array(
  2647. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2648. );
  2649. $this->assertTags($result, $expected);
  2650. }
  2651. /**
  2652. * test that overriding the magic select type widget is possible
  2653. *
  2654. * @return void
  2655. */
  2656. public function testInputOverridingMagicSelectType() {
  2657. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2658. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2659. $expected = array(
  2660. 'div' => array('class' => 'input text'),
  2661. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2662. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2663. '/div'
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. //Check that magic types still work for plural/singular vars
  2667. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2668. $result = $this->Form->input('Model.type');
  2669. $expected = array(
  2670. 'div' => array('class' => 'input select'),
  2671. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2672. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2673. array('option' => array('value' => 'value')), 'good', '/option',
  2674. array('option' => array('value' => 'other')), 'bad', '/option',
  2675. '/select',
  2676. '/div'
  2677. );
  2678. $this->assertTags($result, $expected);
  2679. }
  2680. /**
  2681. * Test that inferred types do not override developer input
  2682. *
  2683. * @return void
  2684. */
  2685. public function testInputMagicTypeDoesNotOverride() {
  2686. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2687. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2688. $expected = array(
  2689. 'div' => array('class' => 'input checkbox'),
  2690. array('input' => array(
  2691. 'type' => 'hidden',
  2692. 'name' => 'data[Model][user]',
  2693. 'id' => 'ModelUser_',
  2694. 'value' => 0,
  2695. )),
  2696. array('input' => array(
  2697. 'name' => 'data[Model][user]',
  2698. 'type' => 'checkbox',
  2699. 'id' => 'ModelUser',
  2700. 'value' => 1
  2701. )),
  2702. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2703. '/div'
  2704. );
  2705. $this->assertTags($result, $expected);
  2706. }
  2707. /**
  2708. * Test that magic input() selects are created for type=number
  2709. *
  2710. * @return void
  2711. */
  2712. public function testInputMagicSelectForTypeNumber() {
  2713. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2714. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2715. $result = $this->Form->input('ValidateUser.balance');
  2716. $expected = array(
  2717. 'div' => array('class' => 'input select'),
  2718. 'label' => array('for' => 'ValidateUserBalance'),
  2719. 'Balance',
  2720. '/label',
  2721. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2722. array('option' => array('value' => '0')),
  2723. 'nothing',
  2724. '/option',
  2725. array('option' => array('value' => '1', 'selected' => 'selected')),
  2726. 'some',
  2727. '/option',
  2728. array('option' => array('value' => '100')),
  2729. 'a lot',
  2730. '/option',
  2731. '/select',
  2732. '/div'
  2733. );
  2734. $this->assertTags($result, $expected);
  2735. }
  2736. /**
  2737. * Test that magic input() selects can easily be converted into radio types without error.
  2738. *
  2739. * @return void
  2740. */
  2741. public function testInputMagicSelectChangeToRadio() {
  2742. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2743. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2744. $this->assertRegExp('/input type="radio"/', $result);
  2745. }
  2746. /**
  2747. * fields with the same name as the model should work.
  2748. *
  2749. * @return void
  2750. */
  2751. public function testInputWithMatchingFieldAndModelName() {
  2752. $this->Form->create('User');
  2753. $this->Form->fieldset = array(
  2754. 'User' => array(
  2755. 'fields' => array(
  2756. 'User' => array('type' => 'text')
  2757. ),
  2758. 'validates' => array(),
  2759. 'key' => 'id'
  2760. )
  2761. );
  2762. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2763. $result = $this->Form->input('User', array('type' => 'text'));
  2764. $expected = array(
  2765. 'div' => array('class' => 'input text'),
  2766. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2767. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2768. '/div'
  2769. );
  2770. $this->assertTags($result, $expected);
  2771. }
  2772. /**
  2773. * testFormInputs method
  2774. *
  2775. * test correct results from form::inputs().
  2776. *
  2777. * @return void
  2778. */
  2779. public function testFormInputs() {
  2780. $this->Form->create('Contact');
  2781. $result = $this->Form->inputs('The Legend');
  2782. $expected = array(
  2783. '<fieldset',
  2784. '<legend',
  2785. 'The Legend',
  2786. '/legend',
  2787. '*/fieldset',
  2788. );
  2789. $this->assertTags($result, $expected);
  2790. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2791. $expected = array(
  2792. 'fieldset' => array('class' => 'classy-stuff'),
  2793. '<legend',
  2794. 'Field of Dreams',
  2795. '/legend',
  2796. '*/fieldset'
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2800. $this->assertTags($result, $expected);
  2801. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2802. $this->assertTags($result, $expected);
  2803. $this->Form->create('Contact');
  2804. $this->Form->request['prefix'] = 'admin';
  2805. $this->Form->request['action'] = 'admin_edit';
  2806. $result = $this->Form->inputs();
  2807. $expected = array(
  2808. '<fieldset',
  2809. '<legend',
  2810. 'Edit Contact',
  2811. '/legend',
  2812. '*/fieldset',
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. $this->Form->create('Contact');
  2816. $result = $this->Form->inputs(false);
  2817. $expected = array(
  2818. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2819. array('div' => array('class' => 'input text')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input email')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input tel')),
  2824. '*/div',
  2825. array('div' => array('class' => 'input password')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input date')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input date')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input datetime')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input number')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input select')),
  2836. '*/div',
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $this->Form->create('Contact');
  2840. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2841. $expected = array(
  2842. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2843. array('div' => array('class' => 'input text')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input email')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input tel')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input password')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input date')),
  2852. '*/div',
  2853. array('div' => array('class' => 'input date')),
  2854. '*/div',
  2855. array('div' => array('class' => 'input datetime')),
  2856. '*/div',
  2857. array('div' => array('class' => 'input number')),
  2858. '*/div',
  2859. array('div' => array('class' => 'input select')),
  2860. '*/div',
  2861. );
  2862. $this->assertTags($result, $expected);
  2863. $this->Form->create('Contact');
  2864. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2865. $this->assertTags($result, $expected);
  2866. $this->Form->create('Contact');
  2867. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2868. $expected = array(
  2869. 'fieldset' => array(),
  2870. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2871. array('div' => array('class' => 'input text')),
  2872. '*/div',
  2873. array('div' => array('class' => 'input email')),
  2874. '*/div',
  2875. array('div' => array('class' => 'input tel')),
  2876. '*/div',
  2877. array('div' => array('class' => 'input password')),
  2878. '*/div',
  2879. array('div' => array('class' => 'input date')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input date')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input datetime')),
  2884. '*/div',
  2885. array('div' => array('class' => 'input number')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input select')),
  2888. '*/div',
  2889. '/fieldset'
  2890. );
  2891. $this->assertTags($result, $expected);
  2892. $this->Form->create('Contact');
  2893. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2894. $expected = array(
  2895. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2896. array('div' => array('class' => 'input text')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input email')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input tel')),
  2901. '*/div',
  2902. array('div' => array('class' => 'input password')),
  2903. '*/div',
  2904. array('div' => array('class' => 'input date')),
  2905. '*/div',
  2906. array('div' => array('class' => 'input date')),
  2907. '*/div',
  2908. array('div' => array('class' => 'input datetime')),
  2909. '*/div',
  2910. array('div' => array('class' => 'input number')),
  2911. '*/div',
  2912. array('div' => array('class' => 'input select')),
  2913. '*/div',
  2914. );
  2915. $this->assertTags($result, $expected);
  2916. $this->Form->create('Contact');
  2917. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2918. $this->assertTags($result, $expected);
  2919. $this->Form->create('Contact');
  2920. $result = $this->Form->inputs('Hello');
  2921. $expected = array(
  2922. 'fieldset' => array(),
  2923. 'legend' => array(),
  2924. 'Hello',
  2925. '/legend',
  2926. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2927. array('div' => array('class' => 'input text')),
  2928. '*/div',
  2929. array('div' => array('class' => 'input email')),
  2930. '*/div',
  2931. array('div' => array('class' => 'input tel')),
  2932. '*/div',
  2933. array('div' => array('class' => 'input password')),
  2934. '*/div',
  2935. array('div' => array('class' => 'input date')),
  2936. '*/div',
  2937. array('div' => array('class' => 'input date')),
  2938. '*/div',
  2939. array('div' => array('class' => 'input datetime')),
  2940. '*/div',
  2941. array('div' => array('class' => 'input number')),
  2942. '*/div',
  2943. array('div' => array('class' => 'input select')),
  2944. '*/div',
  2945. '/fieldset'
  2946. );
  2947. $this->assertTags($result, $expected);
  2948. $this->Form->create('Contact');
  2949. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2950. $expected = array(
  2951. 'fieldset' => array(),
  2952. 'legend' => array(),
  2953. 'Hello',
  2954. '/legend',
  2955. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2956. array('div' => array('class' => 'input text')),
  2957. '*/div',
  2958. array('div' => array('class' => 'input email')),
  2959. '*/div',
  2960. array('div' => array('class' => 'input tel')),
  2961. '*/div',
  2962. array('div' => array('class' => 'input password')),
  2963. '*/div',
  2964. array('div' => array('class' => 'input date')),
  2965. '*/div',
  2966. array('div' => array('class' => 'input date')),
  2967. '*/div',
  2968. array('div' => array('class' => 'input datetime')),
  2969. '*/div',
  2970. array('div' => array('class' => 'input number')),
  2971. '*/div',
  2972. array('div' => array('class' => 'input select')),
  2973. '*/div',
  2974. '/fieldset'
  2975. );
  2976. $this->assertTags($result, $expected);
  2977. $this->Form->create('Contact');
  2978. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2979. $this->assertTags($result, $expected);
  2980. $this->Form->end();
  2981. $this->Form->create(false);
  2982. $expected = array(
  2983. 'fieldset' => array(),
  2984. array('div' => array('class' => 'input text')),
  2985. 'label' => array('for' => 'foo'),
  2986. 'Foo',
  2987. '/label',
  2988. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2989. '*/div',
  2990. '/fieldset'
  2991. );
  2992. $result = $this->Form->inputs(
  2993. array('foo' => array('type' => 'text')),
  2994. array(),
  2995. array('legend' => false)
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. }
  2999. /**
  3000. * Tests inputs() works with plugin models
  3001. *
  3002. * @return void
  3003. */
  3004. public function testInputsPluginModel() {
  3005. $this->loadFixtures('Post');
  3006. App::build(array(
  3007. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  3008. ));
  3009. CakePlugin::load('TestPlugin');
  3010. $this->Form->request['models'] = array(
  3011. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3012. );
  3013. $this->Form->create('TestPlugin.TestPluginPost');
  3014. $result = $this->Form->inputs();
  3015. $this->assertContains('data[TestPluginPost][id]', $result);
  3016. $this->assertContains('data[TestPluginPost][author_id]', $result);
  3017. $this->assertContains('data[TestPluginPost][title]', $result);
  3018. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  3019. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  3020. $this->assertEquals('TestPluginPost', $this->Form->model());
  3021. }
  3022. /**
  3023. * testSelectAsCheckbox method
  3024. *
  3025. * test multi-select widget with checkbox formatting.
  3026. *
  3027. * @return void
  3028. */
  3029. public function testSelectAsCheckbox() {
  3030. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  3031. $expected = array(
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3033. array('div' => array('class' => 'checkbox')),
  3034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3035. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3036. 'first',
  3037. '/label',
  3038. '/div',
  3039. array('div' => array('class' => 'checkbox')),
  3040. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3041. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3042. 'second',
  3043. '/label',
  3044. '/div',
  3045. array('div' => array('class' => 'checkbox')),
  3046. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3047. array('label' => array('for' => 'ModelMultiField2')),
  3048. 'third',
  3049. '/label',
  3050. '/div',
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3054. $expected = array(
  3055. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3056. array('div' => array('class' => 'checkbox')),
  3057. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  3058. array('label' => array('for' => 'ModelMultiField12')),
  3059. 'half',
  3060. '/label',
  3061. '/div',
  3062. );
  3063. $this->assertTags($result, $expected);
  3064. }
  3065. /**
  3066. * testLabel method
  3067. *
  3068. * test label generation.
  3069. *
  3070. * @return void
  3071. */
  3072. public function testLabel() {
  3073. $this->Form->text('Person.name');
  3074. $result = $this->Form->label();
  3075. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3076. $this->Form->text('Person.name');
  3077. $result = $this->Form->label();
  3078. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3079. $result = $this->Form->label('Person.first_name');
  3080. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3081. $result = $this->Form->label('Person.first_name', 'Your first name');
  3082. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3083. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3084. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3085. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3086. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3087. $result = $this->Form->label('Person.first_name', '');
  3088. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3089. $result = $this->Form->label('Person.2.name', '');
  3090. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3091. }
  3092. /**
  3093. * testTextbox method
  3094. *
  3095. * test textbox element generation
  3096. *
  3097. * @return void
  3098. */
  3099. public function testTextbox() {
  3100. $result = $this->Form->text('Model.field');
  3101. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3102. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3103. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3104. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3105. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3106. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3107. $result = $this->Form->text('Model.text');
  3108. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3109. $Contact = ClassRegistry::getObject('Contact');
  3110. $Contact->validationErrors['text'] = array(true);
  3111. $this->Form->request->data['Contact']['text'] = 'test';
  3112. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3113. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3114. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3115. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3116. $expected = array(
  3117. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3118. );
  3119. $this->assertTags($result, $expected);
  3120. }
  3121. /**
  3122. * testDefaultValue method
  3123. *
  3124. * Test default value setting
  3125. *
  3126. * @return void
  3127. */
  3128. public function testDefaultValue() {
  3129. $this->Form->request->data['Model']['field'] = 'test';
  3130. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3131. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3132. unset($this->Form->request->data['Model']['field']);
  3133. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3134. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3135. }
  3136. /**
  3137. * testCheckboxDefaultValue method
  3138. *
  3139. * Test default value setting on checkbox() method
  3140. *
  3141. * @return void
  3142. */
  3143. public function testCheckboxDefaultValue() {
  3144. $this->Form->request->data['Model']['field'] = false;
  3145. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3146. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3147. unset($this->Form->request->data['Model']['field']);
  3148. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3149. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3150. $this->Form->request->data['Model']['field'] = true;
  3151. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3152. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3153. unset($this->Form->request->data['Model']['field']);
  3154. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3155. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3156. }
  3157. /**
  3158. * testError method
  3159. *
  3160. * Test field error generation
  3161. *
  3162. * @return void
  3163. */
  3164. public function testError() {
  3165. $Contact = ClassRegistry::getObject('Contact');
  3166. $Contact->validationErrors['field'] = array(1);
  3167. $result = $this->Form->error('Contact.field');
  3168. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3169. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3170. $this->assertEquals('Error in field Field', $result);
  3171. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3172. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3173. $this->assertEquals('This field contains invalid input', $result);
  3174. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3175. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3176. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3177. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3178. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3179. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3180. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3181. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3182. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3183. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3184. $this->assertEquals('<strong>Badness!</strong>', $result);
  3185. $Contact->validationErrors['field'] = array("email");
  3186. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3187. $expected = array(
  3188. 'div' => array('class' => 'field-error'),
  3189. 'No good!',
  3190. '/div'
  3191. );
  3192. $this->assertTags($result, $expected);
  3193. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3194. $result = $this->Form->error('Contact.field', array(
  3195. 'notEmpty' => 'Cannot be empty',
  3196. 'email' => 'No good!'
  3197. ));
  3198. $expected = array(
  3199. 'div' => array('class' => 'error-message'),
  3200. 'ul' => array(),
  3201. '<li', 'Cannot be empty', '/li',
  3202. '<li', 'No good!', '/li',
  3203. '<li', 'Something else', '/li',
  3204. '/ul',
  3205. '/div'
  3206. );
  3207. $this->assertTags($result, $expected);
  3208. // Testing error messages list options
  3209. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3210. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3211. $expected = array(
  3212. 'div' => array('class' => 'error-message'),
  3213. 'ol' => array(),
  3214. '<li', 'notEmpty', '/li',
  3215. '<li', 'email', '/li',
  3216. '/ol',
  3217. '/div'
  3218. );
  3219. $this->assertTags($result, $expected);
  3220. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3221. $expected = array(
  3222. 'div' => array('class' => 'error-message'),
  3223. 'ol' => array(),
  3224. '<li', 'notEmpty', '/li',
  3225. '<li', 'email', '/li',
  3226. '/ol',
  3227. '/div'
  3228. );
  3229. $this->assertTags($result, $expected);
  3230. $result = $this->Form->error('Contact.field', null, array(
  3231. 'listOptions' => array(
  3232. 'class' => 'ul-class',
  3233. 'itemOptions' => array(
  3234. 'class' => 'li-class'
  3235. )
  3236. )
  3237. ));
  3238. $expected = array(
  3239. 'div' => array('class' => 'error-message'),
  3240. 'ul' => array('class' => 'ul-class'),
  3241. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3242. array('li' => array('class' => 'li-class')), 'email', '/li',
  3243. '/ul',
  3244. '/div'
  3245. );
  3246. $this->assertTags($result, $expected);
  3247. }
  3248. /**
  3249. * test error options when using form->input();
  3250. *
  3251. * @return void
  3252. */
  3253. public function testInputErrorEscape() {
  3254. $this->Form->create('ValidateProfile');
  3255. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3256. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3257. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3258. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3259. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3260. $this->assertRegExp('/required<br>/', $result);
  3261. }
  3262. /**
  3263. * testPassword method
  3264. *
  3265. * Test password element generation
  3266. *
  3267. * @return void
  3268. */
  3269. public function testPassword() {
  3270. $Contact = ClassRegistry::getObject('Contact');
  3271. $result = $this->Form->password('Contact.field');
  3272. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3273. $Contact->validationErrors['passwd'] = 1;
  3274. $this->Form->request->data['Contact']['passwd'] = 'test';
  3275. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3276. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3277. }
  3278. /**
  3279. * testRadio method
  3280. *
  3281. * Test radio element set generation
  3282. *
  3283. * @return void
  3284. */
  3285. public function testRadio() {
  3286. $result = $this->Form->radio('Model.field', array('option A'));
  3287. $expected = array(
  3288. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3289. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3290. 'label' => array('for' => 'ModelField0'),
  3291. 'option A',
  3292. '/label'
  3293. );
  3294. $this->assertTags($result, $expected);
  3295. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3296. $expected = array(
  3297. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3299. 'label' => array('for' => 'ModelField12'),
  3300. 'half',
  3301. '/label'
  3302. );
  3303. $this->assertTags($result, $expected);
  3304. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3305. $expected = array(
  3306. 'fieldset' => array(),
  3307. 'legend' => array(),
  3308. 'Field',
  3309. '/legend',
  3310. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3311. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3312. array('label' => array('for' => 'ModelField0')),
  3313. 'option A',
  3314. '/label',
  3315. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3316. array('label' => array('for' => 'ModelField1')),
  3317. 'option B',
  3318. '/label',
  3319. '/fieldset'
  3320. );
  3321. $this->assertTags($result, $expected);
  3322. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3323. $expected = array(
  3324. 'fieldset' => array(),
  3325. 'legend' => array(),
  3326. 'Field',
  3327. '/legend',
  3328. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3329. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3330. array('label' => array('for' => 'ModelField0')),
  3331. 'option A',
  3332. '/label',
  3333. 'br' => array(),
  3334. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3335. array('label' => array('for' => 'ModelField1')),
  3336. 'option B',
  3337. '/label',
  3338. '/fieldset'
  3339. );
  3340. $this->assertTags($result, $expected);
  3341. $result = $this->Form->radio(
  3342. 'Employee.gender',
  3343. array('male' => 'Male', 'female' => 'Female'),
  3344. array('form' => 'my-form')
  3345. );
  3346. $expected = array(
  3347. 'fieldset' => array(),
  3348. 'legend' => array(),
  3349. 'Gender',
  3350. '/legend',
  3351. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
  3352. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
  3353. array('label' => array('for' => 'EmployeeGenderMale')),
  3354. 'Male',
  3355. '/label',
  3356. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
  3357. array('label' => array('for' => 'EmployeeGenderFemale')),
  3358. 'Female',
  3359. '/label',
  3360. '/fieldset',
  3361. );
  3362. $this->assertTags($result, $expected);
  3363. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3364. $expected = array(
  3365. 'fieldset' => array(),
  3366. 'legend' => array(),
  3367. 'Gender',
  3368. '/legend',
  3369. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3370. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3371. array('label' => array('for' => 'OfficerGenderMale')),
  3372. 'Male',
  3373. '/label',
  3374. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3375. array('label' => array('for' => 'OfficerGenderFemale')),
  3376. 'Female',
  3377. '/label',
  3378. '/fieldset',
  3379. );
  3380. $this->assertTags($result, $expected);
  3381. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3382. $expected = array(
  3383. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3384. array('input' => array(
  3385. 'type' => 'radio',
  3386. 'name' => 'data[Contact][1][imrequired]',
  3387. 'value' => '0',
  3388. 'id' => 'Contact1Imrequired0',
  3389. 'required' => 'required'
  3390. )),
  3391. 'label' => array('for' => 'Contact1Imrequired0'),
  3392. 'option A',
  3393. '/label'
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. $result = $this->Form->radio('Model.1.field', array('option A'));
  3397. $expected = array(
  3398. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3399. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3400. 'label' => array('for' => 'Model1Field0'),
  3401. 'option A',
  3402. '/label'
  3403. );
  3404. $this->assertTags($result, $expected);
  3405. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3406. $expected = array(
  3407. 'fieldset' => array(),
  3408. 'legend' => array(),
  3409. 'Field',
  3410. '/legend',
  3411. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3412. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3413. array('label' => array('for' => 'ModelField0')),
  3414. 'option A',
  3415. '/label',
  3416. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3417. array('label' => array('for' => 'ModelField1')),
  3418. 'option B',
  3419. '/label',
  3420. '/fieldset'
  3421. );
  3422. $this->assertTags($result, $expected);
  3423. $result = $this->Form->radio(
  3424. 'Model.field',
  3425. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3426. );
  3427. $expected = array(
  3428. 'fieldset' => array(),
  3429. 'legend' => array(),
  3430. 'Field',
  3431. '/legend',
  3432. 'input' => array(
  3433. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3434. 'id' => 'ModelField_', 'value' => '',
  3435. ),
  3436. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3437. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3438. array('label' => array('for' => 'ModelFieldAB')),
  3439. 'first',
  3440. '/label',
  3441. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3442. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3443. array('label' => array('for' => 'ModelFieldAB1')),
  3444. 'second',
  3445. '/label',
  3446. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3447. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3448. array('label' => array('for' => 'ModelFieldAB2')),
  3449. 'third',
  3450. '/label',
  3451. '/fieldset'
  3452. );
  3453. $this->assertTags($result, $expected);
  3454. }
  3455. /**
  3456. * testRadioDifferentModel
  3457. * Refs #2911
  3458. *
  3459. * @return void
  3460. */
  3461. public function testRadioDifferentModel() {
  3462. $this->Form->create('User');
  3463. $result = $this->Form->radio(
  3464. 'Model.field',
  3465. array('v1' => 'option A', 'v2' => 'option B'),
  3466. array('label' => true, 'legend' => false, 'value' => false)
  3467. );
  3468. $expected = array(
  3469. array('input' => array(
  3470. 'type' => 'radio', 'name' => 'data[Model][field]',
  3471. 'value' => 'v1', 'id' => 'ModelFieldV1'
  3472. )),
  3473. array('label' => array('for' => 'ModelFieldV1')),
  3474. 'option A',
  3475. '/label',
  3476. array('input' => array(
  3477. 'type' => 'radio', 'name' => 'data[Model][field]',
  3478. 'value' => 'v2', 'id' => 'ModelFieldV2'
  3479. )),
  3480. array('label' => array('for' => 'ModelFieldV2')),
  3481. 'option B',
  3482. '/label'
  3483. );
  3484. $this->assertTags($result, $expected);
  3485. }
  3486. /**
  3487. * Test radio inputs with between as string or array. Also ensure
  3488. * that an array with less between elements works.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testRadioBetween() {
  3493. $result = $this->Form->radio(
  3494. 'Model.field',
  3495. array('option A', 'option B'),
  3496. array('between' => 'I am between')
  3497. );
  3498. $expected = array(
  3499. 'fieldset' => array(),
  3500. 'legend' => array(),
  3501. 'Field',
  3502. '/legend',
  3503. 'I am between',
  3504. 'input' => array(
  3505. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3506. 'value' => '', 'id' => 'ModelField_'
  3507. ),
  3508. array('input' => array(
  3509. 'type' => 'radio', 'name' => 'data[Model][field]',
  3510. 'value' => '0', 'id' => 'ModelField0'
  3511. )),
  3512. array('label' => array('for' => 'ModelField0')),
  3513. 'option A',
  3514. '/label',
  3515. array('input' => array(
  3516. 'type' => 'radio', 'name' => 'data[Model][field]',
  3517. 'value' => '1', 'id' => 'ModelField1'
  3518. )),
  3519. array('label' => array('for' => 'ModelField1')),
  3520. 'option B',
  3521. '/label',
  3522. '/fieldset'
  3523. );
  3524. $this->assertTags($result, $expected);
  3525. $result = $this->Form->radio(
  3526. 'Model.field',
  3527. array('option A', 'option B', 'option C'),
  3528. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3529. );
  3530. $expected = array(
  3531. 'fieldset' => array(),
  3532. 'legend' => array(),
  3533. 'Field',
  3534. '/legend',
  3535. 'input' => array(
  3536. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3537. 'value' => '', 'id' => 'ModelField_'
  3538. ),
  3539. array('input' => array(
  3540. 'type' => 'radio', 'name' => 'data[Model][field]',
  3541. 'value' => '0', 'id' => 'ModelField0'
  3542. )),
  3543. array('label' => array('for' => 'ModelField0')),
  3544. 'option A',
  3545. '/label',
  3546. 'between A',
  3547. '--separator--',
  3548. array('input' => array(
  3549. 'type' => 'radio', 'name' => 'data[Model][field]',
  3550. 'value' => '1', 'id' => 'ModelField1'
  3551. )),
  3552. array('label' => array('for' => 'ModelField1')),
  3553. 'option B',
  3554. '/label',
  3555. 'between B',
  3556. '--separator--',
  3557. array('input' => array(
  3558. 'type' => 'radio', 'name' => 'data[Model][field]',
  3559. 'value' => '2', 'id' => 'ModelField2'
  3560. )),
  3561. array('label' => array('for' => 'ModelField2')),
  3562. 'option C',
  3563. '/label',
  3564. 'between C',
  3565. '/fieldset'
  3566. );
  3567. $this->assertTags($result, $expected);
  3568. $result = $this->Form->input('Model.field', array(
  3569. 'options' => array('1' => 'first', '2' => 'second'),
  3570. 'type' => 'radio',
  3571. 'before' => '--before--',
  3572. 'after' => '--after--',
  3573. 'separator' => '--separator--',
  3574. 'between' => array('--between first--', '--between second--')
  3575. ));
  3576. $expected = array(
  3577. 'div' => array('class' => 'input radio'),
  3578. '--before--',
  3579. 'fieldset' => array(),
  3580. 'legend' => array(),
  3581. 'Field',
  3582. '/legend',
  3583. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3584. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3585. array('label' => array('for' => 'ModelField1')),
  3586. 'first',
  3587. '/label',
  3588. '--between first--',
  3589. '--separator--',
  3590. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3591. array('label' => array('for' => 'ModelField2')),
  3592. 'second',
  3593. '/label',
  3594. '--between second--',
  3595. '/fieldset',
  3596. '--after--',
  3597. '/div'
  3598. );
  3599. $this->assertTags($result, $expected);
  3600. $result = $this->Form->input('Model.field', array(
  3601. 'options' => array('1' => 'first', '2' => 'second'),
  3602. 'type' => 'radio',
  3603. 'before' => '--before--',
  3604. 'after' => '--after--',
  3605. 'separator' => '--separator--',
  3606. 'between' => array('--between first--')
  3607. ));
  3608. $expected = array(
  3609. 'div' => array('class' => 'input radio'),
  3610. '--before--',
  3611. 'fieldset' => array(),
  3612. 'legend' => array(),
  3613. 'Field',
  3614. '/legend',
  3615. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3616. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3617. array('label' => array('for' => 'ModelField1')),
  3618. 'first',
  3619. '/label',
  3620. '--between first--',
  3621. '--separator--',
  3622. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3623. array('label' => array('for' => 'ModelField2')),
  3624. 'second',
  3625. '/label',
  3626. '/fieldset',
  3627. '--after--',
  3628. '/div'
  3629. );
  3630. $this->assertTags($result, $expected);
  3631. }
  3632. /**
  3633. * Test that radios with a 0 value are selected under the correct conditions.
  3634. * Also ensure that values that are booleanish are handled correctly.
  3635. *
  3636. * @return void
  3637. */
  3638. public function testRadioOptionWithBooleanishValues() {
  3639. $expected = array(
  3640. 'fieldset' => array(),
  3641. 'legend' => array(),
  3642. 'Field',
  3643. '/legend',
  3644. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3645. array('label' => array('for' => 'ModelField1')),
  3646. 'Yes',
  3647. '/label',
  3648. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3649. array('label' => array('for' => 'ModelField0')),
  3650. 'No',
  3651. '/label',
  3652. '/fieldset'
  3653. );
  3654. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3655. $this->assertTags($result, $expected);
  3656. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3657. $this->assertTags($result, $expected);
  3658. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3659. $this->assertTags($result, $expected);
  3660. $expected = array(
  3661. 'fieldset' => array(),
  3662. 'legend' => array(),
  3663. 'Field',
  3664. '/legend',
  3665. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3666. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3667. array('label' => array('for' => 'ModelField1')),
  3668. 'Yes',
  3669. '/label',
  3670. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3671. array('label' => array('for' => 'ModelField0')),
  3672. 'No',
  3673. '/label',
  3674. '/fieldset'
  3675. );
  3676. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3677. $this->assertTags($result, $expected);
  3678. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3679. $this->assertTags($result, $expected);
  3680. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3681. $this->assertTags($result, $expected);
  3682. $expected = array(
  3683. 'fieldset' => array(),
  3684. 'legend' => array(),
  3685. 'Field',
  3686. '/legend',
  3687. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3688. array('label' => array('for' => 'ModelField1')),
  3689. 'Yes',
  3690. '/label',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3692. array('label' => array('for' => 'ModelField0')),
  3693. 'No',
  3694. '/label',
  3695. '/fieldset'
  3696. );
  3697. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3698. $this->assertTags($result, $expected);
  3699. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3700. $this->assertTags($result, $expected);
  3701. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3702. $this->assertTags($result, $expected);
  3703. }
  3704. /**
  3705. * test disabled radio options
  3706. *
  3707. * @return void
  3708. */
  3709. public function testRadioDisabled() {
  3710. $result = $this->Form->radio(
  3711. 'Model.field',
  3712. array('option A', 'option B'),
  3713. array('disabled' => array(0), 'value' => '0')
  3714. );
  3715. $expected = array(
  3716. 'fieldset' => array(),
  3717. 'legend' => array(),
  3718. 'Field',
  3719. '/legend',
  3720. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3721. array('label' => array('for' => 'ModelField0')),
  3722. 'option A',
  3723. '/label',
  3724. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3725. array('label' => array('for' => 'ModelField1')),
  3726. 'option B',
  3727. '/label',
  3728. '/fieldset'
  3729. );
  3730. $this->assertTags($result, $expected);
  3731. $result = $this->Form->radio(
  3732. 'Model.field',
  3733. array('option A', 'option B'),
  3734. array('disabled' => true, 'value' => '0')
  3735. );
  3736. $expected = array(
  3737. 'fieldset' => array(),
  3738. 'legend' => array(),
  3739. 'Field',
  3740. '/legend',
  3741. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3742. array('label' => array('for' => 'ModelField0')),
  3743. 'option A',
  3744. '/label',
  3745. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3746. array('label' => array('for' => 'ModelField1')),
  3747. 'option B',
  3748. '/label',
  3749. '/fieldset'
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $result = $this->Form->radio(
  3753. 'Model.field',
  3754. array('option A', 'option B'),
  3755. array('disabled' => 'disabled', 'value' => '0')
  3756. );
  3757. $expected = array(
  3758. 'fieldset' => array(),
  3759. 'legend' => array(),
  3760. 'Field',
  3761. '/legend',
  3762. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3763. array('label' => array('for' => 'ModelField0')),
  3764. 'option A',
  3765. '/label',
  3766. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3767. array('label' => array('for' => 'ModelField1')),
  3768. 'option B',
  3769. '/label',
  3770. '/fieldset'
  3771. );
  3772. $this->assertTags($result, $expected);
  3773. $result = $this->Form->input('Model.field', array(
  3774. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3775. 'type' => 'radio',
  3776. 'disabled' => array(2, '3x'),
  3777. ));
  3778. $expected = array(
  3779. 'div' => array('class' => 'input radio'),
  3780. 'fieldset' => array(),
  3781. 'legend' => array(),
  3782. 'Field',
  3783. '/legend',
  3784. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3785. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3786. array('label' => array('for' => 'ModelField1')),
  3787. 'first',
  3788. '/label',
  3789. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3790. array('label' => array('for' => 'ModelField2')),
  3791. 'second',
  3792. '/label',
  3793. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3794. array('label' => array('for' => 'ModelField2x')),
  3795. '2x',
  3796. '/label',
  3797. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3798. array('label' => array('for' => 'ModelField3')),
  3799. 'third',
  3800. '/label',
  3801. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3802. array('label' => array('for' => 'ModelField3x')),
  3803. '3x',
  3804. '/label',
  3805. '/fieldset',
  3806. '/div'
  3807. );
  3808. $this->assertTags($result, $expected);
  3809. $result = $this->Form->input('Model.field', array(
  3810. 'type' => 'radio',
  3811. 'options' => array(
  3812. 1 => 'A',
  3813. 2 => 'B',
  3814. 3 => 'C'
  3815. ),
  3816. 'disabled' => array(1)
  3817. ));
  3818. $expected = array(
  3819. 'div' => array('class' => 'input radio'),
  3820. 'fieldset' => array(),
  3821. 'legend' => array(),
  3822. 'Field',
  3823. '/legend',
  3824. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3825. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3826. array('label' => array('for' => 'ModelField1')),
  3827. 'A',
  3828. '/label',
  3829. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3830. array('label' => array('for' => 'ModelField2')),
  3831. 'B',
  3832. '/label',
  3833. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3834. array('label' => array('for' => 'ModelField3')),
  3835. 'C',
  3836. '/label',
  3837. '/fieldset',
  3838. '/div'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. }
  3842. /**
  3843. * test disabling the hidden input for radio buttons
  3844. *
  3845. * @return void
  3846. */
  3847. public function testRadioHiddenInputDisabling() {
  3848. $result = $this->Form->input('Model.1.field', array(
  3849. 'type' => 'radio',
  3850. 'options' => array('option A'),
  3851. 'hiddenField' => false
  3852. )
  3853. );
  3854. $expected = array(
  3855. 'div' => array('class' => 'input radio'),
  3856. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3857. 'label' => array('for' => 'Model1Field0'),
  3858. 'option A',
  3859. '/label',
  3860. '/div'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3864. $expected = array(
  3865. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3866. 'label' => array('for' => 'Model1Field0'),
  3867. 'option A',
  3868. '/label'
  3869. );
  3870. $this->assertTags($result, $expected);
  3871. }
  3872. /**
  3873. * test adding an empty option for radio buttons
  3874. *
  3875. * @return void
  3876. */
  3877. public function testRadioAddEmptyOption() {
  3878. $result = $this->Form->input('Model.1.field', array(
  3879. 'type' => 'radio',
  3880. 'options' => array('option A'),
  3881. 'empty' => true,
  3882. 'hiddenField' => false
  3883. ));
  3884. $expected = array(
  3885. 'div' => array('class' => 'input radio'),
  3886. 'fieldset' => array(),
  3887. 'legend' => array(),
  3888. 'Field',
  3889. '/legend',
  3890. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3891. array('label' => array('for' => 'Model1Field')),
  3892. __('empty'),
  3893. '/label',
  3894. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3895. array('label' => array('for' => 'Model1Field0')),
  3896. 'option A',
  3897. '/label',
  3898. '/fieldset',
  3899. '/div'
  3900. );
  3901. $this->assertTags($result, $expected);
  3902. $result = $this->Form->input('Model.1.field', array(
  3903. 'type' => 'radio',
  3904. 'options' => array('option A'),
  3905. 'empty' => 'CustomEmptyLabel',
  3906. 'hiddenField' => false
  3907. ));
  3908. $expected = array(
  3909. 'div' => array('class' => 'input radio'),
  3910. 'fieldset' => array(),
  3911. 'legend' => array(),
  3912. 'Field',
  3913. '/legend',
  3914. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3915. array('label' => array('for' => 'Model1Field')),
  3916. 'CustomEmptyLabel',
  3917. '/label',
  3918. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3919. array('label' => array('for' => 'Model1Field0')),
  3920. 'option A',
  3921. '/label',
  3922. '/fieldset',
  3923. '/div'
  3924. );
  3925. $this->assertTags($result, $expected);
  3926. $result = $this->Form->input('Model.1.field', array(
  3927. 'type' => 'radio',
  3928. 'options' => array('option A'),
  3929. 'empty' => false,
  3930. 'hiddenField' => false
  3931. ));
  3932. $this->assertTextNotContains('"Model1Field"', $result);
  3933. }
  3934. /**
  3935. * Test that radio() accepts an array for label
  3936. *
  3937. * @return void
  3938. */
  3939. public function testRadioLabelArray() {
  3940. $result = $this->Form->input('Model.field', array(
  3941. 'type' => 'radio',
  3942. 'legend' => false,
  3943. 'label' => array(
  3944. 'class' => 'checkbox float-left',
  3945. ),
  3946. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3947. ));
  3948. $this->assertTextContains(
  3949. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3950. $result
  3951. );
  3952. }
  3953. /**
  3954. * Test that label id's match the input element id's when radio is called after create().
  3955. *
  3956. * @return void
  3957. */
  3958. public function testRadioWithCreate() {
  3959. $this->Form->create('Model');
  3960. $result = $this->Form->radio('recipient',
  3961. array('1' => '1', '2' => '2', '3' => '3'),
  3962. array('legend' => false, 'value' => '1')
  3963. );
  3964. $this->assertTextNotContains(
  3965. '<label for="ModelModelRecipient1">1</label>',
  3966. $result
  3967. );
  3968. $this->assertTextContains(
  3969. '<label for="ModelRecipient1">1</label>',
  3970. $result
  3971. );
  3972. }
  3973. /**
  3974. * testDomIdSuffix method
  3975. *
  3976. * @return void
  3977. */
  3978. public function testDomIdSuffix() {
  3979. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3980. $this->assertEquals('1StringWith1DollarSigns', $result);
  3981. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3982. $this->assertEquals('AbcXFooYBar', $result);
  3983. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  3984. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3985. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  3986. $this->assertEquals('AbcX=FooY=Bar', $result);
  3987. }
  3988. /**
  3989. * testDomIdSuffixCollisionResolvement()
  3990. *
  3991. * @return void
  3992. */
  3993. public function testDomIdSuffixCollisionResolvement() {
  3994. $result = $this->Form->domIdSuffix('a>b');
  3995. $this->assertEquals('AB', $result);
  3996. $result = $this->Form->domIdSuffix('a<b');
  3997. $this->assertEquals('AB1', $result);
  3998. $result = $this->Form->domIdSuffix('a\'b');
  3999. $this->assertEquals('AB2', $result);
  4000. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4001. $this->assertEquals('1StringWith1Dollar', $result);
  4002. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4003. $this->assertEquals('1StringWith1Dollar1', $result);
  4004. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4005. $this->assertEquals('1StringWith1Dollar2', $result);
  4006. }
  4007. /**
  4008. * testSelect method
  4009. *
  4010. * Test select element generation.
  4011. *
  4012. * @return void
  4013. */
  4014. public function testSelect() {
  4015. $result = $this->Form->select('Model.field', array());
  4016. $expected = array(
  4017. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4018. array('option' => array('value' => '')),
  4019. '/option',
  4020. '/select'
  4021. );
  4022. $this->assertTags($result, $expected);
  4023. $this->Form->request->data = array('Model' => array('field' => 'value'));
  4024. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4025. $expected = array(
  4026. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4027. array('option' => array('value' => '')),
  4028. '/option',
  4029. array('option' => array('value' => 'value', 'selected' => 'selected')),
  4030. 'good',
  4031. '/option',
  4032. array('option' => array('value' => 'other')),
  4033. 'bad',
  4034. '/option',
  4035. '/select'
  4036. );
  4037. $this->assertTags($result, $expected);
  4038. $this->Form->request->data = array();
  4039. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4040. $expected = array(
  4041. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4042. array('option' => array('value' => '')),
  4043. '/option',
  4044. array('option' => array('value' => 'value')),
  4045. 'good',
  4046. '/option',
  4047. array('option' => array('value' => 'other')),
  4048. 'bad',
  4049. '/option',
  4050. '/select'
  4051. );
  4052. $this->assertTags($result, $expected);
  4053. $result = $this->Form->select(
  4054. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  4055. array('empty' => false)
  4056. );
  4057. $expected = array(
  4058. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4059. array('option' => array('value' => 'first')),
  4060. 'first &quot;html&quot; &lt;chars&gt;',
  4061. '/option',
  4062. array('option' => array('value' => 'second')),
  4063. 'value',
  4064. '/option',
  4065. '/select'
  4066. );
  4067. $this->assertTags($result, $expected);
  4068. $result = $this->Form->select(
  4069. 'Model.field',
  4070. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4071. array('escape' => false, 'empty' => false)
  4072. );
  4073. $expected = array(
  4074. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4075. array('option' => array('value' => 'first')),
  4076. 'first "html" <chars>',
  4077. '/option',
  4078. array('option' => array('value' => 'second')),
  4079. 'value',
  4080. '/option',
  4081. '/select'
  4082. );
  4083. $this->assertTags($result, $expected);
  4084. $options = array(
  4085. array('value' => 'first', 'name' => 'First'),
  4086. array('value' => 'first', 'name' => 'Another First'),
  4087. );
  4088. $result = $this->Form->select(
  4089. 'Model.field',
  4090. $options,
  4091. array('escape' => false, 'empty' => false)
  4092. );
  4093. $expected = array(
  4094. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4095. array('option' => array('value' => 'first')),
  4096. 'First',
  4097. '/option',
  4098. array('option' => array('value' => 'first')),
  4099. 'Another First',
  4100. '/option',
  4101. '/select'
  4102. );
  4103. $this->assertTags($result, $expected);
  4104. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4105. $result = $this->Form->select(
  4106. 'Model.contact_id',
  4107. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4108. array('escape' => false, 'empty' => 'pick something')
  4109. );
  4110. $expected = array(
  4111. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4112. array('option' => array('value' => '')), 'pick something', '/option',
  4113. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4114. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4115. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4116. '/select'
  4117. );
  4118. $this->assertTags($result, $expected);
  4119. $this->Form->request->data['Model']['field'] = 0;
  4120. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4121. $expected = array(
  4122. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4123. array('option' => array('value' => '')), '/option',
  4124. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4125. array('option' => array('value' => '1')), 'Yes', '/option',
  4126. '/select'
  4127. );
  4128. $this->assertTags($result, $expected);
  4129. $this->Form->request->data['Model']['field'] = 50;
  4130. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4131. $expected = array(
  4132. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4133. array('option' => array('value' => '')), '/option',
  4134. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4135. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4136. '/select'
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. $result = $this->Form->select('Contact.required_one', array('option A'));
  4140. $expected = array(
  4141. 'select' => array(
  4142. 'name' => 'data[Contact][required_one]',
  4143. 'id' => 'ContactRequiredOne',
  4144. 'required' => 'required'
  4145. ),
  4146. array('option' => array('value' => '')), '/option',
  4147. array('option' => array('value' => '0')), 'option A', '/option',
  4148. '/select'
  4149. );
  4150. $this->assertTags($result, $expected);
  4151. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4152. $expected = array(
  4153. 'select' => array(
  4154. 'name' => 'data[Contact][required_one]',
  4155. 'id' => 'ContactRequiredOne',
  4156. 'disabled' => 'disabled'
  4157. ),
  4158. array('option' => array('value' => '')), '/option',
  4159. array('option' => array('value' => '0')), 'option A', '/option',
  4160. '/select'
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. }
  4164. /**
  4165. * test that select() with optiongroups listens to the escape param.
  4166. *
  4167. * @return void
  4168. */
  4169. public function testSelectOptionGroupEscaping() {
  4170. $options = array(
  4171. '>< Key' => array(
  4172. 1 => 'One',
  4173. 2 => 'Two'
  4174. )
  4175. );
  4176. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4177. $expected = array(
  4178. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4179. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4180. array('option' => array('value' => '1')), 'One', '/option',
  4181. array('option' => array('value' => '2')), 'Two', '/option',
  4182. '/optgroup',
  4183. '/select'
  4184. );
  4185. $this->assertTags($result, $expected);
  4186. $options = array(
  4187. '>< Key' => array(
  4188. 1 => 'One',
  4189. 2 => 'Two'
  4190. )
  4191. );
  4192. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4193. $expected = array(
  4194. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4195. 'optgroup' => array('label' => '>< Key'),
  4196. array('option' => array('value' => '1')), 'One', '/option',
  4197. array('option' => array('value' => '2')), 'Two', '/option',
  4198. '/optgroup',
  4199. '/select'
  4200. );
  4201. $this->assertTags($result, $expected);
  4202. }
  4203. /**
  4204. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4205. *
  4206. * @return void
  4207. */
  4208. public function testSelectWithNullAttributes() {
  4209. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4210. $expected = array(
  4211. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4212. array('option' => array('value' => '0')),
  4213. 'first',
  4214. '/option',
  4215. array('option' => array('value' => '1')),
  4216. 'second',
  4217. '/option',
  4218. '/select'
  4219. );
  4220. $this->assertTags($result, $expected);
  4221. }
  4222. /**
  4223. * testNestedSelect method
  4224. *
  4225. * test select element generation with optgroups
  4226. *
  4227. * @return void
  4228. */
  4229. public function testNestedSelect() {
  4230. $result = $this->Form->select(
  4231. 'Model.field',
  4232. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4233. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4234. )), array('empty' => false)
  4235. );
  4236. $expected = array(
  4237. 'select' => array('name' => 'data[Model][field]',
  4238. 'id' => 'ModelField'),
  4239. array('option' => array('value' => 1)),
  4240. 'One',
  4241. '/option',
  4242. array('option' => array('value' => 2)),
  4243. 'Two',
  4244. '/option',
  4245. array('optgroup' => array('label' => 'Three')),
  4246. array('option' => array('value' => 4)),
  4247. 'Four',
  4248. '/option',
  4249. array('option' => array('value' => 5)),
  4250. 'Five',
  4251. '/option',
  4252. '/optgroup',
  4253. '/select'
  4254. );
  4255. $this->assertTags($result, $expected);
  4256. $result = $this->Form->select(
  4257. 'Model.field',
  4258. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4259. array('showParents' => true, 'empty' => false)
  4260. );
  4261. $expected = array(
  4262. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4263. array('option' => array('value' => 1)),
  4264. 'One',
  4265. '/option',
  4266. array('option' => array('value' => 2)),
  4267. 'Two',
  4268. '/option',
  4269. array('optgroup' => array('label' => 'Three')),
  4270. array('option' => array('value' => 3)),
  4271. 'Three',
  4272. '/option',
  4273. array('option' => array('value' => 4)),
  4274. 'Four',
  4275. '/option',
  4276. '/optgroup',
  4277. '/select'
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. }
  4281. /**
  4282. * testSelectMultiple method
  4283. *
  4284. * test generation of multiple select elements
  4285. *
  4286. * @return void
  4287. */
  4288. public function testSelectMultiple() {
  4289. $options = array('first', 'second', 'third');
  4290. $result = $this->Form->select(
  4291. 'Model.multi_field', $options, array('multiple' => true)
  4292. );
  4293. $expected = array(
  4294. 'input' => array(
  4295. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4296. ),
  4297. 'select' => array(
  4298. 'name' => 'data[Model][multi_field][]',
  4299. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4300. ),
  4301. array('option' => array('value' => '0')),
  4302. 'first',
  4303. '/option',
  4304. array('option' => array('value' => '1')),
  4305. 'second',
  4306. '/option',
  4307. array('option' => array('value' => '2')),
  4308. 'third',
  4309. '/option',
  4310. '/select'
  4311. );
  4312. $this->assertTags($result, $expected);
  4313. $result = $this->Form->select(
  4314. 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
  4315. );
  4316. $expected = array(
  4317. 'input' => array(
  4318. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
  4319. 'form' => 'my-form',
  4320. ),
  4321. 'select' => array(
  4322. 'name' => 'data[Model][multi_field][]',
  4323. 'id' => 'ModelMultiField',
  4324. 'multiple' => 'multiple',
  4325. 'form' => 'my-form',
  4326. ),
  4327. array('option' => array('value' => '0')),
  4328. 'first',
  4329. '/option',
  4330. array('option' => array('value' => '1')),
  4331. 'second',
  4332. '/option',
  4333. array('option' => array('value' => '2')),
  4334. 'third',
  4335. '/option',
  4336. '/select'
  4337. );
  4338. $this->assertTags($result, $expected);
  4339. $result = $this->Form->select(
  4340. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4341. );
  4342. $expected = array(
  4343. 'input' => array(
  4344. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4345. ),
  4346. 'select' => array(
  4347. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4348. 'multiple' => 'multiple'
  4349. ),
  4350. array('option' => array('value' => '0', 'selected' => 'selected')),
  4351. 'first',
  4352. '/option',
  4353. array('option' => array('value' => '1', 'selected' => 'selected')),
  4354. 'second',
  4355. '/option',
  4356. array('option' => array('value' => '2')),
  4357. 'third',
  4358. '/option',
  4359. '/select'
  4360. );
  4361. $this->assertTags($result, $expected);
  4362. $result = $this->Form->select(
  4363. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4364. );
  4365. $expected = array(
  4366. 'select' => array(
  4367. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4368. ),
  4369. array('option' => array('value' => '0', 'selected' => 'selected')),
  4370. 'first',
  4371. '/option',
  4372. array('option' => array('value' => '1', 'selected' => 'selected')),
  4373. 'second',
  4374. '/option',
  4375. array('option' => array('value' => '2')),
  4376. 'third',
  4377. '/option',
  4378. '/select'
  4379. );
  4380. $this->assertTags($result, $expected);
  4381. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4382. $selected = array('2', '3x');
  4383. $result = $this->Form->select(
  4384. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4385. );
  4386. $expected = array(
  4387. 'input' => array(
  4388. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4389. ),
  4390. 'select' => array(
  4391. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4392. ),
  4393. array('option' => array('value' => '1')),
  4394. 'One',
  4395. '/option',
  4396. array('option' => array('value' => '2', 'selected' => 'selected')),
  4397. 'Two',
  4398. '/option',
  4399. array('option' => array('value' => '3')),
  4400. 'Three',
  4401. '/option',
  4402. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4403. 'Stringy',
  4404. '/option',
  4405. '/select'
  4406. );
  4407. $this->assertTags($result, $expected);
  4408. $result = $this->Form->select('Contact.required_one', array(
  4409. '1' => 'option A',
  4410. '2' => 'option B'
  4411. ), array('multiple' => true));
  4412. $expected = array(
  4413. 'input' => array(
  4414. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4415. ),
  4416. 'select' => array(
  4417. 'name' => 'data[Contact][required_one][]',
  4418. 'id' => 'ContactRequiredOne',
  4419. 'required' => 'required',
  4420. 'multiple' => 'multiple'
  4421. ),
  4422. array('option' => array('value' => '1')), 'option A', '/option',
  4423. array('option' => array('value' => '2')), 'option B', '/option',
  4424. '/select'
  4425. );
  4426. $this->assertTags($result, $expected);
  4427. $result = $this->Form->select(
  4428. 'Model.multi_field',
  4429. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4430. array('multiple' => true)
  4431. );
  4432. $expected = array(
  4433. 'input' => array(
  4434. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4435. 'id' => 'ModelMultiField_'
  4436. ),
  4437. array('select' => array('name' => 'data[Model][multi_field][]',
  4438. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4439. )),
  4440. array('option' => array('value' => 'a&gt;b')),
  4441. 'first',
  4442. '/option',
  4443. array('option' => array('value' => 'a&lt;b')),
  4444. 'second',
  4445. '/option',
  4446. array('option' => array(
  4447. 'value' => 'a&quot;b'
  4448. )),
  4449. 'third',
  4450. '/option',
  4451. '/select'
  4452. );
  4453. $this->assertTags($result, $expected);
  4454. }
  4455. /**
  4456. * Test generating multiple select with disabled elements.
  4457. *
  4458. * @return void
  4459. */
  4460. public function testSelectMultipleWithDisabledElements() {
  4461. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4462. $disabled = array(2, 3);
  4463. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4464. $expected = array(
  4465. 'input' => array(
  4466. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4467. ),
  4468. 'select' => array(
  4469. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4470. ),
  4471. array('option' => array('value' => '1')),
  4472. 'One',
  4473. '/option',
  4474. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4475. 'Two',
  4476. '/option',
  4477. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4478. 'Three',
  4479. '/option',
  4480. array('option' => array('value' => '3x')),
  4481. 'Stringy',
  4482. '/option',
  4483. '/select'
  4484. );
  4485. $this->assertTags($result, $expected);
  4486. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4487. $disabled = array('2', '3x');
  4488. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4489. $expected = array(
  4490. array('div' => array('class' => 'input select')),
  4491. array('label' => array('for' => 'ContactMultiple')),
  4492. 'Multiple',
  4493. '/label',
  4494. 'input' => array(
  4495. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4496. ),
  4497. 'select' => array(
  4498. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4499. ),
  4500. array('option' => array('value' => '1')),
  4501. 'One',
  4502. '/option',
  4503. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4504. 'Two',
  4505. '/option',
  4506. array('option' => array('value' => '3')),
  4507. 'Three',
  4508. '/option',
  4509. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4510. 'Stringy',
  4511. '/option',
  4512. '/select',
  4513. '/div'
  4514. );
  4515. $this->assertTags($result, $expected);
  4516. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4517. $disabled = true;
  4518. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4519. $expected = array(
  4520. array('div' => array('class' => 'input select')),
  4521. array('label' => array('for' => 'ContactMultiple')),
  4522. 'Multiple',
  4523. '/label',
  4524. 'input' => array(
  4525. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4526. ),
  4527. 'select' => array(
  4528. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4529. ),
  4530. array('option' => array('value' => '1')),
  4531. 'One',
  4532. '/option',
  4533. array('option' => array('value' => '2')),
  4534. 'Two',
  4535. '/option',
  4536. array('option' => array('value' => '3')),
  4537. 'Three',
  4538. '/option',
  4539. array('option' => array('value' => '3x')),
  4540. 'Stringy',
  4541. '/option',
  4542. '/select',
  4543. '/div'
  4544. );
  4545. $this->assertTags($result, $expected);
  4546. }
  4547. /**
  4548. * Test generating select with disabled elements.
  4549. *
  4550. * @return void
  4551. */
  4552. public function testSelectWithDisabledElements() {
  4553. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4554. $disabled = array(2, 3);
  4555. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4556. $expected = array(
  4557. 'select' => array(
  4558. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4559. ),
  4560. array('option' => array('value' => '')),
  4561. '/option',
  4562. array('option' => array('value' => '1')),
  4563. 'One',
  4564. '/option',
  4565. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4566. 'Two',
  4567. '/option',
  4568. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4569. 'Three',
  4570. '/option',
  4571. array('option' => array('value' => '3x')),
  4572. 'Stringy',
  4573. '/option',
  4574. '/select'
  4575. );
  4576. $this->assertTags($result, $expected);
  4577. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4578. $disabled = array('2', '3x');
  4579. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4580. $expected = array(
  4581. array('div' => array('class' => 'input select')),
  4582. array('label' => array('for' => 'ModelField')),
  4583. 'Field',
  4584. '/label',
  4585. 'select' => array(
  4586. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4587. ),
  4588. array('option' => array('value' => '1')),
  4589. 'One',
  4590. '/option',
  4591. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4592. 'Two',
  4593. '/option',
  4594. array('option' => array('value' => '3')),
  4595. 'Three',
  4596. '/option',
  4597. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4598. 'Stringy',
  4599. '/option',
  4600. '/select',
  4601. '/div'
  4602. );
  4603. $this->assertTags($result, $expected);
  4604. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4605. $disabled = true;
  4606. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4607. $expected = array(
  4608. array('div' => array('class' => 'input select')),
  4609. array('label' => array('for' => 'ModelField')),
  4610. 'Field',
  4611. '/label',
  4612. 'select' => array(
  4613. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4614. ),
  4615. array('option' => array('value' => '1')),
  4616. 'One',
  4617. '/option',
  4618. array('option' => array('value' => '2')),
  4619. 'Two',
  4620. '/option',
  4621. array('option' => array('value' => '3')),
  4622. 'Three',
  4623. '/option',
  4624. array('option' => array('value' => '3x')),
  4625. 'Stringy',
  4626. '/option',
  4627. '/select',
  4628. '/div'
  4629. );
  4630. $this->assertTags($result, $expected);
  4631. }
  4632. /**
  4633. * test generation of habtm select boxes.
  4634. *
  4635. * @return void
  4636. */
  4637. public function testHabtmSelectBox() {
  4638. $this->View->viewVars['contactTags'] = array(
  4639. 1 => 'blue',
  4640. 2 => 'red',
  4641. 3 => 'green'
  4642. );
  4643. $this->Form->request->data = array(
  4644. 'Contact' => array(),
  4645. 'ContactTag' => array(
  4646. array(
  4647. 'id' => '1',
  4648. 'name' => 'blue'
  4649. ),
  4650. array(
  4651. 'id' => 3,
  4652. 'name' => 'green'
  4653. )
  4654. )
  4655. );
  4656. $this->Form->create('Contact');
  4657. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4658. $expected = array(
  4659. 'input' => array(
  4660. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4661. ),
  4662. 'select' => array(
  4663. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4664. 'multiple' => 'multiple'
  4665. ),
  4666. array('option' => array('value' => '1', 'selected' => 'selected')),
  4667. 'blue',
  4668. '/option',
  4669. array('option' => array('value' => '2')),
  4670. 'red',
  4671. '/option',
  4672. array('option' => array('value' => '3', 'selected' => 'selected')),
  4673. 'green',
  4674. '/option',
  4675. '/select'
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. // make sure only 50 is selected, and not 50f5c0cf
  4679. $this->View->viewVars['contactTags'] = array(
  4680. '1' => 'blue',
  4681. '50f5c0cf' => 'red',
  4682. '50' => 'green'
  4683. );
  4684. $this->Form->request->data = array(
  4685. 'Contact' => array(),
  4686. 'ContactTag' => array(
  4687. array(
  4688. 'id' => 1,
  4689. 'name' => 'blue'
  4690. ),
  4691. array(
  4692. 'id' => 50,
  4693. 'name' => 'green'
  4694. )
  4695. )
  4696. );
  4697. $this->Form->create('Contact');
  4698. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4699. $expected = array(
  4700. 'input' => array(
  4701. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4702. ),
  4703. 'select' => array(
  4704. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4705. 'multiple' => 'multiple'
  4706. ),
  4707. array('option' => array('value' => '1', 'selected' => 'selected')),
  4708. 'blue',
  4709. '/option',
  4710. array('option' => array('value' => '50f5c0cf')),
  4711. 'red',
  4712. '/option',
  4713. array('option' => array('value' => '50', 'selected' => 'selected')),
  4714. 'green',
  4715. '/option',
  4716. '/select'
  4717. );
  4718. $this->assertTags($result, $expected);
  4719. }
  4720. /**
  4721. * test generation of multi select elements in checkbox format
  4722. *
  4723. * @return void
  4724. */
  4725. public function testSelectMultipleCheckboxes() {
  4726. $result = $this->Form->select(
  4727. 'Model.multi_field',
  4728. array('first', 'second', 'third'),
  4729. array('multiple' => 'checkbox')
  4730. );
  4731. $expected = array(
  4732. 'input' => array(
  4733. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4734. ),
  4735. array('div' => array('class' => 'checkbox')),
  4736. array('input' => array(
  4737. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4738. 'value' => '0', 'id' => 'ModelMultiField0'
  4739. )),
  4740. array('label' => array('for' => 'ModelMultiField0')),
  4741. 'first',
  4742. '/label',
  4743. '/div',
  4744. array('div' => array('class' => 'checkbox')),
  4745. array('input' => array(
  4746. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4747. 'value' => '1', 'id' => 'ModelMultiField1'
  4748. )),
  4749. array('label' => array('for' => 'ModelMultiField1')),
  4750. 'second',
  4751. '/label',
  4752. '/div',
  4753. array('div' => array('class' => 'checkbox')),
  4754. array('input' => array(
  4755. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4756. 'value' => '2', 'id' => 'ModelMultiField2'
  4757. )),
  4758. array('label' => array('for' => 'ModelMultiField2')),
  4759. 'third',
  4760. '/label',
  4761. '/div'
  4762. );
  4763. $this->assertTags($result, $expected);
  4764. $result = $this->Form->select(
  4765. 'Model.multi_field',
  4766. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4767. array('multiple' => 'checkbox')
  4768. );
  4769. $expected = array(
  4770. 'input' => array(
  4771. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4772. ),
  4773. array('div' => array('class' => 'checkbox')),
  4774. array('input' => array(
  4775. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4776. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4777. )),
  4778. array('label' => array('for' => 'ModelMultiFieldA')),
  4779. 'first',
  4780. '/label',
  4781. '/div',
  4782. array('div' => array('class' => 'checkbox')),
  4783. array('input' => array(
  4784. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4785. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4786. )),
  4787. array('label' => array('for' => 'ModelMultiFieldB')),
  4788. 'second',
  4789. '/label',
  4790. '/div',
  4791. array('div' => array('class' => 'checkbox')),
  4792. array('input' => array(
  4793. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4794. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4795. )),
  4796. array('label' => array('for' => 'ModelMultiFieldC')),
  4797. 'third',
  4798. '/label',
  4799. '/div'
  4800. );
  4801. $this->assertTags($result, $expected);
  4802. $result = $this->Form->select(
  4803. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4804. );
  4805. $expected = array(
  4806. 'input' => array(
  4807. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4808. ),
  4809. array('div' => array('class' => 'checkbox')),
  4810. array('input' => array(
  4811. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4812. 'value' => '1', 'id' => 'ModelMultiField1'
  4813. )),
  4814. array('label' => array('for' => 'ModelMultiField1')),
  4815. 'first',
  4816. '/label',
  4817. '/div'
  4818. );
  4819. $this->assertTags($result, $expected);
  4820. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4821. $result = $this->Form->select(
  4822. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4823. );
  4824. $expected = array(
  4825. 'input' => array(
  4826. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4827. ),
  4828. array('div' => array('class' => 'checkbox')),
  4829. array('input' => array(
  4830. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4831. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4832. )),
  4833. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4834. 'first',
  4835. '/label',
  4836. '/div',
  4837. array('div' => array('class' => 'checkbox')),
  4838. array('input' => array(
  4839. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4840. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4841. )),
  4842. array('label' => array('for' => 'ModelTagsArray')),
  4843. 'Array',
  4844. '/label',
  4845. '/div'
  4846. );
  4847. $this->assertTags($result, $expected);
  4848. $result = $this->Form->select(
  4849. 'Model.multi_field',
  4850. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4851. array('multiple' => 'checkbox')
  4852. );
  4853. $expected = array(
  4854. 'input' => array(
  4855. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4856. ),
  4857. array('div' => array('class' => 'checkbox')),
  4858. array('input' => array(
  4859. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4860. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4861. )),
  4862. array('label' => array('for' => 'ModelMultiFieldA2')),
  4863. 'first',
  4864. '/label',
  4865. '/div',
  4866. array('div' => array('class' => 'checkbox')),
  4867. array('input' => array(
  4868. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4869. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4870. )),
  4871. array('label' => array('for' => 'ModelMultiFieldA1')),
  4872. 'second',
  4873. '/label',
  4874. '/div',
  4875. array('div' => array('class' => 'checkbox')),
  4876. array('input' => array(
  4877. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4878. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4879. )),
  4880. array('label' => array('for' => 'ModelMultiFieldA')),
  4881. 'third',
  4882. '/label',
  4883. '/div'
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->select(
  4887. 'Model.multi_field',
  4888. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4889. array('multiple' => 'checkbox')
  4890. );
  4891. $expected = array(
  4892. 'input' => array(
  4893. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4894. ),
  4895. array('div' => array('class' => 'checkbox')),
  4896. array('input' => array(
  4897. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4898. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4899. )),
  4900. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4901. 'first',
  4902. '/label',
  4903. '/div',
  4904. array('div' => array('class' => 'checkbox')),
  4905. array('input' => array(
  4906. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4907. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4908. )),
  4909. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4910. 'second',
  4911. '/label',
  4912. '/div',
  4913. array('div' => array('class' => 'checkbox')),
  4914. array('input' => array(
  4915. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4916. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4917. )),
  4918. array('label' => array('for' => 'ModelMultiFieldAB')),
  4919. 'third',
  4920. '/label',
  4921. '/div'
  4922. );
  4923. $this->assertTags($result, $expected);
  4924. }
  4925. /**
  4926. * test multiple checkboxes with div styles.
  4927. *
  4928. * @return void
  4929. */
  4930. public function testSelectMultipleCheckboxDiv() {
  4931. $result = $this->Form->select(
  4932. 'Model.tags',
  4933. array('first', 'second'),
  4934. array('multiple' => 'checkbox', 'class' => 'my-class')
  4935. );
  4936. $expected = array(
  4937. 'input' => array(
  4938. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4939. ),
  4940. array('div' => array('class' => 'my-class')),
  4941. array('input' => array(
  4942. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4943. 'value' => '0', 'id' => 'ModelTags0'
  4944. )),
  4945. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4946. '/div',
  4947. array('div' => array('class' => 'my-class')),
  4948. array('input' => array(
  4949. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4950. 'value' => '1', 'id' => 'ModelTags1'
  4951. )),
  4952. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4953. '/div'
  4954. );
  4955. $this->assertTags($result, $expected);
  4956. $result = $this->Form->input('Model.tags', array(
  4957. 'options' => array('first', 'second'),
  4958. 'multiple' => 'checkbox',
  4959. 'class' => 'my-class',
  4960. 'div' => false,
  4961. 'label' => false
  4962. ));
  4963. $this->assertTags($result, $expected);
  4964. $Contact = ClassRegistry::getObject('Contact');
  4965. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4966. $result = $this->Form->input('Contact.tags', array(
  4967. 'options' => array('one'),
  4968. 'multiple' => 'checkbox',
  4969. 'label' => false,
  4970. 'div' => false
  4971. ));
  4972. $expected = array(
  4973. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4974. array('div' => array('class' => 'checkbox form-error')),
  4975. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4976. array('label' => array('for' => 'ContactTags0')),
  4977. 'one',
  4978. '/label',
  4979. '/div'
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $result = $this->Form->input('Contact.tags', array(
  4983. 'options' => array('one'),
  4984. 'multiple' => 'checkbox',
  4985. 'class' => 'mycheckbox',
  4986. 'label' => false,
  4987. 'div' => false
  4988. ));
  4989. $expected = array(
  4990. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4991. array('div' => array('class' => 'mycheckbox form-error')),
  4992. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4993. array('label' => array('for' => 'ContactTags0')),
  4994. 'one',
  4995. '/label',
  4996. '/div'
  4997. );
  4998. $this->assertTags($result, $expected);
  4999. }
  5000. /**
  5001. * Checks the security hash array generated for multiple-input checkbox elements
  5002. *
  5003. * @return void
  5004. */
  5005. public function testSelectMultipleCheckboxSecurity() {
  5006. $this->Form->request['_Token'] = array('key' => 'testKey');
  5007. $this->assertEquals(array(), $this->Form->fields);
  5008. $result = $this->Form->select(
  5009. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  5010. array('multiple' => 'checkbox')
  5011. );
  5012. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  5013. $result = $this->Form->secure($this->Form->fields);
  5014. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5015. $this->assertRegExp('/"' . $key . '"/', $result);
  5016. }
  5017. /**
  5018. * Multiple select elements should always be secured as they always participate
  5019. * in the POST data.
  5020. *
  5021. * @return void
  5022. */
  5023. public function testSelectMultipleSecureWithNoOptions() {
  5024. $this->Form->request['_Token'] = array('key' => 'testkey');
  5025. $this->assertEquals(array(), $this->Form->fields);
  5026. $this->Form->select(
  5027. 'Model.select',
  5028. array(),
  5029. array('multiple' => true)
  5030. );
  5031. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5032. }
  5033. /**
  5034. * When a select box has no options it should not be added to the fields list
  5035. * as it always fail post validation.
  5036. *
  5037. * @return void
  5038. */
  5039. public function testSelectNoSecureWithNoOptions() {
  5040. $this->Form->request['_Token'] = array('key' => 'testkey');
  5041. $this->assertEquals(array(), $this->Form->fields);
  5042. $this->Form->select(
  5043. 'Model.select',
  5044. array()
  5045. );
  5046. $this->assertEquals(array(), $this->Form->fields);
  5047. $this->Form->select(
  5048. 'Model.select',
  5049. array(),
  5050. array('empty' => true)
  5051. );
  5052. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5053. }
  5054. /**
  5055. * testInputMultipleCheckboxes method
  5056. *
  5057. * test input() resulting in multi select elements being generated.
  5058. *
  5059. * @return void
  5060. */
  5061. public function testInputMultipleCheckboxes() {
  5062. $result = $this->Form->input('Model.multi_field', array(
  5063. 'options' => array('first', 'second', 'third'),
  5064. 'multiple' => 'checkbox'
  5065. ));
  5066. $expected = array(
  5067. array('div' => array('class' => 'input select')),
  5068. array('label' => array('for' => 'ModelMultiField')),
  5069. 'Multi Field',
  5070. '/label',
  5071. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5072. array('div' => array('class' => 'checkbox')),
  5073. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5074. array('label' => array('for' => 'ModelMultiField0')),
  5075. 'first',
  5076. '/label',
  5077. '/div',
  5078. array('div' => array('class' => 'checkbox')),
  5079. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5080. array('label' => array('for' => 'ModelMultiField1')),
  5081. 'second',
  5082. '/label',
  5083. '/div',
  5084. array('div' => array('class' => 'checkbox')),
  5085. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5086. array('label' => array('for' => 'ModelMultiField2')),
  5087. 'third',
  5088. '/label',
  5089. '/div',
  5090. '/div'
  5091. );
  5092. $this->assertTags($result, $expected);
  5093. $result = $this->Form->input('Model.multi_field', array(
  5094. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5095. 'multiple' => 'checkbox'
  5096. ));
  5097. $expected = array(
  5098. array('div' => array('class' => 'input select')),
  5099. array('label' => array('for' => 'ModelMultiField')),
  5100. 'Multi Field',
  5101. '/label',
  5102. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5103. array('div' => array('class' => 'checkbox')),
  5104. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5105. array('label' => array('for' => 'ModelMultiFieldA')),
  5106. 'first',
  5107. '/label',
  5108. '/div',
  5109. array('div' => array('class' => 'checkbox')),
  5110. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5111. array('label' => array('for' => 'ModelMultiFieldB')),
  5112. 'second',
  5113. '/label',
  5114. '/div',
  5115. array('div' => array('class' => 'checkbox')),
  5116. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5117. array('label' => array('for' => 'ModelMultiFieldC')),
  5118. 'third',
  5119. '/label',
  5120. '/div',
  5121. '/div'
  5122. );
  5123. $this->assertTags($result, $expected);
  5124. $result = $this->Form->input('Model.multi_field', array(
  5125. 'options' => array('1' => 'first'),
  5126. 'multiple' => 'checkbox',
  5127. 'label' => false,
  5128. 'div' => false
  5129. ));
  5130. $expected = array(
  5131. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5132. array('div' => array('class' => 'checkbox')),
  5133. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5134. array('label' => array('for' => 'ModelMultiField1')),
  5135. 'first',
  5136. '/label',
  5137. '/div'
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. $result = $this->Form->input('Model.multi_field', array(
  5141. 'options' => array('2' => 'second'),
  5142. 'multiple' => 'checkbox',
  5143. 'label' => false,
  5144. 'div' => false
  5145. ));
  5146. $expected = array(
  5147. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5148. array('div' => array('class' => 'checkbox')),
  5149. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5150. array('label' => array('for' => 'ModelMultiField2')),
  5151. 'second',
  5152. '/label',
  5153. '/div'
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. }
  5157. /**
  5158. * testSelectHiddenFieldOmission method
  5159. *
  5160. * test that select() with 'hiddenField' => false omits the hidden field
  5161. *
  5162. * @return void
  5163. */
  5164. public function testSelectHiddenFieldOmission() {
  5165. $result = $this->Form->select('Model.multi_field',
  5166. array('first', 'second'),
  5167. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5168. );
  5169. $expected = array(
  5170. array('div' => array('class' => 'checkbox')),
  5171. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5172. array('label' => array('for' => 'ModelMultiField0')),
  5173. 'first',
  5174. '/label',
  5175. '/div',
  5176. array('div' => array('class' => 'checkbox')),
  5177. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5178. array('label' => array('for' => 'ModelMultiField1')),
  5179. 'second',
  5180. '/label',
  5181. '/div'
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. $result = $this->Form->input('Model.multi_field', array(
  5185. 'options' => array('first', 'second'),
  5186. 'multiple' => 'checkbox',
  5187. 'hiddenField' => false
  5188. ));
  5189. $expected = array(
  5190. array('div' => array('class' => 'input select')),
  5191. array('label' => array('for' => 'ModelMultiField')),
  5192. 'Multi Field',
  5193. '/label',
  5194. array('div' => array('class' => 'checkbox')),
  5195. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5196. array('label' => array('for' => 'ModelMultiField0')),
  5197. 'first',
  5198. '/label',
  5199. '/div',
  5200. array('div' => array('class' => 'checkbox')),
  5201. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5202. array('label' => array('for' => 'ModelMultiField1')),
  5203. 'second',
  5204. '/label',
  5205. '/div',
  5206. '/div'
  5207. );
  5208. $this->assertTags($result, $expected);
  5209. }
  5210. /**
  5211. * test that select() with multiple = checkbox works with overriding name attribute.
  5212. *
  5213. * @return void
  5214. */
  5215. public function testSelectCheckboxMultipleOverrideName() {
  5216. $result = $this->Form->input('category', array(
  5217. 'type' => 'select',
  5218. 'multiple' => 'checkbox',
  5219. 'name' => 'data[fish]',
  5220. 'options' => array('1', '2'),
  5221. 'div' => false,
  5222. 'label' => false,
  5223. ));
  5224. $expected = array(
  5225. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5226. array('div' => array('class' => 'checkbox')),
  5227. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5228. array('label' => array('for' => 'Category0')), '1', '/label',
  5229. '/div',
  5230. array('div' => array('class' => 'checkbox')),
  5231. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5232. array('label' => array('for' => 'Category1')), '2', '/label',
  5233. '/div'
  5234. );
  5235. $this->assertTags($result, $expected);
  5236. }
  5237. /**
  5238. * Test that 'id' overrides all the checkbox id's as well.
  5239. *
  5240. * @return void
  5241. */
  5242. public function testSelectCheckboxMultipleId() {
  5243. $result = $this->Form->select(
  5244. 'Model.multi_field',
  5245. array('first', 'second', 'third'),
  5246. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5247. );
  5248. $expected = array(
  5249. 'input' => array(
  5250. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5251. ),
  5252. array('div' => array('class' => 'checkbox')),
  5253. array('input' => array(
  5254. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5255. 'value' => '0', 'id' => 'CustomId0'
  5256. )),
  5257. array('label' => array('for' => 'CustomId0')),
  5258. 'first',
  5259. '/label',
  5260. '/div',
  5261. array('div' => array('class' => 'checkbox')),
  5262. array('input' => array(
  5263. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5264. 'value' => '1', 'id' => 'CustomId1'
  5265. )),
  5266. array('label' => array('for' => 'CustomId1')),
  5267. 'second',
  5268. '/label',
  5269. '/div',
  5270. array('div' => array('class' => 'checkbox')),
  5271. array('input' => array(
  5272. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5273. 'value' => '2', 'id' => 'CustomId2'
  5274. )),
  5275. array('label' => array('for' => 'CustomId2')),
  5276. 'third',
  5277. '/label',
  5278. '/div'
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. }
  5282. /**
  5283. * testCheckbox method
  5284. *
  5285. * Test generation of checkboxes
  5286. *
  5287. * @return void
  5288. */
  5289. public function testCheckbox() {
  5290. $result = $this->Form->checkbox('Model.field');
  5291. $expected = array(
  5292. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5293. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $result = $this->Form->checkbox('Model.field', array(
  5297. 'id' => 'theID',
  5298. 'value' => 'myvalue',
  5299. 'form' => 'my-form',
  5300. ));
  5301. $expected = array(
  5302. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
  5303. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
  5304. );
  5305. $this->assertTags($result, $expected);
  5306. $Contact = ClassRegistry::getObject('Contact');
  5307. $Contact->validationErrors['field'] = 1;
  5308. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5309. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5310. $expected = array(
  5311. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5312. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5313. );
  5314. $this->assertTags($result, $expected);
  5315. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5316. $expected = array(
  5317. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5318. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5319. );
  5320. $this->assertTags($result, $expected);
  5321. $this->Form->request->data['Contact']['field'] = '';
  5322. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5323. $expected = array(
  5324. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5325. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $Contact->validationErrors = array();
  5329. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5330. $expected = array(
  5331. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5332. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5333. );
  5334. $this->assertTags($result, $expected);
  5335. $this->Form->request->data['Contact']['published'] = 1;
  5336. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5337. $expected = array(
  5338. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5339. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5340. );
  5341. $this->assertTags($result, $expected);
  5342. $this->Form->request->data['Contact']['published'] = 0;
  5343. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5344. $expected = array(
  5345. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5346. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5347. );
  5348. $this->assertTags($result, $expected);
  5349. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5350. $expected = array(
  5351. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5352. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5353. );
  5354. $this->assertTags($result, $expected);
  5355. $result = $this->Form->checkbox('CustomField.1.value');
  5356. $expected = array(
  5357. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5358. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5359. );
  5360. $this->assertTags($result, $expected);
  5361. }
  5362. /**
  5363. * test checkbox() with a custom name attribute
  5364. *
  5365. * @return void
  5366. */
  5367. public function testCheckboxCustomNameAttribute() {
  5368. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5369. $expected = array(
  5370. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5371. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5372. );
  5373. $this->assertTags($result, $expected);
  5374. }
  5375. /**
  5376. * test the checked option for checkboxes.
  5377. *
  5378. * @return void
  5379. */
  5380. public function testCheckboxCheckedOption() {
  5381. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5382. $expected = array(
  5383. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5384. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5385. );
  5386. $this->assertTags($result, $expected);
  5387. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5388. $expected = array(
  5389. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5390. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5391. );
  5392. $this->assertTags($result, $expected);
  5393. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5394. $expected = array(
  5395. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5396. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5397. );
  5398. $this->assertTags($result, $expected);
  5399. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5400. $expected = array(
  5401. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5402. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. $this->Form->request->data['Model']['field'] = 1;
  5406. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5407. $expected = array(
  5408. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5409. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5410. );
  5411. $this->assertTags($result, $expected);
  5412. }
  5413. /**
  5414. * Test that disabled attribute works on both the checkbox and hidden input.
  5415. *
  5416. * @return void
  5417. */
  5418. public function testCheckboxDisabling() {
  5419. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5420. $expected = array(
  5421. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5422. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5423. );
  5424. $this->assertTags($result, $expected);
  5425. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5426. $expected = array(
  5427. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5428. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5429. );
  5430. $this->assertTags($result, $expected);
  5431. }
  5432. /**
  5433. * Test that the hidden input for checkboxes can be omitted or set to a
  5434. * specific value.
  5435. *
  5436. * @return void
  5437. */
  5438. public function testCheckboxHiddenField() {
  5439. $result = $this->Form->input('UserForm.something', array(
  5440. 'type' => 'checkbox',
  5441. 'hiddenField' => false
  5442. ));
  5443. $expected = array(
  5444. 'div' => array('class' => 'input checkbox'),
  5445. array('input' => array(
  5446. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5447. 'value' => '1', 'id' => 'UserFormSomething'
  5448. )),
  5449. 'label' => array('for' => 'UserFormSomething'),
  5450. 'Something',
  5451. '/label',
  5452. '/div'
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $result = $this->Form->input('UserForm.something', array(
  5456. 'type' => 'checkbox',
  5457. 'value' => 'Y',
  5458. 'hiddenField' => 'N',
  5459. ));
  5460. $expected = array(
  5461. 'div' => array('class' => 'input checkbox'),
  5462. array('input' => array(
  5463. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5464. 'value' => 'N', 'id' => 'UserFormSomething_'
  5465. )),
  5466. array('input' => array(
  5467. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5468. 'value' => 'Y', 'id' => 'UserFormSomething'
  5469. )),
  5470. 'label' => array('for' => 'UserFormSomething'),
  5471. 'Something',
  5472. '/label',
  5473. '/div'
  5474. );
  5475. $this->assertTags($result, $expected);
  5476. }
  5477. /**
  5478. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5479. *
  5480. * @return void
  5481. */
  5482. public function testCheckboxZeroValue() {
  5483. $result = $this->Form->input('User.get_spam', array(
  5484. 'type' => 'checkbox',
  5485. 'value' => '0',
  5486. 'hiddenField' => '1',
  5487. ));
  5488. $expected = array(
  5489. 'div' => array('class' => 'input checkbox'),
  5490. array('input' => array(
  5491. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5492. 'value' => '1', 'id' => 'UserGetSpam_'
  5493. )),
  5494. array('input' => array(
  5495. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5496. 'value' => '0', 'id' => 'UserGetSpam'
  5497. )),
  5498. 'label' => array('for' => 'UserGetSpam'),
  5499. 'Get Spam',
  5500. '/label',
  5501. '/div'
  5502. );
  5503. $this->assertTags($result, $expected);
  5504. }
  5505. /**
  5506. * testDateTime method
  5507. *
  5508. * Test generation of date/time select elements
  5509. *
  5510. * @return void
  5511. */
  5512. public function testDateTime() {
  5513. extract($this->dateRegex);
  5514. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5515. $now = strtotime('now');
  5516. $expected = array(
  5517. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5518. $daysRegex,
  5519. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5520. date('j', $now),
  5521. '/option',
  5522. '*/select',
  5523. '-',
  5524. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5525. $monthsRegex,
  5526. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5527. date('F', $now),
  5528. '/option',
  5529. '*/select',
  5530. '-',
  5531. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5532. $yearsRegex,
  5533. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5534. date('Y', $now),
  5535. '/option',
  5536. '*/select',
  5537. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5538. $hoursRegex,
  5539. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5540. date('g', $now),
  5541. '/option',
  5542. '*/select',
  5543. ':',
  5544. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5545. $minutesRegex,
  5546. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5547. date('i', $now),
  5548. '/option',
  5549. '*/select',
  5550. ' ',
  5551. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5552. $meridianRegex,
  5553. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5554. date('a', $now),
  5555. '/option',
  5556. '*/select'
  5557. );
  5558. $this->assertTags($result, $expected);
  5559. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5560. $expected = array(
  5561. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5562. $daysRegex,
  5563. array('option' => array('value' => '')),
  5564. '/option',
  5565. '*/select',
  5566. '-',
  5567. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5568. $monthsRegex,
  5569. array('option' => array('value' => '')),
  5570. '/option',
  5571. '*/select',
  5572. '-',
  5573. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5574. $yearsRegex,
  5575. array('option' => array('value' => '')),
  5576. '/option',
  5577. '*/select',
  5578. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5579. $hoursRegex,
  5580. array('option' => array('value' => '')),
  5581. '/option',
  5582. '*/select',
  5583. ':',
  5584. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5585. $minutesRegex,
  5586. array('option' => array('value' => '')),
  5587. '/option',
  5588. '*/select',
  5589. ' ',
  5590. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5591. $meridianRegex,
  5592. array('option' => array('value' => '')),
  5593. '/option',
  5594. '*/select'
  5595. );
  5596. $this->assertTags($result, $expected);
  5597. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5598. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5599. $this->assertTags($result, $expected);
  5600. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5601. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5602. $this->assertTags($result, $expected);
  5603. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5604. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5605. $expected = array(
  5606. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5607. $daysRegex,
  5608. array('option' => array('value' => '')),
  5609. '/option',
  5610. '*/select',
  5611. '-',
  5612. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5613. $monthsRegex,
  5614. array('option' => array('value' => '')),
  5615. '/option',
  5616. '*/select',
  5617. '-',
  5618. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5619. $yearsRegex,
  5620. array('option' => array('value' => '')),
  5621. '/option',
  5622. '*/select',
  5623. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5624. $hoursRegex,
  5625. array('option' => array('value' => '')),
  5626. '/option',
  5627. '*/select',
  5628. ':',
  5629. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5630. $minutesRegex,
  5631. array('option' => array('value' => '')),
  5632. '/option',
  5633. array('option' => array('value' => '00')),
  5634. '00',
  5635. '/option',
  5636. array('option' => array('value' => '05')),
  5637. '05',
  5638. '/option',
  5639. array('option' => array('value' => '10')),
  5640. '10',
  5641. '/option',
  5642. '*/select',
  5643. ' ',
  5644. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5645. $meridianRegex,
  5646. array('option' => array('value' => '')),
  5647. '/option',
  5648. '*/select'
  5649. );
  5650. $this->assertTags($result, $expected);
  5651. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5652. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5653. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5654. $this->Form->request->data['Contact']['data'] = null;
  5655. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5656. $expected = array(
  5657. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5658. $daysRegex,
  5659. array('option' => array('value' => '')),
  5660. '/option',
  5661. '*/select',
  5662. '-',
  5663. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5664. $monthsRegex,
  5665. array('option' => array('value' => '')),
  5666. '/option',
  5667. '*/select',
  5668. '-',
  5669. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5670. $yearsRegex,
  5671. array('option' => array('value' => '')),
  5672. '/option',
  5673. '*/select',
  5674. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5675. $hoursRegex,
  5676. array('option' => array('value' => '')),
  5677. '/option',
  5678. '*/select',
  5679. ':',
  5680. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5681. $minutesRegex,
  5682. array('option' => array('value' => '')),
  5683. '/option',
  5684. '*/select',
  5685. ' ',
  5686. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5687. $meridianRegex,
  5688. array('option' => array('value' => '')),
  5689. '/option',
  5690. '*/select'
  5691. );
  5692. $this->assertTags($result, $expected);
  5693. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5694. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5695. $now = strtotime($this->Form->data['Model']['field']);
  5696. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5697. $expected = array(
  5698. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5699. $daysRegex,
  5700. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5701. date('j', $now),
  5702. '/option',
  5703. '*/select',
  5704. '-',
  5705. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5706. $monthsRegex,
  5707. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5708. date('F', $now),
  5709. '/option',
  5710. '*/select',
  5711. '-',
  5712. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5713. $yearsRegex,
  5714. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5715. date('Y', $now),
  5716. '/option',
  5717. '*/select',
  5718. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5719. $hoursRegex,
  5720. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5721. date('g', $now),
  5722. '/option',
  5723. '*/select',
  5724. ':',
  5725. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5726. $minutesRegex,
  5727. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5728. date('i', $now),
  5729. '/option',
  5730. '*/select',
  5731. ' ',
  5732. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5733. $meridianRegex,
  5734. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5735. date('a', $now),
  5736. '/option',
  5737. '*/select'
  5738. );
  5739. $this->assertTags($result, $expected);
  5740. $selected = strtotime('2008-10-26 12:33:00');
  5741. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5742. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5743. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5744. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5745. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5746. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5747. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5748. $this->Form->create('Contact');
  5749. $result = $this->Form->input('published');
  5750. $now = strtotime('now');
  5751. $expected = array(
  5752. 'div' => array('class' => 'input date'),
  5753. 'label' => array('for' => 'ContactPublishedMonth'),
  5754. 'Published',
  5755. '/label',
  5756. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5757. $monthsRegex,
  5758. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5759. date('F', $now),
  5760. '/option',
  5761. '*/select',
  5762. '-',
  5763. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5764. $daysRegex,
  5765. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5766. date('j', $now),
  5767. '/option',
  5768. '*/select',
  5769. '-',
  5770. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5771. $yearsRegex,
  5772. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5773. date('Y', $now),
  5774. '/option',
  5775. '*/select',
  5776. '/div'
  5777. );
  5778. $this->assertTags($result, $expected);
  5779. $result = $this->Form->input('published2', array('type' => 'date'));
  5780. $now = strtotime('now');
  5781. $expected = array(
  5782. 'div' => array('class' => 'input date'),
  5783. 'label' => array('for' => 'ContactPublished2Month'),
  5784. 'Published2',
  5785. '/label',
  5786. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5787. $monthsRegex,
  5788. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5789. date('F', $now),
  5790. '/option',
  5791. '*/select',
  5792. '-',
  5793. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5794. $daysRegex,
  5795. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5796. date('j', $now),
  5797. '/option',
  5798. '*/select',
  5799. '-',
  5800. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5801. $yearsRegex,
  5802. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5803. date('Y', $now),
  5804. '/option',
  5805. '*/select',
  5806. '/div'
  5807. );
  5808. $this->assertTags($result, $expected);
  5809. $this->Form->create('Contact');
  5810. $result = $this->Form->input('published', array('monthNames' => false));
  5811. $now = strtotime('now');
  5812. $expected = array(
  5813. 'div' => array('class' => 'input date'),
  5814. 'label' => array('for' => 'ContactPublishedMonth'),
  5815. 'Published',
  5816. '/label',
  5817. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5818. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5819. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5820. date('m', $now),
  5821. '/option',
  5822. '*/select',
  5823. '-',
  5824. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5825. $daysRegex,
  5826. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5827. date('j', $now),
  5828. '/option',
  5829. '*/select',
  5830. '-',
  5831. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5832. $yearsRegex,
  5833. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5834. date('Y', $now),
  5835. '/option',
  5836. '*/select',
  5837. '/div'
  5838. );
  5839. $this->assertTags($result, $expected);
  5840. $result = $this->Form->input('published', array(
  5841. 'timeFormat' => 24,
  5842. 'interval' => 5,
  5843. 'selected' => strtotime('2009-09-03 13:37:00'),
  5844. 'type' => 'datetime'
  5845. ));
  5846. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5847. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5848. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5849. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5850. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5851. }
  5852. /**
  5853. * Test dateTime with rounding
  5854. *
  5855. * @return void
  5856. */
  5857. public function testDateTimeRounding() {
  5858. $this->Form->request->data['Contact'] = array(
  5859. 'date' => array(
  5860. 'day' => '13',
  5861. 'month' => '12',
  5862. 'year' => '2010',
  5863. 'hour' => '04',
  5864. 'min' => '19',
  5865. 'meridian' => 'AM'
  5866. )
  5867. );
  5868. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5869. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5870. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5871. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5872. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5873. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5874. }
  5875. /**
  5876. * Test that empty values don't trigger errors.
  5877. *
  5878. * @return void
  5879. */
  5880. public function testDateTimeNoErrorsOnEmptyData() {
  5881. $this->Form->request->data['Contact'] = array(
  5882. 'date' => array(
  5883. 'day' => '',
  5884. 'month' => '',
  5885. 'year' => '',
  5886. 'hour' => '',
  5887. 'min' => '',
  5888. 'meridian' => ''
  5889. )
  5890. );
  5891. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5892. $this->assertNotEmpty($result);
  5893. }
  5894. /**
  5895. * test that datetime() and default values work.
  5896. *
  5897. * @return void
  5898. */
  5899. public function testDatetimeWithDefault() {
  5900. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5901. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5902. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5903. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5904. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5905. 'default' => '2009-06-01 11:15:30'
  5906. ));
  5907. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5908. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5909. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5910. }
  5911. /**
  5912. * test that bogus non-date time data doesn't cause errors.
  5913. *
  5914. * @return void
  5915. */
  5916. public function testDateTimeWithBogusData() {
  5917. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5918. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5919. }
  5920. /**
  5921. * testDateTime all zeros
  5922. *
  5923. * @return void
  5924. */
  5925. public function testDateTimeAllZeros() {
  5926. $result = $this->Form->dateTime('Contact.date',
  5927. 'DMY',
  5928. false,
  5929. array(
  5930. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5931. 'value' => '0000-00-00'
  5932. )
  5933. );
  5934. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5935. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5936. }
  5937. /**
  5938. * testDateTimeEmptyAsArray
  5939. *
  5940. * @return void
  5941. */
  5942. public function testDateTimeEmptyAsArray() {
  5943. $result = $this->Form->dateTime('Contact.date',
  5944. 'DMY',
  5945. '12',
  5946. array(
  5947. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5948. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5949. )
  5950. )
  5951. );
  5952. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5953. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5954. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5955. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5956. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5957. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5958. $result = $this->Form->dateTime('Contact.date',
  5959. 'DMY',
  5960. '12',
  5961. array(
  5962. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5963. )
  5964. );
  5965. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5966. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5967. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5968. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5969. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5970. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5971. }
  5972. /**
  5973. * testFormDateTimeMulti method
  5974. *
  5975. * test multiple datetime element generation
  5976. *
  5977. * @return void
  5978. */
  5979. public function testFormDateTimeMulti() {
  5980. extract($this->dateRegex);
  5981. $result = $this->Form->dateTime('Contact.1.updated');
  5982. $expected = array(
  5983. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5984. $daysRegex,
  5985. array('option' => array('value' => '')),
  5986. '/option',
  5987. '*/select',
  5988. '-',
  5989. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5990. $monthsRegex,
  5991. array('option' => array('value' => '')),
  5992. '/option',
  5993. '*/select',
  5994. '-',
  5995. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5996. $yearsRegex,
  5997. array('option' => array('value' => '')),
  5998. '/option',
  5999. '*/select',
  6000. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  6001. $hoursRegex,
  6002. array('option' => array('value' => '')),
  6003. '/option',
  6004. '*/select',
  6005. ':',
  6006. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  6007. $minutesRegex,
  6008. array('option' => array('value' => '')),
  6009. '/option',
  6010. '*/select',
  6011. ' ',
  6012. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  6013. $meridianRegex,
  6014. array('option' => array('value' => '')),
  6015. '/option',
  6016. '*/select'
  6017. );
  6018. $this->assertTags($result, $expected);
  6019. $result = $this->Form->dateTime('Contact.2.updated');
  6020. $expected = array(
  6021. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  6022. $daysRegex,
  6023. array('option' => array('value' => '')),
  6024. '/option',
  6025. '*/select',
  6026. '-',
  6027. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  6028. $monthsRegex,
  6029. array('option' => array('value' => '')),
  6030. '/option',
  6031. '*/select',
  6032. '-',
  6033. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  6034. $yearsRegex,
  6035. array('option' => array('value' => '')),
  6036. '/option',
  6037. '*/select',
  6038. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  6039. $hoursRegex,
  6040. array('option' => array('value' => '')),
  6041. '/option',
  6042. '*/select',
  6043. ':',
  6044. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  6045. $minutesRegex,
  6046. array('option' => array('value' => '')),
  6047. '/option',
  6048. '*/select',
  6049. ' ',
  6050. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  6051. $meridianRegex,
  6052. array('option' => array('value' => '')),
  6053. '/option',
  6054. '*/select'
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. }
  6058. /**
  6059. * When changing the date format, the label should always focus the first select box when
  6060. * clicked.
  6061. *
  6062. * @return void
  6063. */
  6064. public function testDateTimeLabelIdMatchesFirstInput() {
  6065. $result = $this->Form->input('Model.date', array('type' => 'date'));
  6066. $this->assertContains('label for="ModelDateMonth"', $result);
  6067. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  6068. $this->assertContains('label for="ModelDateDay"', $result);
  6069. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  6070. $this->assertContains('label for="ModelDateYear"', $result);
  6071. }
  6072. /**
  6073. * testMonth method
  6074. *
  6075. * @return void
  6076. */
  6077. public function testMonth() {
  6078. $result = $this->Form->month('Model.field');
  6079. $expected = array(
  6080. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6081. array('option' => array('value' => '')),
  6082. '/option',
  6083. array('option' => array('value' => '01')),
  6084. date('F', strtotime('2008-01-01 00:00:00')),
  6085. '/option',
  6086. array('option' => array('value' => '02')),
  6087. date('F', strtotime('2008-02-01 00:00:00')),
  6088. '/option',
  6089. '*/select',
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $result = $this->Form->month('Model.field', array('empty' => true));
  6093. $expected = array(
  6094. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6095. array('option' => array('value' => '')),
  6096. '/option',
  6097. array('option' => array('value' => '01')),
  6098. date('F', strtotime('2008-01-01 00:00:00')),
  6099. '/option',
  6100. array('option' => array('value' => '02')),
  6101. date('F', strtotime('2008-02-01 00:00:00')),
  6102. '/option',
  6103. '*/select',
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6107. $expected = array(
  6108. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6109. array('option' => array('value' => '')),
  6110. '/option',
  6111. array('option' => array('value' => '01')),
  6112. '01',
  6113. '/option',
  6114. array('option' => array('value' => '02')),
  6115. '02',
  6116. '/option',
  6117. '*/select',
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $monthNames = array(
  6121. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6122. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6123. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6124. $expected = array(
  6125. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6126. array('option' => array('value' => '')),
  6127. '/option',
  6128. array('option' => array('value' => '01')),
  6129. 'Jan',
  6130. '/option',
  6131. array('option' => array('value' => '02')),
  6132. 'Feb',
  6133. '/option',
  6134. '*/select',
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6138. $result = $this->Form->month('Project.release');
  6139. $expected = array(
  6140. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6141. array('option' => array('value' => '')),
  6142. '/option',
  6143. array('option' => array('value' => '01')),
  6144. 'January',
  6145. '/option',
  6146. array('option' => array('value' => '02', 'selected' => 'selected')),
  6147. 'February',
  6148. '/option',
  6149. '*/select',
  6150. );
  6151. $this->assertTags($result, $expected);
  6152. $this->Form->request->data['Model']['field'] = '12a';
  6153. $result = $this->Form->month('Model.field');
  6154. $expected = array(
  6155. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6156. array('option' => array('value' => '')),
  6157. '/option',
  6158. array('option' => array('value' => '01')),
  6159. date('F', strtotime('2008-01-01 00:00:00')),
  6160. '/option',
  6161. array('option' => array('value' => '02')),
  6162. date('F', strtotime('2008-02-01 00:00:00')),
  6163. '/option',
  6164. '*/select',
  6165. );
  6166. $this->assertTags($result, $expected);
  6167. }
  6168. /**
  6169. * testDay method
  6170. *
  6171. * @return void
  6172. */
  6173. public function testDay() {
  6174. extract($this->dateRegex);
  6175. $result = $this->Form->day('Model.field', array('value' => false));
  6176. $expected = array(
  6177. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6178. array('option' => array('value' => '')),
  6179. '/option',
  6180. array('option' => array('value' => '01')),
  6181. '1',
  6182. '/option',
  6183. array('option' => array('value' => '02')),
  6184. '2',
  6185. '/option',
  6186. $daysRegex,
  6187. '/select',
  6188. );
  6189. $this->assertTags($result, $expected);
  6190. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6191. $result = $this->Form->day('Model.field');
  6192. $expected = array(
  6193. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6194. array('option' => array('value' => '')),
  6195. '/option',
  6196. array('option' => array('value' => '01')),
  6197. '1',
  6198. '/option',
  6199. array('option' => array('value' => '02')),
  6200. '2',
  6201. '/option',
  6202. $daysRegex,
  6203. array('option' => array('value' => '10', 'selected' => 'selected')),
  6204. '10',
  6205. '/option',
  6206. $daysRegex,
  6207. '/select',
  6208. );
  6209. $this->assertTags($result, $expected);
  6210. $this->Form->request->data['Model']['field'] = '';
  6211. $result = $this->Form->day('Model.field', array('value' => '10'));
  6212. $expected = array(
  6213. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6214. array('option' => array('value' => '')),
  6215. '/option',
  6216. array('option' => array('value' => '01')),
  6217. '1',
  6218. '/option',
  6219. array('option' => array('value' => '02')),
  6220. '2',
  6221. '/option',
  6222. $daysRegex,
  6223. array('option' => array('value' => '10', 'selected' => 'selected')),
  6224. '10',
  6225. '/option',
  6226. $daysRegex,
  6227. '/select',
  6228. );
  6229. $this->assertTags($result, $expected);
  6230. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6231. $result = $this->Form->day('Model.field', array('value' => true));
  6232. $expected = array(
  6233. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6234. array('option' => array('value' => '')),
  6235. '/option',
  6236. array('option' => array('value' => '01')),
  6237. '1',
  6238. '/option',
  6239. array('option' => array('value' => '02')),
  6240. '2',
  6241. '/option',
  6242. $daysRegex,
  6243. array('option' => array('value' => '10', 'selected' => 'selected')),
  6244. '10',
  6245. '/option',
  6246. $daysRegex,
  6247. '/select',
  6248. );
  6249. $this->assertTags($result, $expected);
  6250. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6251. $result = $this->Form->day('Project.release');
  6252. $expected = array(
  6253. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6254. array('option' => array('value' => '')),
  6255. '/option',
  6256. array('option' => array('value' => '01')),
  6257. '1',
  6258. '/option',
  6259. array('option' => array('value' => '02')),
  6260. '2',
  6261. '/option',
  6262. $daysRegex,
  6263. array('option' => array('value' => '10', 'selected' => 'selected')),
  6264. '10',
  6265. '/option',
  6266. $daysRegex,
  6267. '/select',
  6268. );
  6269. $this->assertTags($result, $expected);
  6270. $this->Form->request->data['Model']['field'] = '12e';
  6271. $result = $this->Form->day('Model.field');
  6272. $expected = array(
  6273. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6274. array('option' => array('value' => '')),
  6275. '/option',
  6276. array('option' => array('value' => '01')),
  6277. '1',
  6278. '/option',
  6279. array('option' => array('value' => '02')),
  6280. '2',
  6281. '/option',
  6282. $daysRegex,
  6283. '/select',
  6284. );
  6285. $this->assertTags($result, $expected);
  6286. }
  6287. /**
  6288. * testMinute method
  6289. *
  6290. * @return void
  6291. */
  6292. public function testMinute() {
  6293. extract($this->dateRegex);
  6294. $result = $this->Form->minute('Model.field');
  6295. $expected = array(
  6296. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6297. array('option' => array('value' => '')),
  6298. '/option',
  6299. array('option' => array('value' => '00')),
  6300. '00',
  6301. '/option',
  6302. array('option' => array('value' => '01')),
  6303. '01',
  6304. '/option',
  6305. array('option' => array('value' => '02')),
  6306. '02',
  6307. '/option',
  6308. $minutesRegex,
  6309. '/select',
  6310. );
  6311. $this->assertTags($result, $expected);
  6312. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6313. $result = $this->Form->minute('Model.field');
  6314. $expected = array(
  6315. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6316. array('option' => array('value' => '')),
  6317. '/option',
  6318. array('option' => array('value' => '00')),
  6319. '00',
  6320. '/option',
  6321. array('option' => array('value' => '01')),
  6322. '01',
  6323. '/option',
  6324. array('option' => array('value' => '02')),
  6325. '02',
  6326. '/option',
  6327. $minutesRegex,
  6328. array('option' => array('value' => '12', 'selected' => 'selected')),
  6329. '12',
  6330. '/option',
  6331. $minutesRegex,
  6332. '/select',
  6333. );
  6334. $this->assertTags($result, $expected);
  6335. $this->Form->request->data['Model']['field'] = '';
  6336. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6337. $expected = array(
  6338. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6339. array('option' => array('value' => '')),
  6340. '/option',
  6341. array('option' => array('value' => '00')),
  6342. '00',
  6343. '/option',
  6344. array('option' => array('value' => '05')),
  6345. '05',
  6346. '/option',
  6347. array('option' => array('value' => '10')),
  6348. '10',
  6349. '/option',
  6350. $minutesRegex,
  6351. '/select',
  6352. );
  6353. $this->assertTags($result, $expected);
  6354. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6355. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6356. $expected = array(
  6357. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6358. array('option' => array('value' => '')),
  6359. '/option',
  6360. array('option' => array('value' => '00')),
  6361. '00',
  6362. '/option',
  6363. array('option' => array('value' => '05')),
  6364. '05',
  6365. '/option',
  6366. array('option' => array('value' => '10', 'selected' => 'selected')),
  6367. '10',
  6368. '/option',
  6369. $minutesRegex,
  6370. '/select',
  6371. );
  6372. $this->assertTags($result, $expected);
  6373. $result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
  6374. $expected = array(
  6375. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6376. array('option' => array('value' => '')),
  6377. '/option',
  6378. array('option' => array('value' => '00')),
  6379. '00',
  6380. '/option',
  6381. array('option' => array('value' => '01')),
  6382. '01',
  6383. '/option',
  6384. array('option' => array('value' => '02')),
  6385. '02',
  6386. '/option',
  6387. $minutesRegex,
  6388. '/select',
  6389. );
  6390. $this->assertTags($result, $expected);
  6391. }
  6392. /**
  6393. * testHour method
  6394. *
  6395. * @return void
  6396. */
  6397. public function testHour() {
  6398. extract($this->dateRegex);
  6399. $result = $this->Form->hour('Model.field', false);
  6400. $expected = array(
  6401. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6402. array('option' => array('value' => '')),
  6403. '/option',
  6404. array('option' => array('value' => '01')),
  6405. '1',
  6406. '/option',
  6407. array('option' => array('value' => '02')),
  6408. '2',
  6409. '/option',
  6410. $hoursRegex,
  6411. '/select',
  6412. );
  6413. $this->assertTags($result, $expected);
  6414. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6415. $result = $this->Form->hour('Model.field', false);
  6416. $expected = array(
  6417. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6418. array('option' => array('value' => '')),
  6419. '/option',
  6420. array('option' => array('value' => '01')),
  6421. '1',
  6422. '/option',
  6423. array('option' => array('value' => '02')),
  6424. '2',
  6425. '/option',
  6426. $hoursRegex,
  6427. array('option' => array('value' => '12', 'selected' => 'selected')),
  6428. '12',
  6429. '/option',
  6430. '/select',
  6431. );
  6432. $this->assertTags($result, $expected);
  6433. $this->Form->request->data['Model']['field'] = '';
  6434. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6435. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6436. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6437. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6438. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6439. $result = $this->Form->hour('Model.field', true);
  6440. $expected = array(
  6441. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6442. array('option' => array('value' => '')),
  6443. '/option',
  6444. array('option' => array('value' => '00', 'selected' => 'selected')),
  6445. '0',
  6446. '/option',
  6447. array('option' => array('value' => '01')),
  6448. '1',
  6449. '/option',
  6450. array('option' => array('value' => '02')),
  6451. '2',
  6452. '/option',
  6453. $hoursRegex,
  6454. '/select',
  6455. );
  6456. $this->assertTags($result, $expected);
  6457. unset($this->Form->request->data['Model']['field']);
  6458. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6459. $thisHour = date('H');
  6460. $optValue = date('G');
  6461. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6462. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6463. $result = $this->Form->hour('Model.field', true);
  6464. $expected = array(
  6465. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6466. array('option' => array('value' => '')),
  6467. '/option',
  6468. array('option' => array('value' => '00')),
  6469. '0',
  6470. '/option',
  6471. array('option' => array('value' => '01', 'selected' => 'selected')),
  6472. '1',
  6473. '/option',
  6474. array('option' => array('value' => '02')),
  6475. '2',
  6476. '/option',
  6477. $hoursRegex,
  6478. '/select',
  6479. );
  6480. $this->assertTags($result, $expected);
  6481. $this->Form->request->data['Model']['field'] = '18a';
  6482. $result = $this->Form->hour('Model.field', false);
  6483. $expected = array(
  6484. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6485. array('option' => array('value' => '')),
  6486. '/option',
  6487. array('option' => array('value' => '01')),
  6488. '1',
  6489. '/option',
  6490. array('option' => array('value' => '02')),
  6491. '2',
  6492. '/option',
  6493. $hoursRegex,
  6494. '/select',
  6495. );
  6496. $this->assertTags($result, $expected);
  6497. }
  6498. /**
  6499. * testYear method
  6500. *
  6501. * @return void
  6502. */
  6503. public function testYear() {
  6504. $result = $this->Form->year('Model.field', 2006, 2007);
  6505. $expected = array(
  6506. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6507. array('option' => array('value' => '')),
  6508. '/option',
  6509. array('option' => array('value' => '2007')),
  6510. '2007',
  6511. '/option',
  6512. array('option' => array('value' => '2006')),
  6513. '2006',
  6514. '/option',
  6515. '/select',
  6516. );
  6517. $this->assertTags($result, $expected);
  6518. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6519. $expected = array(
  6520. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6521. array('option' => array('value' => '')),
  6522. '/option',
  6523. array('option' => array('value' => '2006')),
  6524. '2006',
  6525. '/option',
  6526. array('option' => array('value' => '2007')),
  6527. '2007',
  6528. '/option',
  6529. '/select',
  6530. );
  6531. $this->assertTags($result, $expected);
  6532. $this->request->data['Contact']['published'] = '';
  6533. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6534. $expected = array(
  6535. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6536. array('option' => array('value' => '')),
  6537. '/option',
  6538. array('option' => array('value' => '2007')),
  6539. '2007',
  6540. '/option',
  6541. array('option' => array('value' => '2006')),
  6542. '2006',
  6543. '/option',
  6544. '/select',
  6545. );
  6546. $this->assertTags($result, $expected);
  6547. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6548. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6549. $expected = array(
  6550. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6551. array('option' => array('value' => '2007')),
  6552. '2007',
  6553. '/option',
  6554. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6555. '2006',
  6556. '/option',
  6557. '/select',
  6558. );
  6559. $this->assertTags($result, $expected);
  6560. $this->Form->request->data['Contact']['published'] = '';
  6561. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6562. $expected = array(
  6563. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6564. array('option' => array('value' => '')),
  6565. '/option',
  6566. array('option' => array('value' => '2007')),
  6567. '2007',
  6568. '/option',
  6569. array('option' => array('value' => '2006')),
  6570. '2006',
  6571. '/option',
  6572. '/select',
  6573. );
  6574. $this->assertTags($result, $expected);
  6575. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6576. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6577. $expected = array(
  6578. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6579. array('option' => array('value' => '2007')),
  6580. '2007',
  6581. '/option',
  6582. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6583. '2006',
  6584. '/option',
  6585. '/select',
  6586. );
  6587. $this->assertTags($result, $expected);
  6588. $this->Form->request->data['Contact']['published'] = '';
  6589. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6590. $expected = array(
  6591. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6592. array('option' => array('value' => '')),
  6593. '/option',
  6594. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6595. '2007',
  6596. '/option',
  6597. array('option' => array('value' => '2006')),
  6598. '2006',
  6599. '/option',
  6600. '/select',
  6601. );
  6602. $this->assertTags($result, $expected);
  6603. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6604. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6605. $expected = array(
  6606. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6607. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6608. '2007',
  6609. '/option',
  6610. array('option' => array('value' => '2006')),
  6611. '2006',
  6612. '/option',
  6613. '/select',
  6614. );
  6615. $this->assertTags($result, $expected);
  6616. $this->Form->request->data['Contact']['published'] = '';
  6617. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6618. $expected = array(
  6619. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6620. array('option' => array('value' => '2008')),
  6621. '2008',
  6622. '/option',
  6623. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6624. '2007',
  6625. '/option',
  6626. array('option' => array('value' => '2006')),
  6627. '2006',
  6628. '/option',
  6629. '/select',
  6630. );
  6631. $this->assertTags($result, $expected);
  6632. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6633. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6634. $expected = array(
  6635. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6636. array('option' => array('value' => '2008')),
  6637. '2008',
  6638. '/option',
  6639. array('option' => array('value' => '2007')),
  6640. '2007',
  6641. '/option',
  6642. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6643. '2006',
  6644. '/option',
  6645. '/select',
  6646. );
  6647. $this->assertTags($result, $expected);
  6648. $this->Form->request->data = array();
  6649. $this->Form->create('Contact');
  6650. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6651. $expected = array(
  6652. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6653. array('option' => array('value' => '2008')),
  6654. '2008',
  6655. '/option',
  6656. array('option' => array('value' => '2007')),
  6657. '2007',
  6658. '/option',
  6659. array('option' => array('value' => '2006')),
  6660. '2006',
  6661. '/option',
  6662. '/select',
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6666. $this->assertContains('data[Contact][published][year]', $result);
  6667. $this->Form->request->data['Contact']['published'] = '2014ee';
  6668. $result = $this->Form->year('Contact.published', 2010, 2011);
  6669. $expected = array(
  6670. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6671. array('option' => array('value' => '')),
  6672. '/option',
  6673. array('option' => array('value' => '2011')),
  6674. '2011',
  6675. '/option',
  6676. array('option' => array('value' => '2010')),
  6677. '2010',
  6678. '/option',
  6679. '/select',
  6680. );
  6681. $this->assertTags($result, $expected);
  6682. }
  6683. /**
  6684. * testYearAutoExpandRange method
  6685. *
  6686. * @return void
  6687. */
  6688. public function testYearAutoExpandRange() {
  6689. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6690. $result = $this->Form->year('User.birthday');
  6691. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6692. $result = $matches[1];
  6693. $expected = range(date('Y') + 20, 1930);
  6694. $this->assertEquals($expected, $result);
  6695. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6696. $result = $this->Form->year('Project.release');
  6697. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6698. $result = $matches[1];
  6699. $expected = range(2050, date('Y') - 20);
  6700. $this->assertEquals($expected, $result);
  6701. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6702. $result = $this->Form->year('Project.release', 1890, 1900);
  6703. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6704. $result = $matches[1];
  6705. $expected = range(1900, 1881);
  6706. $this->assertEquals($expected, $result);
  6707. }
  6708. /**
  6709. * testInputDate method
  6710. *
  6711. * Test various inputs with type date and different dateFormat values.
  6712. * Failing to provide a dateFormat key should not error.
  6713. * It should simply not pre-select any value then.
  6714. *
  6715. * @return void
  6716. */
  6717. public function testInputDate() {
  6718. $this->Form->request->data = array(
  6719. 'User' => array(
  6720. 'month_year' => array('month' => date('m')),
  6721. 'just_year' => array('month' => date('m')),
  6722. 'just_month' => array('year' => date('Y')),
  6723. 'just_day' => array('month' => date('m')),
  6724. )
  6725. );
  6726. $this->Form->create('User');
  6727. $result = $this->Form->input('month_year',
  6728. array(
  6729. 'label' => false,
  6730. 'div' => false,
  6731. 'type' => 'date',
  6732. 'dateFormat' => 'MY',
  6733. 'minYear' => 2006,
  6734. 'maxYear' => 2008
  6735. )
  6736. );
  6737. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6738. $this->assertNotContains('value="2008" selected="selected"', $result);
  6739. $result = $this->Form->input('just_year',
  6740. array(
  6741. 'type' => 'date',
  6742. 'label' => false,
  6743. 'dateFormat' => 'Y',
  6744. 'minYear' => date('Y'),
  6745. 'maxYear' => date('Y', strtotime('+20 years'))
  6746. )
  6747. );
  6748. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6749. $result = $this->Form->input('just_month',
  6750. array(
  6751. 'type' => 'date',
  6752. 'label' => false,
  6753. 'dateFormat' => 'M',
  6754. 'empty' => false,
  6755. )
  6756. );
  6757. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6758. $result = $this->Form->input('just_day',
  6759. array(
  6760. 'type' => 'date',
  6761. 'label' => false,
  6762. 'dateFormat' => 'D',
  6763. 'empty' => false,
  6764. )
  6765. );
  6766. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6767. }
  6768. /**
  6769. * testInputDateMaxYear method
  6770. *
  6771. * Let's say we want to only allow users born from 2006 to 2008 to register
  6772. * This being the first singup page, we still don't have any data
  6773. *
  6774. * @return void
  6775. */
  6776. public function testInputDateMaxYear() {
  6777. $this->Form->request->data = array();
  6778. $this->Form->create('User');
  6779. $result = $this->Form->input('birthday',
  6780. array(
  6781. 'label' => false,
  6782. 'div' => false,
  6783. 'type' => 'date',
  6784. 'dateFormat' => 'DMY',
  6785. 'minYear' => 2006,
  6786. 'maxYear' => 2008
  6787. )
  6788. );
  6789. $this->assertContains('value="2008" selected="selected"', $result);
  6790. }
  6791. /**
  6792. * testTextArea method
  6793. *
  6794. * @return void
  6795. */
  6796. public function testTextArea() {
  6797. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6798. $result = $this->Form->textarea('Model.field');
  6799. $expected = array(
  6800. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6801. 'some test data',
  6802. '/textarea',
  6803. );
  6804. $this->assertTags($result, $expected);
  6805. $result = $this->Form->textarea('Model.tmp');
  6806. $expected = array(
  6807. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6808. '/textarea',
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6812. $result = $this->Form->textarea('Model.field');
  6813. $expected = array(
  6814. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6815. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6816. '/textarea',
  6817. );
  6818. $this->assertTags($result, $expected);
  6819. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6820. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6821. $expected = array(
  6822. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6823. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6824. '/textarea',
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6828. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6829. $expected = array(
  6830. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6831. '/textarea'
  6832. );
  6833. $this->assertTags($result, $expected);
  6834. }
  6835. /**
  6836. * testTextAreaWithStupidCharacters method
  6837. *
  6838. * test text area with non-ascii characters
  6839. *
  6840. * @return void
  6841. */
  6842. public function testTextAreaWithStupidCharacters() {
  6843. $this->loadFixtures('Post');
  6844. $result = $this->Form->input('Post.content', array(
  6845. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6846. ));
  6847. $expected = array(
  6848. 'div' => array('class' => 'input textarea'),
  6849. 'label' => array('for' => 'PostContent'),
  6850. 'Current Text',
  6851. '/label',
  6852. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6853. 'GREAT®',
  6854. '/textarea',
  6855. '/div'
  6856. );
  6857. $this->assertTags($result, $expected);
  6858. }
  6859. /**
  6860. * testHiddenField method
  6861. *
  6862. * @return void
  6863. */
  6864. public function testHiddenField() {
  6865. $Contact = ClassRegistry::getObject('Contact');
  6866. $Contact->validationErrors['field'] = 1;
  6867. $this->Form->request->data['Contact']['field'] = 'test';
  6868. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6869. $this->assertTags($result, array(
  6870. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6871. );
  6872. }
  6873. /**
  6874. * testFileUploadField method
  6875. *
  6876. * @return void
  6877. */
  6878. public function testFileUploadField() {
  6879. $result = $this->Form->file('Model.upload');
  6880. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6881. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6882. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6883. $expected = array(
  6884. 'div' => array('class' => 'input file'),
  6885. 'label' => array('for' => 'ModelUpload'),
  6886. 'Upload',
  6887. '/label',
  6888. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6889. '/div'
  6890. );
  6891. $this->assertTags($result, $expected);
  6892. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6893. $result = $this->Form->file('Model.upload');
  6894. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6895. }
  6896. /**
  6897. * test File upload input on a model not used in create();
  6898. *
  6899. * @return void
  6900. */
  6901. public function testFileUploadOnOtherModel() {
  6902. $this->Form->create('ValidateUser', array('type' => 'file'));
  6903. $result = $this->Form->file('ValidateProfile.city');
  6904. $expected = array(
  6905. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. }
  6909. /**
  6910. * testButton method
  6911. *
  6912. * @return void
  6913. */
  6914. public function testButton() {
  6915. $result = $this->Form->button('Hi');
  6916. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6917. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6918. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6919. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6920. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6921. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6922. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6923. $result = $this->Form->button('No type', array('type' => false));
  6924. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6925. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6926. $this->assertNotRegExp('/\&039/', $result);
  6927. }
  6928. /**
  6929. * Test that button() makes unlocked fields by default.
  6930. *
  6931. * @return void
  6932. */
  6933. public function testButtonUnlockedByDefault() {
  6934. $this->Form->request->params['_Token']['key'] = 'secured';
  6935. $this->Form->button('Save', array('name' => 'save'));
  6936. $this->Form->button('Clear');
  6937. $result = $this->Form->unlockField();
  6938. $this->assertEquals(array('save'), $result);
  6939. }
  6940. /**
  6941. * testPostButton method
  6942. *
  6943. * @return void
  6944. */
  6945. public function testPostButton() {
  6946. $result = $this->Form->postButton('Hi', '/controller/action');
  6947. $this->assertTags($result, array(
  6948. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6949. 'div' => array('style' => 'display:none;'),
  6950. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6951. '/div',
  6952. 'button' => array('type' => 'submit'),
  6953. 'Hi',
  6954. '/button',
  6955. '/form'
  6956. ));
  6957. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6958. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6959. }
  6960. /**
  6961. * Test using postButton with N dimensional data.
  6962. *
  6963. * @return void
  6964. */
  6965. public function testPostButtonNestedData() {
  6966. $data = array(
  6967. 'one' => array(
  6968. 'two' => array(
  6969. 3, 4, 5
  6970. )
  6971. )
  6972. );
  6973. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  6974. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  6975. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  6976. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  6977. }
  6978. /**
  6979. * Test that postButton adds _Token fields.
  6980. *
  6981. * @return void
  6982. */
  6983. public function testSecurePostButton() {
  6984. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6985. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6986. $expected = array(
  6987. 'form' => array(
  6988. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6989. ),
  6990. array('div' => array('style' => 'display:none;')),
  6991. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6992. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6993. '/div',
  6994. 'button' => array('type' => 'submit'),
  6995. 'Delete',
  6996. '/button',
  6997. array('div' => array('style' => 'display:none;')),
  6998. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6999. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7000. '/div',
  7001. '/form',
  7002. );
  7003. $this->assertTags($result, $expected);
  7004. }
  7005. /**
  7006. * testPostLink method
  7007. *
  7008. * @return void
  7009. */
  7010. public function testPostLink() {
  7011. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7012. $this->assertTags($result, array(
  7013. 'form' => array(
  7014. 'method' => 'post', 'action' => '/posts/delete/1',
  7015. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7016. ),
  7017. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7018. '/form',
  7019. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7020. 'Delete',
  7021. '/a'
  7022. ));
  7023. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  7024. $this->assertTags($result, array(
  7025. 'form' => array(
  7026. 'method' => 'post', 'action' => '/posts/delete/1',
  7027. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7028. ),
  7029. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  7030. '/form',
  7031. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7032. 'Delete',
  7033. '/a'
  7034. ));
  7035. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  7036. $this->assertTags($result, array(
  7037. 'form' => array(
  7038. 'method' => 'post', 'action' => '/posts/delete/1',
  7039. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7040. ),
  7041. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7042. '/form',
  7043. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7044. 'Delete',
  7045. '/a'
  7046. ));
  7047. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  7048. $this->assertTags($result, array(
  7049. 'form' => array(
  7050. 'method' => 'post', 'action' => '/posts/delete/1',
  7051. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7052. ),
  7053. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7054. '/form',
  7055. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7056. 'Delete',
  7057. '/a'
  7058. ));
  7059. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  7060. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  7061. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  7062. $this->assertTags($result, array(
  7063. 'form' => array(
  7064. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7065. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7066. ),
  7067. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7068. '/form',
  7069. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7070. 'Delete',
  7071. '/a'
  7072. ));
  7073. $result = $this->Form->postLink(
  7074. '',
  7075. array('controller' => 'items', 'action' => 'delete', 10),
  7076. array('class' => 'btn btn-danger', 'escape' => false),
  7077. 'Confirm thing'
  7078. );
  7079. $this->assertTags($result, array(
  7080. 'form' => array(
  7081. 'method' => 'post', 'action' => '/items/delete/10',
  7082. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7083. ),
  7084. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7085. '/form',
  7086. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7087. '/a'
  7088. ));
  7089. }
  7090. /**
  7091. * Test that security hashes for postLink include the url.
  7092. *
  7093. * @return void
  7094. */
  7095. public function testPostLinkSecurityHash() {
  7096. $hash = Security::hash(
  7097. '/posts/delete/1' .
  7098. serialize(array()) .
  7099. '' .
  7100. Configure::read('Security.salt')
  7101. );
  7102. $hash .= '%3A';
  7103. $this->Form->request->params['_Token']['key'] = 'test';
  7104. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7105. $this->assertTags($result, array(
  7106. 'form' => array(
  7107. 'method' => 'post', 'action' => '/posts/delete/1',
  7108. 'name', 'id', 'style' => 'display:none;'
  7109. ),
  7110. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7111. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
  7112. 'div' => array('style' => 'display:none;'),
  7113. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
  7114. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
  7115. '/div',
  7116. '/form',
  7117. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7118. 'Delete',
  7119. '/a'
  7120. ));
  7121. }
  7122. /**
  7123. * Test using postLink with N dimensional data.
  7124. *
  7125. * @return void
  7126. */
  7127. public function testPostLinkNestedData() {
  7128. $data = array(
  7129. 'one' => array(
  7130. 'two' => array(
  7131. 3, 4, 5
  7132. )
  7133. )
  7134. );
  7135. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  7136. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  7137. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  7138. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  7139. }
  7140. /**
  7141. * test creating postLinks after a GET form.
  7142. *
  7143. * @return void
  7144. */
  7145. public function testPostLinkAfterGetForm() {
  7146. $this->Form->request->params['_Token']['key'] = 'testkey';
  7147. $this->Form->create('User', array('type' => 'get'));
  7148. $this->Form->end();
  7149. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7150. $this->assertTags($result, array(
  7151. 'form' => array(
  7152. 'method' => 'post', 'action' => '/posts/delete/1',
  7153. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7154. ),
  7155. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7156. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7157. 'div' => array('style' => 'display:none;'),
  7158. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7159. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7160. '/div',
  7161. '/form',
  7162. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7163. 'Delete',
  7164. '/a'
  7165. ));
  7166. }
  7167. /**
  7168. * Test that postLink adds _Token fields.
  7169. *
  7170. * @return void
  7171. */
  7172. public function testSecurePostLink() {
  7173. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  7174. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7175. $expected = array(
  7176. 'form' => array(
  7177. 'method' => 'post', 'action' => '/posts/delete/1',
  7178. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7179. ),
  7180. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7181. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7182. 'div' => array('style' => 'display:none;'),
  7183. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7184. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7185. '/div',
  7186. '/form',
  7187. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7188. 'Delete',
  7189. '/a'
  7190. );
  7191. $this->assertTags($result, $expected);
  7192. }
  7193. /**
  7194. * Test that postLink adds form tags to view block
  7195. *
  7196. * @return void
  7197. */
  7198. public function testPostLinkFormBuffer() {
  7199. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  7200. $this->assertTags($result, array(
  7201. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7202. 'Delete',
  7203. '/a'
  7204. ));
  7205. $result = $this->View->fetch('postLink');
  7206. $this->assertTags($result, array(
  7207. 'form' => array(
  7208. 'method' => 'post', 'action' => '/posts/delete/1',
  7209. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7210. ),
  7211. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7212. '/form'
  7213. ));
  7214. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  7215. array('inline' => false, 'method' => 'DELETE')
  7216. );
  7217. $this->assertTags($result, array(
  7218. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7219. 'Delete',
  7220. '/a'
  7221. ));
  7222. $result = $this->View->fetch('postLink');
  7223. $this->assertTags($result, array(
  7224. 'form' => array(
  7225. 'method' => 'post', 'action' => '/posts/delete/1',
  7226. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7227. ),
  7228. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7229. '/form',
  7230. array(
  7231. 'form' => array(
  7232. 'method' => 'post', 'action' => '/posts/delete/2',
  7233. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7234. ),
  7235. ),
  7236. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  7237. '/form'
  7238. ));
  7239. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  7240. $this->assertTags($result, array(
  7241. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7242. 'Delete',
  7243. '/a'
  7244. ));
  7245. $result = $this->View->fetch('foobar');
  7246. $this->assertTags($result, array(
  7247. 'form' => array(
  7248. 'method' => 'post', 'action' => '/posts/delete/1',
  7249. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7250. ),
  7251. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7252. '/form'
  7253. ));
  7254. }
  7255. /**
  7256. * testSubmitButton method
  7257. *
  7258. * @return void
  7259. */
  7260. public function testSubmitButton() {
  7261. $result = $this->Form->submit('');
  7262. $expected = array(
  7263. 'div' => array('class' => 'submit'),
  7264. 'input' => array('type' => 'submit', 'value' => ''),
  7265. '/div'
  7266. );
  7267. $this->assertTags($result, $expected);
  7268. $result = $this->Form->submit('Test Submit');
  7269. $expected = array(
  7270. 'div' => array('class' => 'submit'),
  7271. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7272. '/div'
  7273. );
  7274. $this->assertTags($result, $expected);
  7275. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7276. $expected = array(
  7277. 'span' => array('class' => 'submit'),
  7278. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7279. '/span'
  7280. );
  7281. $this->assertTags($result, $expected);
  7282. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7283. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7284. $this->assertTags($result, $expected);
  7285. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7286. $expected = array(
  7287. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7288. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7289. '/div'
  7290. );
  7291. $this->assertTags($result, $expected);
  7292. $result = $this->Form->submit('Next >');
  7293. $expected = array(
  7294. 'div' => array('class' => 'submit'),
  7295. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7296. '/div'
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. $result = $this->Form->submit('Next >', array('escape' => false));
  7300. $expected = array(
  7301. 'div' => array('class' => 'submit'),
  7302. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7303. '/div'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7307. $expected = array(
  7308. 'div' => array('class' => 'submit'),
  7309. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. $before = '--before--';
  7314. $after = '--after--';
  7315. $result = $this->Form->submit('Test', array('before' => $before));
  7316. $expected = array(
  7317. 'div' => array('class' => 'submit'),
  7318. '--before--',
  7319. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7320. '/div'
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. $result = $this->Form->submit('Test', array('after' => $after));
  7324. $expected = array(
  7325. 'div' => array('class' => 'submit'),
  7326. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7327. '--after--',
  7328. '/div'
  7329. );
  7330. $this->assertTags($result, $expected);
  7331. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7332. $expected = array(
  7333. 'div' => array('class' => 'submit'),
  7334. '--before--',
  7335. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7336. '--after--',
  7337. '/div'
  7338. );
  7339. $this->assertTags($result, $expected);
  7340. }
  7341. /**
  7342. * test image submit types.
  7343. *
  7344. * @return void
  7345. */
  7346. public function testSubmitImage() {
  7347. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7348. $expected = array(
  7349. 'div' => array('class' => 'submit'),
  7350. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7351. '/div'
  7352. );
  7353. $this->assertTags($result, $expected);
  7354. $result = $this->Form->submit('/relative/cake.power.gif');
  7355. $expected = array(
  7356. 'div' => array('class' => 'submit'),
  7357. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7358. '/div'
  7359. );
  7360. $this->assertTags($result, $expected);
  7361. $result = $this->Form->submit('cake.power.gif');
  7362. $expected = array(
  7363. 'div' => array('class' => 'submit'),
  7364. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7365. '/div'
  7366. );
  7367. $this->assertTags($result, $expected);
  7368. $result = $this->Form->submit('Not.an.image');
  7369. $expected = array(
  7370. 'div' => array('class' => 'submit'),
  7371. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7372. '/div'
  7373. );
  7374. $this->assertTags($result, $expected);
  7375. $after = '--after--';
  7376. $before = '--before--';
  7377. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7378. $expected = array(
  7379. 'div' => array('class' => 'submit'),
  7380. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7381. '--after--',
  7382. '/div'
  7383. );
  7384. $this->assertTags($result, $expected);
  7385. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7386. $expected = array(
  7387. 'div' => array('class' => 'submit'),
  7388. '--before--',
  7389. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7390. '/div'
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7394. $expected = array(
  7395. 'div' => array('class' => 'submit'),
  7396. '--before--',
  7397. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7398. '--after--',
  7399. '/div'
  7400. );
  7401. $this->assertTags($result, $expected);
  7402. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7403. $expected = array(
  7404. 'div' => array('class' => 'submit'),
  7405. '--before--',
  7406. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7407. '--after--',
  7408. '/div'
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. }
  7412. /**
  7413. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7414. * be submitted at a time.
  7415. *
  7416. * @return void
  7417. */
  7418. public function testSubmitUnlockedByDefault() {
  7419. $this->Form->request->params['_Token']['key'] = 'secured';
  7420. $this->Form->submit('Go go');
  7421. $this->Form->submit('Save', array('name' => 'save'));
  7422. $result = $this->Form->unlockField();
  7423. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7424. }
  7425. /**
  7426. * Test submit image with timestamps.
  7427. *
  7428. * @return void
  7429. */
  7430. public function testSubmitImageTimestamp() {
  7431. Configure::write('Asset.timestamp', 'force');
  7432. $result = $this->Form->submit('cake.power.gif');
  7433. $expected = array(
  7434. 'div' => array('class' => 'submit'),
  7435. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7436. '/div'
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. }
  7440. /**
  7441. * test the create() method
  7442. *
  7443. * @return void
  7444. */
  7445. public function testCreate() {
  7446. $result = $this->Form->create('Contact');
  7447. $encoding = strtolower(Configure::read('App.encoding'));
  7448. $expected = array(
  7449. 'form' => array(
  7450. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7451. 'accept-charset' => $encoding
  7452. ),
  7453. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7454. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7455. '/div'
  7456. );
  7457. $this->assertTags($result, $expected);
  7458. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7459. $expected = array('form' => array(
  7460. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7461. 'accept-charset' => $encoding
  7462. ));
  7463. $this->assertTags($result, $expected);
  7464. $result = $this->Form->create('Contact', array('type' => 'get'));
  7465. $expected = array('form' => array(
  7466. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7467. 'accept-charset' => $encoding
  7468. ));
  7469. $this->assertTags($result, $expected);
  7470. $result = $this->Form->create('Contact', array('type' => 'put'));
  7471. $expected = array(
  7472. 'form' => array(
  7473. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7474. 'accept-charset' => $encoding
  7475. ),
  7476. 'div' => array('style' => 'display:none;'),
  7477. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7478. '/div'
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. $result = $this->Form->create('Contact', array('type' => 'file'));
  7482. $expected = array(
  7483. 'form' => array(
  7484. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7485. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7486. ),
  7487. 'div' => array('style' => 'display:none;'),
  7488. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7489. '/div'
  7490. );
  7491. $this->assertTags($result, $expected);
  7492. $this->Form->request->data['Contact']['id'] = 1;
  7493. $this->Form->request->here = '/contacts/edit/1';
  7494. $this->Form->request['action'] = 'edit';
  7495. $result = $this->Form->create('Contact');
  7496. $expected = array(
  7497. 'form' => array(
  7498. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7499. 'accept-charset' => $encoding
  7500. ),
  7501. 'div' => array('style' => 'display:none;'),
  7502. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7503. '/div'
  7504. );
  7505. $this->assertTags($result, $expected);
  7506. $this->Form->request->data['Contact']['id'] = 1;
  7507. $this->Form->request->here = '/contacts/edit/1';
  7508. $this->Form->request['action'] = 'edit';
  7509. $result = $this->Form->create('Contact', array('type' => 'file'));
  7510. $expected = array(
  7511. 'form' => array(
  7512. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7513. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7514. ),
  7515. 'div' => array('style' => 'display:none;'),
  7516. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7517. '/div'
  7518. );
  7519. $this->assertTags($result, $expected);
  7520. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7521. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7522. $expected = array(
  7523. 'form' => array(
  7524. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7525. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7526. ),
  7527. 'div' => array('style' => 'display:none;'),
  7528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7529. '/div'
  7530. );
  7531. $this->assertTags($result, $expected);
  7532. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7533. $expected = array(
  7534. 'form' => array(
  7535. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7536. 'accept-charset' => $encoding
  7537. ),
  7538. 'div' => array('style' => 'display:none;'),
  7539. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $this->Form->request['action'] = 'add';
  7544. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7545. $expected = array(
  7546. 'form' => array(
  7547. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7548. 'accept-charset' => $encoding
  7549. ),
  7550. 'div' => array('style' => 'display:none;'),
  7551. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7552. '/div'
  7553. );
  7554. $this->assertTags($result, $expected);
  7555. $result = $this->Form->create('User', array('action' => 'login'));
  7556. $expected = array(
  7557. 'form' => array(
  7558. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7559. 'accept-charset' => $encoding
  7560. ),
  7561. 'div' => array('style' => 'display:none;'),
  7562. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7563. '/div'
  7564. );
  7565. $this->assertTags($result, $expected);
  7566. $result = $this->Form->create('User', array('url' => '/users/login'));
  7567. $expected = array(
  7568. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7569. 'div' => array('style' => 'display:none;'),
  7570. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7571. '/div'
  7572. );
  7573. $this->assertTags($result, $expected);
  7574. $this->Form->request['controller'] = 'pages';
  7575. $result = $this->Form->create('User', array('action' => 'signup'));
  7576. $expected = array(
  7577. 'form' => array(
  7578. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7579. 'accept-charset' => $encoding
  7580. ),
  7581. 'div' => array('style' => 'display:none;'),
  7582. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7583. '/div'
  7584. );
  7585. $this->assertTags($result, $expected);
  7586. $this->Form->request->data = array();
  7587. $this->Form->request['controller'] = 'contacts';
  7588. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7589. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7590. $expected = array(
  7591. 'form' => array(
  7592. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7593. 'accept-charset' => 'utf-8'
  7594. ),
  7595. 'div' => array('style' => 'display:none;'),
  7596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7597. '/div'
  7598. );
  7599. $this->assertTags($result, $expected);
  7600. }
  7601. /**
  7602. * Test the onsubmit option for create()
  7603. *
  7604. * @return void
  7605. */
  7606. public function testCreateOnSubmit() {
  7607. $this->Form->request->data = array();
  7608. $this->Form->request['controller'] = 'contacts';
  7609. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7610. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7611. $expected = array(
  7612. 'form' => array(
  7613. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7614. 'accept-charset' => 'utf-8'
  7615. ),
  7616. 'div' => array('style' => 'display:none;'),
  7617. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7618. '/div'
  7619. );
  7620. $this->assertTags($result, $expected);
  7621. $this->Form->request->data = array();
  7622. $this->Form->request['controller'] = 'contacts';
  7623. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7624. $result = $this->Form->create(array(
  7625. 'url' => array('action' => 'index', 'param'),
  7626. 'default' => false,
  7627. 'onsubmit' => 'someFunction();'
  7628. ));
  7629. $expected = array(
  7630. 'form' => array(
  7631. 'id' => 'ContactAddForm', 'method' => 'post',
  7632. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7633. 'action' => '/contacts/index/param',
  7634. 'accept-charset' => 'utf-8'
  7635. ),
  7636. 'div' => array('style' => 'display:none;'),
  7637. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7638. '/div'
  7639. );
  7640. $this->assertTags($result, $expected);
  7641. }
  7642. /**
  7643. * test create() with automatic url generation
  7644. *
  7645. * @return void
  7646. */
  7647. public function testCreateAutoUrl() {
  7648. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7649. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7650. $this->Form->request->base = '/base_url';
  7651. $result = $this->Form->create('Contact');
  7652. $expected = array(
  7653. 'form' => array(
  7654. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7655. 'accept-charset' => 'utf-8'
  7656. ),
  7657. 'div' => array('style' => 'display:none;'),
  7658. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7659. '/div'
  7660. );
  7661. $this->assertTags($result, $expected);
  7662. $this->Form->request['action'] = 'delete';
  7663. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7664. $this->Form->request->base = '/base_url';
  7665. $result = $this->Form->create('Contact');
  7666. $expected = array(
  7667. 'form' => array(
  7668. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7669. 'accept-charset' => 'utf-8'
  7670. ),
  7671. 'div' => array('style' => 'display:none;'),
  7672. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7673. '/div'
  7674. );
  7675. $this->assertTags($result, $expected);
  7676. }
  7677. /**
  7678. * test create() with a custom route
  7679. *
  7680. * @return void
  7681. */
  7682. public function testCreateCustomRoute() {
  7683. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7684. $encoding = strtolower(Configure::read('App.encoding'));
  7685. $result = $this->Form->create('User', array('action' => 'login'));
  7686. $expected = array(
  7687. 'form' => array(
  7688. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7689. 'accept-charset' => $encoding
  7690. ),
  7691. 'div' => array('style' => 'display:none;'),
  7692. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7693. '/div'
  7694. );
  7695. $this->assertTags($result, $expected);
  7696. }
  7697. /**
  7698. * test that inputDefaults are stored and used.
  7699. *
  7700. * @return void
  7701. */
  7702. public function testCreateWithInputDefaults() {
  7703. $this->Form->create('User', array(
  7704. 'inputDefaults' => array(
  7705. 'div' => false,
  7706. 'label' => false,
  7707. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7708. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7709. )
  7710. ));
  7711. $result = $this->Form->input('username');
  7712. $expected = array(
  7713. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7714. );
  7715. $this->assertTags($result, $expected);
  7716. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7717. $expected = array(
  7718. 'div' => array('class' => 'input text'),
  7719. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7720. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7721. '/div'
  7722. );
  7723. $this->assertTags($result, $expected);
  7724. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7725. $expected = array(
  7726. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7727. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7728. );
  7729. $this->assertTags($result, $expected);
  7730. $this->Form->create('User', array(
  7731. 'inputDefaults' => array(
  7732. 'div' => false,
  7733. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7734. )
  7735. ));
  7736. $result = $this->Form->input('username', array('div' => true));
  7737. $expected = array(
  7738. 'div' => array('class' => 'input text'),
  7739. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7740. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7741. '/div'
  7742. );
  7743. $this->assertTags($result, $expected);
  7744. }
  7745. /**
  7746. * test automatic accept-charset overriding
  7747. *
  7748. * @return void
  7749. */
  7750. public function testCreateWithAcceptCharset() {
  7751. $result = $this->Form->create('UserForm', array(
  7752. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7753. )
  7754. );
  7755. $expected = array(
  7756. 'form' => array(
  7757. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7758. 'accept-charset' => 'iso-8859-1'
  7759. ),
  7760. 'div' => array('style' => 'display:none;'),
  7761. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7762. '/div'
  7763. );
  7764. $this->assertTags($result, $expected);
  7765. }
  7766. /**
  7767. * Test base form URL when url param is passed with multiple parameters (&)
  7768. *
  7769. * @return void
  7770. */
  7771. public function testCreateQuerystringrequest() {
  7772. $encoding = strtolower(Configure::read('App.encoding'));
  7773. $result = $this->Form->create('Contact', array(
  7774. 'type' => 'post',
  7775. 'escape' => false,
  7776. 'url' => array(
  7777. 'controller' => 'controller',
  7778. 'action' => 'action',
  7779. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7780. )
  7781. ));
  7782. $expected = array(
  7783. 'form' => array(
  7784. 'id' => 'ContactAddForm',
  7785. 'method' => 'post',
  7786. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7787. 'accept-charset' => $encoding
  7788. ),
  7789. 'div' => array('style' => 'display:none;'),
  7790. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7791. '/div'
  7792. );
  7793. $this->assertTags($result, $expected);
  7794. $result = $this->Form->create('Contact', array(
  7795. 'type' => 'post',
  7796. 'url' => array(
  7797. 'controller' => 'controller',
  7798. 'action' => 'action',
  7799. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7800. )
  7801. ));
  7802. $expected = array(
  7803. 'form' => array(
  7804. 'id' => 'ContactAddForm',
  7805. 'method' => 'post',
  7806. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7807. 'accept-charset' => $encoding
  7808. ),
  7809. 'div' => array('style' => 'display:none;'),
  7810. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7811. '/div'
  7812. );
  7813. $this->assertTags($result, $expected);
  7814. }
  7815. /**
  7816. * test that create() doesn't cause errors by multiple id's being in the primary key
  7817. * as could happen with multiple select or checkboxes.
  7818. *
  7819. * @return void
  7820. */
  7821. public function testCreateWithMultipleIdInData() {
  7822. $encoding = strtolower(Configure::read('App.encoding'));
  7823. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7824. $result = $this->Form->create('Contact');
  7825. $expected = array(
  7826. 'form' => array(
  7827. 'id' => 'ContactAddForm',
  7828. 'method' => 'post',
  7829. 'action' => '/contacts/add',
  7830. 'accept-charset' => $encoding
  7831. ),
  7832. 'div' => array('style' => 'display:none;'),
  7833. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7834. '/div'
  7835. );
  7836. $this->assertTags($result, $expected);
  7837. }
  7838. /**
  7839. * test that create() doesn't add in extra passed params.
  7840. *
  7841. * @return void
  7842. */
  7843. public function testCreatePassedArgs() {
  7844. $encoding = strtolower(Configure::read('App.encoding'));
  7845. $this->Form->request->data['Contact']['id'] = 1;
  7846. $result = $this->Form->create('Contact', array(
  7847. 'type' => 'post',
  7848. 'escape' => false,
  7849. 'url' => array(
  7850. 'action' => 'edit',
  7851. 'myparam'
  7852. )
  7853. ));
  7854. $expected = array(
  7855. 'form' => array(
  7856. 'id' => 'ContactAddForm',
  7857. 'method' => 'post',
  7858. 'action' => '/contacts/edit/myparam',
  7859. 'accept-charset' => $encoding
  7860. ),
  7861. 'div' => array('style' => 'display:none;'),
  7862. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7863. '/div'
  7864. );
  7865. $this->assertTags($result, $expected);
  7866. }
  7867. /**
  7868. * test that create() works without raising errors with a Mock Model
  7869. *
  7870. * @return void
  7871. */
  7872. public function testCreateNoErrorsWithMockModel() {
  7873. $encoding = strtolower(Configure::read('App.encoding'));
  7874. $ContactMock = $this->getMockBuilder('Contact')
  7875. ->disableOriginalConstructor()
  7876. ->getMock();
  7877. ClassRegistry::removeObject('Contact');
  7878. ClassRegistry::addObject('Contact', $ContactMock);
  7879. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7880. $expected = array('form' => array(
  7881. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7882. 'accept-charset' => $encoding
  7883. ));
  7884. $this->assertTags($result, $expected);
  7885. }
  7886. /**
  7887. * test creating a get form, and get form inputs.
  7888. *
  7889. * @return void
  7890. */
  7891. public function testGetFormCreate() {
  7892. $encoding = strtolower(Configure::read('App.encoding'));
  7893. $result = $this->Form->create('Contact', array('type' => 'get'));
  7894. $this->assertTags($result, array('form' => array(
  7895. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7896. 'accept-charset' => $encoding
  7897. )));
  7898. $result = $this->Form->text('Contact.name');
  7899. $this->assertTags($result, array('input' => array(
  7900. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7901. )));
  7902. $result = $this->Form->password('password');
  7903. $this->assertTags($result, array('input' => array(
  7904. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7905. )));
  7906. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7907. $result = $this->Form->text('user_form');
  7908. $this->assertTags($result, array('input' => array(
  7909. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7910. )));
  7911. }
  7912. /**
  7913. * test get form, and inputs when the model param is false
  7914. *
  7915. * @return void
  7916. */
  7917. public function testGetFormWithFalseModel() {
  7918. $encoding = strtolower(Configure::read('App.encoding'));
  7919. $this->Form->request['controller'] = 'contact_test';
  7920. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7921. $expected = array('form' => array(
  7922. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7923. 'accept-charset' => $encoding
  7924. ));
  7925. $this->assertTags($result, $expected);
  7926. $result = $this->Form->text('reason');
  7927. $expected = array(
  7928. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7929. );
  7930. $this->assertTags($result, $expected);
  7931. }
  7932. /**
  7933. * test that datetime() works with GET style forms.
  7934. *
  7935. * @return void
  7936. */
  7937. public function testDateTimeWithGetForms() {
  7938. extract($this->dateRegex);
  7939. $this->Form->create('Contact', array('type' => 'get'));
  7940. $result = $this->Form->datetime('created');
  7941. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7942. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7943. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7944. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7945. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7946. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7947. }
  7948. /**
  7949. * testEditFormWithData method
  7950. *
  7951. * test auto populating form elements from submitted data.
  7952. *
  7953. * @return void
  7954. */
  7955. public function testEditFormWithData() {
  7956. $this->Form->request->data = array('Person' => array(
  7957. 'id' => 1,
  7958. 'first_name' => 'Nate',
  7959. 'last_name' => 'Abele',
  7960. 'email' => 'nate@example.com'
  7961. ));
  7962. $this->Form->request->addParams(array(
  7963. 'models' => array('Person'),
  7964. 'controller' => 'people',
  7965. 'action' => 'add'
  7966. ));
  7967. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7968. $this->Form->create();
  7969. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7970. $expected = array(
  7971. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7972. 'select' => array(
  7973. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7974. ),
  7975. array('option' => array('value' => 1)), 'Nate', '/option',
  7976. array('option' => array('value' => 2)), 'Garrett', '/option',
  7977. array('option' => array('value' => 3)), 'Larry', '/option',
  7978. '/select'
  7979. );
  7980. $this->assertTags($result, $expected);
  7981. }
  7982. /**
  7983. * Test that required fields are created for various types of validation.
  7984. *
  7985. * @return void
  7986. */
  7987. public function testFormInputRequiredDetection() {
  7988. $this->Form->create('Contact');
  7989. $result = $this->Form->input('Contact.non_existing');
  7990. $expected = array(
  7991. 'div' => array('class' => 'input text'),
  7992. 'label' => array('for' => 'ContactNonExisting'),
  7993. 'Non Existing',
  7994. '/label',
  7995. 'input' => array(
  7996. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7997. 'id' => 'ContactNonExisting'
  7998. ),
  7999. '/div'
  8000. );
  8001. $this->assertTags($result, $expected);
  8002. $result = $this->Form->input('Contact.imrequired');
  8003. $expected = array(
  8004. 'div' => array('class' => 'input text required'),
  8005. 'label' => array('for' => 'ContactImrequired'),
  8006. 'Imrequired',
  8007. '/label',
  8008. 'input' => array(
  8009. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8010. 'id' => 'ContactImrequired',
  8011. 'required' => 'required'
  8012. ),
  8013. '/div'
  8014. );
  8015. $this->assertTags($result, $expected);
  8016. $result = $this->Form->input('Contact.imalsorequired');
  8017. $expected = array(
  8018. 'div' => array('class' => 'input text required'),
  8019. 'label' => array('for' => 'ContactImalsorequired'),
  8020. 'Imalsorequired',
  8021. '/label',
  8022. 'input' => array(
  8023. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  8024. 'id' => 'ContactImalsorequired',
  8025. 'required' => 'required'
  8026. ),
  8027. '/div'
  8028. );
  8029. $this->assertTags($result, $expected);
  8030. $result = $this->Form->input('Contact.imrequiredtoo');
  8031. $expected = array(
  8032. 'div' => array('class' => 'input text required'),
  8033. 'label' => array('for' => 'ContactImrequiredtoo'),
  8034. 'Imrequiredtoo',
  8035. '/label',
  8036. 'input' => array(
  8037. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  8038. 'id' => 'ContactImrequiredtoo',
  8039. 'required' => 'required'
  8040. ),
  8041. '/div'
  8042. );
  8043. $this->assertTags($result, $expected);
  8044. $result = $this->Form->input('Contact.required_one');
  8045. $expected = array(
  8046. 'div' => array('class' => 'input text required'),
  8047. 'label' => array('for' => 'ContactRequiredOne'),
  8048. 'Required One',
  8049. '/label',
  8050. 'input' => array(
  8051. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  8052. 'id' => 'ContactRequiredOne',
  8053. 'required' => 'required'
  8054. ),
  8055. '/div'
  8056. );
  8057. $this->assertTags($result, $expected);
  8058. $result = $this->Form->input('Contact.string_required');
  8059. $expected = array(
  8060. 'div' => array('class' => 'input text required'),
  8061. 'label' => array('for' => 'ContactStringRequired'),
  8062. 'String Required',
  8063. '/label',
  8064. 'input' => array(
  8065. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  8066. 'id' => 'ContactStringRequired',
  8067. 'required' => 'required'
  8068. ),
  8069. '/div'
  8070. );
  8071. $this->assertTags($result, $expected);
  8072. $result = $this->Form->input('Contact.imnotrequired');
  8073. $expected = array(
  8074. 'div' => array('class' => 'input text'),
  8075. 'label' => array('for' => 'ContactImnotrequired'),
  8076. 'Imnotrequired',
  8077. '/label',
  8078. 'input' => array(
  8079. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  8080. 'id' => 'ContactImnotrequired'
  8081. ),
  8082. '/div'
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. $result = $this->Form->input('Contact.imalsonotrequired');
  8086. $expected = array(
  8087. 'div' => array('class' => 'input text'),
  8088. 'label' => array('for' => 'ContactImalsonotrequired'),
  8089. 'Imalsonotrequired',
  8090. '/label',
  8091. 'input' => array(
  8092. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  8093. 'id' => 'ContactImalsonotrequired'
  8094. ),
  8095. '/div'
  8096. );
  8097. $this->assertTags($result, $expected);
  8098. $result = $this->Form->input('Contact.imalsonotrequired2');
  8099. $expected = array(
  8100. 'div' => array('class' => 'input text'),
  8101. 'label' => array('for' => 'ContactImalsonotrequired2'),
  8102. 'Imalsonotrequired2',
  8103. '/label',
  8104. 'input' => array(
  8105. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  8106. 'id' => 'ContactImalsonotrequired2'
  8107. ),
  8108. '/div'
  8109. );
  8110. $this->assertTags($result, $expected);
  8111. $result = $this->Form->input('Contact.imnotrequiredeither');
  8112. $expected = array(
  8113. 'div' => array('class' => 'input text'),
  8114. 'label' => array('for' => 'ContactImnotrequiredeither'),
  8115. 'Imnotrequiredeither',
  8116. '/label',
  8117. 'input' => array(
  8118. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  8119. 'id' => 'ContactImnotrequiredeither'
  8120. ),
  8121. '/div'
  8122. );
  8123. $this->assertTags($result, $expected);
  8124. $result = $this->Form->input('Contact.iamrequiredalways');
  8125. $expected = array(
  8126. 'div' => array('class' => 'input text required'),
  8127. 'label' => array('for' => 'ContactIamrequiredalways'),
  8128. 'Iamrequiredalways',
  8129. '/label',
  8130. 'input' => array(
  8131. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  8132. 'id' => 'ContactIamrequiredalways',
  8133. 'required' => 'required'
  8134. ),
  8135. '/div'
  8136. );
  8137. $this->assertTags($result, $expected);
  8138. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  8139. $expected = array(
  8140. 'div' => array('class' => 'input checkbox required'),
  8141. array('input' => array(
  8142. 'type' => 'hidden',
  8143. 'name' => 'data[Contact][boolean_field]',
  8144. 'id' => 'ContactBooleanField_',
  8145. 'value' => '0'
  8146. )),
  8147. array('input' => array(
  8148. 'type' => 'checkbox',
  8149. 'name' => 'data[Contact][boolean_field]',
  8150. 'value' => '1',
  8151. 'id' => 'ContactBooleanField'
  8152. )),
  8153. 'label' => array('for' => 'ContactBooleanField'),
  8154. 'Boolean Field',
  8155. '/label',
  8156. '/div'
  8157. );
  8158. $this->assertTags($result, $expected);
  8159. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  8160. $expected = array(
  8161. 'div' => array('class' => 'input checkbox required'),
  8162. array('input' => array(
  8163. 'type' => 'hidden',
  8164. 'name' => 'data[Contact][boolean_field]',
  8165. 'id' => 'ContactBooleanField_',
  8166. 'value' => '0'
  8167. )),
  8168. array('input' => array(
  8169. 'type' => 'checkbox',
  8170. 'name' => 'data[Contact][boolean_field]',
  8171. 'value' => '1',
  8172. 'id' => 'ContactBooleanField',
  8173. 'required' => 'required'
  8174. )),
  8175. 'label' => array('for' => 'ContactBooleanField'),
  8176. 'Boolean Field',
  8177. '/label',
  8178. '/div'
  8179. );
  8180. $this->assertTags($result, $expected);
  8181. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  8182. $expected = array(
  8183. 'div' => array('class' => 'input file required'),
  8184. 'label' => array('for' => 'ContactIamrequiredalways'),
  8185. 'Iamrequiredalways',
  8186. '/label',
  8187. 'input' => array(
  8188. 'type' => 'file',
  8189. 'name' => 'data[Contact][iamrequiredalways]',
  8190. 'id' => 'ContactIamrequiredalways',
  8191. 'required' => 'required'
  8192. ),
  8193. '/div'
  8194. );
  8195. $this->assertTags($result, $expected);
  8196. }
  8197. /**
  8198. * Test that required fields are created when only using ModelValidator::add().
  8199. *
  8200. * @return void
  8201. */
  8202. public function testFormInputRequiredDetectionModelValidator() {
  8203. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  8204. $this->Form->create('ContactTag');
  8205. $result = $this->Form->input('ContactTag.iwillberequired');
  8206. $expected = array(
  8207. 'div' => array('class' => 'input text required'),
  8208. 'label' => array('for' => 'ContactTagIwillberequired'),
  8209. 'Iwillberequired',
  8210. '/label',
  8211. 'input' => array(
  8212. 'name' => 'data[ContactTag][iwillberequired]',
  8213. 'type' => 'text',
  8214. 'id' => 'ContactTagIwillberequired',
  8215. 'required' => 'required'
  8216. ),
  8217. '/div'
  8218. );
  8219. $this->assertTags($result, $expected);
  8220. }
  8221. /**
  8222. * testFormMagicInput method
  8223. *
  8224. * @return void
  8225. */
  8226. public function testFormMagicInput() {
  8227. $encoding = strtolower(Configure::read('App.encoding'));
  8228. $result = $this->Form->create('Contact');
  8229. $expected = array(
  8230. 'form' => array(
  8231. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8232. 'accept-charset' => $encoding
  8233. ),
  8234. 'div' => array('style' => 'display:none;'),
  8235. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8236. '/div'
  8237. );
  8238. $this->assertTags($result, $expected);
  8239. $result = $this->Form->input('name');
  8240. $expected = array(
  8241. 'div' => array('class' => 'input text'),
  8242. 'label' => array('for' => 'ContactName'),
  8243. 'Name',
  8244. '/label',
  8245. 'input' => array(
  8246. 'type' => 'text', 'name' => 'data[Contact][name]',
  8247. 'id' => 'ContactName', 'maxlength' => '255'
  8248. ),
  8249. '/div'
  8250. );
  8251. $this->assertTags($result, $expected);
  8252. $result = $this->Form->input('non_existing_field_in_contact_model');
  8253. $expected = array(
  8254. 'div' => array('class' => 'input text'),
  8255. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  8256. 'Non Existing Field In Contact Model',
  8257. '/label',
  8258. 'input' => array(
  8259. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  8260. 'id' => 'ContactNonExistingFieldInContactModel'
  8261. ),
  8262. '/div'
  8263. );
  8264. $this->assertTags($result, $expected);
  8265. $result = $this->Form->input('Address.street');
  8266. $expected = array(
  8267. 'div' => array('class' => 'input text'),
  8268. 'label' => array('for' => 'AddressStreet'),
  8269. 'Street',
  8270. '/label',
  8271. 'input' => array(
  8272. 'type' => 'text', 'name' => 'data[Address][street]',
  8273. 'id' => 'AddressStreet'
  8274. ),
  8275. '/div'
  8276. );
  8277. $this->assertTags($result, $expected);
  8278. $result = $this->Form->input('Address.non_existing_field_in_model');
  8279. $expected = array(
  8280. 'div' => array('class' => 'input text'),
  8281. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8282. 'Non Existing Field In Model',
  8283. '/label',
  8284. 'input' => array(
  8285. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8286. 'id' => 'AddressNonExistingFieldInModel'
  8287. ),
  8288. '/div'
  8289. );
  8290. $this->assertTags($result, $expected);
  8291. $result = $this->Form->input('name', array('div' => false));
  8292. $expected = array(
  8293. 'label' => array('for' => 'ContactName'),
  8294. 'Name',
  8295. '/label',
  8296. 'input' => array(
  8297. 'type' => 'text', 'name' => 'data[Contact][name]',
  8298. 'id' => 'ContactName', 'maxlength' => '255'
  8299. )
  8300. );
  8301. $this->assertTags($result, $expected);
  8302. extract($this->dateRegex);
  8303. $now = strtotime('now');
  8304. $result = $this->Form->input('Contact.published', array('div' => false));
  8305. $expected = array(
  8306. 'label' => array('for' => 'ContactPublishedMonth'),
  8307. 'Published',
  8308. '/label',
  8309. array('select' => array(
  8310. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8311. )),
  8312. $monthsRegex,
  8313. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8314. date('F', $now),
  8315. '/option',
  8316. '*/select',
  8317. '-',
  8318. array('select' => array(
  8319. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8320. )),
  8321. $daysRegex,
  8322. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8323. date('j', $now),
  8324. '/option',
  8325. '*/select',
  8326. '-',
  8327. array('select' => array(
  8328. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8329. )),
  8330. $yearsRegex,
  8331. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8332. date('Y', $now),
  8333. '*/select'
  8334. );
  8335. $this->assertTags($result, $expected);
  8336. $result = $this->Form->input('Contact.updated', array('div' => false));
  8337. $expected = array(
  8338. 'label' => array('for' => 'ContactUpdatedMonth'),
  8339. 'Updated',
  8340. '/label',
  8341. array('select' => array(
  8342. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8343. )),
  8344. $monthsRegex,
  8345. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8346. date('F', $now),
  8347. '/option',
  8348. '*/select',
  8349. '-',
  8350. array('select' => array(
  8351. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8352. )),
  8353. $daysRegex,
  8354. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8355. date('j', $now),
  8356. '/option',
  8357. '*/select',
  8358. '-',
  8359. array('select' => array(
  8360. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8361. )),
  8362. $yearsRegex,
  8363. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8364. date('Y', $now),
  8365. '*/select'
  8366. );
  8367. $this->assertTags($result, $expected);
  8368. $result = $this->Form->input('UserForm.stuff');
  8369. $expected = array(
  8370. 'div' => array('class' => 'input text'),
  8371. 'label' => array('for' => 'UserFormStuff'),
  8372. 'Stuff',
  8373. '/label',
  8374. 'input' => array(
  8375. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8376. 'id' => 'UserFormStuff', 'maxlength' => 10
  8377. ),
  8378. '/div'
  8379. );
  8380. $this->assertTags($result, $expected);
  8381. }
  8382. /**
  8383. * testForMagicInputNonExistingNorValidated method
  8384. *
  8385. * @return void
  8386. */
  8387. public function testForMagicInputNonExistingNorValidated() {
  8388. $encoding = strtolower(Configure::read('App.encoding'));
  8389. $result = $this->Form->create('Contact');
  8390. $expected = array(
  8391. 'form' => array(
  8392. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8393. 'accept-charset' => $encoding
  8394. ),
  8395. 'div' => array('style' => 'display:none;'),
  8396. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8397. '/div'
  8398. );
  8399. $this->assertTags($result, $expected);
  8400. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8401. $expected = array(
  8402. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8403. 'Non Existing Nor Validated',
  8404. '/label',
  8405. 'input' => array(
  8406. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8407. 'id' => 'ContactNonExistingNorValidated'
  8408. )
  8409. );
  8410. $this->assertTags($result, $expected);
  8411. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8412. 'div' => false, 'value' => 'my value'
  8413. ));
  8414. $expected = array(
  8415. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8416. 'Non Existing Nor Validated',
  8417. '/label',
  8418. 'input' => array(
  8419. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8420. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8421. )
  8422. );
  8423. $this->assertTags($result, $expected);
  8424. $this->Form->request->data = array(
  8425. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8426. ));
  8427. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8428. $expected = array(
  8429. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8430. 'Non Existing Nor Validated',
  8431. '/label',
  8432. 'input' => array(
  8433. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8434. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8435. )
  8436. );
  8437. $this->assertTags($result, $expected);
  8438. }
  8439. /**
  8440. * testFormMagicInputLabel method
  8441. *
  8442. * @return void
  8443. */
  8444. public function testFormMagicInputLabel() {
  8445. $encoding = strtolower(Configure::read('App.encoding'));
  8446. $result = $this->Form->create('Contact');
  8447. $expected = array(
  8448. 'form' => array(
  8449. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8450. 'accept-charset' => $encoding
  8451. ),
  8452. 'div' => array('style' => 'display:none;'),
  8453. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8454. '/div'
  8455. );
  8456. $this->assertTags($result, $expected);
  8457. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8458. $this->assertTags($result, array('input' => array(
  8459. 'name' => 'data[Contact][name]', 'type' => 'text',
  8460. 'id' => 'ContactName', 'maxlength' => '255')
  8461. ));
  8462. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8463. $expected = array(
  8464. 'label' => array('for' => 'ContactName'),
  8465. 'My label',
  8466. '/label',
  8467. 'input' => array(
  8468. 'type' => 'text', 'name' => 'data[Contact][name]',
  8469. 'id' => 'ContactName', 'maxlength' => '255'
  8470. )
  8471. );
  8472. $this->assertTags($result, $expected);
  8473. $result = $this->Form->input('Contact.name', array(
  8474. 'div' => false, 'label' => array('class' => 'mandatory')
  8475. ));
  8476. $expected = array(
  8477. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8478. 'Name',
  8479. '/label',
  8480. 'input' => array(
  8481. 'type' => 'text', 'name' => 'data[Contact][name]',
  8482. 'id' => 'ContactName', 'maxlength' => '255'
  8483. )
  8484. );
  8485. $this->assertTags($result, $expected);
  8486. $result = $this->Form->input('Contact.name', array(
  8487. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8488. ));
  8489. $expected = array(
  8490. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8491. 'My label',
  8492. '/label',
  8493. 'input' => array(
  8494. 'type' => 'text', 'name' => 'data[Contact][name]',
  8495. 'id' => 'ContactName', 'maxlength' => '255'
  8496. )
  8497. );
  8498. $this->assertTags($result, $expected);
  8499. $result = $this->Form->input('Contact.name', array(
  8500. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8501. ));
  8502. $expected = array(
  8503. 'label' => array('for' => 'my_id'),
  8504. 'Name',
  8505. '/label',
  8506. 'input' => array(
  8507. 'type' => 'text', 'name' => 'data[Contact][name]',
  8508. 'id' => 'my_id', 'maxlength' => '255'
  8509. )
  8510. );
  8511. $this->assertTags($result, $expected);
  8512. $result = $this->Form->input('1.id');
  8513. $this->assertTags($result, array('input' => array(
  8514. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8515. 'id' => 'Contact1Id'
  8516. )));
  8517. $result = $this->Form->input("1.name");
  8518. $expected = array(
  8519. 'div' => array('class' => 'input text'),
  8520. 'label' => array('for' => 'Contact1Name'),
  8521. 'Name',
  8522. '/label',
  8523. 'input' => array(
  8524. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8525. 'id' => 'Contact1Name', 'maxlength' => '255'
  8526. ),
  8527. '/div'
  8528. );
  8529. $this->assertTags($result, $expected);
  8530. $result = $this->Form->input('Contact.1.id');
  8531. $this->assertTags($result, array(
  8532. 'input' => array(
  8533. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8534. 'id' => 'Contact1Id'
  8535. )
  8536. ));
  8537. $result = $this->Form->input("Model.1.name");
  8538. $expected = array(
  8539. 'div' => array('class' => 'input text'),
  8540. 'label' => array('for' => 'Model1Name'),
  8541. 'Name',
  8542. '/label',
  8543. 'input' => array(
  8544. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8545. 'id' => 'Model1Name'
  8546. ),
  8547. '/div'
  8548. );
  8549. $this->assertTags($result, $expected);
  8550. }
  8551. /**
  8552. * testFormEnd method
  8553. *
  8554. * @return void
  8555. */
  8556. public function testFormEnd() {
  8557. $this->assertEquals('</form>', $this->Form->end());
  8558. $result = $this->Form->end('', array('form' => 'form-name'));
  8559. $expected = array(
  8560. 'div' => array('class' => 'submit'),
  8561. 'input' => array('type' => 'submit', 'value' => ''),
  8562. '/div',
  8563. '/form'
  8564. );
  8565. $this->assertTags($result, $expected);
  8566. $result = $this->Form->end('');
  8567. $expected = array(
  8568. 'div' => array('class' => 'submit'),
  8569. 'input' => array('type' => 'submit', 'value' => ''),
  8570. '/div',
  8571. '/form'
  8572. );
  8573. $this->assertTags($result, $expected);
  8574. $result = $this->Form->end(array('label' => ''));
  8575. $expected = array(
  8576. 'div' => array('class' => 'submit'),
  8577. 'input' => array('type' => 'submit', 'value' => ''),
  8578. '/div',
  8579. '/form'
  8580. );
  8581. $this->assertTags($result, $expected);
  8582. $result = $this->Form->end('save');
  8583. $expected = array(
  8584. 'div' => array('class' => 'submit'),
  8585. 'input' => array('type' => 'submit', 'value' => 'save'),
  8586. '/div',
  8587. '/form'
  8588. );
  8589. $this->assertTags($result, $expected);
  8590. $result = $this->Form->end(array('label' => 'save'));
  8591. $expected = array(
  8592. 'div' => array('class' => 'submit'),
  8593. 'input' => array('type' => 'submit', 'value' => 'save'),
  8594. '/div',
  8595. '/form'
  8596. );
  8597. $this->assertTags($result, $expected);
  8598. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8599. $expected = array(
  8600. 'div' => array('class' => 'submit'),
  8601. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8602. '/div',
  8603. '/form'
  8604. );
  8605. $this->assertTags($result, $expected);
  8606. $result = $this->Form->end(array('name' => 'Whatever'));
  8607. $expected = array(
  8608. 'div' => array('class' => 'submit'),
  8609. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8610. '/div',
  8611. '/form'
  8612. );
  8613. $this->assertTags($result, $expected);
  8614. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8615. $expected = array(
  8616. 'div' => array('class' => 'good'),
  8617. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8618. '/div',
  8619. '/form'
  8620. );
  8621. $this->assertTags($result, $expected);
  8622. $result = $this->Form->end(array(
  8623. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8624. ));
  8625. $expected = array(
  8626. 'div' => array('class' => 'good'),
  8627. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8628. '/div',
  8629. '/form'
  8630. );
  8631. $this->assertTags($result, $expected);
  8632. }
  8633. /**
  8634. * testMultipleFormWithIdFields method
  8635. *
  8636. * @return void
  8637. */
  8638. public function testMultipleFormWithIdFields() {
  8639. $this->Form->create('UserForm');
  8640. $result = $this->Form->input('id');
  8641. $this->assertTags($result, array('input' => array(
  8642. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8643. )));
  8644. $result = $this->Form->input('ValidateItem.id');
  8645. $this->assertTags($result, array('input' => array(
  8646. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8647. 'id' => 'ValidateItemId'
  8648. )));
  8649. $result = $this->Form->input('ValidateUser.id');
  8650. $this->assertTags($result, array('input' => array(
  8651. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8652. 'id' => 'ValidateUserId'
  8653. )));
  8654. }
  8655. /**
  8656. * testDbLessModel method
  8657. *
  8658. * @return void
  8659. */
  8660. public function testDbLessModel() {
  8661. $this->Form->create('TestMail');
  8662. $result = $this->Form->input('name');
  8663. $expected = array(
  8664. 'div' => array('class' => 'input text'),
  8665. 'label' => array('for' => 'TestMailName'),
  8666. 'Name',
  8667. '/label',
  8668. 'input' => array(
  8669. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8670. 'id' => 'TestMailName'
  8671. ),
  8672. '/div'
  8673. );
  8674. $this->assertTags($result, $expected);
  8675. ClassRegistry::init('TestMail');
  8676. $this->Form->create('TestMail');
  8677. $result = $this->Form->input('name');
  8678. $expected = array(
  8679. 'div' => array('class' => 'input text'),
  8680. 'label' => array('for' => 'TestMailName'),
  8681. 'Name',
  8682. '/label',
  8683. 'input' => array(
  8684. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8685. 'id' => 'TestMailName'
  8686. ),
  8687. '/div'
  8688. );
  8689. $this->assertTags($result, $expected);
  8690. }
  8691. /**
  8692. * testBrokenness method
  8693. *
  8694. * @return void
  8695. */
  8696. public function testBrokenness() {
  8697. /*
  8698. * #4 This test has two parents and four children. By default (as of r7117) both
  8699. * parents are show but the first parent is missing a child. This is the inconsistency
  8700. * in the default behaviour - one parent has all children, the other does not - dependent
  8701. * on the data values.
  8702. */
  8703. $result = $this->Form->select('Model.field', array(
  8704. 'Fred' => array(
  8705. 'freds_son_1' => 'Fred',
  8706. 'freds_son_2' => 'Freddie'
  8707. ),
  8708. 'Bert' => array(
  8709. 'berts_son_1' => 'Albert',
  8710. 'berts_son_2' => 'Bertie')
  8711. ),
  8712. array('showParents' => true, 'empty' => false)
  8713. );
  8714. $expected = array(
  8715. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8716. array('optgroup' => array('label' => 'Fred')),
  8717. array('option' => array('value' => 'freds_son_1')),
  8718. 'Fred',
  8719. '/option',
  8720. array('option' => array('value' => 'freds_son_2')),
  8721. 'Freddie',
  8722. '/option',
  8723. '/optgroup',
  8724. array('optgroup' => array('label' => 'Bert')),
  8725. array('option' => array('value' => 'berts_son_1')),
  8726. 'Albert',
  8727. '/option',
  8728. array('option' => array('value' => 'berts_son_2')),
  8729. 'Bertie',
  8730. '/option',
  8731. '/optgroup',
  8732. '/select'
  8733. );
  8734. $this->assertTags($result, $expected);
  8735. /*
  8736. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8737. * changed, so we should expect the same select list data, just with a different name
  8738. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8739. * This is where data corruption can occur, because when a select value is missing from
  8740. * a list a form will substitute the first value in the list - without the user knowing.
  8741. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8742. * affect the availability of 3 => 'Three' as a valid option.
  8743. */
  8744. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8745. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8746. ));
  8747. $result = $this->Form->select(
  8748. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8749. );
  8750. $expected = array(
  8751. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8752. array('option' => array('value' => 1)),
  8753. 'One',
  8754. '/option',
  8755. array('option' => array('value' => 2)),
  8756. 'Two',
  8757. '/option',
  8758. array('optgroup' => array('label' => 'Three')),
  8759. array('option' => array('value' => 3)),
  8760. 'Three',
  8761. '/option',
  8762. array('option' => array('value' => 4)),
  8763. 'Four',
  8764. '/option',
  8765. array('option' => array('value' => 5)),
  8766. 'Five',
  8767. '/option',
  8768. '/optgroup',
  8769. '/select'
  8770. );
  8771. $this->assertTags($result, $expected);
  8772. }
  8773. /**
  8774. * Test the generation of fields for a multi record form.
  8775. *
  8776. * @return void
  8777. */
  8778. public function testMultiRecordForm() {
  8779. $this->Form->create('ValidateProfile');
  8780. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8781. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8782. $expected = array(
  8783. 'div' => array('class' => 'input textarea'),
  8784. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8785. 'Name',
  8786. '/label',
  8787. 'textarea' => array(
  8788. 'id' => 'ValidateProfile1ValidateItem2Name',
  8789. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8790. 'cols' => 30,
  8791. 'rows' => 6
  8792. ),
  8793. 'Value',
  8794. '/textarea',
  8795. '/div'
  8796. );
  8797. $this->assertTags($result, $expected);
  8798. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8799. $expected = array(
  8800. 'div' => array('class' => 'input date'),
  8801. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8802. 'Created',
  8803. '/label',
  8804. array('select' => array(
  8805. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8806. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8807. )
  8808. ),
  8809. array('option' => array('value' => '')), '/option',
  8810. $this->dateRegex['monthsRegex'],
  8811. '/select', '-',
  8812. array('select' => array(
  8813. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8814. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8815. )
  8816. ),
  8817. array('option' => array('value' => '')), '/option',
  8818. $this->dateRegex['daysRegex'],
  8819. '/select', '-',
  8820. array('select' => array(
  8821. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8822. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8823. )
  8824. ),
  8825. array('option' => array('value' => '')), '/option',
  8826. $this->dateRegex['yearsRegex'],
  8827. '/select',
  8828. '/div'
  8829. );
  8830. $this->assertTags($result, $expected);
  8831. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8832. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8833. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8834. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8835. $expected = array(
  8836. 'div' => array('class' => 'input select error'),
  8837. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8838. 'Profile',
  8839. '/label',
  8840. 'select' => array(
  8841. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8842. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8843. 'class' => 'form-error'
  8844. ),
  8845. '/select',
  8846. array('div' => array('class' => 'error-message')),
  8847. 'Error',
  8848. '/div',
  8849. '/div'
  8850. );
  8851. $this->assertTags($result, $expected);
  8852. }
  8853. /**
  8854. * test the correct display of multi-record form validation errors.
  8855. *
  8856. * @return void
  8857. */
  8858. public function testMultiRecordFormValidationErrors() {
  8859. $this->Form->create('ValidateProfile');
  8860. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8861. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8862. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8863. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8864. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8865. $result = $this->Form->error('ValidateProfile.2.city');
  8866. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8867. $result = $this->Form->error('2.city');
  8868. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8869. }
  8870. /**
  8871. * test the correct display of multi-record form validation errors.
  8872. *
  8873. * @return void
  8874. */
  8875. public function testSaveManyRecordFormValidationErrors() {
  8876. $this->Form->create('ValidateUser');
  8877. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8878. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8879. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8880. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8881. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8882. $result = $this->Form->error('ValidateUser.0.city');
  8883. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8884. }
  8885. /**
  8886. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8887. *
  8888. * @return void
  8889. */
  8890. public function testInputTemplate() {
  8891. $result = $this->Form->input('Contact.email', array(
  8892. 'type' => 'text', 'format' => array('input')
  8893. ));
  8894. $expected = array(
  8895. 'div' => array('class' => 'input text'),
  8896. 'input' => array(
  8897. 'type' => 'text', 'name' => 'data[Contact][email]',
  8898. 'id' => 'ContactEmail'
  8899. ),
  8900. '/div'
  8901. );
  8902. $this->assertTags($result, $expected);
  8903. $result = $this->Form->input('Contact.email', array(
  8904. 'type' => 'text', 'format' => array('input', 'label'),
  8905. 'label' => '<em>Email (required)</em>'
  8906. ));
  8907. $expected = array(
  8908. 'div' => array('class' => 'input text'),
  8909. array('input' => array(
  8910. 'type' => 'text', 'name' => 'data[Contact][email]',
  8911. 'id' => 'ContactEmail'
  8912. )),
  8913. 'label' => array('for' => 'ContactEmail'),
  8914. 'em' => array(),
  8915. 'Email (required)',
  8916. '/em',
  8917. '/label',
  8918. '/div'
  8919. );
  8920. $this->assertTags($result, $expected);
  8921. $result = $this->Form->input('Contact.email', array(
  8922. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8923. 'between' => '<div>Something in the middle</div>',
  8924. 'after' => '<span>Some text at the end</span>'
  8925. ));
  8926. $expected = array(
  8927. 'div' => array('class' => 'input text'),
  8928. array('input' => array(
  8929. 'type' => 'text', 'name' => 'data[Contact][email]',
  8930. 'id' => 'ContactEmail'
  8931. )),
  8932. array('div' => array()),
  8933. 'Something in the middle',
  8934. '/div',
  8935. 'label' => array('for' => 'ContactEmail'),
  8936. 'Email',
  8937. '/label',
  8938. 'span' => array(),
  8939. 'Some text at the end',
  8940. '/span',
  8941. '/div'
  8942. );
  8943. $this->assertTags($result, $expected);
  8944. $result = $this->Form->input('Contact.method', array(
  8945. 'type' => 'radio',
  8946. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8947. 'between' => 'I am between',
  8948. ));
  8949. $expected = array(
  8950. 'div' => array('class' => 'input radio'),
  8951. 'fieldset' => array(),
  8952. 'legend' => array(),
  8953. 'Method',
  8954. '/legend',
  8955. 'I am between',
  8956. 'input' => array(
  8957. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8958. 'value' => '', 'id' => 'ContactMethod_'
  8959. ),
  8960. array('input' => array(
  8961. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8962. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8963. )),
  8964. array('label' => array('for' => 'ContactMethodEmail')),
  8965. 'Email',
  8966. '/label',
  8967. array('input' => array(
  8968. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8969. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8970. )),
  8971. array('label' => array('for' => 'ContactMethodPigeon')),
  8972. 'Pigeon',
  8973. '/label',
  8974. '/fieldset',
  8975. '/div',
  8976. );
  8977. $this->assertTags($result, $expected);
  8978. }
  8979. /**
  8980. * test that some html5 inputs + FormHelper::__call() work
  8981. *
  8982. * @return void
  8983. */
  8984. public function testHtml5Inputs() {
  8985. $result = $this->Form->email('User.email');
  8986. $expected = array(
  8987. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8988. );
  8989. $this->assertTags($result, $expected);
  8990. $result = $this->Form->search('User.query');
  8991. $expected = array(
  8992. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8993. );
  8994. $this->assertTags($result, $expected);
  8995. $result = $this->Form->search('User.query', array('value' => 'test'));
  8996. $expected = array(
  8997. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8998. );
  8999. $this->assertTags($result, $expected);
  9000. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  9001. $expected = array(
  9002. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  9003. );
  9004. $this->assertTags($result, $expected);
  9005. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  9006. $expected = array(
  9007. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  9008. );
  9009. $this->assertTags($result, $expected);
  9010. }
  9011. /**
  9012. *
  9013. * @expectedException CakeException
  9014. * @return void
  9015. */
  9016. public function testHtml5InputException() {
  9017. $this->Form->email();
  9018. }
  9019. /**
  9020. * Tests that a model can be loaded from the model names passed in the request object
  9021. *
  9022. * @return void
  9023. */
  9024. public function testIntrospectModelFromRequest() {
  9025. $this->loadFixtures('Post');
  9026. App::build(array(
  9027. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  9028. ));
  9029. CakePlugin::load('TestPlugin');
  9030. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  9031. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  9032. $this->Form->create('TestPluginPost');
  9033. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  9034. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  9035. CakePlugin::unload();
  9036. App::build();
  9037. }
  9038. /**
  9039. * Tests that it is possible to set the validation errors directly in the helper for a field
  9040. *
  9041. * @return void
  9042. */
  9043. public function testCustomValidationErrors() {
  9044. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  9045. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  9046. $this->assertEquals('Badness!', $result);
  9047. }
  9048. /**
  9049. * Tests that the 'on' key validates as expected on create
  9050. *
  9051. * @return void
  9052. */
  9053. public function testRequiredOnCreate() {
  9054. $this->Form->create('Contact');
  9055. $result = $this->Form->input('Contact.imrequiredonupdate');
  9056. $expected = array(
  9057. 'div' => array('class' => 'input text'),
  9058. 'label' => array('for' => 'ContactImrequiredonupdate'),
  9059. 'Imrequiredonupdate',
  9060. '/label',
  9061. 'input' => array(
  9062. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9063. 'id' => 'ContactImrequiredonupdate'
  9064. ),
  9065. '/div'
  9066. );
  9067. $this->assertTags($result, $expected);
  9068. $result = $this->Form->input('Contact.imrequiredoncreate');
  9069. $expected = array(
  9070. 'div' => array('class' => 'input text required'),
  9071. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9072. 'Imrequiredoncreate',
  9073. '/label',
  9074. 'input' => array(
  9075. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9076. 'id' => 'ContactImrequiredoncreate',
  9077. 'required' => 'required'
  9078. ),
  9079. '/div'
  9080. );
  9081. $this->assertTags($result, $expected);
  9082. $result = $this->Form->input('Contact.imrequiredonboth');
  9083. $expected = array(
  9084. 'div' => array('class' => 'input text required'),
  9085. 'label' => array('for' => 'ContactImrequiredonboth'),
  9086. 'Imrequiredonboth',
  9087. '/label',
  9088. 'input' => array(
  9089. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9090. 'id' => 'ContactImrequiredonboth',
  9091. 'required' => 'required'
  9092. ),
  9093. '/div'
  9094. );
  9095. $this->assertTags($result, $expected);
  9096. $this->Form->inputDefaults(array('required' => false));
  9097. $result = $this->Form->input('Contact.imrequired');
  9098. $expected = array(
  9099. 'div' => array('class' => 'input text'),
  9100. 'label' => array('for' => 'ContactImrequired'),
  9101. 'Imrequired',
  9102. '/label',
  9103. 'input' => array(
  9104. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9105. 'id' => 'ContactImrequired'
  9106. ),
  9107. '/div'
  9108. );
  9109. $this->assertTags($result, $expected);
  9110. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  9111. $this->assertTags($result, $expected);
  9112. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  9113. $expected = array(
  9114. 'div' => array('class' => 'input text required'),
  9115. 'label' => array('for' => 'ContactImrequired'),
  9116. 'Imrequired',
  9117. '/label',
  9118. 'input' => array(
  9119. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9120. 'id' => 'ContactImrequired'
  9121. ),
  9122. '/div'
  9123. );
  9124. $this->assertTags($result, $expected);
  9125. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  9126. $this->assertTags($result, $expected);
  9127. }
  9128. /**
  9129. * Tests that the 'on' key validates as expected on update
  9130. *
  9131. * @return void
  9132. */
  9133. public function testRequiredOnUpdate() {
  9134. $this->Form->request->data['Contact']['id'] = 1;
  9135. $this->Form->create('Contact');
  9136. $result = $this->Form->input('Contact.imrequiredonupdate');
  9137. $expected = array(
  9138. 'div' => array('class' => 'input text required'),
  9139. 'label' => array('for' => 'ContactImrequiredonupdate'),
  9140. 'Imrequiredonupdate',
  9141. '/label',
  9142. 'input' => array(
  9143. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9144. 'id' => 'ContactImrequiredonupdate',
  9145. 'required' => 'required'
  9146. ),
  9147. '/div'
  9148. );
  9149. $this->assertTags($result, $expected);
  9150. $result = $this->Form->input('Contact.imrequiredoncreate');
  9151. $expected = array(
  9152. 'div' => array('class' => 'input text'),
  9153. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9154. 'Imrequiredoncreate',
  9155. '/label',
  9156. 'input' => array(
  9157. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9158. 'id' => 'ContactImrequiredoncreate'
  9159. ),
  9160. '/div'
  9161. );
  9162. $this->assertTags($result, $expected);
  9163. $result = $this->Form->input('Contact.imrequiredonboth');
  9164. $expected = array(
  9165. 'div' => array('class' => 'input text required'),
  9166. 'label' => array('for' => 'ContactImrequiredonboth'),
  9167. 'Imrequiredonboth',
  9168. '/label',
  9169. 'input' => array(
  9170. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9171. 'id' => 'ContactImrequiredonboth',
  9172. 'required' => 'required'
  9173. ),
  9174. '/div'
  9175. );
  9176. $this->assertTags($result, $expected);
  9177. $result = $this->Form->input('Contact.imrequired');
  9178. $expected = array(
  9179. 'div' => array('class' => 'input text required'),
  9180. 'label' => array('for' => 'ContactImrequired'),
  9181. 'Imrequired',
  9182. '/label',
  9183. 'input' => array(
  9184. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9185. 'id' => 'ContactImrequired',
  9186. 'required' => 'required'
  9187. ),
  9188. '/div'
  9189. );
  9190. $this->assertTags($result, $expected);
  9191. }
  9192. /**
  9193. * Test inputDefaults setter and getter
  9194. *
  9195. * @return void
  9196. */
  9197. public function testInputDefaults() {
  9198. $this->Form->create('Contact');
  9199. $this->Form->inputDefaults(array(
  9200. 'label' => false,
  9201. 'div' => array(
  9202. 'style' => 'color: #000;'
  9203. )
  9204. ));
  9205. $result = $this->Form->input('Contact.field1');
  9206. $expected = array(
  9207. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  9208. 'input' => array(
  9209. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9210. 'id' => 'ContactField1'
  9211. ),
  9212. '/div'
  9213. );
  9214. $this->assertTags($result, $expected);
  9215. $this->Form->inputDefaults(array(
  9216. 'div' => false,
  9217. 'label' => 'Label',
  9218. ));
  9219. $result = $this->Form->input('Contact.field1');
  9220. $expected = array(
  9221. 'label' => array('for' => 'ContactField1'),
  9222. 'Label',
  9223. '/label',
  9224. 'input' => array(
  9225. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9226. 'id' => 'ContactField1'
  9227. ),
  9228. );
  9229. $this->assertTags($result, $expected);
  9230. $this->Form->inputDefaults(array(
  9231. 'label' => false,
  9232. ), true);
  9233. $result = $this->Form->input('Contact.field1');
  9234. $expected = array(
  9235. 'input' => array(
  9236. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9237. 'id' => 'ContactField1'
  9238. ),
  9239. );
  9240. $this->assertTags($result, $expected);
  9241. $result = $this->Form->inputDefaults();
  9242. $expected = array(
  9243. 'div' => false,
  9244. 'label' => false,
  9245. );
  9246. $this->assertEquals($expected, $result);
  9247. }
  9248. }