FormHelperTest.php 245 KB

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