FormHelperTest.php 310 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042
  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\Table\ContactsTable;
  43. use TestApp\Model\Table\ValidateUsersTable;
  44. use TestApp\View\Form\StubContext;
  45. /**
  46. * FormHelperTest class
  47. *
  48. * @property \Cake\View\Helper\FormHelper $Form
  49. * @property \Cake\View\View $View
  50. */
  51. class FormHelperTest extends TestCase
  52. {
  53. /**
  54. * Fixtures to be used
  55. *
  56. * @var array<string>
  57. */
  58. protected array $fixtures = ['core.Articles', 'core.Comments'];
  59. /**
  60. * @var array
  61. */
  62. protected $article = [];
  63. /**
  64. * @var string
  65. */
  66. protected $url;
  67. /**
  68. * @var \Cake\View\Helper\FormHelper
  69. */
  70. protected $Form;
  71. /**
  72. * @var \Cake\View\View
  73. */
  74. protected $View;
  75. /**
  76. * setUp method
  77. */
  78. public function setUp(): void
  79. {
  80. parent::setUp();
  81. Configure::write('Config.language', 'eng');
  82. Configure::write('App.base', '');
  83. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  84. $request = new ServerRequest([
  85. 'webroot' => '',
  86. 'base' => '',
  87. 'url' => '/articles/add',
  88. 'params' => [
  89. 'controller' => 'Articles',
  90. 'action' => 'add',
  91. 'plugin' => null,
  92. ],
  93. ]);
  94. $this->View = new View($request);
  95. Router::reload();
  96. Router::setRequest($request);
  97. $this->url = '/articles/add';
  98. $this->Form = new FormHelper($this->View);
  99. $this->article = [
  100. 'schema' => [
  101. 'id' => ['type' => 'integer'],
  102. 'author_id' => ['type' => 'integer', 'null' => true],
  103. 'title' => ['type' => 'string', 'null' => true],
  104. 'body' => 'text',
  105. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  106. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  107. ],
  108. 'required' => [
  109. 'author_id' => true,
  110. 'title' => true,
  111. ],
  112. ];
  113. Security::setSalt('foo!');
  114. $builder = Router::createRouteBuilder('/');
  115. $builder->connect('/{controller}', ['action' => 'index']);
  116. $builder->connect('/{controller}/{action}/*');
  117. }
  118. /**
  119. * tearDown method
  120. */
  121. public function tearDown(): void
  122. {
  123. parent::tearDown();
  124. unset($this->Form, $this->View);
  125. }
  126. /**
  127. * Test construct() with the templates option.
  128. */
  129. public function testConstructTemplatesFile(): void
  130. {
  131. $helper = new FormHelper($this->View, [
  132. 'templates' => 'htmlhelper_tags',
  133. ]);
  134. $result = $helper->control('name');
  135. $this->assertStringContainsString('<input', $result);
  136. }
  137. /**
  138. * Test that when specifying custom widgets the config array for that widget
  139. * is overwritten instead of merged.
  140. */
  141. public function testConstructWithWidgets(): void
  142. {
  143. $config = [
  144. 'widgets' => [
  145. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  146. ],
  147. ];
  148. $helper = new FormHelper($this->View, $config);
  149. $locator = $helper->getWidgetLocator();
  150. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  151. }
  152. /**
  153. * Test that when specifying custom widgets config file and it should be
  154. * added to widgets array. WidgetLocator will load widgets in constructor.
  155. */
  156. public function testConstructWithWidgetsConfig(): void
  157. {
  158. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  159. $locator = $helper->getWidgetLocator();
  160. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  161. }
  162. /**
  163. * Test setting the widget locator
  164. */
  165. public function testSetAndGetWidgetLocator(): void
  166. {
  167. $helper = new FormHelper($this->View);
  168. $locator = new WidgetLocator($helper->templater(), $this->View);
  169. $helper->setWidgetLocator($locator);
  170. $this->assertSame($locator, $helper->getWidgetLocator());
  171. }
  172. /**
  173. * Test overridding grouped input types which controls generation of "for"
  174. * attribute of labels.
  175. */
  176. public function testConstructWithGroupedInputTypes(): void
  177. {
  178. $helper = new FormHelper($this->View, [
  179. 'groupedInputTypes' => ['radio'],
  180. ]);
  181. $result = $helper->control('when', ['type' => 'datetime-local']);
  182. $this->assertStringContainsString('<label for="when">When</label>', $result);
  183. }
  184. /**
  185. * Test registering a new widget class and rendering it.
  186. */
  187. public function testAddWidgetAndRenderWidget(): void
  188. {
  189. $data = [
  190. 'val' => 1,
  191. ];
  192. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  193. $this->Form->addWidget('test', $mock);
  194. $mock->expects($this->once())
  195. ->method('render')
  196. ->with($data)
  197. ->willReturn('HTML');
  198. $result = $this->Form->widget('test', $data);
  199. $this->assertSame('HTML', $result);
  200. }
  201. /**
  202. * Test that secureFields() of widget is called after calling render(),
  203. * not before.
  204. */
  205. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  206. {
  207. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  208. 'unlockedFields' => [],
  209. ]));
  210. $data = [
  211. 'val' => 1,
  212. 'name' => 'test',
  213. ];
  214. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  215. $this->Form->addWidget('test', $mock);
  216. $mock->expects($this->once())
  217. ->method('render')
  218. ->with($data)
  219. ->willReturn('HTML');
  220. $mock->expects($this->once())
  221. ->method('secureFields')
  222. ->with($data)
  223. ->willReturn(['test']);
  224. $this->Form->create();
  225. $result = $this->Form->widget('test', $data + ['secure' => true]);
  226. $this->assertSame('HTML', $result);
  227. }
  228. /**
  229. * Test that empty string is not added to secure fields list when
  230. * rendering input widget without name.
  231. */
  232. public function testRenderingWidgetWithEmptyName(): void
  233. {
  234. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  235. $this->Form->create();
  236. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  237. $this->assertSame('<select name=""></select>', $result);
  238. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  239. $this->assertEquals([], $result);
  240. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  241. $this->assertSame('<select name="0"></select>', $result);
  242. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  243. $this->assertEquals(['0'], $result);
  244. }
  245. /**
  246. * Test adding a new context class.
  247. */
  248. public function testAddContextProvider(): void
  249. {
  250. $context = 'My data';
  251. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  252. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  253. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  254. $this->assertSame($context, $data['entity']);
  255. return $stub;
  256. });
  257. $this->Form->create($context);
  258. $result = $this->Form->context();
  259. $this->assertSame($stub, $result);
  260. }
  261. /**
  262. * Test replacing a context class.
  263. */
  264. public function testAddContextProviderReplace(): void
  265. {
  266. $entity = new Article();
  267. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  268. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  269. return $stub;
  270. });
  271. $this->Form->create($entity);
  272. $result = $this->Form->context();
  273. $this->assertSame($stub, $result);
  274. }
  275. /**
  276. * Test overriding a context class.
  277. */
  278. public function testAddContextProviderAdd(): void
  279. {
  280. $entity = new Article();
  281. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  282. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  283. if ($data['entity'] instanceof Entity) {
  284. return $stub;
  285. }
  286. });
  287. $this->Form->create($entity);
  288. $result = $this->Form->context();
  289. $this->assertSame($stub, $result);
  290. }
  291. /**
  292. * Provides context options for create().
  293. *
  294. * @return array
  295. */
  296. public static function contextSelectionProvider(): array
  297. {
  298. $entity = new Article();
  299. $collection = new Collection([$entity]);
  300. $emptyCollection = new Collection([]);
  301. $data = [
  302. 'schema' => [
  303. 'title' => ['type' => 'string'],
  304. ],
  305. ];
  306. $form = new Form();
  307. $custom = new StubContext();
  308. return [
  309. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  310. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  311. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  312. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  313. 'form' => [$form, 'Cake\View\Form\FormContext'],
  314. 'none' => [null, 'Cake\View\Form\NullContext'],
  315. 'custom' => [$custom, $custom::class],
  316. ];
  317. }
  318. /**
  319. * Test default context selection in create()
  320. *
  321. * @dataProvider contextSelectionProvider
  322. * @param mixed $data
  323. */
  324. public function testCreateContextSelectionBuiltIn($data, string $class): void
  325. {
  326. $this->Form->create($data);
  327. $this->assertInstanceOf($class, $this->Form->context());
  328. }
  329. /**
  330. * Data provider for type option.
  331. *
  332. * @return array
  333. */
  334. public static function requestTypeProvider(): array
  335. {
  336. return [
  337. // type, method, override
  338. ['post', 'post', 'POST'],
  339. ['put', 'post', 'PUT'],
  340. ['patch', 'post', 'PATCH'],
  341. ['delete', 'post', 'DELETE'],
  342. ];
  343. }
  344. /**
  345. * Test creating file forms.
  346. */
  347. public function testCreateFile(): void
  348. {
  349. $encoding = strtolower(Configure::read('App.encoding'));
  350. $result = $this->Form->create(null, ['type' => 'file']);
  351. $expected = [
  352. 'form' => [
  353. 'method' => 'post', 'action' => '/articles/add',
  354. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  355. ],
  356. 'div' => ['style' => 'display:none;'],
  357. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  358. '/div',
  359. ];
  360. $this->assertHtml($expected, $result);
  361. }
  362. /**
  363. * Test creating GET forms.
  364. */
  365. public function testCreateGet(): void
  366. {
  367. $encoding = strtolower(Configure::read('App.encoding'));
  368. $result = $this->Form->create(null, ['type' => 'get']);
  369. $expected = ['form' => [
  370. 'method' => 'get', 'action' => '/articles/add',
  371. 'accept-charset' => $encoding,
  372. ]];
  373. $this->assertHtml($expected, $result);
  374. }
  375. /**
  376. * Test explicit method/enctype options.
  377. *
  378. * Explicit method overwrites inferred method from 'type'
  379. */
  380. public function testCreateExplicitMethodEnctype(): void
  381. {
  382. $encoding = strtolower(Configure::read('App.encoding'));
  383. $result = $this->Form->create(null, [
  384. 'type' => 'get',
  385. 'method' => 'put',
  386. 'enctype' => 'multipart/form-data',
  387. ]);
  388. $expected = ['form' => [
  389. 'method' => 'put',
  390. 'action' => '/articles/add',
  391. 'enctype' => 'multipart/form-data',
  392. 'accept-charset' => $encoding,
  393. ]];
  394. $this->assertHtml($expected, $result);
  395. }
  396. /**
  397. * Test create() with the templates option.
  398. */
  399. public function testCreateTemplatesArray(): void
  400. {
  401. $result = $this->Form->create($this->article, [
  402. 'templates' => [
  403. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  404. ],
  405. ]);
  406. $expected = [
  407. 'form' => [
  408. 'class' => 'form-horizontal',
  409. 'method' => 'post',
  410. 'action' => '/articles/add',
  411. 'accept-charset' => 'utf-8',
  412. ],
  413. ];
  414. $this->assertHtml($expected, $result);
  415. }
  416. /**
  417. * Test create() with the templates option.
  418. */
  419. public function testCreateTemplatesFile(): void
  420. {
  421. $result = $this->Form->create($this->article, [
  422. 'templates' => 'htmlhelper_tags',
  423. ]);
  424. $expected = [
  425. 'start form',
  426. ];
  427. $this->assertHtml($expected, $result);
  428. }
  429. /**
  430. * Test that create() and end() restore templates.
  431. */
  432. public function testCreateEndRestoreTemplates(): void
  433. {
  434. $this->Form->create($this->article, [
  435. 'templates' => ['input' => 'custom input element'],
  436. ]);
  437. $this->Form->end();
  438. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  439. }
  440. /**
  441. * Test create() with the templates option.
  442. */
  443. public function testCreateTemplatesRequiredClass(): void
  444. {
  445. $this->Form->create($this->article, [
  446. 'templates' => [
  447. 'requiredClass' => 'is-required',
  448. ],
  449. ]);
  450. $result = $this->Form->control('title');
  451. $expected = [
  452. 'div' => ['class' => 'input text is-required'],
  453. 'label' => ['for' => 'title'],
  454. 'Title',
  455. '/label',
  456. 'input' => [
  457. 'type' => 'text',
  458. 'name' => 'title',
  459. 'id' => 'title',
  460. 'required' => 'required',
  461. 'data-validity-message' => 'This field cannot be left empty',
  462. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  463. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  464. ],
  465. '/div',
  466. ];
  467. $this->assertHtml($expected, $result);
  468. }
  469. /**
  470. * Test using template vars in various templates used by control() method.
  471. */
  472. public function testControlTemplateVars(): void
  473. {
  474. $result = $this->Form->control('text', [
  475. 'templates' => [
  476. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  477. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  478. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  479. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  480. ],
  481. 'templateVars' => [
  482. 'forinput' => 'in-input',
  483. 'forlabel' => 'in-label',
  484. 'forgroup' => 'in-group',
  485. 'forcontainer' => 'in-container',
  486. ],
  487. ]);
  488. $expected = [
  489. 'div' => ['class'],
  490. 'label' => ['for'],
  491. 'Text in-label',
  492. '/label',
  493. 'in-group',
  494. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  495. 'in-container',
  496. '/div',
  497. ];
  498. $this->assertHtml($expected, $result);
  499. }
  500. /**
  501. * Test ensuring template variables work in template files loaded
  502. * during control().
  503. */
  504. public function testControlTemplatesFromFile(): void
  505. {
  506. $result = $this->Form->control('title', [
  507. 'templates' => 'test_templates',
  508. 'templateVars' => [
  509. 'forcontainer' => 'container-data',
  510. ],
  511. ]);
  512. $expected = [
  513. 'div' => ['class'],
  514. 'label' => ['for'],
  515. 'Title',
  516. '/label',
  517. 'input' => ['name', 'type' => 'text', 'id'],
  518. 'container-data',
  519. '/div',
  520. ];
  521. $this->assertHtml($expected, $result);
  522. }
  523. /**
  524. * Test using template vars in inputSubmit and submitContainer template.
  525. */
  526. public function testSubmitTemplateVars(): void
  527. {
  528. $this->Form->setTemplates([
  529. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  530. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  531. ]);
  532. $result = $this->Form->submit('Submit', [
  533. 'templateVars' => [
  534. 'forinput' => 'in-input',
  535. 'forcontainer' => 'in-container',
  536. ],
  537. ]);
  538. $expected = [
  539. 'div' => ['class'],
  540. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  541. 'in-container',
  542. '/div',
  543. ];
  544. $this->assertHtml($expected, $result);
  545. }
  546. /**
  547. * test the create() method
  548. *
  549. * @dataProvider requestTypeProvider
  550. */
  551. public function testCreateTypeOptions(string $type, string $method, string $override): void
  552. {
  553. $encoding = strtolower(Configure::read('App.encoding'));
  554. $result = $this->Form->create(null, ['type' => $type]);
  555. $expected = [
  556. 'form' => [
  557. 'method' => $method, 'action' => '/articles/add',
  558. 'accept-charset' => $encoding,
  559. ],
  560. ];
  561. $extra = [
  562. 'div' => ['style' => 'display:none;'],
  563. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  564. '/div',
  565. ];
  566. if ($type !== 'post') {
  567. $expected = array_merge($expected, $extra);
  568. }
  569. $this->assertHtml($expected, $result);
  570. }
  571. /**
  572. * Test using template vars in Create (formStart template)
  573. */
  574. public function testCreateTemplateVars(): void
  575. {
  576. $result = $this->Form->create($this->article, [
  577. 'templates' => [
  578. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  579. ],
  580. 'templateVars' => ['header' => 'headertext'],
  581. ]);
  582. $expected = [
  583. 'h4' => ['class'],
  584. 'headertext',
  585. '/h4',
  586. 'form' => [
  587. 'method' => 'post',
  588. 'action' => '/articles/add',
  589. 'accept-charset' => 'utf-8',
  590. ],
  591. ];
  592. $this->assertHtml($expected, $result);
  593. }
  594. /**
  595. * Test opening a form for an update operation.
  596. */
  597. public function testCreateUpdateForm(): void
  598. {
  599. $encoding = strtolower(Configure::read('App.encoding'));
  600. $this->View->setRequest($this->View->getRequest()
  601. ->withRequestTarget('/articles/edit/1')
  602. ->withParam('action', 'edit'));
  603. $this->article['defaults']['id'] = 1;
  604. $result = $this->Form->create($this->article);
  605. $expected = [
  606. 'form' => [
  607. 'method' => 'post',
  608. 'action' => '/articles/edit/1',
  609. 'accept-charset' => $encoding,
  610. ],
  611. 'div' => ['style' => 'display:none;'],
  612. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  613. '/div',
  614. ];
  615. $this->assertHtml($expected, $result);
  616. }
  617. /**
  618. * test create() with automatic url generation
  619. */
  620. public function testCreateAutoUrl(): void
  621. {
  622. $encoding = strtolower(Configure::read('App.encoding'));
  623. $this->View->setRequest($this->View->getRequest()
  624. ->withRequestTarget('/articles/delete/10')
  625. ->withParam('action', 'delete'));
  626. $result = $this->Form->create($this->article);
  627. $expected = [
  628. 'form' => [
  629. 'method' => 'post', 'action' => '/articles/delete/10',
  630. 'accept-charset' => $encoding,
  631. ],
  632. ];
  633. $this->assertHtml($expected, $result);
  634. $this->article['defaults'] = ['id' => 1];
  635. $this->View->setRequest($this->View->getRequest()
  636. ->withRequestTarget('/Articles/edit/1')
  637. ->withParam('action', 'delete'));
  638. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  639. $expected = [
  640. 'form' => [
  641. 'method' => 'post',
  642. 'action' => '/Articles/edit/1',
  643. 'accept-charset' => $encoding,
  644. ],
  645. 'div' => ['style' => 'display:none;'],
  646. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  647. '/div',
  648. ];
  649. $this->assertHtml($expected, $result);
  650. $this->View->setRequest($this->View->getRequest()
  651. ->withParam('action', 'add'));
  652. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  653. $expected = [
  654. 'form' => [
  655. 'method' => 'post',
  656. 'action' => '/Articles/publish/1',
  657. 'accept-charset' => $encoding,
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  661. '/div',
  662. ];
  663. $this->assertHtml($expected, $result);
  664. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  665. $expected = [
  666. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  667. 'div' => ['style' => 'display:none;'],
  668. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  669. '/div',
  670. ];
  671. $this->assertHtml($expected, $result);
  672. $this->View->setRequest($this->View->getRequest()
  673. ->withParam('controller', 'Pages'));
  674. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  675. $expected = [
  676. 'form' => [
  677. 'method' => 'post', 'action' => '/Pages/signup/1',
  678. 'accept-charset' => $encoding,
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div',
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * Test create() with no URL (no "action" attribute for <form> tag)
  688. */
  689. public function testCreateNoUrl(): void
  690. {
  691. $result = $this->Form->create(null, ['url' => false]);
  692. $expected = [
  693. 'form' => [
  694. 'method' => 'post',
  695. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  696. ],
  697. ];
  698. $this->assertHtml($expected, $result);
  699. }
  700. /**
  701. * test create() with a custom route
  702. */
  703. public function testCreateCustomRoute(): void
  704. {
  705. $builder = Router::createRouteBuilder('/');
  706. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  707. $encoding = strtolower(Configure::read('App.encoding'));
  708. $this->View->setRequest($this->View->getRequest()
  709. ->withParam('controller', 'Users'));
  710. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post', 'action' => '/login',
  714. 'accept-charset' => $encoding,
  715. ],
  716. ];
  717. $this->assertHtml($expected, $result);
  718. $builder->connect(
  719. '/new-article',
  720. ['controller' => 'Articles', 'action' => 'myAction'],
  721. ['_name' => 'my-route']
  722. );
  723. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post', 'action' => '/new-article',
  727. 'accept-charset' => $encoding,
  728. ],
  729. ];
  730. $this->assertHtml($expected, $result);
  731. }
  732. /**
  733. * test automatic accept-charset overriding
  734. */
  735. public function testCreateWithAcceptCharset(): void
  736. {
  737. $result = $this->Form->create(
  738. $this->article,
  739. [
  740. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  741. ]
  742. );
  743. $expected = [
  744. 'form' => [
  745. 'method' => 'post', 'action' => '/Articles',
  746. 'accept-charset' => 'iso-8859-1',
  747. ],
  748. ];
  749. $this->assertHtml($expected, $result);
  750. }
  751. /**
  752. * Test base form URL when 'url' param is passed with multiple parameters (&)
  753. */
  754. public function testCreateQueryStringRequest(): void
  755. {
  756. $encoding = strtolower(Configure::read('App.encoding'));
  757. $result = $this->Form->create($this->article, [
  758. 'type' => 'post',
  759. 'escape' => false,
  760. 'url' => [
  761. 'controller' => 'Controller',
  762. 'action' => 'action',
  763. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  764. ],
  765. ]);
  766. $expected = [
  767. 'form' => [
  768. 'method' => 'post',
  769. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  770. 'accept-charset' => $encoding,
  771. ],
  772. ];
  773. $this->assertHtml($expected, $result);
  774. $result = $this->Form->create($this->article, [
  775. 'type' => 'post',
  776. 'url' => [
  777. 'controller' => 'Controller',
  778. 'action' => 'action',
  779. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  780. ],
  781. ]);
  782. $this->assertHtml($expected, $result);
  783. }
  784. /**
  785. * test that create() doesn't cause errors by multiple id's being in the primary key
  786. * as could happen with multiple select or checkboxes.
  787. */
  788. public function testCreateWithMultipleIdInData(): void
  789. {
  790. $encoding = strtolower(Configure::read('App.encoding'));
  791. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  792. $result = $this->Form->create($this->article);
  793. $expected = [
  794. 'form' => [
  795. 'method' => 'post',
  796. 'action' => '/articles/add',
  797. 'accept-charset' => $encoding,
  798. ],
  799. ];
  800. $this->assertHtml($expected, $result);
  801. }
  802. /**
  803. * test that create() doesn't add in extra passed params.
  804. */
  805. public function testCreatePassedArgs(): void
  806. {
  807. $encoding = strtolower(Configure::read('App.encoding'));
  808. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  809. $result = $this->Form->create($this->article, [
  810. 'type' => 'post',
  811. 'escape' => false,
  812. 'url' => [
  813. 'action' => 'edit',
  814. 'myparam',
  815. ],
  816. ]);
  817. $expected = [
  818. 'form' => [
  819. 'method' => 'post',
  820. 'action' => '/Articles/edit/myparam',
  821. 'accept-charset' => $encoding,
  822. ],
  823. ];
  824. $this->assertHtml($expected, $result);
  825. }
  826. /**
  827. * test creating a get form, and get form inputs.
  828. */
  829. public function testGetFormCreate(): void
  830. {
  831. $encoding = strtolower(Configure::read('App.encoding'));
  832. $result = $this->Form->create($this->article, ['type' => 'get']);
  833. $expected = ['form' => [
  834. 'method' => 'get', 'action' => '/articles/add',
  835. 'accept-charset' => $encoding,
  836. ]];
  837. $this->assertHtml($expected, $result);
  838. $result = $this->Form->text('title');
  839. $expected = ['input' => [
  840. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  841. ]];
  842. $this->assertHtml($expected, $result);
  843. $result = $this->Form->password('password');
  844. $expected = ['input' => [
  845. 'name' => 'password', 'type' => 'password',
  846. ]];
  847. $this->assertHtml($expected, $result);
  848. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  849. $result = $this->Form->text('user_form');
  850. $expected = ['input' => [
  851. 'name' => 'user_form', 'type' => 'text',
  852. ]];
  853. $this->assertHtml($expected, $result);
  854. }
  855. /**
  856. * test get form, and inputs when the model param is false
  857. */
  858. public function testGetFormWithFalseModel(): void
  859. {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  862. $result = $this->Form->create(null, [
  863. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  864. ]);
  865. $expected = ['form' => [
  866. 'method' => 'get', 'action' => '/ContactTest/add',
  867. 'accept-charset' => $encoding,
  868. ]];
  869. $this->assertHtml($expected, $result);
  870. $result = $this->Form->text('reason');
  871. $expected = [
  872. 'input' => ['type' => 'text', 'name' => 'reason'],
  873. ];
  874. $this->assertHtml($expected, $result);
  875. }
  876. /**
  877. * testFormCreateWithSecurity method
  878. *
  879. * Test form->create() with security key.
  880. */
  881. public function testCreateWithSecurity(): void
  882. {
  883. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  884. $encoding = strtolower(Configure::read('App.encoding'));
  885. $result = $this->Form->create($this->article, [
  886. 'url' => '/articles/publish',
  887. ]);
  888. $expected = [
  889. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  890. 'div' => ['style' => 'display:none;'],
  891. ['input' => [
  892. 'type' => 'hidden',
  893. 'name' => '_csrfToken',
  894. 'value' => 'testKey',
  895. ]],
  896. '/div',
  897. ];
  898. $this->assertHtml($expected, $result);
  899. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  900. $expected['form']['id'] = 'MyForm';
  901. $this->assertHtml($expected, $result);
  902. }
  903. /**
  904. * testFormCreateGetNoSecurity method
  905. *
  906. * Test form->create() with no security key as its a get form
  907. */
  908. public function testCreateEndGetNoSecurity(): void
  909. {
  910. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  911. $article = new Article();
  912. $result = $this->Form->create($article, [
  913. 'type' => 'get',
  914. 'url' => '/contacts/add',
  915. ]);
  916. $this->assertStringNotContainsString('testKey', $result);
  917. $result = $this->Form->end();
  918. $this->assertStringNotContainsString('testKey', $result);
  919. }
  920. /**
  921. * Tests form hash generation with model-less data
  922. */
  923. public function testValidateHashNoModel(): void
  924. {
  925. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  926. $fields = ['anything'];
  927. $this->Form->create();
  928. $result = $this->Form->secure($fields);
  929. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  930. $this->assertStringContainsString($hash, $result);
  931. }
  932. /**
  933. * Tests that hidden fields generated for checkboxes don't get locked
  934. */
  935. public function testNoCheckboxLocking(): void
  936. {
  937. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  938. $this->Form->create();
  939. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  940. $this->Form->checkbox('check', ['value' => '1']);
  941. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  942. }
  943. /**
  944. * testFormSecurityFields method
  945. *
  946. * Test generation of secure form hash generation.
  947. */
  948. public function testFormSecurityFields(): void
  949. {
  950. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  951. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  952. $this->Form->create();
  953. $result = $this->Form->secure($fields);
  954. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  955. $hash .= ':' . 'Model.valid';
  956. $hash = urlencode($hash);
  957. $tokenDebug = urlencode(json_encode([
  958. $this->url,
  959. $fields,
  960. [],
  961. ]));
  962. $expected = [
  963. 'div' => ['style' => 'display:none;'],
  964. ['input' => [
  965. 'type' => 'hidden',
  966. 'name' => '_Token[fields]',
  967. 'value' => $hash,
  968. ]],
  969. ['input' => [
  970. 'type' => 'hidden',
  971. 'name' => '_Token[unlocked]',
  972. 'value' => '',
  973. ]],
  974. ['input' => [
  975. 'type' => 'hidden',
  976. 'name' => '_Token[debug]',
  977. 'value' => $tokenDebug,
  978. ]],
  979. '/div',
  980. ];
  981. $this->assertHtml($expected, $result);
  982. }
  983. /**
  984. * testFormSecurityFields method
  985. *
  986. * Test debug token is not generated if debug is false
  987. */
  988. public function testFormSecurityFieldsNoDebugMode(): void
  989. {
  990. Configure::write('debug', false);
  991. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  992. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  993. $this->Form->create();
  994. $result = $this->Form->secure($fields);
  995. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  996. $hash .= ':' . 'Model.valid';
  997. $hash = urlencode($hash);
  998. $expected = [
  999. 'div' => ['style' => 'display:none;'],
  1000. ['input' => [
  1001. 'type' => 'hidden',
  1002. 'name' => '_Token[fields]',
  1003. 'value' => $hash,
  1004. ]],
  1005. ['input' => [
  1006. 'type' => 'hidden',
  1007. 'name' => '_Token[unlocked]',
  1008. 'value' => '',
  1009. ]],
  1010. '/div',
  1011. ];
  1012. $this->assertHtml($expected, $result);
  1013. }
  1014. /**
  1015. * Tests correct generation of number fields for smallint
  1016. */
  1017. public function testTextFieldGenerationForSmallint(): void
  1018. {
  1019. $this->article['schema'] = [
  1020. 'foo' => [
  1021. 'type' => 'smallinteger',
  1022. 'null' => false,
  1023. 'default' => null,
  1024. 'length' => 10,
  1025. ],
  1026. ];
  1027. $this->Form->create($this->article);
  1028. $result = $this->Form->control('foo');
  1029. $this->assertStringContainsString('class="input number"', $result);
  1030. $this->assertStringContainsString('type="number"', $result);
  1031. }
  1032. /**
  1033. * Tests correct generation of number fields for tinyint
  1034. */
  1035. public function testTextFieldGenerationForTinyint(): void
  1036. {
  1037. $this->article['schema'] = [
  1038. 'foo' => [
  1039. 'type' => 'tinyinteger',
  1040. 'null' => false,
  1041. 'default' => null,
  1042. 'length' => 10,
  1043. ],
  1044. ];
  1045. $this->Form->create($this->article);
  1046. $result = $this->Form->control('foo');
  1047. $this->assertStringContainsString('class="input number"', $result);
  1048. $this->assertStringContainsString('type="number"', $result);
  1049. }
  1050. /**
  1051. * Tests correct generation of number fields for double and float fields
  1052. */
  1053. public function testTextFieldGenerationForFloats(): void
  1054. {
  1055. $this->article['schema'] = [
  1056. 'foo' => [
  1057. 'type' => 'float',
  1058. 'null' => false,
  1059. 'default' => null,
  1060. 'length' => 10,
  1061. ],
  1062. ];
  1063. $this->Form->create($this->article);
  1064. $result = $this->Form->control('foo');
  1065. $expected = [
  1066. 'div' => ['class' => 'input number'],
  1067. 'label' => ['for' => 'foo'],
  1068. 'Foo',
  1069. '/label',
  1070. ['input' => [
  1071. 'type' => 'number',
  1072. 'name' => 'foo',
  1073. 'id' => 'foo',
  1074. 'step' => 'any',
  1075. ]],
  1076. '/div',
  1077. ];
  1078. $this->assertHtml($expected, $result);
  1079. $result = $this->Form->control('foo', ['step' => 0.5]);
  1080. $expected = [
  1081. 'div' => ['class' => 'input number'],
  1082. 'label' => ['for' => 'foo'],
  1083. 'Foo',
  1084. '/label',
  1085. ['input' => [
  1086. 'type' => 'number',
  1087. 'name' => 'foo',
  1088. 'id' => 'foo',
  1089. 'step' => '0.5',
  1090. ]],
  1091. '/div',
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * Tests correct generation of number fields for integer fields
  1097. */
  1098. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1099. {
  1100. $this->getTableLocator()->get('Contacts', [
  1101. 'className' => ContactsTable::class,
  1102. ]);
  1103. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1104. $result = $this->Form->control('age');
  1105. $expected = [
  1106. 'div' => ['class' => 'input number'],
  1107. 'label' => ['for' => 'age'],
  1108. 'Age',
  1109. '/label',
  1110. ['input' => [
  1111. 'type' => 'number', 'name' => 'age',
  1112. 'id' => 'age',
  1113. ]],
  1114. '/div',
  1115. ];
  1116. $this->assertHtml($expected, $result);
  1117. }
  1118. /**
  1119. * Tests correct generation of file upload fields for binary fields
  1120. */
  1121. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1122. {
  1123. $table = $this->getTableLocator()->get('Contacts', [
  1124. 'className' => ContactsTable::class,
  1125. ]);
  1126. $table->setSchema(['foo' => [
  1127. 'type' => 'binary',
  1128. 'null' => false,
  1129. 'default' => null,
  1130. 'length' => 1024,
  1131. ]]);
  1132. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1133. $result = $this->Form->control('foo');
  1134. $expected = [
  1135. 'div' => ['class' => 'input file'],
  1136. 'label' => ['for' => 'foo'],
  1137. 'Foo',
  1138. '/label',
  1139. ['input' => [
  1140. 'type' => 'file', 'name' => 'foo',
  1141. 'id' => 'foo',
  1142. ]],
  1143. '/div',
  1144. ];
  1145. $this->assertHtml($expected, $result);
  1146. }
  1147. /**
  1148. * testFormSecurityMultipleFields method
  1149. *
  1150. * Test secure() with multiple row form. Ensure hash is correct.
  1151. */
  1152. public function testFormSecurityMultipleFields(): void
  1153. {
  1154. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1155. $this->Form->create();
  1156. $fields = [
  1157. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1158. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1159. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1160. ];
  1161. $result = $this->Form->secure($fields);
  1162. $sortedFields = [
  1163. 'Model.0.password',
  1164. 'Model.0.username',
  1165. 'Model.1.password',
  1166. 'Model.1.username',
  1167. 'Model.0.hidden' => 'value',
  1168. 'Model.0.valid' => '0',
  1169. 'Model.1.hidden' => 'value',
  1170. 'Model.1.valid' => '0',
  1171. ];
  1172. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1173. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1174. $hash = urlencode($hash);
  1175. $tokenDebug = urlencode(json_encode([
  1176. $this->url,
  1177. $fields,
  1178. [],
  1179. ]));
  1180. $expected = [
  1181. 'div' => ['style' => 'display:none;'],
  1182. ['input' => [
  1183. 'type' => 'hidden',
  1184. 'name' => '_Token[fields]',
  1185. 'value' => $hash,
  1186. ]],
  1187. ['input' => [
  1188. 'type' => 'hidden',
  1189. 'name' => '_Token[unlocked]',
  1190. 'value' => '',
  1191. ]],
  1192. ['input' => [
  1193. 'type' => 'hidden',
  1194. 'name' => '_Token[debug]',
  1195. 'value' => $tokenDebug,
  1196. ]],
  1197. '/div',
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. }
  1201. /**
  1202. * testFormSecurityMultipleSubmitButtons
  1203. *
  1204. * test form submit generation and ensure that _Token is only created on end()
  1205. */
  1206. public function testFormSecurityMultipleSubmitButtons(): void
  1207. {
  1208. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1209. $this->Form->create($this->article);
  1210. $this->Form->text('Address.title');
  1211. $this->Form->text('Address.first_name');
  1212. $result = $this->Form->submit('Save', ['name' => 'save']);
  1213. $expected = [
  1214. 'div' => ['class' => 'submit'],
  1215. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1216. '/div',
  1217. ];
  1218. $this->assertHtml($expected, $result);
  1219. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1220. $expected = [
  1221. 'div' => ['class' => 'submit'],
  1222. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1223. '/div',
  1224. ];
  1225. $this->assertHtml($expected, $result);
  1226. $result = $this->Form->end();
  1227. $tokenDebug = urlencode(json_encode([
  1228. '/articles/add',
  1229. [
  1230. 'Address.title',
  1231. 'Address.first_name',
  1232. ],
  1233. ['save', 'cancel'],
  1234. ]));
  1235. $expected = [
  1236. 'div' => ['style' => 'display:none;'],
  1237. ['input' => [
  1238. 'type' => 'hidden',
  1239. 'name' => '_Token[fields]',
  1240. 'value',
  1241. ]],
  1242. ['input' => [
  1243. 'type' => 'hidden',
  1244. 'name' => '_Token[unlocked]',
  1245. 'value' => 'cancel%7Csave',
  1246. ]],
  1247. ['input' => [
  1248. 'type' => 'hidden',
  1249. 'name' => '_Token[debug]',
  1250. 'value' => $tokenDebug,
  1251. ]],
  1252. '/div',
  1253. ];
  1254. $this->assertHtml($expected, $result);
  1255. }
  1256. /**
  1257. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1258. */
  1259. public function testSecurityButtonNestedNamed(): void
  1260. {
  1261. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1262. $this->Form->create();
  1263. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1264. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1265. $this->assertEquals(['Address.button'], $result);
  1266. }
  1267. /**
  1268. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1269. */
  1270. public function testSecuritySubmitNestedNamed(): void
  1271. {
  1272. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1273. $this->Form->create($this->article);
  1274. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1275. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1276. $this->assertEquals(['Address.button'], $result);
  1277. }
  1278. /**
  1279. * Test that the correct fields are unlocked for image submits with no names.
  1280. */
  1281. public function testSecuritySubmitImageNoName(): void
  1282. {
  1283. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1284. $this->Form->create();
  1285. $result = $this->Form->submit('save.png');
  1286. $expected = [
  1287. 'div' => ['class' => 'submit'],
  1288. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1289. '/div',
  1290. ];
  1291. $this->assertHtml($expected, $result);
  1292. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1293. $this->assertEquals(['x', 'y'], $result);
  1294. }
  1295. /**
  1296. * Test that the correct fields are unlocked for image submits with names.
  1297. */
  1298. public function testSecuritySubmitImageName(): void
  1299. {
  1300. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1301. $this->Form->create();
  1302. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1303. $expected = [
  1304. 'div' => ['class' => 'submit'],
  1305. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1306. '/div',
  1307. ];
  1308. $this->assertHtml($expected, $result);
  1309. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1310. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1311. }
  1312. /**
  1313. * testFormSecurityMultipleControlFields method
  1314. *
  1315. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1316. */
  1317. public function testFormSecurityMultipleControlFields(): void
  1318. {
  1319. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1320. $this->Form->create();
  1321. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1322. $this->Form->control('Addresses.0.title');
  1323. $this->Form->control('Addresses.0.first_name');
  1324. $this->Form->control('Addresses.0.last_name');
  1325. $this->Form->control('Addresses.0.address');
  1326. $this->Form->control('Addresses.0.city');
  1327. $this->Form->control('Addresses.0.phone');
  1328. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1329. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1330. $this->Form->control('Addresses.1.title');
  1331. $this->Form->control('Addresses.1.first_name');
  1332. $this->Form->control('Addresses.1.last_name');
  1333. $this->Form->control('Addresses.1.address');
  1334. $this->Form->control('Addresses.1.city');
  1335. $this->Form->control('Addresses.1.phone');
  1336. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1337. $result = $this->Form->secure();
  1338. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1339. $tokenDebug = urlencode(json_encode([
  1340. '/articles/add',
  1341. [
  1342. 'Addresses.0.id' => '123456',
  1343. 'Addresses.0.title',
  1344. 'Addresses.0.first_name',
  1345. 'Addresses.0.last_name',
  1346. 'Addresses.0.address',
  1347. 'Addresses.0.city',
  1348. 'Addresses.0.phone',
  1349. 'Addresses.0.primary',
  1350. 'Addresses.1.id' => '654321',
  1351. 'Addresses.1.title',
  1352. 'Addresses.1.first_name',
  1353. 'Addresses.1.last_name',
  1354. 'Addresses.1.address',
  1355. 'Addresses.1.city',
  1356. 'Addresses.1.phone',
  1357. 'Addresses.1.primary',
  1358. ],
  1359. [],
  1360. ]));
  1361. $expected = [
  1362. 'div' => ['style' => 'display:none;'],
  1363. ['input' => [
  1364. 'type' => 'hidden',
  1365. 'name' => '_Token[fields]',
  1366. 'value' => $hash,
  1367. ]],
  1368. ['input' => [
  1369. 'type' => 'hidden',
  1370. 'name' => '_Token[unlocked]',
  1371. 'value' => '',
  1372. ]],
  1373. ['input' => [
  1374. 'type' => 'hidden',
  1375. 'name' => '_Token[debug]',
  1376. 'value' => $tokenDebug,
  1377. ]],
  1378. '/div',
  1379. ];
  1380. $this->assertHtml($expected, $result);
  1381. }
  1382. /**
  1383. * testFormSecurityArrayFields method
  1384. *
  1385. * Test form security with Model.field.0 style inputs.
  1386. */
  1387. public function testFormSecurityArrayFields(): void
  1388. {
  1389. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1390. $this->Form->create();
  1391. $this->Form->text('Address.primary.1');
  1392. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1393. $this->assertSame('Address.primary', $result[0]);
  1394. $this->Form->text('Address.secondary.1.0');
  1395. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1396. $this->assertSame('Address.secondary', $result[1]);
  1397. }
  1398. /**
  1399. * testFormSecurityMultipleControlDisabledFields method
  1400. *
  1401. * Test secure form generation with multiple records and disabled fields.
  1402. */
  1403. public function testFormSecurityMultipleControlDisabledFields(): void
  1404. {
  1405. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1406. 'unlockedFields' => ['first_name', 'address'],
  1407. ]));
  1408. $this->Form->create();
  1409. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1410. $this->Form->text('Addresses.0.title');
  1411. $this->Form->text('Addresses.0.first_name');
  1412. $this->Form->text('Addresses.0.last_name');
  1413. $this->Form->text('Addresses.0.address');
  1414. $this->Form->text('Addresses.0.city');
  1415. $this->Form->text('Addresses.0.phone');
  1416. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1417. $this->Form->text('Addresses.1.title');
  1418. $this->Form->text('Addresses.1.first_name');
  1419. $this->Form->text('Addresses.1.last_name');
  1420. $this->Form->text('Addresses.1.address');
  1421. $this->Form->text('Addresses.1.city');
  1422. $this->Form->text('Addresses.1.phone');
  1423. $result = $this->Form->secure();
  1424. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1425. $tokenDebug = urlencode(json_encode([
  1426. '/articles/add',
  1427. [
  1428. 'Addresses.0.id' => '123456',
  1429. 'Addresses.0.title',
  1430. 'Addresses.0.last_name',
  1431. 'Addresses.0.city',
  1432. 'Addresses.0.phone',
  1433. 'Addresses.1.id' => '654321',
  1434. 'Addresses.1.title',
  1435. 'Addresses.1.last_name',
  1436. 'Addresses.1.city',
  1437. 'Addresses.1.phone',
  1438. ],
  1439. [
  1440. 'first_name',
  1441. 'address',
  1442. ],
  1443. ]));
  1444. $expected = [
  1445. 'div' => ['style' => 'display:none;'],
  1446. ['input' => [
  1447. 'type' => 'hidden',
  1448. 'name' => '_Token[fields]',
  1449. 'value' => $hash,
  1450. ]],
  1451. ['input' => [
  1452. 'type' => 'hidden',
  1453. 'name' => '_Token[unlocked]',
  1454. 'value' => 'address%7Cfirst_name',
  1455. ]],
  1456. ['input' => [
  1457. 'type' => 'hidden',
  1458. 'name' => '_Token[debug]',
  1459. 'value' => $tokenDebug,
  1460. ]],
  1461. '/div',
  1462. ];
  1463. $this->assertHtml($expected, $result);
  1464. }
  1465. /**
  1466. * testFormSecurityControlDisabledFields method
  1467. *
  1468. * Test single record form with disabled fields.
  1469. */
  1470. public function testFormSecurityControlUnlockedFields(): void
  1471. {
  1472. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1473. 'unlockedFields' => ['first_name', 'address'],
  1474. ]));
  1475. $this->Form->create();
  1476. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1477. $this->assertEquals(
  1478. $this->View->getRequest()->getAttribute('formTokenData'),
  1479. ['unlockedFields' => $result]
  1480. );
  1481. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1482. $this->Form->text('Addresses.title');
  1483. $this->Form->text('Addresses.first_name');
  1484. $this->Form->text('Addresses.last_name');
  1485. $this->Form->text('Addresses.address');
  1486. $this->Form->text('Addresses.city');
  1487. $this->Form->text('Addresses.phone');
  1488. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1489. $expected = [
  1490. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1491. 'Addresses.city', 'Addresses.phone',
  1492. ];
  1493. $this->assertEquals($expected, $result);
  1494. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1495. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1496. $tokenDebug = urlencode(json_encode([
  1497. '/articles/add',
  1498. [
  1499. 'Addresses.id' => '123456',
  1500. 'Addresses.title',
  1501. 'Addresses.last_name',
  1502. 'Addresses.city',
  1503. 'Addresses.phone',
  1504. ],
  1505. [
  1506. 'first_name',
  1507. 'address',
  1508. ],
  1509. ]));
  1510. $expected = [
  1511. 'div' => ['style' => 'display:none;'],
  1512. ['input' => [
  1513. 'type' => 'hidden',
  1514. 'name' => '_Token[fields]',
  1515. 'value' => $hash,
  1516. 'data-foo' => 'bar',
  1517. ]],
  1518. ['input' => [
  1519. 'type' => 'hidden',
  1520. 'name' => '_Token[unlocked]',
  1521. 'value' => 'address%7Cfirst_name',
  1522. 'data-foo' => 'bar',
  1523. ]],
  1524. ['input' => [
  1525. 'type' => 'hidden', 'name' => '_Token[debug]',
  1526. 'value' => $tokenDebug,
  1527. 'data-foo' => 'bar',
  1528. ]],
  1529. '/div',
  1530. ];
  1531. $this->assertHtml($expected, $result);
  1532. }
  1533. /**
  1534. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1535. *
  1536. * Test single record form with debugSecurity param.
  1537. */
  1538. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1539. {
  1540. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1541. 'unlockedFields' => ['first_name', 'address'],
  1542. ]));
  1543. $this->Form->create();
  1544. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1545. $this->assertEquals(
  1546. $this->View->getRequest()->getAttribute('formTokenData'),
  1547. ['unlockedFields' => $result]
  1548. );
  1549. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1550. $this->Form->text('Addresses.title');
  1551. $this->Form->text('Addresses.first_name');
  1552. $this->Form->text('Addresses.last_name');
  1553. $this->Form->text('Addresses.address');
  1554. $this->Form->text('Addresses.city');
  1555. $this->Form->text('Addresses.phone');
  1556. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1557. $expected = [
  1558. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1559. 'Addresses.city', 'Addresses.phone',
  1560. ];
  1561. $this->assertEquals($expected, $result);
  1562. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1563. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1564. $tokenDebug = urlencode(json_encode([
  1565. '/articles/add',
  1566. [
  1567. 'Addresses.id' => '123456',
  1568. 'Addresses.title',
  1569. 'Addresses.last_name',
  1570. 'Addresses.city',
  1571. 'Addresses.phone',
  1572. ],
  1573. [
  1574. 'first_name',
  1575. 'address',
  1576. ],
  1577. ]));
  1578. $expected = [
  1579. 'div' => ['style' => 'display:none;'],
  1580. ['input' => [
  1581. 'type' => 'hidden',
  1582. 'name' => '_Token[fields]',
  1583. 'value' => $hash,
  1584. 'data-foo' => 'bar',
  1585. ]],
  1586. ['input' => [
  1587. 'type' => 'hidden',
  1588. 'name' => '_Token[unlocked]',
  1589. 'value' => 'address%7Cfirst_name',
  1590. 'data-foo' => 'bar',
  1591. ]],
  1592. ['input' => [
  1593. 'type' => 'hidden', 'name' => '_Token[debug]',
  1594. 'value' => $tokenDebug,
  1595. 'data-foo' => 'bar',
  1596. ]],
  1597. '/div',
  1598. ];
  1599. $this->assertHtml($expected, $result);
  1600. }
  1601. /**
  1602. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1603. *
  1604. * Debug is false, debugSecurity is true -> no debug
  1605. */
  1606. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1607. {
  1608. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1609. 'unlockedFields' => ['first_name', 'address'],
  1610. ]));
  1611. $this->Form->create();
  1612. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1613. $this->assertEquals(
  1614. $this->View->getRequest()->getAttribute('formTokenData'),
  1615. ['unlockedFields' => $result]
  1616. );
  1617. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1618. $this->Form->text('Addresses.title');
  1619. $this->Form->text('Addresses.first_name');
  1620. $this->Form->text('Addresses.last_name');
  1621. $this->Form->text('Addresses.address');
  1622. $this->Form->text('Addresses.city');
  1623. $this->Form->text('Addresses.phone');
  1624. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1625. $expected = [
  1626. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1627. 'Addresses.city', 'Addresses.phone',
  1628. ];
  1629. $this->assertEquals($expected, $result);
  1630. Configure::write('debug', false);
  1631. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1632. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1633. $expected = [
  1634. 'div' => ['style' => 'display:none;'],
  1635. ['input' => [
  1636. 'type' => 'hidden',
  1637. 'name' => '_Token[fields]',
  1638. 'value' => $hash,
  1639. 'data-foo' => 'bar',
  1640. ]],
  1641. ['input' => [
  1642. 'type' => 'hidden',
  1643. 'name' => '_Token[unlocked]',
  1644. 'value' => 'address%7Cfirst_name',
  1645. 'data-foo' => 'bar',
  1646. ]],
  1647. '/div',
  1648. ];
  1649. $this->assertHtml($expected, $result);
  1650. }
  1651. /**
  1652. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1653. *
  1654. * Test single record form with debugSecurity param.
  1655. */
  1656. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1657. {
  1658. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1659. 'unlockedFields' => ['first_name', 'address'],
  1660. ]));
  1661. $this->Form->create();
  1662. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1663. $this->assertEquals(
  1664. $this->View->getRequest()->getAttribute('formTokenData'),
  1665. ['unlockedFields' => $result]
  1666. );
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone',
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $expected = [
  1683. 'div' => ['style' => 'display:none;'],
  1684. ['input' => [
  1685. 'type' => 'hidden',
  1686. 'name' => '_Token[fields]',
  1687. 'value' => $hash,
  1688. 'data-foo' => 'bar',
  1689. ]],
  1690. ['input' => [
  1691. 'type' => 'hidden',
  1692. 'name' => '_Token[unlocked]',
  1693. 'value' => 'address%7Cfirst_name',
  1694. 'data-foo' => 'bar',
  1695. ]],
  1696. '/div',
  1697. ];
  1698. $this->assertHtml($expected, $result);
  1699. }
  1700. /**
  1701. * testFormSecureWithCustomNameAttribute method
  1702. *
  1703. * Test securing inputs with custom name attributes.
  1704. */
  1705. public function testFormSecureWithCustomNameAttribute(): void
  1706. {
  1707. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1708. $this->Form->create();
  1709. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1710. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1711. $this->assertSame('User.custom', $result[0]);
  1712. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1713. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1714. $this->assertSame('User.custom.another.value', $result[1]);
  1715. }
  1716. /**
  1717. * testFormSecuredControl method
  1718. *
  1719. * Test generation of entire secure form, assertions made on control() output.
  1720. */
  1721. public function testFormSecuredControl(): void
  1722. {
  1723. $this->View->setRequest($this->View->getRequest()
  1724. ->withAttribute('formTokenData', [])
  1725. ->withAttribute('csrfToken', 'testKey'));
  1726. $this->article['schema'] = [
  1727. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1728. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1729. ];
  1730. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1731. $encoding = strtolower(Configure::read('App.encoding'));
  1732. $expected = [
  1733. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_csrfToken',
  1738. 'value' => 'testKey',
  1739. ]],
  1740. '/div',
  1741. ];
  1742. $this->assertHtml($expected, $result);
  1743. $result = $this->Form->control('ratio');
  1744. $expected = [
  1745. 'div' => ['class'],
  1746. 'label' => ['for'],
  1747. 'Ratio',
  1748. '/label',
  1749. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1750. '/div',
  1751. ];
  1752. $this->assertHtml($expected, $result);
  1753. $result = $this->Form->control('population');
  1754. $expected = [
  1755. 'div' => ['class'],
  1756. 'label' => ['for'],
  1757. 'Population',
  1758. '/label',
  1759. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1760. '/div',
  1761. ];
  1762. $this->assertHtml($expected, $result);
  1763. $result = $this->Form->control('published', ['type' => 'text']);
  1764. $expected = [
  1765. 'div' => ['class' => 'input text'],
  1766. 'label' => ['for' => 'published'],
  1767. 'Published',
  1768. '/label',
  1769. ['input' => [
  1770. 'type' => 'text',
  1771. 'name' => 'published',
  1772. 'id' => 'published',
  1773. ]],
  1774. '/div',
  1775. ];
  1776. $this->assertHtml($expected, $result);
  1777. $result = $this->Form->control('other', ['type' => 'text']);
  1778. $expected = [
  1779. 'div' => ['class' => 'input text'],
  1780. 'label' => ['for' => 'other'],
  1781. 'Other',
  1782. '/label',
  1783. ['input' => [
  1784. 'type' => 'text',
  1785. 'name' => 'other',
  1786. 'id',
  1787. ]],
  1788. '/div',
  1789. ];
  1790. $this->assertHtml($expected, $result);
  1791. $result = $this->Form->hidden('stuff');
  1792. $expected = [
  1793. 'input' => [
  1794. 'type' => 'hidden',
  1795. 'name' => 'stuff',
  1796. ],
  1797. ];
  1798. $this->assertHtml($expected, $result);
  1799. $result = $this->Form->hidden('hidden', ['value' => false]);
  1800. $expected = ['input' => [
  1801. 'type' => 'hidden',
  1802. 'name' => 'hidden',
  1803. 'value' => '0',
  1804. ]];
  1805. $this->assertHtml($expected, $result);
  1806. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1807. $expected = [
  1808. 'div' => ['class' => 'input checkbox'],
  1809. ['input' => [
  1810. 'type' => 'hidden',
  1811. 'name' => 'something',
  1812. 'value' => '0',
  1813. ]],
  1814. 'label' => ['for' => 'something'],
  1815. ['input' => [
  1816. 'type' => 'checkbox',
  1817. 'name' => 'something',
  1818. 'value' => '1',
  1819. 'id' => 'something',
  1820. ]],
  1821. 'Something',
  1822. '/label',
  1823. '/div',
  1824. ];
  1825. $this->assertHtml($expected, $result);
  1826. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1827. $expectedFields = [
  1828. 'ratio',
  1829. 'population',
  1830. 'published',
  1831. 'other',
  1832. 'stuff' => '',
  1833. 'hidden' => '0',
  1834. 'something',
  1835. ];
  1836. $this->assertEquals($expectedFields, $result);
  1837. $result = $this->Form->secure();
  1838. $tokenDebug = urlencode(json_encode([
  1839. '/articles/add',
  1840. $expectedFields,
  1841. [],
  1842. ]));
  1843. $expected = [
  1844. 'div' => ['style' => 'display:none;'],
  1845. ['input' => [
  1846. 'type' => 'hidden',
  1847. 'name' => '_Token[fields]',
  1848. 'value',
  1849. ]],
  1850. ['input' => [
  1851. 'type' => 'hidden',
  1852. 'name' => '_Token[unlocked]',
  1853. 'value' => '',
  1854. ]],
  1855. ['input' => [
  1856. 'type' => 'hidden', 'name' => '_Token[debug]',
  1857. 'value' => $tokenDebug,
  1858. ]],
  1859. '/div',
  1860. ];
  1861. $this->assertHtml($expected, $result);
  1862. $data = [
  1863. 'ratio' => '',
  1864. 'population' => '',
  1865. 'published' => '',
  1866. 'other' => '',
  1867. 'stuff' => '',
  1868. 'hidden' => '0',
  1869. 'something' => '',
  1870. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1871. ];
  1872. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1873. }
  1874. /**
  1875. * testSecuredControlCustomName method
  1876. *
  1877. * Test secured inputs with custom names.
  1878. */
  1879. public function testSecuredControlCustomName(): void
  1880. {
  1881. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1882. $this->Form->create();
  1883. $this->Form->text('text_input', [
  1884. 'name' => 'Option[General.default_role]',
  1885. ]);
  1886. $expected = ['Option.General.default_role'];
  1887. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1888. $this->assertEquals($expected, $result);
  1889. $this->Form->select('select_box', [1, 2], [
  1890. 'name' => 'Option[General.select_role]',
  1891. ]);
  1892. $expected[] = 'Option.General.select_role';
  1893. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1894. $this->assertEquals($expected, $result);
  1895. $this->Form->text('other.things[]');
  1896. $expected[] = 'other.things';
  1897. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1898. $this->assertEquals($expected, $result);
  1899. }
  1900. /**
  1901. * testSecuredControlDuplicate method
  1902. *
  1903. * Test that a hidden field followed by a visible field
  1904. * undoes the hidden field locking.
  1905. */
  1906. public function testSecuredControlDuplicate(): void
  1907. {
  1908. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1909. $this->Form->create();
  1910. $this->Form->control('text_val', [
  1911. 'type' => 'hidden',
  1912. 'value' => 'some text',
  1913. ]);
  1914. $expected = ['text_val' => 'some text'];
  1915. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1916. $this->assertEquals($expected, $result);
  1917. $this->Form->control('text_val', [
  1918. 'type' => 'text',
  1919. ]);
  1920. $expected = ['text_val'];
  1921. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1922. $this->assertEquals($expected, $result);
  1923. }
  1924. /**
  1925. * testFormSecuredFileControl method
  1926. *
  1927. * Tests that the correct keys are added to the field hash index.
  1928. */
  1929. public function testFormSecuredFileControl(): void
  1930. {
  1931. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1932. $this->Form->create();
  1933. $this->Form->file('Attachment.file');
  1934. $expected = ['Attachment.file'];
  1935. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1936. $this->assertEquals($expected, $result);
  1937. }
  1938. /**
  1939. * testFormSecuredMultipleSelect method
  1940. *
  1941. * Test that multiple selects keys are added to field hash.
  1942. */
  1943. public function testFormSecuredMultipleSelect(): void
  1944. {
  1945. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1946. $this->Form->create();
  1947. $options = ['1' => 'one', '2' => 'two'];
  1948. $this->Form->select('Model.select', $options);
  1949. $expected = ['Model.select'];
  1950. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1951. $this->assertEquals($expected, $result);
  1952. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1953. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1954. $this->assertEquals($expected, $result);
  1955. }
  1956. /**
  1957. * testFormSecuredRadio method
  1958. */
  1959. public function testFormSecuredRadio(): void
  1960. {
  1961. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1962. $this->Form->create();
  1963. $options = ['1' => 'option1', '2' => 'option2'];
  1964. $this->Form->radio('Test.test', $options);
  1965. $expected = ['Test.test'];
  1966. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1967. $this->assertEquals($expected, $result);
  1968. $this->Form->radio('Test.all', $options, [
  1969. 'disabled' => ['option1', 'option2'],
  1970. ]);
  1971. $expected = ['Test.test', 'Test.all' => ''];
  1972. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1973. $this->assertEquals($expected, $result);
  1974. $this->Form->radio('Test.some', $options, [
  1975. 'disabled' => ['option1'],
  1976. ]);
  1977. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1978. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1979. $this->assertEquals($expected, $result);
  1980. }
  1981. /**
  1982. * testFormSecuredAndDisabled method
  1983. *
  1984. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1985. * hashing.
  1986. */
  1987. public function testFormSecuredAndDisabled(): void
  1988. {
  1989. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1990. $this->Form->create();
  1991. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1992. $this->Form->text('Model.text', ['disabled' => true]);
  1993. $this->Form->password('Model.text', ['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->day('Model.day', ['disabled' => true]);
  2000. $this->Form->hour('Model.hour', ['disabled' => true]);
  2001. $this->Form->minute('Model.minute', ['disabled' => true]);
  2002. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2003. $expected = [
  2004. 'Model.radio' => '',
  2005. ];
  2006. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2007. $this->assertEquals($expected, $result);
  2008. }
  2009. /**
  2010. * testUnlockFieldAddsToList method
  2011. *
  2012. * Test disableField.
  2013. */
  2014. public function testUnlockFieldAddsToList(): void
  2015. {
  2016. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2017. 'unlockedFields' => [],
  2018. ]));
  2019. $this->Form->create();
  2020. $this->Form->unlockField('Contact.name');
  2021. $this->Form->text('Contact.name');
  2022. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2023. $this->assertEquals([], $result);
  2024. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2025. $this->assertEquals(['Contact.name'], $result);
  2026. }
  2027. /**
  2028. * testUnlockFieldRemovingFromFields method
  2029. *
  2030. * Test unlockField removing from fields array.
  2031. */
  2032. public function testUnlockFieldRemovingFromFields(): void
  2033. {
  2034. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2035. 'unlockedFields' => [],
  2036. ]));
  2037. $this->Form->create($this->article);
  2038. $this->Form->hidden('Article.id', ['value' => 1]);
  2039. $this->Form->text('Article.title');
  2040. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2041. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2042. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2043. $this->Form->unlockField('Article.title');
  2044. $this->Form->unlockField('Article.id');
  2045. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2046. $this->assertEquals([], $result);
  2047. }
  2048. /**
  2049. * testResetUnlockFields method
  2050. *
  2051. * Test reset unlockFields, when create new form.
  2052. */
  2053. public function testResetUnlockFields(): void
  2054. {
  2055. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2056. 'key' => 'testKey',
  2057. 'unlockedFields' => [],
  2058. ]));
  2059. $this->Form->create();
  2060. $this->Form->unlockField('Contact.id');
  2061. $this->Form->hidden('Contact.id', ['value' => 1]);
  2062. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2063. $this->assertEmpty($result, 'Field should be unlocked');
  2064. $this->Form->end();
  2065. $this->Form->create();
  2066. $this->Form->hidden('Contact.id', ['value' => 1]);
  2067. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2068. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2069. }
  2070. /**
  2071. * testSecuredFormUrlIgnoresHost method
  2072. *
  2073. * Test that only the path + query elements of a form's URL show up in their hash.
  2074. */
  2075. public function testSecuredFormUrlIgnoresHost(): void
  2076. {
  2077. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2078. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2079. $this->Form->create($this->article, [
  2080. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2081. ]);
  2082. $result = $this->Form->secure();
  2083. $this->assertStringContainsString($expected, $result);
  2084. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2085. $result = $this->Form->secure();
  2086. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2087. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2088. $result = $this->Form->secure();
  2089. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2090. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2091. $result = $this->Form->secure();
  2092. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2093. }
  2094. /**
  2095. * testSecuredFormUrlHasHtmlAndIdentifier method
  2096. *
  2097. * Test that URL, HTML and identifier show up in their hashes.
  2098. */
  2099. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2100. {
  2101. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2102. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2103. $this->Form->create($this->article, [
  2104. 'url' => [
  2105. 'controller' => 'articles',
  2106. 'action' => 'view',
  2107. '?' => [
  2108. 'page' => 1,
  2109. 'limit' => 10,
  2110. 'html' => '<>"',
  2111. ],
  2112. '#' => 'result',
  2113. ],
  2114. ]);
  2115. $result = $this->Form->secure();
  2116. $this->assertStringContainsString($expected, $result);
  2117. $this->Form->create($this->article, [
  2118. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2119. ]);
  2120. $result = $this->Form->secure();
  2121. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2122. $this->Form->create($this->article, [
  2123. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2124. ]);
  2125. $result = $this->Form->secure();
  2126. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2127. }
  2128. /**
  2129. * testErrorMessageDisplay method
  2130. *
  2131. * Test error message display.
  2132. */
  2133. public function testErrorMessageDisplay(): void
  2134. {
  2135. $this->article['errors'] = [
  2136. 'Article' => [
  2137. 'title' => 'error message',
  2138. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2139. ],
  2140. ];
  2141. $this->Form->create($this->article);
  2142. $result = $this->Form->control('Article.title');
  2143. $expected = [
  2144. 'div' => ['class' => 'input text error'],
  2145. 'label' => ['for' => 'article-title'],
  2146. 'Title',
  2147. '/label',
  2148. 'input' => [
  2149. 'type' => 'text',
  2150. 'name' => 'Article[title]',
  2151. 'id' => 'article-title',
  2152. 'class' => 'form-error',
  2153. 'aria-invalid' => 'true',
  2154. 'aria-describedby' => 'article-title-error',
  2155. ],
  2156. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2157. 'error message',
  2158. '/div',
  2159. '/div',
  2160. ];
  2161. $this->assertHtml($expected, $result);
  2162. $result = $this->Form->control('Article.title', [
  2163. 'templates' => [
  2164. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2165. ],
  2166. ]);
  2167. $expected = [
  2168. 'div' => ['class' => 'input text error'],
  2169. 'label' => ['for' => 'article-title'],
  2170. 'Title',
  2171. '/label',
  2172. 'input' => [
  2173. 'type' => 'text',
  2174. 'name' => 'Article[title]',
  2175. 'id' => 'article-title',
  2176. 'class' => 'form-error',
  2177. // No aria-describedby because error template is custom
  2178. 'aria-invalid' => 'true',
  2179. ],
  2180. '/div',
  2181. ];
  2182. $this->assertHtml($expected, $result);
  2183. $result = $this->Form->control('Article.content');
  2184. $expected = [
  2185. 'div' => ['class' => 'input text error'],
  2186. 'label' => ['for' => 'article-content'],
  2187. 'Content',
  2188. '/label',
  2189. 'input' => [
  2190. 'type' => 'text',
  2191. 'name' => 'Article[content]',
  2192. 'id' => 'article-content',
  2193. 'class' => 'form-error',
  2194. 'aria-invalid' => 'true',
  2195. 'aria-describedby' => 'article-content-error',
  2196. ],
  2197. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2198. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2199. '/div',
  2200. '/div',
  2201. ];
  2202. $this->assertHtml($expected, $result);
  2203. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2204. $expected = [
  2205. 'div' => ['class' => 'input text error'],
  2206. 'label' => ['for' => 'article-content'],
  2207. 'Content',
  2208. '/label',
  2209. 'input' => [
  2210. 'type' => 'text',
  2211. 'name' => 'Article[content]',
  2212. 'id' => 'article-content',
  2213. 'class' => 'form-error',
  2214. 'aria-invalid' => 'true',
  2215. 'aria-describedby' => 'article-content-error',
  2216. ],
  2217. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2218. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2219. '/div',
  2220. '/div',
  2221. ];
  2222. $this->assertHtml($expected, $result);
  2223. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2224. $expected = [
  2225. 'div' => ['class' => 'input text error'],
  2226. 'label' => ['for' => 'article-content'],
  2227. 'Content',
  2228. '/label',
  2229. 'input' => [
  2230. 'type' => 'text',
  2231. 'name' => 'Article[content]',
  2232. 'id' => 'article-content',
  2233. 'class' => 'form-error',
  2234. 'aria-invalid' => 'true',
  2235. 'aria-describedby' => 'article-content-error',
  2236. ],
  2237. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2238. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2239. '/div',
  2240. '/div',
  2241. ];
  2242. $this->assertHtml($expected, $result);
  2243. }
  2244. /**
  2245. * testEmptyErrorValidation method
  2246. *
  2247. * Test validation errors, when validation message is an empty string.
  2248. */
  2249. public function testEmptyErrorValidation(): void
  2250. {
  2251. $this->article['errors'] = [
  2252. 'Article' => ['title' => ''],
  2253. ];
  2254. $this->Form->create($this->article);
  2255. $result = $this->Form->control('Article.title');
  2256. $expected = [
  2257. 'div' => ['class' => 'input text error'],
  2258. 'label' => ['for' => 'article-title'],
  2259. 'Title',
  2260. '/label',
  2261. 'input' => [
  2262. 'type' => 'text',
  2263. 'name' => 'Article[title]',
  2264. 'id' => 'article-title',
  2265. 'class' => 'form-error',
  2266. 'aria-invalid' => 'true',
  2267. 'aria-describedby' => 'article-title-error',
  2268. ],
  2269. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2270. [],
  2271. '/div',
  2272. '/div',
  2273. ];
  2274. $this->assertHtml($expected, $result);
  2275. }
  2276. /**
  2277. * testEmptyControlErrorValidation method
  2278. *
  2279. * Test validation errors, when calling control() overriding validation message by an empty string.
  2280. */
  2281. public function testEmptyControlErrorValidation(): void
  2282. {
  2283. $this->article['errors'] = [
  2284. 'Article' => ['title' => 'error message'],
  2285. ];
  2286. $this->Form->create($this->article);
  2287. $result = $this->Form->control('Article.title', ['error' => '']);
  2288. $expected = [
  2289. 'div' => ['class' => 'input text error'],
  2290. 'label' => ['for' => 'article-title'],
  2291. 'Title',
  2292. '/label',
  2293. 'input' => [
  2294. 'aria-invalid' => 'true',
  2295. 'aria-describedby' => 'article-title-error',
  2296. 'type' => 'text',
  2297. 'name' => 'Article[title]',
  2298. 'id' => 'article-title',
  2299. 'class' => 'form-error',
  2300. ],
  2301. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2302. [],
  2303. '/div',
  2304. '/div',
  2305. ];
  2306. $this->assertHtml($expected, $result);
  2307. }
  2308. /**
  2309. * testControlErrorMessage method
  2310. *
  2311. * Test validation errors, when calling control() overriding validation messages.
  2312. */
  2313. public function testControlErrorMessage(): void
  2314. {
  2315. $this->article['errors'] = [
  2316. 'title' => ['error message'],
  2317. ];
  2318. $this->Form->create($this->article);
  2319. $result = $this->Form->control('title', [
  2320. 'error' => 'Custom error!',
  2321. ]);
  2322. $expected = [
  2323. 'div' => ['class' => 'input text required error'],
  2324. 'label' => ['for' => 'title'],
  2325. 'Title',
  2326. '/label',
  2327. 'input' => [
  2328. 'type' => 'text',
  2329. 'name' => 'title',
  2330. 'id' => 'title',
  2331. 'class' => 'form-error',
  2332. 'required' => 'required',
  2333. 'data-validity-message' => 'This field cannot be left empty',
  2334. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2335. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2336. 'aria-invalid' => 'true',
  2337. 'aria-describedby' => 'title-error',
  2338. ],
  2339. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2340. 'Custom error!',
  2341. '/div',
  2342. '/div',
  2343. ];
  2344. $this->assertHtml($expected, $result);
  2345. $result = $this->Form->control('title', [
  2346. 'error' => ['error message' => 'Custom error!'],
  2347. ]);
  2348. $expected = [
  2349. 'div' => ['class' => 'input text required error'],
  2350. 'label' => ['for' => 'title'],
  2351. 'Title',
  2352. '/label',
  2353. 'input' => [
  2354. 'type' => 'text',
  2355. 'name' => 'title',
  2356. 'id' => 'title',
  2357. 'aria-invalid' => 'true',
  2358. 'aria-describedby' => 'title-error',
  2359. 'class' => 'form-error',
  2360. 'required' => 'required',
  2361. 'data-validity-message' => 'This field cannot be left empty',
  2362. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2363. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2364. ],
  2365. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2366. 'Custom error!',
  2367. '/div',
  2368. '/div',
  2369. ];
  2370. $this->assertHtml($expected, $result);
  2371. }
  2372. /**
  2373. * testFormValidationAssociated method
  2374. *
  2375. * Tests displaying errors for nested entities.
  2376. */
  2377. public function testFormValidationAssociated(): void
  2378. {
  2379. $nested = new Entity(['foo' => 'bar']);
  2380. $nested->setError('foo', ['not a valid bar']);
  2381. $entity = new Entity(['nested' => $nested]);
  2382. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2383. $result = $this->Form->error('nested.foo');
  2384. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2385. }
  2386. /**
  2387. * testFormValidationAssociatedSecondLevel method
  2388. *
  2389. * Test form error display with associated model.
  2390. */
  2391. public function testFormValidationAssociatedSecondLevel(): void
  2392. {
  2393. $inner = new Entity(['bar' => 'baz']);
  2394. $nested = new Entity(['foo' => $inner]);
  2395. $entity = new Entity(['nested' => $nested]);
  2396. $inner->setError('bar', ['not a valid one']);
  2397. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2398. $result = $this->Form->error('nested.foo.bar');
  2399. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2400. }
  2401. /**
  2402. * testFormValidationMultiRecord method
  2403. *
  2404. * Test form error display with multiple records.
  2405. */
  2406. public function testFormValidationMultiRecord(): void
  2407. {
  2408. $one = new Entity();
  2409. $two = new Entity();
  2410. $this->getTableLocator()->get('Contacts', [
  2411. 'className' => ContactsTable::class,
  2412. ]);
  2413. $one->set('email', '');
  2414. $one->setError('email', ['invalid email']);
  2415. $two->set('name', '');
  2416. $two->setError('name', ['This is wrong']);
  2417. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2418. $result = $this->Form->control('0.email');
  2419. $expected = [
  2420. 'div' => ['class' => 'input email error'],
  2421. 'label' => ['for' => '0-email'],
  2422. 'Email',
  2423. '/label',
  2424. 'input' => [
  2425. 'type' => 'email',
  2426. 'name' => '0[email]',
  2427. 'id' => '0-email',
  2428. 'class' => 'form-error',
  2429. 'maxlength' => 255,
  2430. 'value' => '',
  2431. 'aria-invalid' => 'true',
  2432. 'aria-describedby' => '0-email-error',
  2433. ],
  2434. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2435. 'invalid email',
  2436. '/div',
  2437. '/div',
  2438. ];
  2439. $this->assertHtml($expected, $result);
  2440. $result = $this->Form->control('1.name');
  2441. $expected = [
  2442. 'div' => ['class' => 'input text error'],
  2443. 'label' => ['for' => '1-name'],
  2444. 'Name',
  2445. '/label',
  2446. 'input' => [
  2447. 'type' => 'text',
  2448. 'name' => '1[name]',
  2449. 'id' => '1-name',
  2450. 'class' => 'form-error',
  2451. 'maxlength' => 255,
  2452. 'value' => '',
  2453. 'aria-invalid' => 'true',
  2454. 'aria-describedby' => '1-name-error',
  2455. ],
  2456. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2457. 'This is wrong',
  2458. '/div',
  2459. '/div',
  2460. ];
  2461. $this->assertHtml($expected, $result);
  2462. }
  2463. /**
  2464. * testControl method
  2465. *
  2466. * Test various incarnations of control().
  2467. */
  2468. public function testControl(): void
  2469. {
  2470. $this->getTableLocator()->get('ValidateUsers', [
  2471. 'className' => ValidateUsersTable::class,
  2472. ]);
  2473. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2474. $result = $this->Form->control('ValidateUsers.balance');
  2475. $expected = [
  2476. 'div' => ['class'],
  2477. 'label' => ['for'],
  2478. 'Balance',
  2479. '/label',
  2480. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2481. '/div',
  2482. ];
  2483. $this->assertHtml($expected, $result);
  2484. $result = $this->Form->control('ValidateUser.cost_decimal');
  2485. $expected = [
  2486. 'div' => ['class'],
  2487. 'label' => ['for'],
  2488. 'Cost Decimal',
  2489. '/label',
  2490. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2491. '/div',
  2492. ];
  2493. $this->assertHtml($expected, $result);
  2494. $result = $this->Form->control('ValidateUser.null_decimal');
  2495. $expected = [
  2496. 'div' => ['class'],
  2497. 'label' => ['for'],
  2498. 'Null Decimal',
  2499. '/label',
  2500. 'input' => ['name', 'type' => 'number', 'id'],
  2501. '/div',
  2502. ];
  2503. $this->assertHtml($expected, $result);
  2504. }
  2505. /**
  2506. * testControlCustomization method
  2507. *
  2508. * Tests the input method and passing custom options.
  2509. */
  2510. public function testControlCustomization(): void
  2511. {
  2512. $this->getTableLocator()->get('Contacts', [
  2513. 'className' => ContactsTable::class,
  2514. ]);
  2515. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2516. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2517. $expected = [
  2518. 'div' => ['class' => 'input email'],
  2519. 'label' => ['for' => 'custom'],
  2520. 'Email',
  2521. '/label',
  2522. ['input' => [
  2523. 'type' => 'email', 'name' => 'Contact[email]',
  2524. 'id' => 'custom', 'maxlength' => 255,
  2525. ]],
  2526. '/div',
  2527. ];
  2528. $this->assertHtml($expected, $result);
  2529. $result = $this->Form->control('Contact.email', [
  2530. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2531. ]);
  2532. $expected = [
  2533. '<div',
  2534. 'label' => ['for' => 'contact-email'],
  2535. 'Email',
  2536. '/label',
  2537. ['input' => [
  2538. 'type' => 'email', 'name' => 'Contact[email]',
  2539. 'id' => 'contact-email', 'maxlength' => 255,
  2540. ]],
  2541. '/div',
  2542. ];
  2543. $this->assertHtml($expected, $result);
  2544. $result = $this->Form->control('Contact.email', [
  2545. 'templates' => [
  2546. 'formGroup' => '{{input}}',
  2547. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2548. ],
  2549. ]);
  2550. $expected = [
  2551. '<div',
  2552. '<div',
  2553. 'label' => ['for' => 'contact-email'],
  2554. 'Email',
  2555. '/label',
  2556. '/div',
  2557. ['input' => [
  2558. 'type' => 'email', 'name' => 'Contact[email]',
  2559. 'id' => 'contact-email', 'maxlength' => 255,
  2560. ]],
  2561. '/div',
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2565. $expected = [
  2566. 'div' => ['class' => 'input text'],
  2567. 'label' => ['for' => 'contact-email'],
  2568. 'Email',
  2569. '/label',
  2570. ['input' => [
  2571. 'type' => 'text', 'name' => 'Contact[email]',
  2572. 'id' => 'contact-email', 'maxlength' => '255',
  2573. ]],
  2574. '/div',
  2575. ];
  2576. $this->assertHtml($expected, $result);
  2577. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2578. $expected = [
  2579. 'div' => ['class' => 'input text'],
  2580. 'label' => ['for' => 'contact-5-email'],
  2581. 'Email',
  2582. '/label',
  2583. ['input' => [
  2584. 'type' => 'text', 'name' => 'Contact[5][email]',
  2585. 'id' => 'contact-5-email', 'maxlength' => '255',
  2586. ]],
  2587. '/div',
  2588. ];
  2589. $this->assertHtml($expected, $result);
  2590. $result = $this->Form->control('Contact.password');
  2591. $expected = [
  2592. 'div' => ['class' => 'input password'],
  2593. 'label' => ['for' => 'contact-password'],
  2594. 'Password',
  2595. '/label',
  2596. ['input' => [
  2597. 'type' => 'password', 'name' => 'Contact[password]',
  2598. 'id' => 'contact-password',
  2599. ]],
  2600. '/div',
  2601. ];
  2602. $this->assertHtml($expected, $result);
  2603. $result = $this->Form->control('Contact.email', [
  2604. 'type' => 'file', 'class' => 'textbox',
  2605. ]);
  2606. $expected = [
  2607. 'div' => ['class' => 'input file'],
  2608. 'label' => ['for' => 'contact-email'],
  2609. 'Email',
  2610. '/label',
  2611. ['input' => [
  2612. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2613. 'id' => 'contact-email',
  2614. ]],
  2615. '/div',
  2616. ];
  2617. $this->assertHtml($expected, $result);
  2618. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2619. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2620. $result = $this->Form->control('phone');
  2621. $expected = [
  2622. 'div' => ['class' => 'input tel'],
  2623. 'label' => ['for' => 'phone'],
  2624. 'Phone',
  2625. '/label',
  2626. ['input' => [
  2627. 'type' => 'tel', 'name' => 'phone',
  2628. 'value' => 'Hello &amp; World &gt; weird chars',
  2629. 'id' => 'phone', 'maxlength' => 255,
  2630. ]],
  2631. '/div',
  2632. ];
  2633. $this->assertHtml($expected, $result);
  2634. $this->View->setRequest(
  2635. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2636. );
  2637. $this->Form->create();
  2638. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2639. $expected = [
  2640. 'div' => ['class' => 'input text'],
  2641. 'label' => ['for' => 'myId'],
  2642. 'Field',
  2643. '/label',
  2644. 'input' => [
  2645. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2646. 'value' => 'My value', 'id' => 'myId',
  2647. ],
  2648. '/div',
  2649. ];
  2650. $this->assertHtml($expected, $result);
  2651. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2652. $this->Form->create();
  2653. $entity->setError('field', 'Badness!');
  2654. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2655. $result = $this->Form->control('field');
  2656. $expected = [
  2657. 'div' => ['class' => 'input text error'],
  2658. 'label' => ['for' => 'field'],
  2659. 'Field',
  2660. '/label',
  2661. 'input' => [
  2662. 'type' => 'text',
  2663. 'name' => 'field',
  2664. 'id' => 'field',
  2665. 'class' => 'form-error',
  2666. 'aria-invalid' => 'true',
  2667. 'aria-describedby' => 'field-error',
  2668. ],
  2669. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2670. 'Badness!',
  2671. '/div',
  2672. '/div',
  2673. ];
  2674. $this->assertHtml($expected, $result);
  2675. $result = $this->Form->control('field', [
  2676. 'templates' => [
  2677. 'inputContainerError' => '{{content}}{{error}}',
  2678. 'error' => '<span class="error-message">{{content}}</span>',
  2679. ],
  2680. ]);
  2681. $expected = [
  2682. 'label' => ['for' => 'field'],
  2683. 'Field',
  2684. '/label',
  2685. 'input' => [
  2686. 'type' => 'text',
  2687. 'name' => 'field',
  2688. 'id' => 'field',
  2689. 'class' => 'form-error',
  2690. // No aria-describedby because error template is custom
  2691. 'aria-invalid' => 'true',
  2692. ],
  2693. ['span' => ['class' => 'error-message']],
  2694. 'Badness!',
  2695. '/span',
  2696. ];
  2697. $this->assertHtml($expected, $result);
  2698. $entity->setError('field', ['minLength'], true);
  2699. $result = $this->Form->control('field', [
  2700. 'error' => [
  2701. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2702. 'maxLength' => 'login too large',
  2703. ],
  2704. ]);
  2705. $expected = [
  2706. 'div' => ['class' => 'input text error'],
  2707. 'label' => ['for' => 'field'],
  2708. 'Field',
  2709. '/label',
  2710. 'input' => [
  2711. 'type' => 'text',
  2712. 'name' => 'field',
  2713. 'id' => 'field',
  2714. 'class' => 'form-error',
  2715. 'aria-invalid' => 'true',
  2716. 'aria-describedby' => 'field-error',
  2717. ],
  2718. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2719. 'Le login doit contenir au moins 2 caractères',
  2720. '/div',
  2721. '/div',
  2722. ];
  2723. $this->assertHtml($expected, $result);
  2724. $entity->setError('field', ['maxLength'], true);
  2725. $result = $this->Form->control('field', [
  2726. 'error' => [
  2727. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2728. 'maxLength' => 'login too large',
  2729. ],
  2730. ]);
  2731. $expected = [
  2732. 'div' => ['class' => 'input text error'],
  2733. 'label' => ['for' => 'field'],
  2734. 'Field',
  2735. '/label',
  2736. 'input' => [
  2737. 'type' => 'text',
  2738. 'name' => 'field',
  2739. 'id' => 'field',
  2740. 'class' => 'form-error',
  2741. 'aria-invalid' => 'true',
  2742. 'aria-describedby' => 'field-error',
  2743. ],
  2744. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2745. 'login too large',
  2746. '/div',
  2747. '/div',
  2748. ];
  2749. $this->assertHtml($expected, $result);
  2750. }
  2751. /**
  2752. * testControlWithTemplateFile method
  2753. *
  2754. * Test that control() accepts a template file.
  2755. */
  2756. public function testControlWithTemplateFile(): void
  2757. {
  2758. $result = $this->Form->control('field', [
  2759. 'templates' => 'htmlhelper_tags',
  2760. ]);
  2761. $expected = [
  2762. 'label' => ['for' => 'field'],
  2763. 'Field',
  2764. '/label',
  2765. 'input' => [
  2766. 'type' => 'text', 'name' => 'field',
  2767. 'id' => 'field',
  2768. ],
  2769. ];
  2770. $this->assertHtml($expected, $result);
  2771. }
  2772. /**
  2773. * testNestedControlsEndWithBrackets method
  2774. *
  2775. * Test that nested inputs end with brackets.
  2776. */
  2777. public function testNestedControlsEndWithBrackets(): void
  2778. {
  2779. $result = $this->Form->text('nested.text[]');
  2780. $expected = [
  2781. 'input' => [
  2782. 'type' => 'text', 'name' => 'nested[text][]',
  2783. ],
  2784. ];
  2785. $this->assertHtml($expected, $result);
  2786. $result = $this->Form->file('nested.file[]');
  2787. $expected = [
  2788. 'input' => [
  2789. 'type' => 'file', 'name' => 'nested[file][]',
  2790. ],
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. }
  2794. /**
  2795. * testCreateIdPrefix method
  2796. *
  2797. * Test id prefix.
  2798. */
  2799. public function testCreateIdPrefix(): void
  2800. {
  2801. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2802. $result = $this->Form->control('field');
  2803. $expected = [
  2804. 'div' => ['class' => 'input text'],
  2805. 'label' => ['for' => 'prefix-field'],
  2806. 'Field',
  2807. '/label',
  2808. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2809. '/div',
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2813. $expected = [
  2814. 'div' => ['class' => 'input text'],
  2815. 'label' => ['for' => 'custom-id'],
  2816. 'Field',
  2817. '/label',
  2818. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2819. '/div',
  2820. ];
  2821. $this->assertHtml($expected, $result);
  2822. $result = $this->Form->radio('Model.field', ['option A']);
  2823. $expected = [
  2824. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2825. 'label' => ['for' => 'prefix-model-field-0'],
  2826. ['input' => [
  2827. 'type' => 'radio',
  2828. 'name' => 'Model[field]',
  2829. 'value' => '0',
  2830. 'id' => 'prefix-model-field-0',
  2831. ]],
  2832. 'option A',
  2833. '/label',
  2834. ];
  2835. $this->assertHtml($expected, $result);
  2836. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2837. $expected = [
  2838. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2839. 'label' => ['for' => 'prefix-model-field-0'],
  2840. ['input' => [
  2841. 'type' => 'radio',
  2842. 'name' => 'Model[field]',
  2843. 'value' => '0',
  2844. 'id' => 'prefix-model-field-0',
  2845. ]],
  2846. 'option A',
  2847. '/label',
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. $result = $this->Form->select(
  2851. 'Model.multi_field',
  2852. ['first'],
  2853. ['multiple' => 'checkbox']
  2854. );
  2855. $expected = [
  2856. 'input' => [
  2857. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2858. ],
  2859. ['div' => ['class' => 'checkbox']],
  2860. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2861. ['input' => [
  2862. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2863. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2864. ]],
  2865. 'first',
  2866. '/label',
  2867. '/div',
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. $this->Form->end();
  2871. $result = $this->Form->control('field');
  2872. $expected = [
  2873. 'div' => ['class' => 'input text'],
  2874. 'label' => ['for' => 'field'],
  2875. 'Field',
  2876. '/label',
  2877. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2878. '/div',
  2879. ];
  2880. $this->assertHtml($expected, $result);
  2881. }
  2882. /**
  2883. * testControlZero method
  2884. *
  2885. * Test that inputs with 0 can be created.
  2886. */
  2887. public function testControlZero(): void
  2888. {
  2889. $this->getTableLocator()->get('Contacts', [
  2890. 'className' => ContactsTable::class,
  2891. ]);
  2892. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2893. $result = $this->Form->control('0');
  2894. $expected = [
  2895. 'div' => ['class' => 'input text'],
  2896. 'label' => ['for' => '0'], '/label',
  2897. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2898. '/div',
  2899. ];
  2900. $this->assertHtml($expected, $result);
  2901. }
  2902. /**
  2903. * testControlCheckbox method
  2904. *
  2905. * Test control() with checkbox creation.
  2906. */
  2907. public function testControlCheckbox(): void
  2908. {
  2909. $articles = $this->getTableLocator()->get('Articles');
  2910. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2911. $article = $articles->newEmptyEntity();
  2912. $this->Form->create($article);
  2913. $result = $this->Form->control('Articles.active');
  2914. $expected = [
  2915. 'div' => ['class' => 'input checkbox'],
  2916. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2917. 'label' => ['for' => 'articles-active'],
  2918. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2919. 'Active',
  2920. '/label',
  2921. '/div',
  2922. ];
  2923. $this->assertHtml($expected, $result);
  2924. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2925. $expected = [
  2926. 'div' => ['class' => 'input checkbox'],
  2927. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2928. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2929. '/div',
  2930. ];
  2931. $this->assertHtml($expected, $result);
  2932. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2933. $expected = [
  2934. 'div' => ['class' => 'input checkbox'],
  2935. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2936. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2937. '/div',
  2938. ];
  2939. $this->assertHtml($expected, $result);
  2940. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2941. $expected = [
  2942. 'div' => ['class' => 'input checkbox'],
  2943. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2944. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2945. '/div',
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->control('Articles.disabled', [
  2949. 'label' => 'Disabled',
  2950. 'type' => 'checkbox',
  2951. 'data-foo' => 'disabled',
  2952. ]);
  2953. $expected = [
  2954. 'div' => ['class' => 'input checkbox'],
  2955. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2956. 'label' => ['for' => 'articles-disabled'],
  2957. ['input' => [
  2958. 'type' => 'checkbox',
  2959. 'name' => 'Articles[disabled]',
  2960. 'value' => '1',
  2961. 'id' => 'articles-disabled',
  2962. 'data-foo' => 'disabled',
  2963. ]],
  2964. 'Disabled',
  2965. '/label',
  2966. '/div',
  2967. ];
  2968. $this->assertHtml($expected, $result);
  2969. $result = $this->Form->control('Articles.confirm', [
  2970. 'label' => 'Confirm <b>me</b>!',
  2971. 'type' => 'checkbox',
  2972. 'escape' => false,
  2973. ]);
  2974. $expected = [
  2975. 'div' => ['class' => 'input checkbox'],
  2976. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2977. 'label' => ['for' => 'articles-confirm'],
  2978. ['input' => [
  2979. 'type' => 'checkbox',
  2980. 'name' => 'Articles[confirm]',
  2981. 'value' => '1',
  2982. 'id' => 'articles-confirm',
  2983. ]],
  2984. 'Confirm <b>me</b>!',
  2985. '/label',
  2986. '/div',
  2987. ];
  2988. $this->assertHtml($expected, $result);
  2989. }
  2990. /**
  2991. * testControlHidden method
  2992. *
  2993. * Test that control() does not create wrapping div and label tag for hidden fields.
  2994. */
  2995. public function testControlHidden(): void
  2996. {
  2997. $this->getTableLocator()->get('ValidateUsers', [
  2998. 'className' => ValidateUsersTable::class,
  2999. ]);
  3000. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3001. $result = $this->Form->control('ValidateUser.id');
  3002. $expected = [
  3003. 'input' => ['name', 'type' => 'hidden', 'id'],
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3007. $expected = [
  3008. 'input' => ['name', 'type' => 'hidden', 'id'],
  3009. ];
  3010. $this->assertHtml($expected, $result);
  3011. }
  3012. /**
  3013. * testControlDatetime method
  3014. *
  3015. * Test form->control() with datetime.
  3016. */
  3017. public function testControlDatetime(): void
  3018. {
  3019. $result = $this->Form->control('prueba', [
  3020. 'type' => 'datetime',
  3021. 'value' => new DateTime('2019-09-27 02:52:43'),
  3022. ]);
  3023. $expected = [
  3024. 'div' => ['class' => 'input datetime'],
  3025. 'label' => ['for' => 'prueba'],
  3026. 'Prueba',
  3027. '/label',
  3028. 'input' => [
  3029. 'name' => 'prueba',
  3030. 'id' => 'prueba',
  3031. 'type' => 'datetime-local',
  3032. 'value' => '2019-09-27T02:52:43',
  3033. 'step' => '1',
  3034. ],
  3035. '/div',
  3036. ];
  3037. $this->assertHtml($expected, $result);
  3038. }
  3039. /**
  3040. * testControlDatetimeIdPrefix method
  3041. *
  3042. * Test form->control() with datetime with id prefix.
  3043. */
  3044. public function testControlDatetimeIdPrefix(): void
  3045. {
  3046. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3047. $result = $this->Form->control('prueba', [
  3048. 'type' => 'datetime',
  3049. ]);
  3050. $expected = [
  3051. 'div' => ['class' => 'input datetime'],
  3052. 'label' => ['for' => 'prefix-prueba'],
  3053. 'Prueba',
  3054. '/label',
  3055. 'input' => [
  3056. 'name' => 'prueba',
  3057. 'id' => 'prefix-prueba',
  3058. 'type' => 'datetime-local',
  3059. 'value' => '',
  3060. 'step' => '1',
  3061. ],
  3062. '/div',
  3063. ];
  3064. $this->assertHtml($expected, $result);
  3065. }
  3066. /**
  3067. * testControlDatetimeStep method
  3068. *
  3069. * Test form->control() with datetime with custom step size.
  3070. */
  3071. public function testControlDatetimeStep(): void
  3072. {
  3073. $result = $this->Form->control('prueba', [
  3074. 'type' => 'datetime',
  3075. 'value' => new DateTime('2019-09-27 02:52:43'),
  3076. 'step' => '0.5',
  3077. ]);
  3078. $expected = [
  3079. 'div' => ['class' => 'input datetime'],
  3080. 'label' => ['for' => 'prueba'],
  3081. 'Prueba',
  3082. '/label',
  3083. 'input' => [
  3084. 'name' => 'prueba',
  3085. 'id' => 'prueba',
  3086. 'type' => 'datetime-local',
  3087. 'value' => '2019-09-27T02:52:43.000',
  3088. 'step' => '0.5',
  3089. ],
  3090. '/div',
  3091. ];
  3092. $this->assertHtml($expected, $result);
  3093. }
  3094. /**
  3095. * testControlCheckboxWithDisabledElements method
  3096. *
  3097. * Test generating checkboxes with disabled elements.
  3098. */
  3099. public function testControlCheckboxWithDisabledElements(): void
  3100. {
  3101. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3102. $result = $this->Form->control('Contact.multiple', [
  3103. 'multiple' => 'checkbox',
  3104. 'disabled' => 'disabled',
  3105. 'options' => $options,
  3106. ]);
  3107. $expected = [
  3108. ['div' => ['class' => 'input select']],
  3109. ['label' => ['for' => 'contact-multiple']],
  3110. 'Multiple',
  3111. '/label',
  3112. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3113. ['div' => ['class' => 'checkbox']],
  3114. ['label' => ['for' => 'contact-multiple-1']],
  3115. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3116. 'One',
  3117. '/label',
  3118. '/div',
  3119. ['div' => ['class' => 'checkbox']],
  3120. ['label' => ['for' => 'contact-multiple-2']],
  3121. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3122. 'Two',
  3123. '/label',
  3124. '/div',
  3125. ['div' => ['class' => 'checkbox']],
  3126. ['label' => ['for' => 'contact-multiple-3']],
  3127. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3128. 'Three',
  3129. '/label',
  3130. '/div',
  3131. '/div',
  3132. ];
  3133. $this->assertHtml($expected, $result);
  3134. // make sure 50 does only disable 50, and not 50f5c0cf
  3135. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3136. $disabled = [50];
  3137. $expected = [
  3138. ['div' => ['class' => 'input select']],
  3139. ['label' => ['for' => 'contact-multiple']],
  3140. 'Multiple',
  3141. '/label',
  3142. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3143. ['div' => ['class' => 'checkbox']],
  3144. ['label' => ['for' => 'contact-multiple-50']],
  3145. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3146. 'Fifty',
  3147. '/label',
  3148. '/div',
  3149. ['div' => ['class' => 'checkbox']],
  3150. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3151. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3152. 'Stringy',
  3153. '/label',
  3154. '/div',
  3155. '/div',
  3156. ];
  3157. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3158. $this->assertHtml($expected, $result);
  3159. }
  3160. /**
  3161. * testControlWithLeadingInteger method
  3162. *
  3163. * Test input name with leading integer, ensure attributes are generated correctly.
  3164. */
  3165. public function testControlWithLeadingInteger(): void
  3166. {
  3167. $result = $this->Form->text('0.Node.title');
  3168. $expected = [
  3169. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3170. ];
  3171. $this->assertHtml($expected, $result);
  3172. }
  3173. /**
  3174. * testControlSelectType method
  3175. *
  3176. * Test form->control() with select type inputs.
  3177. */
  3178. public function testControlSelectType(): void
  3179. {
  3180. $result = $this->Form->control(
  3181. 'email',
  3182. [
  3183. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3184. );
  3185. $expected = [
  3186. 'div' => ['class' => 'input select'],
  3187. 'label' => ['for' => 'email'],
  3188. 'Email',
  3189. '/label',
  3190. ['select' => ['name' => 'email', 'id' => 'email']],
  3191. ['option' => ['value' => '']],
  3192. '/option',
  3193. ['option' => ['value' => 'è']],
  3194. 'Firést',
  3195. '/option',
  3196. ['option' => ['value' => 'é']],
  3197. 'Secoènd',
  3198. '/option',
  3199. '/select',
  3200. '/div',
  3201. ];
  3202. $this->assertHtml($expected, $result);
  3203. $result = $this->Form->control(
  3204. 'email',
  3205. [
  3206. 'options' => ['First', 'Second'], 'empty' => true]
  3207. );
  3208. $expected = [
  3209. 'div' => ['class' => 'input select'],
  3210. 'label' => ['for' => 'email'],
  3211. 'Email',
  3212. '/label',
  3213. ['select' => ['name' => 'email', 'id' => 'email']],
  3214. ['option' => ['value' => '']],
  3215. '/option',
  3216. ['option' => ['value' => '0']],
  3217. 'First',
  3218. '/option',
  3219. ['option' => ['value' => '1']],
  3220. 'Second',
  3221. '/option',
  3222. '/select',
  3223. '/div',
  3224. ];
  3225. $this->assertHtml($expected, $result);
  3226. $result = $this->Form->control('email', [
  3227. 'type' => 'select',
  3228. 'options' => new ArrayObject(['First', 'Second']),
  3229. 'empty' => true,
  3230. ]);
  3231. $this->assertHtml($expected, $result);
  3232. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3233. $this->View->setRequest(
  3234. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3235. );
  3236. $this->Form->create();
  3237. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3238. $expected = [
  3239. 'div' => ['class' => 'input select'],
  3240. 'label' => ['for' => 'model-user-id'],
  3241. 'User',
  3242. '/label',
  3243. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3244. ['option' => ['value' => '']],
  3245. '/option',
  3246. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3247. 'good',
  3248. '/option',
  3249. ['option' => ['value' => 'other']],
  3250. 'bad',
  3251. '/option',
  3252. '/select',
  3253. '/div',
  3254. ];
  3255. $this->assertHtml($expected, $result);
  3256. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3257. $this->View->setRequest(
  3258. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3259. );
  3260. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3261. $expected = [
  3262. 'div' => ['class' => 'input select'],
  3263. 'label' => ['for' => 'thing-user-id'],
  3264. 'User',
  3265. '/label',
  3266. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3267. ['option' => ['value' => '']],
  3268. 'Some Empty',
  3269. '/option',
  3270. ['option' => ['value' => 'value']],
  3271. 'good',
  3272. '/option',
  3273. ['option' => ['value' => 'other']],
  3274. 'bad',
  3275. '/option',
  3276. '/select',
  3277. '/div',
  3278. ];
  3279. $this->assertHtml($expected, $result);
  3280. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3281. $this->View->setRequest(
  3282. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3283. );
  3284. $this->Form->create();
  3285. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3286. $expected = [
  3287. 'div' => ['class' => 'input select'],
  3288. 'label' => ['for' => 'thing-user-id'],
  3289. 'User',
  3290. '/label',
  3291. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3292. ['option' => ['value' => '']],
  3293. 'Some Empty',
  3294. '/option',
  3295. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3296. 'good',
  3297. '/option',
  3298. ['option' => ['value' => 'other']],
  3299. 'bad',
  3300. '/option',
  3301. '/select',
  3302. '/div',
  3303. ];
  3304. $this->assertHtml($expected, $result);
  3305. $result = $this->Form->control('Publisher.id', [
  3306. 'label' => 'Publisher',
  3307. 'type' => 'select',
  3308. 'multiple' => 'checkbox',
  3309. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3310. ]);
  3311. $expected = [
  3312. ['div' => ['class' => 'input select']],
  3313. ['label' => ['for' => 'publisher-id']],
  3314. 'Publisher',
  3315. '/label',
  3316. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3317. ['div' => ['class' => 'checkbox']],
  3318. ['label' => ['for' => 'publisher-id-value-1']],
  3319. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3320. 'Label 1',
  3321. '/label',
  3322. '/div',
  3323. ['div' => ['class' => 'checkbox']],
  3324. ['label' => ['for' => 'publisher-id-value-2']],
  3325. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3326. 'Label 2',
  3327. '/label',
  3328. '/div',
  3329. '/div',
  3330. ];
  3331. $this->assertHtml($expected, $result);
  3332. $articlesTable = $this->getTableLocator()->get('Articles');
  3333. $articlesTable->getSchema()->setColumnType(
  3334. 'published',
  3335. EnumType::from(ArticleStatus::class)
  3336. );
  3337. $this->Form->create($articlesTable->newEmptyEntity());
  3338. $result = $this->Form->control('published');
  3339. $expected = [
  3340. 'div' => ['class' => 'input select'],
  3341. 'label' => ['for' => 'published'],
  3342. 'Published',
  3343. '/label',
  3344. 'select' => ['name' => 'published', 'id' => 'published'],
  3345. ['option' => ['value' => 'Y']],
  3346. 'PUBLISHED',
  3347. '/option',
  3348. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3349. 'UNPUBLISHED',
  3350. '/option',
  3351. '/select',
  3352. '/div',
  3353. ];
  3354. $this->assertHtml($expected, $result);
  3355. $articlesTable->getSchema()->setColumnType(
  3356. 'published',
  3357. EnumType::from(ArticleStatusLabel::class)
  3358. );
  3359. $this->Form->create($articlesTable->newEmptyEntity());
  3360. $result = $this->Form->control('published');
  3361. $expected = [
  3362. 'div' => ['class' => 'input select'],
  3363. 'label' => ['for' => 'published'],
  3364. 'Published',
  3365. '/label',
  3366. 'select' => ['name' => 'published', 'id' => 'published'],
  3367. ['option' => ['value' => 'Y']],
  3368. 'Is published',
  3369. '/option',
  3370. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3371. 'Is unpublished',
  3372. '/option',
  3373. '/select',
  3374. '/div',
  3375. ];
  3376. $this->assertHtml($expected, $result);
  3377. }
  3378. /**
  3379. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3380. *
  3381. * Test that control() and a non standard primary key makes a hidden input by default.
  3382. */
  3383. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3384. {
  3385. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3386. $this->Form->create($this->article);
  3387. $result = $this->Form->control('title');
  3388. $expected = [
  3389. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3390. ];
  3391. $this->assertHtml($expected, $result);
  3392. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3393. $this->Form->create($this->article);
  3394. $result = $this->Form->control('title');
  3395. $expected = [
  3396. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3397. ];
  3398. $this->assertHtml($expected, $result);
  3399. $result = $this->Form->control('body');
  3400. $expected = [
  3401. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3402. ];
  3403. $this->assertHtml($expected, $result);
  3404. }
  3405. /**
  3406. * testControlOverridingMagicSelectType method
  3407. *
  3408. * Test that overriding the magic select type widget is possible.
  3409. */
  3410. public function testControlOverridingMagicSelectType(): void
  3411. {
  3412. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3413. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3414. $expected = [
  3415. 'div' => ['class' => 'input text'],
  3416. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3417. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3418. '/div',
  3419. ];
  3420. $this->assertHtml($expected, $result);
  3421. //Check that magic types still work for plural/singular vars
  3422. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3423. $result = $this->Form->control('Model.type');
  3424. $expected = [
  3425. 'div' => ['class' => 'input select'],
  3426. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3427. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3428. ['option' => ['value' => 'value']], 'good', '/option',
  3429. ['option' => ['value' => 'other']], 'bad', '/option',
  3430. '/select',
  3431. '/div',
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. }
  3435. /**
  3436. * testControlMagicTypeDoesNotOverride method
  3437. *
  3438. * Test that inferred types do not override developer input.
  3439. */
  3440. public function testControlMagicTypeDoesNotOverride(): void
  3441. {
  3442. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3443. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3444. $expected = [
  3445. 'div' => ['class' => 'input checkbox'],
  3446. ['input' => [
  3447. 'type' => 'hidden',
  3448. 'name' => 'Model[user]',
  3449. 'value' => 0,
  3450. ]],
  3451. 'label' => ['for' => 'model-user'],
  3452. ['input' => [
  3453. 'name' => 'Model[user]',
  3454. 'type' => 'checkbox',
  3455. 'id' => 'model-user',
  3456. 'value' => 1,
  3457. ]],
  3458. 'User',
  3459. '/label',
  3460. '/div',
  3461. ];
  3462. $this->assertHtml($expected, $result);
  3463. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3464. $options = [
  3465. 1 => 'blue',
  3466. 2 => 'red',
  3467. ];
  3468. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3469. $expected = [
  3470. 'div' => ['class' => 'input select'],
  3471. 'label' => ['for' => 'tags-ids'],
  3472. 'Tags',
  3473. '/label',
  3474. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3475. ['div' => ['class' => 'checkbox']],
  3476. ['label' => ['for' => 'tags-ids-1']],
  3477. ['input' => [
  3478. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3479. 'value' => '1', 'name' => 'tags[_ids][]',
  3480. ]],
  3481. 'blue',
  3482. '/label',
  3483. '/div',
  3484. ['div' => ['class' => 'checkbox']],
  3485. ['label' => ['for' => 'tags-ids-2']],
  3486. ['input' => [
  3487. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3488. 'value' => '2', 'name' => 'tags[_ids][]',
  3489. ]],
  3490. 'red',
  3491. '/label',
  3492. '/div',
  3493. '/div',
  3494. ];
  3495. $this->assertHtml($expected, $result);
  3496. }
  3497. /**
  3498. * testControlMagicSelectForTypeNumber method
  3499. *
  3500. * Test that magic control() selects are created for type=number.
  3501. */
  3502. public function testControlMagicSelectForTypeNumber(): void
  3503. {
  3504. $this->getTableLocator()->get('ValidateUsers', [
  3505. 'className' => ValidateUsersTable::class,
  3506. ]);
  3507. $entity = new Entity(['balance' => 1]);
  3508. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3509. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3510. $result = $this->Form->control('balance');
  3511. $expected = [
  3512. 'div' => ['class' => 'input select'],
  3513. 'label' => ['for' => 'balance'],
  3514. 'Balance',
  3515. '/label',
  3516. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3517. ['option' => ['value' => '0']],
  3518. 'nothing',
  3519. '/option',
  3520. ['option' => ['value' => '1', 'selected' => 'selected']],
  3521. 'some',
  3522. '/option',
  3523. ['option' => ['value' => '100']],
  3524. 'a lot',
  3525. '/option',
  3526. '/select',
  3527. '/div',
  3528. ];
  3529. $this->assertHtml($expected, $result);
  3530. }
  3531. /**
  3532. * testInvalidControlTypeOption method
  3533. *
  3534. * Test invalid 'input' type option to control() function.
  3535. */
  3536. public function testInvalidControlTypeOption(): void
  3537. {
  3538. $this->expectException(InvalidArgumentException::class);
  3539. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3540. $this->Form->control('text', ['type' => 'input']);
  3541. }
  3542. /**
  3543. * testControlMagicSelectChangeToRadio method
  3544. *
  3545. * Test that magic control() selects can easily be converted into radio types without error.
  3546. */
  3547. public function testControlMagicSelectChangeToRadio(): void
  3548. {
  3549. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3550. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3551. $this->assertStringContainsString('input type="radio"', $result);
  3552. }
  3553. /**
  3554. * testFormControlSubmit method
  3555. *
  3556. * Test correct results for form::control() and type submit.
  3557. */
  3558. public function testFormControlSubmit(): void
  3559. {
  3560. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3561. $expected = [
  3562. 'div' => ['class' => 'submit'],
  3563. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3564. '/div',
  3565. ];
  3566. $this->assertHtml($expected, $result);
  3567. }
  3568. /**
  3569. * testFormControls method
  3570. *
  3571. * Test correct results from Form::controls().
  3572. */
  3573. public function testFormControlsLegendFieldset(): void
  3574. {
  3575. $this->Form->create($this->article);
  3576. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3577. $expected = [
  3578. '<fieldset',
  3579. '<legend',
  3580. 'The Legend',
  3581. '/legend',
  3582. '*/fieldset',
  3583. ];
  3584. $this->assertHtml($expected, $result);
  3585. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3586. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3587. $this->assertStringContainsString('<fieldset>', $result);
  3588. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3589. $this->assertStringNotContainsString('<legend>', $result);
  3590. $this->assertStringNotContainsString('<fieldset>', $result);
  3591. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3592. $this->assertStringNotContainsString('<legend>', $result);
  3593. $this->assertStringNotContainsString('<fieldset>', $result);
  3594. $this->Form->create($this->article);
  3595. $this->View->setRequest($this->View->getRequest()
  3596. ->withParam('prefix', 'admin')
  3597. ->withParam('action', 'admin_edit')
  3598. ->withParam('controller', 'articles'));
  3599. $result = $this->Form->allControls();
  3600. $expected = [
  3601. '<fieldset',
  3602. '<legend',
  3603. 'New Article',
  3604. '/legend',
  3605. '*/fieldset',
  3606. ];
  3607. $this->assertHtml($expected, $result);
  3608. $this->Form->create($this->article);
  3609. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3610. $expected = [
  3611. '<fieldset',
  3612. '<legend',
  3613. 'The Legend',
  3614. '/legend',
  3615. '*/fieldset',
  3616. ];
  3617. $this->assertHtml($expected, $result);
  3618. $this->Form->create($this->article);
  3619. $result = $this->Form->allControls([], [
  3620. 'fieldset' => [
  3621. 'class' => 'some-class some-other-class',
  3622. 'disabled' => true,
  3623. 'data-param' => 'a-param',
  3624. ],
  3625. 'legend' => 'The Legend',
  3626. ]);
  3627. $expected = [
  3628. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3629. '<legend',
  3630. 'The Legend',
  3631. '/legend',
  3632. '*/fieldset',
  3633. ];
  3634. $this->assertHtml($expected, $result);
  3635. }
  3636. /**
  3637. * testFormControls method
  3638. *
  3639. * Test the controls() method.
  3640. */
  3641. public function testFormControls(): void
  3642. {
  3643. $this->Form->create($this->article);
  3644. $result = $this->Form->allControls();
  3645. $expected = [
  3646. '<fieldset',
  3647. '<legend', 'New Article', '/legend',
  3648. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3649. ['div' => ['class' => 'input select required']],
  3650. '*/div',
  3651. ['div' => ['class' => 'input text required']],
  3652. '*/div',
  3653. ['div' => ['class' => 'input text']],
  3654. '*/div',
  3655. ['div' => ['class' => 'input text']],
  3656. '*/div',
  3657. '/fieldset',
  3658. ];
  3659. $this->assertHtml($expected, $result);
  3660. $result = $this->Form->allControls([
  3661. 'published' => ['type' => 'boolean'],
  3662. ]);
  3663. $expected = [
  3664. '<fieldset',
  3665. '<legend', 'New Article', '/legend',
  3666. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3667. ['div' => ['class' => 'input select required']],
  3668. '*/div',
  3669. ['div' => ['class' => 'input text required']],
  3670. '*/div',
  3671. ['div' => ['class' => 'input text']],
  3672. '*/div',
  3673. ['div' => ['class' => 'input boolean']],
  3674. '*/div',
  3675. '/fieldset',
  3676. ];
  3677. $this->assertHtml($expected, $result);
  3678. $this->Form->create($this->article);
  3679. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3680. $expected = [
  3681. 'fieldset' => [],
  3682. 'legend' => [],
  3683. 'Hello',
  3684. '/legend',
  3685. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3686. ['div' => ['class' => 'input select required']],
  3687. '*/div',
  3688. ['div' => ['class' => 'input text required']],
  3689. '*/div',
  3690. ['div' => ['class' => 'input text']],
  3691. '*/div',
  3692. ['div' => ['class' => 'input text']],
  3693. '*/div',
  3694. '/fieldset',
  3695. ];
  3696. $this->assertHtml($expected, $result);
  3697. $this->Form->create();
  3698. $expected = [
  3699. 'fieldset' => [],
  3700. ['div' => ['class' => 'input text']],
  3701. 'label' => ['for' => 'foo'],
  3702. 'Foo',
  3703. '/label',
  3704. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3705. '*/div',
  3706. '/fieldset',
  3707. ];
  3708. $result = $this->Form->allControls(
  3709. ['foo' => ['type' => 'text']],
  3710. ['legend' => false]
  3711. );
  3712. $this->assertHtml($expected, $result);
  3713. }
  3714. /**
  3715. * testFormControlsBlacklist method
  3716. */
  3717. public function testFormControlsBlacklist(): void
  3718. {
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([
  3721. 'id' => false,
  3722. ]);
  3723. $expected = [
  3724. '<fieldset',
  3725. '<legend', 'New Article', '/legend',
  3726. ['div' => ['class' => 'input select required']],
  3727. '*/div',
  3728. ['div' => ['class' => 'input text required']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text']],
  3733. '*/div',
  3734. '/fieldset',
  3735. ];
  3736. $this->assertHtml($expected, $result);
  3737. $this->Form->create($this->article);
  3738. $result = $this->Form->allControls([
  3739. 'id' => [],
  3740. ]);
  3741. $expected = [
  3742. '<fieldset',
  3743. '<legend', 'New Article', '/legend',
  3744. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3745. ['div' => ['class' => 'input select required']],
  3746. '*/div',
  3747. ['div' => ['class' => 'input text required']],
  3748. '*/div',
  3749. ['div' => ['class' => 'input text']],
  3750. '*/div',
  3751. ['div' => ['class' => 'input text']],
  3752. '*/div',
  3753. '/fieldset',
  3754. ];
  3755. $this->assertHtml($expected, $result, true);
  3756. }
  3757. /**
  3758. * testSelectAsCheckbox method
  3759. *
  3760. * Test multi-select widget with checkbox formatting.
  3761. */
  3762. public function testSelectAsCheckbox(): void
  3763. {
  3764. $result = $this->Form->select(
  3765. 'Model.multi_field',
  3766. ['first', 'second', 'third'],
  3767. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3768. );
  3769. $expected = [
  3770. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3771. ['div' => ['class' => 'checkbox']],
  3772. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3773. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3774. 'first',
  3775. '/label',
  3776. '/div',
  3777. ['div' => ['class' => 'checkbox']],
  3778. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3779. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3780. 'second',
  3781. '/label',
  3782. '/div',
  3783. ['div' => ['class' => 'checkbox']],
  3784. ['label' => ['for' => 'model-multi-field-2']],
  3785. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3786. 'third',
  3787. '/label',
  3788. '/div',
  3789. ];
  3790. $this->assertHtml($expected, $result);
  3791. $result = $this->Form->select(
  3792. 'Model.multi_field',
  3793. ['1/2' => 'half'],
  3794. ['multiple' => 'checkbox']
  3795. );
  3796. $expected = [
  3797. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3798. ['div' => ['class' => 'checkbox']],
  3799. ['label' => ['for' => 'model-multi-field-1-2']],
  3800. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3801. 'half',
  3802. '/label',
  3803. '/div',
  3804. ];
  3805. $this->assertHtml($expected, $result);
  3806. }
  3807. /**
  3808. * testLabel method
  3809. *
  3810. * Test label generation.
  3811. */
  3812. public function testLabel(): void
  3813. {
  3814. $result = $this->Form->label('Person.name');
  3815. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3816. $this->assertHtml($expected, $result);
  3817. $result = $this->Form->label('Person.name');
  3818. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3819. $this->assertHtml($expected, $result);
  3820. $result = $this->Form->label('Person.first_name');
  3821. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3822. $this->assertHtml($expected, $result);
  3823. $result = $this->Form->label('Person.first_name', 'Your first name');
  3824. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3825. $this->assertHtml($expected, $result);
  3826. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3827. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3828. $this->assertHtml($expected, $result);
  3829. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3830. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3831. $this->assertHtml($expected, $result);
  3832. $result = $this->Form->label('Person.first_name', '');
  3833. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3834. $this->assertHtml($expected, $result);
  3835. $result = $this->Form->label('Person.2.name', '');
  3836. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3837. $this->assertHtml($expected, $result);
  3838. }
  3839. /**
  3840. * testLabelContainControl method
  3841. *
  3842. * Test that label() can accept an input with the correct template vars.
  3843. */
  3844. public function testLabelContainControl(): void
  3845. {
  3846. $this->Form->setTemplates([
  3847. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3848. ]);
  3849. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3850. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3851. ]);
  3852. $expected = [
  3853. 'label' => ['for' => 'person-accept-terms'],
  3854. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3855. 'Accept',
  3856. '/label',
  3857. ];
  3858. $this->assertHtml($expected, $result);
  3859. }
  3860. /**
  3861. * testTextbox method
  3862. *
  3863. * Test textbox element generation.
  3864. */
  3865. public function testTextbox(): void
  3866. {
  3867. $result = $this->Form->text('Model.field');
  3868. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3869. $this->assertHtml($expected, $result);
  3870. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3871. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3872. $this->assertHtml($expected, $result);
  3873. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3874. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3875. $this->assertHtml($expected, $result);
  3876. }
  3877. /**
  3878. * testTextBoxDataAndError method
  3879. *
  3880. * Test that text() hooks up with request data and error fields.
  3881. */
  3882. public function testTextBoxDataAndError(): void
  3883. {
  3884. $this->article['errors'] = [
  3885. 'Contact' => ['text' => 'wrong'],
  3886. ];
  3887. $this->View->setRequest($this->View->getRequest()
  3888. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3889. ->withData('Contact.text', 'test'));
  3890. $this->Form->create($this->article);
  3891. $result = $this->Form->text('Model.text');
  3892. $expected = [
  3893. 'input' => [
  3894. 'type' => 'text',
  3895. 'name' => 'Model[text]',
  3896. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3897. ],
  3898. ];
  3899. $this->assertHtml($expected, $result);
  3900. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3901. $expected = [
  3902. 'input' => [
  3903. 'type' => 'text',
  3904. 'name' => 'Contact[text]',
  3905. 'value' => 'test',
  3906. 'id' => 'theID',
  3907. 'class' => 'form-error',
  3908. ],
  3909. ];
  3910. $this->assertHtml($expected, $result);
  3911. }
  3912. /**
  3913. * testDefaultValue method
  3914. *
  3915. * Test default value setting.
  3916. */
  3917. public function testTextDefaultValue(): void
  3918. {
  3919. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3920. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3921. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3922. $this->assertHtml($expected, $result);
  3923. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3924. $this->Form->create();
  3925. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3926. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3927. $this->assertHtml($expected, $result);
  3928. $Articles = $this->getTableLocator()->get('Articles');
  3929. $title = $Articles->getSchema()->getColumn('title');
  3930. $Articles->getSchema()->addColumn(
  3931. 'title',
  3932. ['default' => 'default title', 'length' => 255] + $title
  3933. );
  3934. $entity = $Articles->newEmptyEntity();
  3935. $this->Form->create($entity);
  3936. // Get default value from schema
  3937. $result = $this->Form->text('title');
  3938. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3939. $this->assertHtml($expected, $result);
  3940. // Don't get value from schema
  3941. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3942. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3943. $this->assertHtml($expected, $result);
  3944. // Custom default value overrides default value from schema
  3945. $result = $this->Form->text('title', ['default' => 'override default']);
  3946. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3947. $this->assertHtml($expected, $result);
  3948. // Default value from schema is used only for new entities.
  3949. $entity->setNew(false);
  3950. $result = $this->Form->text('title');
  3951. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3952. $this->assertHtml($expected, $result);
  3953. }
  3954. /**
  3955. * testError method
  3956. *
  3957. * Test field error generation.
  3958. */
  3959. public function testError(): void
  3960. {
  3961. $this->article['errors'] = [
  3962. 'Article' => ['field' => 'email'],
  3963. ];
  3964. $this->Form->create($this->article);
  3965. $result = $this->Form->error('Article.field');
  3966. $expected = [
  3967. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3968. 'email',
  3969. '/div',
  3970. ];
  3971. $this->assertHtml($expected, $result);
  3972. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3973. $expected = [
  3974. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3975. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3976. '/div',
  3977. ];
  3978. $this->assertHtml($expected, $result);
  3979. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3980. $expected = [
  3981. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3982. '<strong', 'Badness!', '/strong',
  3983. '/div',
  3984. ];
  3985. $this->assertHtml($expected, $result);
  3986. }
  3987. /**
  3988. * testErrorRuleName method
  3989. *
  3990. * Test error translation can use rule names for translating.
  3991. */
  3992. public function testErrorRuleName(): void
  3993. {
  3994. $this->article['errors'] = [
  3995. 'Article' => [
  3996. 'field' => ['email' => 'Your email was not good'],
  3997. ],
  3998. ];
  3999. $this->Form->create($this->article);
  4000. $result = $this->Form->error('Article.field');
  4001. $expected = [
  4002. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4003. 'Your email was not good',
  4004. '/div',
  4005. ];
  4006. $this->assertHtml($expected, $result);
  4007. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4008. $expected = [
  4009. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4010. 'Email in use',
  4011. '/div',
  4012. ];
  4013. $this->assertHtml($expected, $result);
  4014. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4015. $expected = [
  4016. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4017. 'Email in use',
  4018. '/div',
  4019. ];
  4020. $this->assertHtml($expected, $result);
  4021. $result = $this->Form->error('Article.field', [
  4022. 'email' => 'Key is preferred',
  4023. 'Your email was not good' => 'Email in use',
  4024. ]);
  4025. $expected = [
  4026. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4027. 'Key is preferred',
  4028. '/div',
  4029. ];
  4030. $this->assertHtml($expected, $result);
  4031. }
  4032. /**
  4033. * testErrorMessages method
  4034. *
  4035. * Test error with nested lists.
  4036. */
  4037. public function testErrorMessages(): void
  4038. {
  4039. $this->article['errors'] = [
  4040. 'Article' => ['field' => 'email'],
  4041. ];
  4042. $this->Form->create($this->article);
  4043. $result = $this->Form->error('Article.field', [
  4044. 'email' => 'No good!',
  4045. ]);
  4046. $expected = [
  4047. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4048. 'No good!',
  4049. '/div',
  4050. ];
  4051. $this->assertHtml($expected, $result);
  4052. }
  4053. /**
  4054. * testErrorMultipleMessages method
  4055. *
  4056. * Test error() with multiple messages.
  4057. */
  4058. public function testErrorMultipleMessages(): void
  4059. {
  4060. $this->article['errors'] = [
  4061. 'field' => ['notBlank', 'email', 'Something else'],
  4062. ];
  4063. $this->Form->create($this->article);
  4064. $result = $this->Form->error('field', [
  4065. 'notBlank' => 'Cannot be empty',
  4066. 'email' => 'No good!',
  4067. ]);
  4068. $expected = [
  4069. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4070. 'ul' => [],
  4071. '<li', 'Cannot be empty', '/li',
  4072. '<li', 'No good!', '/li',
  4073. '<li', 'Something else', '/li',
  4074. '/ul',
  4075. '/div',
  4076. ];
  4077. $this->assertHtml($expected, $result);
  4078. }
  4079. /**
  4080. * testPassword method
  4081. *
  4082. * Test password element generation.
  4083. */
  4084. public function testPassword(): void
  4085. {
  4086. $this->article['errors'] = [
  4087. 'Contact' => [
  4088. 'passwd' => 1,
  4089. ],
  4090. ];
  4091. $this->Form->create($this->article);
  4092. $result = $this->Form->password('Contact.field');
  4093. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4094. $this->assertHtml($expected, $result);
  4095. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4096. $this->Form->create($this->article);
  4097. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4098. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4099. $this->assertHtml($expected, $result);
  4100. }
  4101. /**
  4102. * testRadio method
  4103. *
  4104. * Test radio element set generation.
  4105. */
  4106. public function testRadio(): void
  4107. {
  4108. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4109. $expected = [
  4110. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4111. ['label' => ['for' => 'model-field-0']],
  4112. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4113. 'option A',
  4114. '/label',
  4115. ['label' => ['for' => 'model-field-1']],
  4116. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4117. 'option B',
  4118. '/label',
  4119. ];
  4120. $this->assertHtml($expected, $result);
  4121. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4122. $this->assertHtml($expected, $result);
  4123. $result = $this->Form->radio(
  4124. 'Employee.vegetarian',
  4125. ['yes' => 'Yes', 'no' => 'No'],
  4126. ['form' => 'my-form', 'id' => 'id-veg']
  4127. );
  4128. $expected = [
  4129. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4130. ['label' => ['for' => 'id-veg-yes']],
  4131. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4132. 'Yes',
  4133. '/label',
  4134. ['label' => ['for' => 'id-veg-no']],
  4135. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4136. 'No',
  4137. '/label',
  4138. ];
  4139. $this->assertHtml($expected, $result);
  4140. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4141. $expected = [
  4142. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4143. ['label' => ['for' => 'model-custom-0']],
  4144. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4145. 'option A',
  4146. '/label',
  4147. ['label' => ['for' => 'model-custom-1']],
  4148. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4149. 'option B',
  4150. '/label',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. $result = $this->Form->radio(
  4154. 'Employee.gender',
  4155. [
  4156. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4157. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4158. ]
  4159. );
  4160. $expected = [
  4161. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4162. ['label' => ['for' => 'employee-gender-male']],
  4163. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4164. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4165. 'Male',
  4166. '/label',
  4167. ['label' => ['for' => 'employee-gender-female']],
  4168. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4169. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4170. 'Female',
  4171. '/label',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. $article = new Article([
  4175. 'status' => ArticleStatus::UNPUBLISHED,
  4176. ]);
  4177. $this->Form->create($article);
  4178. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4179. $expected = [
  4180. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4181. ['label' => ['for' => 'status-y']],
  4182. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4183. 'Published',
  4184. '/label',
  4185. ['label' => ['for' => 'status-n']],
  4186. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4187. 'Unpublished',
  4188. '/label',
  4189. ];
  4190. $this->assertHtml($expected, $result);
  4191. }
  4192. /**
  4193. * Test radio with complex options and empty disabled data.
  4194. */
  4195. public function testRadioComplexDisabled(): void
  4196. {
  4197. $options = [
  4198. ['value' => 'r', 'text' => 'red'],
  4199. ['value' => 'b', 'text' => 'blue'],
  4200. ];
  4201. $attrs = ['disabled' => []];
  4202. $result = $this->Form->radio('Model.field', $options, $attrs);
  4203. $expected = [
  4204. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4205. ['label' => ['for' => 'model-field-r']],
  4206. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4207. 'red',
  4208. '/label',
  4209. ['label' => ['for' => 'model-field-b']],
  4210. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4211. 'blue',
  4212. '/label',
  4213. ];
  4214. $this->assertHtml($expected, $result);
  4215. $attrs = ['disabled' => ['r']];
  4216. $result = $this->Form->radio('Model.field', $options, $attrs);
  4217. $this->assertStringContainsString('disabled="disabled"', $result);
  4218. }
  4219. /**
  4220. * testRadioDefaultValue method
  4221. *
  4222. * Test default value setting on radio() method.
  4223. */
  4224. public function testRadioDefaultValue(): void
  4225. {
  4226. $Articles = $this->getTableLocator()->get('Articles');
  4227. $title = $Articles->getSchema()->getColumn('title');
  4228. $Articles->getSchema()->addColumn(
  4229. 'title',
  4230. ['default' => '1'] + $title
  4231. );
  4232. $this->Form->create($Articles->newEmptyEntity());
  4233. $result = $this->Form->radio('title', ['option A', 'option B']);
  4234. $expected = [
  4235. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4236. ['label' => ['for' => 'title-0']],
  4237. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4238. 'option A',
  4239. '/label',
  4240. ['label' => ['for' => 'title-1']],
  4241. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4242. 'option B',
  4243. '/label',
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. }
  4247. /**
  4248. * Test setting a hiddenField value on radio buttons.
  4249. */
  4250. public function testRadioHiddenFieldValue(): void
  4251. {
  4252. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4253. $expected = [
  4254. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4255. 'label' => ['for' => 'title-0'],
  4256. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4257. 'option A',
  4258. '/label',
  4259. ];
  4260. $this->assertHtml($expected, $result);
  4261. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4262. $expected = [
  4263. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4264. 'label' => ['for' => 'title-0'],
  4265. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4266. 'option A',
  4267. '/label',
  4268. ];
  4269. $this->assertHtml($expected, $result);
  4270. }
  4271. /**
  4272. * testControlRadio method
  4273. *
  4274. * Test that input works with radio types.
  4275. */
  4276. public function testControlRadio(): void
  4277. {
  4278. $result = $this->Form->control('test', [
  4279. 'type' => 'radio',
  4280. 'options' => ['A', 'B'],
  4281. ]);
  4282. $expected = [
  4283. ['div' => ['class' => 'input radio']],
  4284. '<label',
  4285. 'Test',
  4286. '/label',
  4287. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4288. ['label' => ['for' => 'test-0']],
  4289. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4290. 'A',
  4291. '/label',
  4292. ['label' => ['for' => 'test-1']],
  4293. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4294. 'B',
  4295. '/label',
  4296. '/div',
  4297. ];
  4298. $this->assertHtml($expected, $result);
  4299. $result = $this->Form->control('test', [
  4300. 'type' => 'radio',
  4301. 'options' => ['A', 'B'],
  4302. 'value' => '0',
  4303. ]);
  4304. $expected = [
  4305. ['div' => ['class' => 'input radio']],
  4306. '<label',
  4307. 'Test',
  4308. '/label',
  4309. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4310. ['label' => ['for' => 'test-0']],
  4311. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4312. 'A',
  4313. '/label',
  4314. ['label' => ['for' => 'test-1']],
  4315. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4316. 'B',
  4317. '/label',
  4318. '/div',
  4319. ];
  4320. $this->assertHtml($expected, $result);
  4321. $result = $this->Form->control('test', [
  4322. 'type' => 'radio',
  4323. 'options' => ['A', 'B'],
  4324. 'label' => false,
  4325. ]);
  4326. $expected = [
  4327. ['div' => ['class' => 'input radio']],
  4328. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4329. ['label' => ['for' => 'test-0']],
  4330. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4331. 'A',
  4332. '/label',
  4333. ['label' => ['for' => 'test-1']],
  4334. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4335. 'B',
  4336. '/label',
  4337. '/div',
  4338. ];
  4339. $this->assertHtml($expected, $result);
  4340. $result = $this->Form->control('accept', [
  4341. 'type' => 'radio',
  4342. 'options' => [
  4343. 1 => 'positive',
  4344. -1 => 'negative',
  4345. ],
  4346. 'label' => false,
  4347. ]);
  4348. $expected = [
  4349. ['div' => ['class' => 'input radio']],
  4350. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4351. ['label' => ['for' => 'accept-1']],
  4352. ['input' => [
  4353. 'type' => 'radio',
  4354. 'name' => 'accept',
  4355. 'value' => '1',
  4356. 'id' => 'accept-1',
  4357. ]],
  4358. 'positive',
  4359. '/label',
  4360. ['label' => ['for' => 'accept--1']],
  4361. ['input' => [
  4362. 'type' => 'radio',
  4363. 'name' => 'accept',
  4364. 'value' => '-1',
  4365. 'id' => 'accept--1',
  4366. ]],
  4367. 'negative',
  4368. '/label',
  4369. '/div',
  4370. ];
  4371. $this->assertHtml($expected, $result);
  4372. $articlesTable = $this->getTableLocator()->get('Articles');
  4373. $articlesTable->getSchema()->setColumnType(
  4374. 'published',
  4375. EnumType::from(ArticleStatus::class)
  4376. );
  4377. $this->Form->create($articlesTable->newEmptyEntity());
  4378. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4379. $expected = [
  4380. ['div' => ['class' => 'input radio']],
  4381. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4382. ['label' => ['for' => 'published-y']],
  4383. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4384. 'PUBLISHED',
  4385. '/label',
  4386. ['label' => ['for' => 'published-n']],
  4387. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4388. 'UNPUBLISHED',
  4389. '/label',
  4390. '/div',
  4391. ];
  4392. $this->assertHtml($expected, $result);
  4393. }
  4394. /**
  4395. * testRadioNoLabel method
  4396. *
  4397. * Test that radio() works with label = false.
  4398. */
  4399. public function testRadioNoLabel(): void
  4400. {
  4401. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4402. $expected = [
  4403. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4404. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4405. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4406. ];
  4407. $this->assertHtml($expected, $result);
  4408. }
  4409. /**
  4410. * testRadioControlInsideLabel method
  4411. *
  4412. * Test generating radio input inside label ala twitter bootstrap.
  4413. */
  4414. public function testRadioControlInsideLabel(): void
  4415. {
  4416. $this->Form->setTemplates([
  4417. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4418. 'radioWrapper' => '{{label}}',
  4419. ]);
  4420. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4421. // phpcs:disable
  4422. $expected = [
  4423. ['input' => [
  4424. 'type' => 'hidden',
  4425. 'name' => 'Model[field]',
  4426. 'value' => '',
  4427. 'id' => 'model-field'
  4428. ]],
  4429. ['label' => ['for' => 'model-field-0']],
  4430. ['input' => [
  4431. 'type' => 'radio',
  4432. 'name' => 'Model[field]',
  4433. 'value' => '0',
  4434. 'id' => 'model-field-0'
  4435. ]],
  4436. 'option A',
  4437. '/label',
  4438. ['label' => ['for' => 'model-field-1']],
  4439. ['input' => [
  4440. 'type' => 'radio',
  4441. 'name' => 'Model[field]',
  4442. 'value' => '1',
  4443. 'id' => 'model-field-1'
  4444. ]],
  4445. 'option B',
  4446. '/label',
  4447. ];
  4448. // phpcs:enable
  4449. $this->assertHtml($expected, $result);
  4450. }
  4451. /**
  4452. * testRadioHiddenControlDisabling method
  4453. *
  4454. * Test disabling the hidden input for radio buttons.
  4455. */
  4456. public function testRadioHiddenControlDisabling(): void
  4457. {
  4458. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4459. $expected = [
  4460. 'label' => ['for' => 'model-1-field-0'],
  4461. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4462. 'option A',
  4463. '/label',
  4464. ];
  4465. $this->assertHtml($expected, $result);
  4466. }
  4467. /**
  4468. * testRadioOutOfRange method
  4469. *
  4470. * Test radio element set generation.
  4471. */
  4472. public function testRadioOutOfRange(): void
  4473. {
  4474. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4475. $expected = [
  4476. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4477. 'label' => ['for' => 'model-field-v'],
  4478. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4479. 'value',
  4480. '/label',
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. }
  4484. /**
  4485. * testSelect method
  4486. *
  4487. * Test select element generation.
  4488. */
  4489. public function testSelect(): void
  4490. {
  4491. $result = $this->Form->select('Model.field', []);
  4492. $expected = [
  4493. 'select' => ['name' => 'Model[field]'],
  4494. '/select',
  4495. ];
  4496. $this->assertHtml($expected, $result);
  4497. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4498. $this->Form->create();
  4499. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4500. $expected = [
  4501. 'select' => ['name' => 'Model[field]'],
  4502. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4503. 'good',
  4504. '/option',
  4505. ['option' => ['value' => 'other']],
  4506. 'bad',
  4507. '/option',
  4508. '/select',
  4509. ];
  4510. $this->assertHtml($expected, $result);
  4511. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4512. $this->assertHtml($expected, $result);
  4513. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4514. $this->Form->create();
  4515. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4516. $expected = [
  4517. 'select' => ['name' => 'Model[field]'],
  4518. ['option' => ['value' => 'value']],
  4519. 'good',
  4520. '/option',
  4521. ['option' => ['value' => 'other']],
  4522. 'bad',
  4523. '/option',
  4524. '/select',
  4525. ];
  4526. $this->assertHtml($expected, $result);
  4527. $options = [
  4528. ['value' => 'first', 'text' => 'First'],
  4529. ['value' => 'first', 'text' => 'Another First'],
  4530. ];
  4531. $result = $this->Form->select(
  4532. 'Model.field',
  4533. $options,
  4534. ['escape' => false, 'empty' => false]
  4535. );
  4536. $expected = [
  4537. 'select' => ['name' => 'Model[field]'],
  4538. ['option' => ['value' => 'first']],
  4539. 'First',
  4540. '/option',
  4541. ['option' => ['value' => 'first']],
  4542. 'Another First',
  4543. '/option',
  4544. '/select',
  4545. ];
  4546. $this->assertHtml($expected, $result);
  4547. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4548. $this->Form->create();
  4549. $result = $this->Form->select(
  4550. 'Model.contact_id',
  4551. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4552. ['escape' => false, 'empty' => 'pick something']
  4553. );
  4554. $expected = [
  4555. 'select' => ['name' => 'Model[contact_id]'],
  4556. ['option' => ['value' => '']], 'pick something', '/option',
  4557. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4558. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4559. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4560. '/select',
  4561. ];
  4562. $this->assertHtml($expected, $result);
  4563. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4564. $this->Form->create();
  4565. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4566. $expected = [
  4567. 'select' => ['name' => 'Model[field]'],
  4568. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4569. ['option' => ['value' => '1']], 'Yes', '/option',
  4570. '/select',
  4571. ];
  4572. $this->assertHtml($expected, $result);
  4573. $article = new Article([
  4574. 'status' => ArticleStatus::UNPUBLISHED,
  4575. ]);
  4576. $this->Form->create($article);
  4577. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4578. $expected = [
  4579. 'select' => ['name' => 'status'],
  4580. ['option' => ['value' => 'Y']], 'Published', '/option',
  4581. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4582. '/select',
  4583. ];
  4584. $this->assertHtml($expected, $result);
  4585. }
  4586. /**
  4587. * testSelectEscapeHtml method
  4588. *
  4589. * Test that select() escapes HTML.
  4590. */
  4591. public function testSelectEscapeHtml(): void
  4592. {
  4593. $result = $this->Form->select(
  4594. 'Model.field',
  4595. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4596. ['empty' => false]
  4597. );
  4598. $expected = [
  4599. 'select' => ['name' => 'Model[field]'],
  4600. ['option' => ['value' => 'first']],
  4601. 'first &quot;html&quot; &lt;chars&gt;',
  4602. '/option',
  4603. ['option' => ['value' => 'second']],
  4604. 'value',
  4605. '/option',
  4606. '/select',
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. $result = $this->Form->select(
  4610. 'Model.field',
  4611. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4612. ['escape' => false, 'empty' => false]
  4613. );
  4614. $expected = [
  4615. 'select' => ['name' => 'Model[field]'],
  4616. ['option' => ['value' => 'first']],
  4617. 'first "html" <chars>',
  4618. '/option',
  4619. ['option' => ['value' => 'second']],
  4620. 'value',
  4621. '/option',
  4622. '/select',
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * testSelectRequired method
  4628. *
  4629. * Test select() with required and disabled attributes.
  4630. */
  4631. public function testSelectRequired(): void
  4632. {
  4633. $this->article['required'] = [
  4634. 'user_id' => true,
  4635. ];
  4636. $this->Form->create($this->article);
  4637. $result = $this->Form->select('user_id', ['option A']);
  4638. $expected = [
  4639. 'select' => [
  4640. 'name' => 'user_id',
  4641. 'required' => 'required',
  4642. ],
  4643. ['option' => ['value' => '0']], 'option A', '/option',
  4644. '/select',
  4645. ];
  4646. $this->assertHtml($expected, $result);
  4647. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4648. $expected = [
  4649. 'select' => [
  4650. 'name' => 'user_id',
  4651. 'disabled' => 'disabled',
  4652. ],
  4653. ['option' => ['value' => '0']], 'option A', '/option',
  4654. '/select',
  4655. ];
  4656. $this->assertHtml($expected, $result);
  4657. }
  4658. public function testSelectEmptyWithRequiredFalse(): void
  4659. {
  4660. $Articles = $this->getTableLocator()->get('Articles');
  4661. $validator = $Articles->getValidator('default');
  4662. $validator->allowEmptyString('user_id');
  4663. $Articles->setValidator('default', $validator);
  4664. $entity = $Articles->newEmptyEntity();
  4665. $this->Form->create($entity);
  4666. $result = $this->Form->select('user_id', ['option A']);
  4667. $expected = [
  4668. 'select' => [
  4669. 'name' => 'user_id',
  4670. ],
  4671. ['option' => ['value' => '']], '/option',
  4672. ['option' => ['value' => '0']], 'option A', '/option',
  4673. '/select',
  4674. ];
  4675. $this->assertHtml($expected, $result);
  4676. }
  4677. /**
  4678. * testNestedSelect method
  4679. *
  4680. * Test select element generation with optgroups.
  4681. */
  4682. public function testNestedSelect(): void
  4683. {
  4684. $result = $this->Form->select(
  4685. 'Model.field',
  4686. [1 => 'One', 2 => 'Two', 'Three' => [
  4687. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4688. ]],
  4689. ['empty' => false]
  4690. );
  4691. $expected = [
  4692. 'select' => ['name' => 'Model[field]'],
  4693. ['option' => ['value' => 1]],
  4694. 'One',
  4695. '/option',
  4696. ['option' => ['value' => 2]],
  4697. 'Two',
  4698. '/option',
  4699. ['optgroup' => ['label' => 'Three']],
  4700. ['option' => ['value' => 3]],
  4701. 'Three',
  4702. '/option',
  4703. ['option' => ['value' => 4]],
  4704. 'Four',
  4705. '/option',
  4706. ['option' => ['value' => 5]],
  4707. 'Five',
  4708. '/option',
  4709. '/optgroup',
  4710. '/select',
  4711. ];
  4712. $this->assertHtml($expected, $result);
  4713. }
  4714. /**
  4715. * testSelectMultiple method
  4716. *
  4717. * Test generation of multiple select elements.
  4718. */
  4719. public function testSelectMultiple(): void
  4720. {
  4721. $options = ['first', 'second', 'third'];
  4722. $result = $this->Form->select(
  4723. 'Model.multi_field',
  4724. $options,
  4725. ['form' => 'my-form', 'multiple' => true]
  4726. );
  4727. $expected = [
  4728. 'input' => [
  4729. 'type' => 'hidden',
  4730. 'name' => 'Model[multi_field]',
  4731. 'value' => '',
  4732. 'form' => 'my-form',
  4733. ],
  4734. 'select' => [
  4735. 'name' => 'Model[multi_field][]',
  4736. 'multiple' => 'multiple',
  4737. 'form' => 'my-form',
  4738. ],
  4739. ['option' => ['value' => '0']],
  4740. 'first',
  4741. '/option',
  4742. ['option' => ['value' => '1']],
  4743. 'second',
  4744. '/option',
  4745. ['option' => ['value' => '2']],
  4746. 'third',
  4747. '/option',
  4748. '/select',
  4749. ];
  4750. $this->assertHtml($expected, $result);
  4751. $result = $this->Form->select(
  4752. 'Model.multi_field',
  4753. $options,
  4754. ['multiple' => 'multiple', 'form' => 'my-form']
  4755. );
  4756. $this->assertHtml($expected, $result);
  4757. $result = $this->Form->select(
  4758. 'Model.multi_field',
  4759. $options,
  4760. ['form' => 'my-form', 'multiple' => false]
  4761. );
  4762. $this->assertStringNotContainsString('multiple', $result);
  4763. }
  4764. /**
  4765. * testCheckboxZeroValue method
  4766. *
  4767. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4768. */
  4769. public function testCheckboxZeroValue(): void
  4770. {
  4771. $result = $this->Form->control('User.get_spam', [
  4772. 'type' => 'checkbox',
  4773. 'value' => '0',
  4774. 'hiddenField' => '1',
  4775. ]);
  4776. $expected = [
  4777. 'div' => ['class' => 'input checkbox'],
  4778. 'label' => ['for' => 'user-get-spam'],
  4779. ['input' => [
  4780. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4781. 'value' => '1',
  4782. ]],
  4783. ['input' => [
  4784. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4785. 'value' => '0', 'id' => 'user-get-spam',
  4786. ]],
  4787. 'Get Spam',
  4788. '/label',
  4789. '/div',
  4790. ];
  4791. $this->assertHtml($expected, $result);
  4792. $result = $this->Form->control('User.get_spam', [
  4793. 'type' => 'checkbox',
  4794. 'value' => '0',
  4795. 'hiddenField' => '',
  4796. ]);
  4797. $expected = [
  4798. 'div' => ['class' => 'input checkbox'],
  4799. 'label' => ['for' => 'user-get-spam'],
  4800. ['input' => [
  4801. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4802. 'value' => '',
  4803. ]],
  4804. ['input' => [
  4805. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4806. 'value' => '0', 'id' => 'user-get-spam',
  4807. ]],
  4808. 'Get Spam',
  4809. '/label',
  4810. '/div',
  4811. ];
  4812. $this->assertHtml($expected, $result);
  4813. }
  4814. /**
  4815. * testHabtmSelectBox method
  4816. *
  4817. * Test generation of habtm select boxes.
  4818. */
  4819. public function testHabtmSelectBox(): void
  4820. {
  4821. $options = [
  4822. 1 => 'blue',
  4823. 2 => 'red',
  4824. 3 => 'green',
  4825. ];
  4826. $tags = [
  4827. new Entity(['id' => 1, 'name' => 'blue']),
  4828. new Entity(['id' => 3, 'name' => 'green']),
  4829. ];
  4830. $article = new Article(['tags' => $tags]);
  4831. $this->Form->create($article);
  4832. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4833. $expected = [
  4834. 'div' => ['class' => 'input select'],
  4835. 'label' => ['for' => 'tags-ids'],
  4836. 'Tags',
  4837. '/label',
  4838. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4839. 'select' => [
  4840. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4841. 'multiple' => 'multiple',
  4842. ],
  4843. ['option' => ['value' => '1', 'selected' => 'selected']],
  4844. 'blue',
  4845. '/option',
  4846. ['option' => ['value' => '2']],
  4847. 'red',
  4848. '/option',
  4849. ['option' => ['value' => '3', 'selected' => 'selected']],
  4850. 'green',
  4851. '/option',
  4852. '/select',
  4853. '/div',
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. // make sure only 50 is selected, and not 50f5c0cf
  4857. $options = [
  4858. '1' => 'blue',
  4859. '50f5c0cf' => 'red',
  4860. '50' => 'green',
  4861. ];
  4862. $tags = [
  4863. new Entity(['id' => 1, 'name' => 'blue']),
  4864. new Entity(['id' => 50, 'name' => 'green']),
  4865. ];
  4866. $article = new Article(['tags' => $tags]);
  4867. $this->Form->create($article);
  4868. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4869. $expected = [
  4870. 'div' => ['class' => 'input select'],
  4871. 'label' => ['for' => 'tags-ids'],
  4872. 'Tags',
  4873. '/label',
  4874. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4875. 'select' => [
  4876. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4877. 'multiple' => 'multiple',
  4878. ],
  4879. ['option' => ['value' => '1', 'selected' => 'selected']],
  4880. 'blue',
  4881. '/option',
  4882. ['option' => ['value' => '50f5c0cf']],
  4883. 'red',
  4884. '/option',
  4885. ['option' => ['value' => '50', 'selected' => 'selected']],
  4886. 'green',
  4887. '/option',
  4888. '/select',
  4889. '/div',
  4890. ];
  4891. $this->assertHtml($expected, $result);
  4892. $spacecraft = [
  4893. 1 => 'Orion',
  4894. 2 => 'Helios',
  4895. ];
  4896. $this->View->set('spacecraft', $spacecraft);
  4897. $this->Form->create();
  4898. $result = $this->Form->control('spacecraft._ids');
  4899. $expected = [
  4900. 'div' => ['class' => 'input select'],
  4901. 'label' => ['for' => 'spacecraft-ids'],
  4902. 'Spacecraft',
  4903. '/label',
  4904. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4905. 'select' => [
  4906. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4907. 'multiple' => 'multiple',
  4908. ],
  4909. ['option' => ['value' => '1']],
  4910. 'Orion',
  4911. '/option',
  4912. ['option' => ['value' => '2']],
  4913. 'Helios',
  4914. '/option',
  4915. '/select',
  4916. '/div',
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. }
  4920. /**
  4921. * testErrorsForBelongsToManySelect method
  4922. *
  4923. * Tests that errors for belongsToMany select fields are being
  4924. * picked up properly.
  4925. */
  4926. public function testErrorsForBelongsToManySelect(): void
  4927. {
  4928. $spacecraft = [
  4929. 1 => 'Orion',
  4930. 2 => 'Helios',
  4931. ];
  4932. $this->View->set('spacecraft', $spacecraft);
  4933. $article = new Article();
  4934. $article->setError('spacecraft', ['Invalid']);
  4935. $this->Form->create($article);
  4936. $result = $this->Form->control('spacecraft._ids');
  4937. $expected = [
  4938. ['div' => ['class' => 'input select error']],
  4939. 'label' => ['for' => 'spacecraft-ids'],
  4940. 'Spacecraft',
  4941. '/label',
  4942. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4943. 'select' => [
  4944. 'name' => 'spacecraft[_ids][]',
  4945. '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' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4956. 'Invalid',
  4957. '/div',
  4958. '/div',
  4959. ];
  4960. $this->assertHtml($expected, $result);
  4961. }
  4962. /**
  4963. * testSelectMultipleCheckboxes method
  4964. *
  4965. * Test generation of multi select elements in checkbox format.
  4966. */
  4967. public function testSelectMultipleCheckboxes(): void
  4968. {
  4969. $result = $this->Form->select(
  4970. 'Model.multi_field',
  4971. ['first', 'second', 'third'],
  4972. ['multiple' => 'checkbox']
  4973. );
  4974. $expected = [
  4975. 'input' => [
  4976. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4977. ],
  4978. ['div' => ['class' => 'checkbox']],
  4979. ['label' => ['for' => 'model-multi-field-0']],
  4980. ['input' => [
  4981. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4982. 'value' => '0', 'id' => 'model-multi-field-0',
  4983. ]],
  4984. 'first',
  4985. '/label',
  4986. '/div',
  4987. ['div' => ['class' => 'checkbox']],
  4988. ['label' => ['for' => 'model-multi-field-1']],
  4989. ['input' => [
  4990. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4991. 'value' => '1', 'id' => 'model-multi-field-1',
  4992. ]],
  4993. 'second',
  4994. '/label',
  4995. '/div',
  4996. ['div' => ['class' => 'checkbox']],
  4997. ['label' => ['for' => 'model-multi-field-2']],
  4998. ['input' => [
  4999. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5000. 'value' => '2', 'id' => 'model-multi-field-2',
  5001. ]],
  5002. 'third',
  5003. '/label',
  5004. '/div',
  5005. ];
  5006. $this->assertHtml($expected, $result);
  5007. $result = $this->Form->select(
  5008. 'Model.multi_field',
  5009. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5010. ['multiple' => 'checkbox']
  5011. );
  5012. $expected = [
  5013. 'input' => [
  5014. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5015. ],
  5016. ['div' => ['class' => 'checkbox']],
  5017. ['label' => ['for' => 'model-multi-field-a+']],
  5018. ['input' => [
  5019. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5020. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5021. ]],
  5022. 'first',
  5023. '/label',
  5024. '/div',
  5025. ['div' => ['class' => 'checkbox']],
  5026. ['label' => ['for' => 'model-multi-field-a++']],
  5027. ['input' => [
  5028. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5029. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5030. ]],
  5031. 'second',
  5032. '/label',
  5033. '/div',
  5034. ['div' => ['class' => 'checkbox']],
  5035. ['label' => ['for' => 'model-multi-field-a+++']],
  5036. ['input' => [
  5037. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5038. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5039. ]],
  5040. 'third',
  5041. '/label',
  5042. '/div',
  5043. ];
  5044. $this->assertHtml($expected, $result);
  5045. $result = $this->Form->select(
  5046. 'Model.multi_field',
  5047. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5048. ['multiple' => 'checkbox']
  5049. );
  5050. $expected = [
  5051. 'input' => [
  5052. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5053. ],
  5054. ['div' => ['class' => 'checkbox']],
  5055. ['label' => ['for' => 'model-multi-field-a-b']],
  5056. ['input' => [
  5057. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5058. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5059. ]],
  5060. 'first',
  5061. '/label',
  5062. '/div',
  5063. ['div' => ['class' => 'checkbox']],
  5064. ['label' => ['for' => 'model-multi-field-a-b1']],
  5065. ['input' => [
  5066. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5067. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5068. ]],
  5069. 'second',
  5070. '/label',
  5071. '/div',
  5072. ['div' => ['class' => 'checkbox']],
  5073. ['label' => ['for' => 'model-multi-field-a-b2']],
  5074. ['input' => [
  5075. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5076. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5077. ]],
  5078. 'third',
  5079. '/label',
  5080. '/div',
  5081. ];
  5082. $this->assertHtml($expected, $result);
  5083. }
  5084. /**
  5085. * testSelectMultipleCheckboxRequestData method
  5086. *
  5087. * Ensure that multiCheckbox reads from the request data.
  5088. */
  5089. public function testSelectMultipleCheckboxRequestData(): void
  5090. {
  5091. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5092. $result = $this->Form->select(
  5093. 'Model.tags',
  5094. ['1' => 'first', 'Array' => 'Array'],
  5095. ['multiple' => 'checkbox']
  5096. );
  5097. $expected = [
  5098. 'input' => [
  5099. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5100. ],
  5101. ['div' => ['class' => 'checkbox']],
  5102. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5103. ['input' => [
  5104. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5105. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5106. ]],
  5107. 'first',
  5108. '/label',
  5109. '/div',
  5110. ['div' => ['class' => 'checkbox']],
  5111. ['label' => ['for' => 'model-tags-array']],
  5112. ['input' => [
  5113. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5114. 'value' => 'Array', 'id' => 'model-tags-array',
  5115. ]],
  5116. 'Array',
  5117. '/label',
  5118. '/div',
  5119. ];
  5120. $this->assertHtml($expected, $result);
  5121. }
  5122. /**
  5123. * testSelectMultipleCheckboxSecurity method
  5124. *
  5125. * Checks the security hash array generated for multiple-input checkbox elements.
  5126. */
  5127. public function testSelectMultipleCheckboxSecurity(): void
  5128. {
  5129. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5130. $this->Form->create();
  5131. $this->Form->select(
  5132. 'Model.multi_field',
  5133. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5134. ['multiple' => 'checkbox']
  5135. );
  5136. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5137. $this->assertEquals(['Model.multi_field'], $fields);
  5138. $result = $this->Form->secure();
  5139. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5140. $hash = urlencode($hash . ':');
  5141. $this->assertStringContainsString('"' . $hash . '"', $result);
  5142. }
  5143. /**
  5144. * testSelectMultipleSecureWithNoOptions method
  5145. *
  5146. * Multiple select elements should always be secured as they always participate
  5147. * in the POST data.
  5148. */
  5149. public function testSelectMultipleSecureWithNoOptions(): void
  5150. {
  5151. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5152. $this->Form->create();
  5153. $this->Form->select(
  5154. 'Model.select',
  5155. [],
  5156. ['multiple' => true]
  5157. );
  5158. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5159. $this->assertEquals(['Model.select'], $result);
  5160. }
  5161. /**
  5162. * testSelectNoSecureWithNoOptions method
  5163. *
  5164. * When a select box has no options it should not be added to the fields list
  5165. * as it always fail post validation.
  5166. */
  5167. public function testSelectNoSecureWithNoOptions(): void
  5168. {
  5169. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5170. $this->Form->create();
  5171. $this->Form->select(
  5172. 'Model.select',
  5173. []
  5174. );
  5175. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5176. $this->assertEquals([], $result);
  5177. $this->Form->select(
  5178. 'Model.user_id',
  5179. [],
  5180. ['empty' => true]
  5181. );
  5182. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5183. $this->assertEquals(['Model.user_id'], $result);
  5184. }
  5185. /**
  5186. * testControlMultipleCheckboxes method
  5187. *
  5188. * Test control() resulting in multi select elements being generated.
  5189. */
  5190. public function testControlMultipleCheckboxes(): void
  5191. {
  5192. $result = $this->Form->control('Model.multi_field', [
  5193. 'options' => ['first', 'second', 'third'],
  5194. 'multiple' => 'checkbox',
  5195. ]);
  5196. $expected = [
  5197. ['div' => ['class' => 'input select']],
  5198. ['label' => ['for' => 'model-multi-field']],
  5199. 'Multi Field',
  5200. '/label',
  5201. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5202. ['div' => ['class' => 'checkbox']],
  5203. ['label' => ['for' => 'model-multi-field-0']],
  5204. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5205. 'first',
  5206. '/label',
  5207. '/div',
  5208. ['div' => ['class' => 'checkbox']],
  5209. ['label' => ['for' => 'model-multi-field-1']],
  5210. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5211. 'second',
  5212. '/label',
  5213. '/div',
  5214. ['div' => ['class' => 'checkbox']],
  5215. ['label' => ['for' => 'model-multi-field-2']],
  5216. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5217. 'third',
  5218. '/label',
  5219. '/div',
  5220. '/div',
  5221. ];
  5222. $this->assertHtml($expected, $result);
  5223. $result = $this->Form->control('Model.multi_field', [
  5224. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5225. 'multiple' => 'checkbox',
  5226. ]);
  5227. $expected = [
  5228. ['div' => ['class' => 'input select']],
  5229. ['label' => ['for' => 'model-multi-field']],
  5230. 'Multi Field',
  5231. '/label',
  5232. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5233. ['div' => ['class' => 'checkbox']],
  5234. ['label' => ['for' => 'model-multi-field-a']],
  5235. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5236. 'first',
  5237. '/label',
  5238. '/div',
  5239. ['div' => ['class' => 'checkbox']],
  5240. ['label' => ['for' => 'model-multi-field-b']],
  5241. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5242. 'second',
  5243. '/label',
  5244. '/div',
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-c']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5248. 'third',
  5249. '/label',
  5250. '/div',
  5251. '/div',
  5252. ];
  5253. $this->assertHtml($expected, $result);
  5254. }
  5255. /**
  5256. * testSelectHiddenFieldOmission method
  5257. *
  5258. * Test that select() with 'hiddenField' => false omits the hidden field.
  5259. */
  5260. public function testSelectHiddenFieldOmission(): void
  5261. {
  5262. $result = $this->Form->select(
  5263. 'Model.multi_field',
  5264. ['first', 'second'],
  5265. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5266. );
  5267. $this->assertStringNotContainsString('type="hidden"', $result);
  5268. }
  5269. /**
  5270. * testSelectCheckboxMultipleOverrideName method
  5271. *
  5272. * Test that select() with multiple = checkbox works with overriding name attribute.
  5273. */
  5274. public function testSelectCheckboxMultipleOverrideName(): void
  5275. {
  5276. $result = $this->Form->select('category', ['1', '2'], [
  5277. 'multiple' => 'checkbox',
  5278. 'name' => 'fish',
  5279. ]);
  5280. $expected = [
  5281. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5282. ['div' => ['class' => 'checkbox']],
  5283. ['label' => ['for' => 'fish-0']],
  5284. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5285. '1',
  5286. '/label',
  5287. '/div',
  5288. ['div' => ['class' => 'checkbox']],
  5289. ['label' => ['for' => 'fish-1']],
  5290. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5291. '2',
  5292. '/label',
  5293. '/div',
  5294. ];
  5295. $this->assertHtml($expected, $result);
  5296. $result = $this->Form->multiCheckbox(
  5297. 'category',
  5298. new Collection(['1', '2']),
  5299. ['name' => 'fish']
  5300. );
  5301. $this->assertHtml($expected, $result);
  5302. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5303. 'name' => 'fish',
  5304. ]);
  5305. $this->assertHtml($expected, $result);
  5306. }
  5307. /**
  5308. * testSelectCheckboxMultipleOverrideName method
  5309. *
  5310. * Test that select() with multiple = checkbox works with overriding name attribute.
  5311. */
  5312. public function testSelectCheckboxMultipleCustomId(): void
  5313. {
  5314. $result = $this->Form->select('category', ['1', '2'], [
  5315. 'multiple' => 'checkbox',
  5316. 'id' => 'cat',
  5317. ]);
  5318. $expected = [
  5319. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5320. ['div' => ['class' => 'checkbox']],
  5321. ['label' => ['for' => 'cat-0']],
  5322. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5323. '1',
  5324. '/label',
  5325. '/div',
  5326. ['div' => ['class' => 'checkbox']],
  5327. ['label' => ['for' => 'cat-1']],
  5328. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5329. '2',
  5330. '/label',
  5331. '/div',
  5332. ];
  5333. $this->assertHtml($expected, $result);
  5334. $result = $this->Form->multiCheckbox(
  5335. 'category',
  5336. ['1', '2'],
  5337. ['id' => 'cat']
  5338. );
  5339. $this->assertHtml($expected, $result);
  5340. }
  5341. /**
  5342. * testControlMultiCheckbox method
  5343. *
  5344. * Test that control() works with multicheckbox.
  5345. */
  5346. public function testControlMultiCheckbox(): void
  5347. {
  5348. $result = $this->Form->control('category', [
  5349. 'type' => 'multicheckbox',
  5350. 'options' => ['1', '2'],
  5351. ]);
  5352. $expected = [
  5353. ['div' => ['class' => 'input multicheckbox']],
  5354. '<label',
  5355. 'Category',
  5356. '/label',
  5357. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5358. ['div' => ['class' => 'checkbox']],
  5359. ['label' => ['for' => 'category-0']],
  5360. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5361. '1',
  5362. '/label',
  5363. '/div',
  5364. ['div' => ['class' => 'checkbox']],
  5365. ['label' => ['for' => 'category-1']],
  5366. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5367. '2',
  5368. '/label',
  5369. '/div',
  5370. '/div',
  5371. ];
  5372. $this->assertHtml($expected, $result);
  5373. }
  5374. /**
  5375. * testCheckbox method
  5376. *
  5377. * Test generation of checkboxes.
  5378. */
  5379. public function testCheckbox(): void
  5380. {
  5381. $result = $this->Form->checkbox('Model.field');
  5382. $expected = [
  5383. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5384. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5385. ];
  5386. $this->assertHtml($expected, $result);
  5387. $result = $this->Form->checkbox('Model.field', [
  5388. 'id' => 'theID',
  5389. 'value' => 'myvalue',
  5390. 'form' => 'my-form',
  5391. ]);
  5392. $expected = [
  5393. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5394. ['input' => [
  5395. 'type' => 'checkbox', 'name' => 'Model[field]',
  5396. 'value' => 'myvalue', 'id' => 'theID',
  5397. 'form' => 'my-form',
  5398. ]],
  5399. ];
  5400. $this->assertHtml($expected, $result);
  5401. }
  5402. /**
  5403. * testCheckboxDefaultValue method
  5404. *
  5405. * Test default value setting on checkbox() method.
  5406. */
  5407. public function testCheckboxDefaultValue(): void
  5408. {
  5409. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5410. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5411. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5412. $this->assertHtml($expected, $result);
  5413. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5414. $this->Form->create();
  5415. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5416. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5417. $this->assertHtml($expected, $result);
  5418. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5419. $this->Form->create();
  5420. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5421. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5422. $this->assertHtml($expected, $result);
  5423. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5424. $this->Form->create();
  5425. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5426. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5427. $this->assertHtml($expected, $result);
  5428. $Articles = $this->getTableLocator()->get('Articles');
  5429. $Articles->getSchema()->addColumn(
  5430. 'published',
  5431. ['type' => 'boolean', 'null' => false, 'default' => true]
  5432. );
  5433. $this->Form->create($Articles->newEmptyEntity());
  5434. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5435. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5436. $this->assertHtml($expected, $result);
  5437. }
  5438. /**
  5439. * testCheckboxCheckedAndError method
  5440. *
  5441. * Test checkbox being checked or having errors.
  5442. */
  5443. public function testCheckboxCheckedAndError(): void
  5444. {
  5445. $this->article['errors'] = [
  5446. 'published' => true,
  5447. ];
  5448. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5449. $this->Form->create($this->article);
  5450. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5451. $expected = [
  5452. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5453. ['input' => [
  5454. 'type' => 'checkbox',
  5455. 'name' => 'published',
  5456. 'value' => 'myvalue',
  5457. 'id' => 'theID',
  5458. 'checked' => 'checked',
  5459. 'class' => 'form-error',
  5460. ]],
  5461. ];
  5462. $this->assertHtml($expected, $result);
  5463. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5464. $this->Form->create($this->article);
  5465. $result = $this->Form->checkbox('published');
  5466. $expected = [
  5467. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5468. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5469. ];
  5470. $this->assertHtml($expected, $result);
  5471. }
  5472. /**
  5473. * testCheckboxCustomNameAttribute method
  5474. *
  5475. * Test checkbox() with a custom name attribute.
  5476. */
  5477. public function testCheckboxCustomNameAttribute(): void
  5478. {
  5479. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5480. $expected = [
  5481. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5482. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5483. ];
  5484. $this->assertHtml($expected, $result);
  5485. }
  5486. /**
  5487. * testCheckboxHiddenField method
  5488. *
  5489. * Test that the hidden input for checkboxes can be omitted or set to a
  5490. * specific value.
  5491. */
  5492. public function testCheckboxHiddenField(): void
  5493. {
  5494. $result = $this->Form->checkbox('UserForm.something', [
  5495. 'hiddenField' => false,
  5496. ]);
  5497. $expected = [
  5498. 'input' => [
  5499. 'type' => 'checkbox',
  5500. 'name' => 'UserForm[something]',
  5501. 'value' => '1',
  5502. ],
  5503. ];
  5504. $this->assertHtml($expected, $result);
  5505. $result = $this->Form->checkbox('UserForm.something', [
  5506. 'value' => 'Y',
  5507. 'hiddenField' => '',
  5508. ]);
  5509. $expected = [
  5510. ['input' => [
  5511. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5512. 'value' => '',
  5513. ]],
  5514. ['input' => [
  5515. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5516. 'value' => 'Y',
  5517. ]],
  5518. ];
  5519. $this->assertHtml($expected, $result);
  5520. $result = $this->Form->checkbox('UserForm.something', [
  5521. 'value' => 'Y',
  5522. 'hiddenField' => 'N',
  5523. ]);
  5524. $expected = [
  5525. ['input' => [
  5526. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5527. 'value' => 'N',
  5528. ]],
  5529. ['input' => [
  5530. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5531. 'value' => 'Y',
  5532. ]],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. }
  5536. /**
  5537. * testTime method
  5538. *
  5539. * Test the time type.
  5540. */
  5541. public function testTime(): void
  5542. {
  5543. $result = $this->Form->time('start_time', [
  5544. 'value' => '2014-03-08 16:30:00',
  5545. ]);
  5546. $expected = [
  5547. 'input' => [
  5548. 'type' => 'time',
  5549. 'name' => 'start_time',
  5550. 'value' => '16:30:00',
  5551. 'step' => '1',
  5552. ],
  5553. ];
  5554. $this->assertHtml($expected, $result);
  5555. $result = $this->Form->time('start_time', [
  5556. 'value' => new ChronosTime('16:30:00'),
  5557. ]);
  5558. $expected = [
  5559. 'input' => [
  5560. 'type' => 'time',
  5561. 'name' => 'start_time',
  5562. 'value' => '16:30:00',
  5563. 'step' => '1',
  5564. ],
  5565. ];
  5566. $this->assertHtml($expected, $result);
  5567. }
  5568. /**
  5569. * testDate method
  5570. *
  5571. * Test the date type.
  5572. */
  5573. public function testDate(): void
  5574. {
  5575. $result = $this->Form->date('start_day', [
  5576. 'value' => '2014-03-08',
  5577. ]);
  5578. $expected = [
  5579. 'input' => [
  5580. 'type' => 'date',
  5581. 'name' => 'start_day',
  5582. 'value' => '2014-03-08',
  5583. ],
  5584. ];
  5585. $this->assertHtml($expected, $result);
  5586. $result = $this->Form->date('start_day', [
  5587. 'value' => new Date('2014-03-08'),
  5588. ]);
  5589. $this->assertHtml($expected, $result);
  5590. }
  5591. /**
  5592. * testDateTime method
  5593. */
  5594. public function testDateTime(): void
  5595. {
  5596. $result = $this->Form->dateTime('date', ['default' => true]);
  5597. $expected = [
  5598. 'input' => [
  5599. 'type' => 'datetime-local',
  5600. 'name' => 'date',
  5601. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5602. 'step' => '1',
  5603. ],
  5604. ];
  5605. $this->assertHtml($expected, $result);
  5606. }
  5607. /**
  5608. * testDateTimeSecured method
  5609. *
  5610. * Test that datetime fields are added to protected fields list.
  5611. */
  5612. public function testDateTimeSecured(): void
  5613. {
  5614. $this->View->setRequest(
  5615. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5616. );
  5617. $this->Form->create();
  5618. $this->Form->dateTime('date');
  5619. $expected = ['date'];
  5620. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5621. $this->assertEquals($expected, $result);
  5622. $this->Form->date('published');
  5623. $expected = ['date', 'published'];
  5624. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5625. $this->assertEquals($expected, $result);
  5626. }
  5627. /**
  5628. * testDateTimeSecuredDisabled method
  5629. *
  5630. * Test that datetime fields are added to protected fields list.
  5631. */
  5632. public function testDateTimeSecuredDisabled(): void
  5633. {
  5634. $this->View->setRequest(
  5635. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5636. );
  5637. $this->Form->create();
  5638. $this->Form->dateTime('date', ['secure' => false]);
  5639. $expected = [];
  5640. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5641. $this->assertEquals($expected, $result);
  5642. $this->Form->date('published', ['secure' => false]);
  5643. $expected = [];
  5644. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5645. $this->assertEquals($expected, $result);
  5646. }
  5647. /**
  5648. * testDatetimeWithDefault method
  5649. *
  5650. * Test that datetime() and default values work.
  5651. */
  5652. public function testDatetimeWithDefault(): void
  5653. {
  5654. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5655. $expected = [
  5656. 'input' => [
  5657. 'type' => 'datetime-local',
  5658. 'name' => 'updated',
  5659. 'value' => '2009-06-01T11:15:30',
  5660. 'step' => '1',
  5661. ],
  5662. ];
  5663. $this->assertHtml($expected, $result);
  5664. $result = $this->Form->dateTime('updated', [
  5665. 'default' => '2009-06-01 11:15:30',
  5666. ]);
  5667. $expected = [
  5668. 'input' => [
  5669. 'type' => 'datetime-local',
  5670. 'name' => 'updated',
  5671. 'value' => '2009-06-01T11:15:30',
  5672. 'step' => '1',
  5673. ],
  5674. ];
  5675. $this->assertHtml($expected, $result);
  5676. }
  5677. /**
  5678. * testMonth method
  5679. *
  5680. * Test generation of a month input.
  5681. */
  5682. public function testMonth(): void
  5683. {
  5684. $result = $this->Form->month('field', ['value' => '']);
  5685. $expected = [
  5686. 'input' => [
  5687. 'type' => 'month',
  5688. 'name' => 'field',
  5689. 'value' => '',
  5690. ],
  5691. ];
  5692. $this->assertHtml($expected, $result);
  5693. $this->View->setRequest(
  5694. $this->View->getRequest()->withData('release', '2050-02-10')
  5695. );
  5696. $this->Form->create();
  5697. $result = $this->Form->month('release');
  5698. $expected = [
  5699. 'input' => [
  5700. 'type' => 'month',
  5701. 'name' => 'release',
  5702. 'value' => '2050-02',
  5703. ],
  5704. ];
  5705. $this->assertHtml($expected, $result);
  5706. $this->View->setRequest(
  5707. $this->View->getRequest()->withData('release', '2050-03')
  5708. );
  5709. $this->Form->create();
  5710. $result = $this->Form->month('release');
  5711. $expected = [
  5712. 'input' => [
  5713. 'type' => 'month',
  5714. 'name' => 'release',
  5715. 'value' => '2050-03',
  5716. ],
  5717. ];
  5718. $this->assertHtml($expected, $result);
  5719. }
  5720. /**
  5721. * testYear method
  5722. *
  5723. * Test generation of a year input.
  5724. */
  5725. public function testYear(): void
  5726. {
  5727. $this->View->setRequest(
  5728. $this->View->getRequest()->withData('published', '2006')
  5729. );
  5730. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5731. $expected = [
  5732. ['select' => ['name' => 'field']],
  5733. ['option' => ['selected' => 'selected', 'value' => '']],
  5734. '/option',
  5735. ['option' => ['value' => '2007']],
  5736. '2007',
  5737. '/option',
  5738. ['option' => ['value' => '2006']],
  5739. '2006',
  5740. '/option',
  5741. '/select',
  5742. ];
  5743. $this->assertHtml($expected, $result);
  5744. $result = $this->Form->year('field', [
  5745. 'value' => '',
  5746. 'min' => 2006,
  5747. 'max' => 2007,
  5748. 'order' => 'asc',
  5749. ]);
  5750. $expected = [
  5751. ['select' => ['name' => 'field']],
  5752. ['option' => ['selected' => 'selected', 'value' => '']],
  5753. '/option',
  5754. ['option' => ['value' => '2006']],
  5755. '2006',
  5756. '/option',
  5757. ['option' => ['value' => '2007']],
  5758. '2007',
  5759. '/option',
  5760. '/select',
  5761. ];
  5762. $this->assertHtml($expected, $result);
  5763. $result = $this->Form->year('published', [
  5764. 'empty' => false,
  5765. 'min' => 2006,
  5766. 'max' => 2007,
  5767. ]);
  5768. $expected = [
  5769. ['select' => ['name' => 'published']],
  5770. ['option' => ['value' => '2007']],
  5771. '2007',
  5772. '/option',
  5773. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5774. '2006',
  5775. '/option',
  5776. '/select',
  5777. ];
  5778. $this->assertHtml($expected, $result);
  5779. $result = $this->Form->year('published', [
  5780. 'empty' => false,
  5781. 'value' => new Date('2008-01-12'),
  5782. 'min' => 2007,
  5783. 'max' => 2009,
  5784. ]);
  5785. $expected = [
  5786. ['select' => ['name' => 'published']],
  5787. ['option' => ['value' => '2009']],
  5788. '2009',
  5789. '/option',
  5790. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5791. '2008',
  5792. '/option',
  5793. ['option' => ['value' => '2007']],
  5794. '2007',
  5795. '/option',
  5796. '/select',
  5797. ];
  5798. $this->assertHtml($expected, $result);
  5799. $result = $this->Form->year('published', [
  5800. 'empty' => 'Published on',
  5801. ]);
  5802. $this->assertStringContainsString('Published on', $result);
  5803. }
  5804. /**
  5805. * testControlYearPreEpoch method
  5806. *
  5807. * Test minYear being prior to the unix epoch.
  5808. */
  5809. public function testControlYearPreEpoch(): void
  5810. {
  5811. $start = date('Y') - 80;
  5812. $end = date('Y') - 18;
  5813. $result = $this->Form->control('birth_year', [
  5814. 'type' => 'year',
  5815. 'label' => 'Birth Year',
  5816. 'min' => $start,
  5817. 'max' => $end,
  5818. ]);
  5819. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5820. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5821. $this->assertStringNotContainsString('value="00">00', $result);
  5822. }
  5823. /**
  5824. * test control() datetime & required attributes
  5825. */
  5826. public function testControlDatetimeRequired(): void
  5827. {
  5828. $result = $this->Form->control('birthday', [
  5829. 'type' => 'date',
  5830. 'required' => true,
  5831. ]);
  5832. $this->assertStringContainsString(
  5833. '<input type="date" name="birthday" required="required"',
  5834. $result
  5835. );
  5836. }
  5837. /**
  5838. * testYearAutoExpandRange method
  5839. */
  5840. public function testYearAutoExpandRange(): void
  5841. {
  5842. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5843. $result = $this->Form->year('birthday');
  5844. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5845. $result = $matches[1];
  5846. $expected = range(date('Y') + 5, 1930);
  5847. $this->assertEquals($expected, $result);
  5848. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5849. $this->Form->create();
  5850. $result = $this->Form->year('release');
  5851. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5852. $result = $matches[1];
  5853. $expected = range(2050, date('Y') - 5);
  5854. $this->assertEquals($expected, $result);
  5855. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5856. $this->Form->create();
  5857. $result = $this->Form->year('release', [
  5858. 'min' => 1890,
  5859. 'max' => 1900,
  5860. ]);
  5861. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5862. $result = $matches[1];
  5863. $expected = range(1900, 1881);
  5864. $this->assertEquals($expected, $result);
  5865. }
  5866. /**
  5867. * test control placeholder + label
  5868. */
  5869. public function testControlLabelAndPlaceholder(): void
  5870. {
  5871. $this->Form->create($this->article);
  5872. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5873. $expected = [
  5874. 'div' => ['class' => 'input text required'],
  5875. 'label' => ['for' => 'title'],
  5876. 'Title',
  5877. '/label',
  5878. 'input' => [
  5879. 'type' => 'text',
  5880. 'required' => 'required',
  5881. 'placeholder' => 'Add title',
  5882. 'id' => 'title',
  5883. 'name' => 'title',
  5884. 'data-validity-message' => 'This field cannot be left empty',
  5885. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5886. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5887. ],
  5888. '/div',
  5889. ];
  5890. $this->assertHtml($expected, $result);
  5891. }
  5892. /**
  5893. * testControlLabelFalse method
  5894. *
  5895. * Test the label option being set to false.
  5896. */
  5897. public function testControlLabelFalse(): void
  5898. {
  5899. $this->Form->create($this->article);
  5900. $result = $this->Form->control('title', ['label' => false]);
  5901. $expected = [
  5902. 'div' => ['class' => 'input text required'],
  5903. 'input' => [
  5904. 'type' => 'text',
  5905. 'required' => 'required',
  5906. 'id' => 'title',
  5907. 'name' => 'title',
  5908. 'data-validity-message' => 'This field cannot be left empty',
  5909. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5910. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5911. ],
  5912. '/div',
  5913. ];
  5914. $this->assertHtml($expected, $result);
  5915. $this->Form->create($this->article);
  5916. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5917. $expected['input'] += [
  5918. 'placeholder' => 'Add title',
  5919. 'aria-label' => 'Add title',
  5920. ];
  5921. $this->assertHtml($expected, $result);
  5922. }
  5923. /**
  5924. * testTextArea method
  5925. *
  5926. * Test generation of a textarea input.
  5927. */
  5928. public function testTextArea(): void
  5929. {
  5930. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5931. $result = $this->Form->textarea('field');
  5932. $expected = [
  5933. 'textarea' => ['name' => 'field', 'rows' => 5],
  5934. 'some test data',
  5935. '/textarea',
  5936. ];
  5937. $this->assertHtml($expected, $result);
  5938. $result = $this->Form->textarea('user.bio');
  5939. $expected = [
  5940. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5941. '/textarea',
  5942. ];
  5943. $this->assertHtml($expected, $result);
  5944. $this->View->setRequest($this->View->getRequest()
  5945. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5946. $this->Form->create();
  5947. $result = $this->Form->textarea('field');
  5948. $expected = [
  5949. 'textarea' => ['name' => 'field', 'rows' => 5],
  5950. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5951. '/textarea',
  5952. ];
  5953. $this->assertHtml($expected, $result);
  5954. $this->View->setRequest($this->View->getRequest()->withData(
  5955. 'Model.field',
  5956. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5957. ));
  5958. $this->Form->create();
  5959. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5960. $expected = [
  5961. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5962. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5963. '/textarea',
  5964. ];
  5965. $this->assertHtml($expected, $result);
  5966. $result = $this->Form->textarea('0.OtherModel.field');
  5967. $expected = [
  5968. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5969. '/textarea',
  5970. ];
  5971. $this->assertHtml($expected, $result);
  5972. }
  5973. /**
  5974. * testTextAreaWithStupidCharacters method
  5975. *
  5976. * Test text area with non-ascii characters.
  5977. */
  5978. public function testTextAreaWithStupidCharacters(): void
  5979. {
  5980. $result = $this->Form->textarea('Post.content', [
  5981. 'value' => 'GREAT®',
  5982. 'rows' => '15',
  5983. 'cols' => '75',
  5984. ]);
  5985. $expected = [
  5986. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5987. 'GREAT®',
  5988. '/textarea',
  5989. ];
  5990. $this->assertHtml($expected, $result);
  5991. }
  5992. /**
  5993. * testTextAreaMaxLength method
  5994. *
  5995. * Test textareas maxlength read from schema.
  5996. */
  5997. public function testTextAreaMaxLength(): void
  5998. {
  5999. $this->Form->create([
  6000. 'schema' => [
  6001. 'stuff' => ['type' => 'string', 'length' => 10],
  6002. ],
  6003. ]);
  6004. $result = $this->Form->control('other', ['type' => 'textarea']);
  6005. $expected = [
  6006. 'div' => ['class' => 'input textarea'],
  6007. 'label' => ['for' => 'other'],
  6008. 'Other',
  6009. '/label',
  6010. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6011. '/textarea',
  6012. '/div',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6016. $expected = [
  6017. 'div' => ['class' => 'input textarea'],
  6018. 'label' => ['for' => 'stuff'],
  6019. 'Stuff',
  6020. '/label',
  6021. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6022. '/textarea',
  6023. '/div',
  6024. ];
  6025. $this->assertHtml($expected, $result);
  6026. }
  6027. /**
  6028. * testHiddenField method
  6029. *
  6030. * Test generation of a hidden input.
  6031. */
  6032. public function testHidden(): void
  6033. {
  6034. $this->article['errors'] = [
  6035. 'field' => true,
  6036. ];
  6037. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6038. $this->Form->create($this->article);
  6039. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6040. $expected = [
  6041. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6042. $this->assertHtml($expected, $result);
  6043. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6044. $expected = [
  6045. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $article = new Article([
  6049. 'status' => ArticleStatus::UNPUBLISHED,
  6050. ]);
  6051. $this->Form->create($article);
  6052. $result = $this->Form->hidden('status');
  6053. $expected = [
  6054. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6055. ];
  6056. $this->assertHtml($expected, $result);
  6057. }
  6058. /**
  6059. * Test hidden() with various boolean values.
  6060. */
  6061. public function testHiddenBooleanValues(): void
  6062. {
  6063. $this->Form->create($this->article);
  6064. $result = $this->Form->hidden('field', ['value' => null]);
  6065. $expected = [
  6066. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6067. ];
  6068. $this->assertHtml($expected, $result);
  6069. $result = $this->Form->hidden('field', ['value' => true]);
  6070. $expected = [
  6071. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6072. ];
  6073. $this->assertHtml($expected, $result);
  6074. $result = $this->Form->hidden('field', ['value' => false]);
  6075. $expected = [
  6076. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6077. ];
  6078. $this->assertHtml($expected, $result);
  6079. }
  6080. /**
  6081. * testFileUploadField method
  6082. *
  6083. * Test generation of a file upload input.
  6084. */
  6085. public function testFileUploadField(): void
  6086. {
  6087. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6088. $result = $this->Form->file('Model.upload');
  6089. $this->assertHtml($expected, $result);
  6090. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6091. 'name' => '', 'type' => '', 'tmp_name' => '',
  6092. 'error' => 4, 'size' => 0,
  6093. ]));
  6094. $result = $this->Form->file('Model.upload');
  6095. $this->assertHtml($expected, $result);
  6096. $this->View->setRequest(
  6097. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6098. );
  6099. $result = $this->Form->file('Model.upload');
  6100. $this->assertHtml($expected, $result);
  6101. }
  6102. /**
  6103. * testFileUploadOnOtherModel method
  6104. *
  6105. * Test File upload input on a model not used in create().
  6106. */
  6107. public function testFileUploadOnOtherModel(): void
  6108. {
  6109. $this->Form->create($this->article, ['type' => 'file']);
  6110. $result = $this->Form->file('ValidateProfile.city');
  6111. $expected = [
  6112. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6113. ];
  6114. $this->assertHtml($expected, $result);
  6115. }
  6116. /**
  6117. * testButton method
  6118. *
  6119. * Test generation of a form button.
  6120. */
  6121. public function testButton(): void
  6122. {
  6123. $result = $this->Form->button('Hi');
  6124. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6125. $this->assertHtml($expected, $result);
  6126. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6127. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6128. $this->assertHtml($expected, $result);
  6129. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6130. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6131. $this->assertHtml($expected, $result);
  6132. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6133. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6134. $this->assertHtml($expected, $result);
  6135. $result = $this->Form->button('No type', ['type' => false]);
  6136. $expected = ['button' => [], 'No type', '/button'];
  6137. $this->assertHtml($expected, $result);
  6138. $result = $this->Form->button('Upload Text', [
  6139. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6140. 'escape' => false,
  6141. ]);
  6142. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6143. }
  6144. /**
  6145. * testButtonUnlockedByDefault method
  6146. *
  6147. * Test that button() makes unlocked fields by default.
  6148. */
  6149. public function testButtonUnlockedByDefault(): void
  6150. {
  6151. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6152. $this->Form->create();
  6153. $this->Form->button('Save', ['name' => 'save']);
  6154. $this->Form->button('Clear');
  6155. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6156. $this->assertEquals(['save'], $result);
  6157. }
  6158. /**
  6159. * Test generation of a form button with confirm message.
  6160. */
  6161. public function testButtonWithConfirm(): void
  6162. {
  6163. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6164. $expected = ['button' => [
  6165. 'type' => 'submit',
  6166. 'data-confirm-message' => 'Confirm me!',
  6167. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6168. ], 'Hi', '/button'];
  6169. $this->assertHtml($expected, $result);
  6170. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6171. $expected = ['button' => [
  6172. 'type' => 'submit',
  6173. 'data-confirm-message' => 'Confirm "me"!',
  6174. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6175. ], 'Hi', '/button'];
  6176. $this->assertHtml($expected, $result);
  6177. }
  6178. /**
  6179. * testPostButton method
  6180. */
  6181. public function testPostButton(): void
  6182. {
  6183. $result = $this->Form->postButton('Hi', '/controller/action');
  6184. $expected = [
  6185. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6186. 'button' => ['type' => 'submit'],
  6187. 'Hi',
  6188. '/button',
  6189. '/form',
  6190. ];
  6191. $this->assertHtml($expected, $result);
  6192. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6193. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6194. }
  6195. /**
  6196. * testPostButtonMethodType method
  6197. */
  6198. public function testPostButtonMethodType(): void
  6199. {
  6200. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6201. $expected = [
  6202. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6203. 'div' => ['style' => 'display:none;'],
  6204. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6205. '/div',
  6206. 'button' => ['type' => 'submit'],
  6207. 'Hi',
  6208. '/button',
  6209. '/form',
  6210. ];
  6211. $this->assertHtml($expected, $result);
  6212. }
  6213. /**
  6214. * testPostButtonFormOptions method
  6215. */
  6216. public function testPostButtonFormOptions(): void
  6217. {
  6218. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6219. $expected = [
  6220. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6221. 'button' => ['type' => 'submit'],
  6222. 'Hi',
  6223. '/button',
  6224. '/form',
  6225. ];
  6226. $this->assertHtml($expected, $result);
  6227. }
  6228. /**
  6229. * testPostButtonNestedData method
  6230. *
  6231. * Test using postButton with N dimensional data.
  6232. */
  6233. public function testPostButtonNestedData(): void
  6234. {
  6235. $data = [
  6236. 'one' => [
  6237. 'two' => [
  6238. 3, 4, 5,
  6239. ],
  6240. ],
  6241. ];
  6242. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6243. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6244. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6245. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6246. }
  6247. /**
  6248. * testSecurePostButton method
  6249. *
  6250. * Test that postButton adds _Token fields.
  6251. */
  6252. public function testSecurePostButton(): void
  6253. {
  6254. $this->View->setRequest($this->View->getRequest()
  6255. ->withAttribute('csrfToken', 'testkey')
  6256. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6257. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6258. $tokenDebug = urlencode(json_encode([
  6259. '/posts/delete/1',
  6260. [],
  6261. [],
  6262. ]));
  6263. $expected = [
  6264. 'form' => [
  6265. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6266. ],
  6267. ['div' => ['style' => 'display:none;']],
  6268. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6269. '/div',
  6270. 'button' => ['type' => 'submit'],
  6271. 'Delete',
  6272. '/button',
  6273. ['div' => ['style' => 'display:none;']],
  6274. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6275. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6276. ['input' => [
  6277. 'type' => 'hidden',
  6278. 'name' => '_Token[debug]',
  6279. 'value' => $tokenDebug,
  6280. ]],
  6281. '/div',
  6282. '/form',
  6283. ];
  6284. $this->assertHtml($expected, $result);
  6285. }
  6286. /**
  6287. * testPostLink method
  6288. */
  6289. public function testPostLink(): void
  6290. {
  6291. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6292. $expected = [
  6293. 'form' => [
  6294. 'method' => 'post', 'action' => '/posts/delete/1',
  6295. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6296. ],
  6297. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6298. '/form',
  6299. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6300. 'Delete',
  6301. '/a',
  6302. ];
  6303. $this->assertHtml($expected, $result);
  6304. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6305. $expected = [
  6306. 'form' => [
  6307. 'method' => 'post', 'action' => '/posts/delete/1',
  6308. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6309. ],
  6310. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6311. '/form',
  6312. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6313. 'Delete',
  6314. '/a',
  6315. ];
  6316. $this->assertHtml($expected, $result);
  6317. $result = $this->Form->postLink(
  6318. 'Delete',
  6319. '/posts/delete/1',
  6320. ['target' => '_blank', 'class' => 'btn btn-danger']
  6321. );
  6322. $expected = [
  6323. 'form' => [
  6324. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6325. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6326. ],
  6327. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6328. '/form',
  6329. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6330. 'Delete',
  6331. '/a',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. }
  6335. /**
  6336. * testPostLinkWithConfirm method
  6337. *
  6338. * Test the confirm option for postLink().
  6339. */
  6340. public function testPostLinkWithConfirm(): void
  6341. {
  6342. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6343. $expected = [
  6344. 'form' => [
  6345. 'method' => 'post', 'action' => '/posts/delete/1',
  6346. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6347. ],
  6348. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6349. '/form',
  6350. 'a' => [
  6351. 'href' => '#',
  6352. 'data-confirm-message' => 'Confirm?',
  6353. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6354. ],
  6355. 'Delete',
  6356. '/a',
  6357. ];
  6358. $this->assertHtml($expected, $result);
  6359. $result = $this->Form->postLink(
  6360. 'Delete',
  6361. '/posts/delete/1',
  6362. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6363. );
  6364. $expected = [
  6365. 'form' => [
  6366. 'method' => 'post', 'action' => '/posts/delete/1',
  6367. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6368. ],
  6369. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6370. '/form',
  6371. 'a' => [
  6372. 'href' => '#',
  6373. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6374. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6375. ],
  6376. 'Delete',
  6377. '/a',
  6378. ];
  6379. $this->assertHtml($expected, $result);
  6380. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6381. $result = $this->Form->postLink(
  6382. 'Delete',
  6383. '/posts/delete/1',
  6384. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6385. );
  6386. $expected = [
  6387. 'form' => [
  6388. 'method' => 'post', 'action' => '/posts/delete/1',
  6389. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6390. ],
  6391. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6392. '/form',
  6393. 'a' => [
  6394. 'href' => '#',
  6395. 'data-confirm-message' => 'Confirm this deletion?',
  6396. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6397. ],
  6398. 'Delete',
  6399. '/a',
  6400. ];
  6401. $this->assertHtml($expected, $result);
  6402. }
  6403. /**
  6404. * testPostLinkWithQuery method
  6405. *
  6406. * Test postLink() with query string args.
  6407. */
  6408. public function testPostLinkWithQuery(): void
  6409. {
  6410. $result = $this->Form->postLink(
  6411. 'Delete',
  6412. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6413. );
  6414. $expected = [
  6415. 'form' => [
  6416. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6417. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6418. ],
  6419. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6420. '/form',
  6421. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6422. 'Delete',
  6423. '/a',
  6424. ];
  6425. $this->assertHtml($expected, $result);
  6426. }
  6427. /**
  6428. * testPostLinkWithData method
  6429. *
  6430. * Test postLink with additional data.
  6431. */
  6432. public function testPostLinkWithData(): void
  6433. {
  6434. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6435. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6436. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6437. $this->Form->create($entity);
  6438. $this->Form->end();
  6439. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6440. $this->assertStringContainsString(
  6441. '<input type="hidden" name="name" value="show"',
  6442. $result,
  6443. 'should not contain entity data.'
  6444. );
  6445. }
  6446. /**
  6447. * testPostLinkSecurityHash method
  6448. *
  6449. * Test that security hashes for postLink include the url.
  6450. */
  6451. public function testPostLinkSecurityHash(): void
  6452. {
  6453. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6454. $hash .= '%3Aid';
  6455. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6456. $result = $this->Form->postLink(
  6457. 'Delete',
  6458. '/posts/delete/1',
  6459. ['data' => ['id' => 1]]
  6460. );
  6461. $tokenDebug = urlencode(json_encode([
  6462. '/posts/delete/1',
  6463. [
  6464. 'id' => 1,
  6465. ],
  6466. [],
  6467. ]));
  6468. $expected = [
  6469. 'form' => [
  6470. 'method' => 'post', 'action' => '/posts/delete/1',
  6471. 'name', 'style' => 'display:none;',
  6472. ],
  6473. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6474. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6475. 'div' => ['style' => 'display:none;'],
  6476. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6477. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6478. ['input' => [
  6479. 'type' => 'hidden',
  6480. 'name' => '_Token[debug]',
  6481. 'value' => $tokenDebug,
  6482. ]],
  6483. '/div',
  6484. '/form',
  6485. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6486. 'Delete',
  6487. '/a',
  6488. ];
  6489. $this->assertHtml($expected, $result);
  6490. }
  6491. /**
  6492. * testPostLinkSecurityHashBlockMode method
  6493. *
  6494. * Test that postLink doesn't modify the fields in the containing form.
  6495. *
  6496. * postLink() calls inside open forms should not modify the field list
  6497. * for the form.
  6498. */
  6499. public function testPostLinkSecurityHashBlockMode(): void
  6500. {
  6501. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6502. $hash .= '%3A';
  6503. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6504. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6505. $this->Form->control('title');
  6506. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6507. $result = $this->View->fetch('postLink');
  6508. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6509. $this->assertEquals(['title'], $fields);
  6510. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6511. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6512. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6513. }
  6514. /**
  6515. * testPostLinkSecurityHashNoDebugMode method
  6516. *
  6517. * Test that security does not include debug token if debug is false.
  6518. */
  6519. public function testPostLinkSecurityHashNoDebugMode(): void
  6520. {
  6521. Configure::write('debug', false);
  6522. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6523. $hash .= '%3Aid';
  6524. $this->View->setRequest($this->View->getRequest()
  6525. ->withAttribute('formTokenData', ['key' => 'test']));
  6526. $result = $this->Form->postLink(
  6527. 'Delete',
  6528. '/posts/delete/1',
  6529. ['data' => ['id' => 1]]
  6530. );
  6531. $expected = [
  6532. 'form' => [
  6533. 'method' => 'post', 'action' => '/posts/delete/1',
  6534. 'name', 'style' => 'display:none;',
  6535. ],
  6536. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6537. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6538. 'div' => ['style' => 'display:none;'],
  6539. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6540. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6541. '/div',
  6542. '/form',
  6543. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6544. 'Delete',
  6545. '/a',
  6546. ];
  6547. $this->assertHtml($expected, $result);
  6548. }
  6549. /**
  6550. * testPostLinkNestedData method
  6551. *
  6552. * Test using postLink with N dimensional data.
  6553. */
  6554. public function testPostLinkNestedData(): void
  6555. {
  6556. $data = [
  6557. 'one' => [
  6558. 'two' => [
  6559. 3, 4, 5,
  6560. ],
  6561. ],
  6562. ];
  6563. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6564. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6565. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6566. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6567. }
  6568. /**
  6569. * testPostLinkAfterGetForm method
  6570. *
  6571. * Test creating postLinks after a GET form.
  6572. */
  6573. public function testPostLinkAfterGetForm(): void
  6574. {
  6575. $this->View->setRequest($this->View->getRequest()
  6576. ->withAttribute('csrfToken', 'testkey')
  6577. ->withAttribute('formTokenData', []));
  6578. $this->Form->create($this->article, ['type' => 'get']);
  6579. $this->Form->end();
  6580. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6581. $tokenDebug = urlencode(json_encode([
  6582. '/posts/delete/1',
  6583. [],
  6584. [],
  6585. ]));
  6586. $expected = [
  6587. 'form' => [
  6588. 'method' => 'post', 'action' => '/posts/delete/1',
  6589. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6590. ],
  6591. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6592. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6593. 'div' => ['style' => 'display:none;'],
  6594. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6595. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6596. ['input' => [
  6597. 'type' => 'hidden', 'name' => '_Token[debug]',
  6598. 'value' => $tokenDebug,
  6599. ]],
  6600. '/div',
  6601. '/form',
  6602. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6603. 'Delete',
  6604. '/a',
  6605. ];
  6606. $this->assertHtml($expected, $result);
  6607. }
  6608. /**
  6609. * testPostLinkFormBuffer method
  6610. *
  6611. * Test that postLink adds form tags to view block.
  6612. */
  6613. public function testPostLinkFormBuffer(): void
  6614. {
  6615. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6616. $expected = [
  6617. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6618. 'Delete',
  6619. '/a',
  6620. ];
  6621. $this->assertHtml($expected, $result);
  6622. $result = $this->View->fetch('postLink');
  6623. $expected = [
  6624. 'form' => [
  6625. 'method' => 'post', 'action' => '/posts/delete/1',
  6626. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6627. ],
  6628. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6629. '/form',
  6630. ];
  6631. $this->assertHtml($expected, $result);
  6632. $result = $this->Form->postLink(
  6633. 'Delete',
  6634. '/posts/delete/2',
  6635. ['block' => true, 'method' => 'DELETE']
  6636. );
  6637. $expected = [
  6638. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6639. 'Delete',
  6640. '/a',
  6641. ];
  6642. $this->assertHtml($expected, $result);
  6643. $result = $this->View->fetch('postLink');
  6644. $expected = [
  6645. 'form' => [
  6646. 'method' => 'post', 'action' => '/posts/delete/1',
  6647. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6648. ],
  6649. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6650. '/form',
  6651. [
  6652. 'form' => [
  6653. 'method' => 'post', 'action' => '/posts/delete/2',
  6654. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6655. ],
  6656. ],
  6657. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6658. '/form',
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6662. $expected = [
  6663. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6664. 'Delete',
  6665. '/a',
  6666. ];
  6667. $this->assertHtml($expected, $result);
  6668. $result = $this->View->fetch('foobar');
  6669. $expected = [
  6670. 'form' => [
  6671. 'method' => 'post', 'action' => '/posts/delete/1',
  6672. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6673. ],
  6674. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6675. '/form',
  6676. ];
  6677. $this->assertHtml($expected, $result);
  6678. }
  6679. /**
  6680. * testSubmitButton method
  6681. */
  6682. public function testSubmitButton(): void
  6683. {
  6684. $result = $this->Form->submit('');
  6685. $expected = [
  6686. 'div' => ['class' => 'submit'],
  6687. 'input' => ['type' => 'submit', 'value' => ''],
  6688. '/div',
  6689. ];
  6690. $this->assertHtml($expected, $result);
  6691. $result = $this->Form->submit('Test Submit');
  6692. $expected = [
  6693. 'div' => ['class' => 'submit'],
  6694. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6695. '/div',
  6696. ];
  6697. $this->assertHtml($expected, $result);
  6698. $result = $this->Form->submit('Next >');
  6699. $expected = [
  6700. 'div' => ['class' => 'submit'],
  6701. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6702. '/div',
  6703. ];
  6704. $this->assertHtml($expected, $result);
  6705. $result = $this->Form->submit('Next >', ['escape' => false]);
  6706. $expected = [
  6707. 'div' => ['class' => 'submit'],
  6708. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6709. '/div',
  6710. ];
  6711. $this->assertHtml($expected, $result);
  6712. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6713. $expected = [
  6714. 'div' => ['class' => 'submit'],
  6715. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6716. '/div',
  6717. ];
  6718. $this->assertHtml($expected, $result);
  6719. }
  6720. /**
  6721. * testSubmitImage method
  6722. *
  6723. * Test image submit types.
  6724. */
  6725. public function testSubmitImage(): void
  6726. {
  6727. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6728. $expected = [
  6729. 'div' => ['class' => 'submit'],
  6730. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6731. '/div',
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. $result = $this->Form->submit('/relative/cake.power.gif');
  6735. $expected = [
  6736. 'div' => ['class' => 'submit'],
  6737. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6738. '/div',
  6739. ];
  6740. $this->assertHtml($expected, $result);
  6741. $result = $this->Form->submit('cake.power.gif');
  6742. $expected = [
  6743. 'div' => ['class' => 'submit'],
  6744. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6745. '/div',
  6746. ];
  6747. $this->assertHtml($expected, $result);
  6748. $result = $this->Form->submit('Not.an.image');
  6749. $expected = [
  6750. 'div' => ['class' => 'submit'],
  6751. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6752. '/div',
  6753. ];
  6754. $this->assertHtml($expected, $result);
  6755. }
  6756. /**
  6757. * testSubmitUnlockedByDefault method
  6758. *
  6759. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6760. * be submitted at a time.
  6761. */
  6762. public function testSubmitUnlockedByDefault(): void
  6763. {
  6764. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6765. $this->Form->create();
  6766. $this->Form->submit('Go go');
  6767. $this->Form->submit('Save', ['name' => 'save']);
  6768. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6769. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6770. }
  6771. /**
  6772. * testSubmitImageTimestamp method
  6773. *
  6774. * Test submit image with timestamps.
  6775. */
  6776. public function testSubmitImageTimestamp(): void
  6777. {
  6778. Configure::write('Asset.timestamp', 'force');
  6779. $result = $this->Form->submit('cake.power.gif');
  6780. $expected = [
  6781. 'div' => ['class' => 'submit'],
  6782. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6783. '/div',
  6784. ];
  6785. $this->assertHtml($expected, $result);
  6786. }
  6787. /**
  6788. * testDateTimeWithGetForms method
  6789. *
  6790. * Test that datetime() works with GET style forms.
  6791. */
  6792. public function testDateTimeWithGetForms(): void
  6793. {
  6794. $this->Form->create($this->article, ['type' => 'get']);
  6795. $result = $this->Form->datetime('created');
  6796. $expected = [
  6797. 'input' => [
  6798. 'type' => 'datetime-local',
  6799. 'name' => 'created',
  6800. 'value' => '',
  6801. 'step' => '1',
  6802. ],
  6803. ];
  6804. $this->assertHtml($expected, $result);
  6805. $result = $this->Form->datetime('created', ['default' => true]);
  6806. $expected = [
  6807. 'input' => [
  6808. 'type' => 'datetime-local',
  6809. 'name' => 'created',
  6810. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6811. 'step' => '1',
  6812. ],
  6813. ];
  6814. $this->assertHtml($expected, $result);
  6815. }
  6816. /**
  6817. * Provides fractional schema types
  6818. *
  6819. * @return array
  6820. */
  6821. public static function fractionalTypeProvider(): array
  6822. {
  6823. return [
  6824. ['datetimefractional'],
  6825. ['timestampfractional'],
  6826. ['timestamptimezone'],
  6827. ];
  6828. }
  6829. /**
  6830. * testDateTimeWithFractional method
  6831. *
  6832. * Test that datetime() works with datetimefractional.
  6833. *
  6834. * @dataProvider fractionalTypeProvider
  6835. */
  6836. public function testDateTimeWithFractional(string $type): void
  6837. {
  6838. $this->Form->create([
  6839. 'schema' => [
  6840. 'created' => ['type' => $type],
  6841. ],
  6842. ]);
  6843. $result = $this->Form->datetime('created', [
  6844. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6845. ]);
  6846. $expected = [
  6847. 'input' => [
  6848. 'type' => 'datetime-local',
  6849. 'name' => 'created',
  6850. 'value' => '2019-09-27T02:52:43.123',
  6851. 'step' => '0.001',
  6852. ],
  6853. ];
  6854. $this->assertHtml($expected, $result);
  6855. }
  6856. /**
  6857. * testControlWithFractional method
  6858. *
  6859. * Test that control() works with datetimefractional.
  6860. *
  6861. * @dataProvider fractionalTypeProvider
  6862. */
  6863. public function testControlWithFractional(string $type): void
  6864. {
  6865. $this->Form->create([
  6866. 'schema' => [
  6867. 'created' => ['type' => $type],
  6868. ],
  6869. ]);
  6870. $result = $this->Form->control('created', [
  6871. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6872. ]);
  6873. $expected = [
  6874. 'div' => ['class' => 'input datetime'],
  6875. 'label' => ['for' => 'created'],
  6876. 'Created',
  6877. '/label',
  6878. 'input' => [
  6879. 'type' => 'datetime-local',
  6880. 'name' => 'created',
  6881. 'id' => 'created',
  6882. 'value' => '2019-09-27T02:52:43.123',
  6883. 'step' => '0.001',
  6884. ],
  6885. '/div',
  6886. ];
  6887. $this->assertHtml($expected, $result);
  6888. }
  6889. /**
  6890. * testForMagicControlNonExistentNotValidated method
  6891. */
  6892. public function testForMagicControlNonExistentNotValidated(): void
  6893. {
  6894. $this->Form->create($this->article);
  6895. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6896. $result = $this->Form->control('nonexistent_not_validated');
  6897. $expected = [
  6898. 'label' => ['for' => 'nonexistent-not-validated'],
  6899. 'Nonexistent Not Validated',
  6900. '/label',
  6901. 'input' => [
  6902. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6903. 'id' => 'nonexistent-not-validated',
  6904. ],
  6905. ];
  6906. $this->assertHtml($expected, $result);
  6907. $result = $this->Form->control('nonexistent_not_validated', [
  6908. 'val' => 'my value',
  6909. ]);
  6910. $expected = [
  6911. 'label' => ['for' => 'nonexistent-not-validated'],
  6912. 'Nonexistent Not Validated',
  6913. '/label',
  6914. 'input' => [
  6915. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6916. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6917. ],
  6918. ];
  6919. $this->assertHtml($expected, $result);
  6920. $this->View->setRequest(
  6921. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6922. );
  6923. $this->Form->create($this->article);
  6924. $result = $this->Form->control('nonexistent_not_validated');
  6925. $expected = [
  6926. 'label' => ['for' => 'nonexistent-not-validated'],
  6927. 'Nonexistent Not Validated',
  6928. '/label',
  6929. 'input' => [
  6930. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6931. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6932. ],
  6933. ];
  6934. $this->assertHtml($expected, $result);
  6935. }
  6936. /**
  6937. * testFormMagicControlLabel method
  6938. */
  6939. public function testFormMagicControlLabel(): void
  6940. {
  6941. $this->getTableLocator()->get('Contacts', [
  6942. 'className' => ContactsTable::class,
  6943. ]);
  6944. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6945. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6946. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6947. $expected = [
  6948. 'label' => ['for' => 'contacts-name'],
  6949. 'My label',
  6950. '/label',
  6951. 'input' => [
  6952. 'type' => 'text',
  6953. 'name' => 'Contacts[name]',
  6954. 'id' => 'contacts-name',
  6955. 'maxlength' => '255',
  6956. ],
  6957. ];
  6958. $this->assertHtml($expected, $result);
  6959. $result = $this->Form->control('name', [
  6960. 'label' => ['class' => 'mandatory'],
  6961. ]);
  6962. $expected = [
  6963. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6964. 'Name',
  6965. '/label',
  6966. 'input' => [
  6967. 'type' => 'text', 'name' => 'name',
  6968. 'id' => 'name', 'maxlength' => '255',
  6969. ],
  6970. ];
  6971. $this->assertHtml($expected, $result);
  6972. $result = $this->Form->control('name', [
  6973. 'div' => false,
  6974. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6975. ]);
  6976. $expected = [
  6977. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6978. 'My label',
  6979. '/label',
  6980. 'input' => [
  6981. 'type' => 'text', 'name' => 'name',
  6982. 'id' => 'name', 'maxlength' => '255',
  6983. ],
  6984. ];
  6985. $this->assertHtml($expected, $result);
  6986. $result = $this->Form->control('Contact.name', [
  6987. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6988. ]);
  6989. $expected = [
  6990. 'label' => ['for' => 'my_id'],
  6991. 'Name',
  6992. '/label',
  6993. 'input' => [
  6994. 'type' => 'text', 'name' => 'Contact[name]',
  6995. 'id' => 'my_id', 'maxlength' => '255',
  6996. ],
  6997. ];
  6998. $this->assertHtml($expected, $result);
  6999. $result = $this->Form->control('1.id');
  7000. $expected = ['input' => [
  7001. 'type' => 'hidden', 'name' => '1[id]',
  7002. 'id' => '1-id',
  7003. ]];
  7004. $this->assertHtml($expected, $result);
  7005. $result = $this->Form->control('1.name');
  7006. $expected = [
  7007. 'label' => ['for' => '1-name'],
  7008. 'Name',
  7009. '/label',
  7010. 'input' => [
  7011. 'type' => 'text', 'name' => '1[name]',
  7012. 'id' => '1-name', 'maxlength' => '255',
  7013. ],
  7014. ];
  7015. $this->assertHtml($expected, $result);
  7016. }
  7017. /**
  7018. * testFormEnd method
  7019. */
  7020. public function testFormEnd(): void
  7021. {
  7022. $this->assertSame('</form>', $this->Form->end());
  7023. }
  7024. /**
  7025. * testMultiRecordForm method
  7026. *
  7027. * Test the generation of fields for a multi record form.
  7028. */
  7029. public function testMultiRecordForm(): void
  7030. {
  7031. $articles = $this->getTableLocator()->get('Articles');
  7032. $articles->hasMany('Comments');
  7033. $comment = new Entity(['comment' => 'Value']);
  7034. $article = new Article(['comments' => [$comment]]);
  7035. $this->Form->create([$article]);
  7036. $result = $this->Form->control('0.comments.1.comment');
  7037. // phpcs:disable
  7038. $expected = [
  7039. 'div' => ['class' => 'input textarea'],
  7040. 'label' => ['for' => '0-comments-1-comment'],
  7041. 'Comment',
  7042. '/label',
  7043. 'textarea' => [
  7044. 'name',
  7045. 'id' => '0-comments-1-comment',
  7046. 'rows' => 5
  7047. ],
  7048. '/textarea',
  7049. '/div'
  7050. ];
  7051. // phpcs:enable
  7052. $this->assertHtml($expected, $result);
  7053. $result = $this->Form->control('0.comments.0.comment');
  7054. // phpcs:disable
  7055. $expected = [
  7056. 'div' => ['class' => 'input textarea'],
  7057. 'label' => ['for' => '0-comments-0-comment'],
  7058. 'Comment',
  7059. '/label',
  7060. 'textarea' => [
  7061. 'name',
  7062. 'id' => '0-comments-0-comment',
  7063. 'rows' => 5
  7064. ],
  7065. 'Value',
  7066. '/textarea',
  7067. '/div'
  7068. ];
  7069. // phpcs:enable
  7070. $this->assertHtml($expected, $result);
  7071. $comment->setError('comment', ['Not valid']);
  7072. $result = $this->Form->control('0.comments.0.comment');
  7073. // phpcs:disable
  7074. $expected = [
  7075. 'div' => ['class' => 'input textarea error'],
  7076. 'label' => ['for' => '0-comments-0-comment'],
  7077. 'Comment',
  7078. '/label',
  7079. 'textarea' => [
  7080. 'name',
  7081. 'class' => 'form-error',
  7082. 'id' => '0-comments-0-comment',
  7083. 'aria-invalid' => 'true',
  7084. 'aria-describedby' => '0-comments-0-comment-error',
  7085. 'rows' => 5
  7086. ],
  7087. 'Value',
  7088. '/textarea',
  7089. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7090. 'Not valid',
  7091. '/div',
  7092. '/div'
  7093. ];
  7094. // phpcs:enable
  7095. $this->assertHtml($expected, $result);
  7096. $this->getTableLocator()->get('Comments')
  7097. ->getValidator('default')
  7098. ->allowEmptyString('comment', null, false);
  7099. $result = $this->Form->control('0.comments.1.comment');
  7100. // phpcs:disable
  7101. $expected = [
  7102. 'div' => ['class' => 'input textarea required'],
  7103. 'label' => ['for' => '0-comments-1-comment'],
  7104. 'Comment',
  7105. '/label',
  7106. 'textarea' => [
  7107. 'name',
  7108. 'required' => 'required',
  7109. 'id' => '0-comments-1-comment',
  7110. 'rows' => 5,
  7111. 'data-validity-message' => 'This field cannot be left empty',
  7112. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7113. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7114. ],
  7115. '/textarea',
  7116. '/div'
  7117. ];
  7118. // phpcs:enable
  7119. $this->assertHtml($expected, $result);
  7120. }
  7121. /**
  7122. * testHtml5Controls method
  7123. *
  7124. * Test that some html5 inputs + FormHelper::__call() work.
  7125. */
  7126. public function testHtml5Controls(): void
  7127. {
  7128. $result = $this->Form->email('User.email');
  7129. $expected = [
  7130. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7131. ];
  7132. $this->assertHtml($expected, $result);
  7133. $result = $this->Form->search('User.query');
  7134. $expected = [
  7135. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7136. ];
  7137. $this->assertHtml($expected, $result);
  7138. $result = $this->Form->search('User.query', ['value' => 'test']);
  7139. $expected = [
  7140. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7141. ];
  7142. $this->assertHtml($expected, $result);
  7143. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7144. $expected = [
  7145. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7146. ];
  7147. $this->assertHtml($expected, $result);
  7148. }
  7149. /**
  7150. * testHtml5ControlWithControl method
  7151. *
  7152. * Test accessing html5 inputs through control().
  7153. */
  7154. public function testHtml5ControlWithControl(): void
  7155. {
  7156. $this->Form->create();
  7157. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7158. $result = $this->Form->control('website', [
  7159. 'type' => 'url',
  7160. 'val' => 'http://domain.tld',
  7161. 'label' => false,
  7162. ]);
  7163. $expected = [
  7164. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7165. ];
  7166. $this->assertHtml($expected, $result);
  7167. }
  7168. /**
  7169. * testHtml5ControlException method
  7170. *
  7171. * Test errors when field name is missing.
  7172. */
  7173. public function testHtml5ControlException(): void
  7174. {
  7175. $this->expectException(CakeException::class);
  7176. $this->Form->email();
  7177. }
  7178. /**
  7179. * tests fields that are required use custom validation messages
  7180. */
  7181. public function testHtml5ErrorMessage(): void
  7182. {
  7183. $this->Form->setConfig('autoSetCustomValidity', true);
  7184. $validator = (new Validator())
  7185. ->notEmptyString('email', 'Custom error message')
  7186. ->requirePresence('password')
  7187. ->alphaNumeric('password')
  7188. ->notBlank('phone');
  7189. $table = $this->getTableLocator()->get('Contacts', [
  7190. 'className' => ContactsTable::class,
  7191. ]);
  7192. $table->setValidator('default', $validator);
  7193. $contact = new Entity();
  7194. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7195. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7196. $result = $this->Form->control('password');
  7197. $expected = [
  7198. 'label' => ['for' => 'password'],
  7199. 'Password',
  7200. '/label',
  7201. 'input' => [
  7202. 'id' => 'password',
  7203. 'name' => 'password',
  7204. 'type' => 'password',
  7205. 'value' => '',
  7206. 'required' => 'required',
  7207. 'data-validity-message' => 'This field cannot be left empty',
  7208. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7209. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7210. ],
  7211. ];
  7212. $this->assertHtml($expected, $result);
  7213. $result = $this->Form->control('phone');
  7214. $expected = [
  7215. 'label' => ['for' => 'phone'],
  7216. 'Phone',
  7217. '/label',
  7218. 'input' => [
  7219. 'id' => 'phone',
  7220. 'name' => 'phone',
  7221. 'type' => 'tel',
  7222. 'value' => '',
  7223. 'maxlength' => 255,
  7224. 'required' => 'required',
  7225. 'data-validity-message' => 'This field cannot be left empty',
  7226. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7227. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7228. ],
  7229. ];
  7230. $this->assertHtml($expected, $result);
  7231. $result = $this->Form->control('email');
  7232. $expected = [
  7233. 'label' => ['for' => 'email'],
  7234. 'Email',
  7235. '/label',
  7236. 'input' => [
  7237. 'id' => 'email',
  7238. 'name' => 'email',
  7239. 'type' => 'email',
  7240. 'value' => '',
  7241. 'maxlength' => 255,
  7242. 'required' => 'required',
  7243. 'data-validity-message' => 'Custom error message',
  7244. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7245. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7246. ],
  7247. ];
  7248. $this->assertHtml($expected, $result);
  7249. }
  7250. /**
  7251. * tests that custom validation messages are in templateVars
  7252. */
  7253. public function testHtml5ErrorMessageInTemplateVars(): void
  7254. {
  7255. $validator = (new Validator())
  7256. ->notEmptyString('email', 'Custom error "message" & entities')
  7257. ->requirePresence('password')
  7258. ->alphaNumeric('password')
  7259. ->notBlank('phone');
  7260. $table = $this->getTableLocator()->get('Contacts', [
  7261. 'className' => ContactsTable::class,
  7262. ]);
  7263. $table->setValidator('default', $validator);
  7264. $contact = new Entity();
  7265. $this->Form->setConfig('autoSetCustomValidity', false);
  7266. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7267. $this->Form->setTemplates([
  7268. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7269. 'inputContainer' => '{{content}}',
  7270. ]);
  7271. $result = $this->Form->control('password');
  7272. $expected = [
  7273. 'label' => ['for' => 'password'],
  7274. 'Password',
  7275. '/label',
  7276. 'input' => [
  7277. 'id' => 'password',
  7278. 'name' => 'password',
  7279. 'type' => 'password',
  7280. 'value' => '',
  7281. 'required' => 'required',
  7282. 'data-message' => 'This field cannot be left empty',
  7283. ],
  7284. ];
  7285. $this->assertHtml($expected, $result);
  7286. $result = $this->Form->control('phone');
  7287. $expected = [
  7288. 'label' => ['for' => 'phone'],
  7289. 'Phone',
  7290. '/label',
  7291. 'input' => [
  7292. 'id' => 'phone',
  7293. 'name' => 'phone',
  7294. 'type' => 'tel',
  7295. 'value' => '',
  7296. 'maxlength' => 255,
  7297. 'required' => 'required',
  7298. 'data-message' => 'This field cannot be left empty',
  7299. ],
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. $result = $this->Form->control('email', [
  7303. 'templateVars' => [
  7304. 'custom' => 'data-custom="1"',
  7305. ],
  7306. ]);
  7307. $expected = [
  7308. 'label' => ['for' => 'email'],
  7309. 'Email',
  7310. '/label',
  7311. 'input' => [
  7312. 'id' => 'email',
  7313. 'name' => 'email',
  7314. 'type' => 'email',
  7315. 'value' => '',
  7316. 'maxlength' => 255,
  7317. 'required' => 'required',
  7318. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7319. 'data-custom' => '1',
  7320. ],
  7321. ];
  7322. $this->assertHtml($expected, $result);
  7323. $this->Form->setConfig('autoSetCustomValidity', true);
  7324. }
  7325. /**
  7326. * testRequiredAttribute method
  7327. *
  7328. * Tests that formhelper sets required attributes.
  7329. */
  7330. public function testRequiredAttribute(): void
  7331. {
  7332. $this->article['required'] = [
  7333. 'title' => true,
  7334. 'body' => false,
  7335. ];
  7336. $this->Form->create($this->article);
  7337. $result = $this->Form->control('title');
  7338. $expected = [
  7339. 'div' => ['class' => 'input text required'],
  7340. 'label' => ['for' => 'title'],
  7341. 'Title',
  7342. '/label',
  7343. 'input' => [
  7344. 'type' => 'text',
  7345. 'name' => 'title',
  7346. 'id' => 'title',
  7347. 'required' => 'required',
  7348. 'data-validity-message' => 'This field cannot be left empty',
  7349. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7350. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7351. ],
  7352. '/div',
  7353. ];
  7354. $this->assertHtml($expected, $result);
  7355. $result = $this->Form->control('title', ['required' => false]);
  7356. $this->assertStringNotContainsString('required', $result);
  7357. $result = $this->Form->control('body');
  7358. $expected = [
  7359. 'div' => ['class' => 'input text'],
  7360. 'label' => ['for' => 'body'],
  7361. 'Body',
  7362. '/label',
  7363. 'input' => [
  7364. 'type' => 'text',
  7365. 'name' => 'body',
  7366. 'id' => 'body',
  7367. ],
  7368. '/div',
  7369. ];
  7370. $this->assertHtml($expected, $result);
  7371. $result = $this->Form->control('body', ['required' => true]);
  7372. $this->assertStringContainsString('required', $result);
  7373. }
  7374. /**
  7375. * testControlsNotNested method
  7376. *
  7377. * Tests that it is possible to put inputs outside of the label.
  7378. */
  7379. public function testControlsNotNested(): void
  7380. {
  7381. $this->Form->setTemplates([
  7382. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7383. 'formGroup' => '{{input}}{{label}}',
  7384. ]);
  7385. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7386. $expected = [
  7387. 'div' => ['class' => 'input checkbox'],
  7388. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7389. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7390. 'label' => ['for' => 'foo'],
  7391. 'Foo',
  7392. '/label',
  7393. '/div',
  7394. ];
  7395. $this->assertHtml($expected, $result);
  7396. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7397. $expected = [
  7398. 'div' => ['class' => 'input checkbox'],
  7399. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7400. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7401. '/div',
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. $result = $this->Form->control('confirm', [
  7405. 'type' => 'radio',
  7406. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7407. ]);
  7408. $expected = [
  7409. 'div' => ['class' => 'input radio'],
  7410. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7411. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7412. ['label' => ['for' => 'confirm-y']],
  7413. 'Yes',
  7414. '/label',
  7415. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7416. ['label' => ['for' => 'confirm-n']],
  7417. 'No',
  7418. '/label',
  7419. '<label',
  7420. 'Confirm',
  7421. '/label',
  7422. '/div',
  7423. ];
  7424. $this->assertHtml($expected, $result);
  7425. $result = $this->Form->select('category', ['1', '2'], [
  7426. 'multiple' => 'checkbox',
  7427. 'name' => 'fish',
  7428. ]);
  7429. $expected = [
  7430. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7431. ['div' => ['class' => 'checkbox']],
  7432. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7433. ['label' => ['for' => 'fish-0']],
  7434. '1',
  7435. '/label',
  7436. '/div',
  7437. ['div' => ['class' => 'checkbox']],
  7438. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7439. ['label' => ['for' => 'fish-1']],
  7440. '2',
  7441. '/label',
  7442. '/div',
  7443. ];
  7444. $this->assertHtml($expected, $result);
  7445. }
  7446. /**
  7447. * testControlContainerTemplates method
  7448. *
  7449. * Test that *Container templates are used by input.
  7450. */
  7451. public function testControlContainerTemplates(): void
  7452. {
  7453. $this->Form->setTemplates([
  7454. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7455. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7456. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7457. ]);
  7458. $this->article['errors'] = [
  7459. 'Article' => ['published' => 'error message'],
  7460. ];
  7461. $this->Form->create($this->article);
  7462. $result = $this->Form->control('accept', [
  7463. 'type' => 'checkbox',
  7464. ]);
  7465. $expected = [
  7466. 'div' => ['class' => 'check'],
  7467. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7468. 'label' => ['for' => 'accept'],
  7469. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7470. 'Accept',
  7471. '/label',
  7472. '/div',
  7473. ];
  7474. $this->assertHtml($expected, $result);
  7475. $result = $this->Form->control('accept', [
  7476. 'type' => 'radio',
  7477. 'options' => ['Y', 'N'],
  7478. ]);
  7479. $this->assertStringContainsString('<div class="rad">', $result);
  7480. $result = $this->Form->control('Article.published', [
  7481. 'type' => 'radio',
  7482. 'options' => ['Y', 'N'],
  7483. ]);
  7484. $this->assertStringContainsString('<div class="rad err">', $result);
  7485. }
  7486. /**
  7487. * testFormGroupTemplates method
  7488. *
  7489. * Test that *Container templates are used by input.
  7490. */
  7491. public function testFormGroupTemplates(): void
  7492. {
  7493. $this->Form->setTemplates([
  7494. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7495. ]);
  7496. $this->Form->create($this->article);
  7497. $result = $this->Form->control('accept', [
  7498. 'type' => 'radio',
  7499. 'options' => ['Y', 'N'],
  7500. ]);
  7501. $this->assertStringContainsString('<div class="radio">', $result);
  7502. }
  7503. /**
  7504. * testResetTemplates method
  7505. *
  7506. * Test resetting templates.
  7507. */
  7508. public function testResetTemplates(): void
  7509. {
  7510. $this->Form->setTemplates(['input' => '<input/>']);
  7511. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7512. $this->Form->resetTemplates();
  7513. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7514. }
  7515. /**
  7516. * testContext method
  7517. *
  7518. * Test the context method.
  7519. */
  7520. public function testContext(): void
  7521. {
  7522. $result = $this->Form->context();
  7523. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7524. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7525. $this->assertSame($mock, $this->Form->context($mock));
  7526. $this->assertSame($mock, $this->Form->context());
  7527. }
  7528. /**
  7529. * testAutoDomId method
  7530. */
  7531. public function testAutoDomId(): void
  7532. {
  7533. $result = $this->Form->text('field', ['id' => true]);
  7534. $expected = [
  7535. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7536. ];
  7537. $this->assertHtml($expected, $result);
  7538. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7539. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7540. $expected = [
  7541. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7542. ['label' => ['for' => 'field-0']],
  7543. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7544. 'option A',
  7545. '/label',
  7546. ['label' => ['for' => 'field-1']],
  7547. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7548. 'option B',
  7549. '/label',
  7550. ];
  7551. $this->assertHtml($expected, $result);
  7552. $result = $this->Form->select(
  7553. 'multi_field',
  7554. ['first', 'second'],
  7555. ['multiple' => 'checkbox', 'id' => true]
  7556. );
  7557. $expected = [
  7558. 'input' => [
  7559. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7560. ],
  7561. ['div' => ['class' => 'checkbox']],
  7562. ['label' => ['for' => 'multi-field-0']],
  7563. ['input' => [
  7564. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7565. 'value' => '0', 'id' => 'multi-field-0',
  7566. ]],
  7567. 'first',
  7568. '/label',
  7569. '/div',
  7570. ['div' => ['class' => 'checkbox']],
  7571. ['label' => ['for' => 'multi-field-1']],
  7572. ['input' => [
  7573. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7574. 'value' => '1', 'id' => 'multi-field-1',
  7575. ]],
  7576. 'second',
  7577. '/label',
  7578. '/div',
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. }
  7582. /**
  7583. * Test the basic setters and getters for value sources
  7584. */
  7585. public function testFormValueSourcesSettersGetters(): void
  7586. {
  7587. $this->View->setRequest($this->View->getRequest()
  7588. ->withData('id', '1')
  7589. ->withQueryParams(['id' => '2']));
  7590. $expected = ['data', 'context'];
  7591. $result = $this->Form->getValueSources();
  7592. $this->assertEquals($expected, $result);
  7593. $this->Form->setValueSources(['context']);
  7594. $result = $this->Form->getSourceValue('id');
  7595. $this->assertNull($result);
  7596. $this->Form->setValueSources('query');
  7597. $expected = ['query'];
  7598. $result = $this->Form->getValueSources();
  7599. $this->assertEquals($expected, $result);
  7600. $expected = '2';
  7601. $result = $this->Form->getSourceValue('id');
  7602. $this->assertSame($expected, $result);
  7603. $this->Form->setValueSources(['data']);
  7604. $expected = '1';
  7605. $result = $this->Form->getSourceValue('id');
  7606. $this->assertSame($expected, $result);
  7607. $this->Form->setValueSources(['query', 'data']);
  7608. $expected = '2';
  7609. $result = $this->Form->getSourceValue('id');
  7610. $this->assertSame($expected, $result);
  7611. }
  7612. public function testValueSourcesValidation(): void
  7613. {
  7614. $this->expectException(InvalidArgumentException::class);
  7615. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7616. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7617. }
  7618. /**
  7619. * Tests the different input rendering values based on sources values switching
  7620. */
  7621. public function testFormValueSourcesSingleSwitchRendering(): void
  7622. {
  7623. $articles = $this->getTableLocator()->get('Articles');
  7624. $article = new Article();
  7625. $articles->patchEntity($article, ['id' => '3']);
  7626. $this->Form->create($article);
  7627. $this->Form->setValueSources(['context']);
  7628. $result = $this->Form->control('id');
  7629. $expected = [
  7630. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7631. ];
  7632. $this->assertHtml($expected, $result);
  7633. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7634. $this->Form->setValueSources(['query']);
  7635. $result = $this->Form->control('id');
  7636. $expected = [
  7637. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7638. ];
  7639. $this->assertHtml($expected, $result);
  7640. $this->View->setRequest($this->View->getRequest()
  7641. ->withQueryParams(['id' => '5a'])
  7642. ->withData('id', '5b'));
  7643. $this->Form->setValueSources(['context']);
  7644. $this->Form->create($article);
  7645. $result = $this->Form->control('id');
  7646. $expected = [
  7647. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7648. ];
  7649. $this->assertHtml($expected, $result);
  7650. $this->Form->setValueSources(['data']);
  7651. $this->Form->create($article);
  7652. $result = $this->Form->control('id');
  7653. $expected = [
  7654. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7655. ];
  7656. $this->assertHtml($expected, $result);
  7657. $this->Form->setValueSources(['query']);
  7658. $result = $this->Form->control('id');
  7659. $expected = [
  7660. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. }
  7664. /**
  7665. * Tests the different input rendering values based on sources values switching while supplying
  7666. * an entity (base context) and multiple sources (such as data, query)
  7667. */
  7668. public function testFormValueSourcesListSwitchRendering(): void
  7669. {
  7670. $articles = $this->getTableLocator()->get('Articles');
  7671. $article = new Article();
  7672. $articles->patchEntity($article, ['id' => '3']);
  7673. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7674. $this->Form->create($article);
  7675. $this->Form->setValueSources(['context', 'query']);
  7676. $result = $this->Form->control('id');
  7677. $expected = [
  7678. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7679. ];
  7680. $this->assertHtml($expected, $result);
  7681. $this->Form->setValueSources(['query', 'context']);
  7682. $result = $this->Form->control('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. $this->Form->setValueSources(['data', 'query', 'context']);
  7688. $result = $this->Form->control('id');
  7689. $expected = [
  7690. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7691. ];
  7692. $this->assertHtml($expected, $result);
  7693. $this->View->setRequest($this->View->getRequest()
  7694. ->withData('id', '8')
  7695. ->withQueryParams(['id' => '9']));
  7696. $this->Form->setValueSources(['data', 'query', 'context']);
  7697. $result = $this->Form->control('id');
  7698. $expected = [
  7699. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. }
  7703. /**
  7704. * Test the different form input renderings based on values sources switchings through form options
  7705. */
  7706. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7707. {
  7708. $articles = $this->getTableLocator()->get('Articles');
  7709. $article = new Article();
  7710. $articles->patchEntity($article, ['id' => '3']);
  7711. $this->View->setRequest(
  7712. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7713. );
  7714. $this->Form->create($article, ['valueSources' => 'query']);
  7715. $result = $this->Form->control('id');
  7716. $expected = [
  7717. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7718. ];
  7719. $this->assertHtml($expected, $result);
  7720. $result = $this->Form->getSourceValue('id');
  7721. $this->assertSame('5', $result);
  7722. $this->Form->setValueSources(['context']);
  7723. $this->Form->create($article, ['valueSources' => 'query']);
  7724. $result = $this->Form->control('id');
  7725. $expected = [
  7726. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7727. ];
  7728. $this->assertHtml($expected, $result);
  7729. $result = $this->Form->getSourceValue('id');
  7730. $this->assertSame('5', $result);
  7731. $this->Form->setValueSources(['query']);
  7732. $this->Form->create($article, ['valueSources' => 'data']);
  7733. $result = $this->Form->control('id');
  7734. $expected = [
  7735. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. $result = $this->Form->getSourceValue('id');
  7739. $this->assertSame('4', $result);
  7740. $this->Form->setValueSources(['query']);
  7741. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7742. $result = $this->Form->control('id');
  7743. $expected = [
  7744. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7745. ];
  7746. $this->assertHtml($expected, $result);
  7747. $result = $this->Form->getSourceValue('id');
  7748. $this->assertSame(3, $result);
  7749. }
  7750. /**
  7751. * Test the different form input renderings based on values sources switchings through form options
  7752. */
  7753. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7754. {
  7755. $articles = $this->getTableLocator()->get('Articles');
  7756. $article = new Article();
  7757. $articles->patchEntity($article, ['id' => '3']);
  7758. $this->View->setRequest(
  7759. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7760. );
  7761. $this->Form->setValueSources(['context'])
  7762. ->create($article, ['valueSources' => ['query', 'data']]);
  7763. $result = $this->Form->control('id');
  7764. $expected = [
  7765. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7766. ];
  7767. $this->assertHtml($expected, $result);
  7768. $result = $this->Form->getSourceValue('id');
  7769. $this->assertSame('11', $result);
  7770. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7771. $this->Form->setValueSources(['context'])
  7772. ->create($article, ['valueSources' => ['query', 'data']]);
  7773. $result = $this->Form->control('id');
  7774. $expected = [
  7775. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7776. ];
  7777. $this->assertHtml($expected, $result);
  7778. $result = $this->Form->getSourceValue('id');
  7779. $this->assertSame('10', $result);
  7780. }
  7781. /**
  7782. * Test the different form values sources resetting through From::end();
  7783. */
  7784. public function testFormValueSourcesResetViaEnd(): void
  7785. {
  7786. $expected = ['data', 'context'];
  7787. $result = $this->Form->getValueSources();
  7788. $this->assertEquals($expected, $result);
  7789. $expected = ['query', 'context', 'data'];
  7790. $this->Form->setValueSources(['query', 'context', 'data']);
  7791. $result = $this->Form->getValueSources();
  7792. $this->assertEquals($expected, $result);
  7793. $this->Form->create();
  7794. $result = $this->Form->getValueSources();
  7795. $this->assertEquals($expected, $result);
  7796. $this->Form->end();
  7797. $result = $this->Form->getValueSources();
  7798. $this->assertEquals(['data', 'context'], $result);
  7799. }
  7800. /**
  7801. * Test sources values defaults handling
  7802. */
  7803. public function testFormValueSourcesDefaults(): void
  7804. {
  7805. $this->View->setRequest(
  7806. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7807. );
  7808. $this->Form->create();
  7809. $result = $this->Form->password('password');
  7810. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7811. $this->assertHtml($expected, $result);
  7812. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7813. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7814. $this->assertHtml($expected, $result);
  7815. $this->Form->setValueSources('query');
  7816. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7817. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7818. $this->assertHtml($expected, $result);
  7819. $this->Form->setValueSources('data');
  7820. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7821. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7822. $this->assertHtml($expected, $result);
  7823. }
  7824. /**
  7825. * Test sources values schema defaults handling
  7826. */
  7827. public function testSourcesValueDoesntExistPassThrough(): void
  7828. {
  7829. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7830. $articles = $this->getTableLocator()->get('Articles');
  7831. $entity = $articles->newEmptyEntity();
  7832. $this->Form->create($entity);
  7833. $this->Form->setValueSources(['query', 'context']);
  7834. $result = $this->Form->getSourceValue('category');
  7835. $this->assertSame('sesame-cookies', $result);
  7836. $this->Form->setValueSources(['context', 'query']);
  7837. $result = $this->Form->getSourceValue('category');
  7838. $this->assertSame('sesame-cookies', $result);
  7839. }
  7840. /**
  7841. * testNestedLabelInput method
  7842. *
  7843. * Test the `nestedInput` parameter
  7844. */
  7845. public function testNestedLabelInput(): void
  7846. {
  7847. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7848. $expected = [
  7849. 'div' => ['class' => 'input text'],
  7850. 'label' => ['for' => 'foo'],
  7851. ['input' => [
  7852. 'type' => 'text',
  7853. 'name' => 'foo',
  7854. 'id' => 'foo',
  7855. ]],
  7856. 'Foo',
  7857. '/label',
  7858. '/div',
  7859. ];
  7860. $this->assertHtml($expected, $result);
  7861. }
  7862. /**
  7863. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7864. *
  7865. * This test makes sure `false` excludes the label from the render
  7866. */
  7867. public function testControlLabelManipulationDisableLabels(): void
  7868. {
  7869. $result = $this->Form->control('test', [
  7870. 'type' => 'radio',
  7871. 'options' => ['A', 'B'],
  7872. 'labelOptions' => false,
  7873. ]);
  7874. $expected = [
  7875. ['div' => ['class' => 'input radio']],
  7876. '<label',
  7877. 'Test',
  7878. '/label',
  7879. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7880. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7881. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7882. '/div',
  7883. ];
  7884. $this->assertHtml($expected, $result);
  7885. $result = $this->Form->control('checkbox1', [
  7886. 'label' => 'My checkboxes',
  7887. 'multiple' => 'checkbox',
  7888. 'type' => 'select',
  7889. 'options' => [
  7890. ['text' => 'First Checkbox', 'value' => 1],
  7891. ['text' => 'Second Checkbox', 'value' => 2],
  7892. ],
  7893. 'labelOptions' => false,
  7894. ]);
  7895. $expected = [
  7896. ['div' => ['class' => 'input select']],
  7897. ['label' => ['for' => 'checkbox1']],
  7898. 'My checkboxes',
  7899. '/label',
  7900. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7901. ['div' => ['class' => 'checkbox']],
  7902. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7903. '/div',
  7904. ['div' => ['class' => 'checkbox']],
  7905. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7906. '/div',
  7907. '/div',
  7908. ];
  7909. $this->assertHtml($expected, $result);
  7910. }
  7911. /**
  7912. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7913. *
  7914. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7915. * added to the class if checked.
  7916. *
  7917. * Also checks to make sure any custom attributes are rendered correctly
  7918. */
  7919. public function testControlLabelManipulationRadios(): void
  7920. {
  7921. $result = $this->Form->control('test', [
  7922. 'type' => 'radio',
  7923. 'options' => ['A', 'B'],
  7924. 'labelOptions' => ['class' => 'custom-class'],
  7925. ]);
  7926. $expected = [
  7927. ['div' => ['class' => 'input radio']],
  7928. '<label',
  7929. 'Test',
  7930. '/label',
  7931. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7932. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7933. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7934. 'A',
  7935. '/label',
  7936. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7937. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7938. 'B',
  7939. '/label',
  7940. '/div',
  7941. ];
  7942. $this->assertHtml($expected, $result);
  7943. $result = $this->Form->control('test', [
  7944. 'type' => 'radio',
  7945. 'options' => ['A', 'B'],
  7946. 'value' => 1,
  7947. 'labelOptions' => ['class' => 'custom-class'],
  7948. ]);
  7949. $expected = [
  7950. ['div' => ['class' => 'input radio']],
  7951. '<label',
  7952. 'Test',
  7953. '/label',
  7954. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7955. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7956. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7957. 'A',
  7958. '/label',
  7959. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7960. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7961. 'B',
  7962. '/label',
  7963. '/div',
  7964. ];
  7965. $this->assertHtml($expected, $result);
  7966. $result = $this->Form->control('test', [
  7967. 'type' => 'radio',
  7968. 'options' => ['A', 'B'],
  7969. 'value' => 1,
  7970. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7971. ]);
  7972. $expected = [
  7973. ['div' => ['class' => 'input radio']],
  7974. '<label',
  7975. 'Test',
  7976. '/label',
  7977. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7978. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7979. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7980. 'A',
  7981. '/label',
  7982. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7983. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7984. 'B',
  7985. '/label',
  7986. '/div',
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. $result = $this->Form->radio('test', ['A', 'B'], [
  7990. 'label' => [
  7991. 'class' => ['custom-class', 'another-class'],
  7992. 'data-name' => 'bob',
  7993. ],
  7994. 'value' => 1,
  7995. ]);
  7996. $expected = [
  7997. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7998. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7999. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8000. 'A',
  8001. '/label',
  8002. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8003. ['input' => [
  8004. 'type' => 'radio',
  8005. 'name' => 'test',
  8006. 'value' => '1',
  8007. 'id' => 'test-1',
  8008. 'checked' => 'checked',
  8009. ]],
  8010. 'B',
  8011. '/label',
  8012. ];
  8013. $this->assertHtml($expected, $result);
  8014. }
  8015. /**
  8016. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8017. *
  8018. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8019. * added to the class if checked.
  8020. *
  8021. * Also checks to make sure any custom attributes are rendered correctly
  8022. */
  8023. public function testControlLabelManipulationCheckboxes(): void
  8024. {
  8025. $result = $this->Form->control('checkbox1', [
  8026. 'label' => 'My checkboxes',
  8027. 'multiple' => 'checkbox',
  8028. 'type' => 'select',
  8029. 'options' => [
  8030. ['text' => 'First Checkbox', 'value' => 1],
  8031. ['text' => 'Second Checkbox', 'value' => 2],
  8032. ],
  8033. 'labelOptions' => ['class' => 'custom-class'],
  8034. 'value' => ['1'],
  8035. ]);
  8036. $expected = [
  8037. ['div' => ['class' => 'input select']],
  8038. ['label' => ['for' => 'checkbox1']],
  8039. 'My checkboxes',
  8040. '/label',
  8041. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8042. ['div' => ['class' => 'checkbox']],
  8043. ['label' => [
  8044. 'class' => 'custom-class selected',
  8045. 'for' => 'checkbox1-1',
  8046. ]],
  8047. ['input' => [
  8048. 'type' => 'checkbox',
  8049. 'name' => 'checkbox1[]',
  8050. 'value' => '1',
  8051. 'id' => 'checkbox1-1',
  8052. 'checked' => 'checked',
  8053. ]],
  8054. 'First Checkbox',
  8055. '/label',
  8056. '/div',
  8057. ['div' => ['class' => 'checkbox']],
  8058. ['label' => [
  8059. 'class' => 'custom-class',
  8060. 'for' => 'checkbox1-2',
  8061. ]],
  8062. ['input' => [
  8063. 'type' => 'checkbox',
  8064. 'name' => 'checkbox1[]',
  8065. 'value' => '2',
  8066. 'id' => 'checkbox1-2',
  8067. ]],
  8068. 'Second Checkbox',
  8069. '/label',
  8070. '/div',
  8071. '/div',
  8072. ];
  8073. $this->assertHtml($expected, $result);
  8074. $result = $this->Form->control('checkbox1', [
  8075. 'label' => 'My checkboxes',
  8076. 'multiple' => 'checkbox',
  8077. 'type' => 'select',
  8078. 'options' => [
  8079. ['text' => 'First Checkbox', 'value' => 1],
  8080. ['text' => 'Second Checkbox', 'value' => 2],
  8081. ],
  8082. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8083. 'value' => ['1'],
  8084. ]);
  8085. $expected = [
  8086. ['div' => ['class' => 'input select']],
  8087. ['label' => ['for' => 'checkbox1']],
  8088. 'My checkboxes',
  8089. '/label',
  8090. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8091. ['div' => ['class' => 'checkbox']],
  8092. ['label' => [
  8093. 'class' => 'custom-class another-class selected',
  8094. 'data-name' => 'bob',
  8095. 'for' => 'checkbox1-1',
  8096. ]],
  8097. ['input' => [
  8098. 'type' => 'checkbox',
  8099. 'name' => 'checkbox1[]',
  8100. 'value' => '1',
  8101. 'id' => 'checkbox1-1',
  8102. 'checked' => 'checked',
  8103. ]],
  8104. 'First Checkbox',
  8105. '/label',
  8106. '/div',
  8107. ['div' => ['class' => 'checkbox']],
  8108. ['label' => [
  8109. 'class' => 'custom-class another-class',
  8110. 'data-name' => 'bob',
  8111. 'for' => 'checkbox1-2',
  8112. ]],
  8113. ['input' => [
  8114. 'type' => 'checkbox',
  8115. 'name' => 'checkbox1[]',
  8116. 'value' => '2',
  8117. 'id' => 'checkbox1-2',
  8118. ]],
  8119. 'Second Checkbox',
  8120. '/label',
  8121. '/div',
  8122. '/div',
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. }
  8126. /**
  8127. * testControlMaxLengthArrayContext method
  8128. *
  8129. * Test control() with maxlength attribute in Array Context.
  8130. */
  8131. public function testControlMaxLengthArrayContext(): void
  8132. {
  8133. $this->article['schema'] = [
  8134. 'title' => ['length' => 10],
  8135. ];
  8136. $this->Form->create($this->article);
  8137. $result = $this->Form->control('title');
  8138. $expected = [
  8139. 'div' => ['class'],
  8140. 'label' => ['for'],
  8141. 'Title',
  8142. '/label',
  8143. 'input' => [
  8144. 'id',
  8145. 'name' => 'title',
  8146. 'type' => 'text',
  8147. 'required' => 'required',
  8148. 'maxlength' => 10,
  8149. 'data-validity-message' => 'This field cannot be left empty',
  8150. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8151. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8152. ],
  8153. '/div',
  8154. ];
  8155. $this->assertHtml($expected, $result);
  8156. }
  8157. /**
  8158. * testControlMaxLengthEntityContext method
  8159. *
  8160. * Test control() with maxlength attribute in Entity Context.
  8161. */
  8162. public function testControlMaxLengthEntityContext(): void
  8163. {
  8164. $this->article['schema']['title']['length'] = 45;
  8165. $validator = new Validator();
  8166. $validator->maxLength('title', 10);
  8167. $article = new EntityContext(
  8168. [
  8169. 'entity' => new Entity($this->article),
  8170. 'table' => new Table([
  8171. 'alias' => 'Articles',
  8172. 'schema' => $this->article['schema'],
  8173. 'validator' => $validator,
  8174. ]),
  8175. ]
  8176. );
  8177. $this->Form->create($article);
  8178. $result = $this->Form->control('title');
  8179. $expected = [
  8180. 'div' => ['class'],
  8181. 'label' => ['for'],
  8182. 'Title',
  8183. '/label',
  8184. 'input' => [
  8185. 'id',
  8186. 'name' => 'title',
  8187. 'type' => 'text',
  8188. 'required' => 'required',
  8189. 'maxlength' => 10,
  8190. 'data-validity-message' => 'This field cannot be left empty',
  8191. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8192. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8193. ],
  8194. '/div',
  8195. ];
  8196. $this->assertHtml($expected, $result);
  8197. $this->article['schema']['title']['length'] = 45;
  8198. $validator = new Validator();
  8199. $validator->maxLength('title', 55);
  8200. $article = new EntityContext(
  8201. [
  8202. 'entity' => new Entity($this->article),
  8203. 'table' => new Table([
  8204. 'schema' => $this->article['schema'],
  8205. 'validator' => $validator,
  8206. 'alias' => 'Articles',
  8207. ]),
  8208. ]
  8209. );
  8210. $this->Form->create($article);
  8211. $result = $this->Form->control('title');
  8212. $expected = [
  8213. 'div' => ['class'],
  8214. 'label' => ['for'],
  8215. 'Title',
  8216. '/label',
  8217. 'input' => [
  8218. 'id',
  8219. 'name' => 'title',
  8220. 'type' => 'text',
  8221. 'required' => 'required',
  8222. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8223. 'data-validity-message' => 'This field cannot be left empty',
  8224. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8225. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8226. ],
  8227. '/div',
  8228. ];
  8229. $this->assertHtml($expected, $result);
  8230. $this->article['schema']['title']['length'] = 45;
  8231. $validator = new Validator();
  8232. $validator->maxLength('title', 55);
  8233. $article = new EntityContext(
  8234. [
  8235. 'entity' => new Entity($this->article),
  8236. 'table' => new Table([
  8237. 'schema' => $this->article['schema'],
  8238. 'validator' => $validator,
  8239. 'alias' => 'Articles',
  8240. ]),
  8241. ]
  8242. );
  8243. $this->Form->create($article);
  8244. $result = $this->Form->control('title', ['maxlength' => 10]);
  8245. $expected = [
  8246. 'div' => ['class'],
  8247. 'label' => ['for'],
  8248. 'Title',
  8249. '/label',
  8250. 'input' => [
  8251. 'id',
  8252. 'name' => 'title',
  8253. 'type' => 'text',
  8254. 'required' => 'required',
  8255. 'maxlength' => 10, // Length set in options should take highest precedence.
  8256. 'data-validity-message' => 'This field cannot be left empty',
  8257. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8258. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8259. ],
  8260. '/div',
  8261. ];
  8262. $this->assertHtml($expected, $result);
  8263. }
  8264. /**
  8265. * testControlMinMaxLengthEntityContext method
  8266. *
  8267. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8268. */
  8269. public function testControlMinMaxLengthEntityContext(): void
  8270. {
  8271. $validator = new Validator();
  8272. $validator->maxLength('title', 10);
  8273. $article = new EntityContext(
  8274. [
  8275. 'entity' => new Entity($this->article),
  8276. 'table' => new Table([
  8277. 'alias' => 'Articles',
  8278. 'schema' => $this->article['schema'],
  8279. 'validator' => $validator,
  8280. ]),
  8281. ]
  8282. );
  8283. $this->Form->create($article);
  8284. $result = $this->Form->control('title');
  8285. $expected = [
  8286. 'div' => ['class'],
  8287. 'label' => ['for'],
  8288. 'Title',
  8289. '/label',
  8290. 'input' => [
  8291. 'id',
  8292. 'name' => 'title',
  8293. 'type' => 'text',
  8294. 'required' => 'required',
  8295. 'maxlength' => 10,
  8296. 'data-validity-message' => 'This field cannot be left empty',
  8297. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8298. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8299. ],
  8300. '/div',
  8301. ];
  8302. $this->assertHtml($expected, $result);
  8303. }
  8304. /**
  8305. * testControlMaxLengthFormContext method
  8306. *
  8307. * Test control() with maxlength attribute in Form Context.
  8308. */
  8309. public function testControlMaxLengthFormContext(): void
  8310. {
  8311. $validator = new Validator();
  8312. $validator->maxLength('title', 10);
  8313. $form = new Form();
  8314. $form->setValidator('default', $validator);
  8315. $this->Form->create($form);
  8316. $result = $this->Form->control('title');
  8317. $expected = [
  8318. 'div' => ['class'],
  8319. 'label' => ['for'],
  8320. 'Title',
  8321. '/label',
  8322. 'input' => [
  8323. 'id',
  8324. 'name' => 'title',
  8325. 'type' => 'text',
  8326. 'required' => 'required',
  8327. 'maxlength' => 10,
  8328. 'data-validity-message' => 'This field cannot be left empty',
  8329. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8330. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8331. ],
  8332. '/div',
  8333. ];
  8334. $this->assertHtml($expected, $result);
  8335. }
  8336. }