FormHelperTest.php 298 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363
  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. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  321. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  322. );
  323. /**
  324. * beforeValidate method
  325. *
  326. * @return void
  327. */
  328. public function beforeValidate($options = array()) {
  329. $this->invalidate('email');
  330. return false;
  331. }
  332. }
  333. /**
  334. * ValidateProfile class
  335. *
  336. * @package Cake.Test.Case.View.Helper
  337. */
  338. class ValidateProfile extends CakeTestModel {
  339. /**
  340. * useTable property
  341. *
  342. * @var boolean
  343. */
  344. public $useTable = false;
  345. /**
  346. * schema property
  347. *
  348. * @var array
  349. */
  350. protected $_schema = array(
  351. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  356. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  357. );
  358. /**
  359. * hasOne property
  360. *
  361. * @var array
  362. */
  363. public $hasOne = array('ValidateItem' => array(
  364. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  365. ));
  366. /**
  367. * belongsTo property
  368. *
  369. * @var array
  370. */
  371. public $belongsTo = array('ValidateUser' => array(
  372. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * beforeValidate method
  376. *
  377. * @return void
  378. */
  379. public function beforeValidate($options = array()) {
  380. $this->invalidate('full_name');
  381. $this->invalidate('city');
  382. return false;
  383. }
  384. }
  385. /**
  386. * ValidateItem class
  387. *
  388. * @package Cake.Test.Case.View.Helper
  389. */
  390. class ValidateItem extends CakeTestModel {
  391. /**
  392. * useTable property
  393. *
  394. * @var boolean
  395. */
  396. public $useTable = false;
  397. /**
  398. * schema property
  399. *
  400. * @var array
  401. */
  402. protected $_schema = array(
  403. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  406. 'description' => array(
  407. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  408. ),
  409. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  410. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  411. );
  412. /**
  413. * belongsTo property
  414. *
  415. * @var array
  416. */
  417. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  418. /**
  419. * beforeValidate method
  420. *
  421. * @return void
  422. */
  423. public function beforeValidate($options = array()) {
  424. $this->invalidate('description');
  425. return false;
  426. }
  427. }
  428. /**
  429. * TestMail class
  430. *
  431. * @package Cake.Test.Case.View.Helper
  432. */
  433. class TestMail extends CakeTestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var boolean
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @package Cake.Test.Case.View.Helper
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends CakeTestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. Configure::write('Config.language', 'eng');
  468. Configure::write('App.base', '');
  469. Configure::delete('Asset');
  470. $this->Controller = new ContactTestController();
  471. $this->View = new View($this->Controller);
  472. $this->Form = new FormHelper($this->View);
  473. $this->Form->Html = new HtmlHelper($this->View);
  474. $this->Form->request = new CakeRequest('contacts/add', false);
  475. $this->Form->request->here = '/contacts/add';
  476. $this->Form->request['action'] = 'add';
  477. $this->Form->request->webroot = '';
  478. $this->Form->request->base = '';
  479. ClassRegistry::addObject('Contact', new Contact());
  480. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  481. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  482. ClassRegistry::addObject('User', new UserForm());
  483. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  484. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  485. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  486. $this->oldSalt = Configure::read('Security.salt');
  487. $this->dateRegex = array(
  488. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  490. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  493. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  494. );
  495. Configure::write('Security.salt', 'foo!');
  496. }
  497. /**
  498. * tearDown method
  499. *
  500. * @return void
  501. */
  502. public function tearDown() {
  503. parent::tearDown();
  504. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  505. Configure::write('Security.salt', $this->oldSalt);
  506. }
  507. /**
  508. * testFormCreateWithSecurity method
  509. *
  510. * Test form->create() with security key.
  511. *
  512. * @return void
  513. */
  514. public function testCreateWithSecurity() {
  515. $this->Form->request['_Token'] = array('key' => 'testKey');
  516. $encoding = strtolower(Configure::read('App.encoding'));
  517. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  518. $expected = array(
  519. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  520. 'div' => array('style' => 'display:none;'),
  521. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  522. array('input' => array(
  523. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  524. )),
  525. '/div'
  526. );
  527. $this->assertTags($result, $expected);
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  529. $expected['form']['id'] = 'MyForm';
  530. $this->assertTags($result, $expected);
  531. }
  532. /**
  533. * testFormCreateGetNoSecurity method
  534. *
  535. * Test form->create() with no security key as its a get form
  536. *
  537. * @return void
  538. */
  539. public function testCreateEndGetNoSecurity() {
  540. $this->Form->request['_Token'] = array('key' => 'testKey');
  541. $encoding = strtolower(Configure::read('App.encoding'));
  542. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  543. $this->assertNotContains('Token', $result);
  544. $result = $this->Form->end('Save');
  545. $this->assertNotContains('Token', $result);
  546. }
  547. /**
  548. * test that create() clears the fields property so it starts fresh
  549. *
  550. * @return void
  551. */
  552. public function testCreateClearingFields() {
  553. $this->Form->fields = array('model_id');
  554. $this->Form->create('Contact');
  555. $this->assertEquals(array(), $this->Form->fields);
  556. }
  557. /**
  558. * Tests form hash generation with model-less data
  559. *
  560. * @return void
  561. */
  562. public function testValidateHashNoModel() {
  563. $this->Form->request['_Token'] = array('key' => 'foo');
  564. $result = $this->Form->secure(array('anything'));
  565. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  566. }
  567. /**
  568. * Tests that models with identical field names get resolved properly
  569. *
  570. * @return void
  571. */
  572. public function testDuplicateFieldNameResolution() {
  573. $result = $this->Form->create('ValidateUser');
  574. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateItem.name');
  576. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  577. $result = $this->Form->input('ValidateUser.name');
  578. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  579. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  580. $this->assertRegExp('/type="text"/', $result);
  581. $result = $this->Form->input('ValidateItem.name');
  582. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  583. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  584. $this->assertRegExp('/<textarea/', $result);
  585. $result = $this->Form->input('name');
  586. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  587. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  588. $this->assertRegExp('/type="text"/', $result);
  589. }
  590. /**
  591. * Tests that hidden fields generated for checkboxes don't get locked
  592. *
  593. * @return void
  594. */
  595. public function testNoCheckboxLocking() {
  596. $this->Form->request['_Token'] = array('key' => 'foo');
  597. $this->assertSame(array(), $this->Form->fields);
  598. $this->Form->checkbox('check', array('value' => '1'));
  599. $this->assertSame($this->Form->fields, array('check'));
  600. }
  601. /**
  602. * testFormSecurityFields method
  603. *
  604. * Test generation of secure form hash generation.
  605. *
  606. * @return void
  607. */
  608. public function testFormSecurityFields() {
  609. $key = 'testKey';
  610. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  611. $this->Form->request['_Token'] = array('key' => $key);
  612. $result = $this->Form->secure($fields);
  613. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  614. $hash .= ':' . 'Model.valid';
  615. $hash = urlencode($hash);
  616. $expected = array(
  617. 'div' => array('style' => 'display:none;'),
  618. array('input' => array(
  619. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  620. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  621. )),
  622. array('input' => array(
  623. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  624. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  630. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  631. $result = $this->Form->secure($fields);
  632. $expected = array(
  633. 'div' => array('class' => 'hidden'),
  634. array('input' => array(
  635. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  636. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  637. )),
  638. array('input' => array(
  639. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  640. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for double and float fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldGenerationForFloats() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'float',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => 10
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number',
  668. 'name' => 'data[Contact][foo]',
  669. 'id' => 'ContactFoo',
  670. 'step' => 'any'
  671. )),
  672. '/div'
  673. );
  674. $this->assertTags($result, $expected);
  675. $result = $this->Form->input('foo', array('step' => 0.5));
  676. $expected = array(
  677. 'div' => array('class' => 'input number'),
  678. 'label' => array('for' => 'ContactFoo'),
  679. 'Foo',
  680. '/label',
  681. array('input' => array(
  682. 'type' => 'number',
  683. 'name' => 'data[Contact][foo]',
  684. 'id' => 'ContactFoo',
  685. 'step' => '0.5'
  686. )),
  687. '/div'
  688. );
  689. $this->assertTags($result, $expected);
  690. }
  691. /**
  692. * Tests correct generation of number fields for integer fields
  693. *
  694. * @return void
  695. */
  696. public function testTextFieldTypeNumberGenerationForIntegers() {
  697. $model = ClassRegistry::getObject('Contact');
  698. $model->setSchema(array('foo' => array(
  699. 'type' => 'integer',
  700. 'null' => false,
  701. 'default' => null,
  702. 'length' => null
  703. )));
  704. $this->Form->create('Contact');
  705. $result = $this->Form->input('foo');
  706. $expected = array(
  707. 'div' => array('class' => 'input number'),
  708. 'label' => array('for' => 'ContactFoo'),
  709. 'Foo',
  710. '/label',
  711. array('input' => array(
  712. 'type' => 'number', 'name' => 'data[Contact][foo]',
  713. 'id' => 'ContactFoo'
  714. )),
  715. '/div'
  716. );
  717. $this->assertTags($result, $expected);
  718. }
  719. /**
  720. * testFormSecurityMultipleFields method
  721. *
  722. * Test secure() with multiple row form. Ensure hash is correct.
  723. *
  724. * @return void
  725. */
  726. public function testFormSecurityMultipleFields() {
  727. $key = 'testKey';
  728. $fields = array(
  729. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  730. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  731. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  732. );
  733. $this->Form->request['_Token'] = array('key' => $key);
  734. $result = $this->Form->secure($fields);
  735. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  736. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  737. $expected = array(
  738. 'div' => array('style' => 'display:none;'),
  739. array('input' => array(
  740. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  741. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  742. )),
  743. array('input' => array(
  744. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  745. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  746. )),
  747. '/div'
  748. );
  749. $this->assertTags($result, $expected);
  750. }
  751. /**
  752. * testFormSecurityMultipleSubmitButtons
  753. *
  754. * test form submit generation and ensure that _Token is only created on end()
  755. *
  756. * @return void
  757. */
  758. public function testFormSecurityMultipleSubmitButtons() {
  759. $key = 'testKey';
  760. $this->Form->request['_Token'] = array('key' => $key);
  761. $this->Form->create('Addresses');
  762. $this->Form->input('Address.title');
  763. $this->Form->input('Address.first_name');
  764. $result = $this->Form->submit('Save', array('name' => 'save'));
  765. $expected = array(
  766. 'div' => array('class' => 'submit'),
  767. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  768. '/div',
  769. );
  770. $this->assertTags($result, $expected);
  771. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  772. $expected = array(
  773. 'div' => array('class' => 'submit'),
  774. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  775. '/div',
  776. );
  777. $this->assertTags($result, $expected);
  778. $result = $this->Form->end(null);
  779. $expected = array(
  780. 'div' => array('style' => 'display:none;'),
  781. array('input' => array(
  782. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  783. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  784. )),
  785. array('input' => array(
  786. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  787. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  788. )),
  789. '/div'
  790. );
  791. $this->assertTags($result, $expected);
  792. }
  793. /**
  794. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  795. *
  796. * @return void
  797. */
  798. public function testSecurityButtonNestedNamed() {
  799. $key = 'testKey';
  800. $this->Form->request['_Token'] = array('key' => $key);
  801. $this->Form->create('Addresses');
  802. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  803. $result = $this->Form->unlockField();
  804. $this->assertEquals(array('Address.button'), $result);
  805. }
  806. /**
  807. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  808. *
  809. * @return void
  810. */
  811. public function testSecuritySubmitNestedNamed() {
  812. $key = 'testKey';
  813. $this->Form->request['_Token'] = array('key' => $key);
  814. $this->Form->create('Addresses');
  815. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  816. $result = $this->Form->unlockField();
  817. $this->assertEquals(array('Address.button'), $result);
  818. }
  819. /**
  820. * Test that the correct fields are unlocked for image submits with no names.
  821. *
  822. * @return void
  823. */
  824. public function testSecuritySubmitImageNoName() {
  825. $key = 'testKey';
  826. $this->Form->request['_Token'] = array('key' => $key);
  827. $this->Form->create('User');
  828. $result = $this->Form->submit('save.png');
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  832. '/div'
  833. );
  834. $this->assertTags($result, $expected);
  835. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  836. }
  837. /**
  838. * Test that the correct fields are unlocked for image submits with names.
  839. *
  840. * @return void
  841. */
  842. public function testSecuritySubmitImageName() {
  843. $key = 'testKey';
  844. $this->Form->request['_Token'] = array('key' => $key);
  845. $this->Form->create('User');
  846. $result = $this->Form->submit('save.png', array('name' => 'test'));
  847. $expected = array(
  848. 'div' => array('class' => 'submit'),
  849. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  850. '/div'
  851. );
  852. $this->assertTags($result, $expected);
  853. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  854. }
  855. /**
  856. * testFormSecurityMultipleInputFields method
  857. *
  858. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  859. *
  860. * @return void
  861. */
  862. public function testFormSecurityMultipleInputFields() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('Addresses');
  866. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  867. $this->Form->input('Addresses.0.title');
  868. $this->Form->input('Addresses.0.first_name');
  869. $this->Form->input('Addresses.0.last_name');
  870. $this->Form->input('Addresses.0.address');
  871. $this->Form->input('Addresses.0.city');
  872. $this->Form->input('Addresses.0.phone');
  873. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  874. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  875. $this->Form->input('Addresses.1.title');
  876. $this->Form->input('Addresses.1.first_name');
  877. $this->Form->input('Addresses.1.last_name');
  878. $this->Form->input('Addresses.1.address');
  879. $this->Form->input('Addresses.1.city');
  880. $this->Form->input('Addresses.1.phone');
  881. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  882. $result = $this->Form->secure($this->Form->fields);
  883. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  884. $expected = array(
  885. 'div' => array('style' => 'display:none;'),
  886. array('input' => array(
  887. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  888. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  889. )),
  890. array('input' => array(
  891. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  892. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  893. )),
  894. '/div'
  895. );
  896. $this->assertTags($result, $expected);
  897. }
  898. /**
  899. * Test form security with Model.field.0 style inputs
  900. *
  901. * @return void
  902. */
  903. public function testFormSecurityArrayFields() {
  904. $key = 'testKey';
  905. $this->Form->request->params['_Token']['key'] = $key;
  906. $this->Form->create('Address');
  907. $this->Form->input('Address.primary.1');
  908. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  909. $this->Form->input('Address.secondary.1.0');
  910. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  911. }
  912. /**
  913. * testFormSecurityMultipleInputDisabledFields method
  914. *
  915. * test secure form generation with multiple records and disabled fields.
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityMultipleInputDisabledFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token'] = array(
  922. 'key' => $key,
  923. 'unlockedFields' => array('first_name', 'address')
  924. );
  925. $this->Form->create();
  926. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  927. $this->Form->input('Addresses.0.title');
  928. $this->Form->input('Addresses.0.first_name');
  929. $this->Form->input('Addresses.0.last_name');
  930. $this->Form->input('Addresses.0.address');
  931. $this->Form->input('Addresses.0.city');
  932. $this->Form->input('Addresses.0.phone');
  933. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  934. $this->Form->input('Addresses.1.title');
  935. $this->Form->input('Addresses.1.first_name');
  936. $this->Form->input('Addresses.1.last_name');
  937. $this->Form->input('Addresses.1.address');
  938. $this->Form->input('Addresses.1.city');
  939. $this->Form->input('Addresses.1.phone');
  940. $result = $this->Form->secure($this->Form->fields);
  941. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  942. $expected = array(
  943. 'div' => array('style' => 'display:none;'),
  944. array('input' => array(
  945. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  946. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  947. )),
  948. array('input' => array(
  949. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  950. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  951. )),
  952. '/div'
  953. );
  954. $this->assertTags($result, $expected);
  955. }
  956. /**
  957. * testFormSecurityInputDisabledFields method
  958. *
  959. * Test single record form with disabled fields.
  960. *
  961. * @return void
  962. */
  963. public function testFormSecurityInputUnlockedFields() {
  964. $key = 'testKey';
  965. $this->Form->request['_Token'] = array(
  966. 'key' => $key,
  967. 'unlockedFields' => array('first_name', 'address')
  968. );
  969. $this->Form->create();
  970. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  971. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  972. $this->Form->input('Addresses.title');
  973. $this->Form->input('Addresses.first_name');
  974. $this->Form->input('Addresses.last_name');
  975. $this->Form->input('Addresses.address');
  976. $this->Form->input('Addresses.city');
  977. $this->Form->input('Addresses.phone');
  978. $result = $this->Form->fields;
  979. $expected = array(
  980. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  981. 'Addresses.city', 'Addresses.phone'
  982. );
  983. $this->assertEquals($expected, $result);
  984. $result = $this->Form->secure($expected);
  985. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  986. $expected = array(
  987. 'div' => array('style' => 'display:none;'),
  988. array('input' => array(
  989. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  990. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  991. )),
  992. array('input' => array(
  993. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  994. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  995. )),
  996. '/div'
  997. );
  998. $this->assertTags($result, $expected);
  999. }
  1000. /**
  1001. * test securing inputs with custom name attributes.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testFormSecureWithCustomNameAttribute() {
  1006. $this->Form->request->params['_Token']['key'] = 'testKey';
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1008. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1009. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1010. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1011. }
  1012. /**
  1013. * testFormSecuredInput method
  1014. *
  1015. * Test generation of entire secure form, assertions made on input() output.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testFormSecuredInput() {
  1020. $this->Form->request['_Token'] = array('key' => 'testKey');
  1021. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1022. $encoding = strtolower(Configure::read('App.encoding'));
  1023. $expected = array(
  1024. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1025. 'div' => array('style' => 'display:none;'),
  1026. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1029. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1035. $expected = array(
  1036. 'div' => array('class' => 'input text'),
  1037. 'label' => array('for' => 'UserFormPublished'),
  1038. 'Published',
  1039. '/label',
  1040. array('input' => array(
  1041. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1042. 'id' => 'UserFormPublished'
  1043. )),
  1044. '/div'
  1045. );
  1046. $this->assertTags($result, $expected);
  1047. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1048. $expected = array(
  1049. 'div' => array('class' => 'input text'),
  1050. 'label' => array('for' => 'UserFormOther'),
  1051. 'Other',
  1052. '/label',
  1053. array('input' => array(
  1054. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1055. 'id' => 'UserFormOther'
  1056. )),
  1057. '/div'
  1058. );
  1059. $this->assertTags($result, $expected);
  1060. $result = $this->Form->hidden('UserForm.stuff');
  1061. $expected = array(
  1062. 'input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1064. 'id' => 'UserFormStuff'
  1065. ));
  1066. $this->assertTags($result, $expected);
  1067. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1068. $expected = array('input' => array(
  1069. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1070. 'value' => '0', 'id' => 'UserFormHidden'
  1071. ));
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1074. $expected = array(
  1075. 'div' => array('class' => 'input checkbox'),
  1076. array('input' => array(
  1077. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1078. 'value' => '0', 'id' => 'UserFormSomething_'
  1079. )),
  1080. array('input' => array(
  1081. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1082. 'value' => '1', 'id' => 'UserFormSomething'
  1083. )),
  1084. 'label' => array('for' => 'UserFormSomething'),
  1085. 'Something',
  1086. '/label',
  1087. '/div'
  1088. );
  1089. $this->assertTags($result, $expected);
  1090. $result = $this->Form->fields;
  1091. $expected = array(
  1092. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1093. 'UserForm.hidden' => '0', 'UserForm.something'
  1094. );
  1095. $this->assertEquals($expected, $result);
  1096. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1097. $result = $this->Form->secure($this->Form->fields);
  1098. $expected = array(
  1099. 'div' => array('style' => 'display:none;'),
  1100. array('input' => array(
  1101. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1102. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1103. )),
  1104. array('input' => array(
  1105. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1106. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1107. )),
  1108. '/div'
  1109. );
  1110. $this->assertTags($result, $expected);
  1111. }
  1112. /**
  1113. * Test secured inputs with custom names.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testSecuredInputCustomName() {
  1118. $this->Form->request['_Token'] = array('key' => 'testKey');
  1119. $this->assertEquals(array(), $this->Form->fields);
  1120. $this->Form->input('text_input', array(
  1121. 'name' => 'data[Option][General.default_role]',
  1122. ));
  1123. $expected = array('Option.General.default_role');
  1124. $this->assertEquals($expected, $this->Form->fields);
  1125. $this->Form->input('select_box', array(
  1126. 'name' => 'data[Option][General.select_role]',
  1127. 'type' => 'select',
  1128. 'options' => array(1, 2),
  1129. ));
  1130. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1131. $this->assertEquals($expected, $this->Form->fields);
  1132. }
  1133. /**
  1134. * Tests that the correct keys are added to the field hash index
  1135. *
  1136. * @return void
  1137. */
  1138. public function testFormSecuredFileInput() {
  1139. $this->Form->request['_Token'] = array('key' => 'testKey');
  1140. $this->assertEquals(array(), $this->Form->fields);
  1141. $this->Form->file('Attachment.file');
  1142. $expected = array(
  1143. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1144. 'Attachment.file.error', 'Attachment.file.size'
  1145. );
  1146. $this->assertEquals($expected, $this->Form->fields);
  1147. }
  1148. /**
  1149. * test that multiple selects keys are added to field hash
  1150. *
  1151. * @return void
  1152. */
  1153. public function testFormSecuredMultipleSelect() {
  1154. $this->Form->request['_Token'] = array('key' => 'testKey');
  1155. $this->assertEquals(array(), $this->Form->fields);
  1156. $options = array('1' => 'one', '2' => 'two');
  1157. $this->Form->select('Model.select', $options);
  1158. $expected = array('Model.select');
  1159. $this->assertEquals($expected, $this->Form->fields);
  1160. $this->Form->fields = array();
  1161. $this->Form->select('Model.select', $options, array('multiple' => true));
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * testFormSecuredRadio method
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecuredRadio() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'option1', '2' => 'option2');
  1173. $this->Form->radio('Test.test', $options);
  1174. $expected = array('Test.test');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * Test that when disabled is in a list based attribute array it works.
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredAndDisabledNotAssoc() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1185. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1186. $this->Form->text('Model.text', array('disabled'));
  1187. $this->Form->textarea('Model.textarea', array('disabled'));
  1188. $this->Form->password('Model.password', array('disabled'));
  1189. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1190. $expected = array(
  1191. 'Model.radio' => ''
  1192. );
  1193. $this->assertEquals($expected, $this->Form->fields);
  1194. }
  1195. /**
  1196. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1197. * hashing.
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredAndDisabled() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1204. $this->Form->text('Model.text', array('disabled' => true));
  1205. $this->Form->password('Model.text', array('disabled' => true));
  1206. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1207. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1208. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1209. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1210. $this->Form->month('Model.month', array('disabled' => true));
  1211. $this->Form->day('Model.day', array('disabled' => true));
  1212. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1213. $this->Form->minute('Model.minute', array('disabled' => true));
  1214. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1215. $expected = array(
  1216. 'Model.radio' => ''
  1217. );
  1218. $this->assertEquals($expected, $this->Form->fields);
  1219. }
  1220. /**
  1221. * testDisableSecurityUsingForm method
  1222. *
  1223. * @return void
  1224. */
  1225. public function testDisableSecurityUsingForm() {
  1226. $this->Form->request['_Token'] = array(
  1227. 'key' => 'testKey',
  1228. 'disabledFields' => array()
  1229. );
  1230. $this->Form->create();
  1231. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1232. $this->Form->input('Addresses.title');
  1233. $this->Form->input('Addresses.first_name', array('secure' => false));
  1234. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1235. $this->Form->input('Addresses.zip', array(
  1236. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1237. ));
  1238. $result = $this->Form->fields;
  1239. $expected = array(
  1240. 'Addresses.id' => '123456', 'Addresses.title',
  1241. );
  1242. $this->assertEquals($expected, $result);
  1243. }
  1244. /**
  1245. * test disableField
  1246. *
  1247. * @return void
  1248. */
  1249. public function testUnlockFieldAddsToList() {
  1250. $this->Form->request['_Token'] = array(
  1251. 'key' => 'testKey',
  1252. 'unlockedFields' => array()
  1253. );
  1254. $this->Form->create('Contact');
  1255. $this->Form->unlockField('Contact.name');
  1256. $this->Form->text('Contact.name');
  1257. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1258. $this->assertEquals(array(), $this->Form->fields);
  1259. }
  1260. /**
  1261. * test unlockField removing from fields array.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUnlockFieldRemovingFromFields() {
  1266. $this->Form->request['_Token'] = array(
  1267. 'key' => 'testKey',
  1268. 'unlockedFields' => array()
  1269. );
  1270. $this->Form->create('Contact');
  1271. $this->Form->hidden('Contact.id', array('value' => 1));
  1272. $this->Form->text('Contact.name');
  1273. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1274. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1275. $this->Form->unlockField('Contact.name');
  1276. $this->Form->unlockField('Contact.id');
  1277. $this->assertEquals(array(), $this->Form->fields);
  1278. }
  1279. /**
  1280. * testTagIsInvalid method
  1281. *
  1282. * @return void
  1283. */
  1284. public function testTagIsInvalid() {
  1285. $Contact = ClassRegistry::getObject('Contact');
  1286. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1287. $this->Form->setEntity('Contact.0.email');
  1288. $result = $this->Form->tagIsInvalid();
  1289. $this->assertEquals($expected, $result);
  1290. $this->Form->setEntity('Contact.1.email');
  1291. $result = $this->Form->tagIsInvalid();
  1292. $this->assertFalse($result);
  1293. $this->Form->setEntity('Contact.0.name');
  1294. $result = $this->Form->tagIsInvalid();
  1295. $this->assertFalse($result);
  1296. }
  1297. /**
  1298. * Test tagIsInvalid with validation errors from a saveMany
  1299. *
  1300. * @return void
  1301. */
  1302. public function testTagIsInvalidSaveMany() {
  1303. $Contact = ClassRegistry::getObject('Contact');
  1304. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1305. $this->Form->create('Contact');
  1306. $this->Form->setEntity('0.email');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $this->assertEquals($expected, $result);
  1309. $this->Form->setEntity('0.Contact.email');
  1310. $result = $this->Form->tagIsInvalid();
  1311. $this->assertEquals($expected, $result);
  1312. }
  1313. /**
  1314. * Test validation errors.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testPasswordValidation() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors['password'] = array('Please provide a password');
  1321. $result = $this->Form->input('Contact.password');
  1322. $expected = array(
  1323. 'div' => array('class' => 'input password error'),
  1324. 'label' => array('for' => 'ContactPassword'),
  1325. 'Password',
  1326. '/label',
  1327. 'input' => array(
  1328. 'type' => 'password', 'name' => 'data[Contact][password]',
  1329. 'id' => 'ContactPassword', 'class' => 'form-error'
  1330. ),
  1331. array('div' => array('class' => 'error-message')),
  1332. 'Please provide a password',
  1333. '/div',
  1334. '/div'
  1335. );
  1336. $this->assertTags($result, $expected);
  1337. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1338. $expected = array(
  1339. 'div' => array('class' => 'input password error'),
  1340. 'label' => array('for' => 'ContactPassword'),
  1341. 'Password',
  1342. '/label',
  1343. 'input' => array(
  1344. 'type' => 'password', 'name' => 'data[Contact][password]',
  1345. 'id' => 'ContactPassword', 'class' => 'form-error'
  1346. ),
  1347. '/div'
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * Test validation errors, when validation message is an empty string.
  1353. *
  1354. * @return void
  1355. */
  1356. public function testEmptyErrorValidation() {
  1357. $this->Form->validationErrors['Contact']['password'] = '';
  1358. $result = $this->Form->input('Contact.password');
  1359. $expected = array(
  1360. 'div' => array('class' => 'input password error'),
  1361. 'label' => array('for' => 'ContactPassword'),
  1362. 'Password',
  1363. '/label',
  1364. 'input' => array(
  1365. 'type' => 'password', 'name' => 'data[Contact][password]',
  1366. 'id' => 'ContactPassword', 'class' => 'form-error'
  1367. ),
  1368. array('div' => array('class' => 'error-message')),
  1369. array(),
  1370. '/div',
  1371. '/div'
  1372. );
  1373. $this->assertTags($result, $expected);
  1374. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1375. $expected = array(
  1376. 'div' => array('class' => 'input password error'),
  1377. 'label' => array('for' => 'ContactPassword'),
  1378. 'Password',
  1379. '/label',
  1380. 'input' => array(
  1381. 'type' => 'password', 'name' => 'data[Contact][password]',
  1382. 'id' => 'ContactPassword', 'class' => 'form-error'
  1383. ),
  1384. '/div'
  1385. );
  1386. $this->assertTags($result, $expected);
  1387. }
  1388. /**
  1389. * Test validation errors, when calling input() overriding validation message by an empty string.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. '/div'
  1422. );
  1423. $this->assertTags($result, $expected);
  1424. }
  1425. /**
  1426. * testFormValidationAssociated method
  1427. *
  1428. * test display of form errors in conjunction with model::validates.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormValidationAssociated() {
  1433. $this->UserForm = ClassRegistry::getObject('UserForm');
  1434. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1435. $data = array(
  1436. 'UserForm' => array('name' => 'user'),
  1437. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1438. );
  1439. $result = $this->UserForm->OpenidUrl->create($data);
  1440. $this->assertFalse(empty($result));
  1441. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1442. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1443. $encoding = strtolower(Configure::read('App.encoding'));
  1444. $expected = array(
  1445. 'form' => array(
  1446. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1447. 'accept-charset' => $encoding
  1448. ),
  1449. 'div' => array('style' => 'display:none;'),
  1450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1451. '/div'
  1452. );
  1453. $this->assertTags($result, $expected);
  1454. $result = $this->Form->error(
  1455. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Error, not registered', $result);
  1458. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1459. }
  1460. /**
  1461. * testFormValidationAssociatedFirstLevel method
  1462. *
  1463. * test form error display with associated model.
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormValidationAssociatedFirstLevel() {
  1468. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1469. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1470. $data = array(
  1471. 'ValidateUser' => array('name' => 'mariano'),
  1472. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1473. );
  1474. $result = $this->ValidateUser->create($data);
  1475. $this->assertFalse(empty($result));
  1476. $this->assertFalse($this->ValidateUser->validates());
  1477. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1478. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1479. $encoding = strtolower(Configure::read('App.encoding'));
  1480. $expected = array(
  1481. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1482. 'div' => array('style' => 'display:none;'),
  1483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1484. '/div'
  1485. );
  1486. $this->assertTags($result, $expected);
  1487. $result = $this->Form->error(
  1488. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid email', $result);
  1491. $result = $this->Form->error(
  1492. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid name', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid city', $result);
  1499. unset($this->ValidateUser->ValidateProfile);
  1500. unset($this->ValidateUser);
  1501. }
  1502. /**
  1503. * testFormValidationAssociatedSecondLevel method
  1504. *
  1505. * test form error display with associated model.
  1506. *
  1507. * @return void
  1508. */
  1509. public function testFormValidationAssociatedSecondLevel() {
  1510. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1511. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1512. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1513. $data = array(
  1514. 'ValidateUser' => array('name' => 'mariano'),
  1515. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1516. 'ValidateItem' => array('name' => 'Item')
  1517. );
  1518. $result = $this->ValidateUser->create($data);
  1519. $this->assertFalse(empty($result));
  1520. $this->assertFalse($this->ValidateUser->validates());
  1521. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $result = $this->Form->error(
  1544. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1545. );
  1546. $this->assertEquals('Invalid description', $result);
  1547. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1548. unset($this->ValidateUser->ValidateProfile);
  1549. unset($this->ValidateUser);
  1550. }
  1551. /**
  1552. * testFormValidationMultiRecord method
  1553. *
  1554. * test form error display with multiple records.
  1555. *
  1556. * @return void
  1557. */
  1558. public function testFormValidationMultiRecord() {
  1559. $Contact = ClassRegistry::getObject('Contact');
  1560. $Contact->validationErrors[2] = array(
  1561. 'name' => array('This field cannot be left blank')
  1562. );
  1563. $result = $this->Form->input('Contact.2.name');
  1564. $expected = array(
  1565. 'div' => array('class' => 'input text error'),
  1566. 'label' => array('for' => 'Contact2Name'),
  1567. 'Name',
  1568. '/label',
  1569. 'input' => array(
  1570. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1571. 'class' => 'form-error', 'maxlength' => 255
  1572. ),
  1573. array('div' => array('class' => 'error-message')),
  1574. 'This field cannot be left blank',
  1575. '/div',
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. }
  1580. /**
  1581. * testMultipleInputValidation method
  1582. *
  1583. * test multiple record form validation error display.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testMultipleInputValidation() {
  1588. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1589. $Address->validationErrors[0] = array(
  1590. 'title' => array('This field cannot be empty'),
  1591. 'first_name' => array('This field cannot be empty')
  1592. );
  1593. $Address->validationErrors[1] = array(
  1594. 'last_name' => array('You must have a last name')
  1595. );
  1596. $this->Form->create();
  1597. $result = $this->Form->input('Address.0.title');
  1598. $expected = array(
  1599. 'div' => array('class'),
  1600. 'label' => array('for'),
  1601. 'preg:/[^<]+/',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1605. ),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.first_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1619. array('div' => array('class' => 'error-message')),
  1620. 'This field cannot be empty',
  1621. '/div',
  1622. '/div'
  1623. );
  1624. $this->assertTags($result, $expected);
  1625. $result = $this->Form->input('Address.0.last_name');
  1626. $expected = array(
  1627. 'div' => array('class'),
  1628. 'label' => array('for'),
  1629. 'preg:/[^<]+/',
  1630. '/label',
  1631. 'input' => array('type' => 'text', 'name', 'id'),
  1632. '/div'
  1633. );
  1634. $this->assertTags($result, $expected);
  1635. $result = $this->Form->input('Address.1.last_name');
  1636. $expected = array(
  1637. 'div' => array('class'),
  1638. 'label' => array('for'),
  1639. 'preg:/[^<]+/',
  1640. '/label',
  1641. 'input' => array(
  1642. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1643. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'You must have a last name',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. }
  1652. /**
  1653. * testInput method
  1654. *
  1655. * Test various incarnations of input().
  1656. *
  1657. * @return void
  1658. */
  1659. public function testInput() {
  1660. $result = $this->Form->input('ValidateUser.balance');
  1661. $expected = array(
  1662. 'div' => array('class'),
  1663. 'label' => array('for'),
  1664. 'Balance',
  1665. '/label',
  1666. 'input' => array('name', 'type' => 'number', 'id'),
  1667. '/div',
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1671. $expected = array(
  1672. 'div' => array('class' => 'input email'),
  1673. 'label' => array('for' => 'custom'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'data[Contact][email]',
  1678. 'id' => 'custom', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1684. $expected = array(
  1685. '<div',
  1686. 'label' => array('for' => 'ContactEmail'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'email', 'name' => 'data[Contact][email]',
  1691. 'id' => 'ContactEmail', 'maxlength' => 255
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->hidden('Contact.idontexist');
  1697. $expected = array('input' => array(
  1698. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1699. 'id' => 'ContactIdontexist'
  1700. ));
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][email]',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'Contact5Email'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1723. 'id' => 'Contact5Email'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.password');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input password'),
  1731. 'label' => array('for' => 'ContactPassword'),
  1732. 'Password',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'password', 'name' => 'data[Contact][password]',
  1736. 'id' => 'ContactPassword'
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array(
  1742. 'type' => 'file', 'class' => 'textbox'
  1743. ));
  1744. $expected = array(
  1745. 'div' => array('class' => 'input file'),
  1746. 'label' => array('for' => 'ContactEmail'),
  1747. 'Email',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1751. 'id' => 'ContactEmail'
  1752. )),
  1753. '/div'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1757. $result = $this->Form->input('Contact.phone');
  1758. $expected = array(
  1759. 'div' => array('class' => 'input tel'),
  1760. 'label' => array('for' => 'ContactPhone'),
  1761. 'Phone',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1765. 'value' => 'Hello &amp; World &gt; weird chars',
  1766. 'id' => 'ContactPhone', 'maxlength' => 255
  1767. )),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1772. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1773. $expected = array(
  1774. 'div' => array('class' => 'input text'),
  1775. 'label' => array('for' => 'myId'),
  1776. 'Field',
  1777. '/label',
  1778. 'input' => array(
  1779. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1780. 'value' => 'My value', 'id' => 'myId'
  1781. ),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. unset($this->Form->request->data);
  1786. $Contact = ClassRegistry::getObject('Contact');
  1787. $Contact->validationErrors['field'] = array('Badness!');
  1788. $result = $this->Form->input('Contact.field');
  1789. $expected = array(
  1790. 'div' => array('class' => 'input text error'),
  1791. 'label' => array('for' => 'ContactField'),
  1792. 'Field',
  1793. '/label',
  1794. 'input' => array(
  1795. 'type' => 'text', 'name' => 'data[Contact][field]',
  1796. 'id' => 'ContactField', 'class' => 'form-error'
  1797. ),
  1798. array('div' => array('class' => 'error-message')),
  1799. 'Badness!',
  1800. '/div',
  1801. '/div'
  1802. );
  1803. $this->assertTags($result, $expected);
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1806. ));
  1807. $expected = array(
  1808. 'label' => array('for' => 'ContactField'),
  1809. 'Field',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => 'data[Contact][field]',
  1813. 'id' => 'ContactField', 'class' => 'form-error'
  1814. ),
  1815. array('span' => array('class' => 'error-message')),
  1816. 'Badness!',
  1817. '/span'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $result = $this->Form->input('Contact.field', array(
  1821. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1822. ));
  1823. $expected = array(
  1824. 'div' => array('class' => 'input text error'),
  1825. 'label' => array('for' => 'ContactField'),
  1826. 'Field',
  1827. '/label',
  1828. 'input' => array(
  1829. 'type' => 'text', 'name' => 'data[Contact][field]',
  1830. 'id' => 'ContactField', 'class' => 'form-error'
  1831. ),
  1832. array('div' => array('class' => 'error')),
  1833. 'Badness!',
  1834. '/div'
  1835. );
  1836. $this->assertTags($result, $expected);
  1837. $result = $this->Form->input('Contact.field', array(
  1838. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1839. ));
  1840. $expected = array(
  1841. 'span' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'Badness!',
  1850. '/span'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1854. $expected = array(
  1855. 'div' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'A message to you, Rudy',
  1864. array('div' => array('class' => 'error-message')),
  1865. 'Badness!',
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $this->Form->setEntity(null);
  1871. $this->Form->setEntity('Contact.field');
  1872. $result = $this->Form->input('Contact.field', array(
  1873. 'after' => 'A message to you, Rudy', 'error' => false
  1874. ));
  1875. $expected = array(
  1876. 'div' => array('class' => 'input text'),
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1881. 'A message to you, Rudy',
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ObjectField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $Contact->validationErrors['field'] = array('minLength');
  1897. $result = $this->Form->input('Contact.field', array(
  1898. 'error' => array(
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large'
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Le login doit contenir au moins 2 caractères',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $Contact->validationErrors['field'] = array('maxLength');
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'error' => array(
  1918. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1919. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1920. 'maxLength' => 'login too large',
  1921. )
  1922. ));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1929. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1930. 'login too large',
  1931. '/span',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test that inputs with 0 can be created.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testInputZero() {
  1942. $this->Form->create('User');
  1943. $result = $this->Form->input('0');
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'User0'), '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1948. '/div'
  1949. );
  1950. $this->assertTags($result, $expected);
  1951. }
  1952. /**
  1953. * test input() with checkbox creation
  1954. *
  1955. * @return void
  1956. */
  1957. public function testInputCheckbox() {
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1975. $expected = array(
  1976. 'div' => array('class' => 'input checkbox'),
  1977. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1978. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $result = $this->Form->input('User.disabled', array(
  1983. 'label' => 'Disabled',
  1984. 'type' => 'checkbox',
  1985. 'data-foo' => 'disabled'
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1990. array('input' => array(
  1991. 'type' => 'checkbox',
  1992. 'name' => 'data[User][disabled]',
  1993. 'value' => '1',
  1994. 'id' => 'UserDisabled',
  1995. 'data-foo' => 'disabled'
  1996. )),
  1997. 'label' => array('for' => 'UserDisabled'),
  1998. 'Disabled',
  1999. '/label',
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. }
  2004. /**
  2005. * test form->input() with time types.
  2006. *
  2007. */
  2008. public function testInputTime() {
  2009. extract($this->dateRegex);
  2010. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2011. $result = explode(':', $result);
  2012. $this->assertRegExp('/option value="23"/', $result[0]);
  2013. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2014. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2015. $result = explode(':', $result);
  2016. $this->assertRegExp('/option value="23"/', $result[0]);
  2017. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('Model.field', array(
  2026. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2027. ));
  2028. $result = explode(':', $result);
  2029. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2030. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2032. $result = $this->Form->input('prueba', array(
  2033. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2034. 'maxYear' => date('Y') + 1, 'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2041. $result = $this->Form->input('Random.start_time', array(
  2042. 'type' => 'time',
  2043. 'selected' => '18:15'
  2044. ));
  2045. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2046. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2047. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2048. $result = $this->Form->input('published', array('type' => 'time'));
  2049. $now = strtotime('now');
  2050. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2051. $now = strtotime('2013-03-09 00:42:21');
  2052. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2053. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2054. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2055. }
  2056. /**
  2057. * Test interval + selected near the hour roll over.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testTimeSelectedWithInterval() {
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'type' => 'time',
  2064. 'interval' => 15,
  2065. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2066. ));
  2067. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'type' => 'time',
  2072. 'interval' => 15,
  2073. 'selected' => '2012-10-23 15:57:00'
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => '15:57'
  2083. ));
  2084. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $result = $this->Form->input('Model.start_time', array(
  2087. 'timeFormat' => 24,
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '23:57'
  2091. ));
  2092. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $result = $this->Form->input('Model.created', array(
  2095. 'timeFormat' => 24,
  2096. 'type' => 'datetime',
  2097. 'interval' => 15,
  2098. 'selected' => '2012-09-30 23:56'
  2099. ));
  2100. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2101. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2102. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. }
  2106. /**
  2107. * Test interval & timeFormat = 12
  2108. *
  2109. * @return void
  2110. */
  2111. public function testInputTimeWithIntervalAnd12HourFormat() {
  2112. $result = $this->Form->input('Model.start_time', array(
  2113. 'type' => 'time',
  2114. 'timeFormat' => 12,
  2115. 'interval' => 5,
  2116. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2117. ));
  2118. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2119. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2120. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2121. $result = $this->Form->input('Model.start_time', array(
  2122. 'type' => 'time',
  2123. 'timeFormat' => '12',
  2124. 'interval' => 5,
  2125. 'selected' => '2013-04-19 16:30:00'
  2126. ));
  2127. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2128. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2129. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2130. $result = $this->Form->input('Model.start_time', array(
  2131. 'type' => 'time',
  2132. 'timeFormat' => '12',
  2133. 'interval' => 10,
  2134. 'selected' => '2013-05-19 00:33:00'
  2135. ));
  2136. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2137. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2138. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => '12',
  2142. 'interval' => 10,
  2143. 'selected' => '2013-05-19 13:33:00'
  2144. ));
  2145. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 10,
  2152. 'selected' => '2013-05-19 01:33:00'
  2153. ));
  2154. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2157. }
  2158. /**
  2159. * test form->input() with datetime, date and time types
  2160. *
  2161. * @return void
  2162. */
  2163. public function testInputDatetime() {
  2164. extract($this->dateRegex);
  2165. $result = $this->Form->input('prueba', array(
  2166. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2167. 'maxYear' => date('Y') + 1, 'interval' => 15
  2168. ));
  2169. $result = explode(':', $result);
  2170. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2171. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2172. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2174. //related to ticket #5013
  2175. $result = $this->Form->input('Contact.date', array(
  2176. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2177. ));
  2178. $this->assertRegExp('/class="customClass"/', $result);
  2179. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2180. $result = $this->Form->input('Contact.date', array(
  2181. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2182. ));
  2183. $this->assertRegExp('/id="customIdDay"/', $result);
  2184. $this->assertRegExp('/id="customIdMonth"/', $result);
  2185. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2186. $result = $this->Form->input('Model.field', array(
  2187. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2188. ));
  2189. $this->assertRegExp('/id="customIDDay"/', $result);
  2190. $this->assertRegExp('/id="customIDHour"/', $result);
  2191. $result = explode('</select><select', $result);
  2192. $result = explode(':', $result[1]);
  2193. $this->assertRegExp('/option value="23"/', $result[0]);
  2194. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2195. $result = $this->Form->input('Model.field', array(
  2196. 'type' => 'datetime', 'timeFormat' => 12
  2197. ));
  2198. $result = explode('</select><select', $result);
  2199. $result = explode(':', $result[1]);
  2200. $this->assertRegExp('/option value="12"/', $result[0]);
  2201. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2202. $this->Form->request->data = array('Contact' => array('created' => null));
  2203. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input date'),
  2206. 'label' => array('for' => 'ContactCreatedMonth'),
  2207. 'Created',
  2208. '/label',
  2209. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2210. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2211. $monthsRegex,
  2212. '/select', '-',
  2213. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2214. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2215. $daysRegex,
  2216. '/select', '-',
  2217. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2218. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2219. $yearsRegex,
  2220. '/select',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $this->Form->request->data = array('Contact' => array('created' => null));
  2225. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2226. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2230. $result = $this->Form->input('Contact.created', array(
  2231. 'type' => 'date',
  2232. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2233. 'timeFormat' => 'NONE'
  2234. ));
  2235. $this->assertRegExp('/for\="created-month"/', $result);
  2236. }
  2237. /**
  2238. * Test generating checkboxes in a loop.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputCheckboxesInLoop() {
  2243. for ($i = 1; $i < 5; $i++) {
  2244. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input checkbox'),
  2247. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2248. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2249. 'label' => array('for' => "Contact{$i}Email"),
  2250. 'Email',
  2251. '/label',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. }
  2257. /**
  2258. * Test generating checkboxes with disabled elements.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputCheckboxWithDisabledElements() {
  2263. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2264. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2265. $expected = array(
  2266. array('div' => array('class' => 'input select')),
  2267. array('label' => array('for' => "ContactMultiple")),
  2268. 'Multiple',
  2269. '/label',
  2270. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2273. array('label' => array('for' => "ContactMultiple1")),
  2274. 'One',
  2275. '/label',
  2276. '/div',
  2277. array('div' => array('class' => 'checkbox')),
  2278. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2279. array('label' => array('for' => "ContactMultiple2")),
  2280. 'Two',
  2281. '/label',
  2282. '/div',
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2285. array('label' => array('for' => "ContactMultiple3")),
  2286. 'Three',
  2287. '/label',
  2288. '/div',
  2289. '/div'
  2290. );
  2291. $this->assertTags($result, $expected);
  2292. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2293. $this->assertTags($result, $expected);
  2294. $disabled = array('2', 3);
  2295. $expected = array(
  2296. array('div' => array('class' => 'input select')),
  2297. array('label' => array('for' => "ContactMultiple")),
  2298. 'Multiple',
  2299. '/label',
  2300. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2301. array('div' => array('class' => 'checkbox')),
  2302. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2303. array('label' => array('for' => "ContactMultiple1")),
  2304. 'One',
  2305. '/label',
  2306. '/div',
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2309. array('label' => array('for' => "ContactMultiple2")),
  2310. 'Two',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2315. array('label' => array('for' => "ContactMultiple3")),
  2316. 'Three',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2322. $this->assertTags($result, $expected);
  2323. // make sure 50 does only disable 50, and not 50f5c0cf
  2324. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2325. $disabled = array(50);
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => "ContactMultiple")),
  2329. 'Multiple',
  2330. '/label',
  2331. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2334. array('label' => array('for' => "ContactMultiple50")),
  2335. 'Fifty',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2340. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2341. 'Stringy',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2347. $this->assertTags($result, $expected);
  2348. }
  2349. /**
  2350. * test input name with leading integer, ensure attributes are generated correctly.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testInputWithLeadingInteger() {
  2355. $result = $this->Form->text('0.Node.title');
  2356. $expected = array(
  2357. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test form->input() with select type inputs.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputSelectType() {
  2367. $result = $this->Form->input('email', array(
  2368. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2369. );
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'email'),
  2373. 'Email',
  2374. '/label',
  2375. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2376. array('option' => array('value' => '')),
  2377. '/option',
  2378. array('option' => array('value' => 'è')),
  2379. 'Firést',
  2380. '/option',
  2381. array('option' => array('value' => 'é')),
  2382. 'Secoènd',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $result = $this->Form->input('email', array(
  2389. 'options' => array('First', 'Second'), 'empty' => true)
  2390. );
  2391. $expected = array(
  2392. 'div' => array('class' => 'input select'),
  2393. 'label' => array('for' => 'email'),
  2394. 'Email',
  2395. '/label',
  2396. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2397. array('option' => array('value' => '')),
  2398. '/option',
  2399. array('option' => array('value' => '0')),
  2400. 'First',
  2401. '/option',
  2402. array('option' => array('value' => '1')),
  2403. 'Second',
  2404. '/option',
  2405. '/select',
  2406. '/div'
  2407. );
  2408. $this->assertTags($result, $expected);
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2411. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2412. $expected = array(
  2413. 'div' => array('class' => 'input select'),
  2414. 'label' => array('for' => 'ModelUserId'),
  2415. 'User',
  2416. '/label',
  2417. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2418. array('option' => array('value' => '')),
  2419. '/option',
  2420. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2421. 'good',
  2422. '/option',
  2423. array('option' => array('value' => 'other')),
  2424. 'bad',
  2425. '/option',
  2426. '/select',
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2431. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2432. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2433. $expected = array(
  2434. 'div' => array('class' => 'input select'),
  2435. 'label' => array('for' => 'ThingUserId'),
  2436. 'User',
  2437. '/label',
  2438. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2439. array('option' => array('value' => '')),
  2440. 'Some Empty',
  2441. '/option',
  2442. array('option' => array('value' => 'value')),
  2443. 'good',
  2444. '/option',
  2445. array('option' => array('value' => 'other')),
  2446. 'bad',
  2447. '/option',
  2448. '/select',
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2453. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2454. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2455. $expected = array(
  2456. 'div' => array('class' => 'input select'),
  2457. 'label' => array('for' => 'ThingUserId'),
  2458. 'User',
  2459. '/label',
  2460. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2461. array('option' => array('value' => '')),
  2462. 'Some Empty',
  2463. '/option',
  2464. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2465. 'good',
  2466. '/option',
  2467. array('option' => array('value' => 'other')),
  2468. 'bad',
  2469. '/option',
  2470. '/select',
  2471. '/div'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2475. $this->Form->request->data = array('User' => array('User' => array('value')));
  2476. $result = $this->Form->input('User.User', array('empty' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input select'),
  2479. 'label' => array('for' => 'UserUser'),
  2480. 'User',
  2481. '/label',
  2482. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2483. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2484. array('option' => array('value' => '')),
  2485. '/option',
  2486. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2487. 'good',
  2488. '/option',
  2489. array('option' => array('value' => 'other')),
  2490. 'bad',
  2491. '/option',
  2492. '/select',
  2493. '/div'
  2494. );
  2495. $this->assertTags($result, $expected);
  2496. $this->Form->data = array();
  2497. $result = $this->Form->input('Publisher.id', array(
  2498. 'label' => 'Publisher',
  2499. 'type' => 'select',
  2500. 'multiple' => 'checkbox',
  2501. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2502. ));
  2503. $expected = array(
  2504. array('div' => array('class' => 'input select')),
  2505. array('label' => array('for' => 'PublisherId')),
  2506. 'Publisher',
  2507. '/label',
  2508. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2509. array('div' => array('class' => 'checkbox')),
  2510. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2511. array('label' => array('for' => 'PublisherIdValue1')),
  2512. 'Label 1',
  2513. '/label',
  2514. '/div',
  2515. array('div' => array('class' => 'checkbox')),
  2516. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2517. array('label' => array('for' => 'PublisherIdValue2')),
  2518. 'Label 2',
  2519. '/label',
  2520. '/div',
  2521. '/div'
  2522. );
  2523. $this->assertTags($result, $expected);
  2524. }
  2525. /**
  2526. * test that input() and a non standard primary key makes a hidden input by default.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2531. $this->Form->create('User');
  2532. $this->Form->fieldset = array(
  2533. 'User' => array(
  2534. 'fields' => array(
  2535. 'model_id' => array('type' => 'integer')
  2536. ),
  2537. 'validates' => array(),
  2538. 'key' => 'model_id'
  2539. )
  2540. );
  2541. $result = $this->Form->input('model_id');
  2542. $expected = array(
  2543. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. }
  2547. /**
  2548. * test that overriding the magic select type widget is possible
  2549. *
  2550. * @return void
  2551. */
  2552. public function testInputOverridingMagicSelectType() {
  2553. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2554. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2555. $expected = array(
  2556. 'div' => array('class' => 'input text'),
  2557. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2558. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2559. '/div'
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. //Check that magic types still work for plural/singular vars
  2563. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2564. $result = $this->Form->input('Model.type');
  2565. $expected = array(
  2566. 'div' => array('class' => 'input select'),
  2567. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2568. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2569. array('option' => array('value' => 'value')), 'good', '/option',
  2570. array('option' => array('value' => 'other')), 'bad', '/option',
  2571. '/select',
  2572. '/div'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. }
  2576. /**
  2577. * Test that magic input() selects are created for type=number
  2578. *
  2579. * @return void
  2580. */
  2581. public function testInputMagicSelectForTypeNumber() {
  2582. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2583. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2584. $result = $this->Form->input('ValidateUser.balance');
  2585. $expected = array(
  2586. 'div' => array('class' => 'input select'),
  2587. 'label' => array('for' => 'ValidateUserBalance'),
  2588. 'Balance',
  2589. '/label',
  2590. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2591. array('option' => array('value' => '0')),
  2592. 'nothing',
  2593. '/option',
  2594. array('option' => array('value' => '1', 'selected' => 'selected')),
  2595. 'some',
  2596. '/option',
  2597. array('option' => array('value' => '100')),
  2598. 'a lot',
  2599. '/option',
  2600. '/select',
  2601. '/div'
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. }
  2605. /**
  2606. * Test that magic input() selects can easily be converted into radio types without error.
  2607. *
  2608. * @return void
  2609. */
  2610. public function testInputMagicSelectChangeToRadio() {
  2611. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2612. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2613. $this->assertRegExp('/input type="radio"/', $result);
  2614. }
  2615. /**
  2616. * fields with the same name as the model should work.
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputWithMatchingFieldAndModelName() {
  2621. $this->Form->create('User');
  2622. $this->Form->fieldset = array(
  2623. 'User' => array(
  2624. 'fields' => array(
  2625. 'User' => array('type' => 'text')
  2626. ),
  2627. 'validates' => array(),
  2628. 'key' => 'id'
  2629. )
  2630. );
  2631. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2632. $result = $this->Form->input('User', array('type' => 'text'));
  2633. $expected = array(
  2634. 'div' => array('class' => 'input text'),
  2635. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2636. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2637. '/div'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputs() {
  2649. $this->Form->create('Contact');
  2650. $result = $this->Form->inputs('The Legend');
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2660. $expected = array(
  2661. 'fieldset' => array('class' => 'classy-stuff'),
  2662. '<legend',
  2663. 'Field of Dreams',
  2664. '/legend',
  2665. '*/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $this->Form->request['prefix'] = 'admin';
  2674. $this->Form->request['action'] = 'admin_edit';
  2675. $result = $this->Form->inputs();
  2676. $expected = array(
  2677. '<fieldset',
  2678. '<legend',
  2679. 'Edit Contact',
  2680. '/legend',
  2681. '*/fieldset',
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $result = $this->Form->inputs(false);
  2686. $expected = array(
  2687. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2688. array('div' => array('class' => 'input text')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input email')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input tel')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input password')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input datetime')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input number')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input select')),
  2705. '*/div',
  2706. );
  2707. $this->assertTags($result, $expected);
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2710. $expected = array(
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. );
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2737. $expected = array(
  2738. 'fieldset' => array(),
  2739. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input email')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input tel')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input password')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input datetime')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input number')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input select')),
  2757. '*/div',
  2758. '/fieldset'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $this->Form->create('Contact');
  2762. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2763. $expected = array(
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input email')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input tel')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. );
  2784. $this->assertTags($result, $expected);
  2785. $this->Form->create('Contact');
  2786. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs('Hello');
  2790. $expected = array(
  2791. 'fieldset' => array(),
  2792. 'legend' => array(),
  2793. 'Hello',
  2794. '/legend',
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. '/fieldset'
  2815. );
  2816. $this->assertTags($result, $expected);
  2817. $this->Form->create('Contact');
  2818. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2819. $expected = array(
  2820. 'fieldset' => array(),
  2821. 'legend' => array(),
  2822. 'Hello',
  2823. '/legend',
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. '/fieldset'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->end();
  2850. $this->Form->create(false);
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. array('div' => array('class' => 'input text')),
  2854. 'label' => array('for' => 'foo'),
  2855. 'Foo',
  2856. '/label',
  2857. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2858. '*/div',
  2859. '/fieldset'
  2860. );
  2861. $result = $this->Form->inputs(
  2862. array('foo' => array('type' => 'text')),
  2863. array(),
  2864. array('legend' => false)
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. }
  2868. /**
  2869. * Tests inputs() works with plugin models
  2870. *
  2871. * @return void
  2872. */
  2873. public function testInputsPluginModel() {
  2874. $this->loadFixtures('Post');
  2875. App::build(array(
  2876. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2877. ));
  2878. CakePlugin::load('TestPlugin');
  2879. $this->Form->request['models'] = array(
  2880. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2881. );
  2882. $this->Form->create('TestPlugin.TestPluginPost');
  2883. $result = $this->Form->inputs();
  2884. $this->assertContains('data[TestPluginPost][id]', $result);
  2885. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2886. $this->assertContains('data[TestPluginPost][title]', $result);
  2887. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2888. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2889. $this->assertEquals('TestPluginPost', $this->Form->model());
  2890. }
  2891. /**
  2892. * testSelectAsCheckbox method
  2893. *
  2894. * test multi-select widget with checkbox formatting.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSelectAsCheckbox() {
  2899. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2902. array('div' => array('class' => 'checkbox')),
  2903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2904. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2905. 'first',
  2906. '/label',
  2907. '/div',
  2908. array('div' => array('class' => 'checkbox')),
  2909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2910. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2911. 'second',
  2912. '/label',
  2913. '/div',
  2914. array('div' => array('class' => 'checkbox')),
  2915. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2916. array('label' => array('for' => 'ModelMultiField2')),
  2917. 'third',
  2918. '/label',
  2919. '/div',
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2923. $expected = array(
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2927. array('label' => array('for' => 'ModelMultiField12')),
  2928. 'half',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. }
  2934. /**
  2935. * testLabel method
  2936. *
  2937. * test label generation.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testLabel() {
  2942. $this->Form->text('Person.name');
  2943. $result = $this->Form->label();
  2944. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2958. $result = $this->Form->label('Person.2.name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2960. }
  2961. /**
  2962. * testTextbox method
  2963. *
  2964. * test textbox element generation
  2965. *
  2966. * @return void
  2967. */
  2968. public function testTextbox() {
  2969. $result = $this->Form->text('Model.field');
  2970. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2974. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2975. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2976. $result = $this->Form->text('Model.text');
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2978. $Contact = ClassRegistry::getObject('Contact');
  2979. $Contact->validationErrors['text'] = array(true);
  2980. $this->Form->request->data['Contact']['text'] = 'test';
  2981. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2984. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2985. $expected = array(
  2986. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. }
  2990. /**
  2991. * testDefaultValue method
  2992. *
  2993. * Test default value setting
  2994. *
  2995. * @return void
  2996. */
  2997. public function testDefaultValue() {
  2998. $this->Form->request->data['Model']['field'] = 'test';
  2999. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3000. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3001. unset($this->Form->request->data['Model']['field']);
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3004. }
  3005. /**
  3006. * testCheckboxDefaultValue method
  3007. *
  3008. * Test default value setting on checkbox() method
  3009. *
  3010. * @return void
  3011. */
  3012. public function testCheckboxDefaultValue() {
  3013. $this->Form->request->data['Model']['field'] = false;
  3014. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3015. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3016. unset($this->Form->request->data['Model']['field']);
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3019. $this->Form->request->data['Model']['field'] = true;
  3020. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. unset($this->Form->request->data['Model']['field']);
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3025. }
  3026. /**
  3027. * testError method
  3028. *
  3029. * Test field error generation
  3030. *
  3031. * @return void
  3032. */
  3033. public function testError() {
  3034. $Contact = ClassRegistry::getObject('Contact');
  3035. $Contact->validationErrors['field'] = array(1);
  3036. $result = $this->Form->error('Contact.field');
  3037. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3038. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3039. $this->assertEquals('Error in field Field', $result);
  3040. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('This field contains invalid input', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3046. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3051. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3052. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3053. $this->assertEquals('<strong>Badness!</strong>', $result);
  3054. $Contact->validationErrors['field'] = array("email");
  3055. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3056. $expected = array(
  3057. 'div' => array('class' => 'field-error'),
  3058. 'No good!',
  3059. '/div'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3063. $result = $this->Form->error('Contact.field', array(
  3064. 'notEmpty' => 'Cannot be empty',
  3065. 'email' => 'No good!'
  3066. ));
  3067. $expected = array(
  3068. 'div' => array('class' => 'error-message'),
  3069. 'ul' => array(),
  3070. '<li', 'Cannot be empty', '/li',
  3071. '<li', 'No good!', '/li',
  3072. '<li', 'Something else', '/li',
  3073. '/ul',
  3074. '/div'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. // Testing error messages list options
  3078. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3079. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3080. $expected = array(
  3081. 'div' => array('class' => 'error-message'),
  3082. 'ol' => array(),
  3083. '<li', 'notEmpty', '/li',
  3084. '<li', 'email', '/li',
  3085. '/ol',
  3086. '/div'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3090. $expected = array(
  3091. 'div' => array('class' => 'error-message'),
  3092. 'ol' => array(),
  3093. '<li', 'notEmpty', '/li',
  3094. '<li', 'email', '/li',
  3095. '/ol',
  3096. '/div'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $result = $this->Form->error('Contact.field', null, array(
  3100. 'listOptions' => array(
  3101. 'class' => 'ul-class',
  3102. 'itemOptions' => array(
  3103. 'class' => 'li-class'
  3104. )
  3105. )
  3106. ));
  3107. $expected = array(
  3108. 'div' => array('class' => 'error-message'),
  3109. 'ul' => array('class' => 'ul-class'),
  3110. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3111. array('li' => array('class' => 'li-class')), 'email', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. }
  3117. /**
  3118. * test error options when using form->input();
  3119. *
  3120. * @return void
  3121. */
  3122. public function testInputErrorEscape() {
  3123. $this->Form->create('ValidateProfile');
  3124. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3125. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3126. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3127. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3128. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3129. $this->assertRegExp('/required<br>/', $result);
  3130. }
  3131. /**
  3132. * testPassword method
  3133. *
  3134. * Test password element generation
  3135. *
  3136. * @return void
  3137. */
  3138. public function testPassword() {
  3139. $Contact = ClassRegistry::getObject('Contact');
  3140. $result = $this->Form->password('Contact.field');
  3141. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3142. $Contact->validationErrors['passwd'] = 1;
  3143. $this->Form->request->data['Contact']['passwd'] = 'test';
  3144. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3145. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3146. }
  3147. /**
  3148. * testRadio method
  3149. *
  3150. * Test radio element set generation
  3151. *
  3152. * @return void
  3153. */
  3154. public function testRadio() {
  3155. $result = $this->Form->radio('Model.field', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. 'label' => array('for' => 'ModelField0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3168. 'label' => array('for' => 'ModelField12'),
  3169. 'half',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3199. array('label' => array('for' => 'ModelField0')),
  3200. 'option A',
  3201. '/label',
  3202. 'br' => array(),
  3203. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3204. array('label' => array('for' => 'ModelField1')),
  3205. 'option B',
  3206. '/label',
  3207. '/fieldset'
  3208. );
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Gender',
  3215. '/legend',
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3218. array('label' => array('for' => 'EmployeeGenderMale')),
  3219. 'Male',
  3220. '/label',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3222. array('label' => array('for' => 'EmployeeGenderFemale')),
  3223. 'Female',
  3224. '/label',
  3225. '/fieldset',
  3226. );
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Gender',
  3233. '/legend',
  3234. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3236. array('label' => array('for' => 'OfficerGenderMale')),
  3237. 'Male',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3240. array('label' => array('for' => 'OfficerGenderFemale')),
  3241. 'Female',
  3242. '/label',
  3243. '/fieldset',
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3247. $expected = array(
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3249. array('input' => array(
  3250. 'type' => 'radio',
  3251. 'name' => 'data[Contact][1][imrequired]',
  3252. 'value' => '0',
  3253. 'id' => 'Contact1Imrequired0',
  3254. 'required' => 'required'
  3255. )),
  3256. 'label' => array('for' => 'Contact1Imrequired0'),
  3257. 'option A',
  3258. '/label'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Model.1.field', array('option A'));
  3262. $expected = array(
  3263. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3271. $expected = array(
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'option A',
  3280. '/label',
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3282. array('label' => array('for' => 'ModelField1')),
  3283. 'option B',
  3284. '/label',
  3285. '/fieldset'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. }
  3289. /**
  3290. * Test radio inputs with between as string or array. Also ensure
  3291. * that an array with less between elements works.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioBetween() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('between' => 'I am between')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. 'I am between',
  3307. 'input' => array(
  3308. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3309. 'value' => '', 'id' => 'ModelField_'
  3310. ),
  3311. array('input' => array(
  3312. 'type' => 'radio', 'name' => 'data[Model][field]',
  3313. 'value' => '0', 'id' => 'ModelField0'
  3314. )),
  3315. array('label' => array('for' => 'ModelField0')),
  3316. 'option A',
  3317. '/label',
  3318. array('input' => array(
  3319. 'type' => 'radio', 'name' => 'data[Model][field]',
  3320. 'value' => '1', 'id' => 'ModelField1'
  3321. )),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B', 'option C'),
  3331. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. 'input' => array(
  3339. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3340. 'value' => '', 'id' => 'ModelField_'
  3341. ),
  3342. array('input' => array(
  3343. 'type' => 'radio', 'name' => 'data[Model][field]',
  3344. 'value' => '0', 'id' => 'ModelField0'
  3345. )),
  3346. array('label' => array('for' => 'ModelField0')),
  3347. 'option A',
  3348. '/label',
  3349. 'between A',
  3350. '--separator--',
  3351. array('input' => array(
  3352. 'type' => 'radio', 'name' => 'data[Model][field]',
  3353. 'value' => '1', 'id' => 'ModelField1'
  3354. )),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. 'between B',
  3359. '--separator--',
  3360. array('input' => array(
  3361. 'type' => 'radio', 'name' => 'data[Model][field]',
  3362. 'value' => '2', 'id' => 'ModelField2'
  3363. )),
  3364. array('label' => array('for' => 'ModelField2')),
  3365. 'option C',
  3366. '/label',
  3367. 'between C',
  3368. '/fieldset'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->input('Model.field', array(
  3372. 'options' => array('1' => 'first', '2' => 'second'),
  3373. 'type' => 'radio',
  3374. 'before' => '--before--',
  3375. 'after' => '--after--',
  3376. 'separator' => '--separator--',
  3377. 'between' => array('--between first--', '--between second--')
  3378. ));
  3379. $expected = array(
  3380. 'div' => array('class' => 'input radio'),
  3381. '--before--',
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3387. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3388. array('label' => array('for' => 'ModelField1')),
  3389. 'first',
  3390. '/label',
  3391. '--between first--',
  3392. '--separator--',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3394. array('label' => array('for' => 'ModelField2')),
  3395. 'second',
  3396. '/label',
  3397. '--between second--',
  3398. '/fieldset',
  3399. '--after--',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->input('Model.field', array(
  3404. 'options' => array('1' => 'first', '2' => 'second'),
  3405. 'type' => 'radio',
  3406. 'before' => '--before--',
  3407. 'after' => '--after--',
  3408. 'separator' => '--separator--',
  3409. 'between' => array('--between first--')
  3410. ));
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. '--before--',
  3414. 'fieldset' => array(),
  3415. 'legend' => array(),
  3416. 'Field',
  3417. '/legend',
  3418. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3420. array('label' => array('for' => 'ModelField1')),
  3421. 'first',
  3422. '/label',
  3423. '--between first--',
  3424. '--separator--',
  3425. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3426. array('label' => array('for' => 'ModelField2')),
  3427. 'second',
  3428. '/label',
  3429. '/fieldset',
  3430. '--after--',
  3431. '/div'
  3432. );
  3433. $this->assertTags($result, $expected);
  3434. }
  3435. /**
  3436. * Test that radios with a 0 value are selected under the correct conditions.
  3437. * Also ensure that values that are booleanish are handled correctly.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testRadioOptionWithBooleanishValues() {
  3442. $expected = array(
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'Yes',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3452. array('label' => array('for' => 'ModelField0')),
  3453. 'No',
  3454. '/label',
  3455. '/fieldset'
  3456. );
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3462. $this->assertTags($result, $expected);
  3463. $expected = array(
  3464. 'fieldset' => array(),
  3465. 'legend' => array(),
  3466. 'Field',
  3467. '/legend',
  3468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3469. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3470. array('label' => array('for' => 'ModelField1')),
  3471. 'Yes',
  3472. '/label',
  3473. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3474. array('label' => array('for' => 'ModelField0')),
  3475. 'No',
  3476. '/label',
  3477. '/fieldset'
  3478. );
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3484. $this->assertTags($result, $expected);
  3485. $expected = array(
  3486. 'fieldset' => array(),
  3487. 'legend' => array(),
  3488. 'Field',
  3489. '/legend',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3491. array('label' => array('for' => 'ModelField1')),
  3492. 'Yes',
  3493. '/label',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3495. array('label' => array('for' => 'ModelField0')),
  3496. 'No',
  3497. '/label',
  3498. '/fieldset'
  3499. );
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3503. $this->assertTags($result, $expected);
  3504. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3505. $this->assertTags($result, $expected);
  3506. }
  3507. /**
  3508. * test disabled radio options
  3509. *
  3510. * @return void
  3511. */
  3512. public function testRadioDisabled() {
  3513. $result = $this->Form->radio(
  3514. 'Model.field',
  3515. array('option A', 'option B'),
  3516. array('disabled' => array(0), 'value' => '0')
  3517. );
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3524. array('label' => array('for' => 'ModelField0')),
  3525. 'option A',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3528. array('label' => array('for' => 'ModelField1')),
  3529. 'option B',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->radio(
  3535. 'Model.field',
  3536. array('option A', 'option B'),
  3537. array('disabled' => true, 'value' => '0')
  3538. );
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3545. array('label' => array('for' => 'ModelField0')),
  3546. 'option A',
  3547. '/label',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'option B',
  3551. '/label',
  3552. '/fieldset'
  3553. );
  3554. $this->assertTags($result, $expected);
  3555. $result = $this->Form->radio(
  3556. 'Model.field',
  3557. array('option A', 'option B'),
  3558. array('disabled' => 'disabled', 'value' => '0')
  3559. );
  3560. $expected = array(
  3561. 'fieldset' => array(),
  3562. 'legend' => array(),
  3563. 'Field',
  3564. '/legend',
  3565. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3566. array('label' => array('for' => 'ModelField0')),
  3567. 'option A',
  3568. '/label',
  3569. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3570. array('label' => array('for' => 'ModelField1')),
  3571. 'option B',
  3572. '/label',
  3573. '/fieldset'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->input('Model.field', array(
  3577. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3578. 'type' => 'radio',
  3579. 'disabled' => array(2, '3x'),
  3580. ));
  3581. $expected = array(
  3582. 'div' => array('class' => 'input radio'),
  3583. 'fieldset' => array(),
  3584. 'legend' => array(),
  3585. 'Field',
  3586. '/legend',
  3587. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3588. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3589. array('label' => array('for' => 'ModelField1')),
  3590. 'first',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3593. array('label' => array('for' => 'ModelField2')),
  3594. 'second',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3597. array('label' => array('for' => 'ModelField2x')),
  3598. '2x',
  3599. '/label',
  3600. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3601. array('label' => array('for' => 'ModelField3')),
  3602. 'third',
  3603. '/label',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3605. array('label' => array('for' => 'ModelField3x')),
  3606. '3x',
  3607. '/label',
  3608. '/fieldset',
  3609. '/div'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. $result = $this->Form->input('Model.field', array(
  3613. 'type' => 'radio',
  3614. 'options' => array(
  3615. 1 => 'A',
  3616. 2 => 'B',
  3617. 3 => 'C'
  3618. ),
  3619. 'disabled' => array(1)
  3620. ));
  3621. $expected = array(
  3622. 'div' => array('class' => 'input radio'),
  3623. 'fieldset' => array(),
  3624. 'legend' => array(),
  3625. 'Field',
  3626. '/legend',
  3627. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3628. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3629. array('label' => array('for' => 'ModelField1')),
  3630. 'A',
  3631. '/label',
  3632. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3633. array('label' => array('for' => 'ModelField2')),
  3634. 'B',
  3635. '/label',
  3636. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3637. array('label' => array('for' => 'ModelField3')),
  3638. 'C',
  3639. '/label',
  3640. '/fieldset',
  3641. '/div'
  3642. );
  3643. $this->assertTags($result, $expected);
  3644. }
  3645. /**
  3646. * test disabling the hidden input for radio buttons
  3647. *
  3648. * @return void
  3649. */
  3650. public function testRadioHiddenInputDisabling() {
  3651. $result = $this->Form->input('Model.1.field', array(
  3652. 'type' => 'radio',
  3653. 'options' => array('option A'),
  3654. 'hiddenField' => false
  3655. )
  3656. );
  3657. $expected = array(
  3658. 'div' => array('class' => 'input radio'),
  3659. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3660. 'label' => array('for' => 'Model1Field0'),
  3661. 'option A',
  3662. '/label',
  3663. '/div'
  3664. );
  3665. $this->assertTags($result, $expected);
  3666. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3667. $expected = array(
  3668. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3669. 'label' => array('for' => 'Model1Field0'),
  3670. 'option A',
  3671. '/label'
  3672. );
  3673. $this->assertTags($result, $expected);
  3674. }
  3675. /**
  3676. * test adding an empty option for radio buttons
  3677. *
  3678. * @return void
  3679. */
  3680. public function testRadioAddEmptyOption() {
  3681. $result = $this->Form->input('Model.1.field', array(
  3682. 'type' => 'radio',
  3683. 'options' => array('option A'),
  3684. 'empty' => true,
  3685. 'hiddenField' => false
  3686. ));
  3687. $expected = array(
  3688. 'div' => array('class' => 'input radio'),
  3689. 'fieldset' => array(),
  3690. 'legend' => array(),
  3691. 'Field',
  3692. '/legend',
  3693. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3694. array('label' => array('for' => 'Model1Field')),
  3695. __('empty'),
  3696. '/label',
  3697. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3698. array('label' => array('for' => 'Model1Field0')),
  3699. 'option A',
  3700. '/label',
  3701. '/fieldset',
  3702. '/div'
  3703. );
  3704. $this->assertTags($result, $expected);
  3705. $result = $this->Form->input('Model.1.field', array(
  3706. 'type' => 'radio',
  3707. 'options' => array('option A'),
  3708. 'empty' => 'CustomEmptyLabel',
  3709. 'hiddenField' => false
  3710. ));
  3711. $expected = array(
  3712. 'div' => array('class' => 'input radio'),
  3713. 'fieldset' => array(),
  3714. 'legend' => array(),
  3715. 'Field',
  3716. '/legend',
  3717. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3718. array('label' => array('for' => 'Model1Field')),
  3719. 'CustomEmptyLabel',
  3720. '/label',
  3721. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3722. array('label' => array('for' => 'Model1Field0')),
  3723. 'option A',
  3724. '/label',
  3725. '/fieldset',
  3726. '/div'
  3727. );
  3728. $this->assertTags($result, $expected);
  3729. $result = $this->Form->input('Model.1.field', array(
  3730. 'type' => 'radio',
  3731. 'options' => array('option A'),
  3732. 'empty' => false,
  3733. 'hiddenField' => false
  3734. ));
  3735. $this->assertTextNotContains('"Model1Field"', $result);
  3736. }
  3737. /**
  3738. * Test that radio() accepts an array for label
  3739. *
  3740. * @return void
  3741. */
  3742. public function testRadioLabelArray() {
  3743. $result = $this->Form->input('Model.field', array(
  3744. 'type' => 'radio',
  3745. 'legend' => false,
  3746. 'label' => array(
  3747. 'class' => 'checkbox float-left',
  3748. ),
  3749. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3750. ));
  3751. $this->assertTextContains(
  3752. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3753. $result
  3754. );
  3755. }
  3756. /**
  3757. * Test that label id's match the input element id's when radio is called after create().
  3758. *
  3759. * @return void
  3760. */
  3761. public function testRadioWithCreate() {
  3762. $this->Form->create('Model');
  3763. $result = $this->Form->radio('recipient',
  3764. array('1' => '1', '2' => '2', '3' => '3'),
  3765. array('legend' => false, 'value' => '1')
  3766. );
  3767. $this->assertTextNotContains(
  3768. '<label for="ModelModelRecipient1">1</label>',
  3769. $result
  3770. );
  3771. $this->assertTextContains(
  3772. '<label for="ModelRecipient1">1</label>',
  3773. $result
  3774. );
  3775. }
  3776. /**
  3777. * testSelect method
  3778. *
  3779. * Test select element generation.
  3780. *
  3781. * @return void
  3782. */
  3783. public function testSelect() {
  3784. $result = $this->Form->select('Model.field', array());
  3785. $expected = array(
  3786. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3787. array('option' => array('value' => '')),
  3788. '/option',
  3789. '/select'
  3790. );
  3791. $this->assertTags($result, $expected);
  3792. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3793. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3794. $expected = array(
  3795. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3796. array('option' => array('value' => '')),
  3797. '/option',
  3798. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3799. 'good',
  3800. '/option',
  3801. array('option' => array('value' => 'other')),
  3802. 'bad',
  3803. '/option',
  3804. '/select'
  3805. );
  3806. $this->assertTags($result, $expected);
  3807. $this->Form->request->data = array();
  3808. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3809. $expected = array(
  3810. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3811. array('option' => array('value' => '')),
  3812. '/option',
  3813. array('option' => array('value' => 'value')),
  3814. 'good',
  3815. '/option',
  3816. array('option' => array('value' => 'other')),
  3817. 'bad',
  3818. '/option',
  3819. '/select'
  3820. );
  3821. $this->assertTags($result, $expected);
  3822. $result = $this->Form->select(
  3823. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3824. array('empty' => false)
  3825. );
  3826. $expected = array(
  3827. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3828. array('option' => array('value' => 'first')),
  3829. 'first &quot;html&quot; &lt;chars&gt;',
  3830. '/option',
  3831. array('option' => array('value' => 'second')),
  3832. 'value',
  3833. '/option',
  3834. '/select'
  3835. );
  3836. $this->assertTags($result, $expected);
  3837. $result = $this->Form->select(
  3838. 'Model.field',
  3839. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3840. array('escape' => false, 'empty' => false)
  3841. );
  3842. $expected = array(
  3843. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3844. array('option' => array('value' => 'first')),
  3845. 'first "html" <chars>',
  3846. '/option',
  3847. array('option' => array('value' => 'second')),
  3848. 'value',
  3849. '/option',
  3850. '/select'
  3851. );
  3852. $this->assertTags($result, $expected);
  3853. $options = array(
  3854. array('value' => 'first', 'name' => 'First'),
  3855. array('value' => 'first', 'name' => 'Another First'),
  3856. );
  3857. $result = $this->Form->select(
  3858. 'Model.field',
  3859. $options,
  3860. array('escape' => false, 'empty' => false)
  3861. );
  3862. $expected = array(
  3863. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3864. array('option' => array('value' => 'first')),
  3865. 'First',
  3866. '/option',
  3867. array('option' => array('value' => 'first')),
  3868. 'Another First',
  3869. '/option',
  3870. '/select'
  3871. );
  3872. $this->assertTags($result, $expected);
  3873. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3874. $result = $this->Form->select(
  3875. 'Model.contact_id',
  3876. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3877. array('escape' => false, 'empty' => 'pick something')
  3878. );
  3879. $expected = array(
  3880. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3881. array('option' => array('value' => '')), 'pick something', '/option',
  3882. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3883. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3884. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3885. '/select'
  3886. );
  3887. $this->assertTags($result, $expected);
  3888. $this->Form->request->data['Model']['field'] = 0;
  3889. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3890. $expected = array(
  3891. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3892. array('option' => array('value' => '')), '/option',
  3893. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3894. array('option' => array('value' => '1')), 'Yes', '/option',
  3895. '/select'
  3896. );
  3897. $this->assertTags($result, $expected);
  3898. $this->Form->request->data['Model']['field'] = 50;
  3899. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3900. $expected = array(
  3901. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3902. array('option' => array('value' => '')), '/option',
  3903. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3904. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3905. '/select'
  3906. );
  3907. $this->assertTags($result, $expected);
  3908. $result = $this->Form->select('Contact.required_one', array('option A'));
  3909. $expected = array(
  3910. 'select' => array(
  3911. 'name' => 'data[Contact][required_one]',
  3912. 'id' => 'ContactRequiredOne',
  3913. 'required' => 'required'
  3914. ),
  3915. array('option' => array('value' => '')), '/option',
  3916. array('option' => array('value' => '0')), 'option A', '/option',
  3917. '/select'
  3918. );
  3919. $this->assertTags($result, $expected);
  3920. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3921. $expected = array(
  3922. 'select' => array(
  3923. 'name' => 'data[Contact][required_one]',
  3924. 'id' => 'ContactRequiredOne',
  3925. 'disabled' => 'disabled'
  3926. ),
  3927. array('option' => array('value' => '')), '/option',
  3928. array('option' => array('value' => '0')), 'option A', '/option',
  3929. '/select'
  3930. );
  3931. $this->assertTags($result, $expected);
  3932. }
  3933. /**
  3934. * test that select() with optiongroups listens to the escape param.
  3935. *
  3936. * @return void
  3937. */
  3938. public function testSelectOptionGroupEscaping() {
  3939. $options = array(
  3940. '>< Key' => array(
  3941. 1 => 'One',
  3942. 2 => 'Two'
  3943. )
  3944. );
  3945. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3946. $expected = array(
  3947. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3948. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3949. array('option' => array('value' => '1')), 'One', '/option',
  3950. array('option' => array('value' => '2')), 'Two', '/option',
  3951. '/optgroup',
  3952. '/select'
  3953. );
  3954. $this->assertTags($result, $expected);
  3955. $options = array(
  3956. '>< Key' => array(
  3957. 1 => 'One',
  3958. 2 => 'Two'
  3959. )
  3960. );
  3961. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3962. $expected = array(
  3963. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3964. 'optgroup' => array('label' => '>< Key'),
  3965. array('option' => array('value' => '1')), 'One', '/option',
  3966. array('option' => array('value' => '2')), 'Two', '/option',
  3967. '/optgroup',
  3968. '/select'
  3969. );
  3970. $this->assertTags($result, $expected);
  3971. }
  3972. /**
  3973. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3974. *
  3975. * @return void
  3976. */
  3977. public function testSelectWithNullAttributes() {
  3978. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3979. $expected = array(
  3980. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3981. array('option' => array('value' => '0')),
  3982. 'first',
  3983. '/option',
  3984. array('option' => array('value' => '1')),
  3985. 'second',
  3986. '/option',
  3987. '/select'
  3988. );
  3989. $this->assertTags($result, $expected);
  3990. }
  3991. /**
  3992. * testNestedSelect method
  3993. *
  3994. * test select element generation with optgroups
  3995. *
  3996. * @return void
  3997. */
  3998. public function testNestedSelect() {
  3999. $result = $this->Form->select(
  4000. 'Model.field',
  4001. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4002. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4003. )), array('empty' => false)
  4004. );
  4005. $expected = array(
  4006. 'select' => array('name' => 'data[Model][field]',
  4007. 'id' => 'ModelField'),
  4008. array('option' => array('value' => 1)),
  4009. 'One',
  4010. '/option',
  4011. array('option' => array('value' => 2)),
  4012. 'Two',
  4013. '/option',
  4014. array('optgroup' => array('label' => 'Three')),
  4015. array('option' => array('value' => 4)),
  4016. 'Four',
  4017. '/option',
  4018. array('option' => array('value' => 5)),
  4019. 'Five',
  4020. '/option',
  4021. '/optgroup',
  4022. '/select'
  4023. );
  4024. $this->assertTags($result, $expected);
  4025. $result = $this->Form->select(
  4026. 'Model.field',
  4027. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4028. array('showParents' => true, 'empty' => false)
  4029. );
  4030. $expected = array(
  4031. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4032. array('option' => array('value' => 1)),
  4033. 'One',
  4034. '/option',
  4035. array('option' => array('value' => 2)),
  4036. 'Two',
  4037. '/option',
  4038. array('optgroup' => array('label' => 'Three')),
  4039. array('option' => array('value' => 3)),
  4040. 'Three',
  4041. '/option',
  4042. array('option' => array('value' => 4)),
  4043. 'Four',
  4044. '/option',
  4045. '/optgroup',
  4046. '/select'
  4047. );
  4048. $this->assertTags($result, $expected);
  4049. }
  4050. /**
  4051. * testSelectMultiple method
  4052. *
  4053. * test generation of multiple select elements
  4054. *
  4055. * @return void
  4056. */
  4057. public function testSelectMultiple() {
  4058. $options = array('first', 'second', 'third');
  4059. $result = $this->Form->select(
  4060. 'Model.multi_field', $options, array('multiple' => true)
  4061. );
  4062. $expected = array(
  4063. 'input' => array(
  4064. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4065. ),
  4066. 'select' => array(
  4067. 'name' => 'data[Model][multi_field][]',
  4068. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4069. ),
  4070. array('option' => array('value' => '0')),
  4071. 'first',
  4072. '/option',
  4073. array('option' => array('value' => '1')),
  4074. 'second',
  4075. '/option',
  4076. array('option' => array('value' => '2')),
  4077. 'third',
  4078. '/option',
  4079. '/select'
  4080. );
  4081. $this->assertTags($result, $expected);
  4082. $result = $this->Form->select(
  4083. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4084. );
  4085. $expected = array(
  4086. 'input' => array(
  4087. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4088. ),
  4089. 'select' => array(
  4090. 'name' => 'data[Model][multi_field][]',
  4091. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4092. ),
  4093. array('option' => array('value' => '0')),
  4094. 'first',
  4095. '/option',
  4096. array('option' => array('value' => '1')),
  4097. 'second',
  4098. '/option',
  4099. array('option' => array('value' => '2')),
  4100. 'third',
  4101. '/option',
  4102. '/select'
  4103. );
  4104. $this->assertTags($result, $expected);
  4105. $result = $this->Form->select(
  4106. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4107. );
  4108. $expected = array(
  4109. 'input' => array(
  4110. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4111. ),
  4112. 'select' => array(
  4113. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4114. 'multiple' => 'multiple'
  4115. ),
  4116. array('option' => array('value' => '0', 'selected' => 'selected')),
  4117. 'first',
  4118. '/option',
  4119. array('option' => array('value' => '1', 'selected' => 'selected')),
  4120. 'second',
  4121. '/option',
  4122. array('option' => array('value' => '2')),
  4123. 'third',
  4124. '/option',
  4125. '/select'
  4126. );
  4127. $this->assertTags($result, $expected);
  4128. $result = $this->Form->select(
  4129. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4130. );
  4131. $expected = array(
  4132. 'select' => array(
  4133. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4134. ),
  4135. array('option' => array('value' => '0', 'selected' => 'selected')),
  4136. 'first',
  4137. '/option',
  4138. array('option' => array('value' => '1', 'selected' => 'selected')),
  4139. 'second',
  4140. '/option',
  4141. array('option' => array('value' => '2')),
  4142. 'third',
  4143. '/option',
  4144. '/select'
  4145. );
  4146. $this->assertTags($result, $expected);
  4147. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4148. $selected = array('2', '3x');
  4149. $result = $this->Form->select(
  4150. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4151. );
  4152. $expected = array(
  4153. 'input' => array(
  4154. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4155. ),
  4156. 'select' => array(
  4157. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4158. ),
  4159. array('option' => array('value' => '1')),
  4160. 'One',
  4161. '/option',
  4162. array('option' => array('value' => '2', 'selected' => 'selected')),
  4163. 'Two',
  4164. '/option',
  4165. array('option' => array('value' => '3')),
  4166. 'Three',
  4167. '/option',
  4168. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4169. 'Stringy',
  4170. '/option',
  4171. '/select'
  4172. );
  4173. $this->assertTags($result, $expected);
  4174. $result = $this->Form->select('Contact.required_one', array(
  4175. '1' => 'option A',
  4176. '2' => 'option B'
  4177. ), array('multiple' => true));
  4178. $expected = array(
  4179. 'input' => array(
  4180. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4181. ),
  4182. 'select' => array(
  4183. 'name' => 'data[Contact][required_one][]',
  4184. 'id' => 'ContactRequiredOne',
  4185. 'required' => 'required',
  4186. 'multiple' => 'multiple'
  4187. ),
  4188. array('option' => array('value' => '1')), 'option A', '/option',
  4189. array('option' => array('value' => '2')), 'option B', '/option',
  4190. '/select'
  4191. );
  4192. $this->assertTags($result, $expected);
  4193. }
  4194. /**
  4195. * Test generating multiple select with disabled elements.
  4196. *
  4197. * @return void
  4198. */
  4199. public function testSelectMultipleWithDisabledElements() {
  4200. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4201. $disabled = array(2, 3);
  4202. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4203. $expected = array(
  4204. 'input' => array(
  4205. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4206. ),
  4207. 'select' => array(
  4208. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4209. ),
  4210. array('option' => array('value' => '1')),
  4211. 'One',
  4212. '/option',
  4213. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4214. 'Two',
  4215. '/option',
  4216. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4217. 'Three',
  4218. '/option',
  4219. array('option' => array('value' => '3x')),
  4220. 'Stringy',
  4221. '/option',
  4222. '/select'
  4223. );
  4224. $this->assertTags($result, $expected);
  4225. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4226. $disabled = array('2', '3x');
  4227. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4228. $expected = array(
  4229. array('div' => array('class' => 'input select')),
  4230. array('label' => array('for' => 'ContactMultiple')),
  4231. 'Multiple',
  4232. '/label',
  4233. 'input' => array(
  4234. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4235. ),
  4236. 'select' => array(
  4237. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4238. ),
  4239. array('option' => array('value' => '1')),
  4240. 'One',
  4241. '/option',
  4242. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4243. 'Two',
  4244. '/option',
  4245. array('option' => array('value' => '3')),
  4246. 'Three',
  4247. '/option',
  4248. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4249. 'Stringy',
  4250. '/option',
  4251. '/select',
  4252. '/div'
  4253. );
  4254. $this->assertTags($result, $expected);
  4255. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4256. $disabled = true;
  4257. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4258. $expected = array(
  4259. array('div' => array('class' => 'input select')),
  4260. array('label' => array('for' => 'ContactMultiple')),
  4261. 'Multiple',
  4262. '/label',
  4263. 'input' => array(
  4264. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4265. ),
  4266. 'select' => array(
  4267. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4268. ),
  4269. array('option' => array('value' => '1')),
  4270. 'One',
  4271. '/option',
  4272. array('option' => array('value' => '2')),
  4273. 'Two',
  4274. '/option',
  4275. array('option' => array('value' => '3')),
  4276. 'Three',
  4277. '/option',
  4278. array('option' => array('value' => '3x')),
  4279. 'Stringy',
  4280. '/option',
  4281. '/select',
  4282. '/div'
  4283. );
  4284. $this->assertTags($result, $expected);
  4285. }
  4286. /**
  4287. * Test generating select with disabled elements.
  4288. *
  4289. * @return void
  4290. */
  4291. public function testSelectWithDisabledElements() {
  4292. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4293. $disabled = array(2, 3);
  4294. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4295. $expected = array(
  4296. 'select' => array(
  4297. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4298. ),
  4299. array('option' => array('value' => '')),
  4300. '/option',
  4301. array('option' => array('value' => '1')),
  4302. 'One',
  4303. '/option',
  4304. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4305. 'Two',
  4306. '/option',
  4307. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4308. 'Three',
  4309. '/option',
  4310. array('option' => array('value' => '3x')),
  4311. 'Stringy',
  4312. '/option',
  4313. '/select'
  4314. );
  4315. $this->assertTags($result, $expected);
  4316. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4317. $disabled = array('2', '3x');
  4318. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4319. $expected = array(
  4320. array('div' => array('class' => 'input select')),
  4321. array('label' => array('for' => 'ModelField')),
  4322. 'Field',
  4323. '/label',
  4324. 'select' => array(
  4325. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4326. ),
  4327. array('option' => array('value' => '1')),
  4328. 'One',
  4329. '/option',
  4330. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4331. 'Two',
  4332. '/option',
  4333. array('option' => array('value' => '3')),
  4334. 'Three',
  4335. '/option',
  4336. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4337. 'Stringy',
  4338. '/option',
  4339. '/select',
  4340. '/div'
  4341. );
  4342. $this->assertTags($result, $expected);
  4343. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4344. $disabled = true;
  4345. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4346. $expected = array(
  4347. array('div' => array('class' => 'input select')),
  4348. array('label' => array('for' => 'ModelField')),
  4349. 'Field',
  4350. '/label',
  4351. 'select' => array(
  4352. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4353. ),
  4354. array('option' => array('value' => '1')),
  4355. 'One',
  4356. '/option',
  4357. array('option' => array('value' => '2')),
  4358. 'Two',
  4359. '/option',
  4360. array('option' => array('value' => '3')),
  4361. 'Three',
  4362. '/option',
  4363. array('option' => array('value' => '3x')),
  4364. 'Stringy',
  4365. '/option',
  4366. '/select',
  4367. '/div'
  4368. );
  4369. $this->assertTags($result, $expected);
  4370. }
  4371. /**
  4372. * test generation of habtm select boxes.
  4373. *
  4374. * @return void
  4375. */
  4376. public function testHabtmSelectBox() {
  4377. $this->View->viewVars['contactTags'] = array(
  4378. 1 => 'blue',
  4379. 2 => 'red',
  4380. 3 => 'green'
  4381. );
  4382. $this->Form->request->data = array(
  4383. 'Contact' => array(),
  4384. 'ContactTag' => array(
  4385. array(
  4386. 'id' => '1',
  4387. 'name' => 'blue'
  4388. ),
  4389. array(
  4390. 'id' => 3,
  4391. 'name' => 'green'
  4392. )
  4393. )
  4394. );
  4395. $this->Form->create('Contact');
  4396. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4397. $expected = array(
  4398. 'input' => array(
  4399. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4400. ),
  4401. 'select' => array(
  4402. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4403. 'multiple' => 'multiple'
  4404. ),
  4405. array('option' => array('value' => '1', 'selected' => 'selected')),
  4406. 'blue',
  4407. '/option',
  4408. array('option' => array('value' => '2')),
  4409. 'red',
  4410. '/option',
  4411. array('option' => array('value' => '3', 'selected' => 'selected')),
  4412. 'green',
  4413. '/option',
  4414. '/select'
  4415. );
  4416. $this->assertTags($result, $expected);
  4417. // make sure only 50 is selected, and not 50f5c0cf
  4418. $this->View->viewVars['contactTags'] = array(
  4419. '1' => 'blue',
  4420. '50f5c0cf' => 'red',
  4421. '50' => 'green'
  4422. );
  4423. $this->Form->request->data = array(
  4424. 'Contact' => array(),
  4425. 'ContactTag' => array(
  4426. array(
  4427. 'id' => 1,
  4428. 'name' => 'blue'
  4429. ),
  4430. array(
  4431. 'id' => 50,
  4432. 'name' => 'green'
  4433. )
  4434. )
  4435. );
  4436. $this->Form->create('Contact');
  4437. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4438. $expected = array(
  4439. 'input' => array(
  4440. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4441. ),
  4442. 'select' => array(
  4443. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4444. 'multiple' => 'multiple'
  4445. ),
  4446. array('option' => array('value' => '1', 'selected' => 'selected')),
  4447. 'blue',
  4448. '/option',
  4449. array('option' => array('value' => '50f5c0cf')),
  4450. 'red',
  4451. '/option',
  4452. array('option' => array('value' => '50', 'selected' => 'selected')),
  4453. 'green',
  4454. '/option',
  4455. '/select'
  4456. );
  4457. $this->assertTags($result, $expected);
  4458. }
  4459. /**
  4460. * test generation of multi select elements in checkbox format
  4461. *
  4462. * @return void
  4463. */
  4464. public function testSelectMultipleCheckboxes() {
  4465. $result = $this->Form->select(
  4466. 'Model.multi_field',
  4467. array('first', 'second', 'third'),
  4468. array('multiple' => 'checkbox')
  4469. );
  4470. $expected = array(
  4471. 'input' => array(
  4472. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4473. ),
  4474. array('div' => array('class' => 'checkbox')),
  4475. array('input' => array(
  4476. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4477. 'value' => '0', 'id' => 'ModelMultiField0'
  4478. )),
  4479. array('label' => array('for' => 'ModelMultiField0')),
  4480. 'first',
  4481. '/label',
  4482. '/div',
  4483. array('div' => array('class' => 'checkbox')),
  4484. array('input' => array(
  4485. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4486. 'value' => '1', 'id' => 'ModelMultiField1'
  4487. )),
  4488. array('label' => array('for' => 'ModelMultiField1')),
  4489. 'second',
  4490. '/label',
  4491. '/div',
  4492. array('div' => array('class' => 'checkbox')),
  4493. array('input' => array(
  4494. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4495. 'value' => '2', 'id' => 'ModelMultiField2'
  4496. )),
  4497. array('label' => array('for' => 'ModelMultiField2')),
  4498. 'third',
  4499. '/label',
  4500. '/div'
  4501. );
  4502. $this->assertTags($result, $expected);
  4503. $result = $this->Form->select(
  4504. 'Model.multi_field',
  4505. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4506. array('multiple' => 'checkbox')
  4507. );
  4508. $expected = array(
  4509. 'input' => array(
  4510. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4511. ),
  4512. array('div' => array('class' => 'checkbox')),
  4513. array('input' => array(
  4514. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4515. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4516. )),
  4517. array('label' => array('for' => 'ModelMultiFieldA')),
  4518. 'first',
  4519. '/label',
  4520. '/div',
  4521. array('div' => array('class' => 'checkbox')),
  4522. array('input' => array(
  4523. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4524. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4525. )),
  4526. array('label' => array('for' => 'ModelMultiFieldB')),
  4527. 'second',
  4528. '/label',
  4529. '/div',
  4530. array('div' => array('class' => 'checkbox')),
  4531. array('input' => array(
  4532. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4533. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4534. )),
  4535. array('label' => array('for' => 'ModelMultiFieldC')),
  4536. 'third',
  4537. '/label',
  4538. '/div'
  4539. );
  4540. $this->assertTags($result, $expected);
  4541. $result = $this->Form->select(
  4542. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4543. );
  4544. $expected = array(
  4545. 'input' => array(
  4546. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4547. ),
  4548. array('div' => array('class' => 'checkbox')),
  4549. array('input' => array(
  4550. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4551. 'value' => '1', 'id' => 'ModelMultiField1'
  4552. )),
  4553. array('label' => array('for' => 'ModelMultiField1')),
  4554. 'first',
  4555. '/label',
  4556. '/div'
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4560. $result = $this->Form->select(
  4561. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4562. );
  4563. $expected = array(
  4564. 'input' => array(
  4565. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4566. ),
  4567. array('div' => array('class' => 'checkbox')),
  4568. array('input' => array(
  4569. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4570. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4571. )),
  4572. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4573. 'first',
  4574. '/label',
  4575. '/div',
  4576. array('div' => array('class' => 'checkbox')),
  4577. array('input' => array(
  4578. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4579. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4580. )),
  4581. array('label' => array('for' => 'ModelTagsArray')),
  4582. 'Array',
  4583. '/label',
  4584. '/div'
  4585. );
  4586. $this->assertTags($result, $expected);
  4587. }
  4588. /**
  4589. * test multiple checkboxes with div styles.
  4590. *
  4591. * @return void
  4592. */
  4593. public function testSelectMultipleCheckboxDiv() {
  4594. $result = $this->Form->select(
  4595. 'Model.tags',
  4596. array('first', 'second'),
  4597. array('multiple' => 'checkbox', 'class' => 'my-class')
  4598. );
  4599. $expected = array(
  4600. 'input' => array(
  4601. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4602. ),
  4603. array('div' => array('class' => 'my-class')),
  4604. array('input' => array(
  4605. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4606. 'value' => '0', 'id' => 'ModelTags0'
  4607. )),
  4608. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4609. '/div',
  4610. array('div' => array('class' => 'my-class')),
  4611. array('input' => array(
  4612. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4613. 'value' => '1', 'id' => 'ModelTags1'
  4614. )),
  4615. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4616. '/div'
  4617. );
  4618. $this->assertTags($result, $expected);
  4619. $result = $this->Form->input('Model.tags', array(
  4620. 'options' => array('first', 'second'),
  4621. 'multiple' => 'checkbox',
  4622. 'class' => 'my-class',
  4623. 'div' => false,
  4624. 'label' => false
  4625. ));
  4626. $this->assertTags($result, $expected);
  4627. $Contact = ClassRegistry::getObject('Contact');
  4628. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4629. $result = $this->Form->input('Contact.tags', array(
  4630. 'options' => array('one'),
  4631. 'multiple' => 'checkbox',
  4632. 'label' => false,
  4633. 'div' => false
  4634. ));
  4635. $expected = array(
  4636. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4637. array('div' => array('class' => 'checkbox form-error')),
  4638. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4639. array('label' => array('for' => 'ContactTags0')),
  4640. 'one',
  4641. '/label',
  4642. '/div'
  4643. );
  4644. $this->assertTags($result, $expected);
  4645. $result = $this->Form->input('Contact.tags', array(
  4646. 'options' => array('one'),
  4647. 'multiple' => 'checkbox',
  4648. 'class' => 'mycheckbox',
  4649. 'label' => false,
  4650. 'div' => false
  4651. ));
  4652. $expected = array(
  4653. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4654. array('div' => array('class' => 'mycheckbox form-error')),
  4655. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4656. array('label' => array('for' => 'ContactTags0')),
  4657. 'one',
  4658. '/label',
  4659. '/div'
  4660. );
  4661. $this->assertTags($result, $expected);
  4662. }
  4663. /**
  4664. * Checks the security hash array generated for multiple-input checkbox elements
  4665. *
  4666. * @return void
  4667. */
  4668. public function testSelectMultipleCheckboxSecurity() {
  4669. $this->Form->request['_Token'] = array('key' => 'testKey');
  4670. $this->assertEquals(array(), $this->Form->fields);
  4671. $result = $this->Form->select(
  4672. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4673. array('multiple' => 'checkbox')
  4674. );
  4675. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4676. $result = $this->Form->secure($this->Form->fields);
  4677. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4678. $this->assertRegExp('/"' . $key . '"/', $result);
  4679. }
  4680. /**
  4681. * Multiple select elements should always be secured as they always participate
  4682. * in the POST data.
  4683. *
  4684. * @return void
  4685. */
  4686. public function testSelectMultipleSecureWithNoOptions() {
  4687. $this->Form->request['_Token'] = array('key' => 'testkey');
  4688. $this->assertEquals(array(), $this->Form->fields);
  4689. $this->Form->select(
  4690. 'Model.select',
  4691. array(),
  4692. array('multiple' => true)
  4693. );
  4694. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4695. }
  4696. /**
  4697. * When a select box has no options it should not be added to the fields list
  4698. * as it always fail post validation.
  4699. *
  4700. * @return void
  4701. */
  4702. public function testSelectNoSecureWithNoOptions() {
  4703. $this->Form->request['_Token'] = array('key' => 'testkey');
  4704. $this->assertEquals(array(), $this->Form->fields);
  4705. $this->Form->select(
  4706. 'Model.select',
  4707. array()
  4708. );
  4709. $this->assertEquals(array(), $this->Form->fields);
  4710. $this->Form->select(
  4711. 'Model.select',
  4712. array(),
  4713. array('empty' => true)
  4714. );
  4715. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4716. }
  4717. /**
  4718. * testInputMultipleCheckboxes method
  4719. *
  4720. * test input() resulting in multi select elements being generated.
  4721. *
  4722. * @return void
  4723. */
  4724. public function testInputMultipleCheckboxes() {
  4725. $result = $this->Form->input('Model.multi_field', array(
  4726. 'options' => array('first', 'second', 'third'),
  4727. 'multiple' => 'checkbox'
  4728. ));
  4729. $expected = array(
  4730. array('div' => array('class' => 'input select')),
  4731. array('label' => array('for' => 'ModelMultiField')),
  4732. 'Multi Field',
  4733. '/label',
  4734. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4735. array('div' => array('class' => 'checkbox')),
  4736. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4737. array('label' => array('for' => 'ModelMultiField0')),
  4738. 'first',
  4739. '/label',
  4740. '/div',
  4741. array('div' => array('class' => 'checkbox')),
  4742. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4743. array('label' => array('for' => 'ModelMultiField1')),
  4744. 'second',
  4745. '/label',
  4746. '/div',
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4749. array('label' => array('for' => 'ModelMultiField2')),
  4750. 'third',
  4751. '/label',
  4752. '/div',
  4753. '/div'
  4754. );
  4755. $this->assertTags($result, $expected);
  4756. $result = $this->Form->input('Model.multi_field', array(
  4757. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4758. 'multiple' => 'checkbox'
  4759. ));
  4760. $expected = array(
  4761. array('div' => array('class' => 'input select')),
  4762. array('label' => array('for' => 'ModelMultiField')),
  4763. 'Multi Field',
  4764. '/label',
  4765. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4766. array('div' => array('class' => 'checkbox')),
  4767. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4768. array('label' => array('for' => 'ModelMultiFieldA')),
  4769. 'first',
  4770. '/label',
  4771. '/div',
  4772. array('div' => array('class' => 'checkbox')),
  4773. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4774. array('label' => array('for' => 'ModelMultiFieldB')),
  4775. 'second',
  4776. '/label',
  4777. '/div',
  4778. array('div' => array('class' => 'checkbox')),
  4779. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4780. array('label' => array('for' => 'ModelMultiFieldC')),
  4781. 'third',
  4782. '/label',
  4783. '/div',
  4784. '/div'
  4785. );
  4786. $this->assertTags($result, $expected);
  4787. $result = $this->Form->input('Model.multi_field', array(
  4788. 'options' => array('1' => 'first'),
  4789. 'multiple' => 'checkbox',
  4790. 'label' => false,
  4791. 'div' => false
  4792. ));
  4793. $expected = array(
  4794. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4795. array('div' => array('class' => 'checkbox')),
  4796. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4797. array('label' => array('for' => 'ModelMultiField1')),
  4798. 'first',
  4799. '/label',
  4800. '/div'
  4801. );
  4802. $this->assertTags($result, $expected);
  4803. $result = $this->Form->input('Model.multi_field', array(
  4804. 'options' => array('2' => 'second'),
  4805. 'multiple' => 'checkbox',
  4806. 'label' => false,
  4807. 'div' => false
  4808. ));
  4809. $expected = array(
  4810. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4811. array('div' => array('class' => 'checkbox')),
  4812. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4813. array('label' => array('for' => 'ModelMultiField2')),
  4814. 'second',
  4815. '/label',
  4816. '/div'
  4817. );
  4818. $this->assertTags($result, $expected);
  4819. }
  4820. /**
  4821. * testSelectHiddenFieldOmission method
  4822. *
  4823. * test that select() with 'hiddenField' => false omits the hidden field
  4824. *
  4825. * @return void
  4826. */
  4827. public function testSelectHiddenFieldOmission() {
  4828. $result = $this->Form->select('Model.multi_field',
  4829. array('first', 'second'),
  4830. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4831. );
  4832. $expected = array(
  4833. array('div' => array('class' => 'checkbox')),
  4834. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4835. array('label' => array('for' => 'ModelMultiField0')),
  4836. 'first',
  4837. '/label',
  4838. '/div',
  4839. array('div' => array('class' => 'checkbox')),
  4840. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4841. array('label' => array('for' => 'ModelMultiField1')),
  4842. 'second',
  4843. '/label',
  4844. '/div'
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. $result = $this->Form->input('Model.multi_field', array(
  4848. 'options' => array('first', 'second'),
  4849. 'multiple' => 'checkbox',
  4850. 'hiddenField' => false
  4851. ));
  4852. $expected = array(
  4853. array('div' => array('class' => 'input select')),
  4854. array('label' => array('for' => 'ModelMultiField')),
  4855. 'Multi Field',
  4856. '/label',
  4857. array('div' => array('class' => 'checkbox')),
  4858. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4859. array('label' => array('for' => 'ModelMultiField0')),
  4860. 'first',
  4861. '/label',
  4862. '/div',
  4863. array('div' => array('class' => 'checkbox')),
  4864. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4865. array('label' => array('for' => 'ModelMultiField1')),
  4866. 'second',
  4867. '/label',
  4868. '/div',
  4869. '/div'
  4870. );
  4871. $this->assertTags($result, $expected);
  4872. }
  4873. /**
  4874. * test that select() with multiple = checkbox works with overriding name attribute.
  4875. *
  4876. * @return void
  4877. */
  4878. public function testSelectCheckboxMultipleOverrideName() {
  4879. $result = $this->Form->input('category', array(
  4880. 'type' => 'select',
  4881. 'multiple' => 'checkbox',
  4882. 'name' => 'data[fish]',
  4883. 'options' => array('1', '2'),
  4884. 'div' => false,
  4885. 'label' => false,
  4886. ));
  4887. $expected = array(
  4888. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4889. array('div' => array('class' => 'checkbox')),
  4890. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4891. array('label' => array('for' => 'Category0')), '1', '/label',
  4892. '/div',
  4893. array('div' => array('class' => 'checkbox')),
  4894. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4895. array('label' => array('for' => 'Category1')), '2', '/label',
  4896. '/div'
  4897. );
  4898. $this->assertTags($result, $expected);
  4899. }
  4900. /**
  4901. * Test that 'id' overrides all the checkbox id's as well.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testSelectCheckboxMultipleId() {
  4906. $result = $this->Form->select(
  4907. 'Model.multi_field',
  4908. array('first', 'second', 'third'),
  4909. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4910. );
  4911. $expected = array(
  4912. 'input' => array(
  4913. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4914. ),
  4915. array('div' => array('class' => 'checkbox')),
  4916. array('input' => array(
  4917. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4918. 'value' => '0', 'id' => 'CustomId0'
  4919. )),
  4920. array('label' => array('for' => 'CustomId0')),
  4921. 'first',
  4922. '/label',
  4923. '/div',
  4924. array('div' => array('class' => 'checkbox')),
  4925. array('input' => array(
  4926. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4927. 'value' => '1', 'id' => 'CustomId1'
  4928. )),
  4929. array('label' => array('for' => 'CustomId1')),
  4930. 'second',
  4931. '/label',
  4932. '/div',
  4933. array('div' => array('class' => 'checkbox')),
  4934. array('input' => array(
  4935. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4936. 'value' => '2', 'id' => 'CustomId2'
  4937. )),
  4938. array('label' => array('for' => 'CustomId2')),
  4939. 'third',
  4940. '/label',
  4941. '/div'
  4942. );
  4943. $this->assertTags($result, $expected);
  4944. }
  4945. /**
  4946. * testCheckbox method
  4947. *
  4948. * Test generation of checkboxes
  4949. *
  4950. * @return void
  4951. */
  4952. public function testCheckbox() {
  4953. $result = $this->Form->checkbox('Model.field');
  4954. $expected = array(
  4955. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4956. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4960. $expected = array(
  4961. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4962. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4963. );
  4964. $this->assertTags($result, $expected);
  4965. $Contact = ClassRegistry::getObject('Contact');
  4966. $Contact->validationErrors['field'] = 1;
  4967. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4968. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4969. $expected = array(
  4970. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4971. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4972. );
  4973. $this->assertTags($result, $expected);
  4974. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4975. $expected = array(
  4976. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4977. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4978. );
  4979. $this->assertTags($result, $expected);
  4980. $this->Form->request->data['Contact']['field'] = '';
  4981. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4982. $expected = array(
  4983. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4984. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4985. );
  4986. $this->assertTags($result, $expected);
  4987. $Contact->validationErrors = array();
  4988. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4989. $expected = array(
  4990. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4991. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4992. );
  4993. $this->assertTags($result, $expected);
  4994. $this->Form->request->data['Contact']['published'] = 1;
  4995. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4996. $expected = array(
  4997. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4998. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4999. );
  5000. $this->assertTags($result, $expected);
  5001. $this->Form->request->data['Contact']['published'] = 0;
  5002. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5003. $expected = array(
  5004. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5005. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5006. );
  5007. $this->assertTags($result, $expected);
  5008. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5009. $expected = array(
  5010. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5011. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $result = $this->Form->checkbox('CustomField.1.value');
  5015. $expected = array(
  5016. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5017. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5018. );
  5019. $this->assertTags($result, $expected);
  5020. }
  5021. /**
  5022. * test checkbox() with a custom name attribute
  5023. *
  5024. * @return void
  5025. */
  5026. public function testCheckboxCustomNameAttribute() {
  5027. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5028. $expected = array(
  5029. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5030. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5031. );
  5032. $this->assertTags($result, $expected);
  5033. }
  5034. /**
  5035. * test the checked option for checkboxes.
  5036. *
  5037. * @return void
  5038. */
  5039. public function testCheckboxCheckedOption() {
  5040. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5041. $expected = array(
  5042. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5043. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5047. $expected = array(
  5048. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5049. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5053. $expected = array(
  5054. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5055. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5056. );
  5057. $this->assertTags($result, $expected);
  5058. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5059. $expected = array(
  5060. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5061. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. $this->Form->request->data['Model']['field'] = 1;
  5065. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5066. $expected = array(
  5067. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5068. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5069. );
  5070. $this->assertTags($result, $expected);
  5071. }
  5072. /**
  5073. * Test that disabled attribute works on both the checkbox and hidden input.
  5074. *
  5075. * @return void
  5076. */
  5077. public function testCheckboxDisabling() {
  5078. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5079. $expected = array(
  5080. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5081. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5082. );
  5083. $this->assertTags($result, $expected);
  5084. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5085. $expected = array(
  5086. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5087. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5088. );
  5089. $this->assertTags($result, $expected);
  5090. }
  5091. /**
  5092. * Test that the hidden input for checkboxes can be omitted or set to a
  5093. * specific value.
  5094. *
  5095. * @return void
  5096. */
  5097. public function testCheckboxHiddenField() {
  5098. $result = $this->Form->input('UserForm.something', array(
  5099. 'type' => 'checkbox',
  5100. 'hiddenField' => false
  5101. ));
  5102. $expected = array(
  5103. 'div' => array('class' => 'input checkbox'),
  5104. array('input' => array(
  5105. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5106. 'value' => '1', 'id' => 'UserFormSomething'
  5107. )),
  5108. 'label' => array('for' => 'UserFormSomething'),
  5109. 'Something',
  5110. '/label',
  5111. '/div'
  5112. );
  5113. $this->assertTags($result, $expected);
  5114. $result = $this->Form->input('UserForm.something', array(
  5115. 'type' => 'checkbox',
  5116. 'value' => 'Y',
  5117. 'hiddenField' => 'N',
  5118. ));
  5119. $expected = array(
  5120. 'div' => array('class' => 'input checkbox'),
  5121. array('input' => array(
  5122. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5123. 'value' => 'N', 'id' => 'UserFormSomething_'
  5124. )),
  5125. array('input' => array(
  5126. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5127. 'value' => 'Y', 'id' => 'UserFormSomething'
  5128. )),
  5129. 'label' => array('for' => 'UserFormSomething'),
  5130. 'Something',
  5131. '/label',
  5132. '/div'
  5133. );
  5134. $this->assertTags($result, $expected);
  5135. }
  5136. /**
  5137. * testDateTime method
  5138. *
  5139. * Test generation of date/time select elements
  5140. *
  5141. * @return void
  5142. */
  5143. public function testDateTime() {
  5144. extract($this->dateRegex);
  5145. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5146. $now = strtotime('now');
  5147. $expected = array(
  5148. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5149. $daysRegex,
  5150. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5151. date('j', $now),
  5152. '/option',
  5153. '*/select',
  5154. '-',
  5155. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5156. $monthsRegex,
  5157. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5158. date('F', $now),
  5159. '/option',
  5160. '*/select',
  5161. '-',
  5162. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5163. $yearsRegex,
  5164. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5165. date('Y', $now),
  5166. '/option',
  5167. '*/select',
  5168. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5169. $hoursRegex,
  5170. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5171. date('g', $now),
  5172. '/option',
  5173. '*/select',
  5174. ':',
  5175. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5176. $minutesRegex,
  5177. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5178. date('i', $now),
  5179. '/option',
  5180. '*/select',
  5181. ' ',
  5182. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5183. $meridianRegex,
  5184. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5185. date('a', $now),
  5186. '/option',
  5187. '*/select'
  5188. );
  5189. $this->assertTags($result, $expected);
  5190. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5191. $expected = array(
  5192. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5193. $daysRegex,
  5194. array('option' => array('value' => '')),
  5195. '/option',
  5196. '*/select',
  5197. '-',
  5198. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5199. $monthsRegex,
  5200. array('option' => array('value' => '')),
  5201. '/option',
  5202. '*/select',
  5203. '-',
  5204. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5205. $yearsRegex,
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. '*/select',
  5209. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5210. $hoursRegex,
  5211. array('option' => array('value' => '')),
  5212. '/option',
  5213. '*/select',
  5214. ':',
  5215. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5216. $minutesRegex,
  5217. array('option' => array('value' => '')),
  5218. '/option',
  5219. '*/select',
  5220. ' ',
  5221. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5222. $meridianRegex,
  5223. array('option' => array('value' => '')),
  5224. '/option',
  5225. '*/select'
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5229. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5230. $this->assertTags($result, $expected);
  5231. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5232. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5233. $this->assertTags($result, $expected);
  5234. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5235. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5236. $expected = array(
  5237. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5238. $daysRegex,
  5239. array('option' => array('value' => '')),
  5240. '/option',
  5241. '*/select',
  5242. '-',
  5243. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5244. $monthsRegex,
  5245. array('option' => array('value' => '')),
  5246. '/option',
  5247. '*/select',
  5248. '-',
  5249. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5250. $yearsRegex,
  5251. array('option' => array('value' => '')),
  5252. '/option',
  5253. '*/select',
  5254. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5255. $hoursRegex,
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. '*/select',
  5259. ':',
  5260. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5261. $minutesRegex,
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. array('option' => array('value' => '00')),
  5265. '00',
  5266. '/option',
  5267. array('option' => array('value' => '05')),
  5268. '05',
  5269. '/option',
  5270. array('option' => array('value' => '10')),
  5271. '10',
  5272. '/option',
  5273. '*/select',
  5274. ' ',
  5275. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5276. $meridianRegex,
  5277. array('option' => array('value' => '')),
  5278. '/option',
  5279. '*/select'
  5280. );
  5281. $this->assertTags($result, $expected);
  5282. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5283. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5284. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5285. $this->Form->request->data['Contact']['data'] = null;
  5286. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5287. $expected = array(
  5288. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5289. $daysRegex,
  5290. array('option' => array('value' => '')),
  5291. '/option',
  5292. '*/select',
  5293. '-',
  5294. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5295. $monthsRegex,
  5296. array('option' => array('value' => '')),
  5297. '/option',
  5298. '*/select',
  5299. '-',
  5300. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5301. $yearsRegex,
  5302. array('option' => array('value' => '')),
  5303. '/option',
  5304. '*/select',
  5305. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5306. $hoursRegex,
  5307. array('option' => array('value' => '')),
  5308. '/option',
  5309. '*/select',
  5310. ':',
  5311. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5312. $minutesRegex,
  5313. array('option' => array('value' => '')),
  5314. '/option',
  5315. '*/select',
  5316. ' ',
  5317. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5318. $meridianRegex,
  5319. array('option' => array('value' => '')),
  5320. '/option',
  5321. '*/select'
  5322. );
  5323. $this->assertTags($result, $expected);
  5324. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5325. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5326. $now = strtotime($this->Form->data['Model']['field']);
  5327. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5328. $expected = array(
  5329. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5330. $daysRegex,
  5331. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5332. date('j', $now),
  5333. '/option',
  5334. '*/select',
  5335. '-',
  5336. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5337. $monthsRegex,
  5338. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5339. date('F', $now),
  5340. '/option',
  5341. '*/select',
  5342. '-',
  5343. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5344. $yearsRegex,
  5345. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5346. date('Y', $now),
  5347. '/option',
  5348. '*/select',
  5349. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5350. $hoursRegex,
  5351. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5352. date('g', $now),
  5353. '/option',
  5354. '*/select',
  5355. ':',
  5356. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5357. $minutesRegex,
  5358. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5359. date('i', $now),
  5360. '/option',
  5361. '*/select',
  5362. ' ',
  5363. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5364. $meridianRegex,
  5365. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5366. date('a', $now),
  5367. '/option',
  5368. '*/select'
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. $selected = strtotime('2008-10-26 12:33:00');
  5372. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5373. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5374. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5375. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5376. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5377. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5378. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5379. $this->Form->create('Contact');
  5380. $result = $this->Form->input('published');
  5381. $now = strtotime('now');
  5382. $expected = array(
  5383. 'div' => array('class' => 'input date'),
  5384. 'label' => array('for' => 'ContactPublishedMonth'),
  5385. 'Published',
  5386. '/label',
  5387. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5388. $monthsRegex,
  5389. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5390. date('F', $now),
  5391. '/option',
  5392. '*/select',
  5393. '-',
  5394. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5395. $daysRegex,
  5396. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5397. date('j', $now),
  5398. '/option',
  5399. '*/select',
  5400. '-',
  5401. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5402. $yearsRegex,
  5403. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5404. date('Y', $now),
  5405. '/option',
  5406. '*/select',
  5407. '/div'
  5408. );
  5409. $this->assertTags($result, $expected);
  5410. $result = $this->Form->input('published2', array('type' => 'date'));
  5411. $now = strtotime('now');
  5412. $expected = array(
  5413. 'div' => array('class' => 'input date'),
  5414. 'label' => array('for' => 'ContactPublished2Month'),
  5415. 'Published2',
  5416. '/label',
  5417. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5418. $monthsRegex,
  5419. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5420. date('F', $now),
  5421. '/option',
  5422. '*/select',
  5423. '-',
  5424. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5425. $daysRegex,
  5426. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5427. date('j', $now),
  5428. '/option',
  5429. '*/select',
  5430. '-',
  5431. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5432. $yearsRegex,
  5433. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5434. date('Y', $now),
  5435. '/option',
  5436. '*/select',
  5437. '/div'
  5438. );
  5439. $this->assertTags($result, $expected);
  5440. $this->Form->create('Contact');
  5441. $result = $this->Form->input('published', array('monthNames' => false));
  5442. $now = strtotime('now');
  5443. $expected = array(
  5444. 'div' => array('class' => 'input date'),
  5445. 'label' => array('for' => 'ContactPublishedMonth'),
  5446. 'Published',
  5447. '/label',
  5448. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5449. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5450. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5451. date('m', $now),
  5452. '/option',
  5453. '*/select',
  5454. '-',
  5455. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5456. $daysRegex,
  5457. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5458. date('j', $now),
  5459. '/option',
  5460. '*/select',
  5461. '-',
  5462. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5463. $yearsRegex,
  5464. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5465. date('Y', $now),
  5466. '/option',
  5467. '*/select',
  5468. '/div'
  5469. );
  5470. $this->assertTags($result, $expected);
  5471. $result = $this->Form->input('published', array(
  5472. 'timeFormat' => 24,
  5473. 'interval' => 5,
  5474. 'selected' => strtotime('2009-09-03 13:37:00'),
  5475. 'type' => 'datetime'
  5476. ));
  5477. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5478. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5479. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5480. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5481. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5482. }
  5483. /**
  5484. * Test dateTime with rounding
  5485. *
  5486. * @return void
  5487. */
  5488. public function testDateTimeRounding() {
  5489. $this->Form->request->data['Contact'] = array(
  5490. 'date' => array(
  5491. 'day' => '13',
  5492. 'month' => '12',
  5493. 'year' => '2010',
  5494. 'hour' => '04',
  5495. 'min' => '19',
  5496. 'meridian' => 'AM'
  5497. )
  5498. );
  5499. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5500. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5501. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5502. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5503. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5504. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5505. }
  5506. /**
  5507. * Test that empty values don't trigger errors.
  5508. *
  5509. * @return void
  5510. */
  5511. public function testDateTimeNoErrorsOnEmptyData() {
  5512. $this->Form->request->data['Contact'] = array(
  5513. 'date' => array(
  5514. 'day' => '',
  5515. 'month' => '',
  5516. 'year' => '',
  5517. 'hour' => '',
  5518. 'min' => '',
  5519. 'meridian' => ''
  5520. )
  5521. );
  5522. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5523. $this->assertNotEmpty($result);
  5524. }
  5525. /**
  5526. * test that datetime() and default values work.
  5527. *
  5528. * @return void
  5529. */
  5530. public function testDatetimeWithDefault() {
  5531. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5532. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5533. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5534. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5535. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5536. 'default' => '2009-06-01 11:15:30'
  5537. ));
  5538. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5539. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5540. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5541. }
  5542. /**
  5543. * test that bogus non-date time data doesn't cause errors.
  5544. *
  5545. * @return void
  5546. */
  5547. public function testDateTimeWithBogusData() {
  5548. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5549. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5550. }
  5551. /**
  5552. * testDateTimeEmptyAsArray
  5553. *
  5554. * @return void
  5555. */
  5556. public function testDateTimeEmptyAsArray() {
  5557. $result = $this->Form->dateTime('Contact.date',
  5558. 'DMY',
  5559. '12',
  5560. array(
  5561. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5562. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5563. )
  5564. )
  5565. );
  5566. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5567. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5568. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5569. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5570. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5571. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5572. $result = $this->Form->dateTime('Contact.date',
  5573. 'DMY',
  5574. '12',
  5575. array(
  5576. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5577. )
  5578. );
  5579. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5580. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5581. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5582. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5583. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5584. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5585. }
  5586. /**
  5587. * testFormDateTimeMulti method
  5588. *
  5589. * test multiple datetime element generation
  5590. *
  5591. * @return void
  5592. */
  5593. public function testFormDateTimeMulti() {
  5594. extract($this->dateRegex);
  5595. $result = $this->Form->dateTime('Contact.1.updated');
  5596. $expected = array(
  5597. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5598. $daysRegex,
  5599. array('option' => array('value' => '')),
  5600. '/option',
  5601. '*/select',
  5602. '-',
  5603. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5604. $monthsRegex,
  5605. array('option' => array('value' => '')),
  5606. '/option',
  5607. '*/select',
  5608. '-',
  5609. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5610. $yearsRegex,
  5611. array('option' => array('value' => '')),
  5612. '/option',
  5613. '*/select',
  5614. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5615. $hoursRegex,
  5616. array('option' => array('value' => '')),
  5617. '/option',
  5618. '*/select',
  5619. ':',
  5620. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5621. $minutesRegex,
  5622. array('option' => array('value' => '')),
  5623. '/option',
  5624. '*/select',
  5625. ' ',
  5626. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5627. $meridianRegex,
  5628. array('option' => array('value' => '')),
  5629. '/option',
  5630. '*/select'
  5631. );
  5632. $this->assertTags($result, $expected);
  5633. $result = $this->Form->dateTime('Contact.2.updated');
  5634. $expected = array(
  5635. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5636. $daysRegex,
  5637. array('option' => array('value' => '')),
  5638. '/option',
  5639. '*/select',
  5640. '-',
  5641. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5642. $monthsRegex,
  5643. array('option' => array('value' => '')),
  5644. '/option',
  5645. '*/select',
  5646. '-',
  5647. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5648. $yearsRegex,
  5649. array('option' => array('value' => '')),
  5650. '/option',
  5651. '*/select',
  5652. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5653. $hoursRegex,
  5654. array('option' => array('value' => '')),
  5655. '/option',
  5656. '*/select',
  5657. ':',
  5658. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5659. $minutesRegex,
  5660. array('option' => array('value' => '')),
  5661. '/option',
  5662. '*/select',
  5663. ' ',
  5664. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5665. $meridianRegex,
  5666. array('option' => array('value' => '')),
  5667. '/option',
  5668. '*/select'
  5669. );
  5670. $this->assertTags($result, $expected);
  5671. }
  5672. /**
  5673. * When changing the date format, the label should always focus the first select box when
  5674. * clicked.
  5675. *
  5676. * @return void
  5677. */
  5678. public function testDateTimeLabelIdMatchesFirstInput() {
  5679. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5680. $this->assertContains('label for="ModelDateMonth"', $result);
  5681. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5682. $this->assertContains('label for="ModelDateDay"', $result);
  5683. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5684. $this->assertContains('label for="ModelDateYear"', $result);
  5685. }
  5686. /**
  5687. * testMonth method
  5688. *
  5689. * @return void
  5690. */
  5691. public function testMonth() {
  5692. $result = $this->Form->month('Model.field');
  5693. $expected = array(
  5694. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5695. array('option' => array('value' => '')),
  5696. '/option',
  5697. array('option' => array('value' => '01')),
  5698. date('F', strtotime('2008-01-01 00:00:00')),
  5699. '/option',
  5700. array('option' => array('value' => '02')),
  5701. date('F', strtotime('2008-02-01 00:00:00')),
  5702. '/option',
  5703. '*/select',
  5704. );
  5705. $this->assertTags($result, $expected);
  5706. $result = $this->Form->month('Model.field', array('empty' => true));
  5707. $expected = array(
  5708. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5709. array('option' => array('value' => '')),
  5710. '/option',
  5711. array('option' => array('value' => '01')),
  5712. date('F', strtotime('2008-01-01 00:00:00')),
  5713. '/option',
  5714. array('option' => array('value' => '02')),
  5715. date('F', strtotime('2008-02-01 00:00:00')),
  5716. '/option',
  5717. '*/select',
  5718. );
  5719. $this->assertTags($result, $expected);
  5720. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5721. $expected = array(
  5722. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5723. array('option' => array('value' => '')),
  5724. '/option',
  5725. array('option' => array('value' => '01')),
  5726. '01',
  5727. '/option',
  5728. array('option' => array('value' => '02')),
  5729. '02',
  5730. '/option',
  5731. '*/select',
  5732. );
  5733. $this->assertTags($result, $expected);
  5734. $monthNames = array(
  5735. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5736. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5737. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5738. $expected = array(
  5739. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5740. array('option' => array('value' => '')),
  5741. '/option',
  5742. array('option' => array('value' => '01')),
  5743. 'Jan',
  5744. '/option',
  5745. array('option' => array('value' => '02')),
  5746. 'Feb',
  5747. '/option',
  5748. '*/select',
  5749. );
  5750. $this->assertTags($result, $expected);
  5751. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5752. $result = $this->Form->month('Project.release');
  5753. $expected = array(
  5754. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5755. array('option' => array('value' => '')),
  5756. '/option',
  5757. array('option' => array('value' => '01')),
  5758. 'January',
  5759. '/option',
  5760. array('option' => array('value' => '02', 'selected' => 'selected')),
  5761. 'February',
  5762. '/option',
  5763. '*/select',
  5764. );
  5765. $this->assertTags($result, $expected);
  5766. }
  5767. /**
  5768. * testDay method
  5769. *
  5770. * @return void
  5771. */
  5772. public function testDay() {
  5773. extract($this->dateRegex);
  5774. $result = $this->Form->day('Model.field', array('value' => false));
  5775. $expected = array(
  5776. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5777. array('option' => array('value' => '')),
  5778. '/option',
  5779. array('option' => array('value' => '01')),
  5780. '1',
  5781. '/option',
  5782. array('option' => array('value' => '02')),
  5783. '2',
  5784. '/option',
  5785. $daysRegex,
  5786. '/select',
  5787. );
  5788. $this->assertTags($result, $expected);
  5789. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5790. $result = $this->Form->day('Model.field');
  5791. $expected = array(
  5792. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5793. array('option' => array('value' => '')),
  5794. '/option',
  5795. array('option' => array('value' => '01')),
  5796. '1',
  5797. '/option',
  5798. array('option' => array('value' => '02')),
  5799. '2',
  5800. '/option',
  5801. $daysRegex,
  5802. array('option' => array('value' => '10', 'selected' => 'selected')),
  5803. '10',
  5804. '/option',
  5805. $daysRegex,
  5806. '/select',
  5807. );
  5808. $this->assertTags($result, $expected);
  5809. $this->Form->request->data['Model']['field'] = '';
  5810. $result = $this->Form->day('Model.field', array('value' => '10'));
  5811. $expected = array(
  5812. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5813. array('option' => array('value' => '')),
  5814. '/option',
  5815. array('option' => array('value' => '01')),
  5816. '1',
  5817. '/option',
  5818. array('option' => array('value' => '02')),
  5819. '2',
  5820. '/option',
  5821. $daysRegex,
  5822. array('option' => array('value' => '10', 'selected' => 'selected')),
  5823. '10',
  5824. '/option',
  5825. $daysRegex,
  5826. '/select',
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5830. $result = $this->Form->day('Model.field', array('value' => true));
  5831. $expected = array(
  5832. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5833. array('option' => array('value' => '')),
  5834. '/option',
  5835. array('option' => array('value' => '01')),
  5836. '1',
  5837. '/option',
  5838. array('option' => array('value' => '02')),
  5839. '2',
  5840. '/option',
  5841. $daysRegex,
  5842. array('option' => array('value' => '10', 'selected' => 'selected')),
  5843. '10',
  5844. '/option',
  5845. $daysRegex,
  5846. '/select',
  5847. );
  5848. $this->assertTags($result, $expected);
  5849. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5850. $result = $this->Form->day('Project.release');
  5851. $expected = array(
  5852. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5853. array('option' => array('value' => '')),
  5854. '/option',
  5855. array('option' => array('value' => '01')),
  5856. '1',
  5857. '/option',
  5858. array('option' => array('value' => '02')),
  5859. '2',
  5860. '/option',
  5861. $daysRegex,
  5862. array('option' => array('value' => '10', 'selected' => 'selected')),
  5863. '10',
  5864. '/option',
  5865. $daysRegex,
  5866. '/select',
  5867. );
  5868. $this->assertTags($result, $expected);
  5869. }
  5870. /**
  5871. * testMinute method
  5872. *
  5873. * @return void
  5874. */
  5875. public function testMinute() {
  5876. extract($this->dateRegex);
  5877. $result = $this->Form->minute('Model.field');
  5878. $expected = array(
  5879. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5880. array('option' => array('value' => '')),
  5881. '/option',
  5882. array('option' => array('value' => '00')),
  5883. '00',
  5884. '/option',
  5885. array('option' => array('value' => '01')),
  5886. '01',
  5887. '/option',
  5888. array('option' => array('value' => '02')),
  5889. '02',
  5890. '/option',
  5891. $minutesRegex,
  5892. '/select',
  5893. );
  5894. $this->assertTags($result, $expected);
  5895. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5896. $result = $this->Form->minute('Model.field');
  5897. $expected = array(
  5898. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5899. array('option' => array('value' => '')),
  5900. '/option',
  5901. array('option' => array('value' => '00')),
  5902. '00',
  5903. '/option',
  5904. array('option' => array('value' => '01')),
  5905. '01',
  5906. '/option',
  5907. array('option' => array('value' => '02')),
  5908. '02',
  5909. '/option',
  5910. $minutesRegex,
  5911. array('option' => array('value' => '12', 'selected' => 'selected')),
  5912. '12',
  5913. '/option',
  5914. $minutesRegex,
  5915. '/select',
  5916. );
  5917. $this->assertTags($result, $expected);
  5918. $this->Form->request->data['Model']['field'] = '';
  5919. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5920. $expected = array(
  5921. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5922. array('option' => array('value' => '')),
  5923. '/option',
  5924. array('option' => array('value' => '00')),
  5925. '00',
  5926. '/option',
  5927. array('option' => array('value' => '05')),
  5928. '05',
  5929. '/option',
  5930. array('option' => array('value' => '10')),
  5931. '10',
  5932. '/option',
  5933. $minutesRegex,
  5934. '/select',
  5935. );
  5936. $this->assertTags($result, $expected);
  5937. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5938. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5939. $expected = array(
  5940. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5941. array('option' => array('value' => '')),
  5942. '/option',
  5943. array('option' => array('value' => '00')),
  5944. '00',
  5945. '/option',
  5946. array('option' => array('value' => '05')),
  5947. '05',
  5948. '/option',
  5949. array('option' => array('value' => '10', 'selected' => 'selected')),
  5950. '10',
  5951. '/option',
  5952. $minutesRegex,
  5953. '/select',
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. }
  5957. /**
  5958. * testHour method
  5959. *
  5960. * @return void
  5961. */
  5962. public function testHour() {
  5963. extract($this->dateRegex);
  5964. $result = $this->Form->hour('Model.field', false);
  5965. $expected = array(
  5966. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5967. array('option' => array('value' => '')),
  5968. '/option',
  5969. array('option' => array('value' => '01')),
  5970. '1',
  5971. '/option',
  5972. array('option' => array('value' => '02')),
  5973. '2',
  5974. '/option',
  5975. $hoursRegex,
  5976. '/select',
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5980. $result = $this->Form->hour('Model.field', false);
  5981. $expected = array(
  5982. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5983. array('option' => array('value' => '')),
  5984. '/option',
  5985. array('option' => array('value' => '01')),
  5986. '1',
  5987. '/option',
  5988. array('option' => array('value' => '02')),
  5989. '2',
  5990. '/option',
  5991. $hoursRegex,
  5992. array('option' => array('value' => '12', 'selected' => 'selected')),
  5993. '12',
  5994. '/option',
  5995. '/select',
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. $this->Form->request->data['Model']['field'] = '';
  5999. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6000. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6001. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6002. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6003. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6004. $result = $this->Form->hour('Model.field', true);
  6005. $expected = array(
  6006. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6007. array('option' => array('value' => '')),
  6008. '/option',
  6009. array('option' => array('value' => '00', 'selected' => 'selected')),
  6010. '0',
  6011. '/option',
  6012. array('option' => array('value' => '01')),
  6013. '1',
  6014. '/option',
  6015. array('option' => array('value' => '02')),
  6016. '2',
  6017. '/option',
  6018. $hoursRegex,
  6019. '/select',
  6020. );
  6021. $this->assertTags($result, $expected);
  6022. unset($this->Form->request->data['Model']['field']);
  6023. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6024. $thisHour = date('H');
  6025. $optValue = date('G');
  6026. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6027. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6028. $result = $this->Form->hour('Model.field', true);
  6029. $expected = array(
  6030. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6031. array('option' => array('value' => '')),
  6032. '/option',
  6033. array('option' => array('value' => '00')),
  6034. '0',
  6035. '/option',
  6036. array('option' => array('value' => '01', 'selected' => 'selected')),
  6037. '1',
  6038. '/option',
  6039. array('option' => array('value' => '02')),
  6040. '2',
  6041. '/option',
  6042. $hoursRegex,
  6043. '/select',
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. }
  6047. /**
  6048. * testYear method
  6049. *
  6050. * @return void
  6051. */
  6052. public function testYear() {
  6053. $result = $this->Form->year('Model.field', 2006, 2007);
  6054. $expected = array(
  6055. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6056. array('option' => array('value' => '')),
  6057. '/option',
  6058. array('option' => array('value' => '2007')),
  6059. '2007',
  6060. '/option',
  6061. array('option' => array('value' => '2006')),
  6062. '2006',
  6063. '/option',
  6064. '/select',
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '2006')),
  6073. '2006',
  6074. '/option',
  6075. array('option' => array('value' => '2007')),
  6076. '2007',
  6077. '/option',
  6078. '/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->request->data['Contact']['published'] = '';
  6082. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6083. $expected = array(
  6084. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6085. array('option' => array('value' => '')),
  6086. '/option',
  6087. array('option' => array('value' => '2007')),
  6088. '2007',
  6089. '/option',
  6090. array('option' => array('value' => '2006')),
  6091. '2006',
  6092. '/option',
  6093. '/select',
  6094. );
  6095. $this->assertTags($result, $expected);
  6096. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6097. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6098. $expected = array(
  6099. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6100. array('option' => array('value' => '2007')),
  6101. '2007',
  6102. '/option',
  6103. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6104. '2006',
  6105. '/option',
  6106. '/select',
  6107. );
  6108. $this->assertTags($result, $expected);
  6109. $this->Form->request->data['Contact']['published'] = '';
  6110. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6111. $expected = array(
  6112. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6113. array('option' => array('value' => '')),
  6114. '/option',
  6115. array('option' => array('value' => '2007')),
  6116. '2007',
  6117. '/option',
  6118. array('option' => array('value' => '2006')),
  6119. '2006',
  6120. '/option',
  6121. '/select',
  6122. );
  6123. $this->assertTags($result, $expected);
  6124. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6125. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6126. $expected = array(
  6127. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6128. array('option' => array('value' => '2007')),
  6129. '2007',
  6130. '/option',
  6131. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6132. '2006',
  6133. '/option',
  6134. '/select',
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. $this->Form->request->data['Contact']['published'] = '';
  6138. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6139. $expected = array(
  6140. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6141. array('option' => array('value' => '')),
  6142. '/option',
  6143. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6144. '2007',
  6145. '/option',
  6146. array('option' => array('value' => '2006')),
  6147. '2006',
  6148. '/option',
  6149. '/select',
  6150. );
  6151. $this->assertTags($result, $expected);
  6152. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6153. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6154. $expected = array(
  6155. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6156. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6157. '2007',
  6158. '/option',
  6159. array('option' => array('value' => '2006')),
  6160. '2006',
  6161. '/option',
  6162. '/select',
  6163. );
  6164. $this->assertTags($result, $expected);
  6165. $this->Form->request->data['Contact']['published'] = '';
  6166. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6167. $expected = array(
  6168. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6169. array('option' => array('value' => '2008')),
  6170. '2008',
  6171. '/option',
  6172. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6173. '2007',
  6174. '/option',
  6175. array('option' => array('value' => '2006')),
  6176. '2006',
  6177. '/option',
  6178. '/select',
  6179. );
  6180. $this->assertTags($result, $expected);
  6181. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6182. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6183. $expected = array(
  6184. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6185. array('option' => array('value' => '2008')),
  6186. '2008',
  6187. '/option',
  6188. array('option' => array('value' => '2007')),
  6189. '2007',
  6190. '/option',
  6191. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6192. '2006',
  6193. '/option',
  6194. '/select',
  6195. );
  6196. $this->assertTags($result, $expected);
  6197. $this->Form->request->data = array();
  6198. $this->Form->create('Contact');
  6199. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6200. $expected = array(
  6201. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6202. array('option' => array('value' => '2008')),
  6203. '2008',
  6204. '/option',
  6205. array('option' => array('value' => '2007')),
  6206. '2007',
  6207. '/option',
  6208. array('option' => array('value' => '2006')),
  6209. '2006',
  6210. '/option',
  6211. '/select',
  6212. );
  6213. $this->assertTags($result, $expected);
  6214. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6215. $this->assertContains('data[Contact][published][year]', $result);
  6216. }
  6217. /**
  6218. * testYearAutoExpandRange method
  6219. *
  6220. * @return void
  6221. */
  6222. public function testYearAutoExpandRange() {
  6223. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6224. $result = $this->Form->year('User.birthday');
  6225. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6226. $result = $matches[1];
  6227. $expected = range(date('Y') + 20, 1930);
  6228. $this->assertEquals($expected, $result);
  6229. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6230. $result = $this->Form->year('Project.release');
  6231. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6232. $result = $matches[1];
  6233. $expected = range(2050, date('Y') - 20);
  6234. $this->assertEquals($expected, $result);
  6235. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6236. $result = $this->Form->year('Project.release', 1890, 1900);
  6237. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6238. $result = $matches[1];
  6239. $expected = range(1900, 1881);
  6240. $this->assertEquals($expected, $result);
  6241. }
  6242. /**
  6243. * testInputDate method
  6244. *
  6245. * Test various inputs with type date and different dateFormat values.
  6246. * Failing to provide a dateFormat key should not error.
  6247. * It should simply not pre-select any value then.
  6248. *
  6249. * @return void
  6250. */
  6251. public function testInputDate() {
  6252. $this->Form->request->data = array(
  6253. 'User' => array(
  6254. 'month_year' => array('month' => date('m')),
  6255. 'just_year' => array('month' => date('m')),
  6256. 'just_month' => array('year' => date('Y')),
  6257. 'just_day' => array('month' => date('m')),
  6258. )
  6259. );
  6260. $this->Form->create('User');
  6261. $result = $this->Form->input('month_year',
  6262. array(
  6263. 'label' => false,
  6264. 'div' => false,
  6265. 'type' => 'date',
  6266. 'dateFormat' => 'MY',
  6267. 'minYear' => 2006,
  6268. 'maxYear' => 2008
  6269. )
  6270. );
  6271. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6272. $this->assertNotContains('value="2008" selected="selected"', $result);
  6273. $result = $this->Form->input('just_year',
  6274. array(
  6275. 'type' => 'date',
  6276. 'label' => false,
  6277. 'dateFormat' => 'Y',
  6278. 'minYear' => date('Y'),
  6279. 'maxYear' => date('Y', strtotime('+20 years'))
  6280. )
  6281. );
  6282. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6283. $result = $this->Form->input('just_month',
  6284. array(
  6285. 'type' => 'date',
  6286. 'label' => false,
  6287. 'dateFormat' => 'M',
  6288. 'empty' => false,
  6289. )
  6290. );
  6291. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6292. $result = $this->Form->input('just_day',
  6293. array(
  6294. 'type' => 'date',
  6295. 'label' => false,
  6296. 'dateFormat' => 'D',
  6297. 'empty' => false,
  6298. )
  6299. );
  6300. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6301. }
  6302. /**
  6303. * testInputDateMaxYear method
  6304. *
  6305. * Let's say we want to only allow users born from 2006 to 2008 to register
  6306. * This being the first singup page, we still don't have any data
  6307. *
  6308. * @return void
  6309. */
  6310. public function testInputDateMaxYear() {
  6311. $this->Form->request->data = array();
  6312. $this->Form->create('User');
  6313. $result = $this->Form->input('birthday',
  6314. array(
  6315. 'label' => false,
  6316. 'div' => false,
  6317. 'type' => 'date',
  6318. 'dateFormat' => 'DMY',
  6319. 'minYear' => 2006,
  6320. 'maxYear' => 2008
  6321. )
  6322. );
  6323. $this->assertContains('value="2008" selected="selected"', $result);
  6324. }
  6325. /**
  6326. * testTextArea method
  6327. *
  6328. * @return void
  6329. */
  6330. public function testTextArea() {
  6331. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6332. $result = $this->Form->textarea('Model.field');
  6333. $expected = array(
  6334. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6335. 'some test data',
  6336. '/textarea',
  6337. );
  6338. $this->assertTags($result, $expected);
  6339. $result = $this->Form->textarea('Model.tmp');
  6340. $expected = array(
  6341. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6342. '/textarea',
  6343. );
  6344. $this->assertTags($result, $expected);
  6345. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6346. $result = $this->Form->textarea('Model.field');
  6347. $expected = array(
  6348. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6349. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6350. '/textarea',
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6354. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6355. $expected = array(
  6356. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6357. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6358. '/textarea',
  6359. );
  6360. $this->assertTags($result, $expected);
  6361. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6362. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6363. $expected = array(
  6364. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6365. '/textarea'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. }
  6369. /**
  6370. * testTextAreaWithStupidCharacters method
  6371. *
  6372. * test text area with non-ascii characters
  6373. *
  6374. * @return void
  6375. */
  6376. public function testTextAreaWithStupidCharacters() {
  6377. $this->loadFixtures('Post');
  6378. $result = $this->Form->input('Post.content', array(
  6379. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6380. ));
  6381. $expected = array(
  6382. 'div' => array('class' => 'input textarea'),
  6383. 'label' => array('for' => 'PostContent'),
  6384. 'Current Text',
  6385. '/label',
  6386. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6387. 'GREAT®',
  6388. '/textarea',
  6389. '/div'
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. }
  6393. /**
  6394. * testHiddenField method
  6395. *
  6396. * @return void
  6397. */
  6398. public function testHiddenField() {
  6399. $Contact = ClassRegistry::getObject('Contact');
  6400. $Contact->validationErrors['field'] = 1;
  6401. $this->Form->request->data['Contact']['field'] = 'test';
  6402. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6403. $this->assertTags($result, array(
  6404. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6405. );
  6406. }
  6407. /**
  6408. * testFileUploadField method
  6409. *
  6410. * @return void
  6411. */
  6412. public function testFileUploadField() {
  6413. $result = $this->Form->file('Model.upload');
  6414. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6415. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6416. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6417. $expected = array(
  6418. 'div' => array('class' => 'input file'),
  6419. 'label' => array('for' => 'ModelUpload'),
  6420. 'Upload',
  6421. '/label',
  6422. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6423. '/div'
  6424. );
  6425. $this->assertTags($result, $expected);
  6426. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6427. $result = $this->Form->file('Model.upload');
  6428. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6429. }
  6430. /**
  6431. * test File upload input on a model not used in create();
  6432. *
  6433. * @return void
  6434. */
  6435. public function testFileUploadOnOtherModel() {
  6436. $this->Form->create('ValidateUser', array('type' => 'file'));
  6437. $result = $this->Form->file('ValidateProfile.city');
  6438. $expected = array(
  6439. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6440. );
  6441. $this->assertTags($result, $expected);
  6442. }
  6443. /**
  6444. * testButton method
  6445. *
  6446. * @return void
  6447. */
  6448. public function testButton() {
  6449. $result = $this->Form->button('Hi');
  6450. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6451. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6452. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6453. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6454. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6455. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6456. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6457. $result = $this->Form->button('No type', array('type' => false));
  6458. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6459. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6460. $this->assertNotRegExp('/\&039/', $result);
  6461. }
  6462. /**
  6463. * Test that button() makes unlocked fields by default.
  6464. *
  6465. * @return void
  6466. */
  6467. public function testButtonUnlockedByDefault() {
  6468. $this->Form->request->params['_Token']['key'] = 'secured';
  6469. $this->Form->button('Save', array('name' => 'save'));
  6470. $this->Form->button('Clear');
  6471. $result = $this->Form->unlockField();
  6472. $this->assertEquals(array('save'), $result);
  6473. }
  6474. /**
  6475. * testPostButton method
  6476. *
  6477. * @return void
  6478. */
  6479. public function testPostButton() {
  6480. $result = $this->Form->postButton('Hi', '/controller/action');
  6481. $this->assertTags($result, array(
  6482. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6483. 'div' => array('style' => 'display:none;'),
  6484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6485. '/div',
  6486. 'button' => array('type' => 'submit'),
  6487. 'Hi',
  6488. '/button',
  6489. '/form'
  6490. ));
  6491. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6492. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6493. }
  6494. /**
  6495. * Test that postButton adds _Token fields.
  6496. *
  6497. * @return void
  6498. */
  6499. public function testSecurePostButton() {
  6500. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6501. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6502. $expected = array(
  6503. 'form' => array(
  6504. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6505. ),
  6506. array('div' => array('style' => 'display:none;')),
  6507. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6508. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6509. '/div',
  6510. 'button' => array('type' => 'submit'),
  6511. 'Delete',
  6512. '/button',
  6513. array('div' => array('style' => 'display:none;')),
  6514. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6515. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6516. '/div',
  6517. '/form',
  6518. );
  6519. $this->assertTags($result, $expected);
  6520. }
  6521. /**
  6522. * testPostLink method
  6523. *
  6524. * @return void
  6525. */
  6526. public function testPostLink() {
  6527. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6528. $this->assertTags($result, array(
  6529. 'form' => array(
  6530. 'method' => 'post', 'action' => '/posts/delete/1',
  6531. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6532. ),
  6533. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6534. '/form',
  6535. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6536. 'Delete',
  6537. '/a'
  6538. ));
  6539. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6540. $this->assertTags($result, array(
  6541. 'form' => array(
  6542. 'method' => 'post', 'action' => '/posts/delete/1',
  6543. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6544. ),
  6545. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6546. '/form',
  6547. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6548. 'Delete',
  6549. '/a'
  6550. ));
  6551. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6552. $this->assertTags($result, array(
  6553. 'form' => array(
  6554. 'method' => 'post', 'action' => '/posts/delete/1',
  6555. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6556. ),
  6557. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6558. '/form',
  6559. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6560. 'Delete',
  6561. '/a'
  6562. ));
  6563. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6564. $this->assertTags($result, array(
  6565. 'form' => array(
  6566. 'method' => 'post', 'action' => '/posts/delete/1',
  6567. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6568. ),
  6569. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6570. '/form',
  6571. '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;/'),
  6572. 'Delete',
  6573. '/a'
  6574. ));
  6575. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6576. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6577. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6578. $this->assertTags($result, array(
  6579. 'form' => array(
  6580. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6581. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6582. ),
  6583. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6584. '/form',
  6585. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6586. 'Delete',
  6587. '/a'
  6588. ));
  6589. $result = $this->Form->postLink(
  6590. '',
  6591. array('controller' => 'items', 'action' => 'delete', 10),
  6592. array('class' => 'btn btn-danger', 'escape' => false),
  6593. 'Confirm thing'
  6594. );
  6595. $this->assertTags($result, array(
  6596. 'form' => array(
  6597. 'method' => 'post', 'action' => '/items/delete/10',
  6598. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6599. ),
  6600. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6601. '/form',
  6602. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6603. '/a'
  6604. ));
  6605. }
  6606. /**
  6607. * test creating postLinks after a GET form.
  6608. *
  6609. * @return void
  6610. */
  6611. public function testPostLinkAfterGetForm() {
  6612. $this->Form->request->params['_Token']['key'] = 'testkey';
  6613. $this->Form->create('User', array('type' => 'get'));
  6614. $this->Form->end();
  6615. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6616. $this->assertTags($result, array(
  6617. 'form' => array(
  6618. 'method' => 'post', 'action' => '/posts/delete/1',
  6619. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6620. ),
  6621. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6622. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6623. 'div' => array('style' => 'display:none;'),
  6624. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6625. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6626. '/div',
  6627. '/form',
  6628. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6629. 'Delete',
  6630. '/a'
  6631. ));
  6632. }
  6633. /**
  6634. * Test that postLink adds _Token fields.
  6635. *
  6636. * @return void
  6637. */
  6638. public function testSecurePostLink() {
  6639. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6640. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6641. $expected = array(
  6642. 'form' => array(
  6643. 'method' => 'post', 'action' => '/posts/delete/1',
  6644. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6645. ),
  6646. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6647. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6648. 'div' => array('style' => 'display:none;'),
  6649. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6650. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6651. '/div',
  6652. '/form',
  6653. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6654. 'Delete',
  6655. '/a'
  6656. );
  6657. $this->assertTags($result, $expected);
  6658. }
  6659. /**
  6660. * testSubmitButton method
  6661. *
  6662. * @return void
  6663. */
  6664. public function testSubmitButton() {
  6665. $result = $this->Form->submit('');
  6666. $expected = array(
  6667. 'div' => array('class' => 'submit'),
  6668. 'input' => array('type' => 'submit', 'value' => ''),
  6669. '/div'
  6670. );
  6671. $this->assertTags($result, $expected);
  6672. $result = $this->Form->submit('Test Submit');
  6673. $expected = array(
  6674. 'div' => array('class' => 'submit'),
  6675. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6676. '/div'
  6677. );
  6678. $this->assertTags($result, $expected);
  6679. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6680. $expected = array(
  6681. 'span' => array('class' => 'submit'),
  6682. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6683. '/span'
  6684. );
  6685. $this->assertTags($result, $expected);
  6686. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6687. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6688. $this->assertTags($result, $expected);
  6689. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6690. $expected = array(
  6691. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6692. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6693. '/div'
  6694. );
  6695. $this->assertTags($result, $expected);
  6696. $result = $this->Form->submit('Next >');
  6697. $expected = array(
  6698. 'div' => array('class' => 'submit'),
  6699. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6700. '/div'
  6701. );
  6702. $this->assertTags($result, $expected);
  6703. $result = $this->Form->submit('Next >', array('escape' => false));
  6704. $expected = array(
  6705. 'div' => array('class' => 'submit'),
  6706. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6707. '/div'
  6708. );
  6709. $this->assertTags($result, $expected);
  6710. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6711. $expected = array(
  6712. 'div' => array('class' => 'submit'),
  6713. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. $before = '--before--';
  6718. $after = '--after--';
  6719. $result = $this->Form->submit('Test', array('before' => $before));
  6720. $expected = array(
  6721. 'div' => array('class' => 'submit'),
  6722. '--before--',
  6723. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6724. '/div'
  6725. );
  6726. $this->assertTags($result, $expected);
  6727. $result = $this->Form->submit('Test', array('after' => $after));
  6728. $expected = array(
  6729. 'div' => array('class' => 'submit'),
  6730. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6731. '--after--',
  6732. '/div'
  6733. );
  6734. $this->assertTags($result, $expected);
  6735. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6736. $expected = array(
  6737. 'div' => array('class' => 'submit'),
  6738. '--before--',
  6739. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6740. '--after--',
  6741. '/div'
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. }
  6745. /**
  6746. * test image submit types.
  6747. *
  6748. * @return void
  6749. */
  6750. public function testSubmitImage() {
  6751. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6752. $expected = array(
  6753. 'div' => array('class' => 'submit'),
  6754. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6755. '/div'
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $result = $this->Form->submit('/relative/cake.power.gif');
  6759. $expected = array(
  6760. 'div' => array('class' => 'submit'),
  6761. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6762. '/div'
  6763. );
  6764. $this->assertTags($result, $expected);
  6765. $result = $this->Form->submit('cake.power.gif');
  6766. $expected = array(
  6767. 'div' => array('class' => 'submit'),
  6768. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6769. '/div'
  6770. );
  6771. $this->assertTags($result, $expected);
  6772. $result = $this->Form->submit('Not.an.image');
  6773. $expected = array(
  6774. 'div' => array('class' => 'submit'),
  6775. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6776. '/div'
  6777. );
  6778. $this->assertTags($result, $expected);
  6779. $after = '--after--';
  6780. $before = '--before--';
  6781. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6782. $expected = array(
  6783. 'div' => array('class' => 'submit'),
  6784. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6785. '--after--',
  6786. '/div'
  6787. );
  6788. $this->assertTags($result, $expected);
  6789. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6790. $expected = array(
  6791. 'div' => array('class' => 'submit'),
  6792. '--before--',
  6793. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6794. '/div'
  6795. );
  6796. $this->assertTags($result, $expected);
  6797. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6798. $expected = array(
  6799. 'div' => array('class' => 'submit'),
  6800. '--before--',
  6801. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6802. '--after--',
  6803. '/div'
  6804. );
  6805. $this->assertTags($result, $expected);
  6806. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6807. $expected = array(
  6808. 'div' => array('class' => 'submit'),
  6809. '--before--',
  6810. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6811. '--after--',
  6812. '/div'
  6813. );
  6814. $this->assertTags($result, $expected);
  6815. }
  6816. /**
  6817. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6818. * be submitted at a time.
  6819. *
  6820. * @return void
  6821. */
  6822. public function testSubmitUnlockedByDefault() {
  6823. $this->Form->request->params['_Token']['key'] = 'secured';
  6824. $this->Form->submit('Go go');
  6825. $this->Form->submit('Save', array('name' => 'save'));
  6826. $result = $this->Form->unlockField();
  6827. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6828. }
  6829. /**
  6830. * Test submit image with timestamps.
  6831. *
  6832. * @return void
  6833. */
  6834. public function testSubmitImageTimestamp() {
  6835. Configure::write('Asset.timestamp', 'force');
  6836. $result = $this->Form->submit('cake.power.gif');
  6837. $expected = array(
  6838. 'div' => array('class' => 'submit'),
  6839. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6840. '/div'
  6841. );
  6842. $this->assertTags($result, $expected);
  6843. }
  6844. /**
  6845. * test the create() method
  6846. *
  6847. * @return void
  6848. */
  6849. public function testCreate() {
  6850. $result = $this->Form->create('Contact');
  6851. $encoding = strtolower(Configure::read('App.encoding'));
  6852. $expected = array(
  6853. 'form' => array(
  6854. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6855. 'accept-charset' => $encoding
  6856. ),
  6857. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6858. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6859. '/div'
  6860. );
  6861. $this->assertTags($result, $expected);
  6862. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6863. $expected = array('form' => array(
  6864. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6865. 'accept-charset' => $encoding
  6866. ));
  6867. $this->assertTags($result, $expected);
  6868. $result = $this->Form->create('Contact', array('type' => 'get'));
  6869. $expected = array('form' => array(
  6870. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6871. 'accept-charset' => $encoding
  6872. ));
  6873. $this->assertTags($result, $expected);
  6874. $result = $this->Form->create('Contact', array('type' => 'put'));
  6875. $expected = array(
  6876. 'form' => array(
  6877. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6878. 'accept-charset' => $encoding
  6879. ),
  6880. 'div' => array('style' => 'display:none;'),
  6881. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6882. '/div'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. $result = $this->Form->create('Contact', array('type' => 'file'));
  6886. $expected = array(
  6887. 'form' => array(
  6888. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6889. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6890. ),
  6891. 'div' => array('style' => 'display:none;'),
  6892. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6893. '/div'
  6894. );
  6895. $this->assertTags($result, $expected);
  6896. $this->Form->request->data['Contact']['id'] = 1;
  6897. $this->Form->request->here = '/contacts/edit/1';
  6898. $this->Form->request['action'] = 'edit';
  6899. $result = $this->Form->create('Contact');
  6900. $expected = array(
  6901. 'form' => array(
  6902. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6903. 'accept-charset' => $encoding
  6904. ),
  6905. 'div' => array('style' => 'display:none;'),
  6906. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6907. '/div'
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. $this->Form->request->data['Contact']['id'] = 1;
  6911. $this->Form->request->here = '/contacts/edit/1';
  6912. $this->Form->request['action'] = 'edit';
  6913. $result = $this->Form->create('Contact', array('type' => 'file'));
  6914. $expected = array(
  6915. 'form' => array(
  6916. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6917. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6918. ),
  6919. 'div' => array('style' => 'display:none;'),
  6920. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6921. '/div'
  6922. );
  6923. $this->assertTags($result, $expected);
  6924. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6925. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6926. $expected = array(
  6927. 'form' => array(
  6928. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6929. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6930. ),
  6931. 'div' => array('style' => 'display:none;'),
  6932. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6933. '/div'
  6934. );
  6935. $this->assertTags($result, $expected);
  6936. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6937. $expected = array(
  6938. 'form' => array(
  6939. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6940. 'accept-charset' => $encoding
  6941. ),
  6942. 'div' => array('style' => 'display:none;'),
  6943. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6944. '/div'
  6945. );
  6946. $this->assertTags($result, $expected);
  6947. $this->Form->request['action'] = 'add';
  6948. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6949. $expected = array(
  6950. 'form' => array(
  6951. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6952. 'accept-charset' => $encoding
  6953. ),
  6954. 'div' => array('style' => 'display:none;'),
  6955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6956. '/div'
  6957. );
  6958. $this->assertTags($result, $expected);
  6959. $result = $this->Form->create('User', array('action' => 'login'));
  6960. $expected = array(
  6961. 'form' => array(
  6962. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6963. 'accept-charset' => $encoding
  6964. ),
  6965. 'div' => array('style' => 'display:none;'),
  6966. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6967. '/div'
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $result = $this->Form->create('User', array('url' => '/users/login'));
  6971. $expected = array(
  6972. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6973. 'div' => array('style' => 'display:none;'),
  6974. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6975. '/div'
  6976. );
  6977. $this->assertTags($result, $expected);
  6978. $this->Form->request['controller'] = 'pages';
  6979. $result = $this->Form->create('User', array('action' => 'signup'));
  6980. $expected = array(
  6981. 'form' => array(
  6982. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6983. 'accept-charset' => $encoding
  6984. ),
  6985. 'div' => array('style' => 'display:none;'),
  6986. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6987. '/div'
  6988. );
  6989. $this->assertTags($result, $expected);
  6990. $this->Form->request->data = array();
  6991. $this->Form->request['controller'] = 'contacts';
  6992. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6993. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6994. $expected = array(
  6995. 'form' => array(
  6996. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6997. 'accept-charset' => 'utf-8'
  6998. ),
  6999. 'div' => array('style' => 'display:none;'),
  7000. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7001. '/div'
  7002. );
  7003. $this->assertTags($result, $expected);
  7004. }
  7005. /**
  7006. * Test the onsubmit option for create()
  7007. *
  7008. * @return void
  7009. */
  7010. public function testCreateOnSubmit() {
  7011. $this->Form->request->data = array();
  7012. $this->Form->request['controller'] = 'contacts';
  7013. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7014. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7015. $expected = array(
  7016. 'form' => array(
  7017. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7018. 'accept-charset' => 'utf-8'
  7019. ),
  7020. 'div' => array('style' => 'display:none;'),
  7021. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7022. '/div'
  7023. );
  7024. $this->assertTags($result, $expected);
  7025. $this->Form->request->data = array();
  7026. $this->Form->request['controller'] = 'contacts';
  7027. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7028. $result = $this->Form->create(array(
  7029. 'url' => array('action' => 'index', 'param'),
  7030. 'default' => false,
  7031. 'onsubmit' => 'someFunction();'
  7032. ));
  7033. $expected = array(
  7034. 'form' => array(
  7035. 'id' => 'ContactAddForm', 'method' => 'post',
  7036. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7037. 'action' => '/contacts/index/param',
  7038. 'accept-charset' => 'utf-8'
  7039. ),
  7040. 'div' => array('style' => 'display:none;'),
  7041. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7042. '/div'
  7043. );
  7044. $this->assertTags($result, $expected);
  7045. }
  7046. /**
  7047. * test create() with automatic url generation
  7048. *
  7049. * @return void
  7050. */
  7051. public function testCreateAutoUrl() {
  7052. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7053. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7054. $this->Form->request->base = '/base_url';
  7055. $result = $this->Form->create('Contact');
  7056. $expected = array(
  7057. 'form' => array(
  7058. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7059. 'accept-charset' => 'utf-8'
  7060. ),
  7061. 'div' => array('style' => 'display:none;'),
  7062. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7063. '/div'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $this->Form->request['action'] = 'delete';
  7067. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7068. $this->Form->request->base = '/base_url';
  7069. $result = $this->Form->create('Contact');
  7070. $expected = array(
  7071. 'form' => array(
  7072. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7073. 'accept-charset' => 'utf-8'
  7074. ),
  7075. 'div' => array('style' => 'display:none;'),
  7076. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. }
  7081. /**
  7082. * test create() with a custom route
  7083. *
  7084. * @return void
  7085. */
  7086. public function testCreateCustomRoute() {
  7087. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7088. $encoding = strtolower(Configure::read('App.encoding'));
  7089. $result = $this->Form->create('User', array('action' => 'login'));
  7090. $expected = array(
  7091. 'form' => array(
  7092. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7093. 'accept-charset' => $encoding
  7094. ),
  7095. 'div' => array('style' => 'display:none;'),
  7096. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7097. '/div'
  7098. );
  7099. $this->assertTags($result, $expected);
  7100. }
  7101. /**
  7102. * test that inputDefaults are stored and used.
  7103. *
  7104. * @return void
  7105. */
  7106. public function testCreateWithInputDefaults() {
  7107. $this->Form->create('User', array(
  7108. 'inputDefaults' => array(
  7109. 'div' => false,
  7110. 'label' => false,
  7111. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7112. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7113. )
  7114. ));
  7115. $result = $this->Form->input('username');
  7116. $expected = array(
  7117. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7118. );
  7119. $this->assertTags($result, $expected);
  7120. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7121. $expected = array(
  7122. 'div' => array('class' => 'input text'),
  7123. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7124. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7125. '/div'
  7126. );
  7127. $this->assertTags($result, $expected);
  7128. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7129. $expected = array(
  7130. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7131. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7132. );
  7133. $this->assertTags($result, $expected);
  7134. $this->Form->create('User', array(
  7135. 'inputDefaults' => array(
  7136. 'div' => false,
  7137. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7138. )
  7139. ));
  7140. $result = $this->Form->input('username', array('div' => true));
  7141. $expected = array(
  7142. 'div' => array('class' => 'input text'),
  7143. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7144. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7145. '/div'
  7146. );
  7147. $this->assertTags($result, $expected);
  7148. }
  7149. /**
  7150. * test automatic accept-charset overriding
  7151. *
  7152. * @return void
  7153. */
  7154. public function testCreateWithAcceptCharset() {
  7155. $result = $this->Form->create('UserForm', array(
  7156. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7157. )
  7158. );
  7159. $expected = array(
  7160. 'form' => array(
  7161. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7162. 'accept-charset' => 'iso-8859-1'
  7163. ),
  7164. 'div' => array('style' => 'display:none;'),
  7165. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7166. '/div'
  7167. );
  7168. $this->assertTags($result, $expected);
  7169. }
  7170. /**
  7171. * Test base form URL when url param is passed with multiple parameters (&)
  7172. *
  7173. */
  7174. public function testCreateQuerystringrequest() {
  7175. $encoding = strtolower(Configure::read('App.encoding'));
  7176. $result = $this->Form->create('Contact', array(
  7177. 'type' => 'post',
  7178. 'escape' => false,
  7179. 'url' => array(
  7180. 'controller' => 'controller',
  7181. 'action' => 'action',
  7182. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7183. )
  7184. ));
  7185. $expected = array(
  7186. 'form' => array(
  7187. 'id' => 'ContactAddForm',
  7188. 'method' => 'post',
  7189. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7190. 'accept-charset' => $encoding
  7191. ),
  7192. 'div' => array('style' => 'display:none;'),
  7193. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7194. '/div'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->create('Contact', array(
  7198. 'type' => 'post',
  7199. 'url' => array(
  7200. 'controller' => 'controller',
  7201. 'action' => 'action',
  7202. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7203. )
  7204. ));
  7205. $expected = array(
  7206. 'form' => array(
  7207. 'id' => 'ContactAddForm',
  7208. 'method' => 'post',
  7209. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7210. 'accept-charset' => $encoding
  7211. ),
  7212. 'div' => array('style' => 'display:none;'),
  7213. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7214. '/div'
  7215. );
  7216. $this->assertTags($result, $expected);
  7217. }
  7218. /**
  7219. * test that create() doesn't cause errors by multiple id's being in the primary key
  7220. * as could happen with multiple select or checkboxes.
  7221. *
  7222. * @return void
  7223. */
  7224. public function testCreateWithMultipleIdInData() {
  7225. $encoding = strtolower(Configure::read('App.encoding'));
  7226. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7227. $result = $this->Form->create('Contact');
  7228. $expected = array(
  7229. 'form' => array(
  7230. 'id' => 'ContactAddForm',
  7231. 'method' => 'post',
  7232. 'action' => '/contacts/add',
  7233. 'accept-charset' => $encoding
  7234. ),
  7235. 'div' => array('style' => 'display:none;'),
  7236. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7237. '/div'
  7238. );
  7239. $this->assertTags($result, $expected);
  7240. }
  7241. /**
  7242. * test that create() doesn't add in extra passed params.
  7243. *
  7244. * @return void
  7245. */
  7246. public function testCreatePassedArgs() {
  7247. $encoding = strtolower(Configure::read('App.encoding'));
  7248. $this->Form->request->data['Contact']['id'] = 1;
  7249. $result = $this->Form->create('Contact', array(
  7250. 'type' => 'post',
  7251. 'escape' => false,
  7252. 'url' => array(
  7253. 'action' => 'edit',
  7254. 'myparam'
  7255. )
  7256. ));
  7257. $expected = array(
  7258. 'form' => array(
  7259. 'id' => 'ContactAddForm',
  7260. 'method' => 'post',
  7261. 'action' => '/contacts/edit/myparam',
  7262. 'accept-charset' => $encoding
  7263. ),
  7264. 'div' => array('style' => 'display:none;'),
  7265. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. }
  7270. /**
  7271. * test creating a get form, and get form inputs.
  7272. *
  7273. * @return void
  7274. */
  7275. public function testGetFormCreate() {
  7276. $encoding = strtolower(Configure::read('App.encoding'));
  7277. $result = $this->Form->create('Contact', array('type' => 'get'));
  7278. $this->assertTags($result, array('form' => array(
  7279. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7280. 'accept-charset' => $encoding
  7281. )));
  7282. $result = $this->Form->text('Contact.name');
  7283. $this->assertTags($result, array('input' => array(
  7284. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7285. )));
  7286. $result = $this->Form->password('password');
  7287. $this->assertTags($result, array('input' => array(
  7288. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7289. )));
  7290. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7291. $result = $this->Form->text('user_form');
  7292. $this->assertTags($result, array('input' => array(
  7293. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7294. )));
  7295. }
  7296. /**
  7297. * test get form, and inputs when the model param is false
  7298. *
  7299. * @return void
  7300. */
  7301. public function testGetFormWithFalseModel() {
  7302. $encoding = strtolower(Configure::read('App.encoding'));
  7303. $this->Form->request['controller'] = 'contact_test';
  7304. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7305. $expected = array('form' => array(
  7306. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7307. 'accept-charset' => $encoding
  7308. ));
  7309. $this->assertTags($result, $expected);
  7310. $result = $this->Form->text('reason');
  7311. $expected = array(
  7312. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7313. );
  7314. $this->assertTags($result, $expected);
  7315. }
  7316. /**
  7317. * test that datetime() works with GET style forms.
  7318. *
  7319. * @return void
  7320. */
  7321. public function testDateTimeWithGetForms() {
  7322. extract($this->dateRegex);
  7323. $this->Form->create('Contact', array('type' => 'get'));
  7324. $result = $this->Form->datetime('created');
  7325. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7326. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7327. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7328. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7329. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7330. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7331. }
  7332. /**
  7333. * testEditFormWithData method
  7334. *
  7335. * test auto populating form elements from submitted data.
  7336. *
  7337. * @return void
  7338. */
  7339. public function testEditFormWithData() {
  7340. $this->Form->request->data = array('Person' => array(
  7341. 'id' => 1,
  7342. 'first_name' => 'Nate',
  7343. 'last_name' => 'Abele',
  7344. 'email' => 'nate@example.com'
  7345. ));
  7346. $this->Form->request->addParams(array(
  7347. 'models' => array('Person'),
  7348. 'controller' => 'people',
  7349. 'action' => 'add'
  7350. ));
  7351. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7352. $this->Form->create();
  7353. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7354. $expected = array(
  7355. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7356. 'select' => array(
  7357. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7358. ),
  7359. array('option' => array('value' => 1)), 'Nate', '/option',
  7360. array('option' => array('value' => 2)), 'Garrett', '/option',
  7361. array('option' => array('value' => 3)), 'Larry', '/option',
  7362. '/select'
  7363. );
  7364. $this->assertTags($result, $expected);
  7365. }
  7366. /**
  7367. * Test that required fields are created for various types of validation.
  7368. *
  7369. * @return void
  7370. */
  7371. public function testFormInputRequiredDetection() {
  7372. $this->Form->create('Contact');
  7373. $result = $this->Form->input('Contact.non_existing');
  7374. $expected = array(
  7375. 'div' => array('class' => 'input text'),
  7376. 'label' => array('for' => 'ContactNonExisting'),
  7377. 'Non Existing',
  7378. '/label',
  7379. 'input' => array(
  7380. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7381. 'id' => 'ContactNonExisting'
  7382. ),
  7383. '/div'
  7384. );
  7385. $this->assertTags($result, $expected);
  7386. $result = $this->Form->input('Contact.imrequired');
  7387. $expected = array(
  7388. 'div' => array('class' => 'input text required'),
  7389. 'label' => array('for' => 'ContactImrequired'),
  7390. 'Imrequired',
  7391. '/label',
  7392. 'input' => array(
  7393. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7394. 'id' => 'ContactImrequired',
  7395. 'required' => 'required'
  7396. ),
  7397. '/div'
  7398. );
  7399. $this->assertTags($result, $expected);
  7400. $result = $this->Form->input('Contact.imalsorequired');
  7401. $expected = array(
  7402. 'div' => array('class' => 'input text required'),
  7403. 'label' => array('for' => 'ContactImalsorequired'),
  7404. 'Imalsorequired',
  7405. '/label',
  7406. 'input' => array(
  7407. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7408. 'id' => 'ContactImalsorequired',
  7409. 'required' => 'required'
  7410. ),
  7411. '/div'
  7412. );
  7413. $this->assertTags($result, $expected);
  7414. $result = $this->Form->input('Contact.imrequiredtoo');
  7415. $expected = array(
  7416. 'div' => array('class' => 'input text required'),
  7417. 'label' => array('for' => 'ContactImrequiredtoo'),
  7418. 'Imrequiredtoo',
  7419. '/label',
  7420. 'input' => array(
  7421. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7422. 'id' => 'ContactImrequiredtoo',
  7423. 'required' => 'required'
  7424. ),
  7425. '/div'
  7426. );
  7427. $this->assertTags($result, $expected);
  7428. $result = $this->Form->input('Contact.required_one');
  7429. $expected = array(
  7430. 'div' => array('class' => 'input text required'),
  7431. 'label' => array('for' => 'ContactRequiredOne'),
  7432. 'Required One',
  7433. '/label',
  7434. 'input' => array(
  7435. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7436. 'id' => 'ContactRequiredOne',
  7437. 'required' => 'required'
  7438. ),
  7439. '/div'
  7440. );
  7441. $this->assertTags($result, $expected);
  7442. $result = $this->Form->input('Contact.string_required');
  7443. $expected = array(
  7444. 'div' => array('class' => 'input text required'),
  7445. 'label' => array('for' => 'ContactStringRequired'),
  7446. 'String Required',
  7447. '/label',
  7448. 'input' => array(
  7449. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7450. 'id' => 'ContactStringRequired',
  7451. 'required' => 'required'
  7452. ),
  7453. '/div'
  7454. );
  7455. $this->assertTags($result, $expected);
  7456. $result = $this->Form->input('Contact.imnotrequired');
  7457. $expected = array(
  7458. 'div' => array('class' => 'input text'),
  7459. 'label' => array('for' => 'ContactImnotrequired'),
  7460. 'Imnotrequired',
  7461. '/label',
  7462. 'input' => array(
  7463. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7464. 'id' => 'ContactImnotrequired'
  7465. ),
  7466. '/div'
  7467. );
  7468. $this->assertTags($result, $expected);
  7469. $result = $this->Form->input('Contact.imalsonotrequired');
  7470. $expected = array(
  7471. 'div' => array('class' => 'input text'),
  7472. 'label' => array('for' => 'ContactImalsonotrequired'),
  7473. 'Imalsonotrequired',
  7474. '/label',
  7475. 'input' => array(
  7476. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7477. 'id' => 'ContactImalsonotrequired'
  7478. ),
  7479. '/div'
  7480. );
  7481. $this->assertTags($result, $expected);
  7482. $result = $this->Form->input('Contact.imalsonotrequired2');
  7483. $expected = array(
  7484. 'div' => array('class' => 'input text'),
  7485. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7486. 'Imalsonotrequired2',
  7487. '/label',
  7488. 'input' => array(
  7489. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7490. 'id' => 'ContactImalsonotrequired2'
  7491. ),
  7492. '/div'
  7493. );
  7494. $this->assertTags($result, $expected);
  7495. $result = $this->Form->input('Contact.imnotrequiredeither');
  7496. $expected = array(
  7497. 'div' => array('class' => 'input text'),
  7498. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7499. 'Imnotrequiredeither',
  7500. '/label',
  7501. 'input' => array(
  7502. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7503. 'id' => 'ContactImnotrequiredeither'
  7504. ),
  7505. '/div'
  7506. );
  7507. $this->assertTags($result, $expected);
  7508. $result = $this->Form->input('Contact.iamrequiredalways');
  7509. $expected = array(
  7510. 'div' => array('class' => 'input text required'),
  7511. 'label' => array('for' => 'ContactIamrequiredalways'),
  7512. 'Iamrequiredalways',
  7513. '/label',
  7514. 'input' => array(
  7515. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7516. 'id' => 'ContactIamrequiredalways',
  7517. 'required' => 'required'
  7518. ),
  7519. '/div'
  7520. );
  7521. $this->assertTags($result, $expected);
  7522. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7523. $expected = array(
  7524. 'div' => array('class' => 'input checkbox required'),
  7525. array('input' => array(
  7526. 'type' => 'hidden',
  7527. 'name' => 'data[Contact][boolean_field]',
  7528. 'id' => 'ContactBooleanField_',
  7529. 'value' => '0'
  7530. )),
  7531. array('input' => array(
  7532. 'type' => 'checkbox',
  7533. 'name' => 'data[Contact][boolean_field]',
  7534. 'value' => '1',
  7535. 'id' => 'ContactBooleanField'
  7536. )),
  7537. 'label' => array('for' => 'ContactBooleanField'),
  7538. 'Boolean Field',
  7539. '/label',
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7544. $expected = array(
  7545. 'div' => array('class' => 'input checkbox required'),
  7546. array('input' => array(
  7547. 'type' => 'hidden',
  7548. 'name' => 'data[Contact][boolean_field]',
  7549. 'id' => 'ContactBooleanField_',
  7550. 'value' => '0'
  7551. )),
  7552. array('input' => array(
  7553. 'type' => 'checkbox',
  7554. 'name' => 'data[Contact][boolean_field]',
  7555. 'value' => '1',
  7556. 'id' => 'ContactBooleanField',
  7557. 'required' => 'required'
  7558. )),
  7559. 'label' => array('for' => 'ContactBooleanField'),
  7560. 'Boolean Field',
  7561. '/label',
  7562. '/div'
  7563. );
  7564. $this->assertTags($result, $expected);
  7565. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7566. $expected = array(
  7567. 'div' => array('class' => 'input file required'),
  7568. 'label' => array('for' => 'ContactIamrequiredalways'),
  7569. 'Iamrequiredalways',
  7570. '/label',
  7571. 'input' => array(
  7572. 'type' => 'file',
  7573. 'name' => 'data[Contact][iamrequiredalways]',
  7574. 'id' => 'ContactIamrequiredalways',
  7575. 'required' => 'required'
  7576. ),
  7577. '/div'
  7578. );
  7579. $this->assertTags($result, $expected);
  7580. }
  7581. /**
  7582. * Test that required fields are created when only using ModelValidator::add().
  7583. *
  7584. * @return void
  7585. */
  7586. public function testFormInputRequiredDetectionModelValidator() {
  7587. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7588. $this->Form->create('ContactTag');
  7589. $result = $this->Form->input('ContactTag.iwillberequired');
  7590. $expected = array(
  7591. 'div' => array('class' => 'input text required'),
  7592. 'label' => array('for' => 'ContactTagIwillberequired'),
  7593. 'Iwillberequired',
  7594. '/label',
  7595. 'input' => array(
  7596. 'name' => 'data[ContactTag][iwillberequired]',
  7597. 'type' => 'text',
  7598. 'id' => 'ContactTagIwillberequired',
  7599. 'required' => 'required'
  7600. ),
  7601. '/div'
  7602. );
  7603. $this->assertTags($result, $expected);
  7604. }
  7605. /**
  7606. * testFormMagicInput method
  7607. *
  7608. * @return void
  7609. */
  7610. public function testFormMagicInput() {
  7611. $encoding = strtolower(Configure::read('App.encoding'));
  7612. $result = $this->Form->create('Contact');
  7613. $expected = array(
  7614. 'form' => array(
  7615. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7616. 'accept-charset' => $encoding
  7617. ),
  7618. 'div' => array('style' => 'display:none;'),
  7619. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7620. '/div'
  7621. );
  7622. $this->assertTags($result, $expected);
  7623. $result = $this->Form->input('name');
  7624. $expected = array(
  7625. 'div' => array('class' => 'input text'),
  7626. 'label' => array('for' => 'ContactName'),
  7627. 'Name',
  7628. '/label',
  7629. 'input' => array(
  7630. 'type' => 'text', 'name' => 'data[Contact][name]',
  7631. 'id' => 'ContactName', 'maxlength' => '255'
  7632. ),
  7633. '/div'
  7634. );
  7635. $this->assertTags($result, $expected);
  7636. $result = $this->Form->input('non_existing_field_in_contact_model');
  7637. $expected = array(
  7638. 'div' => array('class' => 'input text'),
  7639. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7640. 'Non Existing Field In Contact Model',
  7641. '/label',
  7642. 'input' => array(
  7643. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7644. 'id' => 'ContactNonExistingFieldInContactModel'
  7645. ),
  7646. '/div'
  7647. );
  7648. $this->assertTags($result, $expected);
  7649. $result = $this->Form->input('Address.street');
  7650. $expected = array(
  7651. 'div' => array('class' => 'input text'),
  7652. 'label' => array('for' => 'AddressStreet'),
  7653. 'Street',
  7654. '/label',
  7655. 'input' => array(
  7656. 'type' => 'text', 'name' => 'data[Address][street]',
  7657. 'id' => 'AddressStreet'
  7658. ),
  7659. '/div'
  7660. );
  7661. $this->assertTags($result, $expected);
  7662. $result = $this->Form->input('Address.non_existing_field_in_model');
  7663. $expected = array(
  7664. 'div' => array('class' => 'input text'),
  7665. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7666. 'Non Existing Field In Model',
  7667. '/label',
  7668. 'input' => array(
  7669. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7670. 'id' => 'AddressNonExistingFieldInModel'
  7671. ),
  7672. '/div'
  7673. );
  7674. $this->assertTags($result, $expected);
  7675. $result = $this->Form->input('name', array('div' => false));
  7676. $expected = array(
  7677. 'label' => array('for' => 'ContactName'),
  7678. 'Name',
  7679. '/label',
  7680. 'input' => array(
  7681. 'type' => 'text', 'name' => 'data[Contact][name]',
  7682. 'id' => 'ContactName', 'maxlength' => '255'
  7683. )
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. extract($this->dateRegex);
  7687. $now = strtotime('now');
  7688. $result = $this->Form->input('Contact.published', array('div' => false));
  7689. $expected = array(
  7690. 'label' => array('for' => 'ContactPublishedMonth'),
  7691. 'Published',
  7692. '/label',
  7693. array('select' => array(
  7694. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7695. )),
  7696. $monthsRegex,
  7697. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7698. date('F', $now),
  7699. '/option',
  7700. '*/select',
  7701. '-',
  7702. array('select' => array(
  7703. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7704. )),
  7705. $daysRegex,
  7706. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7707. date('j', $now),
  7708. '/option',
  7709. '*/select',
  7710. '-',
  7711. array('select' => array(
  7712. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7713. )),
  7714. $yearsRegex,
  7715. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7716. date('Y', $now),
  7717. '*/select'
  7718. );
  7719. $this->assertTags($result, $expected);
  7720. $result = $this->Form->input('Contact.updated', array('div' => false));
  7721. $expected = array(
  7722. 'label' => array('for' => 'ContactUpdatedMonth'),
  7723. 'Updated',
  7724. '/label',
  7725. array('select' => array(
  7726. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7727. )),
  7728. $monthsRegex,
  7729. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7730. date('F', $now),
  7731. '/option',
  7732. '*/select',
  7733. '-',
  7734. array('select' => array(
  7735. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7736. )),
  7737. $daysRegex,
  7738. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7739. date('j', $now),
  7740. '/option',
  7741. '*/select',
  7742. '-',
  7743. array('select' => array(
  7744. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7745. )),
  7746. $yearsRegex,
  7747. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7748. date('Y', $now),
  7749. '*/select'
  7750. );
  7751. $this->assertTags($result, $expected);
  7752. $result = $this->Form->input('UserForm.stuff');
  7753. $expected = array(
  7754. 'div' => array('class' => 'input text'),
  7755. 'label' => array('for' => 'UserFormStuff'),
  7756. 'Stuff',
  7757. '/label',
  7758. 'input' => array(
  7759. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7760. 'id' => 'UserFormStuff', 'maxlength' => 10
  7761. ),
  7762. '/div'
  7763. );
  7764. $this->assertTags($result, $expected);
  7765. }
  7766. /**
  7767. * testForMagicInputNonExistingNorValidated method
  7768. *
  7769. * @return void
  7770. */
  7771. public function testForMagicInputNonExistingNorValidated() {
  7772. $encoding = strtolower(Configure::read('App.encoding'));
  7773. $result = $this->Form->create('Contact');
  7774. $expected = array(
  7775. 'form' => array(
  7776. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7777. 'accept-charset' => $encoding
  7778. ),
  7779. 'div' => array('style' => 'display:none;'),
  7780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7781. '/div'
  7782. );
  7783. $this->assertTags($result, $expected);
  7784. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7785. $expected = array(
  7786. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7787. 'Non Existing Nor Validated',
  7788. '/label',
  7789. 'input' => array(
  7790. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7791. 'id' => 'ContactNonExistingNorValidated'
  7792. )
  7793. );
  7794. $this->assertTags($result, $expected);
  7795. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7796. 'div' => false, 'value' => 'my value'
  7797. ));
  7798. $expected = array(
  7799. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7800. 'Non Existing Nor Validated',
  7801. '/label',
  7802. 'input' => array(
  7803. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7804. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7805. )
  7806. );
  7807. $this->assertTags($result, $expected);
  7808. $this->Form->request->data = array(
  7809. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7810. ));
  7811. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7812. $expected = array(
  7813. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7814. 'Non Existing Nor Validated',
  7815. '/label',
  7816. 'input' => array(
  7817. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7818. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7819. )
  7820. );
  7821. $this->assertTags($result, $expected);
  7822. }
  7823. /**
  7824. * testFormMagicInputLabel method
  7825. *
  7826. * @return void
  7827. */
  7828. public function testFormMagicInputLabel() {
  7829. $encoding = strtolower(Configure::read('App.encoding'));
  7830. $result = $this->Form->create('Contact');
  7831. $expected = array(
  7832. 'form' => array(
  7833. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7834. 'accept-charset' => $encoding
  7835. ),
  7836. 'div' => array('style' => 'display:none;'),
  7837. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7838. '/div'
  7839. );
  7840. $this->assertTags($result, $expected);
  7841. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7842. $this->assertTags($result, array('input' => array(
  7843. 'name' => 'data[Contact][name]', 'type' => 'text',
  7844. 'id' => 'ContactName', 'maxlength' => '255')
  7845. ));
  7846. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7847. $expected = array(
  7848. 'label' => array('for' => 'ContactName'),
  7849. 'My label',
  7850. '/label',
  7851. 'input' => array(
  7852. 'type' => 'text', 'name' => 'data[Contact][name]',
  7853. 'id' => 'ContactName', 'maxlength' => '255'
  7854. )
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. $result = $this->Form->input('Contact.name', array(
  7858. 'div' => false, 'label' => array('class' => 'mandatory')
  7859. ));
  7860. $expected = array(
  7861. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7862. 'Name',
  7863. '/label',
  7864. 'input' => array(
  7865. 'type' => 'text', 'name' => 'data[Contact][name]',
  7866. 'id' => 'ContactName', 'maxlength' => '255'
  7867. )
  7868. );
  7869. $this->assertTags($result, $expected);
  7870. $result = $this->Form->input('Contact.name', array(
  7871. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7872. ));
  7873. $expected = array(
  7874. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7875. 'My label',
  7876. '/label',
  7877. 'input' => array(
  7878. 'type' => 'text', 'name' => 'data[Contact][name]',
  7879. 'id' => 'ContactName', 'maxlength' => '255'
  7880. )
  7881. );
  7882. $this->assertTags($result, $expected);
  7883. $result = $this->Form->input('Contact.name', array(
  7884. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7885. ));
  7886. $expected = array(
  7887. 'label' => array('for' => 'my_id'),
  7888. 'Name',
  7889. '/label',
  7890. 'input' => array(
  7891. 'type' => 'text', 'name' => 'data[Contact][name]',
  7892. 'id' => 'my_id', 'maxlength' => '255'
  7893. )
  7894. );
  7895. $this->assertTags($result, $expected);
  7896. $result = $this->Form->input('1.id');
  7897. $this->assertTags($result, array('input' => array(
  7898. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7899. 'id' => 'Contact1Id'
  7900. )));
  7901. $result = $this->Form->input("1.name");
  7902. $expected = array(
  7903. 'div' => array('class' => 'input text'),
  7904. 'label' => array('for' => 'Contact1Name'),
  7905. 'Name',
  7906. '/label',
  7907. 'input' => array(
  7908. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7909. 'id' => 'Contact1Name', 'maxlength' => '255'
  7910. ),
  7911. '/div'
  7912. );
  7913. $this->assertTags($result, $expected);
  7914. $result = $this->Form->input('Contact.1.id');
  7915. $this->assertTags($result, array(
  7916. 'input' => array(
  7917. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7918. 'id' => 'Contact1Id'
  7919. )
  7920. ));
  7921. $result = $this->Form->input("Model.1.name");
  7922. $expected = array(
  7923. 'div' => array('class' => 'input text'),
  7924. 'label' => array('for' => 'Model1Name'),
  7925. 'Name',
  7926. '/label',
  7927. 'input' => array(
  7928. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7929. 'id' => 'Model1Name'
  7930. ),
  7931. '/div'
  7932. );
  7933. $this->assertTags($result, $expected);
  7934. }
  7935. /**
  7936. * testFormEnd method
  7937. *
  7938. * @return void
  7939. */
  7940. public function testFormEnd() {
  7941. $this->assertEquals('</form>', $this->Form->end());
  7942. $result = $this->Form->end('');
  7943. $expected = array(
  7944. 'div' => array('class' => 'submit'),
  7945. 'input' => array('type' => 'submit', 'value' => ''),
  7946. '/div',
  7947. '/form'
  7948. );
  7949. $this->assertTags($result, $expected);
  7950. $result = $this->Form->end(array('label' => ''));
  7951. $expected = array(
  7952. 'div' => array('class' => 'submit'),
  7953. 'input' => array('type' => 'submit', 'value' => ''),
  7954. '/div',
  7955. '/form'
  7956. );
  7957. $this->assertTags($result, $expected);
  7958. $result = $this->Form->end('save');
  7959. $expected = array(
  7960. 'div' => array('class' => 'submit'),
  7961. 'input' => array('type' => 'submit', 'value' => 'save'),
  7962. '/div',
  7963. '/form'
  7964. );
  7965. $this->assertTags($result, $expected);
  7966. $result = $this->Form->end(array('label' => 'save'));
  7967. $expected = array(
  7968. 'div' => array('class' => 'submit'),
  7969. 'input' => array('type' => 'submit', 'value' => 'save'),
  7970. '/div',
  7971. '/form'
  7972. );
  7973. $this->assertTags($result, $expected);
  7974. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7975. $expected = array(
  7976. 'div' => array('class' => 'submit'),
  7977. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7978. '/div',
  7979. '/form'
  7980. );
  7981. $this->assertTags($result, $expected);
  7982. $result = $this->Form->end(array('name' => 'Whatever'));
  7983. $expected = array(
  7984. 'div' => array('class' => 'submit'),
  7985. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7986. '/div',
  7987. '/form'
  7988. );
  7989. $this->assertTags($result, $expected);
  7990. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7991. $expected = array(
  7992. 'div' => array('class' => 'good'),
  7993. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7994. '/div',
  7995. '/form'
  7996. );
  7997. $this->assertTags($result, $expected);
  7998. $result = $this->Form->end(array(
  7999. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8000. ));
  8001. $expected = array(
  8002. 'div' => array('class' => 'good'),
  8003. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8004. '/div',
  8005. '/form'
  8006. );
  8007. $this->assertTags($result, $expected);
  8008. }
  8009. /**
  8010. * testMultipleFormWithIdFields method
  8011. *
  8012. * @return void
  8013. */
  8014. public function testMultipleFormWithIdFields() {
  8015. $this->Form->create('UserForm');
  8016. $result = $this->Form->input('id');
  8017. $this->assertTags($result, array('input' => array(
  8018. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8019. )));
  8020. $result = $this->Form->input('ValidateItem.id');
  8021. $this->assertTags($result, array('input' => array(
  8022. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8023. 'id' => 'ValidateItemId'
  8024. )));
  8025. $result = $this->Form->input('ValidateUser.id');
  8026. $this->assertTags($result, array('input' => array(
  8027. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8028. 'id' => 'ValidateUserId'
  8029. )));
  8030. }
  8031. /**
  8032. * testDbLessModel method
  8033. *
  8034. * @return void
  8035. */
  8036. public function testDbLessModel() {
  8037. $this->Form->create('TestMail');
  8038. $result = $this->Form->input('name');
  8039. $expected = array(
  8040. 'div' => array('class' => 'input text'),
  8041. 'label' => array('for' => 'TestMailName'),
  8042. 'Name',
  8043. '/label',
  8044. 'input' => array(
  8045. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8046. 'id' => 'TestMailName'
  8047. ),
  8048. '/div'
  8049. );
  8050. $this->assertTags($result, $expected);
  8051. ClassRegistry::init('TestMail');
  8052. $this->Form->create('TestMail');
  8053. $result = $this->Form->input('name');
  8054. $expected = array(
  8055. 'div' => array('class' => 'input text'),
  8056. 'label' => array('for' => 'TestMailName'),
  8057. 'Name',
  8058. '/label',
  8059. 'input' => array(
  8060. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8061. 'id' => 'TestMailName'
  8062. ),
  8063. '/div'
  8064. );
  8065. $this->assertTags($result, $expected);
  8066. }
  8067. /**
  8068. * testBrokenness method
  8069. *
  8070. * @return void
  8071. */
  8072. public function testBrokenness() {
  8073. /*
  8074. * #4 This test has two parents and four children. By default (as of r7117) both
  8075. * parents are show but the first parent is missing a child. This is the inconsistency
  8076. * in the default behaviour - one parent has all children, the other does not - dependent
  8077. * on the data values.
  8078. */
  8079. $result = $this->Form->select('Model.field', array(
  8080. 'Fred' => array(
  8081. 'freds_son_1' => 'Fred',
  8082. 'freds_son_2' => 'Freddie'
  8083. ),
  8084. 'Bert' => array(
  8085. 'berts_son_1' => 'Albert',
  8086. 'berts_son_2' => 'Bertie')
  8087. ),
  8088. array('showParents' => true, 'empty' => false)
  8089. );
  8090. $expected = array(
  8091. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8092. array('optgroup' => array('label' => 'Fred')),
  8093. array('option' => array('value' => 'freds_son_1')),
  8094. 'Fred',
  8095. '/option',
  8096. array('option' => array('value' => 'freds_son_2')),
  8097. 'Freddie',
  8098. '/option',
  8099. '/optgroup',
  8100. array('optgroup' => array('label' => 'Bert')),
  8101. array('option' => array('value' => 'berts_son_1')),
  8102. 'Albert',
  8103. '/option',
  8104. array('option' => array('value' => 'berts_son_2')),
  8105. 'Bertie',
  8106. '/option',
  8107. '/optgroup',
  8108. '/select'
  8109. );
  8110. $this->assertTags($result, $expected);
  8111. /*
  8112. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8113. * changed, so we should expect the same select list data, just with a different name
  8114. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8115. * This is where data corruption can occur, because when a select value is missing from
  8116. * a list a form will substitute the first value in the list - without the user knowing.
  8117. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8118. * affect the availability of 3 => 'Three' as a valid option.
  8119. */
  8120. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8121. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8122. ));
  8123. $result = $this->Form->select(
  8124. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8125. );
  8126. $expected = array(
  8127. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8128. array('option' => array('value' => 1)),
  8129. 'One',
  8130. '/option',
  8131. array('option' => array('value' => 2)),
  8132. 'Two',
  8133. '/option',
  8134. array('optgroup' => array('label' => 'Three')),
  8135. array('option' => array('value' => 3)),
  8136. 'Three',
  8137. '/option',
  8138. array('option' => array('value' => 4)),
  8139. 'Four',
  8140. '/option',
  8141. array('option' => array('value' => 5)),
  8142. 'Five',
  8143. '/option',
  8144. '/optgroup',
  8145. '/select'
  8146. );
  8147. $this->assertTags($result, $expected);
  8148. }
  8149. /**
  8150. * Test the generation of fields for a multi record form.
  8151. *
  8152. * @return void
  8153. */
  8154. public function testMultiRecordForm() {
  8155. $this->Form->create('ValidateProfile');
  8156. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8157. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8158. $expected = array(
  8159. 'div' => array('class' => 'input textarea'),
  8160. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8161. 'Name',
  8162. '/label',
  8163. 'textarea' => array(
  8164. 'id' => 'ValidateProfile1ValidateItem2Name',
  8165. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8166. 'cols' => 30,
  8167. 'rows' => 6
  8168. ),
  8169. 'Value',
  8170. '/textarea',
  8171. '/div'
  8172. );
  8173. $this->assertTags($result, $expected);
  8174. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8175. $expected = array(
  8176. 'div' => array('class' => 'input date'),
  8177. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8178. 'Created',
  8179. '/label',
  8180. array('select' => array(
  8181. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8182. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8183. )
  8184. ),
  8185. array('option' => array('value' => '')), '/option',
  8186. $this->dateRegex['monthsRegex'],
  8187. '/select', '-',
  8188. array('select' => array(
  8189. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8190. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8191. )
  8192. ),
  8193. array('option' => array('value' => '')), '/option',
  8194. $this->dateRegex['daysRegex'],
  8195. '/select', '-',
  8196. array('select' => array(
  8197. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8198. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8199. )
  8200. ),
  8201. array('option' => array('value' => '')), '/option',
  8202. $this->dateRegex['yearsRegex'],
  8203. '/select',
  8204. '/div'
  8205. );
  8206. $this->assertTags($result, $expected);
  8207. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8208. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8209. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8210. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8211. $expected = array(
  8212. 'div' => array('class' => 'input select error'),
  8213. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8214. 'Profile',
  8215. '/label',
  8216. 'select' => array(
  8217. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8218. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8219. 'class' => 'form-error'
  8220. ),
  8221. '/select',
  8222. array('div' => array('class' => 'error-message')),
  8223. 'Error',
  8224. '/div',
  8225. '/div'
  8226. );
  8227. $this->assertTags($result, $expected);
  8228. }
  8229. /**
  8230. * test the correct display of multi-record form validation errors.
  8231. *
  8232. * @return void
  8233. */
  8234. public function testMultiRecordFormValidationErrors() {
  8235. $this->Form->create('ValidateProfile');
  8236. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8237. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8238. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8239. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8240. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8241. $result = $this->Form->error('ValidateProfile.2.city');
  8242. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8243. $result = $this->Form->error('2.city');
  8244. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8245. }
  8246. /**
  8247. * test the correct display of multi-record form validation errors.
  8248. *
  8249. * @return void
  8250. */
  8251. public function testSaveManyRecordFormValidationErrors() {
  8252. $this->Form->create('ValidateUser');
  8253. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8254. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8255. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8256. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8257. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8258. $result = $this->Form->error('ValidateUser.0.city');
  8259. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8260. }
  8261. /**
  8262. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8263. *
  8264. * @return void
  8265. */
  8266. public function testInputTemplate() {
  8267. $result = $this->Form->input('Contact.email', array(
  8268. 'type' => 'text', 'format' => array('input')
  8269. ));
  8270. $expected = array(
  8271. 'div' => array('class' => 'input text'),
  8272. 'input' => array(
  8273. 'type' => 'text', 'name' => 'data[Contact][email]',
  8274. 'id' => 'ContactEmail'
  8275. ),
  8276. '/div'
  8277. );
  8278. $this->assertTags($result, $expected);
  8279. $result = $this->Form->input('Contact.email', array(
  8280. 'type' => 'text', 'format' => array('input', 'label'),
  8281. 'label' => '<em>Email (required)</em>'
  8282. ));
  8283. $expected = array(
  8284. 'div' => array('class' => 'input text'),
  8285. array('input' => array(
  8286. 'type' => 'text', 'name' => 'data[Contact][email]',
  8287. 'id' => 'ContactEmail'
  8288. )),
  8289. 'label' => array('for' => 'ContactEmail'),
  8290. 'em' => array(),
  8291. 'Email (required)',
  8292. '/em',
  8293. '/label',
  8294. '/div'
  8295. );
  8296. $this->assertTags($result, $expected);
  8297. $result = $this->Form->input('Contact.email', array(
  8298. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8299. 'between' => '<div>Something in the middle</div>',
  8300. 'after' => '<span>Some text at the end</span>'
  8301. ));
  8302. $expected = array(
  8303. 'div' => array('class' => 'input text'),
  8304. array('input' => array(
  8305. 'type' => 'text', 'name' => 'data[Contact][email]',
  8306. 'id' => 'ContactEmail'
  8307. )),
  8308. array('div' => array()),
  8309. 'Something in the middle',
  8310. '/div',
  8311. 'label' => array('for' => 'ContactEmail'),
  8312. 'Email',
  8313. '/label',
  8314. 'span' => array(),
  8315. 'Some text at the end',
  8316. '/span',
  8317. '/div'
  8318. );
  8319. $this->assertTags($result, $expected);
  8320. $result = $this->Form->input('Contact.method', array(
  8321. 'type' => 'radio',
  8322. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8323. 'between' => 'I am between',
  8324. ));
  8325. $expected = array(
  8326. 'div' => array('class' => 'input radio'),
  8327. 'fieldset' => array(),
  8328. 'legend' => array(),
  8329. 'Method',
  8330. '/legend',
  8331. 'I am between',
  8332. 'input' => array(
  8333. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8334. 'value' => '', 'id' => 'ContactMethod_'
  8335. ),
  8336. array('input' => array(
  8337. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8338. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8339. )),
  8340. array('label' => array('for' => 'ContactMethodEmail')),
  8341. 'Email',
  8342. '/label',
  8343. array('input' => array(
  8344. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8345. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8346. )),
  8347. array('label' => array('for' => 'ContactMethodPigeon')),
  8348. 'Pigeon',
  8349. '/label',
  8350. '/fieldset',
  8351. '/div',
  8352. );
  8353. $this->assertTags($result, $expected);
  8354. }
  8355. /**
  8356. * test that some html5 inputs + FormHelper::__call() work
  8357. *
  8358. * @return void
  8359. */
  8360. public function testHtml5Inputs() {
  8361. $result = $this->Form->email('User.email');
  8362. $expected = array(
  8363. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8364. );
  8365. $this->assertTags($result, $expected);
  8366. $result = $this->Form->search('User.query');
  8367. $expected = array(
  8368. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8369. );
  8370. $this->assertTags($result, $expected);
  8371. $result = $this->Form->search('User.query', array('value' => 'test'));
  8372. $expected = array(
  8373. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8374. );
  8375. $this->assertTags($result, $expected);
  8376. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8377. $expected = array(
  8378. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8379. );
  8380. $this->assertTags($result, $expected);
  8381. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8382. $expected = array(
  8383. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8384. );
  8385. $this->assertTags($result, $expected);
  8386. }
  8387. /**
  8388. *
  8389. * @expectedException CakeException
  8390. * @return void
  8391. */
  8392. public function testHtml5InputException() {
  8393. $this->Form->email();
  8394. }
  8395. /**
  8396. * Tests that a model can be loaded from the model names passed in the request object
  8397. *
  8398. * @return void
  8399. */
  8400. public function testIntrospectModelFromRequest() {
  8401. $this->loadFixtures('Post');
  8402. App::build(array(
  8403. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8404. ));
  8405. CakePlugin::load('TestPlugin');
  8406. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8407. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8408. $this->Form->create('TestPluginPost');
  8409. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8410. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8411. CakePlugin::unload();
  8412. App::build();
  8413. }
  8414. /**
  8415. * Tests that it is possible to set the validation errors directly in the helper for a field
  8416. *
  8417. * @return void
  8418. */
  8419. public function testCustomValidationErrors() {
  8420. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8421. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8422. $this->assertEquals('Badness!', $result);
  8423. }
  8424. /**
  8425. * Tests that the 'on' key validates as expected on create
  8426. *
  8427. * @return void
  8428. */
  8429. public function testRequiredOnCreate() {
  8430. $this->Form->create('Contact');
  8431. $result = $this->Form->input('Contact.imrequiredonupdate');
  8432. $expected = array(
  8433. 'div' => array('class' => 'input text'),
  8434. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8435. 'Imrequiredonupdate',
  8436. '/label',
  8437. 'input' => array(
  8438. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8439. 'id' => 'ContactImrequiredonupdate'
  8440. ),
  8441. '/div'
  8442. );
  8443. $this->assertTags($result, $expected);
  8444. $result = $this->Form->input('Contact.imrequiredoncreate');
  8445. $expected = array(
  8446. 'div' => array('class' => 'input text required'),
  8447. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8448. 'Imrequiredoncreate',
  8449. '/label',
  8450. 'input' => array(
  8451. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8452. 'id' => 'ContactImrequiredoncreate',
  8453. 'required' => 'required'
  8454. ),
  8455. '/div'
  8456. );
  8457. $this->assertTags($result, $expected);
  8458. $result = $this->Form->input('Contact.imrequiredonboth');
  8459. $expected = array(
  8460. 'div' => array('class' => 'input text required'),
  8461. 'label' => array('for' => 'ContactImrequiredonboth'),
  8462. 'Imrequiredonboth',
  8463. '/label',
  8464. 'input' => array(
  8465. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8466. 'id' => 'ContactImrequiredonboth',
  8467. 'required' => 'required'
  8468. ),
  8469. '/div'
  8470. );
  8471. $this->assertTags($result, $expected);
  8472. $this->Form->inputDefaults(array('required' => false));
  8473. $result = $this->Form->input('Contact.imrequired');
  8474. $expected = array(
  8475. 'div' => array('class' => 'input text'),
  8476. 'label' => array('for' => 'ContactImrequired'),
  8477. 'Imrequired',
  8478. '/label',
  8479. 'input' => array(
  8480. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8481. 'id' => 'ContactImrequired'
  8482. ),
  8483. '/div'
  8484. );
  8485. $this->assertTags($result, $expected);
  8486. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8487. $this->assertTags($result, $expected);
  8488. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8489. $expected = array(
  8490. 'div' => array('class' => 'input text required'),
  8491. 'label' => array('for' => 'ContactImrequired'),
  8492. 'Imrequired',
  8493. '/label',
  8494. 'input' => array(
  8495. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8496. 'id' => 'ContactImrequired'
  8497. ),
  8498. '/div'
  8499. );
  8500. $this->assertTags($result, $expected);
  8501. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8502. $this->assertTags($result, $expected);
  8503. }
  8504. /**
  8505. * Tests that the 'on' key validates as expected on update
  8506. *
  8507. * @return void
  8508. */
  8509. public function testRequiredOnUpdate() {
  8510. $this->Form->request->data['Contact']['id'] = 1;
  8511. $this->Form->create('Contact');
  8512. $result = $this->Form->input('Contact.imrequiredonupdate');
  8513. $expected = array(
  8514. 'div' => array('class' => 'input text required'),
  8515. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8516. 'Imrequiredonupdate',
  8517. '/label',
  8518. 'input' => array(
  8519. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8520. 'id' => 'ContactImrequiredonupdate',
  8521. 'required' => 'required'
  8522. ),
  8523. '/div'
  8524. );
  8525. $this->assertTags($result, $expected);
  8526. $result = $this->Form->input('Contact.imrequiredoncreate');
  8527. $expected = array(
  8528. 'div' => array('class' => 'input text'),
  8529. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8530. 'Imrequiredoncreate',
  8531. '/label',
  8532. 'input' => array(
  8533. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8534. 'id' => 'ContactImrequiredoncreate'
  8535. ),
  8536. '/div'
  8537. );
  8538. $this->assertTags($result, $expected);
  8539. $result = $this->Form->input('Contact.imrequiredonboth');
  8540. $expected = array(
  8541. 'div' => array('class' => 'input text required'),
  8542. 'label' => array('for' => 'ContactImrequiredonboth'),
  8543. 'Imrequiredonboth',
  8544. '/label',
  8545. 'input' => array(
  8546. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8547. 'id' => 'ContactImrequiredonboth',
  8548. 'required' => 'required'
  8549. ),
  8550. '/div'
  8551. );
  8552. $this->assertTags($result, $expected);
  8553. $result = $this->Form->input('Contact.imrequired');
  8554. $expected = array(
  8555. 'div' => array('class' => 'input text required'),
  8556. 'label' => array('for' => 'ContactImrequired'),
  8557. 'Imrequired',
  8558. '/label',
  8559. 'input' => array(
  8560. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8561. 'id' => 'ContactImrequired',
  8562. 'required' => 'required'
  8563. ),
  8564. '/div'
  8565. );
  8566. $this->assertTags($result, $expected);
  8567. }
  8568. /**
  8569. * Test inputDefaults setter and getter
  8570. *
  8571. * @return void
  8572. */
  8573. public function testInputDefaults() {
  8574. $this->Form->create('Contact');
  8575. $this->Form->inputDefaults(array(
  8576. 'label' => false,
  8577. 'div' => array(
  8578. 'style' => 'color: #000;'
  8579. )
  8580. ));
  8581. $result = $this->Form->input('Contact.field1');
  8582. $expected = array(
  8583. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8584. 'input' => array(
  8585. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8586. 'id' => 'ContactField1'
  8587. ),
  8588. '/div'
  8589. );
  8590. $this->assertTags($result, $expected);
  8591. $this->Form->inputDefaults(array(
  8592. 'div' => false,
  8593. 'label' => 'Label',
  8594. ));
  8595. $result = $this->Form->input('Contact.field1');
  8596. $expected = array(
  8597. 'label' => array('for' => 'ContactField1'),
  8598. 'Label',
  8599. '/label',
  8600. 'input' => array(
  8601. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8602. 'id' => 'ContactField1'
  8603. ),
  8604. );
  8605. $this->assertTags($result, $expected);
  8606. $this->Form->inputDefaults(array(
  8607. 'label' => false,
  8608. ), true);
  8609. $result = $this->Form->input('Contact.field1');
  8610. $expected = array(
  8611. 'input' => array(
  8612. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8613. 'id' => 'ContactField1'
  8614. ),
  8615. );
  8616. $this->assertTags($result, $expected);
  8617. $result = $this->Form->inputDefaults();
  8618. $expected = array(
  8619. 'div' => false,
  8620. 'label' => false,
  8621. );
  8622. $this->assertEquals($expected, $result);
  8623. }
  8624. }