FormHelperTest.php 313 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Chronos\ChronosTime;
  19. use Cake\Collection\Collection;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Exception\CakeException;
  22. use Cake\Database\Type\EnumType;
  23. use Cake\Form\Form;
  24. use Cake\Http\ServerRequest;
  25. use Cake\I18n\Date;
  26. use Cake\I18n\DateTime;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\Table;
  29. use Cake\Routing\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use Cake\Validation\Validator;
  33. use Cake\View\Form\EntityContext;
  34. use Cake\View\Helper\FormHelper;
  35. use Cake\View\View;
  36. use Cake\View\Widget\WidgetLocator;
  37. use InvalidArgumentException;
  38. use ReflectionProperty;
  39. use TestApp\Model\Entity\Article;
  40. use TestApp\Model\Enum\ArticleStatus;
  41. use TestApp\Model\Enum\ArticleStatusLabel;
  42. use TestApp\Model\Enum\ArticleStatusLabelInterface;
  43. use TestApp\Model\Enum\Priority;
  44. use TestApp\Model\Table\ContactsTable;
  45. use TestApp\Model\Table\ValidateUsersTable;
  46. use TestApp\View\Form\StubContext;
  47. /**
  48. * FormHelperTest class
  49. *
  50. * @property \Cake\View\Helper\FormHelper $Form
  51. * @property \Cake\View\View $View
  52. */
  53. class FormHelperTest extends TestCase
  54. {
  55. /**
  56. * Fixtures to be used
  57. *
  58. * @var list<string>
  59. */
  60. protected array $fixtures = ['core.Articles', 'core.Comments'];
  61. /**
  62. * @var array
  63. */
  64. protected $article = [];
  65. /**
  66. * @var string
  67. */
  68. protected $url;
  69. /**
  70. * @var \Cake\View\Helper\FormHelper
  71. */
  72. protected $Form;
  73. /**
  74. * @var \Cake\View\View
  75. */
  76. protected $View;
  77. /**
  78. * setUp method
  79. */
  80. public function setUp(): void
  81. {
  82. parent::setUp();
  83. Configure::write('Config.language', 'eng');
  84. Configure::write('App.base', '');
  85. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  86. $request = new ServerRequest([
  87. 'webroot' => '',
  88. 'base' => '',
  89. 'url' => '/articles/add',
  90. 'params' => [
  91. 'controller' => 'Articles',
  92. 'action' => 'add',
  93. 'plugin' => null,
  94. ],
  95. ]);
  96. $this->View = new View($request);
  97. Router::reload();
  98. Router::setRequest($request);
  99. $this->url = '/articles/add';
  100. $this->Form = new FormHelper($this->View);
  101. $this->article = [
  102. 'schema' => [
  103. 'id' => ['type' => 'integer'],
  104. 'author_id' => ['type' => 'integer', 'null' => true],
  105. 'title' => ['type' => 'string', 'null' => true],
  106. 'body' => 'text',
  107. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  108. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  109. ],
  110. 'required' => [
  111. 'author_id' => true,
  112. 'title' => true,
  113. ],
  114. ];
  115. Security::setSalt('foo!');
  116. $builder = Router::createRouteBuilder('/');
  117. $builder->connect('/{controller}', ['action' => 'index']);
  118. $builder->connect('/{controller}/{action}/*');
  119. }
  120. /**
  121. * tearDown method
  122. */
  123. public function tearDown(): void
  124. {
  125. parent::tearDown();
  126. unset($this->Form, $this->View);
  127. }
  128. /**
  129. * Test construct() with the templates option.
  130. */
  131. public function testConstructTemplatesFile(): void
  132. {
  133. $helper = new FormHelper($this->View, [
  134. 'templates' => 'htmlhelper_tags',
  135. ]);
  136. $result = $helper->control('name');
  137. $this->assertStringContainsString('<input', $result);
  138. }
  139. /**
  140. * Test that when specifying custom widgets the config array for that widget
  141. * is overwritten instead of merged.
  142. */
  143. public function testConstructWithWidgets(): void
  144. {
  145. $config = [
  146. 'widgets' => [
  147. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  148. ],
  149. ];
  150. $helper = new FormHelper($this->View, $config);
  151. $locator = $helper->getWidgetLocator();
  152. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  153. }
  154. /**
  155. * Test that when specifying custom widgets config file and it should be
  156. * added to widgets array. WidgetLocator will load widgets in constructor.
  157. */
  158. public function testConstructWithWidgetsConfig(): void
  159. {
  160. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  161. $locator = $helper->getWidgetLocator();
  162. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  163. }
  164. /**
  165. * Test setting the widget locator
  166. */
  167. public function testSetAndGetWidgetLocator(): void
  168. {
  169. $helper = new FormHelper($this->View);
  170. $locator = new WidgetLocator($helper->templater(), $this->View);
  171. $helper->setWidgetLocator($locator);
  172. $this->assertSame($locator, $helper->getWidgetLocator());
  173. }
  174. /**
  175. * Test overridding grouped input types which controls generation of "for"
  176. * attribute of labels.
  177. */
  178. public function testConstructWithGroupedInputTypes(): void
  179. {
  180. $helper = new FormHelper($this->View, [
  181. 'groupedInputTypes' => ['radio'],
  182. ]);
  183. $result = $helper->control('when', ['type' => 'datetime-local']);
  184. $this->assertStringContainsString('<label for="when">When</label>', $result);
  185. }
  186. /**
  187. * Test registering a new widget class and rendering it.
  188. */
  189. public function testAddWidgetAndRenderWidget(): void
  190. {
  191. $data = [
  192. 'val' => 1,
  193. ];
  194. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  195. $this->Form->addWidget('test', $mock);
  196. $mock->expects($this->once())
  197. ->method('render')
  198. ->with($data)
  199. ->willReturn('HTML');
  200. $result = $this->Form->widget('test', $data);
  201. $this->assertSame('HTML', $result);
  202. }
  203. /**
  204. * Test that secureFields() of widget is called after calling render(),
  205. * not before.
  206. */
  207. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  208. {
  209. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  210. 'unlockedFields' => [],
  211. ]));
  212. $data = [
  213. 'val' => 1,
  214. 'name' => 'test',
  215. ];
  216. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  217. $this->Form->addWidget('test', $mock);
  218. $mock->expects($this->once())
  219. ->method('render')
  220. ->with($data)
  221. ->willReturn('HTML');
  222. $mock->expects($this->once())
  223. ->method('secureFields')
  224. ->with($data)
  225. ->willReturn(['test']);
  226. $this->Form->create();
  227. $result = $this->Form->widget('test', $data + ['secure' => true]);
  228. $this->assertSame('HTML', $result);
  229. }
  230. /**
  231. * Test that empty string is not added to secure fields list when
  232. * rendering input widget without name.
  233. */
  234. public function testRenderingWidgetWithEmptyName(): void
  235. {
  236. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  237. $this->Form->create();
  238. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  239. $this->assertSame('<select name=""></select>', $result);
  240. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  241. $this->assertEquals([], $result);
  242. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  243. $this->assertSame('<select name="0"></select>', $result);
  244. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  245. $this->assertEquals(['0'], $result);
  246. }
  247. /**
  248. * Test adding a new context class.
  249. */
  250. public function testAddContextProvider(): void
  251. {
  252. $context = 'My data';
  253. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  254. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  255. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  256. $this->assertSame($context, $data['entity']);
  257. return $stub;
  258. });
  259. $this->Form->create($context);
  260. $result = $this->Form->context();
  261. $this->assertSame($stub, $result);
  262. }
  263. /**
  264. * Test replacing a context class.
  265. */
  266. public function testAddContextProviderReplace(): void
  267. {
  268. $entity = new Article();
  269. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  270. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  271. return $stub;
  272. });
  273. $this->Form->create($entity);
  274. $result = $this->Form->context();
  275. $this->assertSame($stub, $result);
  276. }
  277. /**
  278. * Test overriding a context class.
  279. */
  280. public function testAddContextProviderAdd(): void
  281. {
  282. $entity = new Article();
  283. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  284. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  285. if ($data['entity'] instanceof Entity) {
  286. return $stub;
  287. }
  288. });
  289. $this->Form->create($entity);
  290. $result = $this->Form->context();
  291. $this->assertSame($stub, $result);
  292. }
  293. /**
  294. * Provides context options for create().
  295. *
  296. * @return array
  297. */
  298. public static function contextSelectionProvider(): array
  299. {
  300. $entity = new Article();
  301. $collection = new Collection([$entity]);
  302. $emptyCollection = new Collection([]);
  303. $data = [
  304. 'schema' => [
  305. 'title' => ['type' => 'string'],
  306. ],
  307. ];
  308. $form = new Form();
  309. $custom = new StubContext();
  310. return [
  311. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  312. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  313. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  314. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  315. 'form' => [$form, 'Cake\View\Form\FormContext'],
  316. 'none' => [null, 'Cake\View\Form\NullContext'],
  317. 'custom' => [$custom, $custom::class],
  318. ];
  319. }
  320. /**
  321. * Test default context selection in create()
  322. *
  323. * @dataProvider contextSelectionProvider
  324. * @param mixed $data
  325. */
  326. public function testCreateContextSelectionBuiltIn($data, string $class): void
  327. {
  328. $this->Form->create($data);
  329. $this->assertInstanceOf($class, $this->Form->context());
  330. }
  331. /**
  332. * Data provider for type option.
  333. *
  334. * @return array
  335. */
  336. public static function requestTypeProvider(): array
  337. {
  338. return [
  339. // type, method, override
  340. ['post', 'post', 'POST'],
  341. ['put', 'post', 'PUT'],
  342. ['patch', 'post', 'PATCH'],
  343. ['delete', 'post', 'DELETE'],
  344. ];
  345. }
  346. /**
  347. * Test creating file forms.
  348. */
  349. public function testCreateFile(): void
  350. {
  351. $encoding = strtolower(Configure::read('App.encoding'));
  352. $result = $this->Form->create(null, ['type' => 'file']);
  353. $expected = [
  354. 'form' => [
  355. 'method' => 'post', 'action' => '/articles/add',
  356. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  357. ],
  358. 'div' => ['style' => 'display:none;'],
  359. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  360. '/div',
  361. ];
  362. $this->assertHtml($expected, $result);
  363. }
  364. /**
  365. * Test creating GET forms.
  366. */
  367. public function testCreateGet(): void
  368. {
  369. $encoding = strtolower(Configure::read('App.encoding'));
  370. $result = $this->Form->create(null, ['type' => 'get']);
  371. $expected = ['form' => [
  372. 'method' => 'get', 'action' => '/articles/add',
  373. 'accept-charset' => $encoding,
  374. ]];
  375. $this->assertHtml($expected, $result);
  376. }
  377. /**
  378. * Test explicit method/enctype options.
  379. *
  380. * Explicit method overwrites inferred method from 'type'
  381. */
  382. public function testCreateExplicitMethodEnctype(): void
  383. {
  384. $encoding = strtolower(Configure::read('App.encoding'));
  385. $result = $this->Form->create(null, [
  386. 'type' => 'get',
  387. 'method' => 'put',
  388. 'enctype' => 'multipart/form-data',
  389. ]);
  390. $expected = ['form' => [
  391. 'method' => 'put',
  392. 'action' => '/articles/add',
  393. 'enctype' => 'multipart/form-data',
  394. 'accept-charset' => $encoding,
  395. ]];
  396. $this->assertHtml($expected, $result);
  397. }
  398. /**
  399. * Test create() with the templates option.
  400. */
  401. public function testCreateTemplatesArray(): void
  402. {
  403. $result = $this->Form->create($this->article, [
  404. 'templates' => [
  405. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  406. ],
  407. ]);
  408. $expected = [
  409. 'form' => [
  410. 'class' => 'form-horizontal',
  411. 'method' => 'post',
  412. 'action' => '/articles/add',
  413. 'accept-charset' => 'utf-8',
  414. ],
  415. ];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test create() with the templates option.
  420. */
  421. public function testCreateTemplatesFile(): void
  422. {
  423. $result = $this->Form->create($this->article, [
  424. 'templates' => 'htmlhelper_tags',
  425. ]);
  426. $expected = [
  427. 'start form',
  428. ];
  429. $this->assertHtml($expected, $result);
  430. }
  431. /**
  432. * Test that create() and end() restore templates.
  433. */
  434. public function testCreateEndRestoreTemplates(): void
  435. {
  436. $this->Form->create($this->article, [
  437. 'templates' => ['input' => 'custom input element'],
  438. ]);
  439. $this->Form->end();
  440. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  441. }
  442. /**
  443. * Test create() with the templates option.
  444. */
  445. public function testCreateTemplatesRequiredClass(): void
  446. {
  447. $this->Form->create($this->article, [
  448. 'templates' => [
  449. 'requiredClass' => 'is-required',
  450. ],
  451. ]);
  452. $result = $this->Form->control('title');
  453. $expected = [
  454. 'div' => ['class' => 'input text is-required'],
  455. 'label' => ['for' => 'title'],
  456. 'Title',
  457. '/label',
  458. 'input' => [
  459. 'type' => 'text',
  460. 'name' => 'title',
  461. 'id' => 'title',
  462. 'required' => 'required',
  463. 'data-validity-message' => 'This field cannot be left empty',
  464. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  465. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  466. ],
  467. '/div',
  468. ];
  469. $this->assertHtml($expected, $result);
  470. }
  471. /**
  472. * Test using template vars in various templates used by control() method.
  473. */
  474. public function testControlTemplateVars(): void
  475. {
  476. $result = $this->Form->control('text', [
  477. 'templates' => [
  478. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  479. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  480. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  481. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  482. ],
  483. 'templateVars' => [
  484. 'forinput' => 'in-input',
  485. 'forlabel' => 'in-label',
  486. 'forgroup' => 'in-group',
  487. 'forcontainer' => 'in-container',
  488. ],
  489. ]);
  490. $expected = [
  491. 'div' => ['class'],
  492. 'label' => ['for'],
  493. 'Text in-label',
  494. '/label',
  495. 'in-group',
  496. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  497. 'in-container',
  498. '/div',
  499. ];
  500. $this->assertHtml($expected, $result);
  501. }
  502. /**
  503. * Test ensuring template variables work in template files loaded
  504. * during control().
  505. */
  506. public function testControlTemplatesFromFile(): void
  507. {
  508. $result = $this->Form->control('title', [
  509. 'templates' => 'test_templates',
  510. 'templateVars' => [
  511. 'forcontainer' => 'container-data',
  512. ],
  513. ]);
  514. $expected = [
  515. 'div' => ['class'],
  516. 'label' => ['for'],
  517. 'Title',
  518. '/label',
  519. 'input' => ['name', 'type' => 'text', 'id'],
  520. 'container-data',
  521. '/div',
  522. ];
  523. $this->assertHtml($expected, $result);
  524. }
  525. /**
  526. * Test using template vars in inputSubmit and submitContainer template.
  527. */
  528. public function testSubmitTemplateVars(): void
  529. {
  530. $this->Form->setTemplates([
  531. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  532. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  533. ]);
  534. $result = $this->Form->submit('Submit', [
  535. 'templateVars' => [
  536. 'forinput' => 'in-input',
  537. 'forcontainer' => 'in-container',
  538. ],
  539. ]);
  540. $expected = [
  541. 'div' => ['class'],
  542. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  543. 'in-container',
  544. '/div',
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * test the create() method
  550. *
  551. * @dataProvider requestTypeProvider
  552. */
  553. public function testCreateTypeOptions(string $type, string $method, string $override): void
  554. {
  555. $encoding = strtolower(Configure::read('App.encoding'));
  556. $result = $this->Form->create(null, ['type' => $type]);
  557. $expected = [
  558. 'form' => [
  559. 'method' => $method, 'action' => '/articles/add',
  560. 'accept-charset' => $encoding,
  561. ],
  562. ];
  563. $extra = [
  564. 'div' => ['style' => 'display:none;'],
  565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  566. '/div',
  567. ];
  568. if ($type !== 'post') {
  569. $expected = array_merge($expected, $extra);
  570. }
  571. $this->assertHtml($expected, $result);
  572. }
  573. /**
  574. * Test using template vars in Create (formStart template)
  575. */
  576. public function testCreateTemplateVars(): void
  577. {
  578. $result = $this->Form->create($this->article, [
  579. 'templates' => [
  580. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  581. ],
  582. 'templateVars' => ['header' => 'headertext'],
  583. ]);
  584. $expected = [
  585. 'h4' => ['class'],
  586. 'headertext',
  587. '/h4',
  588. 'form' => [
  589. 'method' => 'post',
  590. 'action' => '/articles/add',
  591. 'accept-charset' => 'utf-8',
  592. ],
  593. ];
  594. $this->assertHtml($expected, $result);
  595. }
  596. /**
  597. * Test opening a form for an update operation.
  598. */
  599. public function testCreateUpdateForm(): void
  600. {
  601. $encoding = strtolower(Configure::read('App.encoding'));
  602. $this->View->setRequest($this->View->getRequest()
  603. ->withRequestTarget('/articles/edit/1')
  604. ->withParam('action', 'edit'));
  605. $this->article['defaults']['id'] = 1;
  606. $result = $this->Form->create($this->article);
  607. $expected = [
  608. 'form' => [
  609. 'method' => 'post',
  610. 'action' => '/articles/edit/1',
  611. 'accept-charset' => $encoding,
  612. ],
  613. 'div' => ['style' => 'display:none;'],
  614. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. }
  619. /**
  620. * test create() with automatic url generation
  621. */
  622. public function testCreateAutoUrl(): void
  623. {
  624. $encoding = strtolower(Configure::read('App.encoding'));
  625. $this->View->setRequest($this->View->getRequest()
  626. ->withRequestTarget('/articles/delete/10')
  627. ->withParam('action', 'delete'));
  628. $result = $this->Form->create($this->article);
  629. $expected = [
  630. 'form' => [
  631. 'method' => 'post', 'action' => '/articles/delete/10',
  632. 'accept-charset' => $encoding,
  633. ],
  634. ];
  635. $this->assertHtml($expected, $result);
  636. $this->article['defaults'] = ['id' => 1];
  637. $this->View->setRequest($this->View->getRequest()
  638. ->withRequestTarget('/Articles/edit/1')
  639. ->withParam('action', 'delete'));
  640. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  641. $expected = [
  642. 'form' => [
  643. 'method' => 'post',
  644. 'action' => '/Articles/edit/1',
  645. 'accept-charset' => $encoding,
  646. ],
  647. 'div' => ['style' => 'display:none;'],
  648. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  649. '/div',
  650. ];
  651. $this->assertHtml($expected, $result);
  652. $this->View->setRequest($this->View->getRequest()
  653. ->withParam('action', 'add'));
  654. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  655. $expected = [
  656. 'form' => [
  657. 'method' => 'post',
  658. 'action' => '/Articles/publish/1',
  659. 'accept-charset' => $encoding,
  660. ],
  661. 'div' => ['style' => 'display:none;'],
  662. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  663. '/div',
  664. ];
  665. $this->assertHtml($expected, $result);
  666. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  667. $expected = [
  668. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  669. 'div' => ['style' => 'display:none;'],
  670. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  671. '/div',
  672. ];
  673. $this->assertHtml($expected, $result);
  674. $this->View->setRequest($this->View->getRequest()
  675. ->withParam('controller', 'Pages'));
  676. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  677. $expected = [
  678. 'form' => [
  679. 'method' => 'post', 'action' => '/Pages/signup/1',
  680. 'accept-charset' => $encoding,
  681. ],
  682. 'div' => ['style' => 'display:none;'],
  683. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  684. '/div',
  685. ];
  686. $this->assertHtml($expected, $result);
  687. }
  688. /**
  689. * Test create() with no URL (no "action" attribute for <form> tag)
  690. */
  691. public function testCreateNoUrl(): void
  692. {
  693. $result = $this->Form->create(null, ['url' => false]);
  694. $expected = [
  695. 'form' => [
  696. 'method' => 'post',
  697. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  698. ],
  699. ];
  700. $this->assertHtml($expected, $result);
  701. }
  702. /**
  703. * test create() with a custom route
  704. */
  705. public function testCreateCustomRoute(): void
  706. {
  707. $builder = Router::createRouteBuilder('/');
  708. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  709. $encoding = strtolower(Configure::read('App.encoding'));
  710. $this->View->setRequest($this->View->getRequest()
  711. ->withParam('controller', 'Users'));
  712. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post', 'action' => '/login',
  716. 'accept-charset' => $encoding,
  717. ],
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $builder->connect(
  721. '/new-article',
  722. ['controller' => 'Articles', 'action' => 'myAction'],
  723. ['_name' => 'my-route']
  724. );
  725. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  726. $expected = [
  727. 'form' => [
  728. 'method' => 'post', 'action' => '/new-article',
  729. 'accept-charset' => $encoding,
  730. ],
  731. ];
  732. $this->assertHtml($expected, $result);
  733. }
  734. /**
  735. * test automatic accept-charset overriding
  736. */
  737. public function testCreateWithAcceptCharset(): void
  738. {
  739. $result = $this->Form->create(
  740. $this->article,
  741. [
  742. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  743. ]
  744. );
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Articles',
  748. 'accept-charset' => 'iso-8859-1',
  749. ],
  750. ];
  751. $this->assertHtml($expected, $result);
  752. }
  753. /**
  754. * Test base form URL when 'url' param is passed with multiple parameters (&)
  755. */
  756. public function testCreateQueryStringRequest(): void
  757. {
  758. $encoding = strtolower(Configure::read('App.encoding'));
  759. $result = $this->Form->create($this->article, [
  760. 'type' => 'post',
  761. 'escape' => false,
  762. 'url' => [
  763. 'controller' => 'Controller',
  764. 'action' => 'action',
  765. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  766. ],
  767. ]);
  768. $expected = [
  769. 'form' => [
  770. 'method' => 'post',
  771. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  772. 'accept-charset' => $encoding,
  773. ],
  774. ];
  775. $this->assertHtml($expected, $result);
  776. $result = $this->Form->create($this->article, [
  777. 'type' => 'post',
  778. 'url' => [
  779. 'controller' => 'Controller',
  780. 'action' => 'action',
  781. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  782. ],
  783. ]);
  784. $this->assertHtml($expected, $result);
  785. }
  786. /**
  787. * test that create() doesn't cause errors by multiple id's being in the primary key
  788. * as could happen with multiple select or checkboxes.
  789. */
  790. public function testCreateWithMultipleIdInData(): void
  791. {
  792. $encoding = strtolower(Configure::read('App.encoding'));
  793. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  794. $result = $this->Form->create($this->article);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post',
  798. 'action' => '/articles/add',
  799. 'accept-charset' => $encoding,
  800. ],
  801. ];
  802. $this->assertHtml($expected, $result);
  803. }
  804. /**
  805. * test that create() doesn't add in extra passed params.
  806. */
  807. public function testCreatePassedArgs(): void
  808. {
  809. $encoding = strtolower(Configure::read('App.encoding'));
  810. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  811. $result = $this->Form->create($this->article, [
  812. 'type' => 'post',
  813. 'escape' => false,
  814. 'url' => [
  815. 'action' => 'edit',
  816. 'myparam',
  817. ],
  818. ]);
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post',
  822. 'action' => '/Articles/edit/myparam',
  823. 'accept-charset' => $encoding,
  824. ],
  825. ];
  826. $this->assertHtml($expected, $result);
  827. }
  828. /**
  829. * test creating a get form, and get form inputs.
  830. */
  831. public function testGetFormCreate(): void
  832. {
  833. $encoding = strtolower(Configure::read('App.encoding'));
  834. $result = $this->Form->create($this->article, ['type' => 'get']);
  835. $expected = ['form' => [
  836. 'method' => 'get', 'action' => '/articles/add',
  837. 'accept-charset' => $encoding,
  838. ]];
  839. $this->assertHtml($expected, $result);
  840. $result = $this->Form->text('title');
  841. $expected = ['input' => [
  842. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  843. ]];
  844. $this->assertHtml($expected, $result);
  845. $result = $this->Form->password('password');
  846. $expected = ['input' => [
  847. 'name' => 'password', 'type' => 'password',
  848. ]];
  849. $this->assertHtml($expected, $result);
  850. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  851. $result = $this->Form->text('user_form');
  852. $expected = ['input' => [
  853. 'name' => 'user_form', 'type' => 'text',
  854. ]];
  855. $this->assertHtml($expected, $result);
  856. }
  857. /**
  858. * test get form, and inputs when the model param is false
  859. */
  860. public function testGetFormWithFalseModel(): void
  861. {
  862. $encoding = strtolower(Configure::read('App.encoding'));
  863. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  864. $result = $this->Form->create(null, [
  865. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  866. ]);
  867. $expected = ['form' => [
  868. 'method' => 'get', 'action' => '/ContactTest/add',
  869. 'accept-charset' => $encoding,
  870. ]];
  871. $this->assertHtml($expected, $result);
  872. $result = $this->Form->text('reason');
  873. $expected = [
  874. 'input' => ['type' => 'text', 'name' => 'reason'],
  875. ];
  876. $this->assertHtml($expected, $result);
  877. }
  878. /**
  879. * testFormCreateWithSecurity method
  880. *
  881. * Test form->create() with security key.
  882. */
  883. public function testCreateWithSecurity(): void
  884. {
  885. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  886. $encoding = strtolower(Configure::read('App.encoding'));
  887. $result = $this->Form->create($this->article, [
  888. 'url' => '/articles/publish',
  889. ]);
  890. $expected = [
  891. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  892. 'div' => ['style' => 'display:none;'],
  893. ['input' => [
  894. 'type' => 'hidden',
  895. 'name' => '_csrfToken',
  896. 'value' => 'testKey',
  897. ]],
  898. '/div',
  899. ];
  900. $this->assertHtml($expected, $result);
  901. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  902. $expected['form']['id'] = 'MyForm';
  903. $this->assertHtml($expected, $result);
  904. }
  905. /**
  906. * testFormCreateGetNoSecurity method
  907. *
  908. * Test form->create() with no security key as its a get form
  909. */
  910. public function testCreateEndGetNoSecurity(): void
  911. {
  912. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  913. $article = new Article();
  914. $result = $this->Form->create($article, [
  915. 'type' => 'get',
  916. 'url' => '/contacts/add',
  917. ]);
  918. $this->assertStringNotContainsString('testKey', $result);
  919. $result = $this->Form->end();
  920. $this->assertStringNotContainsString('testKey', $result);
  921. }
  922. /**
  923. * Tests form hash generation with model-less data
  924. */
  925. public function testValidateHashNoModel(): void
  926. {
  927. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  928. $fields = ['anything'];
  929. $this->Form->create();
  930. $result = $this->Form->secure($fields);
  931. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  932. $this->assertStringContainsString($hash, $result);
  933. }
  934. /**
  935. * Tests that hidden fields generated for checkboxes don't get locked
  936. */
  937. public function testNoCheckboxLocking(): void
  938. {
  939. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  940. $this->Form->create();
  941. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  942. $this->Form->checkbox('check', ['value' => '1']);
  943. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  944. }
  945. /**
  946. * testFormSecurityFields method
  947. *
  948. * Test generation of secure form hash generation.
  949. */
  950. public function testFormSecurityFields(): void
  951. {
  952. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  953. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  954. $this->Form->create();
  955. $result = $this->Form->secure($fields);
  956. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  957. $hash .= ':' . 'Model.valid';
  958. $hash = urlencode($hash);
  959. $tokenDebug = urlencode(json_encode([
  960. $this->url,
  961. $fields,
  962. [],
  963. ]));
  964. $expected = [
  965. 'div' => ['style' => 'display:none;'],
  966. ['input' => [
  967. 'type' => 'hidden',
  968. 'name' => '_Token[fields]',
  969. 'value' => $hash,
  970. ]],
  971. ['input' => [
  972. 'type' => 'hidden',
  973. 'name' => '_Token[unlocked]',
  974. 'value' => '',
  975. ]],
  976. ['input' => [
  977. 'type' => 'hidden',
  978. 'name' => '_Token[debug]',
  979. 'value' => $tokenDebug,
  980. ]],
  981. '/div',
  982. ];
  983. $this->assertHtml($expected, $result);
  984. }
  985. /**
  986. * testFormSecurityFields method
  987. *
  988. * Test debug token is not generated if debug is false
  989. */
  990. public function testFormSecurityFieldsNoDebugMode(): void
  991. {
  992. Configure::write('debug', false);
  993. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  994. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  995. $this->Form->create();
  996. $result = $this->Form->secure($fields);
  997. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  998. $hash .= ':' . 'Model.valid';
  999. $hash = urlencode($hash);
  1000. $expected = [
  1001. 'div' => ['style' => 'display:none;'],
  1002. ['input' => [
  1003. 'type' => 'hidden',
  1004. 'name' => '_Token[fields]',
  1005. 'value' => $hash,
  1006. ]],
  1007. ['input' => [
  1008. 'type' => 'hidden',
  1009. 'name' => '_Token[unlocked]',
  1010. 'value' => '',
  1011. ]],
  1012. '/div',
  1013. ];
  1014. $this->assertHtml($expected, $result);
  1015. }
  1016. /**
  1017. * Tests correct generation of number fields for smallint
  1018. */
  1019. public function testTextFieldGenerationForSmallint(): void
  1020. {
  1021. $this->article['schema'] = [
  1022. 'foo' => [
  1023. 'type' => 'smallinteger',
  1024. 'null' => false,
  1025. 'default' => null,
  1026. 'length' => 10,
  1027. ],
  1028. ];
  1029. $this->Form->create($this->article);
  1030. $result = $this->Form->control('foo');
  1031. $this->assertStringContainsString('class="input number"', $result);
  1032. $this->assertStringContainsString('type="number"', $result);
  1033. }
  1034. /**
  1035. * Tests correct generation of number fields for tinyint
  1036. */
  1037. public function testTextFieldGenerationForTinyint(): void
  1038. {
  1039. $this->article['schema'] = [
  1040. 'foo' => [
  1041. 'type' => 'tinyinteger',
  1042. 'null' => false,
  1043. 'default' => null,
  1044. 'length' => 10,
  1045. ],
  1046. ];
  1047. $this->Form->create($this->article);
  1048. $result = $this->Form->control('foo');
  1049. $this->assertStringContainsString('class="input number"', $result);
  1050. $this->assertStringContainsString('type="number"', $result);
  1051. }
  1052. /**
  1053. * Tests correct generation of number fields for double and float fields
  1054. */
  1055. public function testTextFieldGenerationForFloats(): void
  1056. {
  1057. $this->article['schema'] = [
  1058. 'foo' => [
  1059. 'type' => 'float',
  1060. 'null' => false,
  1061. 'default' => null,
  1062. 'length' => 10,
  1063. ],
  1064. ];
  1065. $this->Form->create($this->article);
  1066. $result = $this->Form->control('foo');
  1067. $expected = [
  1068. 'div' => ['class' => 'input number'],
  1069. 'label' => ['for' => 'foo'],
  1070. 'Foo',
  1071. '/label',
  1072. ['input' => [
  1073. 'type' => 'number',
  1074. 'name' => 'foo',
  1075. 'id' => 'foo',
  1076. 'step' => 'any',
  1077. ]],
  1078. '/div',
  1079. ];
  1080. $this->assertHtml($expected, $result);
  1081. $result = $this->Form->control('foo', ['step' => 0.5]);
  1082. $expected = [
  1083. 'div' => ['class' => 'input number'],
  1084. 'label' => ['for' => 'foo'],
  1085. 'Foo',
  1086. '/label',
  1087. ['input' => [
  1088. 'type' => 'number',
  1089. 'name' => 'foo',
  1090. 'id' => 'foo',
  1091. 'step' => '0.5',
  1092. ]],
  1093. '/div',
  1094. ];
  1095. $this->assertHtml($expected, $result);
  1096. }
  1097. /**
  1098. * Tests correct generation of number fields for integer fields
  1099. */
  1100. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1101. {
  1102. $this->getTableLocator()->get('Contacts', [
  1103. 'className' => ContactsTable::class,
  1104. ]);
  1105. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1106. $result = $this->Form->control('age');
  1107. $expected = [
  1108. 'div' => ['class' => 'input number'],
  1109. 'label' => ['for' => 'age'],
  1110. 'Age',
  1111. '/label',
  1112. ['input' => [
  1113. 'type' => 'number', 'name' => 'age',
  1114. 'id' => 'age',
  1115. ]],
  1116. '/div',
  1117. ];
  1118. $this->assertHtml($expected, $result);
  1119. }
  1120. /**
  1121. * Tests correct generation of file upload fields for binary fields
  1122. */
  1123. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1124. {
  1125. $table = $this->getTableLocator()->get('Contacts', [
  1126. 'className' => ContactsTable::class,
  1127. ]);
  1128. $table->setSchema(['foo' => [
  1129. 'type' => 'binary',
  1130. 'null' => false,
  1131. 'default' => null,
  1132. 'length' => 1024,
  1133. ]]);
  1134. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1135. $result = $this->Form->control('foo');
  1136. $expected = [
  1137. 'div' => ['class' => 'input file'],
  1138. 'label' => ['for' => 'foo'],
  1139. 'Foo',
  1140. '/label',
  1141. ['input' => [
  1142. 'type' => 'file', 'name' => 'foo',
  1143. 'id' => 'foo',
  1144. ]],
  1145. '/div',
  1146. ];
  1147. $this->assertHtml($expected, $result);
  1148. }
  1149. /**
  1150. * testFormSecurityMultipleFields method
  1151. *
  1152. * Test secure() with multiple row form. Ensure hash is correct.
  1153. */
  1154. public function testFormSecurityMultipleFields(): void
  1155. {
  1156. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1157. $this->Form->create();
  1158. $fields = [
  1159. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1160. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1161. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1162. ];
  1163. $result = $this->Form->secure($fields);
  1164. $sortedFields = [
  1165. 'Model.0.password',
  1166. 'Model.0.username',
  1167. 'Model.1.password',
  1168. 'Model.1.username',
  1169. 'Model.0.hidden' => 'value',
  1170. 'Model.0.valid' => '0',
  1171. 'Model.1.hidden' => 'value',
  1172. 'Model.1.valid' => '0',
  1173. ];
  1174. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1175. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1176. $hash = urlencode($hash);
  1177. $tokenDebug = urlencode(json_encode([
  1178. $this->url,
  1179. $fields,
  1180. [],
  1181. ]));
  1182. $expected = [
  1183. 'div' => ['style' => 'display:none;'],
  1184. ['input' => [
  1185. 'type' => 'hidden',
  1186. 'name' => '_Token[fields]',
  1187. 'value' => $hash,
  1188. ]],
  1189. ['input' => [
  1190. 'type' => 'hidden',
  1191. 'name' => '_Token[unlocked]',
  1192. 'value' => '',
  1193. ]],
  1194. ['input' => [
  1195. 'type' => 'hidden',
  1196. 'name' => '_Token[debug]',
  1197. 'value' => $tokenDebug,
  1198. ]],
  1199. '/div',
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. }
  1203. /**
  1204. * testFormSecurityMultipleSubmitButtons
  1205. *
  1206. * test form submit generation and ensure that _Token is only created on end()
  1207. */
  1208. public function testFormSecurityMultipleSubmitButtons(): void
  1209. {
  1210. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1211. $this->Form->create($this->article);
  1212. $this->Form->text('Address.title');
  1213. $this->Form->text('Address.first_name');
  1214. $result = $this->Form->submit('Save', ['name' => 'save']);
  1215. $expected = [
  1216. 'div' => ['class' => 'submit'],
  1217. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1218. '/div',
  1219. ];
  1220. $this->assertHtml($expected, $result);
  1221. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1222. $expected = [
  1223. 'div' => ['class' => 'submit'],
  1224. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1225. '/div',
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. $result = $this->Form->end();
  1229. $tokenDebug = urlencode(json_encode([
  1230. '/articles/add',
  1231. [
  1232. 'Address.title',
  1233. 'Address.first_name',
  1234. ],
  1235. ['save', 'cancel'],
  1236. ]));
  1237. $expected = [
  1238. 'div' => ['style' => 'display:none;'],
  1239. ['input' => [
  1240. 'type' => 'hidden',
  1241. 'name' => '_Token[fields]',
  1242. 'value',
  1243. ]],
  1244. ['input' => [
  1245. 'type' => 'hidden',
  1246. 'name' => '_Token[unlocked]',
  1247. 'value' => 'cancel%7Csave',
  1248. ]],
  1249. ['input' => [
  1250. 'type' => 'hidden',
  1251. 'name' => '_Token[debug]',
  1252. 'value' => $tokenDebug,
  1253. ]],
  1254. '/div',
  1255. ];
  1256. $this->assertHtml($expected, $result);
  1257. }
  1258. /**
  1259. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1260. */
  1261. public function testSecurityButtonNestedNamed(): void
  1262. {
  1263. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1264. $this->Form->create();
  1265. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1266. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1267. $this->assertEquals(['Address.button'], $result);
  1268. }
  1269. /**
  1270. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1271. */
  1272. public function testSecuritySubmitNestedNamed(): void
  1273. {
  1274. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1275. $this->Form->create($this->article);
  1276. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1277. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1278. $this->assertEquals(['Address.button'], $result);
  1279. }
  1280. /**
  1281. * Test that the correct fields are unlocked for image submits with no names.
  1282. */
  1283. public function testSecuritySubmitImageNoName(): void
  1284. {
  1285. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1286. $this->Form->create();
  1287. $result = $this->Form->submit('save.png');
  1288. $expected = [
  1289. 'div' => ['class' => 'submit'],
  1290. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1291. '/div',
  1292. ];
  1293. $this->assertHtml($expected, $result);
  1294. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1295. $this->assertEquals(['x', 'y'], $result);
  1296. }
  1297. /**
  1298. * Test that the correct fields are unlocked for image submits with names.
  1299. */
  1300. public function testSecuritySubmitImageName(): void
  1301. {
  1302. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1303. $this->Form->create();
  1304. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1305. $expected = [
  1306. 'div' => ['class' => 'submit'],
  1307. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1308. '/div',
  1309. ];
  1310. $this->assertHtml($expected, $result);
  1311. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1312. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1313. }
  1314. /**
  1315. * testFormSecurityMultipleControlFields method
  1316. *
  1317. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1318. */
  1319. public function testFormSecurityMultipleControlFields(): void
  1320. {
  1321. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1322. $this->Form->create();
  1323. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1324. $this->Form->control('Addresses.0.title');
  1325. $this->Form->control('Addresses.0.first_name');
  1326. $this->Form->control('Addresses.0.last_name');
  1327. $this->Form->control('Addresses.0.address');
  1328. $this->Form->control('Addresses.0.city');
  1329. $this->Form->control('Addresses.0.phone');
  1330. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1331. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1332. $this->Form->control('Addresses.1.title');
  1333. $this->Form->control('Addresses.1.first_name');
  1334. $this->Form->control('Addresses.1.last_name');
  1335. $this->Form->control('Addresses.1.address');
  1336. $this->Form->control('Addresses.1.city');
  1337. $this->Form->control('Addresses.1.phone');
  1338. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1339. $result = $this->Form->secure();
  1340. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1341. $tokenDebug = urlencode(json_encode([
  1342. '/articles/add',
  1343. [
  1344. 'Addresses.0.id' => '123456',
  1345. 'Addresses.0.title',
  1346. 'Addresses.0.first_name',
  1347. 'Addresses.0.last_name',
  1348. 'Addresses.0.address',
  1349. 'Addresses.0.city',
  1350. 'Addresses.0.phone',
  1351. 'Addresses.0.primary',
  1352. 'Addresses.1.id' => '654321',
  1353. 'Addresses.1.title',
  1354. 'Addresses.1.first_name',
  1355. 'Addresses.1.last_name',
  1356. 'Addresses.1.address',
  1357. 'Addresses.1.city',
  1358. 'Addresses.1.phone',
  1359. 'Addresses.1.primary',
  1360. ],
  1361. [],
  1362. ]));
  1363. $expected = [
  1364. 'div' => ['style' => 'display:none;'],
  1365. ['input' => [
  1366. 'type' => 'hidden',
  1367. 'name' => '_Token[fields]',
  1368. 'value' => $hash,
  1369. ]],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[unlocked]',
  1373. 'value' => '',
  1374. ]],
  1375. ['input' => [
  1376. 'type' => 'hidden',
  1377. 'name' => '_Token[debug]',
  1378. 'value' => $tokenDebug,
  1379. ]],
  1380. '/div',
  1381. ];
  1382. $this->assertHtml($expected, $result);
  1383. }
  1384. /**
  1385. * testFormSecurityArrayFields method
  1386. *
  1387. * Test form security with Model.field.0 style inputs.
  1388. */
  1389. public function testFormSecurityArrayFields(): void
  1390. {
  1391. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1392. $this->Form->create();
  1393. $this->Form->text('Address.primary.1');
  1394. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1395. $this->assertSame('Address.primary', $result[0]);
  1396. $this->Form->text('Address.secondary.1.0');
  1397. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1398. $this->assertSame('Address.secondary', $result[1]);
  1399. }
  1400. /**
  1401. * testFormSecurityMultipleControlDisabledFields method
  1402. *
  1403. * Test secure form generation with multiple records and disabled fields.
  1404. */
  1405. public function testFormSecurityMultipleControlDisabledFields(): void
  1406. {
  1407. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1408. 'unlockedFields' => ['first_name', 'address'],
  1409. ]));
  1410. $this->Form->create();
  1411. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1412. $this->Form->text('Addresses.0.title');
  1413. $this->Form->text('Addresses.0.first_name');
  1414. $this->Form->text('Addresses.0.last_name');
  1415. $this->Form->text('Addresses.0.address');
  1416. $this->Form->text('Addresses.0.city');
  1417. $this->Form->text('Addresses.0.phone');
  1418. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1419. $this->Form->text('Addresses.1.title');
  1420. $this->Form->text('Addresses.1.first_name');
  1421. $this->Form->text('Addresses.1.last_name');
  1422. $this->Form->text('Addresses.1.address');
  1423. $this->Form->text('Addresses.1.city');
  1424. $this->Form->text('Addresses.1.phone');
  1425. $result = $this->Form->secure();
  1426. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1427. $tokenDebug = urlencode(json_encode([
  1428. '/articles/add',
  1429. [
  1430. 'Addresses.0.id' => '123456',
  1431. 'Addresses.0.title',
  1432. 'Addresses.0.last_name',
  1433. 'Addresses.0.city',
  1434. 'Addresses.0.phone',
  1435. 'Addresses.1.id' => '654321',
  1436. 'Addresses.1.title',
  1437. 'Addresses.1.last_name',
  1438. 'Addresses.1.city',
  1439. 'Addresses.1.phone',
  1440. ],
  1441. [
  1442. 'first_name',
  1443. 'address',
  1444. ],
  1445. ]));
  1446. $expected = [
  1447. 'div' => ['style' => 'display:none;'],
  1448. ['input' => [
  1449. 'type' => 'hidden',
  1450. 'name' => '_Token[fields]',
  1451. 'value' => $hash,
  1452. ]],
  1453. ['input' => [
  1454. 'type' => 'hidden',
  1455. 'name' => '_Token[unlocked]',
  1456. 'value' => 'address%7Cfirst_name',
  1457. ]],
  1458. ['input' => [
  1459. 'type' => 'hidden',
  1460. 'name' => '_Token[debug]',
  1461. 'value' => $tokenDebug,
  1462. ]],
  1463. '/div',
  1464. ];
  1465. $this->assertHtml($expected, $result);
  1466. }
  1467. /**
  1468. * testFormSecurityControlDisabledFields method
  1469. *
  1470. * Test single record form with disabled fields.
  1471. */
  1472. public function testFormSecurityControlUnlockedFields(): void
  1473. {
  1474. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1475. 'unlockedFields' => ['first_name', 'address'],
  1476. ]));
  1477. $this->Form->create();
  1478. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1479. $this->assertEquals(
  1480. $this->View->getRequest()->getAttribute('formTokenData'),
  1481. ['unlockedFields' => $result]
  1482. );
  1483. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1484. $this->Form->text('Addresses.title');
  1485. $this->Form->text('Addresses.first_name');
  1486. $this->Form->text('Addresses.last_name');
  1487. $this->Form->text('Addresses.address');
  1488. $this->Form->text('Addresses.city');
  1489. $this->Form->text('Addresses.phone');
  1490. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1491. $expected = [
  1492. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1493. 'Addresses.city', 'Addresses.phone',
  1494. ];
  1495. $this->assertEquals($expected, $result);
  1496. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1497. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1498. $tokenDebug = urlencode(json_encode([
  1499. '/articles/add',
  1500. [
  1501. 'Addresses.id' => '123456',
  1502. 'Addresses.title',
  1503. 'Addresses.last_name',
  1504. 'Addresses.city',
  1505. 'Addresses.phone',
  1506. ],
  1507. [
  1508. 'first_name',
  1509. 'address',
  1510. ],
  1511. ]));
  1512. $expected = [
  1513. 'div' => ['style' => 'display:none;'],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[fields]',
  1517. 'value' => $hash,
  1518. 'data-foo' => 'bar',
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[unlocked]',
  1523. 'value' => 'address%7Cfirst_name',
  1524. 'data-foo' => 'bar',
  1525. ]],
  1526. ['input' => [
  1527. 'type' => 'hidden', 'name' => '_Token[debug]',
  1528. 'value' => $tokenDebug,
  1529. 'data-foo' => 'bar',
  1530. ]],
  1531. '/div',
  1532. ];
  1533. $this->assertHtml($expected, $result);
  1534. }
  1535. /**
  1536. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1537. *
  1538. * Test single record form with debugSecurity param.
  1539. */
  1540. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1541. {
  1542. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1543. 'unlockedFields' => ['first_name', 'address'],
  1544. ]));
  1545. $this->Form->create();
  1546. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1547. $this->assertEquals(
  1548. $this->View->getRequest()->getAttribute('formTokenData'),
  1549. ['unlockedFields' => $result]
  1550. );
  1551. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1552. $this->Form->text('Addresses.title');
  1553. $this->Form->text('Addresses.first_name');
  1554. $this->Form->text('Addresses.last_name');
  1555. $this->Form->text('Addresses.address');
  1556. $this->Form->text('Addresses.city');
  1557. $this->Form->text('Addresses.phone');
  1558. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1559. $expected = [
  1560. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1561. 'Addresses.city', 'Addresses.phone',
  1562. ];
  1563. $this->assertEquals($expected, $result);
  1564. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1565. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1566. $tokenDebug = urlencode(json_encode([
  1567. '/articles/add',
  1568. [
  1569. 'Addresses.id' => '123456',
  1570. 'Addresses.title',
  1571. 'Addresses.last_name',
  1572. 'Addresses.city',
  1573. 'Addresses.phone',
  1574. ],
  1575. [
  1576. 'first_name',
  1577. 'address',
  1578. ],
  1579. ]));
  1580. $expected = [
  1581. 'div' => ['style' => 'display:none;'],
  1582. ['input' => [
  1583. 'type' => 'hidden',
  1584. 'name' => '_Token[fields]',
  1585. 'value' => $hash,
  1586. 'data-foo' => 'bar',
  1587. ]],
  1588. ['input' => [
  1589. 'type' => 'hidden',
  1590. 'name' => '_Token[unlocked]',
  1591. 'value' => 'address%7Cfirst_name',
  1592. 'data-foo' => 'bar',
  1593. ]],
  1594. ['input' => [
  1595. 'type' => 'hidden', 'name' => '_Token[debug]',
  1596. 'value' => $tokenDebug,
  1597. 'data-foo' => 'bar',
  1598. ]],
  1599. '/div',
  1600. ];
  1601. $this->assertHtml($expected, $result);
  1602. }
  1603. /**
  1604. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1605. *
  1606. * Debug is false, debugSecurity is true -> no debug
  1607. */
  1608. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1609. {
  1610. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1611. 'unlockedFields' => ['first_name', 'address'],
  1612. ]));
  1613. $this->Form->create();
  1614. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1615. $this->assertEquals(
  1616. $this->View->getRequest()->getAttribute('formTokenData'),
  1617. ['unlockedFields' => $result]
  1618. );
  1619. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1620. $this->Form->text('Addresses.title');
  1621. $this->Form->text('Addresses.first_name');
  1622. $this->Form->text('Addresses.last_name');
  1623. $this->Form->text('Addresses.address');
  1624. $this->Form->text('Addresses.city');
  1625. $this->Form->text('Addresses.phone');
  1626. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1627. $expected = [
  1628. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1629. 'Addresses.city', 'Addresses.phone',
  1630. ];
  1631. $this->assertEquals($expected, $result);
  1632. Configure::write('debug', false);
  1633. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1634. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1635. $expected = [
  1636. 'div' => ['style' => 'display:none;'],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[fields]',
  1640. 'value' => $hash,
  1641. 'data-foo' => 'bar',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[unlocked]',
  1646. 'value' => 'address%7Cfirst_name',
  1647. 'data-foo' => 'bar',
  1648. ]],
  1649. '/div',
  1650. ];
  1651. $this->assertHtml($expected, $result);
  1652. }
  1653. /**
  1654. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1655. *
  1656. * Test single record form with debugSecurity param.
  1657. */
  1658. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1659. {
  1660. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1661. 'unlockedFields' => ['first_name', 'address'],
  1662. ]));
  1663. $this->Form->create();
  1664. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1665. $this->assertEquals(
  1666. $this->View->getRequest()->getAttribute('formTokenData'),
  1667. ['unlockedFields' => $result]
  1668. );
  1669. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1670. $this->Form->text('Addresses.title');
  1671. $this->Form->text('Addresses.first_name');
  1672. $this->Form->text('Addresses.last_name');
  1673. $this->Form->text('Addresses.address');
  1674. $this->Form->text('Addresses.city');
  1675. $this->Form->text('Addresses.phone');
  1676. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1677. $expected = [
  1678. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1679. 'Addresses.city', 'Addresses.phone',
  1680. ];
  1681. $this->assertEquals($expected, $result);
  1682. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1683. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1684. $expected = [
  1685. 'div' => ['style' => 'display:none;'],
  1686. ['input' => [
  1687. 'type' => 'hidden',
  1688. 'name' => '_Token[fields]',
  1689. 'value' => $hash,
  1690. 'data-foo' => 'bar',
  1691. ]],
  1692. ['input' => [
  1693. 'type' => 'hidden',
  1694. 'name' => '_Token[unlocked]',
  1695. 'value' => 'address%7Cfirst_name',
  1696. 'data-foo' => 'bar',
  1697. ]],
  1698. '/div',
  1699. ];
  1700. $this->assertHtml($expected, $result);
  1701. }
  1702. /**
  1703. * testFormSecureWithCustomNameAttribute method
  1704. *
  1705. * Test securing inputs with custom name attributes.
  1706. */
  1707. public function testFormSecureWithCustomNameAttribute(): void
  1708. {
  1709. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1710. $this->Form->create();
  1711. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1712. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1713. $this->assertSame('User.custom', $result[0]);
  1714. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1715. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1716. $this->assertSame('User.custom.another.value', $result[1]);
  1717. }
  1718. /**
  1719. * testFormSecuredControl method
  1720. *
  1721. * Test generation of entire secure form, assertions made on control() output.
  1722. */
  1723. public function testFormSecuredControl(): void
  1724. {
  1725. $this->View->setRequest($this->View->getRequest()
  1726. ->withAttribute('formTokenData', [])
  1727. ->withAttribute('csrfToken', 'testKey'));
  1728. $this->article['schema'] = [
  1729. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1730. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1731. ];
  1732. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1733. $encoding = strtolower(Configure::read('App.encoding'));
  1734. $expected = [
  1735. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1736. 'div' => ['style' => 'display:none;'],
  1737. ['input' => [
  1738. 'type' => 'hidden',
  1739. 'name' => '_csrfToken',
  1740. 'value' => 'testKey',
  1741. ]],
  1742. '/div',
  1743. ];
  1744. $this->assertHtml($expected, $result);
  1745. $result = $this->Form->control('ratio');
  1746. $expected = [
  1747. 'div' => ['class'],
  1748. 'label' => ['for'],
  1749. 'Ratio',
  1750. '/label',
  1751. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1752. '/div',
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. $result = $this->Form->control('population');
  1756. $expected = [
  1757. 'div' => ['class'],
  1758. 'label' => ['for'],
  1759. 'Population',
  1760. '/label',
  1761. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1762. '/div',
  1763. ];
  1764. $this->assertHtml($expected, $result);
  1765. $result = $this->Form->control('published', ['type' => 'text']);
  1766. $expected = [
  1767. 'div' => ['class' => 'input text'],
  1768. 'label' => ['for' => 'published'],
  1769. 'Published',
  1770. '/label',
  1771. ['input' => [
  1772. 'type' => 'text',
  1773. 'name' => 'published',
  1774. 'id' => 'published',
  1775. ]],
  1776. '/div',
  1777. ];
  1778. $this->assertHtml($expected, $result);
  1779. $result = $this->Form->control('other', ['type' => 'text']);
  1780. $expected = [
  1781. 'div' => ['class' => 'input text'],
  1782. 'label' => ['for' => 'other'],
  1783. 'Other',
  1784. '/label',
  1785. ['input' => [
  1786. 'type' => 'text',
  1787. 'name' => 'other',
  1788. 'id',
  1789. ]],
  1790. '/div',
  1791. ];
  1792. $this->assertHtml($expected, $result);
  1793. $result = $this->Form->hidden('stuff');
  1794. $expected = [
  1795. 'input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => 'stuff',
  1798. ],
  1799. ];
  1800. $this->assertHtml($expected, $result);
  1801. $result = $this->Form->hidden('hidden', ['value' => false]);
  1802. $expected = ['input' => [
  1803. 'type' => 'hidden',
  1804. 'name' => 'hidden',
  1805. 'value' => '0',
  1806. ]];
  1807. $this->assertHtml($expected, $result);
  1808. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1809. $expected = [
  1810. 'div' => ['class' => 'input checkbox'],
  1811. ['input' => [
  1812. 'type' => 'hidden',
  1813. 'name' => 'something',
  1814. 'value' => '0',
  1815. ]],
  1816. 'label' => ['for' => 'something'],
  1817. ['input' => [
  1818. 'type' => 'checkbox',
  1819. 'name' => 'something',
  1820. 'value' => '1',
  1821. 'id' => 'something',
  1822. ]],
  1823. 'Something',
  1824. '/label',
  1825. '/div',
  1826. ];
  1827. $this->assertHtml($expected, $result);
  1828. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1829. $expectedFields = [
  1830. 'ratio',
  1831. 'population',
  1832. 'published',
  1833. 'other',
  1834. 'stuff' => '',
  1835. 'hidden' => '0',
  1836. 'something',
  1837. ];
  1838. $this->assertEquals($expectedFields, $result);
  1839. $result = $this->Form->secure();
  1840. $tokenDebug = urlencode(json_encode([
  1841. '/articles/add',
  1842. $expectedFields,
  1843. [],
  1844. ]));
  1845. $expected = [
  1846. 'div' => ['style' => 'display:none;'],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[fields]',
  1850. 'value',
  1851. ]],
  1852. ['input' => [
  1853. 'type' => 'hidden',
  1854. 'name' => '_Token[unlocked]',
  1855. 'value' => '',
  1856. ]],
  1857. ['input' => [
  1858. 'type' => 'hidden', 'name' => '_Token[debug]',
  1859. 'value' => $tokenDebug,
  1860. ]],
  1861. '/div',
  1862. ];
  1863. $this->assertHtml($expected, $result);
  1864. $data = [
  1865. 'ratio' => '',
  1866. 'population' => '',
  1867. 'published' => '',
  1868. 'other' => '',
  1869. 'stuff' => '',
  1870. 'hidden' => '0',
  1871. 'something' => '',
  1872. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1873. ];
  1874. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1875. }
  1876. /**
  1877. * testSecuredControlCustomName method
  1878. *
  1879. * Test secured inputs with custom names.
  1880. */
  1881. public function testSecuredControlCustomName(): void
  1882. {
  1883. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1884. $this->Form->create();
  1885. $this->Form->text('text_input', [
  1886. 'name' => 'Option[General.default_role]',
  1887. ]);
  1888. $expected = ['Option.General.default_role'];
  1889. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1890. $this->assertEquals($expected, $result);
  1891. $this->Form->select('select_box', [1, 2], [
  1892. 'name' => 'Option[General.select_role]',
  1893. ]);
  1894. $expected[] = 'Option.General.select_role';
  1895. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1896. $this->assertEquals($expected, $result);
  1897. $this->Form->text('other.things[]');
  1898. $expected[] = 'other.things';
  1899. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1900. $this->assertEquals($expected, $result);
  1901. }
  1902. /**
  1903. * testSecuredControlDuplicate method
  1904. *
  1905. * Test that a hidden field followed by a visible field
  1906. * undoes the hidden field locking.
  1907. */
  1908. public function testSecuredControlDuplicate(): void
  1909. {
  1910. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1911. $this->Form->create();
  1912. $this->Form->control('text_val', [
  1913. 'type' => 'hidden',
  1914. 'value' => 'some text',
  1915. ]);
  1916. $expected = ['text_val' => 'some text'];
  1917. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1918. $this->assertEquals($expected, $result);
  1919. $this->Form->control('text_val', [
  1920. 'type' => 'text',
  1921. ]);
  1922. $expected = ['text_val'];
  1923. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1924. $this->assertEquals($expected, $result);
  1925. }
  1926. /**
  1927. * testFormSecuredFileControl method
  1928. *
  1929. * Tests that the correct keys are added to the field hash index.
  1930. */
  1931. public function testFormSecuredFileControl(): void
  1932. {
  1933. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1934. $this->Form->create();
  1935. $this->Form->file('Attachment.file');
  1936. $expected = ['Attachment.file'];
  1937. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1938. $this->assertEquals($expected, $result);
  1939. }
  1940. /**
  1941. * testFormSecuredMultipleSelect method
  1942. *
  1943. * Test that multiple selects keys are added to field hash.
  1944. */
  1945. public function testFormSecuredMultipleSelect(): void
  1946. {
  1947. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1948. $this->Form->create();
  1949. $options = ['1' => 'one', '2' => 'two'];
  1950. $this->Form->select('Model.select', $options);
  1951. $expected = ['Model.select'];
  1952. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1953. $this->assertEquals($expected, $result);
  1954. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1955. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1956. $this->assertEquals($expected, $result);
  1957. }
  1958. /**
  1959. * testFormSecuredRadio method
  1960. */
  1961. public function testFormSecuredRadio(): void
  1962. {
  1963. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1964. $this->Form->create();
  1965. $options = ['1' => 'option1', '2' => 'option2'];
  1966. $this->Form->radio('Test.test', $options);
  1967. $expected = ['Test.test'];
  1968. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1969. $this->assertEquals($expected, $result);
  1970. $this->Form->radio('Test.all', $options, [
  1971. 'disabled' => ['option1', 'option2'],
  1972. ]);
  1973. $expected = ['Test.test', 'Test.all' => ''];
  1974. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1975. $this->assertEquals($expected, $result);
  1976. $this->Form->radio('Test.some', $options, [
  1977. 'disabled' => ['option1'],
  1978. ]);
  1979. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1980. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1981. $this->assertEquals($expected, $result);
  1982. }
  1983. /**
  1984. * testFormSecuredAndDisabled method
  1985. *
  1986. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1987. * hashing.
  1988. */
  1989. public function testFormSecuredAndDisabled(): void
  1990. {
  1991. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1992. $this->Form->create();
  1993. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1994. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1995. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1996. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1997. $this->Form->year('Model.year', ['disabled' => true]);
  1998. $this->Form->month('Model.month', ['disabled' => true]);
  1999. $this->Form->text('Model.text', ['disabled' => true]);
  2000. $this->Form->password('Model.text', ['disabled' => true]);
  2001. $this->Form->day('Model.day', ['disabled' => true]);
  2002. $this->Form->hour('Model.hour', ['disabled' => true]);
  2003. $this->Form->minute('Model.minute', ['disabled' => true]);
  2004. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2005. $expected = [
  2006. 'Model.radio' => '',
  2007. ];
  2008. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2009. $this->assertEquals($expected, $result);
  2010. }
  2011. /**
  2012. * testUnlockFieldAddsToList method
  2013. *
  2014. * Test disableField.
  2015. */
  2016. public function testUnlockFieldAddsToList(): void
  2017. {
  2018. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2019. 'unlockedFields' => [],
  2020. ]));
  2021. $this->Form->create();
  2022. $this->Form->unlockField('Contact.name');
  2023. $this->Form->text('Contact.name');
  2024. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2025. $this->assertEquals([], $result);
  2026. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2027. $this->assertEquals(['Contact.name'], $result);
  2028. }
  2029. /**
  2030. * testUnlockFieldRemovingFromFields method
  2031. *
  2032. * Test unlockField removing from fields array.
  2033. */
  2034. public function testUnlockFieldRemovingFromFields(): void
  2035. {
  2036. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2037. 'unlockedFields' => [],
  2038. ]));
  2039. $this->Form->create($this->article);
  2040. $this->Form->hidden('Article.id', ['value' => 1]);
  2041. $this->Form->text('Article.title');
  2042. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2043. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2044. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2045. $this->Form->unlockField('Article.title');
  2046. $this->Form->unlockField('Article.id');
  2047. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2048. $this->assertEquals([], $result);
  2049. }
  2050. /**
  2051. * testResetUnlockFields method
  2052. *
  2053. * Test reset unlockFields, when create new form.
  2054. */
  2055. public function testResetUnlockFields(): void
  2056. {
  2057. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2058. 'key' => 'testKey',
  2059. 'unlockedFields' => [],
  2060. ]));
  2061. $this->Form->create();
  2062. $this->Form->unlockField('Contact.id');
  2063. $this->Form->hidden('Contact.id', ['value' => 1]);
  2064. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2065. $this->assertEmpty($result, 'Field should be unlocked');
  2066. $this->Form->end();
  2067. $this->Form->create();
  2068. $this->Form->hidden('Contact.id', ['value' => 1]);
  2069. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2070. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2071. }
  2072. /**
  2073. * testSecuredFormUrlIgnoresHost method
  2074. *
  2075. * Test that only the path + query elements of a form's URL show up in their hash.
  2076. */
  2077. public function testSecuredFormUrlIgnoresHost(): void
  2078. {
  2079. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2080. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2081. $this->Form->create($this->article, [
  2082. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2083. ]);
  2084. $result = $this->Form->secure();
  2085. $this->assertStringContainsString($expected, $result);
  2086. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2089. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2090. $result = $this->Form->secure();
  2091. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2092. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2093. $result = $this->Form->secure();
  2094. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2095. }
  2096. /**
  2097. * testSecuredFormUrlHasHtmlAndIdentifier method
  2098. *
  2099. * Test that URL, HTML and identifier show up in their hashes.
  2100. */
  2101. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2102. {
  2103. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2104. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2105. $this->Form->create($this->article, [
  2106. 'url' => [
  2107. 'controller' => 'articles',
  2108. 'action' => 'view',
  2109. '?' => [
  2110. 'page' => 1,
  2111. 'limit' => 10,
  2112. 'html' => '<>"',
  2113. ],
  2114. '#' => 'result',
  2115. ],
  2116. ]);
  2117. $result = $this->Form->secure();
  2118. $this->assertStringContainsString($expected, $result);
  2119. $this->Form->create($this->article, [
  2120. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2121. ]);
  2122. $result = $this->Form->secure();
  2123. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2124. $this->Form->create($this->article, [
  2125. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2126. ]);
  2127. $result = $this->Form->secure();
  2128. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2129. }
  2130. /**
  2131. * testErrorMessageDisplay method
  2132. *
  2133. * Test error message display.
  2134. */
  2135. public function testErrorMessageDisplay(): void
  2136. {
  2137. $this->article['errors'] = [
  2138. 'Article' => [
  2139. 'title' => 'error message',
  2140. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2141. ],
  2142. ];
  2143. $this->Form->create($this->article);
  2144. $result = $this->Form->control('Article.title');
  2145. $expected = [
  2146. 'div' => ['class' => 'input text error'],
  2147. 'label' => ['for' => 'article-title'],
  2148. 'Title',
  2149. '/label',
  2150. 'input' => [
  2151. 'type' => 'text',
  2152. 'name' => 'Article[title]',
  2153. 'id' => 'article-title',
  2154. 'class' => 'form-error',
  2155. 'aria-invalid' => 'true',
  2156. 'aria-describedby' => 'article-title-error',
  2157. ],
  2158. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2159. 'error message',
  2160. '/div',
  2161. '/div',
  2162. ];
  2163. $this->assertHtml($expected, $result);
  2164. $result = $this->Form->control('Article.title', [
  2165. 'templates' => [
  2166. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2167. ],
  2168. ]);
  2169. $expected = [
  2170. 'div' => ['class' => 'input text error'],
  2171. 'label' => ['for' => 'article-title'],
  2172. 'Title',
  2173. '/label',
  2174. 'input' => [
  2175. 'type' => 'text',
  2176. 'name' => 'Article[title]',
  2177. 'id' => 'article-title',
  2178. 'class' => 'form-error',
  2179. // No aria-describedby because error template is custom
  2180. 'aria-invalid' => 'true',
  2181. ],
  2182. '/div',
  2183. ];
  2184. $this->assertHtml($expected, $result);
  2185. $result = $this->Form->control('Article.content');
  2186. $expected = [
  2187. 'div' => ['class' => 'input text error'],
  2188. 'label' => ['for' => 'article-content'],
  2189. 'Content',
  2190. '/label',
  2191. 'input' => [
  2192. 'type' => 'text',
  2193. 'name' => 'Article[content]',
  2194. 'id' => 'article-content',
  2195. 'class' => 'form-error',
  2196. 'aria-invalid' => 'true',
  2197. 'aria-describedby' => 'article-content-error',
  2198. ],
  2199. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2200. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2201. '/div',
  2202. '/div',
  2203. ];
  2204. $this->assertHtml($expected, $result);
  2205. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2206. $expected = [
  2207. 'div' => ['class' => 'input text error'],
  2208. 'label' => ['for' => 'article-content'],
  2209. 'Content',
  2210. '/label',
  2211. 'input' => [
  2212. 'type' => 'text',
  2213. 'name' => 'Article[content]',
  2214. 'id' => 'article-content',
  2215. 'class' => 'form-error',
  2216. 'aria-invalid' => 'true',
  2217. 'aria-describedby' => 'article-content-error',
  2218. ],
  2219. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2220. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2221. '/div',
  2222. '/div',
  2223. ];
  2224. $this->assertHtml($expected, $result);
  2225. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2226. $expected = [
  2227. 'div' => ['class' => 'input text error'],
  2228. 'label' => ['for' => 'article-content'],
  2229. 'Content',
  2230. '/label',
  2231. 'input' => [
  2232. 'type' => 'text',
  2233. 'name' => 'Article[content]',
  2234. 'id' => 'article-content',
  2235. 'class' => 'form-error',
  2236. 'aria-invalid' => 'true',
  2237. 'aria-describedby' => 'article-content-error',
  2238. ],
  2239. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2240. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2241. '/div',
  2242. '/div',
  2243. ];
  2244. $this->assertHtml($expected, $result);
  2245. }
  2246. /**
  2247. * testEmptyErrorValidation method
  2248. *
  2249. * Test validation errors, when validation message is an empty string.
  2250. */
  2251. public function testEmptyErrorValidation(): void
  2252. {
  2253. $this->article['errors'] = [
  2254. 'Article' => ['title' => ''],
  2255. ];
  2256. $this->Form->create($this->article);
  2257. $result = $this->Form->control('Article.title');
  2258. $expected = [
  2259. 'div' => ['class' => 'input text error'],
  2260. 'label' => ['for' => 'article-title'],
  2261. 'Title',
  2262. '/label',
  2263. 'input' => [
  2264. 'type' => 'text',
  2265. 'name' => 'Article[title]',
  2266. 'id' => 'article-title',
  2267. 'class' => 'form-error',
  2268. 'aria-invalid' => 'true',
  2269. 'aria-describedby' => 'article-title-error',
  2270. ],
  2271. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2272. [],
  2273. '/div',
  2274. '/div',
  2275. ];
  2276. $this->assertHtml($expected, $result);
  2277. }
  2278. /**
  2279. * testEmptyControlErrorValidation method
  2280. *
  2281. * Test validation errors, when calling control() overriding validation message by an empty string.
  2282. */
  2283. public function testEmptyControlErrorValidation(): void
  2284. {
  2285. $this->article['errors'] = [
  2286. 'Article' => ['title' => 'error message'],
  2287. ];
  2288. $this->Form->create($this->article);
  2289. $result = $this->Form->control('Article.title', ['error' => '']);
  2290. $expected = [
  2291. 'div' => ['class' => 'input text error'],
  2292. 'label' => ['for' => 'article-title'],
  2293. 'Title',
  2294. '/label',
  2295. 'input' => [
  2296. 'aria-invalid' => 'true',
  2297. 'aria-describedby' => 'article-title-error',
  2298. 'type' => 'text',
  2299. 'name' => 'Article[title]',
  2300. 'id' => 'article-title',
  2301. 'class' => 'form-error',
  2302. ],
  2303. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2304. [],
  2305. '/div',
  2306. '/div',
  2307. ];
  2308. $this->assertHtml($expected, $result);
  2309. }
  2310. /**
  2311. * testControlErrorMessage method
  2312. *
  2313. * Test validation errors, when calling control() overriding validation messages.
  2314. */
  2315. public function testControlErrorMessage(): void
  2316. {
  2317. $this->article['errors'] = [
  2318. 'title' => ['error message'],
  2319. ];
  2320. $this->Form->create($this->article);
  2321. $result = $this->Form->control('title', [
  2322. 'error' => 'Custom error!',
  2323. ]);
  2324. $expected = [
  2325. 'div' => ['class' => 'input text required error'],
  2326. 'label' => ['for' => 'title'],
  2327. 'Title',
  2328. '/label',
  2329. 'input' => [
  2330. 'type' => 'text',
  2331. 'name' => 'title',
  2332. 'id' => 'title',
  2333. 'class' => 'form-error',
  2334. 'required' => 'required',
  2335. 'data-validity-message' => 'This field cannot be left empty',
  2336. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2337. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2338. 'aria-invalid' => 'true',
  2339. 'aria-describedby' => 'title-error',
  2340. ],
  2341. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2342. 'Custom error!',
  2343. '/div',
  2344. '/div',
  2345. ];
  2346. $this->assertHtml($expected, $result);
  2347. $result = $this->Form->control('title', [
  2348. 'error' => ['error message' => 'Custom error!'],
  2349. ]);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text required error'],
  2352. 'label' => ['for' => 'title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text',
  2357. 'name' => 'title',
  2358. 'id' => 'title',
  2359. 'aria-invalid' => 'true',
  2360. 'aria-describedby' => 'title-error',
  2361. 'class' => 'form-error',
  2362. 'required' => 'required',
  2363. 'data-validity-message' => 'This field cannot be left empty',
  2364. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2365. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2366. ],
  2367. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2368. 'Custom error!',
  2369. '/div',
  2370. '/div',
  2371. ];
  2372. $this->assertHtml($expected, $result);
  2373. }
  2374. /**
  2375. * testFormValidationAssociated method
  2376. *
  2377. * Tests displaying errors for nested entities.
  2378. */
  2379. public function testFormValidationAssociated(): void
  2380. {
  2381. $nested = new Entity(['foo' => 'bar']);
  2382. $nested->setError('foo', ['not a valid bar']);
  2383. $entity = new Entity(['nested' => $nested]);
  2384. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2385. $result = $this->Form->error('nested.foo');
  2386. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2387. }
  2388. /**
  2389. * testFormValidationAssociatedSecondLevel method
  2390. *
  2391. * Test form error display with associated model.
  2392. */
  2393. public function testFormValidationAssociatedSecondLevel(): void
  2394. {
  2395. $inner = new Entity(['bar' => 'baz']);
  2396. $nested = new Entity(['foo' => $inner]);
  2397. $entity = new Entity(['nested' => $nested]);
  2398. $inner->setError('bar', ['not a valid one']);
  2399. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2400. $result = $this->Form->error('nested.foo.bar');
  2401. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2402. }
  2403. /**
  2404. * testFormValidationMultiRecord method
  2405. *
  2406. * Test form error display with multiple records.
  2407. */
  2408. public function testFormValidationMultiRecord(): void
  2409. {
  2410. $one = new Entity();
  2411. $two = new Entity();
  2412. $this->getTableLocator()->get('Contacts', [
  2413. 'className' => ContactsTable::class,
  2414. ]);
  2415. $one->set('email', '');
  2416. $one->setError('email', ['invalid email']);
  2417. $two->set('name', '');
  2418. $two->setError('name', ['This is wrong']);
  2419. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2420. $result = $this->Form->control('0.email');
  2421. $expected = [
  2422. 'div' => ['class' => 'input email error'],
  2423. 'label' => ['for' => '0-email'],
  2424. 'Email',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'email',
  2428. 'name' => '0[email]',
  2429. 'id' => '0-email',
  2430. 'class' => 'form-error',
  2431. 'maxlength' => 255,
  2432. 'value' => '',
  2433. 'aria-invalid' => 'true',
  2434. 'aria-describedby' => '0-email-error',
  2435. ],
  2436. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2437. 'invalid email',
  2438. '/div',
  2439. '/div',
  2440. ];
  2441. $this->assertHtml($expected, $result);
  2442. $result = $this->Form->control('1.name');
  2443. $expected = [
  2444. 'div' => ['class' => 'input text error'],
  2445. 'label' => ['for' => '1-name'],
  2446. 'Name',
  2447. '/label',
  2448. 'input' => [
  2449. 'type' => 'text',
  2450. 'name' => '1[name]',
  2451. 'id' => '1-name',
  2452. 'class' => 'form-error',
  2453. 'maxlength' => 255,
  2454. 'value' => '',
  2455. 'aria-invalid' => 'true',
  2456. 'aria-describedby' => '1-name-error',
  2457. ],
  2458. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2459. 'This is wrong',
  2460. '/div',
  2461. '/div',
  2462. ];
  2463. $this->assertHtml($expected, $result);
  2464. }
  2465. /**
  2466. * testControl method
  2467. *
  2468. * Test various incarnations of control().
  2469. */
  2470. public function testControl(): void
  2471. {
  2472. $this->getTableLocator()->get('ValidateUsers', [
  2473. 'className' => ValidateUsersTable::class,
  2474. ]);
  2475. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2476. $result = $this->Form->control('ValidateUsers.balance');
  2477. $expected = [
  2478. 'div' => ['class'],
  2479. 'label' => ['for'],
  2480. 'Balance',
  2481. '/label',
  2482. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2483. '/div',
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. $result = $this->Form->control('ValidateUser.cost_decimal');
  2487. $expected = [
  2488. 'div' => ['class'],
  2489. 'label' => ['for'],
  2490. 'Cost Decimal',
  2491. '/label',
  2492. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2493. '/div',
  2494. ];
  2495. $this->assertHtml($expected, $result);
  2496. $result = $this->Form->control('ValidateUser.null_decimal');
  2497. $expected = [
  2498. 'div' => ['class'],
  2499. 'label' => ['for'],
  2500. 'Null Decimal',
  2501. '/label',
  2502. 'input' => ['name', 'type' => 'number', 'id'],
  2503. '/div',
  2504. ];
  2505. $this->assertHtml($expected, $result);
  2506. }
  2507. /**
  2508. * testControlCustomization method
  2509. *
  2510. * Tests the input method and passing custom options.
  2511. */
  2512. public function testControlCustomization(): void
  2513. {
  2514. $this->getTableLocator()->get('Contacts', [
  2515. 'className' => ContactsTable::class,
  2516. ]);
  2517. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2518. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2519. $expected = [
  2520. 'div' => ['class' => 'input email'],
  2521. 'label' => ['for' => 'custom'],
  2522. 'Email',
  2523. '/label',
  2524. ['input' => [
  2525. 'type' => 'email', 'name' => 'Contact[email]',
  2526. 'id' => 'custom', 'maxlength' => 255,
  2527. ]],
  2528. '/div',
  2529. ];
  2530. $this->assertHtml($expected, $result);
  2531. $result = $this->Form->control('Contact.email', [
  2532. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2533. ]);
  2534. $expected = [
  2535. '<div',
  2536. 'label' => ['for' => 'contact-email'],
  2537. 'Email',
  2538. '/label',
  2539. ['input' => [
  2540. 'type' => 'email', 'name' => 'Contact[email]',
  2541. 'id' => 'contact-email', 'maxlength' => 255,
  2542. ]],
  2543. '/div',
  2544. ];
  2545. $this->assertHtml($expected, $result);
  2546. $result = $this->Form->control('Contact.email', [
  2547. 'templates' => [
  2548. 'formGroup' => '{{input}}',
  2549. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2550. ],
  2551. ]);
  2552. $expected = [
  2553. '<div',
  2554. '<div',
  2555. 'label' => ['for' => 'contact-email'],
  2556. 'Email',
  2557. '/label',
  2558. '/div',
  2559. ['input' => [
  2560. 'type' => 'email', 'name' => 'Contact[email]',
  2561. 'id' => 'contact-email', 'maxlength' => 255,
  2562. ]],
  2563. '/div',
  2564. ];
  2565. $this->assertHtml($expected, $result);
  2566. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2567. $expected = [
  2568. 'div' => ['class' => 'input text'],
  2569. 'label' => ['for' => 'contact-email'],
  2570. 'Email',
  2571. '/label',
  2572. ['input' => [
  2573. 'type' => 'text', 'name' => 'Contact[email]',
  2574. 'id' => 'contact-email', 'maxlength' => '255',
  2575. ]],
  2576. '/div',
  2577. ];
  2578. $this->assertHtml($expected, $result);
  2579. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2580. $expected = [
  2581. 'div' => ['class' => 'input text'],
  2582. 'label' => ['for' => 'contact-5-email'],
  2583. 'Email',
  2584. '/label',
  2585. ['input' => [
  2586. 'type' => 'text', 'name' => 'Contact[5][email]',
  2587. 'id' => 'contact-5-email', 'maxlength' => '255',
  2588. ]],
  2589. '/div',
  2590. ];
  2591. $this->assertHtml($expected, $result);
  2592. $result = $this->Form->control('Contact.password');
  2593. $expected = [
  2594. 'div' => ['class' => 'input password'],
  2595. 'label' => ['for' => 'contact-password'],
  2596. 'Password',
  2597. '/label',
  2598. ['input' => [
  2599. 'type' => 'password', 'name' => 'Contact[password]',
  2600. 'id' => 'contact-password',
  2601. ]],
  2602. '/div',
  2603. ];
  2604. $this->assertHtml($expected, $result);
  2605. $result = $this->Form->control('Contact.email', [
  2606. 'type' => 'file', 'class' => 'textbox',
  2607. ]);
  2608. $expected = [
  2609. 'div' => ['class' => 'input file'],
  2610. 'label' => ['for' => 'contact-email'],
  2611. 'Email',
  2612. '/label',
  2613. ['input' => [
  2614. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2615. 'id' => 'contact-email',
  2616. ]],
  2617. '/div',
  2618. ];
  2619. $this->assertHtml($expected, $result);
  2620. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2621. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2622. $result = $this->Form->control('phone');
  2623. $expected = [
  2624. 'div' => ['class' => 'input tel'],
  2625. 'label' => ['for' => 'phone'],
  2626. 'Phone',
  2627. '/label',
  2628. ['input' => [
  2629. 'type' => 'tel', 'name' => 'phone',
  2630. 'value' => 'Hello &amp; World &gt; weird chars',
  2631. 'id' => 'phone', 'maxlength' => 255,
  2632. ]],
  2633. '/div',
  2634. ];
  2635. $this->assertHtml($expected, $result);
  2636. $this->View->setRequest(
  2637. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2638. );
  2639. $this->Form->create();
  2640. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2641. $expected = [
  2642. 'div' => ['class' => 'input text'],
  2643. 'label' => ['for' => 'myId'],
  2644. 'Field',
  2645. '/label',
  2646. 'input' => [
  2647. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2648. 'value' => 'My value', 'id' => 'myId',
  2649. ],
  2650. '/div',
  2651. ];
  2652. $this->assertHtml($expected, $result);
  2653. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2654. $this->Form->create();
  2655. $entity->setError('field', 'Badness!');
  2656. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2657. $result = $this->Form->control('field');
  2658. $expected = [
  2659. 'div' => ['class' => 'input text error'],
  2660. 'label' => ['for' => 'field'],
  2661. 'Field',
  2662. '/label',
  2663. 'input' => [
  2664. 'type' => 'text',
  2665. 'name' => 'field',
  2666. 'id' => 'field',
  2667. 'class' => 'form-error',
  2668. 'aria-invalid' => 'true',
  2669. 'aria-describedby' => 'field-error',
  2670. ],
  2671. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2672. 'Badness!',
  2673. '/div',
  2674. '/div',
  2675. ];
  2676. $this->assertHtml($expected, $result);
  2677. $result = $this->Form->control('field', [
  2678. 'templates' => [
  2679. 'inputContainerError' => '{{content}}{{error}}',
  2680. 'error' => '<span class="error-message">{{content}}</span>',
  2681. ],
  2682. ]);
  2683. $expected = [
  2684. 'label' => ['for' => 'field'],
  2685. 'Field',
  2686. '/label',
  2687. 'input' => [
  2688. 'type' => 'text',
  2689. 'name' => 'field',
  2690. 'id' => 'field',
  2691. 'class' => 'form-error',
  2692. // No aria-describedby because error template is custom
  2693. 'aria-invalid' => 'true',
  2694. ],
  2695. ['span' => ['class' => 'error-message']],
  2696. 'Badness!',
  2697. '/span',
  2698. ];
  2699. $this->assertHtml($expected, $result);
  2700. $entity->setError('field', ['minLength'], true);
  2701. $result = $this->Form->control('field', [
  2702. 'error' => [
  2703. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2704. 'maxLength' => 'login too large',
  2705. ],
  2706. ]);
  2707. $expected = [
  2708. 'div' => ['class' => 'input text error'],
  2709. 'label' => ['for' => 'field'],
  2710. 'Field',
  2711. '/label',
  2712. 'input' => [
  2713. 'type' => 'text',
  2714. 'name' => 'field',
  2715. 'id' => 'field',
  2716. 'class' => 'form-error',
  2717. 'aria-invalid' => 'true',
  2718. 'aria-describedby' => 'field-error',
  2719. ],
  2720. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2721. 'Le login doit contenir au moins 2 caractères',
  2722. '/div',
  2723. '/div',
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $entity->setError('field', ['maxLength'], true);
  2727. $result = $this->Form->control('field', [
  2728. 'error' => [
  2729. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2730. 'maxLength' => 'login too large',
  2731. ],
  2732. ]);
  2733. $expected = [
  2734. 'div' => ['class' => 'input text error'],
  2735. 'label' => ['for' => 'field'],
  2736. 'Field',
  2737. '/label',
  2738. 'input' => [
  2739. 'type' => 'text',
  2740. 'name' => 'field',
  2741. 'id' => 'field',
  2742. 'class' => 'form-error',
  2743. 'aria-invalid' => 'true',
  2744. 'aria-describedby' => 'field-error',
  2745. ],
  2746. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2747. 'login too large',
  2748. '/div',
  2749. '/div',
  2750. ];
  2751. $this->assertHtml($expected, $result);
  2752. }
  2753. /**
  2754. * testControlWithTemplateFile method
  2755. *
  2756. * Test that control() accepts a template file.
  2757. */
  2758. public function testControlWithTemplateFile(): void
  2759. {
  2760. $result = $this->Form->control('field', [
  2761. 'templates' => 'htmlhelper_tags',
  2762. ]);
  2763. $expected = [
  2764. 'label' => ['for' => 'field'],
  2765. 'Field',
  2766. '/label',
  2767. 'input' => [
  2768. 'type' => 'text', 'name' => 'field',
  2769. 'id' => 'field',
  2770. ],
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. }
  2774. /**
  2775. * testNestedControlsEndWithBrackets method
  2776. *
  2777. * Test that nested inputs end with brackets.
  2778. */
  2779. public function testNestedControlsEndWithBrackets(): void
  2780. {
  2781. $result = $this->Form->text('nested.text[]');
  2782. $expected = [
  2783. 'input' => [
  2784. 'type' => 'text', 'name' => 'nested[text][]',
  2785. ],
  2786. ];
  2787. $this->assertHtml($expected, $result);
  2788. $result = $this->Form->file('nested.file[]');
  2789. $expected = [
  2790. 'input' => [
  2791. 'type' => 'file', 'name' => 'nested[file][]',
  2792. ],
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. }
  2796. /**
  2797. * testCreateIdPrefix method
  2798. *
  2799. * Test id prefix.
  2800. */
  2801. public function testCreateIdPrefix(): void
  2802. {
  2803. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2804. $result = $this->Form->control('field');
  2805. $expected = [
  2806. 'div' => ['class' => 'input text'],
  2807. 'label' => ['for' => 'prefix-field'],
  2808. 'Field',
  2809. '/label',
  2810. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2811. '/div',
  2812. ];
  2813. $this->assertHtml($expected, $result);
  2814. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2815. $expected = [
  2816. 'div' => ['class' => 'input text'],
  2817. 'label' => ['for' => 'custom-id'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2821. '/div',
  2822. ];
  2823. $this->assertHtml($expected, $result);
  2824. $result = $this->Form->radio('Model.field', ['option A']);
  2825. $expected = [
  2826. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2827. 'label' => ['for' => 'prefix-model-field-0'],
  2828. ['input' => [
  2829. 'type' => 'radio',
  2830. 'name' => 'Model[field]',
  2831. 'value' => '0',
  2832. 'id' => 'prefix-model-field-0',
  2833. ]],
  2834. 'option A',
  2835. '/label',
  2836. ];
  2837. $this->assertHtml($expected, $result);
  2838. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2839. $expected = [
  2840. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2841. 'label' => ['for' => 'prefix-model-field-0'],
  2842. ['input' => [
  2843. 'type' => 'radio',
  2844. 'name' => 'Model[field]',
  2845. 'value' => '0',
  2846. 'id' => 'prefix-model-field-0',
  2847. ]],
  2848. 'option A',
  2849. '/label',
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $result = $this->Form->select(
  2853. 'Model.multi_field',
  2854. ['first'],
  2855. ['multiple' => 'checkbox']
  2856. );
  2857. $expected = [
  2858. 'input' => [
  2859. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2860. ],
  2861. ['div' => ['class' => 'checkbox']],
  2862. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2863. ['input' => [
  2864. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2865. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2866. ]],
  2867. 'first',
  2868. '/label',
  2869. '/div',
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $this->Form->end();
  2873. $result = $this->Form->control('field');
  2874. $expected = [
  2875. 'div' => ['class' => 'input text'],
  2876. 'label' => ['for' => 'field'],
  2877. 'Field',
  2878. '/label',
  2879. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2880. '/div',
  2881. ];
  2882. $this->assertHtml($expected, $result);
  2883. }
  2884. /**
  2885. * testControlZero method
  2886. *
  2887. * Test that inputs with 0 can be created.
  2888. */
  2889. public function testControlZero(): void
  2890. {
  2891. $this->getTableLocator()->get('Contacts', [
  2892. 'className' => ContactsTable::class,
  2893. ]);
  2894. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2895. $result = $this->Form->control('0');
  2896. $expected = [
  2897. 'div' => ['class' => 'input text'],
  2898. 'label' => ['for' => '0'], '/label',
  2899. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2900. '/div',
  2901. ];
  2902. $this->assertHtml($expected, $result);
  2903. }
  2904. /**
  2905. * testControlCheckbox method
  2906. *
  2907. * Test control() with checkbox creation.
  2908. */
  2909. public function testControlCheckbox(): void
  2910. {
  2911. $articles = $this->getTableLocator()->get('Articles');
  2912. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2913. $article = $articles->newEmptyEntity();
  2914. $this->Form->create($article);
  2915. $result = $this->Form->control('Articles.active');
  2916. $expected = [
  2917. 'div' => ['class' => 'input checkbox'],
  2918. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2919. 'label' => ['for' => 'articles-active'],
  2920. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2921. 'Active',
  2922. '/label',
  2923. '/div',
  2924. ];
  2925. $this->assertHtml($expected, $result);
  2926. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2927. $expected = [
  2928. 'div' => ['class' => 'input checkbox'],
  2929. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2930. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2931. '/div',
  2932. ];
  2933. $this->assertHtml($expected, $result);
  2934. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2935. $expected = [
  2936. 'div' => ['class' => 'input checkbox'],
  2937. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2938. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2939. '/div',
  2940. ];
  2941. $this->assertHtml($expected, $result);
  2942. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2943. $expected = [
  2944. 'div' => ['class' => 'input checkbox'],
  2945. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2946. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2947. '/div',
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->control('Articles.disabled', [
  2951. 'label' => 'Disabled',
  2952. 'type' => 'checkbox',
  2953. 'data-foo' => 'disabled',
  2954. ]);
  2955. $expected = [
  2956. 'div' => ['class' => 'input checkbox'],
  2957. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2958. 'label' => ['for' => 'articles-disabled'],
  2959. ['input' => [
  2960. 'type' => 'checkbox',
  2961. 'name' => 'Articles[disabled]',
  2962. 'value' => '1',
  2963. 'id' => 'articles-disabled',
  2964. 'data-foo' => 'disabled',
  2965. ]],
  2966. 'Disabled',
  2967. '/label',
  2968. '/div',
  2969. ];
  2970. $this->assertHtml($expected, $result);
  2971. $result = $this->Form->control('Articles.confirm', [
  2972. 'label' => 'Confirm <b>me</b>!',
  2973. 'type' => 'checkbox',
  2974. 'escape' => false,
  2975. ]);
  2976. $expected = [
  2977. 'div' => ['class' => 'input checkbox'],
  2978. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2979. 'label' => ['for' => 'articles-confirm'],
  2980. ['input' => [
  2981. 'type' => 'checkbox',
  2982. 'name' => 'Articles[confirm]',
  2983. 'value' => '1',
  2984. 'id' => 'articles-confirm',
  2985. ]],
  2986. 'Confirm <b>me</b>!',
  2987. '/label',
  2988. '/div',
  2989. ];
  2990. $this->assertHtml($expected, $result);
  2991. }
  2992. /**
  2993. * testControlHidden method
  2994. *
  2995. * Test that control() does not create wrapping div and label tag for hidden fields.
  2996. */
  2997. public function testControlHidden(): void
  2998. {
  2999. $this->getTableLocator()->get('ValidateUsers', [
  3000. 'className' => ValidateUsersTable::class,
  3001. ]);
  3002. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3003. $result = $this->Form->control('ValidateUser.id');
  3004. $expected = [
  3005. 'input' => ['name', 'type' => 'hidden', 'id'],
  3006. ];
  3007. $this->assertHtml($expected, $result);
  3008. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3009. $expected = [
  3010. 'input' => ['name', 'type' => 'hidden', 'id'],
  3011. ];
  3012. $this->assertHtml($expected, $result);
  3013. }
  3014. /**
  3015. * testControlDatetime method
  3016. *
  3017. * Test form->control() with datetime.
  3018. */
  3019. public function testControlDatetime(): void
  3020. {
  3021. $result = $this->Form->control('prueba', [
  3022. 'type' => 'datetime',
  3023. 'value' => new DateTime('2019-09-27 02:52:43'),
  3024. ]);
  3025. $expected = [
  3026. 'div' => ['class' => 'input datetime'],
  3027. 'label' => ['for' => 'prueba'],
  3028. 'Prueba',
  3029. '/label',
  3030. 'input' => [
  3031. 'name' => 'prueba',
  3032. 'id' => 'prueba',
  3033. 'type' => 'datetime-local',
  3034. 'value' => '2019-09-27T02:52:43',
  3035. 'step' => '1',
  3036. ],
  3037. '/div',
  3038. ];
  3039. $this->assertHtml($expected, $result);
  3040. }
  3041. /**
  3042. * testControlDatetimeIdPrefix method
  3043. *
  3044. * Test form->control() with datetime with id prefix.
  3045. */
  3046. public function testControlDatetimeIdPrefix(): void
  3047. {
  3048. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3049. $result = $this->Form->control('prueba', [
  3050. 'type' => 'datetime',
  3051. ]);
  3052. $expected = [
  3053. 'div' => ['class' => 'input datetime'],
  3054. 'label' => ['for' => 'prefix-prueba'],
  3055. 'Prueba',
  3056. '/label',
  3057. 'input' => [
  3058. 'name' => 'prueba',
  3059. 'id' => 'prefix-prueba',
  3060. 'type' => 'datetime-local',
  3061. 'value' => '',
  3062. 'step' => '1',
  3063. ],
  3064. '/div',
  3065. ];
  3066. $this->assertHtml($expected, $result);
  3067. }
  3068. /**
  3069. * testControlDatetimeStep method
  3070. *
  3071. * Test form->control() with datetime with custom step size.
  3072. */
  3073. public function testControlDatetimeStep(): void
  3074. {
  3075. $result = $this->Form->control('prueba', [
  3076. 'type' => 'datetime',
  3077. 'value' => new DateTime('2019-09-27 02:52:43'),
  3078. 'step' => '0.5',
  3079. ]);
  3080. $expected = [
  3081. 'div' => ['class' => 'input datetime'],
  3082. 'label' => ['for' => 'prueba'],
  3083. 'Prueba',
  3084. '/label',
  3085. 'input' => [
  3086. 'name' => 'prueba',
  3087. 'id' => 'prueba',
  3088. 'type' => 'datetime-local',
  3089. 'value' => '2019-09-27T02:52:43.000',
  3090. 'step' => '0.5',
  3091. ],
  3092. '/div',
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. }
  3096. /**
  3097. * testControlCheckboxWithDisabledElements method
  3098. *
  3099. * Test generating checkboxes with disabled elements.
  3100. */
  3101. public function testControlCheckboxWithDisabledElements(): void
  3102. {
  3103. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3104. $result = $this->Form->control('Contact.multiple', [
  3105. 'multiple' => 'checkbox',
  3106. 'disabled' => 'disabled',
  3107. 'options' => $options,
  3108. ]);
  3109. $expected = [
  3110. ['div' => ['class' => 'input select']],
  3111. ['label' => ['for' => 'contact-multiple']],
  3112. 'Multiple',
  3113. '/label',
  3114. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3115. ['div' => ['class' => 'checkbox']],
  3116. ['label' => ['for' => 'contact-multiple-1']],
  3117. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3118. 'One',
  3119. '/label',
  3120. '/div',
  3121. ['div' => ['class' => 'checkbox']],
  3122. ['label' => ['for' => 'contact-multiple-2']],
  3123. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3124. 'Two',
  3125. '/label',
  3126. '/div',
  3127. ['div' => ['class' => 'checkbox']],
  3128. ['label' => ['for' => 'contact-multiple-3']],
  3129. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3130. 'Three',
  3131. '/label',
  3132. '/div',
  3133. '/div',
  3134. ];
  3135. $this->assertHtml($expected, $result);
  3136. // make sure 50 does only disable 50, and not 50f5c0cf
  3137. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3138. $disabled = [50];
  3139. $expected = [
  3140. ['div' => ['class' => 'input select']],
  3141. ['label' => ['for' => 'contact-multiple']],
  3142. 'Multiple',
  3143. '/label',
  3144. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3145. ['div' => ['class' => 'checkbox']],
  3146. ['label' => ['for' => 'contact-multiple-50']],
  3147. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3148. 'Fifty',
  3149. '/label',
  3150. '/div',
  3151. ['div' => ['class' => 'checkbox']],
  3152. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3153. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3154. 'Stringy',
  3155. '/label',
  3156. '/div',
  3157. '/div',
  3158. ];
  3159. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3160. $this->assertHtml($expected, $result);
  3161. }
  3162. /**
  3163. * testControlWithLeadingInteger method
  3164. *
  3165. * Test input name with leading integer, ensure attributes are generated correctly.
  3166. */
  3167. public function testControlWithLeadingInteger(): void
  3168. {
  3169. $result = $this->Form->text('0.Node.title');
  3170. $expected = [
  3171. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3172. ];
  3173. $this->assertHtml($expected, $result);
  3174. }
  3175. /**
  3176. * testControlSelectType method
  3177. *
  3178. * Test form->control() with select type inputs.
  3179. */
  3180. public function testControlSelectType(): void
  3181. {
  3182. $result = $this->Form->control(
  3183. 'email',
  3184. [
  3185. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3186. );
  3187. $expected = [
  3188. 'div' => ['class' => 'input select'],
  3189. 'label' => ['for' => 'email'],
  3190. 'Email',
  3191. '/label',
  3192. ['select' => ['name' => 'email', 'id' => 'email']],
  3193. ['option' => ['value' => '']],
  3194. '/option',
  3195. ['option' => ['value' => 'è']],
  3196. 'Firést',
  3197. '/option',
  3198. ['option' => ['value' => 'é']],
  3199. 'Secoènd',
  3200. '/option',
  3201. '/select',
  3202. '/div',
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $result = $this->Form->control(
  3206. 'email',
  3207. [
  3208. 'options' => ['First', 'Second'], 'empty' => true]
  3209. );
  3210. $expected = [
  3211. 'div' => ['class' => 'input select'],
  3212. 'label' => ['for' => 'email'],
  3213. 'Email',
  3214. '/label',
  3215. ['select' => ['name' => 'email', 'id' => 'email']],
  3216. ['option' => ['value' => '']],
  3217. '/option',
  3218. ['option' => ['value' => '0']],
  3219. 'First',
  3220. '/option',
  3221. ['option' => ['value' => '1']],
  3222. 'Second',
  3223. '/option',
  3224. '/select',
  3225. '/div',
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. $result = $this->Form->control('email', [
  3229. 'type' => 'select',
  3230. 'options' => new ArrayObject(['First', 'Second']),
  3231. 'empty' => true,
  3232. ]);
  3233. $this->assertHtml($expected, $result);
  3234. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3235. $this->View->setRequest(
  3236. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3237. );
  3238. $this->Form->create();
  3239. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3240. $expected = [
  3241. 'div' => ['class' => 'input select'],
  3242. 'label' => ['for' => 'model-user-id'],
  3243. 'User',
  3244. '/label',
  3245. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3246. ['option' => ['value' => '']],
  3247. '/option',
  3248. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3249. 'good',
  3250. '/option',
  3251. ['option' => ['value' => 'other']],
  3252. 'bad',
  3253. '/option',
  3254. '/select',
  3255. '/div',
  3256. ];
  3257. $this->assertHtml($expected, $result);
  3258. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3259. $this->View->setRequest(
  3260. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3261. );
  3262. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3263. $expected = [
  3264. 'div' => ['class' => 'input select'],
  3265. 'label' => ['for' => 'thing-user-id'],
  3266. 'User',
  3267. '/label',
  3268. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3269. ['option' => ['value' => '']],
  3270. 'Some Empty',
  3271. '/option',
  3272. ['option' => ['value' => 'value']],
  3273. 'good',
  3274. '/option',
  3275. ['option' => ['value' => 'other']],
  3276. 'bad',
  3277. '/option',
  3278. '/select',
  3279. '/div',
  3280. ];
  3281. $this->assertHtml($expected, $result);
  3282. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3283. $this->View->setRequest(
  3284. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3285. );
  3286. $this->Form->create();
  3287. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3288. $expected = [
  3289. 'div' => ['class' => 'input select'],
  3290. 'label' => ['for' => 'thing-user-id'],
  3291. 'User',
  3292. '/label',
  3293. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3294. ['option' => ['value' => '']],
  3295. 'Some Empty',
  3296. '/option',
  3297. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3298. 'good',
  3299. '/option',
  3300. ['option' => ['value' => 'other']],
  3301. 'bad',
  3302. '/option',
  3303. '/select',
  3304. '/div',
  3305. ];
  3306. $this->assertHtml($expected, $result);
  3307. $result = $this->Form->control('Publisher.id', [
  3308. 'label' => 'Publisher',
  3309. 'type' => 'select',
  3310. 'multiple' => 'checkbox',
  3311. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3312. ]);
  3313. $expected = [
  3314. ['div' => ['class' => 'input select']],
  3315. ['label' => ['for' => 'publisher-id']],
  3316. 'Publisher',
  3317. '/label',
  3318. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'publisher-id-value-1']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3322. 'Label 1',
  3323. '/label',
  3324. '/div',
  3325. ['div' => ['class' => 'checkbox']],
  3326. ['label' => ['for' => 'publisher-id-value-2']],
  3327. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3328. 'Label 2',
  3329. '/label',
  3330. '/div',
  3331. '/div',
  3332. ];
  3333. $this->assertHtml($expected, $result);
  3334. $articlesTable = $this->getTableLocator()->get('Articles');
  3335. $articlesTable->getSchema()->setColumnType(
  3336. 'published',
  3337. EnumType::from(ArticleStatus::class)
  3338. );
  3339. $this->Form->create($articlesTable->newEmptyEntity());
  3340. $result = $this->Form->control('published');
  3341. $expected = [
  3342. 'div' => ['class' => 'input select'],
  3343. 'label' => ['for' => 'published'],
  3344. 'Published',
  3345. '/label',
  3346. 'select' => ['name' => 'published', 'id' => 'published'],
  3347. ['option' => ['value' => 'Y']],
  3348. 'Published',
  3349. '/option',
  3350. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3351. 'Unpublished',
  3352. '/option',
  3353. '/select',
  3354. '/div',
  3355. ];
  3356. $this->assertHtml($expected, $result);
  3357. $articlesTable->getSchema()->setColumnType(
  3358. 'published',
  3359. EnumType::from(ArticleStatusLabel::class)
  3360. );
  3361. $this->Form->create($articlesTable->newEmptyEntity());
  3362. $result = $this->Form->control('published');
  3363. $expected = [
  3364. 'div' => ['class' => 'input select'],
  3365. 'label' => ['for' => 'published'],
  3366. 'Published',
  3367. '/label',
  3368. 'select' => ['name' => 'published', 'id' => 'published'],
  3369. ['option' => ['value' => 'Y']],
  3370. 'Is Published',
  3371. '/option',
  3372. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3373. 'Is Unpublished',
  3374. '/option',
  3375. '/select',
  3376. '/div',
  3377. ];
  3378. $this->assertHtml($expected, $result);
  3379. $articlesTable->getSchema()->setColumnType(
  3380. 'published',
  3381. EnumType::from(ArticleStatusLabelInterface::class)
  3382. );
  3383. $this->Form->create($articlesTable->newEmptyEntity());
  3384. $result = $this->Form->control('published');
  3385. $this->assertHtml($expected, $result);
  3386. $articlePriosTable = $this->getTableLocator()->get('ArticlePrios');
  3387. $articlePriosTable->getSchema()->setColumnType(
  3388. 'priority',
  3389. EnumType::from(Priority::class)
  3390. );
  3391. $this->Form->create($articlePriosTable->newEmptyEntity());
  3392. // Select empty by default
  3393. $result = $this->Form->control('priority', ['empty' => ['' => ' - please select - '], 'default' => '']);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'priority'],
  3397. 'Priority',
  3398. '/label',
  3399. 'select' => ['name' => 'priority', 'id' => 'priority'],
  3400. ['option' => ['value' => '', 'selected' => 'selected']],
  3401. ' - please select - ',
  3402. '/option',
  3403. ['option' => ['value' => '1']],
  3404. 'Is Low',
  3405. '/option',
  3406. ['option' => ['value' => '2']],
  3407. 'Is Medium',
  3408. '/option',
  3409. ['option' => ['value' => '3']],
  3410. 'Is High',
  3411. '/option',
  3412. '/select',
  3413. '/div',
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. }
  3417. /**
  3418. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3419. *
  3420. * Test that control() and a non standard primary key makes a hidden input by default.
  3421. */
  3422. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3423. {
  3424. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3425. $this->Form->create($this->article);
  3426. $result = $this->Form->control('title');
  3427. $expected = [
  3428. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3429. ];
  3430. $this->assertHtml($expected, $result);
  3431. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3432. $this->Form->create($this->article);
  3433. $result = $this->Form->control('title');
  3434. $expected = [
  3435. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. $result = $this->Form->control('body');
  3439. $expected = [
  3440. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3441. ];
  3442. $this->assertHtml($expected, $result);
  3443. }
  3444. /**
  3445. * testControlOverridingMagicSelectType method
  3446. *
  3447. * Test that overriding the magic select type widget is possible.
  3448. */
  3449. public function testControlOverridingMagicSelectType(): void
  3450. {
  3451. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3452. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3453. $expected = [
  3454. 'div' => ['class' => 'input text'],
  3455. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3456. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3457. '/div',
  3458. ];
  3459. $this->assertHtml($expected, $result);
  3460. //Check that magic types still work for plural/singular vars
  3461. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3462. $result = $this->Form->control('Model.type');
  3463. $expected = [
  3464. 'div' => ['class' => 'input select'],
  3465. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3466. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3467. ['option' => ['value' => 'value']], 'good', '/option',
  3468. ['option' => ['value' => 'other']], 'bad', '/option',
  3469. '/select',
  3470. '/div',
  3471. ];
  3472. $this->assertHtml($expected, $result);
  3473. }
  3474. /**
  3475. * testControlMagicTypeDoesNotOverride method
  3476. *
  3477. * Test that inferred types do not override developer input.
  3478. */
  3479. public function testControlMagicTypeDoesNotOverride(): void
  3480. {
  3481. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3482. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3483. $expected = [
  3484. 'div' => ['class' => 'input checkbox'],
  3485. ['input' => [
  3486. 'type' => 'hidden',
  3487. 'name' => 'Model[user]',
  3488. 'value' => 0,
  3489. ]],
  3490. 'label' => ['for' => 'model-user'],
  3491. ['input' => [
  3492. 'name' => 'Model[user]',
  3493. 'type' => 'checkbox',
  3494. 'id' => 'model-user',
  3495. 'value' => 1,
  3496. ]],
  3497. 'User',
  3498. '/label',
  3499. '/div',
  3500. ];
  3501. $this->assertHtml($expected, $result);
  3502. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3503. $options = [
  3504. 1 => 'blue',
  3505. 2 => 'red',
  3506. ];
  3507. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3508. $expected = [
  3509. 'div' => ['class' => 'input select'],
  3510. 'label' => ['for' => 'tags-ids'],
  3511. 'Tags',
  3512. '/label',
  3513. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3514. ['div' => ['class' => 'checkbox']],
  3515. ['label' => ['for' => 'tags-ids-1']],
  3516. ['input' => [
  3517. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3518. 'value' => '1', 'name' => 'tags[_ids][]',
  3519. ]],
  3520. 'blue',
  3521. '/label',
  3522. '/div',
  3523. ['div' => ['class' => 'checkbox']],
  3524. ['label' => ['for' => 'tags-ids-2']],
  3525. ['input' => [
  3526. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3527. 'value' => '2', 'name' => 'tags[_ids][]',
  3528. ]],
  3529. 'red',
  3530. '/label',
  3531. '/div',
  3532. '/div',
  3533. ];
  3534. $this->assertHtml($expected, $result);
  3535. }
  3536. /**
  3537. * testControlMagicSelectForTypeNumber method
  3538. *
  3539. * Test that magic control() selects are created for type=number.
  3540. */
  3541. public function testControlMagicSelectForTypeNumber(): void
  3542. {
  3543. $this->getTableLocator()->get('ValidateUsers', [
  3544. 'className' => ValidateUsersTable::class,
  3545. ]);
  3546. $entity = new Entity(['balance' => 1]);
  3547. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3548. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3549. $result = $this->Form->control('balance');
  3550. $expected = [
  3551. 'div' => ['class' => 'input select'],
  3552. 'label' => ['for' => 'balance'],
  3553. 'Balance',
  3554. '/label',
  3555. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3556. ['option' => ['value' => '0']],
  3557. 'nothing',
  3558. '/option',
  3559. ['option' => ['value' => '1', 'selected' => 'selected']],
  3560. 'some',
  3561. '/option',
  3562. ['option' => ['value' => '100']],
  3563. 'a lot',
  3564. '/option',
  3565. '/select',
  3566. '/div',
  3567. ];
  3568. $this->assertHtml($expected, $result);
  3569. }
  3570. /**
  3571. * testInvalidControlTypeOption method
  3572. *
  3573. * Test invalid 'input' type option to control() function.
  3574. */
  3575. public function testInvalidControlTypeOption(): void
  3576. {
  3577. $this->expectException(InvalidArgumentException::class);
  3578. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3579. $this->Form->control('text', ['type' => 'input']);
  3580. }
  3581. /**
  3582. * testControlMagicSelectChangeToRadio method
  3583. *
  3584. * Test that magic control() selects can easily be converted into radio types without error.
  3585. */
  3586. public function testControlMagicSelectChangeToRadio(): void
  3587. {
  3588. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3589. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3590. $this->assertStringContainsString('input type="radio"', $result);
  3591. }
  3592. /**
  3593. * testFormControlSubmit method
  3594. *
  3595. * Test correct results for form::control() and type submit.
  3596. */
  3597. public function testFormControlSubmit(): void
  3598. {
  3599. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3600. $expected = [
  3601. 'div' => ['class' => 'submit'],
  3602. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3603. '/div',
  3604. ];
  3605. $this->assertHtml($expected, $result);
  3606. }
  3607. /**
  3608. * testFormControls method
  3609. *
  3610. * Test correct results from Form::controls().
  3611. */
  3612. public function testFormControlsLegendFieldset(): void
  3613. {
  3614. $this->Form->create($this->article);
  3615. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3616. $expected = [
  3617. '<fieldset',
  3618. '<legend',
  3619. 'The Legend',
  3620. '/legend',
  3621. '*/fieldset',
  3622. ];
  3623. $this->assertHtml($expected, $result);
  3624. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3625. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3626. $this->assertStringContainsString('<fieldset>', $result);
  3627. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3628. $this->assertStringNotContainsString('<legend>', $result);
  3629. $this->assertStringNotContainsString('<fieldset>', $result);
  3630. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3631. $this->assertStringNotContainsString('<legend>', $result);
  3632. $this->assertStringNotContainsString('<fieldset>', $result);
  3633. $this->Form->create($this->article);
  3634. $this->View->setRequest($this->View->getRequest()
  3635. ->withParam('prefix', 'admin')
  3636. ->withParam('action', 'admin_edit')
  3637. ->withParam('controller', 'articles'));
  3638. $result = $this->Form->allControls();
  3639. $expected = [
  3640. '<fieldset',
  3641. '<legend',
  3642. 'New Article',
  3643. '/legend',
  3644. '*/fieldset',
  3645. ];
  3646. $this->assertHtml($expected, $result);
  3647. $this->Form->create($this->article);
  3648. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3649. $expected = [
  3650. '<fieldset',
  3651. '<legend',
  3652. 'The Legend',
  3653. '/legend',
  3654. '*/fieldset',
  3655. ];
  3656. $this->assertHtml($expected, $result);
  3657. $this->Form->create($this->article);
  3658. $result = $this->Form->allControls([], [
  3659. 'fieldset' => [
  3660. 'class' => 'some-class some-other-class',
  3661. 'disabled' => true,
  3662. 'data-param' => 'a-param',
  3663. ],
  3664. 'legend' => 'The Legend',
  3665. ]);
  3666. $expected = [
  3667. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3668. '<legend',
  3669. 'The Legend',
  3670. '/legend',
  3671. '*/fieldset',
  3672. ];
  3673. $this->assertHtml($expected, $result);
  3674. }
  3675. /**
  3676. * testFormControls method
  3677. *
  3678. * Test the controls() method.
  3679. */
  3680. public function testFormControls(): void
  3681. {
  3682. $this->Form->create($this->article);
  3683. $result = $this->Form->allControls();
  3684. $expected = [
  3685. '<fieldset',
  3686. '<legend', 'New Article', '/legend',
  3687. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3688. ['div' => ['class' => 'input select required']],
  3689. '*/div',
  3690. ['div' => ['class' => 'input text required']],
  3691. '*/div',
  3692. ['div' => ['class' => 'input text']],
  3693. '*/div',
  3694. ['div' => ['class' => 'input text']],
  3695. '*/div',
  3696. '/fieldset',
  3697. ];
  3698. $this->assertHtml($expected, $result);
  3699. $result = $this->Form->allControls([
  3700. 'published' => ['type' => 'boolean'],
  3701. ]);
  3702. $expected = [
  3703. '<fieldset',
  3704. '<legend', 'New Article', '/legend',
  3705. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3706. ['div' => ['class' => 'input select required']],
  3707. '*/div',
  3708. ['div' => ['class' => 'input text required']],
  3709. '*/div',
  3710. ['div' => ['class' => 'input text']],
  3711. '*/div',
  3712. ['div' => ['class' => 'input boolean']],
  3713. '*/div',
  3714. '/fieldset',
  3715. ];
  3716. $this->assertHtml($expected, $result);
  3717. $this->Form->create($this->article);
  3718. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3719. $expected = [
  3720. 'fieldset' => [],
  3721. 'legend' => [],
  3722. 'Hello',
  3723. '/legend',
  3724. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3725. ['div' => ['class' => 'input select required']],
  3726. '*/div',
  3727. ['div' => ['class' => 'input text required']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input text']],
  3730. '*/div',
  3731. ['div' => ['class' => 'input text']],
  3732. '*/div',
  3733. '/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $this->Form->create();
  3737. $expected = [
  3738. 'fieldset' => [],
  3739. ['div' => ['class' => 'input text']],
  3740. 'label' => ['for' => 'foo'],
  3741. 'Foo',
  3742. '/label',
  3743. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3744. '*/div',
  3745. '/fieldset',
  3746. ];
  3747. $result = $this->Form->allControls(
  3748. ['foo' => ['type' => 'text']],
  3749. ['legend' => false]
  3750. );
  3751. $this->assertHtml($expected, $result);
  3752. }
  3753. /**
  3754. * testFormControlsBlacklist method
  3755. */
  3756. public function testFormControlsBlacklist(): void
  3757. {
  3758. $this->Form->create($this->article);
  3759. $result = $this->Form->allControls([
  3760. 'id' => false,
  3761. ]);
  3762. $expected = [
  3763. '<fieldset',
  3764. '<legend', 'New Article', '/legend',
  3765. ['div' => ['class' => 'input select required']],
  3766. '*/div',
  3767. ['div' => ['class' => 'input text required']],
  3768. '*/div',
  3769. ['div' => ['class' => 'input text']],
  3770. '*/div',
  3771. ['div' => ['class' => 'input text']],
  3772. '*/div',
  3773. '/fieldset',
  3774. ];
  3775. $this->assertHtml($expected, $result);
  3776. $this->Form->create($this->article);
  3777. $result = $this->Form->allControls([
  3778. 'id' => [],
  3779. ]);
  3780. $expected = [
  3781. '<fieldset',
  3782. '<legend', 'New Article', '/legend',
  3783. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3784. ['div' => ['class' => 'input select required']],
  3785. '*/div',
  3786. ['div' => ['class' => 'input text required']],
  3787. '*/div',
  3788. ['div' => ['class' => 'input text']],
  3789. '*/div',
  3790. ['div' => ['class' => 'input text']],
  3791. '*/div',
  3792. '/fieldset',
  3793. ];
  3794. $this->assertHtml($expected, $result, true);
  3795. }
  3796. /**
  3797. * testSelectAsCheckbox method
  3798. *
  3799. * Test multi-select widget with checkbox formatting.
  3800. */
  3801. public function testSelectAsCheckbox(): void
  3802. {
  3803. $result = $this->Form->select(
  3804. 'Model.multi_field',
  3805. ['first', 'second', 'third'],
  3806. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3807. );
  3808. $expected = [
  3809. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3810. ['div' => ['class' => 'checkbox']],
  3811. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3812. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3813. 'first',
  3814. '/label',
  3815. '/div',
  3816. ['div' => ['class' => 'checkbox']],
  3817. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3818. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3819. 'second',
  3820. '/label',
  3821. '/div',
  3822. ['div' => ['class' => 'checkbox']],
  3823. ['label' => ['for' => 'model-multi-field-2']],
  3824. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3825. 'third',
  3826. '/label',
  3827. '/div',
  3828. ];
  3829. $this->assertHtml($expected, $result);
  3830. $result = $this->Form->select(
  3831. 'Model.multi_field',
  3832. ['1/2' => 'half'],
  3833. ['multiple' => 'checkbox']
  3834. );
  3835. $expected = [
  3836. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3837. ['div' => ['class' => 'checkbox']],
  3838. ['label' => ['for' => 'model-multi-field-1-2']],
  3839. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3840. 'half',
  3841. '/label',
  3842. '/div',
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. }
  3846. /**
  3847. * testLabel method
  3848. *
  3849. * Test label generation.
  3850. */
  3851. public function testLabel(): void
  3852. {
  3853. $result = $this->Form->label('Person.name');
  3854. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3855. $this->assertHtml($expected, $result);
  3856. $result = $this->Form->label('Person.name');
  3857. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3858. $this->assertHtml($expected, $result);
  3859. $result = $this->Form->label('Person.first_name');
  3860. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3861. $this->assertHtml($expected, $result);
  3862. $result = $this->Form->label('Person.first_name', 'Your first name');
  3863. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3864. $this->assertHtml($expected, $result);
  3865. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3866. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3867. $this->assertHtml($expected, $result);
  3868. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3869. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3870. $this->assertHtml($expected, $result);
  3871. $result = $this->Form->label('Person.first_name', '');
  3872. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3873. $this->assertHtml($expected, $result);
  3874. $result = $this->Form->label('Person.2.name', '');
  3875. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3876. $this->assertHtml($expected, $result);
  3877. }
  3878. /**
  3879. * testLabelContainControl method
  3880. *
  3881. * Test that label() can accept an input with the correct template vars.
  3882. */
  3883. public function testLabelContainControl(): void
  3884. {
  3885. $this->Form->setTemplates([
  3886. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3887. ]);
  3888. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3889. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3890. ]);
  3891. $expected = [
  3892. 'label' => ['for' => 'person-accept-terms'],
  3893. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3894. 'Accept',
  3895. '/label',
  3896. ];
  3897. $this->assertHtml($expected, $result);
  3898. }
  3899. /**
  3900. * testTextbox method
  3901. *
  3902. * Test textbox element generation.
  3903. */
  3904. public function testTextbox(): void
  3905. {
  3906. $result = $this->Form->text('Model.field');
  3907. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3908. $this->assertHtml($expected, $result);
  3909. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3910. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3911. $this->assertHtml($expected, $result);
  3912. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3913. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3914. $this->assertHtml($expected, $result);
  3915. }
  3916. /**
  3917. * testTextBoxDataAndError method
  3918. *
  3919. * Test that text() hooks up with request data and error fields.
  3920. */
  3921. public function testTextBoxDataAndError(): void
  3922. {
  3923. $this->article['errors'] = [
  3924. 'Contact' => ['text' => 'wrong'],
  3925. ];
  3926. $this->View->setRequest($this->View->getRequest()
  3927. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3928. ->withData('Contact.text', 'test'));
  3929. $this->Form->create($this->article);
  3930. $result = $this->Form->text('Model.text');
  3931. $expected = [
  3932. 'input' => [
  3933. 'type' => 'text',
  3934. 'name' => 'Model[text]',
  3935. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3936. ],
  3937. ];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3940. $expected = [
  3941. 'input' => [
  3942. 'type' => 'text',
  3943. 'name' => 'Contact[text]',
  3944. 'value' => 'test',
  3945. 'id' => 'theID',
  3946. 'class' => 'form-error',
  3947. ],
  3948. ];
  3949. $this->assertHtml($expected, $result);
  3950. }
  3951. /**
  3952. * testDefaultValue method
  3953. *
  3954. * Test default value setting.
  3955. */
  3956. public function testTextDefaultValue(): void
  3957. {
  3958. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3959. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3960. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3961. $this->assertHtml($expected, $result);
  3962. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3963. $this->Form->create();
  3964. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3965. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3966. $this->assertHtml($expected, $result);
  3967. $Articles = $this->getTableLocator()->get('Articles');
  3968. $title = $Articles->getSchema()->getColumn('title');
  3969. $Articles->getSchema()->addColumn(
  3970. 'title',
  3971. ['default' => 'default title', 'length' => 255] + $title
  3972. );
  3973. $entity = $Articles->newEmptyEntity();
  3974. $this->Form->create($entity);
  3975. // Get default value from schema
  3976. $result = $this->Form->text('title');
  3977. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3978. $this->assertHtml($expected, $result);
  3979. // Don't get value from schema
  3980. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3981. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3982. $this->assertHtml($expected, $result);
  3983. // Custom default value overrides default value from schema
  3984. $result = $this->Form->text('title', ['default' => 'override default']);
  3985. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3986. $this->assertHtml($expected, $result);
  3987. // Default value from schema is used only for new entities.
  3988. $entity->setNew(false);
  3989. $result = $this->Form->text('title');
  3990. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3991. $this->assertHtml($expected, $result);
  3992. }
  3993. /**
  3994. * testError method
  3995. *
  3996. * Test field error generation.
  3997. */
  3998. public function testError(): void
  3999. {
  4000. $this->article['errors'] = [
  4001. 'Article' => ['field' => 'email'],
  4002. ];
  4003. $this->Form->create($this->article);
  4004. $result = $this->Form->error('Article.field');
  4005. $expected = [
  4006. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4007. 'email',
  4008. '/div',
  4009. ];
  4010. $this->assertHtml($expected, $result);
  4011. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4012. $expected = [
  4013. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4014. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4015. '/div',
  4016. ];
  4017. $this->assertHtml($expected, $result);
  4018. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4019. $expected = [
  4020. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4021. '<strong', 'Badness!', '/strong',
  4022. '/div',
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. }
  4026. /**
  4027. * testErrorRuleName method
  4028. *
  4029. * Test error translation can use rule names for translating.
  4030. */
  4031. public function testErrorRuleName(): void
  4032. {
  4033. $this->article['errors'] = [
  4034. 'Article' => [
  4035. 'field' => ['email' => 'Your email was not good'],
  4036. ],
  4037. ];
  4038. $this->Form->create($this->article);
  4039. $result = $this->Form->error('Article.field');
  4040. $expected = [
  4041. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4042. 'Your email was not good',
  4043. '/div',
  4044. ];
  4045. $this->assertHtml($expected, $result);
  4046. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4047. $expected = [
  4048. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4049. 'Email in use',
  4050. '/div',
  4051. ];
  4052. $this->assertHtml($expected, $result);
  4053. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4054. $expected = [
  4055. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4056. 'Email in use',
  4057. '/div',
  4058. ];
  4059. $this->assertHtml($expected, $result);
  4060. $result = $this->Form->error('Article.field', [
  4061. 'email' => 'Key is preferred',
  4062. 'Your email was not good' => 'Email in use',
  4063. ]);
  4064. $expected = [
  4065. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4066. 'Key is preferred',
  4067. '/div',
  4068. ];
  4069. $this->assertHtml($expected, $result);
  4070. }
  4071. /**
  4072. * testErrorMessages method
  4073. *
  4074. * Test error with nested lists.
  4075. */
  4076. public function testErrorMessages(): void
  4077. {
  4078. $this->article['errors'] = [
  4079. 'Article' => ['field' => 'email'],
  4080. ];
  4081. $this->Form->create($this->article);
  4082. $result = $this->Form->error('Article.field', [
  4083. 'email' => 'No good!',
  4084. ]);
  4085. $expected = [
  4086. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4087. 'No good!',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. }
  4092. /**
  4093. * testErrorMultipleMessages method
  4094. *
  4095. * Test error() with multiple messages.
  4096. */
  4097. public function testErrorMultipleMessages(): void
  4098. {
  4099. $this->article['errors'] = [
  4100. 'field' => ['notBlank', 'email', 'Something else'],
  4101. ];
  4102. $this->Form->create($this->article);
  4103. $result = $this->Form->error('field', [
  4104. 'notBlank' => 'Cannot be empty',
  4105. 'email' => 'No good!',
  4106. ]);
  4107. $expected = [
  4108. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4109. 'ul' => [],
  4110. '<li', 'Cannot be empty', '/li',
  4111. '<li', 'No good!', '/li',
  4112. '<li', 'Something else', '/li',
  4113. '/ul',
  4114. '/div',
  4115. ];
  4116. $this->assertHtml($expected, $result);
  4117. }
  4118. /**
  4119. * testPassword method
  4120. *
  4121. * Test password element generation.
  4122. */
  4123. public function testPassword(): void
  4124. {
  4125. $this->article['errors'] = [
  4126. 'Contact' => [
  4127. 'passwd' => 1,
  4128. ],
  4129. ];
  4130. $this->Form->create($this->article);
  4131. $result = $this->Form->password('Contact.field');
  4132. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4133. $this->assertHtml($expected, $result);
  4134. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4135. $this->Form->create($this->article);
  4136. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4137. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4138. $this->assertHtml($expected, $result);
  4139. }
  4140. /**
  4141. * testRadio method
  4142. *
  4143. * Test radio element set generation.
  4144. */
  4145. public function testRadio(): void
  4146. {
  4147. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4148. $expected = [
  4149. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4150. ['label' => ['for' => 'model-field-0']],
  4151. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4152. 'option A',
  4153. '/label',
  4154. ['label' => ['for' => 'model-field-1']],
  4155. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4156. 'option B',
  4157. '/label',
  4158. ];
  4159. $this->assertHtml($expected, $result);
  4160. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4161. $this->assertHtml($expected, $result);
  4162. $result = $this->Form->radio(
  4163. 'Employee.vegetarian',
  4164. ['yes' => 'Yes', 'no' => 'No'],
  4165. ['form' => 'my-form', 'id' => 'id-veg']
  4166. );
  4167. $expected = [
  4168. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4169. ['label' => ['for' => 'id-veg-yes']],
  4170. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4171. 'Yes',
  4172. '/label',
  4173. ['label' => ['for' => 'id-veg-no']],
  4174. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4175. 'No',
  4176. '/label',
  4177. ];
  4178. $this->assertHtml($expected, $result);
  4179. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4180. $expected = [
  4181. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4182. ['label' => ['for' => 'model-custom-0']],
  4183. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4184. 'option A',
  4185. '/label',
  4186. ['label' => ['for' => 'model-custom-1']],
  4187. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4188. 'option B',
  4189. '/label',
  4190. ];
  4191. $this->assertHtml($expected, $result);
  4192. $result = $this->Form->radio(
  4193. 'Employee.gender',
  4194. [
  4195. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4196. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4197. ]
  4198. );
  4199. $expected = [
  4200. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4201. ['label' => ['for' => 'employee-gender-male']],
  4202. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4203. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4204. 'Male',
  4205. '/label',
  4206. ['label' => ['for' => 'employee-gender-female']],
  4207. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4208. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4209. 'Female',
  4210. '/label',
  4211. ];
  4212. $this->assertHtml($expected, $result);
  4213. $article = new Article([
  4214. 'status' => ArticleStatus::Unpublished,
  4215. ]);
  4216. $this->Form->create($article);
  4217. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4218. $expected = [
  4219. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4220. ['label' => ['for' => 'status-y']],
  4221. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4222. 'Published',
  4223. '/label',
  4224. ['label' => ['for' => 'status-n']],
  4225. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4226. 'Unpublished',
  4227. '/label',
  4228. ];
  4229. $this->assertHtml($expected, $result);
  4230. }
  4231. /**
  4232. * Test radio with complex options and empty disabled data.
  4233. */
  4234. public function testRadioComplexDisabled(): void
  4235. {
  4236. $options = [
  4237. ['value' => 'r', 'text' => 'red'],
  4238. ['value' => 'b', 'text' => 'blue'],
  4239. ];
  4240. $attrs = ['disabled' => []];
  4241. $result = $this->Form->radio('Model.field', $options, $attrs);
  4242. $expected = [
  4243. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4244. ['label' => ['for' => 'model-field-r']],
  4245. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4246. 'red',
  4247. '/label',
  4248. ['label' => ['for' => 'model-field-b']],
  4249. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4250. 'blue',
  4251. '/label',
  4252. ];
  4253. $this->assertHtml($expected, $result);
  4254. $attrs = ['disabled' => ['r']];
  4255. $result = $this->Form->radio('Model.field', $options, $attrs);
  4256. $this->assertStringContainsString('disabled="disabled"', $result);
  4257. }
  4258. /**
  4259. * testRadioDefaultValue method
  4260. *
  4261. * Test default value setting on radio() method.
  4262. */
  4263. public function testRadioDefaultValue(): void
  4264. {
  4265. $Articles = $this->getTableLocator()->get('Articles');
  4266. $title = $Articles->getSchema()->getColumn('title');
  4267. $Articles->getSchema()->addColumn(
  4268. 'title',
  4269. ['default' => '1'] + $title
  4270. );
  4271. $this->Form->create($Articles->newEmptyEntity());
  4272. $result = $this->Form->radio('title', ['option A', 'option B']);
  4273. $expected = [
  4274. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4275. ['label' => ['for' => 'title-0']],
  4276. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4277. 'option A',
  4278. '/label',
  4279. ['label' => ['for' => 'title-1']],
  4280. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4281. 'option B',
  4282. '/label',
  4283. ];
  4284. $this->assertHtml($expected, $result);
  4285. }
  4286. /**
  4287. * Test setting a hiddenField value on radio buttons.
  4288. */
  4289. public function testRadioHiddenFieldValue(): void
  4290. {
  4291. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4292. $expected = [
  4293. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4294. 'label' => ['for' => 'title-0'],
  4295. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4296. 'option A',
  4297. '/label',
  4298. ];
  4299. $this->assertHtml($expected, $result);
  4300. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4301. $expected = [
  4302. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4303. 'label' => ['for' => 'title-0'],
  4304. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4305. 'option A',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. }
  4310. /**
  4311. * testControlRadio method
  4312. *
  4313. * Test that input works with radio types.
  4314. */
  4315. public function testControlRadio(): void
  4316. {
  4317. $result = $this->Form->control('test', [
  4318. 'type' => 'radio',
  4319. 'options' => ['A', 'B'],
  4320. ]);
  4321. $expected = [
  4322. ['div' => ['class' => 'input radio']],
  4323. '<label',
  4324. 'Test',
  4325. '/label',
  4326. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4327. ['label' => ['for' => 'test-0']],
  4328. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4329. 'A',
  4330. '/label',
  4331. ['label' => ['for' => 'test-1']],
  4332. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4333. 'B',
  4334. '/label',
  4335. '/div',
  4336. ];
  4337. $this->assertHtml($expected, $result);
  4338. $result = $this->Form->control('test', [
  4339. 'type' => 'radio',
  4340. 'options' => ['A', 'B'],
  4341. 'value' => '0',
  4342. ]);
  4343. $expected = [
  4344. ['div' => ['class' => 'input radio']],
  4345. '<label',
  4346. 'Test',
  4347. '/label',
  4348. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4349. ['label' => ['for' => 'test-0']],
  4350. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4351. 'A',
  4352. '/label',
  4353. ['label' => ['for' => 'test-1']],
  4354. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4355. 'B',
  4356. '/label',
  4357. '/div',
  4358. ];
  4359. $this->assertHtml($expected, $result);
  4360. $result = $this->Form->control('test', [
  4361. 'type' => 'radio',
  4362. 'options' => ['A', 'B'],
  4363. 'label' => false,
  4364. ]);
  4365. $expected = [
  4366. ['div' => ['class' => 'input radio']],
  4367. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4368. ['label' => ['for' => 'test-0']],
  4369. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4370. 'A',
  4371. '/label',
  4372. ['label' => ['for' => 'test-1']],
  4373. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4374. 'B',
  4375. '/label',
  4376. '/div',
  4377. ];
  4378. $this->assertHtml($expected, $result);
  4379. $result = $this->Form->control('accept', [
  4380. 'type' => 'radio',
  4381. 'options' => [
  4382. 1 => 'positive',
  4383. -1 => 'negative',
  4384. ],
  4385. 'label' => false,
  4386. ]);
  4387. $expected = [
  4388. ['div' => ['class' => 'input radio']],
  4389. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4390. ['label' => ['for' => 'accept-1']],
  4391. ['input' => [
  4392. 'type' => 'radio',
  4393. 'name' => 'accept',
  4394. 'value' => '1',
  4395. 'id' => 'accept-1',
  4396. ]],
  4397. 'positive',
  4398. '/label',
  4399. ['label' => ['for' => 'accept--1']],
  4400. ['input' => [
  4401. 'type' => 'radio',
  4402. 'name' => 'accept',
  4403. 'value' => '-1',
  4404. 'id' => 'accept--1',
  4405. ]],
  4406. 'negative',
  4407. '/label',
  4408. '/div',
  4409. ];
  4410. $this->assertHtml($expected, $result);
  4411. $articlesTable = $this->getTableLocator()->get('Articles');
  4412. $articlesTable->getSchema()->setColumnType(
  4413. 'published',
  4414. EnumType::from(ArticleStatus::class)
  4415. );
  4416. $this->Form->create($articlesTable->newEmptyEntity());
  4417. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4418. $expected = [
  4419. ['div' => ['class' => 'input radio']],
  4420. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4421. ['label' => ['for' => 'published-y']],
  4422. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4423. 'Published',
  4424. '/label',
  4425. ['label' => ['for' => 'published-n']],
  4426. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4427. 'Unpublished',
  4428. '/label',
  4429. '/div',
  4430. ];
  4431. $this->assertHtml($expected, $result);
  4432. }
  4433. /**
  4434. * testRadioNoLabel method
  4435. *
  4436. * Test that radio() works with label = false.
  4437. */
  4438. public function testRadioNoLabel(): void
  4439. {
  4440. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4441. $expected = [
  4442. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4443. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4444. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4445. ];
  4446. $this->assertHtml($expected, $result);
  4447. }
  4448. /**
  4449. * testRadioControlInsideLabel method
  4450. *
  4451. * Test generating radio input inside label ala twitter bootstrap.
  4452. */
  4453. public function testRadioControlInsideLabel(): void
  4454. {
  4455. $this->Form->setTemplates([
  4456. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4457. 'radioWrapper' => '{{label}}',
  4458. ]);
  4459. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4460. // phpcs:disable
  4461. $expected = [
  4462. ['input' => [
  4463. 'type' => 'hidden',
  4464. 'name' => 'Model[field]',
  4465. 'value' => '',
  4466. 'id' => 'model-field'
  4467. ]],
  4468. ['label' => ['for' => 'model-field-0']],
  4469. ['input' => [
  4470. 'type' => 'radio',
  4471. 'name' => 'Model[field]',
  4472. 'value' => '0',
  4473. 'id' => 'model-field-0'
  4474. ]],
  4475. 'option A',
  4476. '/label',
  4477. ['label' => ['for' => 'model-field-1']],
  4478. ['input' => [
  4479. 'type' => 'radio',
  4480. 'name' => 'Model[field]',
  4481. 'value' => '1',
  4482. 'id' => 'model-field-1'
  4483. ]],
  4484. 'option B',
  4485. '/label',
  4486. ];
  4487. // phpcs:enable
  4488. $this->assertHtml($expected, $result);
  4489. }
  4490. /**
  4491. * testRadioHiddenControlDisabling method
  4492. *
  4493. * Test disabling the hidden input for radio buttons.
  4494. */
  4495. public function testRadioHiddenControlDisabling(): void
  4496. {
  4497. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4498. $expected = [
  4499. 'label' => ['for' => 'model-1-field-0'],
  4500. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4501. 'option A',
  4502. '/label',
  4503. ];
  4504. $this->assertHtml($expected, $result);
  4505. }
  4506. /**
  4507. * testRadioOutOfRange method
  4508. *
  4509. * Test radio element set generation.
  4510. */
  4511. public function testRadioOutOfRange(): void
  4512. {
  4513. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4514. $expected = [
  4515. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4516. 'label' => ['for' => 'model-field-v'],
  4517. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4518. 'value',
  4519. '/label',
  4520. ];
  4521. $this->assertHtml($expected, $result);
  4522. }
  4523. /**
  4524. * testSelect method
  4525. *
  4526. * Test select element generation.
  4527. */
  4528. public function testSelect(): void
  4529. {
  4530. $result = $this->Form->select('Model.field', []);
  4531. $expected = [
  4532. 'select' => ['name' => 'Model[field]'],
  4533. '/select',
  4534. ];
  4535. $this->assertHtml($expected, $result);
  4536. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4537. $this->Form->create();
  4538. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4539. $expected = [
  4540. 'select' => ['name' => 'Model[field]'],
  4541. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4542. 'good',
  4543. '/option',
  4544. ['option' => ['value' => 'other']],
  4545. 'bad',
  4546. '/option',
  4547. '/select',
  4548. ];
  4549. $this->assertHtml($expected, $result);
  4550. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4551. $this->assertHtml($expected, $result);
  4552. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4553. $this->Form->create();
  4554. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4555. $expected = [
  4556. 'select' => ['name' => 'Model[field]'],
  4557. ['option' => ['value' => 'value']],
  4558. 'good',
  4559. '/option',
  4560. ['option' => ['value' => 'other']],
  4561. 'bad',
  4562. '/option',
  4563. '/select',
  4564. ];
  4565. $this->assertHtml($expected, $result);
  4566. $options = [
  4567. ['value' => 'first', 'text' => 'First'],
  4568. ['value' => 'first', 'text' => 'Another First'],
  4569. ];
  4570. $result = $this->Form->select(
  4571. 'Model.field',
  4572. $options,
  4573. ['escape' => false, 'empty' => false]
  4574. );
  4575. $expected = [
  4576. 'select' => ['name' => 'Model[field]'],
  4577. ['option' => ['value' => 'first']],
  4578. 'First',
  4579. '/option',
  4580. ['option' => ['value' => 'first']],
  4581. 'Another First',
  4582. '/option',
  4583. '/select',
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4587. $this->Form->create();
  4588. $result = $this->Form->select(
  4589. 'Model.contact_id',
  4590. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4591. ['escape' => false, 'empty' => 'pick something']
  4592. );
  4593. $expected = [
  4594. 'select' => ['name' => 'Model[contact_id]'],
  4595. ['option' => ['value' => '']], 'pick something', '/option',
  4596. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4597. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4598. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4599. '/select',
  4600. ];
  4601. $this->assertHtml($expected, $result);
  4602. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4603. $this->Form->create();
  4604. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4605. $expected = [
  4606. 'select' => ['name' => 'Model[field]'],
  4607. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4608. ['option' => ['value' => '1']], 'Yes', '/option',
  4609. '/select',
  4610. ];
  4611. $this->assertHtml($expected, $result);
  4612. $article = new Article([
  4613. 'status' => ArticleStatus::Unpublished,
  4614. ]);
  4615. $this->Form->create($article);
  4616. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4617. $expected = [
  4618. 'select' => ['name' => 'status'],
  4619. ['option' => ['value' => 'Y']], 'Published', '/option',
  4620. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4621. '/select',
  4622. ];
  4623. $this->assertHtml($expected, $result);
  4624. }
  4625. /**
  4626. * testSelectEscapeHtml method
  4627. *
  4628. * Test that select() escapes HTML.
  4629. */
  4630. public function testSelectEscapeHtml(): void
  4631. {
  4632. $result = $this->Form->select(
  4633. 'Model.field',
  4634. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4635. ['empty' => false]
  4636. );
  4637. $expected = [
  4638. 'select' => ['name' => 'Model[field]'],
  4639. ['option' => ['value' => 'first']],
  4640. 'first &quot;html&quot; &lt;chars&gt;',
  4641. '/option',
  4642. ['option' => ['value' => 'second']],
  4643. 'value',
  4644. '/option',
  4645. '/select',
  4646. ];
  4647. $this->assertHtml($expected, $result);
  4648. $result = $this->Form->select(
  4649. 'Model.field',
  4650. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4651. ['escape' => false, 'empty' => false]
  4652. );
  4653. $expected = [
  4654. 'select' => ['name' => 'Model[field]'],
  4655. ['option' => ['value' => 'first']],
  4656. 'first "html" <chars>',
  4657. '/option',
  4658. ['option' => ['value' => 'second']],
  4659. 'value',
  4660. '/option',
  4661. '/select',
  4662. ];
  4663. $this->assertHtml($expected, $result);
  4664. }
  4665. /**
  4666. * testSelectRequired method
  4667. *
  4668. * Test select() with required and disabled attributes.
  4669. */
  4670. public function testSelectRequired(): void
  4671. {
  4672. $this->article['required'] = [
  4673. 'user_id' => true,
  4674. ];
  4675. $this->Form->create($this->article);
  4676. $result = $this->Form->select('user_id', ['option A']);
  4677. $expected = [
  4678. 'select' => [
  4679. 'name' => 'user_id',
  4680. 'required' => 'required',
  4681. ],
  4682. ['option' => ['value' => '0']], 'option A', '/option',
  4683. '/select',
  4684. ];
  4685. $this->assertHtml($expected, $result);
  4686. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4687. $expected = [
  4688. 'select' => [
  4689. 'name' => 'user_id',
  4690. 'disabled' => 'disabled',
  4691. ],
  4692. ['option' => ['value' => '0']], 'option A', '/option',
  4693. '/select',
  4694. ];
  4695. $this->assertHtml($expected, $result);
  4696. }
  4697. public function testSelectEmptyWithRequiredFalse(): void
  4698. {
  4699. $Articles = $this->getTableLocator()->get('Articles');
  4700. $validator = $Articles->getValidator('default');
  4701. $validator->allowEmptyString('user_id');
  4702. $Articles->setValidator('default', $validator);
  4703. $entity = $Articles->newEmptyEntity();
  4704. $this->Form->create($entity);
  4705. $result = $this->Form->select('user_id', ['option A']);
  4706. $expected = [
  4707. 'select' => [
  4708. 'name' => 'user_id',
  4709. ],
  4710. ['option' => ['value' => '']], '/option',
  4711. ['option' => ['value' => '0']], 'option A', '/option',
  4712. '/select',
  4713. ];
  4714. $this->assertHtml($expected, $result);
  4715. }
  4716. /**
  4717. * testNestedSelect method
  4718. *
  4719. * Test select element generation with optgroups.
  4720. */
  4721. public function testNestedSelect(): void
  4722. {
  4723. $result = $this->Form->select(
  4724. 'Model.field',
  4725. [1 => 'One', 2 => 'Two', 'Three' => [
  4726. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4727. ]],
  4728. ['empty' => false]
  4729. );
  4730. $expected = [
  4731. 'select' => ['name' => 'Model[field]'],
  4732. ['option' => ['value' => 1]],
  4733. 'One',
  4734. '/option',
  4735. ['option' => ['value' => 2]],
  4736. 'Two',
  4737. '/option',
  4738. ['optgroup' => ['label' => 'Three']],
  4739. ['option' => ['value' => 3]],
  4740. 'Three',
  4741. '/option',
  4742. ['option' => ['value' => 4]],
  4743. 'Four',
  4744. '/option',
  4745. ['option' => ['value' => 5]],
  4746. 'Five',
  4747. '/option',
  4748. '/optgroup',
  4749. '/select',
  4750. ];
  4751. $this->assertHtml($expected, $result);
  4752. }
  4753. /**
  4754. * testSelectMultiple method
  4755. *
  4756. * Test generation of multiple select elements.
  4757. */
  4758. public function testSelectMultiple(): void
  4759. {
  4760. $options = ['first', 'second', 'third'];
  4761. $result = $this->Form->select(
  4762. 'Model.multi_field',
  4763. $options,
  4764. ['form' => 'my-form', 'multiple' => true]
  4765. );
  4766. $expected = [
  4767. 'input' => [
  4768. 'type' => 'hidden',
  4769. 'name' => 'Model[multi_field]',
  4770. 'value' => '',
  4771. 'form' => 'my-form',
  4772. ],
  4773. 'select' => [
  4774. 'name' => 'Model[multi_field][]',
  4775. 'multiple' => 'multiple',
  4776. 'form' => 'my-form',
  4777. ],
  4778. ['option' => ['value' => '0']],
  4779. 'first',
  4780. '/option',
  4781. ['option' => ['value' => '1']],
  4782. 'second',
  4783. '/option',
  4784. ['option' => ['value' => '2']],
  4785. 'third',
  4786. '/option',
  4787. '/select',
  4788. ];
  4789. $this->assertHtml($expected, $result);
  4790. $result = $this->Form->select(
  4791. 'Model.multi_field',
  4792. $options,
  4793. ['multiple' => 'multiple', 'form' => 'my-form']
  4794. );
  4795. $this->assertHtml($expected, $result);
  4796. $result = $this->Form->select(
  4797. 'Model.multi_field',
  4798. $options,
  4799. ['form' => 'my-form', 'multiple' => false]
  4800. );
  4801. $this->assertStringNotContainsString('multiple', $result);
  4802. }
  4803. /**
  4804. * testCheckboxZeroValue method
  4805. *
  4806. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4807. */
  4808. public function testCheckboxZeroValue(): void
  4809. {
  4810. $result = $this->Form->control('User.get_spam', [
  4811. 'type' => 'checkbox',
  4812. 'value' => '0',
  4813. 'hiddenField' => '1',
  4814. ]);
  4815. $expected = [
  4816. 'div' => ['class' => 'input checkbox'],
  4817. 'label' => ['for' => 'user-get-spam'],
  4818. ['input' => [
  4819. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4820. 'value' => '1',
  4821. ]],
  4822. ['input' => [
  4823. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4824. 'value' => '0', 'id' => 'user-get-spam',
  4825. ]],
  4826. 'Get Spam',
  4827. '/label',
  4828. '/div',
  4829. ];
  4830. $this->assertHtml($expected, $result);
  4831. $result = $this->Form->control('User.get_spam', [
  4832. 'type' => 'checkbox',
  4833. 'value' => '0',
  4834. 'hiddenField' => '',
  4835. ]);
  4836. $expected = [
  4837. 'div' => ['class' => 'input checkbox'],
  4838. 'label' => ['for' => 'user-get-spam'],
  4839. ['input' => [
  4840. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4841. 'value' => '',
  4842. ]],
  4843. ['input' => [
  4844. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4845. 'value' => '0', 'id' => 'user-get-spam',
  4846. ]],
  4847. 'Get Spam',
  4848. '/label',
  4849. '/div',
  4850. ];
  4851. $this->assertHtml($expected, $result);
  4852. }
  4853. /**
  4854. * testHabtmSelectBox method
  4855. *
  4856. * Test generation of habtm select boxes.
  4857. */
  4858. public function testHabtmSelectBox(): void
  4859. {
  4860. $options = [
  4861. 1 => 'blue',
  4862. 2 => 'red',
  4863. 3 => 'green',
  4864. ];
  4865. $tags = [
  4866. new Entity(['id' => 1, 'name' => 'blue']),
  4867. new Entity(['id' => 3, 'name' => 'green']),
  4868. ];
  4869. $article = new Article(['tags' => $tags]);
  4870. $this->Form->create($article);
  4871. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4872. $expected = [
  4873. 'div' => ['class' => 'input select'],
  4874. 'label' => ['for' => 'tags-ids'],
  4875. 'Tags',
  4876. '/label',
  4877. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4878. 'select' => [
  4879. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4880. 'multiple' => 'multiple',
  4881. ],
  4882. ['option' => ['value' => '1', 'selected' => 'selected']],
  4883. 'blue',
  4884. '/option',
  4885. ['option' => ['value' => '2']],
  4886. 'red',
  4887. '/option',
  4888. ['option' => ['value' => '3', 'selected' => 'selected']],
  4889. 'green',
  4890. '/option',
  4891. '/select',
  4892. '/div',
  4893. ];
  4894. $this->assertHtml($expected, $result);
  4895. // make sure only 50 is selected, and not 50f5c0cf
  4896. $options = [
  4897. '1' => 'blue',
  4898. '50f5c0cf' => 'red',
  4899. '50' => 'green',
  4900. ];
  4901. $tags = [
  4902. new Entity(['id' => 1, 'name' => 'blue']),
  4903. new Entity(['id' => 50, 'name' => 'green']),
  4904. ];
  4905. $article = new Article(['tags' => $tags]);
  4906. $this->Form->create($article);
  4907. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4908. $expected = [
  4909. 'div' => ['class' => 'input select'],
  4910. 'label' => ['for' => 'tags-ids'],
  4911. 'Tags',
  4912. '/label',
  4913. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4914. 'select' => [
  4915. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4916. 'multiple' => 'multiple',
  4917. ],
  4918. ['option' => ['value' => '1', 'selected' => 'selected']],
  4919. 'blue',
  4920. '/option',
  4921. ['option' => ['value' => '50f5c0cf']],
  4922. 'red',
  4923. '/option',
  4924. ['option' => ['value' => '50', 'selected' => 'selected']],
  4925. 'green',
  4926. '/option',
  4927. '/select',
  4928. '/div',
  4929. ];
  4930. $this->assertHtml($expected, $result);
  4931. $spacecraft = [
  4932. 1 => 'Orion',
  4933. 2 => 'Helios',
  4934. ];
  4935. $this->View->set('spacecraft', $spacecraft);
  4936. $this->Form->create();
  4937. $result = $this->Form->control('spacecraft._ids');
  4938. $expected = [
  4939. 'div' => ['class' => 'input select'],
  4940. 'label' => ['for' => 'spacecraft-ids'],
  4941. 'Spacecraft',
  4942. '/label',
  4943. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4944. 'select' => [
  4945. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4946. 'multiple' => 'multiple',
  4947. ],
  4948. ['option' => ['value' => '1']],
  4949. 'Orion',
  4950. '/option',
  4951. ['option' => ['value' => '2']],
  4952. 'Helios',
  4953. '/option',
  4954. '/select',
  4955. '/div',
  4956. ];
  4957. $this->assertHtml($expected, $result);
  4958. }
  4959. /**
  4960. * testErrorsForBelongsToManySelect method
  4961. *
  4962. * Tests that errors for belongsToMany select fields are being
  4963. * picked up properly.
  4964. */
  4965. public function testErrorsForBelongsToManySelect(): void
  4966. {
  4967. $spacecraft = [
  4968. 1 => 'Orion',
  4969. 2 => 'Helios',
  4970. ];
  4971. $this->View->set('spacecraft', $spacecraft);
  4972. $article = new Article();
  4973. $article->setError('spacecraft', ['Invalid']);
  4974. $this->Form->create($article);
  4975. $result = $this->Form->control('spacecraft._ids');
  4976. $expected = [
  4977. ['div' => ['class' => 'input select error']],
  4978. 'label' => ['for' => 'spacecraft-ids'],
  4979. 'Spacecraft',
  4980. '/label',
  4981. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4982. 'select' => [
  4983. 'name' => 'spacecraft[_ids][]',
  4984. 'id' => 'spacecraft-ids',
  4985. 'multiple' => 'multiple',
  4986. ],
  4987. ['option' => ['value' => '1']],
  4988. 'Orion',
  4989. '/option',
  4990. ['option' => ['value' => '2']],
  4991. 'Helios',
  4992. '/option',
  4993. '/select',
  4994. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4995. 'Invalid',
  4996. '/div',
  4997. '/div',
  4998. ];
  4999. $this->assertHtml($expected, $result);
  5000. }
  5001. /**
  5002. * testSelectMultipleCheckboxes method
  5003. *
  5004. * Test generation of multi select elements in checkbox format.
  5005. */
  5006. public function testSelectMultipleCheckboxes(): void
  5007. {
  5008. $result = $this->Form->select(
  5009. 'Model.multi_field',
  5010. ['first', 'second', 'third'],
  5011. ['multiple' => 'checkbox']
  5012. );
  5013. $expected = [
  5014. 'input' => [
  5015. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5016. ],
  5017. ['div' => ['class' => 'checkbox']],
  5018. ['label' => ['for' => 'model-multi-field-0']],
  5019. ['input' => [
  5020. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5021. 'value' => '0', 'id' => 'model-multi-field-0',
  5022. ]],
  5023. 'first',
  5024. '/label',
  5025. '/div',
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'model-multi-field-1']],
  5028. ['input' => [
  5029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5030. 'value' => '1', 'id' => 'model-multi-field-1',
  5031. ]],
  5032. 'second',
  5033. '/label',
  5034. '/div',
  5035. ['div' => ['class' => 'checkbox']],
  5036. ['label' => ['for' => 'model-multi-field-2']],
  5037. ['input' => [
  5038. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5039. 'value' => '2', 'id' => 'model-multi-field-2',
  5040. ]],
  5041. 'third',
  5042. '/label',
  5043. '/div',
  5044. ];
  5045. $this->assertHtml($expected, $result);
  5046. $result = $this->Form->select(
  5047. 'Model.multi_field',
  5048. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5049. ['multiple' => 'checkbox']
  5050. );
  5051. $expected = [
  5052. 'input' => [
  5053. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5054. ],
  5055. ['div' => ['class' => 'checkbox']],
  5056. ['label' => ['for' => 'model-multi-field-a+']],
  5057. ['input' => [
  5058. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5059. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5060. ]],
  5061. 'first',
  5062. '/label',
  5063. '/div',
  5064. ['div' => ['class' => 'checkbox']],
  5065. ['label' => ['for' => 'model-multi-field-a++']],
  5066. ['input' => [
  5067. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5068. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5069. ]],
  5070. 'second',
  5071. '/label',
  5072. '/div',
  5073. ['div' => ['class' => 'checkbox']],
  5074. ['label' => ['for' => 'model-multi-field-a+++']],
  5075. ['input' => [
  5076. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5077. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5078. ]],
  5079. 'third',
  5080. '/label',
  5081. '/div',
  5082. ];
  5083. $this->assertHtml($expected, $result);
  5084. $result = $this->Form->select(
  5085. 'Model.multi_field',
  5086. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5087. ['multiple' => 'checkbox']
  5088. );
  5089. $expected = [
  5090. 'input' => [
  5091. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5092. ],
  5093. ['div' => ['class' => 'checkbox']],
  5094. ['label' => ['for' => 'model-multi-field-a-b']],
  5095. ['input' => [
  5096. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5097. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5098. ]],
  5099. 'first',
  5100. '/label',
  5101. '/div',
  5102. ['div' => ['class' => 'checkbox']],
  5103. ['label' => ['for' => 'model-multi-field-a-b1']],
  5104. ['input' => [
  5105. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5106. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5107. ]],
  5108. 'second',
  5109. '/label',
  5110. '/div',
  5111. ['div' => ['class' => 'checkbox']],
  5112. ['label' => ['for' => 'model-multi-field-a-b2']],
  5113. ['input' => [
  5114. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5115. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5116. ]],
  5117. 'third',
  5118. '/label',
  5119. '/div',
  5120. ];
  5121. $this->assertHtml($expected, $result);
  5122. }
  5123. /**
  5124. * testSelectMultipleCheckboxRequestData method
  5125. *
  5126. * Ensure that multiCheckbox reads from the request data.
  5127. */
  5128. public function testSelectMultipleCheckboxRequestData(): void
  5129. {
  5130. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5131. $result = $this->Form->select(
  5132. 'Model.tags',
  5133. ['1' => 'first', 'Array' => 'Array'],
  5134. ['multiple' => 'checkbox']
  5135. );
  5136. $expected = [
  5137. 'input' => [
  5138. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5139. ],
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5142. ['input' => [
  5143. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5144. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5145. ]],
  5146. 'first',
  5147. '/label',
  5148. '/div',
  5149. ['div' => ['class' => 'checkbox']],
  5150. ['label' => ['for' => 'model-tags-array']],
  5151. ['input' => [
  5152. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5153. 'value' => 'Array', 'id' => 'model-tags-array',
  5154. ]],
  5155. 'Array',
  5156. '/label',
  5157. '/div',
  5158. ];
  5159. $this->assertHtml($expected, $result);
  5160. }
  5161. /**
  5162. * testSelectMultipleCheckboxSecurity method
  5163. *
  5164. * Checks the security hash array generated for multiple-input checkbox elements.
  5165. */
  5166. public function testSelectMultipleCheckboxSecurity(): void
  5167. {
  5168. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5169. $this->Form->create();
  5170. $this->Form->select(
  5171. 'Model.multi_field',
  5172. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5173. ['multiple' => 'checkbox']
  5174. );
  5175. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5176. $this->assertEquals(['Model.multi_field'], $fields);
  5177. $result = $this->Form->secure();
  5178. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5179. $hash = urlencode($hash . ':');
  5180. $this->assertStringContainsString('"' . $hash . '"', $result);
  5181. }
  5182. /**
  5183. * testSelectMultipleSecureWithNoOptions method
  5184. *
  5185. * Multiple select elements should always be secured as they always participate
  5186. * in the POST data.
  5187. */
  5188. public function testSelectMultipleSecureWithNoOptions(): void
  5189. {
  5190. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5191. $this->Form->create();
  5192. $this->Form->select(
  5193. 'Model.select',
  5194. [],
  5195. ['multiple' => true]
  5196. );
  5197. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5198. $this->assertEquals(['Model.select'], $result);
  5199. }
  5200. /**
  5201. * testSelectNoSecureWithNoOptions method
  5202. *
  5203. * When a select box has no options it should not be added to the fields list
  5204. * as it always fail post validation.
  5205. */
  5206. public function testSelectNoSecureWithNoOptions(): void
  5207. {
  5208. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5209. $this->Form->create();
  5210. $this->Form->select(
  5211. 'Model.select',
  5212. []
  5213. );
  5214. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5215. $this->assertEquals([], $result);
  5216. $this->Form->select(
  5217. 'Model.user_id',
  5218. [],
  5219. ['empty' => true]
  5220. );
  5221. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5222. $this->assertEquals(['Model.user_id'], $result);
  5223. }
  5224. /**
  5225. * testControlMultipleCheckboxes method
  5226. *
  5227. * Test control() resulting in multi select elements being generated.
  5228. */
  5229. public function testControlMultipleCheckboxes(): void
  5230. {
  5231. $result = $this->Form->control('Model.multi_field', [
  5232. 'options' => ['first', 'second', 'third'],
  5233. 'multiple' => 'checkbox',
  5234. ]);
  5235. $expected = [
  5236. ['div' => ['class' => 'input select']],
  5237. ['label' => ['for' => 'model-multi-field']],
  5238. 'Multi Field',
  5239. '/label',
  5240. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5241. ['div' => ['class' => 'checkbox']],
  5242. ['label' => ['for' => 'model-multi-field-0']],
  5243. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5244. 'first',
  5245. '/label',
  5246. '/div',
  5247. ['div' => ['class' => 'checkbox']],
  5248. ['label' => ['for' => 'model-multi-field-1']],
  5249. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5250. 'second',
  5251. '/label',
  5252. '/div',
  5253. ['div' => ['class' => 'checkbox']],
  5254. ['label' => ['for' => 'model-multi-field-2']],
  5255. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5256. 'third',
  5257. '/label',
  5258. '/div',
  5259. '/div',
  5260. ];
  5261. $this->assertHtml($expected, $result);
  5262. $result = $this->Form->control('Model.multi_field', [
  5263. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5264. 'multiple' => 'checkbox',
  5265. ]);
  5266. $expected = [
  5267. ['div' => ['class' => 'input select']],
  5268. ['label' => ['for' => 'model-multi-field']],
  5269. 'Multi Field',
  5270. '/label',
  5271. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5272. ['div' => ['class' => 'checkbox']],
  5273. ['label' => ['for' => 'model-multi-field-a']],
  5274. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5275. 'first',
  5276. '/label',
  5277. '/div',
  5278. ['div' => ['class' => 'checkbox']],
  5279. ['label' => ['for' => 'model-multi-field-b']],
  5280. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5281. 'second',
  5282. '/label',
  5283. '/div',
  5284. ['div' => ['class' => 'checkbox']],
  5285. ['label' => ['for' => 'model-multi-field-c']],
  5286. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5287. 'third',
  5288. '/label',
  5289. '/div',
  5290. '/div',
  5291. ];
  5292. $this->assertHtml($expected, $result);
  5293. }
  5294. /**
  5295. * testSelectHiddenFieldOmission method
  5296. *
  5297. * Test that select() with 'hiddenField' => false omits the hidden field.
  5298. */
  5299. public function testSelectHiddenFieldOmission(): void
  5300. {
  5301. $result = $this->Form->select(
  5302. 'Model.multi_field',
  5303. ['first', 'second'],
  5304. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5305. );
  5306. $this->assertStringNotContainsString('type="hidden"', $result);
  5307. }
  5308. /**
  5309. * testSelectCheckboxMultipleOverrideName method
  5310. *
  5311. * Test that select() with multiple = checkbox works with overriding name attribute.
  5312. */
  5313. public function testSelectCheckboxMultipleOverrideName(): void
  5314. {
  5315. $result = $this->Form->select('category', ['1', '2'], [
  5316. 'multiple' => 'checkbox',
  5317. 'name' => 'fish',
  5318. ]);
  5319. $expected = [
  5320. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5321. ['div' => ['class' => 'checkbox']],
  5322. ['label' => ['for' => 'fish-0']],
  5323. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5324. '1',
  5325. '/label',
  5326. '/div',
  5327. ['div' => ['class' => 'checkbox']],
  5328. ['label' => ['for' => 'fish-1']],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5330. '2',
  5331. '/label',
  5332. '/div',
  5333. ];
  5334. $this->assertHtml($expected, $result);
  5335. $result = $this->Form->multiCheckbox(
  5336. 'category',
  5337. new Collection(['1', '2']),
  5338. ['name' => 'fish']
  5339. );
  5340. $this->assertHtml($expected, $result);
  5341. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5342. 'name' => 'fish',
  5343. ]);
  5344. $this->assertHtml($expected, $result);
  5345. }
  5346. /**
  5347. * testSelectCheckboxMultipleOverrideName method
  5348. *
  5349. * Test that select() with multiple = checkbox works with overriding name attribute.
  5350. */
  5351. public function testSelectCheckboxMultipleCustomId(): void
  5352. {
  5353. $result = $this->Form->select('category', ['1', '2'], [
  5354. 'multiple' => 'checkbox',
  5355. 'id' => 'cat',
  5356. ]);
  5357. $expected = [
  5358. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5359. ['div' => ['class' => 'checkbox']],
  5360. ['label' => ['for' => 'cat-0']],
  5361. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5362. '1',
  5363. '/label',
  5364. '/div',
  5365. ['div' => ['class' => 'checkbox']],
  5366. ['label' => ['for' => 'cat-1']],
  5367. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5368. '2',
  5369. '/label',
  5370. '/div',
  5371. ];
  5372. $this->assertHtml($expected, $result);
  5373. $result = $this->Form->multiCheckbox(
  5374. 'category',
  5375. ['1', '2'],
  5376. ['id' => 'cat']
  5377. );
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * testControlMultiCheckbox method
  5382. *
  5383. * Test that control() works with multicheckbox.
  5384. */
  5385. public function testControlMultiCheckbox(): void
  5386. {
  5387. $result = $this->Form->control('category', [
  5388. 'type' => 'multicheckbox',
  5389. 'options' => ['1', '2'],
  5390. ]);
  5391. $expected = [
  5392. ['div' => ['class' => 'input multicheckbox']],
  5393. '<label',
  5394. 'Category',
  5395. '/label',
  5396. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5397. ['div' => ['class' => 'checkbox']],
  5398. ['label' => ['for' => 'category-0']],
  5399. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5400. '1',
  5401. '/label',
  5402. '/div',
  5403. ['div' => ['class' => 'checkbox']],
  5404. ['label' => ['for' => 'category-1']],
  5405. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5406. '2',
  5407. '/label',
  5408. '/div',
  5409. '/div',
  5410. ];
  5411. $this->assertHtml($expected, $result);
  5412. }
  5413. /**
  5414. * testCheckbox method
  5415. *
  5416. * Test generation of checkboxes.
  5417. */
  5418. public function testCheckbox(): void
  5419. {
  5420. $result = $this->Form->checkbox('Model.field');
  5421. $expected = [
  5422. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5423. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. $result = $this->Form->checkbox('Model.field', [
  5427. 'id' => 'theID',
  5428. 'value' => 'myvalue',
  5429. 'form' => 'my-form',
  5430. ]);
  5431. $expected = [
  5432. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5433. ['input' => [
  5434. 'type' => 'checkbox', 'name' => 'Model[field]',
  5435. 'value' => 'myvalue', 'id' => 'theID',
  5436. 'form' => 'my-form',
  5437. ]],
  5438. ];
  5439. $this->assertHtml($expected, $result);
  5440. }
  5441. /**
  5442. * testCheckboxDefaultValue method
  5443. *
  5444. * Test default value setting on checkbox() method.
  5445. */
  5446. public function testCheckboxDefaultValue(): void
  5447. {
  5448. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5449. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5450. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5451. $this->assertHtml($expected, $result);
  5452. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5453. $this->Form->create();
  5454. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5455. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5456. $this->assertHtml($expected, $result);
  5457. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5458. $this->Form->create();
  5459. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5460. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5461. $this->assertHtml($expected, $result);
  5462. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5463. $this->Form->create();
  5464. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5465. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5466. $this->assertHtml($expected, $result);
  5467. $Articles = $this->getTableLocator()->get('Articles');
  5468. $Articles->getSchema()->addColumn(
  5469. 'published',
  5470. ['type' => 'boolean', 'null' => false, 'default' => true]
  5471. );
  5472. $this->Form->create($Articles->newEmptyEntity());
  5473. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5474. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5475. $this->assertHtml($expected, $result);
  5476. }
  5477. /**
  5478. * testCheckboxCheckedAndError method
  5479. *
  5480. * Test checkbox being checked or having errors.
  5481. */
  5482. public function testCheckboxCheckedAndError(): void
  5483. {
  5484. $this->article['errors'] = [
  5485. 'published' => true,
  5486. ];
  5487. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5488. $this->Form->create($this->article);
  5489. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5492. ['input' => [
  5493. 'type' => 'checkbox',
  5494. 'name' => 'published',
  5495. 'value' => 'myvalue',
  5496. 'id' => 'theID',
  5497. 'checked' => 'checked',
  5498. 'class' => 'form-error',
  5499. ]],
  5500. ];
  5501. $this->assertHtml($expected, $result);
  5502. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5503. $this->Form->create($this->article);
  5504. $result = $this->Form->checkbox('published');
  5505. $expected = [
  5506. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5507. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5508. ];
  5509. $this->assertHtml($expected, $result);
  5510. }
  5511. /**
  5512. * testCheckboxCustomNameAttribute method
  5513. *
  5514. * Test checkbox() with a custom name attribute.
  5515. */
  5516. public function testCheckboxCustomNameAttribute(): void
  5517. {
  5518. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5519. $expected = [
  5520. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5521. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5522. ];
  5523. $this->assertHtml($expected, $result);
  5524. }
  5525. /**
  5526. * testCheckboxHiddenField method
  5527. *
  5528. * Test that the hidden input for checkboxes can be omitted or set to a
  5529. * specific value.
  5530. */
  5531. public function testCheckboxHiddenField(): void
  5532. {
  5533. $result = $this->Form->checkbox('UserForm.something', [
  5534. 'hiddenField' => false,
  5535. ]);
  5536. $expected = [
  5537. 'input' => [
  5538. 'type' => 'checkbox',
  5539. 'name' => 'UserForm[something]',
  5540. 'value' => '1',
  5541. ],
  5542. ];
  5543. $this->assertHtml($expected, $result);
  5544. $result = $this->Form->checkbox('UserForm.something', [
  5545. 'value' => 'Y',
  5546. 'hiddenField' => '',
  5547. ]);
  5548. $expected = [
  5549. ['input' => [
  5550. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5551. 'value' => '',
  5552. ]],
  5553. ['input' => [
  5554. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5555. 'value' => 'Y',
  5556. ]],
  5557. ];
  5558. $this->assertHtml($expected, $result);
  5559. $result = $this->Form->checkbox('UserForm.something', [
  5560. 'value' => 'Y',
  5561. 'hiddenField' => 'N',
  5562. ]);
  5563. $expected = [
  5564. ['input' => [
  5565. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5566. 'value' => 'N',
  5567. ]],
  5568. ['input' => [
  5569. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5570. 'value' => 'Y',
  5571. ]],
  5572. ];
  5573. $this->assertHtml($expected, $result);
  5574. }
  5575. /**
  5576. * testTime method
  5577. *
  5578. * Test the time type.
  5579. */
  5580. public function testTime(): void
  5581. {
  5582. $result = $this->Form->time('start_time', [
  5583. 'value' => '2014-03-08 16:30:00',
  5584. ]);
  5585. $expected = [
  5586. 'input' => [
  5587. 'type' => 'time',
  5588. 'name' => 'start_time',
  5589. 'value' => '16:30:00',
  5590. 'step' => '1',
  5591. ],
  5592. ];
  5593. $this->assertHtml($expected, $result);
  5594. $result = $this->Form->time('start_time', [
  5595. 'value' => new ChronosTime('16:30:00'),
  5596. ]);
  5597. $expected = [
  5598. 'input' => [
  5599. 'type' => 'time',
  5600. 'name' => 'start_time',
  5601. 'value' => '16:30:00',
  5602. 'step' => '1',
  5603. ],
  5604. ];
  5605. $this->assertHtml($expected, $result);
  5606. }
  5607. /**
  5608. * testDate method
  5609. *
  5610. * Test the date type.
  5611. */
  5612. public function testDate(): void
  5613. {
  5614. $result = $this->Form->date('start_day', [
  5615. 'value' => '2014-03-08',
  5616. ]);
  5617. $expected = [
  5618. 'input' => [
  5619. 'type' => 'date',
  5620. 'name' => 'start_day',
  5621. 'value' => '2014-03-08',
  5622. ],
  5623. ];
  5624. $this->assertHtml($expected, $result);
  5625. $result = $this->Form->date('start_day', [
  5626. 'value' => new Date('2014-03-08'),
  5627. ]);
  5628. $this->assertHtml($expected, $result);
  5629. }
  5630. /**
  5631. * testDateTime method
  5632. */
  5633. public function testDateTime(): void
  5634. {
  5635. $result = $this->Form->dateTime('date', ['default' => true]);
  5636. $expected = [
  5637. 'input' => [
  5638. 'type' => 'datetime-local',
  5639. 'name' => 'date',
  5640. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5641. 'step' => '1',
  5642. ],
  5643. ];
  5644. $this->assertHtml($expected, $result);
  5645. }
  5646. /**
  5647. * testDateTimeSecured method
  5648. *
  5649. * Test that datetime fields are added to protected fields list.
  5650. */
  5651. public function testDateTimeSecured(): void
  5652. {
  5653. $this->View->setRequest(
  5654. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5655. );
  5656. $this->Form->create();
  5657. $this->Form->dateTime('date');
  5658. $expected = ['date'];
  5659. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5660. $this->assertEquals($expected, $result);
  5661. $this->Form->date('published');
  5662. $expected = ['date', 'published'];
  5663. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5664. $this->assertEquals($expected, $result);
  5665. }
  5666. /**
  5667. * testDateTimeSecuredDisabled method
  5668. *
  5669. * Test that datetime fields are added to protected fields list.
  5670. */
  5671. public function testDateTimeSecuredDisabled(): void
  5672. {
  5673. $this->View->setRequest(
  5674. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5675. );
  5676. $this->Form->create();
  5677. $this->Form->dateTime('date', ['secure' => false]);
  5678. $expected = [];
  5679. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5680. $this->assertEquals($expected, $result);
  5681. $this->Form->date('published', ['secure' => false]);
  5682. $expected = [];
  5683. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5684. $this->assertEquals($expected, $result);
  5685. }
  5686. /**
  5687. * testDatetimeWithDefault method
  5688. *
  5689. * Test that datetime() and default values work.
  5690. */
  5691. public function testDatetimeWithDefault(): void
  5692. {
  5693. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5694. $expected = [
  5695. 'input' => [
  5696. 'type' => 'datetime-local',
  5697. 'name' => 'updated',
  5698. 'value' => '2009-06-01T11:15:30',
  5699. 'step' => '1',
  5700. ],
  5701. ];
  5702. $this->assertHtml($expected, $result);
  5703. $result = $this->Form->dateTime('updated', [
  5704. 'default' => '2009-06-01 11:15:30',
  5705. ]);
  5706. $expected = [
  5707. 'input' => [
  5708. 'type' => 'datetime-local',
  5709. 'name' => 'updated',
  5710. 'value' => '2009-06-01T11:15:30',
  5711. 'step' => '1',
  5712. ],
  5713. ];
  5714. $this->assertHtml($expected, $result);
  5715. }
  5716. /**
  5717. * testMonth method
  5718. *
  5719. * Test generation of a month input.
  5720. */
  5721. public function testMonth(): void
  5722. {
  5723. $result = $this->Form->month('field', ['value' => '']);
  5724. $expected = [
  5725. 'input' => [
  5726. 'type' => 'month',
  5727. 'name' => 'field',
  5728. 'value' => '',
  5729. ],
  5730. ];
  5731. $this->assertHtml($expected, $result);
  5732. $this->View->setRequest(
  5733. $this->View->getRequest()->withData('release', '2050-02-10')
  5734. );
  5735. $this->Form->create();
  5736. $result = $this->Form->month('release');
  5737. $expected = [
  5738. 'input' => [
  5739. 'type' => 'month',
  5740. 'name' => 'release',
  5741. 'value' => '2050-02',
  5742. ],
  5743. ];
  5744. $this->assertHtml($expected, $result);
  5745. $this->View->setRequest(
  5746. $this->View->getRequest()->withData('release', '2050-03')
  5747. );
  5748. $this->Form->create();
  5749. $result = $this->Form->month('release');
  5750. $expected = [
  5751. 'input' => [
  5752. 'type' => 'month',
  5753. 'name' => 'release',
  5754. 'value' => '2050-03',
  5755. ],
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. }
  5759. /**
  5760. * testYear method
  5761. *
  5762. * Test generation of a year input.
  5763. */
  5764. public function testYear(): void
  5765. {
  5766. $this->View->setRequest(
  5767. $this->View->getRequest()->withData('published', '2006')
  5768. );
  5769. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5770. $expected = [
  5771. ['select' => ['name' => 'field']],
  5772. ['option' => ['selected' => 'selected', 'value' => '']],
  5773. '/option',
  5774. ['option' => ['value' => '2007']],
  5775. '2007',
  5776. '/option',
  5777. ['option' => ['value' => '2006']],
  5778. '2006',
  5779. '/option',
  5780. '/select',
  5781. ];
  5782. $this->assertHtml($expected, $result);
  5783. $result = $this->Form->year('field', [
  5784. 'value' => '',
  5785. 'min' => 2006,
  5786. 'max' => 2007,
  5787. 'order' => 'asc',
  5788. ]);
  5789. $expected = [
  5790. ['select' => ['name' => 'field']],
  5791. ['option' => ['selected' => 'selected', 'value' => '']],
  5792. '/option',
  5793. ['option' => ['value' => '2006']],
  5794. '2006',
  5795. '/option',
  5796. ['option' => ['value' => '2007']],
  5797. '2007',
  5798. '/option',
  5799. '/select',
  5800. ];
  5801. $this->assertHtml($expected, $result);
  5802. $result = $this->Form->year('published', [
  5803. 'empty' => false,
  5804. 'min' => 2006,
  5805. 'max' => 2007,
  5806. ]);
  5807. $expected = [
  5808. ['select' => ['name' => 'published']],
  5809. ['option' => ['value' => '2007']],
  5810. '2007',
  5811. '/option',
  5812. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5813. '2006',
  5814. '/option',
  5815. '/select',
  5816. ];
  5817. $this->assertHtml($expected, $result);
  5818. $result = $this->Form->year('published', [
  5819. 'empty' => false,
  5820. 'value' => new Date('2008-01-12'),
  5821. 'min' => 2007,
  5822. 'max' => 2009,
  5823. ]);
  5824. $expected = [
  5825. ['select' => ['name' => 'published']],
  5826. ['option' => ['value' => '2009']],
  5827. '2009',
  5828. '/option',
  5829. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5830. '2008',
  5831. '/option',
  5832. ['option' => ['value' => '2007']],
  5833. '2007',
  5834. '/option',
  5835. '/select',
  5836. ];
  5837. $this->assertHtml($expected, $result);
  5838. $result = $this->Form->year('published', [
  5839. 'empty' => 'Published on',
  5840. ]);
  5841. $this->assertStringContainsString('Published on', $result);
  5842. }
  5843. /**
  5844. * testControlYearPreEpoch method
  5845. *
  5846. * Test minYear being prior to the unix epoch.
  5847. */
  5848. public function testControlYearPreEpoch(): void
  5849. {
  5850. $start = date('Y') - 80;
  5851. $end = date('Y') - 18;
  5852. $result = $this->Form->control('birth_year', [
  5853. 'type' => 'year',
  5854. 'label' => 'Birth Year',
  5855. 'min' => $start,
  5856. 'max' => $end,
  5857. ]);
  5858. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5859. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5860. $this->assertStringNotContainsString('value="00">00', $result);
  5861. }
  5862. /**
  5863. * test control() datetime & required attributes
  5864. */
  5865. public function testControlDatetimeRequired(): void
  5866. {
  5867. $result = $this->Form->control('birthday', [
  5868. 'type' => 'date',
  5869. 'required' => true,
  5870. ]);
  5871. $this->assertStringContainsString(
  5872. '<input type="date" name="birthday" required="required"',
  5873. $result
  5874. );
  5875. }
  5876. /**
  5877. * testYearAutoExpandRange method
  5878. */
  5879. public function testYearAutoExpandRange(): void
  5880. {
  5881. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5882. $result = $this->Form->year('birthday');
  5883. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5884. $result = $matches[1];
  5885. $expected = range(date('Y') + 5, 1930);
  5886. $this->assertEquals($expected, $result);
  5887. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5888. $this->Form->create();
  5889. $result = $this->Form->year('release');
  5890. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5891. $result = $matches[1];
  5892. $expected = range(2050, date('Y') - 5);
  5893. $this->assertEquals($expected, $result);
  5894. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5895. $this->Form->create();
  5896. $result = $this->Form->year('release', [
  5897. 'min' => 1890,
  5898. 'max' => 1900,
  5899. ]);
  5900. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5901. $result = $matches[1];
  5902. $expected = range(1900, 1881);
  5903. $this->assertEquals($expected, $result);
  5904. }
  5905. /**
  5906. * test control placeholder + label
  5907. */
  5908. public function testControlLabelAndPlaceholder(): void
  5909. {
  5910. $this->Form->create($this->article);
  5911. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5912. $expected = [
  5913. 'div' => ['class' => 'input text required'],
  5914. 'label' => ['for' => 'title'],
  5915. 'Title',
  5916. '/label',
  5917. 'input' => [
  5918. 'type' => 'text',
  5919. 'required' => 'required',
  5920. 'placeholder' => 'Add title',
  5921. 'id' => 'title',
  5922. 'name' => 'title',
  5923. 'data-validity-message' => 'This field cannot be left empty',
  5924. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5925. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5926. ],
  5927. '/div',
  5928. ];
  5929. $this->assertHtml($expected, $result);
  5930. }
  5931. /**
  5932. * testControlLabelFalse method
  5933. *
  5934. * Test the label option being set to false.
  5935. */
  5936. public function testControlLabelFalse(): void
  5937. {
  5938. $this->Form->create($this->article);
  5939. $result = $this->Form->control('title', ['label' => false]);
  5940. $expected = [
  5941. 'div' => ['class' => 'input text required'],
  5942. 'input' => [
  5943. 'type' => 'text',
  5944. 'required' => 'required',
  5945. 'id' => 'title',
  5946. 'name' => 'title',
  5947. 'data-validity-message' => 'This field cannot be left empty',
  5948. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5949. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5950. ],
  5951. '/div',
  5952. ];
  5953. $this->assertHtml($expected, $result);
  5954. $this->Form->create($this->article);
  5955. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5956. $expected['input'] += [
  5957. 'placeholder' => 'Add title',
  5958. 'aria-label' => 'Add title',
  5959. ];
  5960. $this->assertHtml($expected, $result);
  5961. }
  5962. /**
  5963. * testTextArea method
  5964. *
  5965. * Test generation of a textarea input.
  5966. */
  5967. public function testTextArea(): void
  5968. {
  5969. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5970. $result = $this->Form->textarea('field');
  5971. $expected = [
  5972. 'textarea' => ['name' => 'field', 'rows' => 5],
  5973. 'some test data',
  5974. '/textarea',
  5975. ];
  5976. $this->assertHtml($expected, $result);
  5977. $result = $this->Form->textarea('user.bio');
  5978. $expected = [
  5979. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5980. '/textarea',
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. $this->View->setRequest($this->View->getRequest()
  5984. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5985. $this->Form->create();
  5986. $result = $this->Form->textarea('field');
  5987. $expected = [
  5988. 'textarea' => ['name' => 'field', 'rows' => 5],
  5989. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5990. '/textarea',
  5991. ];
  5992. $this->assertHtml($expected, $result);
  5993. $this->View->setRequest($this->View->getRequest()->withData(
  5994. 'Model.field',
  5995. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5996. ));
  5997. $this->Form->create();
  5998. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5999. $expected = [
  6000. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6001. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6002. '/textarea',
  6003. ];
  6004. $this->assertHtml($expected, $result);
  6005. $result = $this->Form->textarea('0.OtherModel.field');
  6006. $expected = [
  6007. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6008. '/textarea',
  6009. ];
  6010. $this->assertHtml($expected, $result);
  6011. }
  6012. /**
  6013. * testTextAreaWithStupidCharacters method
  6014. *
  6015. * Test text area with non-ascii characters.
  6016. */
  6017. public function testTextAreaWithStupidCharacters(): void
  6018. {
  6019. $result = $this->Form->textarea('Post.content', [
  6020. 'value' => 'GREAT®',
  6021. 'rows' => '15',
  6022. 'cols' => '75',
  6023. ]);
  6024. $expected = [
  6025. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6026. 'GREAT®',
  6027. '/textarea',
  6028. ];
  6029. $this->assertHtml($expected, $result);
  6030. }
  6031. /**
  6032. * testTextAreaMaxLength method
  6033. *
  6034. * Test textareas maxlength read from schema.
  6035. */
  6036. public function testTextAreaMaxLength(): void
  6037. {
  6038. $this->Form->create([
  6039. 'schema' => [
  6040. 'stuff' => ['type' => 'string', 'length' => 10],
  6041. ],
  6042. ]);
  6043. $result = $this->Form->control('other', ['type' => 'textarea']);
  6044. $expected = [
  6045. 'div' => ['class' => 'input textarea'],
  6046. 'label' => ['for' => 'other'],
  6047. 'Other',
  6048. '/label',
  6049. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6050. '/textarea',
  6051. '/div',
  6052. ];
  6053. $this->assertHtml($expected, $result);
  6054. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6055. $expected = [
  6056. 'div' => ['class' => 'input textarea'],
  6057. 'label' => ['for' => 'stuff'],
  6058. 'Stuff',
  6059. '/label',
  6060. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6061. '/textarea',
  6062. '/div',
  6063. ];
  6064. $this->assertHtml($expected, $result);
  6065. }
  6066. /**
  6067. * testHiddenField method
  6068. *
  6069. * Test generation of a hidden input.
  6070. */
  6071. public function testHidden(): void
  6072. {
  6073. $this->article['errors'] = [
  6074. 'field' => true,
  6075. ];
  6076. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6077. $this->Form->create($this->article);
  6078. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6079. $expected = [
  6080. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6081. $this->assertHtml($expected, $result);
  6082. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6083. $expected = [
  6084. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6085. ];
  6086. $this->assertHtml($expected, $result);
  6087. $article = new Article([
  6088. 'status' => ArticleStatus::Unpublished,
  6089. ]);
  6090. $this->Form->create($article);
  6091. $result = $this->Form->hidden('status');
  6092. $expected = [
  6093. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6094. ];
  6095. $this->assertHtml($expected, $result);
  6096. }
  6097. /**
  6098. * Test hidden() with various boolean values.
  6099. */
  6100. public function testHiddenBooleanValues(): void
  6101. {
  6102. $this->Form->create($this->article);
  6103. $result = $this->Form->hidden('field', ['value' => null]);
  6104. $expected = [
  6105. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $result = $this->Form->hidden('field', ['value' => true]);
  6109. $expected = [
  6110. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6111. ];
  6112. $this->assertHtml($expected, $result);
  6113. $result = $this->Form->hidden('field', ['value' => false]);
  6114. $expected = [
  6115. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6116. ];
  6117. $this->assertHtml($expected, $result);
  6118. }
  6119. /**
  6120. * testFileUploadField method
  6121. *
  6122. * Test generation of a file upload input.
  6123. */
  6124. public function testFileUploadField(): void
  6125. {
  6126. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6127. $result = $this->Form->file('Model.upload');
  6128. $this->assertHtml($expected, $result);
  6129. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6130. 'name' => '', 'type' => '', 'tmp_name' => '',
  6131. 'error' => 4, 'size' => 0,
  6132. ]));
  6133. $result = $this->Form->file('Model.upload');
  6134. $this->assertHtml($expected, $result);
  6135. $this->View->setRequest(
  6136. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6137. );
  6138. $result = $this->Form->file('Model.upload');
  6139. $this->assertHtml($expected, $result);
  6140. }
  6141. /**
  6142. * testFileUploadOnOtherModel method
  6143. *
  6144. * Test File upload input on a model not used in create().
  6145. */
  6146. public function testFileUploadOnOtherModel(): void
  6147. {
  6148. $this->Form->create($this->article, ['type' => 'file']);
  6149. $result = $this->Form->file('ValidateProfile.city');
  6150. $expected = [
  6151. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6152. ];
  6153. $this->assertHtml($expected, $result);
  6154. }
  6155. /**
  6156. * testButton method
  6157. *
  6158. * Test generation of a form button.
  6159. */
  6160. public function testButton(): void
  6161. {
  6162. $result = $this->Form->button('Hi');
  6163. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6164. $this->assertHtml($expected, $result);
  6165. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6166. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6167. $this->assertHtml($expected, $result);
  6168. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6169. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6170. $this->assertHtml($expected, $result);
  6171. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6172. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6173. $this->assertHtml($expected, $result);
  6174. $result = $this->Form->button('No type', ['type' => false]);
  6175. $expected = ['button' => [], 'No type', '/button'];
  6176. $this->assertHtml($expected, $result);
  6177. $result = $this->Form->button('Upload Text', [
  6178. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6179. 'escape' => false,
  6180. ]);
  6181. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6182. }
  6183. /**
  6184. * testButtonUnlockedByDefault method
  6185. *
  6186. * Test that button() makes unlocked fields by default.
  6187. */
  6188. public function testButtonUnlockedByDefault(): void
  6189. {
  6190. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6191. $this->Form->create();
  6192. $this->Form->button('Save', ['name' => 'save']);
  6193. $this->Form->button('Clear');
  6194. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6195. $this->assertEquals(['save'], $result);
  6196. }
  6197. /**
  6198. * Test generation of a form button with confirm message.
  6199. */
  6200. public function testButtonWithConfirm(): void
  6201. {
  6202. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6203. $expected = ['button' => [
  6204. 'type' => 'submit',
  6205. 'data-confirm-message' => 'Confirm me!',
  6206. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6207. ], 'Hi', '/button'];
  6208. $this->assertHtml($expected, $result);
  6209. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6210. $expected = ['button' => [
  6211. 'type' => 'submit',
  6212. 'data-confirm-message' => 'Confirm "me"!',
  6213. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6214. ], 'Hi', '/button'];
  6215. $this->assertHtml($expected, $result);
  6216. }
  6217. /**
  6218. * testPostButton method
  6219. */
  6220. public function testPostButton(): void
  6221. {
  6222. $result = $this->Form->postButton('Hi', '/controller/action');
  6223. $expected = [
  6224. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6225. 'button' => ['type' => 'submit'],
  6226. 'Hi',
  6227. '/button',
  6228. '/form',
  6229. ];
  6230. $this->assertHtml($expected, $result);
  6231. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6232. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6233. }
  6234. /**
  6235. * testPostButtonMethodType method
  6236. */
  6237. public function testPostButtonMethodType(): void
  6238. {
  6239. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6240. $expected = [
  6241. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6242. 'div' => ['style' => 'display:none;'],
  6243. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6244. '/div',
  6245. 'button' => ['type' => 'submit'],
  6246. 'Hi',
  6247. '/button',
  6248. '/form',
  6249. ];
  6250. $this->assertHtml($expected, $result);
  6251. }
  6252. /**
  6253. * testPostButtonFormOptions method
  6254. */
  6255. public function testPostButtonFormOptions(): void
  6256. {
  6257. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6258. $expected = [
  6259. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6260. 'button' => ['type' => 'submit'],
  6261. 'Hi',
  6262. '/button',
  6263. '/form',
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. }
  6267. /**
  6268. * testPostButtonNestedData method
  6269. *
  6270. * Test using postButton with N dimensional data.
  6271. */
  6272. public function testPostButtonNestedData(): void
  6273. {
  6274. $data = [
  6275. 'one' => [
  6276. 'two' => [
  6277. 3, 4, 5,
  6278. ],
  6279. ],
  6280. ];
  6281. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6282. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6283. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6284. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6285. }
  6286. /**
  6287. * testSecurePostButton method
  6288. *
  6289. * Test that postButton adds _Token fields.
  6290. */
  6291. public function testSecurePostButton(): void
  6292. {
  6293. $this->View->setRequest($this->View->getRequest()
  6294. ->withAttribute('csrfToken', 'testkey')
  6295. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6296. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6297. $tokenDebug = urlencode(json_encode([
  6298. '/posts/delete/1',
  6299. [],
  6300. [],
  6301. ]));
  6302. $expected = [
  6303. 'form' => [
  6304. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6305. ],
  6306. ['div' => ['style' => 'display:none;']],
  6307. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6308. '/div',
  6309. 'button' => ['type' => 'submit'],
  6310. 'Delete',
  6311. '/button',
  6312. ['div' => ['style' => 'display:none;']],
  6313. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6314. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6315. ['input' => [
  6316. 'type' => 'hidden',
  6317. 'name' => '_Token[debug]',
  6318. 'value' => $tokenDebug,
  6319. ]],
  6320. '/div',
  6321. '/form',
  6322. ];
  6323. $this->assertHtml($expected, $result);
  6324. }
  6325. /**
  6326. * testPostLink method
  6327. */
  6328. public function testPostLink(): void
  6329. {
  6330. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6331. $expected = [
  6332. 'form' => [
  6333. 'method' => 'post', 'action' => '/posts/delete/1',
  6334. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6335. ],
  6336. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6337. '/form',
  6338. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6339. 'Delete',
  6340. '/a',
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6344. $expected = [
  6345. 'form' => [
  6346. 'method' => 'post', 'action' => '/posts/delete/1',
  6347. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6348. ],
  6349. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6350. '/form',
  6351. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6352. 'Delete',
  6353. '/a',
  6354. ];
  6355. $this->assertHtml($expected, $result);
  6356. $result = $this->Form->postLink(
  6357. 'Delete',
  6358. '/posts/delete/1',
  6359. ['target' => '_blank', 'class' => 'btn btn-danger']
  6360. );
  6361. $expected = [
  6362. 'form' => [
  6363. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6364. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6365. ],
  6366. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6367. '/form',
  6368. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6369. 'Delete',
  6370. '/a',
  6371. ];
  6372. $this->assertHtml($expected, $result);
  6373. }
  6374. /**
  6375. * testPostLinkWithConfirm method
  6376. *
  6377. * Test the confirm option for postLink().
  6378. */
  6379. public function testPostLinkWithConfirm(): void
  6380. {
  6381. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6382. $expected = [
  6383. 'form' => [
  6384. 'method' => 'post', 'action' => '/posts/delete/1',
  6385. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6386. ],
  6387. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6388. '/form',
  6389. 'a' => [
  6390. 'href' => '#',
  6391. 'data-confirm-message' => 'Confirm?',
  6392. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6393. ],
  6394. 'Delete',
  6395. '/a',
  6396. ];
  6397. $this->assertHtml($expected, $result);
  6398. $result = $this->Form->postLink(
  6399. 'Delete',
  6400. '/posts/delete/1',
  6401. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6402. );
  6403. $expected = [
  6404. 'form' => [
  6405. 'method' => 'post', 'action' => '/posts/delete/1',
  6406. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6407. ],
  6408. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6409. '/form',
  6410. 'a' => [
  6411. 'href' => '#',
  6412. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6413. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6414. ],
  6415. 'Delete',
  6416. '/a',
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6420. $result = $this->Form->postLink(
  6421. 'Delete',
  6422. '/posts/delete/1',
  6423. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6424. );
  6425. $expected = [
  6426. 'form' => [
  6427. 'method' => 'post', 'action' => '/posts/delete/1',
  6428. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6429. ],
  6430. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6431. '/form',
  6432. 'a' => [
  6433. 'href' => '#',
  6434. 'data-confirm-message' => 'Confirm this deletion?',
  6435. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6436. ],
  6437. 'Delete',
  6438. '/a',
  6439. ];
  6440. $this->assertHtml($expected, $result);
  6441. }
  6442. /**
  6443. * testPostLinkWithQuery method
  6444. *
  6445. * Test postLink() with query string args.
  6446. */
  6447. public function testPostLinkWithQuery(): void
  6448. {
  6449. $result = $this->Form->postLink(
  6450. 'Delete',
  6451. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6452. );
  6453. $expected = [
  6454. 'form' => [
  6455. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6456. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6457. ],
  6458. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6459. '/form',
  6460. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6461. 'Delete',
  6462. '/a',
  6463. ];
  6464. $this->assertHtml($expected, $result);
  6465. }
  6466. /**
  6467. * testPostLinkWithData method
  6468. *
  6469. * Test postLink with additional data.
  6470. */
  6471. public function testPostLinkWithData(): void
  6472. {
  6473. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6474. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6475. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6476. $this->Form->create($entity);
  6477. $this->Form->end();
  6478. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6479. $this->assertStringContainsString(
  6480. '<input type="hidden" name="name" value="show"',
  6481. $result,
  6482. 'should not contain entity data.'
  6483. );
  6484. }
  6485. /**
  6486. * testPostLinkSecurityHash method
  6487. *
  6488. * Test that security hashes for postLink include the url.
  6489. */
  6490. public function testPostLinkSecurityHash(): void
  6491. {
  6492. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6493. $hash .= '%3Aid';
  6494. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6495. $result = $this->Form->postLink(
  6496. 'Delete',
  6497. '/posts/delete/1',
  6498. ['data' => ['id' => 1]]
  6499. );
  6500. $tokenDebug = urlencode(json_encode([
  6501. '/posts/delete/1',
  6502. [
  6503. 'id' => 1,
  6504. ],
  6505. [],
  6506. ]));
  6507. $expected = [
  6508. 'form' => [
  6509. 'method' => 'post', 'action' => '/posts/delete/1',
  6510. 'name', 'style' => 'display:none;',
  6511. ],
  6512. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6513. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6514. 'div' => ['style' => 'display:none;'],
  6515. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6516. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6517. ['input' => [
  6518. 'type' => 'hidden',
  6519. 'name' => '_Token[debug]',
  6520. 'value' => $tokenDebug,
  6521. ]],
  6522. '/div',
  6523. '/form',
  6524. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6525. 'Delete',
  6526. '/a',
  6527. ];
  6528. $this->assertHtml($expected, $result);
  6529. }
  6530. /**
  6531. * testPostLinkSecurityHashBlockMode method
  6532. *
  6533. * Test that postLink doesn't modify the fields in the containing form.
  6534. *
  6535. * postLink() calls inside open forms should not modify the field list
  6536. * for the form.
  6537. */
  6538. public function testPostLinkSecurityHashBlockMode(): void
  6539. {
  6540. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6541. $hash .= '%3A';
  6542. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6543. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6544. $this->Form->control('title');
  6545. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6546. $result = $this->View->fetch('postLink');
  6547. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6548. $this->assertEquals(['title'], $fields);
  6549. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6550. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6551. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6552. }
  6553. /**
  6554. * testPostLinkSecurityHashNoDebugMode method
  6555. *
  6556. * Test that security does not include debug token if debug is false.
  6557. */
  6558. public function testPostLinkSecurityHashNoDebugMode(): void
  6559. {
  6560. Configure::write('debug', false);
  6561. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6562. $hash .= '%3Aid';
  6563. $this->View->setRequest($this->View->getRequest()
  6564. ->withAttribute('formTokenData', ['key' => 'test']));
  6565. $result = $this->Form->postLink(
  6566. 'Delete',
  6567. '/posts/delete/1',
  6568. ['data' => ['id' => 1]]
  6569. );
  6570. $expected = [
  6571. 'form' => [
  6572. 'method' => 'post', 'action' => '/posts/delete/1',
  6573. 'name', 'style' => 'display:none;',
  6574. ],
  6575. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6576. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6577. 'div' => ['style' => 'display:none;'],
  6578. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6579. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6580. '/div',
  6581. '/form',
  6582. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6583. 'Delete',
  6584. '/a',
  6585. ];
  6586. $this->assertHtml($expected, $result);
  6587. }
  6588. /**
  6589. * testPostLinkNestedData method
  6590. *
  6591. * Test using postLink with N dimensional data.
  6592. */
  6593. public function testPostLinkNestedData(): void
  6594. {
  6595. $data = [
  6596. 'one' => [
  6597. 'two' => [
  6598. 3, 4, 5,
  6599. ],
  6600. ],
  6601. ];
  6602. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6603. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6604. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6605. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6606. }
  6607. /**
  6608. * testPostLinkAfterGetForm method
  6609. *
  6610. * Test creating postLinks after a GET form.
  6611. */
  6612. public function testPostLinkAfterGetForm(): void
  6613. {
  6614. $this->View->setRequest($this->View->getRequest()
  6615. ->withAttribute('csrfToken', 'testkey')
  6616. ->withAttribute('formTokenData', []));
  6617. $this->Form->create($this->article, ['type' => 'get']);
  6618. $this->Form->end();
  6619. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6620. $tokenDebug = urlencode(json_encode([
  6621. '/posts/delete/1',
  6622. [],
  6623. [],
  6624. ]));
  6625. $expected = [
  6626. 'form' => [
  6627. 'method' => 'post', 'action' => '/posts/delete/1',
  6628. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6629. ],
  6630. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6631. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6632. 'div' => ['style' => 'display:none;'],
  6633. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6634. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6635. ['input' => [
  6636. 'type' => 'hidden', 'name' => '_Token[debug]',
  6637. 'value' => $tokenDebug,
  6638. ]],
  6639. '/div',
  6640. '/form',
  6641. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6642. 'Delete',
  6643. '/a',
  6644. ];
  6645. $this->assertHtml($expected, $result);
  6646. }
  6647. /**
  6648. * testPostLinkFormBuffer method
  6649. *
  6650. * Test that postLink adds form tags to view block.
  6651. */
  6652. public function testPostLinkFormBuffer(): void
  6653. {
  6654. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6655. $expected = [
  6656. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6657. 'Delete',
  6658. '/a',
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. $result = $this->View->fetch('postLink');
  6662. $expected = [
  6663. 'form' => [
  6664. 'method' => 'post', 'action' => '/posts/delete/1',
  6665. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6666. ],
  6667. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6668. '/form',
  6669. ];
  6670. $this->assertHtml($expected, $result);
  6671. $result = $this->Form->postLink(
  6672. 'Delete',
  6673. '/posts/delete/2',
  6674. ['block' => true, 'method' => 'DELETE']
  6675. );
  6676. $expected = [
  6677. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6678. 'Delete',
  6679. '/a',
  6680. ];
  6681. $this->assertHtml($expected, $result);
  6682. $result = $this->View->fetch('postLink');
  6683. $expected = [
  6684. 'form' => [
  6685. 'method' => 'post', 'action' => '/posts/delete/1',
  6686. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6687. ],
  6688. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6689. '/form',
  6690. [
  6691. 'form' => [
  6692. 'method' => 'post', 'action' => '/posts/delete/2',
  6693. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6694. ],
  6695. ],
  6696. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6697. '/form',
  6698. ];
  6699. $this->assertHtml($expected, $result);
  6700. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6701. $expected = [
  6702. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6703. 'Delete',
  6704. '/a',
  6705. ];
  6706. $this->assertHtml($expected, $result);
  6707. $result = $this->View->fetch('foobar');
  6708. $expected = [
  6709. 'form' => [
  6710. 'method' => 'post', 'action' => '/posts/delete/1',
  6711. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6712. ],
  6713. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6714. '/form',
  6715. ];
  6716. $this->assertHtml($expected, $result);
  6717. }
  6718. /**
  6719. * testSubmitButton method
  6720. */
  6721. public function testSubmitButton(): void
  6722. {
  6723. $result = $this->Form->submit('');
  6724. $expected = [
  6725. 'div' => ['class' => 'submit'],
  6726. 'input' => ['type' => 'submit', 'value' => ''],
  6727. '/div',
  6728. ];
  6729. $this->assertHtml($expected, $result);
  6730. $result = $this->Form->submit('Test Submit');
  6731. $expected = [
  6732. 'div' => ['class' => 'submit'],
  6733. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6734. '/div',
  6735. ];
  6736. $this->assertHtml($expected, $result);
  6737. $result = $this->Form->submit('Next >');
  6738. $expected = [
  6739. 'div' => ['class' => 'submit'],
  6740. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6741. '/div',
  6742. ];
  6743. $this->assertHtml($expected, $result);
  6744. $result = $this->Form->submit('Next >', ['escape' => false]);
  6745. $expected = [
  6746. 'div' => ['class' => 'submit'],
  6747. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6748. '/div',
  6749. ];
  6750. $this->assertHtml($expected, $result);
  6751. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6752. $expected = [
  6753. 'div' => ['class' => 'submit'],
  6754. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6755. '/div',
  6756. ];
  6757. $this->assertHtml($expected, $result);
  6758. }
  6759. /**
  6760. * testSubmitImage method
  6761. *
  6762. * Test image submit types.
  6763. */
  6764. public function testSubmitImage(): void
  6765. {
  6766. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6767. $expected = [
  6768. 'div' => ['class' => 'submit'],
  6769. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6770. '/div',
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. $result = $this->Form->submit('/relative/cake.power.gif');
  6774. $expected = [
  6775. 'div' => ['class' => 'submit'],
  6776. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6777. '/div',
  6778. ];
  6779. $this->assertHtml($expected, $result);
  6780. $result = $this->Form->submit('cake.power.gif');
  6781. $expected = [
  6782. 'div' => ['class' => 'submit'],
  6783. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6784. '/div',
  6785. ];
  6786. $this->assertHtml($expected, $result);
  6787. $result = $this->Form->submit('Not.an.image');
  6788. $expected = [
  6789. 'div' => ['class' => 'submit'],
  6790. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6791. '/div',
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. }
  6795. /**
  6796. * testSubmitUnlockedByDefault method
  6797. *
  6798. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6799. * be submitted at a time.
  6800. */
  6801. public function testSubmitUnlockedByDefault(): void
  6802. {
  6803. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6804. $this->Form->create();
  6805. $this->Form->submit('Go go');
  6806. $this->Form->submit('Save', ['name' => 'save']);
  6807. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6808. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6809. }
  6810. /**
  6811. * testSubmitImageTimestamp method
  6812. *
  6813. * Test submit image with timestamps.
  6814. */
  6815. public function testSubmitImageTimestamp(): void
  6816. {
  6817. Configure::write('Asset.timestamp', 'force');
  6818. $result = $this->Form->submit('cake.power.gif');
  6819. $expected = [
  6820. 'div' => ['class' => 'submit'],
  6821. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6822. '/div',
  6823. ];
  6824. $this->assertHtml($expected, $result);
  6825. }
  6826. /**
  6827. * testDateTimeWithGetForms method
  6828. *
  6829. * Test that datetime() works with GET style forms.
  6830. */
  6831. public function testDateTimeWithGetForms(): void
  6832. {
  6833. $this->Form->create($this->article, ['type' => 'get']);
  6834. $result = $this->Form->datetime('created');
  6835. $expected = [
  6836. 'input' => [
  6837. 'type' => 'datetime-local',
  6838. 'name' => 'created',
  6839. 'value' => '',
  6840. 'step' => '1',
  6841. ],
  6842. ];
  6843. $this->assertHtml($expected, $result);
  6844. $result = $this->Form->datetime('created', ['default' => true]);
  6845. $expected = [
  6846. 'input' => [
  6847. 'type' => 'datetime-local',
  6848. 'name' => 'created',
  6849. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6850. 'step' => '1',
  6851. ],
  6852. ];
  6853. $this->assertHtml($expected, $result);
  6854. }
  6855. /**
  6856. * Provides fractional schema types
  6857. *
  6858. * @return array
  6859. */
  6860. public static function fractionalTypeProvider(): array
  6861. {
  6862. return [
  6863. ['datetimefractional'],
  6864. ['timestampfractional'],
  6865. ['timestamptimezone'],
  6866. ];
  6867. }
  6868. /**
  6869. * testDateTimeWithFractional method
  6870. *
  6871. * Test that datetime() works with datetimefractional.
  6872. *
  6873. * @dataProvider fractionalTypeProvider
  6874. */
  6875. public function testDateTimeWithFractional(string $type): void
  6876. {
  6877. $this->Form->create([
  6878. 'schema' => [
  6879. 'created' => ['type' => $type],
  6880. ],
  6881. ]);
  6882. $result = $this->Form->datetime('created', [
  6883. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6884. ]);
  6885. $expected = [
  6886. 'input' => [
  6887. 'type' => 'datetime-local',
  6888. 'name' => 'created',
  6889. 'value' => '2019-09-27T02:52:43.123',
  6890. 'step' => '0.001',
  6891. ],
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. }
  6895. /**
  6896. * testControlWithFractional method
  6897. *
  6898. * Test that control() works with datetimefractional.
  6899. *
  6900. * @dataProvider fractionalTypeProvider
  6901. */
  6902. public function testControlWithFractional(string $type): void
  6903. {
  6904. $this->Form->create([
  6905. 'schema' => [
  6906. 'created' => ['type' => $type],
  6907. ],
  6908. ]);
  6909. $result = $this->Form->control('created', [
  6910. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6911. ]);
  6912. $expected = [
  6913. 'div' => ['class' => 'input datetime'],
  6914. 'label' => ['for' => 'created'],
  6915. 'Created',
  6916. '/label',
  6917. 'input' => [
  6918. 'type' => 'datetime-local',
  6919. 'name' => 'created',
  6920. 'id' => 'created',
  6921. 'value' => '2019-09-27T02:52:43.123',
  6922. 'step' => '0.001',
  6923. ],
  6924. '/div',
  6925. ];
  6926. $this->assertHtml($expected, $result);
  6927. }
  6928. /**
  6929. * testForMagicControlNonExistentNotValidated method
  6930. */
  6931. public function testForMagicControlNonExistentNotValidated(): void
  6932. {
  6933. $this->Form->create($this->article);
  6934. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6935. $result = $this->Form->control('nonexistent_not_validated');
  6936. $expected = [
  6937. 'label' => ['for' => 'nonexistent-not-validated'],
  6938. 'Nonexistent Not Validated',
  6939. '/label',
  6940. 'input' => [
  6941. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6942. 'id' => 'nonexistent-not-validated',
  6943. ],
  6944. ];
  6945. $this->assertHtml($expected, $result);
  6946. $result = $this->Form->control('nonexistent_not_validated', [
  6947. 'val' => 'my value',
  6948. ]);
  6949. $expected = [
  6950. 'label' => ['for' => 'nonexistent-not-validated'],
  6951. 'Nonexistent Not Validated',
  6952. '/label',
  6953. 'input' => [
  6954. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6955. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6956. ],
  6957. ];
  6958. $this->assertHtml($expected, $result);
  6959. $this->View->setRequest(
  6960. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6961. );
  6962. $this->Form->create($this->article);
  6963. $result = $this->Form->control('nonexistent_not_validated');
  6964. $expected = [
  6965. 'label' => ['for' => 'nonexistent-not-validated'],
  6966. 'Nonexistent Not Validated',
  6967. '/label',
  6968. 'input' => [
  6969. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6970. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6971. ],
  6972. ];
  6973. $this->assertHtml($expected, $result);
  6974. }
  6975. /**
  6976. * testFormMagicControlLabel method
  6977. */
  6978. public function testFormMagicControlLabel(): void
  6979. {
  6980. $this->getTableLocator()->get('Contacts', [
  6981. 'className' => ContactsTable::class,
  6982. ]);
  6983. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6984. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6985. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6986. $expected = [
  6987. 'label' => ['for' => 'contacts-name'],
  6988. 'My label',
  6989. '/label',
  6990. 'input' => [
  6991. 'type' => 'text',
  6992. 'name' => 'Contacts[name]',
  6993. 'id' => 'contacts-name',
  6994. 'maxlength' => '255',
  6995. ],
  6996. ];
  6997. $this->assertHtml($expected, $result);
  6998. $result = $this->Form->control('name', [
  6999. 'label' => ['class' => 'mandatory'],
  7000. ]);
  7001. $expected = [
  7002. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7003. 'Name',
  7004. '/label',
  7005. 'input' => [
  7006. 'type' => 'text', 'name' => 'name',
  7007. 'id' => 'name', 'maxlength' => '255',
  7008. ],
  7009. ];
  7010. $this->assertHtml($expected, $result);
  7011. $result = $this->Form->control('name', [
  7012. 'div' => false,
  7013. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7014. ]);
  7015. $expected = [
  7016. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7017. 'My label',
  7018. '/label',
  7019. 'input' => [
  7020. 'type' => 'text', 'name' => 'name',
  7021. 'id' => 'name', 'maxlength' => '255',
  7022. ],
  7023. ];
  7024. $this->assertHtml($expected, $result);
  7025. $result = $this->Form->control('Contact.name', [
  7026. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7027. ]);
  7028. $expected = [
  7029. 'label' => ['for' => 'my_id'],
  7030. 'Name',
  7031. '/label',
  7032. 'input' => [
  7033. 'type' => 'text', 'name' => 'Contact[name]',
  7034. 'id' => 'my_id', 'maxlength' => '255',
  7035. ],
  7036. ];
  7037. $this->assertHtml($expected, $result);
  7038. $result = $this->Form->control('1.id');
  7039. $expected = ['input' => [
  7040. 'type' => 'hidden', 'name' => '1[id]',
  7041. 'id' => '1-id',
  7042. ]];
  7043. $this->assertHtml($expected, $result);
  7044. $result = $this->Form->control('1.name');
  7045. $expected = [
  7046. 'label' => ['for' => '1-name'],
  7047. 'Name',
  7048. '/label',
  7049. 'input' => [
  7050. 'type' => 'text', 'name' => '1[name]',
  7051. 'id' => '1-name', 'maxlength' => '255',
  7052. ],
  7053. ];
  7054. $this->assertHtml($expected, $result);
  7055. }
  7056. /**
  7057. * testFormEnd method
  7058. */
  7059. public function testFormEnd(): void
  7060. {
  7061. $this->assertSame('</form>', $this->Form->end());
  7062. }
  7063. /**
  7064. * testMultiRecordForm method
  7065. *
  7066. * Test the generation of fields for a multi record form.
  7067. */
  7068. public function testMultiRecordForm(): void
  7069. {
  7070. $articles = $this->getTableLocator()->get('Articles');
  7071. $articles->hasMany('Comments');
  7072. $comment = new Entity(['comment' => 'Value']);
  7073. $article = new Article(['comments' => [$comment]]);
  7074. $this->Form->create([$article]);
  7075. $result = $this->Form->control('0.comments.1.comment');
  7076. // phpcs:disable
  7077. $expected = [
  7078. 'div' => ['class' => 'input textarea'],
  7079. 'label' => ['for' => '0-comments-1-comment'],
  7080. 'Comment',
  7081. '/label',
  7082. 'textarea' => [
  7083. 'name',
  7084. 'id' => '0-comments-1-comment',
  7085. 'rows' => 5
  7086. ],
  7087. '/textarea',
  7088. '/div'
  7089. ];
  7090. // phpcs:enable
  7091. $this->assertHtml($expected, $result);
  7092. $result = $this->Form->control('0.comments.0.comment');
  7093. // phpcs:disable
  7094. $expected = [
  7095. 'div' => ['class' => 'input textarea'],
  7096. 'label' => ['for' => '0-comments-0-comment'],
  7097. 'Comment',
  7098. '/label',
  7099. 'textarea' => [
  7100. 'name',
  7101. 'id' => '0-comments-0-comment',
  7102. 'rows' => 5
  7103. ],
  7104. 'Value',
  7105. '/textarea',
  7106. '/div'
  7107. ];
  7108. // phpcs:enable
  7109. $this->assertHtml($expected, $result);
  7110. $comment->setError('comment', ['Not valid']);
  7111. $result = $this->Form->control('0.comments.0.comment');
  7112. // phpcs:disable
  7113. $expected = [
  7114. 'div' => ['class' => 'input textarea error'],
  7115. 'label' => ['for' => '0-comments-0-comment'],
  7116. 'Comment',
  7117. '/label',
  7118. 'textarea' => [
  7119. 'name',
  7120. 'class' => 'form-error',
  7121. 'id' => '0-comments-0-comment',
  7122. 'aria-invalid' => 'true',
  7123. 'aria-describedby' => '0-comments-0-comment-error',
  7124. 'rows' => 5
  7125. ],
  7126. 'Value',
  7127. '/textarea',
  7128. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7129. 'Not valid',
  7130. '/div',
  7131. '/div'
  7132. ];
  7133. // phpcs:enable
  7134. $this->assertHtml($expected, $result);
  7135. $this->getTableLocator()->get('Comments')
  7136. ->getValidator('default')
  7137. ->allowEmptyString('comment', null, false);
  7138. $result = $this->Form->control('0.comments.1.comment');
  7139. // phpcs:disable
  7140. $expected = [
  7141. 'div' => ['class' => 'input textarea required'],
  7142. 'label' => ['for' => '0-comments-1-comment'],
  7143. 'Comment',
  7144. '/label',
  7145. 'textarea' => [
  7146. 'name',
  7147. 'required' => 'required',
  7148. 'id' => '0-comments-1-comment',
  7149. 'rows' => 5,
  7150. 'data-validity-message' => 'This field cannot be left empty',
  7151. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7152. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7153. ],
  7154. '/textarea',
  7155. '/div'
  7156. ];
  7157. // phpcs:enable
  7158. $this->assertHtml($expected, $result);
  7159. }
  7160. /**
  7161. * testHtml5Controls method
  7162. *
  7163. * Test that some html5 inputs + FormHelper::__call() work.
  7164. */
  7165. public function testHtml5Controls(): void
  7166. {
  7167. $result = $this->Form->email('User.email');
  7168. $expected = [
  7169. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7170. ];
  7171. $this->assertHtml($expected, $result);
  7172. $result = $this->Form->search('User.query');
  7173. $expected = [
  7174. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7175. ];
  7176. $this->assertHtml($expected, $result);
  7177. $result = $this->Form->search('User.query', ['value' => 'test']);
  7178. $expected = [
  7179. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7180. ];
  7181. $this->assertHtml($expected, $result);
  7182. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7183. $expected = [
  7184. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7185. ];
  7186. $this->assertHtml($expected, $result);
  7187. }
  7188. /**
  7189. * testHtml5ControlWithControl method
  7190. *
  7191. * Test accessing html5 inputs through control().
  7192. */
  7193. public function testHtml5ControlWithControl(): void
  7194. {
  7195. $this->Form->create();
  7196. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7197. $result = $this->Form->control('website', [
  7198. 'type' => 'url',
  7199. 'val' => 'http://domain.tld',
  7200. 'label' => false,
  7201. ]);
  7202. $expected = [
  7203. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7204. ];
  7205. $this->assertHtml($expected, $result);
  7206. }
  7207. /**
  7208. * testHtml5ControlException method
  7209. *
  7210. * Test errors when field name is missing.
  7211. */
  7212. public function testHtml5ControlException(): void
  7213. {
  7214. $this->expectException(CakeException::class);
  7215. $this->Form->email();
  7216. }
  7217. /**
  7218. * tests fields that are required use custom validation messages
  7219. */
  7220. public function testHtml5ErrorMessage(): void
  7221. {
  7222. $this->Form->setConfig('autoSetCustomValidity', true);
  7223. $validator = (new Validator())
  7224. ->notEmptyString('email', 'Custom error message')
  7225. ->requirePresence('password')
  7226. ->alphaNumeric('password')
  7227. ->requirePresence('accept_tos')
  7228. ->notBlank('phone');
  7229. $table = $this->getTableLocator()->get('Contacts', [
  7230. 'className' => ContactsTable::class,
  7231. ]);
  7232. $table->setValidator('default', $validator);
  7233. $contact = new Entity();
  7234. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7235. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7236. $result = $this->Form->control('password');
  7237. $expected = [
  7238. 'label' => ['for' => 'password'],
  7239. 'Password',
  7240. '/label',
  7241. 'input' => [
  7242. 'id' => 'password',
  7243. 'name' => 'password',
  7244. 'type' => 'password',
  7245. 'value' => '',
  7246. 'required' => 'required',
  7247. 'data-validity-message' => 'This field cannot be left empty',
  7248. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7249. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7250. ],
  7251. ];
  7252. $this->assertHtml($expected, $result);
  7253. $result = $this->Form->control('phone');
  7254. $expected = [
  7255. 'label' => ['for' => 'phone'],
  7256. 'Phone',
  7257. '/label',
  7258. 'input' => [
  7259. 'id' => 'phone',
  7260. 'name' => 'phone',
  7261. 'type' => 'tel',
  7262. 'value' => '',
  7263. 'maxlength' => 255,
  7264. 'required' => 'required',
  7265. 'data-validity-message' => 'This field cannot be left empty',
  7266. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7267. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7268. ],
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->control('email');
  7272. $expected = [
  7273. 'label' => ['for' => 'email'],
  7274. 'Email',
  7275. '/label',
  7276. 'input' => [
  7277. 'id' => 'email',
  7278. 'name' => 'email',
  7279. 'type' => 'email',
  7280. 'value' => '',
  7281. 'maxlength' => 255,
  7282. 'required' => 'required',
  7283. 'data-validity-message' => 'Custom error message',
  7284. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7285. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7286. ],
  7287. ];
  7288. $this->assertHtml($expected, $result);
  7289. $result = $this->Form->control('accept_tos', ['type' => 'checkbox']);
  7290. $expected = [
  7291. ['input' => ['type' => 'hidden', 'name' => 'accept_tos', 'value' => '0']],
  7292. 'label' => ['for' => 'accept-tos'],
  7293. [
  7294. 'input' => [
  7295. 'aria-required' => 'true',
  7296. 'required' => 'required',
  7297. 'type' => 'checkbox',
  7298. 'name' => 'accept_tos',
  7299. 'id' => 'accept-tos',
  7300. 'value' => '1',
  7301. 'data-validity-message' => 'This field cannot be left empty',
  7302. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7303. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.checked) this.setCustomValidity(this.dataset.validityMessage)',
  7304. ],
  7305. ],
  7306. 'Accept Tos',
  7307. '/label',
  7308. ];
  7309. $this->assertHtml($expected, $result);
  7310. }
  7311. /**
  7312. * tests that custom validation messages are in templateVars
  7313. */
  7314. public function testHtml5ErrorMessageInTemplateVars(): void
  7315. {
  7316. $validator = (new Validator())
  7317. ->notEmptyString('email', 'Custom error "message" & entities')
  7318. ->requirePresence('password')
  7319. ->alphaNumeric('password')
  7320. ->notBlank('phone');
  7321. $table = $this->getTableLocator()->get('Contacts', [
  7322. 'className' => ContactsTable::class,
  7323. ]);
  7324. $table->setValidator('default', $validator);
  7325. $contact = new Entity();
  7326. $this->Form->setConfig('autoSetCustomValidity', false);
  7327. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7328. $this->Form->setTemplates([
  7329. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7330. 'inputContainer' => '{{content}}',
  7331. ]);
  7332. $result = $this->Form->control('password');
  7333. $expected = [
  7334. 'label' => ['for' => 'password'],
  7335. 'Password',
  7336. '/label',
  7337. 'input' => [
  7338. 'id' => 'password',
  7339. 'name' => 'password',
  7340. 'type' => 'password',
  7341. 'value' => '',
  7342. 'required' => 'required',
  7343. 'data-message' => 'This field cannot be left empty',
  7344. ],
  7345. ];
  7346. $this->assertHtml($expected, $result);
  7347. $result = $this->Form->control('phone');
  7348. $expected = [
  7349. 'label' => ['for' => 'phone'],
  7350. 'Phone',
  7351. '/label',
  7352. 'input' => [
  7353. 'id' => 'phone',
  7354. 'name' => 'phone',
  7355. 'type' => 'tel',
  7356. 'value' => '',
  7357. 'maxlength' => 255,
  7358. 'required' => 'required',
  7359. 'data-message' => 'This field cannot be left empty',
  7360. ],
  7361. ];
  7362. $this->assertHtml($expected, $result);
  7363. $result = $this->Form->control('email', [
  7364. 'templateVars' => [
  7365. 'custom' => 'data-custom="1"',
  7366. ],
  7367. ]);
  7368. $expected = [
  7369. 'label' => ['for' => 'email'],
  7370. 'Email',
  7371. '/label',
  7372. 'input' => [
  7373. 'id' => 'email',
  7374. 'name' => 'email',
  7375. 'type' => 'email',
  7376. 'value' => '',
  7377. 'maxlength' => 255,
  7378. 'required' => 'required',
  7379. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7380. 'data-custom' => '1',
  7381. ],
  7382. ];
  7383. $this->assertHtml($expected, $result);
  7384. $this->Form->setConfig('autoSetCustomValidity', true);
  7385. }
  7386. /**
  7387. * testRequiredAttribute method
  7388. *
  7389. * Tests that formhelper sets required attributes.
  7390. */
  7391. public function testRequiredAttribute(): void
  7392. {
  7393. $this->article['required'] = [
  7394. 'title' => true,
  7395. 'body' => false,
  7396. ];
  7397. $this->Form->create($this->article);
  7398. $result = $this->Form->control('title');
  7399. $expected = [
  7400. 'div' => ['class' => 'input text required'],
  7401. 'label' => ['for' => 'title'],
  7402. 'Title',
  7403. '/label',
  7404. 'input' => [
  7405. 'type' => 'text',
  7406. 'name' => 'title',
  7407. 'id' => 'title',
  7408. 'required' => 'required',
  7409. 'data-validity-message' => 'This field cannot be left empty',
  7410. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7411. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7412. ],
  7413. '/div',
  7414. ];
  7415. $this->assertHtml($expected, $result);
  7416. $result = $this->Form->control('title', ['required' => false]);
  7417. $this->assertStringNotContainsString('required', $result);
  7418. $result = $this->Form->control('body');
  7419. $expected = [
  7420. 'div' => ['class' => 'input text'],
  7421. 'label' => ['for' => 'body'],
  7422. 'Body',
  7423. '/label',
  7424. 'input' => [
  7425. 'type' => 'text',
  7426. 'name' => 'body',
  7427. 'id' => 'body',
  7428. ],
  7429. '/div',
  7430. ];
  7431. $this->assertHtml($expected, $result);
  7432. $result = $this->Form->control('body', ['required' => true]);
  7433. $this->assertStringContainsString('required', $result);
  7434. }
  7435. /**
  7436. * testControlsNotNested method
  7437. *
  7438. * Tests that it is possible to put inputs outside of the label.
  7439. */
  7440. public function testControlsNotNested(): void
  7441. {
  7442. $this->Form->setTemplates([
  7443. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7444. 'formGroup' => '{{input}}{{label}}',
  7445. ]);
  7446. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7447. $expected = [
  7448. 'div' => ['class' => 'input checkbox'],
  7449. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7450. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7451. 'label' => ['for' => 'foo'],
  7452. 'Foo',
  7453. '/label',
  7454. '/div',
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7458. $expected = [
  7459. 'div' => ['class' => 'input checkbox'],
  7460. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7461. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7462. '/div',
  7463. ];
  7464. $this->assertHtml($expected, $result);
  7465. $result = $this->Form->control('confirm', [
  7466. 'type' => 'radio',
  7467. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7468. ]);
  7469. $expected = [
  7470. 'div' => ['class' => 'input radio'],
  7471. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7472. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7473. ['label' => ['for' => 'confirm-y']],
  7474. 'Yes',
  7475. '/label',
  7476. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7477. ['label' => ['for' => 'confirm-n']],
  7478. 'No',
  7479. '/label',
  7480. '<label',
  7481. 'Confirm',
  7482. '/label',
  7483. '/div',
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->Form->select('category', ['1', '2'], [
  7487. 'multiple' => 'checkbox',
  7488. 'name' => 'fish',
  7489. ]);
  7490. $expected = [
  7491. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7492. ['div' => ['class' => 'checkbox']],
  7493. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7494. ['label' => ['for' => 'fish-0']],
  7495. '1',
  7496. '/label',
  7497. '/div',
  7498. ['div' => ['class' => 'checkbox']],
  7499. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7500. ['label' => ['for' => 'fish-1']],
  7501. '2',
  7502. '/label',
  7503. '/div',
  7504. ];
  7505. $this->assertHtml($expected, $result);
  7506. }
  7507. /**
  7508. * testControlContainerTemplates method
  7509. *
  7510. * Test that *Container templates are used by input.
  7511. */
  7512. public function testControlContainerTemplates(): void
  7513. {
  7514. $this->Form->setTemplates([
  7515. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7516. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7517. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7518. ]);
  7519. $this->article['errors'] = [
  7520. 'Article' => ['published' => 'error message'],
  7521. ];
  7522. $this->Form->create($this->article);
  7523. $result = $this->Form->control('accept', [
  7524. 'type' => 'checkbox',
  7525. ]);
  7526. $expected = [
  7527. 'div' => ['class' => 'check'],
  7528. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7529. 'label' => ['for' => 'accept'],
  7530. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7531. 'Accept',
  7532. '/label',
  7533. '/div',
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. $result = $this->Form->control('accept', [
  7537. 'type' => 'radio',
  7538. 'options' => ['Y', 'N'],
  7539. ]);
  7540. $this->assertStringContainsString('<div class="rad">', $result);
  7541. $result = $this->Form->control('Article.published', [
  7542. 'type' => 'radio',
  7543. 'options' => ['Y', 'N'],
  7544. ]);
  7545. $this->assertStringContainsString('<div class="rad err">', $result);
  7546. }
  7547. /**
  7548. * testFormGroupTemplates method
  7549. *
  7550. * Test that *Container templates are used by input.
  7551. */
  7552. public function testFormGroupTemplates(): void
  7553. {
  7554. $this->Form->setTemplates([
  7555. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7556. ]);
  7557. $this->Form->create($this->article);
  7558. $result = $this->Form->control('accept', [
  7559. 'type' => 'radio',
  7560. 'options' => ['Y', 'N'],
  7561. ]);
  7562. $this->assertStringContainsString('<div class="radio">', $result);
  7563. }
  7564. /**
  7565. * testResetTemplates method
  7566. *
  7567. * Test resetting templates.
  7568. */
  7569. public function testResetTemplates(): void
  7570. {
  7571. $this->Form->setTemplates(['input' => '<input/>']);
  7572. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7573. $this->Form->resetTemplates();
  7574. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7575. }
  7576. /**
  7577. * testContext method
  7578. *
  7579. * Test the context method.
  7580. */
  7581. public function testContext(): void
  7582. {
  7583. $result = $this->Form->context();
  7584. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7585. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7586. $this->assertSame($mock, $this->Form->context($mock));
  7587. $this->assertSame($mock, $this->Form->context());
  7588. }
  7589. /**
  7590. * testAutoDomId method
  7591. */
  7592. public function testAutoDomId(): void
  7593. {
  7594. $result = $this->Form->text('field', ['id' => true]);
  7595. $expected = [
  7596. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7597. ];
  7598. $this->assertHtml($expected, $result);
  7599. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7600. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7601. $expected = [
  7602. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7603. ['label' => ['for' => 'field-0']],
  7604. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7605. 'option A',
  7606. '/label',
  7607. ['label' => ['for' => 'field-1']],
  7608. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7609. 'option B',
  7610. '/label',
  7611. ];
  7612. $this->assertHtml($expected, $result);
  7613. $result = $this->Form->select(
  7614. 'multi_field',
  7615. ['first', 'second'],
  7616. ['multiple' => 'checkbox', 'id' => true]
  7617. );
  7618. $expected = [
  7619. 'input' => [
  7620. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7621. ],
  7622. ['div' => ['class' => 'checkbox']],
  7623. ['label' => ['for' => 'multi-field-0']],
  7624. ['input' => [
  7625. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7626. 'value' => '0', 'id' => 'multi-field-0',
  7627. ]],
  7628. 'first',
  7629. '/label',
  7630. '/div',
  7631. ['div' => ['class' => 'checkbox']],
  7632. ['label' => ['for' => 'multi-field-1']],
  7633. ['input' => [
  7634. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7635. 'value' => '1', 'id' => 'multi-field-1',
  7636. ]],
  7637. 'second',
  7638. '/label',
  7639. '/div',
  7640. ];
  7641. $this->assertHtml($expected, $result);
  7642. }
  7643. /**
  7644. * Test the basic setters and getters for value sources
  7645. */
  7646. public function testFormValueSourcesSettersGetters(): void
  7647. {
  7648. $this->View->setRequest($this->View->getRequest()
  7649. ->withData('id', '1')
  7650. ->withQueryParams(['id' => '2']));
  7651. $expected = ['data', 'context'];
  7652. $result = $this->Form->getValueSources();
  7653. $this->assertEquals($expected, $result);
  7654. $this->Form->setValueSources(['context']);
  7655. $result = $this->Form->getSourceValue('id');
  7656. $this->assertNull($result);
  7657. $this->Form->setValueSources('query');
  7658. $expected = ['query'];
  7659. $result = $this->Form->getValueSources();
  7660. $this->assertEquals($expected, $result);
  7661. $expected = '2';
  7662. $result = $this->Form->getSourceValue('id');
  7663. $this->assertSame($expected, $result);
  7664. $this->Form->setValueSources(['data']);
  7665. $expected = '1';
  7666. $result = $this->Form->getSourceValue('id');
  7667. $this->assertSame($expected, $result);
  7668. $this->Form->setValueSources(['query', 'data']);
  7669. $expected = '2';
  7670. $result = $this->Form->getSourceValue('id');
  7671. $this->assertSame($expected, $result);
  7672. }
  7673. public function testValueSourcesValidation(): void
  7674. {
  7675. $this->expectException(InvalidArgumentException::class);
  7676. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7677. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7678. }
  7679. /**
  7680. * Tests the different input rendering values based on sources values switching
  7681. */
  7682. public function testFormValueSourcesSingleSwitchRendering(): void
  7683. {
  7684. $articles = $this->getTableLocator()->get('Articles');
  7685. $article = new Article();
  7686. $articles->patchEntity($article, ['id' => '3']);
  7687. $this->Form->create($article);
  7688. $this->Form->setValueSources(['context']);
  7689. $result = $this->Form->control('id');
  7690. $expected = [
  7691. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7692. ];
  7693. $this->assertHtml($expected, $result);
  7694. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7695. $this->Form->setValueSources(['query']);
  7696. $result = $this->Form->control('id');
  7697. $expected = [
  7698. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7699. ];
  7700. $this->assertHtml($expected, $result);
  7701. $this->View->setRequest($this->View->getRequest()
  7702. ->withQueryParams(['id' => '5a'])
  7703. ->withData('id', '5b'));
  7704. $this->Form->setValueSources(['context']);
  7705. $this->Form->create($article);
  7706. $result = $this->Form->control('id');
  7707. $expected = [
  7708. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7709. ];
  7710. $this->assertHtml($expected, $result);
  7711. $this->Form->setValueSources(['data']);
  7712. $this->Form->create($article);
  7713. $result = $this->Form->control('id');
  7714. $expected = [
  7715. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7716. ];
  7717. $this->assertHtml($expected, $result);
  7718. $this->Form->setValueSources(['query']);
  7719. $result = $this->Form->control('id');
  7720. $expected = [
  7721. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7722. ];
  7723. $this->assertHtml($expected, $result);
  7724. }
  7725. /**
  7726. * Tests the different input rendering values based on sources values switching while supplying
  7727. * an entity (base context) and multiple sources (such as data, query)
  7728. */
  7729. public function testFormValueSourcesListSwitchRendering(): void
  7730. {
  7731. $articles = $this->getTableLocator()->get('Articles');
  7732. $article = new Article();
  7733. $articles->patchEntity($article, ['id' => '3']);
  7734. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7735. $this->Form->create($article);
  7736. $this->Form->setValueSources(['context', 'query']);
  7737. $result = $this->Form->control('id');
  7738. $expected = [
  7739. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7740. ];
  7741. $this->assertHtml($expected, $result);
  7742. $this->Form->setValueSources(['query', 'context']);
  7743. $result = $this->Form->control('id');
  7744. $expected = [
  7745. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7746. ];
  7747. $this->assertHtml($expected, $result);
  7748. $this->Form->setValueSources(['data', 'query', 'context']);
  7749. $result = $this->Form->control('id');
  7750. $expected = [
  7751. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7752. ];
  7753. $this->assertHtml($expected, $result);
  7754. $this->View->setRequest($this->View->getRequest()
  7755. ->withData('id', '8')
  7756. ->withQueryParams(['id' => '9']));
  7757. $this->Form->setValueSources(['data', 'query', 'context']);
  7758. $result = $this->Form->control('id');
  7759. $expected = [
  7760. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7761. ];
  7762. $this->assertHtml($expected, $result);
  7763. }
  7764. /**
  7765. * Test the different form input renderings based on values sources switchings through form options
  7766. */
  7767. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7768. {
  7769. $articles = $this->getTableLocator()->get('Articles');
  7770. $article = new Article();
  7771. $articles->patchEntity($article, ['id' => '3']);
  7772. $this->View->setRequest(
  7773. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7774. );
  7775. $this->Form->create($article, ['valueSources' => 'query']);
  7776. $result = $this->Form->control('id');
  7777. $expected = [
  7778. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7779. ];
  7780. $this->assertHtml($expected, $result);
  7781. $result = $this->Form->getSourceValue('id');
  7782. $this->assertSame('5', $result);
  7783. $this->Form->setValueSources(['context']);
  7784. $this->Form->create($article, ['valueSources' => 'query']);
  7785. $result = $this->Form->control('id');
  7786. $expected = [
  7787. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7788. ];
  7789. $this->assertHtml($expected, $result);
  7790. $result = $this->Form->getSourceValue('id');
  7791. $this->assertSame('5', $result);
  7792. $this->Form->setValueSources(['query']);
  7793. $this->Form->create($article, ['valueSources' => 'data']);
  7794. $result = $this->Form->control('id');
  7795. $expected = [
  7796. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7797. ];
  7798. $this->assertHtml($expected, $result);
  7799. $result = $this->Form->getSourceValue('id');
  7800. $this->assertSame('4', $result);
  7801. $this->Form->setValueSources(['query']);
  7802. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7803. $result = $this->Form->control('id');
  7804. $expected = [
  7805. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7806. ];
  7807. $this->assertHtml($expected, $result);
  7808. $result = $this->Form->getSourceValue('id');
  7809. $this->assertSame(3, $result);
  7810. }
  7811. /**
  7812. * Test the different form input renderings based on values sources switchings through form options
  7813. */
  7814. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7815. {
  7816. $articles = $this->getTableLocator()->get('Articles');
  7817. $article = new Article();
  7818. $articles->patchEntity($article, ['id' => '3']);
  7819. $this->View->setRequest(
  7820. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7821. );
  7822. $this->Form->setValueSources(['context'])
  7823. ->create($article, ['valueSources' => ['query', 'data']]);
  7824. $result = $this->Form->control('id');
  7825. $expected = [
  7826. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7827. ];
  7828. $this->assertHtml($expected, $result);
  7829. $result = $this->Form->getSourceValue('id');
  7830. $this->assertSame('11', $result);
  7831. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7832. $this->Form->setValueSources(['context'])
  7833. ->create($article, ['valueSources' => ['query', 'data']]);
  7834. $result = $this->Form->control('id');
  7835. $expected = [
  7836. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. $result = $this->Form->getSourceValue('id');
  7840. $this->assertSame('10', $result);
  7841. }
  7842. /**
  7843. * Test the different form values sources resetting through From::end();
  7844. */
  7845. public function testFormValueSourcesResetViaEnd(): void
  7846. {
  7847. $expected = ['data', 'context'];
  7848. $result = $this->Form->getValueSources();
  7849. $this->assertEquals($expected, $result);
  7850. $expected = ['query', 'context', 'data'];
  7851. $this->Form->setValueSources(['query', 'context', 'data']);
  7852. $result = $this->Form->getValueSources();
  7853. $this->assertEquals($expected, $result);
  7854. $this->Form->create();
  7855. $result = $this->Form->getValueSources();
  7856. $this->assertEquals($expected, $result);
  7857. $this->Form->end();
  7858. $result = $this->Form->getValueSources();
  7859. $this->assertEquals(['data', 'context'], $result);
  7860. }
  7861. /**
  7862. * Test sources values defaults handling
  7863. */
  7864. public function testFormValueSourcesDefaults(): void
  7865. {
  7866. $this->View->setRequest(
  7867. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7868. );
  7869. $this->Form->create();
  7870. $result = $this->Form->password('password');
  7871. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7872. $this->assertHtml($expected, $result);
  7873. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7874. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7875. $this->assertHtml($expected, $result);
  7876. $this->Form->setValueSources('query');
  7877. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7878. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7879. $this->assertHtml($expected, $result);
  7880. $this->Form->setValueSources('data');
  7881. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7882. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7883. $this->assertHtml($expected, $result);
  7884. }
  7885. /**
  7886. * Test sources values schema defaults handling
  7887. */
  7888. public function testSourcesValueDoesntExistPassThrough(): void
  7889. {
  7890. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7891. $articles = $this->getTableLocator()->get('Articles');
  7892. $entity = $articles->newEmptyEntity();
  7893. $this->Form->create($entity);
  7894. $this->Form->setValueSources(['query', 'context']);
  7895. $result = $this->Form->getSourceValue('category');
  7896. $this->assertSame('sesame-cookies', $result);
  7897. $this->Form->setValueSources(['context', 'query']);
  7898. $result = $this->Form->getSourceValue('category');
  7899. $this->assertSame('sesame-cookies', $result);
  7900. }
  7901. /**
  7902. * testNestedLabelInput method
  7903. *
  7904. * Test the `nestedInput` parameter
  7905. */
  7906. public function testNestedLabelInput(): void
  7907. {
  7908. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7909. $expected = [
  7910. 'div' => ['class' => 'input text'],
  7911. 'label' => ['for' => 'foo'],
  7912. ['input' => [
  7913. 'type' => 'text',
  7914. 'name' => 'foo',
  7915. 'id' => 'foo',
  7916. ]],
  7917. 'Foo',
  7918. '/label',
  7919. '/div',
  7920. ];
  7921. $this->assertHtml($expected, $result);
  7922. }
  7923. /**
  7924. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7925. *
  7926. * This test makes sure `false` excludes the label from the render
  7927. */
  7928. public function testControlLabelManipulationDisableLabels(): void
  7929. {
  7930. $result = $this->Form->control('test', [
  7931. 'type' => 'radio',
  7932. 'options' => ['A', 'B'],
  7933. 'labelOptions' => false,
  7934. ]);
  7935. $expected = [
  7936. ['div' => ['class' => 'input radio']],
  7937. '<label',
  7938. 'Test',
  7939. '/label',
  7940. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7941. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7942. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7943. '/div',
  7944. ];
  7945. $this->assertHtml($expected, $result);
  7946. $result = $this->Form->control('checkbox1', [
  7947. 'label' => 'My checkboxes',
  7948. 'multiple' => 'checkbox',
  7949. 'type' => 'select',
  7950. 'options' => [
  7951. ['text' => 'First Checkbox', 'value' => 1],
  7952. ['text' => 'Second Checkbox', 'value' => 2],
  7953. ],
  7954. 'labelOptions' => false,
  7955. ]);
  7956. $expected = [
  7957. ['div' => ['class' => 'input select']],
  7958. ['label' => ['for' => 'checkbox1']],
  7959. 'My checkboxes',
  7960. '/label',
  7961. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7962. ['div' => ['class' => 'checkbox']],
  7963. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7964. '/div',
  7965. ['div' => ['class' => 'checkbox']],
  7966. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7967. '/div',
  7968. '/div',
  7969. ];
  7970. $this->assertHtml($expected, $result);
  7971. }
  7972. /**
  7973. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7974. *
  7975. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7976. * added to the class if checked.
  7977. *
  7978. * Also checks to make sure any custom attributes are rendered correctly
  7979. */
  7980. public function testControlLabelManipulationRadios(): void
  7981. {
  7982. $result = $this->Form->control('test', [
  7983. 'type' => 'radio',
  7984. 'options' => ['A', 'B'],
  7985. 'labelOptions' => ['class' => 'custom-class'],
  7986. ]);
  7987. $expected = [
  7988. ['div' => ['class' => 'input radio']],
  7989. '<label',
  7990. 'Test',
  7991. '/label',
  7992. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7993. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7994. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7995. 'A',
  7996. '/label',
  7997. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7998. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7999. 'B',
  8000. '/label',
  8001. '/div',
  8002. ];
  8003. $this->assertHtml($expected, $result);
  8004. $result = $this->Form->control('test', [
  8005. 'type' => 'radio',
  8006. 'options' => ['A', 'B'],
  8007. 'value' => 1,
  8008. 'labelOptions' => ['class' => 'custom-class'],
  8009. ]);
  8010. $expected = [
  8011. ['div' => ['class' => 'input radio']],
  8012. '<label',
  8013. 'Test',
  8014. '/label',
  8015. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8016. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8017. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8018. 'A',
  8019. '/label',
  8020. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8021. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8022. 'B',
  8023. '/label',
  8024. '/div',
  8025. ];
  8026. $this->assertHtml($expected, $result);
  8027. $result = $this->Form->control('test', [
  8028. 'type' => 'radio',
  8029. 'options' => ['A', 'B'],
  8030. 'value' => 1,
  8031. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8032. ]);
  8033. $expected = [
  8034. ['div' => ['class' => 'input radio']],
  8035. '<label',
  8036. 'Test',
  8037. '/label',
  8038. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8039. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8040. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8041. 'A',
  8042. '/label',
  8043. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8044. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8045. 'B',
  8046. '/label',
  8047. '/div',
  8048. ];
  8049. $this->assertHtml($expected, $result);
  8050. $result = $this->Form->radio('test', ['A', 'B'], [
  8051. 'label' => [
  8052. 'class' => ['custom-class', 'another-class'],
  8053. 'data-name' => 'bob',
  8054. ],
  8055. 'value' => 1,
  8056. ]);
  8057. $expected = [
  8058. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  8059. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8060. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8061. 'A',
  8062. '/label',
  8063. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8064. ['input' => [
  8065. 'type' => 'radio',
  8066. 'name' => 'test',
  8067. 'value' => '1',
  8068. 'id' => 'test-1',
  8069. 'checked' => 'checked',
  8070. ]],
  8071. 'B',
  8072. '/label',
  8073. ];
  8074. $this->assertHtml($expected, $result);
  8075. }
  8076. /**
  8077. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8078. *
  8079. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8080. * added to the class if checked.
  8081. *
  8082. * Also checks to make sure any custom attributes are rendered correctly
  8083. */
  8084. public function testControlLabelManipulationCheckboxes(): void
  8085. {
  8086. $result = $this->Form->control('checkbox1', [
  8087. 'label' => 'My checkboxes',
  8088. 'multiple' => 'checkbox',
  8089. 'type' => 'select',
  8090. 'options' => [
  8091. ['text' => 'First Checkbox', 'value' => 1],
  8092. ['text' => 'Second Checkbox', 'value' => 2],
  8093. ],
  8094. 'labelOptions' => ['class' => 'custom-class'],
  8095. 'value' => ['1'],
  8096. ]);
  8097. $expected = [
  8098. ['div' => ['class' => 'input select']],
  8099. ['label' => ['for' => 'checkbox1']],
  8100. 'My checkboxes',
  8101. '/label',
  8102. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8103. ['div' => ['class' => 'checkbox']],
  8104. ['label' => [
  8105. 'class' => 'custom-class selected',
  8106. 'for' => 'checkbox1-1',
  8107. ]],
  8108. ['input' => [
  8109. 'type' => 'checkbox',
  8110. 'name' => 'checkbox1[]',
  8111. 'value' => '1',
  8112. 'id' => 'checkbox1-1',
  8113. 'checked' => 'checked',
  8114. ]],
  8115. 'First Checkbox',
  8116. '/label',
  8117. '/div',
  8118. ['div' => ['class' => 'checkbox']],
  8119. ['label' => [
  8120. 'class' => 'custom-class',
  8121. 'for' => 'checkbox1-2',
  8122. ]],
  8123. ['input' => [
  8124. 'type' => 'checkbox',
  8125. 'name' => 'checkbox1[]',
  8126. 'value' => '2',
  8127. 'id' => 'checkbox1-2',
  8128. ]],
  8129. 'Second Checkbox',
  8130. '/label',
  8131. '/div',
  8132. '/div',
  8133. ];
  8134. $this->assertHtml($expected, $result);
  8135. $result = $this->Form->control('checkbox1', [
  8136. 'label' => 'My checkboxes',
  8137. 'multiple' => 'checkbox',
  8138. 'type' => 'select',
  8139. 'options' => [
  8140. ['text' => 'First Checkbox', 'value' => 1],
  8141. ['text' => 'Second Checkbox', 'value' => 2],
  8142. ],
  8143. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8144. 'value' => ['1'],
  8145. ]);
  8146. $expected = [
  8147. ['div' => ['class' => 'input select']],
  8148. ['label' => ['for' => 'checkbox1']],
  8149. 'My checkboxes',
  8150. '/label',
  8151. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8152. ['div' => ['class' => 'checkbox']],
  8153. ['label' => [
  8154. 'class' => 'custom-class another-class selected',
  8155. 'data-name' => 'bob',
  8156. 'for' => 'checkbox1-1',
  8157. ]],
  8158. ['input' => [
  8159. 'type' => 'checkbox',
  8160. 'name' => 'checkbox1[]',
  8161. 'value' => '1',
  8162. 'id' => 'checkbox1-1',
  8163. 'checked' => 'checked',
  8164. ]],
  8165. 'First Checkbox',
  8166. '/label',
  8167. '/div',
  8168. ['div' => ['class' => 'checkbox']],
  8169. ['label' => [
  8170. 'class' => 'custom-class another-class',
  8171. 'data-name' => 'bob',
  8172. 'for' => 'checkbox1-2',
  8173. ]],
  8174. ['input' => [
  8175. 'type' => 'checkbox',
  8176. 'name' => 'checkbox1[]',
  8177. 'value' => '2',
  8178. 'id' => 'checkbox1-2',
  8179. ]],
  8180. 'Second Checkbox',
  8181. '/label',
  8182. '/div',
  8183. '/div',
  8184. ];
  8185. $this->assertHtml($expected, $result);
  8186. }
  8187. /**
  8188. * testControlMaxLengthArrayContext method
  8189. *
  8190. * Test control() with maxlength attribute in Array Context.
  8191. */
  8192. public function testControlMaxLengthArrayContext(): void
  8193. {
  8194. $this->article['schema'] = [
  8195. 'title' => ['length' => 10],
  8196. ];
  8197. $this->Form->create($this->article);
  8198. $result = $this->Form->control('title');
  8199. $expected = [
  8200. 'div' => ['class'],
  8201. 'label' => ['for'],
  8202. 'Title',
  8203. '/label',
  8204. 'input' => [
  8205. 'id',
  8206. 'name' => 'title',
  8207. 'type' => 'text',
  8208. 'required' => 'required',
  8209. 'maxlength' => 10,
  8210. 'data-validity-message' => 'This field cannot be left empty',
  8211. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8212. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8213. ],
  8214. '/div',
  8215. ];
  8216. $this->assertHtml($expected, $result);
  8217. }
  8218. /**
  8219. * testControlMaxLengthEntityContext method
  8220. *
  8221. * Test control() with maxlength attribute in Entity Context.
  8222. */
  8223. public function testControlMaxLengthEntityContext(): void
  8224. {
  8225. $this->article['schema']['title']['length'] = 45;
  8226. $validator = new Validator();
  8227. $validator->maxLength('title', 10);
  8228. $article = new EntityContext(
  8229. [
  8230. 'entity' => new Entity($this->article),
  8231. 'table' => new Table([
  8232. 'alias' => 'Articles',
  8233. 'schema' => $this->article['schema'],
  8234. 'validator' => $validator,
  8235. ]),
  8236. ]
  8237. );
  8238. $this->Form->create($article);
  8239. $result = $this->Form->control('title');
  8240. $expected = [
  8241. 'div' => ['class'],
  8242. 'label' => ['for'],
  8243. 'Title',
  8244. '/label',
  8245. 'input' => [
  8246. 'id',
  8247. 'name' => 'title',
  8248. 'type' => 'text',
  8249. 'required' => 'required',
  8250. 'maxlength' => 10,
  8251. 'data-validity-message' => 'This field cannot be left empty',
  8252. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8253. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8254. ],
  8255. '/div',
  8256. ];
  8257. $this->assertHtml($expected, $result);
  8258. $this->article['schema']['title']['length'] = 45;
  8259. $validator = new Validator();
  8260. $validator->maxLength('title', 55);
  8261. $article = new EntityContext(
  8262. [
  8263. 'entity' => new Entity($this->article),
  8264. 'table' => new Table([
  8265. 'schema' => $this->article['schema'],
  8266. 'validator' => $validator,
  8267. 'alias' => 'Articles',
  8268. ]),
  8269. ]
  8270. );
  8271. $this->Form->create($article);
  8272. $result = $this->Form->control('title');
  8273. $expected = [
  8274. 'div' => ['class'],
  8275. 'label' => ['for'],
  8276. 'Title',
  8277. '/label',
  8278. 'input' => [
  8279. 'id',
  8280. 'name' => 'title',
  8281. 'type' => 'text',
  8282. 'required' => 'required',
  8283. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8284. 'data-validity-message' => 'This field cannot be left empty',
  8285. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8286. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8287. ],
  8288. '/div',
  8289. ];
  8290. $this->assertHtml($expected, $result);
  8291. $this->article['schema']['title']['length'] = 45;
  8292. $validator = new Validator();
  8293. $validator->maxLength('title', 55);
  8294. $article = new EntityContext(
  8295. [
  8296. 'entity' => new Entity($this->article),
  8297. 'table' => new Table([
  8298. 'schema' => $this->article['schema'],
  8299. 'validator' => $validator,
  8300. 'alias' => 'Articles',
  8301. ]),
  8302. ]
  8303. );
  8304. $this->Form->create($article);
  8305. $result = $this->Form->control('title', ['maxlength' => 10]);
  8306. $expected = [
  8307. 'div' => ['class'],
  8308. 'label' => ['for'],
  8309. 'Title',
  8310. '/label',
  8311. 'input' => [
  8312. 'id',
  8313. 'name' => 'title',
  8314. 'type' => 'text',
  8315. 'required' => 'required',
  8316. 'maxlength' => 10, // Length set in options should take highest precedence.
  8317. 'data-validity-message' => 'This field cannot be left empty',
  8318. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8319. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8320. ],
  8321. '/div',
  8322. ];
  8323. $this->assertHtml($expected, $result);
  8324. }
  8325. /**
  8326. * testControlMinMaxLengthEntityContext method
  8327. *
  8328. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8329. */
  8330. public function testControlMinMaxLengthEntityContext(): void
  8331. {
  8332. $validator = new Validator();
  8333. $validator->maxLength('title', 10);
  8334. $article = new EntityContext(
  8335. [
  8336. 'entity' => new Entity($this->article),
  8337. 'table' => new Table([
  8338. 'alias' => 'Articles',
  8339. 'schema' => $this->article['schema'],
  8340. 'validator' => $validator,
  8341. ]),
  8342. ]
  8343. );
  8344. $this->Form->create($article);
  8345. $result = $this->Form->control('title');
  8346. $expected = [
  8347. 'div' => ['class'],
  8348. 'label' => ['for'],
  8349. 'Title',
  8350. '/label',
  8351. 'input' => [
  8352. 'id',
  8353. 'name' => 'title',
  8354. 'type' => 'text',
  8355. 'required' => 'required',
  8356. 'maxlength' => 10,
  8357. 'data-validity-message' => 'This field cannot be left empty',
  8358. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8359. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8360. ],
  8361. '/div',
  8362. ];
  8363. $this->assertHtml($expected, $result);
  8364. }
  8365. /**
  8366. * testControlMaxLengthFormContext method
  8367. *
  8368. * Test control() with maxlength attribute in Form Context.
  8369. */
  8370. public function testControlMaxLengthFormContext(): void
  8371. {
  8372. $validator = new Validator();
  8373. $validator->maxLength('title', 10);
  8374. $form = new Form();
  8375. $form->setValidator('default', $validator);
  8376. $this->Form->create($form);
  8377. $result = $this->Form->control('title');
  8378. $expected = [
  8379. 'div' => ['class'],
  8380. 'label' => ['for'],
  8381. 'Title',
  8382. '/label',
  8383. 'input' => [
  8384. 'id',
  8385. 'name' => 'title',
  8386. 'type' => 'text',
  8387. 'required' => 'required',
  8388. 'maxlength' => 10,
  8389. 'data-validity-message' => 'This field cannot be left empty',
  8390. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8391. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8392. ],
  8393. '/div',
  8394. ];
  8395. $this->assertHtml($expected, $result);
  8396. }
  8397. }