FormHelperTest.php 188 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Network\Request;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\View\Helper\FormHelper;
  29. use Cake\View\Helper\HtmlHelper;
  30. use Cake\View\View;
  31. /**
  32. * Test stub.
  33. */
  34. class Article extends Entity {
  35. }
  36. /**
  37. * Contact class
  38. *
  39. */
  40. class ContactsTable extends Table {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = array(
  47. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  48. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  49. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  50. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  51. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  52. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  53. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  54. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  55. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  56. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  57. );
  58. /**
  59. * Initializes the schema
  60. *
  61. * @return void
  62. */
  63. public function initialize(array $config) {
  64. $this->schema($this->_schema);
  65. }
  66. }
  67. /**
  68. * ValidateUser class
  69. *
  70. */
  71. class ValidateUsersTable extends Table {
  72. /**
  73. * schema method
  74. *
  75. * @var array
  76. */
  77. protected $_schema = array(
  78. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  79. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  80. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'balance' => array('type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2),
  82. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3),
  83. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6),
  84. 'population' => array('type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  88. );
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config) {
  95. $this->schema($this->_schema);
  96. }
  97. }
  98. /**
  99. * FormHelperTest class
  100. *
  101. * @property FormHelper $Form
  102. */
  103. class FormHelperTest extends TestCase {
  104. /**
  105. * Fixtures to be used
  106. *
  107. * @var array
  108. */
  109. public $fixtures = array('core.articles', 'core.comments');
  110. /**
  111. * Do not load the fixtures by default
  112. *
  113. * @var bool
  114. */
  115. public $autoFixtures = false;
  116. /**
  117. * setUp method
  118. *
  119. * @return void
  120. */
  121. public function setUp() {
  122. parent::setUp();
  123. Configure::write('Config.language', 'eng');
  124. Configure::write('App.base', '');
  125. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  126. Configure::delete('Asset');
  127. $this->View = new View();
  128. $this->Form = new FormHelper($this->View);
  129. $request = new Request('articles/add');
  130. $request->here = '/articles/add';
  131. $request['controller'] = 'articles';
  132. $request['action'] = 'add';
  133. $request->webroot = '';
  134. $request->base = '';
  135. $this->Form->Url->request = $this->Form->request = $request;
  136. $this->dateRegex = array(
  137. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  138. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  139. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  140. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  142. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  143. );
  144. $this->article = [
  145. 'schema' => [
  146. 'id' => ['type' => 'integer'],
  147. 'author_id' => ['type' => 'integer', 'null' => true],
  148. 'title' => ['type' => 'string', 'null' => true],
  149. 'body' => 'text',
  150. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  151. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  152. ],
  153. 'required' => [
  154. 'author_id' => true,
  155. 'title' => true,
  156. ]
  157. ];
  158. Security::salt('foo!');
  159. Router::connect('/:controller', array('action' => 'index'));
  160. Router::connect('/:controller/:action/*');
  161. }
  162. /**
  163. * tearDown method
  164. *
  165. * @return void
  166. */
  167. public function tearDown() {
  168. parent::tearDown();
  169. unset($this->Form, $this->Controller, $this->View);
  170. TableRegistry::clear();
  171. }
  172. /**
  173. * Test construct() with the templates option.
  174. *
  175. * @return void
  176. */
  177. public function testConstructTemplatesFile() {
  178. $helper = new FormHelper($this->View, [
  179. 'templates' => 'htmlhelper_tags.php'
  180. ]);
  181. $result = $helper->input('name');
  182. $this->assertContains('<input', $result);
  183. }
  184. /**
  185. * Test that when specifying custom widgets the config array for that widget
  186. * is overwritten instead of merged.
  187. *
  188. * @return void
  189. */
  190. public function testConstructWithWidgets() {
  191. $expected = [
  192. 'button' => ['Cake\View\Widget\ButtonWidget'],
  193. 'checkbox' => ['Cake\View\Widget\CheckboxWidget'],
  194. 'file' => ['Cake\View\Widget\FileWidget'],
  195. 'label' => ['Cake\View\Widget\LabelWidget'],
  196. 'nestingLabel' => ['Cake\View\Widget\NestingLabelWidget'],
  197. 'multicheckbox' => ['Cake\View\Widget\MultiCheckboxWidget', 'nestingLabel'],
  198. 'radio' => ['Cake\View\Widget\RadioWidget', 'nestingLabel'],
  199. 'select' => ['Cake\View\Widget\SelectBoxWidget'],
  200. 'textarea' => ['Cake\View\Widget\TextareaWidget'],
  201. 'datetime' => ['MyPlugin\View\Widget\DateTimeWidget', 'select'],
  202. '_default' => ['Cake\View\Widget\BasicWidget']
  203. ];
  204. $helper = $this->getMock(
  205. 'Cake\View\Helper\FormHelper',
  206. ['widgetRegistry'],
  207. [],
  208. '',
  209. false
  210. );
  211. $helper->expects($this->once())
  212. ->method('widgetRegistry')
  213. ->with(null, $expected);
  214. $config = [
  215. 'widgets' => [
  216. 'datetime' => ['MyPlugin\View\Widget\DateTimeWidget', 'select']
  217. ]
  218. ];
  219. $helper->__construct($this->View, $config);
  220. }
  221. /**
  222. * Test registering a new widget class and rendering it.
  223. *
  224. * @return void
  225. */
  226. public function testAddWidgetAndRenderWidget() {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  231. $this->assertNull($this->Form->addWidget('test', $mock));
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test registering an invalid widget class.
  241. *
  242. * @expectedException \RuntimeException
  243. * @return void
  244. */
  245. public function testAddWidgetInvalid() {
  246. $mock = new \StdClass();
  247. $this->Form->addWidget('test', $mock);
  248. $this->Form->widget('test');
  249. }
  250. /**
  251. * Test adding a new context class.
  252. *
  253. * @return void
  254. */
  255. public function testAddContextProvider() {
  256. $context = 'My data';
  257. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  258. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  259. $this->assertInstanceOf('Cake\Network\Request', $request);
  260. $this->assertEquals($context, $data['entity']);
  261. return $stub;
  262. });
  263. $this->Form->create($context);
  264. $result = $this->Form->context();
  265. $this->assertSame($stub, $result);
  266. }
  267. /**
  268. * Test replacing a context class.
  269. *
  270. * @return void
  271. */
  272. public function testAddContextProviderReplace() {
  273. $entity = new Article();
  274. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  275. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  276. return $stub;
  277. });
  278. $this->Form->create($entity);
  279. $result = $this->Form->context();
  280. $this->assertSame($stub, $result);
  281. }
  282. /**
  283. * Test overriding a context class.
  284. *
  285. * @return void
  286. */
  287. public function testAddContextProviderAdd() {
  288. $entity = new Article();
  289. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  290. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  291. if ($data['entity'] instanceof Entity) {
  292. return $stub;
  293. }
  294. });
  295. $this->Form->create($entity);
  296. $result = $this->Form->context();
  297. $this->assertSame($stub, $result);
  298. }
  299. /**
  300. * Test adding an invalid context class.
  301. *
  302. * @expectedException \RuntimeException
  303. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  304. * @return void
  305. */
  306. public function testAddContextProviderInvalid() {
  307. $context = 'My data';
  308. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  309. return new \StdClass();
  310. });
  311. $this->Form->create($context);
  312. }
  313. /**
  314. * Provides context options for create().
  315. *
  316. * @return array
  317. */
  318. public function contextSelectionProvider() {
  319. $entity = new Article();
  320. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  321. $emptyCollection = new Collection([]);
  322. $emptyArray = [];
  323. $arrayObject = new \ArrayObject([]);
  324. $data = [
  325. 'schema' => [
  326. 'title' => ['type' => 'string']
  327. ]
  328. ];
  329. return [
  330. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  331. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  332. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  333. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  334. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  335. 'none' => [null, 'Cake\View\Form\NullContext'],
  336. 'false' => [false, 'Cake\View\Form\NullContext'],
  337. ];
  338. }
  339. /**
  340. * Test default context selection in create()
  341. *
  342. * @dataProvider contextSelectionProvider
  343. * @return void
  344. */
  345. public function testCreateContextSelectionBuiltIn($data, $class) {
  346. $this->loadFixtures('Articles');
  347. $this->Form->create($data);
  348. $this->assertInstanceOf($class, $this->Form->context());
  349. }
  350. /**
  351. * Data provider for type option.
  352. *
  353. * @return array
  354. */
  355. public static function requestTypeProvider() {
  356. return [
  357. // type, method, override
  358. ['post', 'post', 'POST'],
  359. ['put', 'post', 'PUT'],
  360. ['patch', 'post', 'PATCH'],
  361. ['delete', 'post', 'DELETE'],
  362. ];
  363. }
  364. /**
  365. * Test creating file forms.
  366. *
  367. * @return void
  368. */
  369. public function testCreateFile() {
  370. $encoding = strtolower(Configure::read('App.encoding'));
  371. $result = $this->Form->create(false, array('type' => 'file'));
  372. $expected = array(
  373. 'form' => array(
  374. 'method' => 'post', 'action' => '/articles/add',
  375. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  376. ),
  377. 'div' => array('style' => 'display:none;'),
  378. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  379. '/div'
  380. );
  381. $this->assertHtml($expected, $result);
  382. }
  383. /**
  384. * Test creating GET forms.
  385. *
  386. * @return void
  387. */
  388. public function testCreateGet() {
  389. $encoding = strtolower(Configure::read('App.encoding'));
  390. $result = $this->Form->create(false, array('type' => 'get'));
  391. $expected = array('form' => array(
  392. 'method' => 'get', 'action' => '/articles/add',
  393. 'accept-charset' => $encoding
  394. ));
  395. $this->assertHtml($expected, $result);
  396. }
  397. /**
  398. * Test create() with the templates option.
  399. *
  400. * @return void
  401. */
  402. public function testCreateTemplatesArray() {
  403. $result = $this->Form->create($this->article, [
  404. 'templates' => [
  405. 'formstart' => '<form class="form-horizontal"{{attrs}}>',
  406. ]
  407. ]);
  408. $expected = [
  409. 'form' => [
  410. 'class' => 'form-horizontal',
  411. 'method' => 'post',
  412. 'action' => '/articles/add',
  413. 'accept-charset' => 'utf-8'
  414. ]
  415. ];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test create() with the templates option.
  420. *
  421. * @return void
  422. */
  423. public function testCreateTemplatesFile() {
  424. $result = $this->Form->create($this->article, [
  425. 'templates' => 'htmlhelper_tags.php',
  426. ]);
  427. $expected = [
  428. 'start form',
  429. 'div' => ['class' => 'hidden'],
  430. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  431. '/div'
  432. ];
  433. $this->assertHtml($expected, $result);
  434. }
  435. /**
  436. * Test that create() and end() restore templates.
  437. *
  438. * @return void
  439. */
  440. public function testCreateEndRestoreTemplates() {
  441. $this->Form->create($this->article, [
  442. 'templates' => ['input' => 'custom input element']
  443. ]);
  444. $this->Form->end();
  445. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  446. }
  447. /**
  448. * test the create() method
  449. *
  450. * @dataProvider requestTypeProvider
  451. * @return void
  452. */
  453. public function testCreateTypeOptions($type, $method, $override) {
  454. $encoding = strtolower(Configure::read('App.encoding'));
  455. $result = $this->Form->create(false, array('type' => $type));
  456. $expected = array(
  457. 'form' => array(
  458. 'method' => $method, 'action' => '/articles/add',
  459. 'accept-charset' => $encoding
  460. ),
  461. 'div' => array('style' => 'display:none;'),
  462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  463. '/div'
  464. );
  465. $this->assertHtml($expected, $result);
  466. }
  467. /**
  468. * Test opening a form for an update operation.
  469. *
  470. * @return void
  471. */
  472. public function testCreateUpdateForm() {
  473. $encoding = strtolower(Configure::read('App.encoding'));
  474. $this->Form->request->here = '/articles/edit/1';
  475. $this->Form->request['action'] = 'edit';
  476. $this->article['defaults']['id'] = 1;
  477. $result = $this->Form->create($this->article);
  478. $expected = array(
  479. 'form' => array(
  480. 'method' => 'post', 'action' => '/articles/edit/1',
  481. 'accept-charset' => $encoding
  482. ),
  483. 'div' => array('style' => 'display:none;'),
  484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  485. '/div'
  486. );
  487. $this->assertHtml($expected, $result);
  488. }
  489. /**
  490. * test create() with automatic url generation
  491. *
  492. * @return void
  493. */
  494. public function testCreateAutoUrl() {
  495. $encoding = strtolower(Configure::read('App.encoding'));
  496. $this->Form->request['action'] = 'delete';
  497. $this->Form->request->here = '/articles/delete/10';
  498. $this->Form->request->base = '';
  499. $result = $this->Form->create($this->article);
  500. $expected = array(
  501. 'form' => array(
  502. 'method' => 'post', 'action' => '/articles/delete/10',
  503. 'accept-charset' => $encoding
  504. ),
  505. 'div' => array('style' => 'display:none;'),
  506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  507. '/div'
  508. );
  509. $this->assertHtml($expected, $result);
  510. $this->article['defaults'] = ['id' => 1];
  511. $this->Form->request->here = '/articles/edit/1';
  512. $this->Form->request['action'] = 'delete';
  513. $result = $this->Form->create($this->article, ['action' => 'edit']);
  514. $expected = array(
  515. 'form' => array(
  516. 'method' => 'post',
  517. 'action' => '/articles/edit/1',
  518. 'accept-charset' => $encoding
  519. ),
  520. 'div' => array('style' => 'display:none;'),
  521. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  522. '/div'
  523. );
  524. $this->assertHtml($expected, $result);
  525. $this->Form->request['action'] = 'add';
  526. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  527. $expected = array(
  528. 'form' => array(
  529. 'method' => 'post',
  530. 'action' => '/articles/publish/1',
  531. 'accept-charset' => $encoding
  532. ),
  533. 'div' => array('style' => 'display:none;'),
  534. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  535. '/div'
  536. );
  537. $this->assertHtml($expected, $result);
  538. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  539. $expected = array(
  540. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  541. 'div' => array('style' => 'display:none;'),
  542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  543. '/div'
  544. );
  545. $this->assertHtml($expected, $result);
  546. $this->Form->request['controller'] = 'Pages';
  547. $result = $this->Form->create($this->article, array('action' => 'signup'));
  548. $expected = array(
  549. 'form' => array(
  550. 'method' => 'post', 'action' => '/Pages/signup/1',
  551. 'accept-charset' => $encoding
  552. ),
  553. 'div' => array('style' => 'display:none;'),
  554. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  555. '/div'
  556. );
  557. $this->assertHtml($expected, $result);
  558. }
  559. /**
  560. * test create() with a custom route
  561. *
  562. * @return void
  563. */
  564. public function testCreateCustomRoute() {
  565. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  566. $encoding = strtolower(Configure::read('App.encoding'));
  567. $this->Form->request['controller'] = 'users';
  568. $result = $this->Form->create(false, array('action' => 'login'));
  569. $expected = array(
  570. 'form' => array(
  571. 'method' => 'post', 'action' => '/login',
  572. 'accept-charset' => $encoding
  573. ),
  574. 'div' => array('style' => 'display:none;'),
  575. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  576. '/div'
  577. );
  578. $this->assertHtml($expected, $result);
  579. Router::connect(
  580. '/new-article',
  581. ['controller' => 'articles', 'action' => 'myaction'],
  582. ['_name' => 'my-route']
  583. );
  584. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  585. $expected = array(
  586. 'form' => array(
  587. 'method' => 'post', 'action' => '/new-article',
  588. 'accept-charset' => $encoding,
  589. ),
  590. 'div' => array('style' => 'display:none;'),
  591. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  592. '/div'
  593. );
  594. $this->assertHtml($expected, $result);
  595. }
  596. /**
  597. * test automatic accept-charset overriding
  598. *
  599. * @return void
  600. */
  601. public function testCreateWithAcceptCharset() {
  602. $result = $this->Form->create($this->article, array(
  603. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  604. )
  605. );
  606. $expected = array(
  607. 'form' => array(
  608. 'method' => 'post', 'action' => '/articles',
  609. 'accept-charset' => 'iso-8859-1'
  610. ),
  611. 'div' => array('style' => 'display:none;'),
  612. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  613. '/div'
  614. );
  615. $this->assertHtml($expected, $result);
  616. }
  617. /**
  618. * Test base form URL when url param is passed with multiple parameters (&)
  619. *
  620. */
  621. public function testCreateQuerystringrequest() {
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $result = $this->Form->create($this->article, array(
  624. 'type' => 'post',
  625. 'escape' => false,
  626. 'url' => array(
  627. 'controller' => 'controller',
  628. 'action' => 'action',
  629. '?' => array('param1' => 'value1', 'param2' => 'value2')
  630. )
  631. ));
  632. $expected = array(
  633. 'form' => array(
  634. 'method' => 'post',
  635. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  636. 'accept-charset' => $encoding
  637. ),
  638. 'div' => array('style' => 'display:none;'),
  639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  640. '/div'
  641. );
  642. $this->assertHtml($expected, $result);
  643. $result = $this->Form->create($this->article, array(
  644. 'type' => 'post',
  645. 'url' => array(
  646. 'controller' => 'controller',
  647. 'action' => 'action',
  648. '?' => array('param1' => 'value1', 'param2' => 'value2')
  649. )
  650. ));
  651. $this->assertHtml($expected, $result);
  652. }
  653. /**
  654. * test that create() doesn't cause errors by multiple id's being in the primary key
  655. * as could happen with multiple select or checkboxes.
  656. *
  657. * @return void
  658. */
  659. public function testCreateWithMultipleIdInData() {
  660. $encoding = strtolower(Configure::read('App.encoding'));
  661. $this->Form->request->data['Article']['id'] = array(1, 2);
  662. $result = $this->Form->create($this->article);
  663. $expected = array(
  664. 'form' => array(
  665. 'method' => 'post',
  666. 'action' => '/articles/add',
  667. 'accept-charset' => $encoding
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertHtml($expected, $result);
  674. }
  675. /**
  676. * test that create() doesn't add in extra passed params.
  677. *
  678. * @return void
  679. */
  680. public function testCreatePassedArgs() {
  681. $encoding = strtolower(Configure::read('App.encoding'));
  682. $this->Form->request->data['Article']['id'] = 1;
  683. $result = $this->Form->create($this->article, array(
  684. 'type' => 'post',
  685. 'escape' => false,
  686. 'url' => array(
  687. 'action' => 'edit',
  688. 'myparam'
  689. )
  690. ));
  691. $expected = array(
  692. 'form' => array(
  693. 'method' => 'post',
  694. 'action' => '/articles/edit/myparam',
  695. 'accept-charset' => $encoding
  696. ),
  697. 'div' => array('style' => 'display:none;'),
  698. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  699. '/div'
  700. );
  701. $this->assertHtml($expected, $result);
  702. }
  703. /**
  704. * test creating a get form, and get form inputs.
  705. *
  706. * @return void
  707. */
  708. public function testGetFormCreate() {
  709. $encoding = strtolower(Configure::read('App.encoding'));
  710. $result = $this->Form->create($this->article, array('type' => 'get'));
  711. $expected = array('form' => array(
  712. 'method' => 'get', 'action' => '/articles/add',
  713. 'accept-charset' => $encoding
  714. ));
  715. $this->assertHtml($expected, $result);
  716. $result = $this->Form->text('title');
  717. $expected = array('input' => array(
  718. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  719. ));
  720. $this->assertHtml($expected, $result);
  721. $result = $this->Form->password('password');
  722. $expected = array('input' => array(
  723. 'name' => 'password', 'type' => 'password'
  724. ));
  725. $this->assertHtml($expected, $result);
  726. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  727. $result = $this->Form->text('user_form');
  728. $expected = array('input' => array(
  729. 'name' => 'user_form', 'type' => 'text'
  730. ));
  731. $this->assertHtml($expected, $result);
  732. }
  733. /**
  734. * test get form, and inputs when the model param is false
  735. *
  736. * @return void
  737. */
  738. public function testGetFormWithFalseModel() {
  739. $encoding = strtolower(Configure::read('App.encoding'));
  740. $this->Form->request['controller'] = 'contact_test';
  741. $result = $this->Form->create(false, array(
  742. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  743. ));
  744. $expected = array('form' => array(
  745. 'method' => 'get', 'action' => '/contact_test/add',
  746. 'accept-charset' => $encoding
  747. ));
  748. $this->assertHtml($expected, $result);
  749. $result = $this->Form->text('reason');
  750. $expected = array(
  751. 'input' => array('type' => 'text', 'name' => 'reason')
  752. );
  753. $this->assertHtml($expected, $result);
  754. }
  755. /**
  756. * testFormCreateWithSecurity method
  757. *
  758. * Test form->create() with security key.
  759. *
  760. * @return void
  761. */
  762. public function testCreateWithSecurity() {
  763. $this->Form->request->params['_csrfToken'] = 'testKey';
  764. $encoding = strtolower(Configure::read('App.encoding'));
  765. $result = $this->Form->create($this->article, [
  766. 'url' => '/articles/publish',
  767. ]);
  768. $expected = array(
  769. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  770. 'div' => array('style' => 'display:none;'),
  771. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  772. array('input' => array(
  773. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  774. )),
  775. '/div'
  776. );
  777. $this->assertHtml($expected, $result);
  778. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  779. $expected['form']['id'] = 'MyForm';
  780. $this->assertHtml($expected, $result);
  781. }
  782. /**
  783. * testFormCreateGetNoSecurity method
  784. *
  785. * Test form->create() with no security key as its a get form
  786. *
  787. * @return void
  788. */
  789. public function testCreateEndGetNoSecurity() {
  790. $this->Form->request->params['_csrfToken'] = 'testKey';
  791. $encoding = strtolower(Configure::read('App.encoding'));
  792. $article = new Article();
  793. $result = $this->Form->create($article, [
  794. 'type' => 'get',
  795. 'url' => '/contacts/add'
  796. ]);
  797. $this->assertNotContains('testKey', $result);
  798. $result = $this->Form->end();
  799. $this->assertNotContains('testKey', $result);
  800. }
  801. /**
  802. * test that create() clears the fields property so it starts fresh
  803. *
  804. * @return void
  805. */
  806. public function testCreateClearingFields() {
  807. $this->Form->fields = array('model_id');
  808. $this->Form->create($this->article);
  809. $this->assertEquals(array(), $this->Form->fields);
  810. }
  811. /**
  812. * Tests form hash generation with model-less data
  813. *
  814. * @return void
  815. */
  816. public function testValidateHashNoModel() {
  817. $this->Form->request->params['_Token'] = 'foo';
  818. $result = $this->Form->secure(array('anything'));
  819. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  820. }
  821. /**
  822. * Tests that hidden fields generated for checkboxes don't get locked
  823. *
  824. * @return void
  825. */
  826. public function testNoCheckboxLocking() {
  827. $this->Form->request->params['_Token'] = 'foo';
  828. $this->assertSame([], $this->Form->fields);
  829. $this->Form->checkbox('check', array('value' => '1'));
  830. $this->assertSame($this->Form->fields, array('check'));
  831. }
  832. /**
  833. * testFormSecurityFields method
  834. *
  835. * Test generation of secure form hash generation.
  836. *
  837. * @return void
  838. */
  839. public function testFormSecurityFields() {
  840. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  841. $this->Form->request->params['_Token'] = 'testKey';
  842. $result = $this->Form->secure($fields);
  843. $hash = Security::hash(serialize($fields) . Security::salt());
  844. $hash .= ':' . 'Model.valid';
  845. $hash = urlencode($hash);
  846. $expected = array(
  847. 'div' => array('style' => 'display:none;'),
  848. array('input' => array(
  849. 'type' => 'hidden',
  850. 'name' => '_Token[fields]',
  851. 'value' => $hash
  852. )),
  853. array('input' => array(
  854. 'type' => 'hidden',
  855. 'name' => '_Token[unlocked]',
  856. 'value' => '',
  857. )),
  858. '/div'
  859. );
  860. $this->assertHtml($expected, $result);
  861. }
  862. /**
  863. * Tests correct generation of number fields for double and float fields
  864. *
  865. * @return void
  866. */
  867. public function testTextFieldGenerationForFloats() {
  868. $this->article['schema'] = [
  869. 'foo' => [
  870. 'type' => 'float',
  871. 'null' => false,
  872. 'default' => null,
  873. 'length' => 10
  874. ]
  875. ];
  876. $this->Form->create($this->article);
  877. $result = $this->Form->input('foo');
  878. $expected = array(
  879. 'div' => array('class' => 'input number'),
  880. 'label' => array('for' => 'foo'),
  881. 'Foo',
  882. '/label',
  883. array('input' => array(
  884. 'type' => 'number',
  885. 'name' => 'foo',
  886. 'id' => 'foo',
  887. 'step' => 'any'
  888. )),
  889. '/div'
  890. );
  891. $this->assertHtml($expected, $result);
  892. $result = $this->Form->input('foo', array('step' => 0.5));
  893. $expected = array(
  894. 'div' => array('class' => 'input number'),
  895. 'label' => array('for' => 'foo'),
  896. 'Foo',
  897. '/label',
  898. array('input' => array(
  899. 'type' => 'number',
  900. 'name' => 'foo',
  901. 'id' => 'foo',
  902. 'step' => '0.5'
  903. )),
  904. '/div'
  905. );
  906. $this->assertHtml($expected, $result);
  907. }
  908. /**
  909. * Tests correct generation of number fields for integer fields
  910. *
  911. * @return void
  912. */
  913. public function testTextFieldTypeNumberGenerationForIntegers() {
  914. TableRegistry::get('Contacts', [
  915. 'className' => __NAMESPACE__ . '\ContactsTable'
  916. ]);
  917. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  918. $result = $this->Form->input('age');
  919. $expected = array(
  920. 'div' => array('class' => 'input number'),
  921. 'label' => array('for' => 'age'),
  922. 'Age',
  923. '/label',
  924. array('input' => array(
  925. 'type' => 'number', 'name' => 'age',
  926. 'id' => 'age'
  927. )),
  928. '/div'
  929. );
  930. $this->assertHtml($expected, $result);
  931. }
  932. /**
  933. * Tests correct generation of file upload fields for binary fields
  934. *
  935. * @return void
  936. */
  937. public function testFileUploadFieldTypeGenerationForBinaries() {
  938. $table = TableRegistry::get('Contacts', [
  939. 'className' => __NAMESPACE__ . '\ContactsTable'
  940. ]);
  941. $table->schema(array('foo' => array(
  942. 'type' => 'binary',
  943. 'null' => false,
  944. 'default' => null,
  945. 'length' => 1024
  946. )));
  947. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  948. $result = $this->Form->input('foo');
  949. $expected = array(
  950. 'div' => array('class' => 'input file'),
  951. 'label' => array('for' => 'foo'),
  952. 'Foo',
  953. '/label',
  954. array('input' => array(
  955. 'type' => 'file', 'name' => 'foo',
  956. 'id' => 'foo'
  957. )),
  958. '/div'
  959. );
  960. $this->assertHtml($expected, $result);
  961. }
  962. /**
  963. * testFormSecurityMultipleFields method
  964. *
  965. * Test secure() with multiple row form. Ensure hash is correct.
  966. *
  967. * @return void
  968. */
  969. public function testFormSecurityMultipleFields() {
  970. $this->Form->request->params['_Token'] = 'foo';
  971. $fields = array(
  972. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  973. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  974. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  975. );
  976. $result = $this->Form->secure($fields);
  977. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  978. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  979. $expected = array(
  980. 'div' => array('style' => 'display:none;'),
  981. array('input' => array(
  982. 'type' => 'hidden', 'name' => '_Token[fields]',
  983. 'value' => $hash
  984. )),
  985. array('input' => array(
  986. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  987. 'value' => ''
  988. )),
  989. '/div'
  990. );
  991. $this->assertHtml($expected, $result);
  992. }
  993. /**
  994. * testFormSecurityMultipleSubmitButtons
  995. *
  996. * test form submit generation and ensure that _Token is only created on end()
  997. *
  998. * @return void
  999. */
  1000. public function testFormSecurityMultipleSubmitButtons() {
  1001. $this->Form->request->params['_Token'] = 'testKey';
  1002. $this->Form->create($this->article);
  1003. $this->Form->text('Address.title');
  1004. $this->Form->text('Address.first_name');
  1005. $result = $this->Form->submit('Save', array('name' => 'save'));
  1006. $expected = array(
  1007. 'div' => array('class' => 'submit'),
  1008. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1009. '/div',
  1010. );
  1011. $this->assertHtml($expected, $result);
  1012. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1013. $expected = array(
  1014. 'div' => array('class' => 'submit'),
  1015. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1016. '/div',
  1017. );
  1018. $this->assertHtml($expected, $result);
  1019. $result = $this->Form->end();
  1020. $expected = array(
  1021. 'div' => array('style' => 'display:none;'),
  1022. array('input' => array(
  1023. 'type' => 'hidden',
  1024. 'name' => '_Token[fields]',
  1025. 'value'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden',
  1029. 'name' => '_Token[unlocked]',
  1030. 'value' => 'cancel%7Csave'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertHtml($expected, $result);
  1035. }
  1036. /**
  1037. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1038. *
  1039. * @return void
  1040. */
  1041. public function testSecurityButtonNestedNamed() {
  1042. $key = 'testKey';
  1043. $this->Form->request->params['_csrfToken'] = $key;
  1044. $this->Form->create('Addresses');
  1045. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1046. $result = $this->Form->unlockField();
  1047. $this->assertEquals(array('Address.button'), $result);
  1048. }
  1049. /**
  1050. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1051. *
  1052. * @return void
  1053. */
  1054. public function testSecuritySubmitNestedNamed() {
  1055. $this->Form->request->params['_Token'] = 'testKey';
  1056. $this->Form->create($this->article);
  1057. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1058. $result = $this->Form->unlockField();
  1059. $this->assertEquals(array('Address.button'), $result);
  1060. }
  1061. /**
  1062. * Test that the correct fields are unlocked for image submits with no names.
  1063. *
  1064. * @return void
  1065. */
  1066. public function testSecuritySubmitImageNoName() {
  1067. $key = 'testKey';
  1068. $this->Form->request->params['_Token'] = 'testKey';
  1069. $this->Form->create(false);
  1070. $result = $this->Form->submit('save.png');
  1071. $expected = array(
  1072. 'div' => array('class' => 'submit'),
  1073. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1074. '/div'
  1075. );
  1076. $this->assertHtml($expected, $result);
  1077. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1078. }
  1079. /**
  1080. * Test that the correct fields are unlocked for image submits with names.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testSecuritySubmitImageName() {
  1085. $this->Form->request->params['_Token'] = 'testKey';
  1086. $this->Form->create(null);
  1087. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1088. $expected = array(
  1089. 'div' => array('class' => 'submit'),
  1090. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1091. '/div'
  1092. );
  1093. $this->assertHtml($expected, $result);
  1094. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1095. }
  1096. /**
  1097. * testFormSecurityMultipleInputFields method
  1098. *
  1099. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1100. *
  1101. * @return void
  1102. */
  1103. public function testFormSecurityMultipleInputFields() {
  1104. $this->Form->request->params['_Token'] = 'testKey';
  1105. $this->Form->create();
  1106. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1107. $this->Form->input('Addresses.0.title');
  1108. $this->Form->input('Addresses.0.first_name');
  1109. $this->Form->input('Addresses.0.last_name');
  1110. $this->Form->input('Addresses.0.address');
  1111. $this->Form->input('Addresses.0.city');
  1112. $this->Form->input('Addresses.0.phone');
  1113. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1114. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1115. $this->Form->input('Addresses.1.title');
  1116. $this->Form->input('Addresses.1.first_name');
  1117. $this->Form->input('Addresses.1.last_name');
  1118. $this->Form->input('Addresses.1.address');
  1119. $this->Form->input('Addresses.1.city');
  1120. $this->Form->input('Addresses.1.phone');
  1121. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1122. $result = $this->Form->secure($this->Form->fields);
  1123. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1124. $expected = array(
  1125. 'div' => array('style' => 'display:none;'),
  1126. array('input' => array(
  1127. 'type' => 'hidden', 'name' => '_Token[fields]',
  1128. 'value' => $hash
  1129. )),
  1130. array('input' => array(
  1131. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1132. 'value' => ''
  1133. )),
  1134. '/div'
  1135. );
  1136. $this->assertHtml($expected, $result);
  1137. }
  1138. /**
  1139. * Test form security with Model.field.0 style inputs
  1140. *
  1141. * @return void
  1142. */
  1143. public function testFormSecurityArrayFields() {
  1144. $this->Form->request->params['_Token'] = 'testKey';
  1145. $this->Form->create();
  1146. $this->Form->text('Address.primary.1');
  1147. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1148. $this->Form->text('Address.secondary.1.0');
  1149. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1150. }
  1151. /**
  1152. * testFormSecurityMultipleInputDisabledFields method
  1153. *
  1154. * test secure form generation with multiple records and disabled fields.
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFormSecurityMultipleInputDisabledFields() {
  1159. $this->Form->request->params['_Token'] = array(
  1160. 'unlockedFields' => array('first_name', 'address')
  1161. );
  1162. $this->Form->create();
  1163. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1164. $this->Form->text('Addresses.0.title');
  1165. $this->Form->text('Addresses.0.first_name');
  1166. $this->Form->text('Addresses.0.last_name');
  1167. $this->Form->text('Addresses.0.address');
  1168. $this->Form->text('Addresses.0.city');
  1169. $this->Form->text('Addresses.0.phone');
  1170. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1171. $this->Form->text('Addresses.1.title');
  1172. $this->Form->text('Addresses.1.first_name');
  1173. $this->Form->text('Addresses.1.last_name');
  1174. $this->Form->text('Addresses.1.address');
  1175. $this->Form->text('Addresses.1.city');
  1176. $this->Form->text('Addresses.1.phone');
  1177. $result = $this->Form->secure($this->Form->fields);
  1178. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1179. $expected = array(
  1180. 'div' => array('style' => 'display:none;'),
  1181. array('input' => array(
  1182. 'type' => 'hidden',
  1183. 'name' => '_Token[fields]',
  1184. 'value' => $hash
  1185. )),
  1186. array('input' => array(
  1187. 'type' => 'hidden',
  1188. 'name' => '_Token[unlocked]',
  1189. 'value' => 'address%7Cfirst_name',
  1190. )),
  1191. '/div'
  1192. );
  1193. $this->assertHtml($expected, $result);
  1194. }
  1195. /**
  1196. * testFormSecurityInputDisabledFields method
  1197. *
  1198. * Test single record form with disabled fields.
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecurityInputUnlockedFields() {
  1203. $this->Form->request['_Token'] = array(
  1204. 'unlockedFields' => array('first_name', 'address')
  1205. );
  1206. $this->Form->create();
  1207. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1208. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1209. $this->Form->text('Addresses.title');
  1210. $this->Form->text('Addresses.first_name');
  1211. $this->Form->text('Addresses.last_name');
  1212. $this->Form->text('Addresses.address');
  1213. $this->Form->text('Addresses.city');
  1214. $this->Form->text('Addresses.phone');
  1215. $result = $this->Form->fields;
  1216. $expected = array(
  1217. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1218. 'Addresses.city', 'Addresses.phone'
  1219. );
  1220. $this->assertEquals($expected, $result);
  1221. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1222. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1223. $expected = array(
  1224. 'div' => array('style' => 'display:none;'),
  1225. array('input' => array(
  1226. 'type' => 'hidden',
  1227. 'name' => '_Token[fields]',
  1228. 'value' => $hash,
  1229. 'data-foo' => 'bar',
  1230. )),
  1231. array('input' => array(
  1232. 'type' => 'hidden',
  1233. 'name' => '_Token[unlocked]',
  1234. 'value' => 'address%7Cfirst_name',
  1235. 'data-foo' => 'bar',
  1236. )),
  1237. '/div'
  1238. );
  1239. $this->assertHtml($expected, $result);
  1240. }
  1241. /**
  1242. * test securing inputs with custom name attributes.
  1243. *
  1244. * @return void
  1245. */
  1246. public function testFormSecureWithCustomNameAttribute() {
  1247. $this->Form->request->params['_Token'] = 'testKey';
  1248. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1249. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1250. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1251. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1252. }
  1253. /**
  1254. * testFormSecuredInput method
  1255. *
  1256. * Test generation of entire secure form, assertions made on input() output.
  1257. *
  1258. * @return void
  1259. */
  1260. public function testFormSecuredInput() {
  1261. $this->Form->request->params['_csrfToken'] = 'testKey';
  1262. $this->Form->request->params['_Token'] = 'stuff';
  1263. $this->article['schema'] = [
  1264. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1265. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1266. ];
  1267. $result = $this->Form->create($this->article, array('url' => '/articles/add'));
  1268. $encoding = strtolower(Configure::read('App.encoding'));
  1269. $expected = array(
  1270. 'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
  1271. 'div' => array('style' => 'display:none;'),
  1272. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1273. array('input' => array(
  1274. 'type' => 'hidden',
  1275. 'name' => '_csrfToken',
  1276. 'value' => 'testKey'
  1277. )),
  1278. '/div'
  1279. );
  1280. $this->assertHtml($expected, $result);
  1281. $result = $this->Form->input('ratio');
  1282. $expected = array(
  1283. 'div' => array('class'),
  1284. 'label' => array('for'),
  1285. 'Ratio',
  1286. '/label',
  1287. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1288. '/div',
  1289. );
  1290. $this->assertHtml($expected, $result);
  1291. $result = $this->Form->input('population');
  1292. $expected = array(
  1293. 'div' => array('class'),
  1294. 'label' => array('for'),
  1295. 'Population',
  1296. '/label',
  1297. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1298. '/div',
  1299. );
  1300. $this->assertHtml($expected, $result);
  1301. $result = $this->Form->input('published', array('type' => 'text'));
  1302. $expected = array(
  1303. 'div' => array('class' => 'input text'),
  1304. 'label' => array('for' => 'published'),
  1305. 'Published',
  1306. '/label',
  1307. array('input' => array(
  1308. 'type' => 'text',
  1309. 'name' => 'published',
  1310. 'id' => 'published'
  1311. )),
  1312. '/div'
  1313. );
  1314. $this->assertHtml($expected, $result);
  1315. $result = $this->Form->input('other', array('type' => 'text'));
  1316. $expected = array(
  1317. 'div' => array('class' => 'input text'),
  1318. 'label' => array('for' => 'other'),
  1319. 'Other',
  1320. '/label',
  1321. array('input' => array(
  1322. 'type' => 'text',
  1323. 'name' => 'other',
  1324. 'id',
  1325. )),
  1326. '/div'
  1327. );
  1328. $this->assertHtml($expected, $result);
  1329. $result = $this->Form->hidden('stuff');
  1330. $expected = array(
  1331. 'input' => array(
  1332. 'type' => 'hidden',
  1333. 'name' => 'stuff',
  1334. ));
  1335. $this->assertHtml($expected, $result);
  1336. $result = $this->Form->hidden('hidden', array('value' => '0'));
  1337. $expected = array('input' => array(
  1338. 'type' => 'hidden',
  1339. 'name' => 'hidden',
  1340. 'value' => '0'
  1341. ));
  1342. $this->assertHtml($expected, $result);
  1343. $result = $this->Form->input('something', array('type' => 'checkbox'));
  1344. $expected = array(
  1345. 'div' => array('class' => 'input checkbox'),
  1346. array('input' => array(
  1347. 'type' => 'hidden',
  1348. 'name' => 'something',
  1349. 'value' => '0'
  1350. )),
  1351. 'label' => array('for' => 'something'),
  1352. array('input' => array(
  1353. 'type' => 'checkbox',
  1354. 'name' => 'something',
  1355. 'value' => '1',
  1356. 'id' => 'something'
  1357. )),
  1358. 'Something',
  1359. '/label',
  1360. '/div'
  1361. );
  1362. $this->assertHtml($expected, $result);
  1363. $result = $this->Form->fields;
  1364. $expected = array(
  1365. 'ratio', 'population', 'published', 'other',
  1366. 'stuff' => '',
  1367. 'hidden' => '0',
  1368. 'something'
  1369. );
  1370. $this->assertEquals($expected, $result);
  1371. $result = $this->Form->secure($this->Form->fields);
  1372. $expected = array(
  1373. 'div' => array('style' => 'display:none;'),
  1374. array('input' => array(
  1375. 'type' => 'hidden',
  1376. 'name' => '_Token[fields]',
  1377. 'value'
  1378. )),
  1379. array('input' => array(
  1380. 'type' => 'hidden',
  1381. 'name' => '_Token[unlocked]',
  1382. 'value' => ''
  1383. )),
  1384. '/div'
  1385. );
  1386. $this->assertHtml($expected, $result);
  1387. }
  1388. /**
  1389. * Test secured inputs with custom names.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testSecuredInputCustomName() {
  1394. $this->Form->request->params['_Token'] = 'testKey';
  1395. $this->assertEquals(array(), $this->Form->fields);
  1396. $this->Form->text('text_input', array(
  1397. 'name' => 'Option[General.default_role]',
  1398. ));
  1399. $expected = array('Option.General.default_role');
  1400. $this->assertEquals($expected, $this->Form->fields);
  1401. $this->Form->select('select_box', [1, 2], [
  1402. 'name' => 'Option[General.select_role]',
  1403. ]);
  1404. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1405. $this->assertEquals($expected, $this->Form->fields);
  1406. }
  1407. /**
  1408. * Tests that the correct keys are added to the field hash index
  1409. *
  1410. * @return void
  1411. */
  1412. public function testFormSecuredFileInput() {
  1413. $this->assertEquals(array(), $this->Form->fields);
  1414. $this->Form->file('Attachment.file');
  1415. $expected = array(
  1416. 'Attachment.file.name', 'Attachment.file.type',
  1417. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1418. 'Attachment.file.size'
  1419. );
  1420. $this->assertEquals($expected, $this->Form->fields);
  1421. }
  1422. /**
  1423. * test that multiple selects keys are added to field hash
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormSecuredMultipleSelect() {
  1428. $this->Form->request->params['_csrfToken'] = 'testKey';
  1429. $this->assertEquals(array(), $this->Form->fields);
  1430. $options = array('1' => 'one', '2' => 'two');
  1431. $this->Form->select('Model.select', $options);
  1432. $expected = array('Model.select');
  1433. $this->assertEquals($expected, $this->Form->fields);
  1434. $this->Form->fields = array();
  1435. $this->Form->select('Model.select', $options, array('multiple' => true));
  1436. $this->assertEquals($expected, $this->Form->fields);
  1437. }
  1438. /**
  1439. * testFormSecuredRadio method
  1440. *
  1441. * @return void
  1442. */
  1443. public function testFormSecuredRadio() {
  1444. $this->Form->request->params['_Token'] = 'testKey';
  1445. $this->assertEquals(array(), $this->Form->fields);
  1446. $options = array('1' => 'option1', '2' => 'option2');
  1447. $this->Form->radio('Test.test', $options);
  1448. $expected = array('Test.test');
  1449. $this->assertEquals($expected, $this->Form->fields);
  1450. }
  1451. /**
  1452. * Test that when disabled is in a list based attribute array it works.
  1453. *
  1454. * @return void
  1455. */
  1456. public function testFormSecuredAndDisabledNotAssoc() {
  1457. $this->Form->request->params['_Token'] = 'testKey';
  1458. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1459. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1460. $this->Form->text('Model.text', array('disabled'));
  1461. $this->Form->textarea('Model.textarea', array('disabled'));
  1462. $this->Form->password('Model.password', array('disabled'));
  1463. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1464. $expected = array(
  1465. 'Model.radio' => ''
  1466. );
  1467. $this->assertEquals($expected, $this->Form->fields);
  1468. }
  1469. /**
  1470. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1471. * hashing.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testFormSecuredAndDisabled() {
  1476. $this->Form->request->params['_Token'] = 'testKey';
  1477. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1478. $this->Form->text('Model.text', array('disabled' => true));
  1479. $this->Form->password('Model.text', array('disabled' => true));
  1480. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1481. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1482. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1483. $this->Form->year('Model.year', array('disabled' => true));
  1484. $this->Form->month('Model.month', array('disabled' => true));
  1485. $this->Form->day('Model.day', array('disabled' => true));
  1486. $this->Form->hour('Model.hour', array('disabled' => true));
  1487. $this->Form->minute('Model.minute', array('disabled' => true));
  1488. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1489. $expected = array(
  1490. 'Model.radio' => ''
  1491. );
  1492. $this->assertEquals($expected, $this->Form->fields);
  1493. }
  1494. /**
  1495. * testDisableSecurityUsingForm method
  1496. *
  1497. * @return void
  1498. */
  1499. public function testDisableSecurityUsingForm() {
  1500. $this->Form->request['_Token'] = [
  1501. 'disabledFields' => []
  1502. ];
  1503. $this->Form->create();
  1504. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1505. $this->Form->text('Addresses.title');
  1506. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1507. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1508. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1509. $result = $this->Form->fields;
  1510. $expected = [
  1511. 'Addresses.id' => '123456', 'Addresses.title',
  1512. ];
  1513. $this->assertEquals($expected, $result);
  1514. }
  1515. /**
  1516. * test disableField
  1517. *
  1518. * @return void
  1519. */
  1520. public function testUnlockFieldAddsToList() {
  1521. $this->Form->request['_Token'] = array(
  1522. 'unlockedFields' => array()
  1523. );
  1524. $this->Form->unlockField('Contact.name');
  1525. $this->Form->text('Contact.name');
  1526. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1527. $this->assertEquals(array(), $this->Form->fields);
  1528. }
  1529. /**
  1530. * test unlockField removing from fields array.
  1531. *
  1532. * @return void
  1533. */
  1534. public function testUnlockFieldRemovingFromFields() {
  1535. $this->Form->request['_Token'] = array(
  1536. 'unlockedFields' => array()
  1537. );
  1538. $this->Form->create($this->article);
  1539. $this->Form->hidden('Article.id', array('value' => 1));
  1540. $this->Form->text('Article.title');
  1541. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1542. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1543. $this->Form->unlockField('Article.title');
  1544. $this->Form->unlockField('Article.id');
  1545. $this->assertEquals(array(), $this->Form->fields);
  1546. }
  1547. /**
  1548. * Test that only the path + query elements of a form's URL show up in their hash.
  1549. *
  1550. * @return void
  1551. */
  1552. public function testSecuredFormUrlIgnoresHost() {
  1553. $this->Form->request['_Token'] = array('key' => 'testKey');
  1554. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1555. $this->Form->create($this->article, array(
  1556. 'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
  1557. ));
  1558. $result = $this->Form->secure();
  1559. $this->assertContains($expected, $result);
  1560. $this->Form->create($this->article, array('url' => 'http://localhost/articles/view/1?page=1'));
  1561. $result = $this->Form->secure();
  1562. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1563. $this->Form->create($this->article, array('url' => '/articles/view/1?page=1'));
  1564. $result = $this->Form->secure();
  1565. $this->assertContains($expected, $result, 'URL path + query should work.');
  1566. $this->Form->create($this->article, array('url' => '/articles/view/1'));
  1567. $result = $this->Form->secure();
  1568. $this->assertNotContains($expected, $result, 'URL is different');
  1569. }
  1570. /**
  1571. * Test that URL, HTML and identifer show up in their hashs.
  1572. *
  1573. * @return void
  1574. */
  1575. public function testSecuredFormUrlHasHtmlAndIdentifer() {
  1576. $this->Form->request['_Token'] = array('key' => 'testKey');
  1577. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  1578. $res = $this->Form->create($this->article, array(
  1579. 'url' => array(
  1580. 'controller' => 'articles',
  1581. 'action' => 'view',
  1582. '?' => array(
  1583. 'page' => 1,
  1584. 'limit' => 10,
  1585. 'html' => '<>"',
  1586. ),
  1587. '#' => 'result',
  1588. ),
  1589. ));
  1590. $result = $this->Form->secure();
  1591. $this->assertContains($expected, $result);
  1592. $this->Form->create($this->article, array(
  1593. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1594. ));
  1595. $result = $this->Form->secure();
  1596. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1597. $this->Form->create($this->article, array(
  1598. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1599. ));
  1600. $result = $this->Form->secure();
  1601. $this->assertContains($expected, $result, 'URL path + query should work.');
  1602. }
  1603. /**
  1604. * test error message display
  1605. *
  1606. * @return void
  1607. */
  1608. public function testErrorMessageDisplay() {
  1609. $this->article['errors'] = [
  1610. 'Article' => ['title' => 'error message']
  1611. ];
  1612. $this->Form->create($this->article);
  1613. $result = $this->Form->input('Article.title');
  1614. $expected = [
  1615. 'div' => ['class' => 'input text error'],
  1616. 'label' => ['for' => 'article-title'],
  1617. 'Title',
  1618. '/label',
  1619. 'input' => [
  1620. 'type' => 'text', 'name' => 'Article[title]',
  1621. 'id' => 'article-title', 'class' => 'form-error'
  1622. ],
  1623. ['div' => ['class' => 'error-message']],
  1624. 'error message',
  1625. '/div',
  1626. '/div'
  1627. ];
  1628. $this->assertHtml($expected, $result);
  1629. $result = $this->Form->input('Article.title', [
  1630. 'templates' => [
  1631. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1632. ]
  1633. ]);
  1634. $expected = [
  1635. 'div' => ['class' => 'input text error'],
  1636. 'label' => ['for' => 'article-title'],
  1637. 'Title',
  1638. '/label',
  1639. 'input' => [
  1640. 'type' => 'text', 'name' => 'Article[title]',
  1641. 'id' => 'article-title', 'class' => 'form-error'
  1642. ],
  1643. '/div'
  1644. ];
  1645. $this->assertHtml($expected, $result);
  1646. }
  1647. /**
  1648. * Test validation errors, when validation message is an empty string.
  1649. *
  1650. * @return void
  1651. */
  1652. public function testEmptyErrorValidation() {
  1653. $this->article['errors'] = [
  1654. 'Article' => ['title' => '']
  1655. ];
  1656. $this->Form->create($this->article);
  1657. $result = $this->Form->input('Article.title');
  1658. $expected = [
  1659. 'div' => ['class' => 'input text error'],
  1660. 'label' => ['for' => 'article-title'],
  1661. 'Title',
  1662. '/label',
  1663. 'input' => [
  1664. 'type' => 'text', 'name' => 'Article[title]',
  1665. 'id' => 'article-title', 'class' => 'form-error'
  1666. ],
  1667. ['div' => ['class' => 'error-message']],
  1668. [],
  1669. '/div',
  1670. '/div'
  1671. ];
  1672. $this->assertHtml($expected, $result);
  1673. }
  1674. /**
  1675. * Test validation errors, when calling input() overriding validation message by an empty string.
  1676. *
  1677. * @return void
  1678. */
  1679. public function testEmptyInputErrorValidation() {
  1680. $this->article['errors'] = [
  1681. 'Article' => ['title' => 'error message']
  1682. ];
  1683. $this->Form->create($this->article);
  1684. $result = $this->Form->input('Article.title', array('error' => ''));
  1685. $expected = [
  1686. 'div' => ['class' => 'input text error'],
  1687. 'label' => ['for' => 'article-title'],
  1688. 'Title',
  1689. '/label',
  1690. 'input' => [
  1691. 'type' => 'text', 'name' => 'Article[title]',
  1692. 'id' => 'article-title', 'class' => 'form-error'
  1693. ],
  1694. ['div' => ['class' => 'error-message']],
  1695. [],
  1696. '/div',
  1697. '/div'
  1698. ];
  1699. $this->assertHtml($expected, $result);
  1700. }
  1701. /**
  1702. * Tests displaying errors for nested entities
  1703. *
  1704. * @return void
  1705. */
  1706. public function testFormValidationAssociated() {
  1707. $nested = new Entity(['foo' => 'bar']);
  1708. $nested->errors('foo', ['not a valid bar']);
  1709. $entity = new Entity(['nested' => $nested]);
  1710. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1711. $result = $this->Form->error('nested.foo');
  1712. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1713. }
  1714. /**
  1715. * testFormValidationAssociatedSecondLevel method
  1716. *
  1717. * test form error display with associated model.
  1718. *
  1719. * @return void
  1720. */
  1721. public function testFormValidationAssociatedSecondLevel() {
  1722. $inner = new Entity(['bar' => 'baz']);
  1723. $nested = new Entity(['foo' => $inner]);
  1724. $entity = new Entity(['nested' => $nested]);
  1725. $inner->errors('bar', ['not a valid one']);
  1726. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1727. $result = $this->Form->error('nested.foo.bar');
  1728. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1729. }
  1730. /**
  1731. * testFormValidationMultiRecord method
  1732. *
  1733. * test form error display with multiple records.
  1734. *
  1735. * @return void
  1736. */
  1737. public function testFormValidationMultiRecord() {
  1738. $one = new Entity;
  1739. $two = new Entity;
  1740. TableRegistry::get('Contacts', [
  1741. 'className' => __NAMESPACE__ . '\ContactsTable'
  1742. ]);
  1743. $one->set('email', '');
  1744. $one->errors('email', ['invalid email']);
  1745. $two->set('name', '');
  1746. $two->errors('name', ['This is wrong']);
  1747. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1748. $result = $this->Form->input('0.email');
  1749. $expected = array(
  1750. 'div' => array('class' => 'input email error'),
  1751. 'label' => array('for' => '0-email'),
  1752. 'Email',
  1753. '/label',
  1754. 'input' => array(
  1755. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  1756. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  1757. ),
  1758. array('div' => array('class' => 'error-message')),
  1759. 'invalid email',
  1760. '/div',
  1761. '/div'
  1762. );
  1763. $this->assertHtml($expected, $result);
  1764. $result = $this->Form->input('1.name');
  1765. $expected = array(
  1766. 'div' => array('class' => 'input text error'),
  1767. 'label' => array('for' => '1-name'),
  1768. 'Name',
  1769. '/label',
  1770. 'input' => array(
  1771. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  1772. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  1773. ),
  1774. array('div' => array('class' => 'error-message')),
  1775. 'This is wrong',
  1776. '/div',
  1777. '/div'
  1778. );
  1779. $this->assertHtml($expected, $result);
  1780. }
  1781. /**
  1782. * testInput method
  1783. *
  1784. * Test various incarnations of input().
  1785. *
  1786. * @return void
  1787. */
  1788. public function testInput() {
  1789. TableRegistry::get('ValidateUsers', [
  1790. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1791. ]);
  1792. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1793. $result = $this->Form->input('ValidateUsers.balance');
  1794. $expected = array(
  1795. 'div' => array('class'),
  1796. 'label' => array('for'),
  1797. 'Balance',
  1798. '/label',
  1799. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1800. '/div',
  1801. );
  1802. $this->assertHtml($expected, $result);
  1803. $result = $this->Form->input('ValidateUser.cost_decimal');
  1804. $expected = array(
  1805. 'div' => array('class'),
  1806. 'label' => array('for'),
  1807. 'Cost Decimal',
  1808. '/label',
  1809. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1810. '/div',
  1811. );
  1812. $this->assertHtml($expected, $result);
  1813. }
  1814. /**
  1815. * Tests the input method and passing custom options
  1816. *
  1817. * @return void
  1818. */
  1819. public function testInputCustomization() {
  1820. TableRegistry::get('Contacts', [
  1821. 'className' => __NAMESPACE__ . '\ContactsTable'
  1822. ]);
  1823. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1824. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1825. $expected = array(
  1826. 'div' => array('class' => 'input email'),
  1827. 'label' => array('for' => 'custom'),
  1828. 'Email',
  1829. '/label',
  1830. array('input' => array(
  1831. 'type' => 'email', 'name' => 'Contact[email]',
  1832. 'id' => 'custom', 'maxlength' => 255
  1833. )),
  1834. '/div'
  1835. );
  1836. $this->assertHtml($expected, $result);
  1837. $result = $this->Form->input('Contact.email', array(
  1838. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  1839. ));
  1840. $expected = array(
  1841. '<div',
  1842. 'label' => array('for' => 'contact-email'),
  1843. 'Email',
  1844. '/label',
  1845. array('input' => array(
  1846. 'type' => 'email', 'name' => 'Contact[email]',
  1847. 'id' => 'contact-email', 'maxlength' => 255
  1848. )),
  1849. '/div'
  1850. );
  1851. $this->assertHtml($expected, $result);
  1852. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1853. $expected = array(
  1854. 'div' => array('class' => 'input text'),
  1855. 'label' => array('for' => 'contact-email'),
  1856. 'Email',
  1857. '/label',
  1858. array('input' => array(
  1859. 'type' => 'text', 'name' => 'Contact[email]',
  1860. 'id' => 'contact-email', 'maxlength' => '255'
  1861. )),
  1862. '/div'
  1863. );
  1864. $this->assertHtml($expected, $result);
  1865. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text'),
  1868. 'label' => array('for' => 'contact-5-email'),
  1869. 'Email',
  1870. '/label',
  1871. array('input' => array(
  1872. 'type' => 'text', 'name' => 'Contact[5][email]',
  1873. 'id' => 'contact-5-email', 'maxlength' => '255'
  1874. )),
  1875. '/div'
  1876. );
  1877. $this->assertHtml($expected, $result);
  1878. $result = $this->Form->input('Contact.password');
  1879. $expected = array(
  1880. 'div' => array('class' => 'input password'),
  1881. 'label' => array('for' => 'contact-password'),
  1882. 'Password',
  1883. '/label',
  1884. array('input' => array(
  1885. 'type' => 'password', 'name' => 'Contact[password]',
  1886. 'id' => 'contact-password'
  1887. )),
  1888. '/div'
  1889. );
  1890. $this->assertHtml($expected, $result);
  1891. $result = $this->Form->input('Contact.email', array(
  1892. 'type' => 'file', 'class' => 'textbox'
  1893. ));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input file'),
  1896. 'label' => array('for' => 'contact-email'),
  1897. 'Email',
  1898. '/label',
  1899. array('input' => array(
  1900. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1901. 'id' => 'contact-email'
  1902. )),
  1903. '/div'
  1904. );
  1905. $this->assertHtml($expected, $result);
  1906. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1907. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1908. $result = $this->Form->input('phone');
  1909. $expected = array(
  1910. 'div' => array('class' => 'input tel'),
  1911. 'label' => array('for' => 'phone'),
  1912. 'Phone',
  1913. '/label',
  1914. array('input' => array(
  1915. 'type' => 'tel', 'name' => 'phone',
  1916. 'value' => 'Hello &amp; World &gt; weird chars',
  1917. 'id' => 'phone', 'maxlength' => 255
  1918. )),
  1919. '/div'
  1920. );
  1921. $this->assertHtml($expected, $result);
  1922. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1923. $this->Form->create();
  1924. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1925. $expected = array(
  1926. 'div' => array('class' => 'input text'),
  1927. 'label' => array('for' => 'myId'),
  1928. 'Field',
  1929. '/label',
  1930. 'input' => array(
  1931. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1932. 'value' => 'My value', 'id' => 'myId'
  1933. ),
  1934. '/div'
  1935. );
  1936. $this->assertHtml($expected, $result);
  1937. $this->Form->request->data = [];
  1938. $entity->errors('field', 'Badness!');
  1939. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1940. $result = $this->Form->input('field');
  1941. $expected = array(
  1942. 'div' => array('class' => 'input text error'),
  1943. 'label' => array('for' => 'field'),
  1944. 'Field',
  1945. '/label',
  1946. 'input' => array(
  1947. 'type' => 'text', 'name' => 'field',
  1948. 'id' => 'field', 'class' => 'form-error'
  1949. ),
  1950. array('div' => array('class' => 'error-message')),
  1951. 'Badness!',
  1952. '/div',
  1953. '/div'
  1954. );
  1955. $this->assertHtml($expected, $result);
  1956. $result = $this->Form->input('field', array(
  1957. 'templates' => [
  1958. 'inputContainerError' => '{{content}}{{error}}',
  1959. 'error' => '<span class="error-message">{{content}}</span>'
  1960. ]
  1961. ));
  1962. $expected = array(
  1963. 'label' => array('for' => 'field'),
  1964. 'Field',
  1965. '/label',
  1966. 'input' => array(
  1967. 'type' => 'text', 'name' => 'field',
  1968. 'id' => 'field', 'class' => 'form-error'
  1969. ),
  1970. array('span' => array('class' => 'error-message')),
  1971. 'Badness!',
  1972. '/span'
  1973. );
  1974. $this->assertHtml($expected, $result);
  1975. $entity->errors('field', ['minLength']);
  1976. $result = $this->Form->input('field', array(
  1977. 'error' => array(
  1978. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1979. 'maxLength' => 'login too large'
  1980. )
  1981. ));
  1982. $expected = array(
  1983. 'div' => array('class' => 'input text error'),
  1984. 'label' => array('for' => 'field'),
  1985. 'Field',
  1986. '/label',
  1987. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  1988. array('div' => array('class' => 'error-message')),
  1989. 'Le login doit contenir au moins 2 caractères',
  1990. '/div',
  1991. '/div'
  1992. );
  1993. $this->assertHtml($expected, $result);
  1994. $entity->errors('field', ['maxLength']);
  1995. $result = $this->Form->input('field', array(
  1996. 'error' => array(
  1997. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1998. 'maxLength' => 'login too large',
  1999. )
  2000. ));
  2001. $expected = array(
  2002. 'div' => array('class' => 'input text error'),
  2003. 'label' => array('for' => 'field'),
  2004. 'Field',
  2005. '/label',
  2006. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  2007. array('div' => array('class' => 'error-message')),
  2008. 'login too large',
  2009. '/div',
  2010. '/div'
  2011. );
  2012. $this->assertHtml($expected, $result);
  2013. }
  2014. /**
  2015. * Test that input() accepts a template file.
  2016. *
  2017. * @return void
  2018. */
  2019. public function testInputWithTemplateFile() {
  2020. $result = $this->Form->input('field', array(
  2021. 'templates' => 'htmlhelper_tags'
  2022. ));
  2023. $expected = array(
  2024. 'label' => array('for' => 'field'),
  2025. 'Field',
  2026. '/label',
  2027. 'input' => array(
  2028. 'type' => 'text', 'name' => 'field',
  2029. 'id' => 'field'
  2030. ),
  2031. );
  2032. $this->assertHtml($expected, $result);
  2033. }
  2034. /**
  2035. * Test id prefix
  2036. *
  2037. * @return void
  2038. */
  2039. public function testCreateIdPrefix() {
  2040. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2041. $result = $this->Form->input('field');
  2042. $expected = [
  2043. 'div' => ['class' => 'input text'],
  2044. 'label' => ['for' => 'prefix-field'],
  2045. 'Field',
  2046. '/label',
  2047. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2048. '/div'
  2049. ];
  2050. $this->assertHtml($expected, $result);
  2051. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2052. $expected = [
  2053. 'div' => ['class' => 'input text'],
  2054. 'label' => ['for' => 'custom-id'],
  2055. 'Field',
  2056. '/label',
  2057. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2058. '/div'
  2059. ];
  2060. $this->assertHtml($expected, $result);
  2061. $result = $this->Form->radio('Model.field', ['option A']);
  2062. $expected = [
  2063. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2064. 'label' => ['for' => 'prefix-model-field-0'],
  2065. ['input' => [
  2066. 'type' => 'radio',
  2067. 'name' => 'Model[field]',
  2068. 'value' => '0',
  2069. 'id' => 'prefix-model-field-0'
  2070. ]],
  2071. 'option A',
  2072. '/label'
  2073. ];
  2074. $this->assertHtml($expected, $result);
  2075. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2076. $expected = [
  2077. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2078. 'label' => ['for' => 'prefix-model-field-0'],
  2079. ['input' => [
  2080. 'type' => 'radio',
  2081. 'name' => 'Model[field]',
  2082. 'value' => '0',
  2083. 'id' => 'prefix-model-field-0'
  2084. ]],
  2085. 'option A',
  2086. '/label'
  2087. ];
  2088. $this->assertHtml($expected, $result);
  2089. $result = $this->Form->select(
  2090. 'Model.multi_field',
  2091. ['first'],
  2092. ['multiple' => 'checkbox']
  2093. );
  2094. $expected = [
  2095. 'input' => [
  2096. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2097. ],
  2098. ['div' => ['class' => 'checkbox']],
  2099. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2100. ['input' => [
  2101. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2102. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2103. ]],
  2104. 'first',
  2105. '/label',
  2106. '/div',
  2107. ];
  2108. $this->assertHtml($expected, $result);
  2109. $this->Form->end();
  2110. $result = $this->Form->input('field');
  2111. $expected = [
  2112. 'div' => ['class' => 'input text'],
  2113. 'label' => ['for' => 'field'],
  2114. 'Field',
  2115. '/label',
  2116. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2117. '/div'
  2118. ];
  2119. $this->assertHtml($expected, $result);
  2120. }
  2121. /**
  2122. * Test that inputs with 0 can be created.
  2123. *
  2124. * @return void
  2125. */
  2126. public function testInputZero() {
  2127. TableRegistry::get('Contacts', [
  2128. 'className' => __NAMESPACE__ . '\ContactsTable'
  2129. ]);
  2130. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2131. $result = $this->Form->input('0');
  2132. $expected = array(
  2133. 'div' => array('class' => 'input text'),
  2134. 'label' => array('for' => '0'), '/label',
  2135. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2136. '/div'
  2137. );
  2138. $this->assertHtml($expected, $result);
  2139. }
  2140. /**
  2141. * test input() with checkbox creation
  2142. *
  2143. * @return void
  2144. */
  2145. public function testInputCheckbox() {
  2146. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2147. $expected = array(
  2148. 'div' => array('class' => 'input checkbox'),
  2149. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2150. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2151. '/div'
  2152. );
  2153. $this->assertHtml($expected, $result);
  2154. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2155. $expected = array(
  2156. 'div' => array('class' => 'input checkbox'),
  2157. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2158. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2159. '/div'
  2160. );
  2161. $this->assertHtml($expected, $result);
  2162. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2163. $expected = array(
  2164. 'div' => array('class' => 'input checkbox'),
  2165. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2166. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2167. '/div'
  2168. );
  2169. $this->assertHtml($expected, $result);
  2170. $result = $this->Form->input('User.disabled', array(
  2171. 'label' => 'Disabled',
  2172. 'type' => 'checkbox',
  2173. 'data-foo' => 'disabled'
  2174. ));
  2175. $expected = array(
  2176. 'div' => array('class' => 'input checkbox'),
  2177. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2178. 'label' => array('for' => 'user-disabled'),
  2179. array('input' => array(
  2180. 'type' => 'checkbox',
  2181. 'name' => 'User[disabled]',
  2182. 'value' => '1',
  2183. 'id' => 'user-disabled',
  2184. 'data-foo' => 'disabled'
  2185. )),
  2186. 'Disabled',
  2187. '/label',
  2188. '/div'
  2189. );
  2190. $this->assertHtml($expected, $result);
  2191. }
  2192. /**
  2193. * Test that input() does not create wrapping div and label tag for hidden fields
  2194. *
  2195. * @return void
  2196. */
  2197. public function testInputHidden() {
  2198. TableRegistry::get('ValidateUsers', [
  2199. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2200. ]);
  2201. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2202. $result = $this->Form->input('ValidateUser.id');
  2203. $expected = array(
  2204. 'input' => array('name', 'type' => 'hidden', 'id')
  2205. );
  2206. $this->assertHtml($expected, $result);
  2207. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2208. $expected = array(
  2209. 'input' => array('name', 'type' => 'hidden', 'id')
  2210. );
  2211. $this->assertHtml($expected, $result);
  2212. }
  2213. /**
  2214. * test form->input() with datetime
  2215. *
  2216. * @return void
  2217. */
  2218. public function testInputDatetime() {
  2219. $this->Form = $this->getMock(
  2220. 'Cake\View\Helper\FormHelper',
  2221. ['datetime'],
  2222. [new View()]
  2223. );
  2224. $this->Form->expects($this->once())->method('datetime')
  2225. ->with('prueba', [
  2226. 'type' => 'datetime',
  2227. 'timeFormat' => 24,
  2228. 'minYear' => 2008,
  2229. 'maxYear' => 2011,
  2230. 'interval' => 15,
  2231. 'options' => null,
  2232. 'empty' => false,
  2233. 'id' => 'prueba',
  2234. 'required' => false,
  2235. ])
  2236. ->will($this->returnValue('This is it!'));
  2237. $result = $this->Form->input('prueba', array(
  2238. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2239. 'maxYear' => 2011, 'interval' => 15
  2240. ));
  2241. $expected = array(
  2242. 'div' => array('class' => 'input datetime'),
  2243. '<label',
  2244. 'Prueba',
  2245. '/label',
  2246. 'This is it!',
  2247. '/div'
  2248. );
  2249. $this->assertHtml($expected, $result);
  2250. }
  2251. /**
  2252. * test form->input() with datetime with id prefix
  2253. *
  2254. * @return void
  2255. */
  2256. public function testInputDatetimeIdPrefix() {
  2257. $this->Form = $this->getMock(
  2258. 'Cake\View\Helper\FormHelper',
  2259. ['datetime'],
  2260. [new View()]
  2261. );
  2262. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2263. $this->Form->expects($this->once())->method('datetime')
  2264. ->with('prueba', [
  2265. 'type' => 'datetime',
  2266. 'timeFormat' => 24,
  2267. 'minYear' => 2008,
  2268. 'maxYear' => 2011,
  2269. 'interval' => 15,
  2270. 'options' => null,
  2271. 'empty' => false,
  2272. 'id' => 'prefix-prueba',
  2273. 'required' => false,
  2274. ])
  2275. ->will($this->returnValue('This is it!'));
  2276. $result = $this->Form->input('prueba', array(
  2277. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2278. 'maxYear' => 2011, 'interval' => 15
  2279. ));
  2280. $expected = array(
  2281. 'div' => array('class' => 'input datetime'),
  2282. '<label',
  2283. 'Prueba',
  2284. '/label',
  2285. 'This is it!',
  2286. '/div'
  2287. );
  2288. $this->assertHtml($expected, $result);
  2289. }
  2290. /**
  2291. * Test generating checkboxes with disabled elements.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testInputCheckboxWithDisabledElements() {
  2296. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2297. $result = $this->Form->input('Contact.multiple', array(
  2298. 'multiple' => 'checkbox',
  2299. 'disabled' => 'disabled',
  2300. 'options' => $options
  2301. ));
  2302. $expected = array(
  2303. array('div' => array('class' => 'input select')),
  2304. array('label' => array('for' => "contact-multiple")),
  2305. 'Multiple',
  2306. '/label',
  2307. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '')),
  2308. array('div' => array('class' => 'checkbox')),
  2309. array('label' => array('for' => "contact-multiple-1")),
  2310. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  2311. 'One',
  2312. '/label',
  2313. '/div',
  2314. array('div' => array('class' => 'checkbox')),
  2315. array('label' => array('for' => "contact-multiple-2")),
  2316. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  2317. 'Two',
  2318. '/label',
  2319. '/div',
  2320. array('div' => array('class' => 'checkbox')),
  2321. array('label' => array('for' => "contact-multiple-3")),
  2322. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  2323. 'Three',
  2324. '/label',
  2325. '/div',
  2326. '/div'
  2327. );
  2328. $this->assertHtml($expected, $result);
  2329. // make sure 50 does only disable 50, and not 50f5c0cf
  2330. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2331. $disabled = array(50);
  2332. $expected = array(
  2333. array('div' => array('class' => 'input select')),
  2334. array('label' => array('for' => "contact-multiple")),
  2335. 'Multiple',
  2336. '/label',
  2337. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('label' => array('for' => "contact-multiple-50")),
  2340. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  2341. 'Fifty',
  2342. '/label',
  2343. '/div',
  2344. array('div' => array('class' => 'checkbox')),
  2345. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2346. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  2347. 'Stringy',
  2348. '/label',
  2349. '/div',
  2350. '/div'
  2351. );
  2352. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2353. $this->assertHtml($expected, $result);
  2354. }
  2355. /**
  2356. * test input name with leading integer, ensure attributes are generated correctly.
  2357. *
  2358. * @return void
  2359. */
  2360. public function testInputWithLeadingInteger() {
  2361. $result = $this->Form->text('0.Node.title');
  2362. $expected = array(
  2363. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2364. );
  2365. $this->assertHtml($expected, $result);
  2366. }
  2367. /**
  2368. * test form->input() with select type inputs.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testInputSelectType() {
  2373. $result = $this->Form->input('email', array(
  2374. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2375. );
  2376. $expected = array(
  2377. 'div' => array('class' => 'input select'),
  2378. 'label' => array('for' => 'email'),
  2379. 'Email',
  2380. '/label',
  2381. array('select' => array('name' => 'email', 'id' => 'email')),
  2382. array('option' => array('value' => '')),
  2383. '/option',
  2384. array('option' => array('value' => 'è')),
  2385. 'Firést',
  2386. '/option',
  2387. array('option' => array('value' => 'é')),
  2388. 'Secoènd',
  2389. '/option',
  2390. '/select',
  2391. '/div'
  2392. );
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->input('email', array(
  2395. 'options' => array('First', 'Second'), 'empty' => true)
  2396. );
  2397. $expected = array(
  2398. 'div' => array('class' => 'input select'),
  2399. 'label' => array('for' => 'email'),
  2400. 'Email',
  2401. '/label',
  2402. array('select' => array('name' => 'email', 'id' => 'email')),
  2403. array('option' => array('value' => '')),
  2404. '/option',
  2405. array('option' => array('value' => '0')),
  2406. 'First',
  2407. '/option',
  2408. array('option' => array('value' => '1')),
  2409. 'Second',
  2410. '/option',
  2411. '/select',
  2412. '/div'
  2413. );
  2414. $this->assertHtml($expected, $result);
  2415. $result = $this->Form->input('email', [
  2416. 'type' => 'select',
  2417. 'options' => new \ArrayObject(['First', 'Second']),
  2418. 'empty' => true
  2419. ]);
  2420. $this->assertHtml($expected, $result);
  2421. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2422. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2423. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2424. $expected = array(
  2425. 'div' => array('class' => 'input select'),
  2426. 'label' => array('for' => 'model-user-id'),
  2427. 'User',
  2428. '/label',
  2429. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2430. array('option' => array('value' => '')),
  2431. '/option',
  2432. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2433. 'good',
  2434. '/option',
  2435. array('option' => array('value' => 'other')),
  2436. 'bad',
  2437. '/option',
  2438. '/select',
  2439. '/div'
  2440. );
  2441. $this->assertHtml($expected, $result);
  2442. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2443. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2444. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2445. $expected = array(
  2446. 'div' => array('class' => 'input select'),
  2447. 'label' => array('for' => 'thing-user-id'),
  2448. 'User',
  2449. '/label',
  2450. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2451. array('option' => array('value' => '')),
  2452. 'Some Empty',
  2453. '/option',
  2454. array('option' => array('value' => 'value')),
  2455. 'good',
  2456. '/option',
  2457. array('option' => array('value' => 'other')),
  2458. 'bad',
  2459. '/option',
  2460. '/select',
  2461. '/div'
  2462. );
  2463. $this->assertHtml($expected, $result);
  2464. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2465. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2466. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2467. $expected = array(
  2468. 'div' => array('class' => 'input select'),
  2469. 'label' => array('for' => 'thing-user-id'),
  2470. 'User',
  2471. '/label',
  2472. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2473. array('option' => array('value' => '')),
  2474. 'Some Empty',
  2475. '/option',
  2476. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2477. 'good',
  2478. '/option',
  2479. array('option' => array('value' => 'other')),
  2480. 'bad',
  2481. '/option',
  2482. '/select',
  2483. '/div'
  2484. );
  2485. $this->assertHtml($expected, $result);
  2486. $this->Form->data = array();
  2487. $result = $this->Form->input('Publisher.id', array(
  2488. 'label' => 'Publisher',
  2489. 'type' => 'select',
  2490. 'multiple' => 'checkbox',
  2491. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2492. ));
  2493. $expected = array(
  2494. array('div' => array('class' => 'input select')),
  2495. array('label' => array('for' => 'publisher-id')),
  2496. 'Publisher',
  2497. '/label',
  2498. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2499. array('div' => array('class' => 'checkbox')),
  2500. array('label' => array('for' => 'publisher-id-value-1')),
  2501. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2502. 'Label 1',
  2503. '/label',
  2504. '/div',
  2505. array('div' => array('class' => 'checkbox')),
  2506. array('label' => array('for' => 'publisher-id-value-2')),
  2507. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2508. 'Label 2',
  2509. '/label',
  2510. '/div',
  2511. '/div'
  2512. );
  2513. $this->assertHtml($expected, $result);
  2514. }
  2515. /**
  2516. * test that input() and a non standard primary key makes a hidden input by default.
  2517. *
  2518. * @return void
  2519. */
  2520. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2521. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2522. $this->Form->create($this->article);
  2523. $result = $this->Form->input('title');
  2524. $expected = array(
  2525. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2526. );
  2527. $this->assertHtml($expected, $result);
  2528. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2529. $this->Form->create($this->article);
  2530. $result = $this->Form->input('title');
  2531. $expected = array(
  2532. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2533. );
  2534. $this->assertHtml($expected, $result);
  2535. $result = $this->Form->input('body');
  2536. $expected = array(
  2537. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2538. );
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * test that overriding the magic select type widget is possible
  2543. *
  2544. * @return void
  2545. */
  2546. public function testInputOverridingMagicSelectType() {
  2547. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2548. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2549. $expected = array(
  2550. 'div' => array('class' => 'input text'),
  2551. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2552. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2553. '/div'
  2554. );
  2555. $this->assertHtml($expected, $result);
  2556. //Check that magic types still work for plural/singular vars
  2557. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2558. $result = $this->Form->input('Model.type');
  2559. $expected = array(
  2560. 'div' => array('class' => 'input select'),
  2561. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2562. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2563. array('option' => array('value' => 'value')), 'good', '/option',
  2564. array('option' => array('value' => 'other')), 'bad', '/option',
  2565. '/select',
  2566. '/div'
  2567. );
  2568. $this->assertHtml($expected, $result);
  2569. }
  2570. /**
  2571. * Test that inferred types do not override developer input
  2572. *
  2573. * @return void
  2574. */
  2575. public function testInputMagicTypeDoesNotOverride() {
  2576. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2577. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2578. $expected = array(
  2579. 'div' => array('class' => 'input checkbox'),
  2580. array('input' => array(
  2581. 'type' => 'hidden',
  2582. 'name' => 'Model[user]',
  2583. 'value' => 0,
  2584. )),
  2585. 'label' => array('for' => 'model-user'),
  2586. array('input' => array(
  2587. 'name' => 'Model[user]',
  2588. 'type' => 'checkbox',
  2589. 'id' => 'model-user',
  2590. 'value' => 1
  2591. )),
  2592. 'User',
  2593. '/label',
  2594. '/div'
  2595. );
  2596. $this->assertHtml($expected, $result);
  2597. }
  2598. /**
  2599. * Test that magic input() selects are created for type=number
  2600. *
  2601. * @return void
  2602. */
  2603. public function testInputMagicSelectForTypeNumber() {
  2604. TableRegistry::get('ValidateUsers', [
  2605. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2606. ]);
  2607. $entity = new Entity(['balance' => 1]);
  2608. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2609. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2610. $result = $this->Form->input('balance');
  2611. $expected = array(
  2612. 'div' => array('class' => 'input select'),
  2613. 'label' => array('for' => 'balance'),
  2614. 'Balance',
  2615. '/label',
  2616. 'select' => array('name' => 'balance', 'id' => 'balance'),
  2617. array('option' => array('value' => '0')),
  2618. 'nothing',
  2619. '/option',
  2620. array('option' => array('value' => '1', 'selected' => 'selected')),
  2621. 'some',
  2622. '/option',
  2623. array('option' => array('value' => '100')),
  2624. 'a lot',
  2625. '/option',
  2626. '/select',
  2627. '/div'
  2628. );
  2629. $this->assertHtml($expected, $result);
  2630. }
  2631. /**
  2632. * Test that magic input() selects can easily be converted into radio types without error.
  2633. *
  2634. * @return void
  2635. */
  2636. public function testInputMagicSelectChangeToRadio() {
  2637. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2638. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2639. $this->assertContains('input type="radio"', $result);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputsLegendFieldset() {
  2649. $this->Form->create($this->article);
  2650. $result = $this->Form->allInputs([], array('legend' => 'The Legend'));
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertHtml($expected, $result);
  2659. $result = $this->Form->allInputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
  2660. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2661. $this->assertContains('<fieldset>', $result);
  2662. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => false));
  2663. $this->assertNotContains('<legend>', $result);
  2664. $this->assertNotContains('<fieldset>', $result);
  2665. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => 'Hello'));
  2666. $this->assertNotContains('<legend>', $result);
  2667. $this->assertNotContains('<fieldset>', $result);
  2668. $this->Form->create($this->article);
  2669. $this->Form->request->params['prefix'] = 'admin';
  2670. $this->Form->request->params['action'] = 'admin_edit';
  2671. $this->Form->request->params['controller'] = 'articles';
  2672. $result = $this->Form->allInputs();
  2673. $expected = [
  2674. '<fieldset',
  2675. '<legend',
  2676. 'New Article',
  2677. '/legend',
  2678. '*/fieldset',
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. }
  2682. /**
  2683. * Test the inputs() method.
  2684. *
  2685. * @return void
  2686. */
  2687. public function testFormInputs() {
  2688. $this->Form->create($this->article);
  2689. $result = $this->Form->allInputs();
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend', 'New Article', '/legend',
  2693. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2694. array('div' => array('class' => 'input select required')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input text required')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input text')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input text')),
  2701. '*/div',
  2702. '/fieldset',
  2703. );
  2704. $this->assertHtml($expected, $result);
  2705. $result = $this->Form->allInputs([
  2706. 'published' => ['type' => 'boolean']
  2707. ]);
  2708. $expected = array(
  2709. '<fieldset',
  2710. '<legend', 'New Article', '/legend',
  2711. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2712. array('div' => array('class' => 'input select required')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input text required')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input text')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input boolean')),
  2719. '*/div',
  2720. '/fieldset',
  2721. );
  2722. $this->assertHtml($expected, $result);
  2723. $this->Form->create($this->article);
  2724. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  2725. $expected = array(
  2726. 'fieldset' => array(),
  2727. 'legend' => array(),
  2728. 'Hello',
  2729. '/legend',
  2730. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2731. array('div' => array('class' => 'input select required')),
  2732. '*/div',
  2733. array('div' => array('class' => 'input text required')),
  2734. '*/div',
  2735. array('div' => array('class' => 'input text')),
  2736. '*/div',
  2737. array('div' => array('class' => 'input text')),
  2738. '*/div',
  2739. '/fieldset'
  2740. );
  2741. $this->assertHtml($expected, $result);
  2742. $this->Form->create(false);
  2743. $expected = array(
  2744. 'fieldset' => array(),
  2745. array('div' => array('class' => 'input text')),
  2746. 'label' => array('for' => 'foo'),
  2747. 'Foo',
  2748. '/label',
  2749. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2750. '*/div',
  2751. '/fieldset'
  2752. );
  2753. $result = $this->Form->allInputs(
  2754. array('foo' => array('type' => 'text')),
  2755. array('legend' => false)
  2756. );
  2757. $this->assertHtml($expected, $result);
  2758. }
  2759. /**
  2760. * testFormInputsBlacklist
  2761. *
  2762. * @return void
  2763. */
  2764. public function testFormInputsBlacklist() {
  2765. $this->Form->create($this->article);
  2766. $result = $this->Form->allInputs([
  2767. 'id' => false
  2768. ]);
  2769. $expected = array(
  2770. '<fieldset',
  2771. '<legend', 'New Article', '/legend',
  2772. array('div' => array('class' => 'input select required')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input text required')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input text')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input text')),
  2779. '*/div',
  2780. '/fieldset',
  2781. );
  2782. $this->assertHtml($expected, $result);
  2783. $this->Form->create($this->article);
  2784. $result = $this->Form->allInputs([
  2785. 'id' => []
  2786. ]);
  2787. $expected = array(
  2788. '<fieldset',
  2789. '<legend', 'New Article', '/legend',
  2790. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2791. array('div' => array('class' => 'input select required')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input text required')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input text')),
  2796. '*/div',
  2797. array('div' => array('class' => 'input text')),
  2798. '*/div',
  2799. '/fieldset',
  2800. );
  2801. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  2802. }
  2803. /**
  2804. * testSelectAsCheckbox method
  2805. *
  2806. * test multi-select widget with checkbox formatting.
  2807. *
  2808. * @return void
  2809. */
  2810. public function testSelectAsCheckbox() {
  2811. $result = $this->Form->select(
  2812. 'Model.multi_field',
  2813. array('first', 'second', 'third'),
  2814. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2815. );
  2816. $expected = array(
  2817. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2818. array('div' => array('class' => 'checkbox')),
  2819. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2820. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2821. 'first',
  2822. '/label',
  2823. '/div',
  2824. array('div' => array('class' => 'checkbox')),
  2825. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2826. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2827. 'second',
  2828. '/label',
  2829. '/div',
  2830. array('div' => array('class' => 'checkbox')),
  2831. array('label' => array('for' => 'model-multi-field-2')),
  2832. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2833. 'third',
  2834. '/label',
  2835. '/div',
  2836. );
  2837. $this->assertHtml($expected, $result);
  2838. $result = $this->Form->select(
  2839. 'Model.multi_field',
  2840. array('1/2' => 'half'),
  2841. array('multiple' => 'checkbox')
  2842. );
  2843. $expected = array(
  2844. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2845. array('div' => array('class' => 'checkbox')),
  2846. array('label' => array('for' => 'model-multi-field-1-2')),
  2847. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2848. 'half',
  2849. '/label',
  2850. '/div',
  2851. );
  2852. $this->assertHtml($expected, $result);
  2853. }
  2854. /**
  2855. * testLabel method
  2856. *
  2857. * test label generation.
  2858. *
  2859. * @return void
  2860. */
  2861. public function testLabel() {
  2862. $result = $this->Form->label('Person.name');
  2863. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2864. $this->assertHtml($expected, $result);
  2865. $result = $this->Form->label('Person.name');
  2866. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2867. $this->assertHtml($expected, $result);
  2868. $result = $this->Form->label('Person.first_name');
  2869. $expected = array('label' => array('for' => 'person-first-name'), 'First Name', '/label');
  2870. $this->assertHtml($expected, $result);
  2871. $result = $this->Form->label('Person.first_name', 'Your first name');
  2872. $expected = array('label' => array('for' => 'person-first-name'), 'Your first name', '/label');
  2873. $this->assertHtml($expected, $result);
  2874. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2875. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label');
  2876. $this->assertHtml($expected, $result);
  2877. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2878. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label');
  2879. $this->assertHtml($expected, $result);
  2880. $result = $this->Form->label('Person.first_name', '');
  2881. $expected = array('label' => array('for' => 'person-first-name'), '/label');
  2882. $this->assertHtml($expected, $result);
  2883. $result = $this->Form->label('Person.2.name', '');
  2884. $expected = array('label' => array('for' => 'person-2-name'), '/label');
  2885. $this->assertHtml($expected, $result);
  2886. }
  2887. /**
  2888. * Test that label() can accept an input with the correct template vars.
  2889. *
  2890. * @return void
  2891. */
  2892. public function testLabelContainInput() {
  2893. $this->Form->templates([
  2894. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2895. ]);
  2896. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  2897. 'input' => '<input type="checkbox" name="accept_tos" >'
  2898. ]);
  2899. $expected = [
  2900. 'label' => ['for' => 'person-accept-terms'],
  2901. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  2902. 'Accept',
  2903. '/label',
  2904. ];
  2905. $this->assertHtml($expected, $result);
  2906. }
  2907. /**
  2908. * testTextbox method
  2909. *
  2910. * test textbox element generation
  2911. *
  2912. * @return void
  2913. */
  2914. public function testTextbox() {
  2915. $result = $this->Form->text('Model.field');
  2916. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]'));
  2917. $this->assertHtml($expected, $result);
  2918. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2919. $expected = array('input' => array('type' => 'password', 'name' => 'Model[field]'));
  2920. $this->assertHtml($expected, $result);
  2921. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2922. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'));
  2923. $this->assertHtml($expected, $result);
  2924. }
  2925. /**
  2926. * Test that text() hooks up with request data and error fields.
  2927. *
  2928. * @return void
  2929. */
  2930. public function testTextBoxDataAndError() {
  2931. $this->article['errors'] = [
  2932. 'Contact' => ['text' => 'wrong']
  2933. ];
  2934. $this->Form->create($this->article);
  2935. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2936. $result = $this->Form->text('Model.text');
  2937. $expected = [
  2938. 'input' => [
  2939. 'type' => 'text',
  2940. 'name' => 'Model[text]',
  2941. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2942. ]
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $this->Form->request->data['Contact']['text'] = 'test';
  2946. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2947. $expected = [
  2948. 'input' => [
  2949. 'type' => 'text',
  2950. 'name' => 'Contact[text]',
  2951. 'value' => 'test',
  2952. 'id' => 'theID',
  2953. 'class' => 'form-error'
  2954. ]
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. }
  2958. /**
  2959. * testDefaultValue method
  2960. *
  2961. * Test default value setting
  2962. *
  2963. * @return void
  2964. */
  2965. public function testTextDefaultValue() {
  2966. $this->Form->request->data['Model']['field'] = 'test';
  2967. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2968. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2969. $this->assertHtml($expected, $result);
  2970. unset($this->Form->request->data['Model']['field']);
  2971. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2972. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2973. $this->assertHtml($expected, $result);
  2974. }
  2975. /**
  2976. * testError method
  2977. *
  2978. * Test field error generation
  2979. *
  2980. * @return void
  2981. */
  2982. public function testError() {
  2983. $this->article['errors'] = [
  2984. 'Article' => ['field' => 'email']
  2985. ];
  2986. $this->Form->create($this->article);
  2987. $result = $this->Form->error('Article.field');
  2988. $expected = [
  2989. ['div' => ['class' => 'error-message']],
  2990. 'email',
  2991. '/div',
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2995. $expected = [
  2996. ['div' => ['class' => 'error-message']],
  2997. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2998. '/div',
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3002. $expected = [
  3003. ['div' => ['class' => 'error-message']],
  3004. '<strong', 'Badness!', '/strong',
  3005. '/div',
  3006. ];
  3007. $this->assertHtml($expected, $result);
  3008. }
  3009. /**
  3010. * Test error with nested lists.
  3011. *
  3012. * @return void
  3013. */
  3014. public function testErrorMessages() {
  3015. $this->article['errors'] = [
  3016. 'Article' => ['field' => 'email']
  3017. ];
  3018. $this->Form->create($this->article);
  3019. $result = $this->Form->error('Article.field', array(
  3020. 'email' => 'No good!'
  3021. ));
  3022. $expected = array(
  3023. 'div' => array('class' => 'error-message'),
  3024. 'No good!',
  3025. '/div'
  3026. );
  3027. $this->assertHtml($expected, $result);
  3028. }
  3029. /**
  3030. * test error() with multiple messages.
  3031. *
  3032. * @return void
  3033. */
  3034. public function testErrorMultipleMessages() {
  3035. $this->article['errors'] = [
  3036. 'field' => ['notEmpty', 'email', 'Something else']
  3037. ];
  3038. $this->Form->create($this->article);
  3039. $result = $this->Form->error('field', array(
  3040. 'notEmpty' => 'Cannot be empty',
  3041. 'email' => 'No good!'
  3042. ));
  3043. $expected = array(
  3044. 'div' => array('class' => 'error-message'),
  3045. 'ul' => array(),
  3046. '<li', 'Cannot be empty', '/li',
  3047. '<li', 'No good!', '/li',
  3048. '<li', 'Something else', '/li',
  3049. '/ul',
  3050. '/div'
  3051. );
  3052. $this->assertHtml($expected, $result);
  3053. }
  3054. /**
  3055. * testPassword method
  3056. *
  3057. * Test password element generation
  3058. *
  3059. * @return void
  3060. */
  3061. public function testPassword() {
  3062. $this->article['errors'] = [
  3063. 'Contact' => [
  3064. 'passwd' => 1
  3065. ]
  3066. ];
  3067. $this->Form->create($this->article);
  3068. $result = $this->Form->password('Contact.field');
  3069. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[field]'));
  3070. $this->assertHtml($expected, $result);
  3071. $this->Form->request->data['Contact']['passwd'] = 'test';
  3072. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3073. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'));
  3074. $this->assertHtml($expected, $result);
  3075. }
  3076. /**
  3077. * testRadio method
  3078. *
  3079. * Test radio element set generation
  3080. *
  3081. * @return void
  3082. */
  3083. public function testRadio() {
  3084. $result = $this->Form->radio('Model.field', array('option A'));
  3085. $expected = array(
  3086. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3087. 'label' => array('for' => 'model-field-0'),
  3088. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3089. 'option A',
  3090. '/label'
  3091. );
  3092. $this->assertHtml($expected, $result);
  3093. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3096. $expected = array(
  3097. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3098. array('label' => array('for' => 'model-field-0')),
  3099. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3100. 'option A',
  3101. '/label',
  3102. array('label' => array('for' => 'model-field-1')),
  3103. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3104. 'option B',
  3105. '/label',
  3106. );
  3107. $this->assertHtml($expected, $result);
  3108. $result = $this->Form->radio(
  3109. 'Employee.gender',
  3110. array('male' => 'Male', 'female' => 'Female'),
  3111. ['form' => 'my-form']
  3112. );
  3113. $expected = array(
  3114. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'),
  3115. array('label' => array('for' => 'employee-gender-male')),
  3116. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form')),
  3117. 'Male',
  3118. '/label',
  3119. array('label' => array('for' => 'employee-gender-female')),
  3120. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form')),
  3121. 'Female',
  3122. '/label',
  3123. );
  3124. $this->assertHtml($expected, $result);
  3125. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3126. $expected = array(
  3127. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3128. array('label' => array('for' => 'model-custom-0')),
  3129. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3130. 'option A',
  3131. '/label',
  3132. array('label' => array('for' => 'model-custom-1')),
  3133. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3134. 'option B',
  3135. '/label',
  3136. );
  3137. $this->assertHtml($expected, $result);
  3138. }
  3139. /**
  3140. * Test that input works with radio types
  3141. *
  3142. * @return void
  3143. */
  3144. public function testInputRadio() {
  3145. $result = $this->Form->input('test', [
  3146. 'type' => 'radio',
  3147. 'options' => ['A', 'B'],
  3148. ]);
  3149. $expected = [
  3150. ['div' => ['class' => 'input radio']],
  3151. '<label',
  3152. 'Test',
  3153. '/label',
  3154. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3155. ['label' => ['for' => 'test-0']],
  3156. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3157. 'A',
  3158. '/label',
  3159. ['label' => ['for' => 'test-1']],
  3160. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3161. 'B',
  3162. '/label',
  3163. '/div',
  3164. ];
  3165. $this->assertHtml($expected, $result);
  3166. $result = $this->Form->input('test', [
  3167. 'type' => 'radio',
  3168. 'options' => ['A', 'B'],
  3169. 'label' => false
  3170. ]);
  3171. $expected = [
  3172. ['div' => ['class' => 'input radio']],
  3173. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3174. ['label' => ['for' => 'test-0']],
  3175. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3176. 'A',
  3177. '/label',
  3178. ['label' => ['for' => 'test-1']],
  3179. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3180. 'B',
  3181. '/label',
  3182. '/div',
  3183. ];
  3184. $this->assertHtml($expected, $result);
  3185. }
  3186. /**
  3187. * Test that radio() works with label = false
  3188. *
  3189. * @return void
  3190. */
  3191. public function testRadioNoLabel() {
  3192. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  3193. $expected = [
  3194. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3195. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3196. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3197. ];
  3198. $this->assertHtml($expected, $result);
  3199. }
  3200. /**
  3201. * test generating radio input inside label ala twitter bootstrap
  3202. *
  3203. * @return void
  3204. */
  3205. public function testRadioInputInsideLabel() {
  3206. $this->Form->templates([
  3207. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3208. 'radioWrapper' => '{{label}}'
  3209. ]);
  3210. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3211. $expected = [
  3212. ['input' => [
  3213. 'type' => 'hidden',
  3214. 'name' => 'Model[field]',
  3215. 'value' => ''
  3216. ]],
  3217. ['label' => ['for' => 'model-field-0']],
  3218. ['input' => [
  3219. 'type' => 'radio',
  3220. 'name' => 'Model[field]',
  3221. 'value' => '0',
  3222. 'id' => 'model-field-0'
  3223. ]],
  3224. 'option A',
  3225. '/label',
  3226. ['label' => ['for' => 'model-field-1']],
  3227. ['input' => [
  3228. 'type' => 'radio',
  3229. 'name' => 'Model[field]',
  3230. 'value' => '1',
  3231. 'id' => 'model-field-1'
  3232. ]],
  3233. 'option B',
  3234. '/label',
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. }
  3238. /**
  3239. * test disabling the hidden input for radio buttons
  3240. *
  3241. * @return void
  3242. */
  3243. public function testRadioHiddenInputDisabling() {
  3244. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3245. $expected = array(
  3246. 'label' => array('for' => 'model-1-field-0'),
  3247. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3248. 'option A',
  3249. '/label'
  3250. );
  3251. $this->assertHtml($expected, $result);
  3252. }
  3253. /**
  3254. * testSelect method
  3255. *
  3256. * Test select element generation.
  3257. *
  3258. * @return void
  3259. */
  3260. public function testSelect() {
  3261. $result = $this->Form->select('Model.field', array());
  3262. $expected = array(
  3263. 'select' => array('name' => 'Model[field]'),
  3264. '/select'
  3265. );
  3266. $this->assertHtml($expected, $result);
  3267. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3268. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3269. $expected = array(
  3270. 'select' => array('name' => 'Model[field]'),
  3271. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3272. 'good',
  3273. '/option',
  3274. array('option' => array('value' => 'other')),
  3275. 'bad',
  3276. '/option',
  3277. '/select'
  3278. );
  3279. $this->assertHtml($expected, $result);
  3280. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3281. $this->assertHtml($expected, $result);
  3282. $this->Form->request->data = array();
  3283. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3284. $expected = array(
  3285. 'select' => array('name' => 'Model[field]'),
  3286. array('option' => array('value' => 'value')),
  3287. 'good',
  3288. '/option',
  3289. array('option' => array('value' => 'other')),
  3290. 'bad',
  3291. '/option',
  3292. '/select'
  3293. );
  3294. $this->assertHtml($expected, $result);
  3295. $options = array(
  3296. array('value' => 'first', 'text' => 'First'),
  3297. array('value' => 'first', 'text' => 'Another First'),
  3298. );
  3299. $result = $this->Form->select(
  3300. 'Model.field',
  3301. $options,
  3302. array('escape' => false, 'empty' => false)
  3303. );
  3304. $expected = array(
  3305. 'select' => array('name' => 'Model[field]'),
  3306. array('option' => array('value' => 'first')),
  3307. 'First',
  3308. '/option',
  3309. array('option' => array('value' => 'first')),
  3310. 'Another First',
  3311. '/option',
  3312. '/select'
  3313. );
  3314. $this->assertHtml($expected, $result);
  3315. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3316. $result = $this->Form->select(
  3317. 'Model.contact_id',
  3318. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3319. array('escape' => false, 'empty' => 'pick something')
  3320. );
  3321. $expected = array(
  3322. 'select' => array('name' => 'Model[contact_id]'),
  3323. array('option' => array('value' => '')), 'pick something', '/option',
  3324. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3325. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3326. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3327. '/select'
  3328. );
  3329. $this->assertHtml($expected, $result);
  3330. $this->Form->request->data['Model']['field'] = 0;
  3331. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3332. $expected = array(
  3333. 'select' => array('name' => 'Model[field]'),
  3334. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3335. array('option' => array('value' => '1')), 'Yes', '/option',
  3336. '/select'
  3337. );
  3338. $this->assertHtml($expected, $result);
  3339. }
  3340. /**
  3341. * Test that select() escapes HTML.
  3342. *
  3343. * @return void
  3344. */
  3345. public function testSelectEscapeHtml() {
  3346. $result = $this->Form->select(
  3347. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3348. array('empty' => false)
  3349. );
  3350. $expected = array(
  3351. 'select' => array('name' => 'Model[field]'),
  3352. array('option' => array('value' => 'first')),
  3353. 'first &quot;html&quot; &lt;chars&gt;',
  3354. '/option',
  3355. array('option' => array('value' => 'second')),
  3356. 'value',
  3357. '/option',
  3358. '/select'
  3359. );
  3360. $this->assertHtml($expected, $result);
  3361. $result = $this->Form->select(
  3362. 'Model.field',
  3363. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3364. array('escape' => false, 'empty' => false)
  3365. );
  3366. $expected = array(
  3367. 'select' => array('name' => 'Model[field]'),
  3368. array('option' => array('value' => 'first')),
  3369. 'first "html" <chars>',
  3370. '/option',
  3371. array('option' => array('value' => 'second')),
  3372. 'value',
  3373. '/option',
  3374. '/select'
  3375. );
  3376. $this->assertHtml($expected, $result);
  3377. }
  3378. /**
  3379. * test select() with required and disabled attributes.
  3380. *
  3381. * @return void
  3382. */
  3383. public function testSelectRequired() {
  3384. $this->article['required'] = [
  3385. 'user_id' => true
  3386. ];
  3387. $this->Form->create($this->article);
  3388. $result = $this->Form->select('user_id', array('option A'));
  3389. $expected = array(
  3390. 'select' => array(
  3391. 'name' => 'user_id',
  3392. 'required' => 'required'
  3393. ),
  3394. array('option' => array('value' => '0')), 'option A', '/option',
  3395. '/select'
  3396. );
  3397. $this->assertHtml($expected, $result);
  3398. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3399. $expected = array(
  3400. 'select' => array(
  3401. 'name' => 'user_id',
  3402. 'disabled' => 'disabled'
  3403. ),
  3404. array('option' => array('value' => '0')), 'option A', '/option',
  3405. '/select'
  3406. );
  3407. $this->assertHtml($expected, $result);
  3408. }
  3409. /**
  3410. * testNestedSelect method
  3411. *
  3412. * test select element generation with optgroups
  3413. *
  3414. * @return void
  3415. */
  3416. public function testNestedSelect() {
  3417. $result = $this->Form->select(
  3418. 'Model.field',
  3419. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3420. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3421. )), array('empty' => false)
  3422. );
  3423. $expected = array(
  3424. 'select' => array('name' => 'Model[field]'),
  3425. array('option' => array('value' => 1)),
  3426. 'One',
  3427. '/option',
  3428. array('option' => array('value' => 2)),
  3429. 'Two',
  3430. '/option',
  3431. array('optgroup' => array('label' => 'Three')),
  3432. array('option' => array('value' => 3)),
  3433. 'Three',
  3434. '/option',
  3435. array('option' => array('value' => 4)),
  3436. 'Four',
  3437. '/option',
  3438. array('option' => array('value' => 5)),
  3439. 'Five',
  3440. '/option',
  3441. '/optgroup',
  3442. '/select'
  3443. );
  3444. $this->assertHtml($expected, $result);
  3445. }
  3446. /**
  3447. * testSelectMultiple method
  3448. *
  3449. * test generation of multiple select elements
  3450. *
  3451. * @return void
  3452. */
  3453. public function testSelectMultiple() {
  3454. $options = array('first', 'second', 'third');
  3455. $result = $this->Form->select(
  3456. 'Model.multi_field',
  3457. $options,
  3458. ['form' => 'my-form', 'multiple' => true]
  3459. );
  3460. $expected = array(
  3461. 'input' => array(
  3462. 'type' => 'hidden',
  3463. 'name' => 'Model[multi_field]',
  3464. 'value' => '',
  3465. 'form' => 'my-form',
  3466. ),
  3467. 'select' => array(
  3468. 'name' => 'Model[multi_field][]',
  3469. 'multiple' => 'multiple',
  3470. 'form' => 'my-form',
  3471. ),
  3472. array('option' => array('value' => '0')),
  3473. 'first',
  3474. '/option',
  3475. array('option' => array('value' => '1')),
  3476. 'second',
  3477. '/option',
  3478. array('option' => array('value' => '2')),
  3479. 'third',
  3480. '/option',
  3481. '/select'
  3482. );
  3483. $this->assertHtml($expected, $result);
  3484. $result = $this->Form->select(
  3485. 'Model.multi_field',
  3486. $options,
  3487. ['multiple' => 'multiple', 'form' => 'my-form']
  3488. );
  3489. $this->assertHtml($expected, $result);
  3490. }
  3491. /**
  3492. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3493. *
  3494. * @return void
  3495. */
  3496. public function testCheckboxZeroValue() {
  3497. $result = $this->Form->input('User.get_spam', array(
  3498. 'type' => 'checkbox',
  3499. 'value' => '0',
  3500. 'hiddenField' => '1',
  3501. ));
  3502. $expected = array(
  3503. 'div' => array('class' => 'input checkbox'),
  3504. 'label' => array('for' => 'user-get-spam'),
  3505. array('input' => array(
  3506. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3507. 'value' => '1'
  3508. )),
  3509. array('input' => array(
  3510. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3511. 'value' => '0', 'id' => 'user-get-spam'
  3512. )),
  3513. 'Get Spam',
  3514. '/label',
  3515. '/div'
  3516. );
  3517. $this->assertHtml($expected, $result);
  3518. }
  3519. /**
  3520. * test generation of habtm select boxes.
  3521. *
  3522. * @return void
  3523. */
  3524. public function testHabtmSelectBox() {
  3525. $this->loadFixtures('Articles');
  3526. $options = array(
  3527. 1 => 'blue',
  3528. 2 => 'red',
  3529. 3 => 'green'
  3530. );
  3531. $tags = [
  3532. new Entity(['id' => 1, 'name' => 'blue']),
  3533. new Entity(['id' => 3, 'name' => 'green'])
  3534. ];
  3535. $article = new Article(['tags' => $tags]);
  3536. $this->Form->create($article);
  3537. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3538. $expected = array(
  3539. 'div' => array('class' => 'input select'),
  3540. 'label' => array('for' => 'tags-ids'),
  3541. 'Tags',
  3542. '/label',
  3543. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3544. 'select' => array(
  3545. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3546. 'multiple' => 'multiple'
  3547. ),
  3548. array('option' => array('value' => '1', 'selected' => 'selected')),
  3549. 'blue',
  3550. '/option',
  3551. array('option' => array('value' => '2')),
  3552. 'red',
  3553. '/option',
  3554. array('option' => array('value' => '3', 'selected' => 'selected')),
  3555. 'green',
  3556. '/option',
  3557. '/select',
  3558. '/div'
  3559. );
  3560. $this->assertHtml($expected, $result);
  3561. // make sure only 50 is selected, and not 50f5c0cf
  3562. $options = array(
  3563. '1' => 'blue',
  3564. '50f5c0cf' => 'red',
  3565. '50' => 'green'
  3566. );
  3567. $tags = [
  3568. new Entity(['id' => 1, 'name' => 'blue']),
  3569. new Entity(['id' => 50, 'name' => 'green'])
  3570. ];
  3571. $article = new Article(['tags' => $tags]);
  3572. $this->Form->create($article);
  3573. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3574. $expected = array(
  3575. 'div' => array('class' => 'input select'),
  3576. 'label' => array('for' => 'tags-ids'),
  3577. 'Tags',
  3578. '/label',
  3579. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3580. 'select' => array(
  3581. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3582. 'multiple' => 'multiple'
  3583. ),
  3584. array('option' => array('value' => '1', 'selected' => 'selected')),
  3585. 'blue',
  3586. '/option',
  3587. array('option' => array('value' => '50f5c0cf')),
  3588. 'red',
  3589. '/option',
  3590. array('option' => array('value' => '50', 'selected' => 'selected')),
  3591. 'green',
  3592. '/option',
  3593. '/select',
  3594. '/div'
  3595. );
  3596. $this->assertHtml($expected, $result);
  3597. }
  3598. /**
  3599. * test generation of multi select elements in checkbox format
  3600. *
  3601. * @return void
  3602. */
  3603. public function testSelectMultipleCheckboxes() {
  3604. $result = $this->Form->select(
  3605. 'Model.multi_field',
  3606. array('first', 'second', 'third'),
  3607. array('multiple' => 'checkbox')
  3608. );
  3609. $expected = array(
  3610. 'input' => array(
  3611. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3612. ),
  3613. array('div' => array('class' => 'checkbox')),
  3614. array('label' => array('for' => 'model-multi-field-0')),
  3615. array('input' => array(
  3616. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3617. 'value' => '0', 'id' => 'model-multi-field-0'
  3618. )),
  3619. 'first',
  3620. '/label',
  3621. '/div',
  3622. array('div' => array('class' => 'checkbox')),
  3623. array('label' => array('for' => 'model-multi-field-1')),
  3624. array('input' => array(
  3625. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3626. 'value' => '1', 'id' => 'model-multi-field-1'
  3627. )),
  3628. 'second',
  3629. '/label',
  3630. '/div',
  3631. array('div' => array('class' => 'checkbox')),
  3632. array('label' => array('for' => 'model-multi-field-2')),
  3633. array('input' => array(
  3634. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3635. 'value' => '2', 'id' => 'model-multi-field-2'
  3636. )),
  3637. 'third',
  3638. '/label',
  3639. '/div'
  3640. );
  3641. $this->assertHtml($expected, $result);
  3642. $result = $this->Form->select(
  3643. 'Model.multi_field',
  3644. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3645. array('multiple' => 'checkbox')
  3646. );
  3647. $expected = array(
  3648. 'input' => array(
  3649. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3650. ),
  3651. array('div' => array('class' => 'checkbox')),
  3652. array('label' => array('for' => 'model-multi-field-a+')),
  3653. array('input' => array(
  3654. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3655. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3656. )),
  3657. 'first',
  3658. '/label',
  3659. '/div',
  3660. array('div' => array('class' => 'checkbox')),
  3661. array('label' => array('for' => 'model-multi-field-a++')),
  3662. array('input' => array(
  3663. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3664. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3665. )),
  3666. 'second',
  3667. '/label',
  3668. '/div',
  3669. array('div' => array('class' => 'checkbox')),
  3670. array('label' => array('for' => 'model-multi-field-a+++')),
  3671. array('input' => array(
  3672. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3673. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3674. )),
  3675. 'third',
  3676. '/label',
  3677. '/div'
  3678. );
  3679. $this->assertHtml($expected, $result);
  3680. $result = $this->Form->select(
  3681. 'Model.multi_field',
  3682. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3683. array('multiple' => 'checkbox')
  3684. );
  3685. $expected = array(
  3686. 'input' => array(
  3687. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3688. ),
  3689. array('div' => array('class' => 'checkbox')),
  3690. array('label' => array('for' => 'model-multi-field-a-b')),
  3691. array('input' => array(
  3692. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3693. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3694. )),
  3695. 'first',
  3696. '/label',
  3697. '/div',
  3698. array('div' => array('class' => 'checkbox')),
  3699. array('label' => array('for' => 'model-multi-field-a-b1')),
  3700. array('input' => array(
  3701. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3702. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3703. )),
  3704. 'second',
  3705. '/label',
  3706. '/div',
  3707. array('div' => array('class' => 'checkbox')),
  3708. array('label' => array('for' => 'model-multi-field-a-b2')),
  3709. array('input' => array(
  3710. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3711. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3712. )),
  3713. 'third',
  3714. '/label',
  3715. '/div'
  3716. );
  3717. $this->assertHtml($expected, $result);
  3718. }
  3719. /**
  3720. * Ensure that multiCheckbox reads from the request data.
  3721. *
  3722. * @return void
  3723. */
  3724. public function testSelectMultipleCheckboxRequestData() {
  3725. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3726. $result = $this->Form->select(
  3727. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3728. );
  3729. $expected = array(
  3730. 'input' => array(
  3731. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3732. ),
  3733. array('div' => array('class' => 'checkbox')),
  3734. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3735. array('input' => array(
  3736. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3737. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3738. )),
  3739. 'first',
  3740. '/label',
  3741. '/div',
  3742. array('div' => array('class' => 'checkbox')),
  3743. array('label' => array('for' => 'model-tags-array')),
  3744. array('input' => array(
  3745. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3746. 'value' => 'Array', 'id' => 'model-tags-array'
  3747. )),
  3748. 'Array',
  3749. '/label',
  3750. '/div'
  3751. );
  3752. $this->assertHtml($expected, $result);
  3753. }
  3754. /**
  3755. * Checks the security hash array generated for multiple-input checkbox elements
  3756. *
  3757. * @return void
  3758. */
  3759. public function testSelectMultipleCheckboxSecurity() {
  3760. $this->Form->request->params['_Token'] = 'testKey';
  3761. $this->assertEquals(array(), $this->Form->fields);
  3762. $result = $this->Form->select(
  3763. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3764. array('multiple' => 'checkbox')
  3765. );
  3766. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3767. $result = $this->Form->secure($this->Form->fields);
  3768. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3769. $this->assertRegExp('/"' . $key . '"/', $result);
  3770. }
  3771. /**
  3772. * Multiple select elements should always be secured as they always participate
  3773. * in the POST data.
  3774. *
  3775. * @return void
  3776. */
  3777. public function testSelectMultipleSecureWithNoOptions() {
  3778. $this->assertEquals(array(), $this->Form->fields);
  3779. $this->Form->select(
  3780. 'Model.select',
  3781. array(),
  3782. array('multiple' => true)
  3783. );
  3784. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3785. }
  3786. /**
  3787. * When a select box has no options it should not be added to the fields list
  3788. * as it always fail post validation.
  3789. *
  3790. * @return void
  3791. */
  3792. public function testSelectNoSecureWithNoOptions() {
  3793. $this->Form->request->params['_Token'] = 'testkey';
  3794. $this->assertEquals([], $this->Form->fields);
  3795. $this->Form->select(
  3796. 'Model.select',
  3797. []
  3798. );
  3799. $this->assertEquals([], $this->Form->fields);
  3800. $this->Form->select(
  3801. 'Model.user_id',
  3802. [],
  3803. ['empty' => true]
  3804. );
  3805. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3806. }
  3807. /**
  3808. * testInputMultipleCheckboxes method
  3809. *
  3810. * test input() resulting in multi select elements being generated.
  3811. *
  3812. * @return void
  3813. */
  3814. public function testInputMultipleCheckboxes() {
  3815. $result = $this->Form->input('Model.multi_field', array(
  3816. 'options' => array('first', 'second', 'third'),
  3817. 'multiple' => 'checkbox'
  3818. ));
  3819. $expected = array(
  3820. array('div' => array('class' => 'input select')),
  3821. array('label' => array('for' => 'model-multi-field')),
  3822. 'Multi Field',
  3823. '/label',
  3824. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3825. array('div' => array('class' => 'checkbox')),
  3826. array('label' => array('for' => 'model-multi-field-0')),
  3827. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3828. 'first',
  3829. '/label',
  3830. '/div',
  3831. array('div' => array('class' => 'checkbox')),
  3832. array('label' => array('for' => 'model-multi-field-1')),
  3833. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3834. 'second',
  3835. '/label',
  3836. '/div',
  3837. array('div' => array('class' => 'checkbox')),
  3838. array('label' => array('for' => 'model-multi-field-2')),
  3839. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3840. 'third',
  3841. '/label',
  3842. '/div',
  3843. '/div'
  3844. );
  3845. $this->assertHtml($expected, $result);
  3846. $result = $this->Form->input('Model.multi_field', array(
  3847. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3848. 'multiple' => 'checkbox'
  3849. ));
  3850. $expected = array(
  3851. array('div' => array('class' => 'input select')),
  3852. array('label' => array('for' => 'model-multi-field')),
  3853. 'Multi Field',
  3854. '/label',
  3855. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3856. array('div' => array('class' => 'checkbox')),
  3857. array('label' => array('for' => 'model-multi-field-a')),
  3858. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3859. 'first',
  3860. '/label',
  3861. '/div',
  3862. array('div' => array('class' => 'checkbox')),
  3863. array('label' => array('for' => 'model-multi-field-b')),
  3864. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3865. 'second',
  3866. '/label',
  3867. '/div',
  3868. array('div' => array('class' => 'checkbox')),
  3869. array('label' => array('for' => 'model-multi-field-c')),
  3870. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3871. 'third',
  3872. '/label',
  3873. '/div',
  3874. '/div'
  3875. );
  3876. $this->assertHtml($expected, $result);
  3877. }
  3878. /**
  3879. * testSelectHiddenFieldOmission method
  3880. *
  3881. * test that select() with 'hiddenField' => false omits the hidden field
  3882. *
  3883. * @return void
  3884. */
  3885. public function testSelectHiddenFieldOmission() {
  3886. $result = $this->Form->select('Model.multi_field',
  3887. array('first', 'second'),
  3888. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3889. );
  3890. $this->assertNotContains('type="hidden"', $result);
  3891. }
  3892. /**
  3893. * test that select() with multiple = checkbox works with overriding name attribute.
  3894. *
  3895. * @return void
  3896. */
  3897. public function testSelectCheckboxMultipleOverrideName() {
  3898. $result = $this->Form->select('category', ['1', '2'], [
  3899. 'multiple' => 'checkbox',
  3900. 'name' => 'fish',
  3901. ]);
  3902. $expected = array(
  3903. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3904. array('div' => array('class' => 'checkbox')),
  3905. array('label' => array('for' => 'fish-0')),
  3906. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3907. '1',
  3908. '/label',
  3909. '/div',
  3910. array('div' => array('class' => 'checkbox')),
  3911. array('label' => array('for' => 'fish-1')),
  3912. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3913. '2',
  3914. '/label',
  3915. '/div'
  3916. );
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->multiCheckbox(
  3919. 'category',
  3920. new Collection(['1', '2']),
  3921. ['name' => 'fish']
  3922. );
  3923. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3924. 'name' => 'fish',
  3925. ]);
  3926. $this->assertHtml($expected, $result);
  3927. }
  3928. /**
  3929. * Test that input() works with multicheckbox.
  3930. *
  3931. * @return void
  3932. */
  3933. public function testInputMultiCheckbox() {
  3934. $result = $this->Form->input('category', [
  3935. 'type' => 'multicheckbox',
  3936. 'options' => ['1', '2'],
  3937. ]);
  3938. $expected = [
  3939. ['div' => ['class' => 'input multicheckbox']],
  3940. '<label',
  3941. 'Category',
  3942. '/label',
  3943. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  3944. ['div' => ['class' => 'checkbox']],
  3945. ['label' => ['for' => 'category-0']],
  3946. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  3947. '1',
  3948. '/label',
  3949. '/div',
  3950. ['div' => ['class' => 'checkbox']],
  3951. ['label' => ['for' => 'category-1']],
  3952. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  3953. '2',
  3954. '/label',
  3955. '/div',
  3956. '/div',
  3957. ];
  3958. $this->assertHtml($expected, $result);
  3959. }
  3960. /**
  3961. * testCheckbox method
  3962. *
  3963. * Test generation of checkboxes
  3964. *
  3965. * @return void
  3966. */
  3967. public function testCheckbox() {
  3968. $result = $this->Form->checkbox('Model.field');
  3969. $expected = array(
  3970. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3971. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3972. );
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->checkbox('Model.field', array(
  3975. 'id' => 'theID',
  3976. 'value' => 'myvalue',
  3977. 'form' => 'my-form',
  3978. ));
  3979. $expected = array(
  3980. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'),
  3981. array('input' => array(
  3982. 'type' => 'checkbox', 'name' => 'Model[field]',
  3983. 'value' => 'myvalue', 'id' => 'theID',
  3984. 'form' => 'my-form',
  3985. ))
  3986. );
  3987. $this->assertHtml($expected, $result);
  3988. }
  3989. /**
  3990. * testCheckboxDefaultValue method
  3991. *
  3992. * Test default value setting on checkbox() method
  3993. *
  3994. * @return void
  3995. */
  3996. public function testCheckboxDefaultValue() {
  3997. $this->Form->request->data['Model']['field'] = false;
  3998. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3999. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4000. $this->assertHtml($expected, $result);
  4001. unset($this->Form->request->data['Model']['field']);
  4002. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4003. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4004. $this->assertHtml($expected, $result);
  4005. $this->Form->request->data['Model']['field'] = true;
  4006. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4007. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4008. $this->assertHtml($expected, $result);
  4009. unset($this->Form->request->data['Model']['field']);
  4010. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4011. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4012. $this->assertHtml($expected, $result);
  4013. }
  4014. /**
  4015. * Test checkbox being checked or having errors.
  4016. *
  4017. * @return void
  4018. */
  4019. public function testCheckboxCheckedAndError() {
  4020. $this->article['errors'] = [
  4021. 'published' => true
  4022. ];
  4023. $this->Form->request->data['published'] = 'myvalue';
  4024. $this->Form->create($this->article);
  4025. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4026. $expected = array(
  4027. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4028. array('input' => array(
  4029. 'type' => 'checkbox',
  4030. 'name' => 'published',
  4031. 'value' => 'myvalue',
  4032. 'id' => 'theID',
  4033. 'checked' => 'checked',
  4034. 'class' => 'form-error'
  4035. ))
  4036. );
  4037. $this->assertHtml($expected, $result);
  4038. $this->Form->request->data['published'] = '';
  4039. $result = $this->Form->checkbox('published');
  4040. $expected = array(
  4041. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4042. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4043. );
  4044. $this->assertHtml($expected, $result);
  4045. }
  4046. /**
  4047. * test checkbox() with a custom name attribute
  4048. *
  4049. * @return void
  4050. */
  4051. public function testCheckboxCustomNameAttribute() {
  4052. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4053. $expected = array(
  4054. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4055. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4056. );
  4057. $this->assertHtml($expected, $result);
  4058. }
  4059. /**
  4060. * Test that the hidden input for checkboxes can be omitted or set to a
  4061. * specific value.
  4062. *
  4063. * @return void
  4064. */
  4065. public function testCheckboxHiddenField() {
  4066. $result = $this->Form->checkbox('UserForm.something', array(
  4067. 'hiddenField' => false
  4068. ));
  4069. $expected = array(
  4070. 'input' => array(
  4071. 'type' => 'checkbox',
  4072. 'name' => 'UserForm[something]',
  4073. 'value' => '1'
  4074. ),
  4075. );
  4076. $this->assertHtml($expected, $result);
  4077. $result = $this->Form->checkbox('UserForm.something', array(
  4078. 'value' => 'Y',
  4079. 'hiddenField' => 'N',
  4080. ));
  4081. $expected = array(
  4082. array('input' => array(
  4083. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4084. 'value' => 'N'
  4085. )),
  4086. array('input' => array(
  4087. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4088. 'value' => 'Y'
  4089. )),
  4090. );
  4091. $this->assertHtml($expected, $result);
  4092. }
  4093. /**
  4094. * Test the time type.
  4095. *
  4096. * @return void
  4097. */
  4098. public function testTime() {
  4099. $result = $this->Form->time('start_time', array(
  4100. 'timeFormat' => 12,
  4101. 'interval' => 5,
  4102. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  4103. ));
  4104. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4105. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4106. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4107. $this->assertNotContains('year', $result);
  4108. $this->assertNotContains('month', $result);
  4109. $this->assertNotContains('day', $result);
  4110. $result = $this->Form->time('start_time', array(
  4111. 'timeFormat' => 12,
  4112. 'interval' => 5,
  4113. 'value' => '2014-03-08 16:30:00'
  4114. ));
  4115. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4116. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4117. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4118. $this->assertNotContains('year', $result);
  4119. $this->assertNotContains('month', $result);
  4120. $this->assertNotContains('day', $result);
  4121. }
  4122. /**
  4123. * Ensure that timeFormat=24 has no merdian.
  4124. *
  4125. * @return void.
  4126. */
  4127. public function testTimeFormat24NoMeridian() {
  4128. $result = $this->Form->time('start_time', array(
  4129. 'timeFormat' => 24,
  4130. 'interval' => 5,
  4131. 'value' => '2014-03-08 16:30:00'
  4132. ));
  4133. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  4134. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4135. $this->assertNotContains('meridian', $result);
  4136. $this->assertNotContains('pm', $result);
  4137. $this->assertNotContains('year', $result);
  4138. $this->assertNotContains('month', $result);
  4139. $this->assertNotContains('day', $result);
  4140. }
  4141. /**
  4142. * Test the date type.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testDate() {
  4147. $result = $this->Form->date('start_day', array(
  4148. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  4149. ));
  4150. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  4151. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  4152. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  4153. $this->assertNotContains('hour', $result);
  4154. $this->assertNotContains('minute', $result);
  4155. $this->assertNotContains('second', $result);
  4156. $this->assertNotContains('meridian', $result);
  4157. }
  4158. /**
  4159. * testDateTime method
  4160. *
  4161. * Test generation of date/time select elements
  4162. *
  4163. * @return void
  4164. */
  4165. public function testDateTime() {
  4166. extract($this->dateRegex);
  4167. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4168. $now = strtotime('now');
  4169. $expected = array(
  4170. array('select' => array('name' => 'Contact[date][year]')),
  4171. $yearsRegex,
  4172. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4173. date('Y', $now),
  4174. '/option',
  4175. '*/select',
  4176. array('select' => array('name' => 'Contact[date][month]')),
  4177. $monthsRegex,
  4178. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4179. date('F', $now),
  4180. '/option',
  4181. '*/select',
  4182. array('select' => array('name' => 'Contact[date][day]')),
  4183. $daysRegex,
  4184. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4185. date('j', $now),
  4186. '/option',
  4187. '*/select',
  4188. array('select' => array('name' => 'Contact[date][hour]')),
  4189. $hoursRegex,
  4190. array('option' => array('value' => date('H', $now), 'selected' => 'selected')),
  4191. date('G', $now),
  4192. '/option',
  4193. '*/select',
  4194. array('select' => array('name' => 'Contact[date][minute]')),
  4195. $minutesRegex,
  4196. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4197. date('i', $now),
  4198. '/option',
  4199. '*/select',
  4200. );
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * Test that datetime fields are added to protected fields list.
  4205. *
  4206. * @return void
  4207. */
  4208. public function testDateTimeSecured() {
  4209. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4210. $this->Form->dateTime('Contact.date');
  4211. $expected = [
  4212. 'Contact.date.year',
  4213. 'Contact.date.month',
  4214. 'Contact.date.day',
  4215. 'Contact.date.hour',
  4216. 'Contact.date.minute',
  4217. ];
  4218. $this->assertEquals($expected, $this->Form->fields);
  4219. $this->Form->fields = [];
  4220. $this->Form->date('Contact.published');
  4221. $expected = [
  4222. 'Contact.published.year',
  4223. 'Contact.published.month',
  4224. 'Contact.published.day',
  4225. ];
  4226. $this->assertEquals($expected, $this->Form->fields);
  4227. }
  4228. /**
  4229. * Test that datetime fields are added to protected fields list.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testDateTimeSecuredDisabled() {
  4234. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4235. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4236. $expected = [];
  4237. $this->assertEquals($expected, $this->Form->fields);
  4238. $this->Form->fields = [];
  4239. $this->Form->date('Contact.published', ['secure' => false]);
  4240. $expected = [];
  4241. $this->assertEquals($expected, $this->Form->fields);
  4242. }
  4243. /**
  4244. * Test empty defaulting to true for datetime.
  4245. *
  4246. * @return void
  4247. */
  4248. public function testDatetimeEmpty() {
  4249. extract($this->dateRegex);
  4250. $now = strtotime('now');
  4251. $result = $this->Form->dateTime('Contact.date', array(
  4252. 'timeFormat' => 12,
  4253. 'empty' => true,
  4254. ));
  4255. $expected = array(
  4256. array('select' => array('name' => 'Contact[date][year]')),
  4257. $yearsRegex,
  4258. array('option' => array('value' => '')),
  4259. '/option',
  4260. '*/select',
  4261. array('select' => array('name' => 'Contact[date][month]')),
  4262. $monthsRegex,
  4263. array('option' => array('value' => '')),
  4264. '/option',
  4265. '*/select',
  4266. array('select' => array('name' => 'Contact[date][day]')),
  4267. $daysRegex,
  4268. array('option' => array('value' => '')),
  4269. '/option',
  4270. '*/select',
  4271. array('select' => array('name' => 'Contact[date][hour]')),
  4272. $hoursRegex,
  4273. array('option' => array('value' => '')),
  4274. '/option',
  4275. '*/select',
  4276. array('select' => array('name' => 'Contact[date][minute]')),
  4277. $minutesRegex,
  4278. array('option' => array('value' => '')),
  4279. '/option',
  4280. '*/select',
  4281. array('select' => array('name' => 'Contact[date][meridian]')),
  4282. $meridianRegex,
  4283. array('option' => array('value' => '')),
  4284. '/option',
  4285. '*/select'
  4286. );
  4287. $this->assertHtml($expected, $result);
  4288. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4289. }
  4290. /**
  4291. * Test datetime with interval option.
  4292. *
  4293. * @return void
  4294. */
  4295. public function testDatetimeMinuteInterval() {
  4296. extract($this->dateRegex);
  4297. $now = strtotime('now');
  4298. $result = $this->Form->dateTime('Contact.date', array(
  4299. 'interval' => 5,
  4300. 'value' => ''
  4301. ));
  4302. $expected = array(
  4303. array('select' => array('name' => 'Contact[date][year]')),
  4304. $yearsRegex,
  4305. array('option' => array('selected' => 'selected', 'value' => '')),
  4306. '/option',
  4307. '*/select',
  4308. array('select' => array('name' => 'Contact[date][month]')),
  4309. $monthsRegex,
  4310. array('option' => array('selected' => 'selected', 'value' => '')),
  4311. '/option',
  4312. '*/select',
  4313. array('select' => array('name' => 'Contact[date][day]')),
  4314. $daysRegex,
  4315. array('option' => array('selected' => 'selected', 'value' => '')),
  4316. '/option',
  4317. '*/select',
  4318. array('select' => array('name' => 'Contact[date][hour]')),
  4319. $hoursRegex,
  4320. array('option' => array('selected' => 'selected', 'value' => '')),
  4321. '/option',
  4322. '*/select',
  4323. array('select' => array('name' => 'Contact[date][minute]')),
  4324. $minutesRegex,
  4325. array('option' => array('selected' => 'selected', 'value' => '')),
  4326. '/option',
  4327. array('option' => array('value' => '00')),
  4328. '00',
  4329. '/option',
  4330. array('option' => array('value' => '05')),
  4331. '05',
  4332. '/option',
  4333. array('option' => array('value' => '10')),
  4334. '10',
  4335. '/option',
  4336. '*/select',
  4337. );
  4338. $this->assertHtml($expected, $result);
  4339. }
  4340. /**
  4341. * Test dateTime with rounding
  4342. *
  4343. * @return void
  4344. */
  4345. public function testDateTimeRounding() {
  4346. $this->Form->request->data['Contact'] = array(
  4347. 'date' => array(
  4348. 'day' => '13',
  4349. 'month' => '12',
  4350. 'year' => '2010',
  4351. 'hour' => '04',
  4352. 'minute' => '19',
  4353. 'meridian' => 'AM'
  4354. )
  4355. );
  4356. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4357. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4358. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4359. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4360. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4361. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4362. }
  4363. /**
  4364. * test that datetime() and default values work.
  4365. *
  4366. * @return void
  4367. */
  4368. public function testDatetimeWithDefault() {
  4369. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4370. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4371. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4372. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4373. $result = $this->Form->dateTime('Contact.updated', array(
  4374. 'default' => '2009-06-01 11:15:30'
  4375. ));
  4376. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4377. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4378. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4379. }
  4380. /**
  4381. * testDateTime all zeros
  4382. *
  4383. * @return void
  4384. */
  4385. public function testDateTimeAllZeros() {
  4386. $result = $this->Form->dateTime('Contact.date', array(
  4387. 'timeFormat' => false,
  4388. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4389. 'value' => '0000-00-00'
  4390. ));
  4391. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4392. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4393. }
  4394. /**
  4395. * testDateTimeEmptyAsArray
  4396. *
  4397. * @return void
  4398. */
  4399. public function testDateTimeEmptyAsArray() {
  4400. $result = $this->Form->dateTime('Contact.date', array(
  4401. 'empty' => array(
  4402. 'day' => 'DAY',
  4403. 'month' => 'MONTH',
  4404. 'year' => 'YEAR',
  4405. 'hour' => 'HOUR',
  4406. 'minute' => 'MINUTE',
  4407. 'meridian' => false
  4408. )
  4409. ));
  4410. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4411. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4412. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4413. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4414. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4415. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4416. $result = $this->Form->dateTime('Contact.date', array(
  4417. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4418. ));
  4419. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4420. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4421. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4422. }
  4423. /**
  4424. * testFormDateTimeMulti method
  4425. *
  4426. * test multiple datetime element generation
  4427. *
  4428. * @return void
  4429. */
  4430. public function testFormDateTimeMulti() {
  4431. extract($this->dateRegex);
  4432. $result = $this->Form->dateTime('Contact.1.updated');
  4433. $this->assertContains('Contact[1][updated][month]', $result);
  4434. $this->assertContains('Contact[1][updated][day]', $result);
  4435. $this->assertContains('Contact[1][updated][year]', $result);
  4436. $this->assertContains('Contact[1][updated][hour]', $result);
  4437. $this->assertContains('Contact[1][updated][minute]', $result);
  4438. }
  4439. /**
  4440. * When changing the date format, the label should always focus the first select box when
  4441. * clicked.
  4442. *
  4443. * @return void
  4444. */
  4445. public function testDateTimeLabelIdMatchesFirstInput() {
  4446. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4447. $this->assertContains('<label>Date</label>', $result);
  4448. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4449. $this->assertContains('<label>Date</label>', $result);
  4450. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4451. $this->assertContains('<label>Date</label>', $result);
  4452. }
  4453. /**
  4454. * testMonth method
  4455. *
  4456. * @return void
  4457. */
  4458. public function testMonth() {
  4459. $result = $this->Form->month('Model.field', ['value' => '']);
  4460. $expected = array(
  4461. array('select' => array('name' => 'Model[field][month]')),
  4462. array('option' => array('value' => '', 'selected' => 'selected')),
  4463. '/option',
  4464. array('option' => array('value' => '01')),
  4465. date('F', strtotime('2008-01-01 00:00:00')),
  4466. '/option',
  4467. array('option' => array('value' => '02')),
  4468. date('F', strtotime('2008-02-01 00:00:00')),
  4469. '/option',
  4470. '*/select',
  4471. );
  4472. $this->assertHtml($expected, $result);
  4473. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4474. $expected = array(
  4475. array('select' => array('name' => 'Model[field][month]')),
  4476. array('option' => array('selected' => 'selected', 'value' => '')),
  4477. '/option',
  4478. array('option' => array('value' => '01')),
  4479. date('F', strtotime('2008-01-01 00:00:00')),
  4480. '/option',
  4481. array('option' => array('value' => '02')),
  4482. date('F', strtotime('2008-02-01 00:00:00')),
  4483. '/option',
  4484. '*/select',
  4485. );
  4486. $this->assertHtml($expected, $result);
  4487. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4488. $expected = array(
  4489. array('select' => array('name' => 'Model[field][month]')),
  4490. array('option' => array('selected' => 'selected', 'value' => '')),
  4491. '/option',
  4492. array('option' => array('value' => '01')),
  4493. '1',
  4494. '/option',
  4495. array('option' => array('value' => '02')),
  4496. '2',
  4497. '/option',
  4498. '*/select',
  4499. );
  4500. $this->assertHtml($expected, $result);
  4501. $monthNames = [
  4502. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4503. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4504. ];
  4505. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4506. $expected = array(
  4507. array('select' => array('name' => 'Model[field][month]')),
  4508. array('option' => array('value' => '')),
  4509. '/option',
  4510. array('option' => array('value' => '01', 'selected' => 'selected')),
  4511. 'Jan',
  4512. '/option',
  4513. array('option' => array('value' => '02')),
  4514. 'Feb',
  4515. '/option',
  4516. '*/select',
  4517. );
  4518. $this->assertHtml($expected, $result);
  4519. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4520. $result = $this->Form->month('Project.release');
  4521. $expected = array(
  4522. array('select' => array('name' => 'Project[release][month]')),
  4523. array('option' => array('value' => '')),
  4524. '/option',
  4525. array('option' => array('value' => '01')),
  4526. 'January',
  4527. '/option',
  4528. array('option' => array('value' => '02', 'selected' => 'selected')),
  4529. 'February',
  4530. '/option',
  4531. '*/select',
  4532. );
  4533. $this->assertHtml($expected, $result);
  4534. }
  4535. /**
  4536. * testDay method
  4537. *
  4538. * @return void
  4539. */
  4540. public function testDay() {
  4541. extract($this->dateRegex);
  4542. $result = $this->Form->day('Model.field', array('value' => false));
  4543. $expected = array(
  4544. array('select' => array('name' => 'Model[field][day]')),
  4545. array('option' => array('selected' => 'selected', 'value' => '')),
  4546. '/option',
  4547. array('option' => array('value' => '01')),
  4548. '1',
  4549. '/option',
  4550. array('option' => array('value' => '02')),
  4551. '2',
  4552. '/option',
  4553. $daysRegex,
  4554. '/select',
  4555. );
  4556. $this->assertHtml($expected, $result);
  4557. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4558. $result = $this->Form->day('Model.field');
  4559. $expected = array(
  4560. array('select' => array('name' => 'Model[field][day]')),
  4561. array('option' => array('value' => '')),
  4562. '/option',
  4563. array('option' => array('value' => '01')),
  4564. '1',
  4565. '/option',
  4566. array('option' => array('value' => '02')),
  4567. '2',
  4568. '/option',
  4569. $daysRegex,
  4570. array('option' => array('value' => '10', 'selected' => 'selected')),
  4571. '10',
  4572. '/option',
  4573. $daysRegex,
  4574. '/select',
  4575. );
  4576. $this->assertHtml($expected, $result);
  4577. $this->Form->request->data['Model']['field'] = '';
  4578. $result = $this->Form->day('Model.field', array('value' => '10'));
  4579. $expected = array(
  4580. array('select' => array('name' => 'Model[field][day]')),
  4581. array('option' => array('value' => '')),
  4582. '/option',
  4583. array('option' => array('value' => '01')),
  4584. '1',
  4585. '/option',
  4586. array('option' => array('value' => '02')),
  4587. '2',
  4588. '/option',
  4589. $daysRegex,
  4590. array('option' => array('value' => '10', 'selected' => 'selected')),
  4591. '10',
  4592. '/option',
  4593. $daysRegex,
  4594. '/select',
  4595. );
  4596. $this->assertHtml($expected, $result);
  4597. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4598. $result = $this->Form->day('Project.release');
  4599. $expected = array(
  4600. array('select' => array('name' => 'Project[release][day]')),
  4601. array('option' => array('value' => '')),
  4602. '/option',
  4603. array('option' => array('value' => '01')),
  4604. '1',
  4605. '/option',
  4606. array('option' => array('value' => '02')),
  4607. '2',
  4608. '/option',
  4609. $daysRegex,
  4610. array('option' => array('value' => '10', 'selected' => 'selected')),
  4611. '10',
  4612. '/option',
  4613. $daysRegex,
  4614. '/select',
  4615. );
  4616. $this->assertHtml($expected, $result);
  4617. }
  4618. /**
  4619. * testMinute method
  4620. *
  4621. * @return void
  4622. */
  4623. public function testMinute() {
  4624. extract($this->dateRegex);
  4625. $result = $this->Form->minute('Model.field', ['value' => '']);
  4626. $expected = array(
  4627. array('select' => array('name' => 'Model[field][minute]')),
  4628. array('option' => array('selected' => 'selected', 'value' => '')),
  4629. '/option',
  4630. array('option' => array('value' => '00')),
  4631. '00',
  4632. '/option',
  4633. array('option' => array('value' => '01')),
  4634. '01',
  4635. '/option',
  4636. array('option' => array('value' => '02')),
  4637. '02',
  4638. '/option',
  4639. $minutesRegex,
  4640. '/select',
  4641. );
  4642. $this->assertHtml($expected, $result);
  4643. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4644. $result = $this->Form->minute('Model.field');
  4645. $expected = array(
  4646. array('select' => array('name' => 'Model[field][minute]')),
  4647. array('option' => array('value' => '')),
  4648. '/option',
  4649. array('option' => array('value' => '00')),
  4650. '00',
  4651. '/option',
  4652. array('option' => array('value' => '01')),
  4653. '01',
  4654. '/option',
  4655. array('option' => array('value' => '02')),
  4656. '02',
  4657. '/option',
  4658. $minutesRegex,
  4659. array('option' => array('value' => '12', 'selected' => 'selected')),
  4660. '12',
  4661. '/option',
  4662. $minutesRegex,
  4663. '/select',
  4664. );
  4665. $this->assertHtml($expected, $result);
  4666. $this->Form->request->data['Model']['field'] = '';
  4667. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4668. $expected = array(
  4669. array('select' => array('name' => 'Model[field][minute]')),
  4670. array('option' => array('selected' => 'selected', 'value' => '')),
  4671. '/option',
  4672. array('option' => array('value' => '00')),
  4673. '00',
  4674. '/option',
  4675. array('option' => array('value' => '05')),
  4676. '05',
  4677. '/option',
  4678. array('option' => array('value' => '10')),
  4679. '10',
  4680. '/option',
  4681. $minutesRegex,
  4682. '/select',
  4683. );
  4684. $this->assertHtml($expected, $result);
  4685. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4686. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4687. $expected = array(
  4688. array('select' => array('name' => 'Model[field][minute]')),
  4689. array('option' => array('value' => '')),
  4690. '/option',
  4691. array('option' => array('value' => '00')),
  4692. '00',
  4693. '/option',
  4694. array('option' => array('value' => '05')),
  4695. '05',
  4696. '/option',
  4697. array('option' => array('value' => '10', 'selected' => 'selected')),
  4698. '10',
  4699. '/option',
  4700. $minutesRegex,
  4701. '/select',
  4702. );
  4703. $this->assertHtml($expected, $result);
  4704. }
  4705. /**
  4706. * Test generating an input for the meridian.
  4707. *
  4708. * @return void
  4709. */
  4710. public function testMeridian() {
  4711. extract($this->dateRegex);
  4712. $now = time();
  4713. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4714. $expected = [
  4715. array('select' => array('name' => 'Model[field][meridian]')),
  4716. array('option' => array('value' => '')),
  4717. '/option',
  4718. $meridianRegex,
  4719. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4720. date('a', $now),
  4721. '/option',
  4722. '*/select'
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. }
  4726. /**
  4727. * testHour method
  4728. *
  4729. * @return void
  4730. */
  4731. public function testHour() {
  4732. extract($this->dateRegex);
  4733. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  4734. $expected = array(
  4735. array('select' => array('name' => 'Model[field][hour]')),
  4736. array('option' => array('selected' => 'selected', 'value' => '')),
  4737. '/option',
  4738. array('option' => array('value' => '01')),
  4739. '1',
  4740. '/option',
  4741. array('option' => array('value' => '02')),
  4742. '2',
  4743. '/option',
  4744. $hoursRegex,
  4745. '/select',
  4746. );
  4747. $this->assertHtml($expected, $result);
  4748. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4749. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4750. $expected = array(
  4751. array('select' => array('name' => 'Model[field][hour]')),
  4752. array('option' => array('value' => '')),
  4753. '/option',
  4754. array('option' => array('value' => '01')),
  4755. '1',
  4756. '/option',
  4757. array('option' => array('value' => '02')),
  4758. '2',
  4759. '/option',
  4760. $hoursRegex,
  4761. array('option' => array('value' => '12', 'selected' => 'selected')),
  4762. '12',
  4763. '/option',
  4764. '/select',
  4765. );
  4766. $this->assertHtml($expected, $result);
  4767. $this->Form->request->data['Model']['field'] = '';
  4768. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4769. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4770. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4771. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4772. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4773. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4774. $expected = array(
  4775. array('select' => array('name' => 'Model[field][hour]')),
  4776. array('option' => array('value' => '')),
  4777. '/option',
  4778. array('option' => array('value' => '00', 'selected' => 'selected')),
  4779. '0',
  4780. '/option',
  4781. array('option' => array('value' => '01')),
  4782. '1',
  4783. '/option',
  4784. array('option' => array('value' => '02')),
  4785. '2',
  4786. '/option',
  4787. $hoursRegex,
  4788. '/select',
  4789. );
  4790. $this->assertHtml($expected, $result);
  4791. unset($this->Form->request->data['Model']['field']);
  4792. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4793. $thisHour = date('H');
  4794. $optValue = date('G');
  4795. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4796. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4797. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4798. $expected = array(
  4799. array('select' => array('name' => 'Model[field][hour]')),
  4800. array('option' => array('value' => '')),
  4801. '/option',
  4802. array('option' => array('value' => '00')),
  4803. '0',
  4804. '/option',
  4805. array('option' => array('value' => '01', 'selected' => 'selected')),
  4806. '1',
  4807. '/option',
  4808. array('option' => array('value' => '02')),
  4809. '2',
  4810. '/option',
  4811. $hoursRegex,
  4812. '/select',
  4813. );
  4814. $this->assertHtml($expected, $result);
  4815. }
  4816. /**
  4817. * testYear method
  4818. *
  4819. * @return void
  4820. */
  4821. public function testYear() {
  4822. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4823. $expected = array(
  4824. array('select' => array('name' => 'Model[field][year]')),
  4825. array('option' => array('selected' => 'selected', 'value' => '')),
  4826. '/option',
  4827. array('option' => array('value' => '2007')),
  4828. '2007',
  4829. '/option',
  4830. array('option' => array('value' => '2006')),
  4831. '2006',
  4832. '/option',
  4833. '/select',
  4834. );
  4835. $this->assertHtml($expected, $result);
  4836. $result = $this->Form->year('Model.field', [
  4837. 'value' => '',
  4838. 'minYear' => 2006,
  4839. 'maxYear' => 2007,
  4840. 'orderYear' => 'asc'
  4841. ]);
  4842. $expected = array(
  4843. array('select' => array('name' => 'Model[field][year]')),
  4844. array('option' => array('selected' => 'selected', 'value' => '')),
  4845. '/option',
  4846. array('option' => array('value' => '2006')),
  4847. '2006',
  4848. '/option',
  4849. array('option' => array('value' => '2007')),
  4850. '2007',
  4851. '/option',
  4852. '/select',
  4853. );
  4854. $this->assertHtml($expected, $result);
  4855. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4856. $result = $this->Form->year('Contact.published', [
  4857. 'empty' => false,
  4858. 'minYear' => 2006,
  4859. 'maxYear' => 2007,
  4860. ]);
  4861. $expected = array(
  4862. array('select' => array('name' => 'Contact[published][year]')),
  4863. array('option' => array('value' => '2007')),
  4864. '2007',
  4865. '/option',
  4866. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4867. '2006',
  4868. '/option',
  4869. '/select',
  4870. );
  4871. $this->assertHtml($expected, $result);
  4872. }
  4873. /**
  4874. * testYearAutoExpandRange method
  4875. *
  4876. * @return void
  4877. */
  4878. public function testYearAutoExpandRange() {
  4879. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4880. $result = $this->Form->year('User.birthday');
  4881. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4882. $result = $matches[1];
  4883. $expected = range(date('Y') + 5, 1930);
  4884. $this->assertEquals($expected, $result);
  4885. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4886. $result = $this->Form->year('Project.release');
  4887. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4888. $result = $matches[1];
  4889. $expected = range(2050, date('Y') - 5);
  4890. $this->assertEquals($expected, $result);
  4891. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4892. $result = $this->Form->year('Project.release', [
  4893. 'minYear' => 1890,
  4894. 'maxYear' => 1900
  4895. ]);
  4896. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4897. $result = $matches[1];
  4898. $expected = range(1900, 1881);
  4899. $this->assertEquals($expected, $result);
  4900. }
  4901. /**
  4902. * Test that input() accepts the type of date and passes options in.
  4903. *
  4904. * @return void
  4905. */
  4906. public function testInputDate() {
  4907. $this->Form->request->data = array(
  4908. 'month_year' => array('month' => date('m')),
  4909. );
  4910. $this->Form->create($this->article);
  4911. $result = $this->Form->input('month_year', array(
  4912. 'label' => false,
  4913. 'type' => 'date',
  4914. 'minYear' => 2006,
  4915. 'maxYear' => 2008
  4916. ));
  4917. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4918. $this->assertNotContains('value="2008" selected="selected"', $result);
  4919. }
  4920. /**
  4921. * Test the label option being set to false.
  4922. *
  4923. * @return void
  4924. */
  4925. public function testInputLabelFalse() {
  4926. $this->Form->create($this->article);
  4927. $result = $this->Form->input('title', ['label' => false]);
  4928. $expected = [
  4929. 'div' => ['class' => 'input text required'],
  4930. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  4931. '/div'
  4932. ];
  4933. $this->assertHtml($expected, $result);
  4934. }
  4935. /**
  4936. * testInputDateMaxYear method
  4937. *
  4938. * Let's say we want to only allow users born from 2006 to 2008 to register
  4939. * This being the first singup page, we still don't have any data
  4940. *
  4941. * @return void
  4942. */
  4943. public function testInputDateMaxYear() {
  4944. $this->Form->request->data = [];
  4945. $this->Form->create($this->article);
  4946. $result = $this->Form->input('birthday', array(
  4947. 'label' => false,
  4948. 'type' => 'date',
  4949. 'minYear' => 2006,
  4950. 'maxYear' => 2008
  4951. ));
  4952. $this->assertContains('value="2008" selected="selected"', $result);
  4953. }
  4954. /**
  4955. * testTextArea method
  4956. *
  4957. * @return void
  4958. */
  4959. public function testTextArea() {
  4960. $this->Form->request->data = array('field' => 'some test data');
  4961. $result = $this->Form->textarea('field');
  4962. $expected = array(
  4963. 'textarea' => array('name' => 'field'),
  4964. 'some test data',
  4965. '/textarea',
  4966. );
  4967. $this->assertHtml($expected, $result);
  4968. $result = $this->Form->textarea('user.bio');
  4969. $expected = array(
  4970. 'textarea' => array('name' => 'user[bio]'),
  4971. '/textarea',
  4972. );
  4973. $this->assertHtml($expected, $result);
  4974. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4975. $result = $this->Form->textarea('field');
  4976. $expected = array(
  4977. 'textarea' => array('name' => 'field'),
  4978. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4979. '/textarea',
  4980. );
  4981. $this->assertHtml($expected, $result);
  4982. $this->Form->request->data = [
  4983. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4984. ];
  4985. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4986. $expected = array(
  4987. 'textarea' => array('name' => 'Model[field]'),
  4988. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4989. '/textarea',
  4990. );
  4991. $this->assertHtml($expected, $result);
  4992. $result = $this->Form->textarea('0.OtherModel.field');
  4993. $expected = array(
  4994. 'textarea' => array('name' => '0[OtherModel][field]'),
  4995. '/textarea'
  4996. );
  4997. $this->assertHtml($expected, $result);
  4998. }
  4999. /**
  5000. * testTextAreaWithStupidCharacters method
  5001. *
  5002. * test text area with non-ascii characters
  5003. *
  5004. * @return void
  5005. */
  5006. public function testTextAreaWithStupidCharacters() {
  5007. $result = $this->Form->textarea('Post.content', [
  5008. 'value' => "GREAT®",
  5009. 'rows' => '15',
  5010. 'cols' => '75'
  5011. ]);
  5012. $expected = [
  5013. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5014. 'GREAT®',
  5015. '/textarea',
  5016. ];
  5017. $this->assertHtml($expected, $result);
  5018. }
  5019. /**
  5020. * Test textareas waxlength read from schema.
  5021. *
  5022. * @return void
  5023. */
  5024. public function testTextAreaMaxLength() {
  5025. $this->Form->create([
  5026. 'schema' => [
  5027. 'stuff' => ['type' => 'string', 'length' => 10],
  5028. ]
  5029. ]);
  5030. $result = $this->Form->input('other', array('type' => 'textarea'));
  5031. $expected = array(
  5032. 'div' => array('class' => 'input textarea'),
  5033. 'label' => array('for' => 'other'),
  5034. 'Other',
  5035. '/label',
  5036. 'textarea' => array('name' => 'other', 'id' => 'other'),
  5037. '/textarea',
  5038. '/div'
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $result = $this->Form->input('stuff', array('type' => 'textarea'));
  5042. $expected = array(
  5043. 'div' => array('class' => 'input textarea'),
  5044. 'label' => array('for' => 'stuff'),
  5045. 'Stuff',
  5046. '/label',
  5047. 'textarea' => array('name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff'),
  5048. '/textarea',
  5049. '/div'
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. }
  5053. /**
  5054. * testHiddenField method
  5055. *
  5056. * @return void
  5057. */
  5058. public function testHiddenField() {
  5059. $this->article['errors'] = [
  5060. 'field' => true
  5061. ];
  5062. $this->Form->request->data['field'] = 'test';
  5063. $this->Form->create($this->article);
  5064. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5065. $expected = array(
  5066. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'));
  5067. $this->assertHtml($expected, $result);
  5068. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5069. $expected = [
  5070. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5071. ];
  5072. $this->assertHtml($expected, $result);
  5073. }
  5074. /**
  5075. * testFileUploadField method
  5076. *
  5077. * @return void
  5078. */
  5079. public function testFileUploadField() {
  5080. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5081. $result = $this->Form->file('Model.upload');
  5082. $this->assertHtml($expected, $result);
  5083. $this->Form->request->data['Model']['upload'] = [
  5084. 'name' => '', 'type' => '', 'tmp_name' => '',
  5085. 'error' => 4, 'size' => 0
  5086. ];
  5087. $result = $this->Form->file('Model.upload');
  5088. $this->assertHtml($expected, $result);
  5089. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5090. $result = $this->Form->file('Model.upload');
  5091. $this->assertHtml($expected, $result);
  5092. }
  5093. /**
  5094. * test File upload input on a model not used in create();
  5095. *
  5096. * @return void
  5097. */
  5098. public function testFileUploadOnOtherModel() {
  5099. $this->Form->create($this->article, array('type' => 'file'));
  5100. $result = $this->Form->file('ValidateProfile.city');
  5101. $expected = array(
  5102. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5103. );
  5104. $this->assertHtml($expected, $result);
  5105. }
  5106. /**
  5107. * testButton method
  5108. *
  5109. * @return void
  5110. */
  5111. public function testButton() {
  5112. $result = $this->Form->button('Hi');
  5113. $expected = array('button' => array('type' => 'submit'), 'Hi', '/button');
  5114. $this->assertHtml($expected, $result);
  5115. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5116. $expected = array('button' => array('type' => 'reset'), 'Clear Form >', '/button');
  5117. $this->assertHtml($expected, $result);
  5118. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5119. $expected = array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button');
  5120. $this->assertHtml($expected, $result);
  5121. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5122. $expected = array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button');
  5123. $this->assertHtml($expected, $result);
  5124. $result = $this->Form->button('No type', array('type' => false));
  5125. $expected = array('button' => array(), 'No type', '/button');
  5126. $this->assertHtml($expected, $result);
  5127. $result = $this->Form->button('Upload Text', array(
  5128. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5129. 'escape' => false
  5130. ));
  5131. $this->assertNotRegExp('/\&039/', $result);
  5132. }
  5133. /**
  5134. * Test that button() makes unlocked fields by default.
  5135. *
  5136. * @return void
  5137. */
  5138. public function testButtonUnlockedByDefault() {
  5139. $this->Form->request->params['_csrfToken'] = 'secured';
  5140. $this->Form->button('Save', array('name' => 'save'));
  5141. $this->Form->button('Clear');
  5142. $result = $this->Form->unlockField();
  5143. $this->assertEquals(array('save'), $result);
  5144. }
  5145. /**
  5146. * testPostButton method
  5147. *
  5148. * @return void
  5149. */
  5150. public function testPostButton() {
  5151. $result = $this->Form->postButton('Hi', '/controller/action');
  5152. $expected = array(
  5153. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5154. 'div' => array('style' => 'display:none;'),
  5155. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5156. '/div',
  5157. 'button' => array('type' => 'submit'),
  5158. 'Hi',
  5159. '/button',
  5160. '/form'
  5161. );
  5162. $this->assertHtml($expected, $result);
  5163. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5164. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5165. }
  5166. /**
  5167. * Test using postButton with N dimensional data.
  5168. *
  5169. * @return void
  5170. */
  5171. public function testPostButtonNestedData() {
  5172. $data = array(
  5173. 'one' => array(
  5174. 'two' => array(
  5175. 3, 4, 5
  5176. )
  5177. )
  5178. );
  5179. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5180. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5181. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5182. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5183. }
  5184. /**
  5185. * Test that postButton adds _Token fields.
  5186. *
  5187. * @return void
  5188. */
  5189. public function testSecurePostButton() {
  5190. $this->Form->request->params['_csrfToken'] = 'testkey';
  5191. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5192. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5193. $expected = array(
  5194. 'form' => array(
  5195. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5196. ),
  5197. array('div' => array('style' => 'display:none;')),
  5198. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5199. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5200. '/div',
  5201. 'button' => array('type' => 'submit'),
  5202. 'Delete',
  5203. '/button',
  5204. array('div' => array('style' => 'display:none;')),
  5205. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5206. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5207. '/div',
  5208. '/form',
  5209. );
  5210. $this->assertHtml($expected, $result);
  5211. }
  5212. /**
  5213. * testPostLink method
  5214. *
  5215. * @return void
  5216. */
  5217. public function testPostLink() {
  5218. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5219. $expected = array(
  5220. 'form' => array(
  5221. 'method' => 'post', 'action' => '/posts/delete/1',
  5222. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5223. ),
  5224. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5225. '/form',
  5226. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5227. 'Delete',
  5228. '/a'
  5229. );
  5230. $this->assertHtml($expected, $result);
  5231. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5232. $expected = array(
  5233. 'form' => array(
  5234. 'method' => 'post', 'action' => '/posts/delete/1',
  5235. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5236. ),
  5237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5238. '/form',
  5239. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5240. 'Delete',
  5241. '/a'
  5242. );
  5243. $this->assertHtml($expected, $result);
  5244. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('confirm' => 'Confirm?'));
  5245. $expected = array(
  5246. 'form' => array(
  5247. 'method' => 'post', 'action' => '/posts/delete/1',
  5248. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5249. ),
  5250. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5251. '/form',
  5252. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5253. 'Delete',
  5254. '/a'
  5255. );
  5256. $this->assertHtml($expected, $result);
  5257. $result = $this->Form->postLink(
  5258. 'Delete',
  5259. '/posts/delete/1',
  5260. array('escape' => false, 'confirm' => '\'Confirm\' this "deletion"?')
  5261. );
  5262. $expected = array(
  5263. 'form' => array(
  5264. 'method' => 'post', 'action' => '/posts/delete/1',
  5265. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5266. ),
  5267. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5268. '/form',
  5269. '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;/'),
  5270. 'Delete',
  5271. '/a'
  5272. );
  5273. $this->assertHtml($expected, $result);
  5274. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5275. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5276. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5277. $expected = array(
  5278. 'form' => array(
  5279. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5280. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5281. ),
  5282. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5283. '/form',
  5284. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5285. 'Delete',
  5286. '/a'
  5287. );
  5288. $this->assertHtml($expected, $result);
  5289. $result = $this->Form->postLink(
  5290. '',
  5291. array('controller' => 'items', 'action' => 'delete', 10),
  5292. array('class' => 'btn btn-danger', 'escape' => false, 'confirm' => 'Confirm thing')
  5293. );
  5294. $expected = array(
  5295. 'form' => array(
  5296. 'method' => 'post', 'action' => '/items/delete/10',
  5297. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5298. ),
  5299. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5300. '/form',
  5301. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5302. '/a'
  5303. );
  5304. $this->assertHtml($expected, $result);
  5305. }
  5306. /**
  5307. * Test that security hashes for postLink include the url.
  5308. *
  5309. * @return void
  5310. */
  5311. public function testPostLinkSecurityHash() {
  5312. $hash = Security::hash(
  5313. '/posts/delete/1' .
  5314. serialize(array()) .
  5315. '' .
  5316. Security::salt()
  5317. );
  5318. $hash .= '%3A';
  5319. $this->Form->request->params['_Token']['key'] = 'test';
  5320. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5321. $expected = array(
  5322. 'form' => array(
  5323. 'method' => 'post', 'action' => '/posts/delete/1',
  5324. 'name', 'style' => 'display:none;'
  5325. ),
  5326. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5327. 'div' => array('style' => 'display:none;'),
  5328. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash)),
  5329. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5330. '/div',
  5331. '/form',
  5332. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5333. 'Delete',
  5334. '/a'
  5335. );
  5336. $this->assertHtml($expected, $result);
  5337. }
  5338. /**
  5339. * Test using postLink with N dimensional data.
  5340. *
  5341. * @return void
  5342. */
  5343. public function testPostLinkNestedData() {
  5344. $data = array(
  5345. 'one' => array(
  5346. 'two' => array(
  5347. 3, 4, 5
  5348. )
  5349. )
  5350. );
  5351. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5352. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5353. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5354. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5355. }
  5356. /**
  5357. * test creating postLinks after a GET form.
  5358. *
  5359. * @return void
  5360. */
  5361. public function testPostLinkAfterGetForm() {
  5362. $this->Form->request->params['_csrfToken'] = 'testkey';
  5363. $this->Form->request->params['_Token'] = 'val';
  5364. $this->Form->create($this->article, array('type' => 'get'));
  5365. $this->Form->end();
  5366. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5367. $expected = array(
  5368. 'form' => array(
  5369. 'method' => 'post', 'action' => '/posts/delete/1',
  5370. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5371. ),
  5372. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5373. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5374. 'div' => array('style' => 'display:none;'),
  5375. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5376. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5377. '/div',
  5378. '/form',
  5379. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5380. 'Delete',
  5381. '/a'
  5382. );
  5383. $this->assertHtml($expected, $result);
  5384. }
  5385. /**
  5386. * Test that postLink adds form tags to view block
  5387. *
  5388. * @return void
  5389. */
  5390. public function testPostLinkFormBuffer() {
  5391. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
  5392. $expected = array(
  5393. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5394. 'Delete',
  5395. '/a'
  5396. );
  5397. $this->assertHtml($expected, $result);
  5398. $result = $this->View->fetch('postLink');
  5399. $expected = array(
  5400. 'form' => array(
  5401. 'method' => 'post', 'action' => '/posts/delete/1',
  5402. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5403. ),
  5404. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5405. '/form'
  5406. );
  5407. $this->assertHtml($expected, $result);
  5408. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5409. array('block' => true, 'method' => 'DELETE')
  5410. );
  5411. $expected = array(
  5412. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5413. 'Delete',
  5414. '/a'
  5415. );
  5416. $this->assertHtml($expected, $result);
  5417. $result = $this->View->fetch('postLink');
  5418. $expected = array(
  5419. 'form' => array(
  5420. 'method' => 'post', 'action' => '/posts/delete/1',
  5421. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5422. ),
  5423. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5424. '/form',
  5425. array(
  5426. 'form' => array(
  5427. 'method' => 'post', 'action' => '/posts/delete/2',
  5428. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5429. ),
  5430. ),
  5431. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5432. '/form'
  5433. );
  5434. $this->assertHtml($expected, $result);
  5435. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5436. $expected = array(
  5437. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5438. 'Delete',
  5439. '/a'
  5440. );
  5441. $this->assertHtml($expected, $result);
  5442. $result = $this->View->fetch('foobar');
  5443. $expected = array(
  5444. 'form' => array(
  5445. 'method' => 'post', 'action' => '/posts/delete/1',
  5446. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5447. ),
  5448. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5449. '/form'
  5450. );
  5451. $this->assertHtml($expected, $result);
  5452. }
  5453. /**
  5454. * testSubmitButton method
  5455. *
  5456. * @return void
  5457. */
  5458. public function testSubmitButton() {
  5459. $result = $this->Form->submit('');
  5460. $expected = array(
  5461. 'div' => array('class' => 'submit'),
  5462. 'input' => array('type' => 'submit', 'value' => ''),
  5463. '/div'
  5464. );
  5465. $this->assertHtml($expected, $result);
  5466. $result = $this->Form->submit('Test Submit');
  5467. $expected = array(
  5468. 'div' => array('class' => 'submit'),
  5469. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5470. '/div'
  5471. );
  5472. $this->assertHtml($expected, $result);
  5473. $result = $this->Form->submit('Next >');
  5474. $expected = array(
  5475. 'div' => array('class' => 'submit'),
  5476. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5477. '/div'
  5478. );
  5479. $this->assertHtml($expected, $result);
  5480. $result = $this->Form->submit('Next >', array('escape' => false));
  5481. $expected = array(
  5482. 'div' => array('class' => 'submit'),
  5483. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5484. '/div'
  5485. );
  5486. $this->assertHtml($expected, $result);
  5487. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5488. $expected = array(
  5489. 'div' => array('class' => 'submit'),
  5490. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5491. '/div'
  5492. );
  5493. $this->assertHtml($expected, $result);
  5494. }
  5495. /**
  5496. * test image submit types.
  5497. *
  5498. * @return void
  5499. */
  5500. public function testSubmitImage() {
  5501. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5502. $expected = array(
  5503. 'div' => array('class' => 'submit'),
  5504. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5505. '/div'
  5506. );
  5507. $this->assertHtml($expected, $result);
  5508. $result = $this->Form->submit('/relative/cake.power.gif');
  5509. $expected = array(
  5510. 'div' => array('class' => 'submit'),
  5511. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5512. '/div'
  5513. );
  5514. $this->assertHtml($expected, $result);
  5515. $result = $this->Form->submit('cake.power.gif');
  5516. $expected = array(
  5517. 'div' => array('class' => 'submit'),
  5518. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5519. '/div'
  5520. );
  5521. $this->assertHtml($expected, $result);
  5522. $result = $this->Form->submit('Not.an.image');
  5523. $expected = array(
  5524. 'div' => array('class' => 'submit'),
  5525. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5526. '/div'
  5527. );
  5528. $this->assertHtml($expected, $result);
  5529. }
  5530. /**
  5531. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5532. * be submitted at a time.
  5533. *
  5534. * @return void
  5535. */
  5536. public function testSubmitUnlockedByDefault() {
  5537. $this->Form->request->params['_Token'] = 'secured';
  5538. $this->Form->submit('Go go');
  5539. $this->Form->submit('Save', array('name' => 'save'));
  5540. $result = $this->Form->unlockField();
  5541. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5542. }
  5543. /**
  5544. * Test submit image with timestamps.
  5545. *
  5546. * @return void
  5547. */
  5548. public function testSubmitImageTimestamp() {
  5549. Configure::write('Asset.timestamp', 'force');
  5550. $result = $this->Form->submit('cake.power.gif');
  5551. $expected = array(
  5552. 'div' => array('class' => 'submit'),
  5553. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5554. '/div'
  5555. );
  5556. $this->assertHtml($expected, $result);
  5557. }
  5558. /**
  5559. * test that datetime() works with GET style forms.
  5560. *
  5561. * @return void
  5562. */
  5563. public function testDateTimeWithGetForms() {
  5564. extract($this->dateRegex);
  5565. $this->Form->create($this->article, array('type' => 'get'));
  5566. $result = $this->Form->datetime('created');
  5567. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5568. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5569. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5570. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5571. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5572. }
  5573. /**
  5574. * testForMagicInputNonExistingNorValidated method
  5575. *
  5576. * @return void
  5577. */
  5578. public function testForMagicInputNonExistingNorValidated() {
  5579. $result = $this->Form->create($this->article);
  5580. $this->Form->templates(['inputContainer' => '{{content}}']);
  5581. $result = $this->Form->input('non_existing_nor_validated');
  5582. $expected = array(
  5583. 'label' => array('for' => 'non-existing-nor-validated'),
  5584. 'Non Existing Nor Validated',
  5585. '/label',
  5586. 'input' => array(
  5587. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5588. 'id' => 'non-existing-nor-validated'
  5589. )
  5590. );
  5591. $this->assertHtml($expected, $result);
  5592. $result = $this->Form->input('non_existing_nor_validated', array(
  5593. 'val' => 'my value'
  5594. ));
  5595. $expected = array(
  5596. 'label' => array('for' => 'non-existing-nor-validated'),
  5597. 'Non Existing Nor Validated',
  5598. '/label',
  5599. 'input' => array(
  5600. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5601. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5602. )
  5603. );
  5604. $this->assertHtml($expected, $result);
  5605. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5606. $result = $this->Form->input('non_existing_nor_validated');
  5607. $expected = array(
  5608. 'label' => array('for' => 'non-existing-nor-validated'),
  5609. 'Non Existing Nor Validated',
  5610. '/label',
  5611. 'input' => array(
  5612. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5613. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5614. )
  5615. );
  5616. $this->assertHtml($expected, $result);
  5617. }
  5618. /**
  5619. * testFormMagicInputLabel method
  5620. *
  5621. * @return void
  5622. */
  5623. public function testFormMagicInputLabel() {
  5624. TableRegistry::get('Contacts', [
  5625. 'className' => __NAMESPACE__ . '\ContactsTable'
  5626. ]);
  5627. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5628. $this->Form->templates(['inputContainer' => '{{content}}']);
  5629. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5630. $expected = array(
  5631. 'label' => array('for' => 'contacts-name'),
  5632. 'My label',
  5633. '/label',
  5634. 'input' => array(
  5635. 'type' => 'text', 'name' => 'Contacts[name]',
  5636. 'id' => 'contacts-name', 'maxlength' => '255'
  5637. )
  5638. );
  5639. $this->assertHtml($expected, $result);
  5640. $result = $this->Form->input('name', array(
  5641. 'label' => array('class' => 'mandatory')
  5642. ));
  5643. $expected = array(
  5644. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5645. 'Name',
  5646. '/label',
  5647. 'input' => array(
  5648. 'type' => 'text', 'name' => 'name',
  5649. 'id' => 'name', 'maxlength' => '255'
  5650. )
  5651. );
  5652. $this->assertHtml($expected, $result);
  5653. $result = $this->Form->input('name', array(
  5654. 'div' => false,
  5655. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5656. ));
  5657. $expected = array(
  5658. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5659. 'My label',
  5660. '/label',
  5661. 'input' => array(
  5662. 'type' => 'text', 'name' => 'name',
  5663. 'id' => 'name', 'maxlength' => '255'
  5664. )
  5665. );
  5666. $this->assertHtml($expected, $result);
  5667. $result = $this->Form->input('Contact.name', array(
  5668. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5669. ));
  5670. $expected = array(
  5671. 'label' => array('for' => 'my_id'),
  5672. 'Name',
  5673. '/label',
  5674. 'input' => array(
  5675. 'type' => 'text', 'name' => 'Contact[name]',
  5676. 'id' => 'my_id', 'maxlength' => '255'
  5677. )
  5678. );
  5679. $this->assertHtml($expected, $result);
  5680. $result = $this->Form->input('1.id');
  5681. $expected = array('input' => array(
  5682. 'type' => 'hidden', 'name' => '1[id]',
  5683. 'id' => '1-id'
  5684. ));
  5685. $this->assertHtml($expected, $result);
  5686. $result = $this->Form->input("1.name");
  5687. $expected = array(
  5688. 'label' => array('for' => '1-name'),
  5689. 'Name',
  5690. '/label',
  5691. 'input' => array(
  5692. 'type' => 'text', 'name' => '1[name]',
  5693. 'id' => '1-name', 'maxlength' => '255'
  5694. )
  5695. );
  5696. $this->assertHtml($expected, $result);
  5697. }
  5698. /**
  5699. * testFormEnd method
  5700. *
  5701. * @return void
  5702. */
  5703. public function testFormEnd() {
  5704. $this->assertEquals('</form>', $this->Form->end());
  5705. }
  5706. /**
  5707. * Test the generation of fields for a multi record form.
  5708. *
  5709. * @return void
  5710. */
  5711. public function testMultiRecordForm() {
  5712. $this->loadFixtures('Articles', 'Comments');
  5713. $articles = TableRegistry::get('Articles');
  5714. $articles->hasMany('Comments');
  5715. $comment = new Entity(['comment' => 'Value']);
  5716. $article = new Article(['comments' => [$comment]]);
  5717. $this->Form->create([$article]);
  5718. $result = $this->Form->input('0.comments.1.comment');
  5719. $expected = array(
  5720. 'div' => array('class' => 'input textarea'),
  5721. 'label' => array('for' => '0-comments-1-comment'),
  5722. 'Comment',
  5723. '/label',
  5724. 'textarea' => array(
  5725. 'name',
  5726. 'id' => '0-comments-1-comment',
  5727. ),
  5728. '/textarea',
  5729. '/div'
  5730. );
  5731. $this->assertHtml($expected, $result);
  5732. $result = $this->Form->input('0.comments.0.comment');
  5733. $expected = array(
  5734. 'div' => array('class' => 'input textarea'),
  5735. 'label' => array('for' => '0-comments-0-comment'),
  5736. 'Comment',
  5737. '/label',
  5738. 'textarea' => array(
  5739. 'name',
  5740. 'id' => '0-comments-0-comment'
  5741. ),
  5742. 'Value',
  5743. '/textarea',
  5744. '/div'
  5745. );
  5746. $this->assertHtml($expected, $result);
  5747. $comment->errors('comment', ['Not valid']);
  5748. $result = $this->Form->input('0.comments.0.comment');
  5749. $expected = array(
  5750. 'div' => array('class' => 'input textarea error'),
  5751. 'label' => array('for' => '0-comments-0-comment'),
  5752. 'Comment',
  5753. '/label',
  5754. 'textarea' => array(
  5755. 'name',
  5756. 'class' => 'form-error',
  5757. 'id' => '0-comments-0-comment'
  5758. ),
  5759. 'Value',
  5760. '/textarea',
  5761. array('div' => array('class' => 'error-message')),
  5762. 'Not valid',
  5763. '/div',
  5764. '/div'
  5765. );
  5766. $this->assertHtml($expected, $result);
  5767. TableRegistry::get('Comments')
  5768. ->validator('default')
  5769. ->allowEmpty('comment', false);
  5770. $result = $this->Form->input('0.comments.1.comment');
  5771. $expected = array(
  5772. 'div' => array('class' => 'input textarea required'),
  5773. 'label' => array('for' => '0-comments-1-comment'),
  5774. 'Comment',
  5775. '/label',
  5776. 'textarea' => array(
  5777. 'name',
  5778. 'required' => 'required',
  5779. 'id' => '0-comments-1-comment'
  5780. ),
  5781. '/textarea',
  5782. '/div'
  5783. );
  5784. $this->assertHtml($expected, $result);
  5785. }
  5786. /**
  5787. * test that some html5 inputs + FormHelper::__call() work
  5788. *
  5789. * @return void
  5790. */
  5791. public function testHtml5Inputs() {
  5792. $result = $this->Form->email('User.email');
  5793. $expected = array(
  5794. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5795. );
  5796. $this->assertHtml($expected, $result);
  5797. $result = $this->Form->search('User.query');
  5798. $expected = array(
  5799. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5800. );
  5801. $this->assertHtml($expected, $result);
  5802. $result = $this->Form->search('User.query', array('value' => 'test'));
  5803. $expected = array(
  5804. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5805. );
  5806. $this->assertHtml($expected, $result);
  5807. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5808. $expected = array(
  5809. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5810. );
  5811. $this->assertHtml($expected, $result);
  5812. }
  5813. /**
  5814. * Test accessing html5 inputs through input().
  5815. *
  5816. * @return void
  5817. */
  5818. public function testHtml5InputWithInput() {
  5819. $this->Form->create();
  5820. $this->Form->templates(['inputContainer' => '{{content}}']);
  5821. $result = $this->Form->input('website', array(
  5822. 'type' => 'url',
  5823. 'val' => 'http://domain.tld',
  5824. 'label' => false
  5825. ));
  5826. $expected = array(
  5827. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5828. );
  5829. $this->assertHtml($expected, $result);
  5830. }
  5831. /**
  5832. * Test errors when field name is missing.
  5833. *
  5834. * @expectedException \Cake\Core\Exception\Exception
  5835. * @return void
  5836. */
  5837. public function testHtml5InputException() {
  5838. $this->Form->email();
  5839. }
  5840. /**
  5841. * Tests that formhelper sets required attributes.
  5842. *
  5843. * @return void
  5844. */
  5845. public function testRequiredAttribute() {
  5846. $this->article['required'] = [
  5847. 'title' => true,
  5848. 'body' => false,
  5849. ];
  5850. $this->Form->create($this->article);
  5851. $result = $this->Form->input('title');
  5852. $expected = array(
  5853. 'div' => array('class' => 'input text required'),
  5854. 'label' => array('for' => 'title'),
  5855. 'Title',
  5856. '/label',
  5857. 'input' => array(
  5858. 'type' => 'text',
  5859. 'name' => 'title',
  5860. 'id' => 'title',
  5861. 'required' => 'required',
  5862. ),
  5863. '/div'
  5864. );
  5865. $this->assertHtml($expected, $result);
  5866. $result = $this->Form->input('title', ['required' => false]);
  5867. $this->assertNotContains('required', $result);
  5868. $result = $this->Form->input('body');
  5869. $expected = array(
  5870. 'div' => array('class' => 'input text'),
  5871. 'label' => array('for' => 'body'),
  5872. 'Body',
  5873. '/label',
  5874. 'input' => array(
  5875. 'type' => 'text',
  5876. 'name' => 'body',
  5877. 'id' => 'body',
  5878. ),
  5879. '/div'
  5880. );
  5881. $this->assertHtml($expected, $result);
  5882. $result = $this->Form->input('body', ['required' => true]);
  5883. $this->assertContains('required', $result);
  5884. }
  5885. /**
  5886. * Tests that it is possible to put inputs outside of the label
  5887. *
  5888. * @return void
  5889. */
  5890. public function testInputsNotNested() {
  5891. $this->Form->templates([
  5892. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  5893. 'formGroup' => '{{input}}{{label}}',
  5894. ]);
  5895. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  5896. $expected = [
  5897. 'div' => ['class' => 'input checkbox'],
  5898. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5899. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5900. 'label' => ['for' => 'foo'],
  5901. 'Foo',
  5902. '/label',
  5903. '/div'
  5904. ];
  5905. $this->assertHtml($expected, $result);
  5906. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  5907. $expected = [
  5908. 'div' => ['class' => 'input checkbox'],
  5909. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5910. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5911. '/div'
  5912. ];
  5913. $this->assertHtml($expected, $result);
  5914. $result = $this->Form->input('confirm', [
  5915. 'type' => 'radio',
  5916. 'options' => ['Y' => 'Yes', 'N' => 'No']
  5917. ]);
  5918. $expected = [
  5919. 'div' => ['class' => 'input radio'],
  5920. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  5921. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  5922. ['label' => ['for' => 'confirm-y']],
  5923. 'Yes',
  5924. '/label',
  5925. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  5926. ['label' => ['for' => 'confirm-n']],
  5927. 'No',
  5928. '/label',
  5929. '<label',
  5930. 'Confirm',
  5931. '/label',
  5932. '/div',
  5933. ];
  5934. $this->assertHtml($expected, $result);
  5935. $result = $this->Form->select('category', ['1', '2'], [
  5936. 'multiple' => 'checkbox',
  5937. 'name' => 'fish',
  5938. ]);
  5939. $expected = [
  5940. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5941. ['div' => ['class' => 'checkbox']],
  5942. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5943. ['label' => ['for' => 'fish-0']],
  5944. '1',
  5945. '/label',
  5946. '/div',
  5947. ['div' => ['class' => 'checkbox']],
  5948. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5949. ['label' => ['for' => 'fish-1']],
  5950. '2',
  5951. '/label',
  5952. '/div'
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. }
  5956. /**
  5957. * Test that *Container templates are used by input.
  5958. *
  5959. * @return void
  5960. */
  5961. public function testInputContainerTemplates() {
  5962. $this->Form->templates([
  5963. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  5964. 'radioContainer' => '<div class="rad">{{content}}</div>',
  5965. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  5966. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  5967. ]);
  5968. $this->article['errors'] = [
  5969. 'Article' => ['published' => 'error message']
  5970. ];
  5971. $this->Form->create($this->article);
  5972. $result = $this->Form->input('accept', [
  5973. 'type' => 'checkbox'
  5974. ]);
  5975. $expected = [
  5976. 'div' => ['class' => 'check'],
  5977. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  5978. 'label' => ['for' => 'accept'],
  5979. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  5980. 'Accept',
  5981. '/label',
  5982. '/div'
  5983. ];
  5984. $this->assertHtml($expected, $result);
  5985. $result = $this->Form->input('accept', [
  5986. 'type' => 'radio',
  5987. 'options' => ['Y', 'N']
  5988. ]);
  5989. $this->assertContains('<div class="rad">', $result);
  5990. $result = $this->Form->input('Article.published', [
  5991. 'type' => 'radio',
  5992. 'options' => ['Y', 'N']
  5993. ]);
  5994. $this->assertContains('<div class="rad err">', $result);
  5995. $result = $this->Form->input('Article.created', [
  5996. 'type' => 'datetime',
  5997. ]);
  5998. $this->assertContains('<div class="dt">', $result);
  5999. }
  6000. /**
  6001. * Test resetting templates.
  6002. *
  6003. * @return void
  6004. */
  6005. public function testResetTemplates() {
  6006. $this->Form->templates(['input' => '<input>']);
  6007. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  6008. $this->assertNull($this->Form->resetTemplates());
  6009. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  6010. }
  6011. /**
  6012. * Test the context method.
  6013. *
  6014. * @return void
  6015. */
  6016. public function testContext() {
  6017. $result = $this->Form->context();
  6018. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  6019. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  6020. $this->assertSame($mock, $this->Form->context($mock));
  6021. $this->assertSame($mock, $this->Form->context());
  6022. }
  6023. }