FormHelperTest.php 225 KB

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