FormHelperTest.php 296 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package Cake.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var boolean
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var boolean
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var boolean
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var boolean
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var boolean
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var boolean
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  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. $this->Form->request['_Token'] = array('key' => $key);
  613. $result = $this->Form->secure($fields);
  614. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  615. $hash .= ':' . 'Model.valid';
  616. $hash = urlencode($hash);
  617. $expected = array(
  618. 'div' => array('style' => 'display:none;'),
  619. array('input' => array(
  620. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  621. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  622. )),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  625. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  631. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  632. $result = $this->Form->secure($fields);
  633. $expected = array(
  634. 'div' => array('class' => 'hidden'),
  635. array('input' => array(
  636. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  637. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  638. )),
  639. array('input' => array(
  640. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  641. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  642. )),
  643. '/div'
  644. );
  645. $this->assertTags($result, $expected);
  646. }
  647. /**
  648. * Tests correct generation of number fields for double and float fields
  649. *
  650. * @return void
  651. */
  652. public function testTextFieldGenerationForFloats() {
  653. $model = ClassRegistry::getObject('Contact');
  654. $model->setSchema(array('foo' => array(
  655. 'type' => 'float',
  656. 'null' => false,
  657. 'default' => null,
  658. 'length' => 10
  659. )));
  660. $this->Form->create('Contact');
  661. $result = $this->Form->input('foo');
  662. $expected = array(
  663. 'div' => array('class' => 'input number'),
  664. 'label' => array('for' => 'ContactFoo'),
  665. 'Foo',
  666. '/label',
  667. array('input' => array(
  668. 'type' => 'number',
  669. 'name' => 'data[Contact][foo]',
  670. 'id' => 'ContactFoo',
  671. 'step' => 'any'
  672. )),
  673. '/div'
  674. );
  675. $this->assertTags($result, $expected);
  676. $result = $this->Form->input('foo', array('step' => 0.5));
  677. $expected = array(
  678. 'div' => array('class' => 'input number'),
  679. 'label' => array('for' => 'ContactFoo'),
  680. 'Foo',
  681. '/label',
  682. array('input' => array(
  683. 'type' => 'number',
  684. 'name' => 'data[Contact][foo]',
  685. 'id' => 'ContactFoo',
  686. 'step' => '0.5'
  687. )),
  688. '/div'
  689. );
  690. $this->assertTags($result, $expected);
  691. }
  692. /**
  693. * Tests correct generation of number fields for integer fields
  694. *
  695. * @return void
  696. */
  697. public function testTextFieldTypeNumberGenerationForIntegers() {
  698. $model = ClassRegistry::getObject('Contact');
  699. $model->setSchema(array('foo' => array(
  700. 'type' => 'integer',
  701. 'null' => false,
  702. 'default' => null,
  703. 'length' => null
  704. )));
  705. $this->Form->create('Contact');
  706. $result = $this->Form->input('foo');
  707. $expected = array(
  708. 'div' => array('class' => 'input number'),
  709. 'label' => array('for' => 'ContactFoo'),
  710. 'Foo',
  711. '/label',
  712. array('input' => array(
  713. 'type' => 'number', 'name' => 'data[Contact][foo]',
  714. 'id' => 'ContactFoo'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * testFormSecurityMultipleFields method
  722. *
  723. * Test secure() with multiple row form. Ensure hash is correct.
  724. *
  725. * @return void
  726. */
  727. public function testFormSecurityMultipleFields() {
  728. $key = 'testKey';
  729. $fields = array(
  730. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  731. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  732. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  733. );
  734. $this->Form->request['_Token'] = array('key' => $key);
  735. $result = $this->Form->secure($fields);
  736. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  737. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  738. $expected = array(
  739. 'div' => array('style' => 'display:none;'),
  740. array('input' => array(
  741. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  742. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  743. )),
  744. array('input' => array(
  745. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  746. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  747. )),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. }
  752. /**
  753. * testFormSecurityMultipleSubmitButtons
  754. *
  755. * test form submit generation and ensure that _Token is only created on end()
  756. *
  757. * @return void
  758. */
  759. public function testFormSecurityMultipleSubmitButtons() {
  760. $key = 'testKey';
  761. $this->Form->request['_Token'] = array('key' => $key);
  762. $this->Form->create('Addresses');
  763. $this->Form->input('Address.title');
  764. $this->Form->input('Address.first_name');
  765. $result = $this->Form->submit('Save', array('name' => 'save'));
  766. $expected = array(
  767. 'div' => array('class' => 'submit'),
  768. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  769. '/div',
  770. );
  771. $this->assertTags($result, $expected);
  772. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  773. $expected = array(
  774. 'div' => array('class' => 'submit'),
  775. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  776. '/div',
  777. );
  778. $this->assertTags($result, $expected);
  779. $result = $this->Form->end(null);
  780. $expected = array(
  781. 'div' => array('style' => 'display:none;'),
  782. array('input' => array(
  783. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  784. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  785. )),
  786. array('input' => array(
  787. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  788. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  789. )),
  790. '/div'
  791. );
  792. $this->assertTags($result, $expected);
  793. }
  794. /**
  795. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  796. *
  797. * @return void
  798. */
  799. public function testSecurityButtonNestedNamed() {
  800. $key = 'testKey';
  801. $this->Form->request['_Token'] = array('key' => $key);
  802. $this->Form->create('Addresses');
  803. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  804. $result = $this->Form->unlockField();
  805. $this->assertEquals(array('Address.button'), $result);
  806. }
  807. /**
  808. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  809. *
  810. * @return void
  811. */
  812. public function testSecuritySubmitNestedNamed() {
  813. $key = 'testKey';
  814. $this->Form->request['_Token'] = array('key' => $key);
  815. $this->Form->create('Addresses');
  816. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  817. $result = $this->Form->unlockField();
  818. $this->assertEquals(array('Address.button'), $result);
  819. }
  820. /**
  821. * Test that the correct fields are unlocked for image submits with no names.
  822. *
  823. * @return void
  824. */
  825. public function testSecuritySubmitImageNoName() {
  826. $key = 'testKey';
  827. $this->Form->request['_Token'] = array('key' => $key);
  828. $this->Form->create('User');
  829. $result = $this->Form->submit('save.png');
  830. $expected = array(
  831. 'div' => array('class' => 'submit'),
  832. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  833. '/div'
  834. );
  835. $this->assertTags($result, $expected);
  836. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  837. }
  838. /**
  839. * Test that the correct fields are unlocked for image submits with names.
  840. *
  841. * @return void
  842. */
  843. public function testSecuritySubmitImageName() {
  844. $key = 'testKey';
  845. $this->Form->request['_Token'] = array('key' => $key);
  846. $this->Form->create('User');
  847. $result = $this->Form->submit('save.png', array('name' => 'test'));
  848. $expected = array(
  849. 'div' => array('class' => 'submit'),
  850. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  855. }
  856. /**
  857. * testFormSecurityMultipleInputFields method
  858. *
  859. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  860. *
  861. * @return void
  862. */
  863. public function testFormSecurityMultipleInputFields() {
  864. $key = 'testKey';
  865. $this->Form->request['_Token'] = array('key' => $key);
  866. $this->Form->create('Addresses');
  867. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  868. $this->Form->input('Addresses.0.title');
  869. $this->Form->input('Addresses.0.first_name');
  870. $this->Form->input('Addresses.0.last_name');
  871. $this->Form->input('Addresses.0.address');
  872. $this->Form->input('Addresses.0.city');
  873. $this->Form->input('Addresses.0.phone');
  874. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  875. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  876. $this->Form->input('Addresses.1.title');
  877. $this->Form->input('Addresses.1.first_name');
  878. $this->Form->input('Addresses.1.last_name');
  879. $this->Form->input('Addresses.1.address');
  880. $this->Form->input('Addresses.1.city');
  881. $this->Form->input('Addresses.1.phone');
  882. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  883. $result = $this->Form->secure($this->Form->fields);
  884. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  885. $expected = array(
  886. 'div' => array('style' => 'display:none;'),
  887. array('input' => array(
  888. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  889. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  890. )),
  891. array('input' => array(
  892. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  893. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  894. )),
  895. '/div'
  896. );
  897. $this->assertTags($result, $expected);
  898. }
  899. /**
  900. * Test form security with Model.field.0 style inputs
  901. *
  902. * @return void
  903. */
  904. public function testFormSecurityArrayFields() {
  905. $key = 'testKey';
  906. $this->Form->request->params['_Token']['key'] = $key;
  907. $this->Form->create('Address');
  908. $this->Form->input('Address.primary.1');
  909. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  910. $this->Form->input('Address.secondary.1.0');
  911. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  912. }
  913. /**
  914. * testFormSecurityMultipleInputDisabledFields method
  915. *
  916. * test secure form generation with multiple records and disabled fields.
  917. *
  918. * @return void
  919. */
  920. public function testFormSecurityMultipleInputDisabledFields() {
  921. $key = 'testKey';
  922. $this->Form->request->params['_Token'] = array(
  923. 'key' => $key,
  924. 'unlockedFields' => array('first_name', 'address')
  925. );
  926. $this->Form->create();
  927. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  928. $this->Form->input('Addresses.0.title');
  929. $this->Form->input('Addresses.0.first_name');
  930. $this->Form->input('Addresses.0.last_name');
  931. $this->Form->input('Addresses.0.address');
  932. $this->Form->input('Addresses.0.city');
  933. $this->Form->input('Addresses.0.phone');
  934. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  935. $this->Form->input('Addresses.1.title');
  936. $this->Form->input('Addresses.1.first_name');
  937. $this->Form->input('Addresses.1.last_name');
  938. $this->Form->input('Addresses.1.address');
  939. $this->Form->input('Addresses.1.city');
  940. $this->Form->input('Addresses.1.phone');
  941. $result = $this->Form->secure($this->Form->fields);
  942. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  943. $expected = array(
  944. 'div' => array('style' => 'display:none;'),
  945. array('input' => array(
  946. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  947. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  948. )),
  949. array('input' => array(
  950. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  951. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  952. )),
  953. '/div'
  954. );
  955. $this->assertTags($result, $expected);
  956. }
  957. /**
  958. * testFormSecurityInputDisabledFields method
  959. *
  960. * Test single record form with disabled fields.
  961. *
  962. * @return void
  963. */
  964. public function testFormSecurityInputUnlockedFields() {
  965. $key = 'testKey';
  966. $this->Form->request['_Token'] = array(
  967. 'key' => $key,
  968. 'unlockedFields' => array('first_name', 'address')
  969. );
  970. $this->Form->create();
  971. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  972. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  973. $this->Form->input('Addresses.title');
  974. $this->Form->input('Addresses.first_name');
  975. $this->Form->input('Addresses.last_name');
  976. $this->Form->input('Addresses.address');
  977. $this->Form->input('Addresses.city');
  978. $this->Form->input('Addresses.phone');
  979. $result = $this->Form->fields;
  980. $expected = array(
  981. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  982. 'Addresses.city', 'Addresses.phone'
  983. );
  984. $this->assertEquals($expected, $result);
  985. $result = $this->Form->secure($expected);
  986. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  987. $expected = array(
  988. 'div' => array('style' => 'display:none;'),
  989. array('input' => array(
  990. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  991. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  992. )),
  993. array('input' => array(
  994. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  995. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  996. )),
  997. '/div'
  998. );
  999. $this->assertTags($result, $expected);
  1000. }
  1001. /**
  1002. * test securing inputs with custom name attributes.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testFormSecureWithCustomNameAttribute() {
  1007. $this->Form->request->params['_Token']['key'] = 'testKey';
  1008. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1009. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1010. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1011. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1012. }
  1013. /**
  1014. * testFormSecuredInput method
  1015. *
  1016. * Test generation of entire secure form, assertions made on input() output.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testFormSecuredInput() {
  1021. $this->Form->request['_Token'] = array('key' => 'testKey');
  1022. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1023. $encoding = strtolower(Configure::read('App.encoding'));
  1024. $expected = array(
  1025. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1026. 'div' => array('style' => 'display:none;'),
  1027. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1028. array('input' => array(
  1029. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1030. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertTags($result, $expected);
  1035. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1036. $expected = array(
  1037. 'div' => array('class' => 'input text'),
  1038. 'label' => array('for' => 'UserFormPublished'),
  1039. 'Published',
  1040. '/label',
  1041. array('input' => array(
  1042. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1043. 'id' => 'UserFormPublished'
  1044. )),
  1045. '/div'
  1046. );
  1047. $this->assertTags($result, $expected);
  1048. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1049. $expected = array(
  1050. 'div' => array('class' => 'input text'),
  1051. 'label' => array('for' => 'UserFormOther'),
  1052. 'Other',
  1053. '/label',
  1054. array('input' => array(
  1055. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1056. 'id' => 'UserFormOther'
  1057. )),
  1058. '/div'
  1059. );
  1060. $this->assertTags($result, $expected);
  1061. $result = $this->Form->hidden('UserForm.stuff');
  1062. $expected = array(
  1063. 'input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1065. 'id' => 'UserFormStuff'
  1066. ));
  1067. $this->assertTags($result, $expected);
  1068. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1069. $expected = array('input' => array(
  1070. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1071. 'value' => '0', 'id' => 'UserFormHidden'
  1072. ));
  1073. $this->assertTags($result, $expected);
  1074. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1075. $expected = array(
  1076. 'div' => array('class' => 'input checkbox'),
  1077. array('input' => array(
  1078. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1079. 'value' => '0', 'id' => 'UserFormSomething_'
  1080. )),
  1081. array('input' => array(
  1082. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1083. 'value' => '1', 'id' => 'UserFormSomething'
  1084. )),
  1085. 'label' => array('for' => 'UserFormSomething'),
  1086. 'Something',
  1087. '/label',
  1088. '/div'
  1089. );
  1090. $this->assertTags($result, $expected);
  1091. $result = $this->Form->fields;
  1092. $expected = array(
  1093. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1094. 'UserForm.hidden' => '0', 'UserForm.something'
  1095. );
  1096. $this->assertEquals($expected, $result);
  1097. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1098. $result = $this->Form->secure($this->Form->fields);
  1099. $expected = array(
  1100. 'div' => array('style' => 'display:none;'),
  1101. array('input' => array(
  1102. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1103. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1104. )),
  1105. array('input' => array(
  1106. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1107. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1108. )),
  1109. '/div'
  1110. );
  1111. $this->assertTags($result, $expected);
  1112. }
  1113. /**
  1114. * Test secured inputs with custom names.
  1115. *
  1116. * @return void
  1117. */
  1118. public function testSecuredInputCustomName() {
  1119. $this->Form->request['_Token'] = array('key' => 'testKey');
  1120. $this->assertEquals(array(), $this->Form->fields);
  1121. $this->Form->input('text_input', array(
  1122. 'name' => 'data[Option][General.default_role]',
  1123. ));
  1124. $expected = array('Option.General.default_role');
  1125. $this->assertEquals($expected, $this->Form->fields);
  1126. $this->Form->input('select_box', array(
  1127. 'name' => 'data[Option][General.select_role]',
  1128. 'type' => 'select',
  1129. 'options' => array(1, 2),
  1130. ));
  1131. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1132. $this->assertEquals($expected, $this->Form->fields);
  1133. }
  1134. /**
  1135. * Tests that the correct keys are added to the field hash index
  1136. *
  1137. * @return void
  1138. */
  1139. public function testFormSecuredFileInput() {
  1140. $this->Form->request['_Token'] = array('key' => 'testKey');
  1141. $this->assertEquals(array(), $this->Form->fields);
  1142. $this->Form->file('Attachment.file');
  1143. $expected = array(
  1144. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1145. 'Attachment.file.error', 'Attachment.file.size'
  1146. );
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * test that multiple selects keys are added to field hash
  1151. *
  1152. * @return void
  1153. */
  1154. public function testFormSecuredMultipleSelect() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $options = array('1' => 'one', '2' => 'two');
  1158. $this->Form->select('Model.select', $options);
  1159. $expected = array('Model.select');
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. $this->Form->fields = array();
  1162. $this->Form->select('Model.select', $options, array('multiple' => true));
  1163. $this->assertEquals($expected, $this->Form->fields);
  1164. }
  1165. /**
  1166. * testFormSecuredRadio method
  1167. *
  1168. * @return void
  1169. */
  1170. public function testFormSecuredRadio() {
  1171. $this->Form->request['_Token'] = array('key' => 'testKey');
  1172. $this->assertEquals(array(), $this->Form->fields);
  1173. $options = array('1' => 'option1', '2' => 'option2');
  1174. $this->Form->radio('Test.test', $options);
  1175. $expected = array('Test.test');
  1176. $this->assertEquals($expected, $this->Form->fields);
  1177. }
  1178. /**
  1179. * Test that when disabled is in a list based attribute array it works.
  1180. *
  1181. * @return void
  1182. */
  1183. public function testFormSecuredAndDisabledNotAssoc() {
  1184. $this->Form->request['_Token'] = array('key' => 'testKey');
  1185. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1186. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1187. $this->Form->text('Model.text', array('disabled'));
  1188. $this->Form->textarea('Model.textarea', array('disabled'));
  1189. $this->Form->password('Model.password', array('disabled'));
  1190. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1191. $expected = array(
  1192. 'Model.radio' => ''
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1198. * hashing.
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredAndDisabled() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1205. $this->Form->text('Model.text', array('disabled' => true));
  1206. $this->Form->password('Model.text', array('disabled' => true));
  1207. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1208. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1209. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1210. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1211. $this->Form->month('Model.month', array('disabled' => true));
  1212. $this->Form->day('Model.day', array('disabled' => true));
  1213. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1214. $this->Form->minute('Model.minute', array('disabled' => true));
  1215. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1216. $expected = array(
  1217. 'Model.radio' => ''
  1218. );
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testDisableSecurityUsingForm method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testDisableSecurityUsingForm() {
  1227. $this->Form->request['_Token'] = array(
  1228. 'key' => 'testKey',
  1229. 'disabledFields' => array()
  1230. );
  1231. $this->Form->create();
  1232. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1233. $this->Form->input('Addresses.title');
  1234. $this->Form->input('Addresses.first_name', array('secure' => false));
  1235. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1236. $this->Form->input('Addresses.zip', array(
  1237. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1238. ));
  1239. $result = $this->Form->fields;
  1240. $expected = array(
  1241. 'Addresses.id' => '123456', 'Addresses.title',
  1242. );
  1243. $this->assertEquals($expected, $result);
  1244. }
  1245. /**
  1246. * test disableField
  1247. *
  1248. * @return void
  1249. */
  1250. public function testUnlockFieldAddsToList() {
  1251. $this->Form->request['_Token'] = array(
  1252. 'key' => 'testKey',
  1253. 'unlockedFields' => array()
  1254. );
  1255. $this->Form->create('Contact');
  1256. $this->Form->unlockField('Contact.name');
  1257. $this->Form->text('Contact.name');
  1258. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1259. $this->assertEquals(array(), $this->Form->fields);
  1260. }
  1261. /**
  1262. * test unlockField removing from fields array.
  1263. *
  1264. * @return void
  1265. */
  1266. public function testUnlockFieldRemovingFromFields() {
  1267. $this->Form->request['_Token'] = array(
  1268. 'key' => 'testKey',
  1269. 'unlockedFields' => array()
  1270. );
  1271. $this->Form->create('Contact');
  1272. $this->Form->hidden('Contact.id', array('value' => 1));
  1273. $this->Form->text('Contact.name');
  1274. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1275. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1276. $this->Form->unlockField('Contact.name');
  1277. $this->Form->unlockField('Contact.id');
  1278. $this->assertEquals(array(), $this->Form->fields);
  1279. }
  1280. /**
  1281. * testTagIsInvalid method
  1282. *
  1283. * @return void
  1284. */
  1285. public function testTagIsInvalid() {
  1286. $Contact = ClassRegistry::getObject('Contact');
  1287. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1288. $this->Form->setEntity('Contact.0.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertEquals($expected, $result);
  1291. $this->Form->setEntity('Contact.1.email');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. $this->Form->setEntity('Contact.0.name');
  1295. $result = $this->Form->tagIsInvalid();
  1296. $this->assertFalse($result);
  1297. }
  1298. /**
  1299. * Test tagIsInvalid with validation errors from a saveMany
  1300. *
  1301. * @return void
  1302. */
  1303. public function testTagIsInvalidSaveMany() {
  1304. $Contact = ClassRegistry::getObject('Contact');
  1305. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1306. $this->Form->create('Contact');
  1307. $this->Form->setEntity('0.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. $this->Form->setEntity('0.Contact.email');
  1311. $result = $this->Form->tagIsInvalid();
  1312. $this->assertEquals($expected, $result);
  1313. }
  1314. /**
  1315. * Test validation errors.
  1316. *
  1317. * @return void
  1318. */
  1319. public function testPasswordValidation() {
  1320. $Contact = ClassRegistry::getObject('Contact');
  1321. $Contact->validationErrors['password'] = array('Please provide a password');
  1322. $result = $this->Form->input('Contact.password');
  1323. $expected = array(
  1324. 'div' => array('class' => 'input password error'),
  1325. 'label' => array('for' => 'ContactPassword'),
  1326. 'Password',
  1327. '/label',
  1328. 'input' => array(
  1329. 'type' => 'password', 'name' => 'data[Contact][password]',
  1330. 'id' => 'ContactPassword', 'class' => 'form-error'
  1331. ),
  1332. array('div' => array('class' => 'error-message')),
  1333. 'Please provide a password',
  1334. '/div',
  1335. '/div'
  1336. );
  1337. $this->assertTags($result, $expected);
  1338. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1339. $expected = array(
  1340. 'div' => array('class' => 'input password error'),
  1341. 'label' => array('for' => 'ContactPassword'),
  1342. 'Password',
  1343. '/label',
  1344. 'input' => array(
  1345. 'type' => 'password', 'name' => 'data[Contact][password]',
  1346. 'id' => 'ContactPassword', 'class' => 'form-error'
  1347. ),
  1348. '/div'
  1349. );
  1350. $this->assertTags($result, $expected);
  1351. }
  1352. /**
  1353. * Test validation errors, when validation message is an empty string.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testEmptyErrorValidation() {
  1358. $this->Form->validationErrors['Contact']['password'] = '';
  1359. $result = $this->Form->input('Contact.password');
  1360. $expected = array(
  1361. 'div' => array('class' => 'input password error'),
  1362. 'label' => array('for' => 'ContactPassword'),
  1363. 'Password',
  1364. '/label',
  1365. 'input' => array(
  1366. 'type' => 'password', 'name' => 'data[Contact][password]',
  1367. 'id' => 'ContactPassword', 'class' => 'form-error'
  1368. ),
  1369. array('div' => array('class' => 'error-message')),
  1370. array(),
  1371. '/div',
  1372. '/div'
  1373. );
  1374. $this->assertTags($result, $expected);
  1375. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1376. $expected = array(
  1377. 'div' => array('class' => 'input password error'),
  1378. 'label' => array('for' => 'ContactPassword'),
  1379. 'Password',
  1380. '/label',
  1381. 'input' => array(
  1382. 'type' => 'password', 'name' => 'data[Contact][password]',
  1383. 'id' => 'ContactPassword', 'class' => 'form-error'
  1384. ),
  1385. '/div'
  1386. );
  1387. $this->assertTags($result, $expected);
  1388. }
  1389. /**
  1390. * Test validation errors, when calling input() overriding validation message by an empty string.
  1391. *
  1392. * @return void
  1393. */
  1394. public function testEmptyInputErrorValidation() {
  1395. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1396. $result = $this->Form->input('Contact.password', array('error' => ''));
  1397. $expected = array(
  1398. 'div' => array('class' => 'input password error'),
  1399. 'label' => array('for' => 'ContactPassword'),
  1400. 'Password',
  1401. '/label',
  1402. 'input' => array(
  1403. 'type' => 'password', 'name' => 'data[Contact][password]',
  1404. 'id' => 'ContactPassword', 'class' => 'form-error'
  1405. ),
  1406. array('div' => array('class' => 'error-message')),
  1407. array(),
  1408. '/div',
  1409. '/div'
  1410. );
  1411. $this->assertTags($result, $expected);
  1412. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1413. $expected = array(
  1414. 'div' => array('class' => 'input password error'),
  1415. 'label' => array('for' => 'ContactPassword'),
  1416. 'Password',
  1417. '/label',
  1418. 'input' => array(
  1419. 'type' => 'password', 'name' => 'data[Contact][password]',
  1420. 'id' => 'ContactPassword', 'class' => 'form-error'
  1421. ),
  1422. '/div'
  1423. );
  1424. $this->assertTags($result, $expected);
  1425. }
  1426. /**
  1427. * testFormValidationAssociated method
  1428. *
  1429. * test display of form errors in conjunction with model::validates.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testFormValidationAssociated() {
  1434. $this->UserForm = ClassRegistry::getObject('UserForm');
  1435. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1436. $data = array(
  1437. 'UserForm' => array('name' => 'user'),
  1438. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1439. );
  1440. $result = $this->UserForm->OpenidUrl->create($data);
  1441. $this->assertFalse(empty($result));
  1442. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1443. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1444. $encoding = strtolower(Configure::read('App.encoding'));
  1445. $expected = array(
  1446. 'form' => array(
  1447. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1448. 'accept-charset' => $encoding
  1449. ),
  1450. 'div' => array('style' => 'display:none;'),
  1451. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1452. '/div'
  1453. );
  1454. $this->assertTags($result, $expected);
  1455. $result = $this->Form->error(
  1456. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1457. );
  1458. $this->assertEquals('Error, not registered', $result);
  1459. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1460. }
  1461. /**
  1462. * testFormValidationAssociatedFirstLevel method
  1463. *
  1464. * test form error display with associated model.
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormValidationAssociatedFirstLevel() {
  1469. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1470. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1471. $data = array(
  1472. 'ValidateUser' => array('name' => 'mariano'),
  1473. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1474. );
  1475. $result = $this->ValidateUser->create($data);
  1476. $this->assertFalse(empty($result));
  1477. $this->assertFalse($this->ValidateUser->validates());
  1478. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1479. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1480. $encoding = strtolower(Configure::read('App.encoding'));
  1481. $expected = array(
  1482. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1483. 'div' => array('style' => 'display:none;'),
  1484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1485. '/div'
  1486. );
  1487. $this->assertTags($result, $expected);
  1488. $result = $this->Form->error(
  1489. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1490. );
  1491. $this->assertEquals('Invalid email', $result);
  1492. $result = $this->Form->error(
  1493. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1494. );
  1495. $this->assertEquals('Invalid name', $result);
  1496. $result = $this->Form->error(
  1497. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1498. );
  1499. $this->assertEquals('Invalid city', $result);
  1500. unset($this->ValidateUser->ValidateProfile);
  1501. unset($this->ValidateUser);
  1502. }
  1503. /**
  1504. * testFormValidationAssociatedSecondLevel method
  1505. *
  1506. * test form error display with associated model.
  1507. *
  1508. * @return void
  1509. */
  1510. public function testFormValidationAssociatedSecondLevel() {
  1511. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1512. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1513. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1514. $data = array(
  1515. 'ValidateUser' => array('name' => 'mariano'),
  1516. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1517. 'ValidateItem' => array('name' => 'Item')
  1518. );
  1519. $result = $this->ValidateUser->create($data);
  1520. $this->assertFalse(empty($result));
  1521. $this->assertFalse($this->ValidateUser->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1523. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1524. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1525. $encoding = strtolower(Configure::read('App.encoding'));
  1526. $expected = array(
  1527. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1528. 'div' => array('style' => 'display:none;'),
  1529. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1530. '/div'
  1531. );
  1532. $this->assertTags($result, $expected);
  1533. $result = $this->Form->error(
  1534. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1535. );
  1536. $this->assertEquals('Invalid email', $result);
  1537. $result = $this->Form->error(
  1538. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1539. );
  1540. $this->assertEquals('Invalid name', $result);
  1541. $result = $this->Form->error(
  1542. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1543. );
  1544. $result = $this->Form->error(
  1545. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid description', $result);
  1548. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1549. unset($this->ValidateUser->ValidateProfile);
  1550. unset($this->ValidateUser);
  1551. }
  1552. /**
  1553. * testFormValidationMultiRecord method
  1554. *
  1555. * test form error display with multiple records.
  1556. *
  1557. * @return void
  1558. */
  1559. public function testFormValidationMultiRecord() {
  1560. $Contact = ClassRegistry::getObject('Contact');
  1561. $Contact->validationErrors[2] = array(
  1562. 'name' => array('This field cannot be left blank')
  1563. );
  1564. $result = $this->Form->input('Contact.2.name');
  1565. $expected = array(
  1566. 'div' => array('class' => 'input text error'),
  1567. 'label' => array('for' => 'Contact2Name'),
  1568. 'Name',
  1569. '/label',
  1570. 'input' => array(
  1571. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1572. 'class' => 'form-error', 'maxlength' => 255
  1573. ),
  1574. array('div' => array('class' => 'error-message')),
  1575. 'This field cannot be left blank',
  1576. '/div',
  1577. '/div'
  1578. );
  1579. $this->assertTags($result, $expected);
  1580. }
  1581. /**
  1582. * testMultipleInputValidation method
  1583. *
  1584. * test multiple record form validation error display.
  1585. *
  1586. * @return void
  1587. */
  1588. public function testMultipleInputValidation() {
  1589. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1590. $Address->validationErrors[0] = array(
  1591. 'title' => array('This field cannot be empty'),
  1592. 'first_name' => array('This field cannot be empty')
  1593. );
  1594. $Address->validationErrors[1] = array(
  1595. 'last_name' => array('You must have a last name')
  1596. );
  1597. $this->Form->create();
  1598. $result = $this->Form->input('Address.0.title');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array(
  1605. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1606. ),
  1607. array('div' => array('class' => 'error-message')),
  1608. 'This field cannot be empty',
  1609. '/div',
  1610. '/div'
  1611. );
  1612. $this->assertTags($result, $expected);
  1613. $result = $this->Form->input('Address.0.first_name');
  1614. $expected = array(
  1615. 'div' => array('class'),
  1616. 'label' => array('for'),
  1617. 'preg:/[^<]+/',
  1618. '/label',
  1619. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1620. array('div' => array('class' => 'error-message')),
  1621. 'This field cannot be empty',
  1622. '/div',
  1623. '/div'
  1624. );
  1625. $this->assertTags($result, $expected);
  1626. $result = $this->Form->input('Address.0.last_name');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array('type' => 'text', 'name', 'id'),
  1633. '/div'
  1634. );
  1635. $this->assertTags($result, $expected);
  1636. $result = $this->Form->input('Address.1.last_name');
  1637. $expected = array(
  1638. 'div' => array('class'),
  1639. 'label' => array('for'),
  1640. 'preg:/[^<]+/',
  1641. '/label',
  1642. 'input' => array(
  1643. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1644. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1645. ),
  1646. array('div' => array('class' => 'error-message')),
  1647. 'You must have a last name',
  1648. '/div',
  1649. '/div'
  1650. );
  1651. $this->assertTags($result, $expected);
  1652. }
  1653. /**
  1654. * testInput method
  1655. *
  1656. * Test various incarnations of input().
  1657. *
  1658. * @return void
  1659. */
  1660. public function testInput() {
  1661. $result = $this->Form->input('ValidateUser.balance');
  1662. $expected = array(
  1663. 'div' => array('class'),
  1664. 'label' => array('for'),
  1665. 'Balance',
  1666. '/label',
  1667. 'input' => array('name', 'type' => 'number', 'id'),
  1668. '/div',
  1669. );
  1670. $this->assertTags($result, $expected);
  1671. $result = $this->Form->input('ValidateUser.cost_decimal');
  1672. $expected = array(
  1673. 'div' => array('class'),
  1674. 'label' => array('for'),
  1675. 'Cost Decimal',
  1676. '/label',
  1677. 'input' => array('name', 'type' => 'number', 'id'),
  1678. '/div',
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1682. $expected = array(
  1683. 'div' => array('class' => 'input email'),
  1684. 'label' => array('for' => 'custom'),
  1685. 'Email',
  1686. '/label',
  1687. array('input' => array(
  1688. 'type' => 'email', 'name' => 'data[Contact][email]',
  1689. 'id' => 'custom', 'maxlength' => 255
  1690. )),
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1695. $expected = array(
  1696. '<div',
  1697. 'label' => array('for' => 'ContactEmail'),
  1698. 'Email',
  1699. '/label',
  1700. array('input' => array(
  1701. 'type' => 'email', 'name' => 'data[Contact][email]',
  1702. 'id' => 'ContactEmail', 'maxlength' => 255
  1703. )),
  1704. '/div'
  1705. );
  1706. $this->assertTags($result, $expected);
  1707. $result = $this->Form->hidden('Contact.idontexist');
  1708. $expected = array('input' => array(
  1709. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1710. 'id' => 'ContactIdontexist'
  1711. ));
  1712. $this->assertTags($result, $expected);
  1713. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input text'),
  1716. 'label' => array('for' => 'ContactEmail'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'text', 'name' => 'data[Contact][email]',
  1721. 'id' => 'ContactEmail'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1727. $expected = array(
  1728. 'div' => array('class' => 'input text'),
  1729. 'label' => array('for' => 'Contact5Email'),
  1730. 'Email',
  1731. '/label',
  1732. array('input' => array(
  1733. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1734. 'id' => 'Contact5Email'
  1735. )),
  1736. '/div'
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.password');
  1740. $expected = array(
  1741. 'div' => array('class' => 'input password'),
  1742. 'label' => array('for' => 'ContactPassword'),
  1743. 'Password',
  1744. '/label',
  1745. array('input' => array(
  1746. 'type' => 'password', 'name' => 'data[Contact][password]',
  1747. 'id' => 'ContactPassword'
  1748. )),
  1749. '/div'
  1750. );
  1751. $this->assertTags($result, $expected);
  1752. $result = $this->Form->input('Contact.email', array(
  1753. 'type' => 'file', 'class' => 'textbox'
  1754. ));
  1755. $expected = array(
  1756. 'div' => array('class' => 'input file'),
  1757. 'label' => array('for' => 'ContactEmail'),
  1758. 'Email',
  1759. '/label',
  1760. array('input' => array(
  1761. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1762. 'id' => 'ContactEmail'
  1763. )),
  1764. '/div'
  1765. );
  1766. $this->assertTags($result, $expected);
  1767. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1768. $result = $this->Form->input('Contact.phone');
  1769. $expected = array(
  1770. 'div' => array('class' => 'input tel'),
  1771. 'label' => array('for' => 'ContactPhone'),
  1772. 'Phone',
  1773. '/label',
  1774. array('input' => array(
  1775. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1776. 'value' => 'Hello &amp; World &gt; weird chars',
  1777. 'id' => 'ContactPhone', 'maxlength' => 255
  1778. )),
  1779. '/div'
  1780. );
  1781. $this->assertTags($result, $expected);
  1782. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1783. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1784. $expected = array(
  1785. 'div' => array('class' => 'input text'),
  1786. 'label' => array('for' => 'myId'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1791. 'value' => 'My value', 'id' => 'myId'
  1792. ),
  1793. '/div'
  1794. );
  1795. $this->assertTags($result, $expected);
  1796. unset($this->Form->request->data);
  1797. $Contact = ClassRegistry::getObject('Contact');
  1798. $Contact->validationErrors['field'] = array('Badness!');
  1799. $result = $this->Form->input('Contact.field');
  1800. $expected = array(
  1801. 'div' => array('class' => 'input text error'),
  1802. 'label' => array('for' => 'ContactField'),
  1803. 'Field',
  1804. '/label',
  1805. 'input' => array(
  1806. 'type' => 'text', 'name' => 'data[Contact][field]',
  1807. 'id' => 'ContactField', 'class' => 'form-error'
  1808. ),
  1809. array('div' => array('class' => 'error-message')),
  1810. 'Badness!',
  1811. '/div',
  1812. '/div'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1817. ));
  1818. $expected = array(
  1819. 'label' => array('for' => 'ContactField'),
  1820. 'Field',
  1821. '/label',
  1822. 'input' => array(
  1823. 'type' => 'text', 'name' => 'data[Contact][field]',
  1824. 'id' => 'ContactField', 'class' => 'form-error'
  1825. ),
  1826. array('span' => array('class' => 'error-message')),
  1827. 'Badness!',
  1828. '/span'
  1829. );
  1830. $this->assertTags($result, $expected);
  1831. $result = $this->Form->input('Contact.field', array(
  1832. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1833. ));
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text error'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array(
  1840. 'type' => 'text', 'name' => 'data[Contact][field]',
  1841. 'id' => 'ContactField', 'class' => 'form-error'
  1842. ),
  1843. array('div' => array('class' => 'error')),
  1844. 'Badness!',
  1845. '/div'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $result = $this->Form->input('Contact.field', array(
  1849. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1850. ));
  1851. $expected = array(
  1852. 'span' => array('class' => 'input text error'),
  1853. 'label' => array('for' => 'ContactField'),
  1854. 'Field',
  1855. '/label',
  1856. 'input' => array(
  1857. 'type' => 'text', 'name' => 'data[Contact][field]',
  1858. 'id' => 'ContactField', 'class' => 'form-error'
  1859. ),
  1860. 'Badness!',
  1861. '/span'
  1862. );
  1863. $this->assertTags($result, $expected);
  1864. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1865. $expected = array(
  1866. 'div' => array('class' => 'input text error'),
  1867. 'label' => array('for' => 'ContactField'),
  1868. 'Field',
  1869. '/label',
  1870. 'input' => array(
  1871. 'type' => 'text', 'name' => 'data[Contact][field]',
  1872. 'id' => 'ContactField', 'class' => 'form-error'
  1873. ),
  1874. 'A message to you, Rudy',
  1875. array('div' => array('class' => 'error-message')),
  1876. 'Badness!',
  1877. '/div',
  1878. '/div'
  1879. );
  1880. $this->assertTags($result, $expected);
  1881. $this->Form->setEntity(null);
  1882. $this->Form->setEntity('Contact.field');
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'after' => 'A message to you, Rudy', 'error' => false
  1885. ));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ContactField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1897. $expected = array(
  1898. 'div' => array('class' => 'input text'),
  1899. 'label' => array('for' => 'ObjectField'),
  1900. 'Field',
  1901. '/label',
  1902. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1903. 'A message to you, Rudy',
  1904. '/div'
  1905. );
  1906. $this->assertTags($result, $expected);
  1907. $Contact->validationErrors['field'] = array('minLength');
  1908. $result = $this->Form->input('Contact.field', array(
  1909. 'error' => array(
  1910. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1911. 'maxLength' => 'login too large'
  1912. )
  1913. ));
  1914. $expected = array(
  1915. 'div' => array('class' => 'input text error'),
  1916. 'label' => array('for' => 'ContactField'),
  1917. 'Field',
  1918. '/label',
  1919. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1920. array('div' => array('class' => 'error-message')),
  1921. 'Le login doit contenir au moins 2 caractères',
  1922. '/div',
  1923. '/div'
  1924. );
  1925. $this->assertTags($result, $expected);
  1926. $Contact->validationErrors['field'] = array('maxLength');
  1927. $result = $this->Form->input('Contact.field', array(
  1928. 'error' => array(
  1929. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1930. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1931. 'maxLength' => 'login too large',
  1932. )
  1933. ));
  1934. $expected = array(
  1935. 'div' => array('class' => 'input text error'),
  1936. 'label' => array('for' => 'ContactField'),
  1937. 'Field',
  1938. '/label',
  1939. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1940. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1941. 'login too large',
  1942. '/span',
  1943. '/div'
  1944. );
  1945. $this->assertTags($result, $expected);
  1946. }
  1947. /**
  1948. * Test that inputs with 0 can be created.
  1949. *
  1950. * @return void
  1951. */
  1952. public function testInputZero() {
  1953. $this->Form->create('User');
  1954. $result = $this->Form->input('0');
  1955. $expected = array(
  1956. 'div' => array('class' => 'input text'),
  1957. 'label' => array('for' => 'User0'), '/label',
  1958. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1959. '/div'
  1960. );
  1961. $this->assertTags($result, $expected);
  1962. }
  1963. /**
  1964. * test input() with checkbox creation
  1965. *
  1966. * @return void
  1967. */
  1968. public function testInputCheckbox() {
  1969. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1970. $expected = array(
  1971. 'div' => array('class' => 'input checkbox'),
  1972. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1973. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1974. '/div'
  1975. );
  1976. $this->assertTags($result, $expected);
  1977. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1978. $expected = array(
  1979. 'div' => array('class' => 'input checkbox'),
  1980. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1981. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1982. '/div'
  1983. );
  1984. $this->assertTags($result, $expected);
  1985. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1986. $expected = array(
  1987. 'div' => array('class' => 'input checkbox'),
  1988. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1989. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1990. '/div'
  1991. );
  1992. $this->assertTags($result, $expected);
  1993. $result = $this->Form->input('User.disabled', array(
  1994. 'label' => 'Disabled',
  1995. 'type' => 'checkbox',
  1996. 'data-foo' => 'disabled'
  1997. ));
  1998. $expected = array(
  1999. 'div' => array('class' => 'input checkbox'),
  2000. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2001. array('input' => array(
  2002. 'type' => 'checkbox',
  2003. 'name' => 'data[User][disabled]',
  2004. 'value' => '1',
  2005. 'id' => 'UserDisabled',
  2006. 'data-foo' => 'disabled'
  2007. )),
  2008. 'label' => array('for' => 'UserDisabled'),
  2009. 'Disabled',
  2010. '/label',
  2011. '/div'
  2012. );
  2013. $this->assertTags($result, $expected);
  2014. }
  2015. /**
  2016. * test form->input() with time types.
  2017. *
  2018. */
  2019. public function testInputTime() {
  2020. extract($this->dateRegex);
  2021. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2022. $result = explode(':', $result);
  2023. $this->assertRegExp('/option value="23"/', $result[0]);
  2024. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2025. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2026. $result = explode(':', $result);
  2027. $this->assertRegExp('/option value="23"/', $result[0]);
  2028. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2029. $result = $this->Form->input('Model.field', array(
  2030. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2031. ));
  2032. $result = explode(':', $result);
  2033. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2034. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2035. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2036. $result = $this->Form->input('Model.field', array(
  2037. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2038. ));
  2039. $result = explode(':', $result);
  2040. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2041. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2042. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2043. $result = $this->Form->input('prueba', array(
  2044. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2045. 'maxYear' => date('Y') + 1, 'interval' => 15
  2046. ));
  2047. $result = explode(':', $result);
  2048. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2049. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2050. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2051. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2052. $result = $this->Form->input('Random.start_time', array(
  2053. 'type' => 'time',
  2054. 'selected' => '18:15'
  2055. ));
  2056. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2057. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2058. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2059. $result = $this->Form->input('published', array('type' => 'time'));
  2060. $now = strtotime('now');
  2061. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2062. $now = strtotime('2013-03-09 00:42:21');
  2063. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2064. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2065. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2066. }
  2067. /**
  2068. * Test interval + selected near the hour roll over.
  2069. *
  2070. * @return void
  2071. */
  2072. public function testTimeSelectedWithInterval() {
  2073. $result = $this->Form->input('Model.start_time', array(
  2074. 'type' => 'time',
  2075. 'interval' => 15,
  2076. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2077. ));
  2078. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2079. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2080. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2081. $result = $this->Form->input('Model.start_time', array(
  2082. 'type' => 'time',
  2083. 'interval' => 15,
  2084. 'selected' => '2012-10-23 15:57:00'
  2085. ));
  2086. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2087. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2088. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2089. $result = $this->Form->input('Model.start_time', array(
  2090. 'timeFormat' => 24,
  2091. 'type' => 'time',
  2092. 'interval' => 15,
  2093. 'selected' => '15:57'
  2094. ));
  2095. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2096. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2097. $result = $this->Form->input('Model.start_time', array(
  2098. 'timeFormat' => 24,
  2099. 'type' => 'time',
  2100. 'interval' => 15,
  2101. 'selected' => '23:57'
  2102. ));
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. $result = $this->Form->input('Model.created', array(
  2106. 'timeFormat' => 24,
  2107. 'type' => 'datetime',
  2108. 'interval' => 15,
  2109. 'selected' => '2012-09-30 23:56'
  2110. ));
  2111. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2112. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2113. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2114. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2115. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2116. }
  2117. /**
  2118. * Test interval & timeFormat = 12
  2119. *
  2120. * @return void
  2121. */
  2122. public function testInputTimeWithIntervalAnd12HourFormat() {
  2123. $result = $this->Form->input('Model.start_time', array(
  2124. 'type' => 'time',
  2125. 'timeFormat' => 12,
  2126. 'interval' => 5,
  2127. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2128. ));
  2129. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2130. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2131. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2132. $result = $this->Form->input('Model.start_time', array(
  2133. 'type' => 'time',
  2134. 'timeFormat' => '12',
  2135. 'interval' => 5,
  2136. 'selected' => '2013-04-19 16:30:00'
  2137. ));
  2138. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2139. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2140. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2141. $result = $this->Form->input('Model.start_time', array(
  2142. 'type' => 'time',
  2143. 'timeFormat' => '12',
  2144. 'interval' => 10,
  2145. 'selected' => '2013-05-19 00:33:00'
  2146. ));
  2147. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2148. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2149. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2150. $result = $this->Form->input('Model.start_time', array(
  2151. 'type' => 'time',
  2152. 'timeFormat' => '12',
  2153. 'interval' => 10,
  2154. 'selected' => '2013-05-19 13:33:00'
  2155. ));
  2156. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2157. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2158. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2159. $result = $this->Form->input('Model.start_time', array(
  2160. 'type' => 'time',
  2161. 'timeFormat' => '12',
  2162. 'interval' => 10,
  2163. 'selected' => '2013-05-19 01:33:00'
  2164. ));
  2165. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2166. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2167. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2168. }
  2169. /**
  2170. * test form->input() with datetime, date and time types
  2171. *
  2172. * @return void
  2173. */
  2174. public function testInputDatetime() {
  2175. extract($this->dateRegex);
  2176. $result = $this->Form->input('prueba', array(
  2177. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2178. 'maxYear' => date('Y') + 1, 'interval' => 15
  2179. ));
  2180. $result = explode(':', $result);
  2181. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2182. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2183. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2184. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2185. //related to ticket #5013
  2186. $result = $this->Form->input('Contact.date', array(
  2187. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2188. ));
  2189. $this->assertRegExp('/class="customClass"/', $result);
  2190. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2191. $result = $this->Form->input('Contact.date', array(
  2192. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2193. ));
  2194. $this->assertRegExp('/id="customIdDay"/', $result);
  2195. $this->assertRegExp('/id="customIdMonth"/', $result);
  2196. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2197. $result = $this->Form->input('Model.field', array(
  2198. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2199. ));
  2200. $this->assertRegExp('/id="customIDDay"/', $result);
  2201. $this->assertRegExp('/id="customIDHour"/', $result);
  2202. $result = explode('</select><select', $result);
  2203. $result = explode(':', $result[1]);
  2204. $this->assertRegExp('/option value="23"/', $result[0]);
  2205. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2206. $result = $this->Form->input('Model.field', array(
  2207. 'type' => 'datetime', 'timeFormat' => 12
  2208. ));
  2209. $result = explode('</select><select', $result);
  2210. $result = explode(':', $result[1]);
  2211. $this->assertRegExp('/option value="12"/', $result[0]);
  2212. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2213. $this->Form->request->data = array('Contact' => array('created' => null));
  2214. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2215. $expected = array(
  2216. 'div' => array('class' => 'input date'),
  2217. 'label' => array('for' => 'ContactCreatedMonth'),
  2218. 'Created',
  2219. '/label',
  2220. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2221. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2222. $monthsRegex,
  2223. '/select', '-',
  2224. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2225. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2226. $daysRegex,
  2227. '/select', '-',
  2228. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2229. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2230. $yearsRegex,
  2231. '/select',
  2232. '/div'
  2233. );
  2234. $this->assertTags($result, $expected);
  2235. $this->Form->request->data = array('Contact' => array('created' => null));
  2236. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2237. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2238. $this->Form->request->data = array('Contact' => array('created' => null));
  2239. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2240. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2241. $result = $this->Form->input('Contact.created', array(
  2242. 'type' => 'date',
  2243. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2244. 'timeFormat' => 'NONE'
  2245. ));
  2246. $this->assertRegExp('/for\="created-month"/', $result);
  2247. }
  2248. /**
  2249. * Test generating checkboxes in a loop.
  2250. *
  2251. * @return void
  2252. */
  2253. public function testInputCheckboxesInLoop() {
  2254. for ($i = 1; $i < 5; $i++) {
  2255. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2256. $expected = array(
  2257. 'div' => array('class' => 'input checkbox'),
  2258. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2259. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2260. 'label' => array('for' => "Contact{$i}Email"),
  2261. 'Email',
  2262. '/label',
  2263. '/div'
  2264. );
  2265. $this->assertTags($result, $expected);
  2266. }
  2267. }
  2268. /**
  2269. * Test generating checkboxes with disabled elements.
  2270. *
  2271. * @return void
  2272. */
  2273. public function testInputCheckboxWithDisabledElements() {
  2274. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2275. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2276. $expected = array(
  2277. array('div' => array('class' => 'input select')),
  2278. array('label' => array('for' => "ContactMultiple")),
  2279. 'Multiple',
  2280. '/label',
  2281. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2282. array('div' => array('class' => 'checkbox')),
  2283. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2284. array('label' => array('for' => "ContactMultiple1")),
  2285. 'One',
  2286. '/label',
  2287. '/div',
  2288. array('div' => array('class' => 'checkbox')),
  2289. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2290. array('label' => array('for' => "ContactMultiple2")),
  2291. 'Two',
  2292. '/label',
  2293. '/div',
  2294. array('div' => array('class' => 'checkbox')),
  2295. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2296. array('label' => array('for' => "ContactMultiple3")),
  2297. 'Three',
  2298. '/label',
  2299. '/div',
  2300. '/div'
  2301. );
  2302. $this->assertTags($result, $expected);
  2303. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2304. $this->assertTags($result, $expected);
  2305. $disabled = array('2', 3);
  2306. $expected = array(
  2307. array('div' => array('class' => 'input select')),
  2308. array('label' => array('for' => "ContactMultiple")),
  2309. 'Multiple',
  2310. '/label',
  2311. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2312. array('div' => array('class' => 'checkbox')),
  2313. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2314. array('label' => array('for' => "ContactMultiple1")),
  2315. 'One',
  2316. '/label',
  2317. '/div',
  2318. array('div' => array('class' => 'checkbox')),
  2319. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2320. array('label' => array('for' => "ContactMultiple2")),
  2321. 'Two',
  2322. '/label',
  2323. '/div',
  2324. array('div' => array('class' => 'checkbox')),
  2325. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2326. array('label' => array('for' => "ContactMultiple3")),
  2327. 'Three',
  2328. '/label',
  2329. '/div',
  2330. '/div'
  2331. );
  2332. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2333. $this->assertTags($result, $expected);
  2334. // make sure 50 does only disable 50, and not 50f5c0cf
  2335. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2336. $disabled = array(50);
  2337. $expected = array(
  2338. array('div' => array('class' => 'input select')),
  2339. array('label' => array('for' => "ContactMultiple")),
  2340. 'Multiple',
  2341. '/label',
  2342. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2343. array('div' => array('class' => 'checkbox')),
  2344. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2345. array('label' => array('for' => "ContactMultiple50")),
  2346. 'Fifty',
  2347. '/label',
  2348. '/div',
  2349. array('div' => array('class' => 'checkbox')),
  2350. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2351. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2352. 'Stringy',
  2353. '/label',
  2354. '/div',
  2355. '/div'
  2356. );
  2357. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2358. $this->assertTags($result, $expected);
  2359. }
  2360. /**
  2361. * test input name with leading integer, ensure attributes are generated correctly.
  2362. *
  2363. * @return void
  2364. */
  2365. public function testInputWithLeadingInteger() {
  2366. $result = $this->Form->text('0.Node.title');
  2367. $expected = array(
  2368. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2369. );
  2370. $this->assertTags($result, $expected);
  2371. }
  2372. /**
  2373. * test form->input() with select type inputs.
  2374. *
  2375. * @return void
  2376. */
  2377. public function testInputSelectType() {
  2378. $result = $this->Form->input('email', array(
  2379. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2380. );
  2381. $expected = array(
  2382. 'div' => array('class' => 'input select'),
  2383. 'label' => array('for' => 'email'),
  2384. 'Email',
  2385. '/label',
  2386. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2387. array('option' => array('value' => '')),
  2388. '/option',
  2389. array('option' => array('value' => 'è')),
  2390. 'Firést',
  2391. '/option',
  2392. array('option' => array('value' => 'é')),
  2393. 'Secoènd',
  2394. '/option',
  2395. '/select',
  2396. '/div'
  2397. );
  2398. $this->assertTags($result, $expected);
  2399. $result = $this->Form->input('email', array(
  2400. 'options' => array('First', 'Second'), 'empty' => true)
  2401. );
  2402. $expected = array(
  2403. 'div' => array('class' => 'input select'),
  2404. 'label' => array('for' => 'email'),
  2405. 'Email',
  2406. '/label',
  2407. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2408. array('option' => array('value' => '')),
  2409. '/option',
  2410. array('option' => array('value' => '0')),
  2411. 'First',
  2412. '/option',
  2413. array('option' => array('value' => '1')),
  2414. 'Second',
  2415. '/option',
  2416. '/select',
  2417. '/div'
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2421. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2422. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2423. $expected = array(
  2424. 'div' => array('class' => 'input select'),
  2425. 'label' => array('for' => 'ModelUserId'),
  2426. 'User',
  2427. '/label',
  2428. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2429. array('option' => array('value' => '')),
  2430. '/option',
  2431. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2432. 'good',
  2433. '/option',
  2434. array('option' => array('value' => 'other')),
  2435. 'bad',
  2436. '/option',
  2437. '/select',
  2438. '/div'
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2442. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2443. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2444. $expected = array(
  2445. 'div' => array('class' => 'input select'),
  2446. 'label' => array('for' => 'ThingUserId'),
  2447. 'User',
  2448. '/label',
  2449. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2450. array('option' => array('value' => '')),
  2451. 'Some Empty',
  2452. '/option',
  2453. array('option' => array('value' => 'value')),
  2454. 'good',
  2455. '/option',
  2456. array('option' => array('value' => 'other')),
  2457. 'bad',
  2458. '/option',
  2459. '/select',
  2460. '/div'
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2464. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2465. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2466. $expected = array(
  2467. 'div' => array('class' => 'input select'),
  2468. 'label' => array('for' => 'ThingUserId'),
  2469. 'User',
  2470. '/label',
  2471. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2472. array('option' => array('value' => '')),
  2473. 'Some Empty',
  2474. '/option',
  2475. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2476. 'good',
  2477. '/option',
  2478. array('option' => array('value' => 'other')),
  2479. 'bad',
  2480. '/option',
  2481. '/select',
  2482. '/div'
  2483. );
  2484. $this->assertTags($result, $expected);
  2485. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2486. $this->Form->request->data = array('User' => array('User' => array('value')));
  2487. $result = $this->Form->input('User.User', array('empty' => true));
  2488. $expected = array(
  2489. 'div' => array('class' => 'input select'),
  2490. 'label' => array('for' => 'UserUser'),
  2491. 'User',
  2492. '/label',
  2493. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2494. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2495. array('option' => array('value' => '')),
  2496. '/option',
  2497. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2498. 'good',
  2499. '/option',
  2500. array('option' => array('value' => 'other')),
  2501. 'bad',
  2502. '/option',
  2503. '/select',
  2504. '/div'
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $this->Form->data = array();
  2508. $result = $this->Form->input('Publisher.id', array(
  2509. 'label' => 'Publisher',
  2510. 'type' => 'select',
  2511. 'multiple' => 'checkbox',
  2512. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2513. ));
  2514. $expected = array(
  2515. array('div' => array('class' => 'input select')),
  2516. array('label' => array('for' => 'PublisherId')),
  2517. 'Publisher',
  2518. '/label',
  2519. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2520. array('div' => array('class' => 'checkbox')),
  2521. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2522. array('label' => array('for' => 'PublisherIdValue1')),
  2523. 'Label 1',
  2524. '/label',
  2525. '/div',
  2526. array('div' => array('class' => 'checkbox')),
  2527. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2528. array('label' => array('for' => 'PublisherIdValue2')),
  2529. 'Label 2',
  2530. '/label',
  2531. '/div',
  2532. '/div'
  2533. );
  2534. $this->assertTags($result, $expected);
  2535. }
  2536. /**
  2537. * test that input() and a non standard primary key makes a hidden input by default.
  2538. *
  2539. * @return void
  2540. */
  2541. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2542. $this->Form->create('User');
  2543. $this->Form->fieldset = array(
  2544. 'User' => array(
  2545. 'fields' => array(
  2546. 'model_id' => array('type' => 'integer')
  2547. ),
  2548. 'validates' => array(),
  2549. 'key' => 'model_id'
  2550. )
  2551. );
  2552. $result = $this->Form->input('model_id');
  2553. $expected = array(
  2554. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2555. );
  2556. $this->assertTags($result, $expected);
  2557. }
  2558. /**
  2559. * test that overriding the magic select type widget is possible
  2560. *
  2561. * @return void
  2562. */
  2563. public function testInputOverridingMagicSelectType() {
  2564. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2565. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2566. $expected = array(
  2567. 'div' => array('class' => 'input text'),
  2568. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2569. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2570. '/div'
  2571. );
  2572. $this->assertTags($result, $expected);
  2573. //Check that magic types still work for plural/singular vars
  2574. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2575. $result = $this->Form->input('Model.type');
  2576. $expected = array(
  2577. 'div' => array('class' => 'input select'),
  2578. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2579. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2580. array('option' => array('value' => 'value')), 'good', '/option',
  2581. array('option' => array('value' => 'other')), 'bad', '/option',
  2582. '/select',
  2583. '/div'
  2584. );
  2585. $this->assertTags($result, $expected);
  2586. }
  2587. /**
  2588. * Test that magic input() selects are created for type=number
  2589. *
  2590. * @return void
  2591. */
  2592. public function testInputMagicSelectForTypeNumber() {
  2593. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2594. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2595. $result = $this->Form->input('ValidateUser.balance');
  2596. $expected = array(
  2597. 'div' => array('class' => 'input select'),
  2598. 'label' => array('for' => 'ValidateUserBalance'),
  2599. 'Balance',
  2600. '/label',
  2601. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2602. array('option' => array('value' => '0')),
  2603. 'nothing',
  2604. '/option',
  2605. array('option' => array('value' => '1', 'selected' => 'selected')),
  2606. 'some',
  2607. '/option',
  2608. array('option' => array('value' => '100')),
  2609. 'a lot',
  2610. '/option',
  2611. '/select',
  2612. '/div'
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. }
  2616. /**
  2617. * Test that magic input() selects can easily be converted into radio types without error.
  2618. *
  2619. * @return void
  2620. */
  2621. public function testInputMagicSelectChangeToRadio() {
  2622. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2623. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2624. $this->assertRegExp('/input type="radio"/', $result);
  2625. }
  2626. /**
  2627. * fields with the same name as the model should work.
  2628. *
  2629. * @return void
  2630. */
  2631. public function testInputWithMatchingFieldAndModelName() {
  2632. $this->Form->create('User');
  2633. $this->Form->fieldset = array(
  2634. 'User' => array(
  2635. 'fields' => array(
  2636. 'User' => array('type' => 'text')
  2637. ),
  2638. 'validates' => array(),
  2639. 'key' => 'id'
  2640. )
  2641. );
  2642. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2643. $result = $this->Form->input('User', array('type' => 'text'));
  2644. $expected = array(
  2645. 'div' => array('class' => 'input text'),
  2646. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2647. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2648. '/div'
  2649. );
  2650. $this->assertTags($result, $expected);
  2651. }
  2652. /**
  2653. * testFormInputs method
  2654. *
  2655. * test correct results from form::inputs().
  2656. *
  2657. * @return void
  2658. */
  2659. public function testFormInputs() {
  2660. $this->Form->create('Contact');
  2661. $result = $this->Form->inputs('The Legend');
  2662. $expected = array(
  2663. '<fieldset',
  2664. '<legend',
  2665. 'The Legend',
  2666. '/legend',
  2667. '*/fieldset',
  2668. );
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2671. $expected = array(
  2672. 'fieldset' => array('class' => 'classy-stuff'),
  2673. '<legend',
  2674. 'Field of Dreams',
  2675. '/legend',
  2676. '*/fieldset'
  2677. );
  2678. $this->assertTags($result, $expected);
  2679. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2680. $this->assertTags($result, $expected);
  2681. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2682. $this->assertTags($result, $expected);
  2683. $this->Form->create('Contact');
  2684. $this->Form->request['prefix'] = 'admin';
  2685. $this->Form->request['action'] = 'admin_edit';
  2686. $result = $this->Form->inputs();
  2687. $expected = array(
  2688. '<fieldset',
  2689. '<legend',
  2690. 'Edit Contact',
  2691. '/legend',
  2692. '*/fieldset',
  2693. );
  2694. $this->assertTags($result, $expected);
  2695. $this->Form->create('Contact');
  2696. $result = $this->Form->inputs(false);
  2697. $expected = array(
  2698. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2699. array('div' => array('class' => 'input text')),
  2700. '*/div',
  2701. array('div' => array('class' => 'input email')),
  2702. '*/div',
  2703. array('div' => array('class' => 'input tel')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input password')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input date')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input date')),
  2710. '*/div',
  2711. array('div' => array('class' => 'input datetime')),
  2712. '*/div',
  2713. array('div' => array('class' => 'input number')),
  2714. '*/div',
  2715. array('div' => array('class' => 'input select')),
  2716. '*/div',
  2717. );
  2718. $this->assertTags($result, $expected);
  2719. $this->Form->create('Contact');
  2720. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2721. $expected = array(
  2722. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2723. array('div' => array('class' => 'input text')),
  2724. '*/div',
  2725. array('div' => array('class' => 'input email')),
  2726. '*/div',
  2727. array('div' => array('class' => 'input tel')),
  2728. '*/div',
  2729. array('div' => array('class' => 'input password')),
  2730. '*/div',
  2731. array('div' => array('class' => 'input date')),
  2732. '*/div',
  2733. array('div' => array('class' => 'input date')),
  2734. '*/div',
  2735. array('div' => array('class' => 'input datetime')),
  2736. '*/div',
  2737. array('div' => array('class' => 'input number')),
  2738. '*/div',
  2739. array('div' => array('class' => 'input select')),
  2740. '*/div',
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. $this->Form->create('Contact');
  2744. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2745. $this->assertTags($result, $expected);
  2746. $this->Form->create('Contact');
  2747. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2748. $expected = array(
  2749. 'fieldset' => array(),
  2750. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2751. array('div' => array('class' => 'input text')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input email')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input tel')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input password')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input date')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input date')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input datetime')),
  2764. '*/div',
  2765. array('div' => array('class' => 'input number')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input select')),
  2768. '*/div',
  2769. '/fieldset'
  2770. );
  2771. $this->assertTags($result, $expected);
  2772. $this->Form->create('Contact');
  2773. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2774. $expected = array(
  2775. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2776. array('div' => array('class' => 'input text')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input email')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input tel')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input password')),
  2783. '*/div',
  2784. array('div' => array('class' => 'input date')),
  2785. '*/div',
  2786. array('div' => array('class' => 'input date')),
  2787. '*/div',
  2788. array('div' => array('class' => 'input datetime')),
  2789. '*/div',
  2790. array('div' => array('class' => 'input number')),
  2791. '*/div',
  2792. array('div' => array('class' => 'input select')),
  2793. '*/div',
  2794. );
  2795. $this->assertTags($result, $expected);
  2796. $this->Form->create('Contact');
  2797. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2798. $this->assertTags($result, $expected);
  2799. $this->Form->create('Contact');
  2800. $result = $this->Form->inputs('Hello');
  2801. $expected = array(
  2802. 'fieldset' => array(),
  2803. 'legend' => array(),
  2804. 'Hello',
  2805. '/legend',
  2806. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2807. array('div' => array('class' => 'input text')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input email')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input tel')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input password')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input date')),
  2816. '*/div',
  2817. array('div' => array('class' => 'input date')),
  2818. '*/div',
  2819. array('div' => array('class' => 'input datetime')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input number')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input select')),
  2824. '*/div',
  2825. '/fieldset'
  2826. );
  2827. $this->assertTags($result, $expected);
  2828. $this->Form->create('Contact');
  2829. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2830. $expected = array(
  2831. 'fieldset' => array(),
  2832. 'legend' => array(),
  2833. 'Hello',
  2834. '/legend',
  2835. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2836. array('div' => array('class' => 'input text')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input email')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input tel')),
  2841. '*/div',
  2842. array('div' => array('class' => 'input password')),
  2843. '*/div',
  2844. array('div' => array('class' => 'input date')),
  2845. '*/div',
  2846. array('div' => array('class' => 'input date')),
  2847. '*/div',
  2848. array('div' => array('class' => 'input datetime')),
  2849. '*/div',
  2850. array('div' => array('class' => 'input number')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input select')),
  2853. '*/div',
  2854. '/fieldset'
  2855. );
  2856. $this->assertTags($result, $expected);
  2857. $this->Form->create('Contact');
  2858. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2859. $this->assertTags($result, $expected);
  2860. $this->Form->end();
  2861. $this->Form->create(false);
  2862. $expected = array(
  2863. 'fieldset' => array(),
  2864. array('div' => array('class' => 'input text')),
  2865. 'label' => array('for' => 'foo'),
  2866. 'Foo',
  2867. '/label',
  2868. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2869. '*/div',
  2870. '/fieldset'
  2871. );
  2872. $result = $this->Form->inputs(
  2873. array('foo' => array('type' => 'text')),
  2874. array(),
  2875. array('legend' => false)
  2876. );
  2877. $this->assertTags($result, $expected);
  2878. }
  2879. /**
  2880. * Tests inputs() works with plugin models
  2881. *
  2882. * @return void
  2883. */
  2884. public function testInputsPluginModel() {
  2885. $this->loadFixtures('Post');
  2886. App::build(array(
  2887. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2888. ));
  2889. CakePlugin::load('TestPlugin');
  2890. $this->Form->request['models'] = array(
  2891. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2892. );
  2893. $this->Form->create('TestPlugin.TestPluginPost');
  2894. $result = $this->Form->inputs();
  2895. $this->assertContains('data[TestPluginPost][id]', $result);
  2896. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2897. $this->assertContains('data[TestPluginPost][title]', $result);
  2898. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2899. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2900. $this->assertEquals('TestPluginPost', $this->Form->model());
  2901. }
  2902. /**
  2903. * testSelectAsCheckbox method
  2904. *
  2905. * test multi-select widget with checkbox formatting.
  2906. *
  2907. * @return void
  2908. */
  2909. public function testSelectAsCheckbox() {
  2910. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2911. $expected = array(
  2912. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2913. array('div' => array('class' => 'checkbox')),
  2914. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2915. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2916. 'first',
  2917. '/label',
  2918. '/div',
  2919. array('div' => array('class' => 'checkbox')),
  2920. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2921. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2922. 'second',
  2923. '/label',
  2924. '/div',
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2927. array('label' => array('for' => 'ModelMultiField2')),
  2928. 'third',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2934. $expected = array(
  2935. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2936. array('div' => array('class' => 'checkbox')),
  2937. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2938. array('label' => array('for' => 'ModelMultiField12')),
  2939. 'half',
  2940. '/label',
  2941. '/div',
  2942. );
  2943. $this->assertTags($result, $expected);
  2944. }
  2945. /**
  2946. * testLabel method
  2947. *
  2948. * test label generation.
  2949. *
  2950. * @return void
  2951. */
  2952. public function testLabel() {
  2953. $this->Form->text('Person.name');
  2954. $result = $this->Form->label();
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2956. $this->Form->text('Person.name');
  2957. $result = $this->Form->label();
  2958. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2959. $result = $this->Form->label('Person.first_name');
  2960. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2961. $result = $this->Form->label('Person.first_name', 'Your first name');
  2962. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2963. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2964. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2965. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2966. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2967. $result = $this->Form->label('Person.first_name', '');
  2968. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2969. $result = $this->Form->label('Person.2.name', '');
  2970. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2971. }
  2972. /**
  2973. * testTextbox method
  2974. *
  2975. * test textbox element generation
  2976. *
  2977. * @return void
  2978. */
  2979. public function testTextbox() {
  2980. $result = $this->Form->text('Model.field');
  2981. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2982. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2983. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2984. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2985. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2986. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2987. $result = $this->Form->text('Model.text');
  2988. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2989. $Contact = ClassRegistry::getObject('Contact');
  2990. $Contact->validationErrors['text'] = array(true);
  2991. $this->Form->request->data['Contact']['text'] = 'test';
  2992. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2993. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2994. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2995. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2996. $expected = array(
  2997. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2998. );
  2999. $this->assertTags($result, $expected);
  3000. }
  3001. /**
  3002. * testDefaultValue method
  3003. *
  3004. * Test default value setting
  3005. *
  3006. * @return void
  3007. */
  3008. public function testDefaultValue() {
  3009. $this->Form->request->data['Model']['field'] = 'test';
  3010. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3011. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3012. unset($this->Form->request->data['Model']['field']);
  3013. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3014. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3015. }
  3016. /**
  3017. * testCheckboxDefaultValue method
  3018. *
  3019. * Test default value setting on checkbox() method
  3020. *
  3021. * @return void
  3022. */
  3023. public function testCheckboxDefaultValue() {
  3024. $this->Form->request->data['Model']['field'] = false;
  3025. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3026. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3027. unset($this->Form->request->data['Model']['field']);
  3028. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3029. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3030. $this->Form->request->data['Model']['field'] = true;
  3031. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3032. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3033. unset($this->Form->request->data['Model']['field']);
  3034. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3035. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3036. }
  3037. /**
  3038. * testError method
  3039. *
  3040. * Test field error generation
  3041. *
  3042. * @return void
  3043. */
  3044. public function testError() {
  3045. $Contact = ClassRegistry::getObject('Contact');
  3046. $Contact->validationErrors['field'] = array(1);
  3047. $result = $this->Form->error('Contact.field');
  3048. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3049. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3050. $this->assertEquals('Error in field Field', $result);
  3051. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3052. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3053. $this->assertEquals('This field contains invalid input', $result);
  3054. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3055. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3056. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3057. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3058. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3059. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3060. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3061. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3062. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3063. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3064. $this->assertEquals('<strong>Badness!</strong>', $result);
  3065. $Contact->validationErrors['field'] = array("email");
  3066. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3067. $expected = array(
  3068. 'div' => array('class' => 'field-error'),
  3069. 'No good!',
  3070. '/div'
  3071. );
  3072. $this->assertTags($result, $expected);
  3073. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3074. $result = $this->Form->error('Contact.field', array(
  3075. 'notEmpty' => 'Cannot be empty',
  3076. 'email' => 'No good!'
  3077. ));
  3078. $expected = array(
  3079. 'div' => array('class' => 'error-message'),
  3080. 'ul' => array(),
  3081. '<li', 'Cannot be empty', '/li',
  3082. '<li', 'No good!', '/li',
  3083. '<li', 'Something else', '/li',
  3084. '/ul',
  3085. '/div'
  3086. );
  3087. $this->assertTags($result, $expected);
  3088. // Testing error messages list options
  3089. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3090. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3091. $expected = array(
  3092. 'div' => array('class' => 'error-message'),
  3093. 'ol' => array(),
  3094. '<li', 'notEmpty', '/li',
  3095. '<li', 'email', '/li',
  3096. '/ol',
  3097. '/div'
  3098. );
  3099. $this->assertTags($result, $expected);
  3100. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3101. $expected = array(
  3102. 'div' => array('class' => 'error-message'),
  3103. 'ol' => array(),
  3104. '<li', 'notEmpty', '/li',
  3105. '<li', 'email', '/li',
  3106. '/ol',
  3107. '/div'
  3108. );
  3109. $this->assertTags($result, $expected);
  3110. $result = $this->Form->error('Contact.field', null, array(
  3111. 'listOptions' => array(
  3112. 'class' => 'ul-class',
  3113. 'itemOptions' => array(
  3114. 'class' => 'li-class'
  3115. )
  3116. )
  3117. ));
  3118. $expected = array(
  3119. 'div' => array('class' => 'error-message'),
  3120. 'ul' => array('class' => 'ul-class'),
  3121. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3122. array('li' => array('class' => 'li-class')), 'email', '/li',
  3123. '/ul',
  3124. '/div'
  3125. );
  3126. $this->assertTags($result, $expected);
  3127. }
  3128. /**
  3129. * test error options when using form->input();
  3130. *
  3131. * @return void
  3132. */
  3133. public function testInputErrorEscape() {
  3134. $this->Form->create('ValidateProfile');
  3135. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3136. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3137. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3138. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3139. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3140. $this->assertRegExp('/required<br>/', $result);
  3141. }
  3142. /**
  3143. * testPassword method
  3144. *
  3145. * Test password element generation
  3146. *
  3147. * @return void
  3148. */
  3149. public function testPassword() {
  3150. $Contact = ClassRegistry::getObject('Contact');
  3151. $result = $this->Form->password('Contact.field');
  3152. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3153. $Contact->validationErrors['passwd'] = 1;
  3154. $this->Form->request->data['Contact']['passwd'] = 'test';
  3155. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3156. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3157. }
  3158. /**
  3159. * testRadio method
  3160. *
  3161. * Test radio element set generation
  3162. *
  3163. * @return void
  3164. */
  3165. public function testRadio() {
  3166. $result = $this->Form->radio('Model.field', array('option A'));
  3167. $expected = array(
  3168. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3169. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3170. 'label' => array('for' => 'ModelField0'),
  3171. 'option A',
  3172. '/label'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3176. $expected = array(
  3177. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3178. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3179. 'label' => array('for' => 'ModelField12'),
  3180. 'half',
  3181. '/label'
  3182. );
  3183. $this->assertTags($result, $expected);
  3184. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3185. $expected = array(
  3186. 'fieldset' => array(),
  3187. 'legend' => array(),
  3188. 'Field',
  3189. '/legend',
  3190. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3191. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3192. array('label' => array('for' => 'ModelField0')),
  3193. 'option A',
  3194. '/label',
  3195. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3196. array('label' => array('for' => 'ModelField1')),
  3197. 'option B',
  3198. '/label',
  3199. '/fieldset'
  3200. );
  3201. $this->assertTags($result, $expected);
  3202. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3203. $expected = array(
  3204. 'fieldset' => array(),
  3205. 'legend' => array(),
  3206. 'Field',
  3207. '/legend',
  3208. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3209. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3210. array('label' => array('for' => 'ModelField0')),
  3211. 'option A',
  3212. '/label',
  3213. 'br' => array(),
  3214. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3215. array('label' => array('for' => 'ModelField1')),
  3216. 'option B',
  3217. '/label',
  3218. '/fieldset'
  3219. );
  3220. $this->assertTags($result, $expected);
  3221. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3222. $expected = array(
  3223. 'fieldset' => array(),
  3224. 'legend' => array(),
  3225. 'Gender',
  3226. '/legend',
  3227. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3228. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3229. array('label' => array('for' => 'EmployeeGenderMale')),
  3230. 'Male',
  3231. '/label',
  3232. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3233. array('label' => array('for' => 'EmployeeGenderFemale')),
  3234. 'Female',
  3235. '/label',
  3236. '/fieldset',
  3237. );
  3238. $this->assertTags($result, $expected);
  3239. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3240. $expected = array(
  3241. 'fieldset' => array(),
  3242. 'legend' => array(),
  3243. 'Gender',
  3244. '/legend',
  3245. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3246. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3247. array('label' => array('for' => 'OfficerGenderMale')),
  3248. 'Male',
  3249. '/label',
  3250. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3251. array('label' => array('for' => 'OfficerGenderFemale')),
  3252. 'Female',
  3253. '/label',
  3254. '/fieldset',
  3255. );
  3256. $this->assertTags($result, $expected);
  3257. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3258. $expected = array(
  3259. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3260. array('input' => array(
  3261. 'type' => 'radio',
  3262. 'name' => 'data[Contact][1][imrequired]',
  3263. 'value' => '0',
  3264. 'id' => 'Contact1Imrequired0',
  3265. 'required' => 'required'
  3266. )),
  3267. 'label' => array('for' => 'Contact1Imrequired0'),
  3268. 'option A',
  3269. '/label'
  3270. );
  3271. $this->assertTags($result, $expected);
  3272. $result = $this->Form->radio('Model.1.field', array('option A'));
  3273. $expected = array(
  3274. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3275. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3276. 'label' => array('for' => 'Model1Field0'),
  3277. 'option A',
  3278. '/label'
  3279. );
  3280. $this->assertTags($result, $expected);
  3281. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3282. $expected = array(
  3283. 'fieldset' => array(),
  3284. 'legend' => array(),
  3285. 'Field',
  3286. '/legend',
  3287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3289. array('label' => array('for' => 'ModelField0')),
  3290. 'option A',
  3291. '/label',
  3292. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3293. array('label' => array('for' => 'ModelField1')),
  3294. 'option B',
  3295. '/label',
  3296. '/fieldset'
  3297. );
  3298. $this->assertTags($result, $expected);
  3299. }
  3300. /**
  3301. * Test radio inputs with between as string or array. Also ensure
  3302. * that an array with less between elements works.
  3303. *
  3304. * @return void
  3305. */
  3306. public function testRadioBetween() {
  3307. $result = $this->Form->radio(
  3308. 'Model.field',
  3309. array('option A', 'option B'),
  3310. array('between' => 'I am between')
  3311. );
  3312. $expected = array(
  3313. 'fieldset' => array(),
  3314. 'legend' => array(),
  3315. 'Field',
  3316. '/legend',
  3317. 'I am between',
  3318. 'input' => array(
  3319. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3320. 'value' => '', 'id' => 'ModelField_'
  3321. ),
  3322. array('input' => array(
  3323. 'type' => 'radio', 'name' => 'data[Model][field]',
  3324. 'value' => '0', 'id' => 'ModelField0'
  3325. )),
  3326. array('label' => array('for' => 'ModelField0')),
  3327. 'option A',
  3328. '/label',
  3329. array('input' => array(
  3330. 'type' => 'radio', 'name' => 'data[Model][field]',
  3331. 'value' => '1', 'id' => 'ModelField1'
  3332. )),
  3333. array('label' => array('for' => 'ModelField1')),
  3334. 'option B',
  3335. '/label',
  3336. '/fieldset'
  3337. );
  3338. $this->assertTags($result, $expected);
  3339. $result = $this->Form->radio(
  3340. 'Model.field',
  3341. array('option A', 'option B', 'option C'),
  3342. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3343. );
  3344. $expected = array(
  3345. 'fieldset' => array(),
  3346. 'legend' => array(),
  3347. 'Field',
  3348. '/legend',
  3349. 'input' => array(
  3350. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3351. 'value' => '', 'id' => 'ModelField_'
  3352. ),
  3353. array('input' => array(
  3354. 'type' => 'radio', 'name' => 'data[Model][field]',
  3355. 'value' => '0', 'id' => 'ModelField0'
  3356. )),
  3357. array('label' => array('for' => 'ModelField0')),
  3358. 'option A',
  3359. '/label',
  3360. 'between A',
  3361. '--separator--',
  3362. array('input' => array(
  3363. 'type' => 'radio', 'name' => 'data[Model][field]',
  3364. 'value' => '1', 'id' => 'ModelField1'
  3365. )),
  3366. array('label' => array('for' => 'ModelField1')),
  3367. 'option B',
  3368. '/label',
  3369. 'between B',
  3370. '--separator--',
  3371. array('input' => array(
  3372. 'type' => 'radio', 'name' => 'data[Model][field]',
  3373. 'value' => '2', 'id' => 'ModelField2'
  3374. )),
  3375. array('label' => array('for' => 'ModelField2')),
  3376. 'option C',
  3377. '/label',
  3378. 'between C',
  3379. '/fieldset'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $result = $this->Form->input('Model.field', array(
  3383. 'options' => array('1' => 'first', '2' => 'second'),
  3384. 'type' => 'radio',
  3385. 'before' => '--before--',
  3386. 'after' => '--after--',
  3387. 'separator' => '--separator--',
  3388. 'between' => array('--between first--', '--between second--')
  3389. ));
  3390. $expected = array(
  3391. 'div' => array('class' => 'input radio'),
  3392. '--before--',
  3393. 'fieldset' => array(),
  3394. 'legend' => array(),
  3395. 'Field',
  3396. '/legend',
  3397. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3398. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3399. array('label' => array('for' => 'ModelField1')),
  3400. 'first',
  3401. '/label',
  3402. '--between first--',
  3403. '--separator--',
  3404. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3405. array('label' => array('for' => 'ModelField2')),
  3406. 'second',
  3407. '/label',
  3408. '--between second--',
  3409. '/fieldset',
  3410. '--after--',
  3411. '/div'
  3412. );
  3413. $this->assertTags($result, $expected);
  3414. $result = $this->Form->input('Model.field', array(
  3415. 'options' => array('1' => 'first', '2' => 'second'),
  3416. 'type' => 'radio',
  3417. 'before' => '--before--',
  3418. 'after' => '--after--',
  3419. 'separator' => '--separator--',
  3420. 'between' => array('--between first--')
  3421. ));
  3422. $expected = array(
  3423. 'div' => array('class' => 'input radio'),
  3424. '--before--',
  3425. 'fieldset' => array(),
  3426. 'legend' => array(),
  3427. 'Field',
  3428. '/legend',
  3429. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3430. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3431. array('label' => array('for' => 'ModelField1')),
  3432. 'first',
  3433. '/label',
  3434. '--between first--',
  3435. '--separator--',
  3436. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3437. array('label' => array('for' => 'ModelField2')),
  3438. 'second',
  3439. '/label',
  3440. '/fieldset',
  3441. '--after--',
  3442. '/div'
  3443. );
  3444. $this->assertTags($result, $expected);
  3445. }
  3446. /**
  3447. * Test that radios with a 0 value are selected under the correct conditions.
  3448. * Also ensure that values that are booleanish are handled correctly.
  3449. *
  3450. * @return void
  3451. */
  3452. public function testRadioOptionWithBooleanishValues() {
  3453. $expected = array(
  3454. 'fieldset' => array(),
  3455. 'legend' => array(),
  3456. 'Field',
  3457. '/legend',
  3458. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3459. array('label' => array('for' => 'ModelField1')),
  3460. 'Yes',
  3461. '/label',
  3462. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3463. array('label' => array('for' => 'ModelField0')),
  3464. 'No',
  3465. '/label',
  3466. '/fieldset'
  3467. );
  3468. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3469. $this->assertTags($result, $expected);
  3470. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3471. $this->assertTags($result, $expected);
  3472. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3473. $this->assertTags($result, $expected);
  3474. $expected = array(
  3475. 'fieldset' => array(),
  3476. 'legend' => array(),
  3477. 'Field',
  3478. '/legend',
  3479. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3480. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3481. array('label' => array('for' => 'ModelField1')),
  3482. 'Yes',
  3483. '/label',
  3484. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3485. array('label' => array('for' => 'ModelField0')),
  3486. 'No',
  3487. '/label',
  3488. '/fieldset'
  3489. );
  3490. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3491. $this->assertTags($result, $expected);
  3492. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3493. $this->assertTags($result, $expected);
  3494. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3495. $this->assertTags($result, $expected);
  3496. $expected = array(
  3497. 'fieldset' => array(),
  3498. 'legend' => array(),
  3499. 'Field',
  3500. '/legend',
  3501. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3502. array('label' => array('for' => 'ModelField1')),
  3503. 'Yes',
  3504. '/label',
  3505. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3506. array('label' => array('for' => 'ModelField0')),
  3507. 'No',
  3508. '/label',
  3509. '/fieldset'
  3510. );
  3511. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3512. $this->assertTags($result, $expected);
  3513. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3514. $this->assertTags($result, $expected);
  3515. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3516. $this->assertTags($result, $expected);
  3517. }
  3518. /**
  3519. * test disabled radio options
  3520. *
  3521. * @return void
  3522. */
  3523. public function testRadioDisabled() {
  3524. $result = $this->Form->radio(
  3525. 'Model.field',
  3526. array('option A', 'option B'),
  3527. array('disabled' => array(0), 'value' => '0')
  3528. );
  3529. $expected = array(
  3530. 'fieldset' => array(),
  3531. 'legend' => array(),
  3532. 'Field',
  3533. '/legend',
  3534. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3535. array('label' => array('for' => 'ModelField0')),
  3536. 'option A',
  3537. '/label',
  3538. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3539. array('label' => array('for' => 'ModelField1')),
  3540. 'option B',
  3541. '/label',
  3542. '/fieldset'
  3543. );
  3544. $this->assertTags($result, $expected);
  3545. $result = $this->Form->radio(
  3546. 'Model.field',
  3547. array('option A', 'option B'),
  3548. array('disabled' => true, 'value' => '0')
  3549. );
  3550. $expected = array(
  3551. 'fieldset' => array(),
  3552. 'legend' => array(),
  3553. 'Field',
  3554. '/legend',
  3555. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3556. array('label' => array('for' => 'ModelField0')),
  3557. 'option A',
  3558. '/label',
  3559. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3560. array('label' => array('for' => 'ModelField1')),
  3561. 'option B',
  3562. '/label',
  3563. '/fieldset'
  3564. );
  3565. $this->assertTags($result, $expected);
  3566. $result = $this->Form->radio(
  3567. 'Model.field',
  3568. array('option A', 'option B'),
  3569. array('disabled' => 'disabled', 'value' => '0')
  3570. );
  3571. $expected = array(
  3572. 'fieldset' => array(),
  3573. 'legend' => array(),
  3574. 'Field',
  3575. '/legend',
  3576. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3577. array('label' => array('for' => 'ModelField0')),
  3578. 'option A',
  3579. '/label',
  3580. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3581. array('label' => array('for' => 'ModelField1')),
  3582. 'option B',
  3583. '/label',
  3584. '/fieldset'
  3585. );
  3586. $this->assertTags($result, $expected);
  3587. $result = $this->Form->input('Model.field', array(
  3588. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3589. 'type' => 'radio',
  3590. 'disabled' => array(2, '3x'),
  3591. ));
  3592. $expected = array(
  3593. 'div' => array('class' => 'input radio'),
  3594. 'fieldset' => array(),
  3595. 'legend' => array(),
  3596. 'Field',
  3597. '/legend',
  3598. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3599. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3600. array('label' => array('for' => 'ModelField1')),
  3601. 'first',
  3602. '/label',
  3603. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3604. array('label' => array('for' => 'ModelField2')),
  3605. 'second',
  3606. '/label',
  3607. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3608. array('label' => array('for' => 'ModelField2x')),
  3609. '2x',
  3610. '/label',
  3611. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3612. array('label' => array('for' => 'ModelField3')),
  3613. 'third',
  3614. '/label',
  3615. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3616. array('label' => array('for' => 'ModelField3x')),
  3617. '3x',
  3618. '/label',
  3619. '/fieldset',
  3620. '/div'
  3621. );
  3622. $this->assertTags($result, $expected);
  3623. $result = $this->Form->input('Model.field', array(
  3624. 'type' => 'radio',
  3625. 'options' => array(
  3626. 1 => 'A',
  3627. 2 => 'B',
  3628. 3 => 'C'
  3629. ),
  3630. 'disabled' => array(1)
  3631. ));
  3632. $expected = array(
  3633. 'div' => array('class' => 'input radio'),
  3634. 'fieldset' => array(),
  3635. 'legend' => array(),
  3636. 'Field',
  3637. '/legend',
  3638. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3639. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3640. array('label' => array('for' => 'ModelField1')),
  3641. 'A',
  3642. '/label',
  3643. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3644. array('label' => array('for' => 'ModelField2')),
  3645. 'B',
  3646. '/label',
  3647. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3648. array('label' => array('for' => 'ModelField3')),
  3649. 'C',
  3650. '/label',
  3651. '/fieldset',
  3652. '/div'
  3653. );
  3654. $this->assertTags($result, $expected);
  3655. }
  3656. /**
  3657. * test disabling the hidden input for radio buttons
  3658. *
  3659. * @return void
  3660. */
  3661. public function testRadioHiddenInputDisabling() {
  3662. $result = $this->Form->input('Model.1.field', array(
  3663. 'type' => 'radio',
  3664. 'options' => array('option A'),
  3665. 'hiddenField' => false
  3666. )
  3667. );
  3668. $expected = array(
  3669. 'div' => array('class' => 'input radio'),
  3670. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3671. 'label' => array('for' => 'Model1Field0'),
  3672. 'option A',
  3673. '/label',
  3674. '/div'
  3675. );
  3676. $this->assertTags($result, $expected);
  3677. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3678. $expected = array(
  3679. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3680. 'label' => array('for' => 'Model1Field0'),
  3681. 'option A',
  3682. '/label'
  3683. );
  3684. $this->assertTags($result, $expected);
  3685. }
  3686. /**
  3687. * test adding an empty option for radio buttons
  3688. *
  3689. * @return void
  3690. */
  3691. public function testRadioAddEmptyOption() {
  3692. $result = $this->Form->input('Model.1.field', array(
  3693. 'type' => 'radio',
  3694. 'options' => array('option A'),
  3695. 'empty' => true,
  3696. 'hiddenField' => false
  3697. ));
  3698. $expected = array(
  3699. 'div' => array('class' => 'input radio'),
  3700. 'fieldset' => array(),
  3701. 'legend' => array(),
  3702. 'Field',
  3703. '/legend',
  3704. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3705. array('label' => array('for' => 'Model1Field')),
  3706. __('empty'),
  3707. '/label',
  3708. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3709. array('label' => array('for' => 'Model1Field0')),
  3710. 'option A',
  3711. '/label',
  3712. '/fieldset',
  3713. '/div'
  3714. );
  3715. $this->assertTags($result, $expected);
  3716. $result = $this->Form->input('Model.1.field', array(
  3717. 'type' => 'radio',
  3718. 'options' => array('option A'),
  3719. 'empty' => 'CustomEmptyLabel',
  3720. 'hiddenField' => false
  3721. ));
  3722. $expected = array(
  3723. 'div' => array('class' => 'input radio'),
  3724. 'fieldset' => array(),
  3725. 'legend' => array(),
  3726. 'Field',
  3727. '/legend',
  3728. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3729. array('label' => array('for' => 'Model1Field')),
  3730. 'CustomEmptyLabel',
  3731. '/label',
  3732. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3733. array('label' => array('for' => 'Model1Field0')),
  3734. 'option A',
  3735. '/label',
  3736. '/fieldset',
  3737. '/div'
  3738. );
  3739. $this->assertTags($result, $expected);
  3740. $result = $this->Form->input('Model.1.field', array(
  3741. 'type' => 'radio',
  3742. 'options' => array('option A'),
  3743. 'empty' => false,
  3744. 'hiddenField' => false
  3745. ));
  3746. $this->assertTextNotContains('"Model1Field"', $result);
  3747. }
  3748. /**
  3749. * Test that radio() accepts an array for label
  3750. *
  3751. * @return void
  3752. */
  3753. public function testRadioLabelArray() {
  3754. $result = $this->Form->input('Model.field', array(
  3755. 'type' => 'radio',
  3756. 'legend' => false,
  3757. 'label' => array(
  3758. 'class' => 'checkbox float-left',
  3759. ),
  3760. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3761. ));
  3762. $this->assertTextContains(
  3763. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3764. $result
  3765. );
  3766. }
  3767. /**
  3768. * Test that label id's match the input element id's when radio is called after create().
  3769. *
  3770. * @return void
  3771. */
  3772. public function testRadioWithCreate() {
  3773. $this->Form->create('Model');
  3774. $result = $this->Form->radio('recipient',
  3775. array('1' => '1', '2' => '2', '3' => '3'),
  3776. array('legend' => false, 'value' => '1')
  3777. );
  3778. $this->assertTextNotContains(
  3779. '<label for="ModelModelRecipient1">1</label>',
  3780. $result
  3781. );
  3782. $this->assertTextContains(
  3783. '<label for="ModelRecipient1">1</label>',
  3784. $result
  3785. );
  3786. }
  3787. /**
  3788. * testSelect method
  3789. *
  3790. * Test select element generation.
  3791. *
  3792. * @return void
  3793. */
  3794. public function testSelect() {
  3795. $result = $this->Form->select('Model.field', array());
  3796. $expected = array(
  3797. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3798. array('option' => array('value' => '')),
  3799. '/option',
  3800. '/select'
  3801. );
  3802. $this->assertTags($result, $expected);
  3803. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3804. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3805. $expected = array(
  3806. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3807. array('option' => array('value' => '')),
  3808. '/option',
  3809. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3810. 'good',
  3811. '/option',
  3812. array('option' => array('value' => 'other')),
  3813. 'bad',
  3814. '/option',
  3815. '/select'
  3816. );
  3817. $this->assertTags($result, $expected);
  3818. $this->Form->request->data = array();
  3819. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3820. $expected = array(
  3821. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3822. array('option' => array('value' => '')),
  3823. '/option',
  3824. array('option' => array('value' => 'value')),
  3825. 'good',
  3826. '/option',
  3827. array('option' => array('value' => 'other')),
  3828. 'bad',
  3829. '/option',
  3830. '/select'
  3831. );
  3832. $this->assertTags($result, $expected);
  3833. $result = $this->Form->select(
  3834. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3835. array('empty' => false)
  3836. );
  3837. $expected = array(
  3838. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3839. array('option' => array('value' => 'first')),
  3840. 'first &quot;html&quot; &lt;chars&gt;',
  3841. '/option',
  3842. array('option' => array('value' => 'second')),
  3843. 'value',
  3844. '/option',
  3845. '/select'
  3846. );
  3847. $this->assertTags($result, $expected);
  3848. $result = $this->Form->select(
  3849. 'Model.field',
  3850. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3851. array('escape' => false, 'empty' => false)
  3852. );
  3853. $expected = array(
  3854. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3855. array('option' => array('value' => 'first')),
  3856. 'first "html" <chars>',
  3857. '/option',
  3858. array('option' => array('value' => 'second')),
  3859. 'value',
  3860. '/option',
  3861. '/select'
  3862. );
  3863. $this->assertTags($result, $expected);
  3864. $options = array(
  3865. array('value' => 'first', 'name' => 'First'),
  3866. array('value' => 'first', 'name' => 'Another First'),
  3867. );
  3868. $result = $this->Form->select(
  3869. 'Model.field',
  3870. $options,
  3871. array('escape' => false, 'empty' => false)
  3872. );
  3873. $expected = array(
  3874. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3875. array('option' => array('value' => 'first')),
  3876. 'First',
  3877. '/option',
  3878. array('option' => array('value' => 'first')),
  3879. 'Another First',
  3880. '/option',
  3881. '/select'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3885. $result = $this->Form->select(
  3886. 'Model.contact_id',
  3887. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3888. array('escape' => false, 'empty' => 'pick something')
  3889. );
  3890. $expected = array(
  3891. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3892. array('option' => array('value' => '')), 'pick something', '/option',
  3893. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3894. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3895. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3896. '/select'
  3897. );
  3898. $this->assertTags($result, $expected);
  3899. $this->Form->request->data['Model']['field'] = 0;
  3900. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3901. $expected = array(
  3902. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3903. array('option' => array('value' => '')), '/option',
  3904. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3905. array('option' => array('value' => '1')), 'Yes', '/option',
  3906. '/select'
  3907. );
  3908. $this->assertTags($result, $expected);
  3909. $this->Form->request->data['Model']['field'] = 50;
  3910. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3911. $expected = array(
  3912. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3913. array('option' => array('value' => '')), '/option',
  3914. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3915. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3916. '/select'
  3917. );
  3918. $this->assertTags($result, $expected);
  3919. $result = $this->Form->select('Contact.required_one', array('option A'));
  3920. $expected = array(
  3921. 'select' => array(
  3922. 'name' => 'data[Contact][required_one]',
  3923. 'id' => 'ContactRequiredOne',
  3924. 'required' => 'required'
  3925. ),
  3926. array('option' => array('value' => '')), '/option',
  3927. array('option' => array('value' => '0')), 'option A', '/option',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3932. $expected = array(
  3933. 'select' => array(
  3934. 'name' => 'data[Contact][required_one]',
  3935. 'id' => 'ContactRequiredOne',
  3936. 'disabled' => 'disabled'
  3937. ),
  3938. array('option' => array('value' => '')), '/option',
  3939. array('option' => array('value' => '0')), 'option A', '/option',
  3940. '/select'
  3941. );
  3942. $this->assertTags($result, $expected);
  3943. }
  3944. /**
  3945. * test that select() with optiongroups listens to the escape param.
  3946. *
  3947. * @return void
  3948. */
  3949. public function testSelectOptionGroupEscaping() {
  3950. $options = array(
  3951. '>< Key' => array(
  3952. 1 => 'One',
  3953. 2 => 'Two'
  3954. )
  3955. );
  3956. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3957. $expected = array(
  3958. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3959. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3960. array('option' => array('value' => '1')), 'One', '/option',
  3961. array('option' => array('value' => '2')), 'Two', '/option',
  3962. '/optgroup',
  3963. '/select'
  3964. );
  3965. $this->assertTags($result, $expected);
  3966. $options = array(
  3967. '>< Key' => array(
  3968. 1 => 'One',
  3969. 2 => 'Two'
  3970. )
  3971. );
  3972. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3973. $expected = array(
  3974. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3975. 'optgroup' => array('label' => '>< Key'),
  3976. array('option' => array('value' => '1')), 'One', '/option',
  3977. array('option' => array('value' => '2')), 'Two', '/option',
  3978. '/optgroup',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. }
  3983. /**
  3984. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3985. *
  3986. * @return void
  3987. */
  3988. public function testSelectWithNullAttributes() {
  3989. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3990. $expected = array(
  3991. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3992. array('option' => array('value' => '0')),
  3993. 'first',
  3994. '/option',
  3995. array('option' => array('value' => '1')),
  3996. 'second',
  3997. '/option',
  3998. '/select'
  3999. );
  4000. $this->assertTags($result, $expected);
  4001. }
  4002. /**
  4003. * testNestedSelect method
  4004. *
  4005. * test select element generation with optgroups
  4006. *
  4007. * @return void
  4008. */
  4009. public function testNestedSelect() {
  4010. $result = $this->Form->select(
  4011. 'Model.field',
  4012. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4013. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4014. )), array('empty' => false)
  4015. );
  4016. $expected = array(
  4017. 'select' => array('name' => 'data[Model][field]',
  4018. 'id' => 'ModelField'),
  4019. array('option' => array('value' => 1)),
  4020. 'One',
  4021. '/option',
  4022. array('option' => array('value' => 2)),
  4023. 'Two',
  4024. '/option',
  4025. array('optgroup' => array('label' => 'Three')),
  4026. array('option' => array('value' => 4)),
  4027. 'Four',
  4028. '/option',
  4029. array('option' => array('value' => 5)),
  4030. 'Five',
  4031. '/option',
  4032. '/optgroup',
  4033. '/select'
  4034. );
  4035. $this->assertTags($result, $expected);
  4036. $result = $this->Form->select(
  4037. 'Model.field',
  4038. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4039. array('showParents' => true, 'empty' => false)
  4040. );
  4041. $expected = array(
  4042. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4043. array('option' => array('value' => 1)),
  4044. 'One',
  4045. '/option',
  4046. array('option' => array('value' => 2)),
  4047. 'Two',
  4048. '/option',
  4049. array('optgroup' => array('label' => 'Three')),
  4050. array('option' => array('value' => 3)),
  4051. 'Three',
  4052. '/option',
  4053. array('option' => array('value' => 4)),
  4054. 'Four',
  4055. '/option',
  4056. '/optgroup',
  4057. '/select'
  4058. );
  4059. $this->assertTags($result, $expected);
  4060. }
  4061. /**
  4062. * testSelectMultiple method
  4063. *
  4064. * test generation of multiple select elements
  4065. *
  4066. * @return void
  4067. */
  4068. public function testSelectMultiple() {
  4069. $options = array('first', 'second', 'third');
  4070. $result = $this->Form->select(
  4071. 'Model.multi_field', $options, array('multiple' => true)
  4072. );
  4073. $expected = array(
  4074. 'input' => array(
  4075. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4076. ),
  4077. 'select' => array(
  4078. 'name' => 'data[Model][multi_field][]',
  4079. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4080. ),
  4081. array('option' => array('value' => '0')),
  4082. 'first',
  4083. '/option',
  4084. array('option' => array('value' => '1')),
  4085. 'second',
  4086. '/option',
  4087. array('option' => array('value' => '2')),
  4088. 'third',
  4089. '/option',
  4090. '/select'
  4091. );
  4092. $this->assertTags($result, $expected);
  4093. $result = $this->Form->select(
  4094. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4095. );
  4096. $expected = array(
  4097. 'input' => array(
  4098. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4099. ),
  4100. 'select' => array(
  4101. 'name' => 'data[Model][multi_field][]',
  4102. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4103. ),
  4104. array('option' => array('value' => '0')),
  4105. 'first',
  4106. '/option',
  4107. array('option' => array('value' => '1')),
  4108. 'second',
  4109. '/option',
  4110. array('option' => array('value' => '2')),
  4111. 'third',
  4112. '/option',
  4113. '/select'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. $result = $this->Form->select(
  4117. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4118. );
  4119. $expected = array(
  4120. 'input' => array(
  4121. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4122. ),
  4123. 'select' => array(
  4124. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4125. 'multiple' => 'multiple'
  4126. ),
  4127. array('option' => array('value' => '0', 'selected' => 'selected')),
  4128. 'first',
  4129. '/option',
  4130. array('option' => array('value' => '1', 'selected' => 'selected')),
  4131. 'second',
  4132. '/option',
  4133. array('option' => array('value' => '2')),
  4134. 'third',
  4135. '/option',
  4136. '/select'
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. $result = $this->Form->select(
  4140. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4141. );
  4142. $expected = array(
  4143. 'select' => array(
  4144. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4145. ),
  4146. array('option' => array('value' => '0', 'selected' => 'selected')),
  4147. 'first',
  4148. '/option',
  4149. array('option' => array('value' => '1', 'selected' => 'selected')),
  4150. 'second',
  4151. '/option',
  4152. array('option' => array('value' => '2')),
  4153. 'third',
  4154. '/option',
  4155. '/select'
  4156. );
  4157. $this->assertTags($result, $expected);
  4158. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4159. $selected = array('2', '3x');
  4160. $result = $this->Form->select(
  4161. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4162. );
  4163. $expected = array(
  4164. 'input' => array(
  4165. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4166. ),
  4167. 'select' => array(
  4168. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4169. ),
  4170. array('option' => array('value' => '1')),
  4171. 'One',
  4172. '/option',
  4173. array('option' => array('value' => '2', 'selected' => 'selected')),
  4174. 'Two',
  4175. '/option',
  4176. array('option' => array('value' => '3')),
  4177. 'Three',
  4178. '/option',
  4179. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4180. 'Stringy',
  4181. '/option',
  4182. '/select'
  4183. );
  4184. $this->assertTags($result, $expected);
  4185. $result = $this->Form->select('Contact.required_one', array(
  4186. '1' => 'option A',
  4187. '2' => 'option B'
  4188. ), array('multiple' => true));
  4189. $expected = array(
  4190. 'input' => array(
  4191. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4192. ),
  4193. 'select' => array(
  4194. 'name' => 'data[Contact][required_one][]',
  4195. 'id' => 'ContactRequiredOne',
  4196. 'required' => 'required',
  4197. 'multiple' => 'multiple'
  4198. ),
  4199. array('option' => array('value' => '1')), 'option A', '/option',
  4200. array('option' => array('value' => '2')), 'option B', '/option',
  4201. '/select'
  4202. );
  4203. $this->assertTags($result, $expected);
  4204. }
  4205. /**
  4206. * Test generating multiple select with disabled elements.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testSelectMultipleWithDisabledElements() {
  4211. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4212. $disabled = array(2, 3);
  4213. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4214. $expected = array(
  4215. 'input' => array(
  4216. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4217. ),
  4218. 'select' => array(
  4219. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4220. ),
  4221. array('option' => array('value' => '1')),
  4222. 'One',
  4223. '/option',
  4224. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4225. 'Two',
  4226. '/option',
  4227. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4228. 'Three',
  4229. '/option',
  4230. array('option' => array('value' => '3x')),
  4231. 'Stringy',
  4232. '/option',
  4233. '/select'
  4234. );
  4235. $this->assertTags($result, $expected);
  4236. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4237. $disabled = array('2', '3x');
  4238. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4239. $expected = array(
  4240. array('div' => array('class' => 'input select')),
  4241. array('label' => array('for' => 'ContactMultiple')),
  4242. 'Multiple',
  4243. '/label',
  4244. 'input' => array(
  4245. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4246. ),
  4247. 'select' => array(
  4248. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4249. ),
  4250. array('option' => array('value' => '1')),
  4251. 'One',
  4252. '/option',
  4253. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4254. 'Two',
  4255. '/option',
  4256. array('option' => array('value' => '3')),
  4257. 'Three',
  4258. '/option',
  4259. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4260. 'Stringy',
  4261. '/option',
  4262. '/select',
  4263. '/div'
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4267. $disabled = true;
  4268. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4269. $expected = array(
  4270. array('div' => array('class' => 'input select')),
  4271. array('label' => array('for' => 'ContactMultiple')),
  4272. 'Multiple',
  4273. '/label',
  4274. 'input' => array(
  4275. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4276. ),
  4277. 'select' => array(
  4278. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4279. ),
  4280. array('option' => array('value' => '1')),
  4281. 'One',
  4282. '/option',
  4283. array('option' => array('value' => '2')),
  4284. 'Two',
  4285. '/option',
  4286. array('option' => array('value' => '3')),
  4287. 'Three',
  4288. '/option',
  4289. array('option' => array('value' => '3x')),
  4290. 'Stringy',
  4291. '/option',
  4292. '/select',
  4293. '/div'
  4294. );
  4295. $this->assertTags($result, $expected);
  4296. }
  4297. /**
  4298. * Test generating select with disabled elements.
  4299. *
  4300. * @return void
  4301. */
  4302. public function testSelectWithDisabledElements() {
  4303. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4304. $disabled = array(2, 3);
  4305. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4306. $expected = array(
  4307. 'select' => array(
  4308. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4309. ),
  4310. array('option' => array('value' => '')),
  4311. '/option',
  4312. array('option' => array('value' => '1')),
  4313. 'One',
  4314. '/option',
  4315. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4316. 'Two',
  4317. '/option',
  4318. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4319. 'Three',
  4320. '/option',
  4321. array('option' => array('value' => '3x')),
  4322. 'Stringy',
  4323. '/option',
  4324. '/select'
  4325. );
  4326. $this->assertTags($result, $expected);
  4327. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4328. $disabled = array('2', '3x');
  4329. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4330. $expected = array(
  4331. array('div' => array('class' => 'input select')),
  4332. array('label' => array('for' => 'ModelField')),
  4333. 'Field',
  4334. '/label',
  4335. 'select' => array(
  4336. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4337. ),
  4338. array('option' => array('value' => '1')),
  4339. 'One',
  4340. '/option',
  4341. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4342. 'Two',
  4343. '/option',
  4344. array('option' => array('value' => '3')),
  4345. 'Three',
  4346. '/option',
  4347. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4348. 'Stringy',
  4349. '/option',
  4350. '/select',
  4351. '/div'
  4352. );
  4353. $this->assertTags($result, $expected);
  4354. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4355. $disabled = true;
  4356. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4357. $expected = array(
  4358. array('div' => array('class' => 'input select')),
  4359. array('label' => array('for' => 'ModelField')),
  4360. 'Field',
  4361. '/label',
  4362. 'select' => array(
  4363. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4364. ),
  4365. array('option' => array('value' => '1')),
  4366. 'One',
  4367. '/option',
  4368. array('option' => array('value' => '2')),
  4369. 'Two',
  4370. '/option',
  4371. array('option' => array('value' => '3')),
  4372. 'Three',
  4373. '/option',
  4374. array('option' => array('value' => '3x')),
  4375. 'Stringy',
  4376. '/option',
  4377. '/select',
  4378. '/div'
  4379. );
  4380. $this->assertTags($result, $expected);
  4381. }
  4382. /**
  4383. * test generation of habtm select boxes.
  4384. *
  4385. * @return void
  4386. */
  4387. public function testHabtmSelectBox() {
  4388. $this->View->viewVars['contactTags'] = array(
  4389. 1 => 'blue',
  4390. 2 => 'red',
  4391. 3 => 'green'
  4392. );
  4393. $this->Form->request->data = array(
  4394. 'Contact' => array(),
  4395. 'ContactTag' => array(
  4396. array(
  4397. 'id' => '1',
  4398. 'name' => 'blue'
  4399. ),
  4400. array(
  4401. 'id' => 3,
  4402. 'name' => 'green'
  4403. )
  4404. )
  4405. );
  4406. $this->Form->create('Contact');
  4407. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4408. $expected = array(
  4409. 'input' => array(
  4410. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4411. ),
  4412. 'select' => array(
  4413. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4414. 'multiple' => 'multiple'
  4415. ),
  4416. array('option' => array('value' => '1', 'selected' => 'selected')),
  4417. 'blue',
  4418. '/option',
  4419. array('option' => array('value' => '2')),
  4420. 'red',
  4421. '/option',
  4422. array('option' => array('value' => '3', 'selected' => 'selected')),
  4423. 'green',
  4424. '/option',
  4425. '/select'
  4426. );
  4427. $this->assertTags($result, $expected);
  4428. // make sure only 50 is selected, and not 50f5c0cf
  4429. $this->View->viewVars['contactTags'] = array(
  4430. '1' => 'blue',
  4431. '50f5c0cf' => 'red',
  4432. '50' => 'green'
  4433. );
  4434. $this->Form->request->data = array(
  4435. 'Contact' => array(),
  4436. 'ContactTag' => array(
  4437. array(
  4438. 'id' => 1,
  4439. 'name' => 'blue'
  4440. ),
  4441. array(
  4442. 'id' => 50,
  4443. 'name' => 'green'
  4444. )
  4445. )
  4446. );
  4447. $this->Form->create('Contact');
  4448. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4449. $expected = array(
  4450. 'input' => array(
  4451. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4452. ),
  4453. 'select' => array(
  4454. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4455. 'multiple' => 'multiple'
  4456. ),
  4457. array('option' => array('value' => '1', 'selected' => 'selected')),
  4458. 'blue',
  4459. '/option',
  4460. array('option' => array('value' => '50f5c0cf')),
  4461. 'red',
  4462. '/option',
  4463. array('option' => array('value' => '50', 'selected' => 'selected')),
  4464. 'green',
  4465. '/option',
  4466. '/select'
  4467. );
  4468. $this->assertTags($result, $expected);
  4469. }
  4470. /**
  4471. * test generation of multi select elements in checkbox format
  4472. *
  4473. * @return void
  4474. */
  4475. public function testSelectMultipleCheckboxes() {
  4476. $result = $this->Form->select(
  4477. 'Model.multi_field',
  4478. array('first', 'second', 'third'),
  4479. array('multiple' => 'checkbox')
  4480. );
  4481. $expected = array(
  4482. 'input' => array(
  4483. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4484. ),
  4485. array('div' => array('class' => 'checkbox')),
  4486. array('input' => array(
  4487. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4488. 'value' => '0', 'id' => 'ModelMultiField0'
  4489. )),
  4490. array('label' => array('for' => 'ModelMultiField0')),
  4491. 'first',
  4492. '/label',
  4493. '/div',
  4494. array('div' => array('class' => 'checkbox')),
  4495. array('input' => array(
  4496. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4497. 'value' => '1', 'id' => 'ModelMultiField1'
  4498. )),
  4499. array('label' => array('for' => 'ModelMultiField1')),
  4500. 'second',
  4501. '/label',
  4502. '/div',
  4503. array('div' => array('class' => 'checkbox')),
  4504. array('input' => array(
  4505. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4506. 'value' => '2', 'id' => 'ModelMultiField2'
  4507. )),
  4508. array('label' => array('for' => 'ModelMultiField2')),
  4509. 'third',
  4510. '/label',
  4511. '/div'
  4512. );
  4513. $this->assertTags($result, $expected);
  4514. $result = $this->Form->select(
  4515. 'Model.multi_field',
  4516. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4517. array('multiple' => 'checkbox')
  4518. );
  4519. $expected = array(
  4520. 'input' => array(
  4521. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4522. ),
  4523. array('div' => array('class' => 'checkbox')),
  4524. array('input' => array(
  4525. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4526. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4527. )),
  4528. array('label' => array('for' => 'ModelMultiFieldA')),
  4529. 'first',
  4530. '/label',
  4531. '/div',
  4532. array('div' => array('class' => 'checkbox')),
  4533. array('input' => array(
  4534. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4535. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4536. )),
  4537. array('label' => array('for' => 'ModelMultiFieldB')),
  4538. 'second',
  4539. '/label',
  4540. '/div',
  4541. array('div' => array('class' => 'checkbox')),
  4542. array('input' => array(
  4543. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4544. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4545. )),
  4546. array('label' => array('for' => 'ModelMultiFieldC')),
  4547. 'third',
  4548. '/label',
  4549. '/div'
  4550. );
  4551. $this->assertTags($result, $expected);
  4552. $result = $this->Form->select(
  4553. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4554. );
  4555. $expected = array(
  4556. 'input' => array(
  4557. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4558. ),
  4559. array('div' => array('class' => 'checkbox')),
  4560. array('input' => array(
  4561. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4562. 'value' => '1', 'id' => 'ModelMultiField1'
  4563. )),
  4564. array('label' => array('for' => 'ModelMultiField1')),
  4565. 'first',
  4566. '/label',
  4567. '/div'
  4568. );
  4569. $this->assertTags($result, $expected);
  4570. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4571. $result = $this->Form->select(
  4572. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4573. );
  4574. $expected = array(
  4575. 'input' => array(
  4576. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4577. ),
  4578. array('div' => array('class' => 'checkbox')),
  4579. array('input' => array(
  4580. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4581. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4582. )),
  4583. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4584. 'first',
  4585. '/label',
  4586. '/div',
  4587. array('div' => array('class' => 'checkbox')),
  4588. array('input' => array(
  4589. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4590. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4591. )),
  4592. array('label' => array('for' => 'ModelTagsArray')),
  4593. 'Array',
  4594. '/label',
  4595. '/div'
  4596. );
  4597. $this->assertTags($result, $expected);
  4598. }
  4599. /**
  4600. * test multiple checkboxes with div styles.
  4601. *
  4602. * @return void
  4603. */
  4604. public function testSelectMultipleCheckboxDiv() {
  4605. $result = $this->Form->select(
  4606. 'Model.tags',
  4607. array('first', 'second'),
  4608. array('multiple' => 'checkbox', 'class' => 'my-class')
  4609. );
  4610. $expected = array(
  4611. 'input' => array(
  4612. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4613. ),
  4614. array('div' => array('class' => 'my-class')),
  4615. array('input' => array(
  4616. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4617. 'value' => '0', 'id' => 'ModelTags0'
  4618. )),
  4619. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4620. '/div',
  4621. array('div' => array('class' => 'my-class')),
  4622. array('input' => array(
  4623. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4624. 'value' => '1', 'id' => 'ModelTags1'
  4625. )),
  4626. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4627. '/div'
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $result = $this->Form->input('Model.tags', array(
  4631. 'options' => array('first', 'second'),
  4632. 'multiple' => 'checkbox',
  4633. 'class' => 'my-class',
  4634. 'div' => false,
  4635. 'label' => false
  4636. ));
  4637. $this->assertTags($result, $expected);
  4638. $Contact = ClassRegistry::getObject('Contact');
  4639. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4640. $result = $this->Form->input('Contact.tags', array(
  4641. 'options' => array('one'),
  4642. 'multiple' => 'checkbox',
  4643. 'label' => false,
  4644. 'div' => false
  4645. ));
  4646. $expected = array(
  4647. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4648. array('div' => array('class' => 'checkbox form-error')),
  4649. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4650. array('label' => array('for' => 'ContactTags0')),
  4651. 'one',
  4652. '/label',
  4653. '/div'
  4654. );
  4655. $this->assertTags($result, $expected);
  4656. $result = $this->Form->input('Contact.tags', array(
  4657. 'options' => array('one'),
  4658. 'multiple' => 'checkbox',
  4659. 'class' => 'mycheckbox',
  4660. 'label' => false,
  4661. 'div' => false
  4662. ));
  4663. $expected = array(
  4664. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4665. array('div' => array('class' => 'mycheckbox form-error')),
  4666. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4667. array('label' => array('for' => 'ContactTags0')),
  4668. 'one',
  4669. '/label',
  4670. '/div'
  4671. );
  4672. $this->assertTags($result, $expected);
  4673. }
  4674. /**
  4675. * Checks the security hash array generated for multiple-input checkbox elements
  4676. *
  4677. * @return void
  4678. */
  4679. public function testSelectMultipleCheckboxSecurity() {
  4680. $this->Form->request['_Token'] = array('key' => 'testKey');
  4681. $this->assertEquals(array(), $this->Form->fields);
  4682. $result = $this->Form->select(
  4683. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4684. array('multiple' => 'checkbox')
  4685. );
  4686. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4687. $result = $this->Form->secure($this->Form->fields);
  4688. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4689. $this->assertRegExp('/"' . $key . '"/', $result);
  4690. }
  4691. /**
  4692. * Multiple select elements should always be secured as they always participate
  4693. * in the POST data.
  4694. *
  4695. * @return void
  4696. */
  4697. public function testSelectMultipleSecureWithNoOptions() {
  4698. $this->Form->request['_Token'] = array('key' => 'testkey');
  4699. $this->assertEquals(array(), $this->Form->fields);
  4700. $this->Form->select(
  4701. 'Model.select',
  4702. array(),
  4703. array('multiple' => true)
  4704. );
  4705. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4706. }
  4707. /**
  4708. * When a select box has no options it should not be added to the fields list
  4709. * as it always fail post validation.
  4710. *
  4711. * @return void
  4712. */
  4713. public function testSelectNoSecureWithNoOptions() {
  4714. $this->Form->request['_Token'] = array('key' => 'testkey');
  4715. $this->assertEquals(array(), $this->Form->fields);
  4716. $this->Form->select(
  4717. 'Model.select',
  4718. array()
  4719. );
  4720. $this->assertEquals(array(), $this->Form->fields);
  4721. $this->Form->select(
  4722. 'Model.select',
  4723. array(),
  4724. array('empty' => true)
  4725. );
  4726. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4727. }
  4728. /**
  4729. * testInputMultipleCheckboxes method
  4730. *
  4731. * test input() resulting in multi select elements being generated.
  4732. *
  4733. * @return void
  4734. */
  4735. public function testInputMultipleCheckboxes() {
  4736. $result = $this->Form->input('Model.multi_field', array(
  4737. 'options' => array('first', 'second', 'third'),
  4738. 'multiple' => 'checkbox'
  4739. ));
  4740. $expected = array(
  4741. array('div' => array('class' => 'input select')),
  4742. array('label' => array('for' => 'ModelMultiField')),
  4743. 'Multi Field',
  4744. '/label',
  4745. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4746. array('div' => array('class' => 'checkbox')),
  4747. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4748. array('label' => array('for' => 'ModelMultiField0')),
  4749. 'first',
  4750. '/label',
  4751. '/div',
  4752. array('div' => array('class' => 'checkbox')),
  4753. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4754. array('label' => array('for' => 'ModelMultiField1')),
  4755. 'second',
  4756. '/label',
  4757. '/div',
  4758. array('div' => array('class' => 'checkbox')),
  4759. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4760. array('label' => array('for' => 'ModelMultiField2')),
  4761. 'third',
  4762. '/label',
  4763. '/div',
  4764. '/div'
  4765. );
  4766. $this->assertTags($result, $expected);
  4767. $result = $this->Form->input('Model.multi_field', array(
  4768. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4769. 'multiple' => 'checkbox'
  4770. ));
  4771. $expected = array(
  4772. array('div' => array('class' => 'input select')),
  4773. array('label' => array('for' => 'ModelMultiField')),
  4774. 'Multi Field',
  4775. '/label',
  4776. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4777. array('div' => array('class' => 'checkbox')),
  4778. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4779. array('label' => array('for' => 'ModelMultiFieldA')),
  4780. 'first',
  4781. '/label',
  4782. '/div',
  4783. array('div' => array('class' => 'checkbox')),
  4784. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4785. array('label' => array('for' => 'ModelMultiFieldB')),
  4786. 'second',
  4787. '/label',
  4788. '/div',
  4789. array('div' => array('class' => 'checkbox')),
  4790. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4791. array('label' => array('for' => 'ModelMultiFieldC')),
  4792. 'third',
  4793. '/label',
  4794. '/div',
  4795. '/div'
  4796. );
  4797. $this->assertTags($result, $expected);
  4798. $result = $this->Form->input('Model.multi_field', array(
  4799. 'options' => array('1' => 'first'),
  4800. 'multiple' => 'checkbox',
  4801. 'label' => false,
  4802. 'div' => false
  4803. ));
  4804. $expected = array(
  4805. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4806. array('div' => array('class' => 'checkbox')),
  4807. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4808. array('label' => array('for' => 'ModelMultiField1')),
  4809. 'first',
  4810. '/label',
  4811. '/div'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. $result = $this->Form->input('Model.multi_field', array(
  4815. 'options' => array('2' => 'second'),
  4816. 'multiple' => 'checkbox',
  4817. 'label' => false,
  4818. 'div' => false
  4819. ));
  4820. $expected = array(
  4821. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4822. array('div' => array('class' => 'checkbox')),
  4823. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4824. array('label' => array('for' => 'ModelMultiField2')),
  4825. 'second',
  4826. '/label',
  4827. '/div'
  4828. );
  4829. $this->assertTags($result, $expected);
  4830. }
  4831. /**
  4832. * testSelectHiddenFieldOmission method
  4833. *
  4834. * test that select() with 'hiddenField' => false omits the hidden field
  4835. *
  4836. * @return void
  4837. */
  4838. public function testSelectHiddenFieldOmission() {
  4839. $result = $this->Form->select('Model.multi_field',
  4840. array('first', 'second'),
  4841. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4842. );
  4843. $expected = array(
  4844. array('div' => array('class' => 'checkbox')),
  4845. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4846. array('label' => array('for' => 'ModelMultiField0')),
  4847. 'first',
  4848. '/label',
  4849. '/div',
  4850. array('div' => array('class' => 'checkbox')),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4852. array('label' => array('for' => 'ModelMultiField1')),
  4853. 'second',
  4854. '/label',
  4855. '/div'
  4856. );
  4857. $this->assertTags($result, $expected);
  4858. $result = $this->Form->input('Model.multi_field', array(
  4859. 'options' => array('first', 'second'),
  4860. 'multiple' => 'checkbox',
  4861. 'hiddenField' => false
  4862. ));
  4863. $expected = array(
  4864. array('div' => array('class' => 'input select')),
  4865. array('label' => array('for' => 'ModelMultiField')),
  4866. 'Multi Field',
  4867. '/label',
  4868. array('div' => array('class' => 'checkbox')),
  4869. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4870. array('label' => array('for' => 'ModelMultiField0')),
  4871. 'first',
  4872. '/label',
  4873. '/div',
  4874. array('div' => array('class' => 'checkbox')),
  4875. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4876. array('label' => array('for' => 'ModelMultiField1')),
  4877. 'second',
  4878. '/label',
  4879. '/div',
  4880. '/div'
  4881. );
  4882. $this->assertTags($result, $expected);
  4883. }
  4884. /**
  4885. * test that select() with multiple = checkbox works with overriding name attribute.
  4886. *
  4887. * @return void
  4888. */
  4889. public function testSelectCheckboxMultipleOverrideName() {
  4890. $result = $this->Form->input('category', array(
  4891. 'type' => 'select',
  4892. 'multiple' => 'checkbox',
  4893. 'name' => 'data[fish]',
  4894. 'options' => array('1', '2'),
  4895. 'div' => false,
  4896. 'label' => false,
  4897. ));
  4898. $expected = array(
  4899. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4900. array('div' => array('class' => 'checkbox')),
  4901. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4902. array('label' => array('for' => 'Category0')), '1', '/label',
  4903. '/div',
  4904. array('div' => array('class' => 'checkbox')),
  4905. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4906. array('label' => array('for' => 'Category1')), '2', '/label',
  4907. '/div'
  4908. );
  4909. $this->assertTags($result, $expected);
  4910. }
  4911. /**
  4912. * Test that 'id' overrides all the checkbox id's as well.
  4913. *
  4914. * @return void
  4915. */
  4916. public function testSelectCheckboxMultipleId() {
  4917. $result = $this->Form->select(
  4918. 'Model.multi_field',
  4919. array('first', 'second', 'third'),
  4920. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4921. );
  4922. $expected = array(
  4923. 'input' => array(
  4924. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4925. ),
  4926. array('div' => array('class' => 'checkbox')),
  4927. array('input' => array(
  4928. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4929. 'value' => '0', 'id' => 'CustomId0'
  4930. )),
  4931. array('label' => array('for' => 'CustomId0')),
  4932. 'first',
  4933. '/label',
  4934. '/div',
  4935. array('div' => array('class' => 'checkbox')),
  4936. array('input' => array(
  4937. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4938. 'value' => '1', 'id' => 'CustomId1'
  4939. )),
  4940. array('label' => array('for' => 'CustomId1')),
  4941. 'second',
  4942. '/label',
  4943. '/div',
  4944. array('div' => array('class' => 'checkbox')),
  4945. array('input' => array(
  4946. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4947. 'value' => '2', 'id' => 'CustomId2'
  4948. )),
  4949. array('label' => array('for' => 'CustomId2')),
  4950. 'third',
  4951. '/label',
  4952. '/div'
  4953. );
  4954. $this->assertTags($result, $expected);
  4955. }
  4956. /**
  4957. * testCheckbox method
  4958. *
  4959. * Test generation of checkboxes
  4960. *
  4961. * @return void
  4962. */
  4963. public function testCheckbox() {
  4964. $result = $this->Form->checkbox('Model.field');
  4965. $expected = array(
  4966. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4967. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4968. );
  4969. $this->assertTags($result, $expected);
  4970. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4971. $expected = array(
  4972. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4973. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4974. );
  4975. $this->assertTags($result, $expected);
  4976. $Contact = ClassRegistry::getObject('Contact');
  4977. $Contact->validationErrors['field'] = 1;
  4978. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4979. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4980. $expected = array(
  4981. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4982. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4983. );
  4984. $this->assertTags($result, $expected);
  4985. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4986. $expected = array(
  4987. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4988. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4989. );
  4990. $this->assertTags($result, $expected);
  4991. $this->Form->request->data['Contact']['field'] = '';
  4992. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4993. $expected = array(
  4994. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4995. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4996. );
  4997. $this->assertTags($result, $expected);
  4998. $Contact->validationErrors = array();
  4999. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5000. $expected = array(
  5001. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5002. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5003. );
  5004. $this->assertTags($result, $expected);
  5005. $this->Form->request->data['Contact']['published'] = 1;
  5006. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5007. $expected = array(
  5008. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5009. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. $this->Form->request->data['Contact']['published'] = 0;
  5013. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5014. $expected = array(
  5015. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5016. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5020. $expected = array(
  5021. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5022. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5023. );
  5024. $this->assertTags($result, $expected);
  5025. $result = $this->Form->checkbox('CustomField.1.value');
  5026. $expected = array(
  5027. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5028. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5029. );
  5030. $this->assertTags($result, $expected);
  5031. }
  5032. /**
  5033. * test checkbox() with a custom name attribute
  5034. *
  5035. * @return void
  5036. */
  5037. public function testCheckboxCustomNameAttribute() {
  5038. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5039. $expected = array(
  5040. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5041. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5042. );
  5043. $this->assertTags($result, $expected);
  5044. }
  5045. /**
  5046. * test the checked option for checkboxes.
  5047. *
  5048. * @return void
  5049. */
  5050. public function testCheckboxCheckedOption() {
  5051. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5052. $expected = array(
  5053. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5054. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5055. );
  5056. $this->assertTags($result, $expected);
  5057. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5058. $expected = array(
  5059. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5060. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5061. );
  5062. $this->assertTags($result, $expected);
  5063. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5064. $expected = array(
  5065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5066. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5067. );
  5068. $this->assertTags($result, $expected);
  5069. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5070. $expected = array(
  5071. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5072. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5073. );
  5074. $this->assertTags($result, $expected);
  5075. $this->Form->request->data['Model']['field'] = 1;
  5076. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5077. $expected = array(
  5078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5079. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5080. );
  5081. $this->assertTags($result, $expected);
  5082. }
  5083. /**
  5084. * Test that disabled attribute works on both the checkbox and hidden input.
  5085. *
  5086. * @return void
  5087. */
  5088. public function testCheckboxDisabling() {
  5089. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5090. $expected = array(
  5091. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5092. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5096. $expected = array(
  5097. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5098. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5099. );
  5100. $this->assertTags($result, $expected);
  5101. }
  5102. /**
  5103. * Test that the hidden input for checkboxes can be omitted or set to a
  5104. * specific value.
  5105. *
  5106. * @return void
  5107. */
  5108. public function testCheckboxHiddenField() {
  5109. $result = $this->Form->input('UserForm.something', array(
  5110. 'type' => 'checkbox',
  5111. 'hiddenField' => false
  5112. ));
  5113. $expected = array(
  5114. 'div' => array('class' => 'input checkbox'),
  5115. array('input' => array(
  5116. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5117. 'value' => '1', 'id' => 'UserFormSomething'
  5118. )),
  5119. 'label' => array('for' => 'UserFormSomething'),
  5120. 'Something',
  5121. '/label',
  5122. '/div'
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $result = $this->Form->input('UserForm.something', array(
  5126. 'type' => 'checkbox',
  5127. 'value' => 'Y',
  5128. 'hiddenField' => 'N',
  5129. ));
  5130. $expected = array(
  5131. 'div' => array('class' => 'input checkbox'),
  5132. array('input' => array(
  5133. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5134. 'value' => 'N', 'id' => 'UserFormSomething_'
  5135. )),
  5136. array('input' => array(
  5137. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5138. 'value' => 'Y', 'id' => 'UserFormSomething'
  5139. )),
  5140. 'label' => array('for' => 'UserFormSomething'),
  5141. 'Something',
  5142. '/label',
  5143. '/div'
  5144. );
  5145. $this->assertTags($result, $expected);
  5146. }
  5147. /**
  5148. * testDateTime method
  5149. *
  5150. * Test generation of date/time select elements
  5151. *
  5152. * @return void
  5153. */
  5154. public function testDateTime() {
  5155. extract($this->dateRegex);
  5156. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5157. $now = strtotime('now');
  5158. $expected = array(
  5159. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5160. $daysRegex,
  5161. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5162. date('j', $now),
  5163. '/option',
  5164. '*/select',
  5165. '-',
  5166. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5167. $monthsRegex,
  5168. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5169. date('F', $now),
  5170. '/option',
  5171. '*/select',
  5172. '-',
  5173. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5174. $yearsRegex,
  5175. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5176. date('Y', $now),
  5177. '/option',
  5178. '*/select',
  5179. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5180. $hoursRegex,
  5181. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5182. date('g', $now),
  5183. '/option',
  5184. '*/select',
  5185. ':',
  5186. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5187. $minutesRegex,
  5188. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5189. date('i', $now),
  5190. '/option',
  5191. '*/select',
  5192. ' ',
  5193. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5194. $meridianRegex,
  5195. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5196. date('a', $now),
  5197. '/option',
  5198. '*/select'
  5199. );
  5200. $this->assertTags($result, $expected);
  5201. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5202. $expected = array(
  5203. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5204. $daysRegex,
  5205. array('option' => array('value' => '')),
  5206. '/option',
  5207. '*/select',
  5208. '-',
  5209. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5210. $monthsRegex,
  5211. array('option' => array('value' => '')),
  5212. '/option',
  5213. '*/select',
  5214. '-',
  5215. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5216. $yearsRegex,
  5217. array('option' => array('value' => '')),
  5218. '/option',
  5219. '*/select',
  5220. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5221. $hoursRegex,
  5222. array('option' => array('value' => '')),
  5223. '/option',
  5224. '*/select',
  5225. ':',
  5226. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5227. $minutesRegex,
  5228. array('option' => array('value' => '')),
  5229. '/option',
  5230. '*/select',
  5231. ' ',
  5232. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5233. $meridianRegex,
  5234. array('option' => array('value' => '')),
  5235. '/option',
  5236. '*/select'
  5237. );
  5238. $this->assertTags($result, $expected);
  5239. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5240. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5241. $this->assertTags($result, $expected);
  5242. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5243. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5244. $this->assertTags($result, $expected);
  5245. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5246. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5247. $expected = array(
  5248. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5249. $daysRegex,
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. '*/select',
  5253. '-',
  5254. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5255. $monthsRegex,
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. '*/select',
  5259. '-',
  5260. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5261. $yearsRegex,
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. '*/select',
  5265. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5266. $hoursRegex,
  5267. array('option' => array('value' => '')),
  5268. '/option',
  5269. '*/select',
  5270. ':',
  5271. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5272. $minutesRegex,
  5273. array('option' => array('value' => '')),
  5274. '/option',
  5275. array('option' => array('value' => '00')),
  5276. '00',
  5277. '/option',
  5278. array('option' => array('value' => '05')),
  5279. '05',
  5280. '/option',
  5281. array('option' => array('value' => '10')),
  5282. '10',
  5283. '/option',
  5284. '*/select',
  5285. ' ',
  5286. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5287. $meridianRegex,
  5288. array('option' => array('value' => '')),
  5289. '/option',
  5290. '*/select'
  5291. );
  5292. $this->assertTags($result, $expected);
  5293. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5294. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5295. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5296. $this->Form->request->data['Contact']['data'] = null;
  5297. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5300. $daysRegex,
  5301. array('option' => array('value' => '')),
  5302. '/option',
  5303. '*/select',
  5304. '-',
  5305. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5306. $monthsRegex,
  5307. array('option' => array('value' => '')),
  5308. '/option',
  5309. '*/select',
  5310. '-',
  5311. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5312. $yearsRegex,
  5313. array('option' => array('value' => '')),
  5314. '/option',
  5315. '*/select',
  5316. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5317. $hoursRegex,
  5318. array('option' => array('value' => '')),
  5319. '/option',
  5320. '*/select',
  5321. ':',
  5322. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5323. $minutesRegex,
  5324. array('option' => array('value' => '')),
  5325. '/option',
  5326. '*/select',
  5327. ' ',
  5328. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5329. $meridianRegex,
  5330. array('option' => array('value' => '')),
  5331. '/option',
  5332. '*/select'
  5333. );
  5334. $this->assertTags($result, $expected);
  5335. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5336. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5337. $now = strtotime($this->Form->data['Model']['field']);
  5338. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5339. $expected = array(
  5340. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5341. $daysRegex,
  5342. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5343. date('j', $now),
  5344. '/option',
  5345. '*/select',
  5346. '-',
  5347. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5348. $monthsRegex,
  5349. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5350. date('F', $now),
  5351. '/option',
  5352. '*/select',
  5353. '-',
  5354. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5355. $yearsRegex,
  5356. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5357. date('Y', $now),
  5358. '/option',
  5359. '*/select',
  5360. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5361. $hoursRegex,
  5362. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5363. date('g', $now),
  5364. '/option',
  5365. '*/select',
  5366. ':',
  5367. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5368. $minutesRegex,
  5369. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5370. date('i', $now),
  5371. '/option',
  5372. '*/select',
  5373. ' ',
  5374. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5375. $meridianRegex,
  5376. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5377. date('a', $now),
  5378. '/option',
  5379. '*/select'
  5380. );
  5381. $this->assertTags($result, $expected);
  5382. $selected = strtotime('2008-10-26 12:33:00');
  5383. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5384. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5385. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5386. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5387. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5388. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5389. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5390. $this->Form->create('Contact');
  5391. $result = $this->Form->input('published');
  5392. $now = strtotime('now');
  5393. $expected = array(
  5394. 'div' => array('class' => 'input date'),
  5395. 'label' => array('for' => 'ContactPublishedMonth'),
  5396. 'Published',
  5397. '/label',
  5398. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5399. $monthsRegex,
  5400. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5401. date('F', $now),
  5402. '/option',
  5403. '*/select',
  5404. '-',
  5405. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5406. $daysRegex,
  5407. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5408. date('j', $now),
  5409. '/option',
  5410. '*/select',
  5411. '-',
  5412. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5413. $yearsRegex,
  5414. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5415. date('Y', $now),
  5416. '/option',
  5417. '*/select',
  5418. '/div'
  5419. );
  5420. $this->assertTags($result, $expected);
  5421. $result = $this->Form->input('published2', array('type' => 'date'));
  5422. $now = strtotime('now');
  5423. $expected = array(
  5424. 'div' => array('class' => 'input date'),
  5425. 'label' => array('for' => 'ContactPublished2Month'),
  5426. 'Published2',
  5427. '/label',
  5428. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5429. $monthsRegex,
  5430. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5431. date('F', $now),
  5432. '/option',
  5433. '*/select',
  5434. '-',
  5435. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5436. $daysRegex,
  5437. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5438. date('j', $now),
  5439. '/option',
  5440. '*/select',
  5441. '-',
  5442. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5443. $yearsRegex,
  5444. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5445. date('Y', $now),
  5446. '/option',
  5447. '*/select',
  5448. '/div'
  5449. );
  5450. $this->assertTags($result, $expected);
  5451. $this->Form->create('Contact');
  5452. $result = $this->Form->input('published', array('monthNames' => false));
  5453. $now = strtotime('now');
  5454. $expected = array(
  5455. 'div' => array('class' => 'input date'),
  5456. 'label' => array('for' => 'ContactPublishedMonth'),
  5457. 'Published',
  5458. '/label',
  5459. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5460. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5461. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5462. date('m', $now),
  5463. '/option',
  5464. '*/select',
  5465. '-',
  5466. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5467. $daysRegex,
  5468. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5469. date('j', $now),
  5470. '/option',
  5471. '*/select',
  5472. '-',
  5473. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5474. $yearsRegex,
  5475. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5476. date('Y', $now),
  5477. '/option',
  5478. '*/select',
  5479. '/div'
  5480. );
  5481. $this->assertTags($result, $expected);
  5482. $result = $this->Form->input('published', array(
  5483. 'timeFormat' => 24,
  5484. 'interval' => 5,
  5485. 'selected' => strtotime('2009-09-03 13:37:00'),
  5486. 'type' => 'datetime'
  5487. ));
  5488. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5489. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5490. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5491. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5492. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5493. }
  5494. /**
  5495. * Test dateTime with rounding
  5496. *
  5497. * @return void
  5498. */
  5499. public function testDateTimeRounding() {
  5500. $this->Form->request->data['Contact'] = array(
  5501. 'date' => array(
  5502. 'day' => '13',
  5503. 'month' => '12',
  5504. 'year' => '2010',
  5505. 'hour' => '04',
  5506. 'min' => '19',
  5507. 'meridian' => 'AM'
  5508. )
  5509. );
  5510. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5511. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5512. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5513. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5514. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5515. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5516. }
  5517. /**
  5518. * Test that empty values don't trigger errors.
  5519. *
  5520. * @return void
  5521. */
  5522. public function testDateTimeNoErrorsOnEmptyData() {
  5523. $this->Form->request->data['Contact'] = array(
  5524. 'date' => array(
  5525. 'day' => '',
  5526. 'month' => '',
  5527. 'year' => '',
  5528. 'hour' => '',
  5529. 'min' => '',
  5530. 'meridian' => ''
  5531. )
  5532. );
  5533. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5534. $this->assertNotEmpty($result);
  5535. }
  5536. /**
  5537. * test that datetime() and default values work.
  5538. *
  5539. * @return void
  5540. */
  5541. public function testDatetimeWithDefault() {
  5542. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5543. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5544. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5545. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5546. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5547. 'default' => '2009-06-01 11:15:30'
  5548. ));
  5549. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5550. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5551. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5552. }
  5553. /**
  5554. * test that bogus non-date time data doesn't cause errors.
  5555. *
  5556. * @return void
  5557. */
  5558. public function testDateTimeWithBogusData() {
  5559. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5560. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5561. }
  5562. /**
  5563. * testDateTimeEmptyAsArray
  5564. *
  5565. * @return void
  5566. */
  5567. public function testDateTimeEmptyAsArray() {
  5568. $result = $this->Form->dateTime('Contact.date',
  5569. 'DMY',
  5570. '12',
  5571. array(
  5572. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5573. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5574. )
  5575. )
  5576. );
  5577. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5578. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5579. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5580. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5581. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5582. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5583. $result = $this->Form->dateTime('Contact.date',
  5584. 'DMY',
  5585. '12',
  5586. array(
  5587. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5588. )
  5589. );
  5590. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5591. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5592. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5593. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5594. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5595. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5596. }
  5597. /**
  5598. * testFormDateTimeMulti method
  5599. *
  5600. * test multiple datetime element generation
  5601. *
  5602. * @return void
  5603. */
  5604. public function testFormDateTimeMulti() {
  5605. extract($this->dateRegex);
  5606. $result = $this->Form->dateTime('Contact.1.updated');
  5607. $expected = array(
  5608. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5609. $daysRegex,
  5610. array('option' => array('value' => '')),
  5611. '/option',
  5612. '*/select',
  5613. '-',
  5614. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5615. $monthsRegex,
  5616. array('option' => array('value' => '')),
  5617. '/option',
  5618. '*/select',
  5619. '-',
  5620. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5621. $yearsRegex,
  5622. array('option' => array('value' => '')),
  5623. '/option',
  5624. '*/select',
  5625. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5626. $hoursRegex,
  5627. array('option' => array('value' => '')),
  5628. '/option',
  5629. '*/select',
  5630. ':',
  5631. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5632. $minutesRegex,
  5633. array('option' => array('value' => '')),
  5634. '/option',
  5635. '*/select',
  5636. ' ',
  5637. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5638. $meridianRegex,
  5639. array('option' => array('value' => '')),
  5640. '/option',
  5641. '*/select'
  5642. );
  5643. $this->assertTags($result, $expected);
  5644. $result = $this->Form->dateTime('Contact.2.updated');
  5645. $expected = array(
  5646. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5647. $daysRegex,
  5648. array('option' => array('value' => '')),
  5649. '/option',
  5650. '*/select',
  5651. '-',
  5652. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5653. $monthsRegex,
  5654. array('option' => array('value' => '')),
  5655. '/option',
  5656. '*/select',
  5657. '-',
  5658. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5659. $yearsRegex,
  5660. array('option' => array('value' => '')),
  5661. '/option',
  5662. '*/select',
  5663. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5664. $hoursRegex,
  5665. array('option' => array('value' => '')),
  5666. '/option',
  5667. '*/select',
  5668. ':',
  5669. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5670. $minutesRegex,
  5671. array('option' => array('value' => '')),
  5672. '/option',
  5673. '*/select',
  5674. ' ',
  5675. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5676. $meridianRegex,
  5677. array('option' => array('value' => '')),
  5678. '/option',
  5679. '*/select'
  5680. );
  5681. $this->assertTags($result, $expected);
  5682. }
  5683. /**
  5684. * When changing the date format, the label should always focus the first select box when
  5685. * clicked.
  5686. *
  5687. * @return void
  5688. */
  5689. public function testDateTimeLabelIdMatchesFirstInput() {
  5690. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5691. $this->assertContains('label for="ModelDateMonth"', $result);
  5692. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5693. $this->assertContains('label for="ModelDateDay"', $result);
  5694. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5695. $this->assertContains('label for="ModelDateYear"', $result);
  5696. }
  5697. /**
  5698. * testMonth method
  5699. *
  5700. * @return void
  5701. */
  5702. public function testMonth() {
  5703. $result = $this->Form->month('Model.field');
  5704. $expected = array(
  5705. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5706. array('option' => array('value' => '')),
  5707. '/option',
  5708. array('option' => array('value' => '01')),
  5709. date('F', strtotime('2008-01-01 00:00:00')),
  5710. '/option',
  5711. array('option' => array('value' => '02')),
  5712. date('F', strtotime('2008-02-01 00:00:00')),
  5713. '/option',
  5714. '*/select',
  5715. );
  5716. $this->assertTags($result, $expected);
  5717. $result = $this->Form->month('Model.field', array('empty' => true));
  5718. $expected = array(
  5719. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5720. array('option' => array('value' => '')),
  5721. '/option',
  5722. array('option' => array('value' => '01')),
  5723. date('F', strtotime('2008-01-01 00:00:00')),
  5724. '/option',
  5725. array('option' => array('value' => '02')),
  5726. date('F', strtotime('2008-02-01 00:00:00')),
  5727. '/option',
  5728. '*/select',
  5729. );
  5730. $this->assertTags($result, $expected);
  5731. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5732. $expected = array(
  5733. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5734. array('option' => array('value' => '')),
  5735. '/option',
  5736. array('option' => array('value' => '01')),
  5737. '01',
  5738. '/option',
  5739. array('option' => array('value' => '02')),
  5740. '02',
  5741. '/option',
  5742. '*/select',
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. $monthNames = array(
  5746. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5747. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5748. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5749. $expected = array(
  5750. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5751. array('option' => array('value' => '')),
  5752. '/option',
  5753. array('option' => array('value' => '01')),
  5754. 'Jan',
  5755. '/option',
  5756. array('option' => array('value' => '02')),
  5757. 'Feb',
  5758. '/option',
  5759. '*/select',
  5760. );
  5761. $this->assertTags($result, $expected);
  5762. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5763. $result = $this->Form->month('Project.release');
  5764. $expected = array(
  5765. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5766. array('option' => array('value' => '')),
  5767. '/option',
  5768. array('option' => array('value' => '01')),
  5769. 'January',
  5770. '/option',
  5771. array('option' => array('value' => '02', 'selected' => 'selected')),
  5772. 'February',
  5773. '/option',
  5774. '*/select',
  5775. );
  5776. $this->assertTags($result, $expected);
  5777. }
  5778. /**
  5779. * testDay method
  5780. *
  5781. * @return void
  5782. */
  5783. public function testDay() {
  5784. extract($this->dateRegex);
  5785. $result = $this->Form->day('Model.field', array('value' => false));
  5786. $expected = array(
  5787. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5788. array('option' => array('value' => '')),
  5789. '/option',
  5790. array('option' => array('value' => '01')),
  5791. '1',
  5792. '/option',
  5793. array('option' => array('value' => '02')),
  5794. '2',
  5795. '/option',
  5796. $daysRegex,
  5797. '/select',
  5798. );
  5799. $this->assertTags($result, $expected);
  5800. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5801. $result = $this->Form->day('Model.field');
  5802. $expected = array(
  5803. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5804. array('option' => array('value' => '')),
  5805. '/option',
  5806. array('option' => array('value' => '01')),
  5807. '1',
  5808. '/option',
  5809. array('option' => array('value' => '02')),
  5810. '2',
  5811. '/option',
  5812. $daysRegex,
  5813. array('option' => array('value' => '10', 'selected' => 'selected')),
  5814. '10',
  5815. '/option',
  5816. $daysRegex,
  5817. '/select',
  5818. );
  5819. $this->assertTags($result, $expected);
  5820. $this->Form->request->data['Model']['field'] = '';
  5821. $result = $this->Form->day('Model.field', array('value' => '10'));
  5822. $expected = array(
  5823. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5824. array('option' => array('value' => '')),
  5825. '/option',
  5826. array('option' => array('value' => '01')),
  5827. '1',
  5828. '/option',
  5829. array('option' => array('value' => '02')),
  5830. '2',
  5831. '/option',
  5832. $daysRegex,
  5833. array('option' => array('value' => '10', 'selected' => 'selected')),
  5834. '10',
  5835. '/option',
  5836. $daysRegex,
  5837. '/select',
  5838. );
  5839. $this->assertTags($result, $expected);
  5840. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5841. $result = $this->Form->day('Model.field', array('value' => true));
  5842. $expected = array(
  5843. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5844. array('option' => array('value' => '')),
  5845. '/option',
  5846. array('option' => array('value' => '01')),
  5847. '1',
  5848. '/option',
  5849. array('option' => array('value' => '02')),
  5850. '2',
  5851. '/option',
  5852. $daysRegex,
  5853. array('option' => array('value' => '10', 'selected' => 'selected')),
  5854. '10',
  5855. '/option',
  5856. $daysRegex,
  5857. '/select',
  5858. );
  5859. $this->assertTags($result, $expected);
  5860. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5861. $result = $this->Form->day('Project.release');
  5862. $expected = array(
  5863. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5864. array('option' => array('value' => '')),
  5865. '/option',
  5866. array('option' => array('value' => '01')),
  5867. '1',
  5868. '/option',
  5869. array('option' => array('value' => '02')),
  5870. '2',
  5871. '/option',
  5872. $daysRegex,
  5873. array('option' => array('value' => '10', 'selected' => 'selected')),
  5874. '10',
  5875. '/option',
  5876. $daysRegex,
  5877. '/select',
  5878. );
  5879. $this->assertTags($result, $expected);
  5880. }
  5881. /**
  5882. * testMinute method
  5883. *
  5884. * @return void
  5885. */
  5886. public function testMinute() {
  5887. extract($this->dateRegex);
  5888. $result = $this->Form->minute('Model.field');
  5889. $expected = array(
  5890. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5891. array('option' => array('value' => '')),
  5892. '/option',
  5893. array('option' => array('value' => '00')),
  5894. '00',
  5895. '/option',
  5896. array('option' => array('value' => '01')),
  5897. '01',
  5898. '/option',
  5899. array('option' => array('value' => '02')),
  5900. '02',
  5901. '/option',
  5902. $minutesRegex,
  5903. '/select',
  5904. );
  5905. $this->assertTags($result, $expected);
  5906. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5907. $result = $this->Form->minute('Model.field');
  5908. $expected = array(
  5909. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5910. array('option' => array('value' => '')),
  5911. '/option',
  5912. array('option' => array('value' => '00')),
  5913. '00',
  5914. '/option',
  5915. array('option' => array('value' => '01')),
  5916. '01',
  5917. '/option',
  5918. array('option' => array('value' => '02')),
  5919. '02',
  5920. '/option',
  5921. $minutesRegex,
  5922. array('option' => array('value' => '12', 'selected' => 'selected')),
  5923. '12',
  5924. '/option',
  5925. $minutesRegex,
  5926. '/select',
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $this->Form->request->data['Model']['field'] = '';
  5930. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5931. $expected = array(
  5932. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5933. array('option' => array('value' => '')),
  5934. '/option',
  5935. array('option' => array('value' => '00')),
  5936. '00',
  5937. '/option',
  5938. array('option' => array('value' => '05')),
  5939. '05',
  5940. '/option',
  5941. array('option' => array('value' => '10')),
  5942. '10',
  5943. '/option',
  5944. $minutesRegex,
  5945. '/select',
  5946. );
  5947. $this->assertTags($result, $expected);
  5948. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5949. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5950. $expected = array(
  5951. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5952. array('option' => array('value' => '')),
  5953. '/option',
  5954. array('option' => array('value' => '00')),
  5955. '00',
  5956. '/option',
  5957. array('option' => array('value' => '05')),
  5958. '05',
  5959. '/option',
  5960. array('option' => array('value' => '10', 'selected' => 'selected')),
  5961. '10',
  5962. '/option',
  5963. $minutesRegex,
  5964. '/select',
  5965. );
  5966. $this->assertTags($result, $expected);
  5967. }
  5968. /**
  5969. * testHour method
  5970. *
  5971. * @return void
  5972. */
  5973. public function testHour() {
  5974. extract($this->dateRegex);
  5975. $result = $this->Form->hour('Model.field', false);
  5976. $expected = array(
  5977. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5978. array('option' => array('value' => '')),
  5979. '/option',
  5980. array('option' => array('value' => '01')),
  5981. '1',
  5982. '/option',
  5983. array('option' => array('value' => '02')),
  5984. '2',
  5985. '/option',
  5986. $hoursRegex,
  5987. '/select',
  5988. );
  5989. $this->assertTags($result, $expected);
  5990. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5991. $result = $this->Form->hour('Model.field', false);
  5992. $expected = array(
  5993. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5994. array('option' => array('value' => '')),
  5995. '/option',
  5996. array('option' => array('value' => '01')),
  5997. '1',
  5998. '/option',
  5999. array('option' => array('value' => '02')),
  6000. '2',
  6001. '/option',
  6002. $hoursRegex,
  6003. array('option' => array('value' => '12', 'selected' => 'selected')),
  6004. '12',
  6005. '/option',
  6006. '/select',
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. $this->Form->request->data['Model']['field'] = '';
  6010. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6011. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6012. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6013. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6014. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6015. $result = $this->Form->hour('Model.field', true);
  6016. $expected = array(
  6017. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6018. array('option' => array('value' => '')),
  6019. '/option',
  6020. array('option' => array('value' => '00', 'selected' => 'selected')),
  6021. '0',
  6022. '/option',
  6023. array('option' => array('value' => '01')),
  6024. '1',
  6025. '/option',
  6026. array('option' => array('value' => '02')),
  6027. '2',
  6028. '/option',
  6029. $hoursRegex,
  6030. '/select',
  6031. );
  6032. $this->assertTags($result, $expected);
  6033. unset($this->Form->request->data['Model']['field']);
  6034. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6035. $thisHour = date('H');
  6036. $optValue = date('G');
  6037. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6038. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6039. $result = $this->Form->hour('Model.field', true);
  6040. $expected = array(
  6041. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6042. array('option' => array('value' => '')),
  6043. '/option',
  6044. array('option' => array('value' => '00')),
  6045. '0',
  6046. '/option',
  6047. array('option' => array('value' => '01', 'selected' => 'selected')),
  6048. '1',
  6049. '/option',
  6050. array('option' => array('value' => '02')),
  6051. '2',
  6052. '/option',
  6053. $hoursRegex,
  6054. '/select',
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. }
  6058. /**
  6059. * testYear method
  6060. *
  6061. * @return void
  6062. */
  6063. public function testYear() {
  6064. $result = $this->Form->year('Model.field', 2006, 2007);
  6065. $expected = array(
  6066. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6067. array('option' => array('value' => '')),
  6068. '/option',
  6069. array('option' => array('value' => '2007')),
  6070. '2007',
  6071. '/option',
  6072. array('option' => array('value' => '2006')),
  6073. '2006',
  6074. '/option',
  6075. '/select',
  6076. );
  6077. $this->assertTags($result, $expected);
  6078. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6079. $expected = array(
  6080. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6081. array('option' => array('value' => '')),
  6082. '/option',
  6083. array('option' => array('value' => '2006')),
  6084. '2006',
  6085. '/option',
  6086. array('option' => array('value' => '2007')),
  6087. '2007',
  6088. '/option',
  6089. '/select',
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $this->request->data['Contact']['published'] = '';
  6093. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6094. $expected = array(
  6095. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6096. array('option' => array('value' => '')),
  6097. '/option',
  6098. array('option' => array('value' => '2007')),
  6099. '2007',
  6100. '/option',
  6101. array('option' => array('value' => '2006')),
  6102. '2006',
  6103. '/option',
  6104. '/select',
  6105. );
  6106. $this->assertTags($result, $expected);
  6107. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6108. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6109. $expected = array(
  6110. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6111. array('option' => array('value' => '2007')),
  6112. '2007',
  6113. '/option',
  6114. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6115. '2006',
  6116. '/option',
  6117. '/select',
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $this->Form->request->data['Contact']['published'] = '';
  6121. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6122. $expected = array(
  6123. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6124. array('option' => array('value' => '')),
  6125. '/option',
  6126. array('option' => array('value' => '2007')),
  6127. '2007',
  6128. '/option',
  6129. array('option' => array('value' => '2006')),
  6130. '2006',
  6131. '/option',
  6132. '/select',
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6136. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6137. $expected = array(
  6138. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6139. array('option' => array('value' => '2007')),
  6140. '2007',
  6141. '/option',
  6142. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6143. '2006',
  6144. '/option',
  6145. '/select',
  6146. );
  6147. $this->assertTags($result, $expected);
  6148. $this->Form->request->data['Contact']['published'] = '';
  6149. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6150. $expected = array(
  6151. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6152. array('option' => array('value' => '')),
  6153. '/option',
  6154. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6155. '2007',
  6156. '/option',
  6157. array('option' => array('value' => '2006')),
  6158. '2006',
  6159. '/option',
  6160. '/select',
  6161. );
  6162. $this->assertTags($result, $expected);
  6163. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6164. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6165. $expected = array(
  6166. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6167. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6168. '2007',
  6169. '/option',
  6170. array('option' => array('value' => '2006')),
  6171. '2006',
  6172. '/option',
  6173. '/select',
  6174. );
  6175. $this->assertTags($result, $expected);
  6176. $this->Form->request->data['Contact']['published'] = '';
  6177. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6178. $expected = array(
  6179. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6180. array('option' => array('value' => '2008')),
  6181. '2008',
  6182. '/option',
  6183. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6184. '2007',
  6185. '/option',
  6186. array('option' => array('value' => '2006')),
  6187. '2006',
  6188. '/option',
  6189. '/select',
  6190. );
  6191. $this->assertTags($result, $expected);
  6192. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6193. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6194. $expected = array(
  6195. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6196. array('option' => array('value' => '2008')),
  6197. '2008',
  6198. '/option',
  6199. array('option' => array('value' => '2007')),
  6200. '2007',
  6201. '/option',
  6202. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6203. '2006',
  6204. '/option',
  6205. '/select',
  6206. );
  6207. $this->assertTags($result, $expected);
  6208. $this->Form->request->data = array();
  6209. $this->Form->create('Contact');
  6210. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6211. $expected = array(
  6212. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6213. array('option' => array('value' => '2008')),
  6214. '2008',
  6215. '/option',
  6216. array('option' => array('value' => '2007')),
  6217. '2007',
  6218. '/option',
  6219. array('option' => array('value' => '2006')),
  6220. '2006',
  6221. '/option',
  6222. '/select',
  6223. );
  6224. $this->assertTags($result, $expected);
  6225. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6226. $this->assertContains('data[Contact][published][year]', $result);
  6227. }
  6228. /**
  6229. * testYearAutoExpandRange method
  6230. *
  6231. * @return void
  6232. */
  6233. public function testYearAutoExpandRange() {
  6234. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6235. $result = $this->Form->year('User.birthday');
  6236. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6237. $result = $matches[1];
  6238. $expected = range(date('Y') + 20, 1930);
  6239. $this->assertEquals($result, $expected);
  6240. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6241. $result = $this->Form->year('Project.release');
  6242. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6243. $result = $matches[1];
  6244. $expected = range(2050, date('Y') - 20);
  6245. $this->assertEquals($result, $expected);
  6246. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6247. $result = $this->Form->year('Project.release', 1890, 1900);
  6248. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6249. $result = $matches[1];
  6250. $expected = range(1900, 1881);
  6251. $this->assertEquals($result, $expected);
  6252. }
  6253. /**
  6254. * testInputDate method
  6255. *
  6256. * Test various inputs with type date and different dateFormat values.
  6257. * Failing to provide a dateFormat key should not error.
  6258. * It should simply not pre-select any value then.
  6259. *
  6260. * @return void
  6261. */
  6262. public function testInputDate() {
  6263. $this->Form->request->data = array(
  6264. 'User' => array(
  6265. 'month_year' => array('month' => date('m')),
  6266. 'just_year' => array('month' => date('m')),
  6267. 'just_month' => array('year' => date('Y')),
  6268. 'just_day' => array('month' => date('m')),
  6269. )
  6270. );
  6271. $this->Form->create('User');
  6272. $result = $this->Form->input('month_year',
  6273. array(
  6274. 'label' => false,
  6275. 'div' => false,
  6276. 'type' => 'date',
  6277. 'dateFormat' => 'MY',
  6278. 'minYear' => 2006,
  6279. 'maxYear' => 2008
  6280. )
  6281. );
  6282. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6283. $this->assertNotContains('value="2008" selected="selected"', $result);
  6284. $result = $this->Form->input('just_year',
  6285. array(
  6286. 'type' => 'date',
  6287. 'label' => false,
  6288. 'dateFormat' => 'Y',
  6289. 'minYear' => date('Y'),
  6290. 'maxYear' => date('Y', strtotime('+20 years'))
  6291. )
  6292. );
  6293. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6294. $result = $this->Form->input('just_month',
  6295. array(
  6296. 'type' => 'date',
  6297. 'label' => false,
  6298. 'dateFormat' => 'M',
  6299. 'empty' => false,
  6300. )
  6301. );
  6302. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6303. $result = $this->Form->input('just_day',
  6304. array(
  6305. 'type' => 'date',
  6306. 'label' => false,
  6307. 'dateFormat' => 'D',
  6308. 'empty' => false,
  6309. )
  6310. );
  6311. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6312. }
  6313. /**
  6314. * testInputDateMaxYear method
  6315. *
  6316. * Let's say we want to only allow users born from 2006 to 2008 to register
  6317. * This being the first singup page, we still don't have any data
  6318. *
  6319. * @return void
  6320. */
  6321. public function testInputDateMaxYear() {
  6322. $this->Form->request->data = array();
  6323. $this->Form->create('User');
  6324. $result = $this->Form->input('birthday',
  6325. array(
  6326. 'label' => false,
  6327. 'div' => false,
  6328. 'type' => 'date',
  6329. 'dateFormat' => 'DMY',
  6330. 'minYear' => 2006,
  6331. 'maxYear' => 2008
  6332. )
  6333. );
  6334. $this->assertContains('value="2008" selected="selected"', $result);
  6335. }
  6336. /**
  6337. * testTextArea method
  6338. *
  6339. * @return void
  6340. */
  6341. public function testTextArea() {
  6342. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6343. $result = $this->Form->textarea('Model.field');
  6344. $expected = array(
  6345. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6346. 'some test data',
  6347. '/textarea',
  6348. );
  6349. $this->assertTags($result, $expected);
  6350. $result = $this->Form->textarea('Model.tmp');
  6351. $expected = array(
  6352. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6353. '/textarea',
  6354. );
  6355. $this->assertTags($result, $expected);
  6356. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6357. $result = $this->Form->textarea('Model.field');
  6358. $expected = array(
  6359. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6360. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6361. '/textarea',
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6365. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6366. $expected = array(
  6367. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6368. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6369. '/textarea',
  6370. );
  6371. $this->assertTags($result, $expected);
  6372. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6373. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6374. $expected = array(
  6375. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6376. '/textarea'
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. }
  6380. /**
  6381. * testTextAreaWithStupidCharacters method
  6382. *
  6383. * test text area with non-ascii characters
  6384. *
  6385. * @return void
  6386. */
  6387. public function testTextAreaWithStupidCharacters() {
  6388. $this->loadFixtures('Post');
  6389. $result = $this->Form->input('Post.content', array(
  6390. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6391. ));
  6392. $expected = array(
  6393. 'div' => array('class' => 'input textarea'),
  6394. 'label' => array('for' => 'PostContent'),
  6395. 'Current Text',
  6396. '/label',
  6397. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6398. 'GREAT®',
  6399. '/textarea',
  6400. '/div'
  6401. );
  6402. $this->assertTags($result, $expected);
  6403. }
  6404. /**
  6405. * testHiddenField method
  6406. *
  6407. * @return void
  6408. */
  6409. public function testHiddenField() {
  6410. $Contact = ClassRegistry::getObject('Contact');
  6411. $Contact->validationErrors['field'] = 1;
  6412. $this->Form->request->data['Contact']['field'] = 'test';
  6413. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6414. $this->assertTags($result, array(
  6415. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6416. );
  6417. }
  6418. /**
  6419. * testFileUploadField method
  6420. *
  6421. * @return void
  6422. */
  6423. public function testFileUploadField() {
  6424. $result = $this->Form->file('Model.upload');
  6425. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6426. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6427. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6428. $expected = array(
  6429. 'div' => array('class' => 'input file'),
  6430. 'label' => array('for' => 'ModelUpload'),
  6431. 'Upload',
  6432. '/label',
  6433. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6434. '/div'
  6435. );
  6436. $this->assertTags($result, $expected);
  6437. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6438. $result = $this->Form->file('Model.upload');
  6439. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6440. }
  6441. /**
  6442. * test File upload input on a model not used in create();
  6443. *
  6444. * @return void
  6445. */
  6446. public function testFileUploadOnOtherModel() {
  6447. $this->Form->create('ValidateUser', array('type' => 'file'));
  6448. $result = $this->Form->file('ValidateProfile.city');
  6449. $expected = array(
  6450. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. }
  6454. /**
  6455. * testButton method
  6456. *
  6457. * @return void
  6458. */
  6459. public function testButton() {
  6460. $result = $this->Form->button('Hi');
  6461. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6462. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6463. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6464. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6465. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6466. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6467. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6468. $result = $this->Form->button('No type', array('type' => false));
  6469. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6470. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6471. $this->assertNotRegExp('/\&039/', $result);
  6472. }
  6473. /**
  6474. * Test that button() makes unlocked fields by default.
  6475. *
  6476. * @return void
  6477. */
  6478. public function testButtonUnlockedByDefault() {
  6479. $this->Form->request->params['_Token']['key'] = 'secured';
  6480. $this->Form->button('Save', array('name' => 'save'));
  6481. $this->Form->button('Clear');
  6482. $result = $this->Form->unlockField();
  6483. $this->assertEquals(array('save'), $result);
  6484. }
  6485. /**
  6486. * testPostButton method
  6487. *
  6488. * @return void
  6489. */
  6490. public function testPostButton() {
  6491. $result = $this->Form->postButton('Hi', '/controller/action');
  6492. $this->assertTags($result, array(
  6493. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6494. 'div' => array('style' => 'display:none;'),
  6495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6496. '/div',
  6497. 'button' => array('type' => 'submit'),
  6498. 'Hi',
  6499. '/button',
  6500. '/form'
  6501. ));
  6502. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6503. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6504. }
  6505. /**
  6506. * Test that postButton adds _Token fields.
  6507. *
  6508. * @return void
  6509. */
  6510. public function testSecurePostButton() {
  6511. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6512. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6513. $expected = array(
  6514. 'form' => array(
  6515. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6516. ),
  6517. array('div' => array('style' => 'display:none;')),
  6518. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6519. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6520. '/div',
  6521. 'button' => array('type' => 'submit'),
  6522. 'Delete',
  6523. '/button',
  6524. array('div' => array('style' => 'display:none;')),
  6525. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6526. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6527. '/div',
  6528. '/form',
  6529. );
  6530. $this->assertTags($result, $expected);
  6531. }
  6532. /**
  6533. * testPostLink method
  6534. *
  6535. * @return void
  6536. */
  6537. public function testPostLink() {
  6538. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6539. $this->assertTags($result, array(
  6540. 'form' => array(
  6541. 'method' => 'post', 'action' => '/posts/delete/1',
  6542. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6543. ),
  6544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6545. '/form',
  6546. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6547. 'Delete',
  6548. '/a'
  6549. ));
  6550. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6551. $this->assertTags($result, array(
  6552. 'form' => array(
  6553. 'method' => 'post', 'action' => '/posts/delete/1',
  6554. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6555. ),
  6556. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6557. '/form',
  6558. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6559. 'Delete',
  6560. '/a'
  6561. ));
  6562. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6563. $this->assertTags($result, array(
  6564. 'form' => array(
  6565. 'method' => 'post', 'action' => '/posts/delete/1',
  6566. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6567. ),
  6568. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6569. '/form',
  6570. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6571. 'Delete',
  6572. '/a'
  6573. ));
  6574. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6575. $this->assertTags($result, array(
  6576. 'form' => array(
  6577. 'method' => 'post', 'action' => '/posts/delete/1',
  6578. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6579. ),
  6580. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6581. '/form',
  6582. '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;/'),
  6583. 'Delete',
  6584. '/a'
  6585. ));
  6586. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6587. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6588. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6589. $this->assertTags($result, array(
  6590. 'form' => array(
  6591. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6592. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6593. ),
  6594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6595. '/form',
  6596. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6597. 'Delete',
  6598. '/a'
  6599. ));
  6600. $result = $this->Form->postLink(
  6601. '',
  6602. array('controller' => 'items', 'action' => 'delete', 10),
  6603. array('class' => 'btn btn-danger', 'escape' => false),
  6604. 'Confirm thing'
  6605. );
  6606. $this->assertTags($result, array(
  6607. 'form' => array(
  6608. 'method' => 'post', 'action' => '/items/delete/10',
  6609. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6610. ),
  6611. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6612. '/form',
  6613. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6614. '/a'
  6615. ));
  6616. }
  6617. /**
  6618. * Test that postLink adds _Token fields.
  6619. *
  6620. * @return void
  6621. */
  6622. public function testSecurePostLink() {
  6623. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6624. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6625. $expected = array(
  6626. 'form' => array(
  6627. 'method' => 'post', 'action' => '/posts/delete/1',
  6628. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6629. ),
  6630. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6631. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6632. 'div' => array('style' => 'display:none;'),
  6633. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6634. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6635. '/div',
  6636. '/form',
  6637. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6638. 'Delete',
  6639. '/a'
  6640. );
  6641. $this->assertTags($result, $expected);
  6642. }
  6643. /**
  6644. * testSubmitButton method
  6645. *
  6646. * @return void
  6647. */
  6648. public function testSubmitButton() {
  6649. $result = $this->Form->submit('');
  6650. $expected = array(
  6651. 'div' => array('class' => 'submit'),
  6652. 'input' => array('type' => 'submit', 'value' => ''),
  6653. '/div'
  6654. );
  6655. $this->assertTags($result, $expected);
  6656. $result = $this->Form->submit('Test Submit');
  6657. $expected = array(
  6658. 'div' => array('class' => 'submit'),
  6659. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6660. '/div'
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6664. $expected = array(
  6665. 'span' => array('class' => 'submit'),
  6666. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6667. '/span'
  6668. );
  6669. $this->assertTags($result, $expected);
  6670. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6671. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6672. $this->assertTags($result, $expected);
  6673. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6674. $expected = array(
  6675. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6676. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6677. '/div'
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. $result = $this->Form->submit('Next >');
  6681. $expected = array(
  6682. 'div' => array('class' => 'submit'),
  6683. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6684. '/div'
  6685. );
  6686. $this->assertTags($result, $expected);
  6687. $result = $this->Form->submit('Next >', array('escape' => false));
  6688. $expected = array(
  6689. 'div' => array('class' => 'submit'),
  6690. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6691. '/div'
  6692. );
  6693. $this->assertTags($result, $expected);
  6694. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6695. $expected = array(
  6696. 'div' => array('class' => 'submit'),
  6697. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6698. '/div'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. $before = '--before--';
  6702. $after = '--after--';
  6703. $result = $this->Form->submit('Test', array('before' => $before));
  6704. $expected = array(
  6705. 'div' => array('class' => 'submit'),
  6706. '--before--',
  6707. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6708. '/div'
  6709. );
  6710. $this->assertTags($result, $expected);
  6711. $result = $this->Form->submit('Test', array('after' => $after));
  6712. $expected = array(
  6713. 'div' => array('class' => 'submit'),
  6714. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6715. '--after--',
  6716. '/div'
  6717. );
  6718. $this->assertTags($result, $expected);
  6719. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6720. $expected = array(
  6721. 'div' => array('class' => 'submit'),
  6722. '--before--',
  6723. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6724. '--after--',
  6725. '/div'
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. }
  6729. /**
  6730. * test image submit types.
  6731. *
  6732. * @return void
  6733. */
  6734. public function testSubmitImage() {
  6735. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6736. $expected = array(
  6737. 'div' => array('class' => 'submit'),
  6738. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6739. '/div'
  6740. );
  6741. $this->assertTags($result, $expected);
  6742. $result = $this->Form->submit('/relative/cake.power.gif');
  6743. $expected = array(
  6744. 'div' => array('class' => 'submit'),
  6745. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6746. '/div'
  6747. );
  6748. $this->assertTags($result, $expected);
  6749. $result = $this->Form->submit('cake.power.gif');
  6750. $expected = array(
  6751. 'div' => array('class' => 'submit'),
  6752. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6753. '/div'
  6754. );
  6755. $this->assertTags($result, $expected);
  6756. $result = $this->Form->submit('Not.an.image');
  6757. $expected = array(
  6758. 'div' => array('class' => 'submit'),
  6759. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6760. '/div'
  6761. );
  6762. $this->assertTags($result, $expected);
  6763. $after = '--after--';
  6764. $before = '--before--';
  6765. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6766. $expected = array(
  6767. 'div' => array('class' => 'submit'),
  6768. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6769. '--after--',
  6770. '/div'
  6771. );
  6772. $this->assertTags($result, $expected);
  6773. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6774. $expected = array(
  6775. 'div' => array('class' => 'submit'),
  6776. '--before--',
  6777. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6782. $expected = array(
  6783. 'div' => array('class' => 'submit'),
  6784. '--before--',
  6785. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6786. '--after--',
  6787. '/div'
  6788. );
  6789. $this->assertTags($result, $expected);
  6790. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6791. $expected = array(
  6792. 'div' => array('class' => 'submit'),
  6793. '--before--',
  6794. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6795. '--after--',
  6796. '/div'
  6797. );
  6798. $this->assertTags($result, $expected);
  6799. }
  6800. /**
  6801. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6802. * be submitted at a time.
  6803. *
  6804. * @return void
  6805. */
  6806. public function testSubmitUnlockedByDefault() {
  6807. $this->Form->request->params['_Token']['key'] = 'secured';
  6808. $this->Form->submit('Go go');
  6809. $this->Form->submit('Save', array('name' => 'save'));
  6810. $result = $this->Form->unlockField();
  6811. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6812. }
  6813. /**
  6814. * Test submit image with timestamps.
  6815. *
  6816. * @return void
  6817. */
  6818. public function testSubmitImageTimestamp() {
  6819. Configure::write('Asset.timestamp', 'force');
  6820. $result = $this->Form->submit('cake.power.gif');
  6821. $expected = array(
  6822. 'div' => array('class' => 'submit'),
  6823. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6824. '/div'
  6825. );
  6826. $this->assertTags($result, $expected);
  6827. }
  6828. /**
  6829. * test the create() method
  6830. *
  6831. * @return void
  6832. */
  6833. public function testCreate() {
  6834. $result = $this->Form->create('Contact');
  6835. $encoding = strtolower(Configure::read('App.encoding'));
  6836. $expected = array(
  6837. 'form' => array(
  6838. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6839. 'accept-charset' => $encoding
  6840. ),
  6841. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6842. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6843. '/div'
  6844. );
  6845. $this->assertTags($result, $expected);
  6846. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6847. $expected = array('form' => array(
  6848. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6849. 'accept-charset' => $encoding
  6850. ));
  6851. $this->assertTags($result, $expected);
  6852. $result = $this->Form->create('Contact', array('type' => 'get'));
  6853. $expected = array('form' => array(
  6854. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6855. 'accept-charset' => $encoding
  6856. ));
  6857. $this->assertTags($result, $expected);
  6858. $result = $this->Form->create('Contact', array('type' => 'put'));
  6859. $expected = array(
  6860. 'form' => array(
  6861. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6862. 'accept-charset' => $encoding
  6863. ),
  6864. 'div' => array('style' => 'display:none;'),
  6865. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6866. '/div'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $result = $this->Form->create('Contact', array('type' => 'file'));
  6870. $expected = array(
  6871. 'form' => array(
  6872. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6873. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6874. ),
  6875. 'div' => array('style' => 'display:none;'),
  6876. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6877. '/div'
  6878. );
  6879. $this->assertTags($result, $expected);
  6880. $this->Form->request->data['Contact']['id'] = 1;
  6881. $this->Form->request->here = '/contacts/edit/1';
  6882. $this->Form->request['action'] = 'edit';
  6883. $result = $this->Form->create('Contact');
  6884. $expected = array(
  6885. 'form' => array(
  6886. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6887. 'accept-charset' => $encoding
  6888. ),
  6889. 'div' => array('style' => 'display:none;'),
  6890. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6891. '/div'
  6892. );
  6893. $this->assertTags($result, $expected);
  6894. $this->Form->request->data['Contact']['id'] = 1;
  6895. $this->Form->request->here = '/contacts/edit/1';
  6896. $this->Form->request['action'] = 'edit';
  6897. $result = $this->Form->create('Contact', array('type' => 'file'));
  6898. $expected = array(
  6899. 'form' => array(
  6900. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6901. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6902. ),
  6903. 'div' => array('style' => 'display:none;'),
  6904. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6905. '/div'
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6909. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6910. $expected = array(
  6911. 'form' => array(
  6912. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6913. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6914. ),
  6915. 'div' => array('style' => 'display:none;'),
  6916. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6917. '/div'
  6918. );
  6919. $this->assertTags($result, $expected);
  6920. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6921. $expected = array(
  6922. 'form' => array(
  6923. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6924. 'accept-charset' => $encoding
  6925. ),
  6926. 'div' => array('style' => 'display:none;'),
  6927. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6928. '/div'
  6929. );
  6930. $this->assertTags($result, $expected);
  6931. $this->Form->request['action'] = 'add';
  6932. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6933. $expected = array(
  6934. 'form' => array(
  6935. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6936. 'accept-charset' => $encoding
  6937. ),
  6938. 'div' => array('style' => 'display:none;'),
  6939. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6940. '/div'
  6941. );
  6942. $this->assertTags($result, $expected);
  6943. $result = $this->Form->create('User', array('action' => 'login'));
  6944. $expected = array(
  6945. 'form' => array(
  6946. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6947. 'accept-charset' => $encoding
  6948. ),
  6949. 'div' => array('style' => 'display:none;'),
  6950. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6951. '/div'
  6952. );
  6953. $this->assertTags($result, $expected);
  6954. $result = $this->Form->create('User', array('url' => '/users/login'));
  6955. $expected = array(
  6956. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6957. 'div' => array('style' => 'display:none;'),
  6958. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6959. '/div'
  6960. );
  6961. $this->assertTags($result, $expected);
  6962. $this->Form->request['controller'] = 'pages';
  6963. $result = $this->Form->create('User', array('action' => 'signup'));
  6964. $expected = array(
  6965. 'form' => array(
  6966. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6967. 'accept-charset' => $encoding
  6968. ),
  6969. 'div' => array('style' => 'display:none;'),
  6970. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6971. '/div'
  6972. );
  6973. $this->assertTags($result, $expected);
  6974. $this->Form->request->data = array();
  6975. $this->Form->request['controller'] = 'contacts';
  6976. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6977. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6978. $expected = array(
  6979. 'form' => array(
  6980. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6981. 'accept-charset' => 'utf-8'
  6982. ),
  6983. 'div' => array('style' => 'display:none;'),
  6984. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6985. '/div'
  6986. );
  6987. $this->assertTags($result, $expected);
  6988. }
  6989. /**
  6990. * Test the onsubmit option for create()
  6991. *
  6992. * @return void
  6993. */
  6994. public function testCreateOnSubmit() {
  6995. $this->Form->request->data = array();
  6996. $this->Form->request['controller'] = 'contacts';
  6997. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6998. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6999. $expected = array(
  7000. 'form' => array(
  7001. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7002. 'accept-charset' => 'utf-8'
  7003. ),
  7004. 'div' => array('style' => 'display:none;'),
  7005. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7006. '/div'
  7007. );
  7008. $this->assertTags($result, $expected);
  7009. $this->Form->request->data = array();
  7010. $this->Form->request['controller'] = 'contacts';
  7011. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7012. $result = $this->Form->create(array(
  7013. 'url' => array('action' => 'index', 'param'),
  7014. 'default' => false,
  7015. 'onsubmit' => 'someFunction();'
  7016. ));
  7017. $expected = array(
  7018. 'form' => array(
  7019. 'id' => 'ContactAddForm', 'method' => 'post',
  7020. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7021. 'action' => '/contacts/index/param',
  7022. 'accept-charset' => 'utf-8'
  7023. ),
  7024. 'div' => array('style' => 'display:none;'),
  7025. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7026. '/div'
  7027. );
  7028. $this->assertTags($result, $expected);
  7029. }
  7030. /**
  7031. * test create() with automatic url generation
  7032. *
  7033. * @return void
  7034. */
  7035. public function testCreateAutoUrl() {
  7036. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7037. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7038. $this->Form->request->base = '/base_url';
  7039. $result = $this->Form->create('Contact');
  7040. $expected = array(
  7041. 'form' => array(
  7042. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7043. 'accept-charset' => 'utf-8'
  7044. ),
  7045. 'div' => array('style' => 'display:none;'),
  7046. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7047. '/div'
  7048. );
  7049. $this->assertTags($result, $expected);
  7050. $this->Form->request['action'] = 'delete';
  7051. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7052. $this->Form->request->base = '/base_url';
  7053. $result = $this->Form->create('Contact');
  7054. $expected = array(
  7055. 'form' => array(
  7056. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7057. 'accept-charset' => 'utf-8'
  7058. ),
  7059. 'div' => array('style' => 'display:none;'),
  7060. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7061. '/div'
  7062. );
  7063. $this->assertTags($result, $expected);
  7064. }
  7065. /**
  7066. * test create() with a custom route
  7067. *
  7068. * @return void
  7069. */
  7070. public function testCreateCustomRoute() {
  7071. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7072. $encoding = strtolower(Configure::read('App.encoding'));
  7073. $result = $this->Form->create('User', array('action' => 'login'));
  7074. $expected = array(
  7075. 'form' => array(
  7076. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7077. 'accept-charset' => $encoding
  7078. ),
  7079. 'div' => array('style' => 'display:none;'),
  7080. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7081. '/div'
  7082. );
  7083. $this->assertTags($result, $expected);
  7084. }
  7085. /**
  7086. * test that inputDefaults are stored and used.
  7087. *
  7088. * @return void
  7089. */
  7090. public function testCreateWithInputDefaults() {
  7091. $this->Form->create('User', array(
  7092. 'inputDefaults' => array(
  7093. 'div' => false,
  7094. 'label' => false,
  7095. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7096. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7097. )
  7098. ));
  7099. $result = $this->Form->input('username');
  7100. $expected = array(
  7101. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7102. );
  7103. $this->assertTags($result, $expected);
  7104. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7105. $expected = array(
  7106. 'div' => array('class' => 'input text'),
  7107. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7108. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7109. '/div'
  7110. );
  7111. $this->assertTags($result, $expected);
  7112. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7113. $expected = array(
  7114. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7115. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7116. );
  7117. $this->assertTags($result, $expected);
  7118. $this->Form->create('User', array(
  7119. 'inputDefaults' => array(
  7120. 'div' => false,
  7121. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7122. )
  7123. ));
  7124. $result = $this->Form->input('username', array('div' => true));
  7125. $expected = array(
  7126. 'div' => array('class' => 'input text'),
  7127. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7128. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7129. '/div'
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. }
  7133. /**
  7134. * test automatic accept-charset overriding
  7135. *
  7136. * @return void
  7137. */
  7138. public function testCreateWithAcceptCharset() {
  7139. $result = $this->Form->create('UserForm', array(
  7140. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7141. )
  7142. );
  7143. $expected = array(
  7144. 'form' => array(
  7145. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7146. 'accept-charset' => 'iso-8859-1'
  7147. ),
  7148. 'div' => array('style' => 'display:none;'),
  7149. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7150. '/div'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. }
  7154. /**
  7155. * Test base form url when url param is passed with multiple parameters (&)
  7156. *
  7157. */
  7158. public function testCreateQuerystringrequest() {
  7159. $encoding = strtolower(Configure::read('App.encoding'));
  7160. $result = $this->Form->create('Contact', array(
  7161. 'type' => 'post',
  7162. 'escape' => false,
  7163. 'url' => array(
  7164. 'controller' => 'controller',
  7165. 'action' => 'action',
  7166. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7167. )
  7168. ));
  7169. $expected = array(
  7170. 'form' => array(
  7171. 'id' => 'ContactAddForm',
  7172. 'method' => 'post',
  7173. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7174. 'accept-charset' => $encoding
  7175. ),
  7176. 'div' => array('style' => 'display:none;'),
  7177. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7178. '/div'
  7179. );
  7180. $this->assertTags($result, $expected);
  7181. $result = $this->Form->create('Contact', array(
  7182. 'type' => 'post',
  7183. 'url' => array(
  7184. 'controller' => 'controller',
  7185. 'action' => 'action',
  7186. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7187. )
  7188. ));
  7189. $expected = array(
  7190. 'form' => array(
  7191. 'id' => 'ContactAddForm',
  7192. 'method' => 'post',
  7193. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7194. 'accept-charset' => $encoding
  7195. ),
  7196. 'div' => array('style' => 'display:none;'),
  7197. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7198. '/div'
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. }
  7202. /**
  7203. * test that create() doesn't cause errors by multiple id's being in the primary key
  7204. * as could happen with multiple select or checkboxes.
  7205. *
  7206. * @return void
  7207. */
  7208. public function testCreateWithMultipleIdInData() {
  7209. $encoding = strtolower(Configure::read('App.encoding'));
  7210. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7211. $result = $this->Form->create('Contact');
  7212. $expected = array(
  7213. 'form' => array(
  7214. 'id' => 'ContactAddForm',
  7215. 'method' => 'post',
  7216. 'action' => '/contacts/add',
  7217. 'accept-charset' => $encoding
  7218. ),
  7219. 'div' => array('style' => 'display:none;'),
  7220. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7221. '/div'
  7222. );
  7223. $this->assertTags($result, $expected);
  7224. }
  7225. /**
  7226. * test that create() doesn't add in extra passed params.
  7227. *
  7228. * @return void
  7229. */
  7230. public function testCreatePassedArgs() {
  7231. $encoding = strtolower(Configure::read('App.encoding'));
  7232. $this->Form->request->data['Contact']['id'] = 1;
  7233. $result = $this->Form->create('Contact', array(
  7234. 'type' => 'post',
  7235. 'escape' => false,
  7236. 'url' => array(
  7237. 'action' => 'edit',
  7238. 'myparam'
  7239. )
  7240. ));
  7241. $expected = array(
  7242. 'form' => array(
  7243. 'id' => 'ContactAddForm',
  7244. 'method' => 'post',
  7245. 'action' => '/contacts/edit/myparam',
  7246. 'accept-charset' => $encoding
  7247. ),
  7248. 'div' => array('style' => 'display:none;'),
  7249. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7250. '/div'
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. }
  7254. /**
  7255. * test creating a get form, and get form inputs.
  7256. *
  7257. * @return void
  7258. */
  7259. public function testGetFormCreate() {
  7260. $encoding = strtolower(Configure::read('App.encoding'));
  7261. $result = $this->Form->create('Contact', array('type' => 'get'));
  7262. $this->assertTags($result, array('form' => array(
  7263. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7264. 'accept-charset' => $encoding
  7265. )));
  7266. $result = $this->Form->text('Contact.name');
  7267. $this->assertTags($result, array('input' => array(
  7268. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7269. )));
  7270. $result = $this->Form->password('password');
  7271. $this->assertTags($result, array('input' => array(
  7272. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7273. )));
  7274. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7275. $result = $this->Form->text('user_form');
  7276. $this->assertTags($result, array('input' => array(
  7277. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7278. )));
  7279. }
  7280. /**
  7281. * test get form, and inputs when the model param is false
  7282. *
  7283. * @return void
  7284. */
  7285. public function testGetFormWithFalseModel() {
  7286. $encoding = strtolower(Configure::read('App.encoding'));
  7287. $this->Form->request['controller'] = 'contact_test';
  7288. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7289. $expected = array('form' => array(
  7290. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7291. 'accept-charset' => $encoding
  7292. ));
  7293. $this->assertTags($result, $expected);
  7294. $result = $this->Form->text('reason');
  7295. $expected = array(
  7296. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. }
  7300. /**
  7301. * test that datetime() works with GET style forms.
  7302. *
  7303. * @return void
  7304. */
  7305. public function testDateTimeWithGetForms() {
  7306. extract($this->dateRegex);
  7307. $this->Form->create('Contact', array('type' => 'get'));
  7308. $result = $this->Form->datetime('created');
  7309. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7310. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7311. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7312. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7313. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7314. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7315. }
  7316. /**
  7317. * testEditFormWithData method
  7318. *
  7319. * test auto populating form elements from submitted data.
  7320. *
  7321. * @return void
  7322. */
  7323. public function testEditFormWithData() {
  7324. $this->Form->request->data = array('Person' => array(
  7325. 'id' => 1,
  7326. 'first_name' => 'Nate',
  7327. 'last_name' => 'Abele',
  7328. 'email' => 'nate@example.com'
  7329. ));
  7330. $this->Form->request->addParams(array(
  7331. 'models' => array('Person'),
  7332. 'controller' => 'people',
  7333. 'action' => 'add'
  7334. ));
  7335. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7336. $this->Form->create();
  7337. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7338. $expected = array(
  7339. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7340. 'select' => array(
  7341. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7342. ),
  7343. array('option' => array('value' => 1)), 'Nate', '/option',
  7344. array('option' => array('value' => 2)), 'Garrett', '/option',
  7345. array('option' => array('value' => 3)), 'Larry', '/option',
  7346. '/select'
  7347. );
  7348. $this->assertTags($result, $expected);
  7349. }
  7350. /**
  7351. * Test that required fields are created for various types of validation.
  7352. *
  7353. * @return void
  7354. */
  7355. public function testFormInputRequiredDetection() {
  7356. $this->Form->create('Contact');
  7357. $result = $this->Form->input('Contact.non_existing');
  7358. $expected = array(
  7359. 'div' => array('class' => 'input text'),
  7360. 'label' => array('for' => 'ContactNonExisting'),
  7361. 'Non Existing',
  7362. '/label',
  7363. 'input' => array(
  7364. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7365. 'id' => 'ContactNonExisting'
  7366. ),
  7367. '/div'
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. $result = $this->Form->input('Contact.imrequired');
  7371. $expected = array(
  7372. 'div' => array('class' => 'input text required'),
  7373. 'label' => array('for' => 'ContactImrequired'),
  7374. 'Imrequired',
  7375. '/label',
  7376. 'input' => array(
  7377. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7378. 'id' => 'ContactImrequired',
  7379. 'required' => 'required'
  7380. ),
  7381. '/div'
  7382. );
  7383. $this->assertTags($result, $expected);
  7384. $result = $this->Form->input('Contact.imalsorequired');
  7385. $expected = array(
  7386. 'div' => array('class' => 'input text required'),
  7387. 'label' => array('for' => 'ContactImalsorequired'),
  7388. 'Imalsorequired',
  7389. '/label',
  7390. 'input' => array(
  7391. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7392. 'id' => 'ContactImalsorequired',
  7393. 'required' => 'required'
  7394. ),
  7395. '/div'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->input('Contact.imrequiredtoo');
  7399. $expected = array(
  7400. 'div' => array('class' => 'input text required'),
  7401. 'label' => array('for' => 'ContactImrequiredtoo'),
  7402. 'Imrequiredtoo',
  7403. '/label',
  7404. 'input' => array(
  7405. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7406. 'id' => 'ContactImrequiredtoo',
  7407. 'required' => 'required'
  7408. ),
  7409. '/div'
  7410. );
  7411. $this->assertTags($result, $expected);
  7412. $result = $this->Form->input('Contact.required_one');
  7413. $expected = array(
  7414. 'div' => array('class' => 'input text required'),
  7415. 'label' => array('for' => 'ContactRequiredOne'),
  7416. 'Required One',
  7417. '/label',
  7418. 'input' => array(
  7419. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7420. 'id' => 'ContactRequiredOne',
  7421. 'required' => 'required'
  7422. ),
  7423. '/div'
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $result = $this->Form->input('Contact.string_required');
  7427. $expected = array(
  7428. 'div' => array('class' => 'input text required'),
  7429. 'label' => array('for' => 'ContactStringRequired'),
  7430. 'String Required',
  7431. '/label',
  7432. 'input' => array(
  7433. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7434. 'id' => 'ContactStringRequired',
  7435. 'required' => 'required'
  7436. ),
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $result = $this->Form->input('Contact.imnotrequired');
  7441. $expected = array(
  7442. 'div' => array('class' => 'input text'),
  7443. 'label' => array('for' => 'ContactImnotrequired'),
  7444. 'Imnotrequired',
  7445. '/label',
  7446. 'input' => array(
  7447. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7448. 'id' => 'ContactImnotrequired'
  7449. ),
  7450. '/div'
  7451. );
  7452. $this->assertTags($result, $expected);
  7453. $result = $this->Form->input('Contact.imalsonotrequired');
  7454. $expected = array(
  7455. 'div' => array('class' => 'input text'),
  7456. 'label' => array('for' => 'ContactImalsonotrequired'),
  7457. 'Imalsonotrequired',
  7458. '/label',
  7459. 'input' => array(
  7460. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7461. 'id' => 'ContactImalsonotrequired'
  7462. ),
  7463. '/div'
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $result = $this->Form->input('Contact.imalsonotrequired2');
  7467. $expected = array(
  7468. 'div' => array('class' => 'input text'),
  7469. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7470. 'Imalsonotrequired2',
  7471. '/label',
  7472. 'input' => array(
  7473. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7474. 'id' => 'ContactImalsonotrequired2'
  7475. ),
  7476. '/div'
  7477. );
  7478. $this->assertTags($result, $expected);
  7479. $result = $this->Form->input('Contact.imnotrequiredeither');
  7480. $expected = array(
  7481. 'div' => array('class' => 'input text'),
  7482. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7483. 'Imnotrequiredeither',
  7484. '/label',
  7485. 'input' => array(
  7486. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7487. 'id' => 'ContactImnotrequiredeither'
  7488. ),
  7489. '/div'
  7490. );
  7491. $this->assertTags($result, $expected);
  7492. $result = $this->Form->input('Contact.iamrequiredalways');
  7493. $expected = array(
  7494. 'div' => array('class' => 'input text required'),
  7495. 'label' => array('for' => 'ContactIamrequiredalways'),
  7496. 'Iamrequiredalways',
  7497. '/label',
  7498. 'input' => array(
  7499. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7500. 'id' => 'ContactIamrequiredalways',
  7501. 'required' => 'required'
  7502. ),
  7503. '/div'
  7504. );
  7505. $this->assertTags($result, $expected);
  7506. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7507. $expected = array(
  7508. 'div' => array('class' => 'input checkbox required'),
  7509. array('input' => array(
  7510. 'type' => 'hidden',
  7511. 'name' => 'data[Contact][boolean_field]',
  7512. 'id' => 'ContactBooleanField_',
  7513. 'value' => '0'
  7514. )),
  7515. array('input' => array(
  7516. 'type' => 'checkbox',
  7517. 'name' => 'data[Contact][boolean_field]',
  7518. 'value' => '1',
  7519. 'id' => 'ContactBooleanField'
  7520. )),
  7521. 'label' => array('for' => 'ContactBooleanField'),
  7522. 'Boolean Field',
  7523. '/label',
  7524. '/div'
  7525. );
  7526. $this->assertTags($result, $expected);
  7527. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7528. $expected = array(
  7529. 'div' => array('class' => 'input checkbox required'),
  7530. array('input' => array(
  7531. 'type' => 'hidden',
  7532. 'name' => 'data[Contact][boolean_field]',
  7533. 'id' => 'ContactBooleanField_',
  7534. 'value' => '0'
  7535. )),
  7536. array('input' => array(
  7537. 'type' => 'checkbox',
  7538. 'name' => 'data[Contact][boolean_field]',
  7539. 'value' => '1',
  7540. 'id' => 'ContactBooleanField',
  7541. 'required' => 'required'
  7542. )),
  7543. 'label' => array('for' => 'ContactBooleanField'),
  7544. 'Boolean Field',
  7545. '/label',
  7546. '/div'
  7547. );
  7548. $this->assertTags($result, $expected);
  7549. }
  7550. /**
  7551. * Test that required fields are created when only using ModelValidator::add().
  7552. *
  7553. * @return void
  7554. */
  7555. public function testFormInputRequiredDetectionModelValidator() {
  7556. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7557. $this->Form->create('ContactTag');
  7558. $result = $this->Form->input('ContactTag.iwillberequired');
  7559. $expected = array(
  7560. 'div' => array('class' => 'input text required'),
  7561. 'label' => array('for' => 'ContactTagIwillberequired'),
  7562. 'Iwillberequired',
  7563. '/label',
  7564. 'input' => array(
  7565. 'name' => 'data[ContactTag][iwillberequired]',
  7566. 'type' => 'text',
  7567. 'id' => 'ContactTagIwillberequired',
  7568. 'required' => 'required'
  7569. ),
  7570. '/div'
  7571. );
  7572. $this->assertTags($result, $expected);
  7573. }
  7574. /**
  7575. * testFormMagicInput method
  7576. *
  7577. * @return void
  7578. */
  7579. public function testFormMagicInput() {
  7580. $encoding = strtolower(Configure::read('App.encoding'));
  7581. $result = $this->Form->create('Contact');
  7582. $expected = array(
  7583. 'form' => array(
  7584. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7585. 'accept-charset' => $encoding
  7586. ),
  7587. 'div' => array('style' => 'display:none;'),
  7588. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7589. '/div'
  7590. );
  7591. $this->assertTags($result, $expected);
  7592. $result = $this->Form->input('name');
  7593. $expected = array(
  7594. 'div' => array('class' => 'input text'),
  7595. 'label' => array('for' => 'ContactName'),
  7596. 'Name',
  7597. '/label',
  7598. 'input' => array(
  7599. 'type' => 'text', 'name' => 'data[Contact][name]',
  7600. 'id' => 'ContactName', 'maxlength' => '255'
  7601. ),
  7602. '/div'
  7603. );
  7604. $this->assertTags($result, $expected);
  7605. $result = $this->Form->input('non_existing_field_in_contact_model');
  7606. $expected = array(
  7607. 'div' => array('class' => 'input text'),
  7608. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7609. 'Non Existing Field In Contact Model',
  7610. '/label',
  7611. 'input' => array(
  7612. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7613. 'id' => 'ContactNonExistingFieldInContactModel'
  7614. ),
  7615. '/div'
  7616. );
  7617. $this->assertTags($result, $expected);
  7618. $result = $this->Form->input('Address.street');
  7619. $expected = array(
  7620. 'div' => array('class' => 'input text'),
  7621. 'label' => array('for' => 'AddressStreet'),
  7622. 'Street',
  7623. '/label',
  7624. 'input' => array(
  7625. 'type' => 'text', 'name' => 'data[Address][street]',
  7626. 'id' => 'AddressStreet'
  7627. ),
  7628. '/div'
  7629. );
  7630. $this->assertTags($result, $expected);
  7631. $result = $this->Form->input('Address.non_existing_field_in_model');
  7632. $expected = array(
  7633. 'div' => array('class' => 'input text'),
  7634. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7635. 'Non Existing Field In Model',
  7636. '/label',
  7637. 'input' => array(
  7638. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7639. 'id' => 'AddressNonExistingFieldInModel'
  7640. ),
  7641. '/div'
  7642. );
  7643. $this->assertTags($result, $expected);
  7644. $result = $this->Form->input('name', array('div' => false));
  7645. $expected = array(
  7646. 'label' => array('for' => 'ContactName'),
  7647. 'Name',
  7648. '/label',
  7649. 'input' => array(
  7650. 'type' => 'text', 'name' => 'data[Contact][name]',
  7651. 'id' => 'ContactName', 'maxlength' => '255'
  7652. )
  7653. );
  7654. $this->assertTags($result, $expected);
  7655. extract($this->dateRegex);
  7656. $now = strtotime('now');
  7657. $result = $this->Form->input('Contact.published', array('div' => false));
  7658. $expected = array(
  7659. 'label' => array('for' => 'ContactPublishedMonth'),
  7660. 'Published',
  7661. '/label',
  7662. array('select' => array(
  7663. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7664. )),
  7665. $monthsRegex,
  7666. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7667. date('F', $now),
  7668. '/option',
  7669. '*/select',
  7670. '-',
  7671. array('select' => array(
  7672. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7673. )),
  7674. $daysRegex,
  7675. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7676. date('j', $now),
  7677. '/option',
  7678. '*/select',
  7679. '-',
  7680. array('select' => array(
  7681. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7682. )),
  7683. $yearsRegex,
  7684. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7685. date('Y', $now),
  7686. '*/select'
  7687. );
  7688. $this->assertTags($result, $expected);
  7689. $result = $this->Form->input('Contact.updated', array('div' => false));
  7690. $expected = array(
  7691. 'label' => array('for' => 'ContactUpdatedMonth'),
  7692. 'Updated',
  7693. '/label',
  7694. array('select' => array(
  7695. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7696. )),
  7697. $monthsRegex,
  7698. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7699. date('F', $now),
  7700. '/option',
  7701. '*/select',
  7702. '-',
  7703. array('select' => array(
  7704. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7705. )),
  7706. $daysRegex,
  7707. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7708. date('j', $now),
  7709. '/option',
  7710. '*/select',
  7711. '-',
  7712. array('select' => array(
  7713. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7714. )),
  7715. $yearsRegex,
  7716. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7717. date('Y', $now),
  7718. '*/select'
  7719. );
  7720. $this->assertTags($result, $expected);
  7721. $result = $this->Form->input('UserForm.stuff');
  7722. $expected = array(
  7723. 'div' => array('class' => 'input text'),
  7724. 'label' => array('for' => 'UserFormStuff'),
  7725. 'Stuff',
  7726. '/label',
  7727. 'input' => array(
  7728. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7729. 'id' => 'UserFormStuff', 'maxlength' => 10
  7730. ),
  7731. '/div'
  7732. );
  7733. $this->assertTags($result, $expected);
  7734. }
  7735. /**
  7736. * testForMagicInputNonExistingNorValidated method
  7737. *
  7738. * @return void
  7739. */
  7740. public function testForMagicInputNonExistingNorValidated() {
  7741. $encoding = strtolower(Configure::read('App.encoding'));
  7742. $result = $this->Form->create('Contact');
  7743. $expected = array(
  7744. 'form' => array(
  7745. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7746. 'accept-charset' => $encoding
  7747. ),
  7748. 'div' => array('style' => 'display:none;'),
  7749. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7750. '/div'
  7751. );
  7752. $this->assertTags($result, $expected);
  7753. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7754. $expected = array(
  7755. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7756. 'Non Existing Nor Validated',
  7757. '/label',
  7758. 'input' => array(
  7759. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7760. 'id' => 'ContactNonExistingNorValidated'
  7761. )
  7762. );
  7763. $this->assertTags($result, $expected);
  7764. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7765. 'div' => false, 'value' => 'my value'
  7766. ));
  7767. $expected = array(
  7768. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7769. 'Non Existing Nor Validated',
  7770. '/label',
  7771. 'input' => array(
  7772. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7773. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7774. )
  7775. );
  7776. $this->assertTags($result, $expected);
  7777. $this->Form->request->data = array(
  7778. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7779. ));
  7780. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7781. $expected = array(
  7782. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7783. 'Non Existing Nor Validated',
  7784. '/label',
  7785. 'input' => array(
  7786. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7787. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7788. )
  7789. );
  7790. $this->assertTags($result, $expected);
  7791. }
  7792. /**
  7793. * testFormMagicInputLabel method
  7794. *
  7795. * @return void
  7796. */
  7797. public function testFormMagicInputLabel() {
  7798. $encoding = strtolower(Configure::read('App.encoding'));
  7799. $result = $this->Form->create('Contact');
  7800. $expected = array(
  7801. 'form' => array(
  7802. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7803. 'accept-charset' => $encoding
  7804. ),
  7805. 'div' => array('style' => 'display:none;'),
  7806. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7807. '/div'
  7808. );
  7809. $this->assertTags($result, $expected);
  7810. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7811. $this->assertTags($result, array('input' => array(
  7812. 'name' => 'data[Contact][name]', 'type' => 'text',
  7813. 'id' => 'ContactName', 'maxlength' => '255')
  7814. ));
  7815. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7816. $expected = array(
  7817. 'label' => array('for' => 'ContactName'),
  7818. 'My label',
  7819. '/label',
  7820. 'input' => array(
  7821. 'type' => 'text', 'name' => 'data[Contact][name]',
  7822. 'id' => 'ContactName', 'maxlength' => '255'
  7823. )
  7824. );
  7825. $this->assertTags($result, $expected);
  7826. $result = $this->Form->input('Contact.name', array(
  7827. 'div' => false, 'label' => array('class' => 'mandatory')
  7828. ));
  7829. $expected = array(
  7830. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7831. 'Name',
  7832. '/label',
  7833. 'input' => array(
  7834. 'type' => 'text', 'name' => 'data[Contact][name]',
  7835. 'id' => 'ContactName', 'maxlength' => '255'
  7836. )
  7837. );
  7838. $this->assertTags($result, $expected);
  7839. $result = $this->Form->input('Contact.name', array(
  7840. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7841. ));
  7842. $expected = array(
  7843. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7844. 'My label',
  7845. '/label',
  7846. 'input' => array(
  7847. 'type' => 'text', 'name' => 'data[Contact][name]',
  7848. 'id' => 'ContactName', 'maxlength' => '255'
  7849. )
  7850. );
  7851. $this->assertTags($result, $expected);
  7852. $result = $this->Form->input('Contact.name', array(
  7853. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7854. ));
  7855. $expected = array(
  7856. 'label' => array('for' => 'my_id'),
  7857. 'Name',
  7858. '/label',
  7859. 'input' => array(
  7860. 'type' => 'text', 'name' => 'data[Contact][name]',
  7861. 'id' => 'my_id', 'maxlength' => '255'
  7862. )
  7863. );
  7864. $this->assertTags($result, $expected);
  7865. $result = $this->Form->input('1.id');
  7866. $this->assertTags($result, array('input' => array(
  7867. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7868. 'id' => 'Contact1Id'
  7869. )));
  7870. $result = $this->Form->input("1.name");
  7871. $expected = array(
  7872. 'div' => array('class' => 'input text'),
  7873. 'label' => array('for' => 'Contact1Name'),
  7874. 'Name',
  7875. '/label',
  7876. 'input' => array(
  7877. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7878. 'id' => 'Contact1Name', 'maxlength' => '255'
  7879. ),
  7880. '/div'
  7881. );
  7882. $this->assertTags($result, $expected);
  7883. $result = $this->Form->input('Contact.1.id');
  7884. $this->assertTags($result, array(
  7885. 'input' => array(
  7886. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7887. 'id' => 'Contact1Id'
  7888. )
  7889. ));
  7890. $result = $this->Form->input("Model.1.name");
  7891. $expected = array(
  7892. 'div' => array('class' => 'input text'),
  7893. 'label' => array('for' => 'Model1Name'),
  7894. 'Name',
  7895. '/label',
  7896. 'input' => array(
  7897. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7898. 'id' => 'Model1Name'
  7899. ),
  7900. '/div'
  7901. );
  7902. $this->assertTags($result, $expected);
  7903. }
  7904. /**
  7905. * testFormEnd method
  7906. *
  7907. * @return void
  7908. */
  7909. public function testFormEnd() {
  7910. $this->assertEquals('</form>', $this->Form->end());
  7911. $result = $this->Form->end('');
  7912. $expected = array(
  7913. 'div' => array('class' => 'submit'),
  7914. 'input' => array('type' => 'submit', 'value' => ''),
  7915. '/div',
  7916. '/form'
  7917. );
  7918. $this->assertTags($result, $expected);
  7919. $result = $this->Form->end(array('label' => ''));
  7920. $expected = array(
  7921. 'div' => array('class' => 'submit'),
  7922. 'input' => array('type' => 'submit', 'value' => ''),
  7923. '/div',
  7924. '/form'
  7925. );
  7926. $this->assertTags($result, $expected);
  7927. $result = $this->Form->end('save');
  7928. $expected = array(
  7929. 'div' => array('class' => 'submit'),
  7930. 'input' => array('type' => 'submit', 'value' => 'save'),
  7931. '/div',
  7932. '/form'
  7933. );
  7934. $this->assertTags($result, $expected);
  7935. $result = $this->Form->end(array('label' => 'save'));
  7936. $expected = array(
  7937. 'div' => array('class' => 'submit'),
  7938. 'input' => array('type' => 'submit', 'value' => 'save'),
  7939. '/div',
  7940. '/form'
  7941. );
  7942. $this->assertTags($result, $expected);
  7943. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7944. $expected = array(
  7945. 'div' => array('class' => 'submit'),
  7946. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7947. '/div',
  7948. '/form'
  7949. );
  7950. $this->assertTags($result, $expected);
  7951. $result = $this->Form->end(array('name' => 'Whatever'));
  7952. $expected = array(
  7953. 'div' => array('class' => 'submit'),
  7954. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7955. '/div',
  7956. '/form'
  7957. );
  7958. $this->assertTags($result, $expected);
  7959. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7960. $expected = array(
  7961. 'div' => array('class' => 'good'),
  7962. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7963. '/div',
  7964. '/form'
  7965. );
  7966. $this->assertTags($result, $expected);
  7967. $result = $this->Form->end(array(
  7968. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7969. ));
  7970. $expected = array(
  7971. 'div' => array('class' => 'good'),
  7972. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7973. '/div',
  7974. '/form'
  7975. );
  7976. $this->assertTags($result, $expected);
  7977. }
  7978. /**
  7979. * testMultipleFormWithIdFields method
  7980. *
  7981. * @return void
  7982. */
  7983. public function testMultipleFormWithIdFields() {
  7984. $this->Form->create('UserForm');
  7985. $result = $this->Form->input('id');
  7986. $this->assertTags($result, array('input' => array(
  7987. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7988. )));
  7989. $result = $this->Form->input('ValidateItem.id');
  7990. $this->assertTags($result, array('input' => array(
  7991. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7992. 'id' => 'ValidateItemId'
  7993. )));
  7994. $result = $this->Form->input('ValidateUser.id');
  7995. $this->assertTags($result, array('input' => array(
  7996. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7997. 'id' => 'ValidateUserId'
  7998. )));
  7999. }
  8000. /**
  8001. * testDbLessModel method
  8002. *
  8003. * @return void
  8004. */
  8005. public function testDbLessModel() {
  8006. $this->Form->create('TestMail');
  8007. $result = $this->Form->input('name');
  8008. $expected = array(
  8009. 'div' => array('class' => 'input text'),
  8010. 'label' => array('for' => 'TestMailName'),
  8011. 'Name',
  8012. '/label',
  8013. 'input' => array(
  8014. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8015. 'id' => 'TestMailName'
  8016. ),
  8017. '/div'
  8018. );
  8019. $this->assertTags($result, $expected);
  8020. ClassRegistry::init('TestMail');
  8021. $this->Form->create('TestMail');
  8022. $result = $this->Form->input('name');
  8023. $expected = array(
  8024. 'div' => array('class' => 'input text'),
  8025. 'label' => array('for' => 'TestMailName'),
  8026. 'Name',
  8027. '/label',
  8028. 'input' => array(
  8029. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8030. 'id' => 'TestMailName'
  8031. ),
  8032. '/div'
  8033. );
  8034. $this->assertTags($result, $expected);
  8035. }
  8036. /**
  8037. * testBrokenness method
  8038. *
  8039. * @return void
  8040. */
  8041. public function testBrokenness() {
  8042. /*
  8043. * #4 This test has two parents and four children. By default (as of r7117) both
  8044. * parents are show but the first parent is missing a child. This is the inconsistency
  8045. * in the default behaviour - one parent has all children, the other does not - dependent
  8046. * on the data values.
  8047. */
  8048. $result = $this->Form->select('Model.field', array(
  8049. 'Fred' => array(
  8050. 'freds_son_1' => 'Fred',
  8051. 'freds_son_2' => 'Freddie'
  8052. ),
  8053. 'Bert' => array(
  8054. 'berts_son_1' => 'Albert',
  8055. 'berts_son_2' => 'Bertie')
  8056. ),
  8057. array('showParents' => true, 'empty' => false)
  8058. );
  8059. $expected = array(
  8060. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8061. array('optgroup' => array('label' => 'Fred')),
  8062. array('option' => array('value' => 'freds_son_1')),
  8063. 'Fred',
  8064. '/option',
  8065. array('option' => array('value' => 'freds_son_2')),
  8066. 'Freddie',
  8067. '/option',
  8068. '/optgroup',
  8069. array('optgroup' => array('label' => 'Bert')),
  8070. array('option' => array('value' => 'berts_son_1')),
  8071. 'Albert',
  8072. '/option',
  8073. array('option' => array('value' => 'berts_son_2')),
  8074. 'Bertie',
  8075. '/option',
  8076. '/optgroup',
  8077. '/select'
  8078. );
  8079. $this->assertTags($result, $expected);
  8080. /*
  8081. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8082. * changed, so we should expect the same select list data, just with a different name
  8083. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8084. * This is where data corruption can occur, because when a select value is missing from
  8085. * a list a form will substitute the first value in the list - without the user knowing.
  8086. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8087. * affect the availability of 3 => 'Three' as a valid option.
  8088. */
  8089. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8090. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8091. ));
  8092. $result = $this->Form->select(
  8093. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8094. );
  8095. $expected = array(
  8096. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8097. array('option' => array('value' => 1)),
  8098. 'One',
  8099. '/option',
  8100. array('option' => array('value' => 2)),
  8101. 'Two',
  8102. '/option',
  8103. array('optgroup' => array('label' => 'Three')),
  8104. array('option' => array('value' => 3)),
  8105. 'Three',
  8106. '/option',
  8107. array('option' => array('value' => 4)),
  8108. 'Four',
  8109. '/option',
  8110. array('option' => array('value' => 5)),
  8111. 'Five',
  8112. '/option',
  8113. '/optgroup',
  8114. '/select'
  8115. );
  8116. $this->assertTags($result, $expected);
  8117. }
  8118. /**
  8119. * Test the generation of fields for a multi record form.
  8120. *
  8121. * @return void
  8122. */
  8123. public function testMultiRecordForm() {
  8124. $this->Form->create('ValidateProfile');
  8125. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8126. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8127. $expected = array(
  8128. 'div' => array('class' => 'input textarea'),
  8129. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8130. 'Name',
  8131. '/label',
  8132. 'textarea' => array(
  8133. 'id' => 'ValidateProfile1ValidateItem2Name',
  8134. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8135. 'cols' => 30,
  8136. 'rows' => 6
  8137. ),
  8138. 'Value',
  8139. '/textarea',
  8140. '/div'
  8141. );
  8142. $this->assertTags($result, $expected);
  8143. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8144. $expected = array(
  8145. 'div' => array('class' => 'input date'),
  8146. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8147. 'Created',
  8148. '/label',
  8149. array('select' => array(
  8150. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8151. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8152. )
  8153. ),
  8154. array('option' => array('value' => '')), '/option',
  8155. $this->dateRegex['monthsRegex'],
  8156. '/select', '-',
  8157. array('select' => array(
  8158. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8159. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8160. )
  8161. ),
  8162. array('option' => array('value' => '')), '/option',
  8163. $this->dateRegex['daysRegex'],
  8164. '/select', '-',
  8165. array('select' => array(
  8166. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8167. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8168. )
  8169. ),
  8170. array('option' => array('value' => '')), '/option',
  8171. $this->dateRegex['yearsRegex'],
  8172. '/select',
  8173. '/div'
  8174. );
  8175. $this->assertTags($result, $expected);
  8176. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8177. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8178. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8179. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8180. $expected = array(
  8181. 'div' => array('class' => 'input select error'),
  8182. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8183. 'Profile',
  8184. '/label',
  8185. 'select' => array(
  8186. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8187. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8188. 'class' => 'form-error'
  8189. ),
  8190. '/select',
  8191. array('div' => array('class' => 'error-message')),
  8192. 'Error',
  8193. '/div',
  8194. '/div'
  8195. );
  8196. $this->assertTags($result, $expected);
  8197. }
  8198. /**
  8199. * test the correct display of multi-record form validation errors.
  8200. *
  8201. * @return void
  8202. */
  8203. public function testMultiRecordFormValidationErrors() {
  8204. $this->Form->create('ValidateProfile');
  8205. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8206. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8207. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8208. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8209. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8210. $result = $this->Form->error('ValidateProfile.2.city');
  8211. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8212. $result = $this->Form->error('2.city');
  8213. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8214. }
  8215. /**
  8216. * test the correct display of multi-record form validation errors.
  8217. *
  8218. * @return void
  8219. */
  8220. public function testSaveManyRecordFormValidationErrors() {
  8221. $this->Form->create('ValidateUser');
  8222. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8223. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8224. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8225. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8226. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8227. $result = $this->Form->error('ValidateUser.0.city');
  8228. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8229. }
  8230. /**
  8231. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8232. *
  8233. * @return void
  8234. */
  8235. public function testInputTemplate() {
  8236. $result = $this->Form->input('Contact.email', array(
  8237. 'type' => 'text', 'format' => array('input')
  8238. ));
  8239. $expected = array(
  8240. 'div' => array('class' => 'input text'),
  8241. 'input' => array(
  8242. 'type' => 'text', 'name' => 'data[Contact][email]',
  8243. 'id' => 'ContactEmail'
  8244. ),
  8245. '/div'
  8246. );
  8247. $this->assertTags($result, $expected);
  8248. $result = $this->Form->input('Contact.email', array(
  8249. 'type' => 'text', 'format' => array('input', 'label'),
  8250. 'label' => '<em>Email (required)</em>'
  8251. ));
  8252. $expected = array(
  8253. 'div' => array('class' => 'input text'),
  8254. array('input' => array(
  8255. 'type' => 'text', 'name' => 'data[Contact][email]',
  8256. 'id' => 'ContactEmail'
  8257. )),
  8258. 'label' => array('for' => 'ContactEmail'),
  8259. 'em' => array(),
  8260. 'Email (required)',
  8261. '/em',
  8262. '/label',
  8263. '/div'
  8264. );
  8265. $this->assertTags($result, $expected);
  8266. $result = $this->Form->input('Contact.email', array(
  8267. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8268. 'between' => '<div>Something in the middle</div>',
  8269. 'after' => '<span>Some text at the end</span>'
  8270. ));
  8271. $expected = array(
  8272. 'div' => array('class' => 'input text'),
  8273. array('input' => array(
  8274. 'type' => 'text', 'name' => 'data[Contact][email]',
  8275. 'id' => 'ContactEmail'
  8276. )),
  8277. array('div' => array()),
  8278. 'Something in the middle',
  8279. '/div',
  8280. 'label' => array('for' => 'ContactEmail'),
  8281. 'Email',
  8282. '/label',
  8283. 'span' => array(),
  8284. 'Some text at the end',
  8285. '/span',
  8286. '/div'
  8287. );
  8288. $this->assertTags($result, $expected);
  8289. $result = $this->Form->input('Contact.method', array(
  8290. 'type' => 'radio',
  8291. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8292. 'between' => 'I am between',
  8293. ));
  8294. $expected = array(
  8295. 'div' => array('class' => 'input radio'),
  8296. 'fieldset' => array(),
  8297. 'legend' => array(),
  8298. 'Method',
  8299. '/legend',
  8300. 'I am between',
  8301. 'input' => array(
  8302. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8303. 'value' => '', 'id' => 'ContactMethod_'
  8304. ),
  8305. array('input' => array(
  8306. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8307. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8308. )),
  8309. array('label' => array('for' => 'ContactMethodEmail')),
  8310. 'Email',
  8311. '/label',
  8312. array('input' => array(
  8313. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8314. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8315. )),
  8316. array('label' => array('for' => 'ContactMethodPigeon')),
  8317. 'Pigeon',
  8318. '/label',
  8319. '/fieldset',
  8320. '/div',
  8321. );
  8322. $this->assertTags($result, $expected);
  8323. }
  8324. /**
  8325. * test that some html5 inputs + FormHelper::__call() work
  8326. *
  8327. * @return void
  8328. */
  8329. public function testHtml5Inputs() {
  8330. $result = $this->Form->email('User.email');
  8331. $expected = array(
  8332. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8333. );
  8334. $this->assertTags($result, $expected);
  8335. $result = $this->Form->search('User.query');
  8336. $expected = array(
  8337. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8338. );
  8339. $this->assertTags($result, $expected);
  8340. $result = $this->Form->search('User.query', array('value' => 'test'));
  8341. $expected = array(
  8342. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8343. );
  8344. $this->assertTags($result, $expected);
  8345. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8346. $expected = array(
  8347. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8348. );
  8349. $this->assertTags($result, $expected);
  8350. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8351. $expected = array(
  8352. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8353. );
  8354. $this->assertTags($result, $expected);
  8355. }
  8356. /**
  8357. *
  8358. * @expectedException CakeException
  8359. * @return void
  8360. */
  8361. public function testHtml5InputException() {
  8362. $this->Form->email();
  8363. }
  8364. /**
  8365. * Tests that a model can be loaded from the model names passed in the request object
  8366. *
  8367. * @return void
  8368. */
  8369. public function testIntrospectModelFromRequest() {
  8370. $this->loadFixtures('Post');
  8371. App::build(array(
  8372. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8373. ));
  8374. CakePlugin::load('TestPlugin');
  8375. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8376. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8377. $this->Form->create('TestPluginPost');
  8378. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8379. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8380. CakePlugin::unload();
  8381. App::build();
  8382. }
  8383. /**
  8384. * Tests that it is possible to set the validation errors directly in the helper for a field
  8385. *
  8386. * @return void
  8387. */
  8388. public function testCustomValidationErrors() {
  8389. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8390. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8391. $this->assertEquals('Badness!', $result);
  8392. }
  8393. /**
  8394. * Tests that the 'on' key validates as expected on create
  8395. *
  8396. * @return void
  8397. */
  8398. public function testRequiredOnCreate() {
  8399. $this->Form->create('Contact');
  8400. $result = $this->Form->input('Contact.imrequiredonupdate');
  8401. $expected = array(
  8402. 'div' => array('class' => 'input text'),
  8403. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8404. 'Imrequiredonupdate',
  8405. '/label',
  8406. 'input' => array(
  8407. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8408. 'id' => 'ContactImrequiredonupdate'
  8409. ),
  8410. '/div'
  8411. );
  8412. $this->assertTags($result, $expected);
  8413. $result = $this->Form->input('Contact.imrequiredoncreate');
  8414. $expected = array(
  8415. 'div' => array('class' => 'input text required'),
  8416. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8417. 'Imrequiredoncreate',
  8418. '/label',
  8419. 'input' => array(
  8420. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8421. 'id' => 'ContactImrequiredoncreate',
  8422. 'required' => 'required'
  8423. ),
  8424. '/div'
  8425. );
  8426. $this->assertTags($result, $expected);
  8427. $result = $this->Form->input('Contact.imrequiredonboth');
  8428. $expected = array(
  8429. 'div' => array('class' => 'input text required'),
  8430. 'label' => array('for' => 'ContactImrequiredonboth'),
  8431. 'Imrequiredonboth',
  8432. '/label',
  8433. 'input' => array(
  8434. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8435. 'id' => 'ContactImrequiredonboth',
  8436. 'required' => 'required'
  8437. ),
  8438. '/div'
  8439. );
  8440. $this->assertTags($result, $expected);
  8441. $this->Form->inputDefaults(array('required' => false));
  8442. $result = $this->Form->input('Contact.imrequired');
  8443. $expected = array(
  8444. 'div' => array('class' => 'input text'),
  8445. 'label' => array('for' => 'ContactImrequired'),
  8446. 'Imrequired',
  8447. '/label',
  8448. 'input' => array(
  8449. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8450. 'id' => 'ContactImrequired'
  8451. ),
  8452. '/div'
  8453. );
  8454. $this->assertTags($result, $expected);
  8455. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8456. $this->assertTags($result, $expected);
  8457. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8458. $expected = array(
  8459. 'div' => array('class' => 'input text required'),
  8460. 'label' => array('for' => 'ContactImrequired'),
  8461. 'Imrequired',
  8462. '/label',
  8463. 'input' => array(
  8464. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8465. 'id' => 'ContactImrequired'
  8466. ),
  8467. '/div'
  8468. );
  8469. $this->assertTags($result, $expected);
  8470. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8471. $this->assertTags($result, $expected);
  8472. }
  8473. /**
  8474. * Tests that the 'on' key validates as expected on update
  8475. *
  8476. * @return void
  8477. */
  8478. public function testRequiredOnUpdate() {
  8479. $this->Form->request->data['Contact']['id'] = 1;
  8480. $this->Form->create('Contact');
  8481. $result = $this->Form->input('Contact.imrequiredonupdate');
  8482. $expected = array(
  8483. 'div' => array('class' => 'input text required'),
  8484. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8485. 'Imrequiredonupdate',
  8486. '/label',
  8487. 'input' => array(
  8488. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8489. 'id' => 'ContactImrequiredonupdate',
  8490. 'required' => 'required'
  8491. ),
  8492. '/div'
  8493. );
  8494. $this->assertTags($result, $expected);
  8495. $result = $this->Form->input('Contact.imrequiredoncreate');
  8496. $expected = array(
  8497. 'div' => array('class' => 'input text'),
  8498. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8499. 'Imrequiredoncreate',
  8500. '/label',
  8501. 'input' => array(
  8502. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8503. 'id' => 'ContactImrequiredoncreate'
  8504. ),
  8505. '/div'
  8506. );
  8507. $this->assertTags($result, $expected);
  8508. $result = $this->Form->input('Contact.imrequiredonboth');
  8509. $expected = array(
  8510. 'div' => array('class' => 'input text required'),
  8511. 'label' => array('for' => 'ContactImrequiredonboth'),
  8512. 'Imrequiredonboth',
  8513. '/label',
  8514. 'input' => array(
  8515. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8516. 'id' => 'ContactImrequiredonboth',
  8517. 'required' => 'required'
  8518. ),
  8519. '/div'
  8520. );
  8521. $this->assertTags($result, $expected);
  8522. $result = $this->Form->input('Contact.imrequired');
  8523. $expected = array(
  8524. 'div' => array('class' => 'input text required'),
  8525. 'label' => array('for' => 'ContactImrequired'),
  8526. 'Imrequired',
  8527. '/label',
  8528. 'input' => array(
  8529. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8530. 'id' => 'ContactImrequired',
  8531. 'required' => 'required'
  8532. ),
  8533. '/div'
  8534. );
  8535. $this->assertTags($result, $expected);
  8536. }
  8537. /**
  8538. * Test inputDefaults setter and getter
  8539. *
  8540. * @return void
  8541. */
  8542. public function testInputDefaults() {
  8543. $this->Form->create('Contact');
  8544. $this->Form->inputDefaults(array(
  8545. 'label' => false,
  8546. 'div' => array(
  8547. 'style' => 'color: #000;'
  8548. )
  8549. ));
  8550. $result = $this->Form->input('Contact.field1');
  8551. $expected = array(
  8552. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8553. 'input' => array(
  8554. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8555. 'id' => 'ContactField1'
  8556. ),
  8557. '/div'
  8558. );
  8559. $this->assertTags($result, $expected);
  8560. $this->Form->inputDefaults(array(
  8561. 'div' => false,
  8562. 'label' => 'Label',
  8563. ));
  8564. $result = $this->Form->input('Contact.field1');
  8565. $expected = array(
  8566. 'label' => array('for' => 'ContactField1'),
  8567. 'Label',
  8568. '/label',
  8569. 'input' => array(
  8570. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8571. 'id' => 'ContactField1'
  8572. ),
  8573. );
  8574. $this->assertTags($result, $expected);
  8575. $this->Form->inputDefaults(array(
  8576. 'label' => false,
  8577. ), true);
  8578. $result = $this->Form->input('Contact.field1');
  8579. $expected = array(
  8580. 'input' => array(
  8581. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8582. 'id' => 'ContactField1'
  8583. ),
  8584. );
  8585. $this->assertTags($result, $expected);
  8586. $result = $this->Form->inputDefaults();
  8587. $expected = array(
  8588. 'div' => false,
  8589. 'label' => false,
  8590. );
  8591. $this->assertEqual($result, $expected);
  8592. }
  8593. }