FormHelperTest.php 265 KB

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